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