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