1 /*
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>
16 #include <linux/mutex.h>
17 #include <linux/ndctl.h>
18 #include <linux/sysfs.h>
19 #include <linux/delay.h>
20 #include <linux/list.h>
21 #include <linux/acpi.h>
22 #include <linux/sort.h>
23 #include <linux/io.h>
24 #include <linux/nd.h>
25 #include <asm/cacheflush.h>
26 #include "nfit.h"
27
28 /*
29 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
30 * irrelevant.
31 */
32 #include <linux/io-64-nonatomic-hi-lo.h>
33
34 static bool force_enable_dimms;
35 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
37
38 static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
39 module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
40 MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
41
42 /* after three payloads of overflow, it's dead jim */
43 static unsigned int scrub_overflow_abort = 3;
44 module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
45 MODULE_PARM_DESC(scrub_overflow_abort,
46 "Number of times we overflow ARS results before abort");
47
48 static bool disable_vendor_specific;
49 module_param(disable_vendor_specific, bool, S_IRUGO);
50 MODULE_PARM_DESC(disable_vendor_specific,
51 "Limit commands to the publicly specified set");
52
53 static unsigned long override_dsm_mask;
54 module_param(override_dsm_mask, ulong, S_IRUGO);
55 MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
56
57 static int default_dsm_family = -1;
58 module_param(default_dsm_family, int, S_IRUGO);
59 MODULE_PARM_DESC(default_dsm_family,
60 "Try this DSM type first when identifying NVDIMM family");
61
62 LIST_HEAD(acpi_descs);
63 DEFINE_MUTEX(acpi_desc_lock);
64
65 static struct workqueue_struct *nfit_wq;
66
67 struct nfit_table_prev {
68 struct list_head spas;
69 struct list_head memdevs;
70 struct list_head dcrs;
71 struct list_head bdws;
72 struct list_head idts;
73 struct list_head flushes;
74 };
75
76 static guid_t nfit_uuid[NFIT_UUID_MAX];
77
to_nfit_uuid(enum nfit_uuids id)78 const guid_t *to_nfit_uuid(enum nfit_uuids id)
79 {
80 return &nfit_uuid[id];
81 }
82 EXPORT_SYMBOL(to_nfit_uuid);
83
to_acpi_nfit_desc(struct nvdimm_bus_descriptor * nd_desc)84 static struct acpi_nfit_desc *to_acpi_nfit_desc(
85 struct nvdimm_bus_descriptor *nd_desc)
86 {
87 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
88 }
89
to_acpi_dev(struct acpi_nfit_desc * acpi_desc)90 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
91 {
92 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
93
94 /*
95 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
96 * acpi_device.
97 */
98 if (!nd_desc->provider_name
99 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
100 return NULL;
101
102 return to_acpi_device(acpi_desc->dev);
103 }
104
xlat_bus_status(void * buf,unsigned int cmd,u32 status)105 static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
106 {
107 struct nd_cmd_clear_error *clear_err;
108 struct nd_cmd_ars_status *ars_status;
109 u16 flags;
110
111 switch (cmd) {
112 case ND_CMD_ARS_CAP:
113 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
114 return -ENOTTY;
115
116 /* Command failed */
117 if (status & 0xffff)
118 return -EIO;
119
120 /* No supported scan types for this range */
121 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
122 if ((status >> 16 & flags) == 0)
123 return -ENOTTY;
124 return 0;
125 case ND_CMD_ARS_START:
126 /* ARS is in progress */
127 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
128 return -EBUSY;
129
130 /* Command failed */
131 if (status & 0xffff)
132 return -EIO;
133 return 0;
134 case ND_CMD_ARS_STATUS:
135 ars_status = buf;
136 /* Command failed */
137 if (status & 0xffff)
138 return -EIO;
139 /* Check extended status (Upper two bytes) */
140 if (status == NFIT_ARS_STATUS_DONE)
141 return 0;
142
143 /* ARS is in progress */
144 if (status == NFIT_ARS_STATUS_BUSY)
145 return -EBUSY;
146
147 /* No ARS performed for the current boot */
148 if (status == NFIT_ARS_STATUS_NONE)
149 return -EAGAIN;
150
151 /*
152 * ARS interrupted, either we overflowed or some other
153 * agent wants the scan to stop. If we didn't overflow
154 * then just continue with the returned results.
155 */
156 if (status == NFIT_ARS_STATUS_INTR) {
157 if (ars_status->out_length >= 40 && (ars_status->flags
158 & NFIT_ARS_F_OVERFLOW))
159 return -ENOSPC;
160 return 0;
161 }
162
163 /* Unknown status */
164 if (status >> 16)
165 return -EIO;
166 return 0;
167 case ND_CMD_CLEAR_ERROR:
168 clear_err = buf;
169 if (status & 0xffff)
170 return -EIO;
171 if (!clear_err->cleared)
172 return -EIO;
173 if (clear_err->length > clear_err->cleared)
174 return clear_err->cleared;
175 return 0;
176 default:
177 break;
178 }
179
180 /* all other non-zero status results in an error */
181 if (status)
182 return -EIO;
183 return 0;
184 }
185
xlat_nvdimm_status(void * buf,unsigned int cmd,u32 status)186 static int xlat_nvdimm_status(void *buf, unsigned int cmd, u32 status)
187 {
188 switch (cmd) {
189 case ND_CMD_GET_CONFIG_SIZE:
190 if (status >> 16 & ND_CONFIG_LOCKED)
191 return -EACCES;
192 break;
193 default:
194 break;
195 }
196
197 /* all other non-zero status results in an error */
198 if (status)
199 return -EIO;
200 return 0;
201 }
202
xlat_status(struct nvdimm * nvdimm,void * buf,unsigned int cmd,u32 status)203 static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
204 u32 status)
205 {
206 if (!nvdimm)
207 return xlat_bus_status(buf, cmd, status);
208 return xlat_nvdimm_status(buf, cmd, status);
209 }
210
cmd_to_func(struct nfit_mem * nfit_mem,unsigned int cmd,struct nd_cmd_pkg * call_pkg)211 static int cmd_to_func(struct nfit_mem *nfit_mem, unsigned int cmd,
212 struct nd_cmd_pkg *call_pkg)
213 {
214 if (call_pkg) {
215 int i;
216
217 if (nfit_mem->family != call_pkg->nd_family)
218 return -ENOTTY;
219
220 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
221 if (call_pkg->nd_reserved2[i])
222 return -EINVAL;
223 return call_pkg->nd_command;
224 }
225
226 /* Linux ND commands == NVDIMM_FAMILY_INTEL function numbers */
227 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
228 return cmd;
229
230 /*
231 * Force function number validation to fail since 0 is never
232 * published as a valid function in dsm_mask.
233 */
234 return 0;
235 }
236
acpi_nfit_ctl(struct nvdimm_bus_descriptor * nd_desc,struct nvdimm * nvdimm,unsigned int cmd,void * buf,unsigned int buf_len,int * cmd_rc)237 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
238 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
239 {
240 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
241 union acpi_object in_obj, in_buf, *out_obj;
242 const struct nd_cmd_desc *desc = NULL;
243 struct device *dev = acpi_desc->dev;
244 struct nd_cmd_pkg *call_pkg = NULL;
245 const char *cmd_name, *dimm_name;
246 unsigned long cmd_mask, dsm_mask;
247 u32 offset, fw_status = 0;
248 acpi_handle handle;
249 const guid_t *guid;
250 int func, rc, i;
251
252 if (cmd_rc)
253 *cmd_rc = -EINVAL;
254
255 if (nvdimm) {
256 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
257 struct acpi_device *adev = nfit_mem->adev;
258
259 if (!adev)
260 return -ENOTTY;
261
262 if (cmd == ND_CMD_CALL)
263 call_pkg = buf;
264 func = cmd_to_func(nfit_mem, cmd, call_pkg);
265 if (func < 0)
266 return func;
267 dimm_name = nvdimm_name(nvdimm);
268 cmd_name = nvdimm_cmd_name(cmd);
269 cmd_mask = nvdimm_cmd_mask(nvdimm);
270 dsm_mask = nfit_mem->dsm_mask;
271 desc = nd_cmd_dimm_desc(cmd);
272 guid = to_nfit_uuid(nfit_mem->family);
273 handle = adev->handle;
274 } else {
275 struct acpi_device *adev = to_acpi_dev(acpi_desc);
276
277 func = cmd;
278 cmd_name = nvdimm_bus_cmd_name(cmd);
279 cmd_mask = nd_desc->cmd_mask;
280 dsm_mask = cmd_mask;
281 if (cmd == ND_CMD_CALL)
282 dsm_mask = nd_desc->bus_dsm_mask;
283 desc = nd_cmd_bus_desc(cmd);
284 guid = to_nfit_uuid(NFIT_DEV_BUS);
285 handle = adev->handle;
286 dimm_name = "bus";
287 }
288
289 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
290 return -ENOTTY;
291
292 /*
293 * Check for a valid command. For ND_CMD_CALL, we also have to
294 * make sure that the DSM function is supported.
295 */
296 if (cmd == ND_CMD_CALL && !test_bit(func, &dsm_mask))
297 return -ENOTTY;
298 else if (!test_bit(cmd, &cmd_mask))
299 return -ENOTTY;
300
301 in_obj.type = ACPI_TYPE_PACKAGE;
302 in_obj.package.count = 1;
303 in_obj.package.elements = &in_buf;
304 in_buf.type = ACPI_TYPE_BUFFER;
305 in_buf.buffer.pointer = buf;
306 in_buf.buffer.length = 0;
307
308 /* libnvdimm has already validated the input envelope */
309 for (i = 0; i < desc->in_num; i++)
310 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
311 i, buf);
312
313 if (call_pkg) {
314 /* skip over package wrapper */
315 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
316 in_buf.buffer.length = call_pkg->nd_size_in;
317 }
318
319 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
320 __func__, dimm_name, cmd, func, in_buf.buffer.length);
321 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
322 in_buf.buffer.pointer,
323 min_t(u32, 256, in_buf.buffer.length), true);
324
325 out_obj = acpi_evaluate_dsm(handle, guid, 1, func, &in_obj);
326 if (!out_obj) {
327 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
328 cmd_name);
329 return -EINVAL;
330 }
331
332 if (out_obj->type != ACPI_TYPE_BUFFER) {
333 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
334 dimm_name, cmd_name, out_obj->type);
335 rc = -EINVAL;
336 goto out;
337 }
338
339 if (call_pkg) {
340 call_pkg->nd_fw_size = out_obj->buffer.length;
341 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
342 out_obj->buffer.pointer,
343 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
344
345 ACPI_FREE(out_obj);
346 /*
347 * Need to support FW function w/o known size in advance.
348 * Caller can determine required size based upon nd_fw_size.
349 * If we return an error (like elsewhere) then caller wouldn't
350 * be able to rely upon data returned to make calculation.
351 */
352 if (cmd_rc)
353 *cmd_rc = 0;
354 return 0;
355 }
356
357 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, dimm_name,
358 cmd_name, out_obj->buffer.length);
359 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
360 out_obj->buffer.pointer,
361 min_t(u32, 128, out_obj->buffer.length), true);
362
363 for (i = 0, offset = 0; i < desc->out_num; i++) {
364 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
365 (u32 *) out_obj->buffer.pointer,
366 out_obj->buffer.length - offset);
367
368 if (offset + out_size > out_obj->buffer.length) {
369 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
370 __func__, dimm_name, cmd_name, i);
371 break;
372 }
373
374 if (in_buf.buffer.length + offset + out_size > buf_len) {
375 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
376 __func__, dimm_name, cmd_name, i);
377 rc = -ENXIO;
378 goto out;
379 }
380 memcpy(buf + in_buf.buffer.length + offset,
381 out_obj->buffer.pointer + offset, out_size);
382 offset += out_size;
383 }
384
385 /*
386 * Set fw_status for all the commands with a known format to be
387 * later interpreted by xlat_status().
388 */
389 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
390 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
391 fw_status = *(u32 *) out_obj->buffer.pointer;
392
393 if (offset + in_buf.buffer.length < buf_len) {
394 if (i >= 1) {
395 /*
396 * status valid, return the number of bytes left
397 * unfilled in the output buffer
398 */
399 rc = buf_len - offset - in_buf.buffer.length;
400 if (cmd_rc)
401 *cmd_rc = xlat_status(nvdimm, buf, cmd,
402 fw_status);
403 } else {
404 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
405 __func__, dimm_name, cmd_name, buf_len,
406 offset);
407 rc = -ENXIO;
408 }
409 } else {
410 rc = 0;
411 if (cmd_rc)
412 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
413 }
414
415 out:
416 ACPI_FREE(out_obj);
417
418 return rc;
419 }
420 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
421
spa_type_name(u16 type)422 static const char *spa_type_name(u16 type)
423 {
424 static const char *to_name[] = {
425 [NFIT_SPA_VOLATILE] = "volatile",
426 [NFIT_SPA_PM] = "pmem",
427 [NFIT_SPA_DCR] = "dimm-control-region",
428 [NFIT_SPA_BDW] = "block-data-window",
429 [NFIT_SPA_VDISK] = "volatile-disk",
430 [NFIT_SPA_VCD] = "volatile-cd",
431 [NFIT_SPA_PDISK] = "persistent-disk",
432 [NFIT_SPA_PCD] = "persistent-cd",
433
434 };
435
436 if (type > NFIT_SPA_PCD)
437 return "unknown";
438
439 return to_name[type];
440 }
441
nfit_spa_type(struct acpi_nfit_system_address * spa)442 int nfit_spa_type(struct acpi_nfit_system_address *spa)
443 {
444 int i;
445
446 for (i = 0; i < NFIT_UUID_MAX; i++)
447 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
448 return i;
449 return -1;
450 }
451
add_spa(struct acpi_nfit_desc * acpi_desc,struct nfit_table_prev * prev,struct acpi_nfit_system_address * spa)452 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
453 struct nfit_table_prev *prev,
454 struct acpi_nfit_system_address *spa)
455 {
456 struct device *dev = acpi_desc->dev;
457 struct nfit_spa *nfit_spa;
458
459 if (spa->header.length != sizeof(*spa))
460 return false;
461
462 list_for_each_entry(nfit_spa, &prev->spas, list) {
463 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
464 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
465 return true;
466 }
467 }
468
469 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
470 GFP_KERNEL);
471 if (!nfit_spa)
472 return false;
473 INIT_LIST_HEAD(&nfit_spa->list);
474 memcpy(nfit_spa->spa, spa, sizeof(*spa));
475 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
476 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
477 spa->range_index,
478 spa_type_name(nfit_spa_type(spa)));
479 return true;
480 }
481
add_memdev(struct acpi_nfit_desc * acpi_desc,struct nfit_table_prev * prev,struct acpi_nfit_memory_map * memdev)482 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
483 struct nfit_table_prev *prev,
484 struct acpi_nfit_memory_map *memdev)
485 {
486 struct device *dev = acpi_desc->dev;
487 struct nfit_memdev *nfit_memdev;
488
489 if (memdev->header.length != sizeof(*memdev))
490 return false;
491
492 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
493 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
494 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
495 return true;
496 }
497
498 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
499 GFP_KERNEL);
500 if (!nfit_memdev)
501 return false;
502 INIT_LIST_HEAD(&nfit_memdev->list);
503 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
504 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
505 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
506 __func__, memdev->device_handle, memdev->range_index,
507 memdev->region_index, memdev->flags);
508 return true;
509 }
510
511 /*
512 * An implementation may provide a truncated control region if no block windows
513 * are defined.
514 */
sizeof_dcr(struct acpi_nfit_control_region * dcr)515 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
516 {
517 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
518 window_size))
519 return 0;
520 if (dcr->windows)
521 return sizeof(*dcr);
522 return offsetof(struct acpi_nfit_control_region, window_size);
523 }
524
add_dcr(struct acpi_nfit_desc * acpi_desc,struct nfit_table_prev * prev,struct acpi_nfit_control_region * dcr)525 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
526 struct nfit_table_prev *prev,
527 struct acpi_nfit_control_region *dcr)
528 {
529 struct device *dev = acpi_desc->dev;
530 struct nfit_dcr *nfit_dcr;
531
532 if (!sizeof_dcr(dcr))
533 return false;
534
535 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
536 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
537 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
538 return true;
539 }
540
541 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
542 GFP_KERNEL);
543 if (!nfit_dcr)
544 return false;
545 INIT_LIST_HEAD(&nfit_dcr->list);
546 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
547 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
548 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
549 dcr->region_index, dcr->windows);
550 return true;
551 }
552
add_bdw(struct acpi_nfit_desc * acpi_desc,struct nfit_table_prev * prev,struct acpi_nfit_data_region * bdw)553 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
554 struct nfit_table_prev *prev,
555 struct acpi_nfit_data_region *bdw)
556 {
557 struct device *dev = acpi_desc->dev;
558 struct nfit_bdw *nfit_bdw;
559
560 if (bdw->header.length != sizeof(*bdw))
561 return false;
562 list_for_each_entry(nfit_bdw, &prev->bdws, list)
563 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
564 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
565 return true;
566 }
567
568 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
569 GFP_KERNEL);
570 if (!nfit_bdw)
571 return false;
572 INIT_LIST_HEAD(&nfit_bdw->list);
573 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
574 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
575 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
576 bdw->region_index, bdw->windows);
577 return true;
578 }
579
sizeof_idt(struct acpi_nfit_interleave * idt)580 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
581 {
582 if (idt->header.length < sizeof(*idt))
583 return 0;
584 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
585 }
586
add_idt(struct acpi_nfit_desc * acpi_desc,struct nfit_table_prev * prev,struct acpi_nfit_interleave * idt)587 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
588 struct nfit_table_prev *prev,
589 struct acpi_nfit_interleave *idt)
590 {
591 struct device *dev = acpi_desc->dev;
592 struct nfit_idt *nfit_idt;
593
594 if (!sizeof_idt(idt))
595 return false;
596
597 list_for_each_entry(nfit_idt, &prev->idts, list) {
598 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
599 continue;
600
601 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
602 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
603 return true;
604 }
605 }
606
607 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
608 GFP_KERNEL);
609 if (!nfit_idt)
610 return false;
611 INIT_LIST_HEAD(&nfit_idt->list);
612 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
613 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
614 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
615 idt->interleave_index, idt->line_count);
616 return true;
617 }
618
sizeof_flush(struct acpi_nfit_flush_address * flush)619 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
620 {
621 if (flush->header.length < sizeof(*flush))
622 return 0;
623 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
624 }
625
add_flush(struct acpi_nfit_desc * acpi_desc,struct nfit_table_prev * prev,struct acpi_nfit_flush_address * flush)626 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
627 struct nfit_table_prev *prev,
628 struct acpi_nfit_flush_address *flush)
629 {
630 struct device *dev = acpi_desc->dev;
631 struct nfit_flush *nfit_flush;
632
633 if (!sizeof_flush(flush))
634 return false;
635
636 list_for_each_entry(nfit_flush, &prev->flushes, list) {
637 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
638 continue;
639
640 if (memcmp(nfit_flush->flush, flush,
641 sizeof_flush(flush)) == 0) {
642 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
643 return true;
644 }
645 }
646
647 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
648 + sizeof_flush(flush), GFP_KERNEL);
649 if (!nfit_flush)
650 return false;
651 INIT_LIST_HEAD(&nfit_flush->list);
652 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
653 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
654 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
655 flush->device_handle, flush->hint_count);
656 return true;
657 }
658
add_table(struct acpi_nfit_desc * acpi_desc,struct nfit_table_prev * prev,void * table,const void * end)659 static void *add_table(struct acpi_nfit_desc *acpi_desc,
660 struct nfit_table_prev *prev, void *table, const void *end)
661 {
662 struct device *dev = acpi_desc->dev;
663 struct acpi_nfit_header *hdr;
664 void *err = ERR_PTR(-ENOMEM);
665
666 if (table >= end)
667 return NULL;
668
669 hdr = table;
670 if (!hdr->length) {
671 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
672 hdr->type);
673 return NULL;
674 }
675
676 switch (hdr->type) {
677 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
678 if (!add_spa(acpi_desc, prev, table))
679 return err;
680 break;
681 case ACPI_NFIT_TYPE_MEMORY_MAP:
682 if (!add_memdev(acpi_desc, prev, table))
683 return err;
684 break;
685 case ACPI_NFIT_TYPE_CONTROL_REGION:
686 if (!add_dcr(acpi_desc, prev, table))
687 return err;
688 break;
689 case ACPI_NFIT_TYPE_DATA_REGION:
690 if (!add_bdw(acpi_desc, prev, table))
691 return err;
692 break;
693 case ACPI_NFIT_TYPE_INTERLEAVE:
694 if (!add_idt(acpi_desc, prev, table))
695 return err;
696 break;
697 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
698 if (!add_flush(acpi_desc, prev, table))
699 return err;
700 break;
701 case ACPI_NFIT_TYPE_SMBIOS:
702 dev_dbg(dev, "%s: smbios\n", __func__);
703 break;
704 default:
705 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
706 break;
707 }
708
709 return table + hdr->length;
710 }
711
nfit_mem_find_spa_bdw(struct acpi_nfit_desc * acpi_desc,struct nfit_mem * nfit_mem)712 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
713 struct nfit_mem *nfit_mem)
714 {
715 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
716 u16 dcr = nfit_mem->dcr->region_index;
717 struct nfit_spa *nfit_spa;
718
719 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
720 u16 range_index = nfit_spa->spa->range_index;
721 int type = nfit_spa_type(nfit_spa->spa);
722 struct nfit_memdev *nfit_memdev;
723
724 if (type != NFIT_SPA_BDW)
725 continue;
726
727 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
728 if (nfit_memdev->memdev->range_index != range_index)
729 continue;
730 if (nfit_memdev->memdev->device_handle != device_handle)
731 continue;
732 if (nfit_memdev->memdev->region_index != dcr)
733 continue;
734
735 nfit_mem->spa_bdw = nfit_spa->spa;
736 return;
737 }
738 }
739
740 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
741 nfit_mem->spa_dcr->range_index);
742 nfit_mem->bdw = NULL;
743 }
744
nfit_mem_init_bdw(struct acpi_nfit_desc * acpi_desc,struct nfit_mem * nfit_mem,struct acpi_nfit_system_address * spa)745 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
746 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
747 {
748 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
749 struct nfit_memdev *nfit_memdev;
750 struct nfit_bdw *nfit_bdw;
751 struct nfit_idt *nfit_idt;
752 u16 idt_idx, range_index;
753
754 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
755 if (nfit_bdw->bdw->region_index != dcr)
756 continue;
757 nfit_mem->bdw = nfit_bdw->bdw;
758 break;
759 }
760
761 if (!nfit_mem->bdw)
762 return;
763
764 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
765
766 if (!nfit_mem->spa_bdw)
767 return;
768
769 range_index = nfit_mem->spa_bdw->range_index;
770 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
771 if (nfit_memdev->memdev->range_index != range_index ||
772 nfit_memdev->memdev->region_index != dcr)
773 continue;
774 nfit_mem->memdev_bdw = nfit_memdev->memdev;
775 idt_idx = nfit_memdev->memdev->interleave_index;
776 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
777 if (nfit_idt->idt->interleave_index != idt_idx)
778 continue;
779 nfit_mem->idt_bdw = nfit_idt->idt;
780 break;
781 }
782 break;
783 }
784 }
785
__nfit_mem_init(struct acpi_nfit_desc * acpi_desc,struct acpi_nfit_system_address * spa)786 static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
787 struct acpi_nfit_system_address *spa)
788 {
789 struct nfit_mem *nfit_mem, *found;
790 struct nfit_memdev *nfit_memdev;
791 int type = spa ? nfit_spa_type(spa) : 0;
792
793 switch (type) {
794 case NFIT_SPA_DCR:
795 case NFIT_SPA_PM:
796 break;
797 default:
798 if (spa)
799 return 0;
800 }
801
802 /*
803 * This loop runs in two modes, when a dimm is mapped the loop
804 * adds memdev associations to an existing dimm, or creates a
805 * dimm. In the unmapped dimm case this loop sweeps for memdev
806 * instances with an invalid / zero range_index and adds those
807 * dimms without spa associations.
808 */
809 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
810 struct nfit_flush *nfit_flush;
811 struct nfit_dcr *nfit_dcr;
812 u32 device_handle;
813 u16 dcr;
814
815 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
816 continue;
817 if (!spa && nfit_memdev->memdev->range_index)
818 continue;
819 found = NULL;
820 dcr = nfit_memdev->memdev->region_index;
821 device_handle = nfit_memdev->memdev->device_handle;
822 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
823 if (__to_nfit_memdev(nfit_mem)->device_handle
824 == device_handle) {
825 found = nfit_mem;
826 break;
827 }
828
829 if (found)
830 nfit_mem = found;
831 else {
832 nfit_mem = devm_kzalloc(acpi_desc->dev,
833 sizeof(*nfit_mem), GFP_KERNEL);
834 if (!nfit_mem)
835 return -ENOMEM;
836 INIT_LIST_HEAD(&nfit_mem->list);
837 nfit_mem->acpi_desc = acpi_desc;
838 list_add(&nfit_mem->list, &acpi_desc->dimms);
839 }
840
841 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
842 if (nfit_dcr->dcr->region_index != dcr)
843 continue;
844 /*
845 * Record the control region for the dimm. For
846 * the ACPI 6.1 case, where there are separate
847 * control regions for the pmem vs blk
848 * interfaces, be sure to record the extended
849 * blk details.
850 */
851 if (!nfit_mem->dcr)
852 nfit_mem->dcr = nfit_dcr->dcr;
853 else if (nfit_mem->dcr->windows == 0
854 && nfit_dcr->dcr->windows)
855 nfit_mem->dcr = nfit_dcr->dcr;
856 break;
857 }
858
859 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
860 struct acpi_nfit_flush_address *flush;
861 u16 i;
862
863 if (nfit_flush->flush->device_handle != device_handle)
864 continue;
865 nfit_mem->nfit_flush = nfit_flush;
866 flush = nfit_flush->flush;
867 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
868 flush->hint_count
869 * sizeof(struct resource), GFP_KERNEL);
870 if (!nfit_mem->flush_wpq)
871 return -ENOMEM;
872 for (i = 0; i < flush->hint_count; i++) {
873 struct resource *res = &nfit_mem->flush_wpq[i];
874
875 res->start = flush->hint_address[i];
876 res->end = res->start + 8 - 1;
877 }
878 break;
879 }
880
881 if (dcr && !nfit_mem->dcr) {
882 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
883 spa->range_index, dcr);
884 return -ENODEV;
885 }
886
887 if (type == NFIT_SPA_DCR) {
888 struct nfit_idt *nfit_idt;
889 u16 idt_idx;
890
891 /* multiple dimms may share a SPA when interleaved */
892 nfit_mem->spa_dcr = spa;
893 nfit_mem->memdev_dcr = nfit_memdev->memdev;
894 idt_idx = nfit_memdev->memdev->interleave_index;
895 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
896 if (nfit_idt->idt->interleave_index != idt_idx)
897 continue;
898 nfit_mem->idt_dcr = nfit_idt->idt;
899 break;
900 }
901 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
902 } else if (type == NFIT_SPA_PM) {
903 /*
904 * A single dimm may belong to multiple SPA-PM
905 * ranges, record at least one in addition to
906 * any SPA-DCR range.
907 */
908 nfit_mem->memdev_pmem = nfit_memdev->memdev;
909 } else
910 nfit_mem->memdev_dcr = nfit_memdev->memdev;
911 }
912
913 return 0;
914 }
915
nfit_mem_cmp(void * priv,struct list_head * _a,struct list_head * _b)916 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
917 {
918 struct nfit_mem *a = container_of(_a, typeof(*a), list);
919 struct nfit_mem *b = container_of(_b, typeof(*b), list);
920 u32 handleA, handleB;
921
922 handleA = __to_nfit_memdev(a)->device_handle;
923 handleB = __to_nfit_memdev(b)->device_handle;
924 if (handleA < handleB)
925 return -1;
926 else if (handleA > handleB)
927 return 1;
928 return 0;
929 }
930
nfit_mem_init(struct acpi_nfit_desc * acpi_desc)931 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
932 {
933 struct nfit_spa *nfit_spa;
934 int rc;
935
936
937 /*
938 * For each SPA-DCR or SPA-PMEM address range find its
939 * corresponding MEMDEV(s). From each MEMDEV find the
940 * corresponding DCR. Then, if we're operating on a SPA-DCR,
941 * try to find a SPA-BDW and a corresponding BDW that references
942 * the DCR. Throw it all into an nfit_mem object. Note, that
943 * BDWs are optional.
944 */
945 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
946 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
947 if (rc)
948 return rc;
949 }
950
951 /*
952 * If a DIMM has failed to be mapped into SPA there will be no
953 * SPA entries above. Find and register all the unmapped DIMMs
954 * for reporting and recovery purposes.
955 */
956 rc = __nfit_mem_init(acpi_desc, NULL);
957 if (rc)
958 return rc;
959
960 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
961
962 return 0;
963 }
964
bus_dsm_mask_show(struct device * dev,struct device_attribute * attr,char * buf)965 static ssize_t bus_dsm_mask_show(struct device *dev,
966 struct device_attribute *attr, char *buf)
967 {
968 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
969 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
970
971 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
972 }
973 static struct device_attribute dev_attr_bus_dsm_mask =
974 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
975
revision_show(struct device * dev,struct device_attribute * attr,char * buf)976 static ssize_t revision_show(struct device *dev,
977 struct device_attribute *attr, char *buf)
978 {
979 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
980 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
981 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
982
983 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
984 }
985 static DEVICE_ATTR_RO(revision);
986
hw_error_scrub_show(struct device * dev,struct device_attribute * attr,char * buf)987 static ssize_t hw_error_scrub_show(struct device *dev,
988 struct device_attribute *attr, char *buf)
989 {
990 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
991 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
992 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
993
994 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
995 }
996
997 /*
998 * The 'hw_error_scrub' attribute can have the following values written to it:
999 * '0': Switch to the default mode where an exception will only insert
1000 * the address of the memory error into the poison and badblocks lists.
1001 * '1': Enable a full scrub to happen if an exception for a memory error is
1002 * received.
1003 */
hw_error_scrub_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1004 static ssize_t hw_error_scrub_store(struct device *dev,
1005 struct device_attribute *attr, const char *buf, size_t size)
1006 {
1007 struct nvdimm_bus_descriptor *nd_desc;
1008 ssize_t rc;
1009 long val;
1010
1011 rc = kstrtol(buf, 0, &val);
1012 if (rc)
1013 return rc;
1014
1015 device_lock(dev);
1016 nd_desc = dev_get_drvdata(dev);
1017 if (nd_desc) {
1018 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1019
1020 switch (val) {
1021 case HW_ERROR_SCRUB_ON:
1022 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
1023 break;
1024 case HW_ERROR_SCRUB_OFF:
1025 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1026 break;
1027 default:
1028 rc = -EINVAL;
1029 break;
1030 }
1031 }
1032 device_unlock(dev);
1033 if (rc)
1034 return rc;
1035 return size;
1036 }
1037 static DEVICE_ATTR_RW(hw_error_scrub);
1038
1039 /*
1040 * This shows the number of full Address Range Scrubs that have been
1041 * completed since driver load time. Userspace can wait on this using
1042 * select/poll etc. A '+' at the end indicates an ARS is in progress
1043 */
scrub_show(struct device * dev,struct device_attribute * attr,char * buf)1044 static ssize_t scrub_show(struct device *dev,
1045 struct device_attribute *attr, char *buf)
1046 {
1047 struct nvdimm_bus_descriptor *nd_desc;
1048 ssize_t rc = -ENXIO;
1049
1050 device_lock(dev);
1051 nd_desc = dev_get_drvdata(dev);
1052 if (nd_desc) {
1053 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1054
1055 mutex_lock(&acpi_desc->init_mutex);
1056 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1057 work_busy(&acpi_desc->work)
1058 && !acpi_desc->cancel ? "+\n" : "\n");
1059 mutex_unlock(&acpi_desc->init_mutex);
1060 }
1061 device_unlock(dev);
1062 return rc;
1063 }
1064
scrub_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)1065 static ssize_t scrub_store(struct device *dev,
1066 struct device_attribute *attr, const char *buf, size_t size)
1067 {
1068 struct nvdimm_bus_descriptor *nd_desc;
1069 ssize_t rc;
1070 long val;
1071
1072 rc = kstrtol(buf, 0, &val);
1073 if (rc)
1074 return rc;
1075 if (val != 1)
1076 return -EINVAL;
1077
1078 device_lock(dev);
1079 nd_desc = dev_get_drvdata(dev);
1080 if (nd_desc) {
1081 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1082
1083 rc = acpi_nfit_ars_rescan(acpi_desc, 0);
1084 }
1085 device_unlock(dev);
1086 if (rc)
1087 return rc;
1088 return size;
1089 }
1090 static DEVICE_ATTR_RW(scrub);
1091
ars_supported(struct nvdimm_bus * nvdimm_bus)1092 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1093 {
1094 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1095 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1096 | 1 << ND_CMD_ARS_STATUS;
1097
1098 return (nd_desc->cmd_mask & mask) == mask;
1099 }
1100
nfit_visible(struct kobject * kobj,struct attribute * a,int n)1101 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1102 {
1103 struct device *dev = container_of(kobj, struct device, kobj);
1104 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1105
1106 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1107 return 0;
1108 return a->mode;
1109 }
1110
1111 static struct attribute *acpi_nfit_attributes[] = {
1112 &dev_attr_revision.attr,
1113 &dev_attr_scrub.attr,
1114 &dev_attr_hw_error_scrub.attr,
1115 &dev_attr_bus_dsm_mask.attr,
1116 NULL,
1117 };
1118
1119 static const struct attribute_group acpi_nfit_attribute_group = {
1120 .name = "nfit",
1121 .attrs = acpi_nfit_attributes,
1122 .is_visible = nfit_visible,
1123 };
1124
1125 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1126 &nvdimm_bus_attribute_group,
1127 &acpi_nfit_attribute_group,
1128 NULL,
1129 };
1130
to_nfit_memdev(struct device * dev)1131 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1132 {
1133 struct nvdimm *nvdimm = to_nvdimm(dev);
1134 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1135
1136 return __to_nfit_memdev(nfit_mem);
1137 }
1138
to_nfit_dcr(struct device * dev)1139 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1140 {
1141 struct nvdimm *nvdimm = to_nvdimm(dev);
1142 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1143
1144 return nfit_mem->dcr;
1145 }
1146
handle_show(struct device * dev,struct device_attribute * attr,char * buf)1147 static ssize_t handle_show(struct device *dev,
1148 struct device_attribute *attr, char *buf)
1149 {
1150 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1151
1152 return sprintf(buf, "%#x\n", memdev->device_handle);
1153 }
1154 static DEVICE_ATTR_RO(handle);
1155
phys_id_show(struct device * dev,struct device_attribute * attr,char * buf)1156 static ssize_t phys_id_show(struct device *dev,
1157 struct device_attribute *attr, char *buf)
1158 {
1159 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1160
1161 return sprintf(buf, "%#x\n", memdev->physical_id);
1162 }
1163 static DEVICE_ATTR_RO(phys_id);
1164
vendor_show(struct device * dev,struct device_attribute * attr,char * buf)1165 static ssize_t vendor_show(struct device *dev,
1166 struct device_attribute *attr, char *buf)
1167 {
1168 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1169
1170 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1171 }
1172 static DEVICE_ATTR_RO(vendor);
1173
rev_id_show(struct device * dev,struct device_attribute * attr,char * buf)1174 static ssize_t rev_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 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1180 }
1181 static DEVICE_ATTR_RO(rev_id);
1182
device_show(struct device * dev,struct device_attribute * attr,char * buf)1183 static ssize_t device_show(struct device *dev,
1184 struct device_attribute *attr, char *buf)
1185 {
1186 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1187
1188 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1189 }
1190 static DEVICE_ATTR_RO(device);
1191
subsystem_vendor_show(struct device * dev,struct device_attribute * attr,char * buf)1192 static ssize_t subsystem_vendor_show(struct device *dev,
1193 struct device_attribute *attr, char *buf)
1194 {
1195 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1196
1197 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1198 }
1199 static DEVICE_ATTR_RO(subsystem_vendor);
1200
subsystem_rev_id_show(struct device * dev,struct device_attribute * attr,char * buf)1201 static ssize_t subsystem_rev_id_show(struct device *dev,
1202 struct device_attribute *attr, char *buf)
1203 {
1204 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1205
1206 return sprintf(buf, "0x%04x\n",
1207 be16_to_cpu(dcr->subsystem_revision_id));
1208 }
1209 static DEVICE_ATTR_RO(subsystem_rev_id);
1210
subsystem_device_show(struct device * dev,struct device_attribute * attr,char * buf)1211 static ssize_t subsystem_device_show(struct device *dev,
1212 struct device_attribute *attr, char *buf)
1213 {
1214 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1215
1216 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1217 }
1218 static DEVICE_ATTR_RO(subsystem_device);
1219
num_nvdimm_formats(struct nvdimm * nvdimm)1220 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1221 {
1222 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1223 int formats = 0;
1224
1225 if (nfit_mem->memdev_pmem)
1226 formats++;
1227 if (nfit_mem->memdev_bdw)
1228 formats++;
1229 return formats;
1230 }
1231
format_show(struct device * dev,struct device_attribute * attr,char * buf)1232 static ssize_t format_show(struct device *dev,
1233 struct device_attribute *attr, char *buf)
1234 {
1235 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1236
1237 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1238 }
1239 static DEVICE_ATTR_RO(format);
1240
format1_show(struct device * dev,struct device_attribute * attr,char * buf)1241 static ssize_t format1_show(struct device *dev,
1242 struct device_attribute *attr, char *buf)
1243 {
1244 u32 handle;
1245 ssize_t rc = -ENXIO;
1246 struct nfit_mem *nfit_mem;
1247 struct nfit_memdev *nfit_memdev;
1248 struct acpi_nfit_desc *acpi_desc;
1249 struct nvdimm *nvdimm = to_nvdimm(dev);
1250 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1251
1252 nfit_mem = nvdimm_provider_data(nvdimm);
1253 acpi_desc = nfit_mem->acpi_desc;
1254 handle = to_nfit_memdev(dev)->device_handle;
1255
1256 /* assumes DIMMs have at most 2 published interface codes */
1257 mutex_lock(&acpi_desc->init_mutex);
1258 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1259 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1260 struct nfit_dcr *nfit_dcr;
1261
1262 if (memdev->device_handle != handle)
1263 continue;
1264
1265 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1266 if (nfit_dcr->dcr->region_index != memdev->region_index)
1267 continue;
1268 if (nfit_dcr->dcr->code == dcr->code)
1269 continue;
1270 rc = sprintf(buf, "0x%04x\n",
1271 le16_to_cpu(nfit_dcr->dcr->code));
1272 break;
1273 }
1274 if (rc != ENXIO)
1275 break;
1276 }
1277 mutex_unlock(&acpi_desc->init_mutex);
1278 return rc;
1279 }
1280 static DEVICE_ATTR_RO(format1);
1281
formats_show(struct device * dev,struct device_attribute * attr,char * buf)1282 static ssize_t formats_show(struct device *dev,
1283 struct device_attribute *attr, char *buf)
1284 {
1285 struct nvdimm *nvdimm = to_nvdimm(dev);
1286
1287 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1288 }
1289 static DEVICE_ATTR_RO(formats);
1290
serial_show(struct device * dev,struct device_attribute * attr,char * buf)1291 static ssize_t serial_show(struct device *dev,
1292 struct device_attribute *attr, char *buf)
1293 {
1294 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1295
1296 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1297 }
1298 static DEVICE_ATTR_RO(serial);
1299
family_show(struct device * dev,struct device_attribute * attr,char * buf)1300 static ssize_t family_show(struct device *dev,
1301 struct device_attribute *attr, char *buf)
1302 {
1303 struct nvdimm *nvdimm = to_nvdimm(dev);
1304 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1305
1306 if (nfit_mem->family < 0)
1307 return -ENXIO;
1308 return sprintf(buf, "%d\n", nfit_mem->family);
1309 }
1310 static DEVICE_ATTR_RO(family);
1311
dsm_mask_show(struct device * dev,struct device_attribute * attr,char * buf)1312 static ssize_t dsm_mask_show(struct device *dev,
1313 struct device_attribute *attr, char *buf)
1314 {
1315 struct nvdimm *nvdimm = to_nvdimm(dev);
1316 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1317
1318 if (nfit_mem->family < 0)
1319 return -ENXIO;
1320 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1321 }
1322 static DEVICE_ATTR_RO(dsm_mask);
1323
flags_show(struct device * dev,struct device_attribute * attr,char * buf)1324 static ssize_t flags_show(struct device *dev,
1325 struct device_attribute *attr, char *buf)
1326 {
1327 u16 flags = to_nfit_memdev(dev)->flags;
1328
1329 return sprintf(buf, "%s%s%s%s%s%s%s\n",
1330 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1331 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1332 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1333 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1334 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1335 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1336 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1337 }
1338 static DEVICE_ATTR_RO(flags);
1339
id_show(struct device * dev,struct device_attribute * attr,char * buf)1340 static ssize_t id_show(struct device *dev,
1341 struct device_attribute *attr, char *buf)
1342 {
1343 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1344
1345 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1346 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1347 be16_to_cpu(dcr->vendor_id),
1348 dcr->manufacturing_location,
1349 be16_to_cpu(dcr->manufacturing_date),
1350 be32_to_cpu(dcr->serial_number));
1351 else
1352 return sprintf(buf, "%04x-%08x\n",
1353 be16_to_cpu(dcr->vendor_id),
1354 be32_to_cpu(dcr->serial_number));
1355 }
1356 static DEVICE_ATTR_RO(id);
1357
1358 static struct attribute *acpi_nfit_dimm_attributes[] = {
1359 &dev_attr_handle.attr,
1360 &dev_attr_phys_id.attr,
1361 &dev_attr_vendor.attr,
1362 &dev_attr_device.attr,
1363 &dev_attr_rev_id.attr,
1364 &dev_attr_subsystem_vendor.attr,
1365 &dev_attr_subsystem_device.attr,
1366 &dev_attr_subsystem_rev_id.attr,
1367 &dev_attr_format.attr,
1368 &dev_attr_formats.attr,
1369 &dev_attr_format1.attr,
1370 &dev_attr_serial.attr,
1371 &dev_attr_flags.attr,
1372 &dev_attr_id.attr,
1373 &dev_attr_family.attr,
1374 &dev_attr_dsm_mask.attr,
1375 NULL,
1376 };
1377
acpi_nfit_dimm_attr_visible(struct kobject * kobj,struct attribute * a,int n)1378 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1379 struct attribute *a, int n)
1380 {
1381 struct device *dev = container_of(kobj, struct device, kobj);
1382 struct nvdimm *nvdimm = to_nvdimm(dev);
1383
1384 if (!to_nfit_dcr(dev)) {
1385 /* Without a dcr only the memdev attributes can be surfaced */
1386 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1387 || a == &dev_attr_flags.attr
1388 || a == &dev_attr_family.attr
1389 || a == &dev_attr_dsm_mask.attr)
1390 return a->mode;
1391 return 0;
1392 }
1393
1394 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1395 return 0;
1396 return a->mode;
1397 }
1398
1399 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1400 .name = "nfit",
1401 .attrs = acpi_nfit_dimm_attributes,
1402 .is_visible = acpi_nfit_dimm_attr_visible,
1403 };
1404
1405 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1406 &nvdimm_attribute_group,
1407 &nd_device_attribute_group,
1408 &acpi_nfit_dimm_attribute_group,
1409 NULL,
1410 };
1411
acpi_nfit_dimm_by_handle(struct acpi_nfit_desc * acpi_desc,u32 device_handle)1412 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1413 u32 device_handle)
1414 {
1415 struct nfit_mem *nfit_mem;
1416
1417 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1418 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1419 return nfit_mem->nvdimm;
1420
1421 return NULL;
1422 }
1423
__acpi_nvdimm_notify(struct device * dev,u32 event)1424 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1425 {
1426 struct nfit_mem *nfit_mem;
1427 struct acpi_nfit_desc *acpi_desc;
1428
1429 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1430 event);
1431
1432 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1433 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1434 event);
1435 return;
1436 }
1437
1438 acpi_desc = dev_get_drvdata(dev->parent);
1439 if (!acpi_desc)
1440 return;
1441
1442 /*
1443 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1444 * is still valid.
1445 */
1446 nfit_mem = dev_get_drvdata(dev);
1447 if (nfit_mem && nfit_mem->flags_attr)
1448 sysfs_notify_dirent(nfit_mem->flags_attr);
1449 }
1450 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1451
acpi_nvdimm_notify(acpi_handle handle,u32 event,void * data)1452 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1453 {
1454 struct acpi_device *adev = data;
1455 struct device *dev = &adev->dev;
1456
1457 device_lock(dev->parent);
1458 __acpi_nvdimm_notify(dev, event);
1459 device_unlock(dev->parent);
1460 }
1461
acpi_nfit_add_dimm(struct acpi_nfit_desc * acpi_desc,struct nfit_mem * nfit_mem,u32 device_handle)1462 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1463 struct nfit_mem *nfit_mem, u32 device_handle)
1464 {
1465 struct acpi_device *adev, *adev_dimm;
1466 struct device *dev = acpi_desc->dev;
1467 unsigned long dsm_mask;
1468 const guid_t *guid;
1469 int i;
1470 int family = -1;
1471
1472 /* nfit test assumes 1:1 relationship between commands and dsms */
1473 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1474 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1475 adev = to_acpi_dev(acpi_desc);
1476 if (!adev)
1477 return 0;
1478
1479 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1480 nfit_mem->adev = adev_dimm;
1481 if (!adev_dimm) {
1482 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1483 device_handle);
1484 return force_enable_dimms ? 0 : -ENODEV;
1485 }
1486
1487 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1488 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1489 dev_err(dev, "%s: notification registration failed\n",
1490 dev_name(&adev_dimm->dev));
1491 return -ENXIO;
1492 }
1493 /*
1494 * Record nfit_mem for the notification path to track back to
1495 * the nfit sysfs attributes for this dimm device object.
1496 */
1497 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1498
1499 /*
1500 * Until standardization materializes we need to consider 4
1501 * different command sets. Note, that checking for function0 (bit0)
1502 * tells us if any commands are reachable through this GUID.
1503 */
1504 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
1505 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1506 if (family < 0 || i == default_dsm_family)
1507 family = i;
1508
1509 /* limit the supported commands to those that are publicly documented */
1510 nfit_mem->family = family;
1511 if (override_dsm_mask && !disable_vendor_specific)
1512 dsm_mask = override_dsm_mask;
1513 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1514 dsm_mask = 0x3fe;
1515 if (disable_vendor_specific)
1516 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1517 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1518 dsm_mask = 0x1c3c76;
1519 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1520 dsm_mask = 0x1fe;
1521 if (disable_vendor_specific)
1522 dsm_mask &= ~(1 << 8);
1523 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1524 dsm_mask = 0xffffffff;
1525 } else {
1526 dev_dbg(dev, "unknown dimm command family\n");
1527 nfit_mem->family = -1;
1528 /* DSMs are optional, continue loading the driver... */
1529 return 0;
1530 }
1531
1532 /*
1533 * Function 0 is the command interrogation function, don't
1534 * export it to potential userspace use, and enable it to be
1535 * used as an error value in acpi_nfit_ctl().
1536 */
1537 dsm_mask &= ~1UL;
1538
1539 guid = to_nfit_uuid(nfit_mem->family);
1540 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1541 if (acpi_check_dsm(adev_dimm->handle, guid, 1, 1ULL << i))
1542 set_bit(i, &nfit_mem->dsm_mask);
1543
1544 return 0;
1545 }
1546
shutdown_dimm_notify(void * data)1547 static void shutdown_dimm_notify(void *data)
1548 {
1549 struct acpi_nfit_desc *acpi_desc = data;
1550 struct nfit_mem *nfit_mem;
1551
1552 mutex_lock(&acpi_desc->init_mutex);
1553 /*
1554 * Clear out the nfit_mem->flags_attr and shut down dimm event
1555 * notifications.
1556 */
1557 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1558 struct acpi_device *adev_dimm = nfit_mem->adev;
1559
1560 if (nfit_mem->flags_attr) {
1561 sysfs_put(nfit_mem->flags_attr);
1562 nfit_mem->flags_attr = NULL;
1563 }
1564 if (adev_dimm) {
1565 acpi_remove_notify_handler(adev_dimm->handle,
1566 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1567 dev_set_drvdata(&adev_dimm->dev, NULL);
1568 }
1569 }
1570 mutex_unlock(&acpi_desc->init_mutex);
1571 }
1572
acpi_nfit_register_dimms(struct acpi_nfit_desc * acpi_desc)1573 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1574 {
1575 struct nfit_mem *nfit_mem;
1576 int dimm_count = 0, rc;
1577 struct nvdimm *nvdimm;
1578
1579 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1580 struct acpi_nfit_flush_address *flush;
1581 unsigned long flags = 0, cmd_mask;
1582 struct nfit_memdev *nfit_memdev;
1583 u32 device_handle;
1584 u16 mem_flags;
1585
1586 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1587 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1588 if (nvdimm) {
1589 dimm_count++;
1590 continue;
1591 }
1592
1593 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1594 set_bit(NDD_ALIASING, &flags);
1595
1596 /* collate flags across all memdevs for this dimm */
1597 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1598 struct acpi_nfit_memory_map *dimm_memdev;
1599
1600 dimm_memdev = __to_nfit_memdev(nfit_mem);
1601 if (dimm_memdev->device_handle
1602 != nfit_memdev->memdev->device_handle)
1603 continue;
1604 dimm_memdev->flags |= nfit_memdev->memdev->flags;
1605 }
1606
1607 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
1608 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
1609 set_bit(NDD_UNARMED, &flags);
1610
1611 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1612 if (rc)
1613 continue;
1614
1615 /*
1616 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1617 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1618 * userspace interface.
1619 */
1620 cmd_mask = 1UL << ND_CMD_CALL;
1621 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1622 cmd_mask |= nfit_mem->dsm_mask;
1623
1624 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1625 : NULL;
1626 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
1627 acpi_nfit_dimm_attribute_groups,
1628 flags, cmd_mask, flush ? flush->hint_count : 0,
1629 nfit_mem->flush_wpq);
1630 if (!nvdimm)
1631 return -ENOMEM;
1632
1633 nfit_mem->nvdimm = nvdimm;
1634 dimm_count++;
1635
1636 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1637 continue;
1638
1639 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
1640 nvdimm_name(nvdimm),
1641 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1642 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1643 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
1644 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1645 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
1646
1647 }
1648
1649 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1650 if (rc)
1651 return rc;
1652
1653 /*
1654 * Now that dimms are successfully registered, and async registration
1655 * is flushed, attempt to enable event notification.
1656 */
1657 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1658 struct kernfs_node *nfit_kernfs;
1659
1660 nvdimm = nfit_mem->nvdimm;
1661 if (!nvdimm)
1662 continue;
1663
1664 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1665 if (nfit_kernfs)
1666 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1667 "flags");
1668 sysfs_put(nfit_kernfs);
1669 if (!nfit_mem->flags_attr)
1670 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1671 nvdimm_name(nvdimm));
1672 }
1673
1674 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1675 acpi_desc);
1676 }
1677
1678 /*
1679 * These constants are private because there are no kernel consumers of
1680 * these commands.
1681 */
1682 enum nfit_aux_cmds {
1683 NFIT_CMD_TRANSLATE_SPA = 5,
1684 NFIT_CMD_ARS_INJECT_SET = 7,
1685 NFIT_CMD_ARS_INJECT_CLEAR = 8,
1686 NFIT_CMD_ARS_INJECT_GET = 9,
1687 };
1688
acpi_nfit_init_dsms(struct acpi_nfit_desc * acpi_desc)1689 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1690 {
1691 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1692 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
1693 struct acpi_device *adev;
1694 unsigned long dsm_mask;
1695 int i;
1696
1697 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
1698 adev = to_acpi_dev(acpi_desc);
1699 if (!adev)
1700 return;
1701
1702 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
1703 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1704 set_bit(i, &nd_desc->cmd_mask);
1705 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
1706
1707 dsm_mask =
1708 (1 << ND_CMD_ARS_CAP) |
1709 (1 << ND_CMD_ARS_START) |
1710 (1 << ND_CMD_ARS_STATUS) |
1711 (1 << ND_CMD_CLEAR_ERROR) |
1712 (1 << NFIT_CMD_TRANSLATE_SPA) |
1713 (1 << NFIT_CMD_ARS_INJECT_SET) |
1714 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
1715 (1 << NFIT_CMD_ARS_INJECT_GET);
1716 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1717 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1718 set_bit(i, &nd_desc->bus_dsm_mask);
1719 }
1720
range_index_show(struct device * dev,struct device_attribute * attr,char * buf)1721 static ssize_t range_index_show(struct device *dev,
1722 struct device_attribute *attr, char *buf)
1723 {
1724 struct nd_region *nd_region = to_nd_region(dev);
1725 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1726
1727 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1728 }
1729 static DEVICE_ATTR_RO(range_index);
1730
ecc_unit_size_show(struct device * dev,struct device_attribute * attr,char * buf)1731 static ssize_t ecc_unit_size_show(struct device *dev,
1732 struct device_attribute *attr, char *buf)
1733 {
1734 struct nd_region *nd_region = to_nd_region(dev);
1735 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1736
1737 return sprintf(buf, "%d\n", nfit_spa->clear_err_unit);
1738 }
1739 static DEVICE_ATTR_RO(ecc_unit_size);
1740
1741 static struct attribute *acpi_nfit_region_attributes[] = {
1742 &dev_attr_range_index.attr,
1743 &dev_attr_ecc_unit_size.attr,
1744 NULL,
1745 };
1746
1747 static const struct attribute_group acpi_nfit_region_attribute_group = {
1748 .name = "nfit",
1749 .attrs = acpi_nfit_region_attributes,
1750 };
1751
1752 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1753 &nd_region_attribute_group,
1754 &nd_mapping_attribute_group,
1755 &nd_device_attribute_group,
1756 &nd_numa_attribute_group,
1757 &acpi_nfit_region_attribute_group,
1758 NULL,
1759 };
1760
1761 /* enough info to uniquely specify an interleave set */
1762 struct nfit_set_info {
1763 struct nfit_set_info_map {
1764 u64 region_offset;
1765 u32 serial_number;
1766 u32 pad;
1767 } mapping[0];
1768 };
1769
1770 struct nfit_set_info2 {
1771 struct nfit_set_info_map2 {
1772 u64 region_offset;
1773 u32 serial_number;
1774 u16 vendor_id;
1775 u16 manufacturing_date;
1776 u8 manufacturing_location;
1777 u8 reserved[31];
1778 } mapping[0];
1779 };
1780
sizeof_nfit_set_info(int num_mappings)1781 static size_t sizeof_nfit_set_info(int num_mappings)
1782 {
1783 return sizeof(struct nfit_set_info)
1784 + num_mappings * sizeof(struct nfit_set_info_map);
1785 }
1786
sizeof_nfit_set_info2(int num_mappings)1787 static size_t sizeof_nfit_set_info2(int num_mappings)
1788 {
1789 return sizeof(struct nfit_set_info2)
1790 + num_mappings * sizeof(struct nfit_set_info_map2);
1791 }
1792
cmp_map_compat(const void * m0,const void * m1)1793 static int cmp_map_compat(const void *m0, const void *m1)
1794 {
1795 const struct nfit_set_info_map *map0 = m0;
1796 const struct nfit_set_info_map *map1 = m1;
1797
1798 return memcmp(&map0->region_offset, &map1->region_offset,
1799 sizeof(u64));
1800 }
1801
cmp_map(const void * m0,const void * m1)1802 static int cmp_map(const void *m0, const void *m1)
1803 {
1804 const struct nfit_set_info_map *map0 = m0;
1805 const struct nfit_set_info_map *map1 = m1;
1806
1807 if (map0->region_offset < map1->region_offset)
1808 return -1;
1809 else if (map0->region_offset > map1->region_offset)
1810 return 1;
1811 return 0;
1812 }
1813
cmp_map2(const void * m0,const void * m1)1814 static int cmp_map2(const void *m0, const void *m1)
1815 {
1816 const struct nfit_set_info_map2 *map0 = m0;
1817 const struct nfit_set_info_map2 *map1 = m1;
1818
1819 if (map0->region_offset < map1->region_offset)
1820 return -1;
1821 else if (map0->region_offset > map1->region_offset)
1822 return 1;
1823 return 0;
1824 }
1825
1826 /* Retrieve the nth entry referencing this spa */
memdev_from_spa(struct acpi_nfit_desc * acpi_desc,u16 range_index,int n)1827 static struct acpi_nfit_memory_map *memdev_from_spa(
1828 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1829 {
1830 struct nfit_memdev *nfit_memdev;
1831
1832 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1833 if (nfit_memdev->memdev->range_index == range_index)
1834 if (n-- == 0)
1835 return nfit_memdev->memdev;
1836 return NULL;
1837 }
1838
acpi_nfit_init_interleave_set(struct acpi_nfit_desc * acpi_desc,struct nd_region_desc * ndr_desc,struct acpi_nfit_system_address * spa)1839 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1840 struct nd_region_desc *ndr_desc,
1841 struct acpi_nfit_system_address *spa)
1842 {
1843 struct device *dev = acpi_desc->dev;
1844 struct nd_interleave_set *nd_set;
1845 u16 nr = ndr_desc->num_mappings;
1846 struct nfit_set_info2 *info2;
1847 struct nfit_set_info *info;
1848 int i;
1849
1850 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1851 if (!nd_set)
1852 return -ENOMEM;
1853 ndr_desc->nd_set = nd_set;
1854 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
1855
1856 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1857 if (!info)
1858 return -ENOMEM;
1859
1860 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
1861 if (!info2)
1862 return -ENOMEM;
1863
1864 for (i = 0; i < nr; i++) {
1865 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
1866 struct nfit_set_info_map *map = &info->mapping[i];
1867 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
1868 struct nvdimm *nvdimm = mapping->nvdimm;
1869 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1870 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1871 spa->range_index, i);
1872 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1873
1874 if (!memdev || !nfit_mem->dcr) {
1875 dev_err(dev, "%s: failed to find DCR\n", __func__);
1876 return -ENODEV;
1877 }
1878
1879 map->region_offset = memdev->region_offset;
1880 map->serial_number = dcr->serial_number;
1881
1882 map2->region_offset = memdev->region_offset;
1883 map2->serial_number = dcr->serial_number;
1884 map2->vendor_id = dcr->vendor_id;
1885 map2->manufacturing_date = dcr->manufacturing_date;
1886 map2->manufacturing_location = dcr->manufacturing_location;
1887 }
1888
1889 /* v1.1 namespaces */
1890 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1891 cmp_map, NULL);
1892 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1893
1894 /* v1.2 namespaces */
1895 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
1896 cmp_map2, NULL);
1897 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
1898
1899 /* support v1.1 namespaces created with the wrong sort order */
1900 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1901 cmp_map_compat, NULL);
1902 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1903
1904 /* record the result of the sort for the mapping position */
1905 for (i = 0; i < nr; i++) {
1906 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
1907 int j;
1908
1909 for (j = 0; j < nr; j++) {
1910 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
1911 struct nvdimm *nvdimm = mapping->nvdimm;
1912 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1913 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1914
1915 if (map2->serial_number == dcr->serial_number &&
1916 map2->vendor_id == dcr->vendor_id &&
1917 map2->manufacturing_date == dcr->manufacturing_date &&
1918 map2->manufacturing_location
1919 == dcr->manufacturing_location) {
1920 mapping->position = i;
1921 break;
1922 }
1923 }
1924 }
1925
1926 ndr_desc->nd_set = nd_set;
1927 devm_kfree(dev, info);
1928 devm_kfree(dev, info2);
1929
1930 return 0;
1931 }
1932
to_interleave_offset(u64 offset,struct nfit_blk_mmio * mmio)1933 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1934 {
1935 struct acpi_nfit_interleave *idt = mmio->idt;
1936 u32 sub_line_offset, line_index, line_offset;
1937 u64 line_no, table_skip_count, table_offset;
1938
1939 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1940 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1941 line_offset = idt->line_offset[line_index]
1942 * mmio->line_size;
1943 table_offset = table_skip_count * mmio->table_size;
1944
1945 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1946 }
1947
read_blk_stat(struct nfit_blk * nfit_blk,unsigned int bw)1948 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
1949 {
1950 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1951 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
1952 const u32 STATUS_MASK = 0x80000037;
1953
1954 if (mmio->num_lines)
1955 offset = to_interleave_offset(offset, mmio);
1956
1957 return readl(mmio->addr.base + offset) & STATUS_MASK;
1958 }
1959
write_blk_ctl(struct nfit_blk * nfit_blk,unsigned int bw,resource_size_t dpa,unsigned int len,unsigned int write)1960 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1961 resource_size_t dpa, unsigned int len, unsigned int write)
1962 {
1963 u64 cmd, offset;
1964 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1965
1966 enum {
1967 BCW_OFFSET_MASK = (1ULL << 48)-1,
1968 BCW_LEN_SHIFT = 48,
1969 BCW_LEN_MASK = (1ULL << 8) - 1,
1970 BCW_CMD_SHIFT = 56,
1971 };
1972
1973 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1974 len = len >> L1_CACHE_SHIFT;
1975 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1976 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1977
1978 offset = nfit_blk->cmd_offset + mmio->size * bw;
1979 if (mmio->num_lines)
1980 offset = to_interleave_offset(offset, mmio);
1981
1982 writeq(cmd, mmio->addr.base + offset);
1983 nvdimm_flush(nfit_blk->nd_region);
1984
1985 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
1986 readq(mmio->addr.base + offset);
1987 }
1988
acpi_nfit_blk_single_io(struct nfit_blk * nfit_blk,resource_size_t dpa,void * iobuf,size_t len,int rw,unsigned int lane)1989 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1990 resource_size_t dpa, void *iobuf, size_t len, int rw,
1991 unsigned int lane)
1992 {
1993 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1994 unsigned int copied = 0;
1995 u64 base_offset;
1996 int rc;
1997
1998 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1999 + lane * mmio->size;
2000 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2001 while (len) {
2002 unsigned int c;
2003 u64 offset;
2004
2005 if (mmio->num_lines) {
2006 u32 line_offset;
2007
2008 offset = to_interleave_offset(base_offset + copied,
2009 mmio);
2010 div_u64_rem(offset, mmio->line_size, &line_offset);
2011 c = min_t(size_t, len, mmio->line_size - line_offset);
2012 } else {
2013 offset = base_offset + nfit_blk->bdw_offset;
2014 c = len;
2015 }
2016
2017 if (rw)
2018 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
2019 else {
2020 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
2021 arch_invalidate_pmem((void __force *)
2022 mmio->addr.aperture + offset, c);
2023
2024 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
2025 }
2026
2027 copied += c;
2028 len -= c;
2029 }
2030
2031 if (rw)
2032 nvdimm_flush(nfit_blk->nd_region);
2033
2034 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2035 return rc;
2036 }
2037
acpi_nfit_blk_region_do_io(struct nd_blk_region * ndbr,resource_size_t dpa,void * iobuf,u64 len,int rw)2038 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2039 resource_size_t dpa, void *iobuf, u64 len, int rw)
2040 {
2041 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2042 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2043 struct nd_region *nd_region = nfit_blk->nd_region;
2044 unsigned int lane, copied = 0;
2045 int rc = 0;
2046
2047 lane = nd_region_acquire_lane(nd_region);
2048 while (len) {
2049 u64 c = min(len, mmio->size);
2050
2051 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2052 iobuf + copied, c, rw, lane);
2053 if (rc)
2054 break;
2055
2056 copied += c;
2057 len -= c;
2058 }
2059 nd_region_release_lane(nd_region, lane);
2060
2061 return rc;
2062 }
2063
nfit_blk_init_interleave(struct nfit_blk_mmio * mmio,struct acpi_nfit_interleave * idt,u16 interleave_ways)2064 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2065 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2066 {
2067 if (idt) {
2068 mmio->num_lines = idt->line_count;
2069 mmio->line_size = idt->line_size;
2070 if (interleave_ways == 0)
2071 return -ENXIO;
2072 mmio->table_size = mmio->num_lines * interleave_ways
2073 * mmio->line_size;
2074 }
2075
2076 return 0;
2077 }
2078
acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor * nd_desc,struct nvdimm * nvdimm,struct nfit_blk * nfit_blk)2079 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2080 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2081 {
2082 struct nd_cmd_dimm_flags flags;
2083 int rc;
2084
2085 memset(&flags, 0, sizeof(flags));
2086 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
2087 sizeof(flags), NULL);
2088
2089 if (rc >= 0 && flags.status == 0)
2090 nfit_blk->dimm_flags = flags.flags;
2091 else if (rc == -ENOTTY) {
2092 /* fall back to a conservative default */
2093 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2094 rc = 0;
2095 } else
2096 rc = -ENXIO;
2097
2098 return rc;
2099 }
2100
acpi_nfit_blk_region_enable(struct nvdimm_bus * nvdimm_bus,struct device * dev)2101 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2102 struct device *dev)
2103 {
2104 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2105 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2106 struct nfit_blk_mmio *mmio;
2107 struct nfit_blk *nfit_blk;
2108 struct nfit_mem *nfit_mem;
2109 struct nvdimm *nvdimm;
2110 int rc;
2111
2112 nvdimm = nd_blk_region_to_dimm(ndbr);
2113 nfit_mem = nvdimm_provider_data(nvdimm);
2114 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2115 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
2116 nfit_mem ? "" : " nfit_mem",
2117 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2118 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2119 return -ENXIO;
2120 }
2121
2122 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2123 if (!nfit_blk)
2124 return -ENOMEM;
2125 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2126 nfit_blk->nd_region = to_nd_region(dev);
2127
2128 /* map block aperture memory */
2129 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2130 mmio = &nfit_blk->mmio[BDW];
2131 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2132 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2133 if (!mmio->addr.base) {
2134 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
2135 nvdimm_name(nvdimm));
2136 return -ENOMEM;
2137 }
2138 mmio->size = nfit_mem->bdw->size;
2139 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2140 mmio->idt = nfit_mem->idt_bdw;
2141 mmio->spa = nfit_mem->spa_bdw;
2142 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2143 nfit_mem->memdev_bdw->interleave_ways);
2144 if (rc) {
2145 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
2146 __func__, nvdimm_name(nvdimm));
2147 return rc;
2148 }
2149
2150 /* map block control memory */
2151 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2152 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2153 mmio = &nfit_blk->mmio[DCR];
2154 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2155 nfit_mem->spa_dcr->length);
2156 if (!mmio->addr.base) {
2157 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
2158 nvdimm_name(nvdimm));
2159 return -ENOMEM;
2160 }
2161 mmio->size = nfit_mem->dcr->window_size;
2162 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2163 mmio->idt = nfit_mem->idt_dcr;
2164 mmio->spa = nfit_mem->spa_dcr;
2165 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2166 nfit_mem->memdev_dcr->interleave_ways);
2167 if (rc) {
2168 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
2169 __func__, nvdimm_name(nvdimm));
2170 return rc;
2171 }
2172
2173 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2174 if (rc < 0) {
2175 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
2176 __func__, nvdimm_name(nvdimm));
2177 return rc;
2178 }
2179
2180 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2181 dev_warn(dev, "unable to guarantee persistence of writes\n");
2182
2183 if (mmio->line_size == 0)
2184 return 0;
2185
2186 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2187 + 8 > mmio->line_size) {
2188 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2189 return -ENXIO;
2190 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2191 + 8 > mmio->line_size) {
2192 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2193 return -ENXIO;
2194 }
2195
2196 return 0;
2197 }
2198
ars_get_cap(struct acpi_nfit_desc * acpi_desc,struct nd_cmd_ars_cap * cmd,struct nfit_spa * nfit_spa)2199 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2200 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2201 {
2202 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2203 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2204 int cmd_rc, rc;
2205
2206 cmd->address = spa->address;
2207 cmd->length = spa->length;
2208 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2209 sizeof(*cmd), &cmd_rc);
2210 if (rc < 0)
2211 return rc;
2212 return cmd_rc;
2213 }
2214
ars_start(struct acpi_nfit_desc * acpi_desc,struct nfit_spa * nfit_spa)2215 static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
2216 {
2217 int rc;
2218 int cmd_rc;
2219 struct nd_cmd_ars_start ars_start;
2220 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2221 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2222
2223 memset(&ars_start, 0, sizeof(ars_start));
2224 ars_start.address = spa->address;
2225 ars_start.length = spa->length;
2226 ars_start.flags = acpi_desc->ars_start_flags;
2227 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2228 ars_start.type = ND_ARS_PERSISTENT;
2229 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2230 ars_start.type = ND_ARS_VOLATILE;
2231 else
2232 return -ENOTTY;
2233
2234 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2235 sizeof(ars_start), &cmd_rc);
2236
2237 if (rc < 0)
2238 return rc;
2239 return cmd_rc;
2240 }
2241
ars_continue(struct acpi_nfit_desc * acpi_desc)2242 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2243 {
2244 int rc, cmd_rc;
2245 struct nd_cmd_ars_start ars_start;
2246 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2247 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2248
2249 memset(&ars_start, 0, sizeof(ars_start));
2250 ars_start.address = ars_status->restart_address;
2251 ars_start.length = ars_status->restart_length;
2252 ars_start.type = ars_status->type;
2253 ars_start.flags = acpi_desc->ars_start_flags;
2254 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2255 sizeof(ars_start), &cmd_rc);
2256 if (rc < 0)
2257 return rc;
2258 return cmd_rc;
2259 }
2260
ars_get_status(struct acpi_nfit_desc * acpi_desc)2261 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2262 {
2263 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2264 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2265 int rc, cmd_rc;
2266
2267 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2268 acpi_desc->ars_status_size, &cmd_rc);
2269 if (rc < 0)
2270 return rc;
2271 return cmd_rc;
2272 }
2273
ars_status_process_records(struct acpi_nfit_desc * acpi_desc,struct nd_cmd_ars_status * ars_status)2274 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
2275 struct nd_cmd_ars_status *ars_status)
2276 {
2277 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2278 int rc;
2279 u32 i;
2280
2281 /*
2282 * First record starts at 44 byte offset from the start of the
2283 * payload.
2284 */
2285 if (ars_status->out_length < 44)
2286 return 0;
2287 for (i = 0; i < ars_status->num_records; i++) {
2288 /* only process full records */
2289 if (ars_status->out_length
2290 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2291 break;
2292 rc = nvdimm_bus_add_poison(nvdimm_bus,
2293 ars_status->records[i].err_address,
2294 ars_status->records[i].length);
2295 if (rc)
2296 return rc;
2297 }
2298 if (i < ars_status->num_records)
2299 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2300
2301 return 0;
2302 }
2303
acpi_nfit_remove_resource(void * data)2304 static void acpi_nfit_remove_resource(void *data)
2305 {
2306 struct resource *res = data;
2307
2308 remove_resource(res);
2309 }
2310
acpi_nfit_insert_resource(struct acpi_nfit_desc * acpi_desc,struct nd_region_desc * ndr_desc)2311 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2312 struct nd_region_desc *ndr_desc)
2313 {
2314 struct resource *res, *nd_res = ndr_desc->res;
2315 int is_pmem, ret;
2316
2317 /* No operation if the region is already registered as PMEM */
2318 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2319 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2320 if (is_pmem == REGION_INTERSECTS)
2321 return 0;
2322
2323 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2324 if (!res)
2325 return -ENOMEM;
2326
2327 res->name = "Persistent Memory";
2328 res->start = nd_res->start;
2329 res->end = nd_res->end;
2330 res->flags = IORESOURCE_MEM;
2331 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2332
2333 ret = insert_resource(&iomem_resource, res);
2334 if (ret)
2335 return ret;
2336
2337 ret = devm_add_action_or_reset(acpi_desc->dev,
2338 acpi_nfit_remove_resource,
2339 res);
2340 if (ret)
2341 return ret;
2342
2343 return 0;
2344 }
2345
acpi_nfit_init_mapping(struct acpi_nfit_desc * acpi_desc,struct nd_mapping_desc * mapping,struct nd_region_desc * ndr_desc,struct acpi_nfit_memory_map * memdev,struct nfit_spa * nfit_spa)2346 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2347 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2348 struct acpi_nfit_memory_map *memdev,
2349 struct nfit_spa *nfit_spa)
2350 {
2351 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2352 memdev->device_handle);
2353 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2354 struct nd_blk_region_desc *ndbr_desc;
2355 struct nfit_mem *nfit_mem;
2356 int rc;
2357
2358 if (!nvdimm) {
2359 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2360 spa->range_index, memdev->device_handle);
2361 return -ENODEV;
2362 }
2363
2364 mapping->nvdimm = nvdimm;
2365 switch (nfit_spa_type(spa)) {
2366 case NFIT_SPA_PM:
2367 case NFIT_SPA_VOLATILE:
2368 mapping->start = memdev->address;
2369 mapping->size = memdev->region_size;
2370 break;
2371 case NFIT_SPA_DCR:
2372 nfit_mem = nvdimm_provider_data(nvdimm);
2373 if (!nfit_mem || !nfit_mem->bdw) {
2374 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2375 spa->range_index, nvdimm_name(nvdimm));
2376 break;
2377 }
2378
2379 mapping->size = nfit_mem->bdw->capacity;
2380 mapping->start = nfit_mem->bdw->start_address;
2381 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2382 ndr_desc->mapping = mapping;
2383 ndr_desc->num_mappings = 1;
2384 ndbr_desc = to_blk_region_desc(ndr_desc);
2385 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2386 ndbr_desc->do_io = acpi_desc->blk_do_io;
2387 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2388 if (rc)
2389 return rc;
2390 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2391 ndr_desc);
2392 if (!nfit_spa->nd_region)
2393 return -ENOMEM;
2394 break;
2395 }
2396
2397 return 0;
2398 }
2399
nfit_spa_is_virtual(struct acpi_nfit_system_address * spa)2400 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2401 {
2402 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2403 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2404 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2405 nfit_spa_type(spa) == NFIT_SPA_PCD);
2406 }
2407
nfit_spa_is_volatile(struct acpi_nfit_system_address * spa)2408 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2409 {
2410 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2411 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2412 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2413 }
2414
acpi_nfit_register_region(struct acpi_nfit_desc * acpi_desc,struct nfit_spa * nfit_spa)2415 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2416 struct nfit_spa *nfit_spa)
2417 {
2418 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2419 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2420 struct nd_blk_region_desc ndbr_desc;
2421 struct nd_region_desc *ndr_desc;
2422 struct nfit_memdev *nfit_memdev;
2423 struct nvdimm_bus *nvdimm_bus;
2424 struct resource res;
2425 int count = 0, rc;
2426
2427 if (nfit_spa->nd_region)
2428 return 0;
2429
2430 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2431 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2432 __func__);
2433 return 0;
2434 }
2435
2436 memset(&res, 0, sizeof(res));
2437 memset(&mappings, 0, sizeof(mappings));
2438 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2439 res.start = spa->address;
2440 res.end = res.start + spa->length - 1;
2441 ndr_desc = &ndbr_desc.ndr_desc;
2442 ndr_desc->res = &res;
2443 ndr_desc->provider_data = nfit_spa;
2444 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2445 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2446 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2447 spa->proximity_domain);
2448 else
2449 ndr_desc->numa_node = NUMA_NO_NODE;
2450
2451 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2452 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2453 struct nd_mapping_desc *mapping;
2454
2455 if (memdev->range_index != spa->range_index)
2456 continue;
2457 if (count >= ND_MAX_MAPPINGS) {
2458 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2459 spa->range_index, ND_MAX_MAPPINGS);
2460 return -ENXIO;
2461 }
2462 mapping = &mappings[count++];
2463 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2464 memdev, nfit_spa);
2465 if (rc)
2466 goto out;
2467 }
2468
2469 ndr_desc->mapping = mappings;
2470 ndr_desc->num_mappings = count;
2471 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2472 if (rc)
2473 goto out;
2474
2475 nvdimm_bus = acpi_desc->nvdimm_bus;
2476 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2477 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2478 if (rc) {
2479 dev_warn(acpi_desc->dev,
2480 "failed to insert pmem resource to iomem: %d\n",
2481 rc);
2482 goto out;
2483 }
2484
2485 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2486 ndr_desc);
2487 if (!nfit_spa->nd_region)
2488 rc = -ENOMEM;
2489 } else if (nfit_spa_is_volatile(spa)) {
2490 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2491 ndr_desc);
2492 if (!nfit_spa->nd_region)
2493 rc = -ENOMEM;
2494 } else if (nfit_spa_is_virtual(spa)) {
2495 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2496 ndr_desc);
2497 if (!nfit_spa->nd_region)
2498 rc = -ENOMEM;
2499 }
2500
2501 out:
2502 if (rc)
2503 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2504 nfit_spa->spa->range_index);
2505 return rc;
2506 }
2507
ars_status_alloc(struct acpi_nfit_desc * acpi_desc,u32 max_ars)2508 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2509 u32 max_ars)
2510 {
2511 struct device *dev = acpi_desc->dev;
2512 struct nd_cmd_ars_status *ars_status;
2513
2514 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2515 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2516 return 0;
2517 }
2518
2519 if (acpi_desc->ars_status)
2520 devm_kfree(dev, acpi_desc->ars_status);
2521 acpi_desc->ars_status = NULL;
2522 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2523 if (!ars_status)
2524 return -ENOMEM;
2525 acpi_desc->ars_status = ars_status;
2526 acpi_desc->ars_status_size = max_ars;
2527 return 0;
2528 }
2529
acpi_nfit_query_poison(struct acpi_nfit_desc * acpi_desc,struct nfit_spa * nfit_spa)2530 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2531 struct nfit_spa *nfit_spa)
2532 {
2533 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2534 int rc;
2535
2536 if (!nfit_spa->max_ars) {
2537 struct nd_cmd_ars_cap ars_cap;
2538
2539 memset(&ars_cap, 0, sizeof(ars_cap));
2540 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2541 if (rc < 0)
2542 return rc;
2543 nfit_spa->max_ars = ars_cap.max_ars_out;
2544 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2545 /* check that the supported scrub types match the spa type */
2546 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2547 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2548 return -ENOTTY;
2549 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2550 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2551 return -ENOTTY;
2552 }
2553
2554 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2555 return -ENOMEM;
2556
2557 rc = ars_get_status(acpi_desc);
2558 if (rc < 0 && rc != -ENOSPC)
2559 return rc;
2560
2561 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
2562 return -ENOMEM;
2563
2564 return 0;
2565 }
2566
acpi_nfit_async_scrub(struct acpi_nfit_desc * acpi_desc,struct nfit_spa * nfit_spa)2567 static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2568 struct nfit_spa *nfit_spa)
2569 {
2570 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2571 unsigned int overflow_retry = scrub_overflow_abort;
2572 u64 init_ars_start = 0, init_ars_len = 0;
2573 struct device *dev = acpi_desc->dev;
2574 unsigned int tmo = scrub_timeout;
2575 int rc;
2576
2577 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
2578 return;
2579
2580 rc = ars_start(acpi_desc, nfit_spa);
2581 /*
2582 * If we timed out the initial scan we'll still be busy here,
2583 * and will wait another timeout before giving up permanently.
2584 */
2585 if (rc < 0 && rc != -EBUSY)
2586 return;
2587
2588 do {
2589 u64 ars_start, ars_len;
2590
2591 if (acpi_desc->cancel)
2592 break;
2593 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2594 if (rc == -ENOTTY)
2595 break;
2596 if (rc == -EBUSY && !tmo) {
2597 dev_warn(dev, "range %d ars timeout, aborting\n",
2598 spa->range_index);
2599 break;
2600 }
2601
2602 if (rc == -EBUSY) {
2603 /*
2604 * Note, entries may be appended to the list
2605 * while the lock is dropped, but the workqueue
2606 * being active prevents entries being deleted /
2607 * freed.
2608 */
2609 mutex_unlock(&acpi_desc->init_mutex);
2610 ssleep(1);
2611 tmo--;
2612 mutex_lock(&acpi_desc->init_mutex);
2613 continue;
2614 }
2615
2616 /* we got some results, but there are more pending... */
2617 if (rc == -ENOSPC && overflow_retry--) {
2618 if (!init_ars_len) {
2619 init_ars_len = acpi_desc->ars_status->length;
2620 init_ars_start = acpi_desc->ars_status->address;
2621 }
2622 rc = ars_continue(acpi_desc);
2623 }
2624
2625 if (rc < 0) {
2626 dev_warn(dev, "range %d ars continuation failed\n",
2627 spa->range_index);
2628 break;
2629 }
2630
2631 if (init_ars_len) {
2632 ars_start = init_ars_start;
2633 ars_len = init_ars_len;
2634 } else {
2635 ars_start = acpi_desc->ars_status->address;
2636 ars_len = acpi_desc->ars_status->length;
2637 }
2638 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2639 spa->range_index, ars_start, ars_len);
2640 /* notify the region about new poison entries */
2641 nvdimm_region_notify(nfit_spa->nd_region,
2642 NVDIMM_REVALIDATE_POISON);
2643 break;
2644 } while (1);
2645 }
2646
acpi_nfit_scrub(struct work_struct * work)2647 static void acpi_nfit_scrub(struct work_struct *work)
2648 {
2649 struct device *dev;
2650 u64 init_scrub_length = 0;
2651 struct nfit_spa *nfit_spa;
2652 u64 init_scrub_address = 0;
2653 bool init_ars_done = false;
2654 struct acpi_nfit_desc *acpi_desc;
2655 unsigned int tmo = scrub_timeout;
2656 unsigned int overflow_retry = scrub_overflow_abort;
2657
2658 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2659 dev = acpi_desc->dev;
2660
2661 /*
2662 * We scrub in 2 phases. The first phase waits for any platform
2663 * firmware initiated scrubs to complete and then we go search for the
2664 * affected spa regions to mark them scanned. In the second phase we
2665 * initiate a directed scrub for every range that was not scrubbed in
2666 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2667 * the first phase, but really only care about running phase 2, where
2668 * regions can be notified of new poison.
2669 */
2670
2671 /* process platform firmware initiated scrubs */
2672 retry:
2673 mutex_lock(&acpi_desc->init_mutex);
2674 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2675 struct nd_cmd_ars_status *ars_status;
2676 struct acpi_nfit_system_address *spa;
2677 u64 ars_start, ars_len;
2678 int rc;
2679
2680 if (acpi_desc->cancel)
2681 break;
2682
2683 if (nfit_spa->nd_region)
2684 continue;
2685
2686 if (init_ars_done) {
2687 /*
2688 * No need to re-query, we're now just
2689 * reconciling all the ranges covered by the
2690 * initial scrub
2691 */
2692 rc = 0;
2693 } else
2694 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2695
2696 if (rc == -ENOTTY) {
2697 /* no ars capability, just register spa and move on */
2698 acpi_nfit_register_region(acpi_desc, nfit_spa);
2699 continue;
2700 }
2701
2702 if (rc == -EBUSY && !tmo) {
2703 /* fallthrough to directed scrub in phase 2 */
2704 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2705 break;
2706 } else if (rc == -EBUSY) {
2707 mutex_unlock(&acpi_desc->init_mutex);
2708 ssleep(1);
2709 tmo--;
2710 goto retry;
2711 }
2712
2713 /* we got some results, but there are more pending... */
2714 if (rc == -ENOSPC && overflow_retry--) {
2715 ars_status = acpi_desc->ars_status;
2716 /*
2717 * Record the original scrub range, so that we
2718 * can recall all the ranges impacted by the
2719 * initial scrub.
2720 */
2721 if (!init_scrub_length) {
2722 init_scrub_length = ars_status->length;
2723 init_scrub_address = ars_status->address;
2724 }
2725 rc = ars_continue(acpi_desc);
2726 if (rc == 0) {
2727 mutex_unlock(&acpi_desc->init_mutex);
2728 goto retry;
2729 }
2730 }
2731
2732 if (rc < 0) {
2733 /*
2734 * Initial scrub failed, we'll give it one more
2735 * try below...
2736 */
2737 break;
2738 }
2739
2740 /* We got some final results, record completed ranges */
2741 ars_status = acpi_desc->ars_status;
2742 if (init_scrub_length) {
2743 ars_start = init_scrub_address;
2744 ars_len = ars_start + init_scrub_length;
2745 } else {
2746 ars_start = ars_status->address;
2747 ars_len = ars_status->length;
2748 }
2749 spa = nfit_spa->spa;
2750
2751 if (!init_ars_done) {
2752 init_ars_done = true;
2753 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2754 ars_start, ars_len);
2755 }
2756 if (ars_start <= spa->address && ars_start + ars_len
2757 >= spa->address + spa->length)
2758 acpi_nfit_register_region(acpi_desc, nfit_spa);
2759 }
2760
2761 /*
2762 * For all the ranges not covered by an initial scrub we still
2763 * want to see if there are errors, but it's ok to discover them
2764 * asynchronously.
2765 */
2766 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2767 /*
2768 * Flag all the ranges that still need scrubbing, but
2769 * register them now to make data available.
2770 */
2771 if (!nfit_spa->nd_region) {
2772 nfit_spa->ars_required = 1;
2773 acpi_nfit_register_region(acpi_desc, nfit_spa);
2774 }
2775 }
2776 acpi_desc->init_complete = 1;
2777
2778 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2779 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
2780 acpi_desc->scrub_count++;
2781 acpi_desc->ars_start_flags = 0;
2782 if (acpi_desc->scrub_count_state)
2783 sysfs_notify_dirent(acpi_desc->scrub_count_state);
2784 mutex_unlock(&acpi_desc->init_mutex);
2785 }
2786
acpi_nfit_register_regions(struct acpi_nfit_desc * acpi_desc)2787 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2788 {
2789 struct nfit_spa *nfit_spa;
2790
2791 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2792 int rc, type = nfit_spa_type(nfit_spa->spa);
2793
2794 /* PMEM and VMEM will be registered by the ARS workqueue */
2795 if (type == NFIT_SPA_PM || type == NFIT_SPA_VOLATILE)
2796 continue;
2797 /* BLK apertures belong to BLK region registration below */
2798 if (type == NFIT_SPA_BDW)
2799 continue;
2800 /* BLK regions don't need to wait for ARS results */
2801 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2802 if (rc)
2803 return rc;
2804 }
2805
2806 acpi_desc->ars_start_flags = 0;
2807 if (!acpi_desc->cancel)
2808 queue_work(nfit_wq, &acpi_desc->work);
2809 return 0;
2810 }
2811
acpi_nfit_check_deletions(struct acpi_nfit_desc * acpi_desc,struct nfit_table_prev * prev)2812 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2813 struct nfit_table_prev *prev)
2814 {
2815 struct device *dev = acpi_desc->dev;
2816
2817 if (!list_empty(&prev->spas) ||
2818 !list_empty(&prev->memdevs) ||
2819 !list_empty(&prev->dcrs) ||
2820 !list_empty(&prev->bdws) ||
2821 !list_empty(&prev->idts) ||
2822 !list_empty(&prev->flushes)) {
2823 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2824 return -ENXIO;
2825 }
2826 return 0;
2827 }
2828
acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc * acpi_desc)2829 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2830 {
2831 struct device *dev = acpi_desc->dev;
2832 struct kernfs_node *nfit;
2833 struct device *bus_dev;
2834
2835 if (!ars_supported(acpi_desc->nvdimm_bus))
2836 return 0;
2837
2838 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2839 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2840 if (!nfit) {
2841 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2842 return -ENODEV;
2843 }
2844 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2845 sysfs_put(nfit);
2846 if (!acpi_desc->scrub_count_state) {
2847 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2848 return -ENODEV;
2849 }
2850
2851 return 0;
2852 }
2853
acpi_nfit_unregister(void * data)2854 static void acpi_nfit_unregister(void *data)
2855 {
2856 struct acpi_nfit_desc *acpi_desc = data;
2857
2858 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2859 }
2860
acpi_nfit_init(struct acpi_nfit_desc * acpi_desc,void * data,acpi_size sz)2861 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
2862 {
2863 struct device *dev = acpi_desc->dev;
2864 struct nfit_table_prev prev;
2865 const void *end;
2866 int rc;
2867
2868 if (!acpi_desc->nvdimm_bus) {
2869 acpi_nfit_init_dsms(acpi_desc);
2870
2871 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2872 &acpi_desc->nd_desc);
2873 if (!acpi_desc->nvdimm_bus)
2874 return -ENOMEM;
2875
2876 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
2877 acpi_desc);
2878 if (rc)
2879 return rc;
2880
2881 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2882 if (rc)
2883 return rc;
2884
2885 /* register this acpi_desc for mce notifications */
2886 mutex_lock(&acpi_desc_lock);
2887 list_add_tail(&acpi_desc->list, &acpi_descs);
2888 mutex_unlock(&acpi_desc_lock);
2889 }
2890
2891 mutex_lock(&acpi_desc->init_mutex);
2892
2893 INIT_LIST_HEAD(&prev.spas);
2894 INIT_LIST_HEAD(&prev.memdevs);
2895 INIT_LIST_HEAD(&prev.dcrs);
2896 INIT_LIST_HEAD(&prev.bdws);
2897 INIT_LIST_HEAD(&prev.idts);
2898 INIT_LIST_HEAD(&prev.flushes);
2899
2900 list_cut_position(&prev.spas, &acpi_desc->spas,
2901 acpi_desc->spas.prev);
2902 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2903 acpi_desc->memdevs.prev);
2904 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2905 acpi_desc->dcrs.prev);
2906 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2907 acpi_desc->bdws.prev);
2908 list_cut_position(&prev.idts, &acpi_desc->idts,
2909 acpi_desc->idts.prev);
2910 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2911 acpi_desc->flushes.prev);
2912
2913 end = data + sz;
2914 while (!IS_ERR_OR_NULL(data))
2915 data = add_table(acpi_desc, &prev, data, end);
2916
2917 if (IS_ERR(data)) {
2918 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2919 PTR_ERR(data));
2920 rc = PTR_ERR(data);
2921 goto out_unlock;
2922 }
2923
2924 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2925 if (rc)
2926 goto out_unlock;
2927
2928 rc = nfit_mem_init(acpi_desc);
2929 if (rc)
2930 goto out_unlock;
2931
2932 rc = acpi_nfit_register_dimms(acpi_desc);
2933 if (rc)
2934 goto out_unlock;
2935
2936 rc = acpi_nfit_register_regions(acpi_desc);
2937
2938 out_unlock:
2939 mutex_unlock(&acpi_desc->init_mutex);
2940 return rc;
2941 }
2942 EXPORT_SYMBOL_GPL(acpi_nfit_init);
2943
2944 struct acpi_nfit_flush_work {
2945 struct work_struct work;
2946 struct completion cmp;
2947 };
2948
flush_probe(struct work_struct * work)2949 static void flush_probe(struct work_struct *work)
2950 {
2951 struct acpi_nfit_flush_work *flush;
2952
2953 flush = container_of(work, typeof(*flush), work);
2954 complete(&flush->cmp);
2955 }
2956
acpi_nfit_flush_probe(struct nvdimm_bus_descriptor * nd_desc)2957 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2958 {
2959 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2960 struct device *dev = acpi_desc->dev;
2961 struct acpi_nfit_flush_work flush;
2962 int rc;
2963
2964 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2965 device_lock(dev);
2966 device_unlock(dev);
2967
2968 /* bounce the init_mutex to make init_complete valid */
2969 mutex_lock(&acpi_desc->init_mutex);
2970 if (acpi_desc->cancel || acpi_desc->init_complete) {
2971 mutex_unlock(&acpi_desc->init_mutex);
2972 return 0;
2973 }
2974
2975 /*
2976 * Scrub work could take 10s of seconds, userspace may give up so we
2977 * need to be interruptible while waiting.
2978 */
2979 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2980 init_completion(&flush.cmp);
2981 queue_work(nfit_wq, &flush.work);
2982 mutex_unlock(&acpi_desc->init_mutex);
2983
2984 rc = wait_for_completion_interruptible(&flush.cmp);
2985 cancel_work_sync(&flush.work);
2986 return rc;
2987 }
2988
acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor * nd_desc,struct nvdimm * nvdimm,unsigned int cmd)2989 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2990 struct nvdimm *nvdimm, unsigned int cmd)
2991 {
2992 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2993
2994 if (nvdimm)
2995 return 0;
2996 if (cmd != ND_CMD_ARS_START)
2997 return 0;
2998
2999 /*
3000 * The kernel and userspace may race to initiate a scrub, but
3001 * the scrub thread is prepared to lose that initial race. It
3002 * just needs guarantees that any ars it initiates are not
3003 * interrupted by any intervening start reqeusts from userspace.
3004 */
3005 if (work_busy(&acpi_desc->work))
3006 return -EBUSY;
3007
3008 return 0;
3009 }
3010
acpi_nfit_ars_rescan(struct acpi_nfit_desc * acpi_desc,u8 flags)3011 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc, u8 flags)
3012 {
3013 struct device *dev = acpi_desc->dev;
3014 struct nfit_spa *nfit_spa;
3015
3016 if (work_busy(&acpi_desc->work))
3017 return -EBUSY;
3018
3019 mutex_lock(&acpi_desc->init_mutex);
3020 if (acpi_desc->cancel) {
3021 mutex_unlock(&acpi_desc->init_mutex);
3022 return 0;
3023 }
3024
3025 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3026 struct acpi_nfit_system_address *spa = nfit_spa->spa;
3027
3028 if (nfit_spa_type(spa) != NFIT_SPA_PM)
3029 continue;
3030
3031 nfit_spa->ars_required = 1;
3032 }
3033 acpi_desc->ars_start_flags = flags;
3034 queue_work(nfit_wq, &acpi_desc->work);
3035 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
3036 mutex_unlock(&acpi_desc->init_mutex);
3037
3038 return 0;
3039 }
3040
acpi_nfit_desc_init(struct acpi_nfit_desc * acpi_desc,struct device * dev)3041 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
3042 {
3043 struct nvdimm_bus_descriptor *nd_desc;
3044
3045 dev_set_drvdata(dev, acpi_desc);
3046 acpi_desc->dev = dev;
3047 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3048 nd_desc = &acpi_desc->nd_desc;
3049 nd_desc->provider_name = "ACPI.NFIT";
3050 nd_desc->module = THIS_MODULE;
3051 nd_desc->ndctl = acpi_nfit_ctl;
3052 nd_desc->flush_probe = acpi_nfit_flush_probe;
3053 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
3054 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3055
3056 INIT_LIST_HEAD(&acpi_desc->spas);
3057 INIT_LIST_HEAD(&acpi_desc->dcrs);
3058 INIT_LIST_HEAD(&acpi_desc->bdws);
3059 INIT_LIST_HEAD(&acpi_desc->idts);
3060 INIT_LIST_HEAD(&acpi_desc->flushes);
3061 INIT_LIST_HEAD(&acpi_desc->memdevs);
3062 INIT_LIST_HEAD(&acpi_desc->dimms);
3063 INIT_LIST_HEAD(&acpi_desc->list);
3064 mutex_init(&acpi_desc->init_mutex);
3065 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
3066 }
3067 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
3068
acpi_nfit_put_table(void * table)3069 static void acpi_nfit_put_table(void *table)
3070 {
3071 acpi_put_table(table);
3072 }
3073
acpi_nfit_shutdown(void * data)3074 void acpi_nfit_shutdown(void *data)
3075 {
3076 struct acpi_nfit_desc *acpi_desc = data;
3077 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3078
3079 /*
3080 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3081 * race teardown
3082 */
3083 mutex_lock(&acpi_desc_lock);
3084 list_del(&acpi_desc->list);
3085 mutex_unlock(&acpi_desc_lock);
3086
3087 mutex_lock(&acpi_desc->init_mutex);
3088 acpi_desc->cancel = 1;
3089 mutex_unlock(&acpi_desc->init_mutex);
3090
3091 /*
3092 * Bounce the nvdimm bus lock to make sure any in-flight
3093 * acpi_nfit_ars_rescan() submissions have had a chance to
3094 * either submit or see ->cancel set.
3095 */
3096 device_lock(bus_dev);
3097 device_unlock(bus_dev);
3098
3099 flush_workqueue(nfit_wq);
3100 }
3101 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3102
acpi_nfit_add(struct acpi_device * adev)3103 static int acpi_nfit_add(struct acpi_device *adev)
3104 {
3105 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3106 struct acpi_nfit_desc *acpi_desc;
3107 struct device *dev = &adev->dev;
3108 struct acpi_table_header *tbl;
3109 acpi_status status = AE_OK;
3110 acpi_size sz;
3111 int rc = 0;
3112
3113 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3114 if (ACPI_FAILURE(status)) {
3115 /* This is ok, we could have an nvdimm hotplugged later */
3116 dev_dbg(dev, "failed to find NFIT at startup\n");
3117 return 0;
3118 }
3119
3120 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3121 if (rc)
3122 return rc;
3123 sz = tbl->length;
3124
3125 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3126 if (!acpi_desc)
3127 return -ENOMEM;
3128 acpi_nfit_desc_init(acpi_desc, &adev->dev);
3129
3130 /* Save the acpi header for exporting the revision via sysfs */
3131 acpi_desc->acpi_header = *tbl;
3132
3133 /* Evaluate _FIT and override with that if present */
3134 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3135 if (ACPI_SUCCESS(status) && buf.length > 0) {
3136 union acpi_object *obj = buf.pointer;
3137
3138 if (obj->type == ACPI_TYPE_BUFFER)
3139 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3140 obj->buffer.length);
3141 else
3142 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
3143 __func__, (int) obj->type);
3144 kfree(buf.pointer);
3145 } else
3146 /* skip over the lead-in header table */
3147 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3148 + sizeof(struct acpi_table_nfit),
3149 sz - sizeof(struct acpi_table_nfit));
3150
3151 if (rc)
3152 return rc;
3153 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3154 }
3155
acpi_nfit_remove(struct acpi_device * adev)3156 static int acpi_nfit_remove(struct acpi_device *adev)
3157 {
3158 /* see acpi_nfit_unregister */
3159 return 0;
3160 }
3161
acpi_nfit_update_notify(struct device * dev,acpi_handle handle)3162 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3163 {
3164 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3165 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3166 union acpi_object *obj;
3167 acpi_status status;
3168 int ret;
3169
3170 if (!dev->driver) {
3171 /* dev->driver may be null if we're being removed */
3172 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
3173 return;
3174 }
3175
3176 if (!acpi_desc) {
3177 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3178 if (!acpi_desc)
3179 return;
3180 acpi_nfit_desc_init(acpi_desc, dev);
3181 } else {
3182 /*
3183 * Finish previous registration before considering new
3184 * regions.
3185 */
3186 flush_workqueue(nfit_wq);
3187 }
3188
3189 /* Evaluate _FIT */
3190 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3191 if (ACPI_FAILURE(status)) {
3192 dev_err(dev, "failed to evaluate _FIT\n");
3193 return;
3194 }
3195
3196 obj = buf.pointer;
3197 if (obj->type == ACPI_TYPE_BUFFER) {
3198 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3199 obj->buffer.length);
3200 if (ret)
3201 dev_err(dev, "failed to merge updated NFIT\n");
3202 } else
3203 dev_err(dev, "Invalid _FIT\n");
3204 kfree(buf.pointer);
3205 }
3206
acpi_nfit_uc_error_notify(struct device * dev,acpi_handle handle)3207 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3208 {
3209 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3210 u8 flags = (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON) ?
3211 0 : ND_ARS_RETURN_PREV_DATA;
3212
3213 acpi_nfit_ars_rescan(acpi_desc, flags);
3214 }
3215
__acpi_nfit_notify(struct device * dev,acpi_handle handle,u32 event)3216 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3217 {
3218 dev_dbg(dev, "%s: event: 0x%x\n", __func__, event);
3219
3220 switch (event) {
3221 case NFIT_NOTIFY_UPDATE:
3222 return acpi_nfit_update_notify(dev, handle);
3223 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3224 return acpi_nfit_uc_error_notify(dev, handle);
3225 default:
3226 return;
3227 }
3228 }
3229 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3230
acpi_nfit_notify(struct acpi_device * adev,u32 event)3231 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3232 {
3233 device_lock(&adev->dev);
3234 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3235 device_unlock(&adev->dev);
3236 }
3237
3238 static const struct acpi_device_id acpi_nfit_ids[] = {
3239 { "ACPI0012", 0 },
3240 { "", 0 },
3241 };
3242 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3243
3244 static struct acpi_driver acpi_nfit_driver = {
3245 .name = KBUILD_MODNAME,
3246 .ids = acpi_nfit_ids,
3247 .ops = {
3248 .add = acpi_nfit_add,
3249 .remove = acpi_nfit_remove,
3250 .notify = acpi_nfit_notify,
3251 },
3252 };
3253
nfit_init(void)3254 static __init int nfit_init(void)
3255 {
3256 int ret;
3257
3258 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3259 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3260 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3261 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3262 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3263 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3264 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3265
3266 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3267 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3268 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3269 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3270 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3271 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3272 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3273 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3274 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3275 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3276 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3277 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3278 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3279
3280 nfit_wq = create_singlethread_workqueue("nfit");
3281 if (!nfit_wq)
3282 return -ENOMEM;
3283
3284 nfit_mce_register();
3285 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3286 if (ret) {
3287 nfit_mce_unregister();
3288 destroy_workqueue(nfit_wq);
3289 }
3290
3291 return ret;
3292
3293 }
3294
nfit_exit(void)3295 static __exit void nfit_exit(void)
3296 {
3297 nfit_mce_unregister();
3298 acpi_bus_unregister_driver(&acpi_nfit_driver);
3299 destroy_workqueue(nfit_wq);
3300 WARN_ON(!list_empty(&acpi_descs));
3301 }
3302
3303 module_init(nfit_init);
3304 module_exit(nfit_exit);
3305 MODULE_LICENSE("GPL v2");
3306 MODULE_AUTHOR("Intel Corporation");
3307