blob: 05829de43b1d842d975f1fd2594a03dd1a71f918 [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
741static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
742 struct acpi_nfit_system_address *spa)
743{
744 struct nfit_mem *nfit_mem, *found;
745 struct nfit_memdev *nfit_memdev;
746 int type = nfit_spa_type(spa);
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:
753 return 0;
754 }
755
756 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700757 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800758 struct nfit_dcr *nfit_dcr;
759 u32 device_handle;
760 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400761
762 if (nfit_memdev->memdev->range_index != spa->range_index)
763 continue;
764 found = NULL;
765 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800766 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400767 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800768 if (__to_nfit_memdev(nfit_mem)->device_handle
769 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400770 found = nfit_mem;
771 break;
772 }
773
774 if (found)
775 nfit_mem = found;
776 else {
777 nfit_mem = devm_kzalloc(acpi_desc->dev,
778 sizeof(*nfit_mem), GFP_KERNEL);
779 if (!nfit_mem)
780 return -ENOMEM;
781 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700782 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800783 list_add(&nfit_mem->list, &acpi_desc->dimms);
784 }
785
786 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
787 if (nfit_dcr->dcr->region_index != dcr)
788 continue;
789 /*
790 * Record the control region for the dimm. For
791 * the ACPI 6.1 case, where there are separate
792 * control regions for the pmem vs blk
793 * interfaces, be sure to record the extended
794 * blk details.
795 */
796 if (!nfit_mem->dcr)
797 nfit_mem->dcr = nfit_dcr->dcr;
798 else if (nfit_mem->dcr->windows == 0
799 && nfit_dcr->dcr->windows)
800 nfit_mem->dcr = nfit_dcr->dcr;
801 break;
802 }
803
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700804 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700805 struct acpi_nfit_flush_address *flush;
806 u16 i;
807
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700808 if (nfit_flush->flush->device_handle != device_handle)
809 continue;
810 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700811 flush = nfit_flush->flush;
812 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
813 flush->hint_count
814 * sizeof(struct resource), GFP_KERNEL);
815 if (!nfit_mem->flush_wpq)
816 return -ENOMEM;
817 for (i = 0; i < flush->hint_count; i++) {
818 struct resource *res = &nfit_mem->flush_wpq[i];
819
820 res->start = flush->hint_address[i];
821 res->end = res->start + 8 - 1;
822 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700823 break;
824 }
825
Dan Williams6697b2c2016-02-04 16:51:00 -0800826 if (dcr && !nfit_mem->dcr) {
827 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
828 spa->range_index, dcr);
829 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400830 }
831
832 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400833 struct nfit_idt *nfit_idt;
834 u16 idt_idx;
835
Dan Williamsb94d5232015-05-19 22:54:31 -0400836 /* multiple dimms may share a SPA when interleaved */
837 nfit_mem->spa_dcr = spa;
838 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400839 idt_idx = nfit_memdev->memdev->interleave_index;
840 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
841 if (nfit_idt->idt->interleave_index != idt_idx)
842 continue;
843 nfit_mem->idt_dcr = nfit_idt->idt;
844 break;
845 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800846 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400847 } else {
848 /*
849 * A single dimm may belong to multiple SPA-PM
850 * ranges, record at least one in addition to
851 * any SPA-DCR range.
852 */
853 nfit_mem->memdev_pmem = nfit_memdev->memdev;
854 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400855 }
856
857 return 0;
858}
859
860static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
861{
862 struct nfit_mem *a = container_of(_a, typeof(*a), list);
863 struct nfit_mem *b = container_of(_b, typeof(*b), list);
864 u32 handleA, handleB;
865
866 handleA = __to_nfit_memdev(a)->device_handle;
867 handleB = __to_nfit_memdev(b)->device_handle;
868 if (handleA < handleB)
869 return -1;
870 else if (handleA > handleB)
871 return 1;
872 return 0;
873}
874
875static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
876{
877 struct nfit_spa *nfit_spa;
878
879 /*
880 * For each SPA-DCR or SPA-PMEM address range find its
881 * corresponding MEMDEV(s). From each MEMDEV find the
882 * corresponding DCR. Then, if we're operating on a SPA-DCR,
883 * try to find a SPA-BDW and a corresponding BDW that references
884 * the DCR. Throw it all into an nfit_mem object. Note, that
885 * BDWs are optional.
886 */
887 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
888 int rc;
889
890 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
891 if (rc)
892 return rc;
893 }
894
895 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
896
897 return 0;
898}
899
Dan Williams45def222015-04-26 19:26:48 -0400900static ssize_t revision_show(struct device *dev,
901 struct device_attribute *attr, char *buf)
902{
903 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
904 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
905 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
906
Linda Knippers6b577c92015-11-20 19:05:49 -0500907 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400908}
909static DEVICE_ATTR_RO(revision);
910
Vishal Verma9ffd6352016-09-30 17:19:29 -0600911static ssize_t hw_error_scrub_show(struct device *dev,
912 struct device_attribute *attr, char *buf)
913{
914 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
915 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
916 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
917
918 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
919}
920
921/*
922 * The 'hw_error_scrub' attribute can have the following values written to it:
923 * '0': Switch to the default mode where an exception will only insert
924 * the address of the memory error into the poison and badblocks lists.
925 * '1': Enable a full scrub to happen if an exception for a memory error is
926 * received.
927 */
928static ssize_t hw_error_scrub_store(struct device *dev,
929 struct device_attribute *attr, const char *buf, size_t size)
930{
931 struct nvdimm_bus_descriptor *nd_desc;
932 ssize_t rc;
933 long val;
934
935 rc = kstrtol(buf, 0, &val);
936 if (rc)
937 return rc;
938
939 device_lock(dev);
940 nd_desc = dev_get_drvdata(dev);
941 if (nd_desc) {
942 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
943
944 switch (val) {
945 case HW_ERROR_SCRUB_ON:
946 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
947 break;
948 case HW_ERROR_SCRUB_OFF:
949 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
950 break;
951 default:
952 rc = -EINVAL;
953 break;
954 }
955 }
956 device_unlock(dev);
957 if (rc)
958 return rc;
959 return size;
960}
961static DEVICE_ATTR_RW(hw_error_scrub);
962
Vishal Verma37b137f2016-07-23 21:51:42 -0700963/*
964 * This shows the number of full Address Range Scrubs that have been
965 * completed since driver load time. Userspace can wait on this using
966 * select/poll etc. A '+' at the end indicates an ARS is in progress
967 */
968static ssize_t scrub_show(struct device *dev,
969 struct device_attribute *attr, char *buf)
970{
971 struct nvdimm_bus_descriptor *nd_desc;
972 ssize_t rc = -ENXIO;
973
974 device_lock(dev);
975 nd_desc = dev_get_drvdata(dev);
976 if (nd_desc) {
977 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
978
979 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
980 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
981 }
982 device_unlock(dev);
983 return rc;
984}
985
Vishal Verma37b137f2016-07-23 21:51:42 -0700986static ssize_t scrub_store(struct device *dev,
987 struct device_attribute *attr, const char *buf, size_t size)
988{
989 struct nvdimm_bus_descriptor *nd_desc;
990 ssize_t rc;
991 long val;
992
993 rc = kstrtol(buf, 0, &val);
994 if (rc)
995 return rc;
996 if (val != 1)
997 return -EINVAL;
998
999 device_lock(dev);
1000 nd_desc = dev_get_drvdata(dev);
1001 if (nd_desc) {
1002 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1003
1004 rc = acpi_nfit_ars_rescan(acpi_desc);
1005 }
1006 device_unlock(dev);
1007 if (rc)
1008 return rc;
1009 return size;
1010}
1011static DEVICE_ATTR_RW(scrub);
1012
1013static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1014{
1015 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1016 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1017 | 1 << ND_CMD_ARS_STATUS;
1018
1019 return (nd_desc->cmd_mask & mask) == mask;
1020}
1021
1022static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1023{
1024 struct device *dev = container_of(kobj, struct device, kobj);
1025 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1026
1027 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1028 return 0;
1029 return a->mode;
1030}
1031
Dan Williams45def222015-04-26 19:26:48 -04001032static struct attribute *acpi_nfit_attributes[] = {
1033 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -07001034 &dev_attr_scrub.attr,
Vishal Verma9ffd6352016-09-30 17:19:29 -06001035 &dev_attr_hw_error_scrub.attr,
Dan Williams45def222015-04-26 19:26:48 -04001036 NULL,
1037};
1038
1039static struct attribute_group acpi_nfit_attribute_group = {
1040 .name = "nfit",
1041 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -07001042 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -04001043};
1044
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001045static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -04001046 &nvdimm_bus_attribute_group,
1047 &acpi_nfit_attribute_group,
1048 NULL,
1049};
1050
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001051static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1052{
1053 struct nvdimm *nvdimm = to_nvdimm(dev);
1054 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1055
1056 return __to_nfit_memdev(nfit_mem);
1057}
1058
1059static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1060{
1061 struct nvdimm *nvdimm = to_nvdimm(dev);
1062 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1063
1064 return nfit_mem->dcr;
1065}
1066
1067static ssize_t handle_show(struct device *dev,
1068 struct device_attribute *attr, char *buf)
1069{
1070 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1071
1072 return sprintf(buf, "%#x\n", memdev->device_handle);
1073}
1074static DEVICE_ATTR_RO(handle);
1075
1076static ssize_t phys_id_show(struct device *dev,
1077 struct device_attribute *attr, char *buf)
1078{
1079 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1080
1081 return sprintf(buf, "%#x\n", memdev->physical_id);
1082}
1083static DEVICE_ATTR_RO(phys_id);
1084
1085static ssize_t vendor_show(struct device *dev,
1086 struct device_attribute *attr, char *buf)
1087{
1088 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1089
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001090 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001091}
1092static DEVICE_ATTR_RO(vendor);
1093
1094static ssize_t rev_id_show(struct device *dev,
1095 struct device_attribute *attr, char *buf)
1096{
1097 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1098
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001099 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001100}
1101static DEVICE_ATTR_RO(rev_id);
1102
1103static ssize_t device_show(struct device *dev,
1104 struct device_attribute *attr, char *buf)
1105{
1106 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1107
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001108 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001109}
1110static DEVICE_ATTR_RO(device);
1111
Dan Williams6ca72082016-04-29 10:33:23 -07001112static ssize_t subsystem_vendor_show(struct device *dev,
1113 struct device_attribute *attr, char *buf)
1114{
1115 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1116
1117 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1118}
1119static DEVICE_ATTR_RO(subsystem_vendor);
1120
1121static ssize_t subsystem_rev_id_show(struct device *dev,
1122 struct device_attribute *attr, char *buf)
1123{
1124 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1125
1126 return sprintf(buf, "0x%04x\n",
1127 be16_to_cpu(dcr->subsystem_revision_id));
1128}
1129static DEVICE_ATTR_RO(subsystem_rev_id);
1130
1131static ssize_t subsystem_device_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1133{
1134 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1135
1136 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1137}
1138static DEVICE_ATTR_RO(subsystem_device);
1139
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001140static int num_nvdimm_formats(struct nvdimm *nvdimm)
1141{
1142 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1143 int formats = 0;
1144
1145 if (nfit_mem->memdev_pmem)
1146 formats++;
1147 if (nfit_mem->memdev_bdw)
1148 formats++;
1149 return formats;
1150}
1151
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001152static ssize_t format_show(struct device *dev,
1153 struct device_attribute *attr, char *buf)
1154{
1155 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1156
Dan Williams1bcbf422016-06-29 11:19:32 -07001157 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001158}
1159static DEVICE_ATTR_RO(format);
1160
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001161static ssize_t format1_show(struct device *dev,
1162 struct device_attribute *attr, char *buf)
1163{
1164 u32 handle;
1165 ssize_t rc = -ENXIO;
1166 struct nfit_mem *nfit_mem;
1167 struct nfit_memdev *nfit_memdev;
1168 struct acpi_nfit_desc *acpi_desc;
1169 struct nvdimm *nvdimm = to_nvdimm(dev);
1170 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1171
1172 nfit_mem = nvdimm_provider_data(nvdimm);
1173 acpi_desc = nfit_mem->acpi_desc;
1174 handle = to_nfit_memdev(dev)->device_handle;
1175
1176 /* assumes DIMMs have at most 2 published interface codes */
1177 mutex_lock(&acpi_desc->init_mutex);
1178 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1179 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1180 struct nfit_dcr *nfit_dcr;
1181
1182 if (memdev->device_handle != handle)
1183 continue;
1184
1185 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1186 if (nfit_dcr->dcr->region_index != memdev->region_index)
1187 continue;
1188 if (nfit_dcr->dcr->code == dcr->code)
1189 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001190 rc = sprintf(buf, "0x%04x\n",
1191 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001192 break;
1193 }
1194 if (rc != ENXIO)
1195 break;
1196 }
1197 mutex_unlock(&acpi_desc->init_mutex);
1198 return rc;
1199}
1200static DEVICE_ATTR_RO(format1);
1201
1202static ssize_t formats_show(struct device *dev,
1203 struct device_attribute *attr, char *buf)
1204{
1205 struct nvdimm *nvdimm = to_nvdimm(dev);
1206
1207 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1208}
1209static DEVICE_ATTR_RO(formats);
1210
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001211static ssize_t serial_show(struct device *dev,
1212 struct device_attribute *attr, char *buf)
1213{
1214 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1215
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001216 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001217}
1218static DEVICE_ATTR_RO(serial);
1219
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001220static ssize_t family_show(struct device *dev,
1221 struct device_attribute *attr, char *buf)
1222{
1223 struct nvdimm *nvdimm = to_nvdimm(dev);
1224 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1225
1226 if (nfit_mem->family < 0)
1227 return -ENXIO;
1228 return sprintf(buf, "%d\n", nfit_mem->family);
1229}
1230static DEVICE_ATTR_RO(family);
1231
1232static ssize_t dsm_mask_show(struct device *dev,
1233 struct device_attribute *attr, char *buf)
1234{
1235 struct nvdimm *nvdimm = to_nvdimm(dev);
1236 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1237
1238 if (nfit_mem->family < 0)
1239 return -ENXIO;
1240 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1241}
1242static DEVICE_ATTR_RO(dsm_mask);
1243
Dan Williams58138822015-06-23 20:08:34 -04001244static ssize_t flags_show(struct device *dev,
1245 struct device_attribute *attr, char *buf)
1246{
1247 u16 flags = to_nfit_memdev(dev)->flags;
1248
Dan Williamsffab9382017-04-13 15:05:30 -07001249 return sprintf(buf, "%s%s%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001250 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1251 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1252 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001253 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Dan Williamsffab9382017-04-13 15:05:30 -07001254 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1255 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1256 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
Dan Williams58138822015-06-23 20:08:34 -04001257}
1258static DEVICE_ATTR_RO(flags);
1259
Toshi Kani38a879b2016-04-25 15:34:59 -06001260static ssize_t id_show(struct device *dev,
1261 struct device_attribute *attr, char *buf)
1262{
1263 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1264
1265 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1266 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1267 be16_to_cpu(dcr->vendor_id),
1268 dcr->manufacturing_location,
1269 be16_to_cpu(dcr->manufacturing_date),
1270 be32_to_cpu(dcr->serial_number));
1271 else
1272 return sprintf(buf, "%04x-%08x\n",
1273 be16_to_cpu(dcr->vendor_id),
1274 be32_to_cpu(dcr->serial_number));
1275}
1276static DEVICE_ATTR_RO(id);
1277
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001278static struct attribute *acpi_nfit_dimm_attributes[] = {
1279 &dev_attr_handle.attr,
1280 &dev_attr_phys_id.attr,
1281 &dev_attr_vendor.attr,
1282 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001283 &dev_attr_rev_id.attr,
1284 &dev_attr_subsystem_vendor.attr,
1285 &dev_attr_subsystem_device.attr,
1286 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001287 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001288 &dev_attr_formats.attr,
1289 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001290 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001291 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001292 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001293 &dev_attr_family.attr,
1294 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001295 NULL,
1296};
1297
1298static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1299 struct attribute *a, int n)
1300{
1301 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001302 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001303
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001304 if (!to_nfit_dcr(dev))
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001305 return 0;
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001306 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1307 return 0;
1308 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001309}
1310
1311static struct attribute_group acpi_nfit_dimm_attribute_group = {
1312 .name = "nfit",
1313 .attrs = acpi_nfit_dimm_attributes,
1314 .is_visible = acpi_nfit_dimm_attr_visible,
1315};
1316
1317static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001318 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001319 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001320 &acpi_nfit_dimm_attribute_group,
1321 NULL,
1322};
1323
1324static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1325 u32 device_handle)
1326{
1327 struct nfit_mem *nfit_mem;
1328
1329 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1330 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1331 return nfit_mem->nvdimm;
1332
1333 return NULL;
1334}
1335
Dan Williams231bf112016-08-22 19:23:25 -07001336void __acpi_nvdimm_notify(struct device *dev, u32 event)
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001337{
1338 struct nfit_mem *nfit_mem;
1339 struct acpi_nfit_desc *acpi_desc;
1340
1341 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1342 event);
1343
1344 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1345 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1346 event);
1347 return;
1348 }
1349
1350 acpi_desc = dev_get_drvdata(dev->parent);
1351 if (!acpi_desc)
1352 return;
1353
1354 /*
1355 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1356 * is still valid.
1357 */
1358 nfit_mem = dev_get_drvdata(dev);
1359 if (nfit_mem && nfit_mem->flags_attr)
1360 sysfs_notify_dirent(nfit_mem->flags_attr);
1361}
Dan Williams231bf112016-08-22 19:23:25 -07001362EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001363
1364static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1365{
1366 struct acpi_device *adev = data;
1367 struct device *dev = &adev->dev;
1368
1369 device_lock(dev->parent);
1370 __acpi_nvdimm_notify(dev, event);
1371 device_unlock(dev->parent);
1372}
1373
Dan Williams62232e452015-06-08 14:27:06 -04001374static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1375 struct nfit_mem *nfit_mem, u32 device_handle)
1376{
1377 struct acpi_device *adev, *adev_dimm;
1378 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001379 unsigned long dsm_mask;
1380 const u8 *uuid;
Linda Knippers60e95f432015-07-22 16:17:22 -04001381 int i;
Linda Knippersba650cf2017-03-07 16:35:13 -05001382 int family = -1;
Dan Williams62232e452015-06-08 14:27:06 -04001383
Dan Williamse3654ec2016-04-28 16:17:07 -07001384 /* nfit test assumes 1:1 relationship between commands and dsms */
1385 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001386 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001387 adev = to_acpi_dev(acpi_desc);
1388 if (!adev)
1389 return 0;
1390
1391 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1392 nfit_mem->adev = adev_dimm;
1393 if (!adev_dimm) {
1394 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1395 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001396 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001397 }
1398
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001399 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1400 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1401 dev_err(dev, "%s: notification registration failed\n",
1402 dev_name(&adev_dimm->dev));
1403 return -ENXIO;
1404 }
1405
Dan Williams31eca762016-04-28 16:23:43 -07001406 /*
stuart hayese02fb722016-05-26 11:38:41 -05001407 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001408 * different command sets. Note, that checking for function0 (bit0)
1409 * tells us if any commands are reachable through this uuid.
Dan Williams31eca762016-04-28 16:23:43 -07001410 */
stuart hayese02fb722016-05-26 11:38:41 -05001411 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001412 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Linda Knippersba650cf2017-03-07 16:35:13 -05001413 if (family < 0 || i == default_dsm_family)
1414 family = i;
Dan Williams31eca762016-04-28 16:23:43 -07001415
1416 /* limit the supported commands to those that are publicly documented */
Linda Knippersba650cf2017-03-07 16:35:13 -05001417 nfit_mem->family = family;
Linda Knippers095ab4b2017-03-07 16:35:12 -05001418 if (override_dsm_mask && !disable_vendor_specific)
1419 dsm_mask = override_dsm_mask;
1420 else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001421 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001422 if (disable_vendor_specific)
1423 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001424 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001425 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001426 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001427 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001428 if (disable_vendor_specific)
1429 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001430 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1431 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001432 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001433 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001434 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001435 /* DSMs are optional, continue loading the driver... */
1436 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001437 }
1438
1439 uuid = to_nfit_uuid(nfit_mem->family);
1440 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Dan Williams62232e452015-06-08 14:27:06 -04001441 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
1442 set_bit(i, &nfit_mem->dsm_mask);
1443
Linda Knippers60e95f432015-07-22 16:17:22 -04001444 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001445}
1446
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001447static void shutdown_dimm_notify(void *data)
1448{
1449 struct acpi_nfit_desc *acpi_desc = data;
1450 struct nfit_mem *nfit_mem;
1451
1452 mutex_lock(&acpi_desc->init_mutex);
1453 /*
1454 * Clear out the nfit_mem->flags_attr and shut down dimm event
1455 * notifications.
1456 */
1457 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williams231bf112016-08-22 19:23:25 -07001458 struct acpi_device *adev_dimm = nfit_mem->adev;
1459
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001460 if (nfit_mem->flags_attr) {
1461 sysfs_put(nfit_mem->flags_attr);
1462 nfit_mem->flags_attr = NULL;
1463 }
Dan Williams231bf112016-08-22 19:23:25 -07001464 if (adev_dimm)
1465 acpi_remove_notify_handler(adev_dimm->handle,
1466 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001467 }
1468 mutex_unlock(&acpi_desc->init_mutex);
1469}
1470
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001471static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1472{
1473 struct nfit_mem *nfit_mem;
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001474 int dimm_count = 0, rc;
1475 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001476
1477 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001478 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001479 unsigned long flags = 0, cmd_mask;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001480 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001481 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001482
1483 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1484 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1485 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001486 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001487 continue;
1488 }
1489
1490 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1491 flags |= NDD_ALIASING;
1492
Dan Williams58138822015-06-23 20:08:34 -04001493 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001494 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams58138822015-06-23 20:08:34 -04001495 flags |= NDD_UNARMED;
1496
Dan Williams62232e452015-06-08 14:27:06 -04001497 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1498 if (rc)
1499 continue;
1500
Dan Williamse3654ec2016-04-28 16:17:07 -07001501 /*
Dan Williams31eca762016-04-28 16:23:43 -07001502 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1503 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1504 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001505 */
Dan Williams31eca762016-04-28 16:23:43 -07001506 cmd_mask = 1UL << ND_CMD_CALL;
1507 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1508 cmd_mask |= nfit_mem->dsm_mask;
1509
Dan Williamse5ae3b22016-06-07 17:00:04 -07001510 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1511 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001512 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001513 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001514 flags, cmd_mask, flush ? flush->hint_count : 0,
1515 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001516 if (!nvdimm)
1517 return -ENOMEM;
1518
1519 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001520 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001521
1522 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1523 continue;
1524
Toshi Kani402bae52015-08-26 10:20:23 -06001525 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001526 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001527 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1528 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1529 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001530 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
Dan Williams58138822015-06-23 20:08:34 -04001531
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001532 }
1533
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001534 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1535 if (rc)
1536 return rc;
1537
1538 /*
1539 * Now that dimms are successfully registered, and async registration
1540 * is flushed, attempt to enable event notification.
1541 */
1542 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1543 struct kernfs_node *nfit_kernfs;
1544
1545 nvdimm = nfit_mem->nvdimm;
1546 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1547 if (nfit_kernfs)
1548 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1549 "flags");
1550 sysfs_put(nfit_kernfs);
1551 if (!nfit_mem->flags_attr)
1552 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1553 nvdimm_name(nvdimm));
1554 }
1555
1556 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1557 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001558}
1559
Dan Williams62232e452015-06-08 14:27:06 -04001560static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1561{
1562 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1563 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1564 struct acpi_device *adev;
1565 int i;
1566
Dan Williamse3654ec2016-04-28 16:17:07 -07001567 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001568 adev = to_acpi_dev(acpi_desc);
1569 if (!adev)
1570 return;
1571
Dan Williamsd4f32362016-03-03 16:08:54 -08001572 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Dan Williams62232e452015-06-08 14:27:06 -04001573 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001574 set_bit(i, &nd_desc->cmd_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001575}
1576
Dan Williams1f7df6f2015-06-09 20:13:14 -04001577static ssize_t range_index_show(struct device *dev,
1578 struct device_attribute *attr, char *buf)
1579{
1580 struct nd_region *nd_region = to_nd_region(dev);
1581 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1582
1583 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1584}
1585static DEVICE_ATTR_RO(range_index);
1586
1587static struct attribute *acpi_nfit_region_attributes[] = {
1588 &dev_attr_range_index.attr,
1589 NULL,
1590};
1591
1592static struct attribute_group acpi_nfit_region_attribute_group = {
1593 .name = "nfit",
1594 .attrs = acpi_nfit_region_attributes,
1595};
1596
1597static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1598 &nd_region_attribute_group,
1599 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001600 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001601 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001602 &acpi_nfit_region_attribute_group,
1603 NULL,
1604};
1605
Dan Williamseaf96152015-05-01 13:11:27 -04001606/* enough info to uniquely specify an interleave set */
1607struct nfit_set_info {
1608 struct nfit_set_info_map {
1609 u64 region_offset;
1610 u32 serial_number;
1611 u32 pad;
1612 } mapping[0];
1613};
1614
1615static size_t sizeof_nfit_set_info(int num_mappings)
1616{
1617 return sizeof(struct nfit_set_info)
1618 + num_mappings * sizeof(struct nfit_set_info_map);
1619}
1620
Dan Williams86ef58a2017-02-28 18:32:48 -08001621static int cmp_map_compat(const void *m0, const void *m1)
Dan Williamseaf96152015-05-01 13:11:27 -04001622{
1623 const struct nfit_set_info_map *map0 = m0;
1624 const struct nfit_set_info_map *map1 = m1;
1625
1626 return memcmp(&map0->region_offset, &map1->region_offset,
1627 sizeof(u64));
1628}
1629
Dan Williams86ef58a2017-02-28 18:32:48 -08001630static int cmp_map(const void *m0, const void *m1)
1631{
1632 const struct nfit_set_info_map *map0 = m0;
1633 const struct nfit_set_info_map *map1 = m1;
1634
Dan Williamsb03b99a2017-03-27 21:53:38 -07001635 if (map0->region_offset < map1->region_offset)
1636 return -1;
1637 else if (map0->region_offset > map1->region_offset)
1638 return 1;
1639 return 0;
Dan Williams86ef58a2017-02-28 18:32:48 -08001640}
1641
Dan Williamseaf96152015-05-01 13:11:27 -04001642/* Retrieve the nth entry referencing this spa */
1643static struct acpi_nfit_memory_map *memdev_from_spa(
1644 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1645{
1646 struct nfit_memdev *nfit_memdev;
1647
1648 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1649 if (nfit_memdev->memdev->range_index == range_index)
1650 if (n-- == 0)
1651 return nfit_memdev->memdev;
1652 return NULL;
1653}
1654
1655static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1656 struct nd_region_desc *ndr_desc,
1657 struct acpi_nfit_system_address *spa)
1658{
1659 int i, spa_type = nfit_spa_type(spa);
1660 struct device *dev = acpi_desc->dev;
1661 struct nd_interleave_set *nd_set;
1662 u16 nr = ndr_desc->num_mappings;
1663 struct nfit_set_info *info;
1664
1665 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1666 /* pass */;
1667 else
1668 return 0;
1669
1670 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1671 if (!nd_set)
1672 return -ENOMEM;
1673
1674 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1675 if (!info)
1676 return -ENOMEM;
1677 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07001678 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04001679 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07001680 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04001681 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1682 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1683 spa->range_index, i);
1684
1685 if (!memdev || !nfit_mem->dcr) {
1686 dev_err(dev, "%s: failed to find DCR\n", __func__);
1687 return -ENODEV;
1688 }
1689
1690 map->region_offset = memdev->region_offset;
1691 map->serial_number = nfit_mem->dcr->serial_number;
1692 }
1693
1694 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1695 cmp_map, NULL);
1696 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
Dan Williams86ef58a2017-02-28 18:32:48 -08001697
1698 /* support namespaces created with the wrong sort order */
1699 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1700 cmp_map_compat, NULL);
1701 nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1702
Dan Williamseaf96152015-05-01 13:11:27 -04001703 ndr_desc->nd_set = nd_set;
1704 devm_kfree(dev, info);
1705
1706 return 0;
1707}
1708
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001709static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1710{
1711 struct acpi_nfit_interleave *idt = mmio->idt;
1712 u32 sub_line_offset, line_index, line_offset;
1713 u64 line_no, table_skip_count, table_offset;
1714
1715 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1716 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1717 line_offset = idt->line_offset[line_index]
1718 * mmio->line_size;
1719 table_offset = table_skip_count * mmio->table_size;
1720
1721 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1722}
1723
Ross Zwislerde4a1962015-08-20 16:27:38 -06001724static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001725{
1726 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1727 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001728 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001729
1730 if (mmio->num_lines)
1731 offset = to_interleave_offset(offset, mmio);
1732
Ross Zwisler68202c92016-07-29 14:59:12 -06001733 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001734}
1735
1736static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1737 resource_size_t dpa, unsigned int len, unsigned int write)
1738{
1739 u64 cmd, offset;
1740 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1741
1742 enum {
1743 BCW_OFFSET_MASK = (1ULL << 48)-1,
1744 BCW_LEN_SHIFT = 48,
1745 BCW_LEN_MASK = (1ULL << 8) - 1,
1746 BCW_CMD_SHIFT = 56,
1747 };
1748
1749 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1750 len = len >> L1_CACHE_SHIFT;
1751 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1752 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1753
1754 offset = nfit_blk->cmd_offset + mmio->size * bw;
1755 if (mmio->num_lines)
1756 offset = to_interleave_offset(offset, mmio);
1757
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001758 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001759 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001760
Dan Williamsaef25332016-02-12 17:01:11 -08001761 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001762 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001763}
1764
1765static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1766 resource_size_t dpa, void *iobuf, size_t len, int rw,
1767 unsigned int lane)
1768{
1769 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1770 unsigned int copied = 0;
1771 u64 base_offset;
1772 int rc;
1773
1774 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1775 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001776 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1777 while (len) {
1778 unsigned int c;
1779 u64 offset;
1780
1781 if (mmio->num_lines) {
1782 u32 line_offset;
1783
1784 offset = to_interleave_offset(base_offset + copied,
1785 mmio);
1786 div_u64_rem(offset, mmio->line_size, &line_offset);
1787 c = min_t(size_t, len, mmio->line_size - line_offset);
1788 } else {
1789 offset = base_offset + nfit_blk->bdw_offset;
1790 c = len;
1791 }
1792
1793 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001794 memcpy_to_pmem(mmio->addr.aperture + offset,
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001795 iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001796 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001797 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001798 mmio_flush_range((void __force *)
1799 mmio->addr.aperture + offset, c);
1800
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001801 memcpy_from_pmem(iobuf + copied,
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001802 mmio->addr.aperture + offset, c);
1803 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001804
1805 copied += c;
1806 len -= c;
1807 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001808
1809 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001810 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001811
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001812 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1813 return rc;
1814}
1815
1816static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1817 resource_size_t dpa, void *iobuf, u64 len, int rw)
1818{
1819 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1820 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1821 struct nd_region *nd_region = nfit_blk->nd_region;
1822 unsigned int lane, copied = 0;
1823 int rc = 0;
1824
1825 lane = nd_region_acquire_lane(nd_region);
1826 while (len) {
1827 u64 c = min(len, mmio->size);
1828
1829 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1830 iobuf + copied, c, rw, lane);
1831 if (rc)
1832 break;
1833
1834 copied += c;
1835 len -= c;
1836 }
1837 nd_region_release_lane(nd_region, lane);
1838
1839 return rc;
1840}
1841
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001842static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1843 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1844{
1845 if (idt) {
1846 mmio->num_lines = idt->line_count;
1847 mmio->line_size = idt->line_size;
1848 if (interleave_ways == 0)
1849 return -ENXIO;
1850 mmio->table_size = mmio->num_lines * interleave_ways
1851 * mmio->line_size;
1852 }
1853
1854 return 0;
1855}
1856
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001857static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1858 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1859{
1860 struct nd_cmd_dimm_flags flags;
1861 int rc;
1862
1863 memset(&flags, 0, sizeof(flags));
1864 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08001865 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001866
1867 if (rc >= 0 && flags.status == 0)
1868 nfit_blk->dimm_flags = flags.flags;
1869 else if (rc == -ENOTTY) {
1870 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08001871 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001872 rc = 0;
1873 } else
1874 rc = -ENXIO;
1875
1876 return rc;
1877}
1878
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001879static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1880 struct device *dev)
1881{
1882 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001883 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1884 struct nfit_blk_mmio *mmio;
1885 struct nfit_blk *nfit_blk;
1886 struct nfit_mem *nfit_mem;
1887 struct nvdimm *nvdimm;
1888 int rc;
1889
1890 nvdimm = nd_blk_region_to_dimm(ndbr);
1891 nfit_mem = nvdimm_provider_data(nvdimm);
1892 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1893 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1894 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04001895 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1896 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001897 return -ENXIO;
1898 }
1899
1900 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1901 if (!nfit_blk)
1902 return -ENOMEM;
1903 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1904 nfit_blk->nd_region = to_nd_region(dev);
1905
1906 /* map block aperture memory */
1907 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1908 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07001909 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
1910 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001911 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001912 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1913 nvdimm_name(nvdimm));
1914 return -ENOMEM;
1915 }
1916 mmio->size = nfit_mem->bdw->size;
1917 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1918 mmio->idt = nfit_mem->idt_bdw;
1919 mmio->spa = nfit_mem->spa_bdw;
1920 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1921 nfit_mem->memdev_bdw->interleave_ways);
1922 if (rc) {
1923 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1924 __func__, nvdimm_name(nvdimm));
1925 return rc;
1926 }
1927
1928 /* map block control memory */
1929 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1930 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1931 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07001932 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1933 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001934 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001935 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1936 nvdimm_name(nvdimm));
1937 return -ENOMEM;
1938 }
1939 mmio->size = nfit_mem->dcr->window_size;
1940 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1941 mmio->idt = nfit_mem->idt_dcr;
1942 mmio->spa = nfit_mem->spa_dcr;
1943 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1944 nfit_mem->memdev_dcr->interleave_ways);
1945 if (rc) {
1946 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1947 __func__, nvdimm_name(nvdimm));
1948 return rc;
1949 }
1950
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001951 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1952 if (rc < 0) {
1953 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1954 __func__, nvdimm_name(nvdimm));
1955 return rc;
1956 }
1957
Dan Williamsf284a4f2016-07-07 19:44:50 -07001958 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001959 dev_warn(dev, "unable to guarantee persistence of writes\n");
1960
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001961 if (mmio->line_size == 0)
1962 return 0;
1963
1964 if ((u32) nfit_blk->cmd_offset % mmio->line_size
1965 + 8 > mmio->line_size) {
1966 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1967 return -ENXIO;
1968 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1969 + 8 > mmio->line_size) {
1970 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
1971 return -ENXIO;
1972 }
1973
1974 return 0;
1975}
1976
Dan Williamsaef25332016-02-12 17:01:11 -08001977static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08001978 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001979{
Dan Williamsaef25332016-02-12 17:01:11 -08001980 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001981 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08001982 int cmd_rc, rc;
1983
Dan Williams1cf03c02016-02-17 13:01:23 -08001984 cmd->address = spa->address;
1985 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08001986 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
1987 sizeof(*cmd), &cmd_rc);
1988 if (rc < 0)
1989 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001990 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001991}
1992
Dan Williams1cf03c02016-02-17 13:01:23 -08001993static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001994{
1995 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001996 int cmd_rc;
1997 struct nd_cmd_ars_start ars_start;
1998 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1999 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002000
Dan Williams1cf03c02016-02-17 13:01:23 -08002001 memset(&ars_start, 0, sizeof(ars_start));
2002 ars_start.address = spa->address;
2003 ars_start.length = spa->length;
2004 if (nfit_spa_type(spa) == NFIT_SPA_PM)
2005 ars_start.type = ND_ARS_PERSISTENT;
2006 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2007 ars_start.type = ND_ARS_VOLATILE;
2008 else
2009 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07002010
Dan Williams1cf03c02016-02-17 13:01:23 -08002011 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2012 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08002013
Dan Williams1cf03c02016-02-17 13:01:23 -08002014 if (rc < 0)
2015 return rc;
2016 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002017}
2018
Dan Williams1cf03c02016-02-17 13:01:23 -08002019static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07002020{
Dan Williamsaef25332016-02-12 17:01:11 -08002021 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08002022 struct nd_cmd_ars_start ars_start;
2023 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2024 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07002025
Dan Williams1cf03c02016-02-17 13:01:23 -08002026 memset(&ars_start, 0, sizeof(ars_start));
2027 ars_start.address = ars_status->restart_address;
2028 ars_start.length = ars_status->restart_length;
2029 ars_start.type = ars_status->type;
2030 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2031 sizeof(ars_start), &cmd_rc);
2032 if (rc < 0)
2033 return rc;
2034 return cmd_rc;
2035}
Dan Williamsaef25332016-02-12 17:01:11 -08002036
Dan Williams1cf03c02016-02-17 13:01:23 -08002037static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2038{
2039 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2040 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2041 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08002042
Dan Williams1cf03c02016-02-17 13:01:23 -08002043 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2044 acpi_desc->ars_status_size, &cmd_rc);
2045 if (rc < 0)
2046 return rc;
2047 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002048}
2049
Dan Williams82aa37c2016-12-06 12:45:24 -08002050static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002051 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07002052{
Dan Williams82aa37c2016-12-06 12:45:24 -08002053 struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
Vishal Verma0caeef62015-12-24 19:21:43 -07002054 int rc;
2055 u32 i;
2056
Dan Williams82aa37c2016-12-06 12:45:24 -08002057 /*
2058 * First record starts at 44 byte offset from the start of the
2059 * payload.
2060 */
2061 if (ars_status->out_length < 44)
2062 return 0;
Vishal Verma0caeef62015-12-24 19:21:43 -07002063 for (i = 0; i < ars_status->num_records; i++) {
Dan Williams82aa37c2016-12-06 12:45:24 -08002064 /* only process full records */
2065 if (ars_status->out_length
2066 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2067 break;
Vishal Verma0caeef62015-12-24 19:21:43 -07002068 rc = nvdimm_bus_add_poison(nvdimm_bus,
2069 ars_status->records[i].err_address,
2070 ars_status->records[i].length);
2071 if (rc)
2072 return rc;
2073 }
Dan Williams82aa37c2016-12-06 12:45:24 -08002074 if (i < ars_status->num_records)
2075 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
Vishal Verma0caeef62015-12-24 19:21:43 -07002076
2077 return 0;
2078}
2079
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002080static void acpi_nfit_remove_resource(void *data)
2081{
2082 struct resource *res = data;
2083
2084 remove_resource(res);
2085}
2086
2087static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2088 struct nd_region_desc *ndr_desc)
2089{
2090 struct resource *res, *nd_res = ndr_desc->res;
2091 int is_pmem, ret;
2092
2093 /* No operation if the region is already registered as PMEM */
2094 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2095 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2096 if (is_pmem == REGION_INTERSECTS)
2097 return 0;
2098
2099 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2100 if (!res)
2101 return -ENOMEM;
2102
2103 res->name = "Persistent Memory";
2104 res->start = nd_res->start;
2105 res->end = nd_res->end;
2106 res->flags = IORESOURCE_MEM;
2107 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2108
2109 ret = insert_resource(&iomem_resource, res);
2110 if (ret)
2111 return ret;
2112
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302113 ret = devm_add_action_or_reset(acpi_desc->dev,
2114 acpi_nfit_remove_resource,
2115 res);
2116 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002117 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002118
2119 return 0;
2120}
2121
Dan Williams1f7df6f2015-06-09 20:13:14 -04002122static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002123 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002124 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002125 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002126{
2127 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2128 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002129 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002130 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002131 struct nfit_mem *nfit_mem;
2132 int blk_valid = 0;
2133
2134 if (!nvdimm) {
2135 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2136 spa->range_index, memdev->device_handle);
2137 return -ENODEV;
2138 }
2139
Dan Williams44c462e2016-09-19 16:38:50 -07002140 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002141 switch (nfit_spa_type(spa)) {
2142 case NFIT_SPA_PM:
2143 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002144 mapping->start = memdev->address;
2145 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002146 break;
2147 case NFIT_SPA_DCR:
2148 nfit_mem = nvdimm_provider_data(nvdimm);
2149 if (!nfit_mem || !nfit_mem->bdw) {
2150 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2151 spa->range_index, nvdimm_name(nvdimm));
2152 } else {
Dan Williams44c462e2016-09-19 16:38:50 -07002153 mapping->size = nfit_mem->bdw->capacity;
2154 mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04002155 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002156 blk_valid = 1;
2157 }
2158
Dan Williams44c462e2016-09-19 16:38:50 -07002159 ndr_desc->mapping = mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002160 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002161 ndbr_desc = to_blk_region_desc(ndr_desc);
2162 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002163 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williams1cf03c02016-02-17 13:01:23 -08002164 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2165 ndr_desc);
2166 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002167 return -ENOMEM;
2168 break;
2169 }
2170
2171 return 0;
2172}
2173
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002174static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2175{
2176 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2177 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2178 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2179 nfit_spa_type(spa) == NFIT_SPA_PCD);
2180}
2181
Dan Williams1f7df6f2015-06-09 20:13:14 -04002182static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2183 struct nfit_spa *nfit_spa)
2184{
Dan Williams44c462e2016-09-19 16:38:50 -07002185 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002186 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002187 struct nd_blk_region_desc ndbr_desc;
2188 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002189 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002190 struct nvdimm_bus *nvdimm_bus;
2191 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002192 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002193
Dan Williams1cf03c02016-02-17 13:01:23 -08002194 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002195 return 0;
2196
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002197 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04002198 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2199 __func__);
2200 return 0;
2201 }
2202
2203 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002204 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002205 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002206 res.start = spa->address;
2207 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002208 ndr_desc = &ndbr_desc.ndr_desc;
2209 ndr_desc->res = &res;
2210 ndr_desc->provider_data = nfit_spa;
2211 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002212 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2213 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2214 spa->proximity_domain);
2215 else
2216 ndr_desc->numa_node = NUMA_NO_NODE;
2217
Dan Williams1f7df6f2015-06-09 20:13:14 -04002218 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2219 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002220 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002221
2222 if (memdev->range_index != spa->range_index)
2223 continue;
2224 if (count >= ND_MAX_MAPPINGS) {
2225 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2226 spa->range_index, ND_MAX_MAPPINGS);
2227 return -ENXIO;
2228 }
Dan Williams44c462e2016-09-19 16:38:50 -07002229 mapping = &mappings[count++];
2230 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002231 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002232 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002233 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002234 }
2235
Dan Williams44c462e2016-09-19 16:38:50 -07002236 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002237 ndr_desc->num_mappings = count;
2238 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002239 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002240 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002241
Dan Williams1f7df6f2015-06-09 20:13:14 -04002242 nvdimm_bus = acpi_desc->nvdimm_bus;
2243 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002244 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002245 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002246 dev_warn(acpi_desc->dev,
2247 "failed to insert pmem resource to iomem: %d\n",
2248 rc);
Dan Williams48901162016-03-09 17:15:43 -08002249 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002250 }
Dan Williams48901162016-03-09 17:15:43 -08002251
Dan Williams1cf03c02016-02-17 13:01:23 -08002252 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2253 ndr_desc);
2254 if (!nfit_spa->nd_region)
2255 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002256 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002257 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2258 ndr_desc);
2259 if (!nfit_spa->nd_region)
2260 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002261 } else if (nfit_spa_is_virtual(spa)) {
2262 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2263 ndr_desc);
2264 if (!nfit_spa->nd_region)
2265 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002266 }
Vishal Verma20985162015-10-27 16:58:27 -06002267
Dan Williams1cf03c02016-02-17 13:01:23 -08002268 out:
2269 if (rc)
2270 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2271 nfit_spa->spa->range_index);
2272 return rc;
2273}
2274
2275static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2276 u32 max_ars)
2277{
2278 struct device *dev = acpi_desc->dev;
2279 struct nd_cmd_ars_status *ars_status;
2280
2281 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2282 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2283 return 0;
2284 }
2285
2286 if (acpi_desc->ars_status)
2287 devm_kfree(dev, acpi_desc->ars_status);
2288 acpi_desc->ars_status = NULL;
2289 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2290 if (!ars_status)
2291 return -ENOMEM;
2292 acpi_desc->ars_status = ars_status;
2293 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002294 return 0;
2295}
2296
Dan Williams1cf03c02016-02-17 13:01:23 -08002297static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2298 struct nfit_spa *nfit_spa)
2299{
2300 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2301 int rc;
2302
2303 if (!nfit_spa->max_ars) {
2304 struct nd_cmd_ars_cap ars_cap;
2305
2306 memset(&ars_cap, 0, sizeof(ars_cap));
2307 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2308 if (rc < 0)
2309 return rc;
2310 nfit_spa->max_ars = ars_cap.max_ars_out;
2311 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2312 /* check that the supported scrub types match the spa type */
2313 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2314 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2315 return -ENOTTY;
2316 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2317 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2318 return -ENOTTY;
2319 }
2320
2321 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2322 return -ENOMEM;
2323
2324 rc = ars_get_status(acpi_desc);
2325 if (rc < 0 && rc != -ENOSPC)
2326 return rc;
2327
Dan Williams82aa37c2016-12-06 12:45:24 -08002328 if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
Dan Williams1cf03c02016-02-17 13:01:23 -08002329 return -ENOMEM;
2330
2331 return 0;
2332}
2333
2334static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2335 struct nfit_spa *nfit_spa)
2336{
2337 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2338 unsigned int overflow_retry = scrub_overflow_abort;
2339 u64 init_ars_start = 0, init_ars_len = 0;
2340 struct device *dev = acpi_desc->dev;
2341 unsigned int tmo = scrub_timeout;
2342 int rc;
2343
Vishal Verma37b137f2016-07-23 21:51:42 -07002344 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002345 return;
2346
2347 rc = ars_start(acpi_desc, nfit_spa);
2348 /*
2349 * If we timed out the initial scan we'll still be busy here,
2350 * and will wait another timeout before giving up permanently.
2351 */
2352 if (rc < 0 && rc != -EBUSY)
2353 return;
2354
2355 do {
2356 u64 ars_start, ars_len;
2357
2358 if (acpi_desc->cancel)
2359 break;
2360 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2361 if (rc == -ENOTTY)
2362 break;
2363 if (rc == -EBUSY && !tmo) {
2364 dev_warn(dev, "range %d ars timeout, aborting\n",
2365 spa->range_index);
2366 break;
2367 }
2368
2369 if (rc == -EBUSY) {
2370 /*
2371 * Note, entries may be appended to the list
2372 * while the lock is dropped, but the workqueue
2373 * being active prevents entries being deleted /
2374 * freed.
2375 */
2376 mutex_unlock(&acpi_desc->init_mutex);
2377 ssleep(1);
2378 tmo--;
2379 mutex_lock(&acpi_desc->init_mutex);
2380 continue;
2381 }
2382
2383 /* we got some results, but there are more pending... */
2384 if (rc == -ENOSPC && overflow_retry--) {
2385 if (!init_ars_len) {
2386 init_ars_len = acpi_desc->ars_status->length;
2387 init_ars_start = acpi_desc->ars_status->address;
2388 }
2389 rc = ars_continue(acpi_desc);
2390 }
2391
2392 if (rc < 0) {
2393 dev_warn(dev, "range %d ars continuation failed\n",
2394 spa->range_index);
2395 break;
2396 }
2397
2398 if (init_ars_len) {
2399 ars_start = init_ars_start;
2400 ars_len = init_ars_len;
2401 } else {
2402 ars_start = acpi_desc->ars_status->address;
2403 ars_len = acpi_desc->ars_status->length;
2404 }
2405 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2406 spa->range_index, ars_start, ars_len);
2407 /* notify the region about new poison entries */
2408 nvdimm_region_notify(nfit_spa->nd_region,
2409 NVDIMM_REVALIDATE_POISON);
2410 break;
2411 } while (1);
2412}
2413
2414static void acpi_nfit_scrub(struct work_struct *work)
2415{
2416 struct device *dev;
2417 u64 init_scrub_length = 0;
2418 struct nfit_spa *nfit_spa;
2419 u64 init_scrub_address = 0;
2420 bool init_ars_done = false;
2421 struct acpi_nfit_desc *acpi_desc;
2422 unsigned int tmo = scrub_timeout;
2423 unsigned int overflow_retry = scrub_overflow_abort;
2424
2425 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2426 dev = acpi_desc->dev;
2427
2428 /*
2429 * We scrub in 2 phases. The first phase waits for any platform
2430 * firmware initiated scrubs to complete and then we go search for the
2431 * affected spa regions to mark them scanned. In the second phase we
2432 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002433 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2434 * the first phase, but really only care about running phase 2, where
2435 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002436 */
2437
2438 /* process platform firmware initiated scrubs */
2439 retry:
2440 mutex_lock(&acpi_desc->init_mutex);
2441 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2442 struct nd_cmd_ars_status *ars_status;
2443 struct acpi_nfit_system_address *spa;
2444 u64 ars_start, ars_len;
2445 int rc;
2446
2447 if (acpi_desc->cancel)
2448 break;
2449
2450 if (nfit_spa->nd_region)
2451 continue;
2452
2453 if (init_ars_done) {
2454 /*
2455 * No need to re-query, we're now just
2456 * reconciling all the ranges covered by the
2457 * initial scrub
2458 */
2459 rc = 0;
2460 } else
2461 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2462
2463 if (rc == -ENOTTY) {
2464 /* no ars capability, just register spa and move on */
2465 acpi_nfit_register_region(acpi_desc, nfit_spa);
2466 continue;
2467 }
2468
2469 if (rc == -EBUSY && !tmo) {
2470 /* fallthrough to directed scrub in phase 2 */
2471 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2472 break;
2473 } else if (rc == -EBUSY) {
2474 mutex_unlock(&acpi_desc->init_mutex);
2475 ssleep(1);
2476 tmo--;
2477 goto retry;
2478 }
2479
2480 /* we got some results, but there are more pending... */
2481 if (rc == -ENOSPC && overflow_retry--) {
2482 ars_status = acpi_desc->ars_status;
2483 /*
2484 * Record the original scrub range, so that we
2485 * can recall all the ranges impacted by the
2486 * initial scrub.
2487 */
2488 if (!init_scrub_length) {
2489 init_scrub_length = ars_status->length;
2490 init_scrub_address = ars_status->address;
2491 }
2492 rc = ars_continue(acpi_desc);
2493 if (rc == 0) {
2494 mutex_unlock(&acpi_desc->init_mutex);
2495 goto retry;
2496 }
2497 }
2498
2499 if (rc < 0) {
2500 /*
2501 * Initial scrub failed, we'll give it one more
2502 * try below...
2503 */
2504 break;
2505 }
2506
2507 /* We got some final results, record completed ranges */
2508 ars_status = acpi_desc->ars_status;
2509 if (init_scrub_length) {
2510 ars_start = init_scrub_address;
2511 ars_len = ars_start + init_scrub_length;
2512 } else {
2513 ars_start = ars_status->address;
2514 ars_len = ars_status->length;
2515 }
2516 spa = nfit_spa->spa;
2517
2518 if (!init_ars_done) {
2519 init_ars_done = true;
2520 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2521 ars_start, ars_len);
2522 }
2523 if (ars_start <= spa->address && ars_start + ars_len
2524 >= spa->address + spa->length)
2525 acpi_nfit_register_region(acpi_desc, nfit_spa);
2526 }
2527
2528 /*
2529 * For all the ranges not covered by an initial scrub we still
2530 * want to see if there are errors, but it's ok to discover them
2531 * asynchronously.
2532 */
2533 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2534 /*
2535 * Flag all the ranges that still need scrubbing, but
2536 * register them now to make data available.
2537 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002538 if (!nfit_spa->nd_region) {
2539 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002540 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002541 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002542 }
2543
2544 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2545 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002546 acpi_desc->scrub_count++;
2547 if (acpi_desc->scrub_count_state)
2548 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002549 mutex_unlock(&acpi_desc->init_mutex);
2550}
2551
Dan Williams1f7df6f2015-06-09 20:13:14 -04002552static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2553{
2554 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002555 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002556
Dan Williams1cf03c02016-02-17 13:01:23 -08002557 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2558 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2559 /* BLK regions don't need to wait for ars results */
2560 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2561 if (rc)
2562 return rc;
2563 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002564
Dan Williams1cf03c02016-02-17 13:01:23 -08002565 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002566 return 0;
2567}
2568
Vishal Verma20985162015-10-27 16:58:27 -06002569static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2570 struct nfit_table_prev *prev)
2571{
2572 struct device *dev = acpi_desc->dev;
2573
2574 if (!list_empty(&prev->spas) ||
2575 !list_empty(&prev->memdevs) ||
2576 !list_empty(&prev->dcrs) ||
2577 !list_empty(&prev->bdws) ||
2578 !list_empty(&prev->idts) ||
2579 !list_empty(&prev->flushes)) {
2580 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2581 return -ENXIO;
2582 }
2583 return 0;
2584}
2585
Vishal Verma37b137f2016-07-23 21:51:42 -07002586static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2587{
2588 struct device *dev = acpi_desc->dev;
2589 struct kernfs_node *nfit;
2590 struct device *bus_dev;
2591
2592 if (!ars_supported(acpi_desc->nvdimm_bus))
2593 return 0;
2594
2595 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2596 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2597 if (!nfit) {
2598 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2599 return -ENODEV;
2600 }
2601 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2602 sysfs_put(nfit);
2603 if (!acpi_desc->scrub_count_state) {
2604 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2605 return -ENODEV;
2606 }
2607
2608 return 0;
2609}
2610
Dan Williams58cd71b2016-07-21 18:05:36 -07002611static void acpi_nfit_destruct(void *data)
2612{
2613 struct acpi_nfit_desc *acpi_desc = data;
Vishal Verma37b137f2016-07-23 21:51:42 -07002614 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002615
Vishal Verma6839a6d2016-07-23 21:51:21 -07002616 /*
2617 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2618 * race teardown
2619 */
2620 mutex_lock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002621 acpi_desc->cancel = 1;
Vishal Verma37b137f2016-07-23 21:51:42 -07002622 /*
2623 * Bounce the nvdimm bus lock to make sure any in-flight
2624 * acpi_nfit_ars_rescan() submissions have had a chance to
2625 * either submit or see ->cancel set.
2626 */
2627 device_lock(bus_dev);
2628 device_unlock(bus_dev);
2629
Dan Williams58cd71b2016-07-21 18:05:36 -07002630 flush_workqueue(nfit_wq);
Vishal Verma37b137f2016-07-23 21:51:42 -07002631 if (acpi_desc->scrub_count_state)
2632 sysfs_put(acpi_desc->scrub_count_state);
Dan Williams58cd71b2016-07-21 18:05:36 -07002633 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2634 acpi_desc->nvdimm_bus = NULL;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002635 list_del(&acpi_desc->list);
2636 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002637}
2638
Dan Williamse7a11b42016-07-14 16:19:55 -07002639int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002640{
2641 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002642 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002643 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002644 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002645
Dan Williams58cd71b2016-07-21 18:05:36 -07002646 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002647 acpi_nfit_init_dsms(acpi_desc);
2648
Dan Williams58cd71b2016-07-21 18:05:36 -07002649 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2650 &acpi_desc->nd_desc);
2651 if (!acpi_desc->nvdimm_bus)
2652 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002653
Dan Williams58cd71b2016-07-21 18:05:36 -07002654 rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
2655 acpi_desc);
2656 if (rc)
2657 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002658
2659 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2660 if (rc)
2661 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002662
2663 /* register this acpi_desc for mce notifications */
2664 mutex_lock(&acpi_desc_lock);
2665 list_add_tail(&acpi_desc->list, &acpi_descs);
2666 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002667 }
2668
Vishal Verma20985162015-10-27 16:58:27 -06002669 mutex_lock(&acpi_desc->init_mutex);
2670
2671 INIT_LIST_HEAD(&prev.spas);
2672 INIT_LIST_HEAD(&prev.memdevs);
2673 INIT_LIST_HEAD(&prev.dcrs);
2674 INIT_LIST_HEAD(&prev.bdws);
2675 INIT_LIST_HEAD(&prev.idts);
2676 INIT_LIST_HEAD(&prev.flushes);
2677
2678 list_cut_position(&prev.spas, &acpi_desc->spas,
2679 acpi_desc->spas.prev);
2680 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2681 acpi_desc->memdevs.prev);
2682 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2683 acpi_desc->dcrs.prev);
2684 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2685 acpi_desc->bdws.prev);
2686 list_cut_position(&prev.idts, &acpi_desc->idts,
2687 acpi_desc->idts.prev);
2688 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2689 acpi_desc->flushes.prev);
2690
Vishal Verma20985162015-10-27 16:58:27 -06002691 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002692 while (!IS_ERR_OR_NULL(data))
2693 data = add_table(acpi_desc, &prev, data, end);
2694
2695 if (IS_ERR(data)) {
2696 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2697 PTR_ERR(data));
2698 rc = PTR_ERR(data);
2699 goto out_unlock;
2700 }
2701
2702 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2703 if (rc)
2704 goto out_unlock;
2705
Dan Williams81ed4e32016-06-10 18:20:53 -07002706 rc = nfit_mem_init(acpi_desc);
2707 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002708 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002709
2710 rc = acpi_nfit_register_dimms(acpi_desc);
2711 if (rc)
2712 goto out_unlock;
2713
2714 rc = acpi_nfit_register_regions(acpi_desc);
2715
2716 out_unlock:
2717 mutex_unlock(&acpi_desc->init_mutex);
2718 return rc;
2719}
2720EXPORT_SYMBOL_GPL(acpi_nfit_init);
2721
Dan Williams7ae0fa432016-02-19 12:16:34 -08002722struct acpi_nfit_flush_work {
2723 struct work_struct work;
2724 struct completion cmp;
2725};
2726
2727static void flush_probe(struct work_struct *work)
2728{
2729 struct acpi_nfit_flush_work *flush;
2730
2731 flush = container_of(work, typeof(*flush), work);
2732 complete(&flush->cmp);
2733}
2734
2735static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2736{
2737 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2738 struct device *dev = acpi_desc->dev;
2739 struct acpi_nfit_flush_work flush;
Dan Williamse4714862017-02-02 10:31:00 -08002740 int rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002741
2742 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2743 device_lock(dev);
2744 device_unlock(dev);
2745
2746 /*
2747 * Scrub work could take 10s of seconds, userspace may give up so we
2748 * need to be interruptible while waiting.
2749 */
2750 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2751 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2752 queue_work(nfit_wq, &flush.work);
Dan Williamse4714862017-02-02 10:31:00 -08002753
2754 rc = wait_for_completion_interruptible(&flush.cmp);
2755 cancel_work_sync(&flush.work);
2756 return rc;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002757}
2758
Dan Williams87bf5722016-02-22 21:50:31 -08002759static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2760 struct nvdimm *nvdimm, unsigned int cmd)
2761{
2762 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2763
2764 if (nvdimm)
2765 return 0;
2766 if (cmd != ND_CMD_ARS_START)
2767 return 0;
2768
2769 /*
2770 * The kernel and userspace may race to initiate a scrub, but
2771 * the scrub thread is prepared to lose that initial race. It
2772 * just needs guarantees that any ars it initiates are not
2773 * interrupted by any intervening start reqeusts from userspace.
2774 */
2775 if (work_busy(&acpi_desc->work))
2776 return -EBUSY;
2777
2778 return 0;
2779}
2780
Vishal Verma6839a6d2016-07-23 21:51:21 -07002781int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
Vishal Verma37b137f2016-07-23 21:51:42 -07002782{
2783 struct device *dev = acpi_desc->dev;
2784 struct nfit_spa *nfit_spa;
2785
2786 if (work_busy(&acpi_desc->work))
2787 return -EBUSY;
2788
2789 if (acpi_desc->cancel)
2790 return 0;
2791
2792 mutex_lock(&acpi_desc->init_mutex);
2793 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2794 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2795
2796 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2797 continue;
2798
2799 nfit_spa->ars_required = 1;
2800 }
2801 queue_work(nfit_wq, &acpi_desc->work);
2802 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2803 mutex_unlock(&acpi_desc->init_mutex);
2804
2805 return 0;
2806}
2807
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002808void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002809{
2810 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002811
2812 dev_set_drvdata(dev, acpi_desc);
2813 acpi_desc->dev = dev;
2814 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2815 nd_desc = &acpi_desc->nd_desc;
2816 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002817 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002818 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002819 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002820 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002821 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2822
Dan Williamsb94d5232015-05-19 22:54:31 -04002823 INIT_LIST_HEAD(&acpi_desc->spas);
2824 INIT_LIST_HEAD(&acpi_desc->dcrs);
2825 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002826 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002827 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04002828 INIT_LIST_HEAD(&acpi_desc->memdevs);
2829 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002830 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06002831 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08002832 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04002833}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002834EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04002835
Dan Williams3c87f372017-04-03 13:52:14 -07002836static void acpi_nfit_put_table(void *table)
2837{
2838 acpi_put_table(table);
2839}
2840
Dan Williamsb94d5232015-05-19 22:54:31 -04002841static int acpi_nfit_add(struct acpi_device *adev)
2842{
Vishal Verma20985162015-10-27 16:58:27 -06002843 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04002844 struct acpi_nfit_desc *acpi_desc;
2845 struct device *dev = &adev->dev;
2846 struct acpi_table_header *tbl;
2847 acpi_status status = AE_OK;
2848 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07002849 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002850
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002851 status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
Dan Williamsb94d5232015-05-19 22:54:31 -04002852 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06002853 /* This is ok, we could have an nvdimm hotplugged later */
2854 dev_dbg(dev, "failed to find NFIT at startup\n");
2855 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002856 }
Dan Williams3c87f372017-04-03 13:52:14 -07002857
2858 rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
2859 if (rc)
2860 return rc;
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002861 sz = tbl->length;
Dan Williamsb94d5232015-05-19 22:54:31 -04002862
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002863 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2864 if (!acpi_desc)
2865 return -ENOMEM;
2866 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04002867
Dan Williamse7a11b42016-07-14 16:19:55 -07002868 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05002869 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04002870
Vishal Verma20985162015-10-27 16:58:27 -06002871 /* Evaluate _FIT and override with that if present */
2872 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2873 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002874 union acpi_object *obj = buf.pointer;
2875
2876 if (obj->type == ACPI_TYPE_BUFFER)
2877 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2878 obj->buffer.length);
2879 else
Linda Knippers6b577c92015-11-20 19:05:49 -05002880 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2881 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07002882 kfree(buf.pointer);
2883 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07002884 /* skip over the lead-in header table */
2885 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2886 + sizeof(struct acpi_table_nfit),
2887 sz - sizeof(struct acpi_table_nfit));
Dan Williamse7a11b42016-07-14 16:19:55 -07002888 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002889}
2890
2891static int acpi_nfit_remove(struct acpi_device *adev)
2892{
Dan Williams58cd71b2016-07-21 18:05:36 -07002893 /* see acpi_nfit_destruct */
Dan Williamsb94d5232015-05-19 22:54:31 -04002894 return 0;
2895}
2896
Dan Williamsc14a8682016-08-18 22:15:04 -07002897void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
Vishal Verma20985162015-10-27 16:58:27 -06002898{
Dan Williamsc14a8682016-08-18 22:15:04 -07002899 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06002900 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07002901 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06002902 acpi_status status;
2903 int ret;
2904
2905 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2906
Vishal Vermac09f1212016-08-19 14:40:58 -06002907 if (event != NFIT_NOTIFY_UPDATE)
2908 return;
2909
Vishal Verma20985162015-10-27 16:58:27 -06002910 if (!dev->driver) {
2911 /* dev->driver may be null if we're being removed */
2912 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Dan Williamsc14a8682016-08-18 22:15:04 -07002913 return;
Vishal Verma20985162015-10-27 16:58:27 -06002914 }
2915
2916 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002917 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2918 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07002919 return;
2920 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002921 } else {
2922 /*
2923 * Finish previous registration before considering new
2924 * regions.
2925 */
2926 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06002927 }
2928
2929 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07002930 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06002931 if (ACPI_FAILURE(status)) {
2932 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07002933 return;
Vishal Verma20985162015-10-27 16:58:27 -06002934 }
2935
Linda Knippers6b577c92015-11-20 19:05:49 -05002936 obj = buf.pointer;
2937 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002938 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2939 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07002940 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05002941 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07002942 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05002943 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06002944 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07002945}
2946EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06002947
Dan Williamsc14a8682016-08-18 22:15:04 -07002948static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2949{
2950 device_lock(&adev->dev);
2951 __acpi_nfit_notify(&adev->dev, adev->handle, event);
2952 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06002953}
2954
Dan Williamsb94d5232015-05-19 22:54:31 -04002955static const struct acpi_device_id acpi_nfit_ids[] = {
2956 { "ACPI0012", 0 },
2957 { "", 0 },
2958};
2959MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
2960
2961static struct acpi_driver acpi_nfit_driver = {
2962 .name = KBUILD_MODNAME,
2963 .ids = acpi_nfit_ids,
2964 .ops = {
2965 .add = acpi_nfit_add,
2966 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06002967 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04002968 },
2969};
2970
2971static __init int nfit_init(void)
2972{
2973 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
2974 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
2975 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
2976 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
2977 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
2978 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
2979 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
2980
2981 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
2982 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
2983 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
2984 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
2985 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
2986 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
2987 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
2988 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
2989 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
2990 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
Dan Williams31eca762016-04-28 16:23:43 -07002991 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
2992 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
stuart hayese02fb722016-05-26 11:38:41 -05002993 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04002994
Dan Williams7ae0fa432016-02-19 12:16:34 -08002995 nfit_wq = create_singlethread_workqueue("nfit");
2996 if (!nfit_wq)
2997 return -ENOMEM;
2998
Vishal Verma6839a6d2016-07-23 21:51:21 -07002999 nfit_mce_register();
3000
Dan Williamsb94d5232015-05-19 22:54:31 -04003001 return acpi_bus_register_driver(&acpi_nfit_driver);
3002}
3003
3004static __exit void nfit_exit(void)
3005{
Vishal Verma6839a6d2016-07-23 21:51:21 -07003006 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04003007 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08003008 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07003009 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04003010}
3011
3012module_init(nfit_init);
3013module_exit(nfit_exit);
3014MODULE_LICENSE("GPL v2");
3015MODULE_AUTHOR("Intel Corporation");