blob: ea9f3e727fef179a1748d4743a718e8664211448 [file] [log] [blame]
Dan Williamsb94d5232015-05-19 22:54:31 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/list_sort.h>
14#include <linux/libnvdimm.h>
15#include <linux/module.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040016#include <linux/mutex.h>
Dan Williams62232e452015-06-08 14:27:06 -040017#include <linux/ndctl.h>
Vishal Verma37b137f2016-07-23 21:51:42 -070018#include <linux/sysfs.h>
Vishal Verma0caeef62015-12-24 19:21:43 -070019#include <linux/delay.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040020#include <linux/list.h>
21#include <linux/acpi.h>
Dan Williamseaf96152015-05-01 13:11:27 -040022#include <linux/sort.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040023#include <linux/io.h>
Dan Williams1cf03c02016-02-17 13:01:23 -080024#include <linux/nd.h>
Dan Williams96601ad2015-08-24 18:29:38 -040025#include <asm/cacheflush.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040026#include "nfit.h"
27
Ross Zwisler047fc8a2015-06-25 04:21:02 -040028/*
29 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
30 * irrelevant.
31 */
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020032#include <linux/io-64-nonatomic-hi-lo.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040033
Dan Williams4d88a972015-05-31 14:41:48 -040034static bool force_enable_dimms;
35module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
36MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
37
Dan Williams1cf03c02016-02-17 13:01:23 -080038static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
39module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
40MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
41
42/* after three payloads of overflow, it's dead jim */
43static unsigned int scrub_overflow_abort = 3;
44module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
45MODULE_PARM_DESC(scrub_overflow_abort,
46 "Number of times we overflow ARS results before abort");
47
Dan Williams87554092016-04-28 18:01:20 -070048static bool disable_vendor_specific;
49module_param(disable_vendor_specific, bool, S_IRUGO);
50MODULE_PARM_DESC(disable_vendor_specific,
Linda Knippersf2668fa2017-03-07 16:35:14 -050051 "Limit commands to the publicly specified set");
Dan Williams87554092016-04-28 18:01:20 -070052
Linda Knippers095ab4b2017-03-07 16:35:12 -050053static unsigned long override_dsm_mask;
54module_param(override_dsm_mask, ulong, S_IRUGO);
55MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
56
Linda Knippersba650cf2017-03-07 16:35:13 -050057static int default_dsm_family = -1;
58module_param(default_dsm_family, int, S_IRUGO);
59MODULE_PARM_DESC(default_dsm_family,
60 "Try this DSM type first when identifying NVDIMM family");
61
Vishal Verma6839a6d2016-07-23 21:51:21 -070062LIST_HEAD(acpi_descs);
63DEFINE_MUTEX(acpi_desc_lock);
64
Dan Williams7ae0fa432016-02-19 12:16:34 -080065static struct workqueue_struct *nfit_wq;
66
Vishal Verma20985162015-10-27 16:58:27 -060067struct nfit_table_prev {
68 struct list_head spas;
69 struct list_head memdevs;
70 struct list_head dcrs;
71 struct list_head bdws;
72 struct list_head idts;
73 struct list_head flushes;
74};
75
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +030076static guid_t nfit_uuid[NFIT_UUID_MAX];
Dan Williamsb94d5232015-05-19 22:54:31 -040077
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +030078const guid_t *to_nfit_uuid(enum nfit_uuids id)
Dan Williamsb94d5232015-05-19 22:54:31 -040079{
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +030080 return &nfit_uuid[id];
Dan Williamsb94d5232015-05-19 22:54:31 -040081}
Dan Williams6bc75612015-06-17 17:23:32 -040082EXPORT_SYMBOL(to_nfit_uuid);
Dan Williamsb94d5232015-05-19 22:54:31 -040083
Dan Williams62232e452015-06-08 14:27:06 -040084static struct acpi_nfit_desc *to_acpi_nfit_desc(
85 struct nvdimm_bus_descriptor *nd_desc)
86{
87 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
88}
89
90static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
91{
92 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
93
94 /*
95 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
96 * acpi_device.
97 */
98 if (!nd_desc->provider_name
99 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
100 return NULL;
101
102 return to_acpi_device(acpi_desc->dev);
103}
104
Dan Williamsd6eb2702016-12-06 15:06:55 -0800105static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
Dan Williamsaef25332016-02-12 17:01:11 -0800106{
Dan Williamsd4f32362016-03-03 16:08:54 -0800107 struct nd_cmd_clear_error *clear_err;
Dan Williamsaef25332016-02-12 17:01:11 -0800108 struct nd_cmd_ars_status *ars_status;
Dan Williamsaef25332016-02-12 17:01:11 -0800109 u16 flags;
110
111 switch (cmd) {
112 case ND_CMD_ARS_CAP:
Dan Williams11294d62016-09-21 09:21:26 -0700113 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800114 return -ENOTTY;
115
116 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700117 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800118 return -EIO;
119
120 /* No supported scan types for this range */
121 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
Dan Williams11294d62016-09-21 09:21:26 -0700122 if ((status >> 16 & flags) == 0)
Dan Williamsaef25332016-02-12 17:01:11 -0800123 return -ENOTTY;
Vishal Verma9a901f52016-12-05 17:00:37 -0700124 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800125 case ND_CMD_ARS_START:
Dan Williamsaef25332016-02-12 17:01:11 -0800126 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700127 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800128 return -EBUSY;
129
130 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700131 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800132 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700133 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800134 case ND_CMD_ARS_STATUS:
135 ars_status = buf;
136 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700137 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800138 return -EIO;
139 /* Check extended status (Upper two bytes) */
Dan Williams11294d62016-09-21 09:21:26 -0700140 if (status == NFIT_ARS_STATUS_DONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800141 return 0;
142
143 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700144 if (status == NFIT_ARS_STATUS_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800145 return -EBUSY;
146
147 /* No ARS performed for the current boot */
Dan Williams11294d62016-09-21 09:21:26 -0700148 if (status == NFIT_ARS_STATUS_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800149 return -EAGAIN;
150
151 /*
152 * ARS interrupted, either we overflowed or some other
153 * agent wants the scan to stop. If we didn't overflow
154 * then just continue with the returned results.
155 */
Dan Williams11294d62016-09-21 09:21:26 -0700156 if (status == NFIT_ARS_STATUS_INTR) {
Dan Williams82aa37c2016-12-06 12:45:24 -0800157 if (ars_status->out_length >= 40 && (ars_status->flags
158 & NFIT_ARS_F_OVERFLOW))
Dan Williamsaef25332016-02-12 17:01:11 -0800159 return -ENOSPC;
160 return 0;
161 }
162
163 /* Unknown status */
Dan Williams11294d62016-09-21 09:21:26 -0700164 if (status >> 16)
Dan Williamsaef25332016-02-12 17:01:11 -0800165 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700166 return 0;
Dan Williamsd4f32362016-03-03 16:08:54 -0800167 case ND_CMD_CLEAR_ERROR:
168 clear_err = buf;
Dan Williams11294d62016-09-21 09:21:26 -0700169 if (status & 0xffff)
Dan Williamsd4f32362016-03-03 16:08:54 -0800170 return -EIO;
171 if (!clear_err->cleared)
172 return -EIO;
173 if (clear_err->length > clear_err->cleared)
174 return clear_err->cleared;
Vishal Verma9a901f52016-12-05 17:00:37 -0700175 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800176 default:
177 break;
178 }
179
Dan Williams11294d62016-09-21 09:21:26 -0700180 /* all other non-zero status results in an error */
181 if (status)
182 return -EIO;
Dan Williamsaef25332016-02-12 17:01:11 -0800183 return 0;
184}
185
Dan Williams4b27db72017-09-24 09:57:34 -0700186#define ACPI_LABELS_LOCKED 3
187
188static int xlat_nvdimm_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
189 u32 status)
Dan Williams9d62ed92017-05-04 11:47:22 -0700190{
Dan Williams4b27db72017-09-24 09:57:34 -0700191 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
192
Dan Williams9d62ed92017-05-04 11:47:22 -0700193 switch (cmd) {
194 case ND_CMD_GET_CONFIG_SIZE:
Dan Williams4b27db72017-09-24 09:57:34 -0700195 /*
196 * In the _LSI, _LSR, _LSW case the locked status is
197 * communicated via the read/write commands
198 */
Dan Williams466d1492018-03-28 10:44:50 -0700199 if (nfit_mem->has_lsr)
Dan Williams4b27db72017-09-24 09:57:34 -0700200 break;
201
Dan Williams9d62ed92017-05-04 11:47:22 -0700202 if (status >> 16 & ND_CONFIG_LOCKED)
203 return -EACCES;
204 break;
Dan Williams4b27db72017-09-24 09:57:34 -0700205 case ND_CMD_GET_CONFIG_DATA:
206 if (nfit_mem->has_lsr && status == ACPI_LABELS_LOCKED)
207 return -EACCES;
208 break;
209 case ND_CMD_SET_CONFIG_DATA:
210 if (nfit_mem->has_lsw && status == ACPI_LABELS_LOCKED)
211 return -EACCES;
212 break;
Dan Williams9d62ed92017-05-04 11:47:22 -0700213 default:
214 break;
215 }
216
217 /* all other non-zero status results in an error */
218 if (status)
219 return -EIO;
220 return 0;
221}
222
Dan Williamsd6eb2702016-12-06 15:06:55 -0800223static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
224 u32 status)
225{
226 if (!nvdimm)
227 return xlat_bus_status(buf, cmd, status);
Dan Williams4b27db72017-09-24 09:57:34 -0700228 return xlat_nvdimm_status(nvdimm, buf, cmd, status);
229}
230
231/* convert _LS{I,R} packages to the buffer object acpi_nfit_ctl expects */
232static union acpi_object *pkg_to_buf(union acpi_object *pkg)
233{
234 int i;
235 void *dst;
236 size_t size = 0;
237 union acpi_object *buf = NULL;
238
239 if (pkg->type != ACPI_TYPE_PACKAGE) {
240 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
241 pkg->type);
242 goto err;
243 }
244
245 for (i = 0; i < pkg->package.count; i++) {
246 union acpi_object *obj = &pkg->package.elements[i];
247
248 if (obj->type == ACPI_TYPE_INTEGER)
249 size += 4;
250 else if (obj->type == ACPI_TYPE_BUFFER)
251 size += obj->buffer.length;
252 else {
253 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
254 obj->type);
255 goto err;
256 }
257 }
258
259 buf = ACPI_ALLOCATE(sizeof(*buf) + size);
260 if (!buf)
261 goto err;
262
263 dst = buf + 1;
264 buf->type = ACPI_TYPE_BUFFER;
265 buf->buffer.length = size;
266 buf->buffer.pointer = dst;
267 for (i = 0; i < pkg->package.count; i++) {
268 union acpi_object *obj = &pkg->package.elements[i];
269
270 if (obj->type == ACPI_TYPE_INTEGER) {
271 memcpy(dst, &obj->integer.value, 4);
272 dst += 4;
273 } else if (obj->type == ACPI_TYPE_BUFFER) {
274 memcpy(dst, obj->buffer.pointer, obj->buffer.length);
275 dst += obj->buffer.length;
276 }
277 }
278err:
279 ACPI_FREE(pkg);
280 return buf;
281}
282
283static union acpi_object *int_to_buf(union acpi_object *integer)
284{
285 union acpi_object *buf = ACPI_ALLOCATE(sizeof(*buf) + 4);
286 void *dst = NULL;
287
288 if (!buf)
289 goto err;
290
291 if (integer->type != ACPI_TYPE_INTEGER) {
292 WARN_ONCE(1, "BIOS bug, unexpected element type: %d\n",
293 integer->type);
294 goto err;
295 }
296
297 dst = buf + 1;
298 buf->type = ACPI_TYPE_BUFFER;
299 buf->buffer.length = 4;
300 buf->buffer.pointer = dst;
301 memcpy(dst, &integer->integer.value, 4);
302err:
303 ACPI_FREE(integer);
304 return buf;
305}
306
307static union acpi_object *acpi_label_write(acpi_handle handle, u32 offset,
308 u32 len, void *data)
309{
310 acpi_status rc;
311 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
312 struct acpi_object_list input = {
313 .count = 3,
314 .pointer = (union acpi_object []) {
315 [0] = {
316 .integer.type = ACPI_TYPE_INTEGER,
317 .integer.value = offset,
318 },
319 [1] = {
320 .integer.type = ACPI_TYPE_INTEGER,
321 .integer.value = len,
322 },
323 [2] = {
324 .buffer.type = ACPI_TYPE_BUFFER,
325 .buffer.pointer = data,
326 .buffer.length = len,
327 },
328 },
329 };
330
331 rc = acpi_evaluate_object(handle, "_LSW", &input, &buf);
332 if (ACPI_FAILURE(rc))
333 return NULL;
334 return int_to_buf(buf.pointer);
335}
336
337static union acpi_object *acpi_label_read(acpi_handle handle, u32 offset,
338 u32 len)
339{
340 acpi_status rc;
341 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
342 struct acpi_object_list input = {
343 .count = 2,
344 .pointer = (union acpi_object []) {
345 [0] = {
346 .integer.type = ACPI_TYPE_INTEGER,
347 .integer.value = offset,
348 },
349 [1] = {
350 .integer.type = ACPI_TYPE_INTEGER,
351 .integer.value = len,
352 },
353 },
354 };
355
356 rc = acpi_evaluate_object(handle, "_LSR", &input, &buf);
357 if (ACPI_FAILURE(rc))
358 return NULL;
359 return pkg_to_buf(buf.pointer);
360}
361
362static union acpi_object *acpi_label_info(acpi_handle handle)
363{
364 acpi_status rc;
365 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
366
367 rc = acpi_evaluate_object(handle, "_LSI", NULL, &buf);
368 if (ACPI_FAILURE(rc))
369 return NULL;
370 return pkg_to_buf(buf.pointer);
Dan Williamsd6eb2702016-12-06 15:06:55 -0800371}
372
Dan Williams11e14272017-10-20 15:39:43 -0700373static u8 nfit_dsm_revid(unsigned family, unsigned func)
374{
375 static const u8 revid_table[NVDIMM_FAMILY_MAX+1][32] = {
376 [NVDIMM_FAMILY_INTEL] = {
377 [NVDIMM_INTEL_GET_MODES] = 2,
378 [NVDIMM_INTEL_GET_FWINFO] = 2,
379 [NVDIMM_INTEL_START_FWUPDATE] = 2,
380 [NVDIMM_INTEL_SEND_FWUPDATE] = 2,
381 [NVDIMM_INTEL_FINISH_FWUPDATE] = 2,
382 [NVDIMM_INTEL_QUERY_FWUPDATE] = 2,
383 [NVDIMM_INTEL_SET_THRESHOLD] = 2,
384 [NVDIMM_INTEL_INJECT_ERROR] = 2,
385 },
386 };
387 u8 id;
388
389 if (family > NVDIMM_FAMILY_MAX)
390 return 0;
391 if (func > 31)
392 return 0;
393 id = revid_table[family][func];
394 if (id == 0)
395 return 1; /* default */
396 return id;
397}
398
Dan Williamsa7de92d2016-12-05 13:43:25 -0800399int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
400 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400401{
Dan Williams62232e452015-06-08 14:27:06 -0400402 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams4b27db72017-09-24 09:57:34 -0700403 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400404 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700405 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400406 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700407 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400408 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700409 unsigned long cmd_mask, dsm_mask;
Dan Williams11294d62016-09-21 09:21:26 -0700410 u32 offset, fw_status = 0;
Dan Williams62232e452015-06-08 14:27:06 -0400411 acpi_handle handle;
Dan Williams31eca762016-04-28 16:23:43 -0700412 unsigned int func;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300413 const guid_t *guid;
Dan Williams62232e452015-06-08 14:27:06 -0400414 int rc, i;
415
Dan Williams31eca762016-04-28 16:23:43 -0700416 func = cmd;
417 if (cmd == ND_CMD_CALL) {
418 call_pkg = buf;
419 func = call_pkg->nd_command;
Meng Xu9edcad52017-09-04 11:34:33 -0400420
421 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
422 if (call_pkg->nd_reserved2[i])
423 return -EINVAL;
Dan Williams31eca762016-04-28 16:23:43 -0700424 }
425
Dan Williams62232e452015-06-08 14:27:06 -0400426 if (nvdimm) {
Dan Williams62232e452015-06-08 14:27:06 -0400427 struct acpi_device *adev = nfit_mem->adev;
428
429 if (!adev)
430 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700431 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
432 return -ENOTTY;
433
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400434 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400435 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700436 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400437 dsm_mask = nfit_mem->dsm_mask;
438 desc = nd_cmd_dimm_desc(cmd);
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300439 guid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400440 handle = adev->handle;
441 } else {
442 struct acpi_device *adev = to_acpi_dev(acpi_desc);
443
444 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700445 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700446 dsm_mask = cmd_mask;
Jerry Hoemann7db5bb32017-06-30 20:53:24 -0700447 if (cmd == ND_CMD_CALL)
448 dsm_mask = nd_desc->bus_dsm_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400449 desc = nd_cmd_bus_desc(cmd);
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300450 guid = to_nfit_uuid(NFIT_DEV_BUS);
Dan Williams62232e452015-06-08 14:27:06 -0400451 handle = adev->handle;
452 dimm_name = "bus";
453 }
454
455 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
456 return -ENOTTY;
457
Dan Williams31eca762016-04-28 16:23:43 -0700458 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400459 return -ENOTTY;
460
461 in_obj.type = ACPI_TYPE_PACKAGE;
462 in_obj.package.count = 1;
463 in_obj.package.elements = &in_buf;
464 in_buf.type = ACPI_TYPE_BUFFER;
465 in_buf.buffer.pointer = buf;
466 in_buf.buffer.length = 0;
467
468 /* libnvdimm has already validated the input envelope */
469 for (i = 0; i < desc->in_num; i++)
470 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
471 i, buf);
472
Dan Williams31eca762016-04-28 16:23:43 -0700473 if (call_pkg) {
474 /* skip over package wrapper */
475 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
476 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400477 }
478
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100479 dev_dbg(dev, "%s cmd: %d: func: %d input length: %d\n",
480 dimm_name, cmd, func, in_buf.buffer.length);
Dan Williams7699a6a2017-04-28 13:54:30 -0700481 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
Dan Williams31eca762016-04-28 16:23:43 -0700482 in_buf.buffer.pointer,
483 min_t(u32, 256, in_buf.buffer.length), true);
Dan Williams31eca762016-04-28 16:23:43 -0700484
Dan Williams4b27db72017-09-24 09:57:34 -0700485 /* call the BIOS, prefer the named methods over _DSM if available */
Dan Williams466d1492018-03-28 10:44:50 -0700486 if (nvdimm && cmd == ND_CMD_GET_CONFIG_SIZE && nfit_mem->has_lsr)
Dan Williams4b27db72017-09-24 09:57:34 -0700487 out_obj = acpi_label_info(handle);
Dan Williams0e7f0742017-11-12 14:57:09 -0800488 else if (nvdimm && cmd == ND_CMD_GET_CONFIG_DATA && nfit_mem->has_lsr) {
Dan Williams4b27db72017-09-24 09:57:34 -0700489 struct nd_cmd_get_config_data_hdr *p = buf;
490
491 out_obj = acpi_label_read(handle, p->in_offset, p->in_length);
Dan Williams0e7f0742017-11-12 14:57:09 -0800492 } else if (nvdimm && cmd == ND_CMD_SET_CONFIG_DATA
493 && nfit_mem->has_lsw) {
Dan Williams4b27db72017-09-24 09:57:34 -0700494 struct nd_cmd_set_config_hdr *p = buf;
495
496 out_obj = acpi_label_write(handle, p->in_offset, p->in_length,
497 p->in_buf);
Dan Williams11e14272017-10-20 15:39:43 -0700498 } else {
499 u8 revid;
500
Dan Williams0e7f0742017-11-12 14:57:09 -0800501 if (nvdimm)
Dan Williams11e14272017-10-20 15:39:43 -0700502 revid = nfit_dsm_revid(nfit_mem->family, func);
503 else
504 revid = 1;
505 out_obj = acpi_evaluate_dsm(handle, guid, revid, func, &in_obj);
506 }
Dan Williams4b27db72017-09-24 09:57:34 -0700507
Dan Williams62232e452015-06-08 14:27:06 -0400508 if (!out_obj) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100509 dev_dbg(dev, "%s _DSM failed cmd: %s\n", dimm_name, cmd_name);
Dan Williams62232e452015-06-08 14:27:06 -0400510 return -EINVAL;
511 }
512
Dan Williams31eca762016-04-28 16:23:43 -0700513 if (call_pkg) {
514 call_pkg->nd_fw_size = out_obj->buffer.length;
515 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
516 out_obj->buffer.pointer,
517 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
518
519 ACPI_FREE(out_obj);
520 /*
521 * Need to support FW function w/o known size in advance.
522 * Caller can determine required size based upon nd_fw_size.
523 * If we return an error (like elsewhere) then caller wouldn't
524 * be able to rely upon data returned to make calculation.
525 */
526 return 0;
527 }
528
Dan Williams62232e452015-06-08 14:27:06 -0400529 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100530 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
531 dimm_name, cmd_name, out_obj->type);
Dan Williams62232e452015-06-08 14:27:06 -0400532 rc = -EINVAL;
533 goto out;
534 }
535
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100536 dev_dbg(dev, "%s cmd: %s output length: %d\n", dimm_name,
Dan Williams7699a6a2017-04-28 13:54:30 -0700537 cmd_name, out_obj->buffer.length);
538 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
539 out_obj->buffer.pointer,
540 min_t(u32, 128, out_obj->buffer.length), true);
Dan Williams62232e452015-06-08 14:27:06 -0400541
542 for (i = 0, offset = 0; i < desc->out_num; i++) {
543 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800544 (u32 *) out_obj->buffer.pointer,
545 out_obj->buffer.length - offset);
Dan Williams62232e452015-06-08 14:27:06 -0400546
547 if (offset + out_size > out_obj->buffer.length) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100548 dev_dbg(dev, "%s output object underflow cmd: %s field: %d\n",
549 dimm_name, cmd_name, i);
Dan Williams62232e452015-06-08 14:27:06 -0400550 break;
551 }
552
553 if (in_buf.buffer.length + offset + out_size > buf_len) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100554 dev_dbg(dev, "%s output overrun cmd: %s field: %d\n",
555 dimm_name, cmd_name, i);
Dan Williams62232e452015-06-08 14:27:06 -0400556 rc = -ENXIO;
557 goto out;
558 }
559 memcpy(buf + in_buf.buffer.length + offset,
560 out_obj->buffer.pointer + offset, out_size);
561 offset += out_size;
562 }
Dan Williams11294d62016-09-21 09:21:26 -0700563
564 /*
565 * Set fw_status for all the commands with a known format to be
566 * later interpreted by xlat_status().
567 */
Dan Williams0e7f0742017-11-12 14:57:09 -0800568 if (i >= 1 && ((!nvdimm && cmd >= ND_CMD_ARS_CAP
569 && cmd <= ND_CMD_CLEAR_ERROR)
570 || (nvdimm && cmd >= ND_CMD_SMART
571 && cmd <= ND_CMD_VENDOR)))
Dan Williams11294d62016-09-21 09:21:26 -0700572 fw_status = *(u32 *) out_obj->buffer.pointer;
573
Dan Williams62232e452015-06-08 14:27:06 -0400574 if (offset + in_buf.buffer.length < buf_len) {
575 if (i >= 1) {
576 /*
577 * status valid, return the number of bytes left
578 * unfilled in the output buffer
579 */
580 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800581 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800582 *cmd_rc = xlat_status(nvdimm, buf, cmd,
583 fw_status);
Dan Williams62232e452015-06-08 14:27:06 -0400584 } else {
585 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
586 __func__, dimm_name, cmd_name, buf_len,
587 offset);
588 rc = -ENXIO;
589 }
Dan Williams2eea6582016-05-02 09:11:53 -0700590 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400591 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700592 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800593 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
Dan Williams2eea6582016-05-02 09:11:53 -0700594 }
Dan Williams62232e452015-06-08 14:27:06 -0400595
596 out:
597 ACPI_FREE(out_obj);
598
599 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400600}
Dan Williamsa7de92d2016-12-05 13:43:25 -0800601EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
Dan Williamsb94d5232015-05-19 22:54:31 -0400602
603static const char *spa_type_name(u16 type)
604{
605 static const char *to_name[] = {
606 [NFIT_SPA_VOLATILE] = "volatile",
607 [NFIT_SPA_PM] = "pmem",
608 [NFIT_SPA_DCR] = "dimm-control-region",
609 [NFIT_SPA_BDW] = "block-data-window",
610 [NFIT_SPA_VDISK] = "volatile-disk",
611 [NFIT_SPA_VCD] = "volatile-cd",
612 [NFIT_SPA_PDISK] = "persistent-disk",
613 [NFIT_SPA_PCD] = "persistent-cd",
614
615 };
616
617 if (type > NFIT_SPA_PCD)
618 return "unknown";
619
620 return to_name[type];
621}
622
Vishal Verma6839a6d2016-07-23 21:51:21 -0700623int nfit_spa_type(struct acpi_nfit_system_address *spa)
Dan Williamsb94d5232015-05-19 22:54:31 -0400624{
625 int i;
626
627 for (i = 0; i < NFIT_UUID_MAX; i++)
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300628 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
Dan Williamsb94d5232015-05-19 22:54:31 -0400629 return i;
630 return -1;
631}
632
633static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600634 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400635 struct acpi_nfit_system_address *spa)
636{
637 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600638 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400639
Dan Williams31932042016-07-14 17:22:48 -0700640 if (spa->header.length != sizeof(*spa))
641 return false;
642
Vishal Verma20985162015-10-27 16:58:27 -0600643 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700644 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600645 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
646 return true;
647 }
648 }
649
Dan Williams31932042016-07-14 17:22:48 -0700650 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
651 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400652 if (!nfit_spa)
653 return false;
654 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700655 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400656 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100657 dev_dbg(dev, "spa index: %d type: %s\n",
Dan Williamsb94d5232015-05-19 22:54:31 -0400658 spa->range_index,
659 spa_type_name(nfit_spa_type(spa)));
660 return true;
661}
662
663static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600664 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400665 struct acpi_nfit_memory_map *memdev)
666{
667 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600668 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400669
Dan Williams31932042016-07-14 17:22:48 -0700670 if (memdev->header.length != sizeof(*memdev))
671 return false;
672
Vishal Verma20985162015-10-27 16:58:27 -0600673 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700674 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600675 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
676 return true;
677 }
678
Dan Williams31932042016-07-14 17:22:48 -0700679 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
680 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400681 if (!nfit_memdev)
682 return false;
683 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700684 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400685 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100686 dev_dbg(dev, "memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
687 memdev->device_handle, memdev->range_index,
Dan Williamscaa603a2017-04-14 10:27:11 -0700688 memdev->region_index, memdev->flags);
Dan Williamsb94d5232015-05-19 22:54:31 -0400689 return true;
690}
691
Dan Williams31932042016-07-14 17:22:48 -0700692/*
693 * An implementation may provide a truncated control region if no block windows
694 * are defined.
695 */
696static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
697{
698 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
699 window_size))
700 return 0;
701 if (dcr->windows)
702 return sizeof(*dcr);
703 return offsetof(struct acpi_nfit_control_region, window_size);
704}
705
Dan Williamsb94d5232015-05-19 22:54:31 -0400706static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600707 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400708 struct acpi_nfit_control_region *dcr)
709{
710 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600711 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400712
Dan Williams31932042016-07-14 17:22:48 -0700713 if (!sizeof_dcr(dcr))
714 return false;
715
Vishal Verma20985162015-10-27 16:58:27 -0600716 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700717 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600718 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
719 return true;
720 }
721
Dan Williams31932042016-07-14 17:22:48 -0700722 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
723 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400724 if (!nfit_dcr)
725 return false;
726 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700727 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400728 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100729 dev_dbg(dev, "dcr index: %d windows: %d\n",
Dan Williamsb94d5232015-05-19 22:54:31 -0400730 dcr->region_index, dcr->windows);
731 return true;
732}
733
734static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600735 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400736 struct acpi_nfit_data_region *bdw)
737{
738 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600739 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400740
Dan Williams31932042016-07-14 17:22:48 -0700741 if (bdw->header.length != sizeof(*bdw))
742 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600743 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700744 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600745 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
746 return true;
747 }
748
Dan Williams31932042016-07-14 17:22:48 -0700749 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
750 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400751 if (!nfit_bdw)
752 return false;
753 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700754 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400755 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100756 dev_dbg(dev, "bdw dcr: %d windows: %d\n",
Dan Williamsb94d5232015-05-19 22:54:31 -0400757 bdw->region_index, bdw->windows);
758 return true;
759}
760
Dan Williams31932042016-07-14 17:22:48 -0700761static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
762{
763 if (idt->header.length < sizeof(*idt))
764 return 0;
765 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
766}
767
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400768static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600769 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400770 struct acpi_nfit_interleave *idt)
771{
772 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600773 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400774
Dan Williams31932042016-07-14 17:22:48 -0700775 if (!sizeof_idt(idt))
776 return false;
777
778 list_for_each_entry(nfit_idt, &prev->idts, list) {
779 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
780 continue;
781
782 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600783 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
784 return true;
785 }
Dan Williams31932042016-07-14 17:22:48 -0700786 }
Vishal Verma20985162015-10-27 16:58:27 -0600787
Dan Williams31932042016-07-14 17:22:48 -0700788 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
789 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400790 if (!nfit_idt)
791 return false;
792 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700793 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400794 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100795 dev_dbg(dev, "idt index: %d num_lines: %d\n",
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400796 idt->interleave_index, idt->line_count);
797 return true;
798}
799
Dan Williams31932042016-07-14 17:22:48 -0700800static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
801{
802 if (flush->header.length < sizeof(*flush))
803 return 0;
804 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
805}
806
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600807static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600808 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600809 struct acpi_nfit_flush_address *flush)
810{
811 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600812 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600813
Dan Williams31932042016-07-14 17:22:48 -0700814 if (!sizeof_flush(flush))
815 return false;
816
817 list_for_each_entry(nfit_flush, &prev->flushes, list) {
818 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
819 continue;
820
821 if (memcmp(nfit_flush->flush, flush,
822 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600823 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
824 return true;
825 }
Dan Williams31932042016-07-14 17:22:48 -0700826 }
Vishal Verma20985162015-10-27 16:58:27 -0600827
Dan Williams31932042016-07-14 17:22:48 -0700828 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
829 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600830 if (!nfit_flush)
831 return false;
832 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700833 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600834 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100835 dev_dbg(dev, "nfit_flush handle: %d hint_count: %d\n",
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600836 flush->device_handle, flush->hint_count);
837 return true;
838}
839
Dave Jiang06e8ccd2018-01-31 12:45:38 -0700840static bool add_platform_cap(struct acpi_nfit_desc *acpi_desc,
841 struct acpi_nfit_capabilities *pcap)
842{
843 struct device *dev = acpi_desc->dev;
844 u32 mask;
845
846 mask = (1 << (pcap->highest_capability + 1)) - 1;
847 acpi_desc->platform_cap = pcap->capabilities & mask;
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100848 dev_dbg(dev, "cap: %#x\n", acpi_desc->platform_cap);
Dave Jiang06e8ccd2018-01-31 12:45:38 -0700849 return true;
850}
851
Vishal Verma20985162015-10-27 16:58:27 -0600852static void *add_table(struct acpi_nfit_desc *acpi_desc,
853 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400854{
855 struct device *dev = acpi_desc->dev;
856 struct acpi_nfit_header *hdr;
857 void *err = ERR_PTR(-ENOMEM);
858
859 if (table >= end)
860 return NULL;
861
862 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600863 if (!hdr->length) {
864 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
865 hdr->type);
866 return NULL;
867 }
868
Dan Williamsb94d5232015-05-19 22:54:31 -0400869 switch (hdr->type) {
870 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600871 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400872 return err;
873 break;
874 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600875 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400876 return err;
877 break;
878 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600879 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400880 return err;
881 break;
882 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600883 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400884 return err;
885 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400886 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600887 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400888 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400889 break;
890 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600891 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600892 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400893 break;
894 case ACPI_NFIT_TYPE_SMBIOS:
Johannes Thumshirnb8147352018-03-02 13:20:49 +0100895 dev_dbg(dev, "smbios\n");
Dan Williamsb94d5232015-05-19 22:54:31 -0400896 break;
Dave Jiang06e8ccd2018-01-31 12:45:38 -0700897 case ACPI_NFIT_TYPE_CAPABILITIES:
898 if (!add_platform_cap(acpi_desc, table))
899 return err;
900 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400901 default:
902 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
903 break;
904 }
905
906 return table + hdr->length;
907}
908
909static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
910 struct nfit_mem *nfit_mem)
911{
912 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
913 u16 dcr = nfit_mem->dcr->region_index;
914 struct nfit_spa *nfit_spa;
915
916 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
917 u16 range_index = nfit_spa->spa->range_index;
918 int type = nfit_spa_type(nfit_spa->spa);
919 struct nfit_memdev *nfit_memdev;
920
921 if (type != NFIT_SPA_BDW)
922 continue;
923
924 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
925 if (nfit_memdev->memdev->range_index != range_index)
926 continue;
927 if (nfit_memdev->memdev->device_handle != device_handle)
928 continue;
929 if (nfit_memdev->memdev->region_index != dcr)
930 continue;
931
932 nfit_mem->spa_bdw = nfit_spa->spa;
933 return;
934 }
935 }
936
937 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
938 nfit_mem->spa_dcr->range_index);
939 nfit_mem->bdw = NULL;
940}
941
Dan Williams6697b2c2016-02-04 16:51:00 -0800942static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400943 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
944{
945 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400946 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400947 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400948 struct nfit_idt *nfit_idt;
949 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400950
Dan Williamsb94d5232015-05-19 22:54:31 -0400951 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
952 if (nfit_bdw->bdw->region_index != dcr)
953 continue;
954 nfit_mem->bdw = nfit_bdw->bdw;
955 break;
956 }
957
958 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800959 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400960
961 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400962
963 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800964 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400965
966 range_index = nfit_mem->spa_bdw->range_index;
967 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
968 if (nfit_memdev->memdev->range_index != range_index ||
969 nfit_memdev->memdev->region_index != dcr)
970 continue;
971 nfit_mem->memdev_bdw = nfit_memdev->memdev;
972 idt_idx = nfit_memdev->memdev->interleave_index;
973 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
974 if (nfit_idt->idt->interleave_index != idt_idx)
975 continue;
976 nfit_mem->idt_bdw = nfit_idt->idt;
977 break;
978 }
979 break;
980 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400981}
982
Dan Williams14999342017-04-13 19:46:36 -0700983static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400984 struct acpi_nfit_system_address *spa)
985{
986 struct nfit_mem *nfit_mem, *found;
987 struct nfit_memdev *nfit_memdev;
Dan Williams14999342017-04-13 19:46:36 -0700988 int type = spa ? nfit_spa_type(spa) : 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400989
990 switch (type) {
991 case NFIT_SPA_DCR:
992 case NFIT_SPA_PM:
993 break;
994 default:
Dan Williams14999342017-04-13 19:46:36 -0700995 if (spa)
996 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400997 }
998
Dan Williams14999342017-04-13 19:46:36 -0700999 /*
1000 * This loop runs in two modes, when a dimm is mapped the loop
1001 * adds memdev associations to an existing dimm, or creates a
1002 * dimm. In the unmapped dimm case this loop sweeps for memdev
1003 * instances with an invalid / zero range_index and adds those
1004 * dimms without spa associations.
1005 */
Dan Williamsb94d5232015-05-19 22:54:31 -04001006 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -07001007 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -08001008 struct nfit_dcr *nfit_dcr;
1009 u32 device_handle;
1010 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -04001011
Dan Williams14999342017-04-13 19:46:36 -07001012 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
1013 continue;
1014 if (!spa && nfit_memdev->memdev->range_index)
Dan Williamsb94d5232015-05-19 22:54:31 -04001015 continue;
1016 found = NULL;
1017 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -08001018 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -04001019 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -08001020 if (__to_nfit_memdev(nfit_mem)->device_handle
1021 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -04001022 found = nfit_mem;
1023 break;
1024 }
1025
1026 if (found)
1027 nfit_mem = found;
1028 else {
1029 nfit_mem = devm_kzalloc(acpi_desc->dev,
1030 sizeof(*nfit_mem), GFP_KERNEL);
1031 if (!nfit_mem)
1032 return -ENOMEM;
1033 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001034 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -08001035 list_add(&nfit_mem->list, &acpi_desc->dimms);
1036 }
1037
1038 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1039 if (nfit_dcr->dcr->region_index != dcr)
1040 continue;
1041 /*
1042 * Record the control region for the dimm. For
1043 * the ACPI 6.1 case, where there are separate
1044 * control regions for the pmem vs blk
1045 * interfaces, be sure to record the extended
1046 * blk details.
1047 */
1048 if (!nfit_mem->dcr)
1049 nfit_mem->dcr = nfit_dcr->dcr;
1050 else if (nfit_mem->dcr->windows == 0
1051 && nfit_dcr->dcr->windows)
1052 nfit_mem->dcr = nfit_dcr->dcr;
1053 break;
1054 }
1055
Dan Williamsad9ac5e2016-05-26 11:38:08 -07001056 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001057 struct acpi_nfit_flush_address *flush;
1058 u16 i;
1059
Dan Williamsad9ac5e2016-05-26 11:38:08 -07001060 if (nfit_flush->flush->device_handle != device_handle)
1061 continue;
1062 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -07001063 flush = nfit_flush->flush;
1064 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
1065 flush->hint_count
1066 * sizeof(struct resource), GFP_KERNEL);
1067 if (!nfit_mem->flush_wpq)
1068 return -ENOMEM;
1069 for (i = 0; i < flush->hint_count; i++) {
1070 struct resource *res = &nfit_mem->flush_wpq[i];
1071
1072 res->start = flush->hint_address[i];
1073 res->end = res->start + 8 - 1;
1074 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -07001075 break;
1076 }
1077
Dan Williams6697b2c2016-02-04 16:51:00 -08001078 if (dcr && !nfit_mem->dcr) {
1079 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
1080 spa->range_index, dcr);
1081 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -04001082 }
1083
1084 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001085 struct nfit_idt *nfit_idt;
1086 u16 idt_idx;
1087
Dan Williamsb94d5232015-05-19 22:54:31 -04001088 /* multiple dimms may share a SPA when interleaved */
1089 nfit_mem->spa_dcr = spa;
1090 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001091 idt_idx = nfit_memdev->memdev->interleave_index;
1092 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
1093 if (nfit_idt->idt->interleave_index != idt_idx)
1094 continue;
1095 nfit_mem->idt_dcr = nfit_idt->idt;
1096 break;
1097 }
Dan Williams6697b2c2016-02-04 16:51:00 -08001098 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williams14999342017-04-13 19:46:36 -07001099 } else if (type == NFIT_SPA_PM) {
Dan Williamsb94d5232015-05-19 22:54:31 -04001100 /*
1101 * A single dimm may belong to multiple SPA-PM
1102 * ranges, record at least one in addition to
1103 * any SPA-DCR range.
1104 */
1105 nfit_mem->memdev_pmem = nfit_memdev->memdev;
Dan Williams14999342017-04-13 19:46:36 -07001106 } else
1107 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -04001108 }
1109
1110 return 0;
1111}
1112
1113static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
1114{
1115 struct nfit_mem *a = container_of(_a, typeof(*a), list);
1116 struct nfit_mem *b = container_of(_b, typeof(*b), list);
1117 u32 handleA, handleB;
1118
1119 handleA = __to_nfit_memdev(a)->device_handle;
1120 handleB = __to_nfit_memdev(b)->device_handle;
1121 if (handleA < handleB)
1122 return -1;
1123 else if (handleA > handleB)
1124 return 1;
1125 return 0;
1126}
1127
1128static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
1129{
1130 struct nfit_spa *nfit_spa;
Dan Williams14999342017-04-13 19:46:36 -07001131 int rc;
1132
Dan Williamsb94d5232015-05-19 22:54:31 -04001133
1134 /*
1135 * For each SPA-DCR or SPA-PMEM address range find its
1136 * corresponding MEMDEV(s). From each MEMDEV find the
1137 * corresponding DCR. Then, if we're operating on a SPA-DCR,
1138 * try to find a SPA-BDW and a corresponding BDW that references
1139 * the DCR. Throw it all into an nfit_mem object. Note, that
1140 * BDWs are optional.
1141 */
1142 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
Dan Williams14999342017-04-13 19:46:36 -07001143 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
Dan Williamsb94d5232015-05-19 22:54:31 -04001144 if (rc)
1145 return rc;
1146 }
1147
Dan Williams14999342017-04-13 19:46:36 -07001148 /*
1149 * If a DIMM has failed to be mapped into SPA there will be no
1150 * SPA entries above. Find and register all the unmapped DIMMs
1151 * for reporting and recovery purposes.
1152 */
1153 rc = __nfit_mem_init(acpi_desc, NULL);
1154 if (rc)
1155 return rc;
1156
Dan Williamsb94d5232015-05-19 22:54:31 -04001157 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
1158
1159 return 0;
1160}
1161
Jerry Hoemann41f95db2017-06-30 20:41:28 -07001162static ssize_t bus_dsm_mask_show(struct device *dev,
1163 struct device_attribute *attr, char *buf)
1164{
1165 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1166 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1167
1168 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
1169}
1170static struct device_attribute dev_attr_bus_dsm_mask =
1171 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
1172
Dan Williams45def222015-04-26 19:26:48 -04001173static ssize_t revision_show(struct device *dev,
1174 struct device_attribute *attr, char *buf)
1175{
1176 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1177 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1178 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1179
Linda Knippers6b577c92015-11-20 19:05:49 -05001180 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -04001181}
1182static DEVICE_ATTR_RO(revision);
1183
Vishal Verma9ffd6352016-09-30 17:19:29 -06001184static ssize_t hw_error_scrub_show(struct device *dev,
1185 struct device_attribute *attr, char *buf)
1186{
1187 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1188 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1189 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1190
1191 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
1192}
1193
1194/*
1195 * The 'hw_error_scrub' attribute can have the following values written to it:
1196 * '0': Switch to the default mode where an exception will only insert
1197 * the address of the memory error into the poison and badblocks lists.
1198 * '1': Enable a full scrub to happen if an exception for a memory error is
1199 * received.
1200 */
1201static ssize_t hw_error_scrub_store(struct device *dev,
1202 struct device_attribute *attr, const char *buf, size_t size)
1203{
1204 struct nvdimm_bus_descriptor *nd_desc;
1205 ssize_t rc;
1206 long val;
1207
1208 rc = kstrtol(buf, 0, &val);
1209 if (rc)
1210 return rc;
1211
1212 device_lock(dev);
1213 nd_desc = dev_get_drvdata(dev);
1214 if (nd_desc) {
1215 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1216
1217 switch (val) {
1218 case HW_ERROR_SCRUB_ON:
1219 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
1220 break;
1221 case HW_ERROR_SCRUB_OFF:
1222 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1223 break;
1224 default:
1225 rc = -EINVAL;
1226 break;
1227 }
1228 }
1229 device_unlock(dev);
1230 if (rc)
1231 return rc;
1232 return size;
1233}
1234static DEVICE_ATTR_RW(hw_error_scrub);
1235
Vishal Verma37b137f2016-07-23 21:51:42 -07001236/*
1237 * This shows the number of full Address Range Scrubs that have been
1238 * completed since driver load time. Userspace can wait on this using
1239 * select/poll etc. A '+' at the end indicates an ARS is in progress
1240 */
1241static ssize_t scrub_show(struct device *dev,
1242 struct device_attribute *attr, char *buf)
1243{
1244 struct nvdimm_bus_descriptor *nd_desc;
1245 ssize_t rc = -ENXIO;
1246
1247 device_lock(dev);
1248 nd_desc = dev_get_drvdata(dev);
1249 if (nd_desc) {
1250 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1251
1252 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1253 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
1254 }
1255 device_unlock(dev);
1256 return rc;
1257}
1258
Vishal Verma37b137f2016-07-23 21:51:42 -07001259static ssize_t scrub_store(struct device *dev,
1260 struct device_attribute *attr, const char *buf, size_t size)
1261{
1262 struct nvdimm_bus_descriptor *nd_desc;
1263 ssize_t rc;
1264 long val;
1265
1266 rc = kstrtol(buf, 0, &val);
1267 if (rc)
1268 return rc;
1269 if (val != 1)
1270 return -EINVAL;
1271
1272 device_lock(dev);
1273 nd_desc = dev_get_drvdata(dev);
1274 if (nd_desc) {
1275 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1276
Toshi Kani80790032017-06-29 20:41:30 -06001277 rc = acpi_nfit_ars_rescan(acpi_desc, 0);
Vishal Verma37b137f2016-07-23 21:51:42 -07001278 }
1279 device_unlock(dev);
1280 if (rc)
1281 return rc;
1282 return size;
1283}
1284static DEVICE_ATTR_RW(scrub);
1285
1286static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1287{
1288 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1289 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1290 | 1 << ND_CMD_ARS_STATUS;
1291
1292 return (nd_desc->cmd_mask & mask) == mask;
1293}
1294
1295static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1296{
1297 struct device *dev = container_of(kobj, struct device, kobj);
1298 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1299
1300 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1301 return 0;
1302 return a->mode;
1303}
1304
Dan Williams45def222015-04-26 19:26:48 -04001305static struct attribute *acpi_nfit_attributes[] = {
1306 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -07001307 &dev_attr_scrub.attr,
Vishal Verma9ffd6352016-09-30 17:19:29 -06001308 &dev_attr_hw_error_scrub.attr,
Jerry Hoemann41f95db2017-06-30 20:41:28 -07001309 &dev_attr_bus_dsm_mask.attr,
Dan Williams45def222015-04-26 19:26:48 -04001310 NULL,
1311};
1312
Arvind Yadav5e937462017-06-22 15:44:41 +05301313static const struct attribute_group acpi_nfit_attribute_group = {
Dan Williams45def222015-04-26 19:26:48 -04001314 .name = "nfit",
1315 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -07001316 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -04001317};
1318
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001319static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -04001320 &nvdimm_bus_attribute_group,
1321 &acpi_nfit_attribute_group,
1322 NULL,
1323};
1324
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001325static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1326{
1327 struct nvdimm *nvdimm = to_nvdimm(dev);
1328 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1329
1330 return __to_nfit_memdev(nfit_mem);
1331}
1332
1333static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1334{
1335 struct nvdimm *nvdimm = to_nvdimm(dev);
1336 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1337
1338 return nfit_mem->dcr;
1339}
1340
1341static ssize_t handle_show(struct device *dev,
1342 struct device_attribute *attr, char *buf)
1343{
1344 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1345
1346 return sprintf(buf, "%#x\n", memdev->device_handle);
1347}
1348static DEVICE_ATTR_RO(handle);
1349
1350static ssize_t phys_id_show(struct device *dev,
1351 struct device_attribute *attr, char *buf)
1352{
1353 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1354
1355 return sprintf(buf, "%#x\n", memdev->physical_id);
1356}
1357static DEVICE_ATTR_RO(phys_id);
1358
1359static ssize_t vendor_show(struct device *dev,
1360 struct device_attribute *attr, char *buf)
1361{
1362 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1363
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001364 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001365}
1366static DEVICE_ATTR_RO(vendor);
1367
1368static ssize_t rev_id_show(struct device *dev,
1369 struct device_attribute *attr, char *buf)
1370{
1371 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1372
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001373 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001374}
1375static DEVICE_ATTR_RO(rev_id);
1376
1377static ssize_t device_show(struct device *dev,
1378 struct device_attribute *attr, char *buf)
1379{
1380 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1381
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001382 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001383}
1384static DEVICE_ATTR_RO(device);
1385
Dan Williams6ca72082016-04-29 10:33:23 -07001386static ssize_t subsystem_vendor_show(struct device *dev,
1387 struct device_attribute *attr, char *buf)
1388{
1389 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1390
1391 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1392}
1393static DEVICE_ATTR_RO(subsystem_vendor);
1394
1395static ssize_t subsystem_rev_id_show(struct device *dev,
1396 struct device_attribute *attr, char *buf)
1397{
1398 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1399
1400 return sprintf(buf, "0x%04x\n",
1401 be16_to_cpu(dcr->subsystem_revision_id));
1402}
1403static DEVICE_ATTR_RO(subsystem_rev_id);
1404
1405static ssize_t subsystem_device_show(struct device *dev,
1406 struct device_attribute *attr, char *buf)
1407{
1408 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1409
1410 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1411}
1412static DEVICE_ATTR_RO(subsystem_device);
1413
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001414static int num_nvdimm_formats(struct nvdimm *nvdimm)
1415{
1416 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1417 int formats = 0;
1418
1419 if (nfit_mem->memdev_pmem)
1420 formats++;
1421 if (nfit_mem->memdev_bdw)
1422 formats++;
1423 return formats;
1424}
1425
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001426static ssize_t format_show(struct device *dev,
1427 struct device_attribute *attr, char *buf)
1428{
1429 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1430
Dan Williams1bcbf422016-06-29 11:19:32 -07001431 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001432}
1433static DEVICE_ATTR_RO(format);
1434
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001435static ssize_t format1_show(struct device *dev,
1436 struct device_attribute *attr, char *buf)
1437{
1438 u32 handle;
1439 ssize_t rc = -ENXIO;
1440 struct nfit_mem *nfit_mem;
1441 struct nfit_memdev *nfit_memdev;
1442 struct acpi_nfit_desc *acpi_desc;
1443 struct nvdimm *nvdimm = to_nvdimm(dev);
1444 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1445
1446 nfit_mem = nvdimm_provider_data(nvdimm);
1447 acpi_desc = nfit_mem->acpi_desc;
1448 handle = to_nfit_memdev(dev)->device_handle;
1449
1450 /* assumes DIMMs have at most 2 published interface codes */
1451 mutex_lock(&acpi_desc->init_mutex);
1452 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1453 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1454 struct nfit_dcr *nfit_dcr;
1455
1456 if (memdev->device_handle != handle)
1457 continue;
1458
1459 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1460 if (nfit_dcr->dcr->region_index != memdev->region_index)
1461 continue;
1462 if (nfit_dcr->dcr->code == dcr->code)
1463 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001464 rc = sprintf(buf, "0x%04x\n",
1465 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001466 break;
1467 }
1468 if (rc != ENXIO)
1469 break;
1470 }
1471 mutex_unlock(&acpi_desc->init_mutex);
1472 return rc;
1473}
1474static DEVICE_ATTR_RO(format1);
1475
1476static ssize_t formats_show(struct device *dev,
1477 struct device_attribute *attr, char *buf)
1478{
1479 struct nvdimm *nvdimm = to_nvdimm(dev);
1480
1481 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1482}
1483static DEVICE_ATTR_RO(formats);
1484
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001485static ssize_t serial_show(struct device *dev,
1486 struct device_attribute *attr, char *buf)
1487{
1488 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1489
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001490 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001491}
1492static DEVICE_ATTR_RO(serial);
1493
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001494static ssize_t family_show(struct device *dev,
1495 struct device_attribute *attr, char *buf)
1496{
1497 struct nvdimm *nvdimm = to_nvdimm(dev);
1498 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1499
1500 if (nfit_mem->family < 0)
1501 return -ENXIO;
1502 return sprintf(buf, "%d\n", nfit_mem->family);
1503}
1504static DEVICE_ATTR_RO(family);
1505
1506static ssize_t dsm_mask_show(struct device *dev,
1507 struct device_attribute *attr, char *buf)
1508{
1509 struct nvdimm *nvdimm = to_nvdimm(dev);
1510 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1511
1512 if (nfit_mem->family < 0)
1513 return -ENXIO;
1514 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1515}
1516static DEVICE_ATTR_RO(dsm_mask);
1517
Dan Williams58138822015-06-23 20:08:34 -04001518static ssize_t flags_show(struct device *dev,
1519 struct device_attribute *attr, char *buf)
1520{
1521 u16 flags = to_nfit_memdev(dev)->flags;
1522
Dan Williamsffab9382017-04-13 15:05:30 -07001523 return sprintf(buf, "%s%s%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001524 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1525 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1526 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001527 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Dan Williamsffab9382017-04-13 15:05:30 -07001528 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1529 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1530 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
Dan Williams58138822015-06-23 20:08:34 -04001531}
1532static DEVICE_ATTR_RO(flags);
1533
Toshi Kani38a879b2016-04-25 15:34:59 -06001534static ssize_t id_show(struct device *dev,
1535 struct device_attribute *attr, char *buf)
1536{
1537 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1538
1539 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1540 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1541 be16_to_cpu(dcr->vendor_id),
1542 dcr->manufacturing_location,
1543 be16_to_cpu(dcr->manufacturing_date),
1544 be32_to_cpu(dcr->serial_number));
1545 else
1546 return sprintf(buf, "%04x-%08x\n",
1547 be16_to_cpu(dcr->vendor_id),
1548 be32_to_cpu(dcr->serial_number));
1549}
1550static DEVICE_ATTR_RO(id);
1551
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001552static struct attribute *acpi_nfit_dimm_attributes[] = {
1553 &dev_attr_handle.attr,
1554 &dev_attr_phys_id.attr,
1555 &dev_attr_vendor.attr,
1556 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001557 &dev_attr_rev_id.attr,
1558 &dev_attr_subsystem_vendor.attr,
1559 &dev_attr_subsystem_device.attr,
1560 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001561 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001562 &dev_attr_formats.attr,
1563 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001564 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001565 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001566 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001567 &dev_attr_family.attr,
1568 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001569 NULL,
1570};
1571
1572static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1573 struct attribute *a, int n)
1574{
1575 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001576 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001577
Dan Williams14999342017-04-13 19:46:36 -07001578 if (!to_nfit_dcr(dev)) {
1579 /* Without a dcr only the memdev attributes can be surfaced */
1580 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1581 || a == &dev_attr_flags.attr
1582 || a == &dev_attr_family.attr
1583 || a == &dev_attr_dsm_mask.attr)
1584 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001585 return 0;
Dan Williams14999342017-04-13 19:46:36 -07001586 }
1587
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001588 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1589 return 0;
1590 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001591}
1592
Arvind Yadav5e937462017-06-22 15:44:41 +05301593static const struct attribute_group acpi_nfit_dimm_attribute_group = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001594 .name = "nfit",
1595 .attrs = acpi_nfit_dimm_attributes,
1596 .is_visible = acpi_nfit_dimm_attr_visible,
1597};
1598
1599static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001600 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001601 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001602 &acpi_nfit_dimm_attribute_group,
1603 NULL,
1604};
1605
1606static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1607 u32 device_handle)
1608{
1609 struct nfit_mem *nfit_mem;
1610
1611 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1612 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1613 return nfit_mem->nvdimm;
1614
1615 return NULL;
1616}
1617
Dan Williams231bf112016-08-22 19:23:25 -07001618void __acpi_nvdimm_notify(struct device *dev, u32 event)
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001619{
1620 struct nfit_mem *nfit_mem;
1621 struct acpi_nfit_desc *acpi_desc;
1622
Johannes Thumshirnb8147352018-03-02 13:20:49 +01001623 dev_dbg(dev->parent, "%s: event: %d\n", dev_name(dev),
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001624 event);
1625
1626 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1627 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1628 event);
1629 return;
1630 }
1631
1632 acpi_desc = dev_get_drvdata(dev->parent);
1633 if (!acpi_desc)
1634 return;
1635
1636 /*
1637 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1638 * is still valid.
1639 */
1640 nfit_mem = dev_get_drvdata(dev);
1641 if (nfit_mem && nfit_mem->flags_attr)
1642 sysfs_notify_dirent(nfit_mem->flags_attr);
1643}
Dan Williams231bf112016-08-22 19:23:25 -07001644EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001645
1646static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1647{
1648 struct acpi_device *adev = data;
1649 struct device *dev = &adev->dev;
1650
1651 device_lock(dev->parent);
1652 __acpi_nvdimm_notify(dev, event);
1653 device_unlock(dev->parent);
1654}
1655
Dan Williams466d1492018-03-28 10:44:50 -07001656static bool acpi_nvdimm_has_method(struct acpi_device *adev, char *method)
1657{
1658 acpi_handle handle;
1659 acpi_status status;
1660
1661 status = acpi_get_handle(adev->handle, method, &handle);
1662
1663 if (ACPI_SUCCESS(status))
1664 return true;
1665 return false;
1666}
1667
Dan Williams62232e452015-06-08 14:27:06 -04001668static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1669 struct nfit_mem *nfit_mem, u32 device_handle)
1670{
1671 struct acpi_device *adev, *adev_dimm;
1672 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001673 unsigned long dsm_mask;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001674 const guid_t *guid;
Linda Knippers60e95f432015-07-22 16:17:22 -04001675 int i;
Linda Knippersba650cf2017-03-07 16:35:13 -05001676 int family = -1;
Dan Williams62232e452015-06-08 14:27:06 -04001677
Dan Williamse3654ec2016-04-28 16:17:07 -07001678 /* nfit test assumes 1:1 relationship between commands and dsms */
1679 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001680 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001681 adev = to_acpi_dev(acpi_desc);
1682 if (!adev)
1683 return 0;
1684
1685 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1686 nfit_mem->adev = adev_dimm;
1687 if (!adev_dimm) {
1688 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1689 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001690 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001691 }
1692
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001693 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1694 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1695 dev_err(dev, "%s: notification registration failed\n",
1696 dev_name(&adev_dimm->dev));
1697 return -ENXIO;
1698 }
Dan Williamsadf68952017-11-30 19:42:52 -08001699 /*
1700 * Record nfit_mem for the notification path to track back to
1701 * the nfit sysfs attributes for this dimm device object.
1702 */
1703 dev_set_drvdata(&adev_dimm->dev, nfit_mem);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001704
Dan Williams31eca762016-04-28 16:23:43 -07001705 /*
stuart hayese02fb722016-05-26 11:38:41 -05001706 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001707 * different command sets. Note, that checking for function0 (bit0)
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001708 * tells us if any commands are reachable through this GUID.
Dan Williams31eca762016-04-28 16:23:43 -07001709 */
Dan Williams11e14272017-10-20 15:39:43 -07001710 for (i = 0; i <= NVDIMM_FAMILY_MAX; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001711 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Linda Knippersba650cf2017-03-07 16:35:13 -05001712 if (family < 0 || i == default_dsm_family)
1713 family = i;
Dan Williams31eca762016-04-28 16:23:43 -07001714
1715 /* limit the supported commands to those that are publicly documented */
Linda Knippersba650cf2017-03-07 16:35:13 -05001716 nfit_mem->family = family;
Linda Knippers095ab4b2017-03-07 16:35:12 -05001717 if (override_dsm_mask && !disable_vendor_specific)
1718 dsm_mask = override_dsm_mask;
1719 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams11e14272017-10-20 15:39:43 -07001720 dsm_mask = NVDIMM_INTEL_CMDMASK;
Dan Williams87554092016-04-28 18:01:20 -07001721 if (disable_vendor_specific)
1722 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001723 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001724 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001725 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001726 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001727 if (disable_vendor_specific)
1728 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001729 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1730 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001731 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001732 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001733 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001734 /* DSMs are optional, continue loading the driver... */
1735 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001736 }
1737
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001738 guid = to_nfit_uuid(nfit_mem->family);
Dan Williams31eca762016-04-28 16:23:43 -07001739 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Dan Williams11e14272017-10-20 15:39:43 -07001740 if (acpi_check_dsm(adev_dimm->handle, guid,
1741 nfit_dsm_revid(nfit_mem->family, i),
1742 1ULL << i))
Dan Williams62232e452015-06-08 14:27:06 -04001743 set_bit(i, &nfit_mem->dsm_mask);
1744
Dan Williams466d1492018-03-28 10:44:50 -07001745 if (acpi_nvdimm_has_method(adev_dimm, "_LSI")
1746 && acpi_nvdimm_has_method(adev_dimm, "_LSR")) {
Dan Williams4b27db72017-09-24 09:57:34 -07001747 dev_dbg(dev, "%s: has _LSR\n", dev_name(&adev_dimm->dev));
Dan Williams466d1492018-03-28 10:44:50 -07001748 nfit_mem->has_lsr = true;
Dan Williams4b27db72017-09-24 09:57:34 -07001749 }
1750
Dan Williams466d1492018-03-28 10:44:50 -07001751 if (nfit_mem->has_lsr && acpi_nvdimm_has_method(adev_dimm, "_LSW")) {
Dan Williams4b27db72017-09-24 09:57:34 -07001752 dev_dbg(dev, "%s: has _LSW\n", dev_name(&adev_dimm->dev));
Dan Williams466d1492018-03-28 10:44:50 -07001753 nfit_mem->has_lsw = true;
Dan Williams4b27db72017-09-24 09:57:34 -07001754 }
1755
Linda Knippers60e95f432015-07-22 16:17:22 -04001756 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001757}
1758
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001759static void shutdown_dimm_notify(void *data)
1760{
1761 struct acpi_nfit_desc *acpi_desc = data;
1762 struct nfit_mem *nfit_mem;
1763
1764 mutex_lock(&acpi_desc->init_mutex);
1765 /*
1766 * Clear out the nfit_mem->flags_attr and shut down dimm event
1767 * notifications.
1768 */
1769 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williams231bf112016-08-22 19:23:25 -07001770 struct acpi_device *adev_dimm = nfit_mem->adev;
1771
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001772 if (nfit_mem->flags_attr) {
1773 sysfs_put(nfit_mem->flags_attr);
1774 nfit_mem->flags_attr = NULL;
1775 }
Dan Williamsadf68952017-11-30 19:42:52 -08001776 if (adev_dimm) {
Dan Williams231bf112016-08-22 19:23:25 -07001777 acpi_remove_notify_handler(adev_dimm->handle,
1778 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
Dan Williamsadf68952017-11-30 19:42:52 -08001779 dev_set_drvdata(&adev_dimm->dev, NULL);
1780 }
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001781 }
1782 mutex_unlock(&acpi_desc->init_mutex);
1783}
1784
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001785static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1786{
1787 struct nfit_mem *nfit_mem;
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001788 int dimm_count = 0, rc;
1789 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001790
1791 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001792 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001793 unsigned long flags = 0, cmd_mask;
Dan Williamscaa603a2017-04-14 10:27:11 -07001794 struct nfit_memdev *nfit_memdev;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001795 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001796 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001797
1798 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1799 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1800 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001801 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001802 continue;
1803 }
1804
1805 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
Dan Williams8f078b32017-05-04 14:01:24 -07001806 set_bit(NDD_ALIASING, &flags);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001807
Dan Williamscaa603a2017-04-14 10:27:11 -07001808 /* collate flags across all memdevs for this dimm */
1809 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1810 struct acpi_nfit_memory_map *dimm_memdev;
1811
1812 dimm_memdev = __to_nfit_memdev(nfit_mem);
1813 if (dimm_memdev->device_handle
1814 != nfit_memdev->memdev->device_handle)
1815 continue;
1816 dimm_memdev->flags |= nfit_memdev->memdev->flags;
1817 }
1818
Dan Williams58138822015-06-23 20:08:34 -04001819 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001820 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams8f078b32017-05-04 14:01:24 -07001821 set_bit(NDD_UNARMED, &flags);
Dan Williams58138822015-06-23 20:08:34 -04001822
Dan Williams62232e452015-06-08 14:27:06 -04001823 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1824 if (rc)
1825 continue;
1826
Dan Williamse3654ec2016-04-28 16:17:07 -07001827 /*
Dan Williams31eca762016-04-28 16:23:43 -07001828 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1829 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1830 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001831 */
Dan Williams31eca762016-04-28 16:23:43 -07001832 cmd_mask = 1UL << ND_CMD_CALL;
Dan Williamsb9b15042017-10-29 12:13:07 -07001833 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1834 /*
1835 * These commands have a 1:1 correspondence
1836 * between DSM payload and libnvdimm ioctl
1837 * payload format.
1838 */
1839 cmd_mask |= nfit_mem->dsm_mask & NVDIMM_STANDARD_CMDMASK;
1840 }
Dan Williams31eca762016-04-28 16:23:43 -07001841
Dan Williams466d1492018-03-28 10:44:50 -07001842 if (nfit_mem->has_lsr) {
Dan Williams4b27db72017-09-24 09:57:34 -07001843 set_bit(ND_CMD_GET_CONFIG_SIZE, &cmd_mask);
Dan Williams4b27db72017-09-24 09:57:34 -07001844 set_bit(ND_CMD_GET_CONFIG_DATA, &cmd_mask);
Dan Williams466d1492018-03-28 10:44:50 -07001845 }
Dan Williams4b27db72017-09-24 09:57:34 -07001846 if (nfit_mem->has_lsw)
1847 set_bit(ND_CMD_SET_CONFIG_DATA, &cmd_mask);
1848
Dan Williamse5ae3b22016-06-07 17:00:04 -07001849 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1850 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001851 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001852 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001853 flags, cmd_mask, flush ? flush->hint_count : 0,
1854 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001855 if (!nvdimm)
1856 return -ENOMEM;
1857
1858 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001859 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001860
1861 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1862 continue;
1863
Dan Williams14999342017-04-13 19:46:36 -07001864 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001865 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001866 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1867 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1868 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Dan Williams14999342017-04-13 19:46:36 -07001869 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1870 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
Dan Williams58138822015-06-23 20:08:34 -04001871
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001872 }
1873
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001874 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1875 if (rc)
1876 return rc;
1877
1878 /*
1879 * Now that dimms are successfully registered, and async registration
1880 * is flushed, attempt to enable event notification.
1881 */
1882 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1883 struct kernfs_node *nfit_kernfs;
1884
1885 nvdimm = nfit_mem->nvdimm;
Toshi Kani23fbd7c2018-02-02 14:00:36 -07001886 if (!nvdimm)
1887 continue;
1888
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001889 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1890 if (nfit_kernfs)
1891 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1892 "flags");
1893 sysfs_put(nfit_kernfs);
1894 if (!nfit_mem->flags_attr)
1895 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1896 nvdimm_name(nvdimm));
1897 }
1898
1899 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1900 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001901}
1902
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001903/*
1904 * These constants are private because there are no kernel consumers of
1905 * these commands.
1906 */
1907enum nfit_aux_cmds {
1908 NFIT_CMD_TRANSLATE_SPA = 5,
1909 NFIT_CMD_ARS_INJECT_SET = 7,
1910 NFIT_CMD_ARS_INJECT_CLEAR = 8,
1911 NFIT_CMD_ARS_INJECT_GET = 9,
1912};
1913
Dan Williams62232e452015-06-08 14:27:06 -04001914static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1915{
1916 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001917 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
Dan Williams62232e452015-06-08 14:27:06 -04001918 struct acpi_device *adev;
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001919 unsigned long dsm_mask;
Dan Williams62232e452015-06-08 14:27:06 -04001920 int i;
1921
Dan Williamse3654ec2016-04-28 16:17:07 -07001922 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Yasunori Gotob37b3fd2017-09-22 16:47:40 +09001923 nd_desc->bus_dsm_mask = acpi_desc->bus_nfit_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001924 adev = to_acpi_dev(acpi_desc);
1925 if (!adev)
1926 return;
1927
Dan Williamsd4f32362016-03-03 16:08:54 -08001928 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Andy Shevchenko94116f82017-06-05 19:40:46 +03001929 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001930 set_bit(i, &nd_desc->cmd_mask);
Jerry Hoemann37d74842017-06-30 20:41:24 -07001931 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001932
1933 dsm_mask =
1934 (1 << ND_CMD_ARS_CAP) |
1935 (1 << ND_CMD_ARS_START) |
1936 (1 << ND_CMD_ARS_STATUS) |
1937 (1 << ND_CMD_CLEAR_ERROR) |
1938 (1 << NFIT_CMD_TRANSLATE_SPA) |
1939 (1 << NFIT_CMD_ARS_INJECT_SET) |
1940 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
1941 (1 << NFIT_CMD_ARS_INJECT_GET);
1942 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1943 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1944 set_bit(i, &nd_desc->bus_dsm_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001945}
1946
Dan Williams1f7df6f2015-06-09 20:13:14 -04001947static ssize_t range_index_show(struct device *dev,
1948 struct device_attribute *attr, char *buf)
1949{
1950 struct nd_region *nd_region = to_nd_region(dev);
1951 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1952
1953 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1954}
1955static DEVICE_ATTR_RO(range_index);
1956
Dan Williamsa15797f2017-08-31 12:53:36 -07001957static ssize_t ecc_unit_size_show(struct device *dev,
1958 struct device_attribute *attr, char *buf)
1959{
1960 struct nd_region *nd_region = to_nd_region(dev);
1961 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1962
1963 return sprintf(buf, "%d\n", nfit_spa->clear_err_unit);
1964}
1965static DEVICE_ATTR_RO(ecc_unit_size);
1966
Dan Williams1f7df6f2015-06-09 20:13:14 -04001967static struct attribute *acpi_nfit_region_attributes[] = {
1968 &dev_attr_range_index.attr,
Dan Williamsa15797f2017-08-31 12:53:36 -07001969 &dev_attr_ecc_unit_size.attr,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001970 NULL,
1971};
1972
Arvind Yadav5e937462017-06-22 15:44:41 +05301973static const struct attribute_group acpi_nfit_region_attribute_group = {
Dan Williams1f7df6f2015-06-09 20:13:14 -04001974 .name = "nfit",
1975 .attrs = acpi_nfit_region_attributes,
1976};
1977
1978static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1979 &nd_region_attribute_group,
1980 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001981 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001982 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001983 &acpi_nfit_region_attribute_group,
1984 NULL,
1985};
1986
Dan Williamseaf96152015-05-01 13:11:27 -04001987/* enough info to uniquely specify an interleave set */
1988struct nfit_set_info {
1989 struct nfit_set_info_map {
1990 u64 region_offset;
1991 u32 serial_number;
1992 u32 pad;
1993 } mapping[0];
1994};
1995
Dan Williamsc12c48c2017-06-04 10:59:15 +09001996struct nfit_set_info2 {
1997 struct nfit_set_info_map2 {
1998 u64 region_offset;
1999 u32 serial_number;
2000 u16 vendor_id;
2001 u16 manufacturing_date;
2002 u8 manufacturing_location;
2003 u8 reserved[31];
2004 } mapping[0];
2005};
2006
Dan Williamseaf96152015-05-01 13:11:27 -04002007static size_t sizeof_nfit_set_info(int num_mappings)
2008{
2009 return sizeof(struct nfit_set_info)
2010 + num_mappings * sizeof(struct nfit_set_info_map);
2011}
2012
Dan Williamsc12c48c2017-06-04 10:59:15 +09002013static size_t sizeof_nfit_set_info2(int num_mappings)
2014{
2015 return sizeof(struct nfit_set_info2)
2016 + num_mappings * sizeof(struct nfit_set_info_map2);
2017}
2018
Dan Williams86ef58a2017-02-28 18:32:48 -08002019static int cmp_map_compat(const void *m0, const void *m1)
Dan Williamseaf96152015-05-01 13:11:27 -04002020{
2021 const struct nfit_set_info_map *map0 = m0;
2022 const struct nfit_set_info_map *map1 = m1;
2023
2024 return memcmp(&map0->region_offset, &map1->region_offset,
2025 sizeof(u64));
2026}
2027
Dan Williams86ef58a2017-02-28 18:32:48 -08002028static int cmp_map(const void *m0, const void *m1)
2029{
2030 const struct nfit_set_info_map *map0 = m0;
2031 const struct nfit_set_info_map *map1 = m1;
2032
Dan Williamsb03b99a2017-03-27 21:53:38 -07002033 if (map0->region_offset < map1->region_offset)
2034 return -1;
2035 else if (map0->region_offset > map1->region_offset)
2036 return 1;
2037 return 0;
Dan Williams86ef58a2017-02-28 18:32:48 -08002038}
2039
Dan Williamsc12c48c2017-06-04 10:59:15 +09002040static int cmp_map2(const void *m0, const void *m1)
2041{
2042 const struct nfit_set_info_map2 *map0 = m0;
2043 const struct nfit_set_info_map2 *map1 = m1;
2044
2045 if (map0->region_offset < map1->region_offset)
2046 return -1;
2047 else if (map0->region_offset > map1->region_offset)
2048 return 1;
2049 return 0;
2050}
2051
Dan Williamseaf96152015-05-01 13:11:27 -04002052/* Retrieve the nth entry referencing this spa */
2053static struct acpi_nfit_memory_map *memdev_from_spa(
2054 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
2055{
2056 struct nfit_memdev *nfit_memdev;
2057
2058 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
2059 if (nfit_memdev->memdev->range_index == range_index)
2060 if (n-- == 0)
2061 return nfit_memdev->memdev;
2062 return NULL;
2063}
2064
2065static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
2066 struct nd_region_desc *ndr_desc,
2067 struct acpi_nfit_system_address *spa)
2068{
Dan Williamseaf96152015-05-01 13:11:27 -04002069 struct device *dev = acpi_desc->dev;
2070 struct nd_interleave_set *nd_set;
2071 u16 nr = ndr_desc->num_mappings;
Dan Williamsc12c48c2017-06-04 10:59:15 +09002072 struct nfit_set_info2 *info2;
Dan Williamseaf96152015-05-01 13:11:27 -04002073 struct nfit_set_info *info;
Dan Williams8f2bc242017-06-06 11:39:30 -07002074 int i;
Dan Williamseaf96152015-05-01 13:11:27 -04002075
Dan Williamsfaec6f82017-06-06 11:10:51 -07002076 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
2077 if (!nd_set)
2078 return -ENOMEM;
2079 ndr_desc->nd_set = nd_set;
2080 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
2081
Dan Williamseaf96152015-05-01 13:11:27 -04002082 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
2083 if (!info)
2084 return -ENOMEM;
Dan Williamsc12c48c2017-06-04 10:59:15 +09002085
2086 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
2087 if (!info2)
2088 return -ENOMEM;
2089
Dan Williamseaf96152015-05-01 13:11:27 -04002090 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07002091 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04002092 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williamsc12c48c2017-06-04 10:59:15 +09002093 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07002094 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04002095 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
2096 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
2097 spa->range_index, i);
Dan Williamsdcb79b12017-08-07 14:27:57 -07002098 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
Dan Williamseaf96152015-05-01 13:11:27 -04002099
2100 if (!memdev || !nfit_mem->dcr) {
2101 dev_err(dev, "%s: failed to find DCR\n", __func__);
2102 return -ENODEV;
2103 }
2104
2105 map->region_offset = memdev->region_offset;
Dan Williamsdcb79b12017-08-07 14:27:57 -07002106 map->serial_number = dcr->serial_number;
Dan Williamsc12c48c2017-06-04 10:59:15 +09002107
2108 map2->region_offset = memdev->region_offset;
Dan Williamsdcb79b12017-08-07 14:27:57 -07002109 map2->serial_number = dcr->serial_number;
2110 map2->vendor_id = dcr->vendor_id;
2111 map2->manufacturing_date = dcr->manufacturing_date;
2112 map2->manufacturing_location = dcr->manufacturing_location;
Dan Williamseaf96152015-05-01 13:11:27 -04002113 }
2114
Dan Williamsc12c48c2017-06-04 10:59:15 +09002115 /* v1.1 namespaces */
Dan Williamseaf96152015-05-01 13:11:27 -04002116 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2117 cmp_map, NULL);
Dan Williamsc12c48c2017-06-04 10:59:15 +09002118 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
Dan Williams86ef58a2017-02-28 18:32:48 -08002119
Dan Williamsc12c48c2017-06-04 10:59:15 +09002120 /* v1.2 namespaces */
2121 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
2122 cmp_map2, NULL);
2123 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
2124
2125 /* support v1.1 namespaces created with the wrong sort order */
Dan Williams86ef58a2017-02-28 18:32:48 -08002126 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
2127 cmp_map_compat, NULL);
2128 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
2129
Dan Williams401c0a12017-08-04 17:20:16 -07002130 /* record the result of the sort for the mapping position */
2131 for (i = 0; i < nr; i++) {
2132 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
2133 int j;
2134
2135 for (j = 0; j < nr; j++) {
2136 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
2137 struct nvdimm *nvdimm = mapping->nvdimm;
2138 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
Dan Williamsdcb79b12017-08-07 14:27:57 -07002139 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
Dan Williams401c0a12017-08-04 17:20:16 -07002140
Dan Williamsdcb79b12017-08-07 14:27:57 -07002141 if (map2->serial_number == dcr->serial_number &&
2142 map2->vendor_id == dcr->vendor_id &&
2143 map2->manufacturing_date == dcr->manufacturing_date &&
Dan Williams401c0a12017-08-04 17:20:16 -07002144 map2->manufacturing_location
Dan Williamsdcb79b12017-08-07 14:27:57 -07002145 == dcr->manufacturing_location) {
Dan Williams401c0a12017-08-04 17:20:16 -07002146 mapping->position = i;
2147 break;
2148 }
2149 }
2150 }
2151
Dan Williamseaf96152015-05-01 13:11:27 -04002152 ndr_desc->nd_set = nd_set;
2153 devm_kfree(dev, info);
Dan Williamsc12c48c2017-06-04 10:59:15 +09002154 devm_kfree(dev, info2);
Dan Williamseaf96152015-05-01 13:11:27 -04002155
2156 return 0;
2157}
2158
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002159static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
2160{
2161 struct acpi_nfit_interleave *idt = mmio->idt;
2162 u32 sub_line_offset, line_index, line_offset;
2163 u64 line_no, table_skip_count, table_offset;
2164
2165 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
2166 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
2167 line_offset = idt->line_offset[line_index]
2168 * mmio->line_size;
2169 table_offset = table_skip_count * mmio->table_size;
2170
2171 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
2172}
2173
Ross Zwislerde4a1962015-08-20 16:27:38 -06002174static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002175{
2176 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2177 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06002178 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002179
2180 if (mmio->num_lines)
2181 offset = to_interleave_offset(offset, mmio);
2182
Ross Zwisler68202c92016-07-29 14:59:12 -06002183 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002184}
2185
2186static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
2187 resource_size_t dpa, unsigned int len, unsigned int write)
2188{
2189 u64 cmd, offset;
2190 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
2191
2192 enum {
2193 BCW_OFFSET_MASK = (1ULL << 48)-1,
2194 BCW_LEN_SHIFT = 48,
2195 BCW_LEN_MASK = (1ULL << 8) - 1,
2196 BCW_CMD_SHIFT = 56,
2197 };
2198
2199 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
2200 len = len >> L1_CACHE_SHIFT;
2201 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
2202 cmd |= ((u64) write) << BCW_CMD_SHIFT;
2203
2204 offset = nfit_blk->cmd_offset + mmio->size * bw;
2205 if (mmio->num_lines)
2206 offset = to_interleave_offset(offset, mmio);
2207
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002208 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07002209 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002210
Dan Williamsaef25332016-02-12 17:01:11 -08002211 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002212 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002213}
2214
2215static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
2216 resource_size_t dpa, void *iobuf, size_t len, int rw,
2217 unsigned int lane)
2218{
2219 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2220 unsigned int copied = 0;
2221 u64 base_offset;
2222 int rc;
2223
2224 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
2225 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002226 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2227 while (len) {
2228 unsigned int c;
2229 u64 offset;
2230
2231 if (mmio->num_lines) {
2232 u32 line_offset;
2233
2234 offset = to_interleave_offset(base_offset + copied,
2235 mmio);
2236 div_u64_rem(offset, mmio->line_size, &line_offset);
2237 c = min_t(size_t, len, mmio->line_size - line_offset);
2238 } else {
2239 offset = base_offset + nfit_blk->bdw_offset;
2240 c = len;
2241 }
2242
2243 if (rw)
Dan Williams0aed55a2017-05-29 12:22:50 -07002244 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002245 else {
Dan Williamsaef25332016-02-12 17:01:11 -08002246 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Robin Murphy5deb67f2017-08-31 12:27:09 +01002247 arch_invalidate_pmem((void __force *)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002248 mmio->addr.aperture + offset, c);
2249
Dan Williams6abccd12017-01-13 14:14:23 -08002250 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002251 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002252
2253 copied += c;
2254 len -= c;
2255 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002256
2257 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07002258 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002259
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002260 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2261 return rc;
2262}
2263
2264static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2265 resource_size_t dpa, void *iobuf, u64 len, int rw)
2266{
2267 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2268 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2269 struct nd_region *nd_region = nfit_blk->nd_region;
2270 unsigned int lane, copied = 0;
2271 int rc = 0;
2272
2273 lane = nd_region_acquire_lane(nd_region);
2274 while (len) {
2275 u64 c = min(len, mmio->size);
2276
2277 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2278 iobuf + copied, c, rw, lane);
2279 if (rc)
2280 break;
2281
2282 copied += c;
2283 len -= c;
2284 }
2285 nd_region_release_lane(nd_region, lane);
2286
2287 return rc;
2288}
2289
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002290static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2291 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2292{
2293 if (idt) {
2294 mmio->num_lines = idt->line_count;
2295 mmio->line_size = idt->line_size;
2296 if (interleave_ways == 0)
2297 return -ENXIO;
2298 mmio->table_size = mmio->num_lines * interleave_ways
2299 * mmio->line_size;
2300 }
2301
2302 return 0;
2303}
2304
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002305static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2306 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2307{
2308 struct nd_cmd_dimm_flags flags;
2309 int rc;
2310
2311 memset(&flags, 0, sizeof(flags));
2312 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08002313 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002314
2315 if (rc >= 0 && flags.status == 0)
2316 nfit_blk->dimm_flags = flags.flags;
2317 else if (rc == -ENOTTY) {
2318 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08002319 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002320 rc = 0;
2321 } else
2322 rc = -ENXIO;
2323
2324 return rc;
2325}
2326
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002327static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2328 struct device *dev)
2329{
2330 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002331 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2332 struct nfit_blk_mmio *mmio;
2333 struct nfit_blk *nfit_blk;
2334 struct nfit_mem *nfit_mem;
2335 struct nvdimm *nvdimm;
2336 int rc;
2337
2338 nvdimm = nd_blk_region_to_dimm(ndbr);
2339 nfit_mem = nvdimm_provider_data(nvdimm);
2340 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +01002341 dev_dbg(dev, "missing%s%s%s\n",
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002342 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04002343 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2344 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002345 return -ENXIO;
2346 }
2347
2348 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2349 if (!nfit_blk)
2350 return -ENOMEM;
2351 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2352 nfit_blk->nd_region = to_nd_region(dev);
2353
2354 /* map block aperture memory */
2355 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2356 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07002357 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
Dan Williamsca6a4652017-01-13 20:36:58 -08002358 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002359 if (!mmio->addr.base) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +01002360 dev_dbg(dev, "%s failed to map bdw\n",
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002361 nvdimm_name(nvdimm));
2362 return -ENOMEM;
2363 }
2364 mmio->size = nfit_mem->bdw->size;
2365 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2366 mmio->idt = nfit_mem->idt_bdw;
2367 mmio->spa = nfit_mem->spa_bdw;
2368 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2369 nfit_mem->memdev_bdw->interleave_ways);
2370 if (rc) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +01002371 dev_dbg(dev, "%s failed to init bdw interleave\n",
2372 nvdimm_name(nvdimm));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002373 return rc;
2374 }
2375
2376 /* map block control memory */
2377 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2378 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2379 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07002380 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2381 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002382 if (!mmio->addr.base) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +01002383 dev_dbg(dev, "%s failed to map dcr\n",
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002384 nvdimm_name(nvdimm));
2385 return -ENOMEM;
2386 }
2387 mmio->size = nfit_mem->dcr->window_size;
2388 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2389 mmio->idt = nfit_mem->idt_dcr;
2390 mmio->spa = nfit_mem->spa_dcr;
2391 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2392 nfit_mem->memdev_dcr->interleave_ways);
2393 if (rc) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +01002394 dev_dbg(dev, "%s failed to init dcr interleave\n",
2395 nvdimm_name(nvdimm));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002396 return rc;
2397 }
2398
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002399 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2400 if (rc < 0) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +01002401 dev_dbg(dev, "%s failed get DIMM flags\n",
2402 nvdimm_name(nvdimm));
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002403 return rc;
2404 }
2405
Dan Williamsf284a4f2016-07-07 19:44:50 -07002406 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002407 dev_warn(dev, "unable to guarantee persistence of writes\n");
2408
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002409 if (mmio->line_size == 0)
2410 return 0;
2411
2412 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2413 + 8 > mmio->line_size) {
2414 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2415 return -ENXIO;
2416 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2417 + 8 > mmio->line_size) {
2418 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2419 return -ENXIO;
2420 }
2421
2422 return 0;
2423}
2424
Dan Williamsaef25332016-02-12 17:01:11 -08002425static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002426 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002427{
Dan Williamsaef25332016-02-12 17:01:11 -08002428 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002429 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08002430 int cmd_rc, rc;
2431
Dan Williams1cf03c02016-02-17 13:01:23 -08002432 cmd->address = spa->address;
2433 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08002434 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2435 sizeof(*cmd), &cmd_rc);
2436 if (rc < 0)
2437 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002438 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002439}
2440
Dan Williams1cf03c02016-02-17 13:01:23 -08002441static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002442{
2443 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002444 int cmd_rc;
2445 struct nd_cmd_ars_start ars_start;
2446 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2447 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002448
Dan Williams1cf03c02016-02-17 13:01:23 -08002449 memset(&ars_start, 0, sizeof(ars_start));
2450 ars_start.address = spa->address;
2451 ars_start.length = spa->length;
Toshi Kani80790032017-06-29 20:41:30 -06002452 ars_start.flags = acpi_desc->ars_start_flags;
Dan Williams1cf03c02016-02-17 13:01:23 -08002453 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2454 ars_start.type = ND_ARS_PERSISTENT;
2455 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2456 ars_start.type = ND_ARS_VOLATILE;
2457 else
2458 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07002459
Dan Williams1cf03c02016-02-17 13:01:23 -08002460 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2461 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08002462
Dan Williams1cf03c02016-02-17 13:01:23 -08002463 if (rc < 0)
2464 return rc;
2465 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002466}
2467
Dan Williams1cf03c02016-02-17 13:01:23 -08002468static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07002469{
Dan Williamsaef25332016-02-12 17:01:11 -08002470 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002471 struct nd_cmd_ars_start ars_start;
2472 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2473 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07002474
Dan Williams1cf03c02016-02-17 13:01:23 -08002475 memset(&ars_start, 0, sizeof(ars_start));
2476 ars_start.address = ars_status->restart_address;
2477 ars_start.length = ars_status->restart_length;
2478 ars_start.type = ars_status->type;
Toshi Kani80790032017-06-29 20:41:30 -06002479 ars_start.flags = acpi_desc->ars_start_flags;
Dan Williams1cf03c02016-02-17 13:01:23 -08002480 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2481 sizeof(ars_start), &cmd_rc);
2482 if (rc < 0)
2483 return rc;
2484 return cmd_rc;
2485}
Dan Williamsaef25332016-02-12 17:01:11 -08002486
Dan Williams1cf03c02016-02-17 13:01:23 -08002487static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2488{
2489 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2490 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2491 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08002492
Dan Williams1cf03c02016-02-17 13:01:23 -08002493 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2494 acpi_desc->ars_status_size, &cmd_rc);
2495 if (rc < 0)
2496 return rc;
2497 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002498}
2499
Dan Williams82aa37c2016-12-06 12:45:24 -08002500static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002501 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07002502{
Dan Williams82aa37c2016-12-06 12:45:24 -08002503 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
Vishal Verma0caeef62015-12-24 19:21:43 -07002504 int rc;
2505 u32 i;
2506
Dan Williams82aa37c2016-12-06 12:45:24 -08002507 /*
2508 * First record starts at 44 byte offset from the start of the
2509 * payload.
2510 */
2511 if (ars_status->out_length < 44)
2512 return 0;
Vishal Verma0caeef62015-12-24 19:21:43 -07002513 for (i = 0; i < ars_status->num_records; i++) {
Dan Williams82aa37c2016-12-06 12:45:24 -08002514 /* only process full records */
2515 if (ars_status->out_length
2516 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2517 break;
Dave Jiangaa9ad442017-08-23 12:48:26 -07002518 rc = nvdimm_bus_add_badrange(nvdimm_bus,
Vishal Verma0caeef62015-12-24 19:21:43 -07002519 ars_status->records[i].err_address,
2520 ars_status->records[i].length);
2521 if (rc)
2522 return rc;
2523 }
Dan Williams82aa37c2016-12-06 12:45:24 -08002524 if (i < ars_status->num_records)
2525 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
Vishal Verma0caeef62015-12-24 19:21:43 -07002526
2527 return 0;
2528}
2529
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002530static void acpi_nfit_remove_resource(void *data)
2531{
2532 struct resource *res = data;
2533
2534 remove_resource(res);
2535}
2536
2537static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2538 struct nd_region_desc *ndr_desc)
2539{
2540 struct resource *res, *nd_res = ndr_desc->res;
2541 int is_pmem, ret;
2542
2543 /* No operation if the region is already registered as PMEM */
2544 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2545 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2546 if (is_pmem == REGION_INTERSECTS)
2547 return 0;
2548
2549 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2550 if (!res)
2551 return -ENOMEM;
2552
2553 res->name = "Persistent Memory";
2554 res->start = nd_res->start;
2555 res->end = nd_res->end;
2556 res->flags = IORESOURCE_MEM;
2557 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2558
2559 ret = insert_resource(&iomem_resource, res);
2560 if (ret)
2561 return ret;
2562
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302563 ret = devm_add_action_or_reset(acpi_desc->dev,
2564 acpi_nfit_remove_resource,
2565 res);
2566 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002567 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002568
2569 return 0;
2570}
2571
Dan Williams1f7df6f2015-06-09 20:13:14 -04002572static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002573 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002574 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002575 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002576{
2577 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2578 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002579 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002580 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002581 struct nfit_mem *nfit_mem;
Dan Williams0731de42018-03-21 21:22:34 -07002582 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002583
2584 if (!nvdimm) {
2585 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2586 spa->range_index, memdev->device_handle);
2587 return -ENODEV;
2588 }
2589
Dan Williams44c462e2016-09-19 16:38:50 -07002590 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002591 switch (nfit_spa_type(spa)) {
2592 case NFIT_SPA_PM:
2593 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002594 mapping->start = memdev->address;
2595 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002596 break;
2597 case NFIT_SPA_DCR:
2598 nfit_mem = nvdimm_provider_data(nvdimm);
2599 if (!nfit_mem || !nfit_mem->bdw) {
2600 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2601 spa->range_index, nvdimm_name(nvdimm));
Dan Williams0731de42018-03-21 21:22:34 -07002602 break;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002603 }
2604
Dan Williams0731de42018-03-21 21:22:34 -07002605 mapping->size = nfit_mem->bdw->capacity;
2606 mapping->start = nfit_mem->bdw->start_address;
2607 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams44c462e2016-09-19 16:38:50 -07002608 ndr_desc->mapping = mapping;
Dan Williams0731de42018-03-21 21:22:34 -07002609 ndr_desc->num_mappings = 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002610 ndbr_desc = to_blk_region_desc(ndr_desc);
2611 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002612 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williamsfaec6f82017-06-06 11:10:51 -07002613 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2614 if (rc)
2615 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002616 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2617 ndr_desc);
2618 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002619 return -ENOMEM;
2620 break;
2621 }
2622
2623 return 0;
2624}
2625
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002626static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2627{
2628 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2629 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2630 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2631 nfit_spa_type(spa) == NFIT_SPA_PCD);
2632}
2633
Dan Williamsc9e582a2017-05-29 23:12:19 -07002634static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2635{
2636 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2637 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2638 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2639}
2640
Dan Williams1f7df6f2015-06-09 20:13:14 -04002641static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2642 struct nfit_spa *nfit_spa)
2643{
Dan Williams44c462e2016-09-19 16:38:50 -07002644 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002645 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002646 struct nd_blk_region_desc ndbr_desc;
2647 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002648 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002649 struct nvdimm_bus *nvdimm_bus;
2650 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002651 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002652
Dan Williams1cf03c02016-02-17 13:01:23 -08002653 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002654 return 0;
2655
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002656 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +01002657 dev_dbg(acpi_desc->dev, "detected invalid spa index\n");
Dan Williams1f7df6f2015-06-09 20:13:14 -04002658 return 0;
2659 }
2660
2661 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002662 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002663 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002664 res.start = spa->address;
2665 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002666 ndr_desc = &ndbr_desc.ndr_desc;
2667 ndr_desc->res = &res;
2668 ndr_desc->provider_data = nfit_spa;
2669 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002670 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2671 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2672 spa->proximity_domain);
2673 else
2674 ndr_desc->numa_node = NUMA_NO_NODE;
2675
Dave Jiang06e8ccd2018-01-31 12:45:38 -07002676 if(acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_CACHE_FLUSH)
2677 set_bit(ND_REGION_PERSIST_CACHE, &ndr_desc->flags);
2678
Dave Jiang30e6d7b2018-01-31 12:45:43 -07002679 if (acpi_desc->platform_cap & ACPI_NFIT_CAPABILITY_MEM_FLUSH)
2680 set_bit(ND_REGION_PERSIST_MEMCTRL, &ndr_desc->flags);
2681
Dan Williams1f7df6f2015-06-09 20:13:14 -04002682 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2683 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002684 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002685
2686 if (memdev->range_index != spa->range_index)
2687 continue;
2688 if (count >= ND_MAX_MAPPINGS) {
2689 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2690 spa->range_index, ND_MAX_MAPPINGS);
2691 return -ENXIO;
2692 }
Dan Williams44c462e2016-09-19 16:38:50 -07002693 mapping = &mappings[count++];
2694 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002695 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002696 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002697 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002698 }
2699
Dan Williams44c462e2016-09-19 16:38:50 -07002700 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002701 ndr_desc->num_mappings = count;
2702 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002703 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002704 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002705
Dan Williams1f7df6f2015-06-09 20:13:14 -04002706 nvdimm_bus = acpi_desc->nvdimm_bus;
2707 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002708 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002709 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002710 dev_warn(acpi_desc->dev,
2711 "failed to insert pmem resource to iomem: %d\n",
2712 rc);
Dan Williams48901162016-03-09 17:15:43 -08002713 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002714 }
Dan Williams48901162016-03-09 17:15:43 -08002715
Dan Williams1cf03c02016-02-17 13:01:23 -08002716 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2717 ndr_desc);
2718 if (!nfit_spa->nd_region)
2719 rc = -ENOMEM;
Dan Williamsc9e582a2017-05-29 23:12:19 -07002720 } else if (nfit_spa_is_volatile(spa)) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002721 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2722 ndr_desc);
2723 if (!nfit_spa->nd_region)
2724 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002725 } else if (nfit_spa_is_virtual(spa)) {
2726 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2727 ndr_desc);
2728 if (!nfit_spa->nd_region)
2729 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002730 }
Vishal Verma20985162015-10-27 16:58:27 -06002731
Dan Williams1cf03c02016-02-17 13:01:23 -08002732 out:
2733 if (rc)
2734 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2735 nfit_spa->spa->range_index);
2736 return rc;
2737}
2738
2739static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2740 u32 max_ars)
2741{
2742 struct device *dev = acpi_desc->dev;
2743 struct nd_cmd_ars_status *ars_status;
2744
2745 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2746 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2747 return 0;
2748 }
2749
2750 if (acpi_desc->ars_status)
2751 devm_kfree(dev, acpi_desc->ars_status);
2752 acpi_desc->ars_status = NULL;
2753 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2754 if (!ars_status)
2755 return -ENOMEM;
2756 acpi_desc->ars_status = ars_status;
2757 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002758 return 0;
2759}
2760
Dan Williams1cf03c02016-02-17 13:01:23 -08002761static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2762 struct nfit_spa *nfit_spa)
2763{
2764 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2765 int rc;
2766
2767 if (!nfit_spa->max_ars) {
2768 struct nd_cmd_ars_cap ars_cap;
2769
2770 memset(&ars_cap, 0, sizeof(ars_cap));
2771 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2772 if (rc < 0)
2773 return rc;
2774 nfit_spa->max_ars = ars_cap.max_ars_out;
2775 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2776 /* check that the supported scrub types match the spa type */
2777 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2778 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2779 return -ENOTTY;
2780 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2781 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2782 return -ENOTTY;
2783 }
2784
2785 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2786 return -ENOMEM;
2787
2788 rc = ars_get_status(acpi_desc);
2789 if (rc < 0 && rc != -ENOSPC)
2790 return rc;
2791
Dan Williams82aa37c2016-12-06 12:45:24 -08002792 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
Dan Williams1cf03c02016-02-17 13:01:23 -08002793 return -ENOMEM;
2794
2795 return 0;
2796}
2797
2798static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2799 struct nfit_spa *nfit_spa)
2800{
2801 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2802 unsigned int overflow_retry = scrub_overflow_abort;
2803 u64 init_ars_start = 0, init_ars_len = 0;
2804 struct device *dev = acpi_desc->dev;
2805 unsigned int tmo = scrub_timeout;
2806 int rc;
2807
Vishal Verma37b137f2016-07-23 21:51:42 -07002808 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002809 return;
2810
2811 rc = ars_start(acpi_desc, nfit_spa);
2812 /*
2813 * If we timed out the initial scan we'll still be busy here,
2814 * and will wait another timeout before giving up permanently.
2815 */
2816 if (rc < 0 && rc != -EBUSY)
2817 return;
2818
2819 do {
2820 u64 ars_start, ars_len;
2821
2822 if (acpi_desc->cancel)
2823 break;
2824 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2825 if (rc == -ENOTTY)
2826 break;
2827 if (rc == -EBUSY && !tmo) {
2828 dev_warn(dev, "range %d ars timeout, aborting\n",
2829 spa->range_index);
2830 break;
2831 }
2832
2833 if (rc == -EBUSY) {
2834 /*
2835 * Note, entries may be appended to the list
2836 * while the lock is dropped, but the workqueue
2837 * being active prevents entries being deleted /
2838 * freed.
2839 */
2840 mutex_unlock(&acpi_desc->init_mutex);
2841 ssleep(1);
2842 tmo--;
2843 mutex_lock(&acpi_desc->init_mutex);
2844 continue;
2845 }
2846
2847 /* we got some results, but there are more pending... */
2848 if (rc == -ENOSPC && overflow_retry--) {
2849 if (!init_ars_len) {
2850 init_ars_len = acpi_desc->ars_status->length;
2851 init_ars_start = acpi_desc->ars_status->address;
2852 }
2853 rc = ars_continue(acpi_desc);
2854 }
2855
2856 if (rc < 0) {
2857 dev_warn(dev, "range %d ars continuation failed\n",
2858 spa->range_index);
2859 break;
2860 }
2861
2862 if (init_ars_len) {
2863 ars_start = init_ars_start;
2864 ars_len = init_ars_len;
2865 } else {
2866 ars_start = acpi_desc->ars_status->address;
2867 ars_len = acpi_desc->ars_status->length;
2868 }
2869 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2870 spa->range_index, ars_start, ars_len);
2871 /* notify the region about new poison entries */
2872 nvdimm_region_notify(nfit_spa->nd_region,
2873 NVDIMM_REVALIDATE_POISON);
2874 break;
2875 } while (1);
2876}
2877
2878static void acpi_nfit_scrub(struct work_struct *work)
2879{
2880 struct device *dev;
2881 u64 init_scrub_length = 0;
2882 struct nfit_spa *nfit_spa;
2883 u64 init_scrub_address = 0;
2884 bool init_ars_done = false;
2885 struct acpi_nfit_desc *acpi_desc;
2886 unsigned int tmo = scrub_timeout;
2887 unsigned int overflow_retry = scrub_overflow_abort;
2888
2889 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2890 dev = acpi_desc->dev;
2891
2892 /*
2893 * We scrub in 2 phases. The first phase waits for any platform
2894 * firmware initiated scrubs to complete and then we go search for the
2895 * affected spa regions to mark them scanned. In the second phase we
2896 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002897 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2898 * the first phase, but really only care about running phase 2, where
2899 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002900 */
2901
2902 /* process platform firmware initiated scrubs */
2903 retry:
2904 mutex_lock(&acpi_desc->init_mutex);
2905 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2906 struct nd_cmd_ars_status *ars_status;
2907 struct acpi_nfit_system_address *spa;
2908 u64 ars_start, ars_len;
2909 int rc;
2910
2911 if (acpi_desc->cancel)
2912 break;
2913
2914 if (nfit_spa->nd_region)
2915 continue;
2916
2917 if (init_ars_done) {
2918 /*
2919 * No need to re-query, we're now just
2920 * reconciling all the ranges covered by the
2921 * initial scrub
2922 */
2923 rc = 0;
2924 } else
2925 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2926
2927 if (rc == -ENOTTY) {
2928 /* no ars capability, just register spa and move on */
2929 acpi_nfit_register_region(acpi_desc, nfit_spa);
2930 continue;
2931 }
2932
2933 if (rc == -EBUSY && !tmo) {
2934 /* fallthrough to directed scrub in phase 2 */
2935 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2936 break;
2937 } else if (rc == -EBUSY) {
2938 mutex_unlock(&acpi_desc->init_mutex);
2939 ssleep(1);
2940 tmo--;
2941 goto retry;
2942 }
2943
2944 /* we got some results, but there are more pending... */
2945 if (rc == -ENOSPC && overflow_retry--) {
2946 ars_status = acpi_desc->ars_status;
2947 /*
2948 * Record the original scrub range, so that we
2949 * can recall all the ranges impacted by the
2950 * initial scrub.
2951 */
2952 if (!init_scrub_length) {
2953 init_scrub_length = ars_status->length;
2954 init_scrub_address = ars_status->address;
2955 }
2956 rc = ars_continue(acpi_desc);
2957 if (rc == 0) {
2958 mutex_unlock(&acpi_desc->init_mutex);
2959 goto retry;
2960 }
2961 }
2962
2963 if (rc < 0) {
2964 /*
2965 * Initial scrub failed, we'll give it one more
2966 * try below...
2967 */
2968 break;
2969 }
2970
2971 /* We got some final results, record completed ranges */
2972 ars_status = acpi_desc->ars_status;
2973 if (init_scrub_length) {
2974 ars_start = init_scrub_address;
2975 ars_len = ars_start + init_scrub_length;
2976 } else {
2977 ars_start = ars_status->address;
2978 ars_len = ars_status->length;
2979 }
2980 spa = nfit_spa->spa;
2981
2982 if (!init_ars_done) {
2983 init_ars_done = true;
2984 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2985 ars_start, ars_len);
2986 }
2987 if (ars_start <= spa->address && ars_start + ars_len
2988 >= spa->address + spa->length)
2989 acpi_nfit_register_region(acpi_desc, nfit_spa);
2990 }
2991
2992 /*
2993 * For all the ranges not covered by an initial scrub we still
2994 * want to see if there are errors, but it's ok to discover them
2995 * asynchronously.
2996 */
2997 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2998 /*
2999 * Flag all the ranges that still need scrubbing, but
3000 * register them now to make data available.
3001 */
Vishal Verma37b137f2016-07-23 21:51:42 -07003002 if (!nfit_spa->nd_region) {
3003 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08003004 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07003005 }
Dan Williams1cf03c02016-02-17 13:01:23 -08003006 }
Dan Williams9ccaed42017-04-13 22:48:46 -07003007 acpi_desc->init_complete = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08003008
3009 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
3010 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07003011 acpi_desc->scrub_count++;
Toshi Kani80790032017-06-29 20:41:30 -06003012 acpi_desc->ars_start_flags = 0;
Vishal Verma37b137f2016-07-23 21:51:42 -07003013 if (acpi_desc->scrub_count_state)
3014 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08003015 mutex_unlock(&acpi_desc->init_mutex);
3016}
3017
Dan Williams1f7df6f2015-06-09 20:13:14 -04003018static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
3019{
3020 struct nfit_spa *nfit_spa;
3021
Dan Williams8d0d8ed2018-04-02 16:49:30 -07003022 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3023 int rc, type = nfit_spa_type(nfit_spa->spa);
3024
3025 /* PMEM and VMEM will be registered by the ARS workqueue */
3026 if (type == NFIT_SPA_PM || type == NFIT_SPA_VOLATILE)
3027 continue;
3028 /* BLK apertures belong to BLK region registration below */
3029 if (type == NFIT_SPA_BDW)
3030 continue;
3031 /* BLK regions don't need to wait for ARS results */
3032 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
3033 if (rc)
3034 return rc;
3035 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04003036
Toshi Kani80790032017-06-29 20:41:30 -06003037 acpi_desc->ars_start_flags = 0;
Dan Williamsfbabd822017-04-18 09:56:31 -07003038 if (!acpi_desc->cancel)
3039 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04003040 return 0;
3041}
3042
Vishal Verma20985162015-10-27 16:58:27 -06003043static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
3044 struct nfit_table_prev *prev)
3045{
3046 struct device *dev = acpi_desc->dev;
3047
3048 if (!list_empty(&prev->spas) ||
3049 !list_empty(&prev->memdevs) ||
3050 !list_empty(&prev->dcrs) ||
3051 !list_empty(&prev->bdws) ||
3052 !list_empty(&prev->idts) ||
3053 !list_empty(&prev->flushes)) {
3054 dev_err(dev, "new nfit deletes entries (unsupported)\n");
3055 return -ENXIO;
3056 }
3057 return 0;
3058}
3059
Vishal Verma37b137f2016-07-23 21:51:42 -07003060static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
3061{
3062 struct device *dev = acpi_desc->dev;
3063 struct kernfs_node *nfit;
3064 struct device *bus_dev;
3065
3066 if (!ars_supported(acpi_desc->nvdimm_bus))
3067 return 0;
3068
3069 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3070 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
3071 if (!nfit) {
3072 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
3073 return -ENODEV;
3074 }
3075 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
3076 sysfs_put(nfit);
3077 if (!acpi_desc->scrub_count_state) {
3078 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
3079 return -ENODEV;
3080 }
3081
3082 return 0;
3083}
3084
Dan Williamsfbabd822017-04-18 09:56:31 -07003085static void acpi_nfit_unregister(void *data)
Dan Williams58cd71b2016-07-21 18:05:36 -07003086{
3087 struct acpi_nfit_desc *acpi_desc = data;
3088
Dan Williams58cd71b2016-07-21 18:05:36 -07003089 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07003090}
3091
Dan Williamse7a11b42016-07-14 16:19:55 -07003092int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04003093{
3094 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06003095 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04003096 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04003097 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04003098
Dan Williams58cd71b2016-07-21 18:05:36 -07003099 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07003100 acpi_nfit_init_dsms(acpi_desc);
3101
Dan Williams58cd71b2016-07-21 18:05:36 -07003102 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
3103 &acpi_desc->nd_desc);
3104 if (!acpi_desc->nvdimm_bus)
3105 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07003106
Dan Williamsfbabd822017-04-18 09:56:31 -07003107 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
Dan Williams58cd71b2016-07-21 18:05:36 -07003108 acpi_desc);
3109 if (rc)
3110 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07003111
3112 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
3113 if (rc)
3114 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07003115
3116 /* register this acpi_desc for mce notifications */
3117 mutex_lock(&acpi_desc_lock);
3118 list_add_tail(&acpi_desc->list, &acpi_descs);
3119 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07003120 }
3121
Vishal Verma20985162015-10-27 16:58:27 -06003122 mutex_lock(&acpi_desc->init_mutex);
3123
3124 INIT_LIST_HEAD(&prev.spas);
3125 INIT_LIST_HEAD(&prev.memdevs);
3126 INIT_LIST_HEAD(&prev.dcrs);
3127 INIT_LIST_HEAD(&prev.bdws);
3128 INIT_LIST_HEAD(&prev.idts);
3129 INIT_LIST_HEAD(&prev.flushes);
3130
3131 list_cut_position(&prev.spas, &acpi_desc->spas,
3132 acpi_desc->spas.prev);
3133 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
3134 acpi_desc->memdevs.prev);
3135 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
3136 acpi_desc->dcrs.prev);
3137 list_cut_position(&prev.bdws, &acpi_desc->bdws,
3138 acpi_desc->bdws.prev);
3139 list_cut_position(&prev.idts, &acpi_desc->idts,
3140 acpi_desc->idts.prev);
3141 list_cut_position(&prev.flushes, &acpi_desc->flushes,
3142 acpi_desc->flushes.prev);
3143
Vishal Verma20985162015-10-27 16:58:27 -06003144 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06003145 while (!IS_ERR_OR_NULL(data))
3146 data = add_table(acpi_desc, &prev, data, end);
3147
3148 if (IS_ERR(data)) {
Johannes Thumshirnb8147352018-03-02 13:20:49 +01003149 dev_dbg(dev, "nfit table parsing error: %ld\n", PTR_ERR(data));
Vishal Verma20985162015-10-27 16:58:27 -06003150 rc = PTR_ERR(data);
3151 goto out_unlock;
3152 }
3153
3154 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
3155 if (rc)
3156 goto out_unlock;
3157
Dan Williams81ed4e32016-06-10 18:20:53 -07003158 rc = nfit_mem_init(acpi_desc);
3159 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06003160 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06003161
3162 rc = acpi_nfit_register_dimms(acpi_desc);
3163 if (rc)
3164 goto out_unlock;
3165
3166 rc = acpi_nfit_register_regions(acpi_desc);
3167
3168 out_unlock:
3169 mutex_unlock(&acpi_desc->init_mutex);
3170 return rc;
3171}
3172EXPORT_SYMBOL_GPL(acpi_nfit_init);
3173
Dan Williams7ae0fa432016-02-19 12:16:34 -08003174struct acpi_nfit_flush_work {
3175 struct work_struct work;
3176 struct completion cmp;
3177};
3178
3179static void flush_probe(struct work_struct *work)
3180{
3181 struct acpi_nfit_flush_work *flush;
3182
3183 flush = container_of(work, typeof(*flush), work);
3184 complete(&flush->cmp);
3185}
3186
3187static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
3188{
3189 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
3190 struct device *dev = acpi_desc->dev;
3191 struct acpi_nfit_flush_work flush;
Dan Williamse4714862017-02-02 10:31:00 -08003192 int rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08003193
3194 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
3195 device_lock(dev);
3196 device_unlock(dev);
3197
Dan Williams9ccaed42017-04-13 22:48:46 -07003198 /* bounce the init_mutex to make init_complete valid */
3199 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07003200 if (acpi_desc->cancel || acpi_desc->init_complete) {
3201 mutex_unlock(&acpi_desc->init_mutex);
Dan Williams9ccaed42017-04-13 22:48:46 -07003202 return 0;
Dan Williamsfbabd822017-04-18 09:56:31 -07003203 }
Dan Williams9ccaed42017-04-13 22:48:46 -07003204
Dan Williams7ae0fa432016-02-19 12:16:34 -08003205 /*
3206 * Scrub work could take 10s of seconds, userspace may give up so we
3207 * need to be interruptible while waiting.
3208 */
3209 INIT_WORK_ONSTACK(&flush.work, flush_probe);
Boqun Feng1c322ac2017-08-24 22:22:36 +08003210 init_completion(&flush.cmp);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003211 queue_work(nfit_wq, &flush.work);
Dan Williamsfbabd822017-04-18 09:56:31 -07003212 mutex_unlock(&acpi_desc->init_mutex);
Dan Williamse4714862017-02-02 10:31:00 -08003213
3214 rc = wait_for_completion_interruptible(&flush.cmp);
3215 cancel_work_sync(&flush.work);
3216 return rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08003217}
3218
Dan Williams87bf5722016-02-22 21:50:31 -08003219static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
3220 struct nvdimm *nvdimm, unsigned int cmd)
3221{
3222 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
3223
3224 if (nvdimm)
3225 return 0;
3226 if (cmd != ND_CMD_ARS_START)
3227 return 0;
3228
3229 /*
3230 * The kernel and userspace may race to initiate a scrub, but
3231 * the scrub thread is prepared to lose that initial race. It
3232 * just needs guarantees that any ars it initiates are not
3233 * interrupted by any intervening start reqeusts from userspace.
3234 */
3235 if (work_busy(&acpi_desc->work))
3236 return -EBUSY;
3237
3238 return 0;
3239}
3240
Toshi Kani80790032017-06-29 20:41:30 -06003241int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc, u8 flags)
Vishal Verma37b137f2016-07-23 21:51:42 -07003242{
3243 struct device *dev = acpi_desc->dev;
3244 struct nfit_spa *nfit_spa;
3245
3246 if (work_busy(&acpi_desc->work))
3247 return -EBUSY;
3248
Vishal Verma37b137f2016-07-23 21:51:42 -07003249 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07003250 if (acpi_desc->cancel) {
3251 mutex_unlock(&acpi_desc->init_mutex);
3252 return 0;
3253 }
3254
Vishal Verma37b137f2016-07-23 21:51:42 -07003255 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3256 struct acpi_nfit_system_address *spa = nfit_spa->spa;
3257
3258 if (nfit_spa_type(spa) != NFIT_SPA_PM)
3259 continue;
3260
3261 nfit_spa->ars_required = 1;
3262 }
Toshi Kani80790032017-06-29 20:41:30 -06003263 acpi_desc->ars_start_flags = flags;
Vishal Verma37b137f2016-07-23 21:51:42 -07003264 queue_work(nfit_wq, &acpi_desc->work);
Johannes Thumshirnb8147352018-03-02 13:20:49 +01003265 dev_dbg(dev, "ars_scan triggered\n");
Vishal Verma37b137f2016-07-23 21:51:42 -07003266 mutex_unlock(&acpi_desc->init_mutex);
3267
3268 return 0;
3269}
3270
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003271void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06003272{
3273 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06003274
3275 dev_set_drvdata(dev, acpi_desc);
3276 acpi_desc->dev = dev;
3277 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3278 nd_desc = &acpi_desc->nd_desc;
3279 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07003280 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06003281 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08003282 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08003283 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06003284 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3285
Dan Williamsb94d5232015-05-19 22:54:31 -04003286 INIT_LIST_HEAD(&acpi_desc->spas);
3287 INIT_LIST_HEAD(&acpi_desc->dcrs);
3288 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04003289 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06003290 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04003291 INIT_LIST_HEAD(&acpi_desc->memdevs);
3292 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003293 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06003294 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08003295 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04003296}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003297EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04003298
Dan Williams3c87f372017-04-03 13:52:14 -07003299static void acpi_nfit_put_table(void *table)
3300{
3301 acpi_put_table(table);
3302}
3303
Dan Williamsfbabd822017-04-18 09:56:31 -07003304void acpi_nfit_shutdown(void *data)
3305{
3306 struct acpi_nfit_desc *acpi_desc = data;
3307 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3308
3309 /*
3310 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3311 * race teardown
3312 */
3313 mutex_lock(&acpi_desc_lock);
3314 list_del(&acpi_desc->list);
3315 mutex_unlock(&acpi_desc_lock);
3316
3317 mutex_lock(&acpi_desc->init_mutex);
3318 acpi_desc->cancel = 1;
3319 mutex_unlock(&acpi_desc->init_mutex);
3320
3321 /*
3322 * Bounce the nvdimm bus lock to make sure any in-flight
3323 * acpi_nfit_ars_rescan() submissions have had a chance to
3324 * either submit or see ->cancel set.
3325 */
3326 device_lock(bus_dev);
3327 device_unlock(bus_dev);
3328
3329 flush_workqueue(nfit_wq);
3330}
3331EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3332
Dan Williamsb94d5232015-05-19 22:54:31 -04003333static int acpi_nfit_add(struct acpi_device *adev)
3334{
Vishal Verma20985162015-10-27 16:58:27 -06003335 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04003336 struct acpi_nfit_desc *acpi_desc;
3337 struct device *dev = &adev->dev;
3338 struct acpi_table_header *tbl;
3339 acpi_status status = AE_OK;
3340 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07003341 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04003342
Lv Zheng6b11d1d2016-12-14 15:04:39 +08003343 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
Dan Williamsb94d5232015-05-19 22:54:31 -04003344 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06003345 /* This is ok, we could have an nvdimm hotplugged later */
3346 dev_dbg(dev, "failed to find NFIT at startup\n");
3347 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04003348 }
Dan Williams3c87f372017-04-03 13:52:14 -07003349
3350 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3351 if (rc)
3352 return rc;
Lv Zheng6b11d1d2016-12-14 15:04:39 +08003353 sz = tbl->length;
Dan Williamsb94d5232015-05-19 22:54:31 -04003354
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003355 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3356 if (!acpi_desc)
3357 return -ENOMEM;
3358 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04003359
Dan Williamse7a11b42016-07-14 16:19:55 -07003360 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05003361 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04003362
Vishal Verma20985162015-10-27 16:58:27 -06003363 /* Evaluate _FIT and override with that if present */
3364 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3365 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07003366 union acpi_object *obj = buf.pointer;
3367
3368 if (obj->type == ACPI_TYPE_BUFFER)
3369 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3370 obj->buffer.length);
3371 else
Johannes Thumshirnb8147352018-03-02 13:20:49 +01003372 dev_dbg(dev, "invalid type %d, ignoring _FIT\n",
3373 (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07003374 kfree(buf.pointer);
3375 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07003376 /* skip over the lead-in header table */
3377 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3378 + sizeof(struct acpi_table_nfit),
3379 sz - sizeof(struct acpi_table_nfit));
Dan Williamsfbabd822017-04-18 09:56:31 -07003380
3381 if (rc)
3382 return rc;
3383 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
Dan Williamsb94d5232015-05-19 22:54:31 -04003384}
3385
3386static int acpi_nfit_remove(struct acpi_device *adev)
3387{
Dan Williamsfbabd822017-04-18 09:56:31 -07003388 /* see acpi_nfit_unregister */
Dan Williamsb94d5232015-05-19 22:54:31 -04003389 return 0;
3390}
3391
Toshi Kani56b47fe2017-06-08 12:36:57 -06003392static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
Vishal Verma20985162015-10-27 16:58:27 -06003393{
Dan Williamsc14a8682016-08-18 22:15:04 -07003394 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06003395 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07003396 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06003397 acpi_status status;
3398 int ret;
3399
Vishal Verma20985162015-10-27 16:58:27 -06003400 if (!dev->driver) {
3401 /* dev->driver may be null if we're being removed */
Johannes Thumshirnb8147352018-03-02 13:20:49 +01003402 dev_dbg(dev, "no driver found for dev\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07003403 return;
Vishal Verma20985162015-10-27 16:58:27 -06003404 }
3405
3406 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003407 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3408 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07003409 return;
3410 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003411 } else {
3412 /*
3413 * Finish previous registration before considering new
3414 * regions.
3415 */
3416 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06003417 }
3418
3419 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07003420 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06003421 if (ACPI_FAILURE(status)) {
3422 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07003423 return;
Vishal Verma20985162015-10-27 16:58:27 -06003424 }
3425
Linda Knippers6b577c92015-11-20 19:05:49 -05003426 obj = buf.pointer;
3427 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07003428 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3429 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07003430 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05003431 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07003432 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05003433 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06003434 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07003435}
Toshi Kani56b47fe2017-06-08 12:36:57 -06003436
3437static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3438{
3439 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Toshi Kani80790032017-06-29 20:41:30 -06003440 u8 flags = (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON) ?
3441 0 : ND_ARS_RETURN_PREV_DATA;
Toshi Kani56b47fe2017-06-08 12:36:57 -06003442
Toshi Kani80790032017-06-29 20:41:30 -06003443 acpi_nfit_ars_rescan(acpi_desc, flags);
Toshi Kani56b47fe2017-06-08 12:36:57 -06003444}
3445
3446void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3447{
Johannes Thumshirnb8147352018-03-02 13:20:49 +01003448 dev_dbg(dev, "event: 0x%x\n", event);
Toshi Kani56b47fe2017-06-08 12:36:57 -06003449
3450 switch (event) {
3451 case NFIT_NOTIFY_UPDATE:
3452 return acpi_nfit_update_notify(dev, handle);
3453 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3454 return acpi_nfit_uc_error_notify(dev, handle);
3455 default:
3456 return;
3457 }
3458}
Dan Williamsc14a8682016-08-18 22:15:04 -07003459EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06003460
Dan Williamsc14a8682016-08-18 22:15:04 -07003461static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3462{
3463 device_lock(&adev->dev);
3464 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3465 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06003466}
3467
Dan Williamsb94d5232015-05-19 22:54:31 -04003468static const struct acpi_device_id acpi_nfit_ids[] = {
3469 { "ACPI0012", 0 },
3470 { "", 0 },
3471};
3472MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3473
3474static struct acpi_driver acpi_nfit_driver = {
3475 .name = KBUILD_MODNAME,
3476 .ids = acpi_nfit_ids,
3477 .ops = {
3478 .add = acpi_nfit_add,
3479 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06003480 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04003481 },
3482};
3483
3484static __init int nfit_init(void)
3485{
Prarit Bhargava7e700d22017-05-31 13:32:00 -04003486 int ret;
3487
Dan Williamsb94d5232015-05-19 22:54:31 -04003488 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3489 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3490 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3491 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3492 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3493 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3494 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
Dave Jiang06e8ccd2018-01-31 12:45:38 -07003495 BUILD_BUG_ON(sizeof(struct acpi_nfit_capabilities) != 16);
Dan Williamsb94d5232015-05-19 22:54:31 -04003496
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03003497 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3498 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3499 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3500 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3501 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3502 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3503 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3504 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3505 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3506 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3507 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3508 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3509 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04003510
Dan Williams7ae0fa432016-02-19 12:16:34 -08003511 nfit_wq = create_singlethread_workqueue("nfit");
3512 if (!nfit_wq)
3513 return -ENOMEM;
3514
Vishal Verma6839a6d2016-07-23 21:51:21 -07003515 nfit_mce_register();
Prarit Bhargava7e700d22017-05-31 13:32:00 -04003516 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3517 if (ret) {
3518 nfit_mce_unregister();
3519 destroy_workqueue(nfit_wq);
3520 }
Vishal Verma6839a6d2016-07-23 21:51:21 -07003521
Prarit Bhargava7e700d22017-05-31 13:32:00 -04003522 return ret;
3523
Dan Williamsb94d5232015-05-19 22:54:31 -04003524}
3525
3526static __exit void nfit_exit(void)
3527{
Vishal Verma6839a6d2016-07-23 21:51:21 -07003528 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04003529 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003530 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003531 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04003532}
3533
3534module_init(nfit_init);
3535module_exit(nfit_exit);
3536MODULE_LICENSE("GPL v2");
3537MODULE_AUTHOR("Intel Corporation");