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