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>
23 #include <linux/pmem.h>
26 #include <asm/cacheflush.h>
30 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
33 #include <linux/io-64-nonatomic-hi-lo.h>
35 static bool force_enable_dimms;
36 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
37 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
39 static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
40 module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
41 MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
43 /* after three payloads of overflow, it's dead jim */
44 static unsigned int scrub_overflow_abort = 3;
45 module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
46 MODULE_PARM_DESC(scrub_overflow_abort,
47 "Number of times we overflow ARS results before abort");
49 static bool disable_vendor_specific;
50 module_param(disable_vendor_specific, bool, S_IRUGO);
51 MODULE_PARM_DESC(disable_vendor_specific,
52 "Limit commands to the publicly specified set\n");
54 LIST_HEAD(acpi_descs);
55 DEFINE_MUTEX(acpi_desc_lock);
57 static struct workqueue_struct *nfit_wq;
59 struct nfit_table_prev {
60 struct list_head spas;
61 struct list_head memdevs;
62 struct list_head dcrs;
63 struct list_head bdws;
64 struct list_head idts;
65 struct list_head flushes;
68 static u8 nfit_uuid[NFIT_UUID_MAX][16];
70 const u8 *to_nfit_uuid(enum nfit_uuids id)
74 EXPORT_SYMBOL(to_nfit_uuid);
76 static struct acpi_nfit_desc *to_acpi_nfit_desc(
77 struct nvdimm_bus_descriptor *nd_desc)
79 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
82 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
84 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
87 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
90 if (!nd_desc->provider_name
91 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
94 return to_acpi_device(acpi_desc->dev);
97 static int xlat_status(void *buf, unsigned int cmd, u32 status)
99 struct nd_cmd_clear_error *clear_err;
100 struct nd_cmd_ars_status *ars_status;
105 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
112 /* No supported scan types for this range */
113 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
114 if ((status >> 16 & flags) == 0)
117 case ND_CMD_ARS_START:
118 /* ARS is in progress */
119 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
126 case ND_CMD_ARS_STATUS:
131 /* Check extended status (Upper two bytes) */
132 if (status == NFIT_ARS_STATUS_DONE)
135 /* ARS is in progress */
136 if (status == NFIT_ARS_STATUS_BUSY)
139 /* No ARS performed for the current boot */
140 if (status == NFIT_ARS_STATUS_NONE)
144 * ARS interrupted, either we overflowed or some other
145 * agent wants the scan to stop. If we didn't overflow
146 * then just continue with the returned results.
148 if (status == NFIT_ARS_STATUS_INTR) {
149 if (ars_status->flags & NFIT_ARS_F_OVERFLOW)
158 case ND_CMD_CLEAR_ERROR:
162 if (!clear_err->cleared)
164 if (clear_err->length > clear_err->cleared)
165 return clear_err->cleared;
171 /* all other non-zero status results in an error */
177 static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
178 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
179 unsigned int buf_len, int *cmd_rc)
181 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
182 union acpi_object in_obj, in_buf, *out_obj;
183 const struct nd_cmd_desc *desc = NULL;
184 struct device *dev = acpi_desc->dev;
185 struct nd_cmd_pkg *call_pkg = NULL;
186 const char *cmd_name, *dimm_name;
187 unsigned long cmd_mask, dsm_mask;
188 u32 offset, fw_status = 0;
195 if (cmd == ND_CMD_CALL) {
197 func = call_pkg->nd_command;
201 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
202 struct acpi_device *adev = nfit_mem->adev;
206 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
209 dimm_name = nvdimm_name(nvdimm);
210 cmd_name = nvdimm_cmd_name(cmd);
211 cmd_mask = nvdimm_cmd_mask(nvdimm);
212 dsm_mask = nfit_mem->dsm_mask;
213 desc = nd_cmd_dimm_desc(cmd);
214 uuid = to_nfit_uuid(nfit_mem->family);
215 handle = adev->handle;
217 struct acpi_device *adev = to_acpi_dev(acpi_desc);
219 cmd_name = nvdimm_bus_cmd_name(cmd);
220 cmd_mask = nd_desc->cmd_mask;
222 desc = nd_cmd_bus_desc(cmd);
223 uuid = to_nfit_uuid(NFIT_DEV_BUS);
224 handle = adev->handle;
228 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
231 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
234 in_obj.type = ACPI_TYPE_PACKAGE;
235 in_obj.package.count = 1;
236 in_obj.package.elements = &in_buf;
237 in_buf.type = ACPI_TYPE_BUFFER;
238 in_buf.buffer.pointer = buf;
239 in_buf.buffer.length = 0;
241 /* libnvdimm has already validated the input envelope */
242 for (i = 0; i < desc->in_num; i++)
243 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
247 /* skip over package wrapper */
248 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
249 in_buf.buffer.length = call_pkg->nd_size_in;
252 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
253 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
254 __func__, dimm_name, cmd, func,
255 in_buf.buffer.length);
256 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
257 in_buf.buffer.pointer,
258 min_t(u32, 256, in_buf.buffer.length), true);
261 out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj);
263 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
269 call_pkg->nd_fw_size = out_obj->buffer.length;
270 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
271 out_obj->buffer.pointer,
272 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
276 * Need to support FW function w/o known size in advance.
277 * Caller can determine required size based upon nd_fw_size.
278 * If we return an error (like elsewhere) then caller wouldn't
279 * be able to rely upon data returned to make calculation.
284 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
285 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
286 __func__, dimm_name, cmd_name, out_obj->type);
291 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
292 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
293 dimm_name, cmd_name, out_obj->buffer.length);
294 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
295 4, out_obj->buffer.pointer, min_t(u32, 128,
296 out_obj->buffer.length), true);
299 for (i = 0, offset = 0; i < desc->out_num; i++) {
300 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
301 (u32 *) out_obj->buffer.pointer);
303 if (offset + out_size > out_obj->buffer.length) {
304 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
305 __func__, dimm_name, cmd_name, i);
309 if (in_buf.buffer.length + offset + out_size > buf_len) {
310 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
311 __func__, dimm_name, cmd_name, i);
315 memcpy(buf + in_buf.buffer.length + offset,
316 out_obj->buffer.pointer + offset, out_size);
321 * Set fw_status for all the commands with a known format to be
322 * later interpreted by xlat_status().
324 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
325 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
326 fw_status = *(u32 *) out_obj->buffer.pointer;
328 if (offset + in_buf.buffer.length < buf_len) {
331 * status valid, return the number of bytes left
332 * unfilled in the output buffer
334 rc = buf_len - offset - in_buf.buffer.length;
336 *cmd_rc = xlat_status(buf, cmd, fw_status);
338 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
339 __func__, dimm_name, cmd_name, buf_len,
346 *cmd_rc = xlat_status(buf, cmd, fw_status);
355 static const char *spa_type_name(u16 type)
357 static const char *to_name[] = {
358 [NFIT_SPA_VOLATILE] = "volatile",
359 [NFIT_SPA_PM] = "pmem",
360 [NFIT_SPA_DCR] = "dimm-control-region",
361 [NFIT_SPA_BDW] = "block-data-window",
362 [NFIT_SPA_VDISK] = "volatile-disk",
363 [NFIT_SPA_VCD] = "volatile-cd",
364 [NFIT_SPA_PDISK] = "persistent-disk",
365 [NFIT_SPA_PCD] = "persistent-cd",
369 if (type > NFIT_SPA_PCD)
372 return to_name[type];
375 int nfit_spa_type(struct acpi_nfit_system_address *spa)
379 for (i = 0; i < NFIT_UUID_MAX; i++)
380 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
385 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
386 struct nfit_table_prev *prev,
387 struct acpi_nfit_system_address *spa)
389 struct device *dev = acpi_desc->dev;
390 struct nfit_spa *nfit_spa;
392 if (spa->header.length != sizeof(*spa))
395 list_for_each_entry(nfit_spa, &prev->spas, list) {
396 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
397 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
402 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
406 INIT_LIST_HEAD(&nfit_spa->list);
407 memcpy(nfit_spa->spa, spa, sizeof(*spa));
408 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
409 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
411 spa_type_name(nfit_spa_type(spa)));
415 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
416 struct nfit_table_prev *prev,
417 struct acpi_nfit_memory_map *memdev)
419 struct device *dev = acpi_desc->dev;
420 struct nfit_memdev *nfit_memdev;
422 if (memdev->header.length != sizeof(*memdev))
425 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
426 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
427 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
431 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
435 INIT_LIST_HEAD(&nfit_memdev->list);
436 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
437 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
438 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
439 __func__, memdev->device_handle, memdev->range_index,
440 memdev->region_index);
445 * An implementation may provide a truncated control region if no block windows
448 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
450 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
455 return offsetof(struct acpi_nfit_control_region, window_size);
458 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
459 struct nfit_table_prev *prev,
460 struct acpi_nfit_control_region *dcr)
462 struct device *dev = acpi_desc->dev;
463 struct nfit_dcr *nfit_dcr;
465 if (!sizeof_dcr(dcr))
468 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
469 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
470 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
474 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
478 INIT_LIST_HEAD(&nfit_dcr->list);
479 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
480 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
481 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
482 dcr->region_index, dcr->windows);
486 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
487 struct nfit_table_prev *prev,
488 struct acpi_nfit_data_region *bdw)
490 struct device *dev = acpi_desc->dev;
491 struct nfit_bdw *nfit_bdw;
493 if (bdw->header.length != sizeof(*bdw))
495 list_for_each_entry(nfit_bdw, &prev->bdws, list)
496 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
497 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
501 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
505 INIT_LIST_HEAD(&nfit_bdw->list);
506 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
507 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
508 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
509 bdw->region_index, bdw->windows);
513 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
515 if (idt->header.length < sizeof(*idt))
517 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
520 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
521 struct nfit_table_prev *prev,
522 struct acpi_nfit_interleave *idt)
524 struct device *dev = acpi_desc->dev;
525 struct nfit_idt *nfit_idt;
527 if (!sizeof_idt(idt))
530 list_for_each_entry(nfit_idt, &prev->idts, list) {
531 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
534 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
535 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
540 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
544 INIT_LIST_HEAD(&nfit_idt->list);
545 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
546 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
547 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
548 idt->interleave_index, idt->line_count);
552 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
554 if (flush->header.length < sizeof(*flush))
556 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
559 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
560 struct nfit_table_prev *prev,
561 struct acpi_nfit_flush_address *flush)
563 struct device *dev = acpi_desc->dev;
564 struct nfit_flush *nfit_flush;
566 if (!sizeof_flush(flush))
569 list_for_each_entry(nfit_flush, &prev->flushes, list) {
570 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
573 if (memcmp(nfit_flush->flush, flush,
574 sizeof_flush(flush)) == 0) {
575 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
580 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
581 + sizeof_flush(flush), GFP_KERNEL);
584 INIT_LIST_HEAD(&nfit_flush->list);
585 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
586 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
587 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
588 flush->device_handle, flush->hint_count);
592 static void *add_table(struct acpi_nfit_desc *acpi_desc,
593 struct nfit_table_prev *prev, void *table, const void *end)
595 struct device *dev = acpi_desc->dev;
596 struct acpi_nfit_header *hdr;
597 void *err = ERR_PTR(-ENOMEM);
604 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
610 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
611 if (!add_spa(acpi_desc, prev, table))
614 case ACPI_NFIT_TYPE_MEMORY_MAP:
615 if (!add_memdev(acpi_desc, prev, table))
618 case ACPI_NFIT_TYPE_CONTROL_REGION:
619 if (!add_dcr(acpi_desc, prev, table))
622 case ACPI_NFIT_TYPE_DATA_REGION:
623 if (!add_bdw(acpi_desc, prev, table))
626 case ACPI_NFIT_TYPE_INTERLEAVE:
627 if (!add_idt(acpi_desc, prev, table))
630 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
631 if (!add_flush(acpi_desc, prev, table))
634 case ACPI_NFIT_TYPE_SMBIOS:
635 dev_dbg(dev, "%s: smbios\n", __func__);
638 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
642 return table + hdr->length;
645 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
646 struct nfit_mem *nfit_mem)
648 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
649 u16 dcr = nfit_mem->dcr->region_index;
650 struct nfit_spa *nfit_spa;
652 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
653 u16 range_index = nfit_spa->spa->range_index;
654 int type = nfit_spa_type(nfit_spa->spa);
655 struct nfit_memdev *nfit_memdev;
657 if (type != NFIT_SPA_BDW)
660 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
661 if (nfit_memdev->memdev->range_index != range_index)
663 if (nfit_memdev->memdev->device_handle != device_handle)
665 if (nfit_memdev->memdev->region_index != dcr)
668 nfit_mem->spa_bdw = nfit_spa->spa;
673 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
674 nfit_mem->spa_dcr->range_index);
675 nfit_mem->bdw = NULL;
678 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
679 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
681 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
682 struct nfit_memdev *nfit_memdev;
683 struct nfit_bdw *nfit_bdw;
684 struct nfit_idt *nfit_idt;
685 u16 idt_idx, range_index;
687 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
688 if (nfit_bdw->bdw->region_index != dcr)
690 nfit_mem->bdw = nfit_bdw->bdw;
697 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
699 if (!nfit_mem->spa_bdw)
702 range_index = nfit_mem->spa_bdw->range_index;
703 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
704 if (nfit_memdev->memdev->range_index != range_index ||
705 nfit_memdev->memdev->region_index != dcr)
707 nfit_mem->memdev_bdw = nfit_memdev->memdev;
708 idt_idx = nfit_memdev->memdev->interleave_index;
709 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
710 if (nfit_idt->idt->interleave_index != idt_idx)
712 nfit_mem->idt_bdw = nfit_idt->idt;
719 static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
720 struct acpi_nfit_system_address *spa)
722 struct nfit_mem *nfit_mem, *found;
723 struct nfit_memdev *nfit_memdev;
724 int type = nfit_spa_type(spa);
734 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
735 struct nfit_flush *nfit_flush;
736 struct nfit_dcr *nfit_dcr;
740 if (nfit_memdev->memdev->range_index != spa->range_index)
743 dcr = nfit_memdev->memdev->region_index;
744 device_handle = nfit_memdev->memdev->device_handle;
745 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
746 if (__to_nfit_memdev(nfit_mem)->device_handle
755 nfit_mem = devm_kzalloc(acpi_desc->dev,
756 sizeof(*nfit_mem), GFP_KERNEL);
759 INIT_LIST_HEAD(&nfit_mem->list);
760 nfit_mem->acpi_desc = acpi_desc;
761 list_add(&nfit_mem->list, &acpi_desc->dimms);
764 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
765 if (nfit_dcr->dcr->region_index != dcr)
768 * Record the control region for the dimm. For
769 * the ACPI 6.1 case, where there are separate
770 * control regions for the pmem vs blk
771 * interfaces, be sure to record the extended
775 nfit_mem->dcr = nfit_dcr->dcr;
776 else if (nfit_mem->dcr->windows == 0
777 && nfit_dcr->dcr->windows)
778 nfit_mem->dcr = nfit_dcr->dcr;
782 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
783 struct acpi_nfit_flush_address *flush;
786 if (nfit_flush->flush->device_handle != device_handle)
788 nfit_mem->nfit_flush = nfit_flush;
789 flush = nfit_flush->flush;
790 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
792 * sizeof(struct resource), GFP_KERNEL);
793 if (!nfit_mem->flush_wpq)
795 for (i = 0; i < flush->hint_count; i++) {
796 struct resource *res = &nfit_mem->flush_wpq[i];
798 res->start = flush->hint_address[i];
799 res->end = res->start + 8 - 1;
804 if (dcr && !nfit_mem->dcr) {
805 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
806 spa->range_index, dcr);
810 if (type == NFIT_SPA_DCR) {
811 struct nfit_idt *nfit_idt;
814 /* multiple dimms may share a SPA when interleaved */
815 nfit_mem->spa_dcr = spa;
816 nfit_mem->memdev_dcr = nfit_memdev->memdev;
817 idt_idx = nfit_memdev->memdev->interleave_index;
818 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
819 if (nfit_idt->idt->interleave_index != idt_idx)
821 nfit_mem->idt_dcr = nfit_idt->idt;
824 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
827 * A single dimm may belong to multiple SPA-PM
828 * ranges, record at least one in addition to
831 nfit_mem->memdev_pmem = nfit_memdev->memdev;
838 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
840 struct nfit_mem *a = container_of(_a, typeof(*a), list);
841 struct nfit_mem *b = container_of(_b, typeof(*b), list);
842 u32 handleA, handleB;
844 handleA = __to_nfit_memdev(a)->device_handle;
845 handleB = __to_nfit_memdev(b)->device_handle;
846 if (handleA < handleB)
848 else if (handleA > handleB)
853 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
855 struct nfit_spa *nfit_spa;
858 * For each SPA-DCR or SPA-PMEM address range find its
859 * corresponding MEMDEV(s). From each MEMDEV find the
860 * corresponding DCR. Then, if we're operating on a SPA-DCR,
861 * try to find a SPA-BDW and a corresponding BDW that references
862 * the DCR. Throw it all into an nfit_mem object. Note, that
865 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
868 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
873 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
878 static ssize_t revision_show(struct device *dev,
879 struct device_attribute *attr, char *buf)
881 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
882 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
883 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
885 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
887 static DEVICE_ATTR_RO(revision);
890 * This shows the number of full Address Range Scrubs that have been
891 * completed since driver load time. Userspace can wait on this using
892 * select/poll etc. A '+' at the end indicates an ARS is in progress
894 static ssize_t scrub_show(struct device *dev,
895 struct device_attribute *attr, char *buf)
897 struct nvdimm_bus_descriptor *nd_desc;
901 nd_desc = dev_get_drvdata(dev);
903 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
905 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
906 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
912 static ssize_t scrub_store(struct device *dev,
913 struct device_attribute *attr, const char *buf, size_t size)
915 struct nvdimm_bus_descriptor *nd_desc;
919 rc = kstrtol(buf, 0, &val);
926 nd_desc = dev_get_drvdata(dev);
928 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
930 rc = acpi_nfit_ars_rescan(acpi_desc);
937 static DEVICE_ATTR_RW(scrub);
939 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
941 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
942 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
943 | 1 << ND_CMD_ARS_STATUS;
945 return (nd_desc->cmd_mask & mask) == mask;
948 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
950 struct device *dev = container_of(kobj, struct device, kobj);
951 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
953 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
958 static struct attribute *acpi_nfit_attributes[] = {
959 &dev_attr_revision.attr,
960 &dev_attr_scrub.attr,
964 static struct attribute_group acpi_nfit_attribute_group = {
966 .attrs = acpi_nfit_attributes,
967 .is_visible = nfit_visible,
970 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
971 &nvdimm_bus_attribute_group,
972 &acpi_nfit_attribute_group,
976 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
978 struct nvdimm *nvdimm = to_nvdimm(dev);
979 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
981 return __to_nfit_memdev(nfit_mem);
984 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
986 struct nvdimm *nvdimm = to_nvdimm(dev);
987 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
989 return nfit_mem->dcr;
992 static ssize_t handle_show(struct device *dev,
993 struct device_attribute *attr, char *buf)
995 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
997 return sprintf(buf, "%#x\n", memdev->device_handle);
999 static DEVICE_ATTR_RO(handle);
1001 static ssize_t phys_id_show(struct device *dev,
1002 struct device_attribute *attr, char *buf)
1004 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1006 return sprintf(buf, "%#x\n", memdev->physical_id);
1008 static DEVICE_ATTR_RO(phys_id);
1010 static ssize_t vendor_show(struct device *dev,
1011 struct device_attribute *attr, char *buf)
1013 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1015 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1017 static DEVICE_ATTR_RO(vendor);
1019 static ssize_t rev_id_show(struct device *dev,
1020 struct device_attribute *attr, char *buf)
1022 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1024 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1026 static DEVICE_ATTR_RO(rev_id);
1028 static ssize_t device_show(struct device *dev,
1029 struct device_attribute *attr, char *buf)
1031 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1033 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1035 static DEVICE_ATTR_RO(device);
1037 static ssize_t subsystem_vendor_show(struct device *dev,
1038 struct device_attribute *attr, char *buf)
1040 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1042 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1044 static DEVICE_ATTR_RO(subsystem_vendor);
1046 static ssize_t subsystem_rev_id_show(struct device *dev,
1047 struct device_attribute *attr, char *buf)
1049 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1051 return sprintf(buf, "0x%04x\n",
1052 be16_to_cpu(dcr->subsystem_revision_id));
1054 static DEVICE_ATTR_RO(subsystem_rev_id);
1056 static ssize_t subsystem_device_show(struct device *dev,
1057 struct device_attribute *attr, char *buf)
1059 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1061 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1063 static DEVICE_ATTR_RO(subsystem_device);
1065 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1067 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1070 if (nfit_mem->memdev_pmem)
1072 if (nfit_mem->memdev_bdw)
1077 static ssize_t format_show(struct device *dev,
1078 struct device_attribute *attr, char *buf)
1080 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1082 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1084 static DEVICE_ATTR_RO(format);
1086 static ssize_t format1_show(struct device *dev,
1087 struct device_attribute *attr, char *buf)
1090 ssize_t rc = -ENXIO;
1091 struct nfit_mem *nfit_mem;
1092 struct nfit_memdev *nfit_memdev;
1093 struct acpi_nfit_desc *acpi_desc;
1094 struct nvdimm *nvdimm = to_nvdimm(dev);
1095 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1097 nfit_mem = nvdimm_provider_data(nvdimm);
1098 acpi_desc = nfit_mem->acpi_desc;
1099 handle = to_nfit_memdev(dev)->device_handle;
1101 /* assumes DIMMs have at most 2 published interface codes */
1102 mutex_lock(&acpi_desc->init_mutex);
1103 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1104 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1105 struct nfit_dcr *nfit_dcr;
1107 if (memdev->device_handle != handle)
1110 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1111 if (nfit_dcr->dcr->region_index != memdev->region_index)
1113 if (nfit_dcr->dcr->code == dcr->code)
1115 rc = sprintf(buf, "0x%04x\n",
1116 le16_to_cpu(nfit_dcr->dcr->code));
1122 mutex_unlock(&acpi_desc->init_mutex);
1125 static DEVICE_ATTR_RO(format1);
1127 static ssize_t formats_show(struct device *dev,
1128 struct device_attribute *attr, char *buf)
1130 struct nvdimm *nvdimm = to_nvdimm(dev);
1132 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1134 static DEVICE_ATTR_RO(formats);
1136 static ssize_t serial_show(struct device *dev,
1137 struct device_attribute *attr, char *buf)
1139 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1141 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1143 static DEVICE_ATTR_RO(serial);
1145 static ssize_t family_show(struct device *dev,
1146 struct device_attribute *attr, char *buf)
1148 struct nvdimm *nvdimm = to_nvdimm(dev);
1149 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1151 if (nfit_mem->family < 0)
1153 return sprintf(buf, "%d\n", nfit_mem->family);
1155 static DEVICE_ATTR_RO(family);
1157 static ssize_t dsm_mask_show(struct device *dev,
1158 struct device_attribute *attr, char *buf)
1160 struct nvdimm *nvdimm = to_nvdimm(dev);
1161 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1163 if (nfit_mem->family < 0)
1165 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1167 static DEVICE_ATTR_RO(dsm_mask);
1169 static ssize_t flags_show(struct device *dev,
1170 struct device_attribute *attr, char *buf)
1172 u16 flags = to_nfit_memdev(dev)->flags;
1174 return sprintf(buf, "%s%s%s%s%s\n",
1175 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1176 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1177 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1178 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1179 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "");
1181 static DEVICE_ATTR_RO(flags);
1183 static ssize_t id_show(struct device *dev,
1184 struct device_attribute *attr, char *buf)
1186 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1188 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1189 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1190 be16_to_cpu(dcr->vendor_id),
1191 dcr->manufacturing_location,
1192 be16_to_cpu(dcr->manufacturing_date),
1193 be32_to_cpu(dcr->serial_number));
1195 return sprintf(buf, "%04x-%08x\n",
1196 be16_to_cpu(dcr->vendor_id),
1197 be32_to_cpu(dcr->serial_number));
1199 static DEVICE_ATTR_RO(id);
1201 static struct attribute *acpi_nfit_dimm_attributes[] = {
1202 &dev_attr_handle.attr,
1203 &dev_attr_phys_id.attr,
1204 &dev_attr_vendor.attr,
1205 &dev_attr_device.attr,
1206 &dev_attr_rev_id.attr,
1207 &dev_attr_subsystem_vendor.attr,
1208 &dev_attr_subsystem_device.attr,
1209 &dev_attr_subsystem_rev_id.attr,
1210 &dev_attr_format.attr,
1211 &dev_attr_formats.attr,
1212 &dev_attr_format1.attr,
1213 &dev_attr_serial.attr,
1214 &dev_attr_flags.attr,
1216 &dev_attr_family.attr,
1217 &dev_attr_dsm_mask.attr,
1221 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1222 struct attribute *a, int n)
1224 struct device *dev = container_of(kobj, struct device, kobj);
1225 struct nvdimm *nvdimm = to_nvdimm(dev);
1227 if (!to_nfit_dcr(dev))
1229 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1234 static struct attribute_group acpi_nfit_dimm_attribute_group = {
1236 .attrs = acpi_nfit_dimm_attributes,
1237 .is_visible = acpi_nfit_dimm_attr_visible,
1240 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1241 &nvdimm_attribute_group,
1242 &nd_device_attribute_group,
1243 &acpi_nfit_dimm_attribute_group,
1247 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1250 struct nfit_mem *nfit_mem;
1252 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1253 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1254 return nfit_mem->nvdimm;
1259 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1260 struct nfit_mem *nfit_mem, u32 device_handle)
1262 struct acpi_device *adev, *adev_dimm;
1263 struct device *dev = acpi_desc->dev;
1264 unsigned long dsm_mask;
1268 /* nfit test assumes 1:1 relationship between commands and dsms */
1269 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1270 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1271 adev = to_acpi_dev(acpi_desc);
1275 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1276 nfit_mem->adev = adev_dimm;
1278 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1280 return force_enable_dimms ? 0 : -ENODEV;
1284 * Until standardization materializes we need to consider 4
1285 * different command sets. Note, that checking for function0 (bit0)
1286 * tells us if any commands are reachable through this uuid.
1288 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
1289 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1292 /* limit the supported commands to those that are publicly documented */
1293 nfit_mem->family = i;
1294 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1296 if (disable_vendor_specific)
1297 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1298 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1299 dsm_mask = 0x1c3c76;
1300 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1302 if (disable_vendor_specific)
1303 dsm_mask &= ~(1 << 8);
1304 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1305 dsm_mask = 0xffffffff;
1307 dev_dbg(dev, "unknown dimm command family\n");
1308 nfit_mem->family = -1;
1309 /* DSMs are optional, continue loading the driver... */
1313 uuid = to_nfit_uuid(nfit_mem->family);
1314 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1315 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
1316 set_bit(i, &nfit_mem->dsm_mask);
1321 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1323 struct nfit_mem *nfit_mem;
1326 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1327 struct acpi_nfit_flush_address *flush;
1328 unsigned long flags = 0, cmd_mask;
1329 struct nvdimm *nvdimm;
1334 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1335 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1341 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1342 flags |= NDD_ALIASING;
1344 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
1345 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
1346 flags |= NDD_UNARMED;
1348 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1353 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1354 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1355 * userspace interface.
1357 cmd_mask = 1UL << ND_CMD_CALL;
1358 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1359 cmd_mask |= nfit_mem->dsm_mask;
1361 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1363 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
1364 acpi_nfit_dimm_attribute_groups,
1365 flags, cmd_mask, flush ? flush->hint_count : 0,
1366 nfit_mem->flush_wpq);
1370 nfit_mem->nvdimm = nvdimm;
1373 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1376 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
1377 nvdimm_name(nvdimm),
1378 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1379 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1380 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
1381 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
1385 return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1388 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1390 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1391 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1392 struct acpi_device *adev;
1395 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
1396 adev = to_acpi_dev(acpi_desc);
1400 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
1401 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
1402 set_bit(i, &nd_desc->cmd_mask);
1405 static ssize_t range_index_show(struct device *dev,
1406 struct device_attribute *attr, char *buf)
1408 struct nd_region *nd_region = to_nd_region(dev);
1409 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1411 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1413 static DEVICE_ATTR_RO(range_index);
1415 static struct attribute *acpi_nfit_region_attributes[] = {
1416 &dev_attr_range_index.attr,
1420 static struct attribute_group acpi_nfit_region_attribute_group = {
1422 .attrs = acpi_nfit_region_attributes,
1425 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1426 &nd_region_attribute_group,
1427 &nd_mapping_attribute_group,
1428 &nd_device_attribute_group,
1429 &nd_numa_attribute_group,
1430 &acpi_nfit_region_attribute_group,
1434 /* enough info to uniquely specify an interleave set */
1435 struct nfit_set_info {
1436 struct nfit_set_info_map {
1443 static size_t sizeof_nfit_set_info(int num_mappings)
1445 return sizeof(struct nfit_set_info)
1446 + num_mappings * sizeof(struct nfit_set_info_map);
1449 static int cmp_map(const void *m0, const void *m1)
1451 const struct nfit_set_info_map *map0 = m0;
1452 const struct nfit_set_info_map *map1 = m1;
1454 return memcmp(&map0->region_offset, &map1->region_offset,
1458 /* Retrieve the nth entry referencing this spa */
1459 static struct acpi_nfit_memory_map *memdev_from_spa(
1460 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1462 struct nfit_memdev *nfit_memdev;
1464 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1465 if (nfit_memdev->memdev->range_index == range_index)
1467 return nfit_memdev->memdev;
1471 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1472 struct nd_region_desc *ndr_desc,
1473 struct acpi_nfit_system_address *spa)
1475 int i, spa_type = nfit_spa_type(spa);
1476 struct device *dev = acpi_desc->dev;
1477 struct nd_interleave_set *nd_set;
1478 u16 nr = ndr_desc->num_mappings;
1479 struct nfit_set_info *info;
1481 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1486 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1490 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1493 for (i = 0; i < nr; i++) {
1494 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
1495 struct nfit_set_info_map *map = &info->mapping[i];
1496 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1497 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1498 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1499 spa->range_index, i);
1501 if (!memdev || !nfit_mem->dcr) {
1502 dev_err(dev, "%s: failed to find DCR\n", __func__);
1506 map->region_offset = memdev->region_offset;
1507 map->serial_number = nfit_mem->dcr->serial_number;
1510 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1512 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1513 ndr_desc->nd_set = nd_set;
1514 devm_kfree(dev, info);
1519 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1521 struct acpi_nfit_interleave *idt = mmio->idt;
1522 u32 sub_line_offset, line_index, line_offset;
1523 u64 line_no, table_skip_count, table_offset;
1525 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1526 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1527 line_offset = idt->line_offset[line_index]
1529 table_offset = table_skip_count * mmio->table_size;
1531 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1534 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
1536 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1537 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
1538 const u32 STATUS_MASK = 0x80000037;
1540 if (mmio->num_lines)
1541 offset = to_interleave_offset(offset, mmio);
1543 return readl(mmio->addr.base + offset) & STATUS_MASK;
1546 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1547 resource_size_t dpa, unsigned int len, unsigned int write)
1550 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1553 BCW_OFFSET_MASK = (1ULL << 48)-1,
1555 BCW_LEN_MASK = (1ULL << 8) - 1,
1559 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1560 len = len >> L1_CACHE_SHIFT;
1561 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1562 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1564 offset = nfit_blk->cmd_offset + mmio->size * bw;
1565 if (mmio->num_lines)
1566 offset = to_interleave_offset(offset, mmio);
1568 writeq(cmd, mmio->addr.base + offset);
1569 nvdimm_flush(nfit_blk->nd_region);
1571 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
1572 readq(mmio->addr.base + offset);
1575 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1576 resource_size_t dpa, void *iobuf, size_t len, int rw,
1579 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1580 unsigned int copied = 0;
1584 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1585 + lane * mmio->size;
1586 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1591 if (mmio->num_lines) {
1594 offset = to_interleave_offset(base_offset + copied,
1596 div_u64_rem(offset, mmio->line_size, &line_offset);
1597 c = min_t(size_t, len, mmio->line_size - line_offset);
1599 offset = base_offset + nfit_blk->bdw_offset;
1604 memcpy_to_pmem(mmio->addr.aperture + offset,
1607 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
1608 mmio_flush_range((void __force *)
1609 mmio->addr.aperture + offset, c);
1611 memcpy_from_pmem(iobuf + copied,
1612 mmio->addr.aperture + offset, c);
1620 nvdimm_flush(nfit_blk->nd_region);
1622 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1626 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1627 resource_size_t dpa, void *iobuf, u64 len, int rw)
1629 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1630 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1631 struct nd_region *nd_region = nfit_blk->nd_region;
1632 unsigned int lane, copied = 0;
1635 lane = nd_region_acquire_lane(nd_region);
1637 u64 c = min(len, mmio->size);
1639 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1640 iobuf + copied, c, rw, lane);
1647 nd_region_release_lane(nd_region, lane);
1652 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1653 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1656 mmio->num_lines = idt->line_count;
1657 mmio->line_size = idt->line_size;
1658 if (interleave_ways == 0)
1660 mmio->table_size = mmio->num_lines * interleave_ways
1667 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1668 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1670 struct nd_cmd_dimm_flags flags;
1673 memset(&flags, 0, sizeof(flags));
1674 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
1675 sizeof(flags), NULL);
1677 if (rc >= 0 && flags.status == 0)
1678 nfit_blk->dimm_flags = flags.flags;
1679 else if (rc == -ENOTTY) {
1680 /* fall back to a conservative default */
1681 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
1689 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1692 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1693 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1694 struct nfit_blk_mmio *mmio;
1695 struct nfit_blk *nfit_blk;
1696 struct nfit_mem *nfit_mem;
1697 struct nvdimm *nvdimm;
1700 nvdimm = nd_blk_region_to_dimm(ndbr);
1701 nfit_mem = nvdimm_provider_data(nvdimm);
1702 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1703 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1704 nfit_mem ? "" : " nfit_mem",
1705 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1706 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
1710 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1713 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1714 nfit_blk->nd_region = to_nd_region(dev);
1716 /* map block aperture memory */
1717 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1718 mmio = &nfit_blk->mmio[BDW];
1719 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
1720 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
1721 if (!mmio->addr.base) {
1722 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1723 nvdimm_name(nvdimm));
1726 mmio->size = nfit_mem->bdw->size;
1727 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1728 mmio->idt = nfit_mem->idt_bdw;
1729 mmio->spa = nfit_mem->spa_bdw;
1730 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1731 nfit_mem->memdev_bdw->interleave_ways);
1733 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1734 __func__, nvdimm_name(nvdimm));
1738 /* map block control memory */
1739 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1740 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1741 mmio = &nfit_blk->mmio[DCR];
1742 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1743 nfit_mem->spa_dcr->length);
1744 if (!mmio->addr.base) {
1745 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1746 nvdimm_name(nvdimm));
1749 mmio->size = nfit_mem->dcr->window_size;
1750 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1751 mmio->idt = nfit_mem->idt_dcr;
1752 mmio->spa = nfit_mem->spa_dcr;
1753 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1754 nfit_mem->memdev_dcr->interleave_ways);
1756 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1757 __func__, nvdimm_name(nvdimm));
1761 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1763 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1764 __func__, nvdimm_name(nvdimm));
1768 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
1769 dev_warn(dev, "unable to guarantee persistence of writes\n");
1771 if (mmio->line_size == 0)
1774 if ((u32) nfit_blk->cmd_offset % mmio->line_size
1775 + 8 > mmio->line_size) {
1776 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1778 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1779 + 8 > mmio->line_size) {
1780 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
1787 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
1788 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
1790 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1791 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1794 cmd->address = spa->address;
1795 cmd->length = spa->length;
1796 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
1797 sizeof(*cmd), &cmd_rc);
1803 static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
1807 struct nd_cmd_ars_start ars_start;
1808 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1809 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1811 memset(&ars_start, 0, sizeof(ars_start));
1812 ars_start.address = spa->address;
1813 ars_start.length = spa->length;
1814 if (nfit_spa_type(spa) == NFIT_SPA_PM)
1815 ars_start.type = ND_ARS_PERSISTENT;
1816 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
1817 ars_start.type = ND_ARS_VOLATILE;
1821 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1822 sizeof(ars_start), &cmd_rc);
1829 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
1832 struct nd_cmd_ars_start ars_start;
1833 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1834 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
1836 memset(&ars_start, 0, sizeof(ars_start));
1837 ars_start.address = ars_status->restart_address;
1838 ars_start.length = ars_status->restart_length;
1839 ars_start.type = ars_status->type;
1840 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1841 sizeof(ars_start), &cmd_rc);
1847 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
1849 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1850 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
1853 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
1854 acpi_desc->ars_status_size, &cmd_rc);
1860 static int ars_status_process_records(struct nvdimm_bus *nvdimm_bus,
1861 struct nd_cmd_ars_status *ars_status)
1866 for (i = 0; i < ars_status->num_records; i++) {
1867 rc = nvdimm_bus_add_poison(nvdimm_bus,
1868 ars_status->records[i].err_address,
1869 ars_status->records[i].length);
1877 static void acpi_nfit_remove_resource(void *data)
1879 struct resource *res = data;
1881 remove_resource(res);
1884 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
1885 struct nd_region_desc *ndr_desc)
1887 struct resource *res, *nd_res = ndr_desc->res;
1890 /* No operation if the region is already registered as PMEM */
1891 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
1892 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
1893 if (is_pmem == REGION_INTERSECTS)
1896 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
1900 res->name = "Persistent Memory";
1901 res->start = nd_res->start;
1902 res->end = nd_res->end;
1903 res->flags = IORESOURCE_MEM;
1904 res->desc = IORES_DESC_PERSISTENT_MEMORY;
1906 ret = insert_resource(&iomem_resource, res);
1910 ret = devm_add_action_or_reset(acpi_desc->dev,
1911 acpi_nfit_remove_resource,
1919 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
1920 struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
1921 struct acpi_nfit_memory_map *memdev,
1922 struct nfit_spa *nfit_spa)
1924 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
1925 memdev->device_handle);
1926 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1927 struct nd_blk_region_desc *ndbr_desc;
1928 struct nfit_mem *nfit_mem;
1932 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
1933 spa->range_index, memdev->device_handle);
1937 nd_mapping->nvdimm = nvdimm;
1938 switch (nfit_spa_type(spa)) {
1940 case NFIT_SPA_VOLATILE:
1941 nd_mapping->start = memdev->address;
1942 nd_mapping->size = memdev->region_size;
1945 nfit_mem = nvdimm_provider_data(nvdimm);
1946 if (!nfit_mem || !nfit_mem->bdw) {
1947 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
1948 spa->range_index, nvdimm_name(nvdimm));
1950 nd_mapping->size = nfit_mem->bdw->capacity;
1951 nd_mapping->start = nfit_mem->bdw->start_address;
1952 ndr_desc->num_lanes = nfit_mem->bdw->windows;
1956 ndr_desc->nd_mapping = nd_mapping;
1957 ndr_desc->num_mappings = blk_valid;
1958 ndbr_desc = to_blk_region_desc(ndr_desc);
1959 ndbr_desc->enable = acpi_nfit_blk_region_enable;
1960 ndbr_desc->do_io = acpi_desc->blk_do_io;
1961 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
1963 if (!nfit_spa->nd_region)
1971 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
1973 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
1974 nfit_spa_type(spa) == NFIT_SPA_VCD ||
1975 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
1976 nfit_spa_type(spa) == NFIT_SPA_PCD);
1979 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
1980 struct nfit_spa *nfit_spa)
1982 static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
1983 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1984 struct nd_blk_region_desc ndbr_desc;
1985 struct nd_region_desc *ndr_desc;
1986 struct nfit_memdev *nfit_memdev;
1987 struct nvdimm_bus *nvdimm_bus;
1988 struct resource res;
1991 if (nfit_spa->nd_region)
1994 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
1995 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2000 memset(&res, 0, sizeof(res));
2001 memset(&nd_mappings, 0, sizeof(nd_mappings));
2002 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2003 res.start = spa->address;
2004 res.end = res.start + spa->length - 1;
2005 ndr_desc = &ndbr_desc.ndr_desc;
2006 ndr_desc->res = &res;
2007 ndr_desc->provider_data = nfit_spa;
2008 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2009 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2010 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2011 spa->proximity_domain);
2013 ndr_desc->numa_node = NUMA_NO_NODE;
2015 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2016 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2017 struct nd_mapping *nd_mapping;
2019 if (memdev->range_index != spa->range_index)
2021 if (count >= ND_MAX_MAPPINGS) {
2022 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2023 spa->range_index, ND_MAX_MAPPINGS);
2026 nd_mapping = &nd_mappings[count++];
2027 rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc,
2033 ndr_desc->nd_mapping = nd_mappings;
2034 ndr_desc->num_mappings = count;
2035 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2039 nvdimm_bus = acpi_desc->nvdimm_bus;
2040 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2041 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2043 dev_warn(acpi_desc->dev,
2044 "failed to insert pmem resource to iomem: %d\n",
2049 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2051 if (!nfit_spa->nd_region)
2053 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
2054 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2056 if (!nfit_spa->nd_region)
2058 } else if (nfit_spa_is_virtual(spa)) {
2059 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2061 if (!nfit_spa->nd_region)
2067 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2068 nfit_spa->spa->range_index);
2072 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2075 struct device *dev = acpi_desc->dev;
2076 struct nd_cmd_ars_status *ars_status;
2078 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2079 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2083 if (acpi_desc->ars_status)
2084 devm_kfree(dev, acpi_desc->ars_status);
2085 acpi_desc->ars_status = NULL;
2086 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2089 acpi_desc->ars_status = ars_status;
2090 acpi_desc->ars_status_size = max_ars;
2094 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2095 struct nfit_spa *nfit_spa)
2097 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2100 if (!nfit_spa->max_ars) {
2101 struct nd_cmd_ars_cap ars_cap;
2103 memset(&ars_cap, 0, sizeof(ars_cap));
2104 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2107 nfit_spa->max_ars = ars_cap.max_ars_out;
2108 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2109 /* check that the supported scrub types match the spa type */
2110 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2111 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2113 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2114 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2118 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2121 rc = ars_get_status(acpi_desc);
2122 if (rc < 0 && rc != -ENOSPC)
2125 if (ars_status_process_records(acpi_desc->nvdimm_bus,
2126 acpi_desc->ars_status))
2132 static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2133 struct nfit_spa *nfit_spa)
2135 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2136 unsigned int overflow_retry = scrub_overflow_abort;
2137 u64 init_ars_start = 0, init_ars_len = 0;
2138 struct device *dev = acpi_desc->dev;
2139 unsigned int tmo = scrub_timeout;
2142 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
2145 rc = ars_start(acpi_desc, nfit_spa);
2147 * If we timed out the initial scan we'll still be busy here,
2148 * and will wait another timeout before giving up permanently.
2150 if (rc < 0 && rc != -EBUSY)
2154 u64 ars_start, ars_len;
2156 if (acpi_desc->cancel)
2158 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2161 if (rc == -EBUSY && !tmo) {
2162 dev_warn(dev, "range %d ars timeout, aborting\n",
2169 * Note, entries may be appended to the list
2170 * while the lock is dropped, but the workqueue
2171 * being active prevents entries being deleted /
2174 mutex_unlock(&acpi_desc->init_mutex);
2177 mutex_lock(&acpi_desc->init_mutex);
2181 /* we got some results, but there are more pending... */
2182 if (rc == -ENOSPC && overflow_retry--) {
2183 if (!init_ars_len) {
2184 init_ars_len = acpi_desc->ars_status->length;
2185 init_ars_start = acpi_desc->ars_status->address;
2187 rc = ars_continue(acpi_desc);
2191 dev_warn(dev, "range %d ars continuation failed\n",
2197 ars_start = init_ars_start;
2198 ars_len = init_ars_len;
2200 ars_start = acpi_desc->ars_status->address;
2201 ars_len = acpi_desc->ars_status->length;
2203 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2204 spa->range_index, ars_start, ars_len);
2205 /* notify the region about new poison entries */
2206 nvdimm_region_notify(nfit_spa->nd_region,
2207 NVDIMM_REVALIDATE_POISON);
2212 static void acpi_nfit_scrub(struct work_struct *work)
2215 u64 init_scrub_length = 0;
2216 struct nfit_spa *nfit_spa;
2217 u64 init_scrub_address = 0;
2218 bool init_ars_done = false;
2219 struct acpi_nfit_desc *acpi_desc;
2220 unsigned int tmo = scrub_timeout;
2221 unsigned int overflow_retry = scrub_overflow_abort;
2223 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2224 dev = acpi_desc->dev;
2227 * We scrub in 2 phases. The first phase waits for any platform
2228 * firmware initiated scrubs to complete and then we go search for the
2229 * affected spa regions to mark them scanned. In the second phase we
2230 * initiate a directed scrub for every range that was not scrubbed in
2231 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2232 * the first phase, but really only care about running phase 2, where
2233 * regions can be notified of new poison.
2236 /* process platform firmware initiated scrubs */
2238 mutex_lock(&acpi_desc->init_mutex);
2239 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2240 struct nd_cmd_ars_status *ars_status;
2241 struct acpi_nfit_system_address *spa;
2242 u64 ars_start, ars_len;
2245 if (acpi_desc->cancel)
2248 if (nfit_spa->nd_region)
2251 if (init_ars_done) {
2253 * No need to re-query, we're now just
2254 * reconciling all the ranges covered by the
2259 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2261 if (rc == -ENOTTY) {
2262 /* no ars capability, just register spa and move on */
2263 acpi_nfit_register_region(acpi_desc, nfit_spa);
2267 if (rc == -EBUSY && !tmo) {
2268 /* fallthrough to directed scrub in phase 2 */
2269 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2271 } else if (rc == -EBUSY) {
2272 mutex_unlock(&acpi_desc->init_mutex);
2278 /* we got some results, but there are more pending... */
2279 if (rc == -ENOSPC && overflow_retry--) {
2280 ars_status = acpi_desc->ars_status;
2282 * Record the original scrub range, so that we
2283 * can recall all the ranges impacted by the
2286 if (!init_scrub_length) {
2287 init_scrub_length = ars_status->length;
2288 init_scrub_address = ars_status->address;
2290 rc = ars_continue(acpi_desc);
2292 mutex_unlock(&acpi_desc->init_mutex);
2299 * Initial scrub failed, we'll give it one more
2305 /* We got some final results, record completed ranges */
2306 ars_status = acpi_desc->ars_status;
2307 if (init_scrub_length) {
2308 ars_start = init_scrub_address;
2309 ars_len = ars_start + init_scrub_length;
2311 ars_start = ars_status->address;
2312 ars_len = ars_status->length;
2314 spa = nfit_spa->spa;
2316 if (!init_ars_done) {
2317 init_ars_done = true;
2318 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2319 ars_start, ars_len);
2321 if (ars_start <= spa->address && ars_start + ars_len
2322 >= spa->address + spa->length)
2323 acpi_nfit_register_region(acpi_desc, nfit_spa);
2327 * For all the ranges not covered by an initial scrub we still
2328 * want to see if there are errors, but it's ok to discover them
2331 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2333 * Flag all the ranges that still need scrubbing, but
2334 * register them now to make data available.
2336 if (!nfit_spa->nd_region) {
2337 nfit_spa->ars_required = 1;
2338 acpi_nfit_register_region(acpi_desc, nfit_spa);
2342 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2343 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
2344 acpi_desc->scrub_count++;
2345 if (acpi_desc->scrub_count_state)
2346 sysfs_notify_dirent(acpi_desc->scrub_count_state);
2347 mutex_unlock(&acpi_desc->init_mutex);
2350 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2352 struct nfit_spa *nfit_spa;
2355 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2356 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2357 /* BLK regions don't need to wait for ars results */
2358 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2363 queue_work(nfit_wq, &acpi_desc->work);
2367 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2368 struct nfit_table_prev *prev)
2370 struct device *dev = acpi_desc->dev;
2372 if (!list_empty(&prev->spas) ||
2373 !list_empty(&prev->memdevs) ||
2374 !list_empty(&prev->dcrs) ||
2375 !list_empty(&prev->bdws) ||
2376 !list_empty(&prev->idts) ||
2377 !list_empty(&prev->flushes)) {
2378 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2384 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2386 struct device *dev = acpi_desc->dev;
2387 struct kernfs_node *nfit;
2388 struct device *bus_dev;
2390 if (!ars_supported(acpi_desc->nvdimm_bus))
2393 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2394 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2396 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2399 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2401 if (!acpi_desc->scrub_count_state) {
2402 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2409 static void acpi_nfit_destruct(void *data)
2411 struct acpi_nfit_desc *acpi_desc = data;
2412 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2415 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2418 mutex_lock(&acpi_desc_lock);
2419 acpi_desc->cancel = 1;
2421 * Bounce the nvdimm bus lock to make sure any in-flight
2422 * acpi_nfit_ars_rescan() submissions have had a chance to
2423 * either submit or see ->cancel set.
2425 device_lock(bus_dev);
2426 device_unlock(bus_dev);
2428 flush_workqueue(nfit_wq);
2429 if (acpi_desc->scrub_count_state)
2430 sysfs_put(acpi_desc->scrub_count_state);
2431 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2432 acpi_desc->nvdimm_bus = NULL;
2433 list_del(&acpi_desc->list);
2434 mutex_unlock(&acpi_desc_lock);
2437 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
2439 struct device *dev = acpi_desc->dev;
2440 struct nfit_table_prev prev;
2444 if (!acpi_desc->nvdimm_bus) {
2445 acpi_nfit_init_dsms(acpi_desc);
2447 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2448 &acpi_desc->nd_desc);
2449 if (!acpi_desc->nvdimm_bus)
2452 rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
2457 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2461 /* register this acpi_desc for mce notifications */
2462 mutex_lock(&acpi_desc_lock);
2463 list_add_tail(&acpi_desc->list, &acpi_descs);
2464 mutex_unlock(&acpi_desc_lock);
2467 mutex_lock(&acpi_desc->init_mutex);
2469 INIT_LIST_HEAD(&prev.spas);
2470 INIT_LIST_HEAD(&prev.memdevs);
2471 INIT_LIST_HEAD(&prev.dcrs);
2472 INIT_LIST_HEAD(&prev.bdws);
2473 INIT_LIST_HEAD(&prev.idts);
2474 INIT_LIST_HEAD(&prev.flushes);
2476 list_cut_position(&prev.spas, &acpi_desc->spas,
2477 acpi_desc->spas.prev);
2478 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2479 acpi_desc->memdevs.prev);
2480 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2481 acpi_desc->dcrs.prev);
2482 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2483 acpi_desc->bdws.prev);
2484 list_cut_position(&prev.idts, &acpi_desc->idts,
2485 acpi_desc->idts.prev);
2486 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2487 acpi_desc->flushes.prev);
2490 while (!IS_ERR_OR_NULL(data))
2491 data = add_table(acpi_desc, &prev, data, end);
2494 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2500 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2504 rc = nfit_mem_init(acpi_desc);
2508 rc = acpi_nfit_register_dimms(acpi_desc);
2512 rc = acpi_nfit_register_regions(acpi_desc);
2515 mutex_unlock(&acpi_desc->init_mutex);
2518 EXPORT_SYMBOL_GPL(acpi_nfit_init);
2520 struct acpi_nfit_flush_work {
2521 struct work_struct work;
2522 struct completion cmp;
2525 static void flush_probe(struct work_struct *work)
2527 struct acpi_nfit_flush_work *flush;
2529 flush = container_of(work, typeof(*flush), work);
2530 complete(&flush->cmp);
2533 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2535 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2536 struct device *dev = acpi_desc->dev;
2537 struct acpi_nfit_flush_work flush;
2539 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2544 * Scrub work could take 10s of seconds, userspace may give up so we
2545 * need to be interruptible while waiting.
2547 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2548 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2549 queue_work(nfit_wq, &flush.work);
2550 return wait_for_completion_interruptible(&flush.cmp);
2553 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2554 struct nvdimm *nvdimm, unsigned int cmd)
2556 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2560 if (cmd != ND_CMD_ARS_START)
2564 * The kernel and userspace may race to initiate a scrub, but
2565 * the scrub thread is prepared to lose that initial race. It
2566 * just needs guarantees that any ars it initiates are not
2567 * interrupted by any intervening start reqeusts from userspace.
2569 if (work_busy(&acpi_desc->work))
2575 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
2577 struct device *dev = acpi_desc->dev;
2578 struct nfit_spa *nfit_spa;
2580 if (work_busy(&acpi_desc->work))
2583 if (acpi_desc->cancel)
2586 mutex_lock(&acpi_desc->init_mutex);
2587 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2588 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2590 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2593 nfit_spa->ars_required = 1;
2595 queue_work(nfit_wq, &acpi_desc->work);
2596 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2597 mutex_unlock(&acpi_desc->init_mutex);
2602 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
2604 struct nvdimm_bus_descriptor *nd_desc;
2606 dev_set_drvdata(dev, acpi_desc);
2607 acpi_desc->dev = dev;
2608 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2609 nd_desc = &acpi_desc->nd_desc;
2610 nd_desc->provider_name = "ACPI.NFIT";
2611 nd_desc->module = THIS_MODULE;
2612 nd_desc->ndctl = acpi_nfit_ctl;
2613 nd_desc->flush_probe = acpi_nfit_flush_probe;
2614 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
2615 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2617 INIT_LIST_HEAD(&acpi_desc->spas);
2618 INIT_LIST_HEAD(&acpi_desc->dcrs);
2619 INIT_LIST_HEAD(&acpi_desc->bdws);
2620 INIT_LIST_HEAD(&acpi_desc->idts);
2621 INIT_LIST_HEAD(&acpi_desc->flushes);
2622 INIT_LIST_HEAD(&acpi_desc->memdevs);
2623 INIT_LIST_HEAD(&acpi_desc->dimms);
2624 INIT_LIST_HEAD(&acpi_desc->list);
2625 mutex_init(&acpi_desc->init_mutex);
2626 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
2628 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
2630 static int acpi_nfit_add(struct acpi_device *adev)
2632 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
2633 struct acpi_nfit_desc *acpi_desc;
2634 struct device *dev = &adev->dev;
2635 struct acpi_table_header *tbl;
2636 acpi_status status = AE_OK;
2640 status = acpi_get_table_with_size(ACPI_SIG_NFIT, 0, &tbl, &sz);
2641 if (ACPI_FAILURE(status)) {
2642 /* This is ok, we could have an nvdimm hotplugged later */
2643 dev_dbg(dev, "failed to find NFIT at startup\n");
2647 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2650 acpi_nfit_desc_init(acpi_desc, &adev->dev);
2652 /* Save the acpi header for exporting the revision via sysfs */
2653 acpi_desc->acpi_header = *tbl;
2655 /* Evaluate _FIT and override with that if present */
2656 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2657 if (ACPI_SUCCESS(status) && buf.length > 0) {
2658 union acpi_object *obj = buf.pointer;
2660 if (obj->type == ACPI_TYPE_BUFFER)
2661 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2662 obj->buffer.length);
2664 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2665 __func__, (int) obj->type);
2668 /* skip over the lead-in header table */
2669 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2670 + sizeof(struct acpi_table_nfit),
2671 sz - sizeof(struct acpi_table_nfit));
2675 static int acpi_nfit_remove(struct acpi_device *adev)
2677 /* see acpi_nfit_destruct */
2681 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2683 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
2684 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
2685 struct device *dev = &adev->dev;
2686 union acpi_object *obj;
2690 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2694 /* dev->driver may be null if we're being removed */
2695 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
2700 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2703 acpi_nfit_desc_init(acpi_desc, &adev->dev);
2706 * Finish previous registration before considering new
2709 flush_workqueue(nfit_wq);
2713 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2714 if (ACPI_FAILURE(status)) {
2715 dev_err(dev, "failed to evaluate _FIT\n");
2720 if (obj->type == ACPI_TYPE_BUFFER) {
2721 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2722 obj->buffer.length);
2724 dev_err(dev, "failed to merge updated NFIT\n");
2726 dev_err(dev, "Invalid _FIT\n");
2733 static const struct acpi_device_id acpi_nfit_ids[] = {
2737 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
2739 static struct acpi_driver acpi_nfit_driver = {
2740 .name = KBUILD_MODNAME,
2741 .ids = acpi_nfit_ids,
2743 .add = acpi_nfit_add,
2744 .remove = acpi_nfit_remove,
2745 .notify = acpi_nfit_notify,
2749 static __init int nfit_init(void)
2751 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
2752 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
2753 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
2754 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
2755 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
2756 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
2757 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
2759 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
2760 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
2761 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
2762 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
2763 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
2764 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
2765 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
2766 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
2767 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
2768 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
2769 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
2770 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
2771 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
2773 nfit_wq = create_singlethread_workqueue("nfit");
2777 nfit_mce_register();
2779 return acpi_bus_register_driver(&acpi_nfit_driver);
2782 static __exit void nfit_exit(void)
2784 nfit_mce_unregister();
2785 acpi_bus_unregister_driver(&acpi_nfit_driver);
2786 destroy_workqueue(nfit_wq);
2787 WARN_ON(!list_empty(&acpi_descs));
2790 module_init(nfit_init);
2791 module_exit(nfit_exit);
2792 MODULE_LICENSE("GPL v2");
2793 MODULE_AUTHOR("Intel Corporation");