blob: 03105648f9b16d41976bae7202d316312645b109 [file] [log] [blame]
Dan Williamsb94d5232015-05-19 22:54:31 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/list_sort.h>
14#include <linux/libnvdimm.h>
15#include <linux/module.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040016#include <linux/mutex.h>
Dan Williams62232e452015-06-08 14:27:06 -040017#include <linux/ndctl.h>
Vishal Verma37b137f2016-07-23 21:51:42 -070018#include <linux/sysfs.h>
Vishal Verma0caeef62015-12-24 19:21:43 -070019#include <linux/delay.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040020#include <linux/list.h>
21#include <linux/acpi.h>
Dan Williamseaf96152015-05-01 13:11:27 -040022#include <linux/sort.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040023#include <linux/io.h>
Dan Williams1cf03c02016-02-17 13:01:23 -080024#include <linux/nd.h>
Dan Williams96601ad2015-08-24 18:29:38 -040025#include <asm/cacheflush.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040026#include "nfit.h"
27
Ross Zwisler047fc8a2015-06-25 04:21:02 -040028/*
29 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
30 * irrelevant.
31 */
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020032#include <linux/io-64-nonatomic-hi-lo.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040033
Dan Williams4d88a972015-05-31 14:41:48 -040034static bool force_enable_dimms;
35module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
36MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
37
Dan Williams1cf03c02016-02-17 13:01:23 -080038static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
39module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
40MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
41
42/* after three payloads of overflow, it's dead jim */
43static unsigned int scrub_overflow_abort = 3;
44module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
45MODULE_PARM_DESC(scrub_overflow_abort,
46 "Number of times we overflow ARS results before abort");
47
Dan Williams87554092016-04-28 18:01:20 -070048static bool disable_vendor_specific;
49module_param(disable_vendor_specific, bool, S_IRUGO);
50MODULE_PARM_DESC(disable_vendor_specific,
Linda Knippersf2668fa2017-03-07 16:35:14 -050051 "Limit commands to the publicly specified set");
Dan Williams87554092016-04-28 18:01:20 -070052
Linda Knippers095ab4b2017-03-07 16:35:12 -050053static unsigned long override_dsm_mask;
54module_param(override_dsm_mask, ulong, S_IRUGO);
55MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
56
Linda Knippersba650cf2017-03-07 16:35:13 -050057static int default_dsm_family = -1;
58module_param(default_dsm_family, int, S_IRUGO);
59MODULE_PARM_DESC(default_dsm_family,
60 "Try this DSM type first when identifying NVDIMM family");
61
Vishal Verma6839a6d2016-07-23 21:51:21 -070062LIST_HEAD(acpi_descs);
63DEFINE_MUTEX(acpi_desc_lock);
64
Dan Williams7ae0fa432016-02-19 12:16:34 -080065static struct workqueue_struct *nfit_wq;
66
Vishal Verma20985162015-10-27 16:58:27 -060067struct nfit_table_prev {
68 struct list_head spas;
69 struct list_head memdevs;
70 struct list_head dcrs;
71 struct list_head bdws;
72 struct list_head idts;
73 struct list_head flushes;
74};
75
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +030076static guid_t nfit_uuid[NFIT_UUID_MAX];
Dan Williamsb94d5232015-05-19 22:54:31 -040077
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +030078const guid_t *to_nfit_uuid(enum nfit_uuids id)
Dan Williamsb94d5232015-05-19 22:54:31 -040079{
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +030080 return &nfit_uuid[id];
Dan Williamsb94d5232015-05-19 22:54:31 -040081}
Dan Williams6bc75612015-06-17 17:23:32 -040082EXPORT_SYMBOL(to_nfit_uuid);
Dan Williamsb94d5232015-05-19 22:54:31 -040083
Dan Williams62232e452015-06-08 14:27:06 -040084static struct acpi_nfit_desc *to_acpi_nfit_desc(
85 struct nvdimm_bus_descriptor *nd_desc)
86{
87 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
88}
89
90static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
91{
92 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
93
94 /*
95 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
96 * acpi_device.
97 */
98 if (!nd_desc->provider_name
99 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
100 return NULL;
101
102 return to_acpi_device(acpi_desc->dev);
103}
104
Dan Williamsd6eb2702016-12-06 15:06:55 -0800105static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
Dan Williamsaef25332016-02-12 17:01:11 -0800106{
Dan Williamsd4f32362016-03-03 16:08:54 -0800107 struct nd_cmd_clear_error *clear_err;
Dan Williamsaef25332016-02-12 17:01:11 -0800108 struct nd_cmd_ars_status *ars_status;
Dan Williamsaef25332016-02-12 17:01:11 -0800109 u16 flags;
110
111 switch (cmd) {
112 case ND_CMD_ARS_CAP:
Dan Williams11294d62016-09-21 09:21:26 -0700113 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800114 return -ENOTTY;
115
116 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700117 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800118 return -EIO;
119
120 /* No supported scan types for this range */
121 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
Dan Williams11294d62016-09-21 09:21:26 -0700122 if ((status >> 16 & flags) == 0)
Dan Williamsaef25332016-02-12 17:01:11 -0800123 return -ENOTTY;
Vishal Verma9a901f52016-12-05 17:00:37 -0700124 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800125 case ND_CMD_ARS_START:
Dan Williamsaef25332016-02-12 17:01:11 -0800126 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700127 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800128 return -EBUSY;
129
130 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700131 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800132 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700133 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800134 case ND_CMD_ARS_STATUS:
135 ars_status = buf;
136 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700137 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800138 return -EIO;
139 /* Check extended status (Upper two bytes) */
Dan Williams11294d62016-09-21 09:21:26 -0700140 if (status == NFIT_ARS_STATUS_DONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800141 return 0;
142
143 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700144 if (status == NFIT_ARS_STATUS_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800145 return -EBUSY;
146
147 /* No ARS performed for the current boot */
Dan Williams11294d62016-09-21 09:21:26 -0700148 if (status == NFIT_ARS_STATUS_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800149 return -EAGAIN;
150
151 /*
152 * ARS interrupted, either we overflowed or some other
153 * agent wants the scan to stop. If we didn't overflow
154 * then just continue with the returned results.
155 */
Dan Williams11294d62016-09-21 09:21:26 -0700156 if (status == NFIT_ARS_STATUS_INTR) {
Dan Williams82aa37c2016-12-06 12:45:24 -0800157 if (ars_status->out_length >= 40 && (ars_status->flags
158 & NFIT_ARS_F_OVERFLOW))
Dan Williamsaef25332016-02-12 17:01:11 -0800159 return -ENOSPC;
160 return 0;
161 }
162
163 /* Unknown status */
Dan Williams11294d62016-09-21 09:21:26 -0700164 if (status >> 16)
Dan Williamsaef25332016-02-12 17:01:11 -0800165 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700166 return 0;
Dan Williamsd4f32362016-03-03 16:08:54 -0800167 case ND_CMD_CLEAR_ERROR:
168 clear_err = buf;
Dan Williams11294d62016-09-21 09:21:26 -0700169 if (status & 0xffff)
Dan Williamsd4f32362016-03-03 16:08:54 -0800170 return -EIO;
171 if (!clear_err->cleared)
172 return -EIO;
173 if (clear_err->length > clear_err->cleared)
174 return clear_err->cleared;
Vishal Verma9a901f52016-12-05 17:00:37 -0700175 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800176 default:
177 break;
178 }
179
Dan Williams11294d62016-09-21 09:21:26 -0700180 /* all other non-zero status results in an error */
181 if (status)
182 return -EIO;
Dan Williamsaef25332016-02-12 17:01:11 -0800183 return 0;
184}
185
Dan Williams9d62ed92017-05-04 11:47:22 -0700186static int xlat_nvdimm_status(void *buf, unsigned int cmd, u32 status)
187{
188 switch (cmd) {
189 case ND_CMD_GET_CONFIG_SIZE:
190 if (status >> 16 & ND_CONFIG_LOCKED)
191 return -EACCES;
192 break;
193 default:
194 break;
195 }
196
197 /* all other non-zero status results in an error */
198 if (status)
199 return -EIO;
200 return 0;
201}
202
Dan Williamsd6eb2702016-12-06 15:06:55 -0800203static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
204 u32 status)
205{
206 if (!nvdimm)
207 return xlat_bus_status(buf, cmd, status);
Dan Williams9d62ed92017-05-04 11:47:22 -0700208 return xlat_nvdimm_status(buf, cmd, status);
Dan Williamsd6eb2702016-12-06 15:06:55 -0800209}
210
Dan Williamsa7de92d2016-12-05 13:43:25 -0800211int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
212 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400213{
Dan Williams62232e452015-06-08 14:27:06 -0400214 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams62232e452015-06-08 14:27:06 -0400215 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700216 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400217 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700218 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400219 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700220 unsigned long cmd_mask, dsm_mask;
Dan Williams11294d62016-09-21 09:21:26 -0700221 u32 offset, fw_status = 0;
Dan Williams62232e452015-06-08 14:27:06 -0400222 acpi_handle handle;
Dan Williams31eca762016-04-28 16:23:43 -0700223 unsigned int func;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300224 const guid_t *guid;
Dan Williams62232e452015-06-08 14:27:06 -0400225 int rc, i;
226
Dan Williams31eca762016-04-28 16:23:43 -0700227 func = cmd;
228 if (cmd == ND_CMD_CALL) {
229 call_pkg = buf;
230 func = call_pkg->nd_command;
231 }
232
Dan Williams62232e452015-06-08 14:27:06 -0400233 if (nvdimm) {
234 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
235 struct acpi_device *adev = nfit_mem->adev;
236
237 if (!adev)
238 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700239 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
240 return -ENOTTY;
241
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400242 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400243 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700244 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400245 dsm_mask = nfit_mem->dsm_mask;
246 desc = nd_cmd_dimm_desc(cmd);
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300247 guid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400248 handle = adev->handle;
249 } else {
250 struct acpi_device *adev = to_acpi_dev(acpi_desc);
251
252 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700253 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700254 dsm_mask = cmd_mask;
Jerry Hoemann7db5bb32017-06-30 20:53:24 -0700255 if (cmd == ND_CMD_CALL)
256 dsm_mask = nd_desc->bus_dsm_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400257 desc = nd_cmd_bus_desc(cmd);
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300258 guid = to_nfit_uuid(NFIT_DEV_BUS);
Dan Williams62232e452015-06-08 14:27:06 -0400259 handle = adev->handle;
260 dimm_name = "bus";
261 }
262
263 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
264 return -ENOTTY;
265
Dan Williams31eca762016-04-28 16:23:43 -0700266 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400267 return -ENOTTY;
268
269 in_obj.type = ACPI_TYPE_PACKAGE;
270 in_obj.package.count = 1;
271 in_obj.package.elements = &in_buf;
272 in_buf.type = ACPI_TYPE_BUFFER;
273 in_buf.buffer.pointer = buf;
274 in_buf.buffer.length = 0;
275
276 /* libnvdimm has already validated the input envelope */
277 for (i = 0; i < desc->in_num; i++)
278 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
279 i, buf);
280
Dan Williams31eca762016-04-28 16:23:43 -0700281 if (call_pkg) {
282 /* skip over package wrapper */
283 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
284 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400285 }
286
Dan Williams7699a6a2017-04-28 13:54:30 -0700287 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
288 __func__, dimm_name, cmd, func, in_buf.buffer.length);
289 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
Dan Williams31eca762016-04-28 16:23:43 -0700290 in_buf.buffer.pointer,
291 min_t(u32, 256, in_buf.buffer.length), true);
Dan Williams31eca762016-04-28 16:23:43 -0700292
Andy Shevchenko94116f82017-06-05 19:40:46 +0300293 out_obj = acpi_evaluate_dsm(handle, guid, 1, func, &in_obj);
Dan Williams62232e452015-06-08 14:27:06 -0400294 if (!out_obj) {
295 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
296 cmd_name);
297 return -EINVAL;
298 }
299
Dan Williams31eca762016-04-28 16:23:43 -0700300 if (call_pkg) {
301 call_pkg->nd_fw_size = out_obj->buffer.length;
302 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
303 out_obj->buffer.pointer,
304 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
305
306 ACPI_FREE(out_obj);
307 /*
308 * Need to support FW function w/o known size in advance.
309 * Caller can determine required size based upon nd_fw_size.
310 * If we return an error (like elsewhere) then caller wouldn't
311 * be able to rely upon data returned to make calculation.
312 */
313 return 0;
314 }
315
Dan Williams62232e452015-06-08 14:27:06 -0400316 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
317 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
318 __func__, dimm_name, cmd_name, out_obj->type);
319 rc = -EINVAL;
320 goto out;
321 }
322
Dan Williams7699a6a2017-04-28 13:54:30 -0700323 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, dimm_name,
324 cmd_name, out_obj->buffer.length);
325 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
326 out_obj->buffer.pointer,
327 min_t(u32, 128, out_obj->buffer.length), true);
Dan Williams62232e452015-06-08 14:27:06 -0400328
329 for (i = 0, offset = 0; i < desc->out_num; i++) {
330 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800331 (u32 *) out_obj->buffer.pointer,
332 out_obj->buffer.length - offset);
Dan Williams62232e452015-06-08 14:27:06 -0400333
334 if (offset + out_size > out_obj->buffer.length) {
335 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
336 __func__, dimm_name, cmd_name, i);
337 break;
338 }
339
340 if (in_buf.buffer.length + offset + out_size > buf_len) {
341 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
342 __func__, dimm_name, cmd_name, i);
343 rc = -ENXIO;
344 goto out;
345 }
346 memcpy(buf + in_buf.buffer.length + offset,
347 out_obj->buffer.pointer + offset, out_size);
348 offset += out_size;
349 }
Dan Williams11294d62016-09-21 09:21:26 -0700350
351 /*
352 * Set fw_status for all the commands with a known format to be
353 * later interpreted by xlat_status().
354 */
355 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
356 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
357 fw_status = *(u32 *) out_obj->buffer.pointer;
358
Dan Williams62232e452015-06-08 14:27:06 -0400359 if (offset + in_buf.buffer.length < buf_len) {
360 if (i >= 1) {
361 /*
362 * status valid, return the number of bytes left
363 * unfilled in the output buffer
364 */
365 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800366 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800367 *cmd_rc = xlat_status(nvdimm, buf, cmd,
368 fw_status);
Dan Williams62232e452015-06-08 14:27:06 -0400369 } else {
370 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
371 __func__, dimm_name, cmd_name, buf_len,
372 offset);
373 rc = -ENXIO;
374 }
Dan Williams2eea6582016-05-02 09:11:53 -0700375 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400376 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700377 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800378 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
Dan Williams2eea6582016-05-02 09:11:53 -0700379 }
Dan Williams62232e452015-06-08 14:27:06 -0400380
381 out:
382 ACPI_FREE(out_obj);
383
384 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400385}
Dan Williamsa7de92d2016-12-05 13:43:25 -0800386EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
Dan Williamsb94d5232015-05-19 22:54:31 -0400387
388static const char *spa_type_name(u16 type)
389{
390 static const char *to_name[] = {
391 [NFIT_SPA_VOLATILE] = "volatile",
392 [NFIT_SPA_PM] = "pmem",
393 [NFIT_SPA_DCR] = "dimm-control-region",
394 [NFIT_SPA_BDW] = "block-data-window",
395 [NFIT_SPA_VDISK] = "volatile-disk",
396 [NFIT_SPA_VCD] = "volatile-cd",
397 [NFIT_SPA_PDISK] = "persistent-disk",
398 [NFIT_SPA_PCD] = "persistent-cd",
399
400 };
401
402 if (type > NFIT_SPA_PCD)
403 return "unknown";
404
405 return to_name[type];
406}
407
Vishal Verma6839a6d2016-07-23 21:51:21 -0700408int nfit_spa_type(struct acpi_nfit_system_address *spa)
Dan Williamsb94d5232015-05-19 22:54:31 -0400409{
410 int i;
411
412 for (i = 0; i < NFIT_UUID_MAX; i++)
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +0300413 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
Dan Williamsb94d5232015-05-19 22:54:31 -0400414 return i;
415 return -1;
416}
417
418static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600419 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400420 struct acpi_nfit_system_address *spa)
421{
422 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600423 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400424
Dan Williams31932042016-07-14 17:22:48 -0700425 if (spa->header.length != sizeof(*spa))
426 return false;
427
Vishal Verma20985162015-10-27 16:58:27 -0600428 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700429 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600430 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
431 return true;
432 }
433 }
434
Dan Williams31932042016-07-14 17:22:48 -0700435 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
436 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400437 if (!nfit_spa)
438 return false;
439 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700440 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400441 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
442 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
443 spa->range_index,
444 spa_type_name(nfit_spa_type(spa)));
445 return true;
446}
447
448static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600449 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400450 struct acpi_nfit_memory_map *memdev)
451{
452 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600453 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400454
Dan Williams31932042016-07-14 17:22:48 -0700455 if (memdev->header.length != sizeof(*memdev))
456 return false;
457
Vishal Verma20985162015-10-27 16:58:27 -0600458 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700459 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600460 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
461 return true;
462 }
463
Dan Williams31932042016-07-14 17:22:48 -0700464 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
465 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400466 if (!nfit_memdev)
467 return false;
468 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700469 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400470 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
Dan Williamscaa603a2017-04-14 10:27:11 -0700471 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
Dan Williamsb94d5232015-05-19 22:54:31 -0400472 __func__, memdev->device_handle, memdev->range_index,
Dan Williamscaa603a2017-04-14 10:27:11 -0700473 memdev->region_index, memdev->flags);
Dan Williamsb94d5232015-05-19 22:54:31 -0400474 return true;
475}
476
Dan Williams31932042016-07-14 17:22:48 -0700477/*
478 * An implementation may provide a truncated control region if no block windows
479 * are defined.
480 */
481static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
482{
483 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
484 window_size))
485 return 0;
486 if (dcr->windows)
487 return sizeof(*dcr);
488 return offsetof(struct acpi_nfit_control_region, window_size);
489}
490
Dan Williamsb94d5232015-05-19 22:54:31 -0400491static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600492 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400493 struct acpi_nfit_control_region *dcr)
494{
495 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600496 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400497
Dan Williams31932042016-07-14 17:22:48 -0700498 if (!sizeof_dcr(dcr))
499 return false;
500
Vishal Verma20985162015-10-27 16:58:27 -0600501 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700502 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600503 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
504 return true;
505 }
506
Dan Williams31932042016-07-14 17:22:48 -0700507 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
508 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400509 if (!nfit_dcr)
510 return false;
511 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700512 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400513 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
514 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
515 dcr->region_index, dcr->windows);
516 return true;
517}
518
519static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600520 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400521 struct acpi_nfit_data_region *bdw)
522{
523 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600524 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400525
Dan Williams31932042016-07-14 17:22:48 -0700526 if (bdw->header.length != sizeof(*bdw))
527 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600528 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700529 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600530 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
531 return true;
532 }
533
Dan Williams31932042016-07-14 17:22:48 -0700534 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
535 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400536 if (!nfit_bdw)
537 return false;
538 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700539 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400540 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
541 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
542 bdw->region_index, bdw->windows);
543 return true;
544}
545
Dan Williams31932042016-07-14 17:22:48 -0700546static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
547{
548 if (idt->header.length < sizeof(*idt))
549 return 0;
550 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
551}
552
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400553static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600554 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400555 struct acpi_nfit_interleave *idt)
556{
557 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600558 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400559
Dan Williams31932042016-07-14 17:22:48 -0700560 if (!sizeof_idt(idt))
561 return false;
562
563 list_for_each_entry(nfit_idt, &prev->idts, list) {
564 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
565 continue;
566
567 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600568 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
569 return true;
570 }
Dan Williams31932042016-07-14 17:22:48 -0700571 }
Vishal Verma20985162015-10-27 16:58:27 -0600572
Dan Williams31932042016-07-14 17:22:48 -0700573 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
574 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400575 if (!nfit_idt)
576 return false;
577 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700578 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400579 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
580 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
581 idt->interleave_index, idt->line_count);
582 return true;
583}
584
Dan Williams31932042016-07-14 17:22:48 -0700585static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
586{
587 if (flush->header.length < sizeof(*flush))
588 return 0;
589 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
590}
591
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600592static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600593 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600594 struct acpi_nfit_flush_address *flush)
595{
596 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600597 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600598
Dan Williams31932042016-07-14 17:22:48 -0700599 if (!sizeof_flush(flush))
600 return false;
601
602 list_for_each_entry(nfit_flush, &prev->flushes, list) {
603 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
604 continue;
605
606 if (memcmp(nfit_flush->flush, flush,
607 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600608 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
609 return true;
610 }
Dan Williams31932042016-07-14 17:22:48 -0700611 }
Vishal Verma20985162015-10-27 16:58:27 -0600612
Dan Williams31932042016-07-14 17:22:48 -0700613 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
614 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600615 if (!nfit_flush)
616 return false;
617 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700618 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600619 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
620 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
621 flush->device_handle, flush->hint_count);
622 return true;
623}
624
Vishal Verma20985162015-10-27 16:58:27 -0600625static void *add_table(struct acpi_nfit_desc *acpi_desc,
626 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400627{
628 struct device *dev = acpi_desc->dev;
629 struct acpi_nfit_header *hdr;
630 void *err = ERR_PTR(-ENOMEM);
631
632 if (table >= end)
633 return NULL;
634
635 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600636 if (!hdr->length) {
637 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
638 hdr->type);
639 return NULL;
640 }
641
Dan Williamsb94d5232015-05-19 22:54:31 -0400642 switch (hdr->type) {
643 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600644 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400645 return err;
646 break;
647 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600648 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400649 return err;
650 break;
651 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600652 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400653 return err;
654 break;
655 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600656 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400657 return err;
658 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400659 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600660 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400661 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400662 break;
663 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600664 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600665 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400666 break;
667 case ACPI_NFIT_TYPE_SMBIOS:
668 dev_dbg(dev, "%s: smbios\n", __func__);
669 break;
670 default:
671 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
672 break;
673 }
674
675 return table + hdr->length;
676}
677
678static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
679 struct nfit_mem *nfit_mem)
680{
681 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
682 u16 dcr = nfit_mem->dcr->region_index;
683 struct nfit_spa *nfit_spa;
684
685 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
686 u16 range_index = nfit_spa->spa->range_index;
687 int type = nfit_spa_type(nfit_spa->spa);
688 struct nfit_memdev *nfit_memdev;
689
690 if (type != NFIT_SPA_BDW)
691 continue;
692
693 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
694 if (nfit_memdev->memdev->range_index != range_index)
695 continue;
696 if (nfit_memdev->memdev->device_handle != device_handle)
697 continue;
698 if (nfit_memdev->memdev->region_index != dcr)
699 continue;
700
701 nfit_mem->spa_bdw = nfit_spa->spa;
702 return;
703 }
704 }
705
706 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
707 nfit_mem->spa_dcr->range_index);
708 nfit_mem->bdw = NULL;
709}
710
Dan Williams6697b2c2016-02-04 16:51:00 -0800711static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400712 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
713{
714 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400715 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400716 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400717 struct nfit_idt *nfit_idt;
718 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400719
Dan Williamsb94d5232015-05-19 22:54:31 -0400720 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
721 if (nfit_bdw->bdw->region_index != dcr)
722 continue;
723 nfit_mem->bdw = nfit_bdw->bdw;
724 break;
725 }
726
727 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800728 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400729
730 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400731
732 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800733 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400734
735 range_index = nfit_mem->spa_bdw->range_index;
736 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
737 if (nfit_memdev->memdev->range_index != range_index ||
738 nfit_memdev->memdev->region_index != dcr)
739 continue;
740 nfit_mem->memdev_bdw = nfit_memdev->memdev;
741 idt_idx = nfit_memdev->memdev->interleave_index;
742 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
743 if (nfit_idt->idt->interleave_index != idt_idx)
744 continue;
745 nfit_mem->idt_bdw = nfit_idt->idt;
746 break;
747 }
748 break;
749 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400750}
751
Dan Williams14999342017-04-13 19:46:36 -0700752static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400753 struct acpi_nfit_system_address *spa)
754{
755 struct nfit_mem *nfit_mem, *found;
756 struct nfit_memdev *nfit_memdev;
Dan Williams14999342017-04-13 19:46:36 -0700757 int type = spa ? nfit_spa_type(spa) : 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400758
759 switch (type) {
760 case NFIT_SPA_DCR:
761 case NFIT_SPA_PM:
762 break;
763 default:
Dan Williams14999342017-04-13 19:46:36 -0700764 if (spa)
765 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400766 }
767
Dan Williams14999342017-04-13 19:46:36 -0700768 /*
769 * This loop runs in two modes, when a dimm is mapped the loop
770 * adds memdev associations to an existing dimm, or creates a
771 * dimm. In the unmapped dimm case this loop sweeps for memdev
772 * instances with an invalid / zero range_index and adds those
773 * dimms without spa associations.
774 */
Dan Williamsb94d5232015-05-19 22:54:31 -0400775 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700776 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800777 struct nfit_dcr *nfit_dcr;
778 u32 device_handle;
779 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400780
Dan Williams14999342017-04-13 19:46:36 -0700781 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
782 continue;
783 if (!spa && nfit_memdev->memdev->range_index)
Dan Williamsb94d5232015-05-19 22:54:31 -0400784 continue;
785 found = NULL;
786 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800787 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400788 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800789 if (__to_nfit_memdev(nfit_mem)->device_handle
790 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400791 found = nfit_mem;
792 break;
793 }
794
795 if (found)
796 nfit_mem = found;
797 else {
798 nfit_mem = devm_kzalloc(acpi_desc->dev,
799 sizeof(*nfit_mem), GFP_KERNEL);
800 if (!nfit_mem)
801 return -ENOMEM;
802 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700803 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800804 list_add(&nfit_mem->list, &acpi_desc->dimms);
805 }
806
807 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
808 if (nfit_dcr->dcr->region_index != dcr)
809 continue;
810 /*
811 * Record the control region for the dimm. For
812 * the ACPI 6.1 case, where there are separate
813 * control regions for the pmem vs blk
814 * interfaces, be sure to record the extended
815 * blk details.
816 */
817 if (!nfit_mem->dcr)
818 nfit_mem->dcr = nfit_dcr->dcr;
819 else if (nfit_mem->dcr->windows == 0
820 && nfit_dcr->dcr->windows)
821 nfit_mem->dcr = nfit_dcr->dcr;
822 break;
823 }
824
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700825 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700826 struct acpi_nfit_flush_address *flush;
827 u16 i;
828
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700829 if (nfit_flush->flush->device_handle != device_handle)
830 continue;
831 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700832 flush = nfit_flush->flush;
833 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
834 flush->hint_count
835 * sizeof(struct resource), GFP_KERNEL);
836 if (!nfit_mem->flush_wpq)
837 return -ENOMEM;
838 for (i = 0; i < flush->hint_count; i++) {
839 struct resource *res = &nfit_mem->flush_wpq[i];
840
841 res->start = flush->hint_address[i];
842 res->end = res->start + 8 - 1;
843 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700844 break;
845 }
846
Dan Williams6697b2c2016-02-04 16:51:00 -0800847 if (dcr && !nfit_mem->dcr) {
848 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
849 spa->range_index, dcr);
850 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400851 }
852
853 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400854 struct nfit_idt *nfit_idt;
855 u16 idt_idx;
856
Dan Williamsb94d5232015-05-19 22:54:31 -0400857 /* multiple dimms may share a SPA when interleaved */
858 nfit_mem->spa_dcr = spa;
859 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400860 idt_idx = nfit_memdev->memdev->interleave_index;
861 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
862 if (nfit_idt->idt->interleave_index != idt_idx)
863 continue;
864 nfit_mem->idt_dcr = nfit_idt->idt;
865 break;
866 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800867 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williams14999342017-04-13 19:46:36 -0700868 } else if (type == NFIT_SPA_PM) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400869 /*
870 * A single dimm may belong to multiple SPA-PM
871 * ranges, record at least one in addition to
872 * any SPA-DCR range.
873 */
874 nfit_mem->memdev_pmem = nfit_memdev->memdev;
Dan Williams14999342017-04-13 19:46:36 -0700875 } else
876 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400877 }
878
879 return 0;
880}
881
882static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
883{
884 struct nfit_mem *a = container_of(_a, typeof(*a), list);
885 struct nfit_mem *b = container_of(_b, typeof(*b), list);
886 u32 handleA, handleB;
887
888 handleA = __to_nfit_memdev(a)->device_handle;
889 handleB = __to_nfit_memdev(b)->device_handle;
890 if (handleA < handleB)
891 return -1;
892 else if (handleA > handleB)
893 return 1;
894 return 0;
895}
896
897static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
898{
899 struct nfit_spa *nfit_spa;
Dan Williams14999342017-04-13 19:46:36 -0700900 int rc;
901
Dan Williamsb94d5232015-05-19 22:54:31 -0400902
903 /*
904 * For each SPA-DCR or SPA-PMEM address range find its
905 * corresponding MEMDEV(s). From each MEMDEV find the
906 * corresponding DCR. Then, if we're operating on a SPA-DCR,
907 * try to find a SPA-BDW and a corresponding BDW that references
908 * the DCR. Throw it all into an nfit_mem object. Note, that
909 * BDWs are optional.
910 */
911 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
Dan Williams14999342017-04-13 19:46:36 -0700912 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400913 if (rc)
914 return rc;
915 }
916
Dan Williams14999342017-04-13 19:46:36 -0700917 /*
918 * If a DIMM has failed to be mapped into SPA there will be no
919 * SPA entries above. Find and register all the unmapped DIMMs
920 * for reporting and recovery purposes.
921 */
922 rc = __nfit_mem_init(acpi_desc, NULL);
923 if (rc)
924 return rc;
925
Dan Williamsb94d5232015-05-19 22:54:31 -0400926 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
927
928 return 0;
929}
930
Jerry Hoemann41f95db2017-06-30 20:41:28 -0700931static ssize_t bus_dsm_mask_show(struct device *dev,
932 struct device_attribute *attr, char *buf)
933{
934 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
935 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
936
937 return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
938}
939static struct device_attribute dev_attr_bus_dsm_mask =
940 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
941
Dan Williams45def222015-04-26 19:26:48 -0400942static ssize_t revision_show(struct device *dev,
943 struct device_attribute *attr, char *buf)
944{
945 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
946 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
947 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
948
Linda Knippers6b577c92015-11-20 19:05:49 -0500949 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400950}
951static DEVICE_ATTR_RO(revision);
952
Vishal Verma9ffd6352016-09-30 17:19:29 -0600953static ssize_t hw_error_scrub_show(struct device *dev,
954 struct device_attribute *attr, char *buf)
955{
956 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
957 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
958 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
959
960 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
961}
962
963/*
964 * The 'hw_error_scrub' attribute can have the following values written to it:
965 * '0': Switch to the default mode where an exception will only insert
966 * the address of the memory error into the poison and badblocks lists.
967 * '1': Enable a full scrub to happen if an exception for a memory error is
968 * received.
969 */
970static ssize_t hw_error_scrub_store(struct device *dev,
971 struct device_attribute *attr, const char *buf, size_t size)
972{
973 struct nvdimm_bus_descriptor *nd_desc;
974 ssize_t rc;
975 long val;
976
977 rc = kstrtol(buf, 0, &val);
978 if (rc)
979 return rc;
980
981 device_lock(dev);
982 nd_desc = dev_get_drvdata(dev);
983 if (nd_desc) {
984 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
985
986 switch (val) {
987 case HW_ERROR_SCRUB_ON:
988 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
989 break;
990 case HW_ERROR_SCRUB_OFF:
991 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
992 break;
993 default:
994 rc = -EINVAL;
995 break;
996 }
997 }
998 device_unlock(dev);
999 if (rc)
1000 return rc;
1001 return size;
1002}
1003static DEVICE_ATTR_RW(hw_error_scrub);
1004
Vishal Verma37b137f2016-07-23 21:51:42 -07001005/*
1006 * This shows the number of full Address Range Scrubs that have been
1007 * completed since driver load time. Userspace can wait on this using
1008 * select/poll etc. A '+' at the end indicates an ARS is in progress
1009 */
1010static ssize_t scrub_show(struct device *dev,
1011 struct device_attribute *attr, char *buf)
1012{
1013 struct nvdimm_bus_descriptor *nd_desc;
1014 ssize_t rc = -ENXIO;
1015
1016 device_lock(dev);
1017 nd_desc = dev_get_drvdata(dev);
1018 if (nd_desc) {
1019 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1020
1021 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1022 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
1023 }
1024 device_unlock(dev);
1025 return rc;
1026}
1027
Vishal Verma37b137f2016-07-23 21:51:42 -07001028static ssize_t scrub_store(struct device *dev,
1029 struct device_attribute *attr, const char *buf, size_t size)
1030{
1031 struct nvdimm_bus_descriptor *nd_desc;
1032 ssize_t rc;
1033 long val;
1034
1035 rc = kstrtol(buf, 0, &val);
1036 if (rc)
1037 return rc;
1038 if (val != 1)
1039 return -EINVAL;
1040
1041 device_lock(dev);
1042 nd_desc = dev_get_drvdata(dev);
1043 if (nd_desc) {
1044 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1045
Toshi Kani80790032017-06-29 20:41:30 -06001046 rc = acpi_nfit_ars_rescan(acpi_desc, 0);
Vishal Verma37b137f2016-07-23 21:51:42 -07001047 }
1048 device_unlock(dev);
1049 if (rc)
1050 return rc;
1051 return size;
1052}
1053static DEVICE_ATTR_RW(scrub);
1054
1055static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1056{
1057 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1058 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1059 | 1 << ND_CMD_ARS_STATUS;
1060
1061 return (nd_desc->cmd_mask & mask) == mask;
1062}
1063
1064static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1065{
1066 struct device *dev = container_of(kobj, struct device, kobj);
1067 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1068
1069 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1070 return 0;
1071 return a->mode;
1072}
1073
Dan Williams45def222015-04-26 19:26:48 -04001074static struct attribute *acpi_nfit_attributes[] = {
1075 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -07001076 &dev_attr_scrub.attr,
Vishal Verma9ffd6352016-09-30 17:19:29 -06001077 &dev_attr_hw_error_scrub.attr,
Jerry Hoemann41f95db2017-06-30 20:41:28 -07001078 &dev_attr_bus_dsm_mask.attr,
Dan Williams45def222015-04-26 19:26:48 -04001079 NULL,
1080};
1081
Arvind Yadav5e937462017-06-22 15:44:41 +05301082static const struct attribute_group acpi_nfit_attribute_group = {
Dan Williams45def222015-04-26 19:26:48 -04001083 .name = "nfit",
1084 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -07001085 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -04001086};
1087
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001088static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -04001089 &nvdimm_bus_attribute_group,
1090 &acpi_nfit_attribute_group,
1091 NULL,
1092};
1093
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001094static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1095{
1096 struct nvdimm *nvdimm = to_nvdimm(dev);
1097 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1098
1099 return __to_nfit_memdev(nfit_mem);
1100}
1101
1102static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1103{
1104 struct nvdimm *nvdimm = to_nvdimm(dev);
1105 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1106
1107 return nfit_mem->dcr;
1108}
1109
1110static ssize_t handle_show(struct device *dev,
1111 struct device_attribute *attr, char *buf)
1112{
1113 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1114
1115 return sprintf(buf, "%#x\n", memdev->device_handle);
1116}
1117static DEVICE_ATTR_RO(handle);
1118
1119static ssize_t phys_id_show(struct device *dev,
1120 struct device_attribute *attr, char *buf)
1121{
1122 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1123
1124 return sprintf(buf, "%#x\n", memdev->physical_id);
1125}
1126static DEVICE_ATTR_RO(phys_id);
1127
1128static ssize_t vendor_show(struct device *dev,
1129 struct device_attribute *attr, char *buf)
1130{
1131 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1132
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001133 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001134}
1135static DEVICE_ATTR_RO(vendor);
1136
1137static ssize_t rev_id_show(struct device *dev,
1138 struct device_attribute *attr, char *buf)
1139{
1140 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1141
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001142 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001143}
1144static DEVICE_ATTR_RO(rev_id);
1145
1146static ssize_t device_show(struct device *dev,
1147 struct device_attribute *attr, char *buf)
1148{
1149 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1150
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001151 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001152}
1153static DEVICE_ATTR_RO(device);
1154
Dan Williams6ca72082016-04-29 10:33:23 -07001155static ssize_t subsystem_vendor_show(struct device *dev,
1156 struct device_attribute *attr, char *buf)
1157{
1158 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1159
1160 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1161}
1162static DEVICE_ATTR_RO(subsystem_vendor);
1163
1164static ssize_t subsystem_rev_id_show(struct device *dev,
1165 struct device_attribute *attr, char *buf)
1166{
1167 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1168
1169 return sprintf(buf, "0x%04x\n",
1170 be16_to_cpu(dcr->subsystem_revision_id));
1171}
1172static DEVICE_ATTR_RO(subsystem_rev_id);
1173
1174static ssize_t subsystem_device_show(struct device *dev,
1175 struct device_attribute *attr, char *buf)
1176{
1177 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1178
1179 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1180}
1181static DEVICE_ATTR_RO(subsystem_device);
1182
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001183static int num_nvdimm_formats(struct nvdimm *nvdimm)
1184{
1185 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1186 int formats = 0;
1187
1188 if (nfit_mem->memdev_pmem)
1189 formats++;
1190 if (nfit_mem->memdev_bdw)
1191 formats++;
1192 return formats;
1193}
1194
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001195static ssize_t format_show(struct device *dev,
1196 struct device_attribute *attr, char *buf)
1197{
1198 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1199
Dan Williams1bcbf422016-06-29 11:19:32 -07001200 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001201}
1202static DEVICE_ATTR_RO(format);
1203
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001204static ssize_t format1_show(struct device *dev,
1205 struct device_attribute *attr, char *buf)
1206{
1207 u32 handle;
1208 ssize_t rc = -ENXIO;
1209 struct nfit_mem *nfit_mem;
1210 struct nfit_memdev *nfit_memdev;
1211 struct acpi_nfit_desc *acpi_desc;
1212 struct nvdimm *nvdimm = to_nvdimm(dev);
1213 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1214
1215 nfit_mem = nvdimm_provider_data(nvdimm);
1216 acpi_desc = nfit_mem->acpi_desc;
1217 handle = to_nfit_memdev(dev)->device_handle;
1218
1219 /* assumes DIMMs have at most 2 published interface codes */
1220 mutex_lock(&acpi_desc->init_mutex);
1221 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1222 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1223 struct nfit_dcr *nfit_dcr;
1224
1225 if (memdev->device_handle != handle)
1226 continue;
1227
1228 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1229 if (nfit_dcr->dcr->region_index != memdev->region_index)
1230 continue;
1231 if (nfit_dcr->dcr->code == dcr->code)
1232 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001233 rc = sprintf(buf, "0x%04x\n",
1234 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001235 break;
1236 }
1237 if (rc != ENXIO)
1238 break;
1239 }
1240 mutex_unlock(&acpi_desc->init_mutex);
1241 return rc;
1242}
1243static DEVICE_ATTR_RO(format1);
1244
1245static ssize_t formats_show(struct device *dev,
1246 struct device_attribute *attr, char *buf)
1247{
1248 struct nvdimm *nvdimm = to_nvdimm(dev);
1249
1250 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1251}
1252static DEVICE_ATTR_RO(formats);
1253
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001254static ssize_t serial_show(struct device *dev,
1255 struct device_attribute *attr, char *buf)
1256{
1257 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1258
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001259 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001260}
1261static DEVICE_ATTR_RO(serial);
1262
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001263static ssize_t family_show(struct device *dev,
1264 struct device_attribute *attr, char *buf)
1265{
1266 struct nvdimm *nvdimm = to_nvdimm(dev);
1267 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1268
1269 if (nfit_mem->family < 0)
1270 return -ENXIO;
1271 return sprintf(buf, "%d\n", nfit_mem->family);
1272}
1273static DEVICE_ATTR_RO(family);
1274
1275static ssize_t dsm_mask_show(struct device *dev,
1276 struct device_attribute *attr, char *buf)
1277{
1278 struct nvdimm *nvdimm = to_nvdimm(dev);
1279 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1280
1281 if (nfit_mem->family < 0)
1282 return -ENXIO;
1283 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1284}
1285static DEVICE_ATTR_RO(dsm_mask);
1286
Dan Williams58138822015-06-23 20:08:34 -04001287static ssize_t flags_show(struct device *dev,
1288 struct device_attribute *attr, char *buf)
1289{
1290 u16 flags = to_nfit_memdev(dev)->flags;
1291
Dan Williamsffab9382017-04-13 15:05:30 -07001292 return sprintf(buf, "%s%s%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001293 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1294 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1295 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001296 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Dan Williamsffab9382017-04-13 15:05:30 -07001297 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1298 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1299 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
Dan Williams58138822015-06-23 20:08:34 -04001300}
1301static DEVICE_ATTR_RO(flags);
1302
Toshi Kani38a879b2016-04-25 15:34:59 -06001303static ssize_t id_show(struct device *dev,
1304 struct device_attribute *attr, char *buf)
1305{
1306 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1307
1308 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1309 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1310 be16_to_cpu(dcr->vendor_id),
1311 dcr->manufacturing_location,
1312 be16_to_cpu(dcr->manufacturing_date),
1313 be32_to_cpu(dcr->serial_number));
1314 else
1315 return sprintf(buf, "%04x-%08x\n",
1316 be16_to_cpu(dcr->vendor_id),
1317 be32_to_cpu(dcr->serial_number));
1318}
1319static DEVICE_ATTR_RO(id);
1320
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001321static struct attribute *acpi_nfit_dimm_attributes[] = {
1322 &dev_attr_handle.attr,
1323 &dev_attr_phys_id.attr,
1324 &dev_attr_vendor.attr,
1325 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001326 &dev_attr_rev_id.attr,
1327 &dev_attr_subsystem_vendor.attr,
1328 &dev_attr_subsystem_device.attr,
1329 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001330 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001331 &dev_attr_formats.attr,
1332 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001333 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001334 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001335 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001336 &dev_attr_family.attr,
1337 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001338 NULL,
1339};
1340
1341static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1342 struct attribute *a, int n)
1343{
1344 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001345 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001346
Dan Williams14999342017-04-13 19:46:36 -07001347 if (!to_nfit_dcr(dev)) {
1348 /* Without a dcr only the memdev attributes can be surfaced */
1349 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1350 || a == &dev_attr_flags.attr
1351 || a == &dev_attr_family.attr
1352 || a == &dev_attr_dsm_mask.attr)
1353 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001354 return 0;
Dan Williams14999342017-04-13 19:46:36 -07001355 }
1356
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001357 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1358 return 0;
1359 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001360}
1361
Arvind Yadav5e937462017-06-22 15:44:41 +05301362static const struct attribute_group acpi_nfit_dimm_attribute_group = {
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001363 .name = "nfit",
1364 .attrs = acpi_nfit_dimm_attributes,
1365 .is_visible = acpi_nfit_dimm_attr_visible,
1366};
1367
1368static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001369 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001370 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001371 &acpi_nfit_dimm_attribute_group,
1372 NULL,
1373};
1374
1375static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1376 u32 device_handle)
1377{
1378 struct nfit_mem *nfit_mem;
1379
1380 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1381 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1382 return nfit_mem->nvdimm;
1383
1384 return NULL;
1385}
1386
Dan Williams231bf112016-08-22 19:23:25 -07001387void __acpi_nvdimm_notify(struct device *dev, u32 event)
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001388{
1389 struct nfit_mem *nfit_mem;
1390 struct acpi_nfit_desc *acpi_desc;
1391
1392 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1393 event);
1394
1395 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1396 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1397 event);
1398 return;
1399 }
1400
1401 acpi_desc = dev_get_drvdata(dev->parent);
1402 if (!acpi_desc)
1403 return;
1404
1405 /*
1406 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1407 * is still valid.
1408 */
1409 nfit_mem = dev_get_drvdata(dev);
1410 if (nfit_mem && nfit_mem->flags_attr)
1411 sysfs_notify_dirent(nfit_mem->flags_attr);
1412}
Dan Williams231bf112016-08-22 19:23:25 -07001413EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001414
1415static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1416{
1417 struct acpi_device *adev = data;
1418 struct device *dev = &adev->dev;
1419
1420 device_lock(dev->parent);
1421 __acpi_nvdimm_notify(dev, event);
1422 device_unlock(dev->parent);
1423}
1424
Dan Williams62232e452015-06-08 14:27:06 -04001425static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1426 struct nfit_mem *nfit_mem, u32 device_handle)
1427{
1428 struct acpi_device *adev, *adev_dimm;
1429 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001430 unsigned long dsm_mask;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001431 const guid_t *guid;
Linda Knippers60e95f432015-07-22 16:17:22 -04001432 int i;
Linda Knippersba650cf2017-03-07 16:35:13 -05001433 int family = -1;
Dan Williams62232e452015-06-08 14:27:06 -04001434
Dan Williamse3654ec2016-04-28 16:17:07 -07001435 /* nfit test assumes 1:1 relationship between commands and dsms */
1436 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001437 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001438 adev = to_acpi_dev(acpi_desc);
1439 if (!adev)
1440 return 0;
1441
1442 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1443 nfit_mem->adev = adev_dimm;
1444 if (!adev_dimm) {
1445 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1446 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001447 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001448 }
1449
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001450 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1451 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1452 dev_err(dev, "%s: notification registration failed\n",
1453 dev_name(&adev_dimm->dev));
1454 return -ENXIO;
1455 }
1456
Dan Williams31eca762016-04-28 16:23:43 -07001457 /*
stuart hayese02fb722016-05-26 11:38:41 -05001458 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001459 * different command sets. Note, that checking for function0 (bit0)
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001460 * tells us if any commands are reachable through this GUID.
Dan Williams31eca762016-04-28 16:23:43 -07001461 */
stuart hayese02fb722016-05-26 11:38:41 -05001462 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001463 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Linda Knippersba650cf2017-03-07 16:35:13 -05001464 if (family < 0 || i == default_dsm_family)
1465 family = i;
Dan Williams31eca762016-04-28 16:23:43 -07001466
1467 /* limit the supported commands to those that are publicly documented */
Linda Knippersba650cf2017-03-07 16:35:13 -05001468 nfit_mem->family = family;
Linda Knippers095ab4b2017-03-07 16:35:12 -05001469 if (override_dsm_mask && !disable_vendor_specific)
1470 dsm_mask = override_dsm_mask;
1471 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001472 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001473 if (disable_vendor_specific)
1474 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001475 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001476 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001477 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001478 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001479 if (disable_vendor_specific)
1480 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001481 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1482 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001483 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001484 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001485 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001486 /* DSMs are optional, continue loading the driver... */
1487 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001488 }
1489
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001490 guid = to_nfit_uuid(nfit_mem->family);
Dan Williams31eca762016-04-28 16:23:43 -07001491 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Andy Shevchenko94116f82017-06-05 19:40:46 +03001492 if (acpi_check_dsm(adev_dimm->handle, guid, 1, 1ULL << i))
Dan Williams62232e452015-06-08 14:27:06 -04001493 set_bit(i, &nfit_mem->dsm_mask);
1494
Linda Knippers60e95f432015-07-22 16:17:22 -04001495 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001496}
1497
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001498static void shutdown_dimm_notify(void *data)
1499{
1500 struct acpi_nfit_desc *acpi_desc = data;
1501 struct nfit_mem *nfit_mem;
1502
1503 mutex_lock(&acpi_desc->init_mutex);
1504 /*
1505 * Clear out the nfit_mem->flags_attr and shut down dimm event
1506 * notifications.
1507 */
1508 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williams231bf112016-08-22 19:23:25 -07001509 struct acpi_device *adev_dimm = nfit_mem->adev;
1510
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001511 if (nfit_mem->flags_attr) {
1512 sysfs_put(nfit_mem->flags_attr);
1513 nfit_mem->flags_attr = NULL;
1514 }
Dan Williams231bf112016-08-22 19:23:25 -07001515 if (adev_dimm)
1516 acpi_remove_notify_handler(adev_dimm->handle,
1517 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001518 }
1519 mutex_unlock(&acpi_desc->init_mutex);
1520}
1521
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001522static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1523{
1524 struct nfit_mem *nfit_mem;
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001525 int dimm_count = 0, rc;
1526 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001527
1528 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001529 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001530 unsigned long flags = 0, cmd_mask;
Dan Williamscaa603a2017-04-14 10:27:11 -07001531 struct nfit_memdev *nfit_memdev;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001532 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001533 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001534
1535 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1536 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1537 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001538 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001539 continue;
1540 }
1541
1542 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
Dan Williams8f078b32017-05-04 14:01:24 -07001543 set_bit(NDD_ALIASING, &flags);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001544
Dan Williamscaa603a2017-04-14 10:27:11 -07001545 /* collate flags across all memdevs for this dimm */
1546 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1547 struct acpi_nfit_memory_map *dimm_memdev;
1548
1549 dimm_memdev = __to_nfit_memdev(nfit_mem);
1550 if (dimm_memdev->device_handle
1551 != nfit_memdev->memdev->device_handle)
1552 continue;
1553 dimm_memdev->flags |= nfit_memdev->memdev->flags;
1554 }
1555
Dan Williams58138822015-06-23 20:08:34 -04001556 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001557 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams8f078b32017-05-04 14:01:24 -07001558 set_bit(NDD_UNARMED, &flags);
Dan Williams58138822015-06-23 20:08:34 -04001559
Dan Williams62232e452015-06-08 14:27:06 -04001560 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1561 if (rc)
1562 continue;
1563
Dan Williamse3654ec2016-04-28 16:17:07 -07001564 /*
Dan Williams31eca762016-04-28 16:23:43 -07001565 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1566 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1567 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001568 */
Dan Williams31eca762016-04-28 16:23:43 -07001569 cmd_mask = 1UL << ND_CMD_CALL;
1570 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1571 cmd_mask |= nfit_mem->dsm_mask;
1572
Dan Williamse5ae3b22016-06-07 17:00:04 -07001573 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1574 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001575 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001576 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001577 flags, cmd_mask, flush ? flush->hint_count : 0,
1578 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001579 if (!nvdimm)
1580 return -ENOMEM;
1581
1582 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001583 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001584
1585 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1586 continue;
1587
Dan Williams14999342017-04-13 19:46:36 -07001588 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001589 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001590 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1591 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1592 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Dan Williams14999342017-04-13 19:46:36 -07001593 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1594 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
Dan Williams58138822015-06-23 20:08:34 -04001595
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001596 }
1597
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001598 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1599 if (rc)
1600 return rc;
1601
1602 /*
1603 * Now that dimms are successfully registered, and async registration
1604 * is flushed, attempt to enable event notification.
1605 */
1606 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1607 struct kernfs_node *nfit_kernfs;
1608
1609 nvdimm = nfit_mem->nvdimm;
1610 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1611 if (nfit_kernfs)
1612 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1613 "flags");
1614 sysfs_put(nfit_kernfs);
1615 if (!nfit_mem->flags_attr)
1616 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1617 nvdimm_name(nvdimm));
1618 }
1619
1620 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1621 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001622}
1623
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001624/*
1625 * These constants are private because there are no kernel consumers of
1626 * these commands.
1627 */
1628enum nfit_aux_cmds {
1629 NFIT_CMD_TRANSLATE_SPA = 5,
1630 NFIT_CMD_ARS_INJECT_SET = 7,
1631 NFIT_CMD_ARS_INJECT_CLEAR = 8,
1632 NFIT_CMD_ARS_INJECT_GET = 9,
1633};
1634
Dan Williams62232e452015-06-08 14:27:06 -04001635static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1636{
1637 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03001638 const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
Dan Williams62232e452015-06-08 14:27:06 -04001639 struct acpi_device *adev;
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001640 unsigned long dsm_mask;
Dan Williams62232e452015-06-08 14:27:06 -04001641 int i;
1642
Dan Williamse3654ec2016-04-28 16:17:07 -07001643 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001644 adev = to_acpi_dev(acpi_desc);
1645 if (!adev)
1646 return;
1647
Dan Williamsd4f32362016-03-03 16:08:54 -08001648 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Andy Shevchenko94116f82017-06-05 19:40:46 +03001649 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001650 set_bit(i, &nd_desc->cmd_mask);
Jerry Hoemann37d74842017-06-30 20:41:24 -07001651 set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
Jerry Hoemann7db5bb32017-06-30 20:53:24 -07001652
1653 dsm_mask =
1654 (1 << ND_CMD_ARS_CAP) |
1655 (1 << ND_CMD_ARS_START) |
1656 (1 << ND_CMD_ARS_STATUS) |
1657 (1 << ND_CMD_CLEAR_ERROR) |
1658 (1 << NFIT_CMD_TRANSLATE_SPA) |
1659 (1 << NFIT_CMD_ARS_INJECT_SET) |
1660 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
1661 (1 << NFIT_CMD_ARS_INJECT_GET);
1662 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1663 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1664 set_bit(i, &nd_desc->bus_dsm_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001665}
1666
Dan Williams1f7df6f2015-06-09 20:13:14 -04001667static ssize_t range_index_show(struct device *dev,
1668 struct device_attribute *attr, char *buf)
1669{
1670 struct nd_region *nd_region = to_nd_region(dev);
1671 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1672
1673 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1674}
1675static DEVICE_ATTR_RO(range_index);
1676
Dan Williamsa15797f2017-08-31 12:53:36 -07001677static ssize_t ecc_unit_size_show(struct device *dev,
1678 struct device_attribute *attr, char *buf)
1679{
1680 struct nd_region *nd_region = to_nd_region(dev);
1681 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1682
1683 return sprintf(buf, "%d\n", nfit_spa->clear_err_unit);
1684}
1685static DEVICE_ATTR_RO(ecc_unit_size);
1686
Dan Williams1f7df6f2015-06-09 20:13:14 -04001687static struct attribute *acpi_nfit_region_attributes[] = {
1688 &dev_attr_range_index.attr,
Dan Williamsa15797f2017-08-31 12:53:36 -07001689 &dev_attr_ecc_unit_size.attr,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001690 NULL,
1691};
1692
Arvind Yadav5e937462017-06-22 15:44:41 +05301693static const struct attribute_group acpi_nfit_region_attribute_group = {
Dan Williams1f7df6f2015-06-09 20:13:14 -04001694 .name = "nfit",
1695 .attrs = acpi_nfit_region_attributes,
1696};
1697
1698static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1699 &nd_region_attribute_group,
1700 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001701 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001702 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001703 &acpi_nfit_region_attribute_group,
1704 NULL,
1705};
1706
Dan Williamseaf96152015-05-01 13:11:27 -04001707/* enough info to uniquely specify an interleave set */
1708struct nfit_set_info {
1709 struct nfit_set_info_map {
1710 u64 region_offset;
1711 u32 serial_number;
1712 u32 pad;
1713 } mapping[0];
1714};
1715
Dan Williamsc12c48c2017-06-04 10:59:15 +09001716struct nfit_set_info2 {
1717 struct nfit_set_info_map2 {
1718 u64 region_offset;
1719 u32 serial_number;
1720 u16 vendor_id;
1721 u16 manufacturing_date;
1722 u8 manufacturing_location;
1723 u8 reserved[31];
1724 } mapping[0];
1725};
1726
Dan Williamseaf96152015-05-01 13:11:27 -04001727static size_t sizeof_nfit_set_info(int num_mappings)
1728{
1729 return sizeof(struct nfit_set_info)
1730 + num_mappings * sizeof(struct nfit_set_info_map);
1731}
1732
Dan Williamsc12c48c2017-06-04 10:59:15 +09001733static size_t sizeof_nfit_set_info2(int num_mappings)
1734{
1735 return sizeof(struct nfit_set_info2)
1736 + num_mappings * sizeof(struct nfit_set_info_map2);
1737}
1738
Dan Williams86ef58a2017-02-28 18:32:48 -08001739static int cmp_map_compat(const void *m0, const void *m1)
Dan Williamseaf96152015-05-01 13:11:27 -04001740{
1741 const struct nfit_set_info_map *map0 = m0;
1742 const struct nfit_set_info_map *map1 = m1;
1743
1744 return memcmp(&map0->region_offset, &map1->region_offset,
1745 sizeof(u64));
1746}
1747
Dan Williams86ef58a2017-02-28 18:32:48 -08001748static int cmp_map(const void *m0, const void *m1)
1749{
1750 const struct nfit_set_info_map *map0 = m0;
1751 const struct nfit_set_info_map *map1 = m1;
1752
Dan Williamsb03b99a2017-03-27 21:53:38 -07001753 if (map0->region_offset < map1->region_offset)
1754 return -1;
1755 else if (map0->region_offset > map1->region_offset)
1756 return 1;
1757 return 0;
Dan Williams86ef58a2017-02-28 18:32:48 -08001758}
1759
Dan Williamsc12c48c2017-06-04 10:59:15 +09001760static int cmp_map2(const void *m0, const void *m1)
1761{
1762 const struct nfit_set_info_map2 *map0 = m0;
1763 const struct nfit_set_info_map2 *map1 = m1;
1764
1765 if (map0->region_offset < map1->region_offset)
1766 return -1;
1767 else if (map0->region_offset > map1->region_offset)
1768 return 1;
1769 return 0;
1770}
1771
Dan Williamseaf96152015-05-01 13:11:27 -04001772/* Retrieve the nth entry referencing this spa */
1773static struct acpi_nfit_memory_map *memdev_from_spa(
1774 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1775{
1776 struct nfit_memdev *nfit_memdev;
1777
1778 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1779 if (nfit_memdev->memdev->range_index == range_index)
1780 if (n-- == 0)
1781 return nfit_memdev->memdev;
1782 return NULL;
1783}
1784
1785static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1786 struct nd_region_desc *ndr_desc,
1787 struct acpi_nfit_system_address *spa)
1788{
Dan Williamseaf96152015-05-01 13:11:27 -04001789 struct device *dev = acpi_desc->dev;
1790 struct nd_interleave_set *nd_set;
1791 u16 nr = ndr_desc->num_mappings;
Dan Williamsc12c48c2017-06-04 10:59:15 +09001792 struct nfit_set_info2 *info2;
Dan Williamseaf96152015-05-01 13:11:27 -04001793 struct nfit_set_info *info;
Dan Williams8f2bc242017-06-06 11:39:30 -07001794 int i;
Dan Williamseaf96152015-05-01 13:11:27 -04001795
Dan Williamsfaec6f82017-06-06 11:10:51 -07001796 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1797 if (!nd_set)
1798 return -ENOMEM;
1799 ndr_desc->nd_set = nd_set;
1800 guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
1801
Dan Williamseaf96152015-05-01 13:11:27 -04001802 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1803 if (!info)
1804 return -ENOMEM;
Dan Williamsc12c48c2017-06-04 10:59:15 +09001805
1806 info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
1807 if (!info2)
1808 return -ENOMEM;
1809
Dan Williamseaf96152015-05-01 13:11:27 -04001810 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07001811 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04001812 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williamsc12c48c2017-06-04 10:59:15 +09001813 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07001814 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04001815 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1816 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1817 spa->range_index, i);
Dan Williamsdcb79b12017-08-07 14:27:57 -07001818 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
Dan Williamseaf96152015-05-01 13:11:27 -04001819
1820 if (!memdev || !nfit_mem->dcr) {
1821 dev_err(dev, "%s: failed to find DCR\n", __func__);
1822 return -ENODEV;
1823 }
1824
1825 map->region_offset = memdev->region_offset;
Dan Williamsdcb79b12017-08-07 14:27:57 -07001826 map->serial_number = dcr->serial_number;
Dan Williamsc12c48c2017-06-04 10:59:15 +09001827
1828 map2->region_offset = memdev->region_offset;
Dan Williamsdcb79b12017-08-07 14:27:57 -07001829 map2->serial_number = dcr->serial_number;
1830 map2->vendor_id = dcr->vendor_id;
1831 map2->manufacturing_date = dcr->manufacturing_date;
1832 map2->manufacturing_location = dcr->manufacturing_location;
Dan Williamseaf96152015-05-01 13:11:27 -04001833 }
1834
Dan Williamsc12c48c2017-06-04 10:59:15 +09001835 /* v1.1 namespaces */
Dan Williamseaf96152015-05-01 13:11:27 -04001836 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1837 cmp_map, NULL);
Dan Williamsc12c48c2017-06-04 10:59:15 +09001838 nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
Dan Williams86ef58a2017-02-28 18:32:48 -08001839
Dan Williamsc12c48c2017-06-04 10:59:15 +09001840 /* v1.2 namespaces */
1841 sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
1842 cmp_map2, NULL);
1843 nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
1844
1845 /* support v1.1 namespaces created with the wrong sort order */
Dan Williams86ef58a2017-02-28 18:32:48 -08001846 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1847 cmp_map_compat, NULL);
1848 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1849
Dan Williams401c0a12017-08-04 17:20:16 -07001850 /* record the result of the sort for the mapping position */
1851 for (i = 0; i < nr; i++) {
1852 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
1853 int j;
1854
1855 for (j = 0; j < nr; j++) {
1856 struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
1857 struct nvdimm *nvdimm = mapping->nvdimm;
1858 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
Dan Williamsdcb79b12017-08-07 14:27:57 -07001859 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
Dan Williams401c0a12017-08-04 17:20:16 -07001860
Dan Williamsdcb79b12017-08-07 14:27:57 -07001861 if (map2->serial_number == dcr->serial_number &&
1862 map2->vendor_id == dcr->vendor_id &&
1863 map2->manufacturing_date == dcr->manufacturing_date &&
Dan Williams401c0a12017-08-04 17:20:16 -07001864 map2->manufacturing_location
Dan Williamsdcb79b12017-08-07 14:27:57 -07001865 == dcr->manufacturing_location) {
Dan Williams401c0a12017-08-04 17:20:16 -07001866 mapping->position = i;
1867 break;
1868 }
1869 }
1870 }
1871
Dan Williamseaf96152015-05-01 13:11:27 -04001872 ndr_desc->nd_set = nd_set;
1873 devm_kfree(dev, info);
Dan Williamsc12c48c2017-06-04 10:59:15 +09001874 devm_kfree(dev, info2);
Dan Williamseaf96152015-05-01 13:11:27 -04001875
1876 return 0;
1877}
1878
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001879static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1880{
1881 struct acpi_nfit_interleave *idt = mmio->idt;
1882 u32 sub_line_offset, line_index, line_offset;
1883 u64 line_no, table_skip_count, table_offset;
1884
1885 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1886 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1887 line_offset = idt->line_offset[line_index]
1888 * mmio->line_size;
1889 table_offset = table_skip_count * mmio->table_size;
1890
1891 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1892}
1893
Ross Zwislerde4a1962015-08-20 16:27:38 -06001894static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001895{
1896 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1897 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001898 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001899
1900 if (mmio->num_lines)
1901 offset = to_interleave_offset(offset, mmio);
1902
Ross Zwisler68202c92016-07-29 14:59:12 -06001903 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001904}
1905
1906static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1907 resource_size_t dpa, unsigned int len, unsigned int write)
1908{
1909 u64 cmd, offset;
1910 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1911
1912 enum {
1913 BCW_OFFSET_MASK = (1ULL << 48)-1,
1914 BCW_LEN_SHIFT = 48,
1915 BCW_LEN_MASK = (1ULL << 8) - 1,
1916 BCW_CMD_SHIFT = 56,
1917 };
1918
1919 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1920 len = len >> L1_CACHE_SHIFT;
1921 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1922 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1923
1924 offset = nfit_blk->cmd_offset + mmio->size * bw;
1925 if (mmio->num_lines)
1926 offset = to_interleave_offset(offset, mmio);
1927
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001928 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001929 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001930
Dan Williamsaef25332016-02-12 17:01:11 -08001931 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001932 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001933}
1934
1935static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1936 resource_size_t dpa, void *iobuf, size_t len, int rw,
1937 unsigned int lane)
1938{
1939 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1940 unsigned int copied = 0;
1941 u64 base_offset;
1942 int rc;
1943
1944 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1945 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001946 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1947 while (len) {
1948 unsigned int c;
1949 u64 offset;
1950
1951 if (mmio->num_lines) {
1952 u32 line_offset;
1953
1954 offset = to_interleave_offset(base_offset + copied,
1955 mmio);
1956 div_u64_rem(offset, mmio->line_size, &line_offset);
1957 c = min_t(size_t, len, mmio->line_size - line_offset);
1958 } else {
1959 offset = base_offset + nfit_blk->bdw_offset;
1960 c = len;
1961 }
1962
1963 if (rw)
Dan Williams0aed55a2017-05-29 12:22:50 -07001964 memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001965 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001966 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001967 mmio_flush_range((void __force *)
1968 mmio->addr.aperture + offset, c);
1969
Dan Williams6abccd12017-01-13 14:14:23 -08001970 memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001971 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001972
1973 copied += c;
1974 len -= c;
1975 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001976
1977 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001978 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001979
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001980 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1981 return rc;
1982}
1983
1984static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1985 resource_size_t dpa, void *iobuf, u64 len, int rw)
1986{
1987 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1988 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1989 struct nd_region *nd_region = nfit_blk->nd_region;
1990 unsigned int lane, copied = 0;
1991 int rc = 0;
1992
1993 lane = nd_region_acquire_lane(nd_region);
1994 while (len) {
1995 u64 c = min(len, mmio->size);
1996
1997 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1998 iobuf + copied, c, rw, lane);
1999 if (rc)
2000 break;
2001
2002 copied += c;
2003 len -= c;
2004 }
2005 nd_region_release_lane(nd_region, lane);
2006
2007 return rc;
2008}
2009
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002010static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2011 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2012{
2013 if (idt) {
2014 mmio->num_lines = idt->line_count;
2015 mmio->line_size = idt->line_size;
2016 if (interleave_ways == 0)
2017 return -ENXIO;
2018 mmio->table_size = mmio->num_lines * interleave_ways
2019 * mmio->line_size;
2020 }
2021
2022 return 0;
2023}
2024
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002025static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2026 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2027{
2028 struct nd_cmd_dimm_flags flags;
2029 int rc;
2030
2031 memset(&flags, 0, sizeof(flags));
2032 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08002033 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002034
2035 if (rc >= 0 && flags.status == 0)
2036 nfit_blk->dimm_flags = flags.flags;
2037 else if (rc == -ENOTTY) {
2038 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08002039 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002040 rc = 0;
2041 } else
2042 rc = -ENXIO;
2043
2044 return rc;
2045}
2046
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002047static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2048 struct device *dev)
2049{
2050 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002051 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2052 struct nfit_blk_mmio *mmio;
2053 struct nfit_blk *nfit_blk;
2054 struct nfit_mem *nfit_mem;
2055 struct nvdimm *nvdimm;
2056 int rc;
2057
2058 nvdimm = nd_blk_region_to_dimm(ndbr);
2059 nfit_mem = nvdimm_provider_data(nvdimm);
2060 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2061 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
2062 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04002063 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2064 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002065 return -ENXIO;
2066 }
2067
2068 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2069 if (!nfit_blk)
2070 return -ENOMEM;
2071 nd_blk_region_set_provider_data(ndbr, nfit_blk);
2072 nfit_blk->nd_region = to_nd_region(dev);
2073
2074 /* map block aperture memory */
2075 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2076 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07002077 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
Dan Williamsca6a4652017-01-13 20:36:58 -08002078 nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002079 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002080 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
2081 nvdimm_name(nvdimm));
2082 return -ENOMEM;
2083 }
2084 mmio->size = nfit_mem->bdw->size;
2085 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2086 mmio->idt = nfit_mem->idt_bdw;
2087 mmio->spa = nfit_mem->spa_bdw;
2088 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2089 nfit_mem->memdev_bdw->interleave_ways);
2090 if (rc) {
2091 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
2092 __func__, nvdimm_name(nvdimm));
2093 return rc;
2094 }
2095
2096 /* map block control memory */
2097 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2098 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2099 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07002100 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2101 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06002102 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002103 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
2104 nvdimm_name(nvdimm));
2105 return -ENOMEM;
2106 }
2107 mmio->size = nfit_mem->dcr->window_size;
2108 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2109 mmio->idt = nfit_mem->idt_dcr;
2110 mmio->spa = nfit_mem->spa_dcr;
2111 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2112 nfit_mem->memdev_dcr->interleave_ways);
2113 if (rc) {
2114 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
2115 __func__, nvdimm_name(nvdimm));
2116 return rc;
2117 }
2118
Ross Zwislerf0f2c072015-07-10 11:06:14 -06002119 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2120 if (rc < 0) {
2121 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
2122 __func__, nvdimm_name(nvdimm));
2123 return rc;
2124 }
2125
Dan Williamsf284a4f2016-07-07 19:44:50 -07002126 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002127 dev_warn(dev, "unable to guarantee persistence of writes\n");
2128
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002129 if (mmio->line_size == 0)
2130 return 0;
2131
2132 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2133 + 8 > mmio->line_size) {
2134 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2135 return -ENXIO;
2136 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2137 + 8 > mmio->line_size) {
2138 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2139 return -ENXIO;
2140 }
2141
2142 return 0;
2143}
2144
Dan Williamsaef25332016-02-12 17:01:11 -08002145static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002146 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002147{
Dan Williamsaef25332016-02-12 17:01:11 -08002148 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002149 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08002150 int cmd_rc, rc;
2151
Dan Williams1cf03c02016-02-17 13:01:23 -08002152 cmd->address = spa->address;
2153 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08002154 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2155 sizeof(*cmd), &cmd_rc);
2156 if (rc < 0)
2157 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002158 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002159}
2160
Dan Williams1cf03c02016-02-17 13:01:23 -08002161static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002162{
2163 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002164 int cmd_rc;
2165 struct nd_cmd_ars_start ars_start;
2166 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2167 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002168
Dan Williams1cf03c02016-02-17 13:01:23 -08002169 memset(&ars_start, 0, sizeof(ars_start));
2170 ars_start.address = spa->address;
2171 ars_start.length = spa->length;
Toshi Kani80790032017-06-29 20:41:30 -06002172 ars_start.flags = acpi_desc->ars_start_flags;
Dan Williams1cf03c02016-02-17 13:01:23 -08002173 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2174 ars_start.type = ND_ARS_PERSISTENT;
2175 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2176 ars_start.type = ND_ARS_VOLATILE;
2177 else
2178 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07002179
Dan Williams1cf03c02016-02-17 13:01:23 -08002180 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2181 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08002182
Dan Williams1cf03c02016-02-17 13:01:23 -08002183 if (rc < 0)
2184 return rc;
2185 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002186}
2187
Dan Williams1cf03c02016-02-17 13:01:23 -08002188static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07002189{
Dan Williamsaef25332016-02-12 17:01:11 -08002190 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002191 struct nd_cmd_ars_start ars_start;
2192 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2193 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07002194
Dan Williams1cf03c02016-02-17 13:01:23 -08002195 memset(&ars_start, 0, sizeof(ars_start));
2196 ars_start.address = ars_status->restart_address;
2197 ars_start.length = ars_status->restart_length;
2198 ars_start.type = ars_status->type;
Toshi Kani80790032017-06-29 20:41:30 -06002199 ars_start.flags = acpi_desc->ars_start_flags;
Dan Williams1cf03c02016-02-17 13:01:23 -08002200 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2201 sizeof(ars_start), &cmd_rc);
2202 if (rc < 0)
2203 return rc;
2204 return cmd_rc;
2205}
Dan Williamsaef25332016-02-12 17:01:11 -08002206
Dan Williams1cf03c02016-02-17 13:01:23 -08002207static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2208{
2209 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2210 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2211 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08002212
Dan Williams1cf03c02016-02-17 13:01:23 -08002213 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2214 acpi_desc->ars_status_size, &cmd_rc);
2215 if (rc < 0)
2216 return rc;
2217 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002218}
2219
Dan Williams82aa37c2016-12-06 12:45:24 -08002220static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002221 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07002222{
Dan Williams82aa37c2016-12-06 12:45:24 -08002223 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
Vishal Verma0caeef62015-12-24 19:21:43 -07002224 int rc;
2225 u32 i;
2226
Dan Williams82aa37c2016-12-06 12:45:24 -08002227 /*
2228 * First record starts at 44 byte offset from the start of the
2229 * payload.
2230 */
2231 if (ars_status->out_length < 44)
2232 return 0;
Vishal Verma0caeef62015-12-24 19:21:43 -07002233 for (i = 0; i < ars_status->num_records; i++) {
Dan Williams82aa37c2016-12-06 12:45:24 -08002234 /* only process full records */
2235 if (ars_status->out_length
2236 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2237 break;
Vishal Verma0caeef62015-12-24 19:21:43 -07002238 rc = nvdimm_bus_add_poison(nvdimm_bus,
2239 ars_status->records[i].err_address,
2240 ars_status->records[i].length);
2241 if (rc)
2242 return rc;
2243 }
Dan Williams82aa37c2016-12-06 12:45:24 -08002244 if (i < ars_status->num_records)
2245 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
Vishal Verma0caeef62015-12-24 19:21:43 -07002246
2247 return 0;
2248}
2249
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002250static void acpi_nfit_remove_resource(void *data)
2251{
2252 struct resource *res = data;
2253
2254 remove_resource(res);
2255}
2256
2257static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2258 struct nd_region_desc *ndr_desc)
2259{
2260 struct resource *res, *nd_res = ndr_desc->res;
2261 int is_pmem, ret;
2262
2263 /* No operation if the region is already registered as PMEM */
2264 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2265 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2266 if (is_pmem == REGION_INTERSECTS)
2267 return 0;
2268
2269 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2270 if (!res)
2271 return -ENOMEM;
2272
2273 res->name = "Persistent Memory";
2274 res->start = nd_res->start;
2275 res->end = nd_res->end;
2276 res->flags = IORESOURCE_MEM;
2277 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2278
2279 ret = insert_resource(&iomem_resource, res);
2280 if (ret)
2281 return ret;
2282
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302283 ret = devm_add_action_or_reset(acpi_desc->dev,
2284 acpi_nfit_remove_resource,
2285 res);
2286 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002287 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002288
2289 return 0;
2290}
2291
Dan Williams1f7df6f2015-06-09 20:13:14 -04002292static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002293 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002294 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002295 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002296{
2297 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2298 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002299 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002300 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002301 struct nfit_mem *nfit_mem;
Dan Williamsfaec6f82017-06-06 11:10:51 -07002302 int blk_valid = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002303
2304 if (!nvdimm) {
2305 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2306 spa->range_index, memdev->device_handle);
2307 return -ENODEV;
2308 }
2309
Dan Williams44c462e2016-09-19 16:38:50 -07002310 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002311 switch (nfit_spa_type(spa)) {
2312 case NFIT_SPA_PM:
2313 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002314 mapping->start = memdev->address;
2315 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002316 break;
2317 case NFIT_SPA_DCR:
2318 nfit_mem = nvdimm_provider_data(nvdimm);
2319 if (!nfit_mem || !nfit_mem->bdw) {
2320 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2321 spa->range_index, nvdimm_name(nvdimm));
2322 } else {
Dan Williams44c462e2016-09-19 16:38:50 -07002323 mapping->size = nfit_mem->bdw->capacity;
2324 mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04002325 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002326 blk_valid = 1;
2327 }
2328
Dan Williams44c462e2016-09-19 16:38:50 -07002329 ndr_desc->mapping = mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002330 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002331 ndbr_desc = to_blk_region_desc(ndr_desc);
2332 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002333 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williamsfaec6f82017-06-06 11:10:51 -07002334 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2335 if (rc)
2336 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002337 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2338 ndr_desc);
2339 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002340 return -ENOMEM;
2341 break;
2342 }
2343
2344 return 0;
2345}
2346
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002347static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2348{
2349 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2350 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2351 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2352 nfit_spa_type(spa) == NFIT_SPA_PCD);
2353}
2354
Dan Williamsc9e582a2017-05-29 23:12:19 -07002355static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2356{
2357 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2358 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2359 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2360}
2361
Dan Williams1f7df6f2015-06-09 20:13:14 -04002362static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2363 struct nfit_spa *nfit_spa)
2364{
Dan Williams44c462e2016-09-19 16:38:50 -07002365 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002366 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002367 struct nd_blk_region_desc ndbr_desc;
2368 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002369 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002370 struct nvdimm_bus *nvdimm_bus;
2371 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002372 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002373
Dan Williams1cf03c02016-02-17 13:01:23 -08002374 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002375 return 0;
2376
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002377 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04002378 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2379 __func__);
2380 return 0;
2381 }
2382
2383 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002384 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002385 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002386 res.start = spa->address;
2387 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002388 ndr_desc = &ndbr_desc.ndr_desc;
2389 ndr_desc->res = &res;
2390 ndr_desc->provider_data = nfit_spa;
2391 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002392 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2393 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2394 spa->proximity_domain);
2395 else
2396 ndr_desc->numa_node = NUMA_NO_NODE;
2397
Dan Williams1f7df6f2015-06-09 20:13:14 -04002398 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2399 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002400 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002401
2402 if (memdev->range_index != spa->range_index)
2403 continue;
2404 if (count >= ND_MAX_MAPPINGS) {
2405 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2406 spa->range_index, ND_MAX_MAPPINGS);
2407 return -ENXIO;
2408 }
Dan Williams44c462e2016-09-19 16:38:50 -07002409 mapping = &mappings[count++];
2410 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002411 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002412 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002413 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002414 }
2415
Dan Williams44c462e2016-09-19 16:38:50 -07002416 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002417 ndr_desc->num_mappings = count;
2418 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002419 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002420 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002421
Dan Williams1f7df6f2015-06-09 20:13:14 -04002422 nvdimm_bus = acpi_desc->nvdimm_bus;
2423 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002424 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002425 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002426 dev_warn(acpi_desc->dev,
2427 "failed to insert pmem resource to iomem: %d\n",
2428 rc);
Dan Williams48901162016-03-09 17:15:43 -08002429 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002430 }
Dan Williams48901162016-03-09 17:15:43 -08002431
Dan Williams1cf03c02016-02-17 13:01:23 -08002432 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2433 ndr_desc);
2434 if (!nfit_spa->nd_region)
2435 rc = -ENOMEM;
Dan Williamsc9e582a2017-05-29 23:12:19 -07002436 } else if (nfit_spa_is_volatile(spa)) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002437 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2438 ndr_desc);
2439 if (!nfit_spa->nd_region)
2440 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002441 } else if (nfit_spa_is_virtual(spa)) {
2442 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2443 ndr_desc);
2444 if (!nfit_spa->nd_region)
2445 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002446 }
Vishal Verma20985162015-10-27 16:58:27 -06002447
Dan Williams1cf03c02016-02-17 13:01:23 -08002448 out:
2449 if (rc)
2450 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2451 nfit_spa->spa->range_index);
2452 return rc;
2453}
2454
2455static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2456 u32 max_ars)
2457{
2458 struct device *dev = acpi_desc->dev;
2459 struct nd_cmd_ars_status *ars_status;
2460
2461 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2462 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2463 return 0;
2464 }
2465
2466 if (acpi_desc->ars_status)
2467 devm_kfree(dev, acpi_desc->ars_status);
2468 acpi_desc->ars_status = NULL;
2469 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2470 if (!ars_status)
2471 return -ENOMEM;
2472 acpi_desc->ars_status = ars_status;
2473 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002474 return 0;
2475}
2476
Dan Williams1cf03c02016-02-17 13:01:23 -08002477static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2478 struct nfit_spa *nfit_spa)
2479{
2480 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2481 int rc;
2482
2483 if (!nfit_spa->max_ars) {
2484 struct nd_cmd_ars_cap ars_cap;
2485
2486 memset(&ars_cap, 0, sizeof(ars_cap));
2487 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2488 if (rc < 0)
2489 return rc;
2490 nfit_spa->max_ars = ars_cap.max_ars_out;
2491 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2492 /* check that the supported scrub types match the spa type */
2493 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2494 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2495 return -ENOTTY;
2496 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2497 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2498 return -ENOTTY;
2499 }
2500
2501 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2502 return -ENOMEM;
2503
2504 rc = ars_get_status(acpi_desc);
2505 if (rc < 0 && rc != -ENOSPC)
2506 return rc;
2507
Dan Williams82aa37c2016-12-06 12:45:24 -08002508 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
Dan Williams1cf03c02016-02-17 13:01:23 -08002509 return -ENOMEM;
2510
2511 return 0;
2512}
2513
2514static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2515 struct nfit_spa *nfit_spa)
2516{
2517 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2518 unsigned int overflow_retry = scrub_overflow_abort;
2519 u64 init_ars_start = 0, init_ars_len = 0;
2520 struct device *dev = acpi_desc->dev;
2521 unsigned int tmo = scrub_timeout;
2522 int rc;
2523
Vishal Verma37b137f2016-07-23 21:51:42 -07002524 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002525 return;
2526
2527 rc = ars_start(acpi_desc, nfit_spa);
2528 /*
2529 * If we timed out the initial scan we'll still be busy here,
2530 * and will wait another timeout before giving up permanently.
2531 */
2532 if (rc < 0 && rc != -EBUSY)
2533 return;
2534
2535 do {
2536 u64 ars_start, ars_len;
2537
2538 if (acpi_desc->cancel)
2539 break;
2540 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2541 if (rc == -ENOTTY)
2542 break;
2543 if (rc == -EBUSY && !tmo) {
2544 dev_warn(dev, "range %d ars timeout, aborting\n",
2545 spa->range_index);
2546 break;
2547 }
2548
2549 if (rc == -EBUSY) {
2550 /*
2551 * Note, entries may be appended to the list
2552 * while the lock is dropped, but the workqueue
2553 * being active prevents entries being deleted /
2554 * freed.
2555 */
2556 mutex_unlock(&acpi_desc->init_mutex);
2557 ssleep(1);
2558 tmo--;
2559 mutex_lock(&acpi_desc->init_mutex);
2560 continue;
2561 }
2562
2563 /* we got some results, but there are more pending... */
2564 if (rc == -ENOSPC && overflow_retry--) {
2565 if (!init_ars_len) {
2566 init_ars_len = acpi_desc->ars_status->length;
2567 init_ars_start = acpi_desc->ars_status->address;
2568 }
2569 rc = ars_continue(acpi_desc);
2570 }
2571
2572 if (rc < 0) {
2573 dev_warn(dev, "range %d ars continuation failed\n",
2574 spa->range_index);
2575 break;
2576 }
2577
2578 if (init_ars_len) {
2579 ars_start = init_ars_start;
2580 ars_len = init_ars_len;
2581 } else {
2582 ars_start = acpi_desc->ars_status->address;
2583 ars_len = acpi_desc->ars_status->length;
2584 }
2585 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2586 spa->range_index, ars_start, ars_len);
2587 /* notify the region about new poison entries */
2588 nvdimm_region_notify(nfit_spa->nd_region,
2589 NVDIMM_REVALIDATE_POISON);
2590 break;
2591 } while (1);
2592}
2593
2594static void acpi_nfit_scrub(struct work_struct *work)
2595{
2596 struct device *dev;
2597 u64 init_scrub_length = 0;
2598 struct nfit_spa *nfit_spa;
2599 u64 init_scrub_address = 0;
2600 bool init_ars_done = false;
2601 struct acpi_nfit_desc *acpi_desc;
2602 unsigned int tmo = scrub_timeout;
2603 unsigned int overflow_retry = scrub_overflow_abort;
2604
2605 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2606 dev = acpi_desc->dev;
2607
2608 /*
2609 * We scrub in 2 phases. The first phase waits for any platform
2610 * firmware initiated scrubs to complete and then we go search for the
2611 * affected spa regions to mark them scanned. In the second phase we
2612 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002613 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2614 * the first phase, but really only care about running phase 2, where
2615 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002616 */
2617
2618 /* process platform firmware initiated scrubs */
2619 retry:
2620 mutex_lock(&acpi_desc->init_mutex);
2621 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2622 struct nd_cmd_ars_status *ars_status;
2623 struct acpi_nfit_system_address *spa;
2624 u64 ars_start, ars_len;
2625 int rc;
2626
2627 if (acpi_desc->cancel)
2628 break;
2629
2630 if (nfit_spa->nd_region)
2631 continue;
2632
2633 if (init_ars_done) {
2634 /*
2635 * No need to re-query, we're now just
2636 * reconciling all the ranges covered by the
2637 * initial scrub
2638 */
2639 rc = 0;
2640 } else
2641 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2642
2643 if (rc == -ENOTTY) {
2644 /* no ars capability, just register spa and move on */
2645 acpi_nfit_register_region(acpi_desc, nfit_spa);
2646 continue;
2647 }
2648
2649 if (rc == -EBUSY && !tmo) {
2650 /* fallthrough to directed scrub in phase 2 */
2651 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2652 break;
2653 } else if (rc == -EBUSY) {
2654 mutex_unlock(&acpi_desc->init_mutex);
2655 ssleep(1);
2656 tmo--;
2657 goto retry;
2658 }
2659
2660 /* we got some results, but there are more pending... */
2661 if (rc == -ENOSPC && overflow_retry--) {
2662 ars_status = acpi_desc->ars_status;
2663 /*
2664 * Record the original scrub range, so that we
2665 * can recall all the ranges impacted by the
2666 * initial scrub.
2667 */
2668 if (!init_scrub_length) {
2669 init_scrub_length = ars_status->length;
2670 init_scrub_address = ars_status->address;
2671 }
2672 rc = ars_continue(acpi_desc);
2673 if (rc == 0) {
2674 mutex_unlock(&acpi_desc->init_mutex);
2675 goto retry;
2676 }
2677 }
2678
2679 if (rc < 0) {
2680 /*
2681 * Initial scrub failed, we'll give it one more
2682 * try below...
2683 */
2684 break;
2685 }
2686
2687 /* We got some final results, record completed ranges */
2688 ars_status = acpi_desc->ars_status;
2689 if (init_scrub_length) {
2690 ars_start = init_scrub_address;
2691 ars_len = ars_start + init_scrub_length;
2692 } else {
2693 ars_start = ars_status->address;
2694 ars_len = ars_status->length;
2695 }
2696 spa = nfit_spa->spa;
2697
2698 if (!init_ars_done) {
2699 init_ars_done = true;
2700 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2701 ars_start, ars_len);
2702 }
2703 if (ars_start <= spa->address && ars_start + ars_len
2704 >= spa->address + spa->length)
2705 acpi_nfit_register_region(acpi_desc, nfit_spa);
2706 }
2707
2708 /*
2709 * For all the ranges not covered by an initial scrub we still
2710 * want to see if there are errors, but it's ok to discover them
2711 * asynchronously.
2712 */
2713 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2714 /*
2715 * Flag all the ranges that still need scrubbing, but
2716 * register them now to make data available.
2717 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002718 if (!nfit_spa->nd_region) {
2719 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002720 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002721 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002722 }
Dan Williams9ccaed42017-04-13 22:48:46 -07002723 acpi_desc->init_complete = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002724
2725 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2726 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002727 acpi_desc->scrub_count++;
Toshi Kani80790032017-06-29 20:41:30 -06002728 acpi_desc->ars_start_flags = 0;
Vishal Verma37b137f2016-07-23 21:51:42 -07002729 if (acpi_desc->scrub_count_state)
2730 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002731 mutex_unlock(&acpi_desc->init_mutex);
2732}
2733
Dan Williams1f7df6f2015-06-09 20:13:14 -04002734static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2735{
2736 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002737 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002738
Dan Williams1cf03c02016-02-17 13:01:23 -08002739 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2740 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2741 /* BLK regions don't need to wait for ars results */
2742 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2743 if (rc)
2744 return rc;
2745 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002746
Toshi Kani80790032017-06-29 20:41:30 -06002747 acpi_desc->ars_start_flags = 0;
Dan Williamsfbabd822017-04-18 09:56:31 -07002748 if (!acpi_desc->cancel)
2749 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002750 return 0;
2751}
2752
Vishal Verma20985162015-10-27 16:58:27 -06002753static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2754 struct nfit_table_prev *prev)
2755{
2756 struct device *dev = acpi_desc->dev;
2757
2758 if (!list_empty(&prev->spas) ||
2759 !list_empty(&prev->memdevs) ||
2760 !list_empty(&prev->dcrs) ||
2761 !list_empty(&prev->bdws) ||
2762 !list_empty(&prev->idts) ||
2763 !list_empty(&prev->flushes)) {
2764 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2765 return -ENXIO;
2766 }
2767 return 0;
2768}
2769
Vishal Verma37b137f2016-07-23 21:51:42 -07002770static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2771{
2772 struct device *dev = acpi_desc->dev;
2773 struct kernfs_node *nfit;
2774 struct device *bus_dev;
2775
2776 if (!ars_supported(acpi_desc->nvdimm_bus))
2777 return 0;
2778
2779 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2780 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2781 if (!nfit) {
2782 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2783 return -ENODEV;
2784 }
2785 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2786 sysfs_put(nfit);
2787 if (!acpi_desc->scrub_count_state) {
2788 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2789 return -ENODEV;
2790 }
2791
2792 return 0;
2793}
2794
Dan Williamsfbabd822017-04-18 09:56:31 -07002795static void acpi_nfit_unregister(void *data)
Dan Williams58cd71b2016-07-21 18:05:36 -07002796{
2797 struct acpi_nfit_desc *acpi_desc = data;
2798
Dan Williams58cd71b2016-07-21 18:05:36 -07002799 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002800}
2801
Dan Williamse7a11b42016-07-14 16:19:55 -07002802int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002803{
2804 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002805 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002806 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002807 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002808
Dan Williams58cd71b2016-07-21 18:05:36 -07002809 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002810 acpi_nfit_init_dsms(acpi_desc);
2811
Dan Williams58cd71b2016-07-21 18:05:36 -07002812 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2813 &acpi_desc->nd_desc);
2814 if (!acpi_desc->nvdimm_bus)
2815 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002816
Dan Williamsfbabd822017-04-18 09:56:31 -07002817 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
Dan Williams58cd71b2016-07-21 18:05:36 -07002818 acpi_desc);
2819 if (rc)
2820 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002821
2822 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2823 if (rc)
2824 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002825
2826 /* register this acpi_desc for mce notifications */
2827 mutex_lock(&acpi_desc_lock);
2828 list_add_tail(&acpi_desc->list, &acpi_descs);
2829 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002830 }
2831
Vishal Verma20985162015-10-27 16:58:27 -06002832 mutex_lock(&acpi_desc->init_mutex);
2833
2834 INIT_LIST_HEAD(&prev.spas);
2835 INIT_LIST_HEAD(&prev.memdevs);
2836 INIT_LIST_HEAD(&prev.dcrs);
2837 INIT_LIST_HEAD(&prev.bdws);
2838 INIT_LIST_HEAD(&prev.idts);
2839 INIT_LIST_HEAD(&prev.flushes);
2840
2841 list_cut_position(&prev.spas, &acpi_desc->spas,
2842 acpi_desc->spas.prev);
2843 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2844 acpi_desc->memdevs.prev);
2845 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2846 acpi_desc->dcrs.prev);
2847 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2848 acpi_desc->bdws.prev);
2849 list_cut_position(&prev.idts, &acpi_desc->idts,
2850 acpi_desc->idts.prev);
2851 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2852 acpi_desc->flushes.prev);
2853
Vishal Verma20985162015-10-27 16:58:27 -06002854 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002855 while (!IS_ERR_OR_NULL(data))
2856 data = add_table(acpi_desc, &prev, data, end);
2857
2858 if (IS_ERR(data)) {
2859 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2860 PTR_ERR(data));
2861 rc = PTR_ERR(data);
2862 goto out_unlock;
2863 }
2864
2865 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2866 if (rc)
2867 goto out_unlock;
2868
Dan Williams81ed4e32016-06-10 18:20:53 -07002869 rc = nfit_mem_init(acpi_desc);
2870 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002871 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002872
2873 rc = acpi_nfit_register_dimms(acpi_desc);
2874 if (rc)
2875 goto out_unlock;
2876
2877 rc = acpi_nfit_register_regions(acpi_desc);
2878
2879 out_unlock:
2880 mutex_unlock(&acpi_desc->init_mutex);
2881 return rc;
2882}
2883EXPORT_SYMBOL_GPL(acpi_nfit_init);
2884
Dan Williams7ae0fa432016-02-19 12:16:34 -08002885struct acpi_nfit_flush_work {
2886 struct work_struct work;
2887 struct completion cmp;
2888};
2889
2890static void flush_probe(struct work_struct *work)
2891{
2892 struct acpi_nfit_flush_work *flush;
2893
2894 flush = container_of(work, typeof(*flush), work);
2895 complete(&flush->cmp);
2896}
2897
2898static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2899{
2900 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2901 struct device *dev = acpi_desc->dev;
2902 struct acpi_nfit_flush_work flush;
Dan Williamse4714862017-02-02 10:31:00 -08002903 int rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002904
2905 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2906 device_lock(dev);
2907 device_unlock(dev);
2908
Dan Williams9ccaed42017-04-13 22:48:46 -07002909 /* bounce the init_mutex to make init_complete valid */
2910 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07002911 if (acpi_desc->cancel || acpi_desc->init_complete) {
2912 mutex_unlock(&acpi_desc->init_mutex);
Dan Williams9ccaed42017-04-13 22:48:46 -07002913 return 0;
Dan Williamsfbabd822017-04-18 09:56:31 -07002914 }
Dan Williams9ccaed42017-04-13 22:48:46 -07002915
Dan Williams7ae0fa432016-02-19 12:16:34 -08002916 /*
2917 * Scrub work could take 10s of seconds, userspace may give up so we
2918 * need to be interruptible while waiting.
2919 */
2920 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2921 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2922 queue_work(nfit_wq, &flush.work);
Dan Williamsfbabd822017-04-18 09:56:31 -07002923 mutex_unlock(&acpi_desc->init_mutex);
Dan Williamse4714862017-02-02 10:31:00 -08002924
2925 rc = wait_for_completion_interruptible(&flush.cmp);
2926 cancel_work_sync(&flush.work);
2927 return rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002928}
2929
Dan Williams87bf5722016-02-22 21:50:31 -08002930static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2931 struct nvdimm *nvdimm, unsigned int cmd)
2932{
2933 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2934
2935 if (nvdimm)
2936 return 0;
2937 if (cmd != ND_CMD_ARS_START)
2938 return 0;
2939
2940 /*
2941 * The kernel and userspace may race to initiate a scrub, but
2942 * the scrub thread is prepared to lose that initial race. It
2943 * just needs guarantees that any ars it initiates are not
2944 * interrupted by any intervening start reqeusts from userspace.
2945 */
2946 if (work_busy(&acpi_desc->work))
2947 return -EBUSY;
2948
2949 return 0;
2950}
2951
Toshi Kani80790032017-06-29 20:41:30 -06002952int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc, u8 flags)
Vishal Verma37b137f2016-07-23 21:51:42 -07002953{
2954 struct device *dev = acpi_desc->dev;
2955 struct nfit_spa *nfit_spa;
2956
2957 if (work_busy(&acpi_desc->work))
2958 return -EBUSY;
2959
Vishal Verma37b137f2016-07-23 21:51:42 -07002960 mutex_lock(&acpi_desc->init_mutex);
Dan Williamsfbabd822017-04-18 09:56:31 -07002961 if (acpi_desc->cancel) {
2962 mutex_unlock(&acpi_desc->init_mutex);
2963 return 0;
2964 }
2965
Vishal Verma37b137f2016-07-23 21:51:42 -07002966 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2967 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2968
2969 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2970 continue;
2971
2972 nfit_spa->ars_required = 1;
2973 }
Toshi Kani80790032017-06-29 20:41:30 -06002974 acpi_desc->ars_start_flags = flags;
Vishal Verma37b137f2016-07-23 21:51:42 -07002975 queue_work(nfit_wq, &acpi_desc->work);
2976 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2977 mutex_unlock(&acpi_desc->init_mutex);
2978
2979 return 0;
2980}
2981
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002982void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002983{
2984 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002985
2986 dev_set_drvdata(dev, acpi_desc);
2987 acpi_desc->dev = dev;
2988 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2989 nd_desc = &acpi_desc->nd_desc;
2990 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002991 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002992 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002993 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002994 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002995 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2996
Dan Williamsb94d5232015-05-19 22:54:31 -04002997 INIT_LIST_HEAD(&acpi_desc->spas);
2998 INIT_LIST_HEAD(&acpi_desc->dcrs);
2999 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04003000 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06003001 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04003002 INIT_LIST_HEAD(&acpi_desc->memdevs);
3003 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003004 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06003005 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08003006 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04003007}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003008EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04003009
Dan Williams3c87f372017-04-03 13:52:14 -07003010static void acpi_nfit_put_table(void *table)
3011{
3012 acpi_put_table(table);
3013}
3014
Dan Williamsfbabd822017-04-18 09:56:31 -07003015void acpi_nfit_shutdown(void *data)
3016{
3017 struct acpi_nfit_desc *acpi_desc = data;
3018 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3019
3020 /*
3021 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3022 * race teardown
3023 */
3024 mutex_lock(&acpi_desc_lock);
3025 list_del(&acpi_desc->list);
3026 mutex_unlock(&acpi_desc_lock);
3027
3028 mutex_lock(&acpi_desc->init_mutex);
3029 acpi_desc->cancel = 1;
3030 mutex_unlock(&acpi_desc->init_mutex);
3031
3032 /*
3033 * Bounce the nvdimm bus lock to make sure any in-flight
3034 * acpi_nfit_ars_rescan() submissions have had a chance to
3035 * either submit or see ->cancel set.
3036 */
3037 device_lock(bus_dev);
3038 device_unlock(bus_dev);
3039
3040 flush_workqueue(nfit_wq);
3041}
3042EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3043
Dan Williamsb94d5232015-05-19 22:54:31 -04003044static int acpi_nfit_add(struct acpi_device *adev)
3045{
Vishal Verma20985162015-10-27 16:58:27 -06003046 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04003047 struct acpi_nfit_desc *acpi_desc;
3048 struct device *dev = &adev->dev;
3049 struct acpi_table_header *tbl;
3050 acpi_status status = AE_OK;
3051 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07003052 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04003053
Lv Zheng6b11d1d2016-12-14 15:04:39 +08003054 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
Dan Williamsb94d5232015-05-19 22:54:31 -04003055 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06003056 /* This is ok, we could have an nvdimm hotplugged later */
3057 dev_dbg(dev, "failed to find NFIT at startup\n");
3058 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04003059 }
Dan Williams3c87f372017-04-03 13:52:14 -07003060
3061 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3062 if (rc)
3063 return rc;
Lv Zheng6b11d1d2016-12-14 15:04:39 +08003064 sz = tbl->length;
Dan Williamsb94d5232015-05-19 22:54:31 -04003065
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003066 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3067 if (!acpi_desc)
3068 return -ENOMEM;
3069 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04003070
Dan Williamse7a11b42016-07-14 16:19:55 -07003071 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05003072 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04003073
Vishal Verma20985162015-10-27 16:58:27 -06003074 /* Evaluate _FIT and override with that if present */
3075 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3076 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07003077 union acpi_object *obj = buf.pointer;
3078
3079 if (obj->type == ACPI_TYPE_BUFFER)
3080 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3081 obj->buffer.length);
3082 else
Linda Knippers6b577c92015-11-20 19:05:49 -05003083 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
3084 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07003085 kfree(buf.pointer);
3086 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07003087 /* skip over the lead-in header table */
3088 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3089 + sizeof(struct acpi_table_nfit),
3090 sz - sizeof(struct acpi_table_nfit));
Dan Williamsfbabd822017-04-18 09:56:31 -07003091
3092 if (rc)
3093 return rc;
3094 return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
Dan Williamsb94d5232015-05-19 22:54:31 -04003095}
3096
3097static int acpi_nfit_remove(struct acpi_device *adev)
3098{
Dan Williamsfbabd822017-04-18 09:56:31 -07003099 /* see acpi_nfit_unregister */
Dan Williamsb94d5232015-05-19 22:54:31 -04003100 return 0;
3101}
3102
Toshi Kani56b47fe2017-06-08 12:36:57 -06003103static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
Vishal Verma20985162015-10-27 16:58:27 -06003104{
Dan Williamsc14a8682016-08-18 22:15:04 -07003105 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06003106 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07003107 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06003108 acpi_status status;
3109 int ret;
3110
Vishal Verma20985162015-10-27 16:58:27 -06003111 if (!dev->driver) {
3112 /* dev->driver may be null if we're being removed */
3113 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Dan Williamsc14a8682016-08-18 22:15:04 -07003114 return;
Vishal Verma20985162015-10-27 16:58:27 -06003115 }
3116
3117 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08003118 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3119 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07003120 return;
3121 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003122 } else {
3123 /*
3124 * Finish previous registration before considering new
3125 * regions.
3126 */
3127 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06003128 }
3129
3130 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07003131 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06003132 if (ACPI_FAILURE(status)) {
3133 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07003134 return;
Vishal Verma20985162015-10-27 16:58:27 -06003135 }
3136
Linda Knippers6b577c92015-11-20 19:05:49 -05003137 obj = buf.pointer;
3138 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07003139 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3140 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07003141 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05003142 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07003143 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05003144 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06003145 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07003146}
Toshi Kani56b47fe2017-06-08 12:36:57 -06003147
3148static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3149{
3150 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Toshi Kani80790032017-06-29 20:41:30 -06003151 u8 flags = (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON) ?
3152 0 : ND_ARS_RETURN_PREV_DATA;
Toshi Kani56b47fe2017-06-08 12:36:57 -06003153
Toshi Kani80790032017-06-29 20:41:30 -06003154 acpi_nfit_ars_rescan(acpi_desc, flags);
Toshi Kani56b47fe2017-06-08 12:36:57 -06003155}
3156
3157void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3158{
3159 dev_dbg(dev, "%s: event: 0x%x\n", __func__, event);
3160
3161 switch (event) {
3162 case NFIT_NOTIFY_UPDATE:
3163 return acpi_nfit_update_notify(dev, handle);
3164 case NFIT_NOTIFY_UC_MEMORY_ERROR:
3165 return acpi_nfit_uc_error_notify(dev, handle);
3166 default:
3167 return;
3168 }
3169}
Dan Williamsc14a8682016-08-18 22:15:04 -07003170EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06003171
Dan Williamsc14a8682016-08-18 22:15:04 -07003172static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3173{
3174 device_lock(&adev->dev);
3175 __acpi_nfit_notify(&adev->dev, adev->handle, event);
3176 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06003177}
3178
Dan Williamsb94d5232015-05-19 22:54:31 -04003179static const struct acpi_device_id acpi_nfit_ids[] = {
3180 { "ACPI0012", 0 },
3181 { "", 0 },
3182};
3183MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3184
3185static struct acpi_driver acpi_nfit_driver = {
3186 .name = KBUILD_MODNAME,
3187 .ids = acpi_nfit_ids,
3188 .ops = {
3189 .add = acpi_nfit_add,
3190 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06003191 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04003192 },
3193};
3194
3195static __init int nfit_init(void)
3196{
Prarit Bhargava7e700d22017-05-31 13:32:00 -04003197 int ret;
3198
Dan Williamsb94d5232015-05-19 22:54:31 -04003199 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3200 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3201 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3202 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3203 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3204 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3205 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3206
Andy Shevchenko41c8bdb2017-06-05 19:40:42 +03003207 guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3208 guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3209 guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3210 guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3211 guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3212 guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3213 guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3214 guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3215 guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3216 guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3217 guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3218 guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3219 guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04003220
Dan Williams7ae0fa432016-02-19 12:16:34 -08003221 nfit_wq = create_singlethread_workqueue("nfit");
3222 if (!nfit_wq)
3223 return -ENOMEM;
3224
Vishal Verma6839a6d2016-07-23 21:51:21 -07003225 nfit_mce_register();
Prarit Bhargava7e700d22017-05-31 13:32:00 -04003226 ret = acpi_bus_register_driver(&acpi_nfit_driver);
3227 if (ret) {
3228 nfit_mce_unregister();
3229 destroy_workqueue(nfit_wq);
3230 }
Vishal Verma6839a6d2016-07-23 21:51:21 -07003231
Prarit Bhargava7e700d22017-05-31 13:32:00 -04003232 return ret;
3233
Dan Williamsb94d5232015-05-19 22:54:31 -04003234}
3235
3236static __exit void nfit_exit(void)
3237{
Vishal Verma6839a6d2016-07-23 21:51:21 -07003238 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04003239 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003240 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003241 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04003242}
3243
3244module_init(nfit_init);
3245module_exit(nfit_exit);
3246MODULE_LICENSE("GPL v2");
3247MODULE_AUTHOR("Intel Corporation");