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>
26 #include <acpi/nfit.h>
32 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
35 #include <linux/io-64-nonatomic-hi-lo.h>
37 static bool force_enable_dimms;
38 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
39 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
41 static bool disable_vendor_specific;
42 module_param(disable_vendor_specific, bool, S_IRUGO);
43 MODULE_PARM_DESC(disable_vendor_specific,
44 "Limit commands to the publicly specified set");
46 static unsigned long override_dsm_mask;
47 module_param(override_dsm_mask, ulong, S_IRUGO);
48 MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
50 static int default_dsm_family = -1;
51 module_param(default_dsm_family, int, S_IRUGO);
52 MODULE_PARM_DESC(default_dsm_family,
53 "Try this DSM type first when identifying NVDIMM family");
55 static bool no_init_ars;
56 module_param(no_init_ars, bool, 0644);
57 MODULE_PARM_DESC(no_init_ars, "Skip ARS run at nfit init time");
59 static bool force_labels;
60 module_param(force_labels, bool, 0444);
61 MODULE_PARM_DESC(force_labels, "Opt-in to labels despite missing methods");
63 LIST_HEAD(acpi_descs);
64 DEFINE_MUTEX(acpi_desc_lock);
66 static struct workqueue_struct *nfit_wq;
68 struct nfit_table_prev {
69 struct list_head spas;
70 struct list_head memdevs;
71 struct list_head dcrs;
72 struct list_head bdws;
73 struct list_head idts;
74 struct list_head flushes;
77 static guid_t nfit_uuid[NFIT_UUID_MAX];
79 const guid_t *to_nfit_uuid(enum nfit_uuids id)
81 return &nfit_uuid[id];
83 EXPORT_SYMBOL(to_nfit_uuid);
85 static struct acpi_nfit_desc *to_acpi_nfit_desc(
86 struct nvdimm_bus_descriptor *nd_desc)
88 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
91 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
93 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
96 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
99 if (!nd_desc->provider_name
100 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
103 return to_acpi_device(acpi_desc->dev);
106 static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
108 struct nd_cmd_clear_error *clear_err;
109 struct nd_cmd_ars_status *ars_status;
114 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
121 /* No supported scan types for this range */
122 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
123 if ((status >> 16 & flags) == 0)
126 case ND_CMD_ARS_START:
127 /* ARS is in progress */
128 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
135 case ND_CMD_ARS_STATUS:
140 /* Check extended status (Upper two bytes) */
141 if (status == NFIT_ARS_STATUS_DONE)
144 /* ARS is in progress */
145 if (status == NFIT_ARS_STATUS_BUSY)
148 /* No ARS performed for the current boot */
149 if (status == NFIT_ARS_STATUS_NONE)
153 * ARS interrupted, either we overflowed or some other
154 * agent wants the scan to stop. If we didn't overflow
155 * then just continue with the returned results.
157 if (status == NFIT_ARS_STATUS_INTR) {
158 if (ars_status->out_length >= 40 && (ars_status->flags
159 & NFIT_ARS_F_OVERFLOW))
168 case ND_CMD_CLEAR_ERROR:
172 if (!clear_err->cleared)
174 if (clear_err->length > clear_err->cleared)
175 return clear_err->cleared;
181 /* all other non-zero status results in an error */
187 #define ACPI_LABELS_LOCKED 3
189 static int xlat_nvdimm_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
192 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
195 case ND_CMD_GET_CONFIG_SIZE:
197 * In the _LSI, _LSR, _LSW case the locked status is
198 * communicated via the read/write commands
200 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
203 if (status >> 16 & ND_CONFIG_LOCKED)
206 case ND_CMD_GET_CONFIG_DATA:
207 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
208 && status == ACPI_LABELS_LOCKED)
211 case ND_CMD_SET_CONFIG_DATA:
212 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
213 && status == ACPI_LABELS_LOCKED)
220 /* all other non-zero status results in an error */
226 static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
230 return xlat_bus_status(buf, cmd, status);
231 return xlat_nvdimm_status(nvdimm, buf, cmd, status);
234 /* convert _LS{I,R} packages to the buffer object acpi_nfit_ctl expects */
235 static union acpi_object *pkg_to_buf(union acpi_object *pkg)
240 union acpi_object *buf = NULL;
242 if (pkg->type != ACPI_TYPE_PACKAGE) {
243 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
248 for (i = 0; i < pkg->package.count; i++) {
249 union acpi_object *obj = &pkg->package.elements[i];
251 if (obj->type == ACPI_TYPE_INTEGER)
253 else if (obj->type == ACPI_TYPE_BUFFER)
254 size += obj->buffer.length;
256 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
262 buf = ACPI_ALLOCATE(sizeof(*buf) + size);
267 buf->type = ACPI_TYPE_BUFFER;
268 buf->buffer.length = size;
269 buf->buffer.pointer = dst;
270 for (i = 0; i < pkg->package.count; i++) {
271 union acpi_object *obj = &pkg->package.elements[i];
273 if (obj->type == ACPI_TYPE_INTEGER) {
274 memcpy(dst, &obj->integer.value, 4);
276 } else if (obj->type == ACPI_TYPE_BUFFER) {
277 memcpy(dst, obj->buffer.pointer, obj->buffer.length);
278 dst += obj->buffer.length;
286 static union acpi_object *int_to_buf(union acpi_object *integer)
288 union acpi_object *buf = ACPI_ALLOCATE(sizeof(*buf) + 4);
294 if (integer->type != ACPI_TYPE_INTEGER) {
295 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
301 buf->type = ACPI_TYPE_BUFFER;
302 buf->buffer.length = 4;
303 buf->buffer.pointer = dst;
304 memcpy(dst, &integer->integer.value, 4);
310 static union acpi_object *acpi_label_write(acpi_handle handle, u32 offset,
314 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
315 struct acpi_object_list input = {
317 .pointer = (union acpi_object []) {
319 .integer.type = ACPI_TYPE_INTEGER,
320 .integer.value = offset,
323 .integer.type = ACPI_TYPE_INTEGER,
324 .integer.value = len,
327 .buffer.type = ACPI_TYPE_BUFFER,
328 .buffer.pointer = data,
329 .buffer.length = len,
334 rc = acpi_evaluate_object(handle, "_LSW", &input, &buf);
335 if (ACPI_FAILURE(rc))
337 return int_to_buf(buf.pointer);
340 static union acpi_object *acpi_label_read(acpi_handle handle, u32 offset,
344 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
345 struct acpi_object_list input = {
347 .pointer = (union acpi_object []) {
349 .integer.type = ACPI_TYPE_INTEGER,
350 .integer.value = offset,
353 .integer.type = ACPI_TYPE_INTEGER,
354 .integer.value = len,
359 rc = acpi_evaluate_object(handle, "_LSR", &input, &buf);
360 if (ACPI_FAILURE(rc))
362 return pkg_to_buf(buf.pointer);
365 static union acpi_object *acpi_label_info(acpi_handle handle)
368 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
370 rc = acpi_evaluate_object(handle, "_LSI", NULL, &buf);
371 if (ACPI_FAILURE(rc))
373 return pkg_to_buf(buf.pointer);
376 static u8 nfit_dsm_revid(unsigned family, unsigned func)
378 static const u8 revid_table[NVDIMM_FAMILY_MAX+1][32] = {
379 [NVDIMM_FAMILY_INTEL] = {
380 [NVDIMM_INTEL_GET_MODES] = 2,
381 [NVDIMM_INTEL_GET_FWINFO] = 2,
382 [NVDIMM_INTEL_START_FWUPDATE] = 2,
383 [NVDIMM_INTEL_SEND_FWUPDATE] = 2,
384 [NVDIMM_INTEL_FINISH_FWUPDATE] = 2,
385 [NVDIMM_INTEL_QUERY_FWUPDATE] = 2,
386 [NVDIMM_INTEL_SET_THRESHOLD] = 2,
387 [NVDIMM_INTEL_INJECT_ERROR] = 2,
388 [NVDIMM_INTEL_GET_SECURITY_STATE] = 2,
389 [NVDIMM_INTEL_SET_PASSPHRASE] = 2,
390 [NVDIMM_INTEL_DISABLE_PASSPHRASE] = 2,
391 [NVDIMM_INTEL_UNLOCK_UNIT] = 2,
392 [NVDIMM_INTEL_FREEZE_LOCK] = 2,
393 [NVDIMM_INTEL_SECURE_ERASE] = 2,
394 [NVDIMM_INTEL_OVERWRITE] = 2,
395 [NVDIMM_INTEL_QUERY_OVERWRITE] = 2,
396 [NVDIMM_INTEL_SET_MASTER_PASSPHRASE] = 2,
397 [NVDIMM_INTEL_MASTER_SECURE_ERASE] = 2,
402 if (family > NVDIMM_FAMILY_MAX)
406 id = revid_table[family][func];
408 return 1; /* default */
412 static bool payload_dumpable(struct nvdimm *nvdimm, unsigned int func)
414 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
416 if (nfit_mem && nfit_mem->family == NVDIMM_FAMILY_INTEL
417 && func >= NVDIMM_INTEL_GET_SECURITY_STATE
418 && func <= NVDIMM_INTEL_MASTER_SECURE_ERASE)
419 return IS_ENABLED(CONFIG_NFIT_SECURITY_DEBUG);
423 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
424 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
426 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
427 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
428 union acpi_object in_obj, in_buf, *out_obj;
429 const struct nd_cmd_desc *desc = NULL;
430 struct device *dev = acpi_desc->dev;
431 struct nd_cmd_pkg *call_pkg = NULL;
432 const char *cmd_name, *dimm_name;
433 unsigned long cmd_mask, dsm_mask;
434 u32 offset, fw_status = 0;
443 if (cmd == ND_CMD_CALL) {
445 func = call_pkg->nd_command;
447 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
448 if (call_pkg->nd_reserved2[i])
453 struct acpi_device *adev = nfit_mem->adev;
457 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
460 dimm_name = nvdimm_name(nvdimm);
461 cmd_name = nvdimm_cmd_name(cmd);
462 cmd_mask = nvdimm_cmd_mask(nvdimm);
463 dsm_mask = nfit_mem->dsm_mask;
464 desc = nd_cmd_dimm_desc(cmd);
465 guid = to_nfit_uuid(nfit_mem->family);
466 handle = adev->handle;
468 struct acpi_device *adev = to_acpi_dev(acpi_desc);
470 cmd_name = nvdimm_bus_cmd_name(cmd);
471 cmd_mask = nd_desc->cmd_mask;
473 if (cmd == ND_CMD_CALL)
474 dsm_mask = nd_desc->bus_dsm_mask;
475 desc = nd_cmd_bus_desc(cmd);
476 guid = to_nfit_uuid(NFIT_DEV_BUS);
477 handle = adev->handle;
481 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
484 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
487 in_obj.type = ACPI_TYPE_PACKAGE;
488 in_obj.package.count = 1;
489 in_obj.package.elements = &in_buf;
490 in_buf.type = ACPI_TYPE_BUFFER;
491 in_buf.buffer.pointer = buf;
492 in_buf.buffer.length = 0;
494 /* libnvdimm has already validated the input envelope */
495 for (i = 0; i < desc->in_num; i++)
496 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
500 /* skip over package wrapper */
501 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
502 in_buf.buffer.length = call_pkg->nd_size_in;
505 dev_dbg(dev, "%s cmd: %d: func: %d input length: %d\n",
506 dimm_name, cmd, func, in_buf.buffer.length);
507 if (payload_dumpable(nvdimm, func))
508 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
509 in_buf.buffer.pointer,
510 min_t(u32, 256, in_buf.buffer.length), true);
512 /* call the BIOS, prefer the named methods over _DSM if available */
513 if (nvdimm && cmd == ND_CMD_GET_CONFIG_SIZE
514 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags))
515 out_obj = acpi_label_info(handle);
516 else if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA
517 && test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
518 struct nd_cmd_get_config_data_hdr *p = buf;
520 out_obj = acpi_label_read(handle, p->in_offset, p->in_length);
521 } else if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA
522 && test_bit(NFIT_MEM_LSW, &nfit_mem->flags)) {
523 struct nd_cmd_set_config_hdr *p = buf;
525 out_obj = acpi_label_write(handle, p->in_offset, p->in_length,
531 revid = nfit_dsm_revid(nfit_mem->family, func);
534 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
538 dev_dbg(dev, "%s _DSM failed cmd: %s\n", dimm_name, cmd_name);
542 if (out_obj->type != ACPI_TYPE_BUFFER) {
543 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
544 dimm_name, cmd_name, out_obj->type);
550 call_pkg->nd_fw_size = out_obj->buffer.length;
551 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
552 out_obj->buffer.pointer,
553 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
557 * Need to support FW function w/o known size in advance.
558 * Caller can determine required size based upon nd_fw_size.
559 * If we return an error (like elsewhere) then caller wouldn't
560 * be able to rely upon data returned to make calculation.
567 dev_dbg(dev, "%s cmd: %s output length: %d\n", dimm_name,
568 cmd_name, out_obj->buffer.length);
569 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
570 out_obj->buffer.pointer,
571 min_t(u32, 128, out_obj->buffer.length), true);
573 for (i = 0, offset = 0; i < desc->out_num; i++) {
574 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
575 (u32 *) out_obj->buffer.pointer,
576 out_obj->buffer.length - offset);
578 if (offset + out_size > out_obj->buffer.length) {
579 dev_dbg(dev, "%s output object underflow cmd: %s field: %d\n",
580 dimm_name, cmd_name, i);
584 if (in_buf.buffer.length + offset + out_size > buf_len) {
585 dev_dbg(dev, "%s output overrun cmd: %s field: %d\n",
586 dimm_name, cmd_name, i);
590 memcpy(buf + in_buf.buffer.length + offset,
591 out_obj->buffer.pointer + offset, out_size);
596 * Set fw_status for all the commands with a known format to be
597 * later interpreted by xlat_status().
599 if (i >= 1 && ((!nvdimm && cmd >= ND_CMD_ARS_CAP
600 && cmd <= ND_CMD_CLEAR_ERROR)
601 || (nvdimm && cmd >= ND_CMD_SMART
602 && cmd <= ND_CMD_VENDOR)))
603 fw_status = *(u32 *) out_obj->buffer.pointer;
605 if (offset + in_buf.buffer.length < buf_len) {
608 * status valid, return the number of bytes left
609 * unfilled in the output buffer
611 rc = buf_len - offset - in_buf.buffer.length;
613 *cmd_rc = xlat_status(nvdimm, buf, cmd,
616 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
617 __func__, dimm_name, cmd_name, buf_len,
624 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
632 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
634 static const char *spa_type_name(u16 type)
636 static const char *to_name[] = {
637 [NFIT_SPA_VOLATILE] = "volatile",
638 [NFIT_SPA_PM] = "pmem",
639 [NFIT_SPA_DCR] = "dimm-control-region",
640 [NFIT_SPA_BDW] = "block-data-window",
641 [NFIT_SPA_VDISK] = "volatile-disk",
642 [NFIT_SPA_VCD] = "volatile-cd",
643 [NFIT_SPA_PDISK] = "persistent-disk",
644 [NFIT_SPA_PCD] = "persistent-cd",
648 if (type > NFIT_SPA_PCD)
651 return to_name[type];
654 int nfit_spa_type(struct acpi_nfit_system_address *spa)
658 for (i = 0; i < NFIT_UUID_MAX; i++)
659 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
664 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
665 struct nfit_table_prev *prev,
666 struct acpi_nfit_system_address *spa)
668 struct device *dev = acpi_desc->dev;
669 struct nfit_spa *nfit_spa;
671 if (spa->header.length != sizeof(*spa))
674 list_for_each_entry(nfit_spa, &prev->spas, list) {
675 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
676 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
681 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
685 INIT_LIST_HEAD(&nfit_spa->list);
686 memcpy(nfit_spa->spa, spa, sizeof(*spa));
687 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
688 dev_dbg(dev, "spa index: %d type: %s\n",
690 spa_type_name(nfit_spa_type(spa)));
694 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
695 struct nfit_table_prev *prev,
696 struct acpi_nfit_memory_map *memdev)
698 struct device *dev = acpi_desc->dev;
699 struct nfit_memdev *nfit_memdev;
701 if (memdev->header.length != sizeof(*memdev))
704 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
705 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
706 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
710 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
714 INIT_LIST_HEAD(&nfit_memdev->list);
715 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
716 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
717 dev_dbg(dev, "memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
718 memdev->device_handle, memdev->range_index,
719 memdev->region_index, memdev->flags);
723 int nfit_get_smbios_id(u32 device_handle, u16 *flags)
725 struct acpi_nfit_memory_map *memdev;
726 struct acpi_nfit_desc *acpi_desc;
727 struct nfit_mem *nfit_mem;
729 mutex_lock(&acpi_desc_lock);
730 list_for_each_entry(acpi_desc, &acpi_descs, list) {
731 mutex_lock(&acpi_desc->init_mutex);
732 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
733 memdev = __to_nfit_memdev(nfit_mem);
734 if (memdev->device_handle == device_handle) {
735 mutex_unlock(&acpi_desc->init_mutex);
736 mutex_unlock(&acpi_desc_lock);
737 *flags = memdev->flags;
738 return memdev->physical_id;
741 mutex_unlock(&acpi_desc->init_mutex);
743 mutex_unlock(&acpi_desc_lock);
747 EXPORT_SYMBOL_GPL(nfit_get_smbios_id);
750 * An implementation may provide a truncated control region if no block windows
753 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
755 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
760 return offsetof(struct acpi_nfit_control_region, window_size);
763 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
764 struct nfit_table_prev *prev,
765 struct acpi_nfit_control_region *dcr)
767 struct device *dev = acpi_desc->dev;
768 struct nfit_dcr *nfit_dcr;
770 if (!sizeof_dcr(dcr))
773 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
774 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
775 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
779 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
783 INIT_LIST_HEAD(&nfit_dcr->list);
784 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
785 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
786 dev_dbg(dev, "dcr index: %d windows: %d\n",
787 dcr->region_index, dcr->windows);
791 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
792 struct nfit_table_prev *prev,
793 struct acpi_nfit_data_region *bdw)
795 struct device *dev = acpi_desc->dev;
796 struct nfit_bdw *nfit_bdw;
798 if (bdw->header.length != sizeof(*bdw))
800 list_for_each_entry(nfit_bdw, &prev->bdws, list)
801 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
802 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
806 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
810 INIT_LIST_HEAD(&nfit_bdw->list);
811 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
812 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
813 dev_dbg(dev, "bdw dcr: %d windows: %d\n",
814 bdw->region_index, bdw->windows);
818 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
820 if (idt->header.length < sizeof(*idt))
822 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
825 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
826 struct nfit_table_prev *prev,
827 struct acpi_nfit_interleave *idt)
829 struct device *dev = acpi_desc->dev;
830 struct nfit_idt *nfit_idt;
832 if (!sizeof_idt(idt))
835 list_for_each_entry(nfit_idt, &prev->idts, list) {
836 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
839 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
840 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
845 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
849 INIT_LIST_HEAD(&nfit_idt->list);
850 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
851 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
852 dev_dbg(dev, "idt index: %d num_lines: %d\n",
853 idt->interleave_index, idt->line_count);
857 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
859 if (flush->header.length < sizeof(*flush))
861 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
864 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
865 struct nfit_table_prev *prev,
866 struct acpi_nfit_flush_address *flush)
868 struct device *dev = acpi_desc->dev;
869 struct nfit_flush *nfit_flush;
871 if (!sizeof_flush(flush))
874 list_for_each_entry(nfit_flush, &prev->flushes, list) {
875 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
878 if (memcmp(nfit_flush->flush, flush,
879 sizeof_flush(flush)) == 0) {
880 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
885 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
886 + sizeof_flush(flush), GFP_KERNEL);
889 INIT_LIST_HEAD(&nfit_flush->list);
890 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
891 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
892 dev_dbg(dev, "nfit_flush handle: %d hint_count: %d\n",
893 flush->device_handle, flush->hint_count);
897 static bool add_platform_cap(struct acpi_nfit_desc *acpi_desc,
898 struct acpi_nfit_capabilities *pcap)
900 struct device *dev = acpi_desc->dev;
903 mask = (1 << (pcap->highest_capability + 1)) - 1;
904 acpi_desc->platform_cap = pcap->capabilities & mask;
905 dev_dbg(dev, "cap: %#x\n", acpi_desc->platform_cap);
909 static void *add_table(struct acpi_nfit_desc *acpi_desc,
910 struct nfit_table_prev *prev, void *table, const void *end)
912 struct device *dev = acpi_desc->dev;
913 struct acpi_nfit_header *hdr;
914 void *err = ERR_PTR(-ENOMEM);
921 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
927 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
928 if (!add_spa(acpi_desc, prev, table))
931 case ACPI_NFIT_TYPE_MEMORY_MAP:
932 if (!add_memdev(acpi_desc, prev, table))
935 case ACPI_NFIT_TYPE_CONTROL_REGION:
936 if (!add_dcr(acpi_desc, prev, table))
939 case ACPI_NFIT_TYPE_DATA_REGION:
940 if (!add_bdw(acpi_desc, prev, table))
943 case ACPI_NFIT_TYPE_INTERLEAVE:
944 if (!add_idt(acpi_desc, prev, table))
947 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
948 if (!add_flush(acpi_desc, prev, table))
951 case ACPI_NFIT_TYPE_SMBIOS:
952 dev_dbg(dev, "smbios\n");
954 case ACPI_NFIT_TYPE_CAPABILITIES:
955 if (!add_platform_cap(acpi_desc, table))
959 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
963 return table + hdr->length;
966 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
967 struct nfit_mem *nfit_mem)
969 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
970 u16 dcr = nfit_mem->dcr->region_index;
971 struct nfit_spa *nfit_spa;
973 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
974 u16 range_index = nfit_spa->spa->range_index;
975 int type = nfit_spa_type(nfit_spa->spa);
976 struct nfit_memdev *nfit_memdev;
978 if (type != NFIT_SPA_BDW)
981 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
982 if (nfit_memdev->memdev->range_index != range_index)
984 if (nfit_memdev->memdev->device_handle != device_handle)
986 if (nfit_memdev->memdev->region_index != dcr)
989 nfit_mem->spa_bdw = nfit_spa->spa;
994 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
995 nfit_mem->spa_dcr->range_index);
996 nfit_mem->bdw = NULL;
999 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
1000 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
1002 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
1003 struct nfit_memdev *nfit_memdev;
1004 struct nfit_bdw *nfit_bdw;
1005 struct nfit_idt *nfit_idt;
1006 u16 idt_idx, range_index;
1008 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
1009 if (nfit_bdw->bdw->region_index != dcr)
1011 nfit_mem->bdw = nfit_bdw->bdw;
1018 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
1020 if (!nfit_mem->spa_bdw)
1023 range_index = nfit_mem->spa_bdw->range_index;
1024 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1025 if (nfit_memdev->memdev->range_index != range_index ||
1026 nfit_memdev->memdev->region_index != dcr)
1028 nfit_mem->memdev_bdw = nfit_memdev->memdev;
1029 idt_idx = nfit_memdev->memdev->interleave_index;
1030 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1031 if (nfit_idt->idt->interleave_index != idt_idx)
1033 nfit_mem->idt_bdw = nfit_idt->idt;
1040 static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
1041 struct acpi_nfit_system_address *spa)
1043 struct nfit_mem *nfit_mem, *found;
1044 struct nfit_memdev *nfit_memdev;
1045 int type = spa ? nfit_spa_type(spa) : 0;
1057 * This loop runs in two modes, when a dimm is mapped the loop
1058 * adds memdev associations to an existing dimm, or creates a
1059 * dimm. In the unmapped dimm case this loop sweeps for memdev
1060 * instances with an invalid / zero range_index and adds those
1061 * dimms without spa associations.
1063 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1064 struct nfit_flush *nfit_flush;
1065 struct nfit_dcr *nfit_dcr;
1069 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
1071 if (!spa && nfit_memdev->memdev->range_index)
1074 dcr = nfit_memdev->memdev->region_index;
1075 device_handle = nfit_memdev->memdev->device_handle;
1076 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1077 if (__to_nfit_memdev(nfit_mem)->device_handle
1086 nfit_mem = devm_kzalloc(acpi_desc->dev,
1087 sizeof(*nfit_mem), GFP_KERNEL);
1090 INIT_LIST_HEAD(&nfit_mem->list);
1091 nfit_mem->acpi_desc = acpi_desc;
1092 list_add(&nfit_mem->list, &acpi_desc->dimms);
1095 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1096 if (nfit_dcr->dcr->region_index != dcr)
1099 * Record the control region for the dimm. For
1100 * the ACPI 6.1 case, where there are separate
1101 * control regions for the pmem vs blk
1102 * interfaces, be sure to record the extended
1106 nfit_mem->dcr = nfit_dcr->dcr;
1107 else if (nfit_mem->dcr->windows == 0
1108 && nfit_dcr->dcr->windows)
1109 nfit_mem->dcr = nfit_dcr->dcr;
1113 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
1114 struct acpi_nfit_flush_address *flush;
1117 if (nfit_flush->flush->device_handle != device_handle)
1119 nfit_mem->nfit_flush = nfit_flush;
1120 flush = nfit_flush->flush;
1121 nfit_mem->flush_wpq = devm_kcalloc(acpi_desc->dev,
1123 sizeof(struct resource),
1125 if (!nfit_mem->flush_wpq)
1127 for (i = 0; i < flush->hint_count; i++) {
1128 struct resource *res = &nfit_mem->flush_wpq[i];
1130 res->start = flush->hint_address[i];
1131 res->end = res->start + 8 - 1;
1136 if (dcr && !nfit_mem->dcr) {
1137 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
1138 spa->range_index, dcr);
1142 if (type == NFIT_SPA_DCR) {
1143 struct nfit_idt *nfit_idt;
1146 /* multiple dimms may share a SPA when interleaved */
1147 nfit_mem->spa_dcr = spa;
1148 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1149 idt_idx = nfit_memdev->memdev->interleave_index;
1150 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1151 if (nfit_idt->idt->interleave_index != idt_idx)
1153 nfit_mem->idt_dcr = nfit_idt->idt;
1156 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
1157 } else if (type == NFIT_SPA_PM) {
1159 * A single dimm may belong to multiple SPA-PM
1160 * ranges, record at least one in addition to
1161 * any SPA-DCR range.
1163 nfit_mem->memdev_pmem = nfit_memdev->memdev;
1165 nfit_mem->memdev_dcr = nfit_memdev->memdev;
1171 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
1173 struct nfit_mem *a = container_of(_a, typeof(*a), list);
1174 struct nfit_mem *b = container_of(_b, typeof(*b), list);
1175 u32 handleA, handleB;
1177 handleA = __to_nfit_memdev(a)->device_handle;
1178 handleB = __to_nfit_memdev(b)->device_handle;
1179 if (handleA < handleB)
1181 else if (handleA > handleB)
1186 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
1188 struct nfit_spa *nfit_spa;
1193 * For each SPA-DCR or SPA-PMEM address range find its
1194 * corresponding MEMDEV(s). From each MEMDEV find the
1195 * corresponding DCR. Then, if we're operating on a SPA-DCR,
1196 * try to find a SPA-BDW and a corresponding BDW that references
1197 * the DCR. Throw it all into an nfit_mem object. Note, that
1198 * BDWs are optional.
1200 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
1201 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
1207 * If a DIMM has failed to be mapped into SPA there will be no
1208 * SPA entries above. Find and register all the unmapped DIMMs
1209 * for reporting and recovery purposes.
1211 rc = __nfit_mem_init(acpi_desc, NULL);
1215 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
1220 static ssize_t bus_dsm_mask_show(struct device *dev,
1221 struct device_attribute *attr, char *buf)
1223 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1224 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1226 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
1228 static struct device_attribute dev_attr_bus_dsm_mask =
1229 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
1231 static ssize_t revision_show(struct device *dev,
1232 struct device_attribute *attr, char *buf)
1234 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1235 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1236 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1238 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
1240 static DEVICE_ATTR_RO(revision);
1242 static ssize_t hw_error_scrub_show(struct device *dev,
1243 struct device_attribute *attr, char *buf)
1245 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1246 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1247 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1249 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
1253 * The 'hw_error_scrub' attribute can have the following values written to it:
1254 * '0': Switch to the default mode where an exception will only insert
1255 * the address of the memory error into the poison and badblocks lists.
1256 * '1': Enable a full scrub to happen if an exception for a memory error is
1259 static ssize_t hw_error_scrub_store(struct device *dev,
1260 struct device_attribute *attr, const char *buf, size_t size)
1262 struct nvdimm_bus_descriptor *nd_desc;
1266 rc = kstrtol(buf, 0, &val);
1271 nd_desc = dev_get_drvdata(dev);
1273 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1276 case HW_ERROR_SCRUB_ON:
1277 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
1279 case HW_ERROR_SCRUB_OFF:
1280 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1292 static DEVICE_ATTR_RW(hw_error_scrub);
1295 * This shows the number of full Address Range Scrubs that have been
1296 * completed since driver load time. Userspace can wait on this using
1297 * select/poll etc. A '+' at the end indicates an ARS is in progress
1299 static ssize_t scrub_show(struct device *dev,
1300 struct device_attribute *attr, char *buf)
1302 struct nvdimm_bus_descriptor *nd_desc;
1303 ssize_t rc = -ENXIO;
1306 nd_desc = dev_get_drvdata(dev);
1308 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1310 mutex_lock(&acpi_desc->init_mutex);
1311 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1312 acpi_desc->scrub_busy
1313 && !acpi_desc->cancel ? "+\n" : "\n");
1314 mutex_unlock(&acpi_desc->init_mutex);
1320 static ssize_t scrub_store(struct device *dev,
1321 struct device_attribute *attr, const char *buf, size_t size)
1323 struct nvdimm_bus_descriptor *nd_desc;
1327 rc = kstrtol(buf, 0, &val);
1334 nd_desc = dev_get_drvdata(dev);
1336 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1338 rc = acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
1345 static DEVICE_ATTR_RW(scrub);
1347 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1349 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1350 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1351 | 1 << ND_CMD_ARS_STATUS;
1353 return (nd_desc->cmd_mask & mask) == mask;
1356 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1358 struct device *dev = container_of(kobj, struct device, kobj);
1359 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1361 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1366 static struct attribute *acpi_nfit_attributes[] = {
1367 &dev_attr_revision.attr,
1368 &dev_attr_scrub.attr,
1369 &dev_attr_hw_error_scrub.attr,
1370 &dev_attr_bus_dsm_mask.attr,
1374 static const struct attribute_group acpi_nfit_attribute_group = {
1376 .attrs = acpi_nfit_attributes,
1377 .is_visible = nfit_visible,
1380 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1381 &nvdimm_bus_attribute_group,
1382 &acpi_nfit_attribute_group,
1386 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1388 struct nvdimm *nvdimm = to_nvdimm(dev);
1389 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1391 return __to_nfit_memdev(nfit_mem);
1394 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1396 struct nvdimm *nvdimm = to_nvdimm(dev);
1397 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1399 return nfit_mem->dcr;
1402 static ssize_t handle_show(struct device *dev,
1403 struct device_attribute *attr, char *buf)
1405 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1407 return sprintf(buf, "%#x\n", memdev->device_handle);
1409 static DEVICE_ATTR_RO(handle);
1411 static ssize_t phys_id_show(struct device *dev,
1412 struct device_attribute *attr, char *buf)
1414 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1416 return sprintf(buf, "%#x\n", memdev->physical_id);
1418 static DEVICE_ATTR_RO(phys_id);
1420 static ssize_t vendor_show(struct device *dev,
1421 struct device_attribute *attr, char *buf)
1423 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1425 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1427 static DEVICE_ATTR_RO(vendor);
1429 static ssize_t rev_id_show(struct device *dev,
1430 struct device_attribute *attr, char *buf)
1432 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1434 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1436 static DEVICE_ATTR_RO(rev_id);
1438 static ssize_t device_show(struct device *dev,
1439 struct device_attribute *attr, char *buf)
1441 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1443 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1445 static DEVICE_ATTR_RO(device);
1447 static ssize_t subsystem_vendor_show(struct device *dev,
1448 struct device_attribute *attr, char *buf)
1450 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1452 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1454 static DEVICE_ATTR_RO(subsystem_vendor);
1456 static ssize_t subsystem_rev_id_show(struct device *dev,
1457 struct device_attribute *attr, char *buf)
1459 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1461 return sprintf(buf, "0x%04x\n",
1462 be16_to_cpu(dcr->subsystem_revision_id));
1464 static DEVICE_ATTR_RO(subsystem_rev_id);
1466 static ssize_t subsystem_device_show(struct device *dev,
1467 struct device_attribute *attr, char *buf)
1469 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1471 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1473 static DEVICE_ATTR_RO(subsystem_device);
1475 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1477 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1480 if (nfit_mem->memdev_pmem)
1482 if (nfit_mem->memdev_bdw)
1487 static ssize_t format_show(struct device *dev,
1488 struct device_attribute *attr, char *buf)
1490 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1492 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1494 static DEVICE_ATTR_RO(format);
1496 static ssize_t format1_show(struct device *dev,
1497 struct device_attribute *attr, char *buf)
1500 ssize_t rc = -ENXIO;
1501 struct nfit_mem *nfit_mem;
1502 struct nfit_memdev *nfit_memdev;
1503 struct acpi_nfit_desc *acpi_desc;
1504 struct nvdimm *nvdimm = to_nvdimm(dev);
1505 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1507 nfit_mem = nvdimm_provider_data(nvdimm);
1508 acpi_desc = nfit_mem->acpi_desc;
1509 handle = to_nfit_memdev(dev)->device_handle;
1511 /* assumes DIMMs have at most 2 published interface codes */
1512 mutex_lock(&acpi_desc->init_mutex);
1513 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1514 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1515 struct nfit_dcr *nfit_dcr;
1517 if (memdev->device_handle != handle)
1520 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1521 if (nfit_dcr->dcr->region_index != memdev->region_index)
1523 if (nfit_dcr->dcr->code == dcr->code)
1525 rc = sprintf(buf, "0x%04x\n",
1526 le16_to_cpu(nfit_dcr->dcr->code));
1532 mutex_unlock(&acpi_desc->init_mutex);
1535 static DEVICE_ATTR_RO(format1);
1537 static ssize_t formats_show(struct device *dev,
1538 struct device_attribute *attr, char *buf)
1540 struct nvdimm *nvdimm = to_nvdimm(dev);
1542 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1544 static DEVICE_ATTR_RO(formats);
1546 static ssize_t serial_show(struct device *dev,
1547 struct device_attribute *attr, char *buf)
1549 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1551 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1553 static DEVICE_ATTR_RO(serial);
1555 static ssize_t family_show(struct device *dev,
1556 struct device_attribute *attr, char *buf)
1558 struct nvdimm *nvdimm = to_nvdimm(dev);
1559 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1561 if (nfit_mem->family < 0)
1563 return sprintf(buf, "%d\n", nfit_mem->family);
1565 static DEVICE_ATTR_RO(family);
1567 static ssize_t dsm_mask_show(struct device *dev,
1568 struct device_attribute *attr, char *buf)
1570 struct nvdimm *nvdimm = to_nvdimm(dev);
1571 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1573 if (nfit_mem->family < 0)
1575 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1577 static DEVICE_ATTR_RO(dsm_mask);
1579 static ssize_t flags_show(struct device *dev,
1580 struct device_attribute *attr, char *buf)
1582 struct nvdimm *nvdimm = to_nvdimm(dev);
1583 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1584 u16 flags = __to_nfit_memdev(nfit_mem)->flags;
1586 if (test_bit(NFIT_MEM_DIRTY, &nfit_mem->flags))
1587 flags |= ACPI_NFIT_MEM_FLUSH_FAILED;
1589 return sprintf(buf, "%s%s%s%s%s%s%s\n",
1590 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1591 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1592 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1593 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1594 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1595 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1596 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1598 static DEVICE_ATTR_RO(flags);
1600 static ssize_t id_show(struct device *dev,
1601 struct device_attribute *attr, char *buf)
1603 struct nvdimm *nvdimm = to_nvdimm(dev);
1604 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1606 return sprintf(buf, "%s\n", nfit_mem->id);
1608 static DEVICE_ATTR_RO(id);
1610 static ssize_t dirty_shutdown_show(struct device *dev,
1611 struct device_attribute *attr, char *buf)
1613 struct nvdimm *nvdimm = to_nvdimm(dev);
1614 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1616 return sprintf(buf, "%d\n", nfit_mem->dirty_shutdown);
1618 static DEVICE_ATTR_RO(dirty_shutdown);
1620 static struct attribute *acpi_nfit_dimm_attributes[] = {
1621 &dev_attr_handle.attr,
1622 &dev_attr_phys_id.attr,
1623 &dev_attr_vendor.attr,
1624 &dev_attr_device.attr,
1625 &dev_attr_rev_id.attr,
1626 &dev_attr_subsystem_vendor.attr,
1627 &dev_attr_subsystem_device.attr,
1628 &dev_attr_subsystem_rev_id.attr,
1629 &dev_attr_format.attr,
1630 &dev_attr_formats.attr,
1631 &dev_attr_format1.attr,
1632 &dev_attr_serial.attr,
1633 &dev_attr_flags.attr,
1635 &dev_attr_family.attr,
1636 &dev_attr_dsm_mask.attr,
1637 &dev_attr_dirty_shutdown.attr,
1641 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1642 struct attribute *a, int n)
1644 struct device *dev = container_of(kobj, struct device, kobj);
1645 struct nvdimm *nvdimm = to_nvdimm(dev);
1646 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1648 if (!to_nfit_dcr(dev)) {
1649 /* Without a dcr only the memdev attributes can be surfaced */
1650 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1651 || a == &dev_attr_flags.attr
1652 || a == &dev_attr_family.attr
1653 || a == &dev_attr_dsm_mask.attr)
1658 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1661 if (!test_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags)
1662 && a == &dev_attr_dirty_shutdown.attr)
1668 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1670 .attrs = acpi_nfit_dimm_attributes,
1671 .is_visible = acpi_nfit_dimm_attr_visible,
1674 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1675 &nvdimm_attribute_group,
1676 &nd_device_attribute_group,
1677 &acpi_nfit_dimm_attribute_group,
1681 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1684 struct nfit_mem *nfit_mem;
1686 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1687 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1688 return nfit_mem->nvdimm;
1693 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1695 struct nfit_mem *nfit_mem;
1696 struct acpi_nfit_desc *acpi_desc;
1698 dev_dbg(dev->parent, "%s: event: %d\n", dev_name(dev),
1701 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1702 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1707 acpi_desc = dev_get_drvdata(dev->parent);
1712 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1715 nfit_mem = dev_get_drvdata(dev);
1716 if (nfit_mem && nfit_mem->flags_attr)
1717 sysfs_notify_dirent(nfit_mem->flags_attr);
1719 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1721 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1723 struct acpi_device *adev = data;
1724 struct device *dev = &adev->dev;
1726 device_lock(dev->parent);
1727 __acpi_nvdimm_notify(dev, event);
1728 device_unlock(dev->parent);
1731 static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method)
1736 status = acpi_get_handle(adev->handle, method, &handle);
1738 if (ACPI_SUCCESS(status))
1743 __weak void nfit_intel_shutdown_status(struct nfit_mem *nfit_mem)
1745 struct device *dev = &nfit_mem->adev->dev;
1746 struct nd_intel_smart smart = { 0 };
1747 union acpi_object in_buf = {
1748 .buffer.type = ACPI_TYPE_BUFFER,
1751 union acpi_object in_obj = {
1752 .package.type = ACPI_TYPE_PACKAGE,
1754 .package.elements = &in_buf,
1756 const u8 func = ND_INTEL_SMART;
1757 const guid_t *guid = to_nfit_uuid(nfit_mem->family);
1758 u8 revid = nfit_dsm_revid(nfit_mem->family, func);
1759 struct acpi_device *adev = nfit_mem->adev;
1760 acpi_handle handle = adev->handle;
1761 union acpi_object *out_obj;
1763 if ((nfit_mem->dsm_mask & (1 << func)) == 0)
1766 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
1767 if (!out_obj || out_obj->type != ACPI_TYPE_BUFFER
1768 || out_obj->buffer.length < sizeof(smart)) {
1769 dev_dbg(dev->parent, "%s: failed to retrieve initial health\n",
1774 memcpy(&smart, out_obj->buffer.pointer, sizeof(smart));
1777 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_VALID) {
1778 if (smart.shutdown_state)
1779 set_bit(NFIT_MEM_DIRTY, &nfit_mem->flags);
1782 if (smart.flags & ND_INTEL_SMART_SHUTDOWN_COUNT_VALID) {
1783 set_bit(NFIT_MEM_DIRTY_COUNT, &nfit_mem->flags);
1784 nfit_mem->dirty_shutdown = smart.shutdown_count;
1788 static void populate_shutdown_status(struct nfit_mem *nfit_mem)
1791 * For DIMMs that provide a dynamic facility to retrieve a
1792 * dirty-shutdown status and/or a dirty-shutdown count, cache
1793 * these values in nfit_mem.
1795 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1796 nfit_intel_shutdown_status(nfit_mem);
1799 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1800 struct nfit_mem *nfit_mem, u32 device_handle)
1802 struct acpi_device *adev, *adev_dimm;
1803 struct device *dev = acpi_desc->dev;
1804 unsigned long dsm_mask, label_mask;
1808 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1810 /* nfit test assumes 1:1 relationship between commands and dsms */
1811 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1812 nfit_mem->family = NVDIMM_FAMILY_INTEL;
1814 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1815 sprintf(nfit_mem->id, "%04x-%02x-%04x-%08x",
1816 be16_to_cpu(dcr->vendor_id),
1817 dcr->manufacturing_location,
1818 be16_to_cpu(dcr->manufacturing_date),
1819 be32_to_cpu(dcr->serial_number));
1821 sprintf(nfit_mem->id, "%04x-%08x",
1822 be16_to_cpu(dcr->vendor_id),
1823 be32_to_cpu(dcr->serial_number));
1825 adev = to_acpi_dev(acpi_desc);
1827 /* unit test case */
1828 populate_shutdown_status(nfit_mem);
1832 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1833 nfit_mem->adev = adev_dimm;
1835 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1837 return force_enable_dimms ? 0 : -ENODEV;
1840 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1841 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1842 dev_err(dev, "%s: notification registration failed\n",
1843 dev_name(&adev_dimm->dev));
1847 * Record nfit_mem for the notification path to track back to
1848 * the nfit sysfs attributes for this dimm device object.
1850 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1853 * There are 4 "legacy" NVDIMM command sets
1854 * (NVDIMM_FAMILY_{INTEL,MSFT,HPE1,HPE2}) that were created before
1855 * an EFI working group was established to constrain this
1856 * proliferation. The nfit driver probes for the supported command
1857 * set by GUID. Note, if you're a platform developer looking to add
1858 * a new command set to this probe, consider using an existing set,
1859 * or otherwise seek approval to publish the command set at
1860 * http://www.uefi.org/RFIC_LIST.
1862 * Note, that checking for function0 (bit0) tells us if any commands
1863 * are reachable through this GUID.
1865 for (i = 0; i <= NVDIMM_FAMILY_MAX; i++)
1866 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1867 if (family < 0 || i == default_dsm_family)
1870 /* limit the supported commands to those that are publicly documented */
1871 nfit_mem->family = family;
1872 if (override_dsm_mask && !disable_vendor_specific)
1873 dsm_mask = override_dsm_mask;
1874 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1875 dsm_mask = NVDIMM_INTEL_CMDMASK;
1876 if (disable_vendor_specific)
1877 dsm_mask &= ~(1 << ND_CMD_VENDOR);
1878 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1879 dsm_mask = 0x1c3c76;
1880 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1882 if (disable_vendor_specific)
1883 dsm_mask &= ~(1 << 8);
1884 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1885 dsm_mask = 0xffffffff;
1886 } else if (nfit_mem->family == NVDIMM_FAMILY_HYPERV) {
1889 dev_dbg(dev, "unknown dimm command family\n");
1890 nfit_mem->family = -1;
1891 /* DSMs are optional, continue loading the driver... */
1895 guid = to_nfit_uuid(nfit_mem->family);
1896 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1897 if (acpi_check_dsm(adev_dimm->handle, guid,
1898 nfit_dsm_revid(nfit_mem->family, i),
1900 set_bit(i, &nfit_mem->dsm_mask);
1903 * Prefer the NVDIMM_FAMILY_INTEL label read commands if present
1904 * due to their better semantics handling locked capacity.
1906 label_mask = 1 << ND_CMD_GET_CONFIG_SIZE | 1 << ND_CMD_GET_CONFIG_DATA
1907 | 1 << ND_CMD_SET_CONFIG_DATA;
1908 if (family == NVDIMM_FAMILY_INTEL
1909 && (dsm_mask & label_mask) == label_mask)
1910 /* skip _LS{I,R,W} enabling */;
1912 if (acpi_nvdimm_has_method(adev_dimm, "_LSI")
1913 && acpi_nvdimm_has_method(adev_dimm, "_LSR")) {
1914 dev_dbg(dev, "%s: has _LSR\n", dev_name(&adev_dimm->dev));
1915 set_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1918 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)
1919 && acpi_nvdimm_has_method(adev_dimm, "_LSW")) {
1920 dev_dbg(dev, "%s: has _LSW\n", dev_name(&adev_dimm->dev));
1921 set_bit(NFIT_MEM_LSW, &nfit_mem->flags);
1925 * Quirk read-only label configurations to preserve
1926 * access to label-less namespaces by default.
1928 if (!test_bit(NFIT_MEM_LSW, &nfit_mem->flags)
1930 dev_dbg(dev, "%s: No _LSW, disable labels\n",
1931 dev_name(&adev_dimm->dev));
1932 clear_bit(NFIT_MEM_LSR, &nfit_mem->flags);
1934 dev_dbg(dev, "%s: Force enable labels\n",
1935 dev_name(&adev_dimm->dev));
1938 populate_shutdown_status(nfit_mem);
1943 static void shutdown_dimm_notify(void *data)
1945 struct acpi_nfit_desc *acpi_desc = data;
1946 struct nfit_mem *nfit_mem;
1948 mutex_lock(&acpi_desc->init_mutex);
1950 * Clear out the nfit_mem->flags_attr and shut down dimm event
1953 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1954 struct acpi_device *adev_dimm = nfit_mem->adev;
1956 if (nfit_mem->flags_attr) {
1957 sysfs_put(nfit_mem->flags_attr);
1958 nfit_mem->flags_attr = NULL;
1961 acpi_remove_notify_handler(adev_dimm->handle,
1962 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1963 dev_set_drvdata(&adev_dimm->dev, NULL);
1966 mutex_unlock(&acpi_desc->init_mutex);
1969 static const struct nvdimm_security_ops *acpi_nfit_get_security_ops(int family)
1972 case NVDIMM_FAMILY_INTEL:
1973 return intel_security_ops;
1979 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1981 struct nfit_mem *nfit_mem;
1982 int dimm_count = 0, rc;
1983 struct nvdimm *nvdimm;
1985 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1986 struct acpi_nfit_flush_address *flush;
1987 unsigned long flags = 0, cmd_mask;
1988 struct nfit_memdev *nfit_memdev;
1992 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1993 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1999 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
2000 set_bit(NDD_ALIASING, &flags);
2002 /* collate flags across all memdevs for this dimm */
2003 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2004 struct acpi_nfit_memory_map *dimm_memdev;
2006 dimm_memdev = __to_nfit_memdev(nfit_mem);
2007 if (dimm_memdev->device_handle
2008 != nfit_memdev->memdev->device_handle)
2010 dimm_memdev->flags |= nfit_memdev->memdev->flags;
2013 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
2014 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
2015 set_bit(NDD_UNARMED, &flags);
2017 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
2022 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
2023 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
2024 * userspace interface.
2026 cmd_mask = 1UL << ND_CMD_CALL;
2027 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
2029 * These commands have a 1:1 correspondence
2030 * between DSM payload and libnvdimm ioctl
2033 cmd_mask |= nfit_mem->dsm_mask & NVDIMM_STANDARD_CMDMASK;
2036 /* Quirk to ignore LOCAL for labels on HYPERV DIMMs */
2037 if (nfit_mem->family == NVDIMM_FAMILY_HYPERV)
2038 set_bit(NDD_NOBLK, &flags);
2040 if (test_bit(NFIT_MEM_LSR, &nfit_mem->flags)) {
2041 set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
2042 set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
2044 if (test_bit(NFIT_MEM_LSW, &nfit_mem->flags))
2045 set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
2047 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
2049 nvdimm = __nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
2050 acpi_nfit_dimm_attribute_groups,
2051 flags, cmd_mask, flush ? flush->hint_count : 0,
2052 nfit_mem->flush_wpq, &nfit_mem->id[0],
2053 acpi_nfit_get_security_ops(nfit_mem->family));
2057 nfit_mem->nvdimm = nvdimm;
2060 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
2063 dev_err(acpi_desc->dev, "Error found in NVDIMM %s flags:%s%s%s%s%s\n",
2064 nvdimm_name(nvdimm),
2065 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
2066 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
2067 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
2068 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
2069 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
2073 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
2078 * Now that dimms are successfully registered, and async registration
2079 * is flushed, attempt to enable event notification.
2081 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
2082 struct kernfs_node *nfit_kernfs;
2084 nvdimm = nfit_mem->nvdimm;
2088 rc = nvdimm_security_setup_events(nvdimm);
2090 dev_warn(acpi_desc->dev,
2091 "security event setup failed: %d\n", rc);
2093 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
2095 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
2097 sysfs_put(nfit_kernfs);
2098 if (!nfit_mem->flags_attr)
2099 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
2100 nvdimm_name(nvdimm));
2103 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
2108 * These constants are private because there are no kernel consumers of
2111 enum nfit_aux_cmds {
2112 NFIT_CMD_TRANSLATE_SPA = 5,
2113 NFIT_CMD_ARS_INJECT_SET = 7,
2114 NFIT_CMD_ARS_INJECT_CLEAR = 8,
2115 NFIT_CMD_ARS_INJECT_GET = 9,
2118 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
2120 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2121 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
2122 struct acpi_device *adev;
2123 unsigned long dsm_mask;
2126 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
2127 nd_desc->bus_dsm_mask = acpi_desc->bus_nfit_cmd_force_en;
2128 adev = to_acpi_dev(acpi_desc);
2132 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
2133 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2134 set_bit(i, &nd_desc->cmd_mask);
2135 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
2138 (1 << ND_CMD_ARS_CAP) |
2139 (1 << ND_CMD_ARS_START) |
2140 (1 << ND_CMD_ARS_STATUS) |
2141 (1 << ND_CMD_CLEAR_ERROR) |
2142 (1 << NFIT_CMD_TRANSLATE_SPA) |
2143 (1 << NFIT_CMD_ARS_INJECT_SET) |
2144 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
2145 (1 << NFIT_CMD_ARS_INJECT_GET);
2146 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
2147 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
2148 set_bit(i, &nd_desc->bus_dsm_mask);
2151 static ssize_t range_index_show(struct device *dev,
2152 struct device_attribute *attr, char *buf)
2154 struct nd_region *nd_region = to_nd_region(dev);
2155 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
2157 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
2159 static DEVICE_ATTR_RO(range_index);
2161 static struct attribute *acpi_nfit_region_attributes[] = {
2162 &dev_attr_range_index.attr,
2166 static const struct attribute_group acpi_nfit_region_attribute_group = {
2168 .attrs = acpi_nfit_region_attributes,
2171 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
2172 &nd_region_attribute_group,
2173 &nd_mapping_attribute_group,
2174 &nd_device_attribute_group,
2175 &nd_numa_attribute_group,
2176 &acpi_nfit_region_attribute_group,
2180 /* enough info to uniquely specify an interleave set */
2181 struct nfit_set_info {
2182 struct nfit_set_info_map {
2189 struct nfit_set_info2 {
2190 struct nfit_set_info_map2 {
2194 u16 manufacturing_date;
2195 u8 manufacturing_location;
2200 static size_t sizeof_nfit_set_info(int num_mappings)
2202 return sizeof(struct nfit_set_info)
2203 + num_mappings * sizeof(struct nfit_set_info_map);
2206 static size_t sizeof_nfit_set_info2(int num_mappings)
2208 return sizeof(struct nfit_set_info2)
2209 + num_mappings * sizeof(struct nfit_set_info_map2);
2212 static int cmp_map_compat(const void *m0, const void *m1)
2214 const struct nfit_set_info_map *map0 = m0;
2215 const struct nfit_set_info_map *map1 = m1;
2217 return memcmp(&map0->region_offset, &map1->region_offset,
2221 static int cmp_map(const void *m0, const void *m1)
2223 const struct nfit_set_info_map *map0 = m0;
2224 const struct nfit_set_info_map *map1 = m1;
2226 if (map0->region_offset < map1->region_offset)
2228 else if (map0->region_offset > map1->region_offset)
2233 static int cmp_map2(const void *m0, const void *m1)
2235 const struct nfit_set_info_map2 *map0 = m0;
2236 const struct nfit_set_info_map2 *map1 = m1;
2238 if (map0->region_offset < map1->region_offset)
2240 else if (map0->region_offset > map1->region_offset)
2245 /* Retrieve the nth entry referencing this spa */
2246 static struct acpi_nfit_memory_map *memdev_from_spa(
2247 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
2249 struct nfit_memdev *nfit_memdev;
2251 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
2252 if (nfit_memdev->memdev->range_index == range_index)
2254 return nfit_memdev->memdev;
2258 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
2259 struct nd_region_desc *ndr_desc,
2260 struct acpi_nfit_system_address *spa)
2262 struct device *dev = acpi_desc->dev;
2263 struct nd_interleave_set *nd_set;
2264 u16 nr = ndr_desc->num_mappings;
2265 struct nfit_set_info2 *info2;
2266 struct nfit_set_info *info;
2269 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
2272 ndr_desc->nd_set = nd_set;
2273 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
2275 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
2279 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
2283 for (i = 0; i < nr; i++) {
2284 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
2285 struct nfit_set_info_map *map = &info->mapping[i];
2286 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2287 struct nvdimm *nvdimm = mapping->nvdimm;
2288 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2289 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
2290 spa->range_index, i);
2291 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2293 if (!memdev || !nfit_mem->dcr) {
2294 dev_err(dev, "%s: failed to find DCR\n", __func__);
2298 map->region_offset = memdev->region_offset;
2299 map->serial_number = dcr->serial_number;
2301 map2->region_offset = memdev->region_offset;
2302 map2->serial_number = dcr->serial_number;
2303 map2->vendor_id = dcr->vendor_id;
2304 map2->manufacturing_date = dcr->manufacturing_date;
2305 map2->manufacturing_location = dcr->manufacturing_location;
2308 /* v1.1 namespaces */
2309 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2311 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2313 /* v1.2 namespaces */
2314 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
2316 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
2318 /* support v1.1 namespaces created with the wrong sort order */
2319 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2320 cmp_map_compat, NULL);
2321 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2323 /* record the result of the sort for the mapping position */
2324 for (i = 0; i < nr; i++) {
2325 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2328 for (j = 0; j < nr; j++) {
2329 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
2330 struct nvdimm *nvdimm = mapping->nvdimm;
2331 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2332 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
2334 if (map2->serial_number == dcr->serial_number &&
2335 map2->vendor_id == dcr->vendor_id &&
2336 map2->manufacturing_date == dcr->manufacturing_date &&
2337 map2->manufacturing_location
2338 == dcr->manufacturing_location) {
2339 mapping->position = i;
2345 ndr_desc->nd_set = nd_set;
2346 devm_kfree(dev, info);
2347 devm_kfree(dev, info2);
2352 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
2354 struct acpi_nfit_interleave *idt = mmio->idt;
2355 u32 sub_line_offset, line_index, line_offset;
2356 u64 line_no, table_skip_count, table_offset;
2358 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
2359 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
2360 line_offset = idt->line_offset[line_index]
2362 table_offset = table_skip_count * mmio->table_size;
2364 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
2367 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
2369 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2370 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
2371 const u32 STATUS_MASK = 0x80000037;
2373 if (mmio->num_lines)
2374 offset = to_interleave_offset(offset, mmio);
2376 return readl(mmio->addr.base + offset) & STATUS_MASK;
2379 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
2380 resource_size_t dpa, unsigned int len, unsigned int write)
2383 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2386 BCW_OFFSET_MASK = (1ULL << 48)-1,
2388 BCW_LEN_MASK = (1ULL << 8) - 1,
2392 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
2393 len = len >> L1_CACHE_SHIFT;
2394 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
2395 cmd |= ((u64) write) << BCW_CMD_SHIFT;
2397 offset = nfit_blk->cmd_offset + mmio->size * bw;
2398 if (mmio->num_lines)
2399 offset = to_interleave_offset(offset, mmio);
2401 writeq(cmd, mmio->addr.base + offset);
2402 nvdimm_flush(nfit_blk->nd_region);
2404 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
2405 readq(mmio->addr.base + offset);
2408 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
2409 resource_size_t dpa, void *iobuf, size_t len, int rw,
2412 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2413 unsigned int copied = 0;
2417 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
2418 + lane * mmio->size;
2419 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2424 if (mmio->num_lines) {
2427 offset = to_interleave_offset(base_offset + copied,
2429 div_u64_rem(offset, mmio->line_size, &line_offset);
2430 c = min_t(size_t, len, mmio->line_size - line_offset);
2432 offset = base_offset + nfit_blk->bdw_offset;
2437 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
2439 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
2440 arch_invalidate_pmem((void __force *)
2441 mmio->addr.aperture + offset, c);
2443 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
2451 nvdimm_flush(nfit_blk->nd_region);
2453 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2457 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2458 resource_size_t dpa, void *iobuf, u64 len, int rw)
2460 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2461 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2462 struct nd_region *nd_region = nfit_blk->nd_region;
2463 unsigned int lane, copied = 0;
2466 lane = nd_region_acquire_lane(nd_region);
2468 u64 c = min(len, mmio->size);
2470 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2471 iobuf + copied, c, rw, lane);
2478 nd_region_release_lane(nd_region, lane);
2483 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2484 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2487 mmio->num_lines = idt->line_count;
2488 mmio->line_size = idt->line_size;
2489 if (interleave_ways == 0)
2491 mmio->table_size = mmio->num_lines * interleave_ways
2498 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2499 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2501 struct nd_cmd_dimm_flags flags;
2504 memset(&flags, 0, sizeof(flags));
2505 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
2506 sizeof(flags), NULL);
2508 if (rc >= 0 && flags.status == 0)
2509 nfit_blk->dimm_flags = flags.flags;
2510 else if (rc == -ENOTTY) {
2511 /* fall back to a conservative default */
2512 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2520 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2523 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2524 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2525 struct nfit_blk_mmio *mmio;
2526 struct nfit_blk *nfit_blk;
2527 struct nfit_mem *nfit_mem;
2528 struct nvdimm *nvdimm;
2531 nvdimm = nd_blk_region_to_dimm(ndbr);
2532 nfit_mem = nvdimm_provider_data(nvdimm);
2533 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2534 dev_dbg(dev, "missing%s%s%s\n",
2535 nfit_mem ? "" : " nfit_mem",
2536 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2537 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2541 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2544 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2545 nfit_blk->nd_region = to_nd_region(dev);
2547 /* map block aperture memory */
2548 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2549 mmio = &nfit_blk->mmio[BDW];
2550 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2551 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2552 if (!mmio->addr.base) {
2553 dev_dbg(dev, "%s failed to map bdw\n",
2554 nvdimm_name(nvdimm));
2557 mmio->size = nfit_mem->bdw->size;
2558 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2559 mmio->idt = nfit_mem->idt_bdw;
2560 mmio->spa = nfit_mem->spa_bdw;
2561 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2562 nfit_mem->memdev_bdw->interleave_ways);
2564 dev_dbg(dev, "%s failed to init bdw interleave\n",
2565 nvdimm_name(nvdimm));
2569 /* map block control memory */
2570 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2571 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2572 mmio = &nfit_blk->mmio[DCR];
2573 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2574 nfit_mem->spa_dcr->length);
2575 if (!mmio->addr.base) {
2576 dev_dbg(dev, "%s failed to map dcr\n",
2577 nvdimm_name(nvdimm));
2580 mmio->size = nfit_mem->dcr->window_size;
2581 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2582 mmio->idt = nfit_mem->idt_dcr;
2583 mmio->spa = nfit_mem->spa_dcr;
2584 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2585 nfit_mem->memdev_dcr->interleave_ways);
2587 dev_dbg(dev, "%s failed to init dcr interleave\n",
2588 nvdimm_name(nvdimm));
2592 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2594 dev_dbg(dev, "%s failed get DIMM flags\n",
2595 nvdimm_name(nvdimm));
2599 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2600 dev_warn(dev, "unable to guarantee persistence of writes\n");
2602 if (mmio->line_size == 0)
2605 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2606 + 8 > mmio->line_size) {
2607 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2609 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2610 + 8 > mmio->line_size) {
2611 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2618 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2619 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2621 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2622 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2625 cmd->address = spa->address;
2626 cmd->length = spa->length;
2627 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2628 sizeof(*cmd), &cmd_rc);
2634 static int ars_start(struct acpi_nfit_desc *acpi_desc,
2635 struct nfit_spa *nfit_spa, enum nfit_ars_state req_type)
2639 struct nd_cmd_ars_start ars_start;
2640 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2641 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2643 memset(&ars_start, 0, sizeof(ars_start));
2644 ars_start.address = spa->address;
2645 ars_start.length = spa->length;
2646 if (req_type == ARS_REQ_SHORT)
2647 ars_start.flags = ND_ARS_RETURN_PREV_DATA;
2648 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2649 ars_start.type = ND_ARS_PERSISTENT;
2650 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2651 ars_start.type = ND_ARS_VOLATILE;
2655 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2656 sizeof(ars_start), &cmd_rc);
2663 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2666 struct nd_cmd_ars_start ars_start;
2667 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2668 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2670 memset(&ars_start, 0, sizeof(ars_start));
2671 ars_start.address = ars_status->restart_address;
2672 ars_start.length = ars_status->restart_length;
2673 ars_start.type = ars_status->type;
2674 ars_start.flags = acpi_desc->ars_start_flags;
2675 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2676 sizeof(ars_start), &cmd_rc);
2682 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2684 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2685 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2688 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2689 acpi_desc->max_ars, &cmd_rc);
2695 static void ars_complete(struct acpi_nfit_desc *acpi_desc,
2696 struct nfit_spa *nfit_spa)
2698 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2699 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2700 struct nd_region *nd_region = nfit_spa->nd_region;
2703 lockdep_assert_held(&acpi_desc->init_mutex);
2705 * Only advance the ARS state for ARS runs initiated by the
2706 * kernel, ignore ARS results from BIOS initiated runs for scrub
2707 * completion tracking.
2709 if (acpi_desc->scrub_spa != nfit_spa)
2712 if ((ars_status->address >= spa->address && ars_status->address
2713 < spa->address + spa->length)
2714 || (ars_status->address < spa->address)) {
2716 * Assume that if a scrub starts at an offset from the
2717 * start of nfit_spa that we are in the continuation
2720 * Otherwise, if the scrub covers the spa range, mark
2721 * any pending request complete.
2723 if (ars_status->address + ars_status->length
2724 >= spa->address + spa->length)
2731 acpi_desc->scrub_spa = NULL;
2733 dev = nd_region_dev(nd_region);
2734 nvdimm_region_notify(nd_region, NVDIMM_REVALIDATE_POISON);
2736 dev = acpi_desc->dev;
2737 dev_dbg(dev, "ARS: range %d complete\n", spa->range_index);
2740 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc)
2742 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2743 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2748 * First record starts at 44 byte offset from the start of the
2751 if (ars_status->out_length < 44)
2753 for (i = 0; i < ars_status->num_records; i++) {
2754 /* only process full records */
2755 if (ars_status->out_length
2756 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2758 rc = nvdimm_bus_add_badrange(nvdimm_bus,
2759 ars_status->records[i].err_address,
2760 ars_status->records[i].length);
2764 if (i < ars_status->num_records)
2765 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2770 static void acpi_nfit_remove_resource(void *data)
2772 struct resource *res = data;
2774 remove_resource(res);
2777 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2778 struct nd_region_desc *ndr_desc)
2780 struct resource *res, *nd_res = ndr_desc->res;
2783 /* No operation if the region is already registered as PMEM */
2784 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2785 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2786 if (is_pmem == REGION_INTERSECTS)
2789 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2793 res->name = "Persistent Memory";
2794 res->start = nd_res->start;
2795 res->end = nd_res->end;
2796 res->flags = IORESOURCE_MEM;
2797 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2799 ret = insert_resource(&iomem_resource, res);
2803 ret = devm_add_action_or_reset(acpi_desc->dev,
2804 acpi_nfit_remove_resource,
2812 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2813 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2814 struct acpi_nfit_memory_map *memdev,
2815 struct nfit_spa *nfit_spa)
2817 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2818 memdev->device_handle);
2819 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2820 struct nd_blk_region_desc *ndbr_desc;
2821 struct nfit_mem *nfit_mem;
2825 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2826 spa->range_index, memdev->device_handle);
2830 mapping->nvdimm = nvdimm;
2831 switch (nfit_spa_type(spa)) {
2833 case NFIT_SPA_VOLATILE:
2834 mapping->start = memdev->address;
2835 mapping->size = memdev->region_size;
2838 nfit_mem = nvdimm_provider_data(nvdimm);
2839 if (!nfit_mem || !nfit_mem->bdw) {
2840 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2841 spa->range_index, nvdimm_name(nvdimm));
2845 mapping->size = nfit_mem->bdw->capacity;
2846 mapping->start = nfit_mem->bdw->start_address;
2847 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2848 ndr_desc->mapping = mapping;
2849 ndr_desc->num_mappings = 1;
2850 ndbr_desc = to_blk_region_desc(ndr_desc);
2851 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2852 ndbr_desc->do_io = acpi_desc->blk_do_io;
2853 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2856 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2858 if (!nfit_spa->nd_region)
2866 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2868 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2869 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2870 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2871 nfit_spa_type(spa) == NFIT_SPA_PCD);
2874 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2876 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2877 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2878 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2881 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2882 struct nfit_spa *nfit_spa)
2884 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2885 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2886 struct nd_blk_region_desc ndbr_desc;
2887 struct nd_region_desc *ndr_desc;
2888 struct nfit_memdev *nfit_memdev;
2889 struct nvdimm_bus *nvdimm_bus;
2890 struct resource res;
2893 if (nfit_spa->nd_region)
2896 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2897 dev_dbg(acpi_desc->dev, "detected invalid spa index\n");
2901 memset(&res, 0, sizeof(res));
2902 memset(&mappings, 0, sizeof(mappings));
2903 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2904 res.start = spa->address;
2905 res.end = res.start + spa->length - 1;
2906 ndr_desc = &ndbr_desc.ndr_desc;
2907 ndr_desc->res = &res;
2908 ndr_desc->provider_data = nfit_spa;
2909 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2910 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2911 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2912 spa->proximity_domain);
2914 ndr_desc->numa_node = NUMA_NO_NODE;
2917 * Persistence domain bits are hierarchical, if
2918 * ACPI_NFIT_CAPABILITY_CACHE_FLUSH is set then
2919 * ACPI_NFIT_CAPABILITY_MEM_FLUSH is implied.
2921 if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_CACHE_FLUSH)
2922 set_bit(ND_REGION_PERSIST_CACHE, &ndr_desc->flags);
2923 else if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_MEM_FLUSH)
2924 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc->flags);
2926 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2927 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2928 struct nd_mapping_desc *mapping;
2930 if (memdev->range_index != spa->range_index)
2932 if (count >= ND_MAX_MAPPINGS) {
2933 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2934 spa->range_index, ND_MAX_MAPPINGS);
2937 mapping = &mappings[count++];
2938 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2944 ndr_desc->mapping = mappings;
2945 ndr_desc->num_mappings = count;
2946 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2950 nvdimm_bus = acpi_desc->nvdimm_bus;
2951 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2952 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2954 dev_warn(acpi_desc->dev,
2955 "failed to insert pmem resource to iomem: %d\n",
2960 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2962 if (!nfit_spa->nd_region)
2964 } else if (nfit_spa_is_volatile(spa)) {
2965 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2967 if (!nfit_spa->nd_region)
2969 } else if (nfit_spa_is_virtual(spa)) {
2970 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2972 if (!nfit_spa->nd_region)
2978 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2979 nfit_spa->spa->range_index);
2983 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc)
2985 struct device *dev = acpi_desc->dev;
2986 struct nd_cmd_ars_status *ars_status;
2988 if (acpi_desc->ars_status) {
2989 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
2993 ars_status = devm_kzalloc(dev, acpi_desc->max_ars, GFP_KERNEL);
2996 acpi_desc->ars_status = ars_status;
3000 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc)
3004 if (ars_status_alloc(acpi_desc))
3007 rc = ars_get_status(acpi_desc);
3009 if (rc < 0 && rc != -ENOSPC)
3012 if (ars_status_process_records(acpi_desc))
3013 dev_err(acpi_desc->dev, "Failed to process ARS records\n");
3018 static int ars_register(struct acpi_nfit_desc *acpi_desc,
3019 struct nfit_spa *nfit_spa)
3023 if (no_init_ars || test_bit(ARS_FAILED, &nfit_spa->ars_state))
3024 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3026 set_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3027 set_bit(ARS_REQ_LONG, &nfit_spa->ars_state);
3029 switch (acpi_nfit_query_poison(acpi_desc)) {
3032 rc = ars_start(acpi_desc, nfit_spa, ARS_REQ_SHORT);
3033 /* shouldn't happen, try again later */
3037 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3040 clear_bit(ARS_REQ_SHORT, &nfit_spa->ars_state);
3041 rc = acpi_nfit_query_poison(acpi_desc);
3044 acpi_desc->scrub_spa = nfit_spa;
3045 ars_complete(acpi_desc, nfit_spa);
3047 * If ars_complete() says we didn't complete the
3048 * short scrub, we'll try again with a long
3051 acpi_desc->scrub_spa = NULL;
3057 * BIOS was using ARS, wait for it to complete (or
3058 * resources to become available) and then perform our
3063 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3067 return acpi_nfit_register_region(acpi_desc, nfit_spa);
3070 static void ars_complete_all(struct acpi_nfit_desc *acpi_desc)
3072 struct nfit_spa *nfit_spa;
3074 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3075 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3077 ars_complete(acpi_desc, nfit_spa);
3081 static unsigned int __acpi_nfit_scrub(struct acpi_nfit_desc *acpi_desc,
3084 unsigned int tmo = acpi_desc->scrub_tmo;
3085 struct device *dev = acpi_desc->dev;
3086 struct nfit_spa *nfit_spa;
3088 lockdep_assert_held(&acpi_desc->init_mutex);
3090 if (acpi_desc->cancel)
3093 if (query_rc == -EBUSY) {
3094 dev_dbg(dev, "ARS: ARS busy\n");
3095 return min(30U * 60U, tmo * 2);
3097 if (query_rc == -ENOSPC) {
3098 dev_dbg(dev, "ARS: ARS continue\n");
3099 ars_continue(acpi_desc);
3102 if (query_rc && query_rc != -EAGAIN) {
3103 unsigned long long addr, end;
3105 addr = acpi_desc->ars_status->address;
3106 end = addr + acpi_desc->ars_status->length;
3107 dev_dbg(dev, "ARS: %llx-%llx failed (%d)\n", addr, end,
3111 ars_complete_all(acpi_desc);
3112 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3113 enum nfit_ars_state req_type;
3116 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3119 /* prefer short ARS requests first */
3120 if (test_bit(ARS_REQ_SHORT, &nfit_spa->ars_state))
3121 req_type = ARS_REQ_SHORT;
3122 else if (test_bit(ARS_REQ_LONG, &nfit_spa->ars_state))
3123 req_type = ARS_REQ_LONG;
3126 rc = ars_start(acpi_desc, nfit_spa, req_type);
3128 dev = nd_region_dev(nfit_spa->nd_region);
3129 dev_dbg(dev, "ARS: range %d ARS start %s (%d)\n",
3130 nfit_spa->spa->range_index,
3131 req_type == ARS_REQ_SHORT ? "short" : "long",
3134 * Hmm, we raced someone else starting ARS? Try again in
3140 dev_WARN_ONCE(dev, acpi_desc->scrub_spa,
3141 "scrub start while range %d active\n",
3142 acpi_desc->scrub_spa->spa->range_index);
3143 clear_bit(req_type, &nfit_spa->ars_state);
3144 acpi_desc->scrub_spa = nfit_spa;
3146 * Consider this spa last for future scrub
3149 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
3153 dev_err(dev, "ARS: range %d ARS failed (%d)\n",
3154 nfit_spa->spa->range_index, rc);
3155 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3160 static void __sched_ars(struct acpi_nfit_desc *acpi_desc, unsigned int tmo)
3162 lockdep_assert_held(&acpi_desc->init_mutex);
3164 acpi_desc->scrub_busy = 1;
3165 /* note this should only be set from within the workqueue */
3167 acpi_desc->scrub_tmo = tmo;
3168 queue_delayed_work(nfit_wq, &acpi_desc->dwork, tmo * HZ);
3171 static void sched_ars(struct acpi_nfit_desc *acpi_desc)
3173 __sched_ars(acpi_desc, 0);
3176 static void notify_ars_done(struct acpi_nfit_desc *acpi_desc)
3178 lockdep_assert_held(&acpi_desc->init_mutex);
3180 acpi_desc->scrub_busy = 0;
3181 acpi_desc->scrub_count++;
3182 if (acpi_desc->scrub_count_state)
3183 sysfs_notify_dirent(acpi_desc->scrub_count_state);
3186 static void acpi_nfit_scrub(struct work_struct *work)
3188 struct acpi_nfit_desc *acpi_desc;
3192 acpi_desc = container_of(work, typeof(*acpi_desc), dwork.work);
3193 mutex_lock(&acpi_desc->init_mutex);
3194 query_rc = acpi_nfit_query_poison(acpi_desc);
3195 tmo = __acpi_nfit_scrub(acpi_desc, query_rc);
3197 __sched_ars(acpi_desc, tmo);
3199 notify_ars_done(acpi_desc);
3200 memset(acpi_desc->ars_status, 0, acpi_desc->max_ars);
3201 mutex_unlock(&acpi_desc->init_mutex);
3204 static void acpi_nfit_init_ars(struct acpi_nfit_desc *acpi_desc,
3205 struct nfit_spa *nfit_spa)
3207 int type = nfit_spa_type(nfit_spa->spa);
3208 struct nd_cmd_ars_cap ars_cap;
3211 set_bit(ARS_FAILED, &nfit_spa->ars_state);
3212 memset(&ars_cap, 0, sizeof(ars_cap));
3213 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
3216 /* check that the supported scrub types match the spa type */
3217 if (type == NFIT_SPA_VOLATILE && ((ars_cap.status >> 16)
3218 & ND_ARS_VOLATILE) == 0)
3220 if (type == NFIT_SPA_PM && ((ars_cap.status >> 16)
3221 & ND_ARS_PERSISTENT) == 0)
3224 nfit_spa->max_ars = ars_cap.max_ars_out;
3225 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
3226 acpi_desc->max_ars = max(nfit_spa->max_ars, acpi_desc->max_ars);
3227 clear_bit(ARS_FAILED, &nfit_spa->ars_state);
3230 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
3232 struct nfit_spa *nfit_spa;
3235 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3236 switch (nfit_spa_type(nfit_spa->spa)) {
3237 case NFIT_SPA_VOLATILE:
3239 acpi_nfit_init_ars(acpi_desc, nfit_spa);
3244 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
3245 switch (nfit_spa_type(nfit_spa->spa)) {
3246 case NFIT_SPA_VOLATILE:
3248 /* register regions and kick off initial ARS run */
3249 rc = ars_register(acpi_desc, nfit_spa);
3254 /* nothing to register */
3257 case NFIT_SPA_VDISK:
3259 case NFIT_SPA_PDISK:
3261 /* register known regions that don't support ARS */
3262 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
3267 /* don't register unknown regions */
3271 sched_ars(acpi_desc);
3275 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
3276 struct nfit_table_prev *prev)
3278 struct device *dev = acpi_desc->dev;
3280 if (!list_empty(&prev->spas) ||
3281 !list_empty(&prev->memdevs) ||
3282 !list_empty(&prev->dcrs) ||
3283 !list_empty(&prev->bdws) ||
3284 !list_empty(&prev->idts) ||
3285 !list_empty(&prev->flushes)) {
3286 dev_err(dev, "new nfit deletes entries (unsupported)\n");
3292 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
3294 struct device *dev = acpi_desc->dev;
3295 struct kernfs_node *nfit;
3296 struct device *bus_dev;
3298 if (!ars_supported(acpi_desc->nvdimm_bus))
3301 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3302 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
3304 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
3307 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
3309 if (!acpi_desc->scrub_count_state) {
3310 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
3317 static void acpi_nfit_unregister(void *data)
3319 struct acpi_nfit_desc *acpi_desc = data;
3321 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
3324 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
3326 struct device *dev = acpi_desc->dev;
3327 struct nfit_table_prev prev;
3331 if (!acpi_desc->nvdimm_bus) {
3332 acpi_nfit_init_dsms(acpi_desc);
3334 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
3335 &acpi_desc->nd_desc);
3336 if (!acpi_desc->nvdimm_bus)
3339 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
3344 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
3348 /* register this acpi_desc for mce notifications */
3349 mutex_lock(&acpi_desc_lock);
3350 list_add_tail(&acpi_desc->list, &acpi_descs);
3351 mutex_unlock(&acpi_desc_lock);
3354 mutex_lock(&acpi_desc->init_mutex);
3356 INIT_LIST_HEAD(&prev.spas);
3357 INIT_LIST_HEAD(&prev.memdevs);
3358 INIT_LIST_HEAD(&prev.dcrs);
3359 INIT_LIST_HEAD(&prev.bdws);
3360 INIT_LIST_HEAD(&prev.idts);
3361 INIT_LIST_HEAD(&prev.flushes);
3363 list_cut_position(&prev.spas, &acpi_desc->spas,
3364 acpi_desc->spas.prev);
3365 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
3366 acpi_desc->memdevs.prev);
3367 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
3368 acpi_desc->dcrs.prev);
3369 list_cut_position(&prev.bdws, &acpi_desc->bdws,
3370 acpi_desc->bdws.prev);
3371 list_cut_position(&prev.idts, &acpi_desc->idts,
3372 acpi_desc->idts.prev);
3373 list_cut_position(&prev.flushes, &acpi_desc->flushes,
3374 acpi_desc->flushes.prev);
3377 while (!IS_ERR_OR_NULL(data))
3378 data = add_table(acpi_desc, &prev, data, end);
3381 dev_dbg(dev, "nfit table parsing error: %ld\n", PTR_ERR(data));
3386 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
3390 rc = nfit_mem_init(acpi_desc);
3394 rc = acpi_nfit_register_dimms(acpi_desc);
3398 rc = acpi_nfit_register_regions(acpi_desc);
3401 mutex_unlock(&acpi_desc->init_mutex);
3404 EXPORT_SYMBOL_GPL(acpi_nfit_init);
3406 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
3408 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
3409 struct device *dev = acpi_desc->dev;
3411 /* Bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
3415 /* Bounce the init_mutex to complete initial registration */
3416 mutex_lock(&acpi_desc->init_mutex);
3417 mutex_unlock(&acpi_desc->init_mutex);
3422 static int __acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3423 struct nvdimm *nvdimm, unsigned int cmd)
3425 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
3429 if (cmd != ND_CMD_ARS_START)
3433 * The kernel and userspace may race to initiate a scrub, but
3434 * the scrub thread is prepared to lose that initial race. It
3435 * just needs guarantees that any ARS it initiates are not
3436 * interrupted by any intervening start requests from userspace.
3438 if (work_busy(&acpi_desc->dwork.work))
3444 /* prevent security commands from being issued via ioctl */
3445 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3446 struct nvdimm *nvdimm, unsigned int cmd, void *buf)
3448 struct nd_cmd_pkg *call_pkg = buf;
3451 if (nvdimm && cmd == ND_CMD_CALL &&
3452 call_pkg->nd_family == NVDIMM_FAMILY_INTEL) {
3453 func = call_pkg->nd_command;
3454 if ((1 << func) & NVDIMM_INTEL_SECURITY_CMDMASK)
3458 return __acpi_nfit_clear_to_send(nd_desc, nvdimm, cmd);
3461 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc,
3462 enum nfit_ars_state req_type)
3464 struct device *dev = acpi_desc->dev;
3465 int scheduled = 0, busy = 0;
3466 struct nfit_spa *nfit_spa;
3468 mutex_lock(&acpi_desc->init_mutex);
3469 if (acpi_desc->cancel) {
3470 mutex_unlock(&acpi_desc->init_mutex);
3474 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3475 int type = nfit_spa_type(nfit_spa->spa);
3477 if (type != NFIT_SPA_PM && type != NFIT_SPA_VOLATILE)
3479 if (test_bit(ARS_FAILED, &nfit_spa->ars_state))
3482 if (test_and_set_bit(req_type, &nfit_spa->ars_state))
3488 sched_ars(acpi_desc);
3489 dev_dbg(dev, "ars_scan triggered\n");
3491 mutex_unlock(&acpi_desc->init_mutex);
3500 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
3502 struct nvdimm_bus_descriptor *nd_desc;
3504 dev_set_drvdata(dev, acpi_desc);
3505 acpi_desc->dev = dev;
3506 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3507 nd_desc = &acpi_desc->nd_desc;
3508 nd_desc->provider_name = "ACPI.NFIT";
3509 nd_desc->module = THIS_MODULE;
3510 nd_desc->ndctl = acpi_nfit_ctl;
3511 nd_desc->flush_probe = acpi_nfit_flush_probe;
3512 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
3513 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3515 INIT_LIST_HEAD(&acpi_desc->spas);
3516 INIT_LIST_HEAD(&acpi_desc->dcrs);
3517 INIT_LIST_HEAD(&acpi_desc->bdws);
3518 INIT_LIST_HEAD(&acpi_desc->idts);
3519 INIT_LIST_HEAD(&acpi_desc->flushes);
3520 INIT_LIST_HEAD(&acpi_desc->memdevs);
3521 INIT_LIST_HEAD(&acpi_desc->dimms);
3522 INIT_LIST_HEAD(&acpi_desc->list);
3523 mutex_init(&acpi_desc->init_mutex);
3524 acpi_desc->scrub_tmo = 1;
3525 INIT_DELAYED_WORK(&acpi_desc->dwork, acpi_nfit_scrub);
3527 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
3529 static void acpi_nfit_put_table(void *table)
3531 acpi_put_table(table);
3534 void acpi_nfit_shutdown(void *data)
3536 struct acpi_nfit_desc *acpi_desc = data;
3537 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3540 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3543 mutex_lock(&acpi_desc_lock);
3544 list_del(&acpi_desc->list);
3545 mutex_unlock(&acpi_desc_lock);
3547 mutex_lock(&acpi_desc->init_mutex);
3548 acpi_desc->cancel = 1;
3549 cancel_delayed_work_sync(&acpi_desc->dwork);
3550 mutex_unlock(&acpi_desc->init_mutex);
3553 * Bounce the nvdimm bus lock to make sure any in-flight
3554 * acpi_nfit_ars_rescan() submissions have had a chance to
3555 * either submit or see ->cancel set.
3557 device_lock(bus_dev);
3558 device_unlock(bus_dev);
3560 flush_workqueue(nfit_wq);
3562 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3564 static int acpi_nfit_add(struct acpi_device *adev)
3566 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3567 struct acpi_nfit_desc *acpi_desc;
3568 struct device *dev = &adev->dev;
3569 struct acpi_table_header *tbl;
3570 acpi_status status = AE_OK;
3574 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3575 if (ACPI_FAILURE(status)) {
3576 /* The NVDIMM root device allows OS to trigger enumeration of
3577 * NVDIMMs through NFIT at boot time and re-enumeration at
3578 * root level via the _FIT method during runtime.
3579 * This is ok to return 0 here, we could have an nvdimm
3580 * hotplugged later and evaluate _FIT method which returns
3581 * data in the format of a series of NFIT Structures.
3583 dev_dbg(dev, "failed to find NFIT at startup\n");
3587 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3592 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3595 acpi_nfit_desc_init(acpi_desc, &adev->dev);
3597 /* Save the acpi header for exporting the revision via sysfs */
3598 acpi_desc->acpi_header = *tbl;
3600 /* Evaluate _FIT and override with that if present */
3601 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3602 if (ACPI_SUCCESS(status) && buf.length > 0) {
3603 union acpi_object *obj = buf.pointer;
3605 if (obj->type == ACPI_TYPE_BUFFER)
3606 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3607 obj->buffer.length);
3609 dev_dbg(dev, "invalid type %d, ignoring _FIT\n",
3613 /* skip over the lead-in header table */
3614 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3615 + sizeof(struct acpi_table_nfit),
3616 sz - sizeof(struct acpi_table_nfit));
3620 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3623 static int acpi_nfit_remove(struct acpi_device *adev)
3625 /* see acpi_nfit_unregister */
3629 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3631 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3632 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3633 union acpi_object *obj;
3638 /* dev->driver may be null if we're being removed */
3639 dev_dbg(dev, "no driver found for dev\n");
3644 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3647 acpi_nfit_desc_init(acpi_desc, dev);
3650 * Finish previous registration before considering new
3653 flush_workqueue(nfit_wq);
3657 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3658 if (ACPI_FAILURE(status)) {
3659 dev_err(dev, "failed to evaluate _FIT\n");
3664 if (obj->type == ACPI_TYPE_BUFFER) {
3665 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3666 obj->buffer.length);
3668 dev_err(dev, "failed to merge updated NFIT\n");
3670 dev_err(dev, "Invalid _FIT\n");
3674 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3676 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3678 if (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON)
3679 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_LONG);
3681 acpi_nfit_ars_rescan(acpi_desc, ARS_REQ_SHORT);
3684 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3686 dev_dbg(dev, "event: 0x%x\n", event);
3689 case NFIT_NOTIFY_UPDATE:
3690 return acpi_nfit_update_notify(dev, handle);
3691 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3692 return acpi_nfit_uc_error_notify(dev, handle);
3697 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3699 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3701 device_lock(&adev->dev);
3702 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3703 device_unlock(&adev->dev);
3706 static const struct acpi_device_id acpi_nfit_ids[] = {
3710 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3712 static struct acpi_driver acpi_nfit_driver = {
3713 .name = KBUILD_MODNAME,
3714 .ids = acpi_nfit_ids,
3716 .add = acpi_nfit_add,
3717 .remove = acpi_nfit_remove,
3718 .notify = acpi_nfit_notify,
3722 static __init int nfit_init(void)
3726 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3727 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3728 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3729 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3730 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3731 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3732 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3733 BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities) != 16);
3735 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3736 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3737 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3738 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3739 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3740 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3741 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3742 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3743 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3744 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3745 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3746 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3747 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3748 guid_parse(UUID_NFIT_DIMM_N_HYPERV, &nfit_uuid[NFIT_DEV_DIMM_N_HYPERV]);
3750 nfit_wq = create_singlethread_workqueue("nfit");
3754 nfit_mce_register();
3755 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3757 nfit_mce_unregister();
3758 destroy_workqueue(nfit_wq);
3765 static __exit void nfit_exit(void)
3767 nfit_mce_unregister();
3768 acpi_bus_unregister_driver(&acpi_nfit_driver);
3769 destroy_workqueue(nfit_wq);
3770 WARN_ON(!list_empty(&acpi_descs));
3773 module_init(nfit_init);
3774 module_exit(nfit_exit);
3775 MODULE_LICENSE("GPL v2");
3776 MODULE_AUTHOR("Intel Corporation");