blob: 1d54833ade3fd27280b8efc1d37f0f46fc36f87c [file] [log] [blame]
Dan Williamsb94d5232015-05-19 22:54:31 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/list_sort.h>
14#include <linux/libnvdimm.h>
15#include <linux/module.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040016#include <linux/mutex.h>
Dan Williams62232e452015-06-08 14:27:06 -040017#include <linux/ndctl.h>
Vishal Verma37b137f2016-07-23 21:51:42 -070018#include <linux/sysfs.h>
Vishal Verma0caeef62015-12-24 19:21:43 -070019#include <linux/delay.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040020#include <linux/list.h>
21#include <linux/acpi.h>
Dan Williamseaf96152015-05-01 13:11:27 -040022#include <linux/sort.h>
Ross Zwislerc2ad2952015-07-10 11:06:13 -060023#include <linux/pmem.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040024#include <linux/io.h>
Dan Williams1cf03c02016-02-17 13:01:23 -080025#include <linux/nd.h>
Dan Williams96601ad2015-08-24 18:29:38 -040026#include <asm/cacheflush.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040027#include "nfit.h"
28
Ross Zwisler047fc8a2015-06-25 04:21:02 -040029/*
30 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
31 * irrelevant.
32 */
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020033#include <linux/io-64-nonatomic-hi-lo.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040034
Dan Williams4d88a972015-05-31 14:41:48 -040035static bool force_enable_dimms;
36module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
37MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
38
Dan Williams1cf03c02016-02-17 13:01:23 -080039static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
40module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
41MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
42
43/* after three payloads of overflow, it's dead jim */
44static unsigned int scrub_overflow_abort = 3;
45module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
46MODULE_PARM_DESC(scrub_overflow_abort,
47 "Number of times we overflow ARS results before abort");
48
Dan Williams87554092016-04-28 18:01:20 -070049static bool disable_vendor_specific;
50module_param(disable_vendor_specific, bool, S_IRUGO);
51MODULE_PARM_DESC(disable_vendor_specific,
Linda Knippersf2668fa2017-03-07 16:35:14 -050052 "Limit commands to the publicly specified set");
Dan Williams87554092016-04-28 18:01:20 -070053
Linda Knippers095ab4b2017-03-07 16:35:12 -050054static unsigned long override_dsm_mask;
55module_param(override_dsm_mask, ulong, S_IRUGO);
56MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
57
Linda Knippersba650cf2017-03-07 16:35:13 -050058static int default_dsm_family = -1;
59module_param(default_dsm_family, int, S_IRUGO);
60MODULE_PARM_DESC(default_dsm_family,
61 "Try this DSM type first when identifying NVDIMM family");
62
Vishal Verma6839a6d2016-07-23 21:51:21 -070063LIST_HEAD(acpi_descs);
64DEFINE_MUTEX(acpi_desc_lock);
65
Dan Williams7ae0fa432016-02-19 12:16:34 -080066static struct workqueue_struct *nfit_wq;
67
Vishal Verma20985162015-10-27 16:58:27 -060068struct nfit_table_prev {
69 struct list_head spas;
70 struct list_head memdevs;
71 struct list_head dcrs;
72 struct list_head bdws;
73 struct list_head idts;
74 struct list_head flushes;
75};
76
Dan Williamsb94d5232015-05-19 22:54:31 -040077static u8 nfit_uuid[NFIT_UUID_MAX][16];
78
Dan Williams6bc75612015-06-17 17:23:32 -040079const u8 *to_nfit_uuid(enum nfit_uuids id)
Dan Williamsb94d5232015-05-19 22:54:31 -040080{
81 return nfit_uuid[id];
82}
Dan Williams6bc75612015-06-17 17:23:32 -040083EXPORT_SYMBOL(to_nfit_uuid);
Dan Williamsb94d5232015-05-19 22:54:31 -040084
Dan Williams62232e452015-06-08 14:27:06 -040085static struct acpi_nfit_desc *to_acpi_nfit_desc(
86 struct nvdimm_bus_descriptor *nd_desc)
87{
88 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
89}
90
91static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
92{
93 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
94
95 /*
96 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
97 * acpi_device.
98 */
99 if (!nd_desc->provider_name
100 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
101 return NULL;
102
103 return to_acpi_device(acpi_desc->dev);
104}
105
Dan Williamsd6eb2702016-12-06 15:06:55 -0800106static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
Dan Williamsaef25332016-02-12 17:01:11 -0800107{
Dan Williamsd4f32362016-03-03 16:08:54 -0800108 struct nd_cmd_clear_error *clear_err;
Dan Williamsaef25332016-02-12 17:01:11 -0800109 struct nd_cmd_ars_status *ars_status;
Dan Williamsaef25332016-02-12 17:01:11 -0800110 u16 flags;
111
112 switch (cmd) {
113 case ND_CMD_ARS_CAP:
Dan Williams11294d62016-09-21 09:21:26 -0700114 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800115 return -ENOTTY;
116
117 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700118 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800119 return -EIO;
120
121 /* No supported scan types for this range */
122 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
Dan Williams11294d62016-09-21 09:21:26 -0700123 if ((status >> 16 & flags) == 0)
Dan Williamsaef25332016-02-12 17:01:11 -0800124 return -ENOTTY;
Vishal Verma9a901f52016-12-05 17:00:37 -0700125 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800126 case ND_CMD_ARS_START:
Dan Williamsaef25332016-02-12 17:01:11 -0800127 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700128 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800129 return -EBUSY;
130
131 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700132 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800133 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700134 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800135 case ND_CMD_ARS_STATUS:
136 ars_status = buf;
137 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700138 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800139 return -EIO;
140 /* Check extended status (Upper two bytes) */
Dan Williams11294d62016-09-21 09:21:26 -0700141 if (status == NFIT_ARS_STATUS_DONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800142 return 0;
143
144 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700145 if (status == NFIT_ARS_STATUS_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800146 return -EBUSY;
147
148 /* No ARS performed for the current boot */
Dan Williams11294d62016-09-21 09:21:26 -0700149 if (status == NFIT_ARS_STATUS_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800150 return -EAGAIN;
151
152 /*
153 * ARS interrupted, either we overflowed or some other
154 * agent wants the scan to stop. If we didn't overflow
155 * then just continue with the returned results.
156 */
Dan Williams11294d62016-09-21 09:21:26 -0700157 if (status == NFIT_ARS_STATUS_INTR) {
Dan Williams82aa37c2016-12-06 12:45:24 -0800158 if (ars_status->out_length >= 40 && (ars_status->flags
159 & NFIT_ARS_F_OVERFLOW))
Dan Williamsaef25332016-02-12 17:01:11 -0800160 return -ENOSPC;
161 return 0;
162 }
163
164 /* Unknown status */
Dan Williams11294d62016-09-21 09:21:26 -0700165 if (status >> 16)
Dan Williamsaef25332016-02-12 17:01:11 -0800166 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700167 return 0;
Dan Williamsd4f32362016-03-03 16:08:54 -0800168 case ND_CMD_CLEAR_ERROR:
169 clear_err = buf;
Dan Williams11294d62016-09-21 09:21:26 -0700170 if (status & 0xffff)
Dan Williamsd4f32362016-03-03 16:08:54 -0800171 return -EIO;
172 if (!clear_err->cleared)
173 return -EIO;
174 if (clear_err->length > clear_err->cleared)
175 return clear_err->cleared;
Vishal Verma9a901f52016-12-05 17:00:37 -0700176 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800177 default:
178 break;
179 }
180
Dan Williams11294d62016-09-21 09:21:26 -0700181 /* all other non-zero status results in an error */
182 if (status)
183 return -EIO;
Dan Williamsaef25332016-02-12 17:01:11 -0800184 return 0;
185}
186
Dan Williamsd6eb2702016-12-06 15:06:55 -0800187static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
188 u32 status)
189{
190 if (!nvdimm)
191 return xlat_bus_status(buf, cmd, status);
192 if (status)
193 return -EIO;
194 return 0;
195}
196
Dan Williamsa7de92d2016-12-05 13:43:25 -0800197int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
198 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400199{
Dan Williams62232e452015-06-08 14:27:06 -0400200 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams62232e452015-06-08 14:27:06 -0400201 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700202 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400203 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700204 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400205 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700206 unsigned long cmd_mask, dsm_mask;
Dan Williams11294d62016-09-21 09:21:26 -0700207 u32 offset, fw_status = 0;
Dan Williams62232e452015-06-08 14:27:06 -0400208 acpi_handle handle;
Dan Williams31eca762016-04-28 16:23:43 -0700209 unsigned int func;
Dan Williams62232e452015-06-08 14:27:06 -0400210 const u8 *uuid;
Dan Williams62232e452015-06-08 14:27:06 -0400211 int rc, i;
212
Dan Williams31eca762016-04-28 16:23:43 -0700213 func = cmd;
214 if (cmd == ND_CMD_CALL) {
215 call_pkg = buf;
216 func = call_pkg->nd_command;
217 }
218
Dan Williams62232e452015-06-08 14:27:06 -0400219 if (nvdimm) {
220 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
221 struct acpi_device *adev = nfit_mem->adev;
222
223 if (!adev)
224 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700225 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
226 return -ENOTTY;
227
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400228 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400229 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700230 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400231 dsm_mask = nfit_mem->dsm_mask;
232 desc = nd_cmd_dimm_desc(cmd);
Dan Williams31eca762016-04-28 16:23:43 -0700233 uuid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400234 handle = adev->handle;
235 } else {
236 struct acpi_device *adev = to_acpi_dev(acpi_desc);
237
238 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700239 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700240 dsm_mask = cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400241 desc = nd_cmd_bus_desc(cmd);
242 uuid = to_nfit_uuid(NFIT_DEV_BUS);
243 handle = adev->handle;
244 dimm_name = "bus";
245 }
246
247 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
248 return -ENOTTY;
249
Dan Williams31eca762016-04-28 16:23:43 -0700250 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400251 return -ENOTTY;
252
253 in_obj.type = ACPI_TYPE_PACKAGE;
254 in_obj.package.count = 1;
255 in_obj.package.elements = &in_buf;
256 in_buf.type = ACPI_TYPE_BUFFER;
257 in_buf.buffer.pointer = buf;
258 in_buf.buffer.length = 0;
259
260 /* libnvdimm has already validated the input envelope */
261 for (i = 0; i < desc->in_num; i++)
262 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
263 i, buf);
264
Dan Williams31eca762016-04-28 16:23:43 -0700265 if (call_pkg) {
266 /* skip over package wrapper */
267 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
268 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400269 }
270
Dan Williams31eca762016-04-28 16:23:43 -0700271 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
272 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
273 __func__, dimm_name, cmd, func,
274 in_buf.buffer.length);
275 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
276 in_buf.buffer.pointer,
277 min_t(u32, 256, in_buf.buffer.length), true);
278 }
279
280 out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj);
Dan Williams62232e452015-06-08 14:27:06 -0400281 if (!out_obj) {
282 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
283 cmd_name);
284 return -EINVAL;
285 }
286
Dan Williams31eca762016-04-28 16:23:43 -0700287 if (call_pkg) {
288 call_pkg->nd_fw_size = out_obj->buffer.length;
289 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
290 out_obj->buffer.pointer,
291 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
292
293 ACPI_FREE(out_obj);
294 /*
295 * Need to support FW function w/o known size in advance.
296 * Caller can determine required size based upon nd_fw_size.
297 * If we return an error (like elsewhere) then caller wouldn't
298 * be able to rely upon data returned to make calculation.
299 */
300 return 0;
301 }
302
Dan Williams62232e452015-06-08 14:27:06 -0400303 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
304 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
305 __func__, dimm_name, cmd_name, out_obj->type);
306 rc = -EINVAL;
307 goto out;
308 }
309
310 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
311 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
312 dimm_name, cmd_name, out_obj->buffer.length);
313 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
314 4, out_obj->buffer.pointer, min_t(u32, 128,
315 out_obj->buffer.length), true);
316 }
317
318 for (i = 0, offset = 0; i < desc->out_num; i++) {
319 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800320 (u32 *) out_obj->buffer.pointer,
321 out_obj->buffer.length - offset);
Dan Williams62232e452015-06-08 14:27:06 -0400322
323 if (offset + out_size > out_obj->buffer.length) {
324 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
325 __func__, dimm_name, cmd_name, i);
326 break;
327 }
328
329 if (in_buf.buffer.length + offset + out_size > buf_len) {
330 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
331 __func__, dimm_name, cmd_name, i);
332 rc = -ENXIO;
333 goto out;
334 }
335 memcpy(buf + in_buf.buffer.length + offset,
336 out_obj->buffer.pointer + offset, out_size);
337 offset += out_size;
338 }
Dan Williams11294d62016-09-21 09:21:26 -0700339
340 /*
341 * Set fw_status for all the commands with a known format to be
342 * later interpreted by xlat_status().
343 */
344 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
345 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
346 fw_status = *(u32 *) out_obj->buffer.pointer;
347
Dan Williams62232e452015-06-08 14:27:06 -0400348 if (offset + in_buf.buffer.length < buf_len) {
349 if (i >= 1) {
350 /*
351 * status valid, return the number of bytes left
352 * unfilled in the output buffer
353 */
354 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800355 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800356 *cmd_rc = xlat_status(nvdimm, buf, cmd,
357 fw_status);
Dan Williams62232e452015-06-08 14:27:06 -0400358 } else {
359 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
360 __func__, dimm_name, cmd_name, buf_len,
361 offset);
362 rc = -ENXIO;
363 }
Dan Williams2eea6582016-05-02 09:11:53 -0700364 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400365 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700366 if (cmd_rc)
Dan Williamsd6eb2702016-12-06 15:06:55 -0800367 *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
Dan Williams2eea6582016-05-02 09:11:53 -0700368 }
Dan Williams62232e452015-06-08 14:27:06 -0400369
370 out:
371 ACPI_FREE(out_obj);
372
373 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400374}
Dan Williamsa7de92d2016-12-05 13:43:25 -0800375EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
Dan Williamsb94d5232015-05-19 22:54:31 -0400376
377static const char *spa_type_name(u16 type)
378{
379 static const char *to_name[] = {
380 [NFIT_SPA_VOLATILE] = "volatile",
381 [NFIT_SPA_PM] = "pmem",
382 [NFIT_SPA_DCR] = "dimm-control-region",
383 [NFIT_SPA_BDW] = "block-data-window",
384 [NFIT_SPA_VDISK] = "volatile-disk",
385 [NFIT_SPA_VCD] = "volatile-cd",
386 [NFIT_SPA_PDISK] = "persistent-disk",
387 [NFIT_SPA_PCD] = "persistent-cd",
388
389 };
390
391 if (type > NFIT_SPA_PCD)
392 return "unknown";
393
394 return to_name[type];
395}
396
Vishal Verma6839a6d2016-07-23 21:51:21 -0700397int nfit_spa_type(struct acpi_nfit_system_address *spa)
Dan Williamsb94d5232015-05-19 22:54:31 -0400398{
399 int i;
400
401 for (i = 0; i < NFIT_UUID_MAX; i++)
402 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
403 return i;
404 return -1;
405}
406
407static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600408 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400409 struct acpi_nfit_system_address *spa)
410{
411 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600412 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400413
Dan Williams31932042016-07-14 17:22:48 -0700414 if (spa->header.length != sizeof(*spa))
415 return false;
416
Vishal Verma20985162015-10-27 16:58:27 -0600417 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700418 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600419 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
420 return true;
421 }
422 }
423
Dan Williams31932042016-07-14 17:22:48 -0700424 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
425 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400426 if (!nfit_spa)
427 return false;
428 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700429 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400430 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
431 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
432 spa->range_index,
433 spa_type_name(nfit_spa_type(spa)));
434 return true;
435}
436
437static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600438 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400439 struct acpi_nfit_memory_map *memdev)
440{
441 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600442 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400443
Dan Williams31932042016-07-14 17:22:48 -0700444 if (memdev->header.length != sizeof(*memdev))
445 return false;
446
Vishal Verma20985162015-10-27 16:58:27 -0600447 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700448 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600449 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
450 return true;
451 }
452
Dan Williams31932042016-07-14 17:22:48 -0700453 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
454 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400455 if (!nfit_memdev)
456 return false;
457 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700458 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400459 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
Dan Williamscaa603a2017-04-14 10:27:11 -0700460 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
Dan Williamsb94d5232015-05-19 22:54:31 -0400461 __func__, memdev->device_handle, memdev->range_index,
Dan Williamscaa603a2017-04-14 10:27:11 -0700462 memdev->region_index, memdev->flags);
Dan Williamsb94d5232015-05-19 22:54:31 -0400463 return true;
464}
465
Dan Williams31932042016-07-14 17:22:48 -0700466/*
467 * An implementation may provide a truncated control region if no block windows
468 * are defined.
469 */
470static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
471{
472 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
473 window_size))
474 return 0;
475 if (dcr->windows)
476 return sizeof(*dcr);
477 return offsetof(struct acpi_nfit_control_region, window_size);
478}
479
Dan Williamsb94d5232015-05-19 22:54:31 -0400480static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600481 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400482 struct acpi_nfit_control_region *dcr)
483{
484 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600485 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400486
Dan Williams31932042016-07-14 17:22:48 -0700487 if (!sizeof_dcr(dcr))
488 return false;
489
Vishal Verma20985162015-10-27 16:58:27 -0600490 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700491 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600492 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
493 return true;
494 }
495
Dan Williams31932042016-07-14 17:22:48 -0700496 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
497 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400498 if (!nfit_dcr)
499 return false;
500 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700501 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400502 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
503 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
504 dcr->region_index, dcr->windows);
505 return true;
506}
507
508static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600509 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400510 struct acpi_nfit_data_region *bdw)
511{
512 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600513 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400514
Dan Williams31932042016-07-14 17:22:48 -0700515 if (bdw->header.length != sizeof(*bdw))
516 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600517 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700518 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600519 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
520 return true;
521 }
522
Dan Williams31932042016-07-14 17:22:48 -0700523 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
524 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400525 if (!nfit_bdw)
526 return false;
527 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700528 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400529 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
530 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
531 bdw->region_index, bdw->windows);
532 return true;
533}
534
Dan Williams31932042016-07-14 17:22:48 -0700535static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
536{
537 if (idt->header.length < sizeof(*idt))
538 return 0;
539 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
540}
541
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400542static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600543 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400544 struct acpi_nfit_interleave *idt)
545{
546 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600547 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400548
Dan Williams31932042016-07-14 17:22:48 -0700549 if (!sizeof_idt(idt))
550 return false;
551
552 list_for_each_entry(nfit_idt, &prev->idts, list) {
553 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
554 continue;
555
556 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600557 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
558 return true;
559 }
Dan Williams31932042016-07-14 17:22:48 -0700560 }
Vishal Verma20985162015-10-27 16:58:27 -0600561
Dan Williams31932042016-07-14 17:22:48 -0700562 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
563 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400564 if (!nfit_idt)
565 return false;
566 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700567 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400568 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
569 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
570 idt->interleave_index, idt->line_count);
571 return true;
572}
573
Dan Williams31932042016-07-14 17:22:48 -0700574static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
575{
576 if (flush->header.length < sizeof(*flush))
577 return 0;
578 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
579}
580
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600581static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600582 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600583 struct acpi_nfit_flush_address *flush)
584{
585 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600586 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600587
Dan Williams31932042016-07-14 17:22:48 -0700588 if (!sizeof_flush(flush))
589 return false;
590
591 list_for_each_entry(nfit_flush, &prev->flushes, list) {
592 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
593 continue;
594
595 if (memcmp(nfit_flush->flush, flush,
596 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600597 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
598 return true;
599 }
Dan Williams31932042016-07-14 17:22:48 -0700600 }
Vishal Verma20985162015-10-27 16:58:27 -0600601
Dan Williams31932042016-07-14 17:22:48 -0700602 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
603 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600604 if (!nfit_flush)
605 return false;
606 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700607 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600608 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
609 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
610 flush->device_handle, flush->hint_count);
611 return true;
612}
613
Vishal Verma20985162015-10-27 16:58:27 -0600614static void *add_table(struct acpi_nfit_desc *acpi_desc,
615 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400616{
617 struct device *dev = acpi_desc->dev;
618 struct acpi_nfit_header *hdr;
619 void *err = ERR_PTR(-ENOMEM);
620
621 if (table >= end)
622 return NULL;
623
624 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600625 if (!hdr->length) {
626 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
627 hdr->type);
628 return NULL;
629 }
630
Dan Williamsb94d5232015-05-19 22:54:31 -0400631 switch (hdr->type) {
632 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600633 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400634 return err;
635 break;
636 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600637 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400638 return err;
639 break;
640 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600641 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400642 return err;
643 break;
644 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600645 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400646 return err;
647 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400648 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600649 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400650 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400651 break;
652 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600653 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600654 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400655 break;
656 case ACPI_NFIT_TYPE_SMBIOS:
657 dev_dbg(dev, "%s: smbios\n", __func__);
658 break;
659 default:
660 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
661 break;
662 }
663
664 return table + hdr->length;
665}
666
667static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
668 struct nfit_mem *nfit_mem)
669{
670 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
671 u16 dcr = nfit_mem->dcr->region_index;
672 struct nfit_spa *nfit_spa;
673
674 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
675 u16 range_index = nfit_spa->spa->range_index;
676 int type = nfit_spa_type(nfit_spa->spa);
677 struct nfit_memdev *nfit_memdev;
678
679 if (type != NFIT_SPA_BDW)
680 continue;
681
682 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
683 if (nfit_memdev->memdev->range_index != range_index)
684 continue;
685 if (nfit_memdev->memdev->device_handle != device_handle)
686 continue;
687 if (nfit_memdev->memdev->region_index != dcr)
688 continue;
689
690 nfit_mem->spa_bdw = nfit_spa->spa;
691 return;
692 }
693 }
694
695 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
696 nfit_mem->spa_dcr->range_index);
697 nfit_mem->bdw = NULL;
698}
699
Dan Williams6697b2c2016-02-04 16:51:00 -0800700static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400701 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
702{
703 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400704 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400705 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400706 struct nfit_idt *nfit_idt;
707 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400708
Dan Williamsb94d5232015-05-19 22:54:31 -0400709 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
710 if (nfit_bdw->bdw->region_index != dcr)
711 continue;
712 nfit_mem->bdw = nfit_bdw->bdw;
713 break;
714 }
715
716 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800717 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400718
719 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400720
721 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800722 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400723
724 range_index = nfit_mem->spa_bdw->range_index;
725 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
726 if (nfit_memdev->memdev->range_index != range_index ||
727 nfit_memdev->memdev->region_index != dcr)
728 continue;
729 nfit_mem->memdev_bdw = nfit_memdev->memdev;
730 idt_idx = nfit_memdev->memdev->interleave_index;
731 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
732 if (nfit_idt->idt->interleave_index != idt_idx)
733 continue;
734 nfit_mem->idt_bdw = nfit_idt->idt;
735 break;
736 }
737 break;
738 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400739}
740
Dan Williams14999342017-04-13 19:46:36 -0700741static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400742 struct acpi_nfit_system_address *spa)
743{
744 struct nfit_mem *nfit_mem, *found;
745 struct nfit_memdev *nfit_memdev;
Dan Williams14999342017-04-13 19:46:36 -0700746 int type = spa ? nfit_spa_type(spa) : 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400747
748 switch (type) {
749 case NFIT_SPA_DCR:
750 case NFIT_SPA_PM:
751 break;
752 default:
Dan Williams14999342017-04-13 19:46:36 -0700753 if (spa)
754 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -0400755 }
756
Dan Williams14999342017-04-13 19:46:36 -0700757 /*
758 * This loop runs in two modes, when a dimm is mapped the loop
759 * adds memdev associations to an existing dimm, or creates a
760 * dimm. In the unmapped dimm case this loop sweeps for memdev
761 * instances with an invalid / zero range_index and adds those
762 * dimms without spa associations.
763 */
Dan Williamsb94d5232015-05-19 22:54:31 -0400764 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700765 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800766 struct nfit_dcr *nfit_dcr;
767 u32 device_handle;
768 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400769
Dan Williams14999342017-04-13 19:46:36 -0700770 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
771 continue;
772 if (!spa && nfit_memdev->memdev->range_index)
Dan Williamsb94d5232015-05-19 22:54:31 -0400773 continue;
774 found = NULL;
775 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800776 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400777 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800778 if (__to_nfit_memdev(nfit_mem)->device_handle
779 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400780 found = nfit_mem;
781 break;
782 }
783
784 if (found)
785 nfit_mem = found;
786 else {
787 nfit_mem = devm_kzalloc(acpi_desc->dev,
788 sizeof(*nfit_mem), GFP_KERNEL);
789 if (!nfit_mem)
790 return -ENOMEM;
791 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700792 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800793 list_add(&nfit_mem->list, &acpi_desc->dimms);
794 }
795
796 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
797 if (nfit_dcr->dcr->region_index != dcr)
798 continue;
799 /*
800 * Record the control region for the dimm. For
801 * the ACPI 6.1 case, where there are separate
802 * control regions for the pmem vs blk
803 * interfaces, be sure to record the extended
804 * blk details.
805 */
806 if (!nfit_mem->dcr)
807 nfit_mem->dcr = nfit_dcr->dcr;
808 else if (nfit_mem->dcr->windows == 0
809 && nfit_dcr->dcr->windows)
810 nfit_mem->dcr = nfit_dcr->dcr;
811 break;
812 }
813
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700814 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700815 struct acpi_nfit_flush_address *flush;
816 u16 i;
817
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700818 if (nfit_flush->flush->device_handle != device_handle)
819 continue;
820 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700821 flush = nfit_flush->flush;
822 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
823 flush->hint_count
824 * sizeof(struct resource), GFP_KERNEL);
825 if (!nfit_mem->flush_wpq)
826 return -ENOMEM;
827 for (i = 0; i < flush->hint_count; i++) {
828 struct resource *res = &nfit_mem->flush_wpq[i];
829
830 res->start = flush->hint_address[i];
831 res->end = res->start + 8 - 1;
832 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700833 break;
834 }
835
Dan Williams6697b2c2016-02-04 16:51:00 -0800836 if (dcr && !nfit_mem->dcr) {
837 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
838 spa->range_index, dcr);
839 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400840 }
841
842 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400843 struct nfit_idt *nfit_idt;
844 u16 idt_idx;
845
Dan Williamsb94d5232015-05-19 22:54:31 -0400846 /* multiple dimms may share a SPA when interleaved */
847 nfit_mem->spa_dcr = spa;
848 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400849 idt_idx = nfit_memdev->memdev->interleave_index;
850 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
851 if (nfit_idt->idt->interleave_index != idt_idx)
852 continue;
853 nfit_mem->idt_dcr = nfit_idt->idt;
854 break;
855 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800856 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williams14999342017-04-13 19:46:36 -0700857 } else if (type == NFIT_SPA_PM) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400858 /*
859 * A single dimm may belong to multiple SPA-PM
860 * ranges, record at least one in addition to
861 * any SPA-DCR range.
862 */
863 nfit_mem->memdev_pmem = nfit_memdev->memdev;
Dan Williams14999342017-04-13 19:46:36 -0700864 } else
865 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400866 }
867
868 return 0;
869}
870
871static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
872{
873 struct nfit_mem *a = container_of(_a, typeof(*a), list);
874 struct nfit_mem *b = container_of(_b, typeof(*b), list);
875 u32 handleA, handleB;
876
877 handleA = __to_nfit_memdev(a)->device_handle;
878 handleB = __to_nfit_memdev(b)->device_handle;
879 if (handleA < handleB)
880 return -1;
881 else if (handleA > handleB)
882 return 1;
883 return 0;
884}
885
886static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
887{
888 struct nfit_spa *nfit_spa;
Dan Williams14999342017-04-13 19:46:36 -0700889 int rc;
890
Dan Williamsb94d5232015-05-19 22:54:31 -0400891
892 /*
893 * For each SPA-DCR or SPA-PMEM address range find its
894 * corresponding MEMDEV(s). From each MEMDEV find the
895 * corresponding DCR. Then, if we're operating on a SPA-DCR,
896 * try to find a SPA-BDW and a corresponding BDW that references
897 * the DCR. Throw it all into an nfit_mem object. Note, that
898 * BDWs are optional.
899 */
900 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
Dan Williams14999342017-04-13 19:46:36 -0700901 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400902 if (rc)
903 return rc;
904 }
905
Dan Williams14999342017-04-13 19:46:36 -0700906 /*
907 * If a DIMM has failed to be mapped into SPA there will be no
908 * SPA entries above. Find and register all the unmapped DIMMs
909 * for reporting and recovery purposes.
910 */
911 rc = __nfit_mem_init(acpi_desc, NULL);
912 if (rc)
913 return rc;
914
Dan Williamsb94d5232015-05-19 22:54:31 -0400915 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
916
917 return 0;
918}
919
Dan Williams45def222015-04-26 19:26:48 -0400920static ssize_t revision_show(struct device *dev,
921 struct device_attribute *attr, char *buf)
922{
923 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
924 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
925 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
926
Linda Knippers6b577c92015-11-20 19:05:49 -0500927 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400928}
929static DEVICE_ATTR_RO(revision);
930
Vishal Verma9ffd6352016-09-30 17:19:29 -0600931static ssize_t hw_error_scrub_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 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
937
938 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
939}
940
941/*
942 * The 'hw_error_scrub' attribute can have the following values written to it:
943 * '0': Switch to the default mode where an exception will only insert
944 * the address of the memory error into the poison and badblocks lists.
945 * '1': Enable a full scrub to happen if an exception for a memory error is
946 * received.
947 */
948static ssize_t hw_error_scrub_store(struct device *dev,
949 struct device_attribute *attr, const char *buf, size_t size)
950{
951 struct nvdimm_bus_descriptor *nd_desc;
952 ssize_t rc;
953 long val;
954
955 rc = kstrtol(buf, 0, &val);
956 if (rc)
957 return rc;
958
959 device_lock(dev);
960 nd_desc = dev_get_drvdata(dev);
961 if (nd_desc) {
962 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
963
964 switch (val) {
965 case HW_ERROR_SCRUB_ON:
966 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
967 break;
968 case HW_ERROR_SCRUB_OFF:
969 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
970 break;
971 default:
972 rc = -EINVAL;
973 break;
974 }
975 }
976 device_unlock(dev);
977 if (rc)
978 return rc;
979 return size;
980}
981static DEVICE_ATTR_RW(hw_error_scrub);
982
Vishal Verma37b137f2016-07-23 21:51:42 -0700983/*
984 * This shows the number of full Address Range Scrubs that have been
985 * completed since driver load time. Userspace can wait on this using
986 * select/poll etc. A '+' at the end indicates an ARS is in progress
987 */
988static ssize_t scrub_show(struct device *dev,
989 struct device_attribute *attr, char *buf)
990{
991 struct nvdimm_bus_descriptor *nd_desc;
992 ssize_t rc = -ENXIO;
993
994 device_lock(dev);
995 nd_desc = dev_get_drvdata(dev);
996 if (nd_desc) {
997 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
998
999 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1000 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
1001 }
1002 device_unlock(dev);
1003 return rc;
1004}
1005
Vishal Verma37b137f2016-07-23 21:51:42 -07001006static ssize_t scrub_store(struct device *dev,
1007 struct device_attribute *attr, const char *buf, size_t size)
1008{
1009 struct nvdimm_bus_descriptor *nd_desc;
1010 ssize_t rc;
1011 long val;
1012
1013 rc = kstrtol(buf, 0, &val);
1014 if (rc)
1015 return rc;
1016 if (val != 1)
1017 return -EINVAL;
1018
1019 device_lock(dev);
1020 nd_desc = dev_get_drvdata(dev);
1021 if (nd_desc) {
1022 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1023
1024 rc = acpi_nfit_ars_rescan(acpi_desc);
1025 }
1026 device_unlock(dev);
1027 if (rc)
1028 return rc;
1029 return size;
1030}
1031static DEVICE_ATTR_RW(scrub);
1032
1033static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1034{
1035 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1036 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1037 | 1 << ND_CMD_ARS_STATUS;
1038
1039 return (nd_desc->cmd_mask & mask) == mask;
1040}
1041
1042static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1043{
1044 struct device *dev = container_of(kobj, struct device, kobj);
1045 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1046
1047 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1048 return 0;
1049 return a->mode;
1050}
1051
Dan Williams45def222015-04-26 19:26:48 -04001052static struct attribute *acpi_nfit_attributes[] = {
1053 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -07001054 &dev_attr_scrub.attr,
Vishal Verma9ffd6352016-09-30 17:19:29 -06001055 &dev_attr_hw_error_scrub.attr,
Dan Williams45def222015-04-26 19:26:48 -04001056 NULL,
1057};
1058
1059static struct attribute_group acpi_nfit_attribute_group = {
1060 .name = "nfit",
1061 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -07001062 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -04001063};
1064
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001065static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -04001066 &nvdimm_bus_attribute_group,
1067 &acpi_nfit_attribute_group,
1068 NULL,
1069};
1070
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001071static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1072{
1073 struct nvdimm *nvdimm = to_nvdimm(dev);
1074 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1075
1076 return __to_nfit_memdev(nfit_mem);
1077}
1078
1079static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1080{
1081 struct nvdimm *nvdimm = to_nvdimm(dev);
1082 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1083
1084 return nfit_mem->dcr;
1085}
1086
1087static ssize_t handle_show(struct device *dev,
1088 struct device_attribute *attr, char *buf)
1089{
1090 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1091
1092 return sprintf(buf, "%#x\n", memdev->device_handle);
1093}
1094static DEVICE_ATTR_RO(handle);
1095
1096static ssize_t phys_id_show(struct device *dev,
1097 struct device_attribute *attr, char *buf)
1098{
1099 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1100
1101 return sprintf(buf, "%#x\n", memdev->physical_id);
1102}
1103static DEVICE_ATTR_RO(phys_id);
1104
1105static ssize_t vendor_show(struct device *dev,
1106 struct device_attribute *attr, char *buf)
1107{
1108 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1109
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001110 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001111}
1112static DEVICE_ATTR_RO(vendor);
1113
1114static ssize_t rev_id_show(struct device *dev,
1115 struct device_attribute *attr, char *buf)
1116{
1117 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1118
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001119 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001120}
1121static DEVICE_ATTR_RO(rev_id);
1122
1123static ssize_t device_show(struct device *dev,
1124 struct device_attribute *attr, char *buf)
1125{
1126 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1127
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001128 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001129}
1130static DEVICE_ATTR_RO(device);
1131
Dan Williams6ca72082016-04-29 10:33:23 -07001132static ssize_t subsystem_vendor_show(struct device *dev,
1133 struct device_attribute *attr, char *buf)
1134{
1135 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1136
1137 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1138}
1139static DEVICE_ATTR_RO(subsystem_vendor);
1140
1141static ssize_t subsystem_rev_id_show(struct device *dev,
1142 struct device_attribute *attr, char *buf)
1143{
1144 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1145
1146 return sprintf(buf, "0x%04x\n",
1147 be16_to_cpu(dcr->subsystem_revision_id));
1148}
1149static DEVICE_ATTR_RO(subsystem_rev_id);
1150
1151static ssize_t subsystem_device_show(struct device *dev,
1152 struct device_attribute *attr, char *buf)
1153{
1154 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1155
1156 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1157}
1158static DEVICE_ATTR_RO(subsystem_device);
1159
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001160static int num_nvdimm_formats(struct nvdimm *nvdimm)
1161{
1162 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1163 int formats = 0;
1164
1165 if (nfit_mem->memdev_pmem)
1166 formats++;
1167 if (nfit_mem->memdev_bdw)
1168 formats++;
1169 return formats;
1170}
1171
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001172static ssize_t format_show(struct device *dev,
1173 struct device_attribute *attr, char *buf)
1174{
1175 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1176
Dan Williams1bcbf422016-06-29 11:19:32 -07001177 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001178}
1179static DEVICE_ATTR_RO(format);
1180
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001181static ssize_t format1_show(struct device *dev,
1182 struct device_attribute *attr, char *buf)
1183{
1184 u32 handle;
1185 ssize_t rc = -ENXIO;
1186 struct nfit_mem *nfit_mem;
1187 struct nfit_memdev *nfit_memdev;
1188 struct acpi_nfit_desc *acpi_desc;
1189 struct nvdimm *nvdimm = to_nvdimm(dev);
1190 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1191
1192 nfit_mem = nvdimm_provider_data(nvdimm);
1193 acpi_desc = nfit_mem->acpi_desc;
1194 handle = to_nfit_memdev(dev)->device_handle;
1195
1196 /* assumes DIMMs have at most 2 published interface codes */
1197 mutex_lock(&acpi_desc->init_mutex);
1198 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1199 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1200 struct nfit_dcr *nfit_dcr;
1201
1202 if (memdev->device_handle != handle)
1203 continue;
1204
1205 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1206 if (nfit_dcr->dcr->region_index != memdev->region_index)
1207 continue;
1208 if (nfit_dcr->dcr->code == dcr->code)
1209 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001210 rc = sprintf(buf, "0x%04x\n",
1211 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001212 break;
1213 }
1214 if (rc != ENXIO)
1215 break;
1216 }
1217 mutex_unlock(&acpi_desc->init_mutex);
1218 return rc;
1219}
1220static DEVICE_ATTR_RO(format1);
1221
1222static ssize_t formats_show(struct device *dev,
1223 struct device_attribute *attr, char *buf)
1224{
1225 struct nvdimm *nvdimm = to_nvdimm(dev);
1226
1227 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1228}
1229static DEVICE_ATTR_RO(formats);
1230
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001231static ssize_t serial_show(struct device *dev,
1232 struct device_attribute *attr, char *buf)
1233{
1234 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1235
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001236 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001237}
1238static DEVICE_ATTR_RO(serial);
1239
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001240static ssize_t family_show(struct device *dev,
1241 struct device_attribute *attr, char *buf)
1242{
1243 struct nvdimm *nvdimm = to_nvdimm(dev);
1244 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1245
1246 if (nfit_mem->family < 0)
1247 return -ENXIO;
1248 return sprintf(buf, "%d\n", nfit_mem->family);
1249}
1250static DEVICE_ATTR_RO(family);
1251
1252static ssize_t dsm_mask_show(struct device *dev,
1253 struct device_attribute *attr, char *buf)
1254{
1255 struct nvdimm *nvdimm = to_nvdimm(dev);
1256 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1257
1258 if (nfit_mem->family < 0)
1259 return -ENXIO;
1260 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1261}
1262static DEVICE_ATTR_RO(dsm_mask);
1263
Dan Williams58138822015-06-23 20:08:34 -04001264static ssize_t flags_show(struct device *dev,
1265 struct device_attribute *attr, char *buf)
1266{
1267 u16 flags = to_nfit_memdev(dev)->flags;
1268
Dan Williamsffab9382017-04-13 15:05:30 -07001269 return sprintf(buf, "%s%s%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001270 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1271 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1272 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001273 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Dan Williamsffab9382017-04-13 15:05:30 -07001274 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1275 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1276 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
Dan Williams58138822015-06-23 20:08:34 -04001277}
1278static DEVICE_ATTR_RO(flags);
1279
Toshi Kani38a879b2016-04-25 15:34:59 -06001280static ssize_t id_show(struct device *dev,
1281 struct device_attribute *attr, char *buf)
1282{
1283 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1284
1285 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1286 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1287 be16_to_cpu(dcr->vendor_id),
1288 dcr->manufacturing_location,
1289 be16_to_cpu(dcr->manufacturing_date),
1290 be32_to_cpu(dcr->serial_number));
1291 else
1292 return sprintf(buf, "%04x-%08x\n",
1293 be16_to_cpu(dcr->vendor_id),
1294 be32_to_cpu(dcr->serial_number));
1295}
1296static DEVICE_ATTR_RO(id);
1297
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001298static struct attribute *acpi_nfit_dimm_attributes[] = {
1299 &dev_attr_handle.attr,
1300 &dev_attr_phys_id.attr,
1301 &dev_attr_vendor.attr,
1302 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001303 &dev_attr_rev_id.attr,
1304 &dev_attr_subsystem_vendor.attr,
1305 &dev_attr_subsystem_device.attr,
1306 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001307 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001308 &dev_attr_formats.attr,
1309 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001310 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001311 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001312 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001313 &dev_attr_family.attr,
1314 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001315 NULL,
1316};
1317
1318static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1319 struct attribute *a, int n)
1320{
1321 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001322 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001323
Dan Williams14999342017-04-13 19:46:36 -07001324 if (!to_nfit_dcr(dev)) {
1325 /* Without a dcr only the memdev attributes can be surfaced */
1326 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1327 || a == &dev_attr_flags.attr
1328 || a == &dev_attr_family.attr
1329 || a == &dev_attr_dsm_mask.attr)
1330 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001331 return 0;
Dan Williams14999342017-04-13 19:46:36 -07001332 }
1333
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001334 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1335 return 0;
1336 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001337}
1338
1339static struct attribute_group acpi_nfit_dimm_attribute_group = {
1340 .name = "nfit",
1341 .attrs = acpi_nfit_dimm_attributes,
1342 .is_visible = acpi_nfit_dimm_attr_visible,
1343};
1344
1345static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001346 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001347 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001348 &acpi_nfit_dimm_attribute_group,
1349 NULL,
1350};
1351
1352static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1353 u32 device_handle)
1354{
1355 struct nfit_mem *nfit_mem;
1356
1357 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1358 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1359 return nfit_mem->nvdimm;
1360
1361 return NULL;
1362}
1363
Dan Williams231bf112016-08-22 19:23:25 -07001364void __acpi_nvdimm_notify(struct device *dev, u32 event)
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001365{
1366 struct nfit_mem *nfit_mem;
1367 struct acpi_nfit_desc *acpi_desc;
1368
1369 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1370 event);
1371
1372 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1373 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1374 event);
1375 return;
1376 }
1377
1378 acpi_desc = dev_get_drvdata(dev->parent);
1379 if (!acpi_desc)
1380 return;
1381
1382 /*
1383 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1384 * is still valid.
1385 */
1386 nfit_mem = dev_get_drvdata(dev);
1387 if (nfit_mem && nfit_mem->flags_attr)
1388 sysfs_notify_dirent(nfit_mem->flags_attr);
1389}
Dan Williams231bf112016-08-22 19:23:25 -07001390EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001391
1392static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1393{
1394 struct acpi_device *adev = data;
1395 struct device *dev = &adev->dev;
1396
1397 device_lock(dev->parent);
1398 __acpi_nvdimm_notify(dev, event);
1399 device_unlock(dev->parent);
1400}
1401
Dan Williams62232e452015-06-08 14:27:06 -04001402static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1403 struct nfit_mem *nfit_mem, u32 device_handle)
1404{
1405 struct acpi_device *adev, *adev_dimm;
1406 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001407 unsigned long dsm_mask;
1408 const u8 *uuid;
Linda Knippers60e95f42015-07-22 16:17:22 -04001409 int i;
Linda Knippersba650cf2017-03-07 16:35:13 -05001410 int family = -1;
Dan Williams62232e452015-06-08 14:27:06 -04001411
Dan Williamse3654ec2016-04-28 16:17:07 -07001412 /* nfit test assumes 1:1 relationship between commands and dsms */
1413 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001414 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001415 adev = to_acpi_dev(acpi_desc);
1416 if (!adev)
1417 return 0;
1418
1419 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1420 nfit_mem->adev = adev_dimm;
1421 if (!adev_dimm) {
1422 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1423 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001424 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001425 }
1426
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001427 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1428 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1429 dev_err(dev, "%s: notification registration failed\n",
1430 dev_name(&adev_dimm->dev));
1431 return -ENXIO;
1432 }
1433
Dan Williams31eca762016-04-28 16:23:43 -07001434 /*
stuart hayese02fb722016-05-26 11:38:41 -05001435 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001436 * different command sets. Note, that checking for function0 (bit0)
1437 * tells us if any commands are reachable through this uuid.
Dan Williams31eca762016-04-28 16:23:43 -07001438 */
stuart hayese02fb722016-05-26 11:38:41 -05001439 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001440 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Linda Knippersba650cf2017-03-07 16:35:13 -05001441 if (family < 0 || i == default_dsm_family)
1442 family = i;
Dan Williams31eca762016-04-28 16:23:43 -07001443
1444 /* limit the supported commands to those that are publicly documented */
Linda Knippersba650cf2017-03-07 16:35:13 -05001445 nfit_mem->family = family;
Linda Knippers095ab4b2017-03-07 16:35:12 -05001446 if (override_dsm_mask && !disable_vendor_specific)
1447 dsm_mask = override_dsm_mask;
1448 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001449 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001450 if (disable_vendor_specific)
1451 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001452 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001453 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001454 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001455 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001456 if (disable_vendor_specific)
1457 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001458 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1459 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001460 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001461 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001462 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001463 /* DSMs are optional, continue loading the driver... */
1464 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001465 }
1466
1467 uuid = to_nfit_uuid(nfit_mem->family);
1468 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Dan Williams62232e452015-06-08 14:27:06 -04001469 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
1470 set_bit(i, &nfit_mem->dsm_mask);
1471
Linda Knippers60e95f42015-07-22 16:17:22 -04001472 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001473}
1474
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001475static void shutdown_dimm_notify(void *data)
1476{
1477 struct acpi_nfit_desc *acpi_desc = data;
1478 struct nfit_mem *nfit_mem;
1479
1480 mutex_lock(&acpi_desc->init_mutex);
1481 /*
1482 * Clear out the nfit_mem->flags_attr and shut down dimm event
1483 * notifications.
1484 */
1485 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williams231bf112016-08-22 19:23:25 -07001486 struct acpi_device *adev_dimm = nfit_mem->adev;
1487
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001488 if (nfit_mem->flags_attr) {
1489 sysfs_put(nfit_mem->flags_attr);
1490 nfit_mem->flags_attr = NULL;
1491 }
Dan Williams231bf112016-08-22 19:23:25 -07001492 if (adev_dimm)
1493 acpi_remove_notify_handler(adev_dimm->handle,
1494 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001495 }
1496 mutex_unlock(&acpi_desc->init_mutex);
1497}
1498
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001499static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1500{
1501 struct nfit_mem *nfit_mem;
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001502 int dimm_count = 0, rc;
1503 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001504
1505 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001506 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001507 unsigned long flags = 0, cmd_mask;
Dan Williamscaa603a2017-04-14 10:27:11 -07001508 struct nfit_memdev *nfit_memdev;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001509 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001510 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001511
1512 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1513 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1514 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001515 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001516 continue;
1517 }
1518
1519 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1520 flags |= NDD_ALIASING;
1521
Dan Williamscaa603a2017-04-14 10:27:11 -07001522 /* collate flags across all memdevs for this dimm */
1523 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1524 struct acpi_nfit_memory_map *dimm_memdev;
1525
1526 dimm_memdev = __to_nfit_memdev(nfit_mem);
1527 if (dimm_memdev->device_handle
1528 != nfit_memdev->memdev->device_handle)
1529 continue;
1530 dimm_memdev->flags |= nfit_memdev->memdev->flags;
1531 }
1532
Dan Williams58138822015-06-23 20:08:34 -04001533 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001534 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams58138822015-06-23 20:08:34 -04001535 flags |= NDD_UNARMED;
1536
Dan Williams62232e452015-06-08 14:27:06 -04001537 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1538 if (rc)
1539 continue;
1540
Dan Williamse3654ec2016-04-28 16:17:07 -07001541 /*
Dan Williams31eca762016-04-28 16:23:43 -07001542 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1543 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1544 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001545 */
Dan Williams31eca762016-04-28 16:23:43 -07001546 cmd_mask = 1UL << ND_CMD_CALL;
1547 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1548 cmd_mask |= nfit_mem->dsm_mask;
1549
Dan Williamse5ae3b22016-06-07 17:00:04 -07001550 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1551 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001552 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001553 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001554 flags, cmd_mask, flush ? flush->hint_count : 0,
1555 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001556 if (!nvdimm)
1557 return -ENOMEM;
1558
1559 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001560 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001561
1562 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1563 continue;
1564
Dan Williams14999342017-04-13 19:46:36 -07001565 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001566 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001567 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1568 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1569 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Dan Williams14999342017-04-13 19:46:36 -07001570 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1571 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
Dan Williams58138822015-06-23 20:08:34 -04001572
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001573 }
1574
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001575 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1576 if (rc)
1577 return rc;
1578
1579 /*
1580 * Now that dimms are successfully registered, and async registration
1581 * is flushed, attempt to enable event notification.
1582 */
1583 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1584 struct kernfs_node *nfit_kernfs;
1585
1586 nvdimm = nfit_mem->nvdimm;
1587 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1588 if (nfit_kernfs)
1589 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1590 "flags");
1591 sysfs_put(nfit_kernfs);
1592 if (!nfit_mem->flags_attr)
1593 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1594 nvdimm_name(nvdimm));
1595 }
1596
1597 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1598 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001599}
1600
Dan Williams62232e452015-06-08 14:27:06 -04001601static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1602{
1603 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1604 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1605 struct acpi_device *adev;
1606 int i;
1607
Dan Williamse3654ec2016-04-28 16:17:07 -07001608 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001609 adev = to_acpi_dev(acpi_desc);
1610 if (!adev)
1611 return;
1612
Dan Williamsd4f32362016-03-03 16:08:54 -08001613 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Dan Williams62232e452015-06-08 14:27:06 -04001614 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001615 set_bit(i, &nd_desc->cmd_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001616}
1617
Dan Williams1f7df6f2015-06-09 20:13:14 -04001618static ssize_t range_index_show(struct device *dev,
1619 struct device_attribute *attr, char *buf)
1620{
1621 struct nd_region *nd_region = to_nd_region(dev);
1622 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1623
1624 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1625}
1626static DEVICE_ATTR_RO(range_index);
1627
1628static struct attribute *acpi_nfit_region_attributes[] = {
1629 &dev_attr_range_index.attr,
1630 NULL,
1631};
1632
1633static struct attribute_group acpi_nfit_region_attribute_group = {
1634 .name = "nfit",
1635 .attrs = acpi_nfit_region_attributes,
1636};
1637
1638static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1639 &nd_region_attribute_group,
1640 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001641 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001642 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001643 &acpi_nfit_region_attribute_group,
1644 NULL,
1645};
1646
Dan Williamseaf96152015-05-01 13:11:27 -04001647/* enough info to uniquely specify an interleave set */
1648struct nfit_set_info {
1649 struct nfit_set_info_map {
1650 u64 region_offset;
1651 u32 serial_number;
1652 u32 pad;
1653 } mapping[0];
1654};
1655
1656static size_t sizeof_nfit_set_info(int num_mappings)
1657{
1658 return sizeof(struct nfit_set_info)
1659 + num_mappings * sizeof(struct nfit_set_info_map);
1660}
1661
Dan Williams86ef58a2017-02-28 18:32:48 -08001662static int cmp_map_compat(const void *m0, const void *m1)
Dan Williamseaf96152015-05-01 13:11:27 -04001663{
1664 const struct nfit_set_info_map *map0 = m0;
1665 const struct nfit_set_info_map *map1 = m1;
1666
1667 return memcmp(&map0->region_offset, &map1->region_offset,
1668 sizeof(u64));
1669}
1670
Dan Williams86ef58a2017-02-28 18:32:48 -08001671static int cmp_map(const void *m0, const void *m1)
1672{
1673 const struct nfit_set_info_map *map0 = m0;
1674 const struct nfit_set_info_map *map1 = m1;
1675
Dan Williamsb03b99a2017-03-27 21:53:38 -07001676 if (map0->region_offset < map1->region_offset)
1677 return -1;
1678 else if (map0->region_offset > map1->region_offset)
1679 return 1;
1680 return 0;
Dan Williams86ef58a2017-02-28 18:32:48 -08001681}
1682
Dan Williamseaf96152015-05-01 13:11:27 -04001683/* Retrieve the nth entry referencing this spa */
1684static struct acpi_nfit_memory_map *memdev_from_spa(
1685 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1686{
1687 struct nfit_memdev *nfit_memdev;
1688
1689 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1690 if (nfit_memdev->memdev->range_index == range_index)
1691 if (n-- == 0)
1692 return nfit_memdev->memdev;
1693 return NULL;
1694}
1695
1696static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1697 struct nd_region_desc *ndr_desc,
1698 struct acpi_nfit_system_address *spa)
1699{
1700 int i, spa_type = nfit_spa_type(spa);
1701 struct device *dev = acpi_desc->dev;
1702 struct nd_interleave_set *nd_set;
1703 u16 nr = ndr_desc->num_mappings;
1704 struct nfit_set_info *info;
1705
1706 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1707 /* pass */;
1708 else
1709 return 0;
1710
1711 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1712 if (!nd_set)
1713 return -ENOMEM;
1714
1715 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1716 if (!info)
1717 return -ENOMEM;
1718 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07001719 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04001720 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07001721 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04001722 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1723 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1724 spa->range_index, i);
1725
1726 if (!memdev || !nfit_mem->dcr) {
1727 dev_err(dev, "%s: failed to find DCR\n", __func__);
1728 return -ENODEV;
1729 }
1730
1731 map->region_offset = memdev->region_offset;
1732 map->serial_number = nfit_mem->dcr->serial_number;
1733 }
1734
1735 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1736 cmp_map, NULL);
1737 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
Dan Williams86ef58a2017-02-28 18:32:48 -08001738
1739 /* support namespaces created with the wrong sort order */
1740 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1741 cmp_map_compat, NULL);
1742 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1743
Dan Williamseaf96152015-05-01 13:11:27 -04001744 ndr_desc->nd_set = nd_set;
1745 devm_kfree(dev, info);
1746
1747 return 0;
1748}
1749
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001750static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1751{
1752 struct acpi_nfit_interleave *idt = mmio->idt;
1753 u32 sub_line_offset, line_index, line_offset;
1754 u64 line_no, table_skip_count, table_offset;
1755
1756 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1757 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1758 line_offset = idt->line_offset[line_index]
1759 * mmio->line_size;
1760 table_offset = table_skip_count * mmio->table_size;
1761
1762 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1763}
1764
Ross Zwislerde4a1962015-08-20 16:27:38 -06001765static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001766{
1767 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1768 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001769 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001770
1771 if (mmio->num_lines)
1772 offset = to_interleave_offset(offset, mmio);
1773
Ross Zwisler68202c92016-07-29 14:59:12 -06001774 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001775}
1776
1777static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1778 resource_size_t dpa, unsigned int len, unsigned int write)
1779{
1780 u64 cmd, offset;
1781 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1782
1783 enum {
1784 BCW_OFFSET_MASK = (1ULL << 48)-1,
1785 BCW_LEN_SHIFT = 48,
1786 BCW_LEN_MASK = (1ULL << 8) - 1,
1787 BCW_CMD_SHIFT = 56,
1788 };
1789
1790 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1791 len = len >> L1_CACHE_SHIFT;
1792 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1793 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1794
1795 offset = nfit_blk->cmd_offset + mmio->size * bw;
1796 if (mmio->num_lines)
1797 offset = to_interleave_offset(offset, mmio);
1798
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001799 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001800 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001801
Dan Williamsaef25332016-02-12 17:01:11 -08001802 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001803 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001804}
1805
1806static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1807 resource_size_t dpa, void *iobuf, size_t len, int rw,
1808 unsigned int lane)
1809{
1810 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1811 unsigned int copied = 0;
1812 u64 base_offset;
1813 int rc;
1814
1815 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1816 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001817 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1818 while (len) {
1819 unsigned int c;
1820 u64 offset;
1821
1822 if (mmio->num_lines) {
1823 u32 line_offset;
1824
1825 offset = to_interleave_offset(base_offset + copied,
1826 mmio);
1827 div_u64_rem(offset, mmio->line_size, &line_offset);
1828 c = min_t(size_t, len, mmio->line_size - line_offset);
1829 } else {
1830 offset = base_offset + nfit_blk->bdw_offset;
1831 c = len;
1832 }
1833
1834 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001835 memcpy_to_pmem(mmio->addr.aperture + offset,
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001836 iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001837 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001838 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001839 mmio_flush_range((void __force *)
1840 mmio->addr.aperture + offset, c);
1841
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001842 memcpy_from_pmem(iobuf + copied,
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001843 mmio->addr.aperture + offset, c);
1844 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001845
1846 copied += c;
1847 len -= c;
1848 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001849
1850 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001851 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001852
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001853 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1854 return rc;
1855}
1856
1857static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1858 resource_size_t dpa, void *iobuf, u64 len, int rw)
1859{
1860 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1861 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1862 struct nd_region *nd_region = nfit_blk->nd_region;
1863 unsigned int lane, copied = 0;
1864 int rc = 0;
1865
1866 lane = nd_region_acquire_lane(nd_region);
1867 while (len) {
1868 u64 c = min(len, mmio->size);
1869
1870 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1871 iobuf + copied, c, rw, lane);
1872 if (rc)
1873 break;
1874
1875 copied += c;
1876 len -= c;
1877 }
1878 nd_region_release_lane(nd_region, lane);
1879
1880 return rc;
1881}
1882
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001883static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1884 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1885{
1886 if (idt) {
1887 mmio->num_lines = idt->line_count;
1888 mmio->line_size = idt->line_size;
1889 if (interleave_ways == 0)
1890 return -ENXIO;
1891 mmio->table_size = mmio->num_lines * interleave_ways
1892 * mmio->line_size;
1893 }
1894
1895 return 0;
1896}
1897
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001898static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1899 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1900{
1901 struct nd_cmd_dimm_flags flags;
1902 int rc;
1903
1904 memset(&flags, 0, sizeof(flags));
1905 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08001906 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001907
1908 if (rc >= 0 && flags.status == 0)
1909 nfit_blk->dimm_flags = flags.flags;
1910 else if (rc == -ENOTTY) {
1911 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08001912 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001913 rc = 0;
1914 } else
1915 rc = -ENXIO;
1916
1917 return rc;
1918}
1919
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001920static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1921 struct device *dev)
1922{
1923 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001924 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1925 struct nfit_blk_mmio *mmio;
1926 struct nfit_blk *nfit_blk;
1927 struct nfit_mem *nfit_mem;
1928 struct nvdimm *nvdimm;
1929 int rc;
1930
1931 nvdimm = nd_blk_region_to_dimm(ndbr);
1932 nfit_mem = nvdimm_provider_data(nvdimm);
1933 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1934 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1935 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04001936 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1937 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001938 return -ENXIO;
1939 }
1940
1941 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1942 if (!nfit_blk)
1943 return -ENOMEM;
1944 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1945 nfit_blk->nd_region = to_nd_region(dev);
1946
1947 /* map block aperture memory */
1948 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1949 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07001950 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
1951 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001952 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001953 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1954 nvdimm_name(nvdimm));
1955 return -ENOMEM;
1956 }
1957 mmio->size = nfit_mem->bdw->size;
1958 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1959 mmio->idt = nfit_mem->idt_bdw;
1960 mmio->spa = nfit_mem->spa_bdw;
1961 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1962 nfit_mem->memdev_bdw->interleave_ways);
1963 if (rc) {
1964 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1965 __func__, nvdimm_name(nvdimm));
1966 return rc;
1967 }
1968
1969 /* map block control memory */
1970 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1971 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1972 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07001973 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1974 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001975 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001976 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1977 nvdimm_name(nvdimm));
1978 return -ENOMEM;
1979 }
1980 mmio->size = nfit_mem->dcr->window_size;
1981 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1982 mmio->idt = nfit_mem->idt_dcr;
1983 mmio->spa = nfit_mem->spa_dcr;
1984 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1985 nfit_mem->memdev_dcr->interleave_ways);
1986 if (rc) {
1987 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1988 __func__, nvdimm_name(nvdimm));
1989 return rc;
1990 }
1991
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001992 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1993 if (rc < 0) {
1994 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1995 __func__, nvdimm_name(nvdimm));
1996 return rc;
1997 }
1998
Dan Williamsf284a4f2016-07-07 19:44:50 -07001999 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002000 dev_warn(dev, "unable to guarantee persistence of writes\n");
2001
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002002 if (mmio->line_size == 0)
2003 return 0;
2004
2005 if ((u32) nfit_blk->cmd_offset % mmio->line_size
2006 + 8 > mmio->line_size) {
2007 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2008 return -ENXIO;
2009 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2010 + 8 > mmio->line_size) {
2011 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2012 return -ENXIO;
2013 }
2014
2015 return 0;
2016}
2017
Dan Williamsaef25332016-02-12 17:01:11 -08002018static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002019 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002020{
Dan Williamsaef25332016-02-12 17:01:11 -08002021 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002022 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08002023 int cmd_rc, rc;
2024
Dan Williams1cf03c02016-02-17 13:01:23 -08002025 cmd->address = spa->address;
2026 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08002027 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2028 sizeof(*cmd), &cmd_rc);
2029 if (rc < 0)
2030 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002031 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002032}
2033
Dan Williams1cf03c02016-02-17 13:01:23 -08002034static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002035{
2036 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002037 int cmd_rc;
2038 struct nd_cmd_ars_start ars_start;
2039 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2040 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002041
Dan Williams1cf03c02016-02-17 13:01:23 -08002042 memset(&ars_start, 0, sizeof(ars_start));
2043 ars_start.address = spa->address;
2044 ars_start.length = spa->length;
2045 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2046 ars_start.type = ND_ARS_PERSISTENT;
2047 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2048 ars_start.type = ND_ARS_VOLATILE;
2049 else
2050 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07002051
Dan Williams1cf03c02016-02-17 13:01:23 -08002052 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2053 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08002054
Dan Williams1cf03c02016-02-17 13:01:23 -08002055 if (rc < 0)
2056 return rc;
2057 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002058}
2059
Dan Williams1cf03c02016-02-17 13:01:23 -08002060static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07002061{
Dan Williamsaef25332016-02-12 17:01:11 -08002062 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002063 struct nd_cmd_ars_start ars_start;
2064 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2065 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07002066
Dan Williams1cf03c02016-02-17 13:01:23 -08002067 memset(&ars_start, 0, sizeof(ars_start));
2068 ars_start.address = ars_status->restart_address;
2069 ars_start.length = ars_status->restart_length;
2070 ars_start.type = ars_status->type;
2071 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2072 sizeof(ars_start), &cmd_rc);
2073 if (rc < 0)
2074 return rc;
2075 return cmd_rc;
2076}
Dan Williamsaef25332016-02-12 17:01:11 -08002077
Dan Williams1cf03c02016-02-17 13:01:23 -08002078static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2079{
2080 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2081 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2082 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08002083
Dan Williams1cf03c02016-02-17 13:01:23 -08002084 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2085 acpi_desc->ars_status_size, &cmd_rc);
2086 if (rc < 0)
2087 return rc;
2088 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002089}
2090
Dan Williams82aa37c2016-12-06 12:45:24 -08002091static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002092 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07002093{
Dan Williams82aa37c2016-12-06 12:45:24 -08002094 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
Vishal Verma0caeef62015-12-24 19:21:43 -07002095 int rc;
2096 u32 i;
2097
Dan Williams82aa37c2016-12-06 12:45:24 -08002098 /*
2099 * First record starts at 44 byte offset from the start of the
2100 * payload.
2101 */
2102 if (ars_status->out_length < 44)
2103 return 0;
Vishal Verma0caeef62015-12-24 19:21:43 -07002104 for (i = 0; i < ars_status->num_records; i++) {
Dan Williams82aa37c2016-12-06 12:45:24 -08002105 /* only process full records */
2106 if (ars_status->out_length
2107 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2108 break;
Vishal Verma0caeef62015-12-24 19:21:43 -07002109 rc = nvdimm_bus_add_poison(nvdimm_bus,
2110 ars_status->records[i].err_address,
2111 ars_status->records[i].length);
2112 if (rc)
2113 return rc;
2114 }
Dan Williams82aa37c2016-12-06 12:45:24 -08002115 if (i < ars_status->num_records)
2116 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
Vishal Verma0caeef62015-12-24 19:21:43 -07002117
2118 return 0;
2119}
2120
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002121static void acpi_nfit_remove_resource(void *data)
2122{
2123 struct resource *res = data;
2124
2125 remove_resource(res);
2126}
2127
2128static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2129 struct nd_region_desc *ndr_desc)
2130{
2131 struct resource *res, *nd_res = ndr_desc->res;
2132 int is_pmem, ret;
2133
2134 /* No operation if the region is already registered as PMEM */
2135 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2136 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2137 if (is_pmem == REGION_INTERSECTS)
2138 return 0;
2139
2140 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2141 if (!res)
2142 return -ENOMEM;
2143
2144 res->name = "Persistent Memory";
2145 res->start = nd_res->start;
2146 res->end = nd_res->end;
2147 res->flags = IORESOURCE_MEM;
2148 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2149
2150 ret = insert_resource(&iomem_resource, res);
2151 if (ret)
2152 return ret;
2153
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302154 ret = devm_add_action_or_reset(acpi_desc->dev,
2155 acpi_nfit_remove_resource,
2156 res);
2157 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002158 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002159
2160 return 0;
2161}
2162
Dan Williams1f7df6f2015-06-09 20:13:14 -04002163static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002164 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002165 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002166 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002167{
2168 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2169 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002170 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002171 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002172 struct nfit_mem *nfit_mem;
2173 int blk_valid = 0;
2174
2175 if (!nvdimm) {
2176 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2177 spa->range_index, memdev->device_handle);
2178 return -ENODEV;
2179 }
2180
Dan Williams44c462e2016-09-19 16:38:50 -07002181 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002182 switch (nfit_spa_type(spa)) {
2183 case NFIT_SPA_PM:
2184 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002185 mapping->start = memdev->address;
2186 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002187 break;
2188 case NFIT_SPA_DCR:
2189 nfit_mem = nvdimm_provider_data(nvdimm);
2190 if (!nfit_mem || !nfit_mem->bdw) {
2191 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2192 spa->range_index, nvdimm_name(nvdimm));
2193 } else {
Dan Williams44c462e2016-09-19 16:38:50 -07002194 mapping->size = nfit_mem->bdw->capacity;
2195 mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04002196 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002197 blk_valid = 1;
2198 }
2199
Dan Williams44c462e2016-09-19 16:38:50 -07002200 ndr_desc->mapping = mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002201 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002202 ndbr_desc = to_blk_region_desc(ndr_desc);
2203 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002204 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williams1cf03c02016-02-17 13:01:23 -08002205 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2206 ndr_desc);
2207 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002208 return -ENOMEM;
2209 break;
2210 }
2211
2212 return 0;
2213}
2214
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002215static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2216{
2217 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2218 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2219 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2220 nfit_spa_type(spa) == NFIT_SPA_PCD);
2221}
2222
Dan Williams1f7df6f2015-06-09 20:13:14 -04002223static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2224 struct nfit_spa *nfit_spa)
2225{
Dan Williams44c462e2016-09-19 16:38:50 -07002226 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002227 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002228 struct nd_blk_region_desc ndbr_desc;
2229 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002230 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002231 struct nvdimm_bus *nvdimm_bus;
2232 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002233 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002234
Dan Williams1cf03c02016-02-17 13:01:23 -08002235 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002236 return 0;
2237
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002238 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04002239 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2240 __func__);
2241 return 0;
2242 }
2243
2244 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002245 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002246 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002247 res.start = spa->address;
2248 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002249 ndr_desc = &ndbr_desc.ndr_desc;
2250 ndr_desc->res = &res;
2251 ndr_desc->provider_data = nfit_spa;
2252 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002253 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2254 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2255 spa->proximity_domain);
2256 else
2257 ndr_desc->numa_node = NUMA_NO_NODE;
2258
Dan Williams1f7df6f2015-06-09 20:13:14 -04002259 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2260 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002261 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002262
2263 if (memdev->range_index != spa->range_index)
2264 continue;
2265 if (count >= ND_MAX_MAPPINGS) {
2266 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2267 spa->range_index, ND_MAX_MAPPINGS);
2268 return -ENXIO;
2269 }
Dan Williams44c462e2016-09-19 16:38:50 -07002270 mapping = &mappings[count++];
2271 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002272 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002273 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002274 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002275 }
2276
Dan Williams44c462e2016-09-19 16:38:50 -07002277 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002278 ndr_desc->num_mappings = count;
2279 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002280 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002281 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002282
Dan Williams1f7df6f2015-06-09 20:13:14 -04002283 nvdimm_bus = acpi_desc->nvdimm_bus;
2284 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002285 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002286 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002287 dev_warn(acpi_desc->dev,
2288 "failed to insert pmem resource to iomem: %d\n",
2289 rc);
Dan Williams48901162016-03-09 17:15:43 -08002290 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002291 }
Dan Williams48901162016-03-09 17:15:43 -08002292
Dan Williams1cf03c02016-02-17 13:01:23 -08002293 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2294 ndr_desc);
2295 if (!nfit_spa->nd_region)
2296 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002297 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002298 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2299 ndr_desc);
2300 if (!nfit_spa->nd_region)
2301 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002302 } else if (nfit_spa_is_virtual(spa)) {
2303 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2304 ndr_desc);
2305 if (!nfit_spa->nd_region)
2306 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002307 }
Vishal Verma20985162015-10-27 16:58:27 -06002308
Dan Williams1cf03c02016-02-17 13:01:23 -08002309 out:
2310 if (rc)
2311 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2312 nfit_spa->spa->range_index);
2313 return rc;
2314}
2315
2316static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2317 u32 max_ars)
2318{
2319 struct device *dev = acpi_desc->dev;
2320 struct nd_cmd_ars_status *ars_status;
2321
2322 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2323 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2324 return 0;
2325 }
2326
2327 if (acpi_desc->ars_status)
2328 devm_kfree(dev, acpi_desc->ars_status);
2329 acpi_desc->ars_status = NULL;
2330 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2331 if (!ars_status)
2332 return -ENOMEM;
2333 acpi_desc->ars_status = ars_status;
2334 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002335 return 0;
2336}
2337
Dan Williams1cf03c02016-02-17 13:01:23 -08002338static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2339 struct nfit_spa *nfit_spa)
2340{
2341 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2342 int rc;
2343
2344 if (!nfit_spa->max_ars) {
2345 struct nd_cmd_ars_cap ars_cap;
2346
2347 memset(&ars_cap, 0, sizeof(ars_cap));
2348 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2349 if (rc < 0)
2350 return rc;
2351 nfit_spa->max_ars = ars_cap.max_ars_out;
2352 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2353 /* check that the supported scrub types match the spa type */
2354 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2355 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2356 return -ENOTTY;
2357 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2358 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2359 return -ENOTTY;
2360 }
2361
2362 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2363 return -ENOMEM;
2364
2365 rc = ars_get_status(acpi_desc);
2366 if (rc < 0 && rc != -ENOSPC)
2367 return rc;
2368
Dan Williams82aa37c2016-12-06 12:45:24 -08002369 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
Dan Williams1cf03c02016-02-17 13:01:23 -08002370 return -ENOMEM;
2371
2372 return 0;
2373}
2374
2375static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2376 struct nfit_spa *nfit_spa)
2377{
2378 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2379 unsigned int overflow_retry = scrub_overflow_abort;
2380 u64 init_ars_start = 0, init_ars_len = 0;
2381 struct device *dev = acpi_desc->dev;
2382 unsigned int tmo = scrub_timeout;
2383 int rc;
2384
Vishal Verma37b137f2016-07-23 21:51:42 -07002385 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002386 return;
2387
2388 rc = ars_start(acpi_desc, nfit_spa);
2389 /*
2390 * If we timed out the initial scan we'll still be busy here,
2391 * and will wait another timeout before giving up permanently.
2392 */
2393 if (rc < 0 && rc != -EBUSY)
2394 return;
2395
2396 do {
2397 u64 ars_start, ars_len;
2398
2399 if (acpi_desc->cancel)
2400 break;
2401 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2402 if (rc == -ENOTTY)
2403 break;
2404 if (rc == -EBUSY && !tmo) {
2405 dev_warn(dev, "range %d ars timeout, aborting\n",
2406 spa->range_index);
2407 break;
2408 }
2409
2410 if (rc == -EBUSY) {
2411 /*
2412 * Note, entries may be appended to the list
2413 * while the lock is dropped, but the workqueue
2414 * being active prevents entries being deleted /
2415 * freed.
2416 */
2417 mutex_unlock(&acpi_desc->init_mutex);
2418 ssleep(1);
2419 tmo--;
2420 mutex_lock(&acpi_desc->init_mutex);
2421 continue;
2422 }
2423
2424 /* we got some results, but there are more pending... */
2425 if (rc == -ENOSPC && overflow_retry--) {
2426 if (!init_ars_len) {
2427 init_ars_len = acpi_desc->ars_status->length;
2428 init_ars_start = acpi_desc->ars_status->address;
2429 }
2430 rc = ars_continue(acpi_desc);
2431 }
2432
2433 if (rc < 0) {
2434 dev_warn(dev, "range %d ars continuation failed\n",
2435 spa->range_index);
2436 break;
2437 }
2438
2439 if (init_ars_len) {
2440 ars_start = init_ars_start;
2441 ars_len = init_ars_len;
2442 } else {
2443 ars_start = acpi_desc->ars_status->address;
2444 ars_len = acpi_desc->ars_status->length;
2445 }
2446 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2447 spa->range_index, ars_start, ars_len);
2448 /* notify the region about new poison entries */
2449 nvdimm_region_notify(nfit_spa->nd_region,
2450 NVDIMM_REVALIDATE_POISON);
2451 break;
2452 } while (1);
2453}
2454
2455static void acpi_nfit_scrub(struct work_struct *work)
2456{
2457 struct device *dev;
2458 u64 init_scrub_length = 0;
2459 struct nfit_spa *nfit_spa;
2460 u64 init_scrub_address = 0;
2461 bool init_ars_done = false;
2462 struct acpi_nfit_desc *acpi_desc;
2463 unsigned int tmo = scrub_timeout;
2464 unsigned int overflow_retry = scrub_overflow_abort;
2465
2466 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2467 dev = acpi_desc->dev;
2468
2469 /*
2470 * We scrub in 2 phases. The first phase waits for any platform
2471 * firmware initiated scrubs to complete and then we go search for the
2472 * affected spa regions to mark them scanned. In the second phase we
2473 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002474 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2475 * the first phase, but really only care about running phase 2, where
2476 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002477 */
2478
2479 /* process platform firmware initiated scrubs */
2480 retry:
2481 mutex_lock(&acpi_desc->init_mutex);
2482 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2483 struct nd_cmd_ars_status *ars_status;
2484 struct acpi_nfit_system_address *spa;
2485 u64 ars_start, ars_len;
2486 int rc;
2487
2488 if (acpi_desc->cancel)
2489 break;
2490
2491 if (nfit_spa->nd_region)
2492 continue;
2493
2494 if (init_ars_done) {
2495 /*
2496 * No need to re-query, we're now just
2497 * reconciling all the ranges covered by the
2498 * initial scrub
2499 */
2500 rc = 0;
2501 } else
2502 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2503
2504 if (rc == -ENOTTY) {
2505 /* no ars capability, just register spa and move on */
2506 acpi_nfit_register_region(acpi_desc, nfit_spa);
2507 continue;
2508 }
2509
2510 if (rc == -EBUSY && !tmo) {
2511 /* fallthrough to directed scrub in phase 2 */
2512 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2513 break;
2514 } else if (rc == -EBUSY) {
2515 mutex_unlock(&acpi_desc->init_mutex);
2516 ssleep(1);
2517 tmo--;
2518 goto retry;
2519 }
2520
2521 /* we got some results, but there are more pending... */
2522 if (rc == -ENOSPC && overflow_retry--) {
2523 ars_status = acpi_desc->ars_status;
2524 /*
2525 * Record the original scrub range, so that we
2526 * can recall all the ranges impacted by the
2527 * initial scrub.
2528 */
2529 if (!init_scrub_length) {
2530 init_scrub_length = ars_status->length;
2531 init_scrub_address = ars_status->address;
2532 }
2533 rc = ars_continue(acpi_desc);
2534 if (rc == 0) {
2535 mutex_unlock(&acpi_desc->init_mutex);
2536 goto retry;
2537 }
2538 }
2539
2540 if (rc < 0) {
2541 /*
2542 * Initial scrub failed, we'll give it one more
2543 * try below...
2544 */
2545 break;
2546 }
2547
2548 /* We got some final results, record completed ranges */
2549 ars_status = acpi_desc->ars_status;
2550 if (init_scrub_length) {
2551 ars_start = init_scrub_address;
2552 ars_len = ars_start + init_scrub_length;
2553 } else {
2554 ars_start = ars_status->address;
2555 ars_len = ars_status->length;
2556 }
2557 spa = nfit_spa->spa;
2558
2559 if (!init_ars_done) {
2560 init_ars_done = true;
2561 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2562 ars_start, ars_len);
2563 }
2564 if (ars_start <= spa->address && ars_start + ars_len
2565 >= spa->address + spa->length)
2566 acpi_nfit_register_region(acpi_desc, nfit_spa);
2567 }
2568
2569 /*
2570 * For all the ranges not covered by an initial scrub we still
2571 * want to see if there are errors, but it's ok to discover them
2572 * asynchronously.
2573 */
2574 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2575 /*
2576 * Flag all the ranges that still need scrubbing, but
2577 * register them now to make data available.
2578 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002579 if (!nfit_spa->nd_region) {
2580 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002581 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002582 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002583 }
2584
2585 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2586 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002587 acpi_desc->scrub_count++;
2588 if (acpi_desc->scrub_count_state)
2589 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002590 mutex_unlock(&acpi_desc->init_mutex);
2591}
2592
Dan Williams1f7df6f2015-06-09 20:13:14 -04002593static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2594{
2595 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002596 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002597
Dan Williams1cf03c02016-02-17 13:01:23 -08002598 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2599 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2600 /* BLK regions don't need to wait for ars results */
2601 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2602 if (rc)
2603 return rc;
2604 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002605
Dan Williams1cf03c02016-02-17 13:01:23 -08002606 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002607 return 0;
2608}
2609
Vishal Verma20985162015-10-27 16:58:27 -06002610static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2611 struct nfit_table_prev *prev)
2612{
2613 struct device *dev = acpi_desc->dev;
2614
2615 if (!list_empty(&prev->spas) ||
2616 !list_empty(&prev->memdevs) ||
2617 !list_empty(&prev->dcrs) ||
2618 !list_empty(&prev->bdws) ||
2619 !list_empty(&prev->idts) ||
2620 !list_empty(&prev->flushes)) {
2621 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2622 return -ENXIO;
2623 }
2624 return 0;
2625}
2626
Vishal Verma37b137f2016-07-23 21:51:42 -07002627static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2628{
2629 struct device *dev = acpi_desc->dev;
2630 struct kernfs_node *nfit;
2631 struct device *bus_dev;
2632
2633 if (!ars_supported(acpi_desc->nvdimm_bus))
2634 return 0;
2635
2636 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2637 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2638 if (!nfit) {
2639 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2640 return -ENODEV;
2641 }
2642 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2643 sysfs_put(nfit);
2644 if (!acpi_desc->scrub_count_state) {
2645 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2646 return -ENODEV;
2647 }
2648
2649 return 0;
2650}
2651
Dan Williams58cd71b2016-07-21 18:05:36 -07002652static void acpi_nfit_destruct(void *data)
2653{
2654 struct acpi_nfit_desc *acpi_desc = data;
Vishal Verma37b137f2016-07-23 21:51:42 -07002655 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002656
Vishal Verma6839a6d2016-07-23 21:51:21 -07002657 /*
2658 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2659 * race teardown
2660 */
2661 mutex_lock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002662 acpi_desc->cancel = 1;
Vishal Verma37b137f2016-07-23 21:51:42 -07002663 /*
2664 * Bounce the nvdimm bus lock to make sure any in-flight
2665 * acpi_nfit_ars_rescan() submissions have had a chance to
2666 * either submit or see ->cancel set.
2667 */
2668 device_lock(bus_dev);
2669 device_unlock(bus_dev);
2670
Dan Williams58cd71b2016-07-21 18:05:36 -07002671 flush_workqueue(nfit_wq);
Vishal Verma37b137f2016-07-23 21:51:42 -07002672 if (acpi_desc->scrub_count_state)
2673 sysfs_put(acpi_desc->scrub_count_state);
Dan Williams58cd71b2016-07-21 18:05:36 -07002674 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2675 acpi_desc->nvdimm_bus = NULL;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002676 list_del(&acpi_desc->list);
2677 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002678}
2679
Dan Williamse7a11b42016-07-14 16:19:55 -07002680int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002681{
2682 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002683 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002684 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002685 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002686
Dan Williams58cd71b2016-07-21 18:05:36 -07002687 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002688 acpi_nfit_init_dsms(acpi_desc);
2689
Dan Williams58cd71b2016-07-21 18:05:36 -07002690 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2691 &acpi_desc->nd_desc);
2692 if (!acpi_desc->nvdimm_bus)
2693 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002694
Dan Williams58cd71b2016-07-21 18:05:36 -07002695 rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
2696 acpi_desc);
2697 if (rc)
2698 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002699
2700 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2701 if (rc)
2702 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002703
2704 /* register this acpi_desc for mce notifications */
2705 mutex_lock(&acpi_desc_lock);
2706 list_add_tail(&acpi_desc->list, &acpi_descs);
2707 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002708 }
2709
Vishal Verma20985162015-10-27 16:58:27 -06002710 mutex_lock(&acpi_desc->init_mutex);
2711
2712 INIT_LIST_HEAD(&prev.spas);
2713 INIT_LIST_HEAD(&prev.memdevs);
2714 INIT_LIST_HEAD(&prev.dcrs);
2715 INIT_LIST_HEAD(&prev.bdws);
2716 INIT_LIST_HEAD(&prev.idts);
2717 INIT_LIST_HEAD(&prev.flushes);
2718
2719 list_cut_position(&prev.spas, &acpi_desc->spas,
2720 acpi_desc->spas.prev);
2721 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2722 acpi_desc->memdevs.prev);
2723 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2724 acpi_desc->dcrs.prev);
2725 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2726 acpi_desc->bdws.prev);
2727 list_cut_position(&prev.idts, &acpi_desc->idts,
2728 acpi_desc->idts.prev);
2729 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2730 acpi_desc->flushes.prev);
2731
Vishal Verma20985162015-10-27 16:58:27 -06002732 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002733 while (!IS_ERR_OR_NULL(data))
2734 data = add_table(acpi_desc, &prev, data, end);
2735
2736 if (IS_ERR(data)) {
2737 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2738 PTR_ERR(data));
2739 rc = PTR_ERR(data);
2740 goto out_unlock;
2741 }
2742
2743 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2744 if (rc)
2745 goto out_unlock;
2746
Dan Williams81ed4e32016-06-10 18:20:53 -07002747 rc = nfit_mem_init(acpi_desc);
2748 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002749 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002750
2751 rc = acpi_nfit_register_dimms(acpi_desc);
2752 if (rc)
2753 goto out_unlock;
2754
2755 rc = acpi_nfit_register_regions(acpi_desc);
2756
2757 out_unlock:
2758 mutex_unlock(&acpi_desc->init_mutex);
2759 return rc;
2760}
2761EXPORT_SYMBOL_GPL(acpi_nfit_init);
2762
Dan Williams7ae0fa432016-02-19 12:16:34 -08002763struct acpi_nfit_flush_work {
2764 struct work_struct work;
2765 struct completion cmp;
2766};
2767
2768static void flush_probe(struct work_struct *work)
2769{
2770 struct acpi_nfit_flush_work *flush;
2771
2772 flush = container_of(work, typeof(*flush), work);
2773 complete(&flush->cmp);
2774}
2775
2776static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2777{
2778 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2779 struct device *dev = acpi_desc->dev;
2780 struct acpi_nfit_flush_work flush;
Dan Williamse4714862017-02-02 10:31:00 -08002781 int rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002782
2783 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2784 device_lock(dev);
2785 device_unlock(dev);
2786
2787 /*
2788 * Scrub work could take 10s of seconds, userspace may give up so we
2789 * need to be interruptible while waiting.
2790 */
2791 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2792 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2793 queue_work(nfit_wq, &flush.work);
Dan Williamse4714862017-02-02 10:31:00 -08002794
2795 rc = wait_for_completion_interruptible(&flush.cmp);
2796 cancel_work_sync(&flush.work);
2797 return rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002798}
2799
Dan Williams87bf5722016-02-22 21:50:31 -08002800static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2801 struct nvdimm *nvdimm, unsigned int cmd)
2802{
2803 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2804
2805 if (nvdimm)
2806 return 0;
2807 if (cmd != ND_CMD_ARS_START)
2808 return 0;
2809
2810 /*
2811 * The kernel and userspace may race to initiate a scrub, but
2812 * the scrub thread is prepared to lose that initial race. It
2813 * just needs guarantees that any ars it initiates are not
2814 * interrupted by any intervening start reqeusts from userspace.
2815 */
2816 if (work_busy(&acpi_desc->work))
2817 return -EBUSY;
2818
2819 return 0;
2820}
2821
Vishal Verma6839a6d2016-07-23 21:51:21 -07002822int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
Vishal Verma37b137f2016-07-23 21:51:42 -07002823{
2824 struct device *dev = acpi_desc->dev;
2825 struct nfit_spa *nfit_spa;
2826
2827 if (work_busy(&acpi_desc->work))
2828 return -EBUSY;
2829
2830 if (acpi_desc->cancel)
2831 return 0;
2832
2833 mutex_lock(&acpi_desc->init_mutex);
2834 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2835 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2836
2837 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2838 continue;
2839
2840 nfit_spa->ars_required = 1;
2841 }
2842 queue_work(nfit_wq, &acpi_desc->work);
2843 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2844 mutex_unlock(&acpi_desc->init_mutex);
2845
2846 return 0;
2847}
2848
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002849void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002850{
2851 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002852
2853 dev_set_drvdata(dev, acpi_desc);
2854 acpi_desc->dev = dev;
2855 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2856 nd_desc = &acpi_desc->nd_desc;
2857 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002858 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002859 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002860 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002861 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002862 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2863
Dan Williamsb94d5232015-05-19 22:54:31 -04002864 INIT_LIST_HEAD(&acpi_desc->spas);
2865 INIT_LIST_HEAD(&acpi_desc->dcrs);
2866 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002867 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002868 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04002869 INIT_LIST_HEAD(&acpi_desc->memdevs);
2870 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002871 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06002872 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08002873 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04002874}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002875EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04002876
Dan Williams3c87f372017-04-03 13:52:14 -07002877static void acpi_nfit_put_table(void *table)
2878{
2879 acpi_put_table(table);
2880}
2881
Dan Williamsb94d5232015-05-19 22:54:31 -04002882static int acpi_nfit_add(struct acpi_device *adev)
2883{
Vishal Verma20985162015-10-27 16:58:27 -06002884 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04002885 struct acpi_nfit_desc *acpi_desc;
2886 struct device *dev = &adev->dev;
2887 struct acpi_table_header *tbl;
2888 acpi_status status = AE_OK;
2889 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07002890 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002891
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002892 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
Dan Williamsb94d5232015-05-19 22:54:31 -04002893 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06002894 /* This is ok, we could have an nvdimm hotplugged later */
2895 dev_dbg(dev, "failed to find NFIT at startup\n");
2896 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002897 }
Dan Williams3c87f372017-04-03 13:52:14 -07002898
2899 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
2900 if (rc)
2901 return rc;
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002902 sz = tbl->length;
Dan Williamsb94d5232015-05-19 22:54:31 -04002903
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002904 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2905 if (!acpi_desc)
2906 return -ENOMEM;
2907 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04002908
Dan Williamse7a11b42016-07-14 16:19:55 -07002909 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05002910 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04002911
Vishal Verma20985162015-10-27 16:58:27 -06002912 /* Evaluate _FIT and override with that if present */
2913 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2914 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002915 union acpi_object *obj = buf.pointer;
2916
2917 if (obj->type == ACPI_TYPE_BUFFER)
2918 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2919 obj->buffer.length);
2920 else
Linda Knippers6b577c92015-11-20 19:05:49 -05002921 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2922 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07002923 kfree(buf.pointer);
2924 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07002925 /* skip over the lead-in header table */
2926 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2927 + sizeof(struct acpi_table_nfit),
2928 sz - sizeof(struct acpi_table_nfit));
Dan Williamse7a11b42016-07-14 16:19:55 -07002929 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002930}
2931
2932static int acpi_nfit_remove(struct acpi_device *adev)
2933{
Dan Williams58cd71b2016-07-21 18:05:36 -07002934 /* see acpi_nfit_destruct */
Dan Williamsb94d5232015-05-19 22:54:31 -04002935 return 0;
2936}
2937
Dan Williamsc14a8682016-08-18 22:15:04 -07002938void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
Vishal Verma20985162015-10-27 16:58:27 -06002939{
Dan Williamsc14a8682016-08-18 22:15:04 -07002940 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06002941 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07002942 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06002943 acpi_status status;
2944 int ret;
2945
2946 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2947
Vishal Vermac09f1212016-08-19 14:40:58 -06002948 if (event != NFIT_NOTIFY_UPDATE)
2949 return;
2950
Vishal Verma20985162015-10-27 16:58:27 -06002951 if (!dev->driver) {
2952 /* dev->driver may be null if we're being removed */
2953 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Dan Williamsc14a8682016-08-18 22:15:04 -07002954 return;
Vishal Verma20985162015-10-27 16:58:27 -06002955 }
2956
2957 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002958 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2959 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07002960 return;
2961 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002962 } else {
2963 /*
2964 * Finish previous registration before considering new
2965 * regions.
2966 */
2967 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06002968 }
2969
2970 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07002971 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06002972 if (ACPI_FAILURE(status)) {
2973 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07002974 return;
Vishal Verma20985162015-10-27 16:58:27 -06002975 }
2976
Linda Knippers6b577c92015-11-20 19:05:49 -05002977 obj = buf.pointer;
2978 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002979 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2980 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07002981 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05002982 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07002983 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05002984 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06002985 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07002986}
2987EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06002988
Dan Williamsc14a8682016-08-18 22:15:04 -07002989static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2990{
2991 device_lock(&adev->dev);
2992 __acpi_nfit_notify(&adev->dev, adev->handle, event);
2993 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06002994}
2995
Dan Williamsb94d5232015-05-19 22:54:31 -04002996static const struct acpi_device_id acpi_nfit_ids[] = {
2997 { "ACPI0012", 0 },
2998 { "", 0 },
2999};
3000MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3001
3002static struct acpi_driver acpi_nfit_driver = {
3003 .name = KBUILD_MODNAME,
3004 .ids = acpi_nfit_ids,
3005 .ops = {
3006 .add = acpi_nfit_add,
3007 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06003008 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04003009 },
3010};
3011
3012static __init int nfit_init(void)
3013{
3014 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3015 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3016 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3017 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3018 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3019 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3020 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3021
3022 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
3023 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
3024 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
3025 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
3026 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
3027 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
3028 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
3029 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
3030 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
3031 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
Dan Williams31eca762016-04-28 16:23:43 -07003032 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3033 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
stuart hayese02fb722016-05-26 11:38:41 -05003034 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04003035
Dan Williams7ae0fa432016-02-19 12:16:34 -08003036 nfit_wq = create_singlethread_workqueue("nfit");
3037 if (!nfit_wq)
3038 return -ENOMEM;
3039
Vishal Verma6839a6d2016-07-23 21:51:21 -07003040 nfit_mce_register();
3041
Dan Williamsb94d5232015-05-19 22:54:31 -04003042 return acpi_bus_register_driver(&acpi_nfit_driver);
3043}
3044
3045static __exit void nfit_exit(void)
3046{
Vishal Verma6839a6d2016-07-23 21:51:21 -07003047 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04003048 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003049 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003050 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04003051}
3052
3053module_init(nfit_init);
3054module_exit(nfit_exit);
3055MODULE_LICENSE("GPL v2");
3056MODULE_AUTHOR("Intel Corporation");