2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
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.
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.
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>
25 #include <asm/cacheflush.h>
29 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
32 #include <linux/io-64-nonatomic-hi-lo.h>
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");
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");
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");
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");
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");
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");
62 LIST_HEAD(acpi_descs);
63 DEFINE_MUTEX(acpi_desc_lock);
65 static struct workqueue_struct *nfit_wq;
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;
76 static guid_t nfit_uuid[NFIT_UUID_MAX];
78 const guid_t *to_nfit_uuid(enum nfit_uuids id)
80 return &nfit_uuid[id];
82 EXPORT_SYMBOL(to_nfit_uuid);
84 static struct acpi_nfit_desc *to_acpi_nfit_desc(
85 struct nvdimm_bus_descriptor *nd_desc)
87 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
90 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
92 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
95 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
98 if (!nd_desc->provider_name
99 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
102 return to_acpi_device(acpi_desc->dev);
105 static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
107 struct nd_cmd_clear_error *clear_err;
108 struct nd_cmd_ars_status *ars_status;
113 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
120 /* No supported scan types for this range */
121 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
122 if ((status >> 16 & flags) == 0)
125 case ND_CMD_ARS_START:
126 /* ARS is in progress */
127 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
134 case ND_CMD_ARS_STATUS:
139 /* Check extended status (Upper two bytes) */
140 if (status == NFIT_ARS_STATUS_DONE)
143 /* ARS is in progress */
144 if (status == NFIT_ARS_STATUS_BUSY)
147 /* No ARS performed for the current boot */
148 if (status == NFIT_ARS_STATUS_NONE)
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.
156 if (status == NFIT_ARS_STATUS_INTR) {
157 if (ars_status->out_length >= 40 && (ars_status->flags
158 & NFIT_ARS_F_OVERFLOW))
167 case ND_CMD_CLEAR_ERROR:
171 if (!clear_err->cleared)
173 if (clear_err->length > clear_err->cleared)
174 return clear_err->cleared;
180 /* all other non-zero status results in an error */
186 static int xlat_nvdimm_status(void *buf, unsigned int cmd, u32 status)
189 case ND_CMD_GET_CONFIG_SIZE:
190 if (status >> 16 & ND_CONFIG_LOCKED)
197 /* all other non-zero status results in an error */
203 static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
207 return xlat_bus_status(buf, cmd, status);
208 return xlat_nvdimm_status(buf, cmd, status);
211 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
212 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
214 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
215 union acpi_object in_obj, in_buf, *out_obj;
216 const struct nd_cmd_desc *desc = NULL;
217 struct device *dev = acpi_desc->dev;
218 struct nd_cmd_pkg *call_pkg = NULL;
219 const char *cmd_name, *dimm_name;
220 unsigned long cmd_mask, dsm_mask;
221 u32 offset, fw_status = 0;
228 if (cmd == ND_CMD_CALL) {
230 func = call_pkg->nd_command;
234 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
235 struct acpi_device *adev = nfit_mem->adev;
239 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
242 dimm_name = nvdimm_name(nvdimm);
243 cmd_name = nvdimm_cmd_name(cmd);
244 cmd_mask = nvdimm_cmd_mask(nvdimm);
245 dsm_mask = nfit_mem->dsm_mask;
246 desc = nd_cmd_dimm_desc(cmd);
247 guid = to_nfit_uuid(nfit_mem->family);
248 handle = adev->handle;
250 struct acpi_device *adev = to_acpi_dev(acpi_desc);
252 cmd_name = nvdimm_bus_cmd_name(cmd);
253 cmd_mask = nd_desc->cmd_mask;
255 if (cmd == ND_CMD_CALL)
256 dsm_mask = nd_desc->bus_dsm_mask;
257 desc = nd_cmd_bus_desc(cmd);
258 guid = to_nfit_uuid(NFIT_DEV_BUS);
259 handle = adev->handle;
263 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
266 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
269 in_obj.type = ACPI_TYPE_PACKAGE;
270 in_obj.package.count = 1;
271 in_obj.package.elements = &in_buf;
272 in_buf.type = ACPI_TYPE_BUFFER;
273 in_buf.buffer.pointer = buf;
274 in_buf.buffer.length = 0;
276 /* libnvdimm has already validated the input envelope */
277 for (i = 0; i < desc->in_num; i++)
278 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
282 /* skip over package wrapper */
283 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
284 in_buf.buffer.length = call_pkg->nd_size_in;
287 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
288 __func__, dimm_name, cmd, func, in_buf.buffer.length);
289 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
290 in_buf.buffer.pointer,
291 min_t(u32, 256, in_buf.buffer.length), true);
293 out_obj = acpi_evaluate_dsm(handle, guid, 1, func, &in_obj);
295 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
301 call_pkg->nd_fw_size = out_obj->buffer.length;
302 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
303 out_obj->buffer.pointer,
304 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
308 * Need to support FW function w/o known size in advance.
309 * Caller can determine required size based upon nd_fw_size.
310 * If we return an error (like elsewhere) then caller wouldn't
311 * be able to rely upon data returned to make calculation.
316 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
317 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
318 __func__, dimm_name, cmd_name, out_obj->type);
323 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, dimm_name,
324 cmd_name, out_obj->buffer.length);
325 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
326 out_obj->buffer.pointer,
327 min_t(u32, 128, out_obj->buffer.length), true);
329 for (i = 0, offset = 0; i < desc->out_num; i++) {
330 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
331 (u32 *) out_obj->buffer.pointer,
332 out_obj->buffer.length - offset);
334 if (offset + out_size > out_obj->buffer.length) {
335 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
336 __func__, dimm_name, cmd_name, i);
340 if (in_buf.buffer.length + offset + out_size > buf_len) {
341 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
342 __func__, dimm_name, cmd_name, i);
346 memcpy(buf + in_buf.buffer.length + offset,
347 out_obj->buffer.pointer + offset, out_size);
352 * Set fw_status for all the commands with a known format to be
353 * later interpreted by xlat_status().
355 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
356 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
357 fw_status = *(u32 *) out_obj->buffer.pointer;
359 if (offset + in_buf.buffer.length < buf_len) {
362 * status valid, return the number of bytes left
363 * unfilled in the output buffer
365 rc = buf_len - offset - in_buf.buffer.length;
367 *cmd_rc = xlat_status(nvdimm, buf, cmd,
370 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
371 __func__, dimm_name, cmd_name, buf_len,
378 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
386 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
388 static const char *spa_type_name(u16 type)
390 static const char *to_name[] = {
391 [NFIT_SPA_VOLATILE] = "volatile",
392 [NFIT_SPA_PM] = "pmem",
393 [NFIT_SPA_DCR] = "dimm-control-region",
394 [NFIT_SPA_BDW] = "block-data-window",
395 [NFIT_SPA_VDISK] = "volatile-disk",
396 [NFIT_SPA_VCD] = "volatile-cd",
397 [NFIT_SPA_PDISK] = "persistent-disk",
398 [NFIT_SPA_PCD] = "persistent-cd",
402 if (type > NFIT_SPA_PCD)
405 return to_name[type];
408 int nfit_spa_type(struct acpi_nfit_system_address *spa)
412 for (i = 0; i < NFIT_UUID_MAX; i++)
413 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
418 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
419 struct nfit_table_prev *prev,
420 struct acpi_nfit_system_address *spa)
422 struct device *dev = acpi_desc->dev;
423 struct nfit_spa *nfit_spa;
425 if (spa->header.length != sizeof(*spa))
428 list_for_each_entry(nfit_spa, &prev->spas, list) {
429 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
430 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
435 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
439 INIT_LIST_HEAD(&nfit_spa->list);
440 memcpy(nfit_spa->spa, spa, sizeof(*spa));
441 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
442 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
444 spa_type_name(nfit_spa_type(spa)));
448 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
449 struct nfit_table_prev *prev,
450 struct acpi_nfit_memory_map *memdev)
452 struct device *dev = acpi_desc->dev;
453 struct nfit_memdev *nfit_memdev;
455 if (memdev->header.length != sizeof(*memdev))
458 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
459 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
460 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
464 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
468 INIT_LIST_HEAD(&nfit_memdev->list);
469 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
470 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
471 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
472 __func__, memdev->device_handle, memdev->range_index,
473 memdev->region_index, memdev->flags);
478 * An implementation may provide a truncated control region if no block windows
481 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
483 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
488 return offsetof(struct acpi_nfit_control_region, window_size);
491 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
492 struct nfit_table_prev *prev,
493 struct acpi_nfit_control_region *dcr)
495 struct device *dev = acpi_desc->dev;
496 struct nfit_dcr *nfit_dcr;
498 if (!sizeof_dcr(dcr))
501 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
502 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
503 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
507 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
511 INIT_LIST_HEAD(&nfit_dcr->list);
512 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
513 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
514 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
515 dcr->region_index, dcr->windows);
519 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
520 struct nfit_table_prev *prev,
521 struct acpi_nfit_data_region *bdw)
523 struct device *dev = acpi_desc->dev;
524 struct nfit_bdw *nfit_bdw;
526 if (bdw->header.length != sizeof(*bdw))
528 list_for_each_entry(nfit_bdw, &prev->bdws, list)
529 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
530 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
534 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
538 INIT_LIST_HEAD(&nfit_bdw->list);
539 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
540 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
541 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
542 bdw->region_index, bdw->windows);
546 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
548 if (idt->header.length < sizeof(*idt))
550 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
553 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
554 struct nfit_table_prev *prev,
555 struct acpi_nfit_interleave *idt)
557 struct device *dev = acpi_desc->dev;
558 struct nfit_idt *nfit_idt;
560 if (!sizeof_idt(idt))
563 list_for_each_entry(nfit_idt, &prev->idts, list) {
564 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
567 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
568 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
573 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
577 INIT_LIST_HEAD(&nfit_idt->list);
578 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
579 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
580 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
581 idt->interleave_index, idt->line_count);
585 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
587 if (flush->header.length < sizeof(*flush))
589 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
592 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
593 struct nfit_table_prev *prev,
594 struct acpi_nfit_flush_address *flush)
596 struct device *dev = acpi_desc->dev;
597 struct nfit_flush *nfit_flush;
599 if (!sizeof_flush(flush))
602 list_for_each_entry(nfit_flush, &prev->flushes, list) {
603 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
606 if (memcmp(nfit_flush->flush, flush,
607 sizeof_flush(flush)) == 0) {
608 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
613 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
614 + sizeof_flush(flush), GFP_KERNEL);
617 INIT_LIST_HEAD(&nfit_flush->list);
618 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
619 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
620 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
621 flush->device_handle, flush->hint_count);
625 static void *add_table(struct acpi_nfit_desc *acpi_desc,
626 struct nfit_table_prev *prev, void *table, const void *end)
628 struct device *dev = acpi_desc->dev;
629 struct acpi_nfit_header *hdr;
630 void *err = ERR_PTR(-ENOMEM);
637 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
643 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
644 if (!add_spa(acpi_desc, prev, table))
647 case ACPI_NFIT_TYPE_MEMORY_MAP:
648 if (!add_memdev(acpi_desc, prev, table))
651 case ACPI_NFIT_TYPE_CONTROL_REGION:
652 if (!add_dcr(acpi_desc, prev, table))
655 case ACPI_NFIT_TYPE_DATA_REGION:
656 if (!add_bdw(acpi_desc, prev, table))
659 case ACPI_NFIT_TYPE_INTERLEAVE:
660 if (!add_idt(acpi_desc, prev, table))
663 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
664 if (!add_flush(acpi_desc, prev, table))
667 case ACPI_NFIT_TYPE_SMBIOS:
668 dev_dbg(dev, "%s: smbios\n", __func__);
671 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
675 return table + hdr->length;
678 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
679 struct nfit_mem *nfit_mem)
681 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
682 u16 dcr = nfit_mem->dcr->region_index;
683 struct nfit_spa *nfit_spa;
685 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
686 u16 range_index = nfit_spa->spa->range_index;
687 int type = nfit_spa_type(nfit_spa->spa);
688 struct nfit_memdev *nfit_memdev;
690 if (type != NFIT_SPA_BDW)
693 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
694 if (nfit_memdev->memdev->range_index != range_index)
696 if (nfit_memdev->memdev->device_handle != device_handle)
698 if (nfit_memdev->memdev->region_index != dcr)
701 nfit_mem->spa_bdw = nfit_spa->spa;
706 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
707 nfit_mem->spa_dcr->range_index);
708 nfit_mem->bdw = NULL;
711 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
712 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
714 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
715 struct nfit_memdev *nfit_memdev;
716 struct nfit_bdw *nfit_bdw;
717 struct nfit_idt *nfit_idt;
718 u16 idt_idx, range_index;
720 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
721 if (nfit_bdw->bdw->region_index != dcr)
723 nfit_mem->bdw = nfit_bdw->bdw;
730 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
732 if (!nfit_mem->spa_bdw)
735 range_index = nfit_mem->spa_bdw->range_index;
736 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
737 if (nfit_memdev->memdev->range_index != range_index ||
738 nfit_memdev->memdev->region_index != dcr)
740 nfit_mem->memdev_bdw = nfit_memdev->memdev;
741 idt_idx = nfit_memdev->memdev->interleave_index;
742 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
743 if (nfit_idt->idt->interleave_index != idt_idx)
745 nfit_mem->idt_bdw = nfit_idt->idt;
752 static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
753 struct acpi_nfit_system_address *spa)
755 struct nfit_mem *nfit_mem, *found;
756 struct nfit_memdev *nfit_memdev;
757 int type = spa ? nfit_spa_type(spa) : 0;
769 * This loop runs in two modes, when a dimm is mapped the loop
770 * adds memdev associations to an existing dimm, or creates a
771 * dimm. In the unmapped dimm case this loop sweeps for memdev
772 * instances with an invalid / zero range_index and adds those
773 * dimms without spa associations.
775 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
776 struct nfit_flush *nfit_flush;
777 struct nfit_dcr *nfit_dcr;
781 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
783 if (!spa && nfit_memdev->memdev->range_index)
786 dcr = nfit_memdev->memdev->region_index;
787 device_handle = nfit_memdev->memdev->device_handle;
788 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
789 if (__to_nfit_memdev(nfit_mem)->device_handle
798 nfit_mem = devm_kzalloc(acpi_desc->dev,
799 sizeof(*nfit_mem), GFP_KERNEL);
802 INIT_LIST_HEAD(&nfit_mem->list);
803 nfit_mem->acpi_desc = acpi_desc;
804 list_add(&nfit_mem->list, &acpi_desc->dimms);
807 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
808 if (nfit_dcr->dcr->region_index != dcr)
811 * Record the control region for the dimm. For
812 * the ACPI 6.1 case, where there are separate
813 * control regions for the pmem vs blk
814 * interfaces, be sure to record the extended
818 nfit_mem->dcr = nfit_dcr->dcr;
819 else if (nfit_mem->dcr->windows == 0
820 && nfit_dcr->dcr->windows)
821 nfit_mem->dcr = nfit_dcr->dcr;
825 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
826 struct acpi_nfit_flush_address *flush;
829 if (nfit_flush->flush->device_handle != device_handle)
831 nfit_mem->nfit_flush = nfit_flush;
832 flush = nfit_flush->flush;
833 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
835 * sizeof(struct resource), GFP_KERNEL);
836 if (!nfit_mem->flush_wpq)
838 for (i = 0; i < flush->hint_count; i++) {
839 struct resource *res = &nfit_mem->flush_wpq[i];
841 res->start = flush->hint_address[i];
842 res->end = res->start + 8 - 1;
847 if (dcr && !nfit_mem->dcr) {
848 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
849 spa->range_index, dcr);
853 if (type == NFIT_SPA_DCR) {
854 struct nfit_idt *nfit_idt;
857 /* multiple dimms may share a SPA when interleaved */
858 nfit_mem->spa_dcr = spa;
859 nfit_mem->memdev_dcr = nfit_memdev->memdev;
860 idt_idx = nfit_memdev->memdev->interleave_index;
861 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
862 if (nfit_idt->idt->interleave_index != idt_idx)
864 nfit_mem->idt_dcr = nfit_idt->idt;
867 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
868 } else if (type == NFIT_SPA_PM) {
870 * A single dimm may belong to multiple SPA-PM
871 * ranges, record at least one in addition to
874 nfit_mem->memdev_pmem = nfit_memdev->memdev;
876 nfit_mem->memdev_dcr = nfit_memdev->memdev;
882 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
884 struct nfit_mem *a = container_of(_a, typeof(*a), list);
885 struct nfit_mem *b = container_of(_b, typeof(*b), list);
886 u32 handleA, handleB;
888 handleA = __to_nfit_memdev(a)->device_handle;
889 handleB = __to_nfit_memdev(b)->device_handle;
890 if (handleA < handleB)
892 else if (handleA > handleB)
897 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
899 struct nfit_spa *nfit_spa;
904 * For each SPA-DCR or SPA-PMEM address range find its
905 * corresponding MEMDEV(s). From each MEMDEV find the
906 * corresponding DCR. Then, if we're operating on a SPA-DCR,
907 * try to find a SPA-BDW and a corresponding BDW that references
908 * the DCR. Throw it all into an nfit_mem object. Note, that
911 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
912 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
918 * If a DIMM has failed to be mapped into SPA there will be no
919 * SPA entries above. Find and register all the unmapped DIMMs
920 * for reporting and recovery purposes.
922 rc = __nfit_mem_init(acpi_desc, NULL);
926 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
931 static ssize_t bus_dsm_mask_show(struct device *dev,
932 struct device_attribute *attr, char *buf)
934 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
935 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
937 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
939 static struct device_attribute dev_attr_bus_dsm_mask =
940 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
942 static ssize_t revision_show(struct device *dev,
943 struct device_attribute *attr, char *buf)
945 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
946 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
947 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
949 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
951 static DEVICE_ATTR_RO(revision);
953 static ssize_t hw_error_scrub_show(struct device *dev,
954 struct device_attribute *attr, char *buf)
956 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
957 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
958 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
960 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
964 * The 'hw_error_scrub' attribute can have the following values written to it:
965 * '0': Switch to the default mode where an exception will only insert
966 * the address of the memory error into the poison and badblocks lists.
967 * '1': Enable a full scrub to happen if an exception for a memory error is
970 static ssize_t hw_error_scrub_store(struct device *dev,
971 struct device_attribute *attr, const char *buf, size_t size)
973 struct nvdimm_bus_descriptor *nd_desc;
977 rc = kstrtol(buf, 0, &val);
982 nd_desc = dev_get_drvdata(dev);
984 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
987 case HW_ERROR_SCRUB_ON:
988 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
990 case HW_ERROR_SCRUB_OFF:
991 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1003 static DEVICE_ATTR_RW(hw_error_scrub);
1006 * This shows the number of full Address Range Scrubs that have been
1007 * completed since driver load time. Userspace can wait on this using
1008 * select/poll etc. A '+' at the end indicates an ARS is in progress
1010 static ssize_t scrub_show(struct device *dev,
1011 struct device_attribute *attr, char *buf)
1013 struct nvdimm_bus_descriptor *nd_desc;
1014 ssize_t rc = -ENXIO;
1017 nd_desc = dev_get_drvdata(dev);
1019 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1021 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1022 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
1028 static ssize_t scrub_store(struct device *dev,
1029 struct device_attribute *attr, const char *buf, size_t size)
1031 struct nvdimm_bus_descriptor *nd_desc;
1035 rc = kstrtol(buf, 0, &val);
1042 nd_desc = dev_get_drvdata(dev);
1044 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1046 rc = acpi_nfit_ars_rescan(acpi_desc, 0);
1053 static DEVICE_ATTR_RW(scrub);
1055 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1057 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1058 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1059 | 1 << ND_CMD_ARS_STATUS;
1061 return (nd_desc->cmd_mask & mask) == mask;
1064 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1066 struct device *dev = container_of(kobj, struct device, kobj);
1067 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1069 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1074 static struct attribute *acpi_nfit_attributes[] = {
1075 &dev_attr_revision.attr,
1076 &dev_attr_scrub.attr,
1077 &dev_attr_hw_error_scrub.attr,
1078 &dev_attr_bus_dsm_mask.attr,
1082 static const struct attribute_group acpi_nfit_attribute_group = {
1084 .attrs = acpi_nfit_attributes,
1085 .is_visible = nfit_visible,
1088 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1089 &nvdimm_bus_attribute_group,
1090 &acpi_nfit_attribute_group,
1094 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1096 struct nvdimm *nvdimm = to_nvdimm(dev);
1097 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1099 return __to_nfit_memdev(nfit_mem);
1102 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1104 struct nvdimm *nvdimm = to_nvdimm(dev);
1105 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1107 return nfit_mem->dcr;
1110 static ssize_t handle_show(struct device *dev,
1111 struct device_attribute *attr, char *buf)
1113 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1115 return sprintf(buf, "%#x\n", memdev->device_handle);
1117 static DEVICE_ATTR_RO(handle);
1119 static ssize_t phys_id_show(struct device *dev,
1120 struct device_attribute *attr, char *buf)
1122 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1124 return sprintf(buf, "%#x\n", memdev->physical_id);
1126 static DEVICE_ATTR_RO(phys_id);
1128 static ssize_t vendor_show(struct device *dev,
1129 struct device_attribute *attr, char *buf)
1131 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1133 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1135 static DEVICE_ATTR_RO(vendor);
1137 static ssize_t rev_id_show(struct device *dev,
1138 struct device_attribute *attr, char *buf)
1140 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1142 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1144 static DEVICE_ATTR_RO(rev_id);
1146 static ssize_t device_show(struct device *dev,
1147 struct device_attribute *attr, char *buf)
1149 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1151 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1153 static DEVICE_ATTR_RO(device);
1155 static ssize_t subsystem_vendor_show(struct device *dev,
1156 struct device_attribute *attr, char *buf)
1158 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1160 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1162 static DEVICE_ATTR_RO(subsystem_vendor);
1164 static ssize_t subsystem_rev_id_show(struct device *dev,
1165 struct device_attribute *attr, char *buf)
1167 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1169 return sprintf(buf, "0x%04x\n",
1170 be16_to_cpu(dcr->subsystem_revision_id));
1172 static DEVICE_ATTR_RO(subsystem_rev_id);
1174 static ssize_t subsystem_device_show(struct device *dev,
1175 struct device_attribute *attr, char *buf)
1177 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1179 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1181 static DEVICE_ATTR_RO(subsystem_device);
1183 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1185 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1188 if (nfit_mem->memdev_pmem)
1190 if (nfit_mem->memdev_bdw)
1195 static ssize_t format_show(struct device *dev,
1196 struct device_attribute *attr, char *buf)
1198 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1200 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1202 static DEVICE_ATTR_RO(format);
1204 static ssize_t format1_show(struct device *dev,
1205 struct device_attribute *attr, char *buf)
1208 ssize_t rc = -ENXIO;
1209 struct nfit_mem *nfit_mem;
1210 struct nfit_memdev *nfit_memdev;
1211 struct acpi_nfit_desc *acpi_desc;
1212 struct nvdimm *nvdimm = to_nvdimm(dev);
1213 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1215 nfit_mem = nvdimm_provider_data(nvdimm);
1216 acpi_desc = nfit_mem->acpi_desc;
1217 handle = to_nfit_memdev(dev)->device_handle;
1219 /* assumes DIMMs have at most 2 published interface codes */
1220 mutex_lock(&acpi_desc->init_mutex);
1221 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1222 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1223 struct nfit_dcr *nfit_dcr;
1225 if (memdev->device_handle != handle)
1228 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1229 if (nfit_dcr->dcr->region_index != memdev->region_index)
1231 if (nfit_dcr->dcr->code == dcr->code)
1233 rc = sprintf(buf, "0x%04x\n",
1234 le16_to_cpu(nfit_dcr->dcr->code));
1240 mutex_unlock(&acpi_desc->init_mutex);
1243 static DEVICE_ATTR_RO(format1);
1245 static ssize_t formats_show(struct device *dev,
1246 struct device_attribute *attr, char *buf)
1248 struct nvdimm *nvdimm = to_nvdimm(dev);
1250 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1252 static DEVICE_ATTR_RO(formats);
1254 static ssize_t serial_show(struct device *dev,
1255 struct device_attribute *attr, char *buf)
1257 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1259 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1261 static DEVICE_ATTR_RO(serial);
1263 static ssize_t family_show(struct device *dev,
1264 struct device_attribute *attr, char *buf)
1266 struct nvdimm *nvdimm = to_nvdimm(dev);
1267 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1269 if (nfit_mem->family < 0)
1271 return sprintf(buf, "%d\n", nfit_mem->family);
1273 static DEVICE_ATTR_RO(family);
1275 static ssize_t dsm_mask_show(struct device *dev,
1276 struct device_attribute *attr, char *buf)
1278 struct nvdimm *nvdimm = to_nvdimm(dev);
1279 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1281 if (nfit_mem->family < 0)
1283 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1285 static DEVICE_ATTR_RO(dsm_mask);
1287 static ssize_t flags_show(struct device *dev,
1288 struct device_attribute *attr, char *buf)
1290 u16 flags = to_nfit_memdev(dev)->flags;
1292 return sprintf(buf, "%s%s%s%s%s%s%s\n",
1293 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1294 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1295 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1296 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1297 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1298 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1299 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1301 static DEVICE_ATTR_RO(flags);
1303 static ssize_t id_show(struct device *dev,
1304 struct device_attribute *attr, char *buf)
1306 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1308 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1309 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1310 be16_to_cpu(dcr->vendor_id),
1311 dcr->manufacturing_location,
1312 be16_to_cpu(dcr->manufacturing_date),
1313 be32_to_cpu(dcr->serial_number));
1315 return sprintf(buf, "%04x-%08x\n",
1316 be16_to_cpu(dcr->vendor_id),
1317 be32_to_cpu(dcr->serial_number));
1319 static DEVICE_ATTR_RO(id);
1321 static struct attribute *acpi_nfit_dimm_attributes[] = {
1322 &dev_attr_handle.attr,
1323 &dev_attr_phys_id.attr,
1324 &dev_attr_vendor.attr,
1325 &dev_attr_device.attr,
1326 &dev_attr_rev_id.attr,
1327 &dev_attr_subsystem_vendor.attr,
1328 &dev_attr_subsystem_device.attr,
1329 &dev_attr_subsystem_rev_id.attr,
1330 &dev_attr_format.attr,
1331 &dev_attr_formats.attr,
1332 &dev_attr_format1.attr,
1333 &dev_attr_serial.attr,
1334 &dev_attr_flags.attr,
1336 &dev_attr_family.attr,
1337 &dev_attr_dsm_mask.attr,
1341 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1342 struct attribute *a, int n)
1344 struct device *dev = container_of(kobj, struct device, kobj);
1345 struct nvdimm *nvdimm = to_nvdimm(dev);
1347 if (!to_nfit_dcr(dev)) {
1348 /* Without a dcr only the memdev attributes can be surfaced */
1349 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1350 || a == &dev_attr_flags.attr
1351 || a == &dev_attr_family.attr
1352 || a == &dev_attr_dsm_mask.attr)
1357 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1362 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1364 .attrs = acpi_nfit_dimm_attributes,
1365 .is_visible = acpi_nfit_dimm_attr_visible,
1368 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1369 &nvdimm_attribute_group,
1370 &nd_device_attribute_group,
1371 &acpi_nfit_dimm_attribute_group,
1375 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1378 struct nfit_mem *nfit_mem;
1380 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1381 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1382 return nfit_mem->nvdimm;
1387 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1389 struct nfit_mem *nfit_mem;
1390 struct acpi_nfit_desc *acpi_desc;
1392 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1395 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1396 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1401 acpi_desc = dev_get_drvdata(dev->parent);
1406 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1409 nfit_mem = dev_get_drvdata(dev);
1410 if (nfit_mem && nfit_mem->flags_attr)
1411 sysfs_notify_dirent(nfit_mem->flags_attr);
1413 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1415 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1417 struct acpi_device *adev = data;
1418 struct device *dev = &adev->dev;
1420 device_lock(dev->parent);
1421 __acpi_nvdimm_notify(dev, event);
1422 device_unlock(dev->parent);
1425 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1426 struct nfit_mem *nfit_mem, u32 device_handle)
1428 struct acpi_device *adev, *adev_dimm;
1429 struct device *dev = acpi_desc->dev;
1430 unsigned long dsm_mask;
1435 /* nfit test assumes 1:1 relationship between commands and dsms */
1436 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1437 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1438 adev = to_acpi_dev(acpi_desc);
1442 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1443 nfit_mem->adev = adev_dimm;
1445 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1447 return force_enable_dimms ? 0 : -ENODEV;
1450 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1451 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1452 dev_err(dev, "%s: notification registration failed\n",
1453 dev_name(&adev_dimm->dev));
1458 * Until standardization materializes we need to consider 4
1459 * different command sets. Note, that checking for function0 (bit0)
1460 * tells us if any commands are reachable through this GUID.
1462 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
1463 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1464 if (family < 0 || i == default_dsm_family)
1467 /* limit the supported commands to those that are publicly documented */
1468 nfit_mem->family = family;
1469 if (override_dsm_mask && !disable_vendor_specific)
1470 dsm_mask = override_dsm_mask;
1471 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1473 if (disable_vendor_specific)
1474 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1475 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1476 dsm_mask = 0x1c3c76;
1477 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1479 if (disable_vendor_specific)
1480 dsm_mask &= ~(1 << 8);
1481 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1482 dsm_mask = 0xffffffff;
1484 dev_dbg(dev, "unknown dimm command family\n");
1485 nfit_mem->family = -1;
1486 /* DSMs are optional, continue loading the driver... */
1490 guid = to_nfit_uuid(nfit_mem->family);
1491 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1492 if (acpi_check_dsm(adev_dimm->handle, guid, 1, 1ULL << i))
1493 set_bit(i, &nfit_mem->dsm_mask);
1498 static void shutdown_dimm_notify(void *data)
1500 struct acpi_nfit_desc *acpi_desc = data;
1501 struct nfit_mem *nfit_mem;
1503 mutex_lock(&acpi_desc->init_mutex);
1505 * Clear out the nfit_mem->flags_attr and shut down dimm event
1508 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1509 struct acpi_device *adev_dimm = nfit_mem->adev;
1511 if (nfit_mem->flags_attr) {
1512 sysfs_put(nfit_mem->flags_attr);
1513 nfit_mem->flags_attr = NULL;
1516 acpi_remove_notify_handler(adev_dimm->handle,
1517 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1519 mutex_unlock(&acpi_desc->init_mutex);
1522 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1524 struct nfit_mem *nfit_mem;
1525 int dimm_count = 0, rc;
1526 struct nvdimm *nvdimm;
1528 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1529 struct acpi_nfit_flush_address *flush;
1530 unsigned long flags = 0, cmd_mask;
1531 struct nfit_memdev *nfit_memdev;
1535 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1536 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1542 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1543 set_bit(NDD_ALIASING, &flags);
1545 /* collate flags across all memdevs for this dimm */
1546 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1547 struct acpi_nfit_memory_map *dimm_memdev;
1549 dimm_memdev = __to_nfit_memdev(nfit_mem);
1550 if (dimm_memdev->device_handle
1551 != nfit_memdev->memdev->device_handle)
1553 dimm_memdev->flags |= nfit_memdev->memdev->flags;
1556 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
1557 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
1558 set_bit(NDD_UNARMED, &flags);
1560 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1565 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1566 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1567 * userspace interface.
1569 cmd_mask = 1UL << ND_CMD_CALL;
1570 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1571 cmd_mask |= nfit_mem->dsm_mask;
1573 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1575 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
1576 acpi_nfit_dimm_attribute_groups,
1577 flags, cmd_mask, flush ? flush->hint_count : 0,
1578 nfit_mem->flush_wpq);
1582 nfit_mem->nvdimm = nvdimm;
1585 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1588 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
1589 nvdimm_name(nvdimm),
1590 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1591 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1592 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
1593 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1594 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
1598 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1603 * Now that dimms are successfully registered, and async registration
1604 * is flushed, attempt to enable event notification.
1606 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1607 struct kernfs_node *nfit_kernfs;
1609 nvdimm = nfit_mem->nvdimm;
1610 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1612 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1614 sysfs_put(nfit_kernfs);
1615 if (!nfit_mem->flags_attr)
1616 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1617 nvdimm_name(nvdimm));
1620 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1625 * These constants are private because there are no kernel consumers of
1628 enum nfit_aux_cmds {
1629 NFIT_CMD_TRANSLATE_SPA = 5,
1630 NFIT_CMD_ARS_INJECT_SET = 7,
1631 NFIT_CMD_ARS_INJECT_CLEAR = 8,
1632 NFIT_CMD_ARS_INJECT_GET = 9,
1635 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1637 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1638 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
1639 struct acpi_device *adev;
1640 unsigned long dsm_mask;
1643 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
1644 adev = to_acpi_dev(acpi_desc);
1648 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
1649 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1650 set_bit(i, &nd_desc->cmd_mask);
1651 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
1654 (1 << ND_CMD_ARS_CAP) |
1655 (1 << ND_CMD_ARS_START) |
1656 (1 << ND_CMD_ARS_STATUS) |
1657 (1 << ND_CMD_CLEAR_ERROR) |
1658 (1 << NFIT_CMD_TRANSLATE_SPA) |
1659 (1 << NFIT_CMD_ARS_INJECT_SET) |
1660 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
1661 (1 << NFIT_CMD_ARS_INJECT_GET);
1662 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1663 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1664 set_bit(i, &nd_desc->bus_dsm_mask);
1667 static ssize_t range_index_show(struct device *dev,
1668 struct device_attribute *attr, char *buf)
1670 struct nd_region *nd_region = to_nd_region(dev);
1671 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1673 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1675 static DEVICE_ATTR_RO(range_index);
1677 static struct attribute *acpi_nfit_region_attributes[] = {
1678 &dev_attr_range_index.attr,
1682 static const struct attribute_group acpi_nfit_region_attribute_group = {
1684 .attrs = acpi_nfit_region_attributes,
1687 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1688 &nd_region_attribute_group,
1689 &nd_mapping_attribute_group,
1690 &nd_device_attribute_group,
1691 &nd_numa_attribute_group,
1692 &acpi_nfit_region_attribute_group,
1696 /* enough info to uniquely specify an interleave set */
1697 struct nfit_set_info {
1698 struct nfit_set_info_map {
1705 struct nfit_set_info2 {
1706 struct nfit_set_info_map2 {
1710 u16 manufacturing_date;
1711 u8 manufacturing_location;
1716 static size_t sizeof_nfit_set_info(int num_mappings)
1718 return sizeof(struct nfit_set_info)
1719 + num_mappings * sizeof(struct nfit_set_info_map);
1722 static size_t sizeof_nfit_set_info2(int num_mappings)
1724 return sizeof(struct nfit_set_info2)
1725 + num_mappings * sizeof(struct nfit_set_info_map2);
1728 static int cmp_map_compat(const void *m0, const void *m1)
1730 const struct nfit_set_info_map *map0 = m0;
1731 const struct nfit_set_info_map *map1 = m1;
1733 return memcmp(&map0->region_offset, &map1->region_offset,
1737 static int cmp_map(const void *m0, const void *m1)
1739 const struct nfit_set_info_map *map0 = m0;
1740 const struct nfit_set_info_map *map1 = m1;
1742 if (map0->region_offset < map1->region_offset)
1744 else if (map0->region_offset > map1->region_offset)
1749 static int cmp_map2(const void *m0, const void *m1)
1751 const struct nfit_set_info_map2 *map0 = m0;
1752 const struct nfit_set_info_map2 *map1 = m1;
1754 if (map0->region_offset < map1->region_offset)
1756 else if (map0->region_offset > map1->region_offset)
1761 /* Retrieve the nth entry referencing this spa */
1762 static struct acpi_nfit_memory_map *memdev_from_spa(
1763 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1765 struct nfit_memdev *nfit_memdev;
1767 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1768 if (nfit_memdev->memdev->range_index == range_index)
1770 return nfit_memdev->memdev;
1774 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1775 struct nd_region_desc *ndr_desc,
1776 struct acpi_nfit_system_address *spa)
1778 struct device *dev = acpi_desc->dev;
1779 struct nd_interleave_set *nd_set;
1780 u16 nr = ndr_desc->num_mappings;
1781 struct nfit_set_info2 *info2;
1782 struct nfit_set_info *info;
1785 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1788 ndr_desc->nd_set = nd_set;
1789 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
1791 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1795 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
1799 for (i = 0; i < nr; i++) {
1800 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
1801 struct nfit_set_info_map *map = &info->mapping[i];
1802 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
1803 struct nvdimm *nvdimm = mapping->nvdimm;
1804 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1805 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1806 spa->range_index, i);
1808 if (!memdev || !nfit_mem->dcr) {
1809 dev_err(dev, "%s: failed to find DCR\n", __func__);
1813 map->region_offset = memdev->region_offset;
1814 map->serial_number = nfit_mem->dcr->serial_number;
1816 map2->region_offset = memdev->region_offset;
1817 map2->serial_number = nfit_mem->dcr->serial_number;
1818 map2->vendor_id = nfit_mem->dcr->vendor_id;
1819 map2->manufacturing_date = nfit_mem->dcr->manufacturing_date;
1820 map2->manufacturing_location = nfit_mem->dcr->manufacturing_location;
1823 /* v1.1 namespaces */
1824 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1826 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1828 /* v1.2 namespaces */
1829 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
1831 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
1833 /* support v1.1 namespaces created with the wrong sort order */
1834 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1835 cmp_map_compat, NULL);
1836 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1838 ndr_desc->nd_set = nd_set;
1839 devm_kfree(dev, info);
1840 devm_kfree(dev, info2);
1845 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1847 struct acpi_nfit_interleave *idt = mmio->idt;
1848 u32 sub_line_offset, line_index, line_offset;
1849 u64 line_no, table_skip_count, table_offset;
1851 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1852 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1853 line_offset = idt->line_offset[line_index]
1855 table_offset = table_skip_count * mmio->table_size;
1857 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1860 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
1862 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1863 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
1864 const u32 STATUS_MASK = 0x80000037;
1866 if (mmio->num_lines)
1867 offset = to_interleave_offset(offset, mmio);
1869 return readl(mmio->addr.base + offset) & STATUS_MASK;
1872 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1873 resource_size_t dpa, unsigned int len, unsigned int write)
1876 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1879 BCW_OFFSET_MASK = (1ULL << 48)-1,
1881 BCW_LEN_MASK = (1ULL << 8) - 1,
1885 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1886 len = len >> L1_CACHE_SHIFT;
1887 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1888 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1890 offset = nfit_blk->cmd_offset + mmio->size * bw;
1891 if (mmio->num_lines)
1892 offset = to_interleave_offset(offset, mmio);
1894 writeq(cmd, mmio->addr.base + offset);
1895 nvdimm_flush(nfit_blk->nd_region);
1897 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
1898 readq(mmio->addr.base + offset);
1901 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1902 resource_size_t dpa, void *iobuf, size_t len, int rw,
1905 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1906 unsigned int copied = 0;
1910 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1911 + lane * mmio->size;
1912 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1917 if (mmio->num_lines) {
1920 offset = to_interleave_offset(base_offset + copied,
1922 div_u64_rem(offset, mmio->line_size, &line_offset);
1923 c = min_t(size_t, len, mmio->line_size - line_offset);
1925 offset = base_offset + nfit_blk->bdw_offset;
1930 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
1932 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
1933 mmio_flush_range((void __force *)
1934 mmio->addr.aperture + offset, c);
1936 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
1944 nvdimm_flush(nfit_blk->nd_region);
1946 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1950 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1951 resource_size_t dpa, void *iobuf, u64 len, int rw)
1953 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1954 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1955 struct nd_region *nd_region = nfit_blk->nd_region;
1956 unsigned int lane, copied = 0;
1959 lane = nd_region_acquire_lane(nd_region);
1961 u64 c = min(len, mmio->size);
1963 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1964 iobuf + copied, c, rw, lane);
1971 nd_region_release_lane(nd_region, lane);
1976 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1977 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1980 mmio->num_lines = idt->line_count;
1981 mmio->line_size = idt->line_size;
1982 if (interleave_ways == 0)
1984 mmio->table_size = mmio->num_lines * interleave_ways
1991 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1992 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1994 struct nd_cmd_dimm_flags flags;
1997 memset(&flags, 0, sizeof(flags));
1998 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
1999 sizeof(flags), NULL);
2001 if (rc >= 0 && flags.status == 0)
2002 nfit_blk->dimm_flags = flags.flags;
2003 else if (rc == -ENOTTY) {
2004 /* fall back to a conservative default */
2005 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2013 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2016 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2017 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2018 struct nfit_blk_mmio *mmio;
2019 struct nfit_blk *nfit_blk;
2020 struct nfit_mem *nfit_mem;
2021 struct nvdimm *nvdimm;
2024 nvdimm = nd_blk_region_to_dimm(ndbr);
2025 nfit_mem = nvdimm_provider_data(nvdimm);
2026 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2027 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
2028 nfit_mem ? "" : " nfit_mem",
2029 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2030 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2034 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2037 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2038 nfit_blk->nd_region = to_nd_region(dev);
2040 /* map block aperture memory */
2041 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2042 mmio = &nfit_blk->mmio[BDW];
2043 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2044 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2045 if (!mmio->addr.base) {
2046 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
2047 nvdimm_name(nvdimm));
2050 mmio->size = nfit_mem->bdw->size;
2051 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2052 mmio->idt = nfit_mem->idt_bdw;
2053 mmio->spa = nfit_mem->spa_bdw;
2054 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2055 nfit_mem->memdev_bdw->interleave_ways);
2057 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
2058 __func__, nvdimm_name(nvdimm));
2062 /* map block control memory */
2063 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2064 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2065 mmio = &nfit_blk->mmio[DCR];
2066 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2067 nfit_mem->spa_dcr->length);
2068 if (!mmio->addr.base) {
2069 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
2070 nvdimm_name(nvdimm));
2073 mmio->size = nfit_mem->dcr->window_size;
2074 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2075 mmio->idt = nfit_mem->idt_dcr;
2076 mmio->spa = nfit_mem->spa_dcr;
2077 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2078 nfit_mem->memdev_dcr->interleave_ways);
2080 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
2081 __func__, nvdimm_name(nvdimm));
2085 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2087 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
2088 __func__, nvdimm_name(nvdimm));
2092 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2093 dev_warn(dev, "unable to guarantee persistence of writes\n");
2095 if (mmio->line_size == 0)
2098 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2099 + 8 > mmio->line_size) {
2100 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2102 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2103 + 8 > mmio->line_size) {
2104 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2111 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2112 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2114 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2115 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2118 cmd->address = spa->address;
2119 cmd->length = spa->length;
2120 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2121 sizeof(*cmd), &cmd_rc);
2127 static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
2131 struct nd_cmd_ars_start ars_start;
2132 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2133 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2135 memset(&ars_start, 0, sizeof(ars_start));
2136 ars_start.address = spa->address;
2137 ars_start.length = spa->length;
2138 ars_start.flags = acpi_desc->ars_start_flags;
2139 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2140 ars_start.type = ND_ARS_PERSISTENT;
2141 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2142 ars_start.type = ND_ARS_VOLATILE;
2146 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2147 sizeof(ars_start), &cmd_rc);
2154 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2157 struct nd_cmd_ars_start ars_start;
2158 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2159 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2161 memset(&ars_start, 0, sizeof(ars_start));
2162 ars_start.address = ars_status->restart_address;
2163 ars_start.length = ars_status->restart_length;
2164 ars_start.type = ars_status->type;
2165 ars_start.flags = acpi_desc->ars_start_flags;
2166 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2167 sizeof(ars_start), &cmd_rc);
2173 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2175 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2176 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2179 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2180 acpi_desc->ars_status_size, &cmd_rc);
2186 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
2187 struct nd_cmd_ars_status *ars_status)
2189 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2194 * First record starts at 44 byte offset from the start of the
2197 if (ars_status->out_length < 44)
2199 for (i = 0; i < ars_status->num_records; i++) {
2200 /* only process full records */
2201 if (ars_status->out_length
2202 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2204 rc = nvdimm_bus_add_poison(nvdimm_bus,
2205 ars_status->records[i].err_address,
2206 ars_status->records[i].length);
2210 if (i < ars_status->num_records)
2211 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2216 static void acpi_nfit_remove_resource(void *data)
2218 struct resource *res = data;
2220 remove_resource(res);
2223 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2224 struct nd_region_desc *ndr_desc)
2226 struct resource *res, *nd_res = ndr_desc->res;
2229 /* No operation if the region is already registered as PMEM */
2230 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2231 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2232 if (is_pmem == REGION_INTERSECTS)
2235 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2239 res->name = "Persistent Memory";
2240 res->start = nd_res->start;
2241 res->end = nd_res->end;
2242 res->flags = IORESOURCE_MEM;
2243 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2245 ret = insert_resource(&iomem_resource, res);
2249 ret = devm_add_action_or_reset(acpi_desc->dev,
2250 acpi_nfit_remove_resource,
2258 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2259 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2260 struct acpi_nfit_memory_map *memdev,
2261 struct nfit_spa *nfit_spa)
2263 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2264 memdev->device_handle);
2265 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2266 struct nd_blk_region_desc *ndbr_desc;
2267 struct nfit_mem *nfit_mem;
2268 int blk_valid = 0, rc;
2271 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2272 spa->range_index, memdev->device_handle);
2276 mapping->nvdimm = nvdimm;
2277 switch (nfit_spa_type(spa)) {
2279 case NFIT_SPA_VOLATILE:
2280 mapping->start = memdev->address;
2281 mapping->size = memdev->region_size;
2284 nfit_mem = nvdimm_provider_data(nvdimm);
2285 if (!nfit_mem || !nfit_mem->bdw) {
2286 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2287 spa->range_index, nvdimm_name(nvdimm));
2289 mapping->size = nfit_mem->bdw->capacity;
2290 mapping->start = nfit_mem->bdw->start_address;
2291 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2295 ndr_desc->mapping = mapping;
2296 ndr_desc->num_mappings = blk_valid;
2297 ndbr_desc = to_blk_region_desc(ndr_desc);
2298 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2299 ndbr_desc->do_io = acpi_desc->blk_do_io;
2300 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2303 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2305 if (!nfit_spa->nd_region)
2313 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2315 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2316 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2317 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2318 nfit_spa_type(spa) == NFIT_SPA_PCD);
2321 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2323 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2324 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2325 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2328 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2329 struct nfit_spa *nfit_spa)
2331 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2332 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2333 struct nd_blk_region_desc ndbr_desc;
2334 struct nd_region_desc *ndr_desc;
2335 struct nfit_memdev *nfit_memdev;
2336 struct nvdimm_bus *nvdimm_bus;
2337 struct resource res;
2340 if (nfit_spa->nd_region)
2343 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2344 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2349 memset(&res, 0, sizeof(res));
2350 memset(&mappings, 0, sizeof(mappings));
2351 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2352 res.start = spa->address;
2353 res.end = res.start + spa->length - 1;
2354 ndr_desc = &ndbr_desc.ndr_desc;
2355 ndr_desc->res = &res;
2356 ndr_desc->provider_data = nfit_spa;
2357 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2358 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2359 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2360 spa->proximity_domain);
2362 ndr_desc->numa_node = NUMA_NO_NODE;
2364 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2365 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2366 struct nd_mapping_desc *mapping;
2368 if (memdev->range_index != spa->range_index)
2370 if (count >= ND_MAX_MAPPINGS) {
2371 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2372 spa->range_index, ND_MAX_MAPPINGS);
2375 mapping = &mappings[count++];
2376 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2382 ndr_desc->mapping = mappings;
2383 ndr_desc->num_mappings = count;
2384 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2388 nvdimm_bus = acpi_desc->nvdimm_bus;
2389 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2390 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2392 dev_warn(acpi_desc->dev,
2393 "failed to insert pmem resource to iomem: %d\n",
2398 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2400 if (!nfit_spa->nd_region)
2402 } else if (nfit_spa_is_volatile(spa)) {
2403 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2405 if (!nfit_spa->nd_region)
2407 } else if (nfit_spa_is_virtual(spa)) {
2408 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2410 if (!nfit_spa->nd_region)
2416 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2417 nfit_spa->spa->range_index);
2421 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2424 struct device *dev = acpi_desc->dev;
2425 struct nd_cmd_ars_status *ars_status;
2427 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2428 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2432 if (acpi_desc->ars_status)
2433 devm_kfree(dev, acpi_desc->ars_status);
2434 acpi_desc->ars_status = NULL;
2435 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2438 acpi_desc->ars_status = ars_status;
2439 acpi_desc->ars_status_size = max_ars;
2443 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2444 struct nfit_spa *nfit_spa)
2446 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2449 if (!nfit_spa->max_ars) {
2450 struct nd_cmd_ars_cap ars_cap;
2452 memset(&ars_cap, 0, sizeof(ars_cap));
2453 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2456 nfit_spa->max_ars = ars_cap.max_ars_out;
2457 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2458 /* check that the supported scrub types match the spa type */
2459 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2460 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2462 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2463 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2467 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2470 rc = ars_get_status(acpi_desc);
2471 if (rc < 0 && rc != -ENOSPC)
2474 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
2480 static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2481 struct nfit_spa *nfit_spa)
2483 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2484 unsigned int overflow_retry = scrub_overflow_abort;
2485 u64 init_ars_start = 0, init_ars_len = 0;
2486 struct device *dev = acpi_desc->dev;
2487 unsigned int tmo = scrub_timeout;
2490 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
2493 rc = ars_start(acpi_desc, nfit_spa);
2495 * If we timed out the initial scan we'll still be busy here,
2496 * and will wait another timeout before giving up permanently.
2498 if (rc < 0 && rc != -EBUSY)
2502 u64 ars_start, ars_len;
2504 if (acpi_desc->cancel)
2506 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2509 if (rc == -EBUSY && !tmo) {
2510 dev_warn(dev, "range %d ars timeout, aborting\n",
2517 * Note, entries may be appended to the list
2518 * while the lock is dropped, but the workqueue
2519 * being active prevents entries being deleted /
2522 mutex_unlock(&acpi_desc->init_mutex);
2525 mutex_lock(&acpi_desc->init_mutex);
2529 /* we got some results, but there are more pending... */
2530 if (rc == -ENOSPC && overflow_retry--) {
2531 if (!init_ars_len) {
2532 init_ars_len = acpi_desc->ars_status->length;
2533 init_ars_start = acpi_desc->ars_status->address;
2535 rc = ars_continue(acpi_desc);
2539 dev_warn(dev, "range %d ars continuation failed\n",
2545 ars_start = init_ars_start;
2546 ars_len = init_ars_len;
2548 ars_start = acpi_desc->ars_status->address;
2549 ars_len = acpi_desc->ars_status->length;
2551 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2552 spa->range_index, ars_start, ars_len);
2553 /* notify the region about new poison entries */
2554 nvdimm_region_notify(nfit_spa->nd_region,
2555 NVDIMM_REVALIDATE_POISON);
2560 static void acpi_nfit_scrub(struct work_struct *work)
2563 u64 init_scrub_length = 0;
2564 struct nfit_spa *nfit_spa;
2565 u64 init_scrub_address = 0;
2566 bool init_ars_done = false;
2567 struct acpi_nfit_desc *acpi_desc;
2568 unsigned int tmo = scrub_timeout;
2569 unsigned int overflow_retry = scrub_overflow_abort;
2571 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2572 dev = acpi_desc->dev;
2575 * We scrub in 2 phases. The first phase waits for any platform
2576 * firmware initiated scrubs to complete and then we go search for the
2577 * affected spa regions to mark them scanned. In the second phase we
2578 * initiate a directed scrub for every range that was not scrubbed in
2579 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2580 * the first phase, but really only care about running phase 2, where
2581 * regions can be notified of new poison.
2584 /* process platform firmware initiated scrubs */
2586 mutex_lock(&acpi_desc->init_mutex);
2587 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2588 struct nd_cmd_ars_status *ars_status;
2589 struct acpi_nfit_system_address *spa;
2590 u64 ars_start, ars_len;
2593 if (acpi_desc->cancel)
2596 if (nfit_spa->nd_region)
2599 if (init_ars_done) {
2601 * No need to re-query, we're now just
2602 * reconciling all the ranges covered by the
2607 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2609 if (rc == -ENOTTY) {
2610 /* no ars capability, just register spa and move on */
2611 acpi_nfit_register_region(acpi_desc, nfit_spa);
2615 if (rc == -EBUSY && !tmo) {
2616 /* fallthrough to directed scrub in phase 2 */
2617 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2619 } else if (rc == -EBUSY) {
2620 mutex_unlock(&acpi_desc->init_mutex);
2626 /* we got some results, but there are more pending... */
2627 if (rc == -ENOSPC && overflow_retry--) {
2628 ars_status = acpi_desc->ars_status;
2630 * Record the original scrub range, so that we
2631 * can recall all the ranges impacted by the
2634 if (!init_scrub_length) {
2635 init_scrub_length = ars_status->length;
2636 init_scrub_address = ars_status->address;
2638 rc = ars_continue(acpi_desc);
2640 mutex_unlock(&acpi_desc->init_mutex);
2647 * Initial scrub failed, we'll give it one more
2653 /* We got some final results, record completed ranges */
2654 ars_status = acpi_desc->ars_status;
2655 if (init_scrub_length) {
2656 ars_start = init_scrub_address;
2657 ars_len = ars_start + init_scrub_length;
2659 ars_start = ars_status->address;
2660 ars_len = ars_status->length;
2662 spa = nfit_spa->spa;
2664 if (!init_ars_done) {
2665 init_ars_done = true;
2666 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2667 ars_start, ars_len);
2669 if (ars_start <= spa->address && ars_start + ars_len
2670 >= spa->address + spa->length)
2671 acpi_nfit_register_region(acpi_desc, nfit_spa);
2675 * For all the ranges not covered by an initial scrub we still
2676 * want to see if there are errors, but it's ok to discover them
2679 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2681 * Flag all the ranges that still need scrubbing, but
2682 * register them now to make data available.
2684 if (!nfit_spa->nd_region) {
2685 nfit_spa->ars_required = 1;
2686 acpi_nfit_register_region(acpi_desc, nfit_spa);
2689 acpi_desc->init_complete = 1;
2691 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2692 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
2693 acpi_desc->scrub_count++;
2694 acpi_desc->ars_start_flags = 0;
2695 if (acpi_desc->scrub_count_state)
2696 sysfs_notify_dirent(acpi_desc->scrub_count_state);
2697 mutex_unlock(&acpi_desc->init_mutex);
2700 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2702 struct nfit_spa *nfit_spa;
2705 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2706 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2707 /* BLK regions don't need to wait for ars results */
2708 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2713 acpi_desc->ars_start_flags = 0;
2714 if (!acpi_desc->cancel)
2715 queue_work(nfit_wq, &acpi_desc->work);
2719 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2720 struct nfit_table_prev *prev)
2722 struct device *dev = acpi_desc->dev;
2724 if (!list_empty(&prev->spas) ||
2725 !list_empty(&prev->memdevs) ||
2726 !list_empty(&prev->dcrs) ||
2727 !list_empty(&prev->bdws) ||
2728 !list_empty(&prev->idts) ||
2729 !list_empty(&prev->flushes)) {
2730 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2736 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2738 struct device *dev = acpi_desc->dev;
2739 struct kernfs_node *nfit;
2740 struct device *bus_dev;
2742 if (!ars_supported(acpi_desc->nvdimm_bus))
2745 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2746 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2748 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2751 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2753 if (!acpi_desc->scrub_count_state) {
2754 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2761 static void acpi_nfit_unregister(void *data)
2763 struct acpi_nfit_desc *acpi_desc = data;
2765 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2768 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
2770 struct device *dev = acpi_desc->dev;
2771 struct nfit_table_prev prev;
2775 if (!acpi_desc->nvdimm_bus) {
2776 acpi_nfit_init_dsms(acpi_desc);
2778 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2779 &acpi_desc->nd_desc);
2780 if (!acpi_desc->nvdimm_bus)
2783 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
2788 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2792 /* register this acpi_desc for mce notifications */
2793 mutex_lock(&acpi_desc_lock);
2794 list_add_tail(&acpi_desc->list, &acpi_descs);
2795 mutex_unlock(&acpi_desc_lock);
2798 mutex_lock(&acpi_desc->init_mutex);
2800 INIT_LIST_HEAD(&prev.spas);
2801 INIT_LIST_HEAD(&prev.memdevs);
2802 INIT_LIST_HEAD(&prev.dcrs);
2803 INIT_LIST_HEAD(&prev.bdws);
2804 INIT_LIST_HEAD(&prev.idts);
2805 INIT_LIST_HEAD(&prev.flushes);
2807 list_cut_position(&prev.spas, &acpi_desc->spas,
2808 acpi_desc->spas.prev);
2809 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2810 acpi_desc->memdevs.prev);
2811 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2812 acpi_desc->dcrs.prev);
2813 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2814 acpi_desc->bdws.prev);
2815 list_cut_position(&prev.idts, &acpi_desc->idts,
2816 acpi_desc->idts.prev);
2817 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2818 acpi_desc->flushes.prev);
2821 while (!IS_ERR_OR_NULL(data))
2822 data = add_table(acpi_desc, &prev, data, end);
2825 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2831 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2835 rc = nfit_mem_init(acpi_desc);
2839 rc = acpi_nfit_register_dimms(acpi_desc);
2843 rc = acpi_nfit_register_regions(acpi_desc);
2846 mutex_unlock(&acpi_desc->init_mutex);
2849 EXPORT_SYMBOL_GPL(acpi_nfit_init);
2851 struct acpi_nfit_flush_work {
2852 struct work_struct work;
2853 struct completion cmp;
2856 static void flush_probe(struct work_struct *work)
2858 struct acpi_nfit_flush_work *flush;
2860 flush = container_of(work, typeof(*flush), work);
2861 complete(&flush->cmp);
2864 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2866 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2867 struct device *dev = acpi_desc->dev;
2868 struct acpi_nfit_flush_work flush;
2871 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2875 /* bounce the init_mutex to make init_complete valid */
2876 mutex_lock(&acpi_desc->init_mutex);
2877 if (acpi_desc->cancel || acpi_desc->init_complete) {
2878 mutex_unlock(&acpi_desc->init_mutex);
2883 * Scrub work could take 10s of seconds, userspace may give up so we
2884 * need to be interruptible while waiting.
2886 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2887 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2888 queue_work(nfit_wq, &flush.work);
2889 mutex_unlock(&acpi_desc->init_mutex);
2891 rc = wait_for_completion_interruptible(&flush.cmp);
2892 cancel_work_sync(&flush.work);
2896 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2897 struct nvdimm *nvdimm, unsigned int cmd)
2899 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2903 if (cmd != ND_CMD_ARS_START)
2907 * The kernel and userspace may race to initiate a scrub, but
2908 * the scrub thread is prepared to lose that initial race. It
2909 * just needs guarantees that any ars it initiates are not
2910 * interrupted by any intervening start reqeusts from userspace.
2912 if (work_busy(&acpi_desc->work))
2918 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc, u8 flags)
2920 struct device *dev = acpi_desc->dev;
2921 struct nfit_spa *nfit_spa;
2923 if (work_busy(&acpi_desc->work))
2926 mutex_lock(&acpi_desc->init_mutex);
2927 if (acpi_desc->cancel) {
2928 mutex_unlock(&acpi_desc->init_mutex);
2932 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2933 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2935 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2938 nfit_spa->ars_required = 1;
2940 acpi_desc->ars_start_flags = flags;
2941 queue_work(nfit_wq, &acpi_desc->work);
2942 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2943 mutex_unlock(&acpi_desc->init_mutex);
2948 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
2950 struct nvdimm_bus_descriptor *nd_desc;
2952 dev_set_drvdata(dev, acpi_desc);
2953 acpi_desc->dev = dev;
2954 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2955 nd_desc = &acpi_desc->nd_desc;
2956 nd_desc->provider_name = "ACPI.NFIT";
2957 nd_desc->module = THIS_MODULE;
2958 nd_desc->ndctl = acpi_nfit_ctl;
2959 nd_desc->flush_probe = acpi_nfit_flush_probe;
2960 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
2961 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2963 INIT_LIST_HEAD(&acpi_desc->spas);
2964 INIT_LIST_HEAD(&acpi_desc->dcrs);
2965 INIT_LIST_HEAD(&acpi_desc->bdws);
2966 INIT_LIST_HEAD(&acpi_desc->idts);
2967 INIT_LIST_HEAD(&acpi_desc->flushes);
2968 INIT_LIST_HEAD(&acpi_desc->memdevs);
2969 INIT_LIST_HEAD(&acpi_desc->dimms);
2970 INIT_LIST_HEAD(&acpi_desc->list);
2971 mutex_init(&acpi_desc->init_mutex);
2972 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
2974 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
2976 static void acpi_nfit_put_table(void *table)
2978 acpi_put_table(table);
2981 void acpi_nfit_shutdown(void *data)
2983 struct acpi_nfit_desc *acpi_desc = data;
2984 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2987 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2990 mutex_lock(&acpi_desc_lock);
2991 list_del(&acpi_desc->list);
2992 mutex_unlock(&acpi_desc_lock);
2994 mutex_lock(&acpi_desc->init_mutex);
2995 acpi_desc->cancel = 1;
2996 mutex_unlock(&acpi_desc->init_mutex);
2999 * Bounce the nvdimm bus lock to make sure any in-flight
3000 * acpi_nfit_ars_rescan() submissions have had a chance to
3001 * either submit or see ->cancel set.
3003 device_lock(bus_dev);
3004 device_unlock(bus_dev);
3006 flush_workqueue(nfit_wq);
3008 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3010 static int acpi_nfit_add(struct acpi_device *adev)
3012 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3013 struct acpi_nfit_desc *acpi_desc;
3014 struct device *dev = &adev->dev;
3015 struct acpi_table_header *tbl;
3016 acpi_status status = AE_OK;
3020 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3021 if (ACPI_FAILURE(status)) {
3022 /* This is ok, we could have an nvdimm hotplugged later */
3023 dev_dbg(dev, "failed to find NFIT at startup\n");
3027 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3032 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3035 acpi_nfit_desc_init(acpi_desc, &adev->dev);
3037 /* Save the acpi header for exporting the revision via sysfs */
3038 acpi_desc->acpi_header = *tbl;
3040 /* Evaluate _FIT and override with that if present */
3041 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3042 if (ACPI_SUCCESS(status) && buf.length > 0) {
3043 union acpi_object *obj = buf.pointer;
3045 if (obj->type == ACPI_TYPE_BUFFER)
3046 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3047 obj->buffer.length);
3049 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
3050 __func__, (int) obj->type);
3053 /* skip over the lead-in header table */
3054 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3055 + sizeof(struct acpi_table_nfit),
3056 sz - sizeof(struct acpi_table_nfit));
3060 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3063 static int acpi_nfit_remove(struct acpi_device *adev)
3065 /* see acpi_nfit_unregister */
3069 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3071 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3072 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3073 union acpi_object *obj;
3078 /* dev->driver may be null if we're being removed */
3079 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
3084 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3087 acpi_nfit_desc_init(acpi_desc, dev);
3090 * Finish previous registration before considering new
3093 flush_workqueue(nfit_wq);
3097 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3098 if (ACPI_FAILURE(status)) {
3099 dev_err(dev, "failed to evaluate _FIT\n");
3104 if (obj->type == ACPI_TYPE_BUFFER) {
3105 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3106 obj->buffer.length);
3108 dev_err(dev, "failed to merge updated NFIT\n");
3110 dev_err(dev, "Invalid _FIT\n");
3114 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3116 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3117 u8 flags = (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON) ?
3118 0 : ND_ARS_RETURN_PREV_DATA;
3120 acpi_nfit_ars_rescan(acpi_desc, flags);
3123 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3125 dev_dbg(dev, "%s: event: 0x%x\n", __func__, event);
3128 case NFIT_NOTIFY_UPDATE:
3129 return acpi_nfit_update_notify(dev, handle);
3130 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3131 return acpi_nfit_uc_error_notify(dev, handle);
3136 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3138 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3140 device_lock(&adev->dev);
3141 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3142 device_unlock(&adev->dev);
3145 static const struct acpi_device_id acpi_nfit_ids[] = {
3149 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3151 static struct acpi_driver acpi_nfit_driver = {
3152 .name = KBUILD_MODNAME,
3153 .ids = acpi_nfit_ids,
3155 .add = acpi_nfit_add,
3156 .remove = acpi_nfit_remove,
3157 .notify = acpi_nfit_notify,
3161 static __init int nfit_init(void)
3163 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3164 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3165 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3166 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3167 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3168 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3169 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3171 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3172 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3173 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3174 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3175 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3176 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3177 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3178 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3179 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3180 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3181 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3182 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3183 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3185 nfit_wq = create_singlethread_workqueue("nfit");
3189 nfit_mce_register();
3191 return acpi_bus_register_driver(&acpi_nfit_driver);
3194 static __exit void nfit_exit(void)
3196 nfit_mce_unregister();
3197 acpi_bus_unregister_driver(&acpi_nfit_driver);
3198 destroy_workqueue(nfit_wq);
3199 WARN_ON(!list_empty(&acpi_descs));
3202 module_init(nfit_init);
3203 module_exit(nfit_exit);
3204 MODULE_LICENSE("GPL v2");
3205 MODULE_AUTHOR("Intel Corporation");