blob: 3369bb8fd9bae1d093e819a71013dafe8ca1fad3 [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 Williams9d62ed92017-05-04 11:47:22 -0700186static int xlat_nvdimm_status(void *buf, unsigned int cmd, u32 status)
187{
188 switch (cmd) {
189 case ND_CMD_GET_CONFIG_SIZE:
190 if (status >> 16 & ND_CONFIG_LOCKED)
191 return -EACCES;
192 break;
193 default:
194 break;
195 }
196
197 /* all other non-zero status results in an error */
198 if (status)
199 return -EIO;
200 return 0;
201}
202
Dan Williamsd6eb2702016-12-06 15:06:55 -0800203static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
204 u32 status)
205{
206 if (!nvdimm)
207 return xlat_bus_status(buf, cmd, status);
Dan Williams9d62ed92017-05-04 11:47:22 -0700208 return xlat_nvdimm_status(buf, cmd, status);
Dan Williamsd6eb2702016-12-06 15:06:55 -0800209}
210
Dan Williamsa7de92d2016-12-05 13:43:25 -0800211int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
212 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400213{
Dan Williams62232e452015-06-08 14:27:06 -0400214 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams62232e452015-06-08 14:27:06 -0400215 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700216 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400217 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700218 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400219 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700220 unsigned long cmd_mask, dsm_mask;
Dan Williams11294d62016-09-21 09:21:26 -0700221 u32 offset, fw_status = 0;
Dan Williams62232e452015-06-08 14:27:06 -0400222 acpi_handle handle;
Dan Williams31eca762016-04-28 16:23:43 -0700223 unsigned int func;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300224 const guid_t *guid;
Dan Williams62232e452015-06-08 14:27:06 -0400225 int rc, i;
226
Dan Williams31eca762016-04-28 16:23:43 -0700227 func = cmd;
228 if (cmd == ND_CMD_CALL) {
229 call_pkg = buf;
230 func = call_pkg->nd_command;
Meng Xu9edcad52017-09-04 11:34:33 -0400231
232 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
233 if (call_pkg->nd_reserved2[i])
234 return -EINVAL;
Dan Williams31eca762016-04-28 16:23:43 -0700235 }
236
Dan Williams62232e452015-06-08 14:27:06 -0400237 if (nvdimm) {
238 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
239 struct acpi_device *adev = nfit_mem->adev;
240
241 if (!adev)
242 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700243 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
244 return -ENOTTY;
245
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400246 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400247 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700248 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400249 dsm_mask = nfit_mem->dsm_mask;
250 desc = nd_cmd_dimm_desc(cmd);
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300251 guid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400252 handle = adev->handle;
253 } else {
254 struct acpi_device *adev = to_acpi_dev(acpi_desc);
255
256 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700257 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700258 dsm_mask = cmd_mask;
Jerry Hoemann7db5bb32017-06-30 20:53:24 -0700259 if (cmd == ND_CMD_CALL)
260 dsm_mask = nd_desc->bus_dsm_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400261 desc = nd_cmd_bus_desc(cmd);
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300262 guid = to_nfit_uuid(NFIT_DEV_BUS);
Dan Williams62232e452015-06-08 14:27:06 -0400263 handle = adev->handle;
264 dimm_name = "bus";
265 }
266
267 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
268 return -ENOTTY;
269
Dan Williams31eca762016-04-28 16:23:43 -0700270 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400271 return -ENOTTY;
272
273 in_obj.type = ACPI_TYPE_PACKAGE;
274 in_obj.package.count = 1;
275 in_obj.package.elements = &in_buf;
276 in_buf.type = ACPI_TYPE_BUFFER;
277 in_buf.buffer.pointer = buf;
278 in_buf.buffer.length = 0;
279
280 /* libnvdimm has already validated the input envelope */
281 for (i = 0; i < desc->in_num; i++)
282 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
283 i, buf);
284
Dan Williams31eca762016-04-28 16:23:43 -0700285 if (call_pkg) {
286 /* skip over package wrapper */
287 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
288 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400289 }
290
Dan Williams7699a6a2017-04-28 13:54:30 -0700291 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
292 __func__, dimm_name, cmd, func, in_buf.buffer.length);
293 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
Dan Williams31eca762016-04-28 16:23:43 -0700294 in_buf.buffer.pointer,
295 min_t(u32, 256, in_buf.buffer.length), true);
Dan Williams31eca762016-04-28 16:23:43 -0700296
Andy Shevchenko94116f82017-06-05 19:40:46 +0300297 out_obj = acpi_evaluate_dsm(handle, guid, 1, func, &in_obj);
Dan Williams62232e452015-06-08 14:27:06 -0400298 if (!out_obj) {
299 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
300 cmd_name);
301 return -EINVAL;
302 }
303
Dan Williams31eca762016-04-28 16:23:43 -0700304 if (call_pkg) {
305 call_pkg->nd_fw_size = out_obj->buffer.length;
306 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
307 out_obj->buffer.pointer,
308 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
309
310 ACPI_FREE(out_obj);
311 /*
312 * Need to support FW function w/o known size in advance.
313 * Caller can determine required size based upon nd_fw_size.
314 * If we return an error (like elsewhere) then caller wouldn't
315 * be able to rely upon data returned to make calculation.
316 */
317 return 0;
318 }
319
Dan Williams62232e452015-06-08 14:27:06 -0400320 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
321 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
322 __func__, dimm_name, cmd_name, out_obj->type);
323 rc = -EINVAL;
324 goto out;
325 }
326
Dan Williams7699a6a2017-04-28 13:54:30 -0700327 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, dimm_name,
328 cmd_name, out_obj->buffer.length);
329 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
330 out_obj->buffer.pointer,
331 min_t(u32, 128, out_obj->buffer.length), true);
Dan Williams62232e452015-06-08 14:27:06 -0400332
333 for (i = 0, offset = 0; i < desc->out_num; i++) {
334 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800335 (u32 *) out_obj->buffer.pointer,
336 out_obj->buffer.length - offset);
Dan Williams62232e452015-06-08 14:27:06 -0400337
338 if (offset + out_size > out_obj->buffer.length) {
339 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
340 __func__, dimm_name, cmd_name, i);
341 break;
342 }
343
344 if (in_buf.buffer.length + offset + out_size > buf_len) {
345 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
346 __func__, dimm_name, cmd_name, i);
347 rc = -ENXIO;
348 goto out;
349 }
350 memcpy(buf + in_buf.buffer.length + offset,
351 out_obj->buffer.pointer + offset, out_size);
352 offset += out_size;
353 }
Dan Williams11294d62016-09-21 09:21:26 -0700354
355 /*
356 * Set fw_status for all the commands with a known format to be
357 * later interpreted by xlat_status().
358 */
359 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
360 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
361 fw_status = *(u32 *) out_obj->buffer.pointer;
362
Dan Williams62232e452015-06-08 14:27:06 -0400363 if (offset + in_buf.buffer.length < buf_len) {
364 if (i >= 1) {
365 /*
366 * status valid, return the number of bytes left
367 * unfilled in the output buffer
368 */
369 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800370 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800371 *cmd_rc = xlat_status(nvdimm, buf, cmd,
372 fw_status);
Dan Williams62232e452015-06-08 14:27:06 -0400373 } else {
374 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
375 __func__, dimm_name, cmd_name, buf_len,
376 offset);
377 rc = -ENXIO;
378 }
Dan Williams2eea6582016-05-02 09:11:53 -0700379 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400380 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700381 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800382 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
Dan Williams2eea6582016-05-02 09:11:53 -0700383 }
Dan Williams62232e452015-06-08 14:27:06 -0400384
385 out:
386 ACPI_FREE(out_obj);
387
388 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400389}
Dan Williamsa7de92d2016-12-05 13:43:25 -0800390EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
Dan Williamsb94d5232015-05-19 22:54:31 -0400391
392static const char *spa_type_name(u16 type)
393{
394 static const char *to_name[] = {
395 [NFIT_SPA_VOLATILE] = "volatile",
396 [NFIT_SPA_PM] = "pmem",
397 [NFIT_SPA_DCR] = "dimm-control-region",
398 [NFIT_SPA_BDW] = "block-data-window",
399 [NFIT_SPA_VDISK] = "volatile-disk",
400 [NFIT_SPA_VCD] = "volatile-cd",
401 [NFIT_SPA_PDISK] = "persistent-disk",
402 [NFIT_SPA_PCD] = "persistent-cd",
403
404 };
405
406 if (type > NFIT_SPA_PCD)
407 return "unknown";
408
409 return to_name[type];
410}
411
Vishal Verma6839a6d2016-07-23 21:51:21 -0700412int nfit_spa_type(struct acpi_nfit_system_address *spa)
Dan Williamsb94d5232015-05-19 22:54:31 -0400413{
414 int i;
415
416 for (i = 0; i < NFIT_UUID_MAX; i++)
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300417 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
Dan Williamsb94d5232015-05-19 22:54:31 -0400418 return i;
419 return -1;
420}
421
422static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600423 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400424 struct acpi_nfit_system_address *spa)
425{
426 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600427 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400428
Dan Williams31932042016-07-14 17:22:48 -0700429 if (spa->header.length != sizeof(*spa))
430 return false;
431
Vishal Verma20985162015-10-27 16:58:27 -0600432 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700433 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600434 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
435 return true;
436 }
437 }
438
Dan Williams31932042016-07-14 17:22:48 -0700439 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
440 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400441 if (!nfit_spa)
442 return false;
443 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700444 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400445 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
446 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
447 spa->range_index,
448 spa_type_name(nfit_spa_type(spa)));
449 return true;
450}
451
452static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600453 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400454 struct acpi_nfit_memory_map *memdev)
455{
456 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600457 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400458
Dan Williams31932042016-07-14 17:22:48 -0700459 if (memdev->header.length != sizeof(*memdev))
460 return false;
461
Vishal Verma20985162015-10-27 16:58:27 -0600462 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700463 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600464 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
465 return true;
466 }
467
Dan Williams31932042016-07-14 17:22:48 -0700468 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
469 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400470 if (!nfit_memdev)
471 return false;
472 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700473 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400474 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
Dan Williamscaa603a2017-04-14 10:27:11 -0700475 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
Dan Williamsb94d5232015-05-19 22:54:31 -0400476 __func__, memdev->device_handle, memdev->range_index,
Dan Williamscaa603a2017-04-14 10:27:11 -0700477 memdev->region_index, memdev->flags);
Dan Williamsb94d5232015-05-19 22:54:31 -0400478 return true;
479}
480
Dan Williams31932042016-07-14 17:22:48 -0700481/*
482 * An implementation may provide a truncated control region if no block windows
483 * are defined.
484 */
485static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
486{
487 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
488 window_size))
489 return 0;
490 if (dcr->windows)
491 return sizeof(*dcr);
492 return offsetof(struct acpi_nfit_control_region, window_size);
493}
494
Dan Williamsb94d5232015-05-19 22:54:31 -0400495static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600496 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400497 struct acpi_nfit_control_region *dcr)
498{
499 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600500 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400501
Dan Williams31932042016-07-14 17:22:48 -0700502 if (!sizeof_dcr(dcr))
503 return false;
504
Vishal Verma20985162015-10-27 16:58:27 -0600505 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700506 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600507 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
508 return true;
509 }
510
Dan Williams31932042016-07-14 17:22:48 -0700511 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
512 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400513 if (!nfit_dcr)
514 return false;
515 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700516 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400517 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
518 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
519 dcr->region_index, dcr->windows);
520 return true;
521}
522
523static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600524 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400525 struct acpi_nfit_data_region *bdw)
526{
527 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600528 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400529
Dan Williams31932042016-07-14 17:22:48 -0700530 if (bdw->header.length != sizeof(*bdw))
531 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600532 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700533 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600534 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
535 return true;
536 }
537
Dan Williams31932042016-07-14 17:22:48 -0700538 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
539 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400540 if (!nfit_bdw)
541 return false;
542 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700543 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400544 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
545 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
546 bdw->region_index, bdw->windows);
547 return true;
548}
549
Dan Williams31932042016-07-14 17:22:48 -0700550static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
551{
552 if (idt->header.length < sizeof(*idt))
553 return 0;
554 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
555}
556
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400557static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600558 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400559 struct acpi_nfit_interleave *idt)
560{
561 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600562 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400563
Dan Williams31932042016-07-14 17:22:48 -0700564 if (!sizeof_idt(idt))
565 return false;
566
567 list_for_each_entry(nfit_idt, &prev->idts, list) {
568 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
569 continue;
570
571 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600572 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
573 return true;
574 }
Dan Williams31932042016-07-14 17:22:48 -0700575 }
Vishal Verma20985162015-10-27 16:58:27 -0600576
Dan Williams31932042016-07-14 17:22:48 -0700577 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
578 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400579 if (!nfit_idt)
580 return false;
581 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700582 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400583 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
584 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
585 idt->interleave_index, idt->line_count);
586 return true;
587}
588
Dan Williams31932042016-07-14 17:22:48 -0700589static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
590{
591 if (flush->header.length < sizeof(*flush))
592 return 0;
593 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
594}
595
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600596static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600597 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600598 struct acpi_nfit_flush_address *flush)
599{
600 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600601 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600602
Dan Williams31932042016-07-14 17:22:48 -0700603 if (!sizeof_flush(flush))
604 return false;
605
606 list_for_each_entry(nfit_flush, &prev->flushes, list) {
607 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
608 continue;
609
610 if (memcmp(nfit_flush->flush, flush,
611 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600612 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
613 return true;
614 }
Dan Williams31932042016-07-14 17:22:48 -0700615 }
Vishal Verma20985162015-10-27 16:58:27 -0600616
Dan Williams31932042016-07-14 17:22:48 -0700617 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
618 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600619 if (!nfit_flush)
620 return false;
621 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700622 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600623 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
624 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
625 flush->device_handle, flush->hint_count);
626 return true;
627}
628
Vishal Verma20985162015-10-27 16:58:27 -0600629static void *add_table(struct acpi_nfit_desc *acpi_desc,
630 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400631{
632 struct device *dev = acpi_desc->dev;
633 struct acpi_nfit_header *hdr;
634 void *err = ERR_PTR(-ENOMEM);
635
636 if (table >= end)
637 return NULL;
638
639 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600640 if (!hdr->length) {
641 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
642 hdr->type);
643 return NULL;
644 }
645
Dan Williamsb94d5232015-05-19 22:54:31 -0400646 switch (hdr->type) {
647 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600648 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400649 return err;
650 break;
651 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600652 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400653 return err;
654 break;
655 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600656 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400657 return err;
658 break;
659 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600660 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400661 return err;
662 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400663 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600664 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400665 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400666 break;
667 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600668 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600669 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400670 break;
671 case ACPI_NFIT_TYPE_SMBIOS:
672 dev_dbg(dev, "%s: smbios\n", __func__);
673 break;
674 default:
675 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
676 break;
677 }
678
679 return table + hdr->length;
680}
681
682static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
683 struct nfit_mem *nfit_mem)
684{
685 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
686 u16 dcr = nfit_mem->dcr->region_index;
687 struct nfit_spa *nfit_spa;
688
689 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
690 u16 range_index = nfit_spa->spa->range_index;
691 int type = nfit_spa_type(nfit_spa->spa);
692 struct nfit_memdev *nfit_memdev;
693
694 if (type != NFIT_SPA_BDW)
695 continue;
696
697 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
698 if (nfit_memdev->memdev->range_index != range_index)
699 continue;
700 if (nfit_memdev->memdev->device_handle != device_handle)
701 continue;
702 if (nfit_memdev->memdev->region_index != dcr)
703 continue;
704
705 nfit_mem->spa_bdw = nfit_spa->spa;
706 return;
707 }
708 }
709
710 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
711 nfit_mem->spa_dcr->range_index);
712 nfit_mem->bdw = NULL;
713}
714
Dan Williams6697b2c2016-02-04 16:51:00 -0800715static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400716 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
717{
718 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400719 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400720 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400721 struct nfit_idt *nfit_idt;
722 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400723
Dan Williamsb94d5232015-05-19 22:54:31 -0400724 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
725 if (nfit_bdw->bdw->region_index != dcr)
726 continue;
727 nfit_mem->bdw = nfit_bdw->bdw;
728 break;
729 }
730
731 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800732 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400733
734 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400735
736 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800737 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400738
739 range_index = nfit_mem->spa_bdw->range_index;
740 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
741 if (nfit_memdev->memdev->range_index != range_index ||
742 nfit_memdev->memdev->region_index != dcr)
743 continue;
744 nfit_mem->memdev_bdw = nfit_memdev->memdev;
745 idt_idx = nfit_memdev->memdev->interleave_index;
746 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
747 if (nfit_idt->idt->interleave_index != idt_idx)
748 continue;
749 nfit_mem->idt_bdw = nfit_idt->idt;
750 break;
751 }
752 break;
753 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400754}
755
Dan Williams14999342017-04-13 19:46:36 -0700756static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400757 struct acpi_nfit_system_address *spa)
758{
759 struct nfit_mem *nfit_mem, *found;
760 struct nfit_memdev *nfit_memdev;
Dan Williams14999342017-04-13 19:46:36 -0700761 int type = spa ? nfit_spa_type(spa) : 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400762
763 switch (type) {
764 case NFIT_SPA_DCR:
765 case NFIT_SPA_PM:
766 break;
767 default:
Dan Williams14999342017-04-13 19:46:36 -0700768 if (spa)
769 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400770 }
771
Dan Williams14999342017-04-13 19:46:36 -0700772 /*
773 * This loop runs in two modes, when a dimm is mapped the loop
774 * adds memdev associations to an existing dimm, or creates a
775 * dimm. In the unmapped dimm case this loop sweeps for memdev
776 * instances with an invalid / zero range_index and adds those
777 * dimms without spa associations.
778 */
Dan Williamsb94d5232015-05-19 22:54:31 -0400779 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700780 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800781 struct nfit_dcr *nfit_dcr;
782 u32 device_handle;
783 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400784
Dan Williams14999342017-04-13 19:46:36 -0700785 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
786 continue;
787 if (!spa && nfit_memdev->memdev->range_index)
Dan Williamsb94d5232015-05-19 22:54:31 -0400788 continue;
789 found = NULL;
790 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800791 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400792 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800793 if (__to_nfit_memdev(nfit_mem)->device_handle
794 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400795 found = nfit_mem;
796 break;
797 }
798
799 if (found)
800 nfit_mem = found;
801 else {
802 nfit_mem = devm_kzalloc(acpi_desc->dev,
803 sizeof(*nfit_mem), GFP_KERNEL);
804 if (!nfit_mem)
805 return -ENOMEM;
806 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700807 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800808 list_add(&nfit_mem->list, &acpi_desc->dimms);
809 }
810
811 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
812 if (nfit_dcr->dcr->region_index != dcr)
813 continue;
814 /*
815 * Record the control region for the dimm. For
816 * the ACPI 6.1 case, where there are separate
817 * control regions for the pmem vs blk
818 * interfaces, be sure to record the extended
819 * blk details.
820 */
821 if (!nfit_mem->dcr)
822 nfit_mem->dcr = nfit_dcr->dcr;
823 else if (nfit_mem->dcr->windows == 0
824 && nfit_dcr->dcr->windows)
825 nfit_mem->dcr = nfit_dcr->dcr;
826 break;
827 }
828
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700829 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700830 struct acpi_nfit_flush_address *flush;
831 u16 i;
832
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700833 if (nfit_flush->flush->device_handle != device_handle)
834 continue;
835 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700836 flush = nfit_flush->flush;
837 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
838 flush->hint_count
839 * sizeof(struct resource), GFP_KERNEL);
840 if (!nfit_mem->flush_wpq)
841 return -ENOMEM;
842 for (i = 0; i < flush->hint_count; i++) {
843 struct resource *res = &nfit_mem->flush_wpq[i];
844
845 res->start = flush->hint_address[i];
846 res->end = res->start + 8 - 1;
847 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700848 break;
849 }
850
Dan Williams6697b2c2016-02-04 16:51:00 -0800851 if (dcr && !nfit_mem->dcr) {
852 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
853 spa->range_index, dcr);
854 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400855 }
856
857 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400858 struct nfit_idt *nfit_idt;
859 u16 idt_idx;
860
Dan Williamsb94d5232015-05-19 22:54:31 -0400861 /* multiple dimms may share a SPA when interleaved */
862 nfit_mem->spa_dcr = spa;
863 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400864 idt_idx = nfit_memdev->memdev->interleave_index;
865 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
866 if (nfit_idt->idt->interleave_index != idt_idx)
867 continue;
868 nfit_mem->idt_dcr = nfit_idt->idt;
869 break;
870 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800871 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williams14999342017-04-13 19:46:36 -0700872 } else if (type == NFIT_SPA_PM) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400873 /*
874 * A single dimm may belong to multiple SPA-PM
875 * ranges, record at least one in addition to
876 * any SPA-DCR range.
877 */
878 nfit_mem->memdev_pmem = nfit_memdev->memdev;
Dan Williams14999342017-04-13 19:46:36 -0700879 } else
880 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400881 }
882
883 return 0;
884}
885
886static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
887{
888 struct nfit_mem *a = container_of(_a, typeof(*a), list);
889 struct nfit_mem *b = container_of(_b, typeof(*b), list);
890 u32 handleA, handleB;
891
892 handleA = __to_nfit_memdev(a)->device_handle;
893 handleB = __to_nfit_memdev(b)->device_handle;
894 if (handleA < handleB)
895 return -1;
896 else if (handleA > handleB)
897 return 1;
898 return 0;
899}
900
901static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
902{
903 struct nfit_spa *nfit_spa;
Dan Williams14999342017-04-13 19:46:36 -0700904 int rc;
905
Dan Williamsb94d5232015-05-19 22:54:31 -0400906
907 /*
908 * For each SPA-DCR or SPA-PMEM address range find its
909 * corresponding MEMDEV(s). From each MEMDEV find the
910 * corresponding DCR. Then, if we're operating on a SPA-DCR,
911 * try to find a SPA-BDW and a corresponding BDW that references
912 * the DCR. Throw it all into an nfit_mem object. Note, that
913 * BDWs are optional.
914 */
915 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
Dan Williams14999342017-04-13 19:46:36 -0700916 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400917 if (rc)
918 return rc;
919 }
920
Dan Williams14999342017-04-13 19:46:36 -0700921 /*
922 * If a DIMM has failed to be mapped into SPA there will be no
923 * SPA entries above. Find and register all the unmapped DIMMs
924 * for reporting and recovery purposes.
925 */
926 rc = __nfit_mem_init(acpi_desc, NULL);
927 if (rc)
928 return rc;
929
Dan Williamsb94d5232015-05-19 22:54:31 -0400930 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
931
932 return 0;
933}
934
Jerry Hoemann41f95db2017-06-30 20:41:28 -0700935static ssize_t bus_dsm_mask_show(struct device *dev,
936 struct device_attribute *attr, char *buf)
937{
938 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
939 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
940
941 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
942}
943static struct device_attribute dev_attr_bus_dsm_mask =
944 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
945
Dan Williams45def222015-04-26 19:26:48 -0400946static ssize_t revision_show(struct device *dev,
947 struct device_attribute *attr, char *buf)
948{
949 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
950 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
951 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
952
Linda Knippers6b577c92015-11-20 19:05:49 -0500953 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400954}
955static DEVICE_ATTR_RO(revision);
956
Vishal Verma9ffd6352016-09-30 17:19:29 -0600957static ssize_t hw_error_scrub_show(struct device *dev,
958 struct device_attribute *attr, char *buf)
959{
960 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
961 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
962 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
963
964 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
965}
966
967/*
968 * The 'hw_error_scrub' attribute can have the following values written to it:
969 * '0': Switch to the default mode where an exception will only insert
970 * the address of the memory error into the poison and badblocks lists.
971 * '1': Enable a full scrub to happen if an exception for a memory error is
972 * received.
973 */
974static ssize_t hw_error_scrub_store(struct device *dev,
975 struct device_attribute *attr, const char *buf, size_t size)
976{
977 struct nvdimm_bus_descriptor *nd_desc;
978 ssize_t rc;
979 long val;
980
981 rc = kstrtol(buf, 0, &val);
982 if (rc)
983 return rc;
984
985 device_lock(dev);
986 nd_desc = dev_get_drvdata(dev);
987 if (nd_desc) {
988 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
989
990 switch (val) {
991 case HW_ERROR_SCRUB_ON:
992 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
993 break;
994 case HW_ERROR_SCRUB_OFF:
995 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
996 break;
997 default:
998 rc = -EINVAL;
999 break;
1000 }
1001 }
1002 device_unlock(dev);
1003 if (rc)
1004 return rc;
1005 return size;
1006}
1007static DEVICE_ATTR_RW(hw_error_scrub);
1008
Vishal Verma37b137f2016-07-23 21:51:42 -07001009/*
1010 * This shows the number of full Address Range Scrubs that have been
1011 * completed since driver load time. Userspace can wait on this using
1012 * select/poll etc. A '+' at the end indicates an ARS is in progress
1013 */
1014static ssize_t scrub_show(struct device *dev,
1015 struct device_attribute *attr, char *buf)
1016{
1017 struct nvdimm_bus_descriptor *nd_desc;
1018 ssize_t rc = -ENXIO;
1019
1020 device_lock(dev);
1021 nd_desc = dev_get_drvdata(dev);
1022 if (nd_desc) {
1023 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1024
1025 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1026 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
1027 }
1028 device_unlock(dev);
1029 return rc;
1030}
1031
Vishal Verma37b137f2016-07-23 21:51:42 -07001032static ssize_t scrub_store(struct device *dev,
1033 struct device_attribute *attr, const char *buf, size_t size)
1034{
1035 struct nvdimm_bus_descriptor *nd_desc;
1036 ssize_t rc;
1037 long val;
1038
1039 rc = kstrtol(buf, 0, &val);
1040 if (rc)
1041 return rc;
1042 if (val != 1)
1043 return -EINVAL;
1044
1045 device_lock(dev);
1046 nd_desc = dev_get_drvdata(dev);
1047 if (nd_desc) {
1048 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1049
Toshi Kani80790032017-06-29 20:41:30 -06001050 rc = acpi_nfit_ars_rescan(acpi_desc, 0);
Vishal Verma37b137f2016-07-23 21:51:42 -07001051 }
1052 device_unlock(dev);
1053 if (rc)
1054 return rc;
1055 return size;
1056}
1057static DEVICE_ATTR_RW(scrub);
1058
1059static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1060{
1061 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1062 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1063 | 1 << ND_CMD_ARS_STATUS;
1064
1065 return (nd_desc->cmd_mask & mask) == mask;
1066}
1067
1068static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1069{
1070 struct device *dev = container_of(kobj, struct device, kobj);
1071 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1072
1073 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1074 return 0;
1075 return a->mode;
1076}
1077
Dan Williams45def222015-04-26 19:26:48 -04001078static struct attribute *acpi_nfit_attributes[] = {
1079 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -07001080 &dev_attr_scrub.attr,
Vishal Verma9ffd6352016-09-30 17:19:29 -06001081 &dev_attr_hw_error_scrub.attr,
Jerry Hoemann41f95db2017-06-30 20:41:28 -07001082 &dev_attr_bus_dsm_mask.attr,
Dan Williams45def222015-04-26 19:26:48 -04001083 NULL,
1084};
1085
Arvind Yadav5e937462017-06-22 15:44:41 +05301086static const struct attribute_group acpi_nfit_attribute_group = {
Dan Williams45def222015-04-26 19:26:48 -04001087 .name = "nfit",
1088 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -07001089 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -04001090};
1091
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001092static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -04001093 &nvdimm_bus_attribute_group,
1094 &acpi_nfit_attribute_group,
1095 NULL,
1096};
1097
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001098static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1099{
1100 struct nvdimm *nvdimm = to_nvdimm(dev);
1101 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1102
1103 return __to_nfit_memdev(nfit_mem);
1104}
1105
1106static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1107{
1108 struct nvdimm *nvdimm = to_nvdimm(dev);
1109 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1110
1111 return nfit_mem->dcr;
1112}
1113
1114static ssize_t handle_show(struct device *dev,
1115 struct device_attribute *attr, char *buf)
1116{
1117 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1118
1119 return sprintf(buf, "%#x\n", memdev->device_handle);
1120}
1121static DEVICE_ATTR_RO(handle);
1122
1123static ssize_t phys_id_show(struct device *dev,
1124 struct device_attribute *attr, char *buf)
1125{
1126 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1127
1128 return sprintf(buf, "%#x\n", memdev->physical_id);
1129}
1130static DEVICE_ATTR_RO(phys_id);
1131
1132static ssize_t vendor_show(struct device *dev,
1133 struct device_attribute *attr, char *buf)
1134{
1135 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1136
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001137 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001138}
1139static DEVICE_ATTR_RO(vendor);
1140
1141static ssize_t rev_id_show(struct device *dev,
1142 struct device_attribute *attr, char *buf)
1143{
1144 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1145
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001146 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001147}
1148static DEVICE_ATTR_RO(rev_id);
1149
1150static ssize_t device_show(struct device *dev,
1151 struct device_attribute *attr, char *buf)
1152{
1153 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1154
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001155 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001156}
1157static DEVICE_ATTR_RO(device);
1158
Dan Williams6ca72082016-04-29 10:33:23 -07001159static ssize_t subsystem_vendor_show(struct device *dev,
1160 struct device_attribute *attr, char *buf)
1161{
1162 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1163
1164 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1165}
1166static DEVICE_ATTR_RO(subsystem_vendor);
1167
1168static ssize_t subsystem_rev_id_show(struct device *dev,
1169 struct device_attribute *attr, char *buf)
1170{
1171 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1172
1173 return sprintf(buf, "0x%04x\n",
1174 be16_to_cpu(dcr->subsystem_revision_id));
1175}
1176static DEVICE_ATTR_RO(subsystem_rev_id);
1177
1178static ssize_t subsystem_device_show(struct device *dev,
1179 struct device_attribute *attr, char *buf)
1180{
1181 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1182
1183 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1184}
1185static DEVICE_ATTR_RO(subsystem_device);
1186
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001187static int num_nvdimm_formats(struct nvdimm *nvdimm)
1188{
1189 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1190 int formats = 0;
1191
1192 if (nfit_mem->memdev_pmem)
1193 formats++;
1194 if (nfit_mem->memdev_bdw)
1195 formats++;
1196 return formats;
1197}
1198
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001199static ssize_t format_show(struct device *dev,
1200 struct device_attribute *attr, char *buf)
1201{
1202 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1203
Dan Williams1bcbf422016-06-29 11:19:32 -07001204 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001205}
1206static DEVICE_ATTR_RO(format);
1207
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001208static ssize_t format1_show(struct device *dev,
1209 struct device_attribute *attr, char *buf)
1210{
1211 u32 handle;
1212 ssize_t rc = -ENXIO;
1213 struct nfit_mem *nfit_mem;
1214 struct nfit_memdev *nfit_memdev;
1215 struct acpi_nfit_desc *acpi_desc;
1216 struct nvdimm *nvdimm = to_nvdimm(dev);
1217 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1218
1219 nfit_mem = nvdimm_provider_data(nvdimm);
1220 acpi_desc = nfit_mem->acpi_desc;
1221 handle = to_nfit_memdev(dev)->device_handle;
1222
1223 /* assumes DIMMs have at most 2 published interface codes */
1224 mutex_lock(&acpi_desc->init_mutex);
1225 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1226 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1227 struct nfit_dcr *nfit_dcr;
1228
1229 if (memdev->device_handle != handle)
1230 continue;
1231
1232 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1233 if (nfit_dcr->dcr->region_index != memdev->region_index)
1234 continue;
1235 if (nfit_dcr->dcr->code == dcr->code)
1236 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001237 rc = sprintf(buf, "0x%04x\n",
1238 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001239 break;
1240 }
1241 if (rc != ENXIO)
1242 break;
1243 }
1244 mutex_unlock(&acpi_desc->init_mutex);
1245 return rc;
1246}
1247static DEVICE_ATTR_RO(format1);
1248
1249static ssize_t formats_show(struct device *dev,
1250 struct device_attribute *attr, char *buf)
1251{
1252 struct nvdimm *nvdimm = to_nvdimm(dev);
1253
1254 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1255}
1256static DEVICE_ATTR_RO(formats);
1257
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001258static ssize_t serial_show(struct device *dev,
1259 struct device_attribute *attr, char *buf)
1260{
1261 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1262
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001263 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001264}
1265static DEVICE_ATTR_RO(serial);
1266
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001267static ssize_t family_show(struct device *dev,
1268 struct device_attribute *attr, char *buf)
1269{
1270 struct nvdimm *nvdimm = to_nvdimm(dev);
1271 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1272
1273 if (nfit_mem->family < 0)
1274 return -ENXIO;
1275 return sprintf(buf, "%d\n", nfit_mem->family);
1276}
1277static DEVICE_ATTR_RO(family);
1278
1279static ssize_t dsm_mask_show(struct device *dev,
1280 struct device_attribute *attr, char *buf)
1281{
1282 struct nvdimm *nvdimm = to_nvdimm(dev);
1283 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1284
1285 if (nfit_mem->family < 0)
1286 return -ENXIO;
1287 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1288}
1289static DEVICE_ATTR_RO(dsm_mask);
1290
Dan Williams58138822015-06-23 20:08:34 -04001291static ssize_t flags_show(struct device *dev,
1292 struct device_attribute *attr, char *buf)
1293{
1294 u16 flags = to_nfit_memdev(dev)->flags;
1295
Dan Williamsffab9382017-04-13 15:05:30 -07001296 return sprintf(buf, "%s%s%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001297 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1298 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1299 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001300 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Dan Williamsffab9382017-04-13 15:05:30 -07001301 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1302 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1303 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
Dan Williams58138822015-06-23 20:08:34 -04001304}
1305static DEVICE_ATTR_RO(flags);
1306
Toshi Kani38a879b2016-04-25 15:34:59 -06001307static ssize_t id_show(struct device *dev,
1308 struct device_attribute *attr, char *buf)
1309{
1310 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1311
1312 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1313 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1314 be16_to_cpu(dcr->vendor_id),
1315 dcr->manufacturing_location,
1316 be16_to_cpu(dcr->manufacturing_date),
1317 be32_to_cpu(dcr->serial_number));
1318 else
1319 return sprintf(buf, "%04x-%08x\n",
1320 be16_to_cpu(dcr->vendor_id),
1321 be32_to_cpu(dcr->serial_number));
1322}
1323static DEVICE_ATTR_RO(id);
1324
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001325static struct attribute *acpi_nfit_dimm_attributes[] = {
1326 &dev_attr_handle.attr,
1327 &dev_attr_phys_id.attr,
1328 &dev_attr_vendor.attr,
1329 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001330 &dev_attr_rev_id.attr,
1331 &dev_attr_subsystem_vendor.attr,
1332 &dev_attr_subsystem_device.attr,
1333 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001334 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001335 &dev_attr_formats.attr,
1336 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001337 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001338 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001339 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001340 &dev_attr_family.attr,
1341 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001342 NULL,
1343};
1344
1345static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1346 struct attribute *a, int n)
1347{
1348 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001349 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001350
Dan Williams14999342017-04-13 19:46:36 -07001351 if (!to_nfit_dcr(dev)) {
1352 /* Without a dcr only the memdev attributes can be surfaced */
1353 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1354 || a == &dev_attr_flags.attr
1355 || a == &dev_attr_family.attr
1356 || a == &dev_attr_dsm_mask.attr)
1357 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001358 return 0;
Dan Williams14999342017-04-13 19:46:36 -07001359 }
1360
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001361 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1362 return 0;
1363 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001364}
1365
Arvind Yadav5e937462017-06-22 15:44:41 +05301366static const struct attribute_group acpi_nfit_dimm_attribute_group = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001367 .name = "nfit",
1368 .attrs = acpi_nfit_dimm_attributes,
1369 .is_visible = acpi_nfit_dimm_attr_visible,
1370};
1371
1372static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001373 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001374 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001375 &acpi_nfit_dimm_attribute_group,
1376 NULL,
1377};
1378
1379static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1380 u32 device_handle)
1381{
1382 struct nfit_mem *nfit_mem;
1383
1384 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1385 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1386 return nfit_mem->nvdimm;
1387
1388 return NULL;
1389}
1390
Dan Williams231bf112016-08-22 19:23:25 -07001391void __acpi_nvdimm_notify(struct device *dev, u32 event)
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001392{
1393 struct nfit_mem *nfit_mem;
1394 struct acpi_nfit_desc *acpi_desc;
1395
1396 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1397 event);
1398
1399 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1400 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1401 event);
1402 return;
1403 }
1404
1405 acpi_desc = dev_get_drvdata(dev->parent);
1406 if (!acpi_desc)
1407 return;
1408
1409 /*
1410 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1411 * is still valid.
1412 */
1413 nfit_mem = dev_get_drvdata(dev);
1414 if (nfit_mem && nfit_mem->flags_attr)
1415 sysfs_notify_dirent(nfit_mem->flags_attr);
1416}
Dan Williams231bf112016-08-22 19:23:25 -07001417EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001418
1419static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1420{
1421 struct acpi_device *adev = data;
1422 struct device *dev = &adev->dev;
1423
1424 device_lock(dev->parent);
1425 __acpi_nvdimm_notify(dev, event);
1426 device_unlock(dev->parent);
1427}
1428
Dan Williams62232e452015-06-08 14:27:06 -04001429static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1430 struct nfit_mem *nfit_mem, u32 device_handle)
1431{
1432 struct acpi_device *adev, *adev_dimm;
1433 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001434 unsigned long dsm_mask;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001435 const guid_t *guid;
Linda Knippers60e95f42015-07-22 16:17:22 -04001436 int i;
Linda Knippersba650cf2017-03-07 16:35:13 -05001437 int family = -1;
Dan Williams62232e452015-06-08 14:27:06 -04001438
Dan Williamse3654ec2016-04-28 16:17:07 -07001439 /* nfit test assumes 1:1 relationship between commands and dsms */
1440 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001441 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001442 adev = to_acpi_dev(acpi_desc);
1443 if (!adev)
1444 return 0;
1445
1446 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1447 nfit_mem->adev = adev_dimm;
1448 if (!adev_dimm) {
1449 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1450 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001451 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001452 }
1453
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001454 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1455 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1456 dev_err(dev, "%s: notification registration failed\n",
1457 dev_name(&adev_dimm->dev));
1458 return -ENXIO;
1459 }
1460
Dan Williams31eca762016-04-28 16:23:43 -07001461 /*
stuart hayese02fb722016-05-26 11:38:41 -05001462 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001463 * different command sets. Note, that checking for function0 (bit0)
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001464 * tells us if any commands are reachable through this GUID.
Dan Williams31eca762016-04-28 16:23:43 -07001465 */
stuart hayese02fb722016-05-26 11:38:41 -05001466 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001467 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Linda Knippersba650cf2017-03-07 16:35:13 -05001468 if (family < 0 || i == default_dsm_family)
1469 family = i;
Dan Williams31eca762016-04-28 16:23:43 -07001470
1471 /* limit the supported commands to those that are publicly documented */
Linda Knippersba650cf2017-03-07 16:35:13 -05001472 nfit_mem->family = family;
Linda Knippers095ab4b2017-03-07 16:35:12 -05001473 if (override_dsm_mask && !disable_vendor_specific)
1474 dsm_mask = override_dsm_mask;
1475 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001476 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001477 if (disable_vendor_specific)
1478 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001479 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001480 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001481 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001482 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001483 if (disable_vendor_specific)
1484 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001485 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1486 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001487 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001488 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001489 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001490 /* DSMs are optional, continue loading the driver... */
1491 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001492 }
1493
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001494 guid = to_nfit_uuid(nfit_mem->family);
Dan Williams31eca762016-04-28 16:23:43 -07001495 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Andy Shevchenko94116f82017-06-05 19:40:46 +03001496 if (acpi_check_dsm(adev_dimm->handle, guid, 1, 1ULL << i))
Dan Williams62232e452015-06-08 14:27:06 -04001497 set_bit(i, &nfit_mem->dsm_mask);
1498
Linda Knippers60e95f42015-07-22 16:17:22 -04001499 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001500}
1501
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001502static void shutdown_dimm_notify(void *data)
1503{
1504 struct acpi_nfit_desc *acpi_desc = data;
1505 struct nfit_mem *nfit_mem;
1506
1507 mutex_lock(&acpi_desc->init_mutex);
1508 /*
1509 * Clear out the nfit_mem->flags_attr and shut down dimm event
1510 * notifications.
1511 */
1512 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williams231bf112016-08-22 19:23:25 -07001513 struct acpi_device *adev_dimm = nfit_mem->adev;
1514
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001515 if (nfit_mem->flags_attr) {
1516 sysfs_put(nfit_mem->flags_attr);
1517 nfit_mem->flags_attr = NULL;
1518 }
Dan Williams231bf112016-08-22 19:23:25 -07001519 if (adev_dimm)
1520 acpi_remove_notify_handler(adev_dimm->handle,
1521 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001522 }
1523 mutex_unlock(&acpi_desc->init_mutex);
1524}
1525
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001526static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1527{
1528 struct nfit_mem *nfit_mem;
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001529 int dimm_count = 0, rc;
1530 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001531
1532 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001533 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001534 unsigned long flags = 0, cmd_mask;
Dan Williamscaa603a2017-04-14 10:27:11 -07001535 struct nfit_memdev *nfit_memdev;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001536 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001537 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001538
1539 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1540 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1541 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001542 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001543 continue;
1544 }
1545
1546 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
Dan Williams8f078b32017-05-04 14:01:24 -07001547 set_bit(NDD_ALIASING, &flags);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001548
Dan Williamscaa603a2017-04-14 10:27:11 -07001549 /* collate flags across all memdevs for this dimm */
1550 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1551 struct acpi_nfit_memory_map *dimm_memdev;
1552
1553 dimm_memdev = __to_nfit_memdev(nfit_mem);
1554 if (dimm_memdev->device_handle
1555 != nfit_memdev->memdev->device_handle)
1556 continue;
1557 dimm_memdev->flags |= nfit_memdev->memdev->flags;
1558 }
1559
Dan Williams58138822015-06-23 20:08:34 -04001560 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001561 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams8f078b32017-05-04 14:01:24 -07001562 set_bit(NDD_UNARMED, &flags);
Dan Williams58138822015-06-23 20:08:34 -04001563
Dan Williams62232e452015-06-08 14:27:06 -04001564 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1565 if (rc)
1566 continue;
1567
Dan Williamse3654ec2016-04-28 16:17:07 -07001568 /*
Dan Williams31eca762016-04-28 16:23:43 -07001569 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1570 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1571 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001572 */
Dan Williams31eca762016-04-28 16:23:43 -07001573 cmd_mask = 1UL << ND_CMD_CALL;
1574 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1575 cmd_mask |= nfit_mem->dsm_mask;
1576
Dan Williamse5ae3b22016-06-07 17:00:04 -07001577 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1578 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001579 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001580 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001581 flags, cmd_mask, flush ? flush->hint_count : 0,
1582 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001583 if (!nvdimm)
1584 return -ENOMEM;
1585
1586 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001587 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001588
1589 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1590 continue;
1591
Dan Williams14999342017-04-13 19:46:36 -07001592 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001593 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001594 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1595 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1596 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Dan Williams14999342017-04-13 19:46:36 -07001597 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1598 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
Dan Williams58138822015-06-23 20:08:34 -04001599
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001600 }
1601
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001602 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1603 if (rc)
1604 return rc;
1605
1606 /*
1607 * Now that dimms are successfully registered, and async registration
1608 * is flushed, attempt to enable event notification.
1609 */
1610 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1611 struct kernfs_node *nfit_kernfs;
1612
1613 nvdimm = nfit_mem->nvdimm;
1614 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1615 if (nfit_kernfs)
1616 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1617 "flags");
1618 sysfs_put(nfit_kernfs);
1619 if (!nfit_mem->flags_attr)
1620 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1621 nvdimm_name(nvdimm));
1622 }
1623
1624 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1625 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001626}
1627
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001628/*
1629 * These constants are private because there are no kernel consumers of
1630 * these commands.
1631 */
1632enum nfit_aux_cmds {
1633 NFIT_CMD_TRANSLATE_SPA = 5,
1634 NFIT_CMD_ARS_INJECT_SET = 7,
1635 NFIT_CMD_ARS_INJECT_CLEAR = 8,
1636 NFIT_CMD_ARS_INJECT_GET = 9,
1637};
1638
Dan Williams62232e452015-06-08 14:27:06 -04001639static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1640{
1641 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001642 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
Dan Williams62232e452015-06-08 14:27:06 -04001643 struct acpi_device *adev;
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001644 unsigned long dsm_mask;
Dan Williams62232e452015-06-08 14:27:06 -04001645 int i;
1646
Dan Williamse3654ec2016-04-28 16:17:07 -07001647 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Yasunori Gotob37b3fd2017-09-22 16:47:40 +09001648 nd_desc->bus_dsm_mask = acpi_desc->bus_nfit_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001649 adev = to_acpi_dev(acpi_desc);
1650 if (!adev)
1651 return;
1652
Dan Williamsd4f32362016-03-03 16:08:54 -08001653 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Andy Shevchenko94116f82017-06-05 19:40:46 +03001654 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001655 set_bit(i, &nd_desc->cmd_mask);
Jerry Hoemann37d74842017-06-30 20:41:24 -07001656 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001657
1658 dsm_mask =
1659 (1 << ND_CMD_ARS_CAP) |
1660 (1 << ND_CMD_ARS_START) |
1661 (1 << ND_CMD_ARS_STATUS) |
1662 (1 << ND_CMD_CLEAR_ERROR) |
1663 (1 << NFIT_CMD_TRANSLATE_SPA) |
1664 (1 << NFIT_CMD_ARS_INJECT_SET) |
1665 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
1666 (1 << NFIT_CMD_ARS_INJECT_GET);
1667 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1668 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1669 set_bit(i, &nd_desc->bus_dsm_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001670}
1671
Dan Williams1f7df6f2015-06-09 20:13:14 -04001672static ssize_t range_index_show(struct device *dev,
1673 struct device_attribute *attr, char *buf)
1674{
1675 struct nd_region *nd_region = to_nd_region(dev);
1676 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1677
1678 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1679}
1680static DEVICE_ATTR_RO(range_index);
1681
Dan Williamsa15797f2017-08-31 12:53:36 -07001682static ssize_t ecc_unit_size_show(struct device *dev,
1683 struct device_attribute *attr, char *buf)
1684{
1685 struct nd_region *nd_region = to_nd_region(dev);
1686 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1687
1688 return sprintf(buf, "%d\n", nfit_spa->clear_err_unit);
1689}
1690static DEVICE_ATTR_RO(ecc_unit_size);
1691
Dan Williams1f7df6f2015-06-09 20:13:14 -04001692static struct attribute *acpi_nfit_region_attributes[] = {
1693 &dev_attr_range_index.attr,
Dan Williamsa15797f2017-08-31 12:53:36 -07001694 &dev_attr_ecc_unit_size.attr,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001695 NULL,
1696};
1697
Arvind Yadav5e937462017-06-22 15:44:41 +05301698static const struct attribute_group acpi_nfit_region_attribute_group = {
Dan Williams1f7df6f2015-06-09 20:13:14 -04001699 .name = "nfit",
1700 .attrs = acpi_nfit_region_attributes,
1701};
1702
1703static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1704 &nd_region_attribute_group,
1705 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001706 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001707 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001708 &acpi_nfit_region_attribute_group,
1709 NULL,
1710};
1711
Dan Williamseaf96152015-05-01 13:11:27 -04001712/* enough info to uniquely specify an interleave set */
1713struct nfit_set_info {
1714 struct nfit_set_info_map {
1715 u64 region_offset;
1716 u32 serial_number;
1717 u32 pad;
1718 } mapping[0];
1719};
1720
Dan Williamsc12c48c2017-06-04 10:59:15 +09001721struct nfit_set_info2 {
1722 struct nfit_set_info_map2 {
1723 u64 region_offset;
1724 u32 serial_number;
1725 u16 vendor_id;
1726 u16 manufacturing_date;
1727 u8 manufacturing_location;
1728 u8 reserved[31];
1729 } mapping[0];
1730};
1731
Dan Williamseaf96152015-05-01 13:11:27 -04001732static size_t sizeof_nfit_set_info(int num_mappings)
1733{
1734 return sizeof(struct nfit_set_info)
1735 + num_mappings * sizeof(struct nfit_set_info_map);
1736}
1737
Dan Williamsc12c48c2017-06-04 10:59:15 +09001738static size_t sizeof_nfit_set_info2(int num_mappings)
1739{
1740 return sizeof(struct nfit_set_info2)
1741 + num_mappings * sizeof(struct nfit_set_info_map2);
1742}
1743
Dan Williams86ef58a2017-02-28 18:32:48 -08001744static int cmp_map_compat(const void *m0, const void *m1)
Dan Williamseaf96152015-05-01 13:11:27 -04001745{
1746 const struct nfit_set_info_map *map0 = m0;
1747 const struct nfit_set_info_map *map1 = m1;
1748
1749 return memcmp(&map0->region_offset, &map1->region_offset,
1750 sizeof(u64));
1751}
1752
Dan Williams86ef58a2017-02-28 18:32:48 -08001753static int cmp_map(const void *m0, const void *m1)
1754{
1755 const struct nfit_set_info_map *map0 = m0;
1756 const struct nfit_set_info_map *map1 = m1;
1757
Dan Williamsb03b99a2017-03-27 21:53:38 -07001758 if (map0->region_offset < map1->region_offset)
1759 return -1;
1760 else if (map0->region_offset > map1->region_offset)
1761 return 1;
1762 return 0;
Dan Williams86ef58a2017-02-28 18:32:48 -08001763}
1764
Dan Williamsc12c48c2017-06-04 10:59:15 +09001765static int cmp_map2(const void *m0, const void *m1)
1766{
1767 const struct nfit_set_info_map2 *map0 = m0;
1768 const struct nfit_set_info_map2 *map1 = m1;
1769
1770 if (map0->region_offset < map1->region_offset)
1771 return -1;
1772 else if (map0->region_offset > map1->region_offset)
1773 return 1;
1774 return 0;
1775}
1776
Dan Williamseaf96152015-05-01 13:11:27 -04001777/* Retrieve the nth entry referencing this spa */
1778static struct acpi_nfit_memory_map *memdev_from_spa(
1779 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1780{
1781 struct nfit_memdev *nfit_memdev;
1782
1783 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1784 if (nfit_memdev->memdev->range_index == range_index)
1785 if (n-- == 0)
1786 return nfit_memdev->memdev;
1787 return NULL;
1788}
1789
1790static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1791 struct nd_region_desc *ndr_desc,
1792 struct acpi_nfit_system_address *spa)
1793{
Dan Williamseaf96152015-05-01 13:11:27 -04001794 struct device *dev = acpi_desc->dev;
1795 struct nd_interleave_set *nd_set;
1796 u16 nr = ndr_desc->num_mappings;
Dan Williamsc12c48c2017-06-04 10:59:15 +09001797 struct nfit_set_info2 *info2;
Dan Williamseaf96152015-05-01 13:11:27 -04001798 struct nfit_set_info *info;
Dan Williams8f2bc242017-06-06 11:39:30 -07001799 int i;
Dan Williamseaf96152015-05-01 13:11:27 -04001800
Dan Williamsfaec6f82017-06-06 11:10:51 -07001801 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1802 if (!nd_set)
1803 return -ENOMEM;
1804 ndr_desc->nd_set = nd_set;
1805 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
1806
Dan Williamseaf96152015-05-01 13:11:27 -04001807 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1808 if (!info)
1809 return -ENOMEM;
Dan Williamsc12c48c2017-06-04 10:59:15 +09001810
1811 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
1812 if (!info2)
1813 return -ENOMEM;
1814
Dan Williamseaf96152015-05-01 13:11:27 -04001815 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07001816 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04001817 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williamsc12c48c2017-06-04 10:59:15 +09001818 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07001819 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04001820 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1821 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1822 spa->range_index, i);
Dan Williamsdcb79b12017-08-07 14:27:57 -07001823 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
Dan Williamseaf96152015-05-01 13:11:27 -04001824
1825 if (!memdev || !nfit_mem->dcr) {
1826 dev_err(dev, "%s: failed to find DCR\n", __func__);
1827 return -ENODEV;
1828 }
1829
1830 map->region_offset = memdev->region_offset;
Dan Williamsdcb79b12017-08-07 14:27:57 -07001831 map->serial_number = dcr->serial_number;
Dan Williamsc12c48c2017-06-04 10:59:15 +09001832
1833 map2->region_offset = memdev->region_offset;
Dan Williamsdcb79b12017-08-07 14:27:57 -07001834 map2->serial_number = dcr->serial_number;
1835 map2->vendor_id = dcr->vendor_id;
1836 map2->manufacturing_date = dcr->manufacturing_date;
1837 map2->manufacturing_location = dcr->manufacturing_location;
Dan Williamseaf96152015-05-01 13:11:27 -04001838 }
1839
Dan Williamsc12c48c2017-06-04 10:59:15 +09001840 /* v1.1 namespaces */
Dan Williamseaf96152015-05-01 13:11:27 -04001841 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1842 cmp_map, NULL);
Dan Williamsc12c48c2017-06-04 10:59:15 +09001843 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
Dan Williams86ef58a2017-02-28 18:32:48 -08001844
Dan Williamsc12c48c2017-06-04 10:59:15 +09001845 /* v1.2 namespaces */
1846 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
1847 cmp_map2, NULL);
1848 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
1849
1850 /* support v1.1 namespaces created with the wrong sort order */
Dan Williams86ef58a2017-02-28 18:32:48 -08001851 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1852 cmp_map_compat, NULL);
1853 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1854
Dan Williams401c0a12017-08-04 17:20:16 -07001855 /* record the result of the sort for the mapping position */
1856 for (i = 0; i < nr; i++) {
1857 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
1858 int j;
1859
1860 for (j = 0; j < nr; j++) {
1861 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
1862 struct nvdimm *nvdimm = mapping->nvdimm;
1863 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
Dan Williamsdcb79b12017-08-07 14:27:57 -07001864 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
Dan Williams401c0a12017-08-04 17:20:16 -07001865
Dan Williamsdcb79b12017-08-07 14:27:57 -07001866 if (map2->serial_number == dcr->serial_number &&
1867 map2->vendor_id == dcr->vendor_id &&
1868 map2->manufacturing_date == dcr->manufacturing_date &&
Dan Williams401c0a12017-08-04 17:20:16 -07001869 map2->manufacturing_location
Dan Williamsdcb79b12017-08-07 14:27:57 -07001870 == dcr->manufacturing_location) {
Dan Williams401c0a12017-08-04 17:20:16 -07001871 mapping->position = i;
1872 break;
1873 }
1874 }
1875 }
1876
Dan Williamseaf96152015-05-01 13:11:27 -04001877 ndr_desc->nd_set = nd_set;
1878 devm_kfree(dev, info);
Dan Williamsc12c48c2017-06-04 10:59:15 +09001879 devm_kfree(dev, info2);
Dan Williamseaf96152015-05-01 13:11:27 -04001880
1881 return 0;
1882}
1883
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001884static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1885{
1886 struct acpi_nfit_interleave *idt = mmio->idt;
1887 u32 sub_line_offset, line_index, line_offset;
1888 u64 line_no, table_skip_count, table_offset;
1889
1890 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1891 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1892 line_offset = idt->line_offset[line_index]
1893 * mmio->line_size;
1894 table_offset = table_skip_count * mmio->table_size;
1895
1896 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1897}
1898
Ross Zwislerde4a1962015-08-20 16:27:38 -06001899static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001900{
1901 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1902 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001903 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001904
1905 if (mmio->num_lines)
1906 offset = to_interleave_offset(offset, mmio);
1907
Ross Zwisler68202c92016-07-29 14:59:12 -06001908 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001909}
1910
1911static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1912 resource_size_t dpa, unsigned int len, unsigned int write)
1913{
1914 u64 cmd, offset;
1915 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1916
1917 enum {
1918 BCW_OFFSET_MASK = (1ULL << 48)-1,
1919 BCW_LEN_SHIFT = 48,
1920 BCW_LEN_MASK = (1ULL << 8) - 1,
1921 BCW_CMD_SHIFT = 56,
1922 };
1923
1924 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1925 len = len >> L1_CACHE_SHIFT;
1926 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1927 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1928
1929 offset = nfit_blk->cmd_offset + mmio->size * bw;
1930 if (mmio->num_lines)
1931 offset = to_interleave_offset(offset, mmio);
1932
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001933 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001934 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001935
Dan Williamsaef25332016-02-12 17:01:11 -08001936 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001937 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001938}
1939
1940static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1941 resource_size_t dpa, void *iobuf, size_t len, int rw,
1942 unsigned int lane)
1943{
1944 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1945 unsigned int copied = 0;
1946 u64 base_offset;
1947 int rc;
1948
1949 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1950 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001951 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1952 while (len) {
1953 unsigned int c;
1954 u64 offset;
1955
1956 if (mmio->num_lines) {
1957 u32 line_offset;
1958
1959 offset = to_interleave_offset(base_offset + copied,
1960 mmio);
1961 div_u64_rem(offset, mmio->line_size, &line_offset);
1962 c = min_t(size_t, len, mmio->line_size - line_offset);
1963 } else {
1964 offset = base_offset + nfit_blk->bdw_offset;
1965 c = len;
1966 }
1967
1968 if (rw)
Dan Williams0aed55a2017-05-29 12:22:50 -07001969 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001970 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001971 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Robin Murphy5deb67f2017-08-31 12:27:09 +01001972 arch_invalidate_pmem((void __force *)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001973 mmio->addr.aperture + offset, c);
1974
Dan Williams6abccd12017-01-13 14:14:23 -08001975 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001976 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001977
1978 copied += c;
1979 len -= c;
1980 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001981
1982 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001983 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001984
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001985 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1986 return rc;
1987}
1988
1989static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1990 resource_size_t dpa, void *iobuf, u64 len, int rw)
1991{
1992 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1993 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1994 struct nd_region *nd_region = nfit_blk->nd_region;
1995 unsigned int lane, copied = 0;
1996 int rc = 0;
1997
1998 lane = nd_region_acquire_lane(nd_region);
1999 while (len) {
2000 u64 c = min(len, mmio->size);
2001
2002 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2003 iobuf + copied, c, rw, lane);
2004 if (rc)
2005 break;
2006
2007 copied += c;
2008 len -= c;
2009 }
2010 nd_region_release_lane(nd_region, lane);
2011
2012 return rc;
2013}
2014
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002015static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2016 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2017{
2018 if (idt) {
2019 mmio->num_lines = idt->line_count;
2020 mmio->line_size = idt->line_size;
2021 if (interleave_ways == 0)
2022 return -ENXIO;
2023 mmio->table_size = mmio->num_lines * interleave_ways
2024 * mmio->line_size;
2025 }
2026
2027 return 0;
2028}
2029
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002030static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2031 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2032{
2033 struct nd_cmd_dimm_flags flags;
2034 int rc;
2035
2036 memset(&flags, 0, sizeof(flags));
2037 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08002038 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002039
2040 if (rc >= 0 && flags.status == 0)
2041 nfit_blk->dimm_flags = flags.flags;
2042 else if (rc == -ENOTTY) {
2043 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08002044 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002045 rc = 0;
2046 } else
2047 rc = -ENXIO;
2048
2049 return rc;
2050}
2051
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002052static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2053 struct device *dev)
2054{
2055 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002056 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2057 struct nfit_blk_mmio *mmio;
2058 struct nfit_blk *nfit_blk;
2059 struct nfit_mem *nfit_mem;
2060 struct nvdimm *nvdimm;
2061 int rc;
2062
2063 nvdimm = nd_blk_region_to_dimm(ndbr);
2064 nfit_mem = nvdimm_provider_data(nvdimm);
2065 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2066 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
2067 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04002068 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2069 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002070 return -ENXIO;
2071 }
2072
2073 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2074 if (!nfit_blk)
2075 return -ENOMEM;
2076 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2077 nfit_blk->nd_region = to_nd_region(dev);
2078
2079 /* map block aperture memory */
2080 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2081 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07002082 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
Dan Williamsca6a4652017-01-13 20:36:58 -08002083 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002084 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002085 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
2086 nvdimm_name(nvdimm));
2087 return -ENOMEM;
2088 }
2089 mmio->size = nfit_mem->bdw->size;
2090 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2091 mmio->idt = nfit_mem->idt_bdw;
2092 mmio->spa = nfit_mem->spa_bdw;
2093 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2094 nfit_mem->memdev_bdw->interleave_ways);
2095 if (rc) {
2096 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
2097 __func__, nvdimm_name(nvdimm));
2098 return rc;
2099 }
2100
2101 /* map block control memory */
2102 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2103 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2104 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07002105 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2106 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002107 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002108 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
2109 nvdimm_name(nvdimm));
2110 return -ENOMEM;
2111 }
2112 mmio->size = nfit_mem->dcr->window_size;
2113 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2114 mmio->idt = nfit_mem->idt_dcr;
2115 mmio->spa = nfit_mem->spa_dcr;
2116 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2117 nfit_mem->memdev_dcr->interleave_ways);
2118 if (rc) {
2119 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
2120 __func__, nvdimm_name(nvdimm));
2121 return rc;
2122 }
2123
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002124 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2125 if (rc < 0) {
2126 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
2127 __func__, nvdimm_name(nvdimm));
2128 return rc;
2129 }
2130
Dan Williamsf284a4f2016-07-07 19:44:50 -07002131 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002132 dev_warn(dev, "unable to guarantee persistence of writes\n");
2133
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002134 if (mmio->line_size == 0)
2135 return 0;
2136
2137 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2138 + 8 > mmio->line_size) {
2139 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2140 return -ENXIO;
2141 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2142 + 8 > mmio->line_size) {
2143 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2144 return -ENXIO;
2145 }
2146
2147 return 0;
2148}
2149
Dan Williamsaef25332016-02-12 17:01:11 -08002150static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002151 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002152{
Dan Williamsaef25332016-02-12 17:01:11 -08002153 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002154 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08002155 int cmd_rc, rc;
2156
Dan Williams1cf03c02016-02-17 13:01:23 -08002157 cmd->address = spa->address;
2158 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08002159 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2160 sizeof(*cmd), &cmd_rc);
2161 if (rc < 0)
2162 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002163 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002164}
2165
Dan Williams1cf03c02016-02-17 13:01:23 -08002166static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002167{
2168 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002169 int cmd_rc;
2170 struct nd_cmd_ars_start ars_start;
2171 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2172 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002173
Dan Williams1cf03c02016-02-17 13:01:23 -08002174 memset(&ars_start, 0, sizeof(ars_start));
2175 ars_start.address = spa->address;
2176 ars_start.length = spa->length;
Toshi Kani80790032017-06-29 20:41:30 -06002177 ars_start.flags = acpi_desc->ars_start_flags;
Dan Williams1cf03c02016-02-17 13:01:23 -08002178 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2179 ars_start.type = ND_ARS_PERSISTENT;
2180 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2181 ars_start.type = ND_ARS_VOLATILE;
2182 else
2183 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07002184
Dan Williams1cf03c02016-02-17 13:01:23 -08002185 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2186 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08002187
Dan Williams1cf03c02016-02-17 13:01:23 -08002188 if (rc < 0)
2189 return rc;
2190 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002191}
2192
Dan Williams1cf03c02016-02-17 13:01:23 -08002193static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07002194{
Dan Williamsaef25332016-02-12 17:01:11 -08002195 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002196 struct nd_cmd_ars_start ars_start;
2197 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2198 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07002199
Dan Williams1cf03c02016-02-17 13:01:23 -08002200 memset(&ars_start, 0, sizeof(ars_start));
2201 ars_start.address = ars_status->restart_address;
2202 ars_start.length = ars_status->restart_length;
2203 ars_start.type = ars_status->type;
Toshi Kani80790032017-06-29 20:41:30 -06002204 ars_start.flags = acpi_desc->ars_start_flags;
Dan Williams1cf03c02016-02-17 13:01:23 -08002205 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2206 sizeof(ars_start), &cmd_rc);
2207 if (rc < 0)
2208 return rc;
2209 return cmd_rc;
2210}
Dan Williamsaef25332016-02-12 17:01:11 -08002211
Dan Williams1cf03c02016-02-17 13:01:23 -08002212static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2213{
2214 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2215 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2216 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08002217
Dan Williams1cf03c02016-02-17 13:01:23 -08002218 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2219 acpi_desc->ars_status_size, &cmd_rc);
2220 if (rc < 0)
2221 return rc;
2222 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002223}
2224
Dan Williams82aa37c2016-12-06 12:45:24 -08002225static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002226 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07002227{
Dan Williams82aa37c2016-12-06 12:45:24 -08002228 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
Vishal Verma0caeef62015-12-24 19:21:43 -07002229 int rc;
2230 u32 i;
2231
Dan Williams82aa37c2016-12-06 12:45:24 -08002232 /*
2233 * First record starts at 44 byte offset from the start of the
2234 * payload.
2235 */
2236 if (ars_status->out_length < 44)
2237 return 0;
Vishal Verma0caeef62015-12-24 19:21:43 -07002238 for (i = 0; i < ars_status->num_records; i++) {
Dan Williams82aa37c2016-12-06 12:45:24 -08002239 /* only process full records */
2240 if (ars_status->out_length
2241 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2242 break;
Vishal Verma0caeef62015-12-24 19:21:43 -07002243 rc = nvdimm_bus_add_poison(nvdimm_bus,
2244 ars_status->records[i].err_address,
2245 ars_status->records[i].length);
2246 if (rc)
2247 return rc;
2248 }
Dan Williams82aa37c2016-12-06 12:45:24 -08002249 if (i < ars_status->num_records)
2250 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
Vishal Verma0caeef62015-12-24 19:21:43 -07002251
2252 return 0;
2253}
2254
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002255static void acpi_nfit_remove_resource(void *data)
2256{
2257 struct resource *res = data;
2258
2259 remove_resource(res);
2260}
2261
2262static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2263 struct nd_region_desc *ndr_desc)
2264{
2265 struct resource *res, *nd_res = ndr_desc->res;
2266 int is_pmem, ret;
2267
2268 /* No operation if the region is already registered as PMEM */
2269 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2270 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2271 if (is_pmem == REGION_INTERSECTS)
2272 return 0;
2273
2274 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2275 if (!res)
2276 return -ENOMEM;
2277
2278 res->name = "Persistent Memory";
2279 res->start = nd_res->start;
2280 res->end = nd_res->end;
2281 res->flags = IORESOURCE_MEM;
2282 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2283
2284 ret = insert_resource(&iomem_resource, res);
2285 if (ret)
2286 return ret;
2287
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302288 ret = devm_add_action_or_reset(acpi_desc->dev,
2289 acpi_nfit_remove_resource,
2290 res);
2291 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002292 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002293
2294 return 0;
2295}
2296
Dan Williams1f7df6f2015-06-09 20:13:14 -04002297static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002298 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002299 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002300 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002301{
2302 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2303 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002304 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002305 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002306 struct nfit_mem *nfit_mem;
Dan Williamsfaec6f82017-06-06 11:10:51 -07002307 int blk_valid = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002308
2309 if (!nvdimm) {
2310 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2311 spa->range_index, memdev->device_handle);
2312 return -ENODEV;
2313 }
2314
Dan Williams44c462e2016-09-19 16:38:50 -07002315 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002316 switch (nfit_spa_type(spa)) {
2317 case NFIT_SPA_PM:
2318 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002319 mapping->start = memdev->address;
2320 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002321 break;
2322 case NFIT_SPA_DCR:
2323 nfit_mem = nvdimm_provider_data(nvdimm);
2324 if (!nfit_mem || !nfit_mem->bdw) {
2325 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2326 spa->range_index, nvdimm_name(nvdimm));
2327 } else {
Dan Williams44c462e2016-09-19 16:38:50 -07002328 mapping->size = nfit_mem->bdw->capacity;
2329 mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04002330 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002331 blk_valid = 1;
2332 }
2333
Dan Williams44c462e2016-09-19 16:38:50 -07002334 ndr_desc->mapping = mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002335 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002336 ndbr_desc = to_blk_region_desc(ndr_desc);
2337 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002338 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williamsfaec6f82017-06-06 11:10:51 -07002339 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2340 if (rc)
2341 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002342 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2343 ndr_desc);
2344 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002345 return -ENOMEM;
2346 break;
2347 }
2348
2349 return 0;
2350}
2351
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002352static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2353{
2354 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2355 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2356 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2357 nfit_spa_type(spa) == NFIT_SPA_PCD);
2358}
2359
Dan Williamsc9e582a2017-05-29 23:12:19 -07002360static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2361{
2362 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2363 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2364 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2365}
2366
Dan Williams1f7df6f2015-06-09 20:13:14 -04002367static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2368 struct nfit_spa *nfit_spa)
2369{
Dan Williams44c462e2016-09-19 16:38:50 -07002370 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002371 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002372 struct nd_blk_region_desc ndbr_desc;
2373 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002374 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002375 struct nvdimm_bus *nvdimm_bus;
2376 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002377 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002378
Dan Williams1cf03c02016-02-17 13:01:23 -08002379 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002380 return 0;
2381
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002382 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04002383 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2384 __func__);
2385 return 0;
2386 }
2387
2388 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002389 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002390 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002391 res.start = spa->address;
2392 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002393 ndr_desc = &ndbr_desc.ndr_desc;
2394 ndr_desc->res = &res;
2395 ndr_desc->provider_data = nfit_spa;
2396 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002397 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2398 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2399 spa->proximity_domain);
2400 else
2401 ndr_desc->numa_node = NUMA_NO_NODE;
2402
Dan Williams1f7df6f2015-06-09 20:13:14 -04002403 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2404 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002405 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002406
2407 if (memdev->range_index != spa->range_index)
2408 continue;
2409 if (count >= ND_MAX_MAPPINGS) {
2410 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2411 spa->range_index, ND_MAX_MAPPINGS);
2412 return -ENXIO;
2413 }
Dan Williams44c462e2016-09-19 16:38:50 -07002414 mapping = &mappings[count++];
2415 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002416 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002417 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002418 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002419 }
2420
Dan Williams44c462e2016-09-19 16:38:50 -07002421 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002422 ndr_desc->num_mappings = count;
2423 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002424 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002425 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002426
Dan Williams1f7df6f2015-06-09 20:13:14 -04002427 nvdimm_bus = acpi_desc->nvdimm_bus;
2428 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002429 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002430 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002431 dev_warn(acpi_desc->dev,
2432 "failed to insert pmem resource to iomem: %d\n",
2433 rc);
Dan Williams48901162016-03-09 17:15:43 -08002434 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002435 }
Dan Williams48901162016-03-09 17:15:43 -08002436
Dan Williams1cf03c02016-02-17 13:01:23 -08002437 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2438 ndr_desc);
2439 if (!nfit_spa->nd_region)
2440 rc = -ENOMEM;
Dan Williamsc9e582a2017-05-29 23:12:19 -07002441 } else if (nfit_spa_is_volatile(spa)) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002442 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2443 ndr_desc);
2444 if (!nfit_spa->nd_region)
2445 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002446 } else if (nfit_spa_is_virtual(spa)) {
2447 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2448 ndr_desc);
2449 if (!nfit_spa->nd_region)
2450 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002451 }
Vishal Verma20985162015-10-27 16:58:27 -06002452
Dan Williams1cf03c02016-02-17 13:01:23 -08002453 out:
2454 if (rc)
2455 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2456 nfit_spa->spa->range_index);
2457 return rc;
2458}
2459
2460static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2461 u32 max_ars)
2462{
2463 struct device *dev = acpi_desc->dev;
2464 struct nd_cmd_ars_status *ars_status;
2465
2466 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2467 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2468 return 0;
2469 }
2470
2471 if (acpi_desc->ars_status)
2472 devm_kfree(dev, acpi_desc->ars_status);
2473 acpi_desc->ars_status = NULL;
2474 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2475 if (!ars_status)
2476 return -ENOMEM;
2477 acpi_desc->ars_status = ars_status;
2478 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002479 return 0;
2480}
2481
Dan Williams1cf03c02016-02-17 13:01:23 -08002482static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2483 struct nfit_spa *nfit_spa)
2484{
2485 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2486 int rc;
2487
2488 if (!nfit_spa->max_ars) {
2489 struct nd_cmd_ars_cap ars_cap;
2490
2491 memset(&ars_cap, 0, sizeof(ars_cap));
2492 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2493 if (rc < 0)
2494 return rc;
2495 nfit_spa->max_ars = ars_cap.max_ars_out;
2496 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2497 /* check that the supported scrub types match the spa type */
2498 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2499 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2500 return -ENOTTY;
2501 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2502 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2503 return -ENOTTY;
2504 }
2505
2506 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2507 return -ENOMEM;
2508
2509 rc = ars_get_status(acpi_desc);
2510 if (rc < 0 && rc != -ENOSPC)
2511 return rc;
2512
Dan Williams82aa37c2016-12-06 12:45:24 -08002513 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
Dan Williams1cf03c02016-02-17 13:01:23 -08002514 return -ENOMEM;
2515
2516 return 0;
2517}
2518
2519static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2520 struct nfit_spa *nfit_spa)
2521{
2522 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2523 unsigned int overflow_retry = scrub_overflow_abort;
2524 u64 init_ars_start = 0, init_ars_len = 0;
2525 struct device *dev = acpi_desc->dev;
2526 unsigned int tmo = scrub_timeout;
2527 int rc;
2528
Vishal Verma37b137f2016-07-23 21:51:42 -07002529 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002530 return;
2531
2532 rc = ars_start(acpi_desc, nfit_spa);
2533 /*
2534 * If we timed out the initial scan we'll still be busy here,
2535 * and will wait another timeout before giving up permanently.
2536 */
2537 if (rc < 0 && rc != -EBUSY)
2538 return;
2539
2540 do {
2541 u64 ars_start, ars_len;
2542
2543 if (acpi_desc->cancel)
2544 break;
2545 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2546 if (rc == -ENOTTY)
2547 break;
2548 if (rc == -EBUSY && !tmo) {
2549 dev_warn(dev, "range %d ars timeout, aborting\n",
2550 spa->range_index);
2551 break;
2552 }
2553
2554 if (rc == -EBUSY) {
2555 /*
2556 * Note, entries may be appended to the list
2557 * while the lock is dropped, but the workqueue
2558 * being active prevents entries being deleted /
2559 * freed.
2560 */
2561 mutex_unlock(&acpi_desc->init_mutex);
2562 ssleep(1);
2563 tmo--;
2564 mutex_lock(&acpi_desc->init_mutex);
2565 continue;
2566 }
2567
2568 /* we got some results, but there are more pending... */
2569 if (rc == -ENOSPC && overflow_retry--) {
2570 if (!init_ars_len) {
2571 init_ars_len = acpi_desc->ars_status->length;
2572 init_ars_start = acpi_desc->ars_status->address;
2573 }
2574 rc = ars_continue(acpi_desc);
2575 }
2576
2577 if (rc < 0) {
2578 dev_warn(dev, "range %d ars continuation failed\n",
2579 spa->range_index);
2580 break;
2581 }
2582
2583 if (init_ars_len) {
2584 ars_start = init_ars_start;
2585 ars_len = init_ars_len;
2586 } else {
2587 ars_start = acpi_desc->ars_status->address;
2588 ars_len = acpi_desc->ars_status->length;
2589 }
2590 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2591 spa->range_index, ars_start, ars_len);
2592 /* notify the region about new poison entries */
2593 nvdimm_region_notify(nfit_spa->nd_region,
2594 NVDIMM_REVALIDATE_POISON);
2595 break;
2596 } while (1);
2597}
2598
2599static void acpi_nfit_scrub(struct work_struct *work)
2600{
2601 struct device *dev;
2602 u64 init_scrub_length = 0;
2603 struct nfit_spa *nfit_spa;
2604 u64 init_scrub_address = 0;
2605 bool init_ars_done = false;
2606 struct acpi_nfit_desc *acpi_desc;
2607 unsigned int tmo = scrub_timeout;
2608 unsigned int overflow_retry = scrub_overflow_abort;
2609
2610 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2611 dev = acpi_desc->dev;
2612
2613 /*
2614 * We scrub in 2 phases. The first phase waits for any platform
2615 * firmware initiated scrubs to complete and then we go search for the
2616 * affected spa regions to mark them scanned. In the second phase we
2617 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002618 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2619 * the first phase, but really only care about running phase 2, where
2620 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002621 */
2622
2623 /* process platform firmware initiated scrubs */
2624 retry:
2625 mutex_lock(&acpi_desc->init_mutex);
2626 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2627 struct nd_cmd_ars_status *ars_status;
2628 struct acpi_nfit_system_address *spa;
2629 u64 ars_start, ars_len;
2630 int rc;
2631
2632 if (acpi_desc->cancel)
2633 break;
2634
2635 if (nfit_spa->nd_region)
2636 continue;
2637
2638 if (init_ars_done) {
2639 /*
2640 * No need to re-query, we're now just
2641 * reconciling all the ranges covered by the
2642 * initial scrub
2643 */
2644 rc = 0;
2645 } else
2646 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2647
2648 if (rc == -ENOTTY) {
2649 /* no ars capability, just register spa and move on */
2650 acpi_nfit_register_region(acpi_desc, nfit_spa);
2651 continue;
2652 }
2653
2654 if (rc == -EBUSY && !tmo) {
2655 /* fallthrough to directed scrub in phase 2 */
2656 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2657 break;
2658 } else if (rc == -EBUSY) {
2659 mutex_unlock(&acpi_desc->init_mutex);
2660 ssleep(1);
2661 tmo--;
2662 goto retry;
2663 }
2664
2665 /* we got some results, but there are more pending... */
2666 if (rc == -ENOSPC && overflow_retry--) {
2667 ars_status = acpi_desc->ars_status;
2668 /*
2669 * Record the original scrub range, so that we
2670 * can recall all the ranges impacted by the
2671 * initial scrub.
2672 */
2673 if (!init_scrub_length) {
2674 init_scrub_length = ars_status->length;
2675 init_scrub_address = ars_status->address;
2676 }
2677 rc = ars_continue(acpi_desc);
2678 if (rc == 0) {
2679 mutex_unlock(&acpi_desc->init_mutex);
2680 goto retry;
2681 }
2682 }
2683
2684 if (rc < 0) {
2685 /*
2686 * Initial scrub failed, we'll give it one more
2687 * try below...
2688 */
2689 break;
2690 }
2691
2692 /* We got some final results, record completed ranges */
2693 ars_status = acpi_desc->ars_status;
2694 if (init_scrub_length) {
2695 ars_start = init_scrub_address;
2696 ars_len = ars_start + init_scrub_length;
2697 } else {
2698 ars_start = ars_status->address;
2699 ars_len = ars_status->length;
2700 }
2701 spa = nfit_spa->spa;
2702
2703 if (!init_ars_done) {
2704 init_ars_done = true;
2705 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2706 ars_start, ars_len);
2707 }
2708 if (ars_start <= spa->address && ars_start + ars_len
2709 >= spa->address + spa->length)
2710 acpi_nfit_register_region(acpi_desc, nfit_spa);
2711 }
2712
2713 /*
2714 * For all the ranges not covered by an initial scrub we still
2715 * want to see if there are errors, but it's ok to discover them
2716 * asynchronously.
2717 */
2718 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2719 /*
2720 * Flag all the ranges that still need scrubbing, but
2721 * register them now to make data available.
2722 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002723 if (!nfit_spa->nd_region) {
2724 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002725 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002726 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002727 }
Dan Williams9ccaed42017-04-13 22:48:46 -07002728 acpi_desc->init_complete = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002729
2730 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2731 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002732 acpi_desc->scrub_count++;
Toshi Kani80790032017-06-29 20:41:30 -06002733 acpi_desc->ars_start_flags = 0;
Vishal Verma37b137f2016-07-23 21:51:42 -07002734 if (acpi_desc->scrub_count_state)
2735 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002736 mutex_unlock(&acpi_desc->init_mutex);
2737}
2738
Dan Williams1f7df6f2015-06-09 20:13:14 -04002739static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2740{
2741 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002742 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002743
Dan Williams1cf03c02016-02-17 13:01:23 -08002744 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2745 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2746 /* BLK regions don't need to wait for ars results */
2747 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2748 if (rc)
2749 return rc;
2750 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002751
Toshi Kani80790032017-06-29 20:41:30 -06002752 acpi_desc->ars_start_flags = 0;
Dan Williamsfbabd822017-04-18 09:56:31 -07002753 if (!acpi_desc->cancel)
2754 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002755 return 0;
2756}
2757
Vishal Verma20985162015-10-27 16:58:27 -06002758static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2759 struct nfit_table_prev *prev)
2760{
2761 struct device *dev = acpi_desc->dev;
2762
2763 if (!list_empty(&prev->spas) ||
2764 !list_empty(&prev->memdevs) ||
2765 !list_empty(&prev->dcrs) ||
2766 !list_empty(&prev->bdws) ||
2767 !list_empty(&prev->idts) ||
2768 !list_empty(&prev->flushes)) {
2769 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2770 return -ENXIO;
2771 }
2772 return 0;
2773}
2774
Vishal Verma37b137f2016-07-23 21:51:42 -07002775static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2776{
2777 struct device *dev = acpi_desc->dev;
2778 struct kernfs_node *nfit;
2779 struct device *bus_dev;
2780
2781 if (!ars_supported(acpi_desc->nvdimm_bus))
2782 return 0;
2783
2784 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2785 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2786 if (!nfit) {
2787 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2788 return -ENODEV;
2789 }
2790 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2791 sysfs_put(nfit);
2792 if (!acpi_desc->scrub_count_state) {
2793 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2794 return -ENODEV;
2795 }
2796
2797 return 0;
2798}
2799
Dan Williamsfbabd822017-04-18 09:56:31 -07002800static void acpi_nfit_unregister(void *data)
Dan Williams58cd71b2016-07-21 18:05:36 -07002801{
2802 struct acpi_nfit_desc *acpi_desc = data;
2803
Dan Williams58cd71b2016-07-21 18:05:36 -07002804 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002805}
2806
Dan Williamse7a11b42016-07-14 16:19:55 -07002807int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002808{
2809 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002810 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002811 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002812 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002813
Dan Williams58cd71b2016-07-21 18:05:36 -07002814 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002815 acpi_nfit_init_dsms(acpi_desc);
2816
Dan Williams58cd71b2016-07-21 18:05:36 -07002817 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2818 &acpi_desc->nd_desc);
2819 if (!acpi_desc->nvdimm_bus)
2820 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002821
Dan Williamsfbabd822017-04-18 09:56:31 -07002822 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
Dan Williams58cd71b2016-07-21 18:05:36 -07002823 acpi_desc);
2824 if (rc)
2825 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002826
2827 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2828 if (rc)
2829 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002830
2831 /* register this acpi_desc for mce notifications */
2832 mutex_lock(&acpi_desc_lock);
2833 list_add_tail(&acpi_desc->list, &acpi_descs);
2834 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002835 }
2836
Vishal Verma20985162015-10-27 16:58:27 -06002837 mutex_lock(&acpi_desc->init_mutex);
2838
2839 INIT_LIST_HEAD(&prev.spas);
2840 INIT_LIST_HEAD(&prev.memdevs);
2841 INIT_LIST_HEAD(&prev.dcrs);
2842 INIT_LIST_HEAD(&prev.bdws);
2843 INIT_LIST_HEAD(&prev.idts);
2844 INIT_LIST_HEAD(&prev.flushes);
2845
2846 list_cut_position(&prev.spas, &acpi_desc->spas,
2847 acpi_desc->spas.prev);
2848 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2849 acpi_desc->memdevs.prev);
2850 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2851 acpi_desc->dcrs.prev);
2852 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2853 acpi_desc->bdws.prev);
2854 list_cut_position(&prev.idts, &acpi_desc->idts,
2855 acpi_desc->idts.prev);
2856 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2857 acpi_desc->flushes.prev);
2858
Vishal Verma20985162015-10-27 16:58:27 -06002859 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002860 while (!IS_ERR_OR_NULL(data))
2861 data = add_table(acpi_desc, &prev, data, end);
2862
2863 if (IS_ERR(data)) {
2864 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2865 PTR_ERR(data));
2866 rc = PTR_ERR(data);
2867 goto out_unlock;
2868 }
2869
2870 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2871 if (rc)
2872 goto out_unlock;
2873
Dan Williams81ed4e32016-06-10 18:20:53 -07002874 rc = nfit_mem_init(acpi_desc);
2875 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002876 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002877
2878 rc = acpi_nfit_register_dimms(acpi_desc);
2879 if (rc)
2880 goto out_unlock;
2881
2882 rc = acpi_nfit_register_regions(acpi_desc);
2883
2884 out_unlock:
2885 mutex_unlock(&acpi_desc->init_mutex);
2886 return rc;
2887}
2888EXPORT_SYMBOL_GPL(acpi_nfit_init);
2889
Dan Williams7ae0fa432016-02-19 12:16:34 -08002890struct acpi_nfit_flush_work {
2891 struct work_struct work;
2892 struct completion cmp;
2893};
2894
2895static void flush_probe(struct work_struct *work)
2896{
2897 struct acpi_nfit_flush_work *flush;
2898
2899 flush = container_of(work, typeof(*flush), work);
2900 complete(&flush->cmp);
2901}
2902
2903static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2904{
2905 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2906 struct device *dev = acpi_desc->dev;
2907 struct acpi_nfit_flush_work flush;
Dan Williamse4714862017-02-02 10:31:00 -08002908 int rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002909
2910 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2911 device_lock(dev);
2912 device_unlock(dev);
2913
Dan Williams9ccaed42017-04-13 22:48:46 -07002914 /* bounce the init_mutex to make init_complete valid */
2915 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07002916 if (acpi_desc->cancel || acpi_desc->init_complete) {
2917 mutex_unlock(&acpi_desc->init_mutex);
Dan Williams9ccaed42017-04-13 22:48:46 -07002918 return 0;
Dan Williamsfbabd822017-04-18 09:56:31 -07002919 }
Dan Williams9ccaed42017-04-13 22:48:46 -07002920
Dan Williams7ae0fa432016-02-19 12:16:34 -08002921 /*
2922 * Scrub work could take 10s of seconds, userspace may give up so we
2923 * need to be interruptible while waiting.
2924 */
2925 INIT_WORK_ONSTACK(&flush.work, flush_probe);
Boqun Feng1c322ac2017-08-24 22:22:36 +08002926 init_completion(&flush.cmp);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002927 queue_work(nfit_wq, &flush.work);
Dan Williamsfbabd822017-04-18 09:56:31 -07002928 mutex_unlock(&acpi_desc->init_mutex);
Dan Williamse4714862017-02-02 10:31:00 -08002929
2930 rc = wait_for_completion_interruptible(&flush.cmp);
2931 cancel_work_sync(&flush.work);
2932 return rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002933}
2934
Dan Williams87bf5722016-02-22 21:50:31 -08002935static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2936 struct nvdimm *nvdimm, unsigned int cmd)
2937{
2938 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2939
2940 if (nvdimm)
2941 return 0;
2942 if (cmd != ND_CMD_ARS_START)
2943 return 0;
2944
2945 /*
2946 * The kernel and userspace may race to initiate a scrub, but
2947 * the scrub thread is prepared to lose that initial race. It
2948 * just needs guarantees that any ars it initiates are not
2949 * interrupted by any intervening start reqeusts from userspace.
2950 */
2951 if (work_busy(&acpi_desc->work))
2952 return -EBUSY;
2953
2954 return 0;
2955}
2956
Toshi Kani80790032017-06-29 20:41:30 -06002957int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc, u8 flags)
Vishal Verma37b137f2016-07-23 21:51:42 -07002958{
2959 struct device *dev = acpi_desc->dev;
2960 struct nfit_spa *nfit_spa;
2961
2962 if (work_busy(&acpi_desc->work))
2963 return -EBUSY;
2964
Vishal Verma37b137f2016-07-23 21:51:42 -07002965 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07002966 if (acpi_desc->cancel) {
2967 mutex_unlock(&acpi_desc->init_mutex);
2968 return 0;
2969 }
2970
Vishal Verma37b137f2016-07-23 21:51:42 -07002971 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2972 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2973
2974 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2975 continue;
2976
2977 nfit_spa->ars_required = 1;
2978 }
Toshi Kani80790032017-06-29 20:41:30 -06002979 acpi_desc->ars_start_flags = flags;
Vishal Verma37b137f2016-07-23 21:51:42 -07002980 queue_work(nfit_wq, &acpi_desc->work);
2981 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2982 mutex_unlock(&acpi_desc->init_mutex);
2983
2984 return 0;
2985}
2986
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002987void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002988{
2989 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002990
2991 dev_set_drvdata(dev, acpi_desc);
2992 acpi_desc->dev = dev;
2993 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2994 nd_desc = &acpi_desc->nd_desc;
2995 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002996 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002997 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002998 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002999 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06003000 nd_desc->attr_groups = acpi_nfit_attribute_groups;
3001
Dan Williamsb94d5232015-05-19 22:54:31 -04003002 INIT_LIST_HEAD(&acpi_desc->spas);
3003 INIT_LIST_HEAD(&acpi_desc->dcrs);
3004 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04003005 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06003006 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04003007 INIT_LIST_HEAD(&acpi_desc->memdevs);
3008 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003009 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06003010 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08003011 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04003012}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003013EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04003014
Dan Williams3c87f372017-04-03 13:52:14 -07003015static void acpi_nfit_put_table(void *table)
3016{
3017 acpi_put_table(table);
3018}
3019
Dan Williamsfbabd822017-04-18 09:56:31 -07003020void acpi_nfit_shutdown(void *data)
3021{
3022 struct acpi_nfit_desc *acpi_desc = data;
3023 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3024
3025 /*
3026 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3027 * race teardown
3028 */
3029 mutex_lock(&acpi_desc_lock);
3030 list_del(&acpi_desc->list);
3031 mutex_unlock(&acpi_desc_lock);
3032
3033 mutex_lock(&acpi_desc->init_mutex);
3034 acpi_desc->cancel = 1;
3035 mutex_unlock(&acpi_desc->init_mutex);
3036
3037 /*
3038 * Bounce the nvdimm bus lock to make sure any in-flight
3039 * acpi_nfit_ars_rescan() submissions have had a chance to
3040 * either submit or see ->cancel set.
3041 */
3042 device_lock(bus_dev);
3043 device_unlock(bus_dev);
3044
3045 flush_workqueue(nfit_wq);
3046}
3047EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3048
Dan Williamsb94d5232015-05-19 22:54:31 -04003049static int acpi_nfit_add(struct acpi_device *adev)
3050{
Vishal Verma20985162015-10-27 16:58:27 -06003051 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04003052 struct acpi_nfit_desc *acpi_desc;
3053 struct device *dev = &adev->dev;
3054 struct acpi_table_header *tbl;
3055 acpi_status status = AE_OK;
3056 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07003057 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04003058
Lv Zheng6b11d1d2016-12-14 15:04:39 +08003059 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
Dan Williamsb94d5232015-05-19 22:54:31 -04003060 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06003061 /* This is ok, we could have an nvdimm hotplugged later */
3062 dev_dbg(dev, "failed to find NFIT at startup\n");
3063 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04003064 }
Dan Williams3c87f372017-04-03 13:52:14 -07003065
3066 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3067 if (rc)
3068 return rc;
Lv Zheng6b11d1d2016-12-14 15:04:39 +08003069 sz = tbl->length;
Dan Williamsb94d5232015-05-19 22:54:31 -04003070
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003071 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3072 if (!acpi_desc)
3073 return -ENOMEM;
3074 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04003075
Dan Williamse7a11b42016-07-14 16:19:55 -07003076 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05003077 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04003078
Vishal Verma20985162015-10-27 16:58:27 -06003079 /* Evaluate _FIT and override with that if present */
3080 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3081 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07003082 union acpi_object *obj = buf.pointer;
3083
3084 if (obj->type == ACPI_TYPE_BUFFER)
3085 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3086 obj->buffer.length);
3087 else
Linda Knippers6b577c92015-11-20 19:05:49 -05003088 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
3089 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07003090 kfree(buf.pointer);
3091 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07003092 /* skip over the lead-in header table */
3093 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3094 + sizeof(struct acpi_table_nfit),
3095 sz - sizeof(struct acpi_table_nfit));
Dan Williamsfbabd822017-04-18 09:56:31 -07003096
3097 if (rc)
3098 return rc;
3099 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
Dan Williamsb94d5232015-05-19 22:54:31 -04003100}
3101
3102static int acpi_nfit_remove(struct acpi_device *adev)
3103{
Dan Williamsfbabd822017-04-18 09:56:31 -07003104 /* see acpi_nfit_unregister */
Dan Williamsb94d5232015-05-19 22:54:31 -04003105 return 0;
3106}
3107
Toshi Kani56b47fe2017-06-08 12:36:57 -06003108static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
Vishal Verma20985162015-10-27 16:58:27 -06003109{
Dan Williamsc14a8682016-08-18 22:15:04 -07003110 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06003111 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07003112 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06003113 acpi_status status;
3114 int ret;
3115
Vishal Verma20985162015-10-27 16:58:27 -06003116 if (!dev->driver) {
3117 /* dev->driver may be null if we're being removed */
3118 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Dan Williamsc14a8682016-08-18 22:15:04 -07003119 return;
Vishal Verma20985162015-10-27 16:58:27 -06003120 }
3121
3122 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003123 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3124 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07003125 return;
3126 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003127 } else {
3128 /*
3129 * Finish previous registration before considering new
3130 * regions.
3131 */
3132 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06003133 }
3134
3135 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07003136 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06003137 if (ACPI_FAILURE(status)) {
3138 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07003139 return;
Vishal Verma20985162015-10-27 16:58:27 -06003140 }
3141
Linda Knippers6b577c92015-11-20 19:05:49 -05003142 obj = buf.pointer;
3143 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07003144 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3145 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07003146 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05003147 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07003148 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05003149 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06003150 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07003151}
Toshi Kani56b47fe2017-06-08 12:36:57 -06003152
3153static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3154{
3155 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Toshi Kani80790032017-06-29 20:41:30 -06003156 u8 flags = (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON) ?
3157 0 : ND_ARS_RETURN_PREV_DATA;
Toshi Kani56b47fe2017-06-08 12:36:57 -06003158
Toshi Kani80790032017-06-29 20:41:30 -06003159 acpi_nfit_ars_rescan(acpi_desc, flags);
Toshi Kani56b47fe2017-06-08 12:36:57 -06003160}
3161
3162void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3163{
3164 dev_dbg(dev, "%s: event: 0x%x\n", __func__, event);
3165
3166 switch (event) {
3167 case NFIT_NOTIFY_UPDATE:
3168 return acpi_nfit_update_notify(dev, handle);
3169 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3170 return acpi_nfit_uc_error_notify(dev, handle);
3171 default:
3172 return;
3173 }
3174}
Dan Williamsc14a8682016-08-18 22:15:04 -07003175EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06003176
Dan Williamsc14a8682016-08-18 22:15:04 -07003177static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3178{
3179 device_lock(&adev->dev);
3180 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3181 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06003182}
3183
Dan Williamsb94d5232015-05-19 22:54:31 -04003184static const struct acpi_device_id acpi_nfit_ids[] = {
3185 { "ACPI0012", 0 },
3186 { "", 0 },
3187};
3188MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3189
3190static struct acpi_driver acpi_nfit_driver = {
3191 .name = KBUILD_MODNAME,
3192 .ids = acpi_nfit_ids,
3193 .ops = {
3194 .add = acpi_nfit_add,
3195 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06003196 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04003197 },
3198};
3199
3200static __init int nfit_init(void)
3201{
Prarit Bhargava7e700d22017-05-31 13:32:00 -04003202 int ret;
3203
Dan Williamsb94d5232015-05-19 22:54:31 -04003204 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3205 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3206 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3207 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3208 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3209 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3210 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3211
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03003212 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3213 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3214 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3215 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3216 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3217 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3218 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3219 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3220 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3221 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3222 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3223 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3224 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04003225
Dan Williams7ae0fa432016-02-19 12:16:34 -08003226 nfit_wq = create_singlethread_workqueue("nfit");
3227 if (!nfit_wq)
3228 return -ENOMEM;
3229
Vishal Verma6839a6d2016-07-23 21:51:21 -07003230 nfit_mce_register();
Prarit Bhargava7e700d22017-05-31 13:32:00 -04003231 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3232 if (ret) {
3233 nfit_mce_unregister();
3234 destroy_workqueue(nfit_wq);
3235 }
Vishal Verma6839a6d2016-07-23 21:51:21 -07003236
Prarit Bhargava7e700d22017-05-31 13:32:00 -04003237 return ret;
3238
Dan Williamsb94d5232015-05-19 22:54:31 -04003239}
3240
3241static __exit void nfit_exit(void)
3242{
Vishal Verma6839a6d2016-07-23 21:51:21 -07003243 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04003244 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003245 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003246 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04003247}
3248
3249module_init(nfit_init);
3250module_exit(nfit_exit);
3251MODULE_LICENSE("GPL v2");
3252MODULE_AUTHOR("Intel Corporation");