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