blob: 2bd842c46fbb4e10feeaa100741488cd627ed0c4 [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);
460 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
461 __func__, memdev->device_handle, memdev->range_index,
462 memdev->region_index);
463 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 Williamse6dfb2d2015-04-25 03:56:17 -04001508 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001509 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001510
1511 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1512 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1513 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001514 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001515 continue;
1516 }
1517
1518 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1519 flags |= NDD_ALIASING;
1520
Dan Williams58138822015-06-23 20:08:34 -04001521 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001522 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams58138822015-06-23 20:08:34 -04001523 flags |= NDD_UNARMED;
1524
Dan Williams62232e452015-06-08 14:27:06 -04001525 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1526 if (rc)
1527 continue;
1528
Dan Williamse3654ec2016-04-28 16:17:07 -07001529 /*
Dan Williams31eca762016-04-28 16:23:43 -07001530 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1531 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1532 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001533 */
Dan Williams31eca762016-04-28 16:23:43 -07001534 cmd_mask = 1UL << ND_CMD_CALL;
1535 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1536 cmd_mask |= nfit_mem->dsm_mask;
1537
Dan Williamse5ae3b22016-06-07 17:00:04 -07001538 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1539 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001540 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001541 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001542 flags, cmd_mask, flush ? flush->hint_count : 0,
1543 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001544 if (!nvdimm)
1545 return -ENOMEM;
1546
1547 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001548 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001549
1550 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1551 continue;
1552
Dan Williams14999342017-04-13 19:46:36 -07001553 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001554 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001555 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1556 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1557 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Dan Williams14999342017-04-13 19:46:36 -07001558 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1559 mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
Dan Williams58138822015-06-23 20:08:34 -04001560
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001561 }
1562
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001563 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1564 if (rc)
1565 return rc;
1566
1567 /*
1568 * Now that dimms are successfully registered, and async registration
1569 * is flushed, attempt to enable event notification.
1570 */
1571 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1572 struct kernfs_node *nfit_kernfs;
1573
1574 nvdimm = nfit_mem->nvdimm;
1575 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1576 if (nfit_kernfs)
1577 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1578 "flags");
1579 sysfs_put(nfit_kernfs);
1580 if (!nfit_mem->flags_attr)
1581 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1582 nvdimm_name(nvdimm));
1583 }
1584
1585 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1586 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001587}
1588
Dan Williams62232e452015-06-08 14:27:06 -04001589static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1590{
1591 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1592 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1593 struct acpi_device *adev;
1594 int i;
1595
Dan Williamse3654ec2016-04-28 16:17:07 -07001596 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001597 adev = to_acpi_dev(acpi_desc);
1598 if (!adev)
1599 return;
1600
Dan Williamsd4f32362016-03-03 16:08:54 -08001601 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Dan Williams62232e452015-06-08 14:27:06 -04001602 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001603 set_bit(i, &nd_desc->cmd_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001604}
1605
Dan Williams1f7df6f2015-06-09 20:13:14 -04001606static ssize_t range_index_show(struct device *dev,
1607 struct device_attribute *attr, char *buf)
1608{
1609 struct nd_region *nd_region = to_nd_region(dev);
1610 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1611
1612 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1613}
1614static DEVICE_ATTR_RO(range_index);
1615
1616static struct attribute *acpi_nfit_region_attributes[] = {
1617 &dev_attr_range_index.attr,
1618 NULL,
1619};
1620
1621static struct attribute_group acpi_nfit_region_attribute_group = {
1622 .name = "nfit",
1623 .attrs = acpi_nfit_region_attributes,
1624};
1625
1626static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1627 &nd_region_attribute_group,
1628 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001629 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001630 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001631 &acpi_nfit_region_attribute_group,
1632 NULL,
1633};
1634
Dan Williamseaf96152015-05-01 13:11:27 -04001635/* enough info to uniquely specify an interleave set */
1636struct nfit_set_info {
1637 struct nfit_set_info_map {
1638 u64 region_offset;
1639 u32 serial_number;
1640 u32 pad;
1641 } mapping[0];
1642};
1643
1644static size_t sizeof_nfit_set_info(int num_mappings)
1645{
1646 return sizeof(struct nfit_set_info)
1647 + num_mappings * sizeof(struct nfit_set_info_map);
1648}
1649
Dan Williams86ef58a2017-02-28 18:32:48 -08001650static int cmp_map_compat(const void *m0, const void *m1)
Dan Williamseaf96152015-05-01 13:11:27 -04001651{
1652 const struct nfit_set_info_map *map0 = m0;
1653 const struct nfit_set_info_map *map1 = m1;
1654
1655 return memcmp(&map0->region_offset, &map1->region_offset,
1656 sizeof(u64));
1657}
1658
Dan Williams86ef58a2017-02-28 18:32:48 -08001659static int cmp_map(const void *m0, const void *m1)
1660{
1661 const struct nfit_set_info_map *map0 = m0;
1662 const struct nfit_set_info_map *map1 = m1;
1663
Dan Williamsb03b99a2017-03-27 21:53:38 -07001664 if (map0->region_offset < map1->region_offset)
1665 return -1;
1666 else if (map0->region_offset > map1->region_offset)
1667 return 1;
1668 return 0;
Dan Williams86ef58a2017-02-28 18:32:48 -08001669}
1670
Dan Williamseaf96152015-05-01 13:11:27 -04001671/* Retrieve the nth entry referencing this spa */
1672static struct acpi_nfit_memory_map *memdev_from_spa(
1673 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1674{
1675 struct nfit_memdev *nfit_memdev;
1676
1677 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1678 if (nfit_memdev->memdev->range_index == range_index)
1679 if (n-- == 0)
1680 return nfit_memdev->memdev;
1681 return NULL;
1682}
1683
1684static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1685 struct nd_region_desc *ndr_desc,
1686 struct acpi_nfit_system_address *spa)
1687{
1688 int i, spa_type = nfit_spa_type(spa);
1689 struct device *dev = acpi_desc->dev;
1690 struct nd_interleave_set *nd_set;
1691 u16 nr = ndr_desc->num_mappings;
1692 struct nfit_set_info *info;
1693
1694 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1695 /* pass */;
1696 else
1697 return 0;
1698
1699 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1700 if (!nd_set)
1701 return -ENOMEM;
1702
1703 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1704 if (!info)
1705 return -ENOMEM;
1706 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07001707 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04001708 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07001709 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04001710 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1711 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1712 spa->range_index, i);
1713
1714 if (!memdev || !nfit_mem->dcr) {
1715 dev_err(dev, "%s: failed to find DCR\n", __func__);
1716 return -ENODEV;
1717 }
1718
1719 map->region_offset = memdev->region_offset;
1720 map->serial_number = nfit_mem->dcr->serial_number;
1721 }
1722
1723 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1724 cmp_map, NULL);
1725 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
Dan Williams86ef58a2017-02-28 18:32:48 -08001726
1727 /* support namespaces created with the wrong sort order */
1728 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1729 cmp_map_compat, NULL);
1730 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1731
Dan Williamseaf96152015-05-01 13:11:27 -04001732 ndr_desc->nd_set = nd_set;
1733 devm_kfree(dev, info);
1734
1735 return 0;
1736}
1737
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001738static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1739{
1740 struct acpi_nfit_interleave *idt = mmio->idt;
1741 u32 sub_line_offset, line_index, line_offset;
1742 u64 line_no, table_skip_count, table_offset;
1743
1744 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1745 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1746 line_offset = idt->line_offset[line_index]
1747 * mmio->line_size;
1748 table_offset = table_skip_count * mmio->table_size;
1749
1750 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1751}
1752
Ross Zwislerde4a1962015-08-20 16:27:38 -06001753static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001754{
1755 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1756 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001757 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001758
1759 if (mmio->num_lines)
1760 offset = to_interleave_offset(offset, mmio);
1761
Ross Zwisler68202c92016-07-29 14:59:12 -06001762 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001763}
1764
1765static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1766 resource_size_t dpa, unsigned int len, unsigned int write)
1767{
1768 u64 cmd, offset;
1769 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1770
1771 enum {
1772 BCW_OFFSET_MASK = (1ULL << 48)-1,
1773 BCW_LEN_SHIFT = 48,
1774 BCW_LEN_MASK = (1ULL << 8) - 1,
1775 BCW_CMD_SHIFT = 56,
1776 };
1777
1778 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1779 len = len >> L1_CACHE_SHIFT;
1780 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1781 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1782
1783 offset = nfit_blk->cmd_offset + mmio->size * bw;
1784 if (mmio->num_lines)
1785 offset = to_interleave_offset(offset, mmio);
1786
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001787 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001788 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001789
Dan Williamsaef25332016-02-12 17:01:11 -08001790 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001791 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001792}
1793
1794static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1795 resource_size_t dpa, void *iobuf, size_t len, int rw,
1796 unsigned int lane)
1797{
1798 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1799 unsigned int copied = 0;
1800 u64 base_offset;
1801 int rc;
1802
1803 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1804 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001805 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1806 while (len) {
1807 unsigned int c;
1808 u64 offset;
1809
1810 if (mmio->num_lines) {
1811 u32 line_offset;
1812
1813 offset = to_interleave_offset(base_offset + copied,
1814 mmio);
1815 div_u64_rem(offset, mmio->line_size, &line_offset);
1816 c = min_t(size_t, len, mmio->line_size - line_offset);
1817 } else {
1818 offset = base_offset + nfit_blk->bdw_offset;
1819 c = len;
1820 }
1821
1822 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001823 memcpy_to_pmem(mmio->addr.aperture + offset,
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001824 iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001825 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001826 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001827 mmio_flush_range((void __force *)
1828 mmio->addr.aperture + offset, c);
1829
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001830 memcpy_from_pmem(iobuf + copied,
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001831 mmio->addr.aperture + offset, c);
1832 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001833
1834 copied += c;
1835 len -= c;
1836 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001837
1838 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001839 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001840
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001841 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1842 return rc;
1843}
1844
1845static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1846 resource_size_t dpa, void *iobuf, u64 len, int rw)
1847{
1848 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1849 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1850 struct nd_region *nd_region = nfit_blk->nd_region;
1851 unsigned int lane, copied = 0;
1852 int rc = 0;
1853
1854 lane = nd_region_acquire_lane(nd_region);
1855 while (len) {
1856 u64 c = min(len, mmio->size);
1857
1858 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1859 iobuf + copied, c, rw, lane);
1860 if (rc)
1861 break;
1862
1863 copied += c;
1864 len -= c;
1865 }
1866 nd_region_release_lane(nd_region, lane);
1867
1868 return rc;
1869}
1870
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001871static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1872 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1873{
1874 if (idt) {
1875 mmio->num_lines = idt->line_count;
1876 mmio->line_size = idt->line_size;
1877 if (interleave_ways == 0)
1878 return -ENXIO;
1879 mmio->table_size = mmio->num_lines * interleave_ways
1880 * mmio->line_size;
1881 }
1882
1883 return 0;
1884}
1885
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001886static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1887 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1888{
1889 struct nd_cmd_dimm_flags flags;
1890 int rc;
1891
1892 memset(&flags, 0, sizeof(flags));
1893 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08001894 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001895
1896 if (rc >= 0 && flags.status == 0)
1897 nfit_blk->dimm_flags = flags.flags;
1898 else if (rc == -ENOTTY) {
1899 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08001900 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001901 rc = 0;
1902 } else
1903 rc = -ENXIO;
1904
1905 return rc;
1906}
1907
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001908static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1909 struct device *dev)
1910{
1911 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001912 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1913 struct nfit_blk_mmio *mmio;
1914 struct nfit_blk *nfit_blk;
1915 struct nfit_mem *nfit_mem;
1916 struct nvdimm *nvdimm;
1917 int rc;
1918
1919 nvdimm = nd_blk_region_to_dimm(ndbr);
1920 nfit_mem = nvdimm_provider_data(nvdimm);
1921 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1922 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1923 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04001924 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1925 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001926 return -ENXIO;
1927 }
1928
1929 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1930 if (!nfit_blk)
1931 return -ENOMEM;
1932 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1933 nfit_blk->nd_region = to_nd_region(dev);
1934
1935 /* map block aperture memory */
1936 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1937 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07001938 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
1939 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001940 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001941 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1942 nvdimm_name(nvdimm));
1943 return -ENOMEM;
1944 }
1945 mmio->size = nfit_mem->bdw->size;
1946 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1947 mmio->idt = nfit_mem->idt_bdw;
1948 mmio->spa = nfit_mem->spa_bdw;
1949 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1950 nfit_mem->memdev_bdw->interleave_ways);
1951 if (rc) {
1952 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1953 __func__, nvdimm_name(nvdimm));
1954 return rc;
1955 }
1956
1957 /* map block control memory */
1958 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1959 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1960 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07001961 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1962 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001963 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001964 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1965 nvdimm_name(nvdimm));
1966 return -ENOMEM;
1967 }
1968 mmio->size = nfit_mem->dcr->window_size;
1969 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1970 mmio->idt = nfit_mem->idt_dcr;
1971 mmio->spa = nfit_mem->spa_dcr;
1972 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1973 nfit_mem->memdev_dcr->interleave_ways);
1974 if (rc) {
1975 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1976 __func__, nvdimm_name(nvdimm));
1977 return rc;
1978 }
1979
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001980 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1981 if (rc < 0) {
1982 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1983 __func__, nvdimm_name(nvdimm));
1984 return rc;
1985 }
1986
Dan Williamsf284a4f2016-07-07 19:44:50 -07001987 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001988 dev_warn(dev, "unable to guarantee persistence of writes\n");
1989
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001990 if (mmio->line_size == 0)
1991 return 0;
1992
1993 if ((u32) nfit_blk->cmd_offset % mmio->line_size
1994 + 8 > mmio->line_size) {
1995 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1996 return -ENXIO;
1997 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1998 + 8 > mmio->line_size) {
1999 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2000 return -ENXIO;
2001 }
2002
2003 return 0;
2004}
2005
Dan Williamsaef25332016-02-12 17:01:11 -08002006static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002007 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002008{
Dan Williamsaef25332016-02-12 17:01:11 -08002009 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002010 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08002011 int cmd_rc, rc;
2012
Dan Williams1cf03c02016-02-17 13:01:23 -08002013 cmd->address = spa->address;
2014 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08002015 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2016 sizeof(*cmd), &cmd_rc);
2017 if (rc < 0)
2018 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002019 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002020}
2021
Dan Williams1cf03c02016-02-17 13:01:23 -08002022static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07002023{
2024 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002025 int cmd_rc;
2026 struct nd_cmd_ars_start ars_start;
2027 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2028 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002029
Dan Williams1cf03c02016-02-17 13:01:23 -08002030 memset(&ars_start, 0, sizeof(ars_start));
2031 ars_start.address = spa->address;
2032 ars_start.length = spa->length;
2033 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2034 ars_start.type = ND_ARS_PERSISTENT;
2035 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2036 ars_start.type = ND_ARS_VOLATILE;
2037 else
2038 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07002039
Dan Williams1cf03c02016-02-17 13:01:23 -08002040 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2041 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08002042
Dan Williams1cf03c02016-02-17 13:01:23 -08002043 if (rc < 0)
2044 return rc;
2045 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002046}
2047
Dan Williams1cf03c02016-02-17 13:01:23 -08002048static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07002049{
Dan Williamsaef25332016-02-12 17:01:11 -08002050 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002051 struct nd_cmd_ars_start ars_start;
2052 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2053 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07002054
Dan Williams1cf03c02016-02-17 13:01:23 -08002055 memset(&ars_start, 0, sizeof(ars_start));
2056 ars_start.address = ars_status->restart_address;
2057 ars_start.length = ars_status->restart_length;
2058 ars_start.type = ars_status->type;
2059 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2060 sizeof(ars_start), &cmd_rc);
2061 if (rc < 0)
2062 return rc;
2063 return cmd_rc;
2064}
Dan Williamsaef25332016-02-12 17:01:11 -08002065
Dan Williams1cf03c02016-02-17 13:01:23 -08002066static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2067{
2068 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2069 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2070 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08002071
Dan Williams1cf03c02016-02-17 13:01:23 -08002072 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2073 acpi_desc->ars_status_size, &cmd_rc);
2074 if (rc < 0)
2075 return rc;
2076 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002077}
2078
Dan Williams82aa37c2016-12-06 12:45:24 -08002079static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002080 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07002081{
Dan Williams82aa37c2016-12-06 12:45:24 -08002082 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
Vishal Verma0caeef62015-12-24 19:21:43 -07002083 int rc;
2084 u32 i;
2085
Dan Williams82aa37c2016-12-06 12:45:24 -08002086 /*
2087 * First record starts at 44 byte offset from the start of the
2088 * payload.
2089 */
2090 if (ars_status->out_length < 44)
2091 return 0;
Vishal Verma0caeef62015-12-24 19:21:43 -07002092 for (i = 0; i < ars_status->num_records; i++) {
Dan Williams82aa37c2016-12-06 12:45:24 -08002093 /* only process full records */
2094 if (ars_status->out_length
2095 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2096 break;
Vishal Verma0caeef62015-12-24 19:21:43 -07002097 rc = nvdimm_bus_add_poison(nvdimm_bus,
2098 ars_status->records[i].err_address,
2099 ars_status->records[i].length);
2100 if (rc)
2101 return rc;
2102 }
Dan Williams82aa37c2016-12-06 12:45:24 -08002103 if (i < ars_status->num_records)
2104 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
Vishal Verma0caeef62015-12-24 19:21:43 -07002105
2106 return 0;
2107}
2108
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002109static void acpi_nfit_remove_resource(void *data)
2110{
2111 struct resource *res = data;
2112
2113 remove_resource(res);
2114}
2115
2116static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2117 struct nd_region_desc *ndr_desc)
2118{
2119 struct resource *res, *nd_res = ndr_desc->res;
2120 int is_pmem, ret;
2121
2122 /* No operation if the region is already registered as PMEM */
2123 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2124 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2125 if (is_pmem == REGION_INTERSECTS)
2126 return 0;
2127
2128 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2129 if (!res)
2130 return -ENOMEM;
2131
2132 res->name = "Persistent Memory";
2133 res->start = nd_res->start;
2134 res->end = nd_res->end;
2135 res->flags = IORESOURCE_MEM;
2136 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2137
2138 ret = insert_resource(&iomem_resource, res);
2139 if (ret)
2140 return ret;
2141
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302142 ret = devm_add_action_or_reset(acpi_desc->dev,
2143 acpi_nfit_remove_resource,
2144 res);
2145 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002146 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002147
2148 return 0;
2149}
2150
Dan Williams1f7df6f2015-06-09 20:13:14 -04002151static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002152 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002153 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002154 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002155{
2156 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2157 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002158 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002159 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002160 struct nfit_mem *nfit_mem;
2161 int blk_valid = 0;
2162
2163 if (!nvdimm) {
2164 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2165 spa->range_index, memdev->device_handle);
2166 return -ENODEV;
2167 }
2168
Dan Williams44c462e2016-09-19 16:38:50 -07002169 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002170 switch (nfit_spa_type(spa)) {
2171 case NFIT_SPA_PM:
2172 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002173 mapping->start = memdev->address;
2174 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002175 break;
2176 case NFIT_SPA_DCR:
2177 nfit_mem = nvdimm_provider_data(nvdimm);
2178 if (!nfit_mem || !nfit_mem->bdw) {
2179 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2180 spa->range_index, nvdimm_name(nvdimm));
2181 } else {
Dan Williams44c462e2016-09-19 16:38:50 -07002182 mapping->size = nfit_mem->bdw->capacity;
2183 mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04002184 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002185 blk_valid = 1;
2186 }
2187
Dan Williams44c462e2016-09-19 16:38:50 -07002188 ndr_desc->mapping = mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002189 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002190 ndbr_desc = to_blk_region_desc(ndr_desc);
2191 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002192 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williams1cf03c02016-02-17 13:01:23 -08002193 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2194 ndr_desc);
2195 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002196 return -ENOMEM;
2197 break;
2198 }
2199
2200 return 0;
2201}
2202
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002203static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2204{
2205 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2206 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2207 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2208 nfit_spa_type(spa) == NFIT_SPA_PCD);
2209}
2210
Dan Williams1f7df6f2015-06-09 20:13:14 -04002211static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2212 struct nfit_spa *nfit_spa)
2213{
Dan Williams44c462e2016-09-19 16:38:50 -07002214 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002215 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002216 struct nd_blk_region_desc ndbr_desc;
2217 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002218 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002219 struct nvdimm_bus *nvdimm_bus;
2220 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002221 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002222
Dan Williams1cf03c02016-02-17 13:01:23 -08002223 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002224 return 0;
2225
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002226 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04002227 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2228 __func__);
2229 return 0;
2230 }
2231
2232 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002233 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002234 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002235 res.start = spa->address;
2236 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002237 ndr_desc = &ndbr_desc.ndr_desc;
2238 ndr_desc->res = &res;
2239 ndr_desc->provider_data = nfit_spa;
2240 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002241 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2242 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2243 spa->proximity_domain);
2244 else
2245 ndr_desc->numa_node = NUMA_NO_NODE;
2246
Dan Williams1f7df6f2015-06-09 20:13:14 -04002247 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2248 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002249 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002250
2251 if (memdev->range_index != spa->range_index)
2252 continue;
2253 if (count >= ND_MAX_MAPPINGS) {
2254 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2255 spa->range_index, ND_MAX_MAPPINGS);
2256 return -ENXIO;
2257 }
Dan Williams44c462e2016-09-19 16:38:50 -07002258 mapping = &mappings[count++];
2259 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002260 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002261 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002262 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002263 }
2264
Dan Williams44c462e2016-09-19 16:38:50 -07002265 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002266 ndr_desc->num_mappings = count;
2267 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002268 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002269 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002270
Dan Williams1f7df6f2015-06-09 20:13:14 -04002271 nvdimm_bus = acpi_desc->nvdimm_bus;
2272 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002273 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002274 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002275 dev_warn(acpi_desc->dev,
2276 "failed to insert pmem resource to iomem: %d\n",
2277 rc);
Dan Williams48901162016-03-09 17:15:43 -08002278 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002279 }
Dan Williams48901162016-03-09 17:15:43 -08002280
Dan Williams1cf03c02016-02-17 13:01:23 -08002281 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2282 ndr_desc);
2283 if (!nfit_spa->nd_region)
2284 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002285 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002286 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2287 ndr_desc);
2288 if (!nfit_spa->nd_region)
2289 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002290 } else if (nfit_spa_is_virtual(spa)) {
2291 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2292 ndr_desc);
2293 if (!nfit_spa->nd_region)
2294 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002295 }
Vishal Verma20985162015-10-27 16:58:27 -06002296
Dan Williams1cf03c02016-02-17 13:01:23 -08002297 out:
2298 if (rc)
2299 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2300 nfit_spa->spa->range_index);
2301 return rc;
2302}
2303
2304static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2305 u32 max_ars)
2306{
2307 struct device *dev = acpi_desc->dev;
2308 struct nd_cmd_ars_status *ars_status;
2309
2310 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2311 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2312 return 0;
2313 }
2314
2315 if (acpi_desc->ars_status)
2316 devm_kfree(dev, acpi_desc->ars_status);
2317 acpi_desc->ars_status = NULL;
2318 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2319 if (!ars_status)
2320 return -ENOMEM;
2321 acpi_desc->ars_status = ars_status;
2322 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002323 return 0;
2324}
2325
Dan Williams1cf03c02016-02-17 13:01:23 -08002326static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2327 struct nfit_spa *nfit_spa)
2328{
2329 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2330 int rc;
2331
2332 if (!nfit_spa->max_ars) {
2333 struct nd_cmd_ars_cap ars_cap;
2334
2335 memset(&ars_cap, 0, sizeof(ars_cap));
2336 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2337 if (rc < 0)
2338 return rc;
2339 nfit_spa->max_ars = ars_cap.max_ars_out;
2340 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2341 /* check that the supported scrub types match the spa type */
2342 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2343 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2344 return -ENOTTY;
2345 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2346 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2347 return -ENOTTY;
2348 }
2349
2350 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2351 return -ENOMEM;
2352
2353 rc = ars_get_status(acpi_desc);
2354 if (rc < 0 && rc != -ENOSPC)
2355 return rc;
2356
Dan Williams82aa37c2016-12-06 12:45:24 -08002357 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
Dan Williams1cf03c02016-02-17 13:01:23 -08002358 return -ENOMEM;
2359
2360 return 0;
2361}
2362
2363static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2364 struct nfit_spa *nfit_spa)
2365{
2366 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2367 unsigned int overflow_retry = scrub_overflow_abort;
2368 u64 init_ars_start = 0, init_ars_len = 0;
2369 struct device *dev = acpi_desc->dev;
2370 unsigned int tmo = scrub_timeout;
2371 int rc;
2372
Vishal Verma37b137f2016-07-23 21:51:42 -07002373 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002374 return;
2375
2376 rc = ars_start(acpi_desc, nfit_spa);
2377 /*
2378 * If we timed out the initial scan we'll still be busy here,
2379 * and will wait another timeout before giving up permanently.
2380 */
2381 if (rc < 0 && rc != -EBUSY)
2382 return;
2383
2384 do {
2385 u64 ars_start, ars_len;
2386
2387 if (acpi_desc->cancel)
2388 break;
2389 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2390 if (rc == -ENOTTY)
2391 break;
2392 if (rc == -EBUSY && !tmo) {
2393 dev_warn(dev, "range %d ars timeout, aborting\n",
2394 spa->range_index);
2395 break;
2396 }
2397
2398 if (rc == -EBUSY) {
2399 /*
2400 * Note, entries may be appended to the list
2401 * while the lock is dropped, but the workqueue
2402 * being active prevents entries being deleted /
2403 * freed.
2404 */
2405 mutex_unlock(&acpi_desc->init_mutex);
2406 ssleep(1);
2407 tmo--;
2408 mutex_lock(&acpi_desc->init_mutex);
2409 continue;
2410 }
2411
2412 /* we got some results, but there are more pending... */
2413 if (rc == -ENOSPC && overflow_retry--) {
2414 if (!init_ars_len) {
2415 init_ars_len = acpi_desc->ars_status->length;
2416 init_ars_start = acpi_desc->ars_status->address;
2417 }
2418 rc = ars_continue(acpi_desc);
2419 }
2420
2421 if (rc < 0) {
2422 dev_warn(dev, "range %d ars continuation failed\n",
2423 spa->range_index);
2424 break;
2425 }
2426
2427 if (init_ars_len) {
2428 ars_start = init_ars_start;
2429 ars_len = init_ars_len;
2430 } else {
2431 ars_start = acpi_desc->ars_status->address;
2432 ars_len = acpi_desc->ars_status->length;
2433 }
2434 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2435 spa->range_index, ars_start, ars_len);
2436 /* notify the region about new poison entries */
2437 nvdimm_region_notify(nfit_spa->nd_region,
2438 NVDIMM_REVALIDATE_POISON);
2439 break;
2440 } while (1);
2441}
2442
2443static void acpi_nfit_scrub(struct work_struct *work)
2444{
2445 struct device *dev;
2446 u64 init_scrub_length = 0;
2447 struct nfit_spa *nfit_spa;
2448 u64 init_scrub_address = 0;
2449 bool init_ars_done = false;
2450 struct acpi_nfit_desc *acpi_desc;
2451 unsigned int tmo = scrub_timeout;
2452 unsigned int overflow_retry = scrub_overflow_abort;
2453
2454 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2455 dev = acpi_desc->dev;
2456
2457 /*
2458 * We scrub in 2 phases. The first phase waits for any platform
2459 * firmware initiated scrubs to complete and then we go search for the
2460 * affected spa regions to mark them scanned. In the second phase we
2461 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002462 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2463 * the first phase, but really only care about running phase 2, where
2464 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002465 */
2466
2467 /* process platform firmware initiated scrubs */
2468 retry:
2469 mutex_lock(&acpi_desc->init_mutex);
2470 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2471 struct nd_cmd_ars_status *ars_status;
2472 struct acpi_nfit_system_address *spa;
2473 u64 ars_start, ars_len;
2474 int rc;
2475
2476 if (acpi_desc->cancel)
2477 break;
2478
2479 if (nfit_spa->nd_region)
2480 continue;
2481
2482 if (init_ars_done) {
2483 /*
2484 * No need to re-query, we're now just
2485 * reconciling all the ranges covered by the
2486 * initial scrub
2487 */
2488 rc = 0;
2489 } else
2490 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2491
2492 if (rc == -ENOTTY) {
2493 /* no ars capability, just register spa and move on */
2494 acpi_nfit_register_region(acpi_desc, nfit_spa);
2495 continue;
2496 }
2497
2498 if (rc == -EBUSY && !tmo) {
2499 /* fallthrough to directed scrub in phase 2 */
2500 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2501 break;
2502 } else if (rc == -EBUSY) {
2503 mutex_unlock(&acpi_desc->init_mutex);
2504 ssleep(1);
2505 tmo--;
2506 goto retry;
2507 }
2508
2509 /* we got some results, but there are more pending... */
2510 if (rc == -ENOSPC && overflow_retry--) {
2511 ars_status = acpi_desc->ars_status;
2512 /*
2513 * Record the original scrub range, so that we
2514 * can recall all the ranges impacted by the
2515 * initial scrub.
2516 */
2517 if (!init_scrub_length) {
2518 init_scrub_length = ars_status->length;
2519 init_scrub_address = ars_status->address;
2520 }
2521 rc = ars_continue(acpi_desc);
2522 if (rc == 0) {
2523 mutex_unlock(&acpi_desc->init_mutex);
2524 goto retry;
2525 }
2526 }
2527
2528 if (rc < 0) {
2529 /*
2530 * Initial scrub failed, we'll give it one more
2531 * try below...
2532 */
2533 break;
2534 }
2535
2536 /* We got some final results, record completed ranges */
2537 ars_status = acpi_desc->ars_status;
2538 if (init_scrub_length) {
2539 ars_start = init_scrub_address;
2540 ars_len = ars_start + init_scrub_length;
2541 } else {
2542 ars_start = ars_status->address;
2543 ars_len = ars_status->length;
2544 }
2545 spa = nfit_spa->spa;
2546
2547 if (!init_ars_done) {
2548 init_ars_done = true;
2549 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2550 ars_start, ars_len);
2551 }
2552 if (ars_start <= spa->address && ars_start + ars_len
2553 >= spa->address + spa->length)
2554 acpi_nfit_register_region(acpi_desc, nfit_spa);
2555 }
2556
2557 /*
2558 * For all the ranges not covered by an initial scrub we still
2559 * want to see if there are errors, but it's ok to discover them
2560 * asynchronously.
2561 */
2562 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2563 /*
2564 * Flag all the ranges that still need scrubbing, but
2565 * register them now to make data available.
2566 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002567 if (!nfit_spa->nd_region) {
2568 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002569 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002570 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002571 }
2572
2573 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2574 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002575 acpi_desc->scrub_count++;
2576 if (acpi_desc->scrub_count_state)
2577 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002578 mutex_unlock(&acpi_desc->init_mutex);
2579}
2580
Dan Williams1f7df6f2015-06-09 20:13:14 -04002581static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2582{
2583 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002584 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002585
Dan Williams1cf03c02016-02-17 13:01:23 -08002586 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2587 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2588 /* BLK regions don't need to wait for ars results */
2589 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2590 if (rc)
2591 return rc;
2592 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002593
Dan Williams1cf03c02016-02-17 13:01:23 -08002594 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002595 return 0;
2596}
2597
Vishal Verma20985162015-10-27 16:58:27 -06002598static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2599 struct nfit_table_prev *prev)
2600{
2601 struct device *dev = acpi_desc->dev;
2602
2603 if (!list_empty(&prev->spas) ||
2604 !list_empty(&prev->memdevs) ||
2605 !list_empty(&prev->dcrs) ||
2606 !list_empty(&prev->bdws) ||
2607 !list_empty(&prev->idts) ||
2608 !list_empty(&prev->flushes)) {
2609 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2610 return -ENXIO;
2611 }
2612 return 0;
2613}
2614
Vishal Verma37b137f2016-07-23 21:51:42 -07002615static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2616{
2617 struct device *dev = acpi_desc->dev;
2618 struct kernfs_node *nfit;
2619 struct device *bus_dev;
2620
2621 if (!ars_supported(acpi_desc->nvdimm_bus))
2622 return 0;
2623
2624 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2625 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2626 if (!nfit) {
2627 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2628 return -ENODEV;
2629 }
2630 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2631 sysfs_put(nfit);
2632 if (!acpi_desc->scrub_count_state) {
2633 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2634 return -ENODEV;
2635 }
2636
2637 return 0;
2638}
2639
Dan Williams58cd71b2016-07-21 18:05:36 -07002640static void acpi_nfit_destruct(void *data)
2641{
2642 struct acpi_nfit_desc *acpi_desc = data;
Vishal Verma37b137f2016-07-23 21:51:42 -07002643 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002644
Vishal Verma6839a6d2016-07-23 21:51:21 -07002645 /*
2646 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2647 * race teardown
2648 */
2649 mutex_lock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002650 acpi_desc->cancel = 1;
Vishal Verma37b137f2016-07-23 21:51:42 -07002651 /*
2652 * Bounce the nvdimm bus lock to make sure any in-flight
2653 * acpi_nfit_ars_rescan() submissions have had a chance to
2654 * either submit or see ->cancel set.
2655 */
2656 device_lock(bus_dev);
2657 device_unlock(bus_dev);
2658
Dan Williams58cd71b2016-07-21 18:05:36 -07002659 flush_workqueue(nfit_wq);
Vishal Verma37b137f2016-07-23 21:51:42 -07002660 if (acpi_desc->scrub_count_state)
2661 sysfs_put(acpi_desc->scrub_count_state);
Dan Williams58cd71b2016-07-21 18:05:36 -07002662 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2663 acpi_desc->nvdimm_bus = NULL;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002664 list_del(&acpi_desc->list);
2665 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002666}
2667
Dan Williamse7a11b42016-07-14 16:19:55 -07002668int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002669{
2670 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002671 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002672 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002673 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002674
Dan Williams58cd71b2016-07-21 18:05:36 -07002675 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002676 acpi_nfit_init_dsms(acpi_desc);
2677
Dan Williams58cd71b2016-07-21 18:05:36 -07002678 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2679 &acpi_desc->nd_desc);
2680 if (!acpi_desc->nvdimm_bus)
2681 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002682
Dan Williams58cd71b2016-07-21 18:05:36 -07002683 rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
2684 acpi_desc);
2685 if (rc)
2686 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002687
2688 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2689 if (rc)
2690 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002691
2692 /* register this acpi_desc for mce notifications */
2693 mutex_lock(&acpi_desc_lock);
2694 list_add_tail(&acpi_desc->list, &acpi_descs);
2695 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002696 }
2697
Vishal Verma20985162015-10-27 16:58:27 -06002698 mutex_lock(&acpi_desc->init_mutex);
2699
2700 INIT_LIST_HEAD(&prev.spas);
2701 INIT_LIST_HEAD(&prev.memdevs);
2702 INIT_LIST_HEAD(&prev.dcrs);
2703 INIT_LIST_HEAD(&prev.bdws);
2704 INIT_LIST_HEAD(&prev.idts);
2705 INIT_LIST_HEAD(&prev.flushes);
2706
2707 list_cut_position(&prev.spas, &acpi_desc->spas,
2708 acpi_desc->spas.prev);
2709 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2710 acpi_desc->memdevs.prev);
2711 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2712 acpi_desc->dcrs.prev);
2713 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2714 acpi_desc->bdws.prev);
2715 list_cut_position(&prev.idts, &acpi_desc->idts,
2716 acpi_desc->idts.prev);
2717 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2718 acpi_desc->flushes.prev);
2719
Vishal Verma20985162015-10-27 16:58:27 -06002720 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002721 while (!IS_ERR_OR_NULL(data))
2722 data = add_table(acpi_desc, &prev, data, end);
2723
2724 if (IS_ERR(data)) {
2725 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2726 PTR_ERR(data));
2727 rc = PTR_ERR(data);
2728 goto out_unlock;
2729 }
2730
2731 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2732 if (rc)
2733 goto out_unlock;
2734
Dan Williams81ed4e32016-06-10 18:20:53 -07002735 rc = nfit_mem_init(acpi_desc);
2736 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002737 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002738
2739 rc = acpi_nfit_register_dimms(acpi_desc);
2740 if (rc)
2741 goto out_unlock;
2742
2743 rc = acpi_nfit_register_regions(acpi_desc);
2744
2745 out_unlock:
2746 mutex_unlock(&acpi_desc->init_mutex);
2747 return rc;
2748}
2749EXPORT_SYMBOL_GPL(acpi_nfit_init);
2750
Dan Williams7ae0fa432016-02-19 12:16:34 -08002751struct acpi_nfit_flush_work {
2752 struct work_struct work;
2753 struct completion cmp;
2754};
2755
2756static void flush_probe(struct work_struct *work)
2757{
2758 struct acpi_nfit_flush_work *flush;
2759
2760 flush = container_of(work, typeof(*flush), work);
2761 complete(&flush->cmp);
2762}
2763
2764static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2765{
2766 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2767 struct device *dev = acpi_desc->dev;
2768 struct acpi_nfit_flush_work flush;
Dan Williamse4714862017-02-02 10:31:00 -08002769 int rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002770
2771 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2772 device_lock(dev);
2773 device_unlock(dev);
2774
2775 /*
2776 * Scrub work could take 10s of seconds, userspace may give up so we
2777 * need to be interruptible while waiting.
2778 */
2779 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2780 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2781 queue_work(nfit_wq, &flush.work);
Dan Williamse4714862017-02-02 10:31:00 -08002782
2783 rc = wait_for_completion_interruptible(&flush.cmp);
2784 cancel_work_sync(&flush.work);
2785 return rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002786}
2787
Dan Williams87bf5722016-02-22 21:50:31 -08002788static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2789 struct nvdimm *nvdimm, unsigned int cmd)
2790{
2791 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2792
2793 if (nvdimm)
2794 return 0;
2795 if (cmd != ND_CMD_ARS_START)
2796 return 0;
2797
2798 /*
2799 * The kernel and userspace may race to initiate a scrub, but
2800 * the scrub thread is prepared to lose that initial race. It
2801 * just needs guarantees that any ars it initiates are not
2802 * interrupted by any intervening start reqeusts from userspace.
2803 */
2804 if (work_busy(&acpi_desc->work))
2805 return -EBUSY;
2806
2807 return 0;
2808}
2809
Vishal Verma6839a6d2016-07-23 21:51:21 -07002810int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
Vishal Verma37b137f2016-07-23 21:51:42 -07002811{
2812 struct device *dev = acpi_desc->dev;
2813 struct nfit_spa *nfit_spa;
2814
2815 if (work_busy(&acpi_desc->work))
2816 return -EBUSY;
2817
2818 if (acpi_desc->cancel)
2819 return 0;
2820
2821 mutex_lock(&acpi_desc->init_mutex);
2822 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2823 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2824
2825 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2826 continue;
2827
2828 nfit_spa->ars_required = 1;
2829 }
2830 queue_work(nfit_wq, &acpi_desc->work);
2831 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2832 mutex_unlock(&acpi_desc->init_mutex);
2833
2834 return 0;
2835}
2836
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002837void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002838{
2839 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002840
2841 dev_set_drvdata(dev, acpi_desc);
2842 acpi_desc->dev = dev;
2843 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2844 nd_desc = &acpi_desc->nd_desc;
2845 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002846 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002847 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002848 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002849 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002850 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2851
Dan Williamsb94d5232015-05-19 22:54:31 -04002852 INIT_LIST_HEAD(&acpi_desc->spas);
2853 INIT_LIST_HEAD(&acpi_desc->dcrs);
2854 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002855 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002856 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04002857 INIT_LIST_HEAD(&acpi_desc->memdevs);
2858 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002859 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06002860 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08002861 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04002862}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002863EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04002864
Dan Williams3c87f372017-04-03 13:52:14 -07002865static void acpi_nfit_put_table(void *table)
2866{
2867 acpi_put_table(table);
2868}
2869
Dan Williamsb94d5232015-05-19 22:54:31 -04002870static int acpi_nfit_add(struct acpi_device *adev)
2871{
Vishal Verma20985162015-10-27 16:58:27 -06002872 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04002873 struct acpi_nfit_desc *acpi_desc;
2874 struct device *dev = &adev->dev;
2875 struct acpi_table_header *tbl;
2876 acpi_status status = AE_OK;
2877 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07002878 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002879
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002880 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
Dan Williamsb94d5232015-05-19 22:54:31 -04002881 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06002882 /* This is ok, we could have an nvdimm hotplugged later */
2883 dev_dbg(dev, "failed to find NFIT at startup\n");
2884 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002885 }
Dan Williams3c87f372017-04-03 13:52:14 -07002886
2887 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
2888 if (rc)
2889 return rc;
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002890 sz = tbl->length;
Dan Williamsb94d5232015-05-19 22:54:31 -04002891
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002892 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2893 if (!acpi_desc)
2894 return -ENOMEM;
2895 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04002896
Dan Williamse7a11b42016-07-14 16:19:55 -07002897 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05002898 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04002899
Vishal Verma20985162015-10-27 16:58:27 -06002900 /* Evaluate _FIT and override with that if present */
2901 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2902 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002903 union acpi_object *obj = buf.pointer;
2904
2905 if (obj->type == ACPI_TYPE_BUFFER)
2906 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2907 obj->buffer.length);
2908 else
Linda Knippers6b577c92015-11-20 19:05:49 -05002909 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2910 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07002911 kfree(buf.pointer);
2912 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07002913 /* skip over the lead-in header table */
2914 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2915 + sizeof(struct acpi_table_nfit),
2916 sz - sizeof(struct acpi_table_nfit));
Dan Williamse7a11b42016-07-14 16:19:55 -07002917 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002918}
2919
2920static int acpi_nfit_remove(struct acpi_device *adev)
2921{
Dan Williams58cd71b2016-07-21 18:05:36 -07002922 /* see acpi_nfit_destruct */
Dan Williamsb94d5232015-05-19 22:54:31 -04002923 return 0;
2924}
2925
Dan Williamsc14a8682016-08-18 22:15:04 -07002926void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
Vishal Verma20985162015-10-27 16:58:27 -06002927{
Dan Williamsc14a8682016-08-18 22:15:04 -07002928 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06002929 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07002930 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06002931 acpi_status status;
2932 int ret;
2933
2934 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2935
Vishal Vermac09f1212016-08-19 14:40:58 -06002936 if (event != NFIT_NOTIFY_UPDATE)
2937 return;
2938
Vishal Verma20985162015-10-27 16:58:27 -06002939 if (!dev->driver) {
2940 /* dev->driver may be null if we're being removed */
2941 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Dan Williamsc14a8682016-08-18 22:15:04 -07002942 return;
Vishal Verma20985162015-10-27 16:58:27 -06002943 }
2944
2945 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002946 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2947 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07002948 return;
2949 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002950 } else {
2951 /*
2952 * Finish previous registration before considering new
2953 * regions.
2954 */
2955 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06002956 }
2957
2958 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07002959 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06002960 if (ACPI_FAILURE(status)) {
2961 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07002962 return;
Vishal Verma20985162015-10-27 16:58:27 -06002963 }
2964
Linda Knippers6b577c92015-11-20 19:05:49 -05002965 obj = buf.pointer;
2966 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002967 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2968 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07002969 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05002970 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07002971 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05002972 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06002973 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07002974}
2975EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06002976
Dan Williamsc14a8682016-08-18 22:15:04 -07002977static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2978{
2979 device_lock(&adev->dev);
2980 __acpi_nfit_notify(&adev->dev, adev->handle, event);
2981 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06002982}
2983
Dan Williamsb94d5232015-05-19 22:54:31 -04002984static const struct acpi_device_id acpi_nfit_ids[] = {
2985 { "ACPI0012", 0 },
2986 { "", 0 },
2987};
2988MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
2989
2990static struct acpi_driver acpi_nfit_driver = {
2991 .name = KBUILD_MODNAME,
2992 .ids = acpi_nfit_ids,
2993 .ops = {
2994 .add = acpi_nfit_add,
2995 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06002996 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04002997 },
2998};
2999
3000static __init int nfit_init(void)
3001{
3002 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3003 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3004 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3005 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3006 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3007 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3008 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3009
3010 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
3011 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
3012 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
3013 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
3014 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
3015 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
3016 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
3017 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
3018 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
3019 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
Dan Williams31eca762016-04-28 16:23:43 -07003020 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3021 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
stuart hayese02fb722016-05-26 11:38:41 -05003022 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04003023
Dan Williams7ae0fa432016-02-19 12:16:34 -08003024 nfit_wq = create_singlethread_workqueue("nfit");
3025 if (!nfit_wq)
3026 return -ENOMEM;
3027
Vishal Verma6839a6d2016-07-23 21:51:21 -07003028 nfit_mce_register();
3029
Dan Williamsb94d5232015-05-19 22:54:31 -04003030 return acpi_bus_register_driver(&acpi_nfit_driver);
3031}
3032
3033static __exit void nfit_exit(void)
3034{
Vishal Verma6839a6d2016-07-23 21:51:21 -07003035 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04003036 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003037 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003038 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04003039}
3040
3041module_init(nfit_init);
3042module_exit(nfit_exit);
3043MODULE_LICENSE("GPL v2");
3044MODULE_AUTHOR("Intel Corporation");