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