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