blob: e58ec32393b786409e4c00772c5531783aa45aaa [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,
52 "Limit commands to the publicly specified set\n");
53
Vishal Verma6839a6d2016-07-23 21:51:21 -070054LIST_HEAD(acpi_descs);
55DEFINE_MUTEX(acpi_desc_lock);
56
Dan Williams7ae0fa432016-02-19 12:16:34 -080057static struct workqueue_struct *nfit_wq;
58
Vishal Verma20985162015-10-27 16:58:27 -060059struct nfit_table_prev {
60 struct list_head spas;
61 struct list_head memdevs;
62 struct list_head dcrs;
63 struct list_head bdws;
64 struct list_head idts;
65 struct list_head flushes;
66};
67
Dan Williamsb94d5232015-05-19 22:54:31 -040068static u8 nfit_uuid[NFIT_UUID_MAX][16];
69
Dan Williams6bc75612015-06-17 17:23:32 -040070const u8 *to_nfit_uuid(enum nfit_uuids id)
Dan Williamsb94d5232015-05-19 22:54:31 -040071{
72 return nfit_uuid[id];
73}
Dan Williams6bc75612015-06-17 17:23:32 -040074EXPORT_SYMBOL(to_nfit_uuid);
Dan Williamsb94d5232015-05-19 22:54:31 -040075
Dan Williams62232e452015-06-08 14:27:06 -040076static struct acpi_nfit_desc *to_acpi_nfit_desc(
77 struct nvdimm_bus_descriptor *nd_desc)
78{
79 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
80}
81
82static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
83{
84 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
85
86 /*
87 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
88 * acpi_device.
89 */
90 if (!nd_desc->provider_name
91 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
92 return NULL;
93
94 return to_acpi_device(acpi_desc->dev);
95}
96
Dan Williams11294d62016-09-21 09:21:26 -070097static int xlat_status(void *buf, unsigned int cmd, u32 status)
Dan Williamsaef25332016-02-12 17:01:11 -080098{
Dan Williamsd4f32362016-03-03 16:08:54 -080099 struct nd_cmd_clear_error *clear_err;
Dan Williamsaef25332016-02-12 17:01:11 -0800100 struct nd_cmd_ars_status *ars_status;
Dan Williamsaef25332016-02-12 17:01:11 -0800101 u16 flags;
102
103 switch (cmd) {
104 case ND_CMD_ARS_CAP:
Dan Williams11294d62016-09-21 09:21:26 -0700105 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800106 return -ENOTTY;
107
108 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700109 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800110 return -EIO;
111
112 /* No supported scan types for this range */
113 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
Dan Williams11294d62016-09-21 09:21:26 -0700114 if ((status >> 16 & flags) == 0)
Dan Williamsaef25332016-02-12 17:01:11 -0800115 return -ENOTTY;
Vishal Verma9a901f52016-12-05 17:00:37 -0700116 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800117 case ND_CMD_ARS_START:
Dan Williamsaef25332016-02-12 17:01:11 -0800118 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700119 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800120 return -EBUSY;
121
122 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700123 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800124 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700125 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800126 case ND_CMD_ARS_STATUS:
127 ars_status = buf;
128 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700129 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800130 return -EIO;
131 /* Check extended status (Upper two bytes) */
Dan Williams11294d62016-09-21 09:21:26 -0700132 if (status == NFIT_ARS_STATUS_DONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800133 return 0;
134
135 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700136 if (status == NFIT_ARS_STATUS_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800137 return -EBUSY;
138
139 /* No ARS performed for the current boot */
Dan Williams11294d62016-09-21 09:21:26 -0700140 if (status == NFIT_ARS_STATUS_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800141 return -EAGAIN;
142
143 /*
144 * ARS interrupted, either we overflowed or some other
145 * agent wants the scan to stop. If we didn't overflow
146 * then just continue with the returned results.
147 */
Dan Williams11294d62016-09-21 09:21:26 -0700148 if (status == NFIT_ARS_STATUS_INTR) {
Dan Williamsaef25332016-02-12 17:01:11 -0800149 if (ars_status->flags & NFIT_ARS_F_OVERFLOW)
150 return -ENOSPC;
151 return 0;
152 }
153
154 /* Unknown status */
Dan Williams11294d62016-09-21 09:21:26 -0700155 if (status >> 16)
Dan Williamsaef25332016-02-12 17:01:11 -0800156 return -EIO;
Vishal Verma9a901f52016-12-05 17:00:37 -0700157 return 0;
Dan Williamsd4f32362016-03-03 16:08:54 -0800158 case ND_CMD_CLEAR_ERROR:
159 clear_err = buf;
Dan Williams11294d62016-09-21 09:21:26 -0700160 if (status & 0xffff)
Dan Williamsd4f32362016-03-03 16:08:54 -0800161 return -EIO;
162 if (!clear_err->cleared)
163 return -EIO;
164 if (clear_err->length > clear_err->cleared)
165 return clear_err->cleared;
Vishal Verma9a901f52016-12-05 17:00:37 -0700166 return 0;
Dan Williamsaef25332016-02-12 17:01:11 -0800167 default:
168 break;
169 }
170
Dan Williams11294d62016-09-21 09:21:26 -0700171 /* all other non-zero status results in an error */
172 if (status)
173 return -EIO;
Dan Williamsaef25332016-02-12 17:01:11 -0800174 return 0;
175}
176
Dan Williamsb94d5232015-05-19 22:54:31 -0400177static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
178 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
Dan Williamsaef25332016-02-12 17:01:11 -0800179 unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400180{
Dan Williams62232e452015-06-08 14:27:06 -0400181 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams62232e452015-06-08 14:27:06 -0400182 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700183 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400184 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700185 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400186 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700187 unsigned long cmd_mask, dsm_mask;
Dan Williams11294d62016-09-21 09:21:26 -0700188 u32 offset, fw_status = 0;
Dan Williams62232e452015-06-08 14:27:06 -0400189 acpi_handle handle;
Dan Williams31eca762016-04-28 16:23:43 -0700190 unsigned int func;
Dan Williams62232e452015-06-08 14:27:06 -0400191 const u8 *uuid;
Dan Williams62232e452015-06-08 14:27:06 -0400192 int rc, i;
193
Dan Williams31eca762016-04-28 16:23:43 -0700194 func = cmd;
195 if (cmd == ND_CMD_CALL) {
196 call_pkg = buf;
197 func = call_pkg->nd_command;
198 }
199
Dan Williams62232e452015-06-08 14:27:06 -0400200 if (nvdimm) {
201 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
202 struct acpi_device *adev = nfit_mem->adev;
203
204 if (!adev)
205 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700206 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
207 return -ENOTTY;
208
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400209 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400210 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700211 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400212 dsm_mask = nfit_mem->dsm_mask;
213 desc = nd_cmd_dimm_desc(cmd);
Dan Williams31eca762016-04-28 16:23:43 -0700214 uuid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400215 handle = adev->handle;
216 } else {
217 struct acpi_device *adev = to_acpi_dev(acpi_desc);
218
219 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700220 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700221 dsm_mask = cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400222 desc = nd_cmd_bus_desc(cmd);
223 uuid = to_nfit_uuid(NFIT_DEV_BUS);
224 handle = adev->handle;
225 dimm_name = "bus";
226 }
227
228 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
229 return -ENOTTY;
230
Dan Williams31eca762016-04-28 16:23:43 -0700231 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400232 return -ENOTTY;
233
234 in_obj.type = ACPI_TYPE_PACKAGE;
235 in_obj.package.count = 1;
236 in_obj.package.elements = &in_buf;
237 in_buf.type = ACPI_TYPE_BUFFER;
238 in_buf.buffer.pointer = buf;
239 in_buf.buffer.length = 0;
240
241 /* libnvdimm has already validated the input envelope */
242 for (i = 0; i < desc->in_num; i++)
243 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
244 i, buf);
245
Dan Williams31eca762016-04-28 16:23:43 -0700246 if (call_pkg) {
247 /* skip over package wrapper */
248 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
249 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400250 }
251
Dan Williams31eca762016-04-28 16:23:43 -0700252 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
253 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
254 __func__, dimm_name, cmd, func,
255 in_buf.buffer.length);
256 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
257 in_buf.buffer.pointer,
258 min_t(u32, 256, in_buf.buffer.length), true);
259 }
260
261 out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj);
Dan Williams62232e452015-06-08 14:27:06 -0400262 if (!out_obj) {
263 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
264 cmd_name);
265 return -EINVAL;
266 }
267
Dan Williams31eca762016-04-28 16:23:43 -0700268 if (call_pkg) {
269 call_pkg->nd_fw_size = out_obj->buffer.length;
270 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
271 out_obj->buffer.pointer,
272 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
273
274 ACPI_FREE(out_obj);
275 /*
276 * Need to support FW function w/o known size in advance.
277 * Caller can determine required size based upon nd_fw_size.
278 * If we return an error (like elsewhere) then caller wouldn't
279 * be able to rely upon data returned to make calculation.
280 */
281 return 0;
282 }
283
Dan Williams62232e452015-06-08 14:27:06 -0400284 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
285 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
286 __func__, dimm_name, cmd_name, out_obj->type);
287 rc = -EINVAL;
288 goto out;
289 }
290
291 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
292 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
293 dimm_name, cmd_name, out_obj->buffer.length);
294 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
295 4, out_obj->buffer.pointer, min_t(u32, 128,
296 out_obj->buffer.length), true);
297 }
298
299 for (i = 0, offset = 0; i < desc->out_num; i++) {
300 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
Dan Williamsefda1b5d2016-12-06 09:10:12 -0800301 (u32 *) out_obj->buffer.pointer,
302 out_obj->buffer.length - offset);
Dan Williams62232e452015-06-08 14:27:06 -0400303
304 if (offset + out_size > out_obj->buffer.length) {
305 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
306 __func__, dimm_name, cmd_name, i);
307 break;
308 }
309
310 if (in_buf.buffer.length + offset + out_size > buf_len) {
311 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
312 __func__, dimm_name, cmd_name, i);
313 rc = -ENXIO;
314 goto out;
315 }
316 memcpy(buf + in_buf.buffer.length + offset,
317 out_obj->buffer.pointer + offset, out_size);
318 offset += out_size;
319 }
Dan Williams11294d62016-09-21 09:21:26 -0700320
321 /*
322 * Set fw_status for all the commands with a known format to be
323 * later interpreted by xlat_status().
324 */
325 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
326 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
327 fw_status = *(u32 *) out_obj->buffer.pointer;
328
Dan Williams62232e452015-06-08 14:27:06 -0400329 if (offset + in_buf.buffer.length < buf_len) {
330 if (i >= 1) {
331 /*
332 * status valid, return the number of bytes left
333 * unfilled in the output buffer
334 */
335 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800336 if (cmd_rc)
Dan Williams11294d62016-09-21 09:21:26 -0700337 *cmd_rc = xlat_status(buf, cmd, fw_status);
Dan Williams62232e452015-06-08 14:27:06 -0400338 } else {
339 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
340 __func__, dimm_name, cmd_name, buf_len,
341 offset);
342 rc = -ENXIO;
343 }
Dan Williams2eea6582016-05-02 09:11:53 -0700344 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400345 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700346 if (cmd_rc)
Dan Williams11294d62016-09-21 09:21:26 -0700347 *cmd_rc = xlat_status(buf, cmd, fw_status);
Dan Williams2eea6582016-05-02 09:11:53 -0700348 }
Dan Williams62232e452015-06-08 14:27:06 -0400349
350 out:
351 ACPI_FREE(out_obj);
352
353 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400354}
355
356static const char *spa_type_name(u16 type)
357{
358 static const char *to_name[] = {
359 [NFIT_SPA_VOLATILE] = "volatile",
360 [NFIT_SPA_PM] = "pmem",
361 [NFIT_SPA_DCR] = "dimm-control-region",
362 [NFIT_SPA_BDW] = "block-data-window",
363 [NFIT_SPA_VDISK] = "volatile-disk",
364 [NFIT_SPA_VCD] = "volatile-cd",
365 [NFIT_SPA_PDISK] = "persistent-disk",
366 [NFIT_SPA_PCD] = "persistent-cd",
367
368 };
369
370 if (type > NFIT_SPA_PCD)
371 return "unknown";
372
373 return to_name[type];
374}
375
Vishal Verma6839a6d2016-07-23 21:51:21 -0700376int nfit_spa_type(struct acpi_nfit_system_address *spa)
Dan Williamsb94d5232015-05-19 22:54:31 -0400377{
378 int i;
379
380 for (i = 0; i < NFIT_UUID_MAX; i++)
381 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
382 return i;
383 return -1;
384}
385
386static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600387 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400388 struct acpi_nfit_system_address *spa)
389{
390 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600391 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400392
Dan Williams31932042016-07-14 17:22:48 -0700393 if (spa->header.length != sizeof(*spa))
394 return false;
395
Vishal Verma20985162015-10-27 16:58:27 -0600396 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700397 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600398 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
399 return true;
400 }
401 }
402
Dan Williams31932042016-07-14 17:22:48 -0700403 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
404 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400405 if (!nfit_spa)
406 return false;
407 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700408 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400409 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
410 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
411 spa->range_index,
412 spa_type_name(nfit_spa_type(spa)));
413 return true;
414}
415
416static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600417 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400418 struct acpi_nfit_memory_map *memdev)
419{
420 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600421 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400422
Dan Williams31932042016-07-14 17:22:48 -0700423 if (memdev->header.length != sizeof(*memdev))
424 return false;
425
Vishal Verma20985162015-10-27 16:58:27 -0600426 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700427 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600428 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
429 return true;
430 }
431
Dan Williams31932042016-07-14 17:22:48 -0700432 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
433 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400434 if (!nfit_memdev)
435 return false;
436 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700437 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400438 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
439 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
440 __func__, memdev->device_handle, memdev->range_index,
441 memdev->region_index);
442 return true;
443}
444
Dan Williams31932042016-07-14 17:22:48 -0700445/*
446 * An implementation may provide a truncated control region if no block windows
447 * are defined.
448 */
449static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
450{
451 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
452 window_size))
453 return 0;
454 if (dcr->windows)
455 return sizeof(*dcr);
456 return offsetof(struct acpi_nfit_control_region, window_size);
457}
458
Dan Williamsb94d5232015-05-19 22:54:31 -0400459static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600460 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400461 struct acpi_nfit_control_region *dcr)
462{
463 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600464 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400465
Dan Williams31932042016-07-14 17:22:48 -0700466 if (!sizeof_dcr(dcr))
467 return false;
468
Vishal Verma20985162015-10-27 16:58:27 -0600469 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700470 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600471 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
472 return true;
473 }
474
Dan Williams31932042016-07-14 17:22:48 -0700475 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
476 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400477 if (!nfit_dcr)
478 return false;
479 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700480 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400481 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
482 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
483 dcr->region_index, dcr->windows);
484 return true;
485}
486
487static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600488 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400489 struct acpi_nfit_data_region *bdw)
490{
491 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600492 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400493
Dan Williams31932042016-07-14 17:22:48 -0700494 if (bdw->header.length != sizeof(*bdw))
495 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600496 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700497 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600498 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
499 return true;
500 }
501
Dan Williams31932042016-07-14 17:22:48 -0700502 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
503 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400504 if (!nfit_bdw)
505 return false;
506 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700507 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400508 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
509 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
510 bdw->region_index, bdw->windows);
511 return true;
512}
513
Dan Williams31932042016-07-14 17:22:48 -0700514static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
515{
516 if (idt->header.length < sizeof(*idt))
517 return 0;
518 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
519}
520
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400521static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600522 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400523 struct acpi_nfit_interleave *idt)
524{
525 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600526 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400527
Dan Williams31932042016-07-14 17:22:48 -0700528 if (!sizeof_idt(idt))
529 return false;
530
531 list_for_each_entry(nfit_idt, &prev->idts, list) {
532 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
533 continue;
534
535 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600536 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
537 return true;
538 }
Dan Williams31932042016-07-14 17:22:48 -0700539 }
Vishal Verma20985162015-10-27 16:58:27 -0600540
Dan Williams31932042016-07-14 17:22:48 -0700541 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
542 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400543 if (!nfit_idt)
544 return false;
545 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700546 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400547 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
548 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
549 idt->interleave_index, idt->line_count);
550 return true;
551}
552
Dan Williams31932042016-07-14 17:22:48 -0700553static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
554{
555 if (flush->header.length < sizeof(*flush))
556 return 0;
557 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
558}
559
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600560static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600561 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600562 struct acpi_nfit_flush_address *flush)
563{
564 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600565 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600566
Dan Williams31932042016-07-14 17:22:48 -0700567 if (!sizeof_flush(flush))
568 return false;
569
570 list_for_each_entry(nfit_flush, &prev->flushes, list) {
571 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
572 continue;
573
574 if (memcmp(nfit_flush->flush, flush,
575 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600576 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
577 return true;
578 }
Dan Williams31932042016-07-14 17:22:48 -0700579 }
Vishal Verma20985162015-10-27 16:58:27 -0600580
Dan Williams31932042016-07-14 17:22:48 -0700581 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
582 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600583 if (!nfit_flush)
584 return false;
585 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700586 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600587 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
588 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
589 flush->device_handle, flush->hint_count);
590 return true;
591}
592
Vishal Verma20985162015-10-27 16:58:27 -0600593static void *add_table(struct acpi_nfit_desc *acpi_desc,
594 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400595{
596 struct device *dev = acpi_desc->dev;
597 struct acpi_nfit_header *hdr;
598 void *err = ERR_PTR(-ENOMEM);
599
600 if (table >= end)
601 return NULL;
602
603 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600604 if (!hdr->length) {
605 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
606 hdr->type);
607 return NULL;
608 }
609
Dan Williamsb94d5232015-05-19 22:54:31 -0400610 switch (hdr->type) {
611 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600612 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400613 return err;
614 break;
615 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600616 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400617 return err;
618 break;
619 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600620 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400621 return err;
622 break;
623 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600624 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400625 return err;
626 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400627 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600628 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400629 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400630 break;
631 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600632 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600633 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400634 break;
635 case ACPI_NFIT_TYPE_SMBIOS:
636 dev_dbg(dev, "%s: smbios\n", __func__);
637 break;
638 default:
639 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
640 break;
641 }
642
643 return table + hdr->length;
644}
645
646static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
647 struct nfit_mem *nfit_mem)
648{
649 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
650 u16 dcr = nfit_mem->dcr->region_index;
651 struct nfit_spa *nfit_spa;
652
653 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
654 u16 range_index = nfit_spa->spa->range_index;
655 int type = nfit_spa_type(nfit_spa->spa);
656 struct nfit_memdev *nfit_memdev;
657
658 if (type != NFIT_SPA_BDW)
659 continue;
660
661 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
662 if (nfit_memdev->memdev->range_index != range_index)
663 continue;
664 if (nfit_memdev->memdev->device_handle != device_handle)
665 continue;
666 if (nfit_memdev->memdev->region_index != dcr)
667 continue;
668
669 nfit_mem->spa_bdw = nfit_spa->spa;
670 return;
671 }
672 }
673
674 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
675 nfit_mem->spa_dcr->range_index);
676 nfit_mem->bdw = NULL;
677}
678
Dan Williams6697b2c2016-02-04 16:51:00 -0800679static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400680 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
681{
682 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400683 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400684 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400685 struct nfit_idt *nfit_idt;
686 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400687
Dan Williamsb94d5232015-05-19 22:54:31 -0400688 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
689 if (nfit_bdw->bdw->region_index != dcr)
690 continue;
691 nfit_mem->bdw = nfit_bdw->bdw;
692 break;
693 }
694
695 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800696 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400697
698 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400699
700 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800701 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400702
703 range_index = nfit_mem->spa_bdw->range_index;
704 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
705 if (nfit_memdev->memdev->range_index != range_index ||
706 nfit_memdev->memdev->region_index != dcr)
707 continue;
708 nfit_mem->memdev_bdw = nfit_memdev->memdev;
709 idt_idx = nfit_memdev->memdev->interleave_index;
710 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
711 if (nfit_idt->idt->interleave_index != idt_idx)
712 continue;
713 nfit_mem->idt_bdw = nfit_idt->idt;
714 break;
715 }
716 break;
717 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400718}
719
720static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
721 struct acpi_nfit_system_address *spa)
722{
723 struct nfit_mem *nfit_mem, *found;
724 struct nfit_memdev *nfit_memdev;
725 int type = nfit_spa_type(spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400726
727 switch (type) {
728 case NFIT_SPA_DCR:
729 case NFIT_SPA_PM:
730 break;
731 default:
732 return 0;
733 }
734
735 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700736 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800737 struct nfit_dcr *nfit_dcr;
738 u32 device_handle;
739 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400740
741 if (nfit_memdev->memdev->range_index != spa->range_index)
742 continue;
743 found = NULL;
744 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800745 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400746 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800747 if (__to_nfit_memdev(nfit_mem)->device_handle
748 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400749 found = nfit_mem;
750 break;
751 }
752
753 if (found)
754 nfit_mem = found;
755 else {
756 nfit_mem = devm_kzalloc(acpi_desc->dev,
757 sizeof(*nfit_mem), GFP_KERNEL);
758 if (!nfit_mem)
759 return -ENOMEM;
760 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700761 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800762 list_add(&nfit_mem->list, &acpi_desc->dimms);
763 }
764
765 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
766 if (nfit_dcr->dcr->region_index != dcr)
767 continue;
768 /*
769 * Record the control region for the dimm. For
770 * the ACPI 6.1 case, where there are separate
771 * control regions for the pmem vs blk
772 * interfaces, be sure to record the extended
773 * blk details.
774 */
775 if (!nfit_mem->dcr)
776 nfit_mem->dcr = nfit_dcr->dcr;
777 else if (nfit_mem->dcr->windows == 0
778 && nfit_dcr->dcr->windows)
779 nfit_mem->dcr = nfit_dcr->dcr;
780 break;
781 }
782
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700783 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700784 struct acpi_nfit_flush_address *flush;
785 u16 i;
786
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700787 if (nfit_flush->flush->device_handle != device_handle)
788 continue;
789 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700790 flush = nfit_flush->flush;
791 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
792 flush->hint_count
793 * sizeof(struct resource), GFP_KERNEL);
794 if (!nfit_mem->flush_wpq)
795 return -ENOMEM;
796 for (i = 0; i < flush->hint_count; i++) {
797 struct resource *res = &nfit_mem->flush_wpq[i];
798
799 res->start = flush->hint_address[i];
800 res->end = res->start + 8 - 1;
801 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700802 break;
803 }
804
Dan Williams6697b2c2016-02-04 16:51:00 -0800805 if (dcr && !nfit_mem->dcr) {
806 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
807 spa->range_index, dcr);
808 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400809 }
810
811 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400812 struct nfit_idt *nfit_idt;
813 u16 idt_idx;
814
Dan Williamsb94d5232015-05-19 22:54:31 -0400815 /* multiple dimms may share a SPA when interleaved */
816 nfit_mem->spa_dcr = spa;
817 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400818 idt_idx = nfit_memdev->memdev->interleave_index;
819 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
820 if (nfit_idt->idt->interleave_index != idt_idx)
821 continue;
822 nfit_mem->idt_dcr = nfit_idt->idt;
823 break;
824 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800825 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400826 } else {
827 /*
828 * A single dimm may belong to multiple SPA-PM
829 * ranges, record at least one in addition to
830 * any SPA-DCR range.
831 */
832 nfit_mem->memdev_pmem = nfit_memdev->memdev;
833 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400834 }
835
836 return 0;
837}
838
839static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
840{
841 struct nfit_mem *a = container_of(_a, typeof(*a), list);
842 struct nfit_mem *b = container_of(_b, typeof(*b), list);
843 u32 handleA, handleB;
844
845 handleA = __to_nfit_memdev(a)->device_handle;
846 handleB = __to_nfit_memdev(b)->device_handle;
847 if (handleA < handleB)
848 return -1;
849 else if (handleA > handleB)
850 return 1;
851 return 0;
852}
853
854static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
855{
856 struct nfit_spa *nfit_spa;
857
858 /*
859 * For each SPA-DCR or SPA-PMEM address range find its
860 * corresponding MEMDEV(s). From each MEMDEV find the
861 * corresponding DCR. Then, if we're operating on a SPA-DCR,
862 * try to find a SPA-BDW and a corresponding BDW that references
863 * the DCR. Throw it all into an nfit_mem object. Note, that
864 * BDWs are optional.
865 */
866 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
867 int rc;
868
869 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
870 if (rc)
871 return rc;
872 }
873
874 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
875
876 return 0;
877}
878
Dan Williams45def222015-04-26 19:26:48 -0400879static ssize_t revision_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
881{
882 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
883 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
884 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
885
Linda Knippers6b577c92015-11-20 19:05:49 -0500886 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400887}
888static DEVICE_ATTR_RO(revision);
889
Vishal Verma9ffd6352016-09-30 17:19:29 -0600890static ssize_t hw_error_scrub_show(struct device *dev,
891 struct device_attribute *attr, char *buf)
892{
893 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
894 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
895 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
896
897 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
898}
899
900/*
901 * The 'hw_error_scrub' attribute can have the following values written to it:
902 * '0': Switch to the default mode where an exception will only insert
903 * the address of the memory error into the poison and badblocks lists.
904 * '1': Enable a full scrub to happen if an exception for a memory error is
905 * received.
906 */
907static ssize_t hw_error_scrub_store(struct device *dev,
908 struct device_attribute *attr, const char *buf, size_t size)
909{
910 struct nvdimm_bus_descriptor *nd_desc;
911 ssize_t rc;
912 long val;
913
914 rc = kstrtol(buf, 0, &val);
915 if (rc)
916 return rc;
917
918 device_lock(dev);
919 nd_desc = dev_get_drvdata(dev);
920 if (nd_desc) {
921 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
922
923 switch (val) {
924 case HW_ERROR_SCRUB_ON:
925 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
926 break;
927 case HW_ERROR_SCRUB_OFF:
928 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
929 break;
930 default:
931 rc = -EINVAL;
932 break;
933 }
934 }
935 device_unlock(dev);
936 if (rc)
937 return rc;
938 return size;
939}
940static DEVICE_ATTR_RW(hw_error_scrub);
941
Vishal Verma37b137f2016-07-23 21:51:42 -0700942/*
943 * This shows the number of full Address Range Scrubs that have been
944 * completed since driver load time. Userspace can wait on this using
945 * select/poll etc. A '+' at the end indicates an ARS is in progress
946 */
947static ssize_t scrub_show(struct device *dev,
948 struct device_attribute *attr, char *buf)
949{
950 struct nvdimm_bus_descriptor *nd_desc;
951 ssize_t rc = -ENXIO;
952
953 device_lock(dev);
954 nd_desc = dev_get_drvdata(dev);
955 if (nd_desc) {
956 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
957
958 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
959 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
960 }
961 device_unlock(dev);
962 return rc;
963}
964
Vishal Verma37b137f2016-07-23 21:51:42 -0700965static ssize_t scrub_store(struct device *dev,
966 struct device_attribute *attr, const char *buf, size_t size)
967{
968 struct nvdimm_bus_descriptor *nd_desc;
969 ssize_t rc;
970 long val;
971
972 rc = kstrtol(buf, 0, &val);
973 if (rc)
974 return rc;
975 if (val != 1)
976 return -EINVAL;
977
978 device_lock(dev);
979 nd_desc = dev_get_drvdata(dev);
980 if (nd_desc) {
981 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
982
983 rc = acpi_nfit_ars_rescan(acpi_desc);
984 }
985 device_unlock(dev);
986 if (rc)
987 return rc;
988 return size;
989}
990static DEVICE_ATTR_RW(scrub);
991
992static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
993{
994 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
995 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
996 | 1 << ND_CMD_ARS_STATUS;
997
998 return (nd_desc->cmd_mask & mask) == mask;
999}
1000
1001static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1002{
1003 struct device *dev = container_of(kobj, struct device, kobj);
1004 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1005
1006 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1007 return 0;
1008 return a->mode;
1009}
1010
Dan Williams45def222015-04-26 19:26:48 -04001011static struct attribute *acpi_nfit_attributes[] = {
1012 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -07001013 &dev_attr_scrub.attr,
Vishal Verma9ffd6352016-09-30 17:19:29 -06001014 &dev_attr_hw_error_scrub.attr,
Dan Williams45def222015-04-26 19:26:48 -04001015 NULL,
1016};
1017
1018static struct attribute_group acpi_nfit_attribute_group = {
1019 .name = "nfit",
1020 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -07001021 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -04001022};
1023
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001024static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -04001025 &nvdimm_bus_attribute_group,
1026 &acpi_nfit_attribute_group,
1027 NULL,
1028};
1029
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001030static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1031{
1032 struct nvdimm *nvdimm = to_nvdimm(dev);
1033 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1034
1035 return __to_nfit_memdev(nfit_mem);
1036}
1037
1038static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1039{
1040 struct nvdimm *nvdimm = to_nvdimm(dev);
1041 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1042
1043 return nfit_mem->dcr;
1044}
1045
1046static ssize_t handle_show(struct device *dev,
1047 struct device_attribute *attr, char *buf)
1048{
1049 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1050
1051 return sprintf(buf, "%#x\n", memdev->device_handle);
1052}
1053static DEVICE_ATTR_RO(handle);
1054
1055static ssize_t phys_id_show(struct device *dev,
1056 struct device_attribute *attr, char *buf)
1057{
1058 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1059
1060 return sprintf(buf, "%#x\n", memdev->physical_id);
1061}
1062static DEVICE_ATTR_RO(phys_id);
1063
1064static ssize_t vendor_show(struct device *dev,
1065 struct device_attribute *attr, char *buf)
1066{
1067 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1068
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001069 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001070}
1071static DEVICE_ATTR_RO(vendor);
1072
1073static ssize_t rev_id_show(struct device *dev,
1074 struct device_attribute *attr, char *buf)
1075{
1076 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1077
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001078 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001079}
1080static DEVICE_ATTR_RO(rev_id);
1081
1082static ssize_t device_show(struct device *dev,
1083 struct device_attribute *attr, char *buf)
1084{
1085 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1086
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001087 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001088}
1089static DEVICE_ATTR_RO(device);
1090
Dan Williams6ca72082016-04-29 10:33:23 -07001091static ssize_t subsystem_vendor_show(struct device *dev,
1092 struct device_attribute *attr, char *buf)
1093{
1094 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1095
1096 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1097}
1098static DEVICE_ATTR_RO(subsystem_vendor);
1099
1100static ssize_t subsystem_rev_id_show(struct device *dev,
1101 struct device_attribute *attr, char *buf)
1102{
1103 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1104
1105 return sprintf(buf, "0x%04x\n",
1106 be16_to_cpu(dcr->subsystem_revision_id));
1107}
1108static DEVICE_ATTR_RO(subsystem_rev_id);
1109
1110static ssize_t subsystem_device_show(struct device *dev,
1111 struct device_attribute *attr, char *buf)
1112{
1113 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1114
1115 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1116}
1117static DEVICE_ATTR_RO(subsystem_device);
1118
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001119static int num_nvdimm_formats(struct nvdimm *nvdimm)
1120{
1121 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1122 int formats = 0;
1123
1124 if (nfit_mem->memdev_pmem)
1125 formats++;
1126 if (nfit_mem->memdev_bdw)
1127 formats++;
1128 return formats;
1129}
1130
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001131static ssize_t format_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1133{
1134 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1135
Dan Williams1bcbf422016-06-29 11:19:32 -07001136 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001137}
1138static DEVICE_ATTR_RO(format);
1139
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001140static ssize_t format1_show(struct device *dev,
1141 struct device_attribute *attr, char *buf)
1142{
1143 u32 handle;
1144 ssize_t rc = -ENXIO;
1145 struct nfit_mem *nfit_mem;
1146 struct nfit_memdev *nfit_memdev;
1147 struct acpi_nfit_desc *acpi_desc;
1148 struct nvdimm *nvdimm = to_nvdimm(dev);
1149 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1150
1151 nfit_mem = nvdimm_provider_data(nvdimm);
1152 acpi_desc = nfit_mem->acpi_desc;
1153 handle = to_nfit_memdev(dev)->device_handle;
1154
1155 /* assumes DIMMs have at most 2 published interface codes */
1156 mutex_lock(&acpi_desc->init_mutex);
1157 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1158 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1159 struct nfit_dcr *nfit_dcr;
1160
1161 if (memdev->device_handle != handle)
1162 continue;
1163
1164 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1165 if (nfit_dcr->dcr->region_index != memdev->region_index)
1166 continue;
1167 if (nfit_dcr->dcr->code == dcr->code)
1168 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001169 rc = sprintf(buf, "0x%04x\n",
1170 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001171 break;
1172 }
1173 if (rc != ENXIO)
1174 break;
1175 }
1176 mutex_unlock(&acpi_desc->init_mutex);
1177 return rc;
1178}
1179static DEVICE_ATTR_RO(format1);
1180
1181static ssize_t formats_show(struct device *dev,
1182 struct device_attribute *attr, char *buf)
1183{
1184 struct nvdimm *nvdimm = to_nvdimm(dev);
1185
1186 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1187}
1188static DEVICE_ATTR_RO(formats);
1189
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001190static ssize_t serial_show(struct device *dev,
1191 struct device_attribute *attr, char *buf)
1192{
1193 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1194
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001195 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001196}
1197static DEVICE_ATTR_RO(serial);
1198
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001199static ssize_t family_show(struct device *dev,
1200 struct device_attribute *attr, char *buf)
1201{
1202 struct nvdimm *nvdimm = to_nvdimm(dev);
1203 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1204
1205 if (nfit_mem->family < 0)
1206 return -ENXIO;
1207 return sprintf(buf, "%d\n", nfit_mem->family);
1208}
1209static DEVICE_ATTR_RO(family);
1210
1211static ssize_t dsm_mask_show(struct device *dev,
1212 struct device_attribute *attr, char *buf)
1213{
1214 struct nvdimm *nvdimm = to_nvdimm(dev);
1215 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1216
1217 if (nfit_mem->family < 0)
1218 return -ENXIO;
1219 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1220}
1221static DEVICE_ATTR_RO(dsm_mask);
1222
Dan Williams58138822015-06-23 20:08:34 -04001223static ssize_t flags_show(struct device *dev,
1224 struct device_attribute *attr, char *buf)
1225{
1226 u16 flags = to_nfit_memdev(dev)->flags;
1227
1228 return sprintf(buf, "%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001229 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1230 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1231 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001232 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Toshi Kani402bae52015-08-26 10:20:23 -06001233 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "");
Dan Williams58138822015-06-23 20:08:34 -04001234}
1235static DEVICE_ATTR_RO(flags);
1236
Toshi Kani38a879b2016-04-25 15:34:59 -06001237static ssize_t id_show(struct device *dev,
1238 struct device_attribute *attr, char *buf)
1239{
1240 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1241
1242 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1243 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1244 be16_to_cpu(dcr->vendor_id),
1245 dcr->manufacturing_location,
1246 be16_to_cpu(dcr->manufacturing_date),
1247 be32_to_cpu(dcr->serial_number));
1248 else
1249 return sprintf(buf, "%04x-%08x\n",
1250 be16_to_cpu(dcr->vendor_id),
1251 be32_to_cpu(dcr->serial_number));
1252}
1253static DEVICE_ATTR_RO(id);
1254
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001255static struct attribute *acpi_nfit_dimm_attributes[] = {
1256 &dev_attr_handle.attr,
1257 &dev_attr_phys_id.attr,
1258 &dev_attr_vendor.attr,
1259 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001260 &dev_attr_rev_id.attr,
1261 &dev_attr_subsystem_vendor.attr,
1262 &dev_attr_subsystem_device.attr,
1263 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001264 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001265 &dev_attr_formats.attr,
1266 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001267 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001268 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001269 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001270 &dev_attr_family.attr,
1271 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001272 NULL,
1273};
1274
1275static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1276 struct attribute *a, int n)
1277{
1278 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001279 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001280
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001281 if (!to_nfit_dcr(dev))
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001282 return 0;
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001283 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1284 return 0;
1285 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001286}
1287
1288static struct attribute_group acpi_nfit_dimm_attribute_group = {
1289 .name = "nfit",
1290 .attrs = acpi_nfit_dimm_attributes,
1291 .is_visible = acpi_nfit_dimm_attr_visible,
1292};
1293
1294static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001295 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001296 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001297 &acpi_nfit_dimm_attribute_group,
1298 NULL,
1299};
1300
1301static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1302 u32 device_handle)
1303{
1304 struct nfit_mem *nfit_mem;
1305
1306 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1307 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1308 return nfit_mem->nvdimm;
1309
1310 return NULL;
1311}
1312
Dan Williams231bf112016-08-22 19:23:25 -07001313void __acpi_nvdimm_notify(struct device *dev, u32 event)
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001314{
1315 struct nfit_mem *nfit_mem;
1316 struct acpi_nfit_desc *acpi_desc;
1317
1318 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1319 event);
1320
1321 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1322 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1323 event);
1324 return;
1325 }
1326
1327 acpi_desc = dev_get_drvdata(dev->parent);
1328 if (!acpi_desc)
1329 return;
1330
1331 /*
1332 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1333 * is still valid.
1334 */
1335 nfit_mem = dev_get_drvdata(dev);
1336 if (nfit_mem && nfit_mem->flags_attr)
1337 sysfs_notify_dirent(nfit_mem->flags_attr);
1338}
Dan Williams231bf112016-08-22 19:23:25 -07001339EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001340
1341static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1342{
1343 struct acpi_device *adev = data;
1344 struct device *dev = &adev->dev;
1345
1346 device_lock(dev->parent);
1347 __acpi_nvdimm_notify(dev, event);
1348 device_unlock(dev->parent);
1349}
1350
Dan Williams62232e452015-06-08 14:27:06 -04001351static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1352 struct nfit_mem *nfit_mem, u32 device_handle)
1353{
1354 struct acpi_device *adev, *adev_dimm;
1355 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001356 unsigned long dsm_mask;
1357 const u8 *uuid;
Linda Knippers60e95f432015-07-22 16:17:22 -04001358 int i;
Dan Williams62232e452015-06-08 14:27:06 -04001359
Dan Williamse3654ec2016-04-28 16:17:07 -07001360 /* nfit test assumes 1:1 relationship between commands and dsms */
1361 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001362 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001363 adev = to_acpi_dev(acpi_desc);
1364 if (!adev)
1365 return 0;
1366
1367 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1368 nfit_mem->adev = adev_dimm;
1369 if (!adev_dimm) {
1370 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1371 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001372 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001373 }
1374
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001375 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1376 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1377 dev_err(dev, "%s: notification registration failed\n",
1378 dev_name(&adev_dimm->dev));
1379 return -ENXIO;
1380 }
1381
Dan Williams31eca762016-04-28 16:23:43 -07001382 /*
stuart hayese02fb722016-05-26 11:38:41 -05001383 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001384 * different command sets. Note, that checking for function0 (bit0)
1385 * tells us if any commands are reachable through this uuid.
Dan Williams31eca762016-04-28 16:23:43 -07001386 */
stuart hayese02fb722016-05-26 11:38:41 -05001387 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001388 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Dan Williams31eca762016-04-28 16:23:43 -07001389 break;
1390
1391 /* limit the supported commands to those that are publicly documented */
1392 nfit_mem->family = i;
Dan Williams87554092016-04-28 18:01:20 -07001393 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001394 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001395 if (disable_vendor_specific)
1396 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001397 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001398 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001399 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001400 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001401 if (disable_vendor_specific)
1402 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001403 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1404 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001405 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001406 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001407 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001408 /* DSMs are optional, continue loading the driver... */
1409 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001410 }
1411
1412 uuid = to_nfit_uuid(nfit_mem->family);
1413 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Dan Williams62232e452015-06-08 14:27:06 -04001414 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
1415 set_bit(i, &nfit_mem->dsm_mask);
1416
Linda Knippers60e95f432015-07-22 16:17:22 -04001417 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001418}
1419
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001420static void shutdown_dimm_notify(void *data)
1421{
1422 struct acpi_nfit_desc *acpi_desc = data;
1423 struct nfit_mem *nfit_mem;
1424
1425 mutex_lock(&acpi_desc->init_mutex);
1426 /*
1427 * Clear out the nfit_mem->flags_attr and shut down dimm event
1428 * notifications.
1429 */
1430 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williams231bf112016-08-22 19:23:25 -07001431 struct acpi_device *adev_dimm = nfit_mem->adev;
1432
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001433 if (nfit_mem->flags_attr) {
1434 sysfs_put(nfit_mem->flags_attr);
1435 nfit_mem->flags_attr = NULL;
1436 }
Dan Williams231bf112016-08-22 19:23:25 -07001437 if (adev_dimm)
1438 acpi_remove_notify_handler(adev_dimm->handle,
1439 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001440 }
1441 mutex_unlock(&acpi_desc->init_mutex);
1442}
1443
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001444static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1445{
1446 struct nfit_mem *nfit_mem;
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001447 int dimm_count = 0, rc;
1448 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001449
1450 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001451 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001452 unsigned long flags = 0, cmd_mask;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001453 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001454 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001455
1456 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1457 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1458 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001459 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001460 continue;
1461 }
1462
1463 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1464 flags |= NDD_ALIASING;
1465
Dan Williams58138822015-06-23 20:08:34 -04001466 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001467 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams58138822015-06-23 20:08:34 -04001468 flags |= NDD_UNARMED;
1469
Dan Williams62232e452015-06-08 14:27:06 -04001470 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1471 if (rc)
1472 continue;
1473
Dan Williamse3654ec2016-04-28 16:17:07 -07001474 /*
Dan Williams31eca762016-04-28 16:23:43 -07001475 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1476 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1477 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001478 */
Dan Williams31eca762016-04-28 16:23:43 -07001479 cmd_mask = 1UL << ND_CMD_CALL;
1480 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1481 cmd_mask |= nfit_mem->dsm_mask;
1482
Dan Williamse5ae3b22016-06-07 17:00:04 -07001483 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1484 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001485 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001486 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001487 flags, cmd_mask, flush ? flush->hint_count : 0,
1488 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001489 if (!nvdimm)
1490 return -ENOMEM;
1491
1492 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001493 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001494
1495 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1496 continue;
1497
Toshi Kani402bae52015-08-26 10:20:23 -06001498 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001499 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001500 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1501 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1502 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001503 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
Dan Williams58138822015-06-23 20:08:34 -04001504
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001505 }
1506
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001507 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1508 if (rc)
1509 return rc;
1510
1511 /*
1512 * Now that dimms are successfully registered, and async registration
1513 * is flushed, attempt to enable event notification.
1514 */
1515 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1516 struct kernfs_node *nfit_kernfs;
1517
1518 nvdimm = nfit_mem->nvdimm;
1519 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1520 if (nfit_kernfs)
1521 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1522 "flags");
1523 sysfs_put(nfit_kernfs);
1524 if (!nfit_mem->flags_attr)
1525 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1526 nvdimm_name(nvdimm));
1527 }
1528
1529 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1530 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001531}
1532
Dan Williams62232e452015-06-08 14:27:06 -04001533static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1534{
1535 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1536 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1537 struct acpi_device *adev;
1538 int i;
1539
Dan Williamse3654ec2016-04-28 16:17:07 -07001540 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001541 adev = to_acpi_dev(acpi_desc);
1542 if (!adev)
1543 return;
1544
Dan Williamsd4f32362016-03-03 16:08:54 -08001545 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Dan Williams62232e452015-06-08 14:27:06 -04001546 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001547 set_bit(i, &nd_desc->cmd_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001548}
1549
Dan Williams1f7df6f2015-06-09 20:13:14 -04001550static ssize_t range_index_show(struct device *dev,
1551 struct device_attribute *attr, char *buf)
1552{
1553 struct nd_region *nd_region = to_nd_region(dev);
1554 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1555
1556 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1557}
1558static DEVICE_ATTR_RO(range_index);
1559
1560static struct attribute *acpi_nfit_region_attributes[] = {
1561 &dev_attr_range_index.attr,
1562 NULL,
1563};
1564
1565static struct attribute_group acpi_nfit_region_attribute_group = {
1566 .name = "nfit",
1567 .attrs = acpi_nfit_region_attributes,
1568};
1569
1570static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1571 &nd_region_attribute_group,
1572 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001573 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001574 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001575 &acpi_nfit_region_attribute_group,
1576 NULL,
1577};
1578
Dan Williamseaf96152015-05-01 13:11:27 -04001579/* enough info to uniquely specify an interleave set */
1580struct nfit_set_info {
1581 struct nfit_set_info_map {
1582 u64 region_offset;
1583 u32 serial_number;
1584 u32 pad;
1585 } mapping[0];
1586};
1587
1588static size_t sizeof_nfit_set_info(int num_mappings)
1589{
1590 return sizeof(struct nfit_set_info)
1591 + num_mappings * sizeof(struct nfit_set_info_map);
1592}
1593
1594static int cmp_map(const void *m0, const void *m1)
1595{
1596 const struct nfit_set_info_map *map0 = m0;
1597 const struct nfit_set_info_map *map1 = m1;
1598
1599 return memcmp(&map0->region_offset, &map1->region_offset,
1600 sizeof(u64));
1601}
1602
1603/* Retrieve the nth entry referencing this spa */
1604static struct acpi_nfit_memory_map *memdev_from_spa(
1605 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1606{
1607 struct nfit_memdev *nfit_memdev;
1608
1609 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1610 if (nfit_memdev->memdev->range_index == range_index)
1611 if (n-- == 0)
1612 return nfit_memdev->memdev;
1613 return NULL;
1614}
1615
1616static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1617 struct nd_region_desc *ndr_desc,
1618 struct acpi_nfit_system_address *spa)
1619{
1620 int i, spa_type = nfit_spa_type(spa);
1621 struct device *dev = acpi_desc->dev;
1622 struct nd_interleave_set *nd_set;
1623 u16 nr = ndr_desc->num_mappings;
1624 struct nfit_set_info *info;
1625
1626 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1627 /* pass */;
1628 else
1629 return 0;
1630
1631 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1632 if (!nd_set)
1633 return -ENOMEM;
1634
1635 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1636 if (!info)
1637 return -ENOMEM;
1638 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07001639 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04001640 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07001641 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04001642 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1643 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1644 spa->range_index, i);
1645
1646 if (!memdev || !nfit_mem->dcr) {
1647 dev_err(dev, "%s: failed to find DCR\n", __func__);
1648 return -ENODEV;
1649 }
1650
1651 map->region_offset = memdev->region_offset;
1652 map->serial_number = nfit_mem->dcr->serial_number;
1653 }
1654
1655 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1656 cmp_map, NULL);
1657 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1658 ndr_desc->nd_set = nd_set;
1659 devm_kfree(dev, info);
1660
1661 return 0;
1662}
1663
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001664static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1665{
1666 struct acpi_nfit_interleave *idt = mmio->idt;
1667 u32 sub_line_offset, line_index, line_offset;
1668 u64 line_no, table_skip_count, table_offset;
1669
1670 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1671 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1672 line_offset = idt->line_offset[line_index]
1673 * mmio->line_size;
1674 table_offset = table_skip_count * mmio->table_size;
1675
1676 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1677}
1678
Ross Zwislerde4a1962015-08-20 16:27:38 -06001679static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001680{
1681 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1682 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001683 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001684
1685 if (mmio->num_lines)
1686 offset = to_interleave_offset(offset, mmio);
1687
Ross Zwisler68202c92016-07-29 14:59:12 -06001688 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001689}
1690
1691static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1692 resource_size_t dpa, unsigned int len, unsigned int write)
1693{
1694 u64 cmd, offset;
1695 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1696
1697 enum {
1698 BCW_OFFSET_MASK = (1ULL << 48)-1,
1699 BCW_LEN_SHIFT = 48,
1700 BCW_LEN_MASK = (1ULL << 8) - 1,
1701 BCW_CMD_SHIFT = 56,
1702 };
1703
1704 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1705 len = len >> L1_CACHE_SHIFT;
1706 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1707 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1708
1709 offset = nfit_blk->cmd_offset + mmio->size * bw;
1710 if (mmio->num_lines)
1711 offset = to_interleave_offset(offset, mmio);
1712
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001713 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001714 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001715
Dan Williamsaef25332016-02-12 17:01:11 -08001716 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001717 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001718}
1719
1720static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1721 resource_size_t dpa, void *iobuf, size_t len, int rw,
1722 unsigned int lane)
1723{
1724 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1725 unsigned int copied = 0;
1726 u64 base_offset;
1727 int rc;
1728
1729 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1730 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001731 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1732 while (len) {
1733 unsigned int c;
1734 u64 offset;
1735
1736 if (mmio->num_lines) {
1737 u32 line_offset;
1738
1739 offset = to_interleave_offset(base_offset + copied,
1740 mmio);
1741 div_u64_rem(offset, mmio->line_size, &line_offset);
1742 c = min_t(size_t, len, mmio->line_size - line_offset);
1743 } else {
1744 offset = base_offset + nfit_blk->bdw_offset;
1745 c = len;
1746 }
1747
1748 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001749 memcpy_to_pmem(mmio->addr.aperture + offset,
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001750 iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001751 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001752 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001753 mmio_flush_range((void __force *)
1754 mmio->addr.aperture + offset, c);
1755
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001756 memcpy_from_pmem(iobuf + copied,
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001757 mmio->addr.aperture + offset, c);
1758 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001759
1760 copied += c;
1761 len -= c;
1762 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001763
1764 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001765 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001766
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001767 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1768 return rc;
1769}
1770
1771static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1772 resource_size_t dpa, void *iobuf, u64 len, int rw)
1773{
1774 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1775 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1776 struct nd_region *nd_region = nfit_blk->nd_region;
1777 unsigned int lane, copied = 0;
1778 int rc = 0;
1779
1780 lane = nd_region_acquire_lane(nd_region);
1781 while (len) {
1782 u64 c = min(len, mmio->size);
1783
1784 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1785 iobuf + copied, c, rw, lane);
1786 if (rc)
1787 break;
1788
1789 copied += c;
1790 len -= c;
1791 }
1792 nd_region_release_lane(nd_region, lane);
1793
1794 return rc;
1795}
1796
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001797static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1798 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1799{
1800 if (idt) {
1801 mmio->num_lines = idt->line_count;
1802 mmio->line_size = idt->line_size;
1803 if (interleave_ways == 0)
1804 return -ENXIO;
1805 mmio->table_size = mmio->num_lines * interleave_ways
1806 * mmio->line_size;
1807 }
1808
1809 return 0;
1810}
1811
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001812static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1813 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1814{
1815 struct nd_cmd_dimm_flags flags;
1816 int rc;
1817
1818 memset(&flags, 0, sizeof(flags));
1819 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08001820 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001821
1822 if (rc >= 0 && flags.status == 0)
1823 nfit_blk->dimm_flags = flags.flags;
1824 else if (rc == -ENOTTY) {
1825 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08001826 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001827 rc = 0;
1828 } else
1829 rc = -ENXIO;
1830
1831 return rc;
1832}
1833
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001834static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1835 struct device *dev)
1836{
1837 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001838 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1839 struct nfit_blk_mmio *mmio;
1840 struct nfit_blk *nfit_blk;
1841 struct nfit_mem *nfit_mem;
1842 struct nvdimm *nvdimm;
1843 int rc;
1844
1845 nvdimm = nd_blk_region_to_dimm(ndbr);
1846 nfit_mem = nvdimm_provider_data(nvdimm);
1847 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1848 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1849 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04001850 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1851 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001852 return -ENXIO;
1853 }
1854
1855 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1856 if (!nfit_blk)
1857 return -ENOMEM;
1858 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1859 nfit_blk->nd_region = to_nd_region(dev);
1860
1861 /* map block aperture memory */
1862 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1863 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07001864 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
1865 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001866 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001867 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1868 nvdimm_name(nvdimm));
1869 return -ENOMEM;
1870 }
1871 mmio->size = nfit_mem->bdw->size;
1872 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1873 mmio->idt = nfit_mem->idt_bdw;
1874 mmio->spa = nfit_mem->spa_bdw;
1875 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1876 nfit_mem->memdev_bdw->interleave_ways);
1877 if (rc) {
1878 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1879 __func__, nvdimm_name(nvdimm));
1880 return rc;
1881 }
1882
1883 /* map block control memory */
1884 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1885 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1886 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07001887 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1888 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001889 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001890 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1891 nvdimm_name(nvdimm));
1892 return -ENOMEM;
1893 }
1894 mmio->size = nfit_mem->dcr->window_size;
1895 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1896 mmio->idt = nfit_mem->idt_dcr;
1897 mmio->spa = nfit_mem->spa_dcr;
1898 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1899 nfit_mem->memdev_dcr->interleave_ways);
1900 if (rc) {
1901 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1902 __func__, nvdimm_name(nvdimm));
1903 return rc;
1904 }
1905
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001906 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1907 if (rc < 0) {
1908 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1909 __func__, nvdimm_name(nvdimm));
1910 return rc;
1911 }
1912
Dan Williamsf284a4f2016-07-07 19:44:50 -07001913 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001914 dev_warn(dev, "unable to guarantee persistence of writes\n");
1915
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001916 if (mmio->line_size == 0)
1917 return 0;
1918
1919 if ((u32) nfit_blk->cmd_offset % mmio->line_size
1920 + 8 > mmio->line_size) {
1921 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1922 return -ENXIO;
1923 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1924 + 8 > mmio->line_size) {
1925 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
1926 return -ENXIO;
1927 }
1928
1929 return 0;
1930}
1931
Dan Williamsaef25332016-02-12 17:01:11 -08001932static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08001933 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001934{
Dan Williamsaef25332016-02-12 17:01:11 -08001935 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001936 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08001937 int cmd_rc, rc;
1938
Dan Williams1cf03c02016-02-17 13:01:23 -08001939 cmd->address = spa->address;
1940 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08001941 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
1942 sizeof(*cmd), &cmd_rc);
1943 if (rc < 0)
1944 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001945 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001946}
1947
Dan Williams1cf03c02016-02-17 13:01:23 -08001948static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001949{
1950 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001951 int cmd_rc;
1952 struct nd_cmd_ars_start ars_start;
1953 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1954 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001955
Dan Williams1cf03c02016-02-17 13:01:23 -08001956 memset(&ars_start, 0, sizeof(ars_start));
1957 ars_start.address = spa->address;
1958 ars_start.length = spa->length;
1959 if (nfit_spa_type(spa) == NFIT_SPA_PM)
1960 ars_start.type = ND_ARS_PERSISTENT;
1961 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
1962 ars_start.type = ND_ARS_VOLATILE;
1963 else
1964 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07001965
Dan Williams1cf03c02016-02-17 13:01:23 -08001966 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1967 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08001968
Dan Williams1cf03c02016-02-17 13:01:23 -08001969 if (rc < 0)
1970 return rc;
1971 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001972}
1973
Dan Williams1cf03c02016-02-17 13:01:23 -08001974static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07001975{
Dan Williamsaef25332016-02-12 17:01:11 -08001976 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001977 struct nd_cmd_ars_start ars_start;
1978 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1979 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07001980
Dan Williams1cf03c02016-02-17 13:01:23 -08001981 memset(&ars_start, 0, sizeof(ars_start));
1982 ars_start.address = ars_status->restart_address;
1983 ars_start.length = ars_status->restart_length;
1984 ars_start.type = ars_status->type;
1985 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1986 sizeof(ars_start), &cmd_rc);
1987 if (rc < 0)
1988 return rc;
1989 return cmd_rc;
1990}
Dan Williamsaef25332016-02-12 17:01:11 -08001991
Dan Williams1cf03c02016-02-17 13:01:23 -08001992static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
1993{
1994 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1995 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
1996 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08001997
Dan Williams1cf03c02016-02-17 13:01:23 -08001998 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
1999 acpi_desc->ars_status_size, &cmd_rc);
2000 if (rc < 0)
2001 return rc;
2002 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07002003}
2004
2005static int ars_status_process_records(struct nvdimm_bus *nvdimm_bus,
Dan Williams1cf03c02016-02-17 13:01:23 -08002006 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07002007{
2008 int rc;
2009 u32 i;
2010
Vishal Verma0caeef62015-12-24 19:21:43 -07002011 for (i = 0; i < ars_status->num_records; i++) {
2012 rc = nvdimm_bus_add_poison(nvdimm_bus,
2013 ars_status->records[i].err_address,
2014 ars_status->records[i].length);
2015 if (rc)
2016 return rc;
2017 }
2018
2019 return 0;
2020}
2021
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002022static void acpi_nfit_remove_resource(void *data)
2023{
2024 struct resource *res = data;
2025
2026 remove_resource(res);
2027}
2028
2029static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2030 struct nd_region_desc *ndr_desc)
2031{
2032 struct resource *res, *nd_res = ndr_desc->res;
2033 int is_pmem, ret;
2034
2035 /* No operation if the region is already registered as PMEM */
2036 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2037 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2038 if (is_pmem == REGION_INTERSECTS)
2039 return 0;
2040
2041 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2042 if (!res)
2043 return -ENOMEM;
2044
2045 res->name = "Persistent Memory";
2046 res->start = nd_res->start;
2047 res->end = nd_res->end;
2048 res->flags = IORESOURCE_MEM;
2049 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2050
2051 ret = insert_resource(&iomem_resource, res);
2052 if (ret)
2053 return ret;
2054
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302055 ret = devm_add_action_or_reset(acpi_desc->dev,
2056 acpi_nfit_remove_resource,
2057 res);
2058 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002059 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002060
2061 return 0;
2062}
2063
Dan Williams1f7df6f2015-06-09 20:13:14 -04002064static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002065 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002066 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002067 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002068{
2069 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2070 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002071 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002072 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002073 struct nfit_mem *nfit_mem;
2074 int blk_valid = 0;
2075
2076 if (!nvdimm) {
2077 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2078 spa->range_index, memdev->device_handle);
2079 return -ENODEV;
2080 }
2081
Dan Williams44c462e2016-09-19 16:38:50 -07002082 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002083 switch (nfit_spa_type(spa)) {
2084 case NFIT_SPA_PM:
2085 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002086 mapping->start = memdev->address;
2087 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002088 break;
2089 case NFIT_SPA_DCR:
2090 nfit_mem = nvdimm_provider_data(nvdimm);
2091 if (!nfit_mem || !nfit_mem->bdw) {
2092 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2093 spa->range_index, nvdimm_name(nvdimm));
2094 } else {
Dan Williams44c462e2016-09-19 16:38:50 -07002095 mapping->size = nfit_mem->bdw->capacity;
2096 mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04002097 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002098 blk_valid = 1;
2099 }
2100
Dan Williams44c462e2016-09-19 16:38:50 -07002101 ndr_desc->mapping = mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002102 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002103 ndbr_desc = to_blk_region_desc(ndr_desc);
2104 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002105 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williams1cf03c02016-02-17 13:01:23 -08002106 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2107 ndr_desc);
2108 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002109 return -ENOMEM;
2110 break;
2111 }
2112
2113 return 0;
2114}
2115
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002116static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2117{
2118 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2119 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2120 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2121 nfit_spa_type(spa) == NFIT_SPA_PCD);
2122}
2123
Dan Williams1f7df6f2015-06-09 20:13:14 -04002124static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2125 struct nfit_spa *nfit_spa)
2126{
Dan Williams44c462e2016-09-19 16:38:50 -07002127 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002128 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002129 struct nd_blk_region_desc ndbr_desc;
2130 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002131 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002132 struct nvdimm_bus *nvdimm_bus;
2133 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002134 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002135
Dan Williams1cf03c02016-02-17 13:01:23 -08002136 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002137 return 0;
2138
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002139 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04002140 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2141 __func__);
2142 return 0;
2143 }
2144
2145 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002146 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002147 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002148 res.start = spa->address;
2149 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002150 ndr_desc = &ndbr_desc.ndr_desc;
2151 ndr_desc->res = &res;
2152 ndr_desc->provider_data = nfit_spa;
2153 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002154 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2155 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2156 spa->proximity_domain);
2157 else
2158 ndr_desc->numa_node = NUMA_NO_NODE;
2159
Dan Williams1f7df6f2015-06-09 20:13:14 -04002160 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2161 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002162 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002163
2164 if (memdev->range_index != spa->range_index)
2165 continue;
2166 if (count >= ND_MAX_MAPPINGS) {
2167 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2168 spa->range_index, ND_MAX_MAPPINGS);
2169 return -ENXIO;
2170 }
Dan Williams44c462e2016-09-19 16:38:50 -07002171 mapping = &mappings[count++];
2172 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002173 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002174 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002175 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002176 }
2177
Dan Williams44c462e2016-09-19 16:38:50 -07002178 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002179 ndr_desc->num_mappings = count;
2180 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002181 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002182 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002183
Dan Williams1f7df6f2015-06-09 20:13:14 -04002184 nvdimm_bus = acpi_desc->nvdimm_bus;
2185 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002186 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002187 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002188 dev_warn(acpi_desc->dev,
2189 "failed to insert pmem resource to iomem: %d\n",
2190 rc);
Dan Williams48901162016-03-09 17:15:43 -08002191 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002192 }
Dan Williams48901162016-03-09 17:15:43 -08002193
Dan Williams1cf03c02016-02-17 13:01:23 -08002194 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2195 ndr_desc);
2196 if (!nfit_spa->nd_region)
2197 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002198 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002199 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2200 ndr_desc);
2201 if (!nfit_spa->nd_region)
2202 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002203 } else if (nfit_spa_is_virtual(spa)) {
2204 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2205 ndr_desc);
2206 if (!nfit_spa->nd_region)
2207 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002208 }
Vishal Verma20985162015-10-27 16:58:27 -06002209
Dan Williams1cf03c02016-02-17 13:01:23 -08002210 out:
2211 if (rc)
2212 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2213 nfit_spa->spa->range_index);
2214 return rc;
2215}
2216
2217static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2218 u32 max_ars)
2219{
2220 struct device *dev = acpi_desc->dev;
2221 struct nd_cmd_ars_status *ars_status;
2222
2223 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2224 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2225 return 0;
2226 }
2227
2228 if (acpi_desc->ars_status)
2229 devm_kfree(dev, acpi_desc->ars_status);
2230 acpi_desc->ars_status = NULL;
2231 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2232 if (!ars_status)
2233 return -ENOMEM;
2234 acpi_desc->ars_status = ars_status;
2235 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002236 return 0;
2237}
2238
Dan Williams1cf03c02016-02-17 13:01:23 -08002239static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2240 struct nfit_spa *nfit_spa)
2241{
2242 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2243 int rc;
2244
2245 if (!nfit_spa->max_ars) {
2246 struct nd_cmd_ars_cap ars_cap;
2247
2248 memset(&ars_cap, 0, sizeof(ars_cap));
2249 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2250 if (rc < 0)
2251 return rc;
2252 nfit_spa->max_ars = ars_cap.max_ars_out;
2253 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2254 /* check that the supported scrub types match the spa type */
2255 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2256 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2257 return -ENOTTY;
2258 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2259 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2260 return -ENOTTY;
2261 }
2262
2263 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2264 return -ENOMEM;
2265
2266 rc = ars_get_status(acpi_desc);
2267 if (rc < 0 && rc != -ENOSPC)
2268 return rc;
2269
2270 if (ars_status_process_records(acpi_desc->nvdimm_bus,
2271 acpi_desc->ars_status))
2272 return -ENOMEM;
2273
2274 return 0;
2275}
2276
2277static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2278 struct nfit_spa *nfit_spa)
2279{
2280 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2281 unsigned int overflow_retry = scrub_overflow_abort;
2282 u64 init_ars_start = 0, init_ars_len = 0;
2283 struct device *dev = acpi_desc->dev;
2284 unsigned int tmo = scrub_timeout;
2285 int rc;
2286
Vishal Verma37b137f2016-07-23 21:51:42 -07002287 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002288 return;
2289
2290 rc = ars_start(acpi_desc, nfit_spa);
2291 /*
2292 * If we timed out the initial scan we'll still be busy here,
2293 * and will wait another timeout before giving up permanently.
2294 */
2295 if (rc < 0 && rc != -EBUSY)
2296 return;
2297
2298 do {
2299 u64 ars_start, ars_len;
2300
2301 if (acpi_desc->cancel)
2302 break;
2303 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2304 if (rc == -ENOTTY)
2305 break;
2306 if (rc == -EBUSY && !tmo) {
2307 dev_warn(dev, "range %d ars timeout, aborting\n",
2308 spa->range_index);
2309 break;
2310 }
2311
2312 if (rc == -EBUSY) {
2313 /*
2314 * Note, entries may be appended to the list
2315 * while the lock is dropped, but the workqueue
2316 * being active prevents entries being deleted /
2317 * freed.
2318 */
2319 mutex_unlock(&acpi_desc->init_mutex);
2320 ssleep(1);
2321 tmo--;
2322 mutex_lock(&acpi_desc->init_mutex);
2323 continue;
2324 }
2325
2326 /* we got some results, but there are more pending... */
2327 if (rc == -ENOSPC && overflow_retry--) {
2328 if (!init_ars_len) {
2329 init_ars_len = acpi_desc->ars_status->length;
2330 init_ars_start = acpi_desc->ars_status->address;
2331 }
2332 rc = ars_continue(acpi_desc);
2333 }
2334
2335 if (rc < 0) {
2336 dev_warn(dev, "range %d ars continuation failed\n",
2337 spa->range_index);
2338 break;
2339 }
2340
2341 if (init_ars_len) {
2342 ars_start = init_ars_start;
2343 ars_len = init_ars_len;
2344 } else {
2345 ars_start = acpi_desc->ars_status->address;
2346 ars_len = acpi_desc->ars_status->length;
2347 }
2348 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2349 spa->range_index, ars_start, ars_len);
2350 /* notify the region about new poison entries */
2351 nvdimm_region_notify(nfit_spa->nd_region,
2352 NVDIMM_REVALIDATE_POISON);
2353 break;
2354 } while (1);
2355}
2356
2357static void acpi_nfit_scrub(struct work_struct *work)
2358{
2359 struct device *dev;
2360 u64 init_scrub_length = 0;
2361 struct nfit_spa *nfit_spa;
2362 u64 init_scrub_address = 0;
2363 bool init_ars_done = false;
2364 struct acpi_nfit_desc *acpi_desc;
2365 unsigned int tmo = scrub_timeout;
2366 unsigned int overflow_retry = scrub_overflow_abort;
2367
2368 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2369 dev = acpi_desc->dev;
2370
2371 /*
2372 * We scrub in 2 phases. The first phase waits for any platform
2373 * firmware initiated scrubs to complete and then we go search for the
2374 * affected spa regions to mark them scanned. In the second phase we
2375 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002376 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2377 * the first phase, but really only care about running phase 2, where
2378 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002379 */
2380
2381 /* process platform firmware initiated scrubs */
2382 retry:
2383 mutex_lock(&acpi_desc->init_mutex);
2384 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2385 struct nd_cmd_ars_status *ars_status;
2386 struct acpi_nfit_system_address *spa;
2387 u64 ars_start, ars_len;
2388 int rc;
2389
2390 if (acpi_desc->cancel)
2391 break;
2392
2393 if (nfit_spa->nd_region)
2394 continue;
2395
2396 if (init_ars_done) {
2397 /*
2398 * No need to re-query, we're now just
2399 * reconciling all the ranges covered by the
2400 * initial scrub
2401 */
2402 rc = 0;
2403 } else
2404 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2405
2406 if (rc == -ENOTTY) {
2407 /* no ars capability, just register spa and move on */
2408 acpi_nfit_register_region(acpi_desc, nfit_spa);
2409 continue;
2410 }
2411
2412 if (rc == -EBUSY && !tmo) {
2413 /* fallthrough to directed scrub in phase 2 */
2414 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2415 break;
2416 } else if (rc == -EBUSY) {
2417 mutex_unlock(&acpi_desc->init_mutex);
2418 ssleep(1);
2419 tmo--;
2420 goto retry;
2421 }
2422
2423 /* we got some results, but there are more pending... */
2424 if (rc == -ENOSPC && overflow_retry--) {
2425 ars_status = acpi_desc->ars_status;
2426 /*
2427 * Record the original scrub range, so that we
2428 * can recall all the ranges impacted by the
2429 * initial scrub.
2430 */
2431 if (!init_scrub_length) {
2432 init_scrub_length = ars_status->length;
2433 init_scrub_address = ars_status->address;
2434 }
2435 rc = ars_continue(acpi_desc);
2436 if (rc == 0) {
2437 mutex_unlock(&acpi_desc->init_mutex);
2438 goto retry;
2439 }
2440 }
2441
2442 if (rc < 0) {
2443 /*
2444 * Initial scrub failed, we'll give it one more
2445 * try below...
2446 */
2447 break;
2448 }
2449
2450 /* We got some final results, record completed ranges */
2451 ars_status = acpi_desc->ars_status;
2452 if (init_scrub_length) {
2453 ars_start = init_scrub_address;
2454 ars_len = ars_start + init_scrub_length;
2455 } else {
2456 ars_start = ars_status->address;
2457 ars_len = ars_status->length;
2458 }
2459 spa = nfit_spa->spa;
2460
2461 if (!init_ars_done) {
2462 init_ars_done = true;
2463 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2464 ars_start, ars_len);
2465 }
2466 if (ars_start <= spa->address && ars_start + ars_len
2467 >= spa->address + spa->length)
2468 acpi_nfit_register_region(acpi_desc, nfit_spa);
2469 }
2470
2471 /*
2472 * For all the ranges not covered by an initial scrub we still
2473 * want to see if there are errors, but it's ok to discover them
2474 * asynchronously.
2475 */
2476 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2477 /*
2478 * Flag all the ranges that still need scrubbing, but
2479 * register them now to make data available.
2480 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002481 if (!nfit_spa->nd_region) {
2482 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002483 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002484 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002485 }
2486
2487 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2488 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002489 acpi_desc->scrub_count++;
2490 if (acpi_desc->scrub_count_state)
2491 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002492 mutex_unlock(&acpi_desc->init_mutex);
2493}
2494
Dan Williams1f7df6f2015-06-09 20:13:14 -04002495static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2496{
2497 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002498 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002499
Dan Williams1cf03c02016-02-17 13:01:23 -08002500 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2501 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2502 /* BLK regions don't need to wait for ars results */
2503 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2504 if (rc)
2505 return rc;
2506 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002507
Dan Williams1cf03c02016-02-17 13:01:23 -08002508 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002509 return 0;
2510}
2511
Vishal Verma20985162015-10-27 16:58:27 -06002512static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2513 struct nfit_table_prev *prev)
2514{
2515 struct device *dev = acpi_desc->dev;
2516
2517 if (!list_empty(&prev->spas) ||
2518 !list_empty(&prev->memdevs) ||
2519 !list_empty(&prev->dcrs) ||
2520 !list_empty(&prev->bdws) ||
2521 !list_empty(&prev->idts) ||
2522 !list_empty(&prev->flushes)) {
2523 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2524 return -ENXIO;
2525 }
2526 return 0;
2527}
2528
Vishal Verma37b137f2016-07-23 21:51:42 -07002529static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2530{
2531 struct device *dev = acpi_desc->dev;
2532 struct kernfs_node *nfit;
2533 struct device *bus_dev;
2534
2535 if (!ars_supported(acpi_desc->nvdimm_bus))
2536 return 0;
2537
2538 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2539 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2540 if (!nfit) {
2541 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2542 return -ENODEV;
2543 }
2544 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2545 sysfs_put(nfit);
2546 if (!acpi_desc->scrub_count_state) {
2547 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2548 return -ENODEV;
2549 }
2550
2551 return 0;
2552}
2553
Dan Williams58cd71b2016-07-21 18:05:36 -07002554static void acpi_nfit_destruct(void *data)
2555{
2556 struct acpi_nfit_desc *acpi_desc = data;
Vishal Verma37b137f2016-07-23 21:51:42 -07002557 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002558
Vishal Verma6839a6d2016-07-23 21:51:21 -07002559 /*
2560 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2561 * race teardown
2562 */
2563 mutex_lock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002564 acpi_desc->cancel = 1;
Vishal Verma37b137f2016-07-23 21:51:42 -07002565 /*
2566 * Bounce the nvdimm bus lock to make sure any in-flight
2567 * acpi_nfit_ars_rescan() submissions have had a chance to
2568 * either submit or see ->cancel set.
2569 */
2570 device_lock(bus_dev);
2571 device_unlock(bus_dev);
2572
Dan Williams58cd71b2016-07-21 18:05:36 -07002573 flush_workqueue(nfit_wq);
Vishal Verma37b137f2016-07-23 21:51:42 -07002574 if (acpi_desc->scrub_count_state)
2575 sysfs_put(acpi_desc->scrub_count_state);
Dan Williams58cd71b2016-07-21 18:05:36 -07002576 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2577 acpi_desc->nvdimm_bus = NULL;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002578 list_del(&acpi_desc->list);
2579 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002580}
2581
Dan Williamse7a11b42016-07-14 16:19:55 -07002582int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002583{
2584 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002585 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002586 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002587 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002588
Dan Williams58cd71b2016-07-21 18:05:36 -07002589 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002590 acpi_nfit_init_dsms(acpi_desc);
2591
Dan Williams58cd71b2016-07-21 18:05:36 -07002592 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2593 &acpi_desc->nd_desc);
2594 if (!acpi_desc->nvdimm_bus)
2595 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002596
Dan Williams58cd71b2016-07-21 18:05:36 -07002597 rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
2598 acpi_desc);
2599 if (rc)
2600 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002601
2602 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2603 if (rc)
2604 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002605
2606 /* register this acpi_desc for mce notifications */
2607 mutex_lock(&acpi_desc_lock);
2608 list_add_tail(&acpi_desc->list, &acpi_descs);
2609 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002610 }
2611
Vishal Verma20985162015-10-27 16:58:27 -06002612 mutex_lock(&acpi_desc->init_mutex);
2613
2614 INIT_LIST_HEAD(&prev.spas);
2615 INIT_LIST_HEAD(&prev.memdevs);
2616 INIT_LIST_HEAD(&prev.dcrs);
2617 INIT_LIST_HEAD(&prev.bdws);
2618 INIT_LIST_HEAD(&prev.idts);
2619 INIT_LIST_HEAD(&prev.flushes);
2620
2621 list_cut_position(&prev.spas, &acpi_desc->spas,
2622 acpi_desc->spas.prev);
2623 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2624 acpi_desc->memdevs.prev);
2625 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2626 acpi_desc->dcrs.prev);
2627 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2628 acpi_desc->bdws.prev);
2629 list_cut_position(&prev.idts, &acpi_desc->idts,
2630 acpi_desc->idts.prev);
2631 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2632 acpi_desc->flushes.prev);
2633
Vishal Verma20985162015-10-27 16:58:27 -06002634 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002635 while (!IS_ERR_OR_NULL(data))
2636 data = add_table(acpi_desc, &prev, data, end);
2637
2638 if (IS_ERR(data)) {
2639 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2640 PTR_ERR(data));
2641 rc = PTR_ERR(data);
2642 goto out_unlock;
2643 }
2644
2645 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2646 if (rc)
2647 goto out_unlock;
2648
Dan Williams81ed4e32016-06-10 18:20:53 -07002649 rc = nfit_mem_init(acpi_desc);
2650 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002651 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002652
2653 rc = acpi_nfit_register_dimms(acpi_desc);
2654 if (rc)
2655 goto out_unlock;
2656
2657 rc = acpi_nfit_register_regions(acpi_desc);
2658
2659 out_unlock:
2660 mutex_unlock(&acpi_desc->init_mutex);
2661 return rc;
2662}
2663EXPORT_SYMBOL_GPL(acpi_nfit_init);
2664
Dan Williams7ae0fa432016-02-19 12:16:34 -08002665struct acpi_nfit_flush_work {
2666 struct work_struct work;
2667 struct completion cmp;
2668};
2669
2670static void flush_probe(struct work_struct *work)
2671{
2672 struct acpi_nfit_flush_work *flush;
2673
2674 flush = container_of(work, typeof(*flush), work);
2675 complete(&flush->cmp);
2676}
2677
2678static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2679{
2680 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2681 struct device *dev = acpi_desc->dev;
2682 struct acpi_nfit_flush_work flush;
2683
2684 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2685 device_lock(dev);
2686 device_unlock(dev);
2687
2688 /*
2689 * Scrub work could take 10s of seconds, userspace may give up so we
2690 * need to be interruptible while waiting.
2691 */
2692 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2693 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2694 queue_work(nfit_wq, &flush.work);
2695 return wait_for_completion_interruptible(&flush.cmp);
2696}
2697
Dan Williams87bf5722016-02-22 21:50:31 -08002698static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2699 struct nvdimm *nvdimm, unsigned int cmd)
2700{
2701 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2702
2703 if (nvdimm)
2704 return 0;
2705 if (cmd != ND_CMD_ARS_START)
2706 return 0;
2707
2708 /*
2709 * The kernel and userspace may race to initiate a scrub, but
2710 * the scrub thread is prepared to lose that initial race. It
2711 * just needs guarantees that any ars it initiates are not
2712 * interrupted by any intervening start reqeusts from userspace.
2713 */
2714 if (work_busy(&acpi_desc->work))
2715 return -EBUSY;
2716
2717 return 0;
2718}
2719
Vishal Verma6839a6d2016-07-23 21:51:21 -07002720int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
Vishal Verma37b137f2016-07-23 21:51:42 -07002721{
2722 struct device *dev = acpi_desc->dev;
2723 struct nfit_spa *nfit_spa;
2724
2725 if (work_busy(&acpi_desc->work))
2726 return -EBUSY;
2727
2728 if (acpi_desc->cancel)
2729 return 0;
2730
2731 mutex_lock(&acpi_desc->init_mutex);
2732 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2733 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2734
2735 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2736 continue;
2737
2738 nfit_spa->ars_required = 1;
2739 }
2740 queue_work(nfit_wq, &acpi_desc->work);
2741 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2742 mutex_unlock(&acpi_desc->init_mutex);
2743
2744 return 0;
2745}
2746
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002747void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002748{
2749 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002750
2751 dev_set_drvdata(dev, acpi_desc);
2752 acpi_desc->dev = dev;
2753 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2754 nd_desc = &acpi_desc->nd_desc;
2755 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002756 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002757 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002758 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002759 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002760 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2761
Dan Williamsb94d5232015-05-19 22:54:31 -04002762 INIT_LIST_HEAD(&acpi_desc->spas);
2763 INIT_LIST_HEAD(&acpi_desc->dcrs);
2764 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002765 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002766 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04002767 INIT_LIST_HEAD(&acpi_desc->memdevs);
2768 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002769 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06002770 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08002771 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04002772}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002773EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04002774
2775static int acpi_nfit_add(struct acpi_device *adev)
2776{
Vishal Verma20985162015-10-27 16:58:27 -06002777 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04002778 struct acpi_nfit_desc *acpi_desc;
2779 struct device *dev = &adev->dev;
2780 struct acpi_table_header *tbl;
2781 acpi_status status = AE_OK;
2782 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07002783 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002784
Lee, Chun-Yi82595422016-01-21 20:32:10 +08002785 status = acpi_get_table_with_size(ACPI_SIG_NFIT, 0, &tbl, &sz);
Dan Williamsb94d5232015-05-19 22:54:31 -04002786 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06002787 /* This is ok, we could have an nvdimm hotplugged later */
2788 dev_dbg(dev, "failed to find NFIT at startup\n");
2789 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002790 }
2791
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002792 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2793 if (!acpi_desc)
2794 return -ENOMEM;
2795 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04002796
Dan Williamse7a11b42016-07-14 16:19:55 -07002797 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05002798 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04002799
Vishal Verma20985162015-10-27 16:58:27 -06002800 /* Evaluate _FIT and override with that if present */
2801 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2802 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002803 union acpi_object *obj = buf.pointer;
2804
2805 if (obj->type == ACPI_TYPE_BUFFER)
2806 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2807 obj->buffer.length);
2808 else
Linda Knippers6b577c92015-11-20 19:05:49 -05002809 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2810 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07002811 kfree(buf.pointer);
2812 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07002813 /* skip over the lead-in header table */
2814 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2815 + sizeof(struct acpi_table_nfit),
2816 sz - sizeof(struct acpi_table_nfit));
Dan Williamse7a11b42016-07-14 16:19:55 -07002817 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002818}
2819
2820static int acpi_nfit_remove(struct acpi_device *adev)
2821{
Dan Williams58cd71b2016-07-21 18:05:36 -07002822 /* see acpi_nfit_destruct */
Dan Williamsb94d5232015-05-19 22:54:31 -04002823 return 0;
2824}
2825
Dan Williamsc14a8682016-08-18 22:15:04 -07002826void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
Vishal Verma20985162015-10-27 16:58:27 -06002827{
Dan Williamsc14a8682016-08-18 22:15:04 -07002828 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06002829 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07002830 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06002831 acpi_status status;
2832 int ret;
2833
2834 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2835
Vishal Vermac09f1212016-08-19 14:40:58 -06002836 if (event != NFIT_NOTIFY_UPDATE)
2837 return;
2838
Vishal Verma20985162015-10-27 16:58:27 -06002839 if (!dev->driver) {
2840 /* dev->driver may be null if we're being removed */
2841 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Dan Williamsc14a8682016-08-18 22:15:04 -07002842 return;
Vishal Verma20985162015-10-27 16:58:27 -06002843 }
2844
2845 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002846 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2847 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07002848 return;
2849 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002850 } else {
2851 /*
2852 * Finish previous registration before considering new
2853 * regions.
2854 */
2855 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06002856 }
2857
2858 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07002859 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06002860 if (ACPI_FAILURE(status)) {
2861 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07002862 return;
Vishal Verma20985162015-10-27 16:58:27 -06002863 }
2864
Linda Knippers6b577c92015-11-20 19:05:49 -05002865 obj = buf.pointer;
2866 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002867 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2868 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07002869 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05002870 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07002871 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05002872 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06002873 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07002874}
2875EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06002876
Dan Williamsc14a8682016-08-18 22:15:04 -07002877static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2878{
2879 device_lock(&adev->dev);
2880 __acpi_nfit_notify(&adev->dev, adev->handle, event);
2881 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06002882}
2883
Dan Williamsb94d5232015-05-19 22:54:31 -04002884static const struct acpi_device_id acpi_nfit_ids[] = {
2885 { "ACPI0012", 0 },
2886 { "", 0 },
2887};
2888MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
2889
2890static struct acpi_driver acpi_nfit_driver = {
2891 .name = KBUILD_MODNAME,
2892 .ids = acpi_nfit_ids,
2893 .ops = {
2894 .add = acpi_nfit_add,
2895 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06002896 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04002897 },
2898};
2899
2900static __init int nfit_init(void)
2901{
2902 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
2903 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
2904 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
2905 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
2906 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
2907 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
2908 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
2909
2910 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
2911 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
2912 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
2913 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
2914 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
2915 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
2916 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
2917 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
2918 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
2919 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
Dan Williams31eca762016-04-28 16:23:43 -07002920 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
2921 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
stuart hayese02fb722016-05-26 11:38:41 -05002922 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04002923
Dan Williams7ae0fa432016-02-19 12:16:34 -08002924 nfit_wq = create_singlethread_workqueue("nfit");
2925 if (!nfit_wq)
2926 return -ENOMEM;
2927
Vishal Verma6839a6d2016-07-23 21:51:21 -07002928 nfit_mce_register();
2929
Dan Williamsb94d5232015-05-19 22:54:31 -04002930 return acpi_bus_register_driver(&acpi_nfit_driver);
2931}
2932
2933static __exit void nfit_exit(void)
2934{
Vishal Verma6839a6d2016-07-23 21:51:21 -07002935 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04002936 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002937 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002938 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04002939}
2940
2941module_init(nfit_init);
2942module_exit(nfit_exit);
2943MODULE_LICENSE("GPL v2");
2944MODULE_AUTHOR("Intel Corporation");