blob: ac2436538b7e8447483769b4d5fb312de48c33f8 [file] [log] [blame]
Dan Williamsb94d5232015-05-19 22:54:31 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/list_sort.h>
14#include <linux/libnvdimm.h>
15#include <linux/module.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040016#include <linux/mutex.h>
Dan Williams62232e452015-06-08 14:27:06 -040017#include <linux/ndctl.h>
Vishal Verma37b137f2016-07-23 21:51:42 -070018#include <linux/sysfs.h>
Vishal Verma0caeef62015-12-24 19:21:43 -070019#include <linux/delay.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040020#include <linux/list.h>
21#include <linux/acpi.h>
Dan Williamseaf96152015-05-01 13:11:27 -040022#include <linux/sort.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040023#include <linux/io.h>
Dan Williams1cf03c02016-02-17 13:01:23 -080024#include <linux/nd.h>
Dan Williams96601ad2015-08-24 18:29:38 -040025#include <asm/cacheflush.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040026#include "nfit.h"
27
Ross Zwisler047fc8a2015-06-25 04:21:02 -040028/*
29 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
30 * irrelevant.
31 */
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020032#include <linux/io-64-nonatomic-hi-lo.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040033
Dan Williams4d88a972015-05-31 14:41:48 -040034static bool force_enable_dimms;
35module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
36MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
37
Dan Williams1cf03c02016-02-17 13:01:23 -080038static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
39module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
40MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
41
42/* after three payloads of overflow, it's dead jim */
43static unsigned int scrub_overflow_abort = 3;
44module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
45MODULE_PARM_DESC(scrub_overflow_abort,
46 "Number of times we overflow ARS results before abort");
47
Dan Williams87554092016-04-28 18:01:20 -070048static bool disable_vendor_specific;
49module_param(disable_vendor_specific, bool, S_IRUGO);
50MODULE_PARM_DESC(disable_vendor_specific,
Linda Knippersf2668fa2017-03-07 16:35:14 -050051 "Limit commands to the publicly specified set");
Dan Williams87554092016-04-28 18:01:20 -070052
Linda Knippers095ab4b2017-03-07 16:35:12 -050053static unsigned long override_dsm_mask;
54module_param(override_dsm_mask, ulong, S_IRUGO);
55MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
56
Linda Knippersba650cf2017-03-07 16:35:13 -050057static int default_dsm_family = -1;
58module_param(default_dsm_family, int, S_IRUGO);
59MODULE_PARM_DESC(default_dsm_family,
60 "Try this DSM type first when identifying NVDIMM family");
61
Vishal Verma6839a6d2016-07-23 21:51:21 -070062LIST_HEAD(acpi_descs);
63DEFINE_MUTEX(acpi_desc_lock);
64
Dan Williams7ae0fa432016-02-19 12:16:34 -080065static struct workqueue_struct *nfit_wq;
66
Vishal Verma20985162015-10-27 16:58:27 -060067struct nfit_table_prev {
68 struct list_head spas;
69 struct list_head memdevs;
70 struct list_head dcrs;
71 struct list_head bdws;
72 struct list_head idts;
73 struct list_head flushes;
74};
75
Dan Williamsb94d5232015-05-19 22:54:31 -040076static u8 nfit_uuid[NFIT_UUID_MAX][16];
77
Dan Williams6bc75612015-06-17 17:23:32 -040078const u8 *to_nfit_uuid(enum nfit_uuids id)
Dan Williamsb94d5232015-05-19 22:54:31 -040079{
80 return nfit_uuid[id];
81}
Dan Williams6bc75612015-06-17 17:23:32 -040082EXPORT_SYMBOL(to_nfit_uuid);
Dan Williamsb94d5232015-05-19 22:54:31 -040083
Dan Williams62232e452015-06-08 14:27:06 -040084static struct acpi_nfit_desc *to_acpi_nfit_desc(
85 struct nvdimm_bus_descriptor *nd_desc)
86{
87 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
88}
89
90static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
91{
92 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
93
94 /*
95 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
96 * acpi_device.
97 */
98 if (!nd_desc->provider_name
99 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
100 return NULL;
101
102 return to_acpi_device(acpi_desc->dev);
103}
104
Dan Williamsd6eb2702016-12-06 15:06:55 -0800105static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
Dan Williamsaef25332016-02-12 17:01:11 -0800106{
Dan Williamsd4f32362016-03-03 16:08:54 -0800107 struct nd_cmd_clear_error *clear_err;
Dan Williamsaef25332016-02-12 17:01:11 -0800108 struct nd_cmd_ars_status *ars_status;
Dan Williamsaef25332016-02-12 17:01:11 -0800109 u16 flags;
110
111 switch (cmd) {
112 case ND_CMD_ARS_CAP:
Dan Williams11294d62016-09-21 09:21:26 -0700113 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800114 return -ENOTTY;
115
116 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700117 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800118 return -EIO;
119
120 /* No supported scan types for this range */
121 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
Dan Williams11294d62016-09-21 09:21:26 -0700122 if ((status >> 16 & flags) == 0)
Dan Williamsaef25332016-02-12 17:01:11 -0800123 return -ENOTTY;
Vishal Verma9a901f52016-12-05 17:00:37 -0700124 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800125 case ND_CMD_ARS_START:
Dan Williamsaef25332016-02-12 17:01:11 -0800126 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700127 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800128 return -EBUSY;
129
130 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700131 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800132 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700133 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800134 case ND_CMD_ARS_STATUS:
135 ars_status = buf;
136 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700137 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800138 return -EIO;
139 /* Check extended status (Upper two bytes) */
Dan Williams11294d62016-09-21 09:21:26 -0700140 if (status == NFIT_ARS_STATUS_DONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800141 return 0;
142
143 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700144 if (status == NFIT_ARS_STATUS_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800145 return -EBUSY;
146
147 /* No ARS performed for the current boot */
Dan Williams11294d62016-09-21 09:21:26 -0700148 if (status == NFIT_ARS_STATUS_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800149 return -EAGAIN;
150
151 /*
152 * ARS interrupted, either we overflowed or some other
153 * agent wants the scan to stop. If we didn't overflow
154 * then just continue with the returned results.
155 */
Dan Williams11294d62016-09-21 09:21:26 -0700156 if (status == NFIT_ARS_STATUS_INTR) {
Dan Williams82aa37c2016-12-06 12:45:24 -0800157 if (ars_status->out_length >= 40 && (ars_status->flags
158 & NFIT_ARS_F_OVERFLOW))
Dan Williamsaef25332016-02-12 17:01:11 -0800159 return -ENOSPC;
160 return 0;
161 }
162
163 /* Unknown status */
Dan Williams11294d62016-09-21 09:21:26 -0700164 if (status >> 16)
Dan Williamsaef25332016-02-12 17:01:11 -0800165 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700166 return 0;
Dan Williamsd4f32362016-03-03 16:08:54 -0800167 case ND_CMD_CLEAR_ERROR:
168 clear_err = buf;
Dan Williams11294d62016-09-21 09:21:26 -0700169 if (status & 0xffff)
Dan Williamsd4f32362016-03-03 16:08:54 -0800170 return -EIO;
171 if (!clear_err->cleared)
172 return -EIO;
173 if (clear_err->length > clear_err->cleared)
174 return clear_err->cleared;
Vishal Verma9a901f52016-12-05 17:00:37 -0700175 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800176 default:
177 break;
178 }
179
Dan Williams11294d62016-09-21 09:21:26 -0700180 /* all other non-zero status results in an error */
181 if (status)
182 return -EIO;
Dan Williamsaef25332016-02-12 17:01:11 -0800183 return 0;
184}
185
Dan Williams9d62ed92017-05-04 11:47:22 -0700186static int xlat_nvdimm_status(void *buf, unsigned int cmd, u32 status)
187{
188 switch (cmd) {
189 case ND_CMD_GET_CONFIG_SIZE:
190 if (status >> 16 & ND_CONFIG_LOCKED)
191 return -EACCES;
192 break;
193 default:
194 break;
195 }
196
197 /* all other non-zero status results in an error */
198 if (status)
199 return -EIO;
200 return 0;
201}
202
Dan Williamsd6eb2702016-12-06 15:06:55 -0800203static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
204 u32 status)
205{
206 if (!nvdimm)
207 return xlat_bus_status(buf, cmd, status);
Dan Williams9d62ed92017-05-04 11:47:22 -0700208 return xlat_nvdimm_status(buf, cmd, status);
Dan Williamsd6eb2702016-12-06 15:06:55 -0800209}
210
Dan Williamsa7de92d2016-12-05 13:43:25 -0800211int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
212 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400213{
Dan Williams62232e452015-06-08 14:27:06 -0400214 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams62232e452015-06-08 14:27:06 -0400215 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700216 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400217 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700218 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400219 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700220 unsigned long cmd_mask, dsm_mask;
Dan Williams11294d62016-09-21 09:21:26 -0700221 u32 offset, fw_status = 0;
Dan Williams62232e452015-06-08 14:27:06 -0400222 acpi_handle handle;
Dan Williams31eca762016-04-28 16:23:43 -0700223 unsigned int func;
Dan Williams62232e452015-06-08 14:27:06 -0400224 const u8 *uuid;
Dan Williams62232e452015-06-08 14:27:06 -0400225 int rc, i;
226
Dan Williams31eca762016-04-28 16:23:43 -0700227 func = cmd;
228 if (cmd == ND_CMD_CALL) {
229 call_pkg = buf;
230 func = call_pkg->nd_command;
231 }
232
Dan Williams62232e452015-06-08 14:27:06 -0400233 if (nvdimm) {
234 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
235 struct acpi_device *adev = nfit_mem->adev;
236
237 if (!adev)
238 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700239 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
240 return -ENOTTY;
241
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400242 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400243 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700244 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400245 dsm_mask = nfit_mem->dsm_mask;
246 desc = nd_cmd_dimm_desc(cmd);
Dan Williams31eca762016-04-28 16:23:43 -0700247 uuid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400248 handle = adev->handle;
249 } else {
250 struct acpi_device *adev = to_acpi_dev(acpi_desc);
251
252 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700253 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700254 dsm_mask = cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400255 desc = nd_cmd_bus_desc(cmd);
256 uuid = to_nfit_uuid(NFIT_DEV_BUS);
257 handle = adev->handle;
258 dimm_name = "bus";
259 }
260
261 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
262 return -ENOTTY;
263
Dan Williams31eca762016-04-28 16:23:43 -0700264 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400265 return -ENOTTY;
266
267 in_obj.type = ACPI_TYPE_PACKAGE;
268 in_obj.package.count = 1;
269 in_obj.package.elements = &in_buf;
270 in_buf.type = ACPI_TYPE_BUFFER;
271 in_buf.buffer.pointer = buf;
272 in_buf.buffer.length = 0;
273
274 /* libnvdimm has already validated the input envelope */
275 for (i = 0; i < desc->in_num; i++)
276 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
277 i, buf);
278
Dan Williams31eca762016-04-28 16:23:43 -0700279 if (call_pkg) {
280 /* skip over package wrapper */
281 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
282 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400283 }
284
Dan Williams7699a6a2017-04-28 13:54:30 -0700285 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
286 __func__, dimm_name, cmd, func, in_buf.buffer.length);
287 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
Dan Williams31eca762016-04-28 16:23:43 -0700288 in_buf.buffer.pointer,
289 min_t(u32, 256, in_buf.buffer.length), true);
Dan Williams31eca762016-04-28 16:23:43 -0700290
291 out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj);
Dan Williams62232e452015-06-08 14:27:06 -0400292 if (!out_obj) {
293 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
294 cmd_name);
295 return -EINVAL;
296 }
297
Dan Williams31eca762016-04-28 16:23:43 -0700298 if (call_pkg) {
299 call_pkg->nd_fw_size = out_obj->buffer.length;
300 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
301 out_obj->buffer.pointer,
302 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
303
304 ACPI_FREE(out_obj);
305 /*
306 * Need to support FW function w/o known size in advance.
307 * Caller can determine required size based upon nd_fw_size.
308 * If we return an error (like elsewhere) then caller wouldn't
309 * be able to rely upon data returned to make calculation.
310 */
311 return 0;
312 }
313
Dan Williams62232e452015-06-08 14:27:06 -0400314 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
315 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
316 __func__, dimm_name, cmd_name, out_obj->type);
317 rc = -EINVAL;
318 goto out;
319 }
320
Dan Williams7699a6a2017-04-28 13:54:30 -0700321 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, dimm_name,
322 cmd_name, out_obj->buffer.length);
323 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
324 out_obj->buffer.pointer,
325 min_t(u32, 128, out_obj->buffer.length), true);
Dan Williams62232e452015-06-08 14:27:06 -0400326
327 for (i = 0, offset = 0; i < desc->out_num; i++) {
328 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800329 (u32 *) out_obj->buffer.pointer,
330 out_obj->buffer.length - offset);
Dan Williams62232e452015-06-08 14:27:06 -0400331
332 if (offset + out_size > out_obj->buffer.length) {
333 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
334 __func__, dimm_name, cmd_name, i);
335 break;
336 }
337
338 if (in_buf.buffer.length + offset + out_size > buf_len) {
339 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
340 __func__, dimm_name, cmd_name, i);
341 rc = -ENXIO;
342 goto out;
343 }
344 memcpy(buf + in_buf.buffer.length + offset,
345 out_obj->buffer.pointer + offset, out_size);
346 offset += out_size;
347 }
Dan Williams11294d62016-09-21 09:21:26 -0700348
349 /*
350 * Set fw_status for all the commands with a known format to be
351 * later interpreted by xlat_status().
352 */
353 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
354 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
355 fw_status = *(u32 *) out_obj->buffer.pointer;
356
Dan Williams62232e452015-06-08 14:27:06 -0400357 if (offset + in_buf.buffer.length < buf_len) {
358 if (i >= 1) {
359 /*
360 * status valid, return the number of bytes left
361 * unfilled in the output buffer
362 */
363 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800364 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800365 *cmd_rc = xlat_status(nvdimm, buf, cmd,
366 fw_status);
Dan Williams62232e452015-06-08 14:27:06 -0400367 } else {
368 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
369 __func__, dimm_name, cmd_name, buf_len,
370 offset);
371 rc = -ENXIO;
372 }
Dan Williams2eea6582016-05-02 09:11:53 -0700373 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400374 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700375 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800376 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
Dan Williams2eea6582016-05-02 09:11:53 -0700377 }
Dan Williams62232e452015-06-08 14:27:06 -0400378
379 out:
380 ACPI_FREE(out_obj);
381
382 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400383}
Dan Williamsa7de92d2016-12-05 13:43:25 -0800384EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
Dan Williamsb94d5232015-05-19 22:54:31 -0400385
386static const char *spa_type_name(u16 type)
387{
388 static const char *to_name[] = {
389 [NFIT_SPA_VOLATILE] = "volatile",
390 [NFIT_SPA_PM] = "pmem",
391 [NFIT_SPA_DCR] = "dimm-control-region",
392 [NFIT_SPA_BDW] = "block-data-window",
393 [NFIT_SPA_VDISK] = "volatile-disk",
394 [NFIT_SPA_VCD] = "volatile-cd",
395 [NFIT_SPA_PDISK] = "persistent-disk",
396 [NFIT_SPA_PCD] = "persistent-cd",
397
398 };
399
400 if (type > NFIT_SPA_PCD)
401 return "unknown";
402
403 return to_name[type];
404}
405
Vishal Verma6839a6d2016-07-23 21:51:21 -0700406int nfit_spa_type(struct acpi_nfit_system_address *spa)
Dan Williamsb94d5232015-05-19 22:54:31 -0400407{
408 int i;
409
410 for (i = 0; i < NFIT_UUID_MAX; i++)
411 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
412 return i;
413 return -1;
414}
415
416static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600417 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400418 struct acpi_nfit_system_address *spa)
419{
420 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600421 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400422
Dan Williams31932042016-07-14 17:22:48 -0700423 if (spa->header.length != sizeof(*spa))
424 return false;
425
Vishal Verma20985162015-10-27 16:58:27 -0600426 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700427 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600428 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
429 return true;
430 }
431 }
432
Dan Williams31932042016-07-14 17:22:48 -0700433 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
434 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400435 if (!nfit_spa)
436 return false;
437 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700438 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400439 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
440 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
441 spa->range_index,
442 spa_type_name(nfit_spa_type(spa)));
443 return true;
444}
445
446static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600447 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400448 struct acpi_nfit_memory_map *memdev)
449{
450 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600451 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400452
Dan Williams31932042016-07-14 17:22:48 -0700453 if (memdev->header.length != sizeof(*memdev))
454 return false;
455
Vishal Verma20985162015-10-27 16:58:27 -0600456 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700457 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600458 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
459 return true;
460 }
461
Dan Williams31932042016-07-14 17:22:48 -0700462 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
463 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400464 if (!nfit_memdev)
465 return false;
466 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700467 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400468 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
Dan Williamscaa603a2017-04-14 10:27:11 -0700469 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
Dan Williamsb94d5232015-05-19 22:54:31 -0400470 __func__, memdev->device_handle, memdev->range_index,
Dan Williamscaa603a2017-04-14 10:27:11 -0700471 memdev->region_index, memdev->flags);
Dan Williamsb94d5232015-05-19 22:54:31 -0400472 return true;
473}
474
Dan Williams31932042016-07-14 17:22:48 -0700475/*
476 * An implementation may provide a truncated control region if no block windows
477 * are defined.
478 */
479static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
480{
481 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
482 window_size))
483 return 0;
484 if (dcr->windows)
485 return sizeof(*dcr);
486 return offsetof(struct acpi_nfit_control_region, window_size);
487}
488
Dan Williamsb94d5232015-05-19 22:54:31 -0400489static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600490 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400491 struct acpi_nfit_control_region *dcr)
492{
493 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600494 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400495
Dan Williams31932042016-07-14 17:22:48 -0700496 if (!sizeof_dcr(dcr))
497 return false;
498
Vishal Verma20985162015-10-27 16:58:27 -0600499 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700500 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600501 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
502 return true;
503 }
504
Dan Williams31932042016-07-14 17:22:48 -0700505 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
506 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400507 if (!nfit_dcr)
508 return false;
509 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700510 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400511 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
512 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
513 dcr->region_index, dcr->windows);
514 return true;
515}
516
517static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600518 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400519 struct acpi_nfit_data_region *bdw)
520{
521 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600522 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400523
Dan Williams31932042016-07-14 17:22:48 -0700524 if (bdw->header.length != sizeof(*bdw))
525 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600526 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700527 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600528 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
529 return true;
530 }
531
Dan Williams31932042016-07-14 17:22:48 -0700532 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
533 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400534 if (!nfit_bdw)
535 return false;
536 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700537 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400538 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
539 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
540 bdw->region_index, bdw->windows);
541 return true;
542}
543
Dan Williams31932042016-07-14 17:22:48 -0700544static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
545{
546 if (idt->header.length < sizeof(*idt))
547 return 0;
548 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
549}
550
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400551static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600552 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400553 struct acpi_nfit_interleave *idt)
554{
555 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600556 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400557
Dan Williams31932042016-07-14 17:22:48 -0700558 if (!sizeof_idt(idt))
559 return false;
560
561 list_for_each_entry(nfit_idt, &prev->idts, list) {
562 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
563 continue;
564
565 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600566 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
567 return true;
568 }
Dan Williams31932042016-07-14 17:22:48 -0700569 }
Vishal Verma20985162015-10-27 16:58:27 -0600570
Dan Williams31932042016-07-14 17:22:48 -0700571 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
572 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400573 if (!nfit_idt)
574 return false;
575 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700576 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400577 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
578 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
579 idt->interleave_index, idt->line_count);
580 return true;
581}
582
Dan Williams31932042016-07-14 17:22:48 -0700583static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
584{
585 if (flush->header.length < sizeof(*flush))
586 return 0;
587 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
588}
589
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600590static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600591 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600592 struct acpi_nfit_flush_address *flush)
593{
594 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600595 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600596
Dan Williams31932042016-07-14 17:22:48 -0700597 if (!sizeof_flush(flush))
598 return false;
599
600 list_for_each_entry(nfit_flush, &prev->flushes, list) {
601 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
602 continue;
603
604 if (memcmp(nfit_flush->flush, flush,
605 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600606 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
607 return true;
608 }
Dan Williams31932042016-07-14 17:22:48 -0700609 }
Vishal Verma20985162015-10-27 16:58:27 -0600610
Dan Williams31932042016-07-14 17:22:48 -0700611 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
612 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600613 if (!nfit_flush)
614 return false;
615 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700616 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600617 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
618 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
619 flush->device_handle, flush->hint_count);
620 return true;
621}
622
Vishal Verma20985162015-10-27 16:58:27 -0600623static void *add_table(struct acpi_nfit_desc *acpi_desc,
624 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400625{
626 struct device *dev = acpi_desc->dev;
627 struct acpi_nfit_header *hdr;
628 void *err = ERR_PTR(-ENOMEM);
629
630 if (table >= end)
631 return NULL;
632
633 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600634 if (!hdr->length) {
635 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
636 hdr->type);
637 return NULL;
638 }
639
Dan Williamsb94d5232015-05-19 22:54:31 -0400640 switch (hdr->type) {
641 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600642 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400643 return err;
644 break;
645 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600646 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400647 return err;
648 break;
649 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600650 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400651 return err;
652 break;
653 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600654 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400655 return err;
656 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400657 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600658 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400659 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400660 break;
661 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600662 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600663 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400664 break;
665 case ACPI_NFIT_TYPE_SMBIOS:
666 dev_dbg(dev, "%s: smbios\n", __func__);
667 break;
668 default:
669 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
670 break;
671 }
672
673 return table + hdr->length;
674}
675
676static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
677 struct nfit_mem *nfit_mem)
678{
679 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
680 u16 dcr = nfit_mem->dcr->region_index;
681 struct nfit_spa *nfit_spa;
682
683 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
684 u16 range_index = nfit_spa->spa->range_index;
685 int type = nfit_spa_type(nfit_spa->spa);
686 struct nfit_memdev *nfit_memdev;
687
688 if (type != NFIT_SPA_BDW)
689 continue;
690
691 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
692 if (nfit_memdev->memdev->range_index != range_index)
693 continue;
694 if (nfit_memdev->memdev->device_handle != device_handle)
695 continue;
696 if (nfit_memdev->memdev->region_index != dcr)
697 continue;
698
699 nfit_mem->spa_bdw = nfit_spa->spa;
700 return;
701 }
702 }
703
704 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
705 nfit_mem->spa_dcr->range_index);
706 nfit_mem->bdw = NULL;
707}
708
Dan Williams6697b2c2016-02-04 16:51:00 -0800709static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400710 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
711{
712 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400713 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400714 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400715 struct nfit_idt *nfit_idt;
716 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400717
Dan Williamsb94d5232015-05-19 22:54:31 -0400718 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
719 if (nfit_bdw->bdw->region_index != dcr)
720 continue;
721 nfit_mem->bdw = nfit_bdw->bdw;
722 break;
723 }
724
725 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800726 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400727
728 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400729
730 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800731 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400732
733 range_index = nfit_mem->spa_bdw->range_index;
734 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
735 if (nfit_memdev->memdev->range_index != range_index ||
736 nfit_memdev->memdev->region_index != dcr)
737 continue;
738 nfit_mem->memdev_bdw = nfit_memdev->memdev;
739 idt_idx = nfit_memdev->memdev->interleave_index;
740 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
741 if (nfit_idt->idt->interleave_index != idt_idx)
742 continue;
743 nfit_mem->idt_bdw = nfit_idt->idt;
744 break;
745 }
746 break;
747 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400748}
749
Dan Williams14999342017-04-13 19:46:36 -0700750static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400751 struct acpi_nfit_system_address *spa)
752{
753 struct nfit_mem *nfit_mem, *found;
754 struct nfit_memdev *nfit_memdev;
Dan Williams14999342017-04-13 19:46:36 -0700755 int type = spa ? nfit_spa_type(spa) : 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400756
757 switch (type) {
758 case NFIT_SPA_DCR:
759 case NFIT_SPA_PM:
760 break;
761 default:
Dan Williams14999342017-04-13 19:46:36 -0700762 if (spa)
763 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400764 }
765
Dan Williams14999342017-04-13 19:46:36 -0700766 /*
767 * This loop runs in two modes, when a dimm is mapped the loop
768 * adds memdev associations to an existing dimm, or creates a
769 * dimm. In the unmapped dimm case this loop sweeps for memdev
770 * instances with an invalid / zero range_index and adds those
771 * dimms without spa associations.
772 */
Dan Williamsb94d5232015-05-19 22:54:31 -0400773 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700774 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800775 struct nfit_dcr *nfit_dcr;
776 u32 device_handle;
777 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400778
Dan Williams14999342017-04-13 19:46:36 -0700779 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
780 continue;
781 if (!spa && nfit_memdev->memdev->range_index)
Dan Williamsb94d5232015-05-19 22:54:31 -0400782 continue;
783 found = NULL;
784 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800785 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400786 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800787 if (__to_nfit_memdev(nfit_mem)->device_handle
788 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400789 found = nfit_mem;
790 break;
791 }
792
793 if (found)
794 nfit_mem = found;
795 else {
796 nfit_mem = devm_kzalloc(acpi_desc->dev,
797 sizeof(*nfit_mem), GFP_KERNEL);
798 if (!nfit_mem)
799 return -ENOMEM;
800 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700801 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800802 list_add(&nfit_mem->list, &acpi_desc->dimms);
803 }
804
805 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
806 if (nfit_dcr->dcr->region_index != dcr)
807 continue;
808 /*
809 * Record the control region for the dimm. For
810 * the ACPI 6.1 case, where there are separate
811 * control regions for the pmem vs blk
812 * interfaces, be sure to record the extended
813 * blk details.
814 */
815 if (!nfit_mem->dcr)
816 nfit_mem->dcr = nfit_dcr->dcr;
817 else if (nfit_mem->dcr->windows == 0
818 && nfit_dcr->dcr->windows)
819 nfit_mem->dcr = nfit_dcr->dcr;
820 break;
821 }
822
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700823 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700824 struct acpi_nfit_flush_address *flush;
825 u16 i;
826
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700827 if (nfit_flush->flush->device_handle != device_handle)
828 continue;
829 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700830 flush = nfit_flush->flush;
831 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
832 flush->hint_count
833 * sizeof(struct resource), GFP_KERNEL);
834 if (!nfit_mem->flush_wpq)
835 return -ENOMEM;
836 for (i = 0; i < flush->hint_count; i++) {
837 struct resource *res = &nfit_mem->flush_wpq[i];
838
839 res->start = flush->hint_address[i];
840 res->end = res->start + 8 - 1;
841 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700842 break;
843 }
844
Dan Williams6697b2c2016-02-04 16:51:00 -0800845 if (dcr && !nfit_mem->dcr) {
846 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
847 spa->range_index, dcr);
848 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400849 }
850
851 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400852 struct nfit_idt *nfit_idt;
853 u16 idt_idx;
854
Dan Williamsb94d5232015-05-19 22:54:31 -0400855 /* multiple dimms may share a SPA when interleaved */
856 nfit_mem->spa_dcr = spa;
857 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400858 idt_idx = nfit_memdev->memdev->interleave_index;
859 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
860 if (nfit_idt->idt->interleave_index != idt_idx)
861 continue;
862 nfit_mem->idt_dcr = nfit_idt->idt;
863 break;
864 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800865 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williams14999342017-04-13 19:46:36 -0700866 } else if (type == NFIT_SPA_PM) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400867 /*
868 * A single dimm may belong to multiple SPA-PM
869 * ranges, record at least one in addition to
870 * any SPA-DCR range.
871 */
872 nfit_mem->memdev_pmem = nfit_memdev->memdev;
Dan Williams14999342017-04-13 19:46:36 -0700873 } else
874 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400875 }
876
877 return 0;
878}
879
880static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
881{
882 struct nfit_mem *a = container_of(_a, typeof(*a), list);
883 struct nfit_mem *b = container_of(_b, typeof(*b), list);
884 u32 handleA, handleB;
885
886 handleA = __to_nfit_memdev(a)->device_handle;
887 handleB = __to_nfit_memdev(b)->device_handle;
888 if (handleA < handleB)
889 return -1;
890 else if (handleA > handleB)
891 return 1;
892 return 0;
893}
894
895static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
896{
897 struct nfit_spa *nfit_spa;
Dan Williams14999342017-04-13 19:46:36 -0700898 int rc;
899
Dan Williamsb94d5232015-05-19 22:54:31 -0400900
901 /*
902 * For each SPA-DCR or SPA-PMEM address range find its
903 * corresponding MEMDEV(s). From each MEMDEV find the
904 * corresponding DCR. Then, if we're operating on a SPA-DCR,
905 * try to find a SPA-BDW and a corresponding BDW that references
906 * the DCR. Throw it all into an nfit_mem object. Note, that
907 * BDWs are optional.
908 */
909 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
Dan Williams14999342017-04-13 19:46:36 -0700910 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400911 if (rc)
912 return rc;
913 }
914
Dan Williams14999342017-04-13 19:46:36 -0700915 /*
916 * If a DIMM has failed to be mapped into SPA there will be no
917 * SPA entries above. Find and register all the unmapped DIMMs
918 * for reporting and recovery purposes.
919 */
920 rc = __nfit_mem_init(acpi_desc, NULL);
921 if (rc)
922 return rc;
923
Dan Williamsb94d5232015-05-19 22:54:31 -0400924 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
925
926 return 0;
927}
928
Dan Williams45def222015-04-26 19:26:48 -0400929static ssize_t revision_show(struct device *dev,
930 struct device_attribute *attr, char *buf)
931{
932 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
933 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
934 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
935
Linda Knippers6b577c92015-11-20 19:05:49 -0500936 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400937}
938static DEVICE_ATTR_RO(revision);
939
Vishal Verma9ffd6352016-09-30 17:19:29 -0600940static ssize_t hw_error_scrub_show(struct device *dev,
941 struct device_attribute *attr, char *buf)
942{
943 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
944 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
945 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
946
947 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
948}
949
950/*
951 * The 'hw_error_scrub' attribute can have the following values written to it:
952 * '0': Switch to the default mode where an exception will only insert
953 * the address of the memory error into the poison and badblocks lists.
954 * '1': Enable a full scrub to happen if an exception for a memory error is
955 * received.
956 */
957static ssize_t hw_error_scrub_store(struct device *dev,
958 struct device_attribute *attr, const char *buf, size_t size)
959{
960 struct nvdimm_bus_descriptor *nd_desc;
961 ssize_t rc;
962 long val;
963
964 rc = kstrtol(buf, 0, &val);
965 if (rc)
966 return rc;
967
968 device_lock(dev);
969 nd_desc = dev_get_drvdata(dev);
970 if (nd_desc) {
971 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
972
973 switch (val) {
974 case HW_ERROR_SCRUB_ON:
975 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
976 break;
977 case HW_ERROR_SCRUB_OFF:
978 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
979 break;
980 default:
981 rc = -EINVAL;
982 break;
983 }
984 }
985 device_unlock(dev);
986 if (rc)
987 return rc;
988 return size;
989}
990static DEVICE_ATTR_RW(hw_error_scrub);
991
Vishal Verma37b137f2016-07-23 21:51:42 -0700992/*
993 * This shows the number of full Address Range Scrubs that have been
994 * completed since driver load time. Userspace can wait on this using
995 * select/poll etc. A '+' at the end indicates an ARS is in progress
996 */
997static ssize_t scrub_show(struct device *dev,
998 struct device_attribute *attr, char *buf)
999{
1000 struct nvdimm_bus_descriptor *nd_desc;
1001 ssize_t rc = -ENXIO;
1002
1003 device_lock(dev);
1004 nd_desc = dev_get_drvdata(dev);
1005 if (nd_desc) {
1006 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1007
1008 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1009 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
1010 }
1011 device_unlock(dev);
1012 return rc;
1013}
1014
Vishal Verma37b137f2016-07-23 21:51:42 -07001015static ssize_t scrub_store(struct device *dev,
1016 struct device_attribute *attr, const char *buf, size_t size)
1017{
1018 struct nvdimm_bus_descriptor *nd_desc;
1019 ssize_t rc;
1020 long val;
1021
1022 rc = kstrtol(buf, 0, &val);
1023 if (rc)
1024 return rc;
1025 if (val != 1)
1026 return -EINVAL;
1027
1028 device_lock(dev);
1029 nd_desc = dev_get_drvdata(dev);
1030 if (nd_desc) {
1031 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1032
1033 rc = acpi_nfit_ars_rescan(acpi_desc);
1034 }
1035 device_unlock(dev);
1036 if (rc)
1037 return rc;
1038 return size;
1039}
1040static DEVICE_ATTR_RW(scrub);
1041
1042static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1043{
1044 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1045 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1046 | 1 << ND_CMD_ARS_STATUS;
1047
1048 return (nd_desc->cmd_mask & mask) == mask;
1049}
1050
1051static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1052{
1053 struct device *dev = container_of(kobj, struct device, kobj);
1054 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1055
1056 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1057 return 0;
1058 return a->mode;
1059}
1060
Dan Williams45def222015-04-26 19:26:48 -04001061static struct attribute *acpi_nfit_attributes[] = {
1062 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -07001063 &dev_attr_scrub.attr,
Vishal Verma9ffd6352016-09-30 17:19:29 -06001064 &dev_attr_hw_error_scrub.attr,
Dan Williams45def222015-04-26 19:26:48 -04001065 NULL,
1066};
1067
1068static struct attribute_group acpi_nfit_attribute_group = {
1069 .name = "nfit",
1070 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -07001071 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -04001072};
1073
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001074static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -04001075 &nvdimm_bus_attribute_group,
1076 &acpi_nfit_attribute_group,
1077 NULL,
1078};
1079
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001080static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1081{
1082 struct nvdimm *nvdimm = to_nvdimm(dev);
1083 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1084
1085 return __to_nfit_memdev(nfit_mem);
1086}
1087
1088static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1089{
1090 struct nvdimm *nvdimm = to_nvdimm(dev);
1091 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1092
1093 return nfit_mem->dcr;
1094}
1095
1096static ssize_t handle_show(struct device *dev,
1097 struct device_attribute *attr, char *buf)
1098{
1099 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1100
1101 return sprintf(buf, "%#x\n", memdev->device_handle);
1102}
1103static DEVICE_ATTR_RO(handle);
1104
1105static ssize_t phys_id_show(struct device *dev,
1106 struct device_attribute *attr, char *buf)
1107{
1108 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1109
1110 return sprintf(buf, "%#x\n", memdev->physical_id);
1111}
1112static DEVICE_ATTR_RO(phys_id);
1113
1114static ssize_t vendor_show(struct device *dev,
1115 struct device_attribute *attr, char *buf)
1116{
1117 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1118
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001119 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001120}
1121static DEVICE_ATTR_RO(vendor);
1122
1123static ssize_t rev_id_show(struct device *dev,
1124 struct device_attribute *attr, char *buf)
1125{
1126 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1127
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001128 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001129}
1130static DEVICE_ATTR_RO(rev_id);
1131
1132static ssize_t device_show(struct device *dev,
1133 struct device_attribute *attr, char *buf)
1134{
1135 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1136
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001137 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001138}
1139static DEVICE_ATTR_RO(device);
1140
Dan Williams6ca72082016-04-29 10:33:23 -07001141static ssize_t subsystem_vendor_show(struct device *dev,
1142 struct device_attribute *attr, char *buf)
1143{
1144 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1145
1146 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1147}
1148static DEVICE_ATTR_RO(subsystem_vendor);
1149
1150static ssize_t subsystem_rev_id_show(struct device *dev,
1151 struct device_attribute *attr, char *buf)
1152{
1153 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1154
1155 return sprintf(buf, "0x%04x\n",
1156 be16_to_cpu(dcr->subsystem_revision_id));
1157}
1158static DEVICE_ATTR_RO(subsystem_rev_id);
1159
1160static ssize_t subsystem_device_show(struct device *dev,
1161 struct device_attribute *attr, char *buf)
1162{
1163 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1164
1165 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1166}
1167static DEVICE_ATTR_RO(subsystem_device);
1168
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001169static int num_nvdimm_formats(struct nvdimm *nvdimm)
1170{
1171 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1172 int formats = 0;
1173
1174 if (nfit_mem->memdev_pmem)
1175 formats++;
1176 if (nfit_mem->memdev_bdw)
1177 formats++;
1178 return formats;
1179}
1180
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001181static ssize_t format_show(struct device *dev,
1182 struct device_attribute *attr, char *buf)
1183{
1184 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1185
Dan Williams1bcbf422016-06-29 11:19:32 -07001186 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001187}
1188static DEVICE_ATTR_RO(format);
1189
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001190static ssize_t format1_show(struct device *dev,
1191 struct device_attribute *attr, char *buf)
1192{
1193 u32 handle;
1194 ssize_t rc = -ENXIO;
1195 struct nfit_mem *nfit_mem;
1196 struct nfit_memdev *nfit_memdev;
1197 struct acpi_nfit_desc *acpi_desc;
1198 struct nvdimm *nvdimm = to_nvdimm(dev);
1199 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1200
1201 nfit_mem = nvdimm_provider_data(nvdimm);
1202 acpi_desc = nfit_mem->acpi_desc;
1203 handle = to_nfit_memdev(dev)->device_handle;
1204
1205 /* assumes DIMMs have at most 2 published interface codes */
1206 mutex_lock(&acpi_desc->init_mutex);
1207 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1208 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1209 struct nfit_dcr *nfit_dcr;
1210
1211 if (memdev->device_handle != handle)
1212 continue;
1213
1214 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1215 if (nfit_dcr->dcr->region_index != memdev->region_index)
1216 continue;
1217 if (nfit_dcr->dcr->code == dcr->code)
1218 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001219 rc = sprintf(buf, "0x%04x\n",
1220 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001221 break;
1222 }
1223 if (rc != ENXIO)
1224 break;
1225 }
1226 mutex_unlock(&acpi_desc->init_mutex);
1227 return rc;
1228}
1229static DEVICE_ATTR_RO(format1);
1230
1231static ssize_t formats_show(struct device *dev,
1232 struct device_attribute *attr, char *buf)
1233{
1234 struct nvdimm *nvdimm = to_nvdimm(dev);
1235
1236 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1237}
1238static DEVICE_ATTR_RO(formats);
1239
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001240static ssize_t serial_show(struct device *dev,
1241 struct device_attribute *attr, char *buf)
1242{
1243 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1244
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001245 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001246}
1247static DEVICE_ATTR_RO(serial);
1248
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001249static ssize_t family_show(struct device *dev,
1250 struct device_attribute *attr, char *buf)
1251{
1252 struct nvdimm *nvdimm = to_nvdimm(dev);
1253 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1254
1255 if (nfit_mem->family < 0)
1256 return -ENXIO;
1257 return sprintf(buf, "%d\n", nfit_mem->family);
1258}
1259static DEVICE_ATTR_RO(family);
1260
1261static ssize_t dsm_mask_show(struct device *dev,
1262 struct device_attribute *attr, char *buf)
1263{
1264 struct nvdimm *nvdimm = to_nvdimm(dev);
1265 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1266
1267 if (nfit_mem->family < 0)
1268 return -ENXIO;
1269 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1270}
1271static DEVICE_ATTR_RO(dsm_mask);
1272
Dan Williams58138822015-06-23 20:08:34 -04001273static ssize_t flags_show(struct device *dev,
1274 struct device_attribute *attr, char *buf)
1275{
1276 u16 flags = to_nfit_memdev(dev)->flags;
1277
Dan Williamsffab9382017-04-13 15:05:30 -07001278 return sprintf(buf, "%s%s%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001279 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1280 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1281 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001282 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Dan Williamsffab9382017-04-13 15:05:30 -07001283 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1284 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1285 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
Dan Williams58138822015-06-23 20:08:34 -04001286}
1287static DEVICE_ATTR_RO(flags);
1288
Toshi Kani38a879b2016-04-25 15:34:59 -06001289static ssize_t id_show(struct device *dev,
1290 struct device_attribute *attr, char *buf)
1291{
1292 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1293
1294 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1295 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1296 be16_to_cpu(dcr->vendor_id),
1297 dcr->manufacturing_location,
1298 be16_to_cpu(dcr->manufacturing_date),
1299 be32_to_cpu(dcr->serial_number));
1300 else
1301 return sprintf(buf, "%04x-%08x\n",
1302 be16_to_cpu(dcr->vendor_id),
1303 be32_to_cpu(dcr->serial_number));
1304}
1305static DEVICE_ATTR_RO(id);
1306
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001307static struct attribute *acpi_nfit_dimm_attributes[] = {
1308 &dev_attr_handle.attr,
1309 &dev_attr_phys_id.attr,
1310 &dev_attr_vendor.attr,
1311 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001312 &dev_attr_rev_id.attr,
1313 &dev_attr_subsystem_vendor.attr,
1314 &dev_attr_subsystem_device.attr,
1315 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001316 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001317 &dev_attr_formats.attr,
1318 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001319 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001320 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001321 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001322 &dev_attr_family.attr,
1323 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001324 NULL,
1325};
1326
1327static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1328 struct attribute *a, int n)
1329{
1330 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001331 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001332
Dan Williams14999342017-04-13 19:46:36 -07001333 if (!to_nfit_dcr(dev)) {
1334 /* Without a dcr only the memdev attributes can be surfaced */
1335 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1336 || a == &dev_attr_flags.attr
1337 || a == &dev_attr_family.attr
1338 || a == &dev_attr_dsm_mask.attr)
1339 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001340 return 0;
Dan Williams14999342017-04-13 19:46:36 -07001341 }
1342
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001343 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1344 return 0;
1345 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001346}
1347
1348static struct attribute_group acpi_nfit_dimm_attribute_group = {
1349 .name = "nfit",
1350 .attrs = acpi_nfit_dimm_attributes,
1351 .is_visible = acpi_nfit_dimm_attr_visible,
1352};
1353
1354static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001355 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001356 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001357 &acpi_nfit_dimm_attribute_group,
1358 NULL,
1359};
1360
1361static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1362 u32 device_handle)
1363{
1364 struct nfit_mem *nfit_mem;
1365
1366 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1367 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1368 return nfit_mem->nvdimm;
1369
1370 return NULL;
1371}
1372
Dan Williams231bf112016-08-22 19:23:25 -07001373void __acpi_nvdimm_notify(struct device *dev, u32 event)
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001374{
1375 struct nfit_mem *nfit_mem;
1376 struct acpi_nfit_desc *acpi_desc;
1377
1378 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1379 event);
1380
1381 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1382 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1383 event);
1384 return;
1385 }
1386
1387 acpi_desc = dev_get_drvdata(dev->parent);
1388 if (!acpi_desc)
1389 return;
1390
1391 /*
1392 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1393 * is still valid.
1394 */
1395 nfit_mem = dev_get_drvdata(dev);
1396 if (nfit_mem && nfit_mem->flags_attr)
1397 sysfs_notify_dirent(nfit_mem->flags_attr);
1398}
Dan Williams231bf112016-08-22 19:23:25 -07001399EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001400
1401static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1402{
1403 struct acpi_device *adev = data;
1404 struct device *dev = &adev->dev;
1405
1406 device_lock(dev->parent);
1407 __acpi_nvdimm_notify(dev, event);
1408 device_unlock(dev->parent);
1409}
1410
Dan Williams62232e452015-06-08 14:27:06 -04001411static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1412 struct nfit_mem *nfit_mem, u32 device_handle)
1413{
1414 struct acpi_device *adev, *adev_dimm;
1415 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001416 unsigned long dsm_mask;
1417 const u8 *uuid;
Linda Knippers60e95f432015-07-22 16:17:22 -04001418 int i;
Linda Knippersba650cf2017-03-07 16:35:13 -05001419 int family = -1;
Dan Williams62232e452015-06-08 14:27:06 -04001420
Dan Williamse3654ec2016-04-28 16:17:07 -07001421 /* nfit test assumes 1:1 relationship between commands and dsms */
1422 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001423 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001424 adev = to_acpi_dev(acpi_desc);
1425 if (!adev)
1426 return 0;
1427
1428 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1429 nfit_mem->adev = adev_dimm;
1430 if (!adev_dimm) {
1431 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1432 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001433 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001434 }
1435
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001436 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1437 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1438 dev_err(dev, "%s: notification registration failed\n",
1439 dev_name(&adev_dimm->dev));
1440 return -ENXIO;
1441 }
1442
Dan Williams31eca762016-04-28 16:23:43 -07001443 /*
stuart hayese02fb722016-05-26 11:38:41 -05001444 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001445 * different command sets. Note, that checking for function0 (bit0)
1446 * tells us if any commands are reachable through this uuid.
Dan Williams31eca762016-04-28 16:23:43 -07001447 */
stuart hayese02fb722016-05-26 11:38:41 -05001448 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001449 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Linda Knippersba650cf2017-03-07 16:35:13 -05001450 if (family < 0 || i == default_dsm_family)
1451 family = i;
Dan Williams31eca762016-04-28 16:23:43 -07001452
1453 /* limit the supported commands to those that are publicly documented */
Linda Knippersba650cf2017-03-07 16:35:13 -05001454 nfit_mem->family = family;
Linda Knippers095ab4b2017-03-07 16:35:12 -05001455 if (override_dsm_mask && !disable_vendor_specific)
1456 dsm_mask = override_dsm_mask;
1457 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001458 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001459 if (disable_vendor_specific)
1460 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001461 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001462 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001463 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001464 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001465 if (disable_vendor_specific)
1466 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001467 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1468 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001469 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001470 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001471 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001472 /* DSMs are optional, continue loading the driver... */
1473 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001474 }
1475
1476 uuid = to_nfit_uuid(nfit_mem->family);
1477 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Dan Williams62232e452015-06-08 14:27:06 -04001478 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
1479 set_bit(i, &nfit_mem->dsm_mask);
1480
Linda Knippers60e95f432015-07-22 16:17:22 -04001481 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001482}
1483
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001484static void shutdown_dimm_notify(void *data)
1485{
1486 struct acpi_nfit_desc *acpi_desc = data;
1487 struct nfit_mem *nfit_mem;
1488
1489 mutex_lock(&acpi_desc->init_mutex);
1490 /*
1491 * Clear out the nfit_mem->flags_attr and shut down dimm event
1492 * notifications.
1493 */
1494 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williams231bf112016-08-22 19:23:25 -07001495 struct acpi_device *adev_dimm = nfit_mem->adev;
1496
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001497 if (nfit_mem->flags_attr) {
1498 sysfs_put(nfit_mem->flags_attr);
1499 nfit_mem->flags_attr = NULL;
1500 }
Dan Williams231bf112016-08-22 19:23:25 -07001501 if (adev_dimm)
1502 acpi_remove_notify_handler(adev_dimm->handle,
1503 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001504 }
1505 mutex_unlock(&acpi_desc->init_mutex);
1506}
1507
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001508static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1509{
1510 struct nfit_mem *nfit_mem;
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001511 int dimm_count = 0, rc;
1512 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001513
1514 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001515 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001516 unsigned long flags = 0, cmd_mask;
Dan Williamscaa603a2017-04-14 10:27:11 -07001517 struct nfit_memdev *nfit_memdev;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001518 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001519 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001520
1521 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1522 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1523 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001524 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001525 continue;
1526 }
1527
1528 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
Dan Williams8f078b32017-05-04 14:01:24 -07001529 set_bit(NDD_ALIASING, &flags);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001530
Dan Williamscaa603a2017-04-14 10:27:11 -07001531 /* collate flags across all memdevs for this dimm */
1532 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1533 struct acpi_nfit_memory_map *dimm_memdev;
1534
1535 dimm_memdev = __to_nfit_memdev(nfit_mem);
1536 if (dimm_memdev->device_handle
1537 != nfit_memdev->memdev->device_handle)
1538 continue;
1539 dimm_memdev->flags |= nfit_memdev->memdev->flags;
1540 }
1541
Dan Williams58138822015-06-23 20:08:34 -04001542 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001543 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams8f078b32017-05-04 14:01:24 -07001544 set_bit(NDD_UNARMED, &flags);
Dan Williams58138822015-06-23 20:08:34 -04001545
Dan Williams62232e452015-06-08 14:27:06 -04001546 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1547 if (rc)
1548 continue;
1549
Dan Williamse3654ec2016-04-28 16:17:07 -07001550 /*
Dan Williams31eca762016-04-28 16:23:43 -07001551 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1552 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1553 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001554 */
Dan Williams31eca762016-04-28 16:23:43 -07001555 cmd_mask = 1UL << ND_CMD_CALL;
1556 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1557 cmd_mask |= nfit_mem->dsm_mask;
1558
Dan Williamse5ae3b22016-06-07 17:00:04 -07001559 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1560 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001561 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001562 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001563 flags, cmd_mask, flush ? flush->hint_count : 0,
1564 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001565 if (!nvdimm)
1566 return -ENOMEM;
1567
1568 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001569 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001570
1571 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1572 continue;
1573
Dan Williams14999342017-04-13 19:46:36 -07001574 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001575 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001576 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1577 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1578 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Dan Williams14999342017-04-13 19:46:36 -07001579 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1580 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
Dan Williams58138822015-06-23 20:08:34 -04001581
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001582 }
1583
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001584 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1585 if (rc)
1586 return rc;
1587
1588 /*
1589 * Now that dimms are successfully registered, and async registration
1590 * is flushed, attempt to enable event notification.
1591 */
1592 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1593 struct kernfs_node *nfit_kernfs;
1594
1595 nvdimm = nfit_mem->nvdimm;
1596 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1597 if (nfit_kernfs)
1598 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1599 "flags");
1600 sysfs_put(nfit_kernfs);
1601 if (!nfit_mem->flags_attr)
1602 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1603 nvdimm_name(nvdimm));
1604 }
1605
1606 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1607 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001608}
1609
Dan Williams62232e452015-06-08 14:27:06 -04001610static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1611{
1612 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1613 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1614 struct acpi_device *adev;
1615 int i;
1616
Dan Williamse3654ec2016-04-28 16:17:07 -07001617 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001618 adev = to_acpi_dev(acpi_desc);
1619 if (!adev)
1620 return;
1621
Dan Williamsd4f32362016-03-03 16:08:54 -08001622 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Dan Williams62232e452015-06-08 14:27:06 -04001623 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001624 set_bit(i, &nd_desc->cmd_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001625}
1626
Dan Williams1f7df6f2015-06-09 20:13:14 -04001627static ssize_t range_index_show(struct device *dev,
1628 struct device_attribute *attr, char *buf)
1629{
1630 struct nd_region *nd_region = to_nd_region(dev);
1631 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1632
1633 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1634}
1635static DEVICE_ATTR_RO(range_index);
1636
1637static struct attribute *acpi_nfit_region_attributes[] = {
1638 &dev_attr_range_index.attr,
1639 NULL,
1640};
1641
1642static struct attribute_group acpi_nfit_region_attribute_group = {
1643 .name = "nfit",
1644 .attrs = acpi_nfit_region_attributes,
1645};
1646
1647static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1648 &nd_region_attribute_group,
1649 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001650 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001651 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001652 &acpi_nfit_region_attribute_group,
1653 NULL,
1654};
1655
Dan Williamseaf96152015-05-01 13:11:27 -04001656/* enough info to uniquely specify an interleave set */
1657struct nfit_set_info {
1658 struct nfit_set_info_map {
1659 u64 region_offset;
1660 u32 serial_number;
1661 u32 pad;
1662 } mapping[0];
1663};
1664
1665static size_t sizeof_nfit_set_info(int num_mappings)
1666{
1667 return sizeof(struct nfit_set_info)
1668 + num_mappings * sizeof(struct nfit_set_info_map);
1669}
1670
Dan Williams86ef58a2017-02-28 18:32:48 -08001671static int cmp_map_compat(const void *m0, const void *m1)
Dan Williamseaf96152015-05-01 13:11:27 -04001672{
1673 const struct nfit_set_info_map *map0 = m0;
1674 const struct nfit_set_info_map *map1 = m1;
1675
1676 return memcmp(&map0->region_offset, &map1->region_offset,
1677 sizeof(u64));
1678}
1679
Dan Williams86ef58a2017-02-28 18:32:48 -08001680static int cmp_map(const void *m0, const void *m1)
1681{
1682 const struct nfit_set_info_map *map0 = m0;
1683 const struct nfit_set_info_map *map1 = m1;
1684
Dan Williamsb03b99a2017-03-27 21:53:38 -07001685 if (map0->region_offset < map1->region_offset)
1686 return -1;
1687 else if (map0->region_offset > map1->region_offset)
1688 return 1;
1689 return 0;
Dan Williams86ef58a2017-02-28 18:32:48 -08001690}
1691
Dan Williamseaf96152015-05-01 13:11:27 -04001692/* Retrieve the nth entry referencing this spa */
1693static struct acpi_nfit_memory_map *memdev_from_spa(
1694 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1695{
1696 struct nfit_memdev *nfit_memdev;
1697
1698 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1699 if (nfit_memdev->memdev->range_index == range_index)
1700 if (n-- == 0)
1701 return nfit_memdev->memdev;
1702 return NULL;
1703}
1704
1705static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1706 struct nd_region_desc *ndr_desc,
1707 struct acpi_nfit_system_address *spa)
1708{
1709 int i, spa_type = nfit_spa_type(spa);
1710 struct device *dev = acpi_desc->dev;
1711 struct nd_interleave_set *nd_set;
1712 u16 nr = ndr_desc->num_mappings;
1713 struct nfit_set_info *info;
1714
1715 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1716 /* pass */;
1717 else
1718 return 0;
1719
1720 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1721 if (!nd_set)
1722 return -ENOMEM;
1723
1724 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1725 if (!info)
1726 return -ENOMEM;
1727 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07001728 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04001729 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07001730 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04001731 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1732 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1733 spa->range_index, i);
1734
1735 if (!memdev || !nfit_mem->dcr) {
1736 dev_err(dev, "%s: failed to find DCR\n", __func__);
1737 return -ENODEV;
1738 }
1739
1740 map->region_offset = memdev->region_offset;
1741 map->serial_number = nfit_mem->dcr->serial_number;
1742 }
1743
1744 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1745 cmp_map, NULL);
1746 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
Dan Williams86ef58a2017-02-28 18:32:48 -08001747
1748 /* support namespaces created with the wrong sort order */
1749 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1750 cmp_map_compat, NULL);
1751 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1752
Dan Williamseaf96152015-05-01 13:11:27 -04001753 ndr_desc->nd_set = nd_set;
1754 devm_kfree(dev, info);
1755
1756 return 0;
1757}
1758
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001759static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1760{
1761 struct acpi_nfit_interleave *idt = mmio->idt;
1762 u32 sub_line_offset, line_index, line_offset;
1763 u64 line_no, table_skip_count, table_offset;
1764
1765 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1766 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1767 line_offset = idt->line_offset[line_index]
1768 * mmio->line_size;
1769 table_offset = table_skip_count * mmio->table_size;
1770
1771 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1772}
1773
Ross Zwislerde4a1962015-08-20 16:27:38 -06001774static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001775{
1776 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1777 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001778 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001779
1780 if (mmio->num_lines)
1781 offset = to_interleave_offset(offset, mmio);
1782
Ross Zwisler68202c92016-07-29 14:59:12 -06001783 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001784}
1785
1786static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1787 resource_size_t dpa, unsigned int len, unsigned int write)
1788{
1789 u64 cmd, offset;
1790 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1791
1792 enum {
1793 BCW_OFFSET_MASK = (1ULL << 48)-1,
1794 BCW_LEN_SHIFT = 48,
1795 BCW_LEN_MASK = (1ULL << 8) - 1,
1796 BCW_CMD_SHIFT = 56,
1797 };
1798
1799 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1800 len = len >> L1_CACHE_SHIFT;
1801 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1802 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1803
1804 offset = nfit_blk->cmd_offset + mmio->size * bw;
1805 if (mmio->num_lines)
1806 offset = to_interleave_offset(offset, mmio);
1807
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001808 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001809 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001810
Dan Williamsaef25332016-02-12 17:01:11 -08001811 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001812 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001813}
1814
1815static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1816 resource_size_t dpa, void *iobuf, size_t len, int rw,
1817 unsigned int lane)
1818{
1819 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1820 unsigned int copied = 0;
1821 u64 base_offset;
1822 int rc;
1823
1824 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1825 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001826 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1827 while (len) {
1828 unsigned int c;
1829 u64 offset;
1830
1831 if (mmio->num_lines) {
1832 u32 line_offset;
1833
1834 offset = to_interleave_offset(base_offset + copied,
1835 mmio);
1836 div_u64_rem(offset, mmio->line_size, &line_offset);
1837 c = min_t(size_t, len, mmio->line_size - line_offset);
1838 } else {
1839 offset = base_offset + nfit_blk->bdw_offset;
1840 c = len;
1841 }
1842
1843 if (rw)
Dan Williams0aed55a2017-05-29 12:22:50 -07001844 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001845 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001846 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001847 mmio_flush_range((void __force *)
1848 mmio->addr.aperture + offset, c);
1849
Dan Williams6abccd12017-01-13 14:14:23 -08001850 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001851 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001852
1853 copied += c;
1854 len -= c;
1855 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001856
1857 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001858 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001859
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001860 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1861 return rc;
1862}
1863
1864static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1865 resource_size_t dpa, void *iobuf, u64 len, int rw)
1866{
1867 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1868 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1869 struct nd_region *nd_region = nfit_blk->nd_region;
1870 unsigned int lane, copied = 0;
1871 int rc = 0;
1872
1873 lane = nd_region_acquire_lane(nd_region);
1874 while (len) {
1875 u64 c = min(len, mmio->size);
1876
1877 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1878 iobuf + copied, c, rw, lane);
1879 if (rc)
1880 break;
1881
1882 copied += c;
1883 len -= c;
1884 }
1885 nd_region_release_lane(nd_region, lane);
1886
1887 return rc;
1888}
1889
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001890static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1891 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1892{
1893 if (idt) {
1894 mmio->num_lines = idt->line_count;
1895 mmio->line_size = idt->line_size;
1896 if (interleave_ways == 0)
1897 return -ENXIO;
1898 mmio->table_size = mmio->num_lines * interleave_ways
1899 * mmio->line_size;
1900 }
1901
1902 return 0;
1903}
1904
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001905static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1906 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1907{
1908 struct nd_cmd_dimm_flags flags;
1909 int rc;
1910
1911 memset(&flags, 0, sizeof(flags));
1912 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08001913 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001914
1915 if (rc >= 0 && flags.status == 0)
1916 nfit_blk->dimm_flags = flags.flags;
1917 else if (rc == -ENOTTY) {
1918 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08001919 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001920 rc = 0;
1921 } else
1922 rc = -ENXIO;
1923
1924 return rc;
1925}
1926
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001927static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1928 struct device *dev)
1929{
1930 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001931 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1932 struct nfit_blk_mmio *mmio;
1933 struct nfit_blk *nfit_blk;
1934 struct nfit_mem *nfit_mem;
1935 struct nvdimm *nvdimm;
1936 int rc;
1937
1938 nvdimm = nd_blk_region_to_dimm(ndbr);
1939 nfit_mem = nvdimm_provider_data(nvdimm);
1940 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1941 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1942 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04001943 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1944 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001945 return -ENXIO;
1946 }
1947
1948 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1949 if (!nfit_blk)
1950 return -ENOMEM;
1951 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1952 nfit_blk->nd_region = to_nd_region(dev);
1953
1954 /* map block aperture memory */
1955 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1956 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07001957 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
Dan Williamsca6a4652017-01-13 20:36:58 -08001958 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001959 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001960 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1961 nvdimm_name(nvdimm));
1962 return -ENOMEM;
1963 }
1964 mmio->size = nfit_mem->bdw->size;
1965 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1966 mmio->idt = nfit_mem->idt_bdw;
1967 mmio->spa = nfit_mem->spa_bdw;
1968 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1969 nfit_mem->memdev_bdw->interleave_ways);
1970 if (rc) {
1971 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1972 __func__, nvdimm_name(nvdimm));
1973 return rc;
1974 }
1975
1976 /* map block control memory */
1977 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1978 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1979 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07001980 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1981 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001982 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001983 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1984 nvdimm_name(nvdimm));
1985 return -ENOMEM;
1986 }
1987 mmio->size = nfit_mem->dcr->window_size;
1988 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1989 mmio->idt = nfit_mem->idt_dcr;
1990 mmio->spa = nfit_mem->spa_dcr;
1991 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1992 nfit_mem->memdev_dcr->interleave_ways);
1993 if (rc) {
1994 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1995 __func__, nvdimm_name(nvdimm));
1996 return rc;
1997 }
1998
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001999 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2000 if (rc < 0) {
2001 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
2002 __func__, nvdimm_name(nvdimm));
2003 return rc;
2004 }
2005
Dan Williamsf284a4f2016-07-07 19:44:50 -07002006 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002007 dev_warn(dev, "unable to guarantee persistence of writes\n");
2008
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002009 if (mmio->line_size == 0)
2010 return 0;
2011
2012 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2013 + 8 > mmio->line_size) {
2014 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2015 return -ENXIO;
2016 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2017 + 8 > mmio->line_size) {
2018 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2019 return -ENXIO;
2020 }
2021
2022 return 0;
2023}
2024
Dan Williamsaef25332016-02-12 17:01:11 -08002025static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002026 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002027{
Dan Williamsaef25332016-02-12 17:01:11 -08002028 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002029 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08002030 int cmd_rc, rc;
2031
Dan Williams1cf03c02016-02-17 13:01:23 -08002032 cmd->address = spa->address;
2033 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08002034 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2035 sizeof(*cmd), &cmd_rc);
2036 if (rc < 0)
2037 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002038 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002039}
2040
Dan Williams1cf03c02016-02-17 13:01:23 -08002041static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002042{
2043 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002044 int cmd_rc;
2045 struct nd_cmd_ars_start ars_start;
2046 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2047 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002048
Dan Williams1cf03c02016-02-17 13:01:23 -08002049 memset(&ars_start, 0, sizeof(ars_start));
2050 ars_start.address = spa->address;
2051 ars_start.length = spa->length;
2052 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2053 ars_start.type = ND_ARS_PERSISTENT;
2054 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2055 ars_start.type = ND_ARS_VOLATILE;
2056 else
2057 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07002058
Dan Williams1cf03c02016-02-17 13:01:23 -08002059 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2060 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08002061
Dan Williams1cf03c02016-02-17 13:01:23 -08002062 if (rc < 0)
2063 return rc;
2064 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002065}
2066
Dan Williams1cf03c02016-02-17 13:01:23 -08002067static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07002068{
Dan Williamsaef25332016-02-12 17:01:11 -08002069 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002070 struct nd_cmd_ars_start ars_start;
2071 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2072 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07002073
Dan Williams1cf03c02016-02-17 13:01:23 -08002074 memset(&ars_start, 0, sizeof(ars_start));
2075 ars_start.address = ars_status->restart_address;
2076 ars_start.length = ars_status->restart_length;
2077 ars_start.type = ars_status->type;
2078 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2079 sizeof(ars_start), &cmd_rc);
2080 if (rc < 0)
2081 return rc;
2082 return cmd_rc;
2083}
Dan Williamsaef25332016-02-12 17:01:11 -08002084
Dan Williams1cf03c02016-02-17 13:01:23 -08002085static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2086{
2087 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2088 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2089 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08002090
Dan Williams1cf03c02016-02-17 13:01:23 -08002091 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2092 acpi_desc->ars_status_size, &cmd_rc);
2093 if (rc < 0)
2094 return rc;
2095 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002096}
2097
Dan Williams82aa37c2016-12-06 12:45:24 -08002098static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002099 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07002100{
Dan Williams82aa37c2016-12-06 12:45:24 -08002101 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
Vishal Verma0caeef62015-12-24 19:21:43 -07002102 int rc;
2103 u32 i;
2104
Dan Williams82aa37c2016-12-06 12:45:24 -08002105 /*
2106 * First record starts at 44 byte offset from the start of the
2107 * payload.
2108 */
2109 if (ars_status->out_length < 44)
2110 return 0;
Vishal Verma0caeef62015-12-24 19:21:43 -07002111 for (i = 0; i < ars_status->num_records; i++) {
Dan Williams82aa37c2016-12-06 12:45:24 -08002112 /* only process full records */
2113 if (ars_status->out_length
2114 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2115 break;
Vishal Verma0caeef62015-12-24 19:21:43 -07002116 rc = nvdimm_bus_add_poison(nvdimm_bus,
2117 ars_status->records[i].err_address,
2118 ars_status->records[i].length);
2119 if (rc)
2120 return rc;
2121 }
Dan Williams82aa37c2016-12-06 12:45:24 -08002122 if (i < ars_status->num_records)
2123 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
Vishal Verma0caeef62015-12-24 19:21:43 -07002124
2125 return 0;
2126}
2127
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002128static void acpi_nfit_remove_resource(void *data)
2129{
2130 struct resource *res = data;
2131
2132 remove_resource(res);
2133}
2134
2135static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2136 struct nd_region_desc *ndr_desc)
2137{
2138 struct resource *res, *nd_res = ndr_desc->res;
2139 int is_pmem, ret;
2140
2141 /* No operation if the region is already registered as PMEM */
2142 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2143 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2144 if (is_pmem == REGION_INTERSECTS)
2145 return 0;
2146
2147 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2148 if (!res)
2149 return -ENOMEM;
2150
2151 res->name = "Persistent Memory";
2152 res->start = nd_res->start;
2153 res->end = nd_res->end;
2154 res->flags = IORESOURCE_MEM;
2155 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2156
2157 ret = insert_resource(&iomem_resource, res);
2158 if (ret)
2159 return ret;
2160
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302161 ret = devm_add_action_or_reset(acpi_desc->dev,
2162 acpi_nfit_remove_resource,
2163 res);
2164 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002165 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002166
2167 return 0;
2168}
2169
Dan Williams1f7df6f2015-06-09 20:13:14 -04002170static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002171 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002172 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002173 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002174{
2175 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2176 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002177 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002178 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002179 struct nfit_mem *nfit_mem;
2180 int blk_valid = 0;
2181
2182 if (!nvdimm) {
2183 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2184 spa->range_index, memdev->device_handle);
2185 return -ENODEV;
2186 }
2187
Dan Williams44c462e2016-09-19 16:38:50 -07002188 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002189 switch (nfit_spa_type(spa)) {
2190 case NFIT_SPA_PM:
2191 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002192 mapping->start = memdev->address;
2193 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002194 break;
2195 case NFIT_SPA_DCR:
2196 nfit_mem = nvdimm_provider_data(nvdimm);
2197 if (!nfit_mem || !nfit_mem->bdw) {
2198 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2199 spa->range_index, nvdimm_name(nvdimm));
2200 } else {
Dan Williams44c462e2016-09-19 16:38:50 -07002201 mapping->size = nfit_mem->bdw->capacity;
2202 mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04002203 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002204 blk_valid = 1;
2205 }
2206
Dan Williams44c462e2016-09-19 16:38:50 -07002207 ndr_desc->mapping = mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002208 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002209 ndbr_desc = to_blk_region_desc(ndr_desc);
2210 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002211 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williams1cf03c02016-02-17 13:01:23 -08002212 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2213 ndr_desc);
2214 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002215 return -ENOMEM;
2216 break;
2217 }
2218
2219 return 0;
2220}
2221
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002222static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2223{
2224 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2225 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2226 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2227 nfit_spa_type(spa) == NFIT_SPA_PCD);
2228}
2229
Dan Williams1f7df6f2015-06-09 20:13:14 -04002230static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2231 struct nfit_spa *nfit_spa)
2232{
Dan Williams44c462e2016-09-19 16:38:50 -07002233 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002234 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002235 struct nd_blk_region_desc ndbr_desc;
2236 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002237 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002238 struct nvdimm_bus *nvdimm_bus;
2239 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002240 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002241
Dan Williams1cf03c02016-02-17 13:01:23 -08002242 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002243 return 0;
2244
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002245 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04002246 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2247 __func__);
2248 return 0;
2249 }
2250
2251 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002252 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002253 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002254 res.start = spa->address;
2255 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002256 ndr_desc = &ndbr_desc.ndr_desc;
2257 ndr_desc->res = &res;
2258 ndr_desc->provider_data = nfit_spa;
2259 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002260 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2261 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2262 spa->proximity_domain);
2263 else
2264 ndr_desc->numa_node = NUMA_NO_NODE;
2265
Dan Williams1f7df6f2015-06-09 20:13:14 -04002266 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2267 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002268 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002269
2270 if (memdev->range_index != spa->range_index)
2271 continue;
2272 if (count >= ND_MAX_MAPPINGS) {
2273 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2274 spa->range_index, ND_MAX_MAPPINGS);
2275 return -ENXIO;
2276 }
Dan Williams44c462e2016-09-19 16:38:50 -07002277 mapping = &mappings[count++];
2278 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002279 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002280 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002281 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002282 }
2283
Dan Williams44c462e2016-09-19 16:38:50 -07002284 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002285 ndr_desc->num_mappings = count;
2286 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002287 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002288 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002289
Dan Williams1f7df6f2015-06-09 20:13:14 -04002290 nvdimm_bus = acpi_desc->nvdimm_bus;
2291 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002292 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002293 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002294 dev_warn(acpi_desc->dev,
2295 "failed to insert pmem resource to iomem: %d\n",
2296 rc);
Dan Williams48901162016-03-09 17:15:43 -08002297 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002298 }
Dan Williams48901162016-03-09 17:15:43 -08002299
Dan Williams1cf03c02016-02-17 13:01:23 -08002300 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2301 ndr_desc);
2302 if (!nfit_spa->nd_region)
2303 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002304 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002305 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2306 ndr_desc);
2307 if (!nfit_spa->nd_region)
2308 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002309 } else if (nfit_spa_is_virtual(spa)) {
2310 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2311 ndr_desc);
2312 if (!nfit_spa->nd_region)
2313 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002314 }
Vishal Verma20985162015-10-27 16:58:27 -06002315
Dan Williams1cf03c02016-02-17 13:01:23 -08002316 out:
2317 if (rc)
2318 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2319 nfit_spa->spa->range_index);
2320 return rc;
2321}
2322
2323static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2324 u32 max_ars)
2325{
2326 struct device *dev = acpi_desc->dev;
2327 struct nd_cmd_ars_status *ars_status;
2328
2329 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2330 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2331 return 0;
2332 }
2333
2334 if (acpi_desc->ars_status)
2335 devm_kfree(dev, acpi_desc->ars_status);
2336 acpi_desc->ars_status = NULL;
2337 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2338 if (!ars_status)
2339 return -ENOMEM;
2340 acpi_desc->ars_status = ars_status;
2341 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002342 return 0;
2343}
2344
Dan Williams1cf03c02016-02-17 13:01:23 -08002345static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2346 struct nfit_spa *nfit_spa)
2347{
2348 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2349 int rc;
2350
2351 if (!nfit_spa->max_ars) {
2352 struct nd_cmd_ars_cap ars_cap;
2353
2354 memset(&ars_cap, 0, sizeof(ars_cap));
2355 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2356 if (rc < 0)
2357 return rc;
2358 nfit_spa->max_ars = ars_cap.max_ars_out;
2359 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2360 /* check that the supported scrub types match the spa type */
2361 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2362 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2363 return -ENOTTY;
2364 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2365 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2366 return -ENOTTY;
2367 }
2368
2369 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2370 return -ENOMEM;
2371
2372 rc = ars_get_status(acpi_desc);
2373 if (rc < 0 && rc != -ENOSPC)
2374 return rc;
2375
Dan Williams82aa37c2016-12-06 12:45:24 -08002376 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
Dan Williams1cf03c02016-02-17 13:01:23 -08002377 return -ENOMEM;
2378
2379 return 0;
2380}
2381
2382static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2383 struct nfit_spa *nfit_spa)
2384{
2385 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2386 unsigned int overflow_retry = scrub_overflow_abort;
2387 u64 init_ars_start = 0, init_ars_len = 0;
2388 struct device *dev = acpi_desc->dev;
2389 unsigned int tmo = scrub_timeout;
2390 int rc;
2391
Vishal Verma37b137f2016-07-23 21:51:42 -07002392 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002393 return;
2394
2395 rc = ars_start(acpi_desc, nfit_spa);
2396 /*
2397 * If we timed out the initial scan we'll still be busy here,
2398 * and will wait another timeout before giving up permanently.
2399 */
2400 if (rc < 0 && rc != -EBUSY)
2401 return;
2402
2403 do {
2404 u64 ars_start, ars_len;
2405
2406 if (acpi_desc->cancel)
2407 break;
2408 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2409 if (rc == -ENOTTY)
2410 break;
2411 if (rc == -EBUSY && !tmo) {
2412 dev_warn(dev, "range %d ars timeout, aborting\n",
2413 spa->range_index);
2414 break;
2415 }
2416
2417 if (rc == -EBUSY) {
2418 /*
2419 * Note, entries may be appended to the list
2420 * while the lock is dropped, but the workqueue
2421 * being active prevents entries being deleted /
2422 * freed.
2423 */
2424 mutex_unlock(&acpi_desc->init_mutex);
2425 ssleep(1);
2426 tmo--;
2427 mutex_lock(&acpi_desc->init_mutex);
2428 continue;
2429 }
2430
2431 /* we got some results, but there are more pending... */
2432 if (rc == -ENOSPC && overflow_retry--) {
2433 if (!init_ars_len) {
2434 init_ars_len = acpi_desc->ars_status->length;
2435 init_ars_start = acpi_desc->ars_status->address;
2436 }
2437 rc = ars_continue(acpi_desc);
2438 }
2439
2440 if (rc < 0) {
2441 dev_warn(dev, "range %d ars continuation failed\n",
2442 spa->range_index);
2443 break;
2444 }
2445
2446 if (init_ars_len) {
2447 ars_start = init_ars_start;
2448 ars_len = init_ars_len;
2449 } else {
2450 ars_start = acpi_desc->ars_status->address;
2451 ars_len = acpi_desc->ars_status->length;
2452 }
2453 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2454 spa->range_index, ars_start, ars_len);
2455 /* notify the region about new poison entries */
2456 nvdimm_region_notify(nfit_spa->nd_region,
2457 NVDIMM_REVALIDATE_POISON);
2458 break;
2459 } while (1);
2460}
2461
2462static void acpi_nfit_scrub(struct work_struct *work)
2463{
2464 struct device *dev;
2465 u64 init_scrub_length = 0;
2466 struct nfit_spa *nfit_spa;
2467 u64 init_scrub_address = 0;
2468 bool init_ars_done = false;
2469 struct acpi_nfit_desc *acpi_desc;
2470 unsigned int tmo = scrub_timeout;
2471 unsigned int overflow_retry = scrub_overflow_abort;
2472
2473 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2474 dev = acpi_desc->dev;
2475
2476 /*
2477 * We scrub in 2 phases. The first phase waits for any platform
2478 * firmware initiated scrubs to complete and then we go search for the
2479 * affected spa regions to mark them scanned. In the second phase we
2480 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002481 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2482 * the first phase, but really only care about running phase 2, where
2483 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002484 */
2485
2486 /* process platform firmware initiated scrubs */
2487 retry:
2488 mutex_lock(&acpi_desc->init_mutex);
2489 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2490 struct nd_cmd_ars_status *ars_status;
2491 struct acpi_nfit_system_address *spa;
2492 u64 ars_start, ars_len;
2493 int rc;
2494
2495 if (acpi_desc->cancel)
2496 break;
2497
2498 if (nfit_spa->nd_region)
2499 continue;
2500
2501 if (init_ars_done) {
2502 /*
2503 * No need to re-query, we're now just
2504 * reconciling all the ranges covered by the
2505 * initial scrub
2506 */
2507 rc = 0;
2508 } else
2509 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2510
2511 if (rc == -ENOTTY) {
2512 /* no ars capability, just register spa and move on */
2513 acpi_nfit_register_region(acpi_desc, nfit_spa);
2514 continue;
2515 }
2516
2517 if (rc == -EBUSY && !tmo) {
2518 /* fallthrough to directed scrub in phase 2 */
2519 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2520 break;
2521 } else if (rc == -EBUSY) {
2522 mutex_unlock(&acpi_desc->init_mutex);
2523 ssleep(1);
2524 tmo--;
2525 goto retry;
2526 }
2527
2528 /* we got some results, but there are more pending... */
2529 if (rc == -ENOSPC && overflow_retry--) {
2530 ars_status = acpi_desc->ars_status;
2531 /*
2532 * Record the original scrub range, so that we
2533 * can recall all the ranges impacted by the
2534 * initial scrub.
2535 */
2536 if (!init_scrub_length) {
2537 init_scrub_length = ars_status->length;
2538 init_scrub_address = ars_status->address;
2539 }
2540 rc = ars_continue(acpi_desc);
2541 if (rc == 0) {
2542 mutex_unlock(&acpi_desc->init_mutex);
2543 goto retry;
2544 }
2545 }
2546
2547 if (rc < 0) {
2548 /*
2549 * Initial scrub failed, we'll give it one more
2550 * try below...
2551 */
2552 break;
2553 }
2554
2555 /* We got some final results, record completed ranges */
2556 ars_status = acpi_desc->ars_status;
2557 if (init_scrub_length) {
2558 ars_start = init_scrub_address;
2559 ars_len = ars_start + init_scrub_length;
2560 } else {
2561 ars_start = ars_status->address;
2562 ars_len = ars_status->length;
2563 }
2564 spa = nfit_spa->spa;
2565
2566 if (!init_ars_done) {
2567 init_ars_done = true;
2568 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2569 ars_start, ars_len);
2570 }
2571 if (ars_start <= spa->address && ars_start + ars_len
2572 >= spa->address + spa->length)
2573 acpi_nfit_register_region(acpi_desc, nfit_spa);
2574 }
2575
2576 /*
2577 * For all the ranges not covered by an initial scrub we still
2578 * want to see if there are errors, but it's ok to discover them
2579 * asynchronously.
2580 */
2581 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2582 /*
2583 * Flag all the ranges that still need scrubbing, but
2584 * register them now to make data available.
2585 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002586 if (!nfit_spa->nd_region) {
2587 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002588 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002589 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002590 }
Dan Williams9ccaed42017-04-13 22:48:46 -07002591 acpi_desc->init_complete = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002592
2593 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2594 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002595 acpi_desc->scrub_count++;
2596 if (acpi_desc->scrub_count_state)
2597 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002598 mutex_unlock(&acpi_desc->init_mutex);
2599}
2600
Dan Williams1f7df6f2015-06-09 20:13:14 -04002601static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2602{
2603 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002604 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002605
Dan Williams1cf03c02016-02-17 13:01:23 -08002606 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2607 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2608 /* BLK regions don't need to wait for ars results */
2609 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2610 if (rc)
2611 return rc;
2612 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002613
Dan Williamsfbabd822017-04-18 09:56:31 -07002614 if (!acpi_desc->cancel)
2615 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002616 return 0;
2617}
2618
Vishal Verma20985162015-10-27 16:58:27 -06002619static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2620 struct nfit_table_prev *prev)
2621{
2622 struct device *dev = acpi_desc->dev;
2623
2624 if (!list_empty(&prev->spas) ||
2625 !list_empty(&prev->memdevs) ||
2626 !list_empty(&prev->dcrs) ||
2627 !list_empty(&prev->bdws) ||
2628 !list_empty(&prev->idts) ||
2629 !list_empty(&prev->flushes)) {
2630 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2631 return -ENXIO;
2632 }
2633 return 0;
2634}
2635
Vishal Verma37b137f2016-07-23 21:51:42 -07002636static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2637{
2638 struct device *dev = acpi_desc->dev;
2639 struct kernfs_node *nfit;
2640 struct device *bus_dev;
2641
2642 if (!ars_supported(acpi_desc->nvdimm_bus))
2643 return 0;
2644
2645 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2646 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2647 if (!nfit) {
2648 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2649 return -ENODEV;
2650 }
2651 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2652 sysfs_put(nfit);
2653 if (!acpi_desc->scrub_count_state) {
2654 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2655 return -ENODEV;
2656 }
2657
2658 return 0;
2659}
2660
Dan Williamsfbabd822017-04-18 09:56:31 -07002661static void acpi_nfit_unregister(void *data)
Dan Williams58cd71b2016-07-21 18:05:36 -07002662{
2663 struct acpi_nfit_desc *acpi_desc = data;
2664
Dan Williams58cd71b2016-07-21 18:05:36 -07002665 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002666}
2667
Dan Williamse7a11b42016-07-14 16:19:55 -07002668int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002669{
2670 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002671 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002672 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002673 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002674
Dan Williams58cd71b2016-07-21 18:05:36 -07002675 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002676 acpi_nfit_init_dsms(acpi_desc);
2677
Dan Williams58cd71b2016-07-21 18:05:36 -07002678 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2679 &acpi_desc->nd_desc);
2680 if (!acpi_desc->nvdimm_bus)
2681 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002682
Dan Williamsfbabd822017-04-18 09:56:31 -07002683 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
Dan Williams58cd71b2016-07-21 18:05:36 -07002684 acpi_desc);
2685 if (rc)
2686 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002687
2688 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2689 if (rc)
2690 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002691
2692 /* register this acpi_desc for mce notifications */
2693 mutex_lock(&acpi_desc_lock);
2694 list_add_tail(&acpi_desc->list, &acpi_descs);
2695 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002696 }
2697
Vishal Verma20985162015-10-27 16:58:27 -06002698 mutex_lock(&acpi_desc->init_mutex);
2699
2700 INIT_LIST_HEAD(&prev.spas);
2701 INIT_LIST_HEAD(&prev.memdevs);
2702 INIT_LIST_HEAD(&prev.dcrs);
2703 INIT_LIST_HEAD(&prev.bdws);
2704 INIT_LIST_HEAD(&prev.idts);
2705 INIT_LIST_HEAD(&prev.flushes);
2706
2707 list_cut_position(&prev.spas, &acpi_desc->spas,
2708 acpi_desc->spas.prev);
2709 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2710 acpi_desc->memdevs.prev);
2711 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2712 acpi_desc->dcrs.prev);
2713 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2714 acpi_desc->bdws.prev);
2715 list_cut_position(&prev.idts, &acpi_desc->idts,
2716 acpi_desc->idts.prev);
2717 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2718 acpi_desc->flushes.prev);
2719
Vishal Verma20985162015-10-27 16:58:27 -06002720 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002721 while (!IS_ERR_OR_NULL(data))
2722 data = add_table(acpi_desc, &prev, data, end);
2723
2724 if (IS_ERR(data)) {
2725 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2726 PTR_ERR(data));
2727 rc = PTR_ERR(data);
2728 goto out_unlock;
2729 }
2730
2731 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2732 if (rc)
2733 goto out_unlock;
2734
Dan Williams81ed4e32016-06-10 18:20:53 -07002735 rc = nfit_mem_init(acpi_desc);
2736 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002737 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002738
2739 rc = acpi_nfit_register_dimms(acpi_desc);
2740 if (rc)
2741 goto out_unlock;
2742
2743 rc = acpi_nfit_register_regions(acpi_desc);
2744
2745 out_unlock:
2746 mutex_unlock(&acpi_desc->init_mutex);
2747 return rc;
2748}
2749EXPORT_SYMBOL_GPL(acpi_nfit_init);
2750
Dan Williams7ae0fa432016-02-19 12:16:34 -08002751struct acpi_nfit_flush_work {
2752 struct work_struct work;
2753 struct completion cmp;
2754};
2755
2756static void flush_probe(struct work_struct *work)
2757{
2758 struct acpi_nfit_flush_work *flush;
2759
2760 flush = container_of(work, typeof(*flush), work);
2761 complete(&flush->cmp);
2762}
2763
2764static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2765{
2766 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2767 struct device *dev = acpi_desc->dev;
2768 struct acpi_nfit_flush_work flush;
Dan Williamse4714862017-02-02 10:31:00 -08002769 int rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002770
2771 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2772 device_lock(dev);
2773 device_unlock(dev);
2774
Dan Williams9ccaed42017-04-13 22:48:46 -07002775 /* bounce the init_mutex to make init_complete valid */
2776 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07002777 if (acpi_desc->cancel || acpi_desc->init_complete) {
2778 mutex_unlock(&acpi_desc->init_mutex);
Dan Williams9ccaed42017-04-13 22:48:46 -07002779 return 0;
Dan Williamsfbabd822017-04-18 09:56:31 -07002780 }
Dan Williams9ccaed42017-04-13 22:48:46 -07002781
Dan Williams7ae0fa432016-02-19 12:16:34 -08002782 /*
2783 * Scrub work could take 10s of seconds, userspace may give up so we
2784 * need to be interruptible while waiting.
2785 */
2786 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2787 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2788 queue_work(nfit_wq, &flush.work);
Dan Williamsfbabd822017-04-18 09:56:31 -07002789 mutex_unlock(&acpi_desc->init_mutex);
Dan Williamse4714862017-02-02 10:31:00 -08002790
2791 rc = wait_for_completion_interruptible(&flush.cmp);
2792 cancel_work_sync(&flush.work);
2793 return rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002794}
2795
Dan Williams87bf5722016-02-22 21:50:31 -08002796static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2797 struct nvdimm *nvdimm, unsigned int cmd)
2798{
2799 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2800
2801 if (nvdimm)
2802 return 0;
2803 if (cmd != ND_CMD_ARS_START)
2804 return 0;
2805
2806 /*
2807 * The kernel and userspace may race to initiate a scrub, but
2808 * the scrub thread is prepared to lose that initial race. It
2809 * just needs guarantees that any ars it initiates are not
2810 * interrupted by any intervening start reqeusts from userspace.
2811 */
2812 if (work_busy(&acpi_desc->work))
2813 return -EBUSY;
2814
2815 return 0;
2816}
2817
Vishal Verma6839a6d2016-07-23 21:51:21 -07002818int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
Vishal Verma37b137f2016-07-23 21:51:42 -07002819{
2820 struct device *dev = acpi_desc->dev;
2821 struct nfit_spa *nfit_spa;
2822
2823 if (work_busy(&acpi_desc->work))
2824 return -EBUSY;
2825
Vishal Verma37b137f2016-07-23 21:51:42 -07002826 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07002827 if (acpi_desc->cancel) {
2828 mutex_unlock(&acpi_desc->init_mutex);
2829 return 0;
2830 }
2831
Vishal Verma37b137f2016-07-23 21:51:42 -07002832 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2833 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2834
2835 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2836 continue;
2837
2838 nfit_spa->ars_required = 1;
2839 }
2840 queue_work(nfit_wq, &acpi_desc->work);
2841 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2842 mutex_unlock(&acpi_desc->init_mutex);
2843
2844 return 0;
2845}
2846
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002847void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002848{
2849 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002850
2851 dev_set_drvdata(dev, acpi_desc);
2852 acpi_desc->dev = dev;
2853 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2854 nd_desc = &acpi_desc->nd_desc;
2855 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002856 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002857 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002858 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002859 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002860 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2861
Dan Williamsb94d5232015-05-19 22:54:31 -04002862 INIT_LIST_HEAD(&acpi_desc->spas);
2863 INIT_LIST_HEAD(&acpi_desc->dcrs);
2864 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002865 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002866 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04002867 INIT_LIST_HEAD(&acpi_desc->memdevs);
2868 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002869 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06002870 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08002871 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04002872}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002873EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04002874
Dan Williams3c87f372017-04-03 13:52:14 -07002875static void acpi_nfit_put_table(void *table)
2876{
2877 acpi_put_table(table);
2878}
2879
Dan Williamsfbabd822017-04-18 09:56:31 -07002880void acpi_nfit_shutdown(void *data)
2881{
2882 struct acpi_nfit_desc *acpi_desc = data;
2883 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2884
2885 /*
2886 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2887 * race teardown
2888 */
2889 mutex_lock(&acpi_desc_lock);
2890 list_del(&acpi_desc->list);
2891 mutex_unlock(&acpi_desc_lock);
2892
2893 mutex_lock(&acpi_desc->init_mutex);
2894 acpi_desc->cancel = 1;
2895 mutex_unlock(&acpi_desc->init_mutex);
2896
2897 /*
2898 * Bounce the nvdimm bus lock to make sure any in-flight
2899 * acpi_nfit_ars_rescan() submissions have had a chance to
2900 * either submit or see ->cancel set.
2901 */
2902 device_lock(bus_dev);
2903 device_unlock(bus_dev);
2904
2905 flush_workqueue(nfit_wq);
2906}
2907EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
2908
Dan Williamsb94d5232015-05-19 22:54:31 -04002909static int acpi_nfit_add(struct acpi_device *adev)
2910{
Vishal Verma20985162015-10-27 16:58:27 -06002911 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04002912 struct acpi_nfit_desc *acpi_desc;
2913 struct device *dev = &adev->dev;
2914 struct acpi_table_header *tbl;
2915 acpi_status status = AE_OK;
2916 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07002917 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002918
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002919 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
Dan Williamsb94d5232015-05-19 22:54:31 -04002920 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06002921 /* This is ok, we could have an nvdimm hotplugged later */
2922 dev_dbg(dev, "failed to find NFIT at startup\n");
2923 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002924 }
Dan Williams3c87f372017-04-03 13:52:14 -07002925
2926 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
2927 if (rc)
2928 return rc;
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002929 sz = tbl->length;
Dan Williamsb94d5232015-05-19 22:54:31 -04002930
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002931 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2932 if (!acpi_desc)
2933 return -ENOMEM;
2934 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04002935
Dan Williamse7a11b42016-07-14 16:19:55 -07002936 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05002937 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04002938
Vishal Verma20985162015-10-27 16:58:27 -06002939 /* Evaluate _FIT and override with that if present */
2940 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2941 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002942 union acpi_object *obj = buf.pointer;
2943
2944 if (obj->type == ACPI_TYPE_BUFFER)
2945 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2946 obj->buffer.length);
2947 else
Linda Knippers6b577c92015-11-20 19:05:49 -05002948 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2949 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07002950 kfree(buf.pointer);
2951 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07002952 /* skip over the lead-in header table */
2953 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2954 + sizeof(struct acpi_table_nfit),
2955 sz - sizeof(struct acpi_table_nfit));
Dan Williamsfbabd822017-04-18 09:56:31 -07002956
2957 if (rc)
2958 return rc;
2959 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
Dan Williamsb94d5232015-05-19 22:54:31 -04002960}
2961
2962static int acpi_nfit_remove(struct acpi_device *adev)
2963{
Dan Williamsfbabd822017-04-18 09:56:31 -07002964 /* see acpi_nfit_unregister */
Dan Williamsb94d5232015-05-19 22:54:31 -04002965 return 0;
2966}
2967
Dan Williamsc14a8682016-08-18 22:15:04 -07002968void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
Vishal Verma20985162015-10-27 16:58:27 -06002969{
Dan Williamsc14a8682016-08-18 22:15:04 -07002970 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06002971 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07002972 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06002973 acpi_status status;
2974 int ret;
2975
2976 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2977
Vishal Vermac09f1212016-08-19 14:40:58 -06002978 if (event != NFIT_NOTIFY_UPDATE)
2979 return;
2980
Vishal Verma20985162015-10-27 16:58:27 -06002981 if (!dev->driver) {
2982 /* dev->driver may be null if we're being removed */
2983 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Dan Williamsc14a8682016-08-18 22:15:04 -07002984 return;
Vishal Verma20985162015-10-27 16:58:27 -06002985 }
2986
2987 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002988 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2989 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07002990 return;
2991 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002992 } else {
2993 /*
2994 * Finish previous registration before considering new
2995 * regions.
2996 */
2997 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06002998 }
2999
3000 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07003001 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06003002 if (ACPI_FAILURE(status)) {
3003 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07003004 return;
Vishal Verma20985162015-10-27 16:58:27 -06003005 }
3006
Linda Knippers6b577c92015-11-20 19:05:49 -05003007 obj = buf.pointer;
3008 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07003009 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3010 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07003011 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05003012 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07003013 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05003014 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06003015 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07003016}
3017EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06003018
Dan Williamsc14a8682016-08-18 22:15:04 -07003019static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3020{
3021 device_lock(&adev->dev);
3022 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3023 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06003024}
3025
Dan Williamsb94d5232015-05-19 22:54:31 -04003026static const struct acpi_device_id acpi_nfit_ids[] = {
3027 { "ACPI0012", 0 },
3028 { "", 0 },
3029};
3030MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3031
3032static struct acpi_driver acpi_nfit_driver = {
3033 .name = KBUILD_MODNAME,
3034 .ids = acpi_nfit_ids,
3035 .ops = {
3036 .add = acpi_nfit_add,
3037 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06003038 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04003039 },
3040};
3041
3042static __init int nfit_init(void)
3043{
3044 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3045 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3046 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3047 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3048 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3049 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3050 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3051
3052 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
3053 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
3054 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
3055 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
3056 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
3057 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
3058 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
3059 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
3060 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
3061 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
Dan Williams31eca762016-04-28 16:23:43 -07003062 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3063 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
stuart hayese02fb722016-05-26 11:38:41 -05003064 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04003065
Dan Williams7ae0fa432016-02-19 12:16:34 -08003066 nfit_wq = create_singlethread_workqueue("nfit");
3067 if (!nfit_wq)
3068 return -ENOMEM;
3069
Vishal Verma6839a6d2016-07-23 21:51:21 -07003070 nfit_mce_register();
3071
Dan Williamsb94d5232015-05-19 22:54:31 -04003072 return acpi_bus_register_driver(&acpi_nfit_driver);
3073}
3074
3075static __exit void nfit_exit(void)
3076{
Vishal Verma6839a6d2016-07-23 21:51:21 -07003077 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04003078 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003079 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003080 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04003081}
3082
3083module_init(nfit_init);
3084module_exit(nfit_exit);
3085MODULE_LICENSE("GPL v2");
3086MODULE_AUTHOR("Intel Corporation");