blob: b7d7cc4fd2d0158980bb57e4c2205e809ff81df9 [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 Zwislerc2ad2952015-07-10 11:06:13 -060023#include <linux/pmem.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040024#include <linux/io.h>
Dan Williams1cf03c02016-02-17 13:01:23 -080025#include <linux/nd.h>
Dan Williams96601ad2015-08-24 18:29:38 -040026#include <asm/cacheflush.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040027#include "nfit.h"
28
Ross Zwisler047fc8a2015-06-25 04:21:02 -040029/*
30 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
31 * irrelevant.
32 */
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020033#include <linux/io-64-nonatomic-hi-lo.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040034
Dan Williams4d88a972015-05-31 14:41:48 -040035static bool force_enable_dimms;
36module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
37MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
38
Dan Williams1cf03c02016-02-17 13:01:23 -080039static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
40module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
41MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
42
43/* after three payloads of overflow, it's dead jim */
44static unsigned int scrub_overflow_abort = 3;
45module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
46MODULE_PARM_DESC(scrub_overflow_abort,
47 "Number of times we overflow ARS results before abort");
48
Dan Williams87554092016-04-28 18:01:20 -070049static bool disable_vendor_specific;
50module_param(disable_vendor_specific, bool, S_IRUGO);
51MODULE_PARM_DESC(disable_vendor_specific,
Linda Knippersf2668fa2017-03-07 16:35:14 -050052 "Limit commands to the publicly specified set");
Dan Williams87554092016-04-28 18:01:20 -070053
Linda Knippers095ab4b2017-03-07 16:35:12 -050054static unsigned long override_dsm_mask;
55module_param(override_dsm_mask, ulong, S_IRUGO);
56MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
57
Linda Knippersba650cf2017-03-07 16:35:13 -050058static int default_dsm_family = -1;
59module_param(default_dsm_family, int, S_IRUGO);
60MODULE_PARM_DESC(default_dsm_family,
61 "Try this DSM type first when identifying NVDIMM family");
62
Vishal Verma6839a6d2016-07-23 21:51:21 -070063LIST_HEAD(acpi_descs);
64DEFINE_MUTEX(acpi_desc_lock);
65
Dan Williams7ae0fa432016-02-19 12:16:34 -080066static struct workqueue_struct *nfit_wq;
67
Vishal Verma20985162015-10-27 16:58:27 -060068struct nfit_table_prev {
69 struct list_head spas;
70 struct list_head memdevs;
71 struct list_head dcrs;
72 struct list_head bdws;
73 struct list_head idts;
74 struct list_head flushes;
75};
76
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +030077static guid_t nfit_uuid[NFIT_UUID_MAX];
Dan Williamsb94d5232015-05-19 22:54:31 -040078
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +030079const guid_t *to_nfit_uuid(enum nfit_uuids id)
Dan Williamsb94d5232015-05-19 22:54:31 -040080{
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +030081 return &nfit_uuid[id];
Dan Williamsb94d5232015-05-19 22:54:31 -040082}
Dan Williams6bc75612015-06-17 17:23:32 -040083EXPORT_SYMBOL(to_nfit_uuid);
Dan Williamsb94d5232015-05-19 22:54:31 -040084
Dan Williams62232e452015-06-08 14:27:06 -040085static struct acpi_nfit_desc *to_acpi_nfit_desc(
86 struct nvdimm_bus_descriptor *nd_desc)
87{
88 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
89}
90
91static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
92{
93 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
94
95 /*
96 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
97 * acpi_device.
98 */
99 if (!nd_desc->provider_name
100 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
101 return NULL;
102
103 return to_acpi_device(acpi_desc->dev);
104}
105
Dan Williamsd6eb2702016-12-06 15:06:55 -0800106static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
Dan Williamsaef25332016-02-12 17:01:11 -0800107{
Dan Williamsd4f32362016-03-03 16:08:54 -0800108 struct nd_cmd_clear_error *clear_err;
Dan Williamsaef25332016-02-12 17:01:11 -0800109 struct nd_cmd_ars_status *ars_status;
Dan Williamsaef25332016-02-12 17:01:11 -0800110 u16 flags;
111
112 switch (cmd) {
113 case ND_CMD_ARS_CAP:
Dan Williams11294d62016-09-21 09:21:26 -0700114 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800115 return -ENOTTY;
116
117 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700118 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800119 return -EIO;
120
121 /* No supported scan types for this range */
122 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
Dan Williams11294d62016-09-21 09:21:26 -0700123 if ((status >> 16 & flags) == 0)
Dan Williamsaef25332016-02-12 17:01:11 -0800124 return -ENOTTY;
Vishal Verma9a901f52016-12-05 17:00:37 -0700125 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800126 case ND_CMD_ARS_START:
Dan Williamsaef25332016-02-12 17:01:11 -0800127 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700128 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800129 return -EBUSY;
130
131 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700132 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800133 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700134 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800135 case ND_CMD_ARS_STATUS:
136 ars_status = buf;
137 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700138 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800139 return -EIO;
140 /* Check extended status (Upper two bytes) */
Dan Williams11294d62016-09-21 09:21:26 -0700141 if (status == NFIT_ARS_STATUS_DONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800142 return 0;
143
144 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700145 if (status == NFIT_ARS_STATUS_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800146 return -EBUSY;
147
148 /* No ARS performed for the current boot */
Dan Williams11294d62016-09-21 09:21:26 -0700149 if (status == NFIT_ARS_STATUS_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800150 return -EAGAIN;
151
152 /*
153 * ARS interrupted, either we overflowed or some other
154 * agent wants the scan to stop. If we didn't overflow
155 * then just continue with the returned results.
156 */
Dan Williams11294d62016-09-21 09:21:26 -0700157 if (status == NFIT_ARS_STATUS_INTR) {
Dan Williams82aa37c2016-12-06 12:45:24 -0800158 if (ars_status->out_length >= 40 && (ars_status->flags
159 & NFIT_ARS_F_OVERFLOW))
Dan Williamsaef25332016-02-12 17:01:11 -0800160 return -ENOSPC;
161 return 0;
162 }
163
164 /* Unknown status */
Dan Williams11294d62016-09-21 09:21:26 -0700165 if (status >> 16)
Dan Williamsaef25332016-02-12 17:01:11 -0800166 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700167 return 0;
Dan Williamsd4f32362016-03-03 16:08:54 -0800168 case ND_CMD_CLEAR_ERROR:
169 clear_err = buf;
Dan Williams11294d62016-09-21 09:21:26 -0700170 if (status & 0xffff)
Dan Williamsd4f32362016-03-03 16:08:54 -0800171 return -EIO;
172 if (!clear_err->cleared)
173 return -EIO;
174 if (clear_err->length > clear_err->cleared)
175 return clear_err->cleared;
Vishal Verma9a901f52016-12-05 17:00:37 -0700176 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800177 default:
178 break;
179 }
180
Dan Williams11294d62016-09-21 09:21:26 -0700181 /* all other non-zero status results in an error */
182 if (status)
183 return -EIO;
Dan Williamsaef25332016-02-12 17:01:11 -0800184 return 0;
185}
186
Dan Williams9d62ed92017-05-04 11:47:22 -0700187static int xlat_nvdimm_status(void *buf, unsigned int cmd, u32 status)
188{
189 switch (cmd) {
190 case ND_CMD_GET_CONFIG_SIZE:
191 if (status >> 16 & ND_CONFIG_LOCKED)
192 return -EACCES;
193 break;
194 default:
195 break;
196 }
197
198 /* all other non-zero status results in an error */
199 if (status)
200 return -EIO;
201 return 0;
202}
203
Dan Williamsd6eb2702016-12-06 15:06:55 -0800204static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
205 u32 status)
206{
207 if (!nvdimm)
208 return xlat_bus_status(buf, cmd, status);
Dan Williams9d62ed92017-05-04 11:47:22 -0700209 return xlat_nvdimm_status(buf, cmd, status);
Dan Williamsd6eb2702016-12-06 15:06:55 -0800210}
211
Dan Williamsa7de92d2016-12-05 13:43:25 -0800212int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
213 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400214{
Dan Williams62232e452015-06-08 14:27:06 -0400215 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams62232e452015-06-08 14:27:06 -0400216 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700217 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400218 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700219 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400220 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700221 unsigned long cmd_mask, dsm_mask;
Dan Williams11294d62016-09-21 09:21:26 -0700222 u32 offset, fw_status = 0;
Dan Williams62232e452015-06-08 14:27:06 -0400223 acpi_handle handle;
Dan Williams31eca762016-04-28 16:23:43 -0700224 unsigned int func;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300225 const guid_t *guid;
Dan Williams62232e452015-06-08 14:27:06 -0400226 int rc, i;
227
Dan Williams31eca762016-04-28 16:23:43 -0700228 func = cmd;
229 if (cmd == ND_CMD_CALL) {
230 call_pkg = buf;
231 func = call_pkg->nd_command;
232 }
233
Dan Williams62232e452015-06-08 14:27:06 -0400234 if (nvdimm) {
235 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
236 struct acpi_device *adev = nfit_mem->adev;
237
238 if (!adev)
239 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700240 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
241 return -ENOTTY;
242
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400243 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400244 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700245 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400246 dsm_mask = nfit_mem->dsm_mask;
247 desc = nd_cmd_dimm_desc(cmd);
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300248 guid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400249 handle = adev->handle;
250 } else {
251 struct acpi_device *adev = to_acpi_dev(acpi_desc);
252
253 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700254 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700255 dsm_mask = cmd_mask;
Jerry Hoemann7db5bb32017-06-30 20:53:24 -0700256 if (cmd == ND_CMD_CALL)
257 dsm_mask = nd_desc->bus_dsm_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400258 desc = nd_cmd_bus_desc(cmd);
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300259 guid = to_nfit_uuid(NFIT_DEV_BUS);
Dan Williams62232e452015-06-08 14:27:06 -0400260 handle = adev->handle;
261 dimm_name = "bus";
262 }
263
264 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
265 return -ENOTTY;
266
Dan Williams31eca762016-04-28 16:23:43 -0700267 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400268 return -ENOTTY;
269
270 in_obj.type = ACPI_TYPE_PACKAGE;
271 in_obj.package.count = 1;
272 in_obj.package.elements = &in_buf;
273 in_buf.type = ACPI_TYPE_BUFFER;
274 in_buf.buffer.pointer = buf;
275 in_buf.buffer.length = 0;
276
277 /* libnvdimm has already validated the input envelope */
278 for (i = 0; i < desc->in_num; i++)
279 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
280 i, buf);
281
Dan Williams31eca762016-04-28 16:23:43 -0700282 if (call_pkg) {
283 /* skip over package wrapper */
284 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
285 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400286 }
287
Dan Williams7699a6a2017-04-28 13:54:30 -0700288 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
289 __func__, dimm_name, cmd, func, in_buf.buffer.length);
290 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
Dan Williams31eca762016-04-28 16:23:43 -0700291 in_buf.buffer.pointer,
292 min_t(u32, 256, in_buf.buffer.length), true);
Dan Williams31eca762016-04-28 16:23:43 -0700293
Andy Shevchenko94116f82017-06-05 19:40:46 +0300294 out_obj = acpi_evaluate_dsm(handle, guid, 1, func, &in_obj);
Dan Williams62232e452015-06-08 14:27:06 -0400295 if (!out_obj) {
296 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
297 cmd_name);
298 return -EINVAL;
299 }
300
Dan Williams31eca762016-04-28 16:23:43 -0700301 if (call_pkg) {
302 call_pkg->nd_fw_size = out_obj->buffer.length;
303 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
304 out_obj->buffer.pointer,
305 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
306
307 ACPI_FREE(out_obj);
308 /*
309 * Need to support FW function w/o known size in advance.
310 * Caller can determine required size based upon nd_fw_size.
311 * If we return an error (like elsewhere) then caller wouldn't
312 * be able to rely upon data returned to make calculation.
313 */
314 return 0;
315 }
316
Dan Williams62232e452015-06-08 14:27:06 -0400317 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
318 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
319 __func__, dimm_name, cmd_name, out_obj->type);
320 rc = -EINVAL;
321 goto out;
322 }
323
Dan Williams7699a6a2017-04-28 13:54:30 -0700324 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, dimm_name,
325 cmd_name, out_obj->buffer.length);
326 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
327 out_obj->buffer.pointer,
328 min_t(u32, 128, out_obj->buffer.length), true);
Dan Williams62232e452015-06-08 14:27:06 -0400329
330 for (i = 0, offset = 0; i < desc->out_num; i++) {
331 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800332 (u32 *) out_obj->buffer.pointer,
333 out_obj->buffer.length - offset);
Dan Williams62232e452015-06-08 14:27:06 -0400334
335 if (offset + out_size > out_obj->buffer.length) {
336 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
337 __func__, dimm_name, cmd_name, i);
338 break;
339 }
340
341 if (in_buf.buffer.length + offset + out_size > buf_len) {
342 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
343 __func__, dimm_name, cmd_name, i);
344 rc = -ENXIO;
345 goto out;
346 }
347 memcpy(buf + in_buf.buffer.length + offset,
348 out_obj->buffer.pointer + offset, out_size);
349 offset += out_size;
350 }
Dan Williams11294d62016-09-21 09:21:26 -0700351
352 /*
353 * Set fw_status for all the commands with a known format to be
354 * later interpreted by xlat_status().
355 */
356 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
357 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
358 fw_status = *(u32 *) out_obj->buffer.pointer;
359
Dan Williams62232e452015-06-08 14:27:06 -0400360 if (offset + in_buf.buffer.length < buf_len) {
361 if (i >= 1) {
362 /*
363 * status valid, return the number of bytes left
364 * unfilled in the output buffer
365 */
366 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800367 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800368 *cmd_rc = xlat_status(nvdimm, buf, cmd,
369 fw_status);
Dan Williams62232e452015-06-08 14:27:06 -0400370 } else {
371 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
372 __func__, dimm_name, cmd_name, buf_len,
373 offset);
374 rc = -ENXIO;
375 }
Dan Williams2eea6582016-05-02 09:11:53 -0700376 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400377 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700378 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800379 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
Dan Williams2eea6582016-05-02 09:11:53 -0700380 }
Dan Williams62232e452015-06-08 14:27:06 -0400381
382 out:
383 ACPI_FREE(out_obj);
384
385 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400386}
Dan Williamsa7de92d2016-12-05 13:43:25 -0800387EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
Dan Williamsb94d5232015-05-19 22:54:31 -0400388
389static const char *spa_type_name(u16 type)
390{
391 static const char *to_name[] = {
392 [NFIT_SPA_VOLATILE] = "volatile",
393 [NFIT_SPA_PM] = "pmem",
394 [NFIT_SPA_DCR] = "dimm-control-region",
395 [NFIT_SPA_BDW] = "block-data-window",
396 [NFIT_SPA_VDISK] = "volatile-disk",
397 [NFIT_SPA_VCD] = "volatile-cd",
398 [NFIT_SPA_PDISK] = "persistent-disk",
399 [NFIT_SPA_PCD] = "persistent-cd",
400
401 };
402
403 if (type > NFIT_SPA_PCD)
404 return "unknown";
405
406 return to_name[type];
407}
408
Vishal Verma6839a6d2016-07-23 21:51:21 -0700409int nfit_spa_type(struct acpi_nfit_system_address *spa)
Dan Williamsb94d5232015-05-19 22:54:31 -0400410{
411 int i;
412
413 for (i = 0; i < NFIT_UUID_MAX; i++)
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300414 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
Dan Williamsb94d5232015-05-19 22:54:31 -0400415 return i;
416 return -1;
417}
418
419static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600420 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400421 struct acpi_nfit_system_address *spa)
422{
423 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600424 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400425
Dan Williams31932042016-07-14 17:22:48 -0700426 if (spa->header.length != sizeof(*spa))
427 return false;
428
Vishal Verma20985162015-10-27 16:58:27 -0600429 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700430 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600431 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
432 return true;
433 }
434 }
435
Dan Williams31932042016-07-14 17:22:48 -0700436 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
437 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400438 if (!nfit_spa)
439 return false;
440 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700441 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400442 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
443 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
444 spa->range_index,
445 spa_type_name(nfit_spa_type(spa)));
446 return true;
447}
448
449static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600450 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400451 struct acpi_nfit_memory_map *memdev)
452{
453 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600454 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400455
Dan Williams31932042016-07-14 17:22:48 -0700456 if (memdev->header.length != sizeof(*memdev))
457 return false;
458
Vishal Verma20985162015-10-27 16:58:27 -0600459 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700460 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600461 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
462 return true;
463 }
464
Dan Williams31932042016-07-14 17:22:48 -0700465 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
466 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400467 if (!nfit_memdev)
468 return false;
469 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700470 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400471 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
Dan Williamscaa603a2017-04-14 10:27:11 -0700472 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
Dan Williamsb94d5232015-05-19 22:54:31 -0400473 __func__, memdev->device_handle, memdev->range_index,
Dan Williamscaa603a2017-04-14 10:27:11 -0700474 memdev->region_index, memdev->flags);
Dan Williamsb94d5232015-05-19 22:54:31 -0400475 return true;
476}
477
Dan Williams31932042016-07-14 17:22:48 -0700478/*
479 * An implementation may provide a truncated control region if no block windows
480 * are defined.
481 */
482static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
483{
484 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
485 window_size))
486 return 0;
487 if (dcr->windows)
488 return sizeof(*dcr);
489 return offsetof(struct acpi_nfit_control_region, window_size);
490}
491
Dan Williamsb94d5232015-05-19 22:54:31 -0400492static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600493 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400494 struct acpi_nfit_control_region *dcr)
495{
496 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600497 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400498
Dan Williams31932042016-07-14 17:22:48 -0700499 if (!sizeof_dcr(dcr))
500 return false;
501
Vishal Verma20985162015-10-27 16:58:27 -0600502 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700503 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600504 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
505 return true;
506 }
507
Dan Williams31932042016-07-14 17:22:48 -0700508 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
509 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400510 if (!nfit_dcr)
511 return false;
512 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700513 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400514 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
515 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
516 dcr->region_index, dcr->windows);
517 return true;
518}
519
520static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600521 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400522 struct acpi_nfit_data_region *bdw)
523{
524 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600525 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400526
Dan Williams31932042016-07-14 17:22:48 -0700527 if (bdw->header.length != sizeof(*bdw))
528 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600529 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700530 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600531 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
532 return true;
533 }
534
Dan Williams31932042016-07-14 17:22:48 -0700535 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
536 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400537 if (!nfit_bdw)
538 return false;
539 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700540 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400541 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
542 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
543 bdw->region_index, bdw->windows);
544 return true;
545}
546
Dan Williams31932042016-07-14 17:22:48 -0700547static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
548{
549 if (idt->header.length < sizeof(*idt))
550 return 0;
551 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
552}
553
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400554static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600555 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400556 struct acpi_nfit_interleave *idt)
557{
558 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600559 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400560
Dan Williams31932042016-07-14 17:22:48 -0700561 if (!sizeof_idt(idt))
562 return false;
563
564 list_for_each_entry(nfit_idt, &prev->idts, list) {
565 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
566 continue;
567
568 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600569 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
570 return true;
571 }
Dan Williams31932042016-07-14 17:22:48 -0700572 }
Vishal Verma20985162015-10-27 16:58:27 -0600573
Dan Williams31932042016-07-14 17:22:48 -0700574 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
575 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400576 if (!nfit_idt)
577 return false;
578 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700579 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400580 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
581 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
582 idt->interleave_index, idt->line_count);
583 return true;
584}
585
Dan Williams31932042016-07-14 17:22:48 -0700586static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
587{
588 if (flush->header.length < sizeof(*flush))
589 return 0;
590 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
591}
592
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600593static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600594 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600595 struct acpi_nfit_flush_address *flush)
596{
597 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600598 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600599
Dan Williams31932042016-07-14 17:22:48 -0700600 if (!sizeof_flush(flush))
601 return false;
602
603 list_for_each_entry(nfit_flush, &prev->flushes, list) {
604 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
605 continue;
606
607 if (memcmp(nfit_flush->flush, flush,
608 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600609 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
610 return true;
611 }
Dan Williams31932042016-07-14 17:22:48 -0700612 }
Vishal Verma20985162015-10-27 16:58:27 -0600613
Dan Williams31932042016-07-14 17:22:48 -0700614 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
615 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600616 if (!nfit_flush)
617 return false;
618 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700619 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600620 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
621 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
622 flush->device_handle, flush->hint_count);
623 return true;
624}
625
Vishal Verma20985162015-10-27 16:58:27 -0600626static void *add_table(struct acpi_nfit_desc *acpi_desc,
627 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400628{
629 struct device *dev = acpi_desc->dev;
630 struct acpi_nfit_header *hdr;
631 void *err = ERR_PTR(-ENOMEM);
632
633 if (table >= end)
634 return NULL;
635
636 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600637 if (!hdr->length) {
638 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
639 hdr->type);
640 return NULL;
641 }
642
Dan Williamsb94d5232015-05-19 22:54:31 -0400643 switch (hdr->type) {
644 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600645 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400646 return err;
647 break;
648 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600649 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400650 return err;
651 break;
652 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600653 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400654 return err;
655 break;
656 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600657 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400658 return err;
659 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400660 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600661 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400662 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400663 break;
664 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600665 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600666 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400667 break;
668 case ACPI_NFIT_TYPE_SMBIOS:
669 dev_dbg(dev, "%s: smbios\n", __func__);
670 break;
671 default:
672 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
673 break;
674 }
675
676 return table + hdr->length;
677}
678
679static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
680 struct nfit_mem *nfit_mem)
681{
682 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
683 u16 dcr = nfit_mem->dcr->region_index;
684 struct nfit_spa *nfit_spa;
685
686 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
687 u16 range_index = nfit_spa->spa->range_index;
688 int type = nfit_spa_type(nfit_spa->spa);
689 struct nfit_memdev *nfit_memdev;
690
691 if (type != NFIT_SPA_BDW)
692 continue;
693
694 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
695 if (nfit_memdev->memdev->range_index != range_index)
696 continue;
697 if (nfit_memdev->memdev->device_handle != device_handle)
698 continue;
699 if (nfit_memdev->memdev->region_index != dcr)
700 continue;
701
702 nfit_mem->spa_bdw = nfit_spa->spa;
703 return;
704 }
705 }
706
707 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
708 nfit_mem->spa_dcr->range_index);
709 nfit_mem->bdw = NULL;
710}
711
Dan Williams6697b2c2016-02-04 16:51:00 -0800712static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400713 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
714{
715 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400716 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400717 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400718 struct nfit_idt *nfit_idt;
719 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400720
Dan Williamsb94d5232015-05-19 22:54:31 -0400721 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
722 if (nfit_bdw->bdw->region_index != dcr)
723 continue;
724 nfit_mem->bdw = nfit_bdw->bdw;
725 break;
726 }
727
728 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800729 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400730
731 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400732
733 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800734 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400735
736 range_index = nfit_mem->spa_bdw->range_index;
737 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
738 if (nfit_memdev->memdev->range_index != range_index ||
739 nfit_memdev->memdev->region_index != dcr)
740 continue;
741 nfit_mem->memdev_bdw = nfit_memdev->memdev;
742 idt_idx = nfit_memdev->memdev->interleave_index;
743 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
744 if (nfit_idt->idt->interleave_index != idt_idx)
745 continue;
746 nfit_mem->idt_bdw = nfit_idt->idt;
747 break;
748 }
749 break;
750 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400751}
752
Dan Williams14999342017-04-13 19:46:36 -0700753static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400754 struct acpi_nfit_system_address *spa)
755{
756 struct nfit_mem *nfit_mem, *found;
757 struct nfit_memdev *nfit_memdev;
Dan Williams14999342017-04-13 19:46:36 -0700758 int type = spa ? nfit_spa_type(spa) : 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400759
760 switch (type) {
761 case NFIT_SPA_DCR:
762 case NFIT_SPA_PM:
763 break;
764 default:
Dan Williams14999342017-04-13 19:46:36 -0700765 if (spa)
766 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400767 }
768
Dan Williams14999342017-04-13 19:46:36 -0700769 /*
770 * This loop runs in two modes, when a dimm is mapped the loop
771 * adds memdev associations to an existing dimm, or creates a
772 * dimm. In the unmapped dimm case this loop sweeps for memdev
773 * instances with an invalid / zero range_index and adds those
774 * dimms without spa associations.
775 */
Dan Williamsb94d5232015-05-19 22:54:31 -0400776 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700777 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800778 struct nfit_dcr *nfit_dcr;
779 u32 device_handle;
780 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400781
Dan Williams14999342017-04-13 19:46:36 -0700782 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
783 continue;
784 if (!spa && nfit_memdev->memdev->range_index)
Dan Williamsb94d5232015-05-19 22:54:31 -0400785 continue;
786 found = NULL;
787 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800788 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400789 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800790 if (__to_nfit_memdev(nfit_mem)->device_handle
791 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400792 found = nfit_mem;
793 break;
794 }
795
796 if (found)
797 nfit_mem = found;
798 else {
799 nfit_mem = devm_kzalloc(acpi_desc->dev,
800 sizeof(*nfit_mem), GFP_KERNEL);
801 if (!nfit_mem)
802 return -ENOMEM;
803 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700804 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800805 list_add(&nfit_mem->list, &acpi_desc->dimms);
806 }
807
808 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
809 if (nfit_dcr->dcr->region_index != dcr)
810 continue;
811 /*
812 * Record the control region for the dimm. For
813 * the ACPI 6.1 case, where there are separate
814 * control regions for the pmem vs blk
815 * interfaces, be sure to record the extended
816 * blk details.
817 */
818 if (!nfit_mem->dcr)
819 nfit_mem->dcr = nfit_dcr->dcr;
820 else if (nfit_mem->dcr->windows == 0
821 && nfit_dcr->dcr->windows)
822 nfit_mem->dcr = nfit_dcr->dcr;
823 break;
824 }
825
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700826 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700827 struct acpi_nfit_flush_address *flush;
828 u16 i;
829
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700830 if (nfit_flush->flush->device_handle != device_handle)
831 continue;
832 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700833 flush = nfit_flush->flush;
834 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
835 flush->hint_count
836 * sizeof(struct resource), GFP_KERNEL);
837 if (!nfit_mem->flush_wpq)
838 return -ENOMEM;
839 for (i = 0; i < flush->hint_count; i++) {
840 struct resource *res = &nfit_mem->flush_wpq[i];
841
842 res->start = flush->hint_address[i];
843 res->end = res->start + 8 - 1;
844 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700845 break;
846 }
847
Dan Williams6697b2c2016-02-04 16:51:00 -0800848 if (dcr && !nfit_mem->dcr) {
849 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
850 spa->range_index, dcr);
851 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400852 }
853
854 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400855 struct nfit_idt *nfit_idt;
856 u16 idt_idx;
857
Dan Williamsb94d5232015-05-19 22:54:31 -0400858 /* multiple dimms may share a SPA when interleaved */
859 nfit_mem->spa_dcr = spa;
860 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400861 idt_idx = nfit_memdev->memdev->interleave_index;
862 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
863 if (nfit_idt->idt->interleave_index != idt_idx)
864 continue;
865 nfit_mem->idt_dcr = nfit_idt->idt;
866 break;
867 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800868 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williams14999342017-04-13 19:46:36 -0700869 } else if (type == NFIT_SPA_PM) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400870 /*
871 * A single dimm may belong to multiple SPA-PM
872 * ranges, record at least one in addition to
873 * any SPA-DCR range.
874 */
875 nfit_mem->memdev_pmem = nfit_memdev->memdev;
Dan Williams14999342017-04-13 19:46:36 -0700876 } else
877 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400878 }
879
880 return 0;
881}
882
883static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
884{
885 struct nfit_mem *a = container_of(_a, typeof(*a), list);
886 struct nfit_mem *b = container_of(_b, typeof(*b), list);
887 u32 handleA, handleB;
888
889 handleA = __to_nfit_memdev(a)->device_handle;
890 handleB = __to_nfit_memdev(b)->device_handle;
891 if (handleA < handleB)
892 return -1;
893 else if (handleA > handleB)
894 return 1;
895 return 0;
896}
897
898static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
899{
900 struct nfit_spa *nfit_spa;
Dan Williams14999342017-04-13 19:46:36 -0700901 int rc;
902
Dan Williamsb94d5232015-05-19 22:54:31 -0400903
904 /*
905 * For each SPA-DCR or SPA-PMEM address range find its
906 * corresponding MEMDEV(s). From each MEMDEV find the
907 * corresponding DCR. Then, if we're operating on a SPA-DCR,
908 * try to find a SPA-BDW and a corresponding BDW that references
909 * the DCR. Throw it all into an nfit_mem object. Note, that
910 * BDWs are optional.
911 */
912 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
Dan Williams14999342017-04-13 19:46:36 -0700913 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400914 if (rc)
915 return rc;
916 }
917
Dan Williams14999342017-04-13 19:46:36 -0700918 /*
919 * If a DIMM has failed to be mapped into SPA there will be no
920 * SPA entries above. Find and register all the unmapped DIMMs
921 * for reporting and recovery purposes.
922 */
923 rc = __nfit_mem_init(acpi_desc, NULL);
924 if (rc)
925 return rc;
926
Dan Williamsb94d5232015-05-19 22:54:31 -0400927 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
928
929 return 0;
930}
931
Dan Williams45def222015-04-26 19:26:48 -0400932static ssize_t revision_show(struct device *dev,
933 struct device_attribute *attr, char *buf)
934{
935 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
936 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
937 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
938
Linda Knippers6b577c92015-11-20 19:05:49 -0500939 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400940}
941static DEVICE_ATTR_RO(revision);
942
Vishal Verma9ffd6352016-09-30 17:19:29 -0600943static ssize_t hw_error_scrub_show(struct device *dev,
944 struct device_attribute *attr, char *buf)
945{
946 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
947 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
948 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
949
950 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
951}
952
953/*
954 * The 'hw_error_scrub' attribute can have the following values written to it:
955 * '0': Switch to the default mode where an exception will only insert
956 * the address of the memory error into the poison and badblocks lists.
957 * '1': Enable a full scrub to happen if an exception for a memory error is
958 * received.
959 */
960static ssize_t hw_error_scrub_store(struct device *dev,
961 struct device_attribute *attr, const char *buf, size_t size)
962{
963 struct nvdimm_bus_descriptor *nd_desc;
964 ssize_t rc;
965 long val;
966
967 rc = kstrtol(buf, 0, &val);
968 if (rc)
969 return rc;
970
971 device_lock(dev);
972 nd_desc = dev_get_drvdata(dev);
973 if (nd_desc) {
974 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
975
976 switch (val) {
977 case HW_ERROR_SCRUB_ON:
978 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
979 break;
980 case HW_ERROR_SCRUB_OFF:
981 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
982 break;
983 default:
984 rc = -EINVAL;
985 break;
986 }
987 }
988 device_unlock(dev);
989 if (rc)
990 return rc;
991 return size;
992}
993static DEVICE_ATTR_RW(hw_error_scrub);
994
Vishal Verma37b137f2016-07-23 21:51:42 -0700995/*
996 * This shows the number of full Address Range Scrubs that have been
997 * completed since driver load time. Userspace can wait on this using
998 * select/poll etc. A '+' at the end indicates an ARS is in progress
999 */
1000static ssize_t scrub_show(struct device *dev,
1001 struct device_attribute *attr, char *buf)
1002{
1003 struct nvdimm_bus_descriptor *nd_desc;
1004 ssize_t rc = -ENXIO;
1005
1006 device_lock(dev);
1007 nd_desc = dev_get_drvdata(dev);
1008 if (nd_desc) {
1009 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1010
1011 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1012 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
1013 }
1014 device_unlock(dev);
1015 return rc;
1016}
1017
Vishal Verma37b137f2016-07-23 21:51:42 -07001018static ssize_t scrub_store(struct device *dev,
1019 struct device_attribute *attr, const char *buf, size_t size)
1020{
1021 struct nvdimm_bus_descriptor *nd_desc;
1022 ssize_t rc;
1023 long val;
1024
1025 rc = kstrtol(buf, 0, &val);
1026 if (rc)
1027 return rc;
1028 if (val != 1)
1029 return -EINVAL;
1030
1031 device_lock(dev);
1032 nd_desc = dev_get_drvdata(dev);
1033 if (nd_desc) {
1034 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1035
1036 rc = acpi_nfit_ars_rescan(acpi_desc);
1037 }
1038 device_unlock(dev);
1039 if (rc)
1040 return rc;
1041 return size;
1042}
1043static DEVICE_ATTR_RW(scrub);
1044
1045static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1046{
1047 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1048 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1049 | 1 << ND_CMD_ARS_STATUS;
1050
1051 return (nd_desc->cmd_mask & mask) == mask;
1052}
1053
1054static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1055{
1056 struct device *dev = container_of(kobj, struct device, kobj);
1057 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1058
1059 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1060 return 0;
1061 return a->mode;
1062}
1063
Dan Williams45def222015-04-26 19:26:48 -04001064static struct attribute *acpi_nfit_attributes[] = {
1065 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -07001066 &dev_attr_scrub.attr,
Vishal Verma9ffd6352016-09-30 17:19:29 -06001067 &dev_attr_hw_error_scrub.attr,
Dan Williams45def222015-04-26 19:26:48 -04001068 NULL,
1069};
1070
Arvind Yadav5e937462017-06-22 15:44:41 +05301071static const struct attribute_group acpi_nfit_attribute_group = {
Dan Williams45def222015-04-26 19:26:48 -04001072 .name = "nfit",
1073 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -07001074 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -04001075};
1076
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001077static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -04001078 &nvdimm_bus_attribute_group,
1079 &acpi_nfit_attribute_group,
1080 NULL,
1081};
1082
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001083static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1084{
1085 struct nvdimm *nvdimm = to_nvdimm(dev);
1086 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1087
1088 return __to_nfit_memdev(nfit_mem);
1089}
1090
1091static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1092{
1093 struct nvdimm *nvdimm = to_nvdimm(dev);
1094 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1095
1096 return nfit_mem->dcr;
1097}
1098
1099static ssize_t handle_show(struct device *dev,
1100 struct device_attribute *attr, char *buf)
1101{
1102 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1103
1104 return sprintf(buf, "%#x\n", memdev->device_handle);
1105}
1106static DEVICE_ATTR_RO(handle);
1107
1108static ssize_t phys_id_show(struct device *dev,
1109 struct device_attribute *attr, char *buf)
1110{
1111 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1112
1113 return sprintf(buf, "%#x\n", memdev->physical_id);
1114}
1115static DEVICE_ATTR_RO(phys_id);
1116
1117static ssize_t vendor_show(struct device *dev,
1118 struct device_attribute *attr, char *buf)
1119{
1120 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1121
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001122 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001123}
1124static DEVICE_ATTR_RO(vendor);
1125
1126static ssize_t rev_id_show(struct device *dev,
1127 struct device_attribute *attr, char *buf)
1128{
1129 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1130
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001131 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001132}
1133static DEVICE_ATTR_RO(rev_id);
1134
1135static ssize_t device_show(struct device *dev,
1136 struct device_attribute *attr, char *buf)
1137{
1138 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1139
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001140 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001141}
1142static DEVICE_ATTR_RO(device);
1143
Dan Williams6ca72082016-04-29 10:33:23 -07001144static ssize_t subsystem_vendor_show(struct device *dev,
1145 struct device_attribute *attr, char *buf)
1146{
1147 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1148
1149 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1150}
1151static DEVICE_ATTR_RO(subsystem_vendor);
1152
1153static ssize_t subsystem_rev_id_show(struct device *dev,
1154 struct device_attribute *attr, char *buf)
1155{
1156 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1157
1158 return sprintf(buf, "0x%04x\n",
1159 be16_to_cpu(dcr->subsystem_revision_id));
1160}
1161static DEVICE_ATTR_RO(subsystem_rev_id);
1162
1163static ssize_t subsystem_device_show(struct device *dev,
1164 struct device_attribute *attr, char *buf)
1165{
1166 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1167
1168 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1169}
1170static DEVICE_ATTR_RO(subsystem_device);
1171
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001172static int num_nvdimm_formats(struct nvdimm *nvdimm)
1173{
1174 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1175 int formats = 0;
1176
1177 if (nfit_mem->memdev_pmem)
1178 formats++;
1179 if (nfit_mem->memdev_bdw)
1180 formats++;
1181 return formats;
1182}
1183
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001184static ssize_t format_show(struct device *dev,
1185 struct device_attribute *attr, char *buf)
1186{
1187 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1188
Dan Williams1bcbf422016-06-29 11:19:32 -07001189 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001190}
1191static DEVICE_ATTR_RO(format);
1192
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001193static ssize_t format1_show(struct device *dev,
1194 struct device_attribute *attr, char *buf)
1195{
1196 u32 handle;
1197 ssize_t rc = -ENXIO;
1198 struct nfit_mem *nfit_mem;
1199 struct nfit_memdev *nfit_memdev;
1200 struct acpi_nfit_desc *acpi_desc;
1201 struct nvdimm *nvdimm = to_nvdimm(dev);
1202 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1203
1204 nfit_mem = nvdimm_provider_data(nvdimm);
1205 acpi_desc = nfit_mem->acpi_desc;
1206 handle = to_nfit_memdev(dev)->device_handle;
1207
1208 /* assumes DIMMs have at most 2 published interface codes */
1209 mutex_lock(&acpi_desc->init_mutex);
1210 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1211 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1212 struct nfit_dcr *nfit_dcr;
1213
1214 if (memdev->device_handle != handle)
1215 continue;
1216
1217 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1218 if (nfit_dcr->dcr->region_index != memdev->region_index)
1219 continue;
1220 if (nfit_dcr->dcr->code == dcr->code)
1221 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001222 rc = sprintf(buf, "0x%04x\n",
1223 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001224 break;
1225 }
1226 if (rc != ENXIO)
1227 break;
1228 }
1229 mutex_unlock(&acpi_desc->init_mutex);
1230 return rc;
1231}
1232static DEVICE_ATTR_RO(format1);
1233
1234static ssize_t formats_show(struct device *dev,
1235 struct device_attribute *attr, char *buf)
1236{
1237 struct nvdimm *nvdimm = to_nvdimm(dev);
1238
1239 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1240}
1241static DEVICE_ATTR_RO(formats);
1242
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001243static ssize_t serial_show(struct device *dev,
1244 struct device_attribute *attr, char *buf)
1245{
1246 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1247
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001248 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001249}
1250static DEVICE_ATTR_RO(serial);
1251
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001252static ssize_t family_show(struct device *dev,
1253 struct device_attribute *attr, char *buf)
1254{
1255 struct nvdimm *nvdimm = to_nvdimm(dev);
1256 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1257
1258 if (nfit_mem->family < 0)
1259 return -ENXIO;
1260 return sprintf(buf, "%d\n", nfit_mem->family);
1261}
1262static DEVICE_ATTR_RO(family);
1263
1264static ssize_t dsm_mask_show(struct device *dev,
1265 struct device_attribute *attr, char *buf)
1266{
1267 struct nvdimm *nvdimm = to_nvdimm(dev);
1268 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1269
1270 if (nfit_mem->family < 0)
1271 return -ENXIO;
1272 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1273}
1274static DEVICE_ATTR_RO(dsm_mask);
1275
Dan Williams58138822015-06-23 20:08:34 -04001276static ssize_t flags_show(struct device *dev,
1277 struct device_attribute *attr, char *buf)
1278{
1279 u16 flags = to_nfit_memdev(dev)->flags;
1280
Dan Williamsffab9382017-04-13 15:05:30 -07001281 return sprintf(buf, "%s%s%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001282 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1283 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1284 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001285 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Dan Williamsffab9382017-04-13 15:05:30 -07001286 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1287 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1288 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
Dan Williams58138822015-06-23 20:08:34 -04001289}
1290static DEVICE_ATTR_RO(flags);
1291
Toshi Kani38a879b2016-04-25 15:34:59 -06001292static ssize_t id_show(struct device *dev,
1293 struct device_attribute *attr, char *buf)
1294{
1295 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1296
1297 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1298 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1299 be16_to_cpu(dcr->vendor_id),
1300 dcr->manufacturing_location,
1301 be16_to_cpu(dcr->manufacturing_date),
1302 be32_to_cpu(dcr->serial_number));
1303 else
1304 return sprintf(buf, "%04x-%08x\n",
1305 be16_to_cpu(dcr->vendor_id),
1306 be32_to_cpu(dcr->serial_number));
1307}
1308static DEVICE_ATTR_RO(id);
1309
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001310static struct attribute *acpi_nfit_dimm_attributes[] = {
1311 &dev_attr_handle.attr,
1312 &dev_attr_phys_id.attr,
1313 &dev_attr_vendor.attr,
1314 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001315 &dev_attr_rev_id.attr,
1316 &dev_attr_subsystem_vendor.attr,
1317 &dev_attr_subsystem_device.attr,
1318 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001319 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001320 &dev_attr_formats.attr,
1321 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001322 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001323 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001324 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001325 &dev_attr_family.attr,
1326 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001327 NULL,
1328};
1329
1330static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1331 struct attribute *a, int n)
1332{
1333 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001334 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001335
Dan Williams14999342017-04-13 19:46:36 -07001336 if (!to_nfit_dcr(dev)) {
1337 /* Without a dcr only the memdev attributes can be surfaced */
1338 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1339 || a == &dev_attr_flags.attr
1340 || a == &dev_attr_family.attr
1341 || a == &dev_attr_dsm_mask.attr)
1342 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001343 return 0;
Dan Williams14999342017-04-13 19:46:36 -07001344 }
1345
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001346 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1347 return 0;
1348 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001349}
1350
Arvind Yadav5e937462017-06-22 15:44:41 +05301351static const struct attribute_group acpi_nfit_dimm_attribute_group = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001352 .name = "nfit",
1353 .attrs = acpi_nfit_dimm_attributes,
1354 .is_visible = acpi_nfit_dimm_attr_visible,
1355};
1356
1357static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001358 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001359 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001360 &acpi_nfit_dimm_attribute_group,
1361 NULL,
1362};
1363
1364static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1365 u32 device_handle)
1366{
1367 struct nfit_mem *nfit_mem;
1368
1369 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1370 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1371 return nfit_mem->nvdimm;
1372
1373 return NULL;
1374}
1375
Dan Williams231bf112016-08-22 19:23:25 -07001376void __acpi_nvdimm_notify(struct device *dev, u32 event)
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001377{
1378 struct nfit_mem *nfit_mem;
1379 struct acpi_nfit_desc *acpi_desc;
1380
1381 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1382 event);
1383
1384 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1385 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1386 event);
1387 return;
1388 }
1389
1390 acpi_desc = dev_get_drvdata(dev->parent);
1391 if (!acpi_desc)
1392 return;
1393
1394 /*
1395 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1396 * is still valid.
1397 */
1398 nfit_mem = dev_get_drvdata(dev);
1399 if (nfit_mem && nfit_mem->flags_attr)
1400 sysfs_notify_dirent(nfit_mem->flags_attr);
1401}
Dan Williams231bf112016-08-22 19:23:25 -07001402EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001403
1404static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1405{
1406 struct acpi_device *adev = data;
1407 struct device *dev = &adev->dev;
1408
1409 device_lock(dev->parent);
1410 __acpi_nvdimm_notify(dev, event);
1411 device_unlock(dev->parent);
1412}
1413
Dan Williams62232e452015-06-08 14:27:06 -04001414static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1415 struct nfit_mem *nfit_mem, u32 device_handle)
1416{
1417 struct acpi_device *adev, *adev_dimm;
1418 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001419 unsigned long dsm_mask;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001420 const guid_t *guid;
Linda Knippers60e95f432015-07-22 16:17:22 -04001421 int i;
Linda Knippersba650cf2017-03-07 16:35:13 -05001422 int family = -1;
Dan Williams62232e452015-06-08 14:27:06 -04001423
Dan Williamse3654ec2016-04-28 16:17:07 -07001424 /* nfit test assumes 1:1 relationship between commands and dsms */
1425 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001426 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001427 adev = to_acpi_dev(acpi_desc);
1428 if (!adev)
1429 return 0;
1430
1431 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1432 nfit_mem->adev = adev_dimm;
1433 if (!adev_dimm) {
1434 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1435 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001436 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001437 }
1438
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001439 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1440 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1441 dev_err(dev, "%s: notification registration failed\n",
1442 dev_name(&adev_dimm->dev));
1443 return -ENXIO;
1444 }
1445
Dan Williams31eca762016-04-28 16:23:43 -07001446 /*
stuart hayese02fb722016-05-26 11:38:41 -05001447 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001448 * different command sets. Note, that checking for function0 (bit0)
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001449 * tells us if any commands are reachable through this GUID.
Dan Williams31eca762016-04-28 16:23:43 -07001450 */
stuart hayese02fb722016-05-26 11:38:41 -05001451 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001452 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Linda Knippersba650cf2017-03-07 16:35:13 -05001453 if (family < 0 || i == default_dsm_family)
1454 family = i;
Dan Williams31eca762016-04-28 16:23:43 -07001455
1456 /* limit the supported commands to those that are publicly documented */
Linda Knippersba650cf2017-03-07 16:35:13 -05001457 nfit_mem->family = family;
Linda Knippers095ab4b2017-03-07 16:35:12 -05001458 if (override_dsm_mask && !disable_vendor_specific)
1459 dsm_mask = override_dsm_mask;
1460 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001461 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001462 if (disable_vendor_specific)
1463 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001464 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001465 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001466 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001467 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001468 if (disable_vendor_specific)
1469 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001470 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1471 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001472 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001473 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001474 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001475 /* DSMs are optional, continue loading the driver... */
1476 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001477 }
1478
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001479 guid = to_nfit_uuid(nfit_mem->family);
Dan Williams31eca762016-04-28 16:23:43 -07001480 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Andy Shevchenko94116f82017-06-05 19:40:46 +03001481 if (acpi_check_dsm(adev_dimm->handle, guid, 1, 1ULL << i))
Dan Williams62232e452015-06-08 14:27:06 -04001482 set_bit(i, &nfit_mem->dsm_mask);
1483
Linda Knippers60e95f432015-07-22 16:17:22 -04001484 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001485}
1486
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001487static void shutdown_dimm_notify(void *data)
1488{
1489 struct acpi_nfit_desc *acpi_desc = data;
1490 struct nfit_mem *nfit_mem;
1491
1492 mutex_lock(&acpi_desc->init_mutex);
1493 /*
1494 * Clear out the nfit_mem->flags_attr and shut down dimm event
1495 * notifications.
1496 */
1497 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williams231bf112016-08-22 19:23:25 -07001498 struct acpi_device *adev_dimm = nfit_mem->adev;
1499
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001500 if (nfit_mem->flags_attr) {
1501 sysfs_put(nfit_mem->flags_attr);
1502 nfit_mem->flags_attr = NULL;
1503 }
Dan Williams231bf112016-08-22 19:23:25 -07001504 if (adev_dimm)
1505 acpi_remove_notify_handler(adev_dimm->handle,
1506 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001507 }
1508 mutex_unlock(&acpi_desc->init_mutex);
1509}
1510
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001511static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1512{
1513 struct nfit_mem *nfit_mem;
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001514 int dimm_count = 0, rc;
1515 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001516
1517 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001518 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001519 unsigned long flags = 0, cmd_mask;
Dan Williamscaa603a2017-04-14 10:27:11 -07001520 struct nfit_memdev *nfit_memdev;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001521 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001522 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001523
1524 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1525 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1526 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001527 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001528 continue;
1529 }
1530
1531 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
Dan Williams8f078b32017-05-04 14:01:24 -07001532 set_bit(NDD_ALIASING, &flags);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001533
Dan Williamscaa603a2017-04-14 10:27:11 -07001534 /* collate flags across all memdevs for this dimm */
1535 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1536 struct acpi_nfit_memory_map *dimm_memdev;
1537
1538 dimm_memdev = __to_nfit_memdev(nfit_mem);
1539 if (dimm_memdev->device_handle
1540 != nfit_memdev->memdev->device_handle)
1541 continue;
1542 dimm_memdev->flags |= nfit_memdev->memdev->flags;
1543 }
1544
Dan Williams58138822015-06-23 20:08:34 -04001545 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001546 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams8f078b32017-05-04 14:01:24 -07001547 set_bit(NDD_UNARMED, &flags);
Dan Williams58138822015-06-23 20:08:34 -04001548
Dan Williams62232e452015-06-08 14:27:06 -04001549 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1550 if (rc)
1551 continue;
1552
Dan Williamse3654ec2016-04-28 16:17:07 -07001553 /*
Dan Williams31eca762016-04-28 16:23:43 -07001554 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1555 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1556 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001557 */
Dan Williams31eca762016-04-28 16:23:43 -07001558 cmd_mask = 1UL << ND_CMD_CALL;
1559 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1560 cmd_mask |= nfit_mem->dsm_mask;
1561
Dan Williamse5ae3b22016-06-07 17:00:04 -07001562 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1563 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001564 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001565 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001566 flags, cmd_mask, flush ? flush->hint_count : 0,
1567 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001568 if (!nvdimm)
1569 return -ENOMEM;
1570
1571 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001572 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001573
1574 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1575 continue;
1576
Dan Williams14999342017-04-13 19:46:36 -07001577 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001578 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001579 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1580 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1581 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Dan Williams14999342017-04-13 19:46:36 -07001582 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1583 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
Dan Williams58138822015-06-23 20:08:34 -04001584
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001585 }
1586
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001587 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1588 if (rc)
1589 return rc;
1590
1591 /*
1592 * Now that dimms are successfully registered, and async registration
1593 * is flushed, attempt to enable event notification.
1594 */
1595 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1596 struct kernfs_node *nfit_kernfs;
1597
1598 nvdimm = nfit_mem->nvdimm;
1599 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1600 if (nfit_kernfs)
1601 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1602 "flags");
1603 sysfs_put(nfit_kernfs);
1604 if (!nfit_mem->flags_attr)
1605 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1606 nvdimm_name(nvdimm));
1607 }
1608
1609 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1610 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001611}
1612
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001613/*
1614 * These constants are private because there are no kernel consumers of
1615 * these commands.
1616 */
1617enum nfit_aux_cmds {
1618 NFIT_CMD_TRANSLATE_SPA = 5,
1619 NFIT_CMD_ARS_INJECT_SET = 7,
1620 NFIT_CMD_ARS_INJECT_CLEAR = 8,
1621 NFIT_CMD_ARS_INJECT_GET = 9,
1622};
1623
Dan Williams62232e452015-06-08 14:27:06 -04001624static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1625{
1626 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001627 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
Dan Williams62232e452015-06-08 14:27:06 -04001628 struct acpi_device *adev;
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001629 unsigned long dsm_mask;
Dan Williams62232e452015-06-08 14:27:06 -04001630 int i;
1631
Dan Williamse3654ec2016-04-28 16:17:07 -07001632 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001633 adev = to_acpi_dev(acpi_desc);
1634 if (!adev)
1635 return;
1636
Dan Williamsd4f32362016-03-03 16:08:54 -08001637 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Andy Shevchenko94116f82017-06-05 19:40:46 +03001638 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001639 set_bit(i, &nd_desc->cmd_mask);
Jerry Hoemann37d74842017-06-30 20:41:24 -07001640 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001641
1642 dsm_mask =
1643 (1 << ND_CMD_ARS_CAP) |
1644 (1 << ND_CMD_ARS_START) |
1645 (1 << ND_CMD_ARS_STATUS) |
1646 (1 << ND_CMD_CLEAR_ERROR) |
1647 (1 << NFIT_CMD_TRANSLATE_SPA) |
1648 (1 << NFIT_CMD_ARS_INJECT_SET) |
1649 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
1650 (1 << NFIT_CMD_ARS_INJECT_GET);
1651 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1652 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1653 set_bit(i, &nd_desc->bus_dsm_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001654}
1655
Dan Williams1f7df6f2015-06-09 20:13:14 -04001656static ssize_t range_index_show(struct device *dev,
1657 struct device_attribute *attr, char *buf)
1658{
1659 struct nd_region *nd_region = to_nd_region(dev);
1660 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1661
1662 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1663}
1664static DEVICE_ATTR_RO(range_index);
1665
1666static struct attribute *acpi_nfit_region_attributes[] = {
1667 &dev_attr_range_index.attr,
1668 NULL,
1669};
1670
Arvind Yadav5e937462017-06-22 15:44:41 +05301671static const struct attribute_group acpi_nfit_region_attribute_group = {
Dan Williams1f7df6f2015-06-09 20:13:14 -04001672 .name = "nfit",
1673 .attrs = acpi_nfit_region_attributes,
1674};
1675
1676static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1677 &nd_region_attribute_group,
1678 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001679 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001680 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001681 &acpi_nfit_region_attribute_group,
1682 NULL,
1683};
1684
Dan Williamseaf96152015-05-01 13:11:27 -04001685/* enough info to uniquely specify an interleave set */
1686struct nfit_set_info {
1687 struct nfit_set_info_map {
1688 u64 region_offset;
1689 u32 serial_number;
1690 u32 pad;
1691 } mapping[0];
1692};
1693
Dan Williamsc12c48c2017-06-04 10:59:15 +09001694struct nfit_set_info2 {
1695 struct nfit_set_info_map2 {
1696 u64 region_offset;
1697 u32 serial_number;
1698 u16 vendor_id;
1699 u16 manufacturing_date;
1700 u8 manufacturing_location;
1701 u8 reserved[31];
1702 } mapping[0];
1703};
1704
Dan Williamseaf96152015-05-01 13:11:27 -04001705static size_t sizeof_nfit_set_info(int num_mappings)
1706{
1707 return sizeof(struct nfit_set_info)
1708 + num_mappings * sizeof(struct nfit_set_info_map);
1709}
1710
Dan Williamsc12c48c2017-06-04 10:59:15 +09001711static size_t sizeof_nfit_set_info2(int num_mappings)
1712{
1713 return sizeof(struct nfit_set_info2)
1714 + num_mappings * sizeof(struct nfit_set_info_map2);
1715}
1716
Dan Williams86ef58a2017-02-28 18:32:48 -08001717static int cmp_map_compat(const void *m0, const void *m1)
Dan Williamseaf96152015-05-01 13:11:27 -04001718{
1719 const struct nfit_set_info_map *map0 = m0;
1720 const struct nfit_set_info_map *map1 = m1;
1721
1722 return memcmp(&map0->region_offset, &map1->region_offset,
1723 sizeof(u64));
1724}
1725
Dan Williams86ef58a2017-02-28 18:32:48 -08001726static int cmp_map(const void *m0, const void *m1)
1727{
1728 const struct nfit_set_info_map *map0 = m0;
1729 const struct nfit_set_info_map *map1 = m1;
1730
Dan Williamsb03b99a2017-03-27 21:53:38 -07001731 if (map0->region_offset < map1->region_offset)
1732 return -1;
1733 else if (map0->region_offset > map1->region_offset)
1734 return 1;
1735 return 0;
Dan Williams86ef58a2017-02-28 18:32:48 -08001736}
1737
Dan Williamsc12c48c2017-06-04 10:59:15 +09001738static int cmp_map2(const void *m0, const void *m1)
1739{
1740 const struct nfit_set_info_map2 *map0 = m0;
1741 const struct nfit_set_info_map2 *map1 = m1;
1742
1743 if (map0->region_offset < map1->region_offset)
1744 return -1;
1745 else if (map0->region_offset > map1->region_offset)
1746 return 1;
1747 return 0;
1748}
1749
Dan Williamseaf96152015-05-01 13:11:27 -04001750/* Retrieve the nth entry referencing this spa */
1751static struct acpi_nfit_memory_map *memdev_from_spa(
1752 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1753{
1754 struct nfit_memdev *nfit_memdev;
1755
1756 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1757 if (nfit_memdev->memdev->range_index == range_index)
1758 if (n-- == 0)
1759 return nfit_memdev->memdev;
1760 return NULL;
1761}
1762
1763static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1764 struct nd_region_desc *ndr_desc,
1765 struct acpi_nfit_system_address *spa)
1766{
Dan Williamseaf96152015-05-01 13:11:27 -04001767 struct device *dev = acpi_desc->dev;
1768 struct nd_interleave_set *nd_set;
1769 u16 nr = ndr_desc->num_mappings;
Dan Williamsc12c48c2017-06-04 10:59:15 +09001770 struct nfit_set_info2 *info2;
Dan Williamseaf96152015-05-01 13:11:27 -04001771 struct nfit_set_info *info;
Dan Williams8f2bc242017-06-06 11:39:30 -07001772 int i;
Dan Williamseaf96152015-05-01 13:11:27 -04001773
Dan Williamsfaec6f82017-06-06 11:10:51 -07001774 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1775 if (!nd_set)
1776 return -ENOMEM;
1777 ndr_desc->nd_set = nd_set;
1778 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
1779
Dan Williamseaf96152015-05-01 13:11:27 -04001780 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1781 if (!info)
1782 return -ENOMEM;
Dan Williamsc12c48c2017-06-04 10:59:15 +09001783
1784 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
1785 if (!info2)
1786 return -ENOMEM;
1787
Dan Williamseaf96152015-05-01 13:11:27 -04001788 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07001789 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04001790 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williamsc12c48c2017-06-04 10:59:15 +09001791 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07001792 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04001793 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1794 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1795 spa->range_index, i);
1796
1797 if (!memdev || !nfit_mem->dcr) {
1798 dev_err(dev, "%s: failed to find DCR\n", __func__);
1799 return -ENODEV;
1800 }
1801
1802 map->region_offset = memdev->region_offset;
1803 map->serial_number = nfit_mem->dcr->serial_number;
Dan Williamsc12c48c2017-06-04 10:59:15 +09001804
1805 map2->region_offset = memdev->region_offset;
1806 map2->serial_number = nfit_mem->dcr->serial_number;
1807 map2->vendor_id = nfit_mem->dcr->vendor_id;
1808 map2->manufacturing_date = nfit_mem->dcr->manufacturing_date;
1809 map2->manufacturing_location = nfit_mem->dcr->manufacturing_location;
Dan Williamseaf96152015-05-01 13:11:27 -04001810 }
1811
Dan Williamsc12c48c2017-06-04 10:59:15 +09001812 /* v1.1 namespaces */
Dan Williamseaf96152015-05-01 13:11:27 -04001813 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1814 cmp_map, NULL);
Dan Williamsc12c48c2017-06-04 10:59:15 +09001815 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
Dan Williams86ef58a2017-02-28 18:32:48 -08001816
Dan Williamsc12c48c2017-06-04 10:59:15 +09001817 /* v1.2 namespaces */
1818 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
1819 cmp_map2, NULL);
1820 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
1821
1822 /* support v1.1 namespaces created with the wrong sort order */
Dan Williams86ef58a2017-02-28 18:32:48 -08001823 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1824 cmp_map_compat, NULL);
1825 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1826
Dan Williamseaf96152015-05-01 13:11:27 -04001827 ndr_desc->nd_set = nd_set;
1828 devm_kfree(dev, info);
Dan Williamsc12c48c2017-06-04 10:59:15 +09001829 devm_kfree(dev, info2);
Dan Williamseaf96152015-05-01 13:11:27 -04001830
1831 return 0;
1832}
1833
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001834static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1835{
1836 struct acpi_nfit_interleave *idt = mmio->idt;
1837 u32 sub_line_offset, line_index, line_offset;
1838 u64 line_no, table_skip_count, table_offset;
1839
1840 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1841 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1842 line_offset = idt->line_offset[line_index]
1843 * mmio->line_size;
1844 table_offset = table_skip_count * mmio->table_size;
1845
1846 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1847}
1848
Ross Zwislerde4a1962015-08-20 16:27:38 -06001849static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001850{
1851 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1852 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001853 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001854
1855 if (mmio->num_lines)
1856 offset = to_interleave_offset(offset, mmio);
1857
Ross Zwisler68202c92016-07-29 14:59:12 -06001858 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001859}
1860
1861static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1862 resource_size_t dpa, unsigned int len, unsigned int write)
1863{
1864 u64 cmd, offset;
1865 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1866
1867 enum {
1868 BCW_OFFSET_MASK = (1ULL << 48)-1,
1869 BCW_LEN_SHIFT = 48,
1870 BCW_LEN_MASK = (1ULL << 8) - 1,
1871 BCW_CMD_SHIFT = 56,
1872 };
1873
1874 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1875 len = len >> L1_CACHE_SHIFT;
1876 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1877 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1878
1879 offset = nfit_blk->cmd_offset + mmio->size * bw;
1880 if (mmio->num_lines)
1881 offset = to_interleave_offset(offset, mmio);
1882
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001883 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001884 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001885
Dan Williamsaef25332016-02-12 17:01:11 -08001886 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001887 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001888}
1889
1890static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1891 resource_size_t dpa, void *iobuf, size_t len, int rw,
1892 unsigned int lane)
1893{
1894 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1895 unsigned int copied = 0;
1896 u64 base_offset;
1897 int rc;
1898
1899 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1900 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001901 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1902 while (len) {
1903 unsigned int c;
1904 u64 offset;
1905
1906 if (mmio->num_lines) {
1907 u32 line_offset;
1908
1909 offset = to_interleave_offset(base_offset + copied,
1910 mmio);
1911 div_u64_rem(offset, mmio->line_size, &line_offset);
1912 c = min_t(size_t, len, mmio->line_size - line_offset);
1913 } else {
1914 offset = base_offset + nfit_blk->bdw_offset;
1915 c = len;
1916 }
1917
1918 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001919 memcpy_to_pmem(mmio->addr.aperture + offset,
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001920 iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001921 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001922 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001923 mmio_flush_range((void __force *)
1924 mmio->addr.aperture + offset, c);
1925
Dan Williams6abccd12017-01-13 14:14:23 -08001926 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001927 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001928
1929 copied += c;
1930 len -= c;
1931 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001932
1933 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001934 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001935
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001936 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1937 return rc;
1938}
1939
1940static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1941 resource_size_t dpa, void *iobuf, u64 len, int rw)
1942{
1943 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1944 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1945 struct nd_region *nd_region = nfit_blk->nd_region;
1946 unsigned int lane, copied = 0;
1947 int rc = 0;
1948
1949 lane = nd_region_acquire_lane(nd_region);
1950 while (len) {
1951 u64 c = min(len, mmio->size);
1952
1953 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1954 iobuf + copied, c, rw, lane);
1955 if (rc)
1956 break;
1957
1958 copied += c;
1959 len -= c;
1960 }
1961 nd_region_release_lane(nd_region, lane);
1962
1963 return rc;
1964}
1965
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001966static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1967 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1968{
1969 if (idt) {
1970 mmio->num_lines = idt->line_count;
1971 mmio->line_size = idt->line_size;
1972 if (interleave_ways == 0)
1973 return -ENXIO;
1974 mmio->table_size = mmio->num_lines * interleave_ways
1975 * mmio->line_size;
1976 }
1977
1978 return 0;
1979}
1980
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001981static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1982 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1983{
1984 struct nd_cmd_dimm_flags flags;
1985 int rc;
1986
1987 memset(&flags, 0, sizeof(flags));
1988 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08001989 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001990
1991 if (rc >= 0 && flags.status == 0)
1992 nfit_blk->dimm_flags = flags.flags;
1993 else if (rc == -ENOTTY) {
1994 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08001995 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001996 rc = 0;
1997 } else
1998 rc = -ENXIO;
1999
2000 return rc;
2001}
2002
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002003static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2004 struct device *dev)
2005{
2006 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002007 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2008 struct nfit_blk_mmio *mmio;
2009 struct nfit_blk *nfit_blk;
2010 struct nfit_mem *nfit_mem;
2011 struct nvdimm *nvdimm;
2012 int rc;
2013
2014 nvdimm = nd_blk_region_to_dimm(ndbr);
2015 nfit_mem = nvdimm_provider_data(nvdimm);
2016 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2017 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
2018 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04002019 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2020 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002021 return -ENXIO;
2022 }
2023
2024 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2025 if (!nfit_blk)
2026 return -ENOMEM;
2027 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2028 nfit_blk->nd_region = to_nd_region(dev);
2029
2030 /* map block aperture memory */
2031 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2032 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07002033 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2034 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002035 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002036 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
2037 nvdimm_name(nvdimm));
2038 return -ENOMEM;
2039 }
2040 mmio->size = nfit_mem->bdw->size;
2041 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2042 mmio->idt = nfit_mem->idt_bdw;
2043 mmio->spa = nfit_mem->spa_bdw;
2044 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2045 nfit_mem->memdev_bdw->interleave_ways);
2046 if (rc) {
2047 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
2048 __func__, nvdimm_name(nvdimm));
2049 return rc;
2050 }
2051
2052 /* map block control memory */
2053 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2054 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2055 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07002056 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2057 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002058 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002059 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
2060 nvdimm_name(nvdimm));
2061 return -ENOMEM;
2062 }
2063 mmio->size = nfit_mem->dcr->window_size;
2064 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2065 mmio->idt = nfit_mem->idt_dcr;
2066 mmio->spa = nfit_mem->spa_dcr;
2067 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2068 nfit_mem->memdev_dcr->interleave_ways);
2069 if (rc) {
2070 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
2071 __func__, nvdimm_name(nvdimm));
2072 return rc;
2073 }
2074
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002075 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2076 if (rc < 0) {
2077 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
2078 __func__, nvdimm_name(nvdimm));
2079 return rc;
2080 }
2081
Dan Williamsf284a4f2016-07-07 19:44:50 -07002082 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002083 dev_warn(dev, "unable to guarantee persistence of writes\n");
2084
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002085 if (mmio->line_size == 0)
2086 return 0;
2087
2088 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2089 + 8 > mmio->line_size) {
2090 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2091 return -ENXIO;
2092 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2093 + 8 > mmio->line_size) {
2094 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2095 return -ENXIO;
2096 }
2097
2098 return 0;
2099}
2100
Dan Williamsaef25332016-02-12 17:01:11 -08002101static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002102 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002103{
Dan Williamsaef25332016-02-12 17:01:11 -08002104 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002105 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08002106 int cmd_rc, rc;
2107
Dan Williams1cf03c02016-02-17 13:01:23 -08002108 cmd->address = spa->address;
2109 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08002110 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2111 sizeof(*cmd), &cmd_rc);
2112 if (rc < 0)
2113 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002114 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002115}
2116
Dan Williams1cf03c02016-02-17 13:01:23 -08002117static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002118{
2119 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002120 int cmd_rc;
2121 struct nd_cmd_ars_start ars_start;
2122 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2123 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002124
Dan Williams1cf03c02016-02-17 13:01:23 -08002125 memset(&ars_start, 0, sizeof(ars_start));
2126 ars_start.address = spa->address;
2127 ars_start.length = spa->length;
2128 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2129 ars_start.type = ND_ARS_PERSISTENT;
2130 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2131 ars_start.type = ND_ARS_VOLATILE;
2132 else
2133 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07002134
Dan Williams1cf03c02016-02-17 13:01:23 -08002135 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2136 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08002137
Dan Williams1cf03c02016-02-17 13:01:23 -08002138 if (rc < 0)
2139 return rc;
2140 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002141}
2142
Dan Williams1cf03c02016-02-17 13:01:23 -08002143static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07002144{
Dan Williamsaef25332016-02-12 17:01:11 -08002145 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002146 struct nd_cmd_ars_start ars_start;
2147 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2148 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07002149
Dan Williams1cf03c02016-02-17 13:01:23 -08002150 memset(&ars_start, 0, sizeof(ars_start));
2151 ars_start.address = ars_status->restart_address;
2152 ars_start.length = ars_status->restart_length;
2153 ars_start.type = ars_status->type;
2154 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2155 sizeof(ars_start), &cmd_rc);
2156 if (rc < 0)
2157 return rc;
2158 return cmd_rc;
2159}
Dan Williamsaef25332016-02-12 17:01:11 -08002160
Dan Williams1cf03c02016-02-17 13:01:23 -08002161static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2162{
2163 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2164 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2165 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08002166
Dan Williams1cf03c02016-02-17 13:01:23 -08002167 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2168 acpi_desc->ars_status_size, &cmd_rc);
2169 if (rc < 0)
2170 return rc;
2171 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002172}
2173
Dan Williams82aa37c2016-12-06 12:45:24 -08002174static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002175 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07002176{
Dan Williams82aa37c2016-12-06 12:45:24 -08002177 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
Vishal Verma0caeef62015-12-24 19:21:43 -07002178 int rc;
2179 u32 i;
2180
Dan Williams82aa37c2016-12-06 12:45:24 -08002181 /*
2182 * First record starts at 44 byte offset from the start of the
2183 * payload.
2184 */
2185 if (ars_status->out_length < 44)
2186 return 0;
Vishal Verma0caeef62015-12-24 19:21:43 -07002187 for (i = 0; i < ars_status->num_records; i++) {
Dan Williams82aa37c2016-12-06 12:45:24 -08002188 /* only process full records */
2189 if (ars_status->out_length
2190 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2191 break;
Vishal Verma0caeef62015-12-24 19:21:43 -07002192 rc = nvdimm_bus_add_poison(nvdimm_bus,
2193 ars_status->records[i].err_address,
2194 ars_status->records[i].length);
2195 if (rc)
2196 return rc;
2197 }
Dan Williams82aa37c2016-12-06 12:45:24 -08002198 if (i < ars_status->num_records)
2199 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
Vishal Verma0caeef62015-12-24 19:21:43 -07002200
2201 return 0;
2202}
2203
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002204static void acpi_nfit_remove_resource(void *data)
2205{
2206 struct resource *res = data;
2207
2208 remove_resource(res);
2209}
2210
2211static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2212 struct nd_region_desc *ndr_desc)
2213{
2214 struct resource *res, *nd_res = ndr_desc->res;
2215 int is_pmem, ret;
2216
2217 /* No operation if the region is already registered as PMEM */
2218 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2219 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2220 if (is_pmem == REGION_INTERSECTS)
2221 return 0;
2222
2223 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2224 if (!res)
2225 return -ENOMEM;
2226
2227 res->name = "Persistent Memory";
2228 res->start = nd_res->start;
2229 res->end = nd_res->end;
2230 res->flags = IORESOURCE_MEM;
2231 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2232
2233 ret = insert_resource(&iomem_resource, res);
2234 if (ret)
2235 return ret;
2236
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302237 ret = devm_add_action_or_reset(acpi_desc->dev,
2238 acpi_nfit_remove_resource,
2239 res);
2240 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002241 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002242
2243 return 0;
2244}
2245
Dan Williams1f7df6f2015-06-09 20:13:14 -04002246static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002247 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002248 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002249 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002250{
2251 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2252 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002253 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002254 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002255 struct nfit_mem *nfit_mem;
Dan Williamsfaec6f82017-06-06 11:10:51 -07002256 int blk_valid = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002257
2258 if (!nvdimm) {
2259 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2260 spa->range_index, memdev->device_handle);
2261 return -ENODEV;
2262 }
2263
Dan Williams44c462e2016-09-19 16:38:50 -07002264 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002265 switch (nfit_spa_type(spa)) {
2266 case NFIT_SPA_PM:
2267 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002268 mapping->start = memdev->address;
2269 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002270 break;
2271 case NFIT_SPA_DCR:
2272 nfit_mem = nvdimm_provider_data(nvdimm);
2273 if (!nfit_mem || !nfit_mem->bdw) {
2274 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2275 spa->range_index, nvdimm_name(nvdimm));
2276 } else {
Dan Williams44c462e2016-09-19 16:38:50 -07002277 mapping->size = nfit_mem->bdw->capacity;
2278 mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04002279 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002280 blk_valid = 1;
2281 }
2282
Dan Williams44c462e2016-09-19 16:38:50 -07002283 ndr_desc->mapping = mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002284 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002285 ndbr_desc = to_blk_region_desc(ndr_desc);
2286 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002287 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williamsfaec6f82017-06-06 11:10:51 -07002288 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2289 if (rc)
2290 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002291 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2292 ndr_desc);
2293 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002294 return -ENOMEM;
2295 break;
2296 }
2297
2298 return 0;
2299}
2300
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002301static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2302{
2303 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2304 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2305 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2306 nfit_spa_type(spa) == NFIT_SPA_PCD);
2307}
2308
Dan Williams1f7df6f2015-06-09 20:13:14 -04002309static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2310 struct nfit_spa *nfit_spa)
2311{
Dan Williams44c462e2016-09-19 16:38:50 -07002312 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002313 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002314 struct nd_blk_region_desc ndbr_desc;
2315 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002316 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002317 struct nvdimm_bus *nvdimm_bus;
2318 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002319 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002320
Dan Williams1cf03c02016-02-17 13:01:23 -08002321 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002322 return 0;
2323
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002324 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04002325 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2326 __func__);
2327 return 0;
2328 }
2329
2330 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002331 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002332 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002333 res.start = spa->address;
2334 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002335 ndr_desc = &ndbr_desc.ndr_desc;
2336 ndr_desc->res = &res;
2337 ndr_desc->provider_data = nfit_spa;
2338 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002339 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2340 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2341 spa->proximity_domain);
2342 else
2343 ndr_desc->numa_node = NUMA_NO_NODE;
2344
Dan Williams1f7df6f2015-06-09 20:13:14 -04002345 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2346 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002347 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002348
2349 if (memdev->range_index != spa->range_index)
2350 continue;
2351 if (count >= ND_MAX_MAPPINGS) {
2352 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2353 spa->range_index, ND_MAX_MAPPINGS);
2354 return -ENXIO;
2355 }
Dan Williams44c462e2016-09-19 16:38:50 -07002356 mapping = &mappings[count++];
2357 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002358 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002359 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002360 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002361 }
2362
Dan Williams44c462e2016-09-19 16:38:50 -07002363 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002364 ndr_desc->num_mappings = count;
2365 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002366 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002367 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002368
Dan Williams1f7df6f2015-06-09 20:13:14 -04002369 nvdimm_bus = acpi_desc->nvdimm_bus;
2370 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002371 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002372 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002373 dev_warn(acpi_desc->dev,
2374 "failed to insert pmem resource to iomem: %d\n",
2375 rc);
Dan Williams48901162016-03-09 17:15:43 -08002376 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002377 }
Dan Williams48901162016-03-09 17:15:43 -08002378
Dan Williams1cf03c02016-02-17 13:01:23 -08002379 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2380 ndr_desc);
2381 if (!nfit_spa->nd_region)
2382 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002383 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002384 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2385 ndr_desc);
2386 if (!nfit_spa->nd_region)
2387 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002388 } else if (nfit_spa_is_virtual(spa)) {
2389 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2390 ndr_desc);
2391 if (!nfit_spa->nd_region)
2392 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002393 }
Vishal Verma20985162015-10-27 16:58:27 -06002394
Dan Williams1cf03c02016-02-17 13:01:23 -08002395 out:
2396 if (rc)
2397 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2398 nfit_spa->spa->range_index);
2399 return rc;
2400}
2401
2402static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2403 u32 max_ars)
2404{
2405 struct device *dev = acpi_desc->dev;
2406 struct nd_cmd_ars_status *ars_status;
2407
2408 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2409 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2410 return 0;
2411 }
2412
2413 if (acpi_desc->ars_status)
2414 devm_kfree(dev, acpi_desc->ars_status);
2415 acpi_desc->ars_status = NULL;
2416 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2417 if (!ars_status)
2418 return -ENOMEM;
2419 acpi_desc->ars_status = ars_status;
2420 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002421 return 0;
2422}
2423
Dan Williams1cf03c02016-02-17 13:01:23 -08002424static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2425 struct nfit_spa *nfit_spa)
2426{
2427 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2428 int rc;
2429
2430 if (!nfit_spa->max_ars) {
2431 struct nd_cmd_ars_cap ars_cap;
2432
2433 memset(&ars_cap, 0, sizeof(ars_cap));
2434 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2435 if (rc < 0)
2436 return rc;
2437 nfit_spa->max_ars = ars_cap.max_ars_out;
2438 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2439 /* check that the supported scrub types match the spa type */
2440 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2441 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2442 return -ENOTTY;
2443 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2444 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2445 return -ENOTTY;
2446 }
2447
2448 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2449 return -ENOMEM;
2450
2451 rc = ars_get_status(acpi_desc);
2452 if (rc < 0 && rc != -ENOSPC)
2453 return rc;
2454
Dan Williams82aa37c2016-12-06 12:45:24 -08002455 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
Dan Williams1cf03c02016-02-17 13:01:23 -08002456 return -ENOMEM;
2457
2458 return 0;
2459}
2460
2461static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2462 struct nfit_spa *nfit_spa)
2463{
2464 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2465 unsigned int overflow_retry = scrub_overflow_abort;
2466 u64 init_ars_start = 0, init_ars_len = 0;
2467 struct device *dev = acpi_desc->dev;
2468 unsigned int tmo = scrub_timeout;
2469 int rc;
2470
Vishal Verma37b137f2016-07-23 21:51:42 -07002471 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002472 return;
2473
2474 rc = ars_start(acpi_desc, nfit_spa);
2475 /*
2476 * If we timed out the initial scan we'll still be busy here,
2477 * and will wait another timeout before giving up permanently.
2478 */
2479 if (rc < 0 && rc != -EBUSY)
2480 return;
2481
2482 do {
2483 u64 ars_start, ars_len;
2484
2485 if (acpi_desc->cancel)
2486 break;
2487 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2488 if (rc == -ENOTTY)
2489 break;
2490 if (rc == -EBUSY && !tmo) {
2491 dev_warn(dev, "range %d ars timeout, aborting\n",
2492 spa->range_index);
2493 break;
2494 }
2495
2496 if (rc == -EBUSY) {
2497 /*
2498 * Note, entries may be appended to the list
2499 * while the lock is dropped, but the workqueue
2500 * being active prevents entries being deleted /
2501 * freed.
2502 */
2503 mutex_unlock(&acpi_desc->init_mutex);
2504 ssleep(1);
2505 tmo--;
2506 mutex_lock(&acpi_desc->init_mutex);
2507 continue;
2508 }
2509
2510 /* we got some results, but there are more pending... */
2511 if (rc == -ENOSPC && overflow_retry--) {
2512 if (!init_ars_len) {
2513 init_ars_len = acpi_desc->ars_status->length;
2514 init_ars_start = acpi_desc->ars_status->address;
2515 }
2516 rc = ars_continue(acpi_desc);
2517 }
2518
2519 if (rc < 0) {
2520 dev_warn(dev, "range %d ars continuation failed\n",
2521 spa->range_index);
2522 break;
2523 }
2524
2525 if (init_ars_len) {
2526 ars_start = init_ars_start;
2527 ars_len = init_ars_len;
2528 } else {
2529 ars_start = acpi_desc->ars_status->address;
2530 ars_len = acpi_desc->ars_status->length;
2531 }
2532 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2533 spa->range_index, ars_start, ars_len);
2534 /* notify the region about new poison entries */
2535 nvdimm_region_notify(nfit_spa->nd_region,
2536 NVDIMM_REVALIDATE_POISON);
2537 break;
2538 } while (1);
2539}
2540
2541static void acpi_nfit_scrub(struct work_struct *work)
2542{
2543 struct device *dev;
2544 u64 init_scrub_length = 0;
2545 struct nfit_spa *nfit_spa;
2546 u64 init_scrub_address = 0;
2547 bool init_ars_done = false;
2548 struct acpi_nfit_desc *acpi_desc;
2549 unsigned int tmo = scrub_timeout;
2550 unsigned int overflow_retry = scrub_overflow_abort;
2551
2552 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2553 dev = acpi_desc->dev;
2554
2555 /*
2556 * We scrub in 2 phases. The first phase waits for any platform
2557 * firmware initiated scrubs to complete and then we go search for the
2558 * affected spa regions to mark them scanned. In the second phase we
2559 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002560 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2561 * the first phase, but really only care about running phase 2, where
2562 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002563 */
2564
2565 /* process platform firmware initiated scrubs */
2566 retry:
2567 mutex_lock(&acpi_desc->init_mutex);
2568 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2569 struct nd_cmd_ars_status *ars_status;
2570 struct acpi_nfit_system_address *spa;
2571 u64 ars_start, ars_len;
2572 int rc;
2573
2574 if (acpi_desc->cancel)
2575 break;
2576
2577 if (nfit_spa->nd_region)
2578 continue;
2579
2580 if (init_ars_done) {
2581 /*
2582 * No need to re-query, we're now just
2583 * reconciling all the ranges covered by the
2584 * initial scrub
2585 */
2586 rc = 0;
2587 } else
2588 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2589
2590 if (rc == -ENOTTY) {
2591 /* no ars capability, just register spa and move on */
2592 acpi_nfit_register_region(acpi_desc, nfit_spa);
2593 continue;
2594 }
2595
2596 if (rc == -EBUSY && !tmo) {
2597 /* fallthrough to directed scrub in phase 2 */
2598 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2599 break;
2600 } else if (rc == -EBUSY) {
2601 mutex_unlock(&acpi_desc->init_mutex);
2602 ssleep(1);
2603 tmo--;
2604 goto retry;
2605 }
2606
2607 /* we got some results, but there are more pending... */
2608 if (rc == -ENOSPC && overflow_retry--) {
2609 ars_status = acpi_desc->ars_status;
2610 /*
2611 * Record the original scrub range, so that we
2612 * can recall all the ranges impacted by the
2613 * initial scrub.
2614 */
2615 if (!init_scrub_length) {
2616 init_scrub_length = ars_status->length;
2617 init_scrub_address = ars_status->address;
2618 }
2619 rc = ars_continue(acpi_desc);
2620 if (rc == 0) {
2621 mutex_unlock(&acpi_desc->init_mutex);
2622 goto retry;
2623 }
2624 }
2625
2626 if (rc < 0) {
2627 /*
2628 * Initial scrub failed, we'll give it one more
2629 * try below...
2630 */
2631 break;
2632 }
2633
2634 /* We got some final results, record completed ranges */
2635 ars_status = acpi_desc->ars_status;
2636 if (init_scrub_length) {
2637 ars_start = init_scrub_address;
2638 ars_len = ars_start + init_scrub_length;
2639 } else {
2640 ars_start = ars_status->address;
2641 ars_len = ars_status->length;
2642 }
2643 spa = nfit_spa->spa;
2644
2645 if (!init_ars_done) {
2646 init_ars_done = true;
2647 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2648 ars_start, ars_len);
2649 }
2650 if (ars_start <= spa->address && ars_start + ars_len
2651 >= spa->address + spa->length)
2652 acpi_nfit_register_region(acpi_desc, nfit_spa);
2653 }
2654
2655 /*
2656 * For all the ranges not covered by an initial scrub we still
2657 * want to see if there are errors, but it's ok to discover them
2658 * asynchronously.
2659 */
2660 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2661 /*
2662 * Flag all the ranges that still need scrubbing, but
2663 * register them now to make data available.
2664 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002665 if (!nfit_spa->nd_region) {
2666 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002667 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002668 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002669 }
Dan Williams9ccaed42017-04-13 22:48:46 -07002670 acpi_desc->init_complete = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002671
2672 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2673 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002674 acpi_desc->scrub_count++;
2675 if (acpi_desc->scrub_count_state)
2676 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002677 mutex_unlock(&acpi_desc->init_mutex);
2678}
2679
Dan Williams1f7df6f2015-06-09 20:13:14 -04002680static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2681{
2682 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002683 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002684
Dan Williams1cf03c02016-02-17 13:01:23 -08002685 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2686 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2687 /* BLK regions don't need to wait for ars results */
2688 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2689 if (rc)
2690 return rc;
2691 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002692
Dan Williamsfbabd822017-04-18 09:56:31 -07002693 if (!acpi_desc->cancel)
2694 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002695 return 0;
2696}
2697
Vishal Verma20985162015-10-27 16:58:27 -06002698static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2699 struct nfit_table_prev *prev)
2700{
2701 struct device *dev = acpi_desc->dev;
2702
2703 if (!list_empty(&prev->spas) ||
2704 !list_empty(&prev->memdevs) ||
2705 !list_empty(&prev->dcrs) ||
2706 !list_empty(&prev->bdws) ||
2707 !list_empty(&prev->idts) ||
2708 !list_empty(&prev->flushes)) {
2709 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2710 return -ENXIO;
2711 }
2712 return 0;
2713}
2714
Vishal Verma37b137f2016-07-23 21:51:42 -07002715static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2716{
2717 struct device *dev = acpi_desc->dev;
2718 struct kernfs_node *nfit;
2719 struct device *bus_dev;
2720
2721 if (!ars_supported(acpi_desc->nvdimm_bus))
2722 return 0;
2723
2724 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2725 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2726 if (!nfit) {
2727 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2728 return -ENODEV;
2729 }
2730 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2731 sysfs_put(nfit);
2732 if (!acpi_desc->scrub_count_state) {
2733 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2734 return -ENODEV;
2735 }
2736
2737 return 0;
2738}
2739
Dan Williamsfbabd822017-04-18 09:56:31 -07002740static void acpi_nfit_unregister(void *data)
Dan Williams58cd71b2016-07-21 18:05:36 -07002741{
2742 struct acpi_nfit_desc *acpi_desc = data;
2743
Dan Williams58cd71b2016-07-21 18:05:36 -07002744 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002745}
2746
Dan Williamse7a11b42016-07-14 16:19:55 -07002747int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002748{
2749 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002750 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002751 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002752 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002753
Dan Williams58cd71b2016-07-21 18:05:36 -07002754 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002755 acpi_nfit_init_dsms(acpi_desc);
2756
Dan Williams58cd71b2016-07-21 18:05:36 -07002757 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2758 &acpi_desc->nd_desc);
2759 if (!acpi_desc->nvdimm_bus)
2760 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002761
Dan Williamsfbabd822017-04-18 09:56:31 -07002762 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
Dan Williams58cd71b2016-07-21 18:05:36 -07002763 acpi_desc);
2764 if (rc)
2765 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002766
2767 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2768 if (rc)
2769 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002770
2771 /* register this acpi_desc for mce notifications */
2772 mutex_lock(&acpi_desc_lock);
2773 list_add_tail(&acpi_desc->list, &acpi_descs);
2774 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002775 }
2776
Vishal Verma20985162015-10-27 16:58:27 -06002777 mutex_lock(&acpi_desc->init_mutex);
2778
2779 INIT_LIST_HEAD(&prev.spas);
2780 INIT_LIST_HEAD(&prev.memdevs);
2781 INIT_LIST_HEAD(&prev.dcrs);
2782 INIT_LIST_HEAD(&prev.bdws);
2783 INIT_LIST_HEAD(&prev.idts);
2784 INIT_LIST_HEAD(&prev.flushes);
2785
2786 list_cut_position(&prev.spas, &acpi_desc->spas,
2787 acpi_desc->spas.prev);
2788 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2789 acpi_desc->memdevs.prev);
2790 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2791 acpi_desc->dcrs.prev);
2792 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2793 acpi_desc->bdws.prev);
2794 list_cut_position(&prev.idts, &acpi_desc->idts,
2795 acpi_desc->idts.prev);
2796 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2797 acpi_desc->flushes.prev);
2798
Vishal Verma20985162015-10-27 16:58:27 -06002799 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002800 while (!IS_ERR_OR_NULL(data))
2801 data = add_table(acpi_desc, &prev, data, end);
2802
2803 if (IS_ERR(data)) {
2804 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2805 PTR_ERR(data));
2806 rc = PTR_ERR(data);
2807 goto out_unlock;
2808 }
2809
2810 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2811 if (rc)
2812 goto out_unlock;
2813
Dan Williams81ed4e32016-06-10 18:20:53 -07002814 rc = nfit_mem_init(acpi_desc);
2815 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002816 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002817
2818 rc = acpi_nfit_register_dimms(acpi_desc);
2819 if (rc)
2820 goto out_unlock;
2821
2822 rc = acpi_nfit_register_regions(acpi_desc);
2823
2824 out_unlock:
2825 mutex_unlock(&acpi_desc->init_mutex);
2826 return rc;
2827}
2828EXPORT_SYMBOL_GPL(acpi_nfit_init);
2829
Dan Williams7ae0fa432016-02-19 12:16:34 -08002830struct acpi_nfit_flush_work {
2831 struct work_struct work;
2832 struct completion cmp;
2833};
2834
2835static void flush_probe(struct work_struct *work)
2836{
2837 struct acpi_nfit_flush_work *flush;
2838
2839 flush = container_of(work, typeof(*flush), work);
2840 complete(&flush->cmp);
2841}
2842
2843static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2844{
2845 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2846 struct device *dev = acpi_desc->dev;
2847 struct acpi_nfit_flush_work flush;
Dan Williamse4714862017-02-02 10:31:00 -08002848 int rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002849
2850 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2851 device_lock(dev);
2852 device_unlock(dev);
2853
Dan Williams9ccaed42017-04-13 22:48:46 -07002854 /* bounce the init_mutex to make init_complete valid */
2855 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07002856 if (acpi_desc->cancel || acpi_desc->init_complete) {
2857 mutex_unlock(&acpi_desc->init_mutex);
Dan Williams9ccaed42017-04-13 22:48:46 -07002858 return 0;
Dan Williamsfbabd822017-04-18 09:56:31 -07002859 }
Dan Williams9ccaed42017-04-13 22:48:46 -07002860
Dan Williams7ae0fa432016-02-19 12:16:34 -08002861 /*
2862 * Scrub work could take 10s of seconds, userspace may give up so we
2863 * need to be interruptible while waiting.
2864 */
2865 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2866 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2867 queue_work(nfit_wq, &flush.work);
Dan Williamsfbabd822017-04-18 09:56:31 -07002868 mutex_unlock(&acpi_desc->init_mutex);
Dan Williamse4714862017-02-02 10:31:00 -08002869
2870 rc = wait_for_completion_interruptible(&flush.cmp);
2871 cancel_work_sync(&flush.work);
2872 return rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002873}
2874
Dan Williams87bf5722016-02-22 21:50:31 -08002875static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2876 struct nvdimm *nvdimm, unsigned int cmd)
2877{
2878 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2879
2880 if (nvdimm)
2881 return 0;
2882 if (cmd != ND_CMD_ARS_START)
2883 return 0;
2884
2885 /*
2886 * The kernel and userspace may race to initiate a scrub, but
2887 * the scrub thread is prepared to lose that initial race. It
2888 * just needs guarantees that any ars it initiates are not
2889 * interrupted by any intervening start reqeusts from userspace.
2890 */
2891 if (work_busy(&acpi_desc->work))
2892 return -EBUSY;
2893
2894 return 0;
2895}
2896
Vishal Verma6839a6d2016-07-23 21:51:21 -07002897int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
Vishal Verma37b137f2016-07-23 21:51:42 -07002898{
2899 struct device *dev = acpi_desc->dev;
2900 struct nfit_spa *nfit_spa;
2901
2902 if (work_busy(&acpi_desc->work))
2903 return -EBUSY;
2904
Vishal Verma37b137f2016-07-23 21:51:42 -07002905 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07002906 if (acpi_desc->cancel) {
2907 mutex_unlock(&acpi_desc->init_mutex);
2908 return 0;
2909 }
2910
Vishal Verma37b137f2016-07-23 21:51:42 -07002911 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2912 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2913
2914 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2915 continue;
2916
2917 nfit_spa->ars_required = 1;
2918 }
2919 queue_work(nfit_wq, &acpi_desc->work);
2920 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2921 mutex_unlock(&acpi_desc->init_mutex);
2922
2923 return 0;
2924}
2925
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002926void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002927{
2928 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002929
2930 dev_set_drvdata(dev, acpi_desc);
2931 acpi_desc->dev = dev;
2932 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2933 nd_desc = &acpi_desc->nd_desc;
2934 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002935 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002936 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002937 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002938 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002939 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2940
Dan Williamsb94d5232015-05-19 22:54:31 -04002941 INIT_LIST_HEAD(&acpi_desc->spas);
2942 INIT_LIST_HEAD(&acpi_desc->dcrs);
2943 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002944 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002945 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04002946 INIT_LIST_HEAD(&acpi_desc->memdevs);
2947 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002948 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06002949 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08002950 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04002951}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002952EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04002953
Dan Williams3c87f372017-04-03 13:52:14 -07002954static void acpi_nfit_put_table(void *table)
2955{
2956 acpi_put_table(table);
2957}
2958
Dan Williamsfbabd822017-04-18 09:56:31 -07002959void acpi_nfit_shutdown(void *data)
2960{
2961 struct acpi_nfit_desc *acpi_desc = data;
2962 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2963
2964 /*
2965 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2966 * race teardown
2967 */
2968 mutex_lock(&acpi_desc_lock);
2969 list_del(&acpi_desc->list);
2970 mutex_unlock(&acpi_desc_lock);
2971
2972 mutex_lock(&acpi_desc->init_mutex);
2973 acpi_desc->cancel = 1;
2974 mutex_unlock(&acpi_desc->init_mutex);
2975
2976 /*
2977 * Bounce the nvdimm bus lock to make sure any in-flight
2978 * acpi_nfit_ars_rescan() submissions have had a chance to
2979 * either submit or see ->cancel set.
2980 */
2981 device_lock(bus_dev);
2982 device_unlock(bus_dev);
2983
2984 flush_workqueue(nfit_wq);
2985}
2986EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
2987
Dan Williamsb94d5232015-05-19 22:54:31 -04002988static int acpi_nfit_add(struct acpi_device *adev)
2989{
Vishal Verma20985162015-10-27 16:58:27 -06002990 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04002991 struct acpi_nfit_desc *acpi_desc;
2992 struct device *dev = &adev->dev;
2993 struct acpi_table_header *tbl;
2994 acpi_status status = AE_OK;
2995 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07002996 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002997
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002998 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
Dan Williamsb94d5232015-05-19 22:54:31 -04002999 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06003000 /* This is ok, we could have an nvdimm hotplugged later */
3001 dev_dbg(dev, "failed to find NFIT at startup\n");
3002 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04003003 }
Dan Williams3c87f372017-04-03 13:52:14 -07003004
3005 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3006 if (rc)
3007 return rc;
Lv Zheng6b11d1d2016-12-14 15:04:39 +08003008 sz = tbl->length;
Dan Williamsb94d5232015-05-19 22:54:31 -04003009
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003010 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3011 if (!acpi_desc)
3012 return -ENOMEM;
3013 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04003014
Dan Williamse7a11b42016-07-14 16:19:55 -07003015 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05003016 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04003017
Vishal Verma20985162015-10-27 16:58:27 -06003018 /* Evaluate _FIT and override with that if present */
3019 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3020 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07003021 union acpi_object *obj = buf.pointer;
3022
3023 if (obj->type == ACPI_TYPE_BUFFER)
3024 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3025 obj->buffer.length);
3026 else
Linda Knippers6b577c92015-11-20 19:05:49 -05003027 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
3028 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07003029 kfree(buf.pointer);
3030 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07003031 /* skip over the lead-in header table */
3032 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3033 + sizeof(struct acpi_table_nfit),
3034 sz - sizeof(struct acpi_table_nfit));
Dan Williamsfbabd822017-04-18 09:56:31 -07003035
3036 if (rc)
3037 return rc;
3038 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
Dan Williamsb94d5232015-05-19 22:54:31 -04003039}
3040
3041static int acpi_nfit_remove(struct acpi_device *adev)
3042{
Dan Williamsfbabd822017-04-18 09:56:31 -07003043 /* see acpi_nfit_unregister */
Dan Williamsb94d5232015-05-19 22:54:31 -04003044 return 0;
3045}
3046
Toshi Kani56b47fe2017-06-08 12:36:57 -06003047static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
Vishal Verma20985162015-10-27 16:58:27 -06003048{
Dan Williamsc14a8682016-08-18 22:15:04 -07003049 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06003050 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07003051 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06003052 acpi_status status;
3053 int ret;
3054
Vishal Verma20985162015-10-27 16:58:27 -06003055 if (!dev->driver) {
3056 /* dev->driver may be null if we're being removed */
3057 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Dan Williamsc14a8682016-08-18 22:15:04 -07003058 return;
Vishal Verma20985162015-10-27 16:58:27 -06003059 }
3060
3061 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003062 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3063 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07003064 return;
3065 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003066 } else {
3067 /*
3068 * Finish previous registration before considering new
3069 * regions.
3070 */
3071 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06003072 }
3073
3074 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07003075 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06003076 if (ACPI_FAILURE(status)) {
3077 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07003078 return;
Vishal Verma20985162015-10-27 16:58:27 -06003079 }
3080
Linda Knippers6b577c92015-11-20 19:05:49 -05003081 obj = buf.pointer;
3082 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07003083 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3084 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07003085 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05003086 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07003087 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05003088 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06003089 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07003090}
Toshi Kani56b47fe2017-06-08 12:36:57 -06003091
3092static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3093{
3094 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3095
3096 acpi_nfit_ars_rescan(acpi_desc);
3097}
3098
3099void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3100{
3101 dev_dbg(dev, "%s: event: 0x%x\n", __func__, event);
3102
3103 switch (event) {
3104 case NFIT_NOTIFY_UPDATE:
3105 return acpi_nfit_update_notify(dev, handle);
3106 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3107 return acpi_nfit_uc_error_notify(dev, handle);
3108 default:
3109 return;
3110 }
3111}
Dan Williamsc14a8682016-08-18 22:15:04 -07003112EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06003113
Dan Williamsc14a8682016-08-18 22:15:04 -07003114static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3115{
3116 device_lock(&adev->dev);
3117 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3118 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06003119}
3120
Dan Williamsb94d5232015-05-19 22:54:31 -04003121static const struct acpi_device_id acpi_nfit_ids[] = {
3122 { "ACPI0012", 0 },
3123 { "", 0 },
3124};
3125MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3126
3127static struct acpi_driver acpi_nfit_driver = {
3128 .name = KBUILD_MODNAME,
3129 .ids = acpi_nfit_ids,
3130 .ops = {
3131 .add = acpi_nfit_add,
3132 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06003133 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04003134 },
3135};
3136
3137static __init int nfit_init(void)
3138{
3139 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3140 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3141 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3142 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3143 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3144 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3145 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3146
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03003147 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3148 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3149 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3150 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3151 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3152 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3153 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3154 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3155 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3156 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3157 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3158 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3159 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04003160
Dan Williams7ae0fa432016-02-19 12:16:34 -08003161 nfit_wq = create_singlethread_workqueue("nfit");
3162 if (!nfit_wq)
3163 return -ENOMEM;
3164
Vishal Verma6839a6d2016-07-23 21:51:21 -07003165 nfit_mce_register();
3166
Dan Williamsb94d5232015-05-19 22:54:31 -04003167 return acpi_bus_register_driver(&acpi_nfit_driver);
3168}
3169
3170static __exit void nfit_exit(void)
3171{
Vishal Verma6839a6d2016-07-23 21:51:21 -07003172 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04003173 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003174 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003175 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04003176}
3177
3178module_init(nfit_init);
3179module_exit(nfit_exit);
3180MODULE_LICENSE("GPL v2");
3181MODULE_AUTHOR("Intel Corporation");