blob: 19d0dfdf96338290db0c1644cef14058510ae039 [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 Zwislerc2ad2952015-07-10 11:06:13 -060023#include <linux/pmem.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040024#include <linux/io.h>
Dan Williams1cf03c02016-02-17 13:01:23 -080025#include <linux/nd.h>
Dan Williams96601ad2015-08-24 18:29:38 -040026#include <asm/cacheflush.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040027#include "nfit.h"
28
Ross Zwisler047fc8a2015-06-25 04:21:02 -040029/*
30 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
31 * irrelevant.
32 */
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020033#include <linux/io-64-nonatomic-hi-lo.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040034
Dan Williams4d88a972015-05-31 14:41:48 -040035static bool force_enable_dimms;
36module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
37MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
38
Dan Williams1cf03c02016-02-17 13:01:23 -080039static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
40module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
41MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
42
43/* after three payloads of overflow, it's dead jim */
44static unsigned int scrub_overflow_abort = 3;
45module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
46MODULE_PARM_DESC(scrub_overflow_abort,
47 "Number of times we overflow ARS results before abort");
48
Dan Williams87554092016-04-28 18:01:20 -070049static bool disable_vendor_specific;
50module_param(disable_vendor_specific, bool, S_IRUGO);
51MODULE_PARM_DESC(disable_vendor_specific,
52 "Limit commands to the publicly specified set\n");
53
Dan Williams7ae0fa432016-02-19 12:16:34 -080054static struct workqueue_struct *nfit_wq;
55
Vishal Verma20985162015-10-27 16:58:27 -060056struct nfit_table_prev {
57 struct list_head spas;
58 struct list_head memdevs;
59 struct list_head dcrs;
60 struct list_head bdws;
61 struct list_head idts;
62 struct list_head flushes;
63};
64
Dan Williamsb94d5232015-05-19 22:54:31 -040065static u8 nfit_uuid[NFIT_UUID_MAX][16];
66
Dan Williams6bc75612015-06-17 17:23:32 -040067const u8 *to_nfit_uuid(enum nfit_uuids id)
Dan Williamsb94d5232015-05-19 22:54:31 -040068{
69 return nfit_uuid[id];
70}
Dan Williams6bc75612015-06-17 17:23:32 -040071EXPORT_SYMBOL(to_nfit_uuid);
Dan Williamsb94d5232015-05-19 22:54:31 -040072
Dan Williams62232e452015-06-08 14:27:06 -040073static struct acpi_nfit_desc *to_acpi_nfit_desc(
74 struct nvdimm_bus_descriptor *nd_desc)
75{
76 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
77}
78
79static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
80{
81 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
82
83 /*
84 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
85 * acpi_device.
86 */
87 if (!nd_desc->provider_name
88 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
89 return NULL;
90
91 return to_acpi_device(acpi_desc->dev);
92}
93
Dan Williamsaef25332016-02-12 17:01:11 -080094static int xlat_status(void *buf, unsigned int cmd)
95{
Dan Williamsd4f32362016-03-03 16:08:54 -080096 struct nd_cmd_clear_error *clear_err;
Dan Williamsaef25332016-02-12 17:01:11 -080097 struct nd_cmd_ars_status *ars_status;
98 struct nd_cmd_ars_start *ars_start;
99 struct nd_cmd_ars_cap *ars_cap;
100 u16 flags;
101
102 switch (cmd) {
103 case ND_CMD_ARS_CAP:
104 ars_cap = buf;
105 if ((ars_cap->status & 0xffff) == NFIT_ARS_CAP_NONE)
106 return -ENOTTY;
107
108 /* Command failed */
109 if (ars_cap->status & 0xffff)
110 return -EIO;
111
112 /* No supported scan types for this range */
113 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
114 if ((ars_cap->status >> 16 & flags) == 0)
115 return -ENOTTY;
116 break;
117 case ND_CMD_ARS_START:
118 ars_start = buf;
119 /* ARS is in progress */
120 if ((ars_start->status & 0xffff) == NFIT_ARS_START_BUSY)
121 return -EBUSY;
122
123 /* Command failed */
124 if (ars_start->status & 0xffff)
125 return -EIO;
126 break;
127 case ND_CMD_ARS_STATUS:
128 ars_status = buf;
129 /* Command failed */
130 if (ars_status->status & 0xffff)
131 return -EIO;
132 /* Check extended status (Upper two bytes) */
133 if (ars_status->status == NFIT_ARS_STATUS_DONE)
134 return 0;
135
136 /* ARS is in progress */
137 if (ars_status->status == NFIT_ARS_STATUS_BUSY)
138 return -EBUSY;
139
140 /* No ARS performed for the current boot */
141 if (ars_status->status == NFIT_ARS_STATUS_NONE)
142 return -EAGAIN;
143
144 /*
145 * ARS interrupted, either we overflowed or some other
146 * agent wants the scan to stop. If we didn't overflow
147 * then just continue with the returned results.
148 */
149 if (ars_status->status == NFIT_ARS_STATUS_INTR) {
150 if (ars_status->flags & NFIT_ARS_F_OVERFLOW)
151 return -ENOSPC;
152 return 0;
153 }
154
155 /* Unknown status */
156 if (ars_status->status >> 16)
157 return -EIO;
158 break;
Dan Williamsd4f32362016-03-03 16:08:54 -0800159 case ND_CMD_CLEAR_ERROR:
160 clear_err = buf;
161 if (clear_err->status & 0xffff)
162 return -EIO;
163 if (!clear_err->cleared)
164 return -EIO;
165 if (clear_err->length > clear_err->cleared)
166 return clear_err->cleared;
167 break;
Dan Williamsaef25332016-02-12 17:01:11 -0800168 default:
169 break;
170 }
171
172 return 0;
173}
174
Dan Williamsb94d5232015-05-19 22:54:31 -0400175static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
176 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
Dan Williamsaef25332016-02-12 17:01:11 -0800177 unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400178{
Dan Williams62232e452015-06-08 14:27:06 -0400179 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams62232e452015-06-08 14:27:06 -0400180 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700181 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400182 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700183 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400184 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700185 unsigned long cmd_mask, dsm_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400186 acpi_handle handle;
Dan Williams31eca762016-04-28 16:23:43 -0700187 unsigned int func;
Dan Williams62232e452015-06-08 14:27:06 -0400188 const u8 *uuid;
189 u32 offset;
190 int rc, i;
191
Dan Williams31eca762016-04-28 16:23:43 -0700192 func = cmd;
193 if (cmd == ND_CMD_CALL) {
194 call_pkg = buf;
195 func = call_pkg->nd_command;
196 }
197
Dan Williams62232e452015-06-08 14:27:06 -0400198 if (nvdimm) {
199 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
200 struct acpi_device *adev = nfit_mem->adev;
201
202 if (!adev)
203 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700204 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
205 return -ENOTTY;
206
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400207 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400208 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700209 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400210 dsm_mask = nfit_mem->dsm_mask;
211 desc = nd_cmd_dimm_desc(cmd);
Dan Williams31eca762016-04-28 16:23:43 -0700212 uuid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400213 handle = adev->handle;
214 } else {
215 struct acpi_device *adev = to_acpi_dev(acpi_desc);
216
217 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700218 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700219 dsm_mask = cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400220 desc = nd_cmd_bus_desc(cmd);
221 uuid = to_nfit_uuid(NFIT_DEV_BUS);
222 handle = adev->handle;
223 dimm_name = "bus";
224 }
225
226 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
227 return -ENOTTY;
228
Dan Williams31eca762016-04-28 16:23:43 -0700229 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400230 return -ENOTTY;
231
232 in_obj.type = ACPI_TYPE_PACKAGE;
233 in_obj.package.count = 1;
234 in_obj.package.elements = &in_buf;
235 in_buf.type = ACPI_TYPE_BUFFER;
236 in_buf.buffer.pointer = buf;
237 in_buf.buffer.length = 0;
238
239 /* libnvdimm has already validated the input envelope */
240 for (i = 0; i < desc->in_num; i++)
241 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
242 i, buf);
243
Dan Williams31eca762016-04-28 16:23:43 -0700244 if (call_pkg) {
245 /* skip over package wrapper */
246 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
247 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400248 }
249
Dan Williams31eca762016-04-28 16:23:43 -0700250 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
251 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
252 __func__, dimm_name, cmd, func,
253 in_buf.buffer.length);
254 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
255 in_buf.buffer.pointer,
256 min_t(u32, 256, in_buf.buffer.length), true);
257 }
258
259 out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj);
Dan Williams62232e452015-06-08 14:27:06 -0400260 if (!out_obj) {
261 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
262 cmd_name);
263 return -EINVAL;
264 }
265
Dan Williams31eca762016-04-28 16:23:43 -0700266 if (call_pkg) {
267 call_pkg->nd_fw_size = out_obj->buffer.length;
268 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
269 out_obj->buffer.pointer,
270 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
271
272 ACPI_FREE(out_obj);
273 /*
274 * Need to support FW function w/o known size in advance.
275 * Caller can determine required size based upon nd_fw_size.
276 * If we return an error (like elsewhere) then caller wouldn't
277 * be able to rely upon data returned to make calculation.
278 */
279 return 0;
280 }
281
Dan Williams62232e452015-06-08 14:27:06 -0400282 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
283 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
284 __func__, dimm_name, cmd_name, out_obj->type);
285 rc = -EINVAL;
286 goto out;
287 }
288
289 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
290 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
291 dimm_name, cmd_name, out_obj->buffer.length);
292 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
293 4, out_obj->buffer.pointer, min_t(u32, 128,
294 out_obj->buffer.length), true);
295 }
296
297 for (i = 0, offset = 0; i < desc->out_num; i++) {
298 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
299 (u32 *) out_obj->buffer.pointer);
300
301 if (offset + out_size > out_obj->buffer.length) {
302 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
303 __func__, dimm_name, cmd_name, i);
304 break;
305 }
306
307 if (in_buf.buffer.length + offset + out_size > buf_len) {
308 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
309 __func__, dimm_name, cmd_name, i);
310 rc = -ENXIO;
311 goto out;
312 }
313 memcpy(buf + in_buf.buffer.length + offset,
314 out_obj->buffer.pointer + offset, out_size);
315 offset += out_size;
316 }
317 if (offset + in_buf.buffer.length < buf_len) {
318 if (i >= 1) {
319 /*
320 * status valid, return the number of bytes left
321 * unfilled in the output buffer
322 */
323 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800324 if (cmd_rc)
325 *cmd_rc = xlat_status(buf, cmd);
Dan Williams62232e452015-06-08 14:27:06 -0400326 } else {
327 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
328 __func__, dimm_name, cmd_name, buf_len,
329 offset);
330 rc = -ENXIO;
331 }
Dan Williams2eea6582016-05-02 09:11:53 -0700332 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400333 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700334 if (cmd_rc)
335 *cmd_rc = xlat_status(buf, cmd);
336 }
Dan Williams62232e452015-06-08 14:27:06 -0400337
338 out:
339 ACPI_FREE(out_obj);
340
341 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400342}
343
344static const char *spa_type_name(u16 type)
345{
346 static const char *to_name[] = {
347 [NFIT_SPA_VOLATILE] = "volatile",
348 [NFIT_SPA_PM] = "pmem",
349 [NFIT_SPA_DCR] = "dimm-control-region",
350 [NFIT_SPA_BDW] = "block-data-window",
351 [NFIT_SPA_VDISK] = "volatile-disk",
352 [NFIT_SPA_VCD] = "volatile-cd",
353 [NFIT_SPA_PDISK] = "persistent-disk",
354 [NFIT_SPA_PCD] = "persistent-cd",
355
356 };
357
358 if (type > NFIT_SPA_PCD)
359 return "unknown";
360
361 return to_name[type];
362}
363
364static int nfit_spa_type(struct acpi_nfit_system_address *spa)
365{
366 int i;
367
368 for (i = 0; i < NFIT_UUID_MAX; i++)
369 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
370 return i;
371 return -1;
372}
373
374static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600375 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400376 struct acpi_nfit_system_address *spa)
377{
378 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600379 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400380
Dan Williams31932042016-07-14 17:22:48 -0700381 if (spa->header.length != sizeof(*spa))
382 return false;
383
Vishal Verma20985162015-10-27 16:58:27 -0600384 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700385 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600386 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
387 return true;
388 }
389 }
390
Dan Williams31932042016-07-14 17:22:48 -0700391 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
392 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400393 if (!nfit_spa)
394 return false;
395 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700396 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400397 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
398 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
399 spa->range_index,
400 spa_type_name(nfit_spa_type(spa)));
401 return true;
402}
403
404static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600405 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400406 struct acpi_nfit_memory_map *memdev)
407{
408 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600409 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400410
Dan Williams31932042016-07-14 17:22:48 -0700411 if (memdev->header.length != sizeof(*memdev))
412 return false;
413
Vishal Verma20985162015-10-27 16:58:27 -0600414 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700415 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600416 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
417 return true;
418 }
419
Dan Williams31932042016-07-14 17:22:48 -0700420 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
421 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400422 if (!nfit_memdev)
423 return false;
424 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700425 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400426 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
427 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
428 __func__, memdev->device_handle, memdev->range_index,
429 memdev->region_index);
430 return true;
431}
432
Dan Williams31932042016-07-14 17:22:48 -0700433/*
434 * An implementation may provide a truncated control region if no block windows
435 * are defined.
436 */
437static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
438{
439 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
440 window_size))
441 return 0;
442 if (dcr->windows)
443 return sizeof(*dcr);
444 return offsetof(struct acpi_nfit_control_region, window_size);
445}
446
Dan Williamsb94d5232015-05-19 22:54:31 -0400447static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600448 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400449 struct acpi_nfit_control_region *dcr)
450{
451 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600452 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400453
Dan Williams31932042016-07-14 17:22:48 -0700454 if (!sizeof_dcr(dcr))
455 return false;
456
Vishal Verma20985162015-10-27 16:58:27 -0600457 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700458 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600459 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
460 return true;
461 }
462
Dan Williams31932042016-07-14 17:22:48 -0700463 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
464 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400465 if (!nfit_dcr)
466 return false;
467 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700468 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400469 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
470 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
471 dcr->region_index, dcr->windows);
472 return true;
473}
474
475static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600476 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400477 struct acpi_nfit_data_region *bdw)
478{
479 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600480 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400481
Dan Williams31932042016-07-14 17:22:48 -0700482 if (bdw->header.length != sizeof(*bdw))
483 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600484 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700485 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600486 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
487 return true;
488 }
489
Dan Williams31932042016-07-14 17:22:48 -0700490 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
491 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400492 if (!nfit_bdw)
493 return false;
494 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700495 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400496 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
497 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
498 bdw->region_index, bdw->windows);
499 return true;
500}
501
Dan Williams31932042016-07-14 17:22:48 -0700502static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
503{
504 if (idt->header.length < sizeof(*idt))
505 return 0;
506 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
507}
508
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400509static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600510 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400511 struct acpi_nfit_interleave *idt)
512{
513 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600514 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400515
Dan Williams31932042016-07-14 17:22:48 -0700516 if (!sizeof_idt(idt))
517 return false;
518
519 list_for_each_entry(nfit_idt, &prev->idts, list) {
520 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
521 continue;
522
523 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600524 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
525 return true;
526 }
Dan Williams31932042016-07-14 17:22:48 -0700527 }
Vishal Verma20985162015-10-27 16:58:27 -0600528
Dan Williams31932042016-07-14 17:22:48 -0700529 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
530 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400531 if (!nfit_idt)
532 return false;
533 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700534 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400535 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
536 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
537 idt->interleave_index, idt->line_count);
538 return true;
539}
540
Dan Williams31932042016-07-14 17:22:48 -0700541static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
542{
543 if (flush->header.length < sizeof(*flush))
544 return 0;
545 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
546}
547
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600548static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600549 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600550 struct acpi_nfit_flush_address *flush)
551{
552 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600553 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600554
Dan Williams31932042016-07-14 17:22:48 -0700555 if (!sizeof_flush(flush))
556 return false;
557
558 list_for_each_entry(nfit_flush, &prev->flushes, list) {
559 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
560 continue;
561
562 if (memcmp(nfit_flush->flush, flush,
563 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600564 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
565 return true;
566 }
Dan Williams31932042016-07-14 17:22:48 -0700567 }
Vishal Verma20985162015-10-27 16:58:27 -0600568
Dan Williams31932042016-07-14 17:22:48 -0700569 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
570 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600571 if (!nfit_flush)
572 return false;
573 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700574 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600575 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
576 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
577 flush->device_handle, flush->hint_count);
578 return true;
579}
580
Vishal Verma20985162015-10-27 16:58:27 -0600581static void *add_table(struct acpi_nfit_desc *acpi_desc,
582 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400583{
584 struct device *dev = acpi_desc->dev;
585 struct acpi_nfit_header *hdr;
586 void *err = ERR_PTR(-ENOMEM);
587
588 if (table >= end)
589 return NULL;
590
591 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600592 if (!hdr->length) {
593 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
594 hdr->type);
595 return NULL;
596 }
597
Dan Williamsb94d5232015-05-19 22:54:31 -0400598 switch (hdr->type) {
599 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600600 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400601 return err;
602 break;
603 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600604 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400605 return err;
606 break;
607 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600608 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400609 return err;
610 break;
611 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600612 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400613 return err;
614 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400615 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600616 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400617 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400618 break;
619 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600620 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600621 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400622 break;
623 case ACPI_NFIT_TYPE_SMBIOS:
624 dev_dbg(dev, "%s: smbios\n", __func__);
625 break;
626 default:
627 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
628 break;
629 }
630
631 return table + hdr->length;
632}
633
634static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
635 struct nfit_mem *nfit_mem)
636{
637 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
638 u16 dcr = nfit_mem->dcr->region_index;
639 struct nfit_spa *nfit_spa;
640
641 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
642 u16 range_index = nfit_spa->spa->range_index;
643 int type = nfit_spa_type(nfit_spa->spa);
644 struct nfit_memdev *nfit_memdev;
645
646 if (type != NFIT_SPA_BDW)
647 continue;
648
649 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
650 if (nfit_memdev->memdev->range_index != range_index)
651 continue;
652 if (nfit_memdev->memdev->device_handle != device_handle)
653 continue;
654 if (nfit_memdev->memdev->region_index != dcr)
655 continue;
656
657 nfit_mem->spa_bdw = nfit_spa->spa;
658 return;
659 }
660 }
661
662 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
663 nfit_mem->spa_dcr->range_index);
664 nfit_mem->bdw = NULL;
665}
666
Dan Williams6697b2c2016-02-04 16:51:00 -0800667static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400668 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
669{
670 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400671 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400672 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400673 struct nfit_idt *nfit_idt;
674 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400675
Dan Williamsb94d5232015-05-19 22:54:31 -0400676 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
677 if (nfit_bdw->bdw->region_index != dcr)
678 continue;
679 nfit_mem->bdw = nfit_bdw->bdw;
680 break;
681 }
682
683 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800684 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400685
686 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400687
688 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800689 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400690
691 range_index = nfit_mem->spa_bdw->range_index;
692 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
693 if (nfit_memdev->memdev->range_index != range_index ||
694 nfit_memdev->memdev->region_index != dcr)
695 continue;
696 nfit_mem->memdev_bdw = nfit_memdev->memdev;
697 idt_idx = nfit_memdev->memdev->interleave_index;
698 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
699 if (nfit_idt->idt->interleave_index != idt_idx)
700 continue;
701 nfit_mem->idt_bdw = nfit_idt->idt;
702 break;
703 }
704 break;
705 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400706}
707
708static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
709 struct acpi_nfit_system_address *spa)
710{
711 struct nfit_mem *nfit_mem, *found;
712 struct nfit_memdev *nfit_memdev;
713 int type = nfit_spa_type(spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400714
715 switch (type) {
716 case NFIT_SPA_DCR:
717 case NFIT_SPA_PM:
718 break;
719 default:
720 return 0;
721 }
722
723 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700724 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800725 struct nfit_dcr *nfit_dcr;
726 u32 device_handle;
727 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400728
729 if (nfit_memdev->memdev->range_index != spa->range_index)
730 continue;
731 found = NULL;
732 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800733 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400734 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800735 if (__to_nfit_memdev(nfit_mem)->device_handle
736 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400737 found = nfit_mem;
738 break;
739 }
740
741 if (found)
742 nfit_mem = found;
743 else {
744 nfit_mem = devm_kzalloc(acpi_desc->dev,
745 sizeof(*nfit_mem), GFP_KERNEL);
746 if (!nfit_mem)
747 return -ENOMEM;
748 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700749 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800750 list_add(&nfit_mem->list, &acpi_desc->dimms);
751 }
752
753 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
754 if (nfit_dcr->dcr->region_index != dcr)
755 continue;
756 /*
757 * Record the control region for the dimm. For
758 * the ACPI 6.1 case, where there are separate
759 * control regions for the pmem vs blk
760 * interfaces, be sure to record the extended
761 * blk details.
762 */
763 if (!nfit_mem->dcr)
764 nfit_mem->dcr = nfit_dcr->dcr;
765 else if (nfit_mem->dcr->windows == 0
766 && nfit_dcr->dcr->windows)
767 nfit_mem->dcr = nfit_dcr->dcr;
768 break;
769 }
770
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700771 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700772 struct acpi_nfit_flush_address *flush;
773 u16 i;
774
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700775 if (nfit_flush->flush->device_handle != device_handle)
776 continue;
777 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700778 flush = nfit_flush->flush;
779 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
780 flush->hint_count
781 * sizeof(struct resource), GFP_KERNEL);
782 if (!nfit_mem->flush_wpq)
783 return -ENOMEM;
784 for (i = 0; i < flush->hint_count; i++) {
785 struct resource *res = &nfit_mem->flush_wpq[i];
786
787 res->start = flush->hint_address[i];
788 res->end = res->start + 8 - 1;
789 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700790 break;
791 }
792
Dan Williams6697b2c2016-02-04 16:51:00 -0800793 if (dcr && !nfit_mem->dcr) {
794 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
795 spa->range_index, dcr);
796 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400797 }
798
799 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400800 struct nfit_idt *nfit_idt;
801 u16 idt_idx;
802
Dan Williamsb94d5232015-05-19 22:54:31 -0400803 /* multiple dimms may share a SPA when interleaved */
804 nfit_mem->spa_dcr = spa;
805 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400806 idt_idx = nfit_memdev->memdev->interleave_index;
807 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
808 if (nfit_idt->idt->interleave_index != idt_idx)
809 continue;
810 nfit_mem->idt_dcr = nfit_idt->idt;
811 break;
812 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800813 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400814 } else {
815 /*
816 * A single dimm may belong to multiple SPA-PM
817 * ranges, record at least one in addition to
818 * any SPA-DCR range.
819 */
820 nfit_mem->memdev_pmem = nfit_memdev->memdev;
821 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400822 }
823
824 return 0;
825}
826
827static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
828{
829 struct nfit_mem *a = container_of(_a, typeof(*a), list);
830 struct nfit_mem *b = container_of(_b, typeof(*b), list);
831 u32 handleA, handleB;
832
833 handleA = __to_nfit_memdev(a)->device_handle;
834 handleB = __to_nfit_memdev(b)->device_handle;
835 if (handleA < handleB)
836 return -1;
837 else if (handleA > handleB)
838 return 1;
839 return 0;
840}
841
842static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
843{
844 struct nfit_spa *nfit_spa;
845
846 /*
847 * For each SPA-DCR or SPA-PMEM address range find its
848 * corresponding MEMDEV(s). From each MEMDEV find the
849 * corresponding DCR. Then, if we're operating on a SPA-DCR,
850 * try to find a SPA-BDW and a corresponding BDW that references
851 * the DCR. Throw it all into an nfit_mem object. Note, that
852 * BDWs are optional.
853 */
854 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
855 int rc;
856
857 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
858 if (rc)
859 return rc;
860 }
861
862 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
863
864 return 0;
865}
866
Dan Williams45def222015-04-26 19:26:48 -0400867static ssize_t revision_show(struct device *dev,
868 struct device_attribute *attr, char *buf)
869{
870 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
871 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
872 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
873
Linda Knippers6b577c92015-11-20 19:05:49 -0500874 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400875}
876static DEVICE_ATTR_RO(revision);
877
Vishal Verma37b137f2016-07-23 21:51:42 -0700878/*
879 * This shows the number of full Address Range Scrubs that have been
880 * completed since driver load time. Userspace can wait on this using
881 * select/poll etc. A '+' at the end indicates an ARS is in progress
882 */
883static ssize_t scrub_show(struct device *dev,
884 struct device_attribute *attr, char *buf)
885{
886 struct nvdimm_bus_descriptor *nd_desc;
887 ssize_t rc = -ENXIO;
888
889 device_lock(dev);
890 nd_desc = dev_get_drvdata(dev);
891 if (nd_desc) {
892 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
893
894 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
895 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
896 }
897 device_unlock(dev);
898 return rc;
899}
900
901static int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc);
902
903static ssize_t scrub_store(struct device *dev,
904 struct device_attribute *attr, const char *buf, size_t size)
905{
906 struct nvdimm_bus_descriptor *nd_desc;
907 ssize_t rc;
908 long val;
909
910 rc = kstrtol(buf, 0, &val);
911 if (rc)
912 return rc;
913 if (val != 1)
914 return -EINVAL;
915
916 device_lock(dev);
917 nd_desc = dev_get_drvdata(dev);
918 if (nd_desc) {
919 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
920
921 rc = acpi_nfit_ars_rescan(acpi_desc);
922 }
923 device_unlock(dev);
924 if (rc)
925 return rc;
926 return size;
927}
928static DEVICE_ATTR_RW(scrub);
929
930static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
931{
932 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
933 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
934 | 1 << ND_CMD_ARS_STATUS;
935
936 return (nd_desc->cmd_mask & mask) == mask;
937}
938
939static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
940{
941 struct device *dev = container_of(kobj, struct device, kobj);
942 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
943
944 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
945 return 0;
946 return a->mode;
947}
948
Dan Williams45def222015-04-26 19:26:48 -0400949static struct attribute *acpi_nfit_attributes[] = {
950 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -0700951 &dev_attr_scrub.attr,
Dan Williams45def222015-04-26 19:26:48 -0400952 NULL,
953};
954
955static struct attribute_group acpi_nfit_attribute_group = {
956 .name = "nfit",
957 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -0700958 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -0400959};
960
Dan Williamsa61fe6f2016-02-19 12:29:32 -0800961static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -0400962 &nvdimm_bus_attribute_group,
963 &acpi_nfit_attribute_group,
964 NULL,
965};
966
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400967static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
968{
969 struct nvdimm *nvdimm = to_nvdimm(dev);
970 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
971
972 return __to_nfit_memdev(nfit_mem);
973}
974
975static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
976{
977 struct nvdimm *nvdimm = to_nvdimm(dev);
978 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
979
980 return nfit_mem->dcr;
981}
982
983static ssize_t handle_show(struct device *dev,
984 struct device_attribute *attr, char *buf)
985{
986 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
987
988 return sprintf(buf, "%#x\n", memdev->device_handle);
989}
990static DEVICE_ATTR_RO(handle);
991
992static ssize_t phys_id_show(struct device *dev,
993 struct device_attribute *attr, char *buf)
994{
995 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
996
997 return sprintf(buf, "%#x\n", memdev->physical_id);
998}
999static DEVICE_ATTR_RO(phys_id);
1000
1001static ssize_t vendor_show(struct device *dev,
1002 struct device_attribute *attr, char *buf)
1003{
1004 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1005
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001006 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001007}
1008static DEVICE_ATTR_RO(vendor);
1009
1010static ssize_t rev_id_show(struct device *dev,
1011 struct device_attribute *attr, char *buf)
1012{
1013 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1014
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001015 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001016}
1017static DEVICE_ATTR_RO(rev_id);
1018
1019static ssize_t device_show(struct device *dev,
1020 struct device_attribute *attr, char *buf)
1021{
1022 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1023
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001024 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001025}
1026static DEVICE_ATTR_RO(device);
1027
Dan Williams6ca72082016-04-29 10:33:23 -07001028static ssize_t subsystem_vendor_show(struct device *dev,
1029 struct device_attribute *attr, char *buf)
1030{
1031 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1032
1033 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1034}
1035static DEVICE_ATTR_RO(subsystem_vendor);
1036
1037static ssize_t subsystem_rev_id_show(struct device *dev,
1038 struct device_attribute *attr, char *buf)
1039{
1040 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1041
1042 return sprintf(buf, "0x%04x\n",
1043 be16_to_cpu(dcr->subsystem_revision_id));
1044}
1045static DEVICE_ATTR_RO(subsystem_rev_id);
1046
1047static ssize_t subsystem_device_show(struct device *dev,
1048 struct device_attribute *attr, char *buf)
1049{
1050 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1051
1052 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1053}
1054static DEVICE_ATTR_RO(subsystem_device);
1055
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001056static int num_nvdimm_formats(struct nvdimm *nvdimm)
1057{
1058 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1059 int formats = 0;
1060
1061 if (nfit_mem->memdev_pmem)
1062 formats++;
1063 if (nfit_mem->memdev_bdw)
1064 formats++;
1065 return formats;
1066}
1067
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001068static ssize_t format_show(struct device *dev,
1069 struct device_attribute *attr, char *buf)
1070{
1071 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1072
Dan Williams1b982ba2016-05-18 10:07:19 -07001073 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001074}
1075static DEVICE_ATTR_RO(format);
1076
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001077static ssize_t format1_show(struct device *dev,
1078 struct device_attribute *attr, char *buf)
1079{
1080 u32 handle;
1081 ssize_t rc = -ENXIO;
1082 struct nfit_mem *nfit_mem;
1083 struct nfit_memdev *nfit_memdev;
1084 struct acpi_nfit_desc *acpi_desc;
1085 struct nvdimm *nvdimm = to_nvdimm(dev);
1086 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1087
1088 nfit_mem = nvdimm_provider_data(nvdimm);
1089 acpi_desc = nfit_mem->acpi_desc;
1090 handle = to_nfit_memdev(dev)->device_handle;
1091
1092 /* assumes DIMMs have at most 2 published interface codes */
1093 mutex_lock(&acpi_desc->init_mutex);
1094 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1095 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1096 struct nfit_dcr *nfit_dcr;
1097
1098 if (memdev->device_handle != handle)
1099 continue;
1100
1101 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1102 if (nfit_dcr->dcr->region_index != memdev->region_index)
1103 continue;
1104 if (nfit_dcr->dcr->code == dcr->code)
1105 continue;
Dan Williams1b982ba2016-05-18 10:07:19 -07001106 rc = sprintf(buf, "%#x\n",
1107 be16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001108 break;
1109 }
1110 if (rc != ENXIO)
1111 break;
1112 }
1113 mutex_unlock(&acpi_desc->init_mutex);
1114 return rc;
1115}
1116static DEVICE_ATTR_RO(format1);
1117
1118static ssize_t formats_show(struct device *dev,
1119 struct device_attribute *attr, char *buf)
1120{
1121 struct nvdimm *nvdimm = to_nvdimm(dev);
1122
1123 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1124}
1125static DEVICE_ATTR_RO(formats);
1126
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001127static ssize_t serial_show(struct device *dev,
1128 struct device_attribute *attr, char *buf)
1129{
1130 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1131
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001132 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001133}
1134static DEVICE_ATTR_RO(serial);
1135
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001136static ssize_t family_show(struct device *dev,
1137 struct device_attribute *attr, char *buf)
1138{
1139 struct nvdimm *nvdimm = to_nvdimm(dev);
1140 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1141
1142 if (nfit_mem->family < 0)
1143 return -ENXIO;
1144 return sprintf(buf, "%d\n", nfit_mem->family);
1145}
1146static DEVICE_ATTR_RO(family);
1147
1148static ssize_t dsm_mask_show(struct device *dev,
1149 struct device_attribute *attr, char *buf)
1150{
1151 struct nvdimm *nvdimm = to_nvdimm(dev);
1152 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1153
1154 if (nfit_mem->family < 0)
1155 return -ENXIO;
1156 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1157}
1158static DEVICE_ATTR_RO(dsm_mask);
1159
Dan Williams58138822015-06-23 20:08:34 -04001160static ssize_t flags_show(struct device *dev,
1161 struct device_attribute *attr, char *buf)
1162{
1163 u16 flags = to_nfit_memdev(dev)->flags;
1164
1165 return sprintf(buf, "%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001166 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1167 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1168 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001169 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Toshi Kani402bae52015-08-26 10:20:23 -06001170 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "");
Dan Williams58138822015-06-23 20:08:34 -04001171}
1172static DEVICE_ATTR_RO(flags);
1173
Toshi Kani38a879b2016-04-25 15:34:59 -06001174static ssize_t id_show(struct device *dev,
1175 struct device_attribute *attr, char *buf)
1176{
1177 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1178
1179 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1180 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1181 be16_to_cpu(dcr->vendor_id),
1182 dcr->manufacturing_location,
1183 be16_to_cpu(dcr->manufacturing_date),
1184 be32_to_cpu(dcr->serial_number));
1185 else
1186 return sprintf(buf, "%04x-%08x\n",
1187 be16_to_cpu(dcr->vendor_id),
1188 be32_to_cpu(dcr->serial_number));
1189}
1190static DEVICE_ATTR_RO(id);
1191
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001192static struct attribute *acpi_nfit_dimm_attributes[] = {
1193 &dev_attr_handle.attr,
1194 &dev_attr_phys_id.attr,
1195 &dev_attr_vendor.attr,
1196 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001197 &dev_attr_rev_id.attr,
1198 &dev_attr_subsystem_vendor.attr,
1199 &dev_attr_subsystem_device.attr,
1200 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001201 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001202 &dev_attr_formats.attr,
1203 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001204 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001205 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001206 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001207 &dev_attr_family.attr,
1208 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001209 NULL,
1210};
1211
1212static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1213 struct attribute *a, int n)
1214{
1215 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001216 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001217
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001218 if (!to_nfit_dcr(dev))
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001219 return 0;
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001220 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1221 return 0;
1222 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001223}
1224
1225static struct attribute_group acpi_nfit_dimm_attribute_group = {
1226 .name = "nfit",
1227 .attrs = acpi_nfit_dimm_attributes,
1228 .is_visible = acpi_nfit_dimm_attr_visible,
1229};
1230
1231static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001232 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001233 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001234 &acpi_nfit_dimm_attribute_group,
1235 NULL,
1236};
1237
1238static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1239 u32 device_handle)
1240{
1241 struct nfit_mem *nfit_mem;
1242
1243 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1244 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1245 return nfit_mem->nvdimm;
1246
1247 return NULL;
1248}
1249
Dan Williams62232e452015-06-08 14:27:06 -04001250static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1251 struct nfit_mem *nfit_mem, u32 device_handle)
1252{
1253 struct acpi_device *adev, *adev_dimm;
1254 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001255 unsigned long dsm_mask;
1256 const u8 *uuid;
Linda Knippers60e95f432015-07-22 16:17:22 -04001257 int i;
Dan Williams62232e452015-06-08 14:27:06 -04001258
Dan Williamse3654ec2016-04-28 16:17:07 -07001259 /* nfit test assumes 1:1 relationship between commands and dsms */
1260 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001261 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001262 adev = to_acpi_dev(acpi_desc);
1263 if (!adev)
1264 return 0;
1265
1266 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1267 nfit_mem->adev = adev_dimm;
1268 if (!adev_dimm) {
1269 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1270 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001271 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001272 }
1273
Dan Williams31eca762016-04-28 16:23:43 -07001274 /*
stuart hayese02fb722016-05-26 11:38:41 -05001275 * Until standardization materializes we need to consider 4
Dan Williams31eca762016-04-28 16:23:43 -07001276 * different command sets. Note, that checking for function0 (bit0)
1277 * tells us if any commands are reachable through this uuid.
1278 */
stuart hayese02fb722016-05-26 11:38:41 -05001279 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williams31eca762016-04-28 16:23:43 -07001280 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1281 break;
1282
1283 /* limit the supported commands to those that are publicly documented */
1284 nfit_mem->family = i;
Dan Williams87554092016-04-28 18:01:20 -07001285 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001286 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001287 if (disable_vendor_specific)
1288 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001289 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001290 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001291 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001292 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001293 if (disable_vendor_specific)
1294 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001295 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1296 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001297 } else {
Dan Williams31eca762016-04-28 16:23:43 -07001298 dev_err(dev, "unknown dimm command family\n");
1299 nfit_mem->family = -1;
1300 return force_enable_dimms ? 0 : -ENODEV;
1301 }
1302
1303 uuid = to_nfit_uuid(nfit_mem->family);
1304 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Dan Williams62232e452015-06-08 14:27:06 -04001305 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
1306 set_bit(i, &nfit_mem->dsm_mask);
1307
Linda Knippers60e95f432015-07-22 16:17:22 -04001308 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001309}
1310
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001311static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1312{
1313 struct nfit_mem *nfit_mem;
Dan Williams4d88a972015-05-31 14:41:48 -04001314 int dimm_count = 0;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001315
1316 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001317 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001318 unsigned long flags = 0, cmd_mask;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001319 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001320 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001321 u16 mem_flags;
Dan Williams62232e452015-06-08 14:27:06 -04001322 int rc;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001323
1324 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1325 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1326 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001327 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001328 continue;
1329 }
1330
1331 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1332 flags |= NDD_ALIASING;
1333
Dan Williams58138822015-06-23 20:08:34 -04001334 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001335 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams58138822015-06-23 20:08:34 -04001336 flags |= NDD_UNARMED;
1337
Dan Williams62232e452015-06-08 14:27:06 -04001338 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1339 if (rc)
1340 continue;
1341
Dan Williamse3654ec2016-04-28 16:17:07 -07001342 /*
Dan Williams31eca762016-04-28 16:23:43 -07001343 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1344 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1345 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001346 */
Dan Williams31eca762016-04-28 16:23:43 -07001347 cmd_mask = 1UL << ND_CMD_CALL;
1348 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1349 cmd_mask |= nfit_mem->dsm_mask;
1350
Dan Williamse5ae3b22016-06-07 17:00:04 -07001351 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1352 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001353 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001354 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001355 flags, cmd_mask, flush ? flush->hint_count : 0,
1356 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001357 if (!nvdimm)
1358 return -ENOMEM;
1359
1360 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001361 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001362
1363 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1364 continue;
1365
Toshi Kani402bae52015-08-26 10:20:23 -06001366 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001367 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001368 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1369 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1370 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001371 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
Dan Williams58138822015-06-23 20:08:34 -04001372
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001373 }
1374
Dan Williams4d88a972015-05-31 14:41:48 -04001375 return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001376}
1377
Dan Williams62232e452015-06-08 14:27:06 -04001378static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1379{
1380 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1381 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1382 struct acpi_device *adev;
1383 int i;
1384
Dan Williamse3654ec2016-04-28 16:17:07 -07001385 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001386 adev = to_acpi_dev(acpi_desc);
1387 if (!adev)
1388 return;
1389
Dan Williamsd4f32362016-03-03 16:08:54 -08001390 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Dan Williams62232e452015-06-08 14:27:06 -04001391 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001392 set_bit(i, &nd_desc->cmd_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001393}
1394
Dan Williams1f7df6f2015-06-09 20:13:14 -04001395static ssize_t range_index_show(struct device *dev,
1396 struct device_attribute *attr, char *buf)
1397{
1398 struct nd_region *nd_region = to_nd_region(dev);
1399 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1400
1401 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1402}
1403static DEVICE_ATTR_RO(range_index);
1404
1405static struct attribute *acpi_nfit_region_attributes[] = {
1406 &dev_attr_range_index.attr,
1407 NULL,
1408};
1409
1410static struct attribute_group acpi_nfit_region_attribute_group = {
1411 .name = "nfit",
1412 .attrs = acpi_nfit_region_attributes,
1413};
1414
1415static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1416 &nd_region_attribute_group,
1417 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001418 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001419 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001420 &acpi_nfit_region_attribute_group,
1421 NULL,
1422};
1423
Dan Williamseaf96152015-05-01 13:11:27 -04001424/* enough info to uniquely specify an interleave set */
1425struct nfit_set_info {
1426 struct nfit_set_info_map {
1427 u64 region_offset;
1428 u32 serial_number;
1429 u32 pad;
1430 } mapping[0];
1431};
1432
1433static size_t sizeof_nfit_set_info(int num_mappings)
1434{
1435 return sizeof(struct nfit_set_info)
1436 + num_mappings * sizeof(struct nfit_set_info_map);
1437}
1438
1439static int cmp_map(const void *m0, const void *m1)
1440{
1441 const struct nfit_set_info_map *map0 = m0;
1442 const struct nfit_set_info_map *map1 = m1;
1443
1444 return memcmp(&map0->region_offset, &map1->region_offset,
1445 sizeof(u64));
1446}
1447
1448/* Retrieve the nth entry referencing this spa */
1449static struct acpi_nfit_memory_map *memdev_from_spa(
1450 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1451{
1452 struct nfit_memdev *nfit_memdev;
1453
1454 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1455 if (nfit_memdev->memdev->range_index == range_index)
1456 if (n-- == 0)
1457 return nfit_memdev->memdev;
1458 return NULL;
1459}
1460
1461static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1462 struct nd_region_desc *ndr_desc,
1463 struct acpi_nfit_system_address *spa)
1464{
1465 int i, spa_type = nfit_spa_type(spa);
1466 struct device *dev = acpi_desc->dev;
1467 struct nd_interleave_set *nd_set;
1468 u16 nr = ndr_desc->num_mappings;
1469 struct nfit_set_info *info;
1470
1471 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1472 /* pass */;
1473 else
1474 return 0;
1475
1476 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1477 if (!nd_set)
1478 return -ENOMEM;
1479
1480 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1481 if (!info)
1482 return -ENOMEM;
1483 for (i = 0; i < nr; i++) {
1484 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
1485 struct nfit_set_info_map *map = &info->mapping[i];
1486 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1487 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1488 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1489 spa->range_index, i);
1490
1491 if (!memdev || !nfit_mem->dcr) {
1492 dev_err(dev, "%s: failed to find DCR\n", __func__);
1493 return -ENODEV;
1494 }
1495
1496 map->region_offset = memdev->region_offset;
1497 map->serial_number = nfit_mem->dcr->serial_number;
1498 }
1499
1500 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1501 cmp_map, NULL);
1502 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1503 ndr_desc->nd_set = nd_set;
1504 devm_kfree(dev, info);
1505
1506 return 0;
1507}
1508
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001509static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1510{
1511 struct acpi_nfit_interleave *idt = mmio->idt;
1512 u32 sub_line_offset, line_index, line_offset;
1513 u64 line_no, table_skip_count, table_offset;
1514
1515 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1516 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1517 line_offset = idt->line_offset[line_index]
1518 * mmio->line_size;
1519 table_offset = table_skip_count * mmio->table_size;
1520
1521 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1522}
1523
Ross Zwislerde4a1962015-08-20 16:27:38 -06001524static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001525{
1526 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1527 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
1528
1529 if (mmio->num_lines)
1530 offset = to_interleave_offset(offset, mmio);
1531
Linus Torvalds12f03ee2015-09-08 14:35:59 -07001532 return readl(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001533}
1534
1535static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1536 resource_size_t dpa, unsigned int len, unsigned int write)
1537{
1538 u64 cmd, offset;
1539 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1540
1541 enum {
1542 BCW_OFFSET_MASK = (1ULL << 48)-1,
1543 BCW_LEN_SHIFT = 48,
1544 BCW_LEN_MASK = (1ULL << 8) - 1,
1545 BCW_CMD_SHIFT = 56,
1546 };
1547
1548 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1549 len = len >> L1_CACHE_SHIFT;
1550 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1551 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1552
1553 offset = nfit_blk->cmd_offset + mmio->size * bw;
1554 if (mmio->num_lines)
1555 offset = to_interleave_offset(offset, mmio);
1556
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001557 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001558 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001559
Dan Williamsaef25332016-02-12 17:01:11 -08001560 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001561 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001562}
1563
1564static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1565 resource_size_t dpa, void *iobuf, size_t len, int rw,
1566 unsigned int lane)
1567{
1568 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1569 unsigned int copied = 0;
1570 u64 base_offset;
1571 int rc;
1572
1573 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1574 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001575 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1576 while (len) {
1577 unsigned int c;
1578 u64 offset;
1579
1580 if (mmio->num_lines) {
1581 u32 line_offset;
1582
1583 offset = to_interleave_offset(base_offset + copied,
1584 mmio);
1585 div_u64_rem(offset, mmio->line_size, &line_offset);
1586 c = min_t(size_t, len, mmio->line_size - line_offset);
1587 } else {
1588 offset = base_offset + nfit_blk->bdw_offset;
1589 c = len;
1590 }
1591
1592 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001593 memcpy_to_pmem(mmio->addr.aperture + offset,
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001594 iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001595 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001596 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001597 mmio_flush_range((void __force *)
1598 mmio->addr.aperture + offset, c);
1599
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001600 memcpy_from_pmem(iobuf + copied,
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001601 mmio->addr.aperture + offset, c);
1602 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001603
1604 copied += c;
1605 len -= c;
1606 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001607
1608 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001609 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001610
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001611 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1612 return rc;
1613}
1614
1615static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1616 resource_size_t dpa, void *iobuf, u64 len, int rw)
1617{
1618 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1619 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1620 struct nd_region *nd_region = nfit_blk->nd_region;
1621 unsigned int lane, copied = 0;
1622 int rc = 0;
1623
1624 lane = nd_region_acquire_lane(nd_region);
1625 while (len) {
1626 u64 c = min(len, mmio->size);
1627
1628 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1629 iobuf + copied, c, rw, lane);
1630 if (rc)
1631 break;
1632
1633 copied += c;
1634 len -= c;
1635 }
1636 nd_region_release_lane(nd_region, lane);
1637
1638 return rc;
1639}
1640
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001641static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1642 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1643{
1644 if (idt) {
1645 mmio->num_lines = idt->line_count;
1646 mmio->line_size = idt->line_size;
1647 if (interleave_ways == 0)
1648 return -ENXIO;
1649 mmio->table_size = mmio->num_lines * interleave_ways
1650 * mmio->line_size;
1651 }
1652
1653 return 0;
1654}
1655
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001656static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1657 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1658{
1659 struct nd_cmd_dimm_flags flags;
1660 int rc;
1661
1662 memset(&flags, 0, sizeof(flags));
1663 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08001664 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001665
1666 if (rc >= 0 && flags.status == 0)
1667 nfit_blk->dimm_flags = flags.flags;
1668 else if (rc == -ENOTTY) {
1669 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08001670 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001671 rc = 0;
1672 } else
1673 rc = -ENXIO;
1674
1675 return rc;
1676}
1677
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001678static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1679 struct device *dev)
1680{
1681 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001682 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1683 struct nfit_blk_mmio *mmio;
1684 struct nfit_blk *nfit_blk;
1685 struct nfit_mem *nfit_mem;
1686 struct nvdimm *nvdimm;
1687 int rc;
1688
1689 nvdimm = nd_blk_region_to_dimm(ndbr);
1690 nfit_mem = nvdimm_provider_data(nvdimm);
1691 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1692 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1693 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04001694 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1695 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001696 return -ENXIO;
1697 }
1698
1699 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1700 if (!nfit_blk)
1701 return -ENOMEM;
1702 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1703 nfit_blk->nd_region = to_nd_region(dev);
1704
1705 /* map block aperture memory */
1706 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1707 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07001708 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
1709 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001710 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001711 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1712 nvdimm_name(nvdimm));
1713 return -ENOMEM;
1714 }
1715 mmio->size = nfit_mem->bdw->size;
1716 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1717 mmio->idt = nfit_mem->idt_bdw;
1718 mmio->spa = nfit_mem->spa_bdw;
1719 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1720 nfit_mem->memdev_bdw->interleave_ways);
1721 if (rc) {
1722 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1723 __func__, nvdimm_name(nvdimm));
1724 return rc;
1725 }
1726
1727 /* map block control memory */
1728 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1729 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1730 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07001731 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1732 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001733 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001734 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1735 nvdimm_name(nvdimm));
1736 return -ENOMEM;
1737 }
1738 mmio->size = nfit_mem->dcr->window_size;
1739 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1740 mmio->idt = nfit_mem->idt_dcr;
1741 mmio->spa = nfit_mem->spa_dcr;
1742 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1743 nfit_mem->memdev_dcr->interleave_ways);
1744 if (rc) {
1745 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1746 __func__, nvdimm_name(nvdimm));
1747 return rc;
1748 }
1749
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001750 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1751 if (rc < 0) {
1752 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1753 __func__, nvdimm_name(nvdimm));
1754 return rc;
1755 }
1756
Dan Williamsf284a4f2016-07-07 19:44:50 -07001757 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001758 dev_warn(dev, "unable to guarantee persistence of writes\n");
1759
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001760 if (mmio->line_size == 0)
1761 return 0;
1762
1763 if ((u32) nfit_blk->cmd_offset % mmio->line_size
1764 + 8 > mmio->line_size) {
1765 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1766 return -ENXIO;
1767 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1768 + 8 > mmio->line_size) {
1769 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
1770 return -ENXIO;
1771 }
1772
1773 return 0;
1774}
1775
Dan Williamsaef25332016-02-12 17:01:11 -08001776static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08001777 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001778{
Dan Williamsaef25332016-02-12 17:01:11 -08001779 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001780 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08001781 int cmd_rc, rc;
1782
Dan Williams1cf03c02016-02-17 13:01:23 -08001783 cmd->address = spa->address;
1784 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08001785 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
1786 sizeof(*cmd), &cmd_rc);
1787 if (rc < 0)
1788 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001789 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001790}
1791
Dan Williams1cf03c02016-02-17 13:01:23 -08001792static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001793{
1794 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001795 int cmd_rc;
1796 struct nd_cmd_ars_start ars_start;
1797 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1798 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001799
Dan Williams1cf03c02016-02-17 13:01:23 -08001800 memset(&ars_start, 0, sizeof(ars_start));
1801 ars_start.address = spa->address;
1802 ars_start.length = spa->length;
1803 if (nfit_spa_type(spa) == NFIT_SPA_PM)
1804 ars_start.type = ND_ARS_PERSISTENT;
1805 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
1806 ars_start.type = ND_ARS_VOLATILE;
1807 else
1808 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07001809
Dan Williams1cf03c02016-02-17 13:01:23 -08001810 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1811 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08001812
Dan Williams1cf03c02016-02-17 13:01:23 -08001813 if (rc < 0)
1814 return rc;
1815 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001816}
1817
Dan Williams1cf03c02016-02-17 13:01:23 -08001818static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07001819{
Dan Williamsaef25332016-02-12 17:01:11 -08001820 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001821 struct nd_cmd_ars_start ars_start;
1822 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1823 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07001824
Dan Williams1cf03c02016-02-17 13:01:23 -08001825 memset(&ars_start, 0, sizeof(ars_start));
1826 ars_start.address = ars_status->restart_address;
1827 ars_start.length = ars_status->restart_length;
1828 ars_start.type = ars_status->type;
1829 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1830 sizeof(ars_start), &cmd_rc);
1831 if (rc < 0)
1832 return rc;
1833 return cmd_rc;
1834}
Dan Williamsaef25332016-02-12 17:01:11 -08001835
Dan Williams1cf03c02016-02-17 13:01:23 -08001836static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
1837{
1838 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1839 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
1840 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08001841
Dan Williams1cf03c02016-02-17 13:01:23 -08001842 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
1843 acpi_desc->ars_status_size, &cmd_rc);
1844 if (rc < 0)
1845 return rc;
1846 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001847}
1848
1849static int ars_status_process_records(struct nvdimm_bus *nvdimm_bus,
Dan Williams1cf03c02016-02-17 13:01:23 -08001850 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07001851{
1852 int rc;
1853 u32 i;
1854
Vishal Verma0caeef62015-12-24 19:21:43 -07001855 for (i = 0; i < ars_status->num_records; i++) {
1856 rc = nvdimm_bus_add_poison(nvdimm_bus,
1857 ars_status->records[i].err_address,
1858 ars_status->records[i].length);
1859 if (rc)
1860 return rc;
1861 }
1862
1863 return 0;
1864}
1865
Toshi Kaniaf1996e2016-03-09 12:47:06 -07001866static void acpi_nfit_remove_resource(void *data)
1867{
1868 struct resource *res = data;
1869
1870 remove_resource(res);
1871}
1872
1873static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
1874 struct nd_region_desc *ndr_desc)
1875{
1876 struct resource *res, *nd_res = ndr_desc->res;
1877 int is_pmem, ret;
1878
1879 /* No operation if the region is already registered as PMEM */
1880 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
1881 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
1882 if (is_pmem == REGION_INTERSECTS)
1883 return 0;
1884
1885 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
1886 if (!res)
1887 return -ENOMEM;
1888
1889 res->name = "Persistent Memory";
1890 res->start = nd_res->start;
1891 res->end = nd_res->end;
1892 res->flags = IORESOURCE_MEM;
1893 res->desc = IORES_DESC_PERSISTENT_MEMORY;
1894
1895 ret = insert_resource(&iomem_resource, res);
1896 if (ret)
1897 return ret;
1898
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05301899 ret = devm_add_action_or_reset(acpi_desc->dev,
1900 acpi_nfit_remove_resource,
1901 res);
1902 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07001903 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07001904
1905 return 0;
1906}
1907
Dan Williams1f7df6f2015-06-09 20:13:14 -04001908static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
1909 struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
1910 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08001911 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04001912{
1913 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
1914 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08001915 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001916 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001917 struct nfit_mem *nfit_mem;
1918 int blk_valid = 0;
1919
1920 if (!nvdimm) {
1921 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
1922 spa->range_index, memdev->device_handle);
1923 return -ENODEV;
1924 }
1925
1926 nd_mapping->nvdimm = nvdimm;
1927 switch (nfit_spa_type(spa)) {
1928 case NFIT_SPA_PM:
1929 case NFIT_SPA_VOLATILE:
1930 nd_mapping->start = memdev->address;
1931 nd_mapping->size = memdev->region_size;
1932 break;
1933 case NFIT_SPA_DCR:
1934 nfit_mem = nvdimm_provider_data(nvdimm);
1935 if (!nfit_mem || !nfit_mem->bdw) {
1936 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
1937 spa->range_index, nvdimm_name(nvdimm));
1938 } else {
1939 nd_mapping->size = nfit_mem->bdw->capacity;
1940 nd_mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04001941 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001942 blk_valid = 1;
1943 }
1944
1945 ndr_desc->nd_mapping = nd_mapping;
1946 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001947 ndbr_desc = to_blk_region_desc(ndr_desc);
1948 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04001949 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williams1cf03c02016-02-17 13:01:23 -08001950 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
1951 ndr_desc);
1952 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04001953 return -ENOMEM;
1954 break;
1955 }
1956
1957 return 0;
1958}
1959
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08001960static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
1961{
1962 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
1963 nfit_spa_type(spa) == NFIT_SPA_VCD ||
1964 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
1965 nfit_spa_type(spa) == NFIT_SPA_PCD);
1966}
1967
Dan Williams1f7df6f2015-06-09 20:13:14 -04001968static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
1969 struct nfit_spa *nfit_spa)
1970{
1971 static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
1972 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001973 struct nd_blk_region_desc ndbr_desc;
1974 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001975 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001976 struct nvdimm_bus *nvdimm_bus;
1977 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04001978 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001979
Dan Williams1cf03c02016-02-17 13:01:23 -08001980 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06001981 return 0;
1982
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08001983 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04001984 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
1985 __func__);
1986 return 0;
1987 }
1988
1989 memset(&res, 0, sizeof(res));
1990 memset(&nd_mappings, 0, sizeof(nd_mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001991 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04001992 res.start = spa->address;
1993 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001994 ndr_desc = &ndbr_desc.ndr_desc;
1995 ndr_desc->res = &res;
1996 ndr_desc->provider_data = nfit_spa;
1997 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06001998 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
1999 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2000 spa->proximity_domain);
2001 else
2002 ndr_desc->numa_node = NUMA_NO_NODE;
2003
Dan Williams1f7df6f2015-06-09 20:13:14 -04002004 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2005 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2006 struct nd_mapping *nd_mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002007
2008 if (memdev->range_index != spa->range_index)
2009 continue;
2010 if (count >= ND_MAX_MAPPINGS) {
2011 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2012 spa->range_index, ND_MAX_MAPPINGS);
2013 return -ENXIO;
2014 }
2015 nd_mapping = &nd_mappings[count++];
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002016 rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002017 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002018 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002019 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002020 }
2021
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002022 ndr_desc->nd_mapping = nd_mappings;
2023 ndr_desc->num_mappings = count;
2024 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002025 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002026 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002027
Dan Williams1f7df6f2015-06-09 20:13:14 -04002028 nvdimm_bus = acpi_desc->nvdimm_bus;
2029 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002030 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002031 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002032 dev_warn(acpi_desc->dev,
2033 "failed to insert pmem resource to iomem: %d\n",
2034 rc);
Dan Williams48901162016-03-09 17:15:43 -08002035 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002036 }
Dan Williams48901162016-03-09 17:15:43 -08002037
Dan Williams1cf03c02016-02-17 13:01:23 -08002038 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2039 ndr_desc);
2040 if (!nfit_spa->nd_region)
2041 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002042 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002043 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2044 ndr_desc);
2045 if (!nfit_spa->nd_region)
2046 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002047 } else if (nfit_spa_is_virtual(spa)) {
2048 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2049 ndr_desc);
2050 if (!nfit_spa->nd_region)
2051 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002052 }
Vishal Verma20985162015-10-27 16:58:27 -06002053
Dan Williams1cf03c02016-02-17 13:01:23 -08002054 out:
2055 if (rc)
2056 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2057 nfit_spa->spa->range_index);
2058 return rc;
2059}
2060
2061static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2062 u32 max_ars)
2063{
2064 struct device *dev = acpi_desc->dev;
2065 struct nd_cmd_ars_status *ars_status;
2066
2067 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2068 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2069 return 0;
2070 }
2071
2072 if (acpi_desc->ars_status)
2073 devm_kfree(dev, acpi_desc->ars_status);
2074 acpi_desc->ars_status = NULL;
2075 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2076 if (!ars_status)
2077 return -ENOMEM;
2078 acpi_desc->ars_status = ars_status;
2079 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002080 return 0;
2081}
2082
Dan Williams1cf03c02016-02-17 13:01:23 -08002083static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2084 struct nfit_spa *nfit_spa)
2085{
2086 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2087 int rc;
2088
2089 if (!nfit_spa->max_ars) {
2090 struct nd_cmd_ars_cap ars_cap;
2091
2092 memset(&ars_cap, 0, sizeof(ars_cap));
2093 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2094 if (rc < 0)
2095 return rc;
2096 nfit_spa->max_ars = ars_cap.max_ars_out;
2097 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2098 /* check that the supported scrub types match the spa type */
2099 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2100 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2101 return -ENOTTY;
2102 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2103 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2104 return -ENOTTY;
2105 }
2106
2107 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2108 return -ENOMEM;
2109
2110 rc = ars_get_status(acpi_desc);
2111 if (rc < 0 && rc != -ENOSPC)
2112 return rc;
2113
2114 if (ars_status_process_records(acpi_desc->nvdimm_bus,
2115 acpi_desc->ars_status))
2116 return -ENOMEM;
2117
2118 return 0;
2119}
2120
2121static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2122 struct nfit_spa *nfit_spa)
2123{
2124 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2125 unsigned int overflow_retry = scrub_overflow_abort;
2126 u64 init_ars_start = 0, init_ars_len = 0;
2127 struct device *dev = acpi_desc->dev;
2128 unsigned int tmo = scrub_timeout;
2129 int rc;
2130
Vishal Verma37b137f2016-07-23 21:51:42 -07002131 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002132 return;
2133
2134 rc = ars_start(acpi_desc, nfit_spa);
2135 /*
2136 * If we timed out the initial scan we'll still be busy here,
2137 * and will wait another timeout before giving up permanently.
2138 */
2139 if (rc < 0 && rc != -EBUSY)
2140 return;
2141
2142 do {
2143 u64 ars_start, ars_len;
2144
2145 if (acpi_desc->cancel)
2146 break;
2147 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2148 if (rc == -ENOTTY)
2149 break;
2150 if (rc == -EBUSY && !tmo) {
2151 dev_warn(dev, "range %d ars timeout, aborting\n",
2152 spa->range_index);
2153 break;
2154 }
2155
2156 if (rc == -EBUSY) {
2157 /*
2158 * Note, entries may be appended to the list
2159 * while the lock is dropped, but the workqueue
2160 * being active prevents entries being deleted /
2161 * freed.
2162 */
2163 mutex_unlock(&acpi_desc->init_mutex);
2164 ssleep(1);
2165 tmo--;
2166 mutex_lock(&acpi_desc->init_mutex);
2167 continue;
2168 }
2169
2170 /* we got some results, but there are more pending... */
2171 if (rc == -ENOSPC && overflow_retry--) {
2172 if (!init_ars_len) {
2173 init_ars_len = acpi_desc->ars_status->length;
2174 init_ars_start = acpi_desc->ars_status->address;
2175 }
2176 rc = ars_continue(acpi_desc);
2177 }
2178
2179 if (rc < 0) {
2180 dev_warn(dev, "range %d ars continuation failed\n",
2181 spa->range_index);
2182 break;
2183 }
2184
2185 if (init_ars_len) {
2186 ars_start = init_ars_start;
2187 ars_len = init_ars_len;
2188 } else {
2189 ars_start = acpi_desc->ars_status->address;
2190 ars_len = acpi_desc->ars_status->length;
2191 }
2192 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2193 spa->range_index, ars_start, ars_len);
2194 /* notify the region about new poison entries */
2195 nvdimm_region_notify(nfit_spa->nd_region,
2196 NVDIMM_REVALIDATE_POISON);
2197 break;
2198 } while (1);
2199}
2200
2201static void acpi_nfit_scrub(struct work_struct *work)
2202{
2203 struct device *dev;
2204 u64 init_scrub_length = 0;
2205 struct nfit_spa *nfit_spa;
2206 u64 init_scrub_address = 0;
2207 bool init_ars_done = false;
2208 struct acpi_nfit_desc *acpi_desc;
2209 unsigned int tmo = scrub_timeout;
2210 unsigned int overflow_retry = scrub_overflow_abort;
2211
2212 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2213 dev = acpi_desc->dev;
2214
2215 /*
2216 * We scrub in 2 phases. The first phase waits for any platform
2217 * firmware initiated scrubs to complete and then we go search for the
2218 * affected spa regions to mark them scanned. In the second phase we
2219 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002220 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2221 * the first phase, but really only care about running phase 2, where
2222 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002223 */
2224
2225 /* process platform firmware initiated scrubs */
2226 retry:
2227 mutex_lock(&acpi_desc->init_mutex);
2228 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2229 struct nd_cmd_ars_status *ars_status;
2230 struct acpi_nfit_system_address *spa;
2231 u64 ars_start, ars_len;
2232 int rc;
2233
2234 if (acpi_desc->cancel)
2235 break;
2236
2237 if (nfit_spa->nd_region)
2238 continue;
2239
2240 if (init_ars_done) {
2241 /*
2242 * No need to re-query, we're now just
2243 * reconciling all the ranges covered by the
2244 * initial scrub
2245 */
2246 rc = 0;
2247 } else
2248 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2249
2250 if (rc == -ENOTTY) {
2251 /* no ars capability, just register spa and move on */
2252 acpi_nfit_register_region(acpi_desc, nfit_spa);
2253 continue;
2254 }
2255
2256 if (rc == -EBUSY && !tmo) {
2257 /* fallthrough to directed scrub in phase 2 */
2258 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2259 break;
2260 } else if (rc == -EBUSY) {
2261 mutex_unlock(&acpi_desc->init_mutex);
2262 ssleep(1);
2263 tmo--;
2264 goto retry;
2265 }
2266
2267 /* we got some results, but there are more pending... */
2268 if (rc == -ENOSPC && overflow_retry--) {
2269 ars_status = acpi_desc->ars_status;
2270 /*
2271 * Record the original scrub range, so that we
2272 * can recall all the ranges impacted by the
2273 * initial scrub.
2274 */
2275 if (!init_scrub_length) {
2276 init_scrub_length = ars_status->length;
2277 init_scrub_address = ars_status->address;
2278 }
2279 rc = ars_continue(acpi_desc);
2280 if (rc == 0) {
2281 mutex_unlock(&acpi_desc->init_mutex);
2282 goto retry;
2283 }
2284 }
2285
2286 if (rc < 0) {
2287 /*
2288 * Initial scrub failed, we'll give it one more
2289 * try below...
2290 */
2291 break;
2292 }
2293
2294 /* We got some final results, record completed ranges */
2295 ars_status = acpi_desc->ars_status;
2296 if (init_scrub_length) {
2297 ars_start = init_scrub_address;
2298 ars_len = ars_start + init_scrub_length;
2299 } else {
2300 ars_start = ars_status->address;
2301 ars_len = ars_status->length;
2302 }
2303 spa = nfit_spa->spa;
2304
2305 if (!init_ars_done) {
2306 init_ars_done = true;
2307 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2308 ars_start, ars_len);
2309 }
2310 if (ars_start <= spa->address && ars_start + ars_len
2311 >= spa->address + spa->length)
2312 acpi_nfit_register_region(acpi_desc, nfit_spa);
2313 }
2314
2315 /*
2316 * For all the ranges not covered by an initial scrub we still
2317 * want to see if there are errors, but it's ok to discover them
2318 * asynchronously.
2319 */
2320 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2321 /*
2322 * Flag all the ranges that still need scrubbing, but
2323 * register them now to make data available.
2324 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002325 if (!nfit_spa->nd_region) {
2326 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002327 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002328 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002329 }
2330
2331 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2332 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002333 acpi_desc->scrub_count++;
2334 if (acpi_desc->scrub_count_state)
2335 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002336 mutex_unlock(&acpi_desc->init_mutex);
2337}
2338
Dan Williams1f7df6f2015-06-09 20:13:14 -04002339static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2340{
2341 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002342 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002343
Dan Williams1cf03c02016-02-17 13:01:23 -08002344 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2345 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2346 /* BLK regions don't need to wait for ars results */
2347 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2348 if (rc)
2349 return rc;
2350 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002351
Dan Williams1cf03c02016-02-17 13:01:23 -08002352 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002353 return 0;
2354}
2355
Vishal Verma20985162015-10-27 16:58:27 -06002356static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2357 struct nfit_table_prev *prev)
2358{
2359 struct device *dev = acpi_desc->dev;
2360
2361 if (!list_empty(&prev->spas) ||
2362 !list_empty(&prev->memdevs) ||
2363 !list_empty(&prev->dcrs) ||
2364 !list_empty(&prev->bdws) ||
2365 !list_empty(&prev->idts) ||
2366 !list_empty(&prev->flushes)) {
2367 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2368 return -ENXIO;
2369 }
2370 return 0;
2371}
2372
Vishal Verma37b137f2016-07-23 21:51:42 -07002373static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2374{
2375 struct device *dev = acpi_desc->dev;
2376 struct kernfs_node *nfit;
2377 struct device *bus_dev;
2378
2379 if (!ars_supported(acpi_desc->nvdimm_bus))
2380 return 0;
2381
2382 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2383 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2384 if (!nfit) {
2385 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2386 return -ENODEV;
2387 }
2388 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2389 sysfs_put(nfit);
2390 if (!acpi_desc->scrub_count_state) {
2391 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2392 return -ENODEV;
2393 }
2394
2395 return 0;
2396}
2397
Dan Williams58cd71b2016-07-21 18:05:36 -07002398static void acpi_nfit_destruct(void *data)
2399{
2400 struct acpi_nfit_desc *acpi_desc = data;
Vishal Verma37b137f2016-07-23 21:51:42 -07002401 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002402
2403 acpi_desc->cancel = 1;
Vishal Verma37b137f2016-07-23 21:51:42 -07002404 /*
2405 * Bounce the nvdimm bus lock to make sure any in-flight
2406 * acpi_nfit_ars_rescan() submissions have had a chance to
2407 * either submit or see ->cancel set.
2408 */
2409 device_lock(bus_dev);
2410 device_unlock(bus_dev);
2411
Dan Williams58cd71b2016-07-21 18:05:36 -07002412 flush_workqueue(nfit_wq);
Vishal Verma37b137f2016-07-23 21:51:42 -07002413 if (acpi_desc->scrub_count_state)
2414 sysfs_put(acpi_desc->scrub_count_state);
Dan Williams58cd71b2016-07-21 18:05:36 -07002415 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2416 acpi_desc->nvdimm_bus = NULL;
2417}
2418
Dan Williamse7a11b42016-07-14 16:19:55 -07002419int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002420{
2421 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002422 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002423 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002424 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002425
Dan Williams58cd71b2016-07-21 18:05:36 -07002426 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002427 acpi_nfit_init_dsms(acpi_desc);
2428
Dan Williams58cd71b2016-07-21 18:05:36 -07002429 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2430 &acpi_desc->nd_desc);
2431 if (!acpi_desc->nvdimm_bus)
2432 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002433
Dan Williams58cd71b2016-07-21 18:05:36 -07002434 rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
2435 acpi_desc);
2436 if (rc)
2437 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002438
2439 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2440 if (rc)
2441 return rc;
Dan Williams58cd71b2016-07-21 18:05:36 -07002442 }
2443
Vishal Verma20985162015-10-27 16:58:27 -06002444 mutex_lock(&acpi_desc->init_mutex);
2445
2446 INIT_LIST_HEAD(&prev.spas);
2447 INIT_LIST_HEAD(&prev.memdevs);
2448 INIT_LIST_HEAD(&prev.dcrs);
2449 INIT_LIST_HEAD(&prev.bdws);
2450 INIT_LIST_HEAD(&prev.idts);
2451 INIT_LIST_HEAD(&prev.flushes);
2452
2453 list_cut_position(&prev.spas, &acpi_desc->spas,
2454 acpi_desc->spas.prev);
2455 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2456 acpi_desc->memdevs.prev);
2457 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2458 acpi_desc->dcrs.prev);
2459 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2460 acpi_desc->bdws.prev);
2461 list_cut_position(&prev.idts, &acpi_desc->idts,
2462 acpi_desc->idts.prev);
2463 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2464 acpi_desc->flushes.prev);
2465
Vishal Verma20985162015-10-27 16:58:27 -06002466 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002467 while (!IS_ERR_OR_NULL(data))
2468 data = add_table(acpi_desc, &prev, data, end);
2469
2470 if (IS_ERR(data)) {
2471 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2472 PTR_ERR(data));
2473 rc = PTR_ERR(data);
2474 goto out_unlock;
2475 }
2476
2477 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2478 if (rc)
2479 goto out_unlock;
2480
Dan Williams81ed4e32016-06-10 18:20:53 -07002481 rc = nfit_mem_init(acpi_desc);
2482 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002483 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002484
Vishal Verma20985162015-10-27 16:58:27 -06002485 rc = acpi_nfit_register_dimms(acpi_desc);
2486 if (rc)
2487 goto out_unlock;
2488
2489 rc = acpi_nfit_register_regions(acpi_desc);
2490
2491 out_unlock:
2492 mutex_unlock(&acpi_desc->init_mutex);
2493 return rc;
2494}
2495EXPORT_SYMBOL_GPL(acpi_nfit_init);
2496
Dan Williams7ae0fa432016-02-19 12:16:34 -08002497struct acpi_nfit_flush_work {
2498 struct work_struct work;
2499 struct completion cmp;
2500};
2501
2502static void flush_probe(struct work_struct *work)
2503{
2504 struct acpi_nfit_flush_work *flush;
2505
2506 flush = container_of(work, typeof(*flush), work);
2507 complete(&flush->cmp);
2508}
2509
2510static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2511{
2512 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2513 struct device *dev = acpi_desc->dev;
2514 struct acpi_nfit_flush_work flush;
2515
2516 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2517 device_lock(dev);
2518 device_unlock(dev);
2519
2520 /*
2521 * Scrub work could take 10s of seconds, userspace may give up so we
2522 * need to be interruptible while waiting.
2523 */
2524 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2525 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2526 queue_work(nfit_wq, &flush.work);
2527 return wait_for_completion_interruptible(&flush.cmp);
2528}
2529
Dan Williams87bf5722016-02-22 21:50:31 -08002530static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2531 struct nvdimm *nvdimm, unsigned int cmd)
2532{
2533 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2534
2535 if (nvdimm)
2536 return 0;
2537 if (cmd != ND_CMD_ARS_START)
2538 return 0;
2539
2540 /*
2541 * The kernel and userspace may race to initiate a scrub, but
2542 * the scrub thread is prepared to lose that initial race. It
2543 * just needs guarantees that any ars it initiates are not
2544 * interrupted by any intervening start reqeusts from userspace.
2545 */
2546 if (work_busy(&acpi_desc->work))
2547 return -EBUSY;
2548
2549 return 0;
2550}
2551
Vishal Verma37b137f2016-07-23 21:51:42 -07002552static int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
2553{
2554 struct device *dev = acpi_desc->dev;
2555 struct nfit_spa *nfit_spa;
2556
2557 if (work_busy(&acpi_desc->work))
2558 return -EBUSY;
2559
2560 if (acpi_desc->cancel)
2561 return 0;
2562
2563 mutex_lock(&acpi_desc->init_mutex);
2564 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2565 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2566
2567 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2568 continue;
2569
2570 nfit_spa->ars_required = 1;
2571 }
2572 queue_work(nfit_wq, &acpi_desc->work);
2573 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2574 mutex_unlock(&acpi_desc->init_mutex);
2575
2576 return 0;
2577}
2578
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002579void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002580{
2581 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002582
2583 dev_set_drvdata(dev, acpi_desc);
2584 acpi_desc->dev = dev;
2585 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2586 nd_desc = &acpi_desc->nd_desc;
2587 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002588 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002589 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002590 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002591 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002592 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2593
Dan Williamsb94d5232015-05-19 22:54:31 -04002594 INIT_LIST_HEAD(&acpi_desc->spas);
2595 INIT_LIST_HEAD(&acpi_desc->dcrs);
2596 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002597 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002598 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04002599 INIT_LIST_HEAD(&acpi_desc->memdevs);
2600 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma20985162015-10-27 16:58:27 -06002601 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08002602 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04002603}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002604EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04002605
2606static int acpi_nfit_add(struct acpi_device *adev)
2607{
Vishal Verma20985162015-10-27 16:58:27 -06002608 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04002609 struct acpi_nfit_desc *acpi_desc;
2610 struct device *dev = &adev->dev;
2611 struct acpi_table_header *tbl;
2612 acpi_status status = AE_OK;
2613 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07002614 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002615
Lee, Chun-Yi82595422016-01-21 20:32:10 +08002616 status = acpi_get_table_with_size(ACPI_SIG_NFIT, 0, &tbl, &sz);
Dan Williamsb94d5232015-05-19 22:54:31 -04002617 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06002618 /* This is ok, we could have an nvdimm hotplugged later */
2619 dev_dbg(dev, "failed to find NFIT at startup\n");
2620 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002621 }
2622
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002623 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2624 if (!acpi_desc)
2625 return -ENOMEM;
2626 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04002627
Dan Williamse7a11b42016-07-14 16:19:55 -07002628 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05002629 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04002630
Vishal Verma20985162015-10-27 16:58:27 -06002631 /* Evaluate _FIT and override with that if present */
2632 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2633 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002634 union acpi_object *obj = buf.pointer;
2635
2636 if (obj->type == ACPI_TYPE_BUFFER)
2637 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2638 obj->buffer.length);
2639 else
Linda Knippers6b577c92015-11-20 19:05:49 -05002640 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2641 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07002642 kfree(buf.pointer);
2643 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07002644 /* skip over the lead-in header table */
2645 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2646 + sizeof(struct acpi_table_nfit),
2647 sz - sizeof(struct acpi_table_nfit));
Dan Williamse7a11b42016-07-14 16:19:55 -07002648 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002649}
2650
2651static int acpi_nfit_remove(struct acpi_device *adev)
2652{
Dan Williams58cd71b2016-07-21 18:05:36 -07002653 /* see acpi_nfit_destruct */
Dan Williamsb94d5232015-05-19 22:54:31 -04002654 return 0;
2655}
2656
Vishal Verma20985162015-10-27 16:58:27 -06002657static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2658{
2659 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
2660 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Vishal Verma20985162015-10-27 16:58:27 -06002661 struct device *dev = &adev->dev;
Dan Williamse7a11b42016-07-14 16:19:55 -07002662 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06002663 acpi_status status;
2664 int ret;
2665
2666 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2667
2668 device_lock(dev);
2669 if (!dev->driver) {
2670 /* dev->driver may be null if we're being removed */
2671 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Alexey Khoroshilovd91e8922015-12-11 23:24:10 +03002672 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002673 }
2674
2675 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002676 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2677 if (!acpi_desc)
Vishal Verma20985162015-10-27 16:58:27 -06002678 goto out_unlock;
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002679 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002680 } else {
2681 /*
2682 * Finish previous registration before considering new
2683 * regions.
2684 */
2685 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06002686 }
2687
2688 /* Evaluate _FIT */
2689 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2690 if (ACPI_FAILURE(status)) {
2691 dev_err(dev, "failed to evaluate _FIT\n");
2692 goto out_unlock;
2693 }
2694
Linda Knippers6b577c92015-11-20 19:05:49 -05002695 obj = buf.pointer;
2696 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002697 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2698 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07002699 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05002700 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07002701 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05002702 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06002703 kfree(buf.pointer);
2704
2705 out_unlock:
2706 device_unlock(dev);
2707}
2708
Dan Williamsb94d5232015-05-19 22:54:31 -04002709static const struct acpi_device_id acpi_nfit_ids[] = {
2710 { "ACPI0012", 0 },
2711 { "", 0 },
2712};
2713MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
2714
2715static struct acpi_driver acpi_nfit_driver = {
2716 .name = KBUILD_MODNAME,
2717 .ids = acpi_nfit_ids,
2718 .ops = {
2719 .add = acpi_nfit_add,
2720 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06002721 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04002722 },
2723};
2724
2725static __init int nfit_init(void)
2726{
2727 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
2728 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
2729 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
2730 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
2731 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
2732 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
2733 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
2734
2735 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
2736 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
2737 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
2738 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
2739 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
2740 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
2741 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
2742 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
2743 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
2744 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
Dan Williams31eca762016-04-28 16:23:43 -07002745 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
2746 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
stuart hayese02fb722016-05-26 11:38:41 -05002747 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04002748
Dan Williams7ae0fa432016-02-19 12:16:34 -08002749 nfit_wq = create_singlethread_workqueue("nfit");
2750 if (!nfit_wq)
2751 return -ENOMEM;
2752
Dan Williamsb94d5232015-05-19 22:54:31 -04002753 return acpi_bus_register_driver(&acpi_nfit_driver);
2754}
2755
2756static __exit void nfit_exit(void)
2757{
2758 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002759 destroy_workqueue(nfit_wq);
Dan Williamsb94d5232015-05-19 22:54:31 -04002760}
2761
2762module_init(nfit_init);
2763module_exit(nfit_exit);
2764MODULE_LICENSE("GPL v2");
2765MODULE_AUTHOR("Intel Corporation");