blob: 6490a15abdd3a77605f4667aacd3fcba1d40a7bd [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 Williamsaef25332016-02-12 17:01:11 -080097static int xlat_status(void *buf, unsigned int cmd)
98{
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;
101 struct nd_cmd_ars_start *ars_start;
102 struct nd_cmd_ars_cap *ars_cap;
103 u16 flags;
104
105 switch (cmd) {
106 case ND_CMD_ARS_CAP:
107 ars_cap = buf;
108 if ((ars_cap->status & 0xffff) == NFIT_ARS_CAP_NONE)
109 return -ENOTTY;
110
111 /* Command failed */
112 if (ars_cap->status & 0xffff)
113 return -EIO;
114
115 /* No supported scan types for this range */
116 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
117 if ((ars_cap->status >> 16 & flags) == 0)
118 return -ENOTTY;
119 break;
120 case ND_CMD_ARS_START:
121 ars_start = buf;
122 /* ARS is in progress */
123 if ((ars_start->status & 0xffff) == NFIT_ARS_START_BUSY)
124 return -EBUSY;
125
126 /* Command failed */
127 if (ars_start->status & 0xffff)
128 return -EIO;
129 break;
130 case ND_CMD_ARS_STATUS:
131 ars_status = buf;
132 /* Command failed */
133 if (ars_status->status & 0xffff)
134 return -EIO;
135 /* Check extended status (Upper two bytes) */
136 if (ars_status->status == NFIT_ARS_STATUS_DONE)
137 return 0;
138
139 /* ARS is in progress */
140 if (ars_status->status == NFIT_ARS_STATUS_BUSY)
141 return -EBUSY;
142
143 /* No ARS performed for the current boot */
144 if (ars_status->status == NFIT_ARS_STATUS_NONE)
145 return -EAGAIN;
146
147 /*
148 * ARS interrupted, either we overflowed or some other
149 * agent wants the scan to stop. If we didn't overflow
150 * then just continue with the returned results.
151 */
152 if (ars_status->status == NFIT_ARS_STATUS_INTR) {
153 if (ars_status->flags & NFIT_ARS_F_OVERFLOW)
154 return -ENOSPC;
155 return 0;
156 }
157
158 /* Unknown status */
159 if (ars_status->status >> 16)
160 return -EIO;
161 break;
Dan Williamsd4f32362016-03-03 16:08:54 -0800162 case ND_CMD_CLEAR_ERROR:
163 clear_err = buf;
164 if (clear_err->status & 0xffff)
165 return -EIO;
166 if (!clear_err->cleared)
167 return -EIO;
168 if (clear_err->length > clear_err->cleared)
169 return clear_err->cleared;
170 break;
Dan Williamsaef25332016-02-12 17:01:11 -0800171 default:
172 break;
173 }
174
175 return 0;
176}
177
Dan Williamsb94d5232015-05-19 22:54:31 -0400178static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
179 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
Dan Williamsaef25332016-02-12 17:01:11 -0800180 unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400181{
Dan Williams62232e452015-06-08 14:27:06 -0400182 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams62232e452015-06-08 14:27:06 -0400183 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700184 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400185 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700186 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400187 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700188 unsigned long cmd_mask, dsm_mask;
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;
192 u32 offset;
193 int rc, i;
194
Dan Williams31eca762016-04-28 16:23:43 -0700195 func = cmd;
196 if (cmd == ND_CMD_CALL) {
197 call_pkg = buf;
198 func = call_pkg->nd_command;
199 }
200
Dan Williams62232e452015-06-08 14:27:06 -0400201 if (nvdimm) {
202 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
203 struct acpi_device *adev = nfit_mem->adev;
204
205 if (!adev)
206 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700207 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
208 return -ENOTTY;
209
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400210 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400211 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700212 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400213 dsm_mask = nfit_mem->dsm_mask;
214 desc = nd_cmd_dimm_desc(cmd);
Dan Williams31eca762016-04-28 16:23:43 -0700215 uuid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400216 handle = adev->handle;
217 } else {
218 struct acpi_device *adev = to_acpi_dev(acpi_desc);
219
220 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700221 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700222 dsm_mask = cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400223 desc = nd_cmd_bus_desc(cmd);
224 uuid = to_nfit_uuid(NFIT_DEV_BUS);
225 handle = adev->handle;
226 dimm_name = "bus";
227 }
228
229 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
230 return -ENOTTY;
231
Dan Williams31eca762016-04-28 16:23:43 -0700232 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400233 return -ENOTTY;
234
235 in_obj.type = ACPI_TYPE_PACKAGE;
236 in_obj.package.count = 1;
237 in_obj.package.elements = &in_buf;
238 in_buf.type = ACPI_TYPE_BUFFER;
239 in_buf.buffer.pointer = buf;
240 in_buf.buffer.length = 0;
241
242 /* libnvdimm has already validated the input envelope */
243 for (i = 0; i < desc->in_num; i++)
244 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
245 i, buf);
246
Dan Williams31eca762016-04-28 16:23:43 -0700247 if (call_pkg) {
248 /* skip over package wrapper */
249 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
250 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400251 }
252
Dan Williams31eca762016-04-28 16:23:43 -0700253 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
254 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
255 __func__, dimm_name, cmd, func,
256 in_buf.buffer.length);
257 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
258 in_buf.buffer.pointer,
259 min_t(u32, 256, in_buf.buffer.length), true);
260 }
261
262 out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj);
Dan Williams62232e452015-06-08 14:27:06 -0400263 if (!out_obj) {
264 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
265 cmd_name);
266 return -EINVAL;
267 }
268
Dan Williams31eca762016-04-28 16:23:43 -0700269 if (call_pkg) {
270 call_pkg->nd_fw_size = out_obj->buffer.length;
271 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
272 out_obj->buffer.pointer,
273 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
274
275 ACPI_FREE(out_obj);
276 /*
277 * Need to support FW function w/o known size in advance.
278 * Caller can determine required size based upon nd_fw_size.
279 * If we return an error (like elsewhere) then caller wouldn't
280 * be able to rely upon data returned to make calculation.
281 */
282 return 0;
283 }
284
Dan Williams62232e452015-06-08 14:27:06 -0400285 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
286 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
287 __func__, dimm_name, cmd_name, out_obj->type);
288 rc = -EINVAL;
289 goto out;
290 }
291
292 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
293 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
294 dimm_name, cmd_name, out_obj->buffer.length);
295 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
296 4, out_obj->buffer.pointer, min_t(u32, 128,
297 out_obj->buffer.length), true);
298 }
299
300 for (i = 0, offset = 0; i < desc->out_num; i++) {
301 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
302 (u32 *) out_obj->buffer.pointer);
303
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 }
320 if (offset + in_buf.buffer.length < buf_len) {
321 if (i >= 1) {
322 /*
323 * status valid, return the number of bytes left
324 * unfilled in the output buffer
325 */
326 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800327 if (cmd_rc)
328 *cmd_rc = xlat_status(buf, cmd);
Dan Williams62232e452015-06-08 14:27:06 -0400329 } else {
330 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
331 __func__, dimm_name, cmd_name, buf_len,
332 offset);
333 rc = -ENXIO;
334 }
Dan Williams2eea6582016-05-02 09:11:53 -0700335 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400336 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700337 if (cmd_rc)
338 *cmd_rc = xlat_status(buf, cmd);
339 }
Dan Williams62232e452015-06-08 14:27:06 -0400340
341 out:
342 ACPI_FREE(out_obj);
343
344 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400345}
346
347static const char *spa_type_name(u16 type)
348{
349 static const char *to_name[] = {
350 [NFIT_SPA_VOLATILE] = "volatile",
351 [NFIT_SPA_PM] = "pmem",
352 [NFIT_SPA_DCR] = "dimm-control-region",
353 [NFIT_SPA_BDW] = "block-data-window",
354 [NFIT_SPA_VDISK] = "volatile-disk",
355 [NFIT_SPA_VCD] = "volatile-cd",
356 [NFIT_SPA_PDISK] = "persistent-disk",
357 [NFIT_SPA_PCD] = "persistent-cd",
358
359 };
360
361 if (type > NFIT_SPA_PCD)
362 return "unknown";
363
364 return to_name[type];
365}
366
Vishal Verma6839a6d2016-07-23 21:51:21 -0700367int nfit_spa_type(struct acpi_nfit_system_address *spa)
Dan Williamsb94d5232015-05-19 22:54:31 -0400368{
369 int i;
370
371 for (i = 0; i < NFIT_UUID_MAX; i++)
372 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
373 return i;
374 return -1;
375}
376
377static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600378 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400379 struct acpi_nfit_system_address *spa)
380{
381 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600382 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400383
Dan Williams31932042016-07-14 17:22:48 -0700384 if (spa->header.length != sizeof(*spa))
385 return false;
386
Vishal Verma20985162015-10-27 16:58:27 -0600387 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700388 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600389 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
390 return true;
391 }
392 }
393
Dan Williams31932042016-07-14 17:22:48 -0700394 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
395 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400396 if (!nfit_spa)
397 return false;
398 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700399 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400400 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
401 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
402 spa->range_index,
403 spa_type_name(nfit_spa_type(spa)));
404 return true;
405}
406
407static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600408 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400409 struct acpi_nfit_memory_map *memdev)
410{
411 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600412 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400413
Dan Williams31932042016-07-14 17:22:48 -0700414 if (memdev->header.length != sizeof(*memdev))
415 return false;
416
Vishal Verma20985162015-10-27 16:58:27 -0600417 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700418 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600419 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
420 return true;
421 }
422
Dan Williams31932042016-07-14 17:22:48 -0700423 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
424 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400425 if (!nfit_memdev)
426 return false;
427 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700428 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400429 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
430 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
431 __func__, memdev->device_handle, memdev->range_index,
432 memdev->region_index);
433 return true;
434}
435
Dan Williams31932042016-07-14 17:22:48 -0700436/*
437 * An implementation may provide a truncated control region if no block windows
438 * are defined.
439 */
440static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
441{
442 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
443 window_size))
444 return 0;
445 if (dcr->windows)
446 return sizeof(*dcr);
447 return offsetof(struct acpi_nfit_control_region, window_size);
448}
449
Dan Williamsb94d5232015-05-19 22:54:31 -0400450static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600451 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400452 struct acpi_nfit_control_region *dcr)
453{
454 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600455 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400456
Dan Williams31932042016-07-14 17:22:48 -0700457 if (!sizeof_dcr(dcr))
458 return false;
459
Vishal Verma20985162015-10-27 16:58:27 -0600460 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700461 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600462 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
463 return true;
464 }
465
Dan Williams31932042016-07-14 17:22:48 -0700466 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
467 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400468 if (!nfit_dcr)
469 return false;
470 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700471 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400472 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
473 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
474 dcr->region_index, dcr->windows);
475 return true;
476}
477
478static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600479 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400480 struct acpi_nfit_data_region *bdw)
481{
482 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600483 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400484
Dan Williams31932042016-07-14 17:22:48 -0700485 if (bdw->header.length != sizeof(*bdw))
486 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600487 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700488 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600489 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
490 return true;
491 }
492
Dan Williams31932042016-07-14 17:22:48 -0700493 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
494 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400495 if (!nfit_bdw)
496 return false;
497 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700498 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400499 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
500 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
501 bdw->region_index, bdw->windows);
502 return true;
503}
504
Dan Williams31932042016-07-14 17:22:48 -0700505static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
506{
507 if (idt->header.length < sizeof(*idt))
508 return 0;
509 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
510}
511
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400512static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600513 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400514 struct acpi_nfit_interleave *idt)
515{
516 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600517 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400518
Dan Williams31932042016-07-14 17:22:48 -0700519 if (!sizeof_idt(idt))
520 return false;
521
522 list_for_each_entry(nfit_idt, &prev->idts, list) {
523 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
524 continue;
525
526 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600527 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
528 return true;
529 }
Dan Williams31932042016-07-14 17:22:48 -0700530 }
Vishal Verma20985162015-10-27 16:58:27 -0600531
Dan Williams31932042016-07-14 17:22:48 -0700532 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
533 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400534 if (!nfit_idt)
535 return false;
536 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700537 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400538 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
539 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
540 idt->interleave_index, idt->line_count);
541 return true;
542}
543
Dan Williams31932042016-07-14 17:22:48 -0700544static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
545{
546 if (flush->header.length < sizeof(*flush))
547 return 0;
548 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
549}
550
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600551static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600552 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600553 struct acpi_nfit_flush_address *flush)
554{
555 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600556 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600557
Dan Williams31932042016-07-14 17:22:48 -0700558 if (!sizeof_flush(flush))
559 return false;
560
561 list_for_each_entry(nfit_flush, &prev->flushes, list) {
562 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
563 continue;
564
565 if (memcmp(nfit_flush->flush, flush,
566 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600567 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
568 return true;
569 }
Dan Williams31932042016-07-14 17:22:48 -0700570 }
Vishal Verma20985162015-10-27 16:58:27 -0600571
Dan Williams31932042016-07-14 17:22:48 -0700572 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
573 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600574 if (!nfit_flush)
575 return false;
576 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700577 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600578 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
579 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
580 flush->device_handle, flush->hint_count);
581 return true;
582}
583
Vishal Verma20985162015-10-27 16:58:27 -0600584static void *add_table(struct acpi_nfit_desc *acpi_desc,
585 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400586{
587 struct device *dev = acpi_desc->dev;
588 struct acpi_nfit_header *hdr;
589 void *err = ERR_PTR(-ENOMEM);
590
591 if (table >= end)
592 return NULL;
593
594 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600595 if (!hdr->length) {
596 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
597 hdr->type);
598 return NULL;
599 }
600
Dan Williamsb94d5232015-05-19 22:54:31 -0400601 switch (hdr->type) {
602 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600603 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400604 return err;
605 break;
606 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600607 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400608 return err;
609 break;
610 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600611 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400612 return err;
613 break;
614 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600615 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400616 return err;
617 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400618 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600619 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400620 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400621 break;
622 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600623 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600624 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400625 break;
626 case ACPI_NFIT_TYPE_SMBIOS:
627 dev_dbg(dev, "%s: smbios\n", __func__);
628 break;
629 default:
630 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
631 break;
632 }
633
634 return table + hdr->length;
635}
636
637static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
638 struct nfit_mem *nfit_mem)
639{
640 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
641 u16 dcr = nfit_mem->dcr->region_index;
642 struct nfit_spa *nfit_spa;
643
644 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
645 u16 range_index = nfit_spa->spa->range_index;
646 int type = nfit_spa_type(nfit_spa->spa);
647 struct nfit_memdev *nfit_memdev;
648
649 if (type != NFIT_SPA_BDW)
650 continue;
651
652 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
653 if (nfit_memdev->memdev->range_index != range_index)
654 continue;
655 if (nfit_memdev->memdev->device_handle != device_handle)
656 continue;
657 if (nfit_memdev->memdev->region_index != dcr)
658 continue;
659
660 nfit_mem->spa_bdw = nfit_spa->spa;
661 return;
662 }
663 }
664
665 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
666 nfit_mem->spa_dcr->range_index);
667 nfit_mem->bdw = NULL;
668}
669
Dan Williams6697b2c2016-02-04 16:51:00 -0800670static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400671 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
672{
673 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400674 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400675 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400676 struct nfit_idt *nfit_idt;
677 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400678
Dan Williamsb94d5232015-05-19 22:54:31 -0400679 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
680 if (nfit_bdw->bdw->region_index != dcr)
681 continue;
682 nfit_mem->bdw = nfit_bdw->bdw;
683 break;
684 }
685
686 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800687 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400688
689 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400690
691 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800692 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400693
694 range_index = nfit_mem->spa_bdw->range_index;
695 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
696 if (nfit_memdev->memdev->range_index != range_index ||
697 nfit_memdev->memdev->region_index != dcr)
698 continue;
699 nfit_mem->memdev_bdw = nfit_memdev->memdev;
700 idt_idx = nfit_memdev->memdev->interleave_index;
701 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
702 if (nfit_idt->idt->interleave_index != idt_idx)
703 continue;
704 nfit_mem->idt_bdw = nfit_idt->idt;
705 break;
706 }
707 break;
708 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400709}
710
711static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
712 struct acpi_nfit_system_address *spa)
713{
714 struct nfit_mem *nfit_mem, *found;
715 struct nfit_memdev *nfit_memdev;
716 int type = nfit_spa_type(spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400717
718 switch (type) {
719 case NFIT_SPA_DCR:
720 case NFIT_SPA_PM:
721 break;
722 default:
723 return 0;
724 }
725
726 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700727 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800728 struct nfit_dcr *nfit_dcr;
729 u32 device_handle;
730 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400731
732 if (nfit_memdev->memdev->range_index != spa->range_index)
733 continue;
734 found = NULL;
735 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800736 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400737 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800738 if (__to_nfit_memdev(nfit_mem)->device_handle
739 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400740 found = nfit_mem;
741 break;
742 }
743
744 if (found)
745 nfit_mem = found;
746 else {
747 nfit_mem = devm_kzalloc(acpi_desc->dev,
748 sizeof(*nfit_mem), GFP_KERNEL);
749 if (!nfit_mem)
750 return -ENOMEM;
751 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700752 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800753 list_add(&nfit_mem->list, &acpi_desc->dimms);
754 }
755
756 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
757 if (nfit_dcr->dcr->region_index != dcr)
758 continue;
759 /*
760 * Record the control region for the dimm. For
761 * the ACPI 6.1 case, where there are separate
762 * control regions for the pmem vs blk
763 * interfaces, be sure to record the extended
764 * blk details.
765 */
766 if (!nfit_mem->dcr)
767 nfit_mem->dcr = nfit_dcr->dcr;
768 else if (nfit_mem->dcr->windows == 0
769 && nfit_dcr->dcr->windows)
770 nfit_mem->dcr = nfit_dcr->dcr;
771 break;
772 }
773
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700774 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700775 struct acpi_nfit_flush_address *flush;
776 u16 i;
777
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700778 if (nfit_flush->flush->device_handle != device_handle)
779 continue;
780 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700781 flush = nfit_flush->flush;
782 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
783 flush->hint_count
784 * sizeof(struct resource), GFP_KERNEL);
785 if (!nfit_mem->flush_wpq)
786 return -ENOMEM;
787 for (i = 0; i < flush->hint_count; i++) {
788 struct resource *res = &nfit_mem->flush_wpq[i];
789
790 res->start = flush->hint_address[i];
791 res->end = res->start + 8 - 1;
792 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700793 break;
794 }
795
Dan Williams6697b2c2016-02-04 16:51:00 -0800796 if (dcr && !nfit_mem->dcr) {
797 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
798 spa->range_index, dcr);
799 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400800 }
801
802 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400803 struct nfit_idt *nfit_idt;
804 u16 idt_idx;
805
Dan Williamsb94d5232015-05-19 22:54:31 -0400806 /* multiple dimms may share a SPA when interleaved */
807 nfit_mem->spa_dcr = spa;
808 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400809 idt_idx = nfit_memdev->memdev->interleave_index;
810 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
811 if (nfit_idt->idt->interleave_index != idt_idx)
812 continue;
813 nfit_mem->idt_dcr = nfit_idt->idt;
814 break;
815 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800816 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400817 } else {
818 /*
819 * A single dimm may belong to multiple SPA-PM
820 * ranges, record at least one in addition to
821 * any SPA-DCR range.
822 */
823 nfit_mem->memdev_pmem = nfit_memdev->memdev;
824 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400825 }
826
827 return 0;
828}
829
830static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
831{
832 struct nfit_mem *a = container_of(_a, typeof(*a), list);
833 struct nfit_mem *b = container_of(_b, typeof(*b), list);
834 u32 handleA, handleB;
835
836 handleA = __to_nfit_memdev(a)->device_handle;
837 handleB = __to_nfit_memdev(b)->device_handle;
838 if (handleA < handleB)
839 return -1;
840 else if (handleA > handleB)
841 return 1;
842 return 0;
843}
844
845static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
846{
847 struct nfit_spa *nfit_spa;
848
849 /*
850 * For each SPA-DCR or SPA-PMEM address range find its
851 * corresponding MEMDEV(s). From each MEMDEV find the
852 * corresponding DCR. Then, if we're operating on a SPA-DCR,
853 * try to find a SPA-BDW and a corresponding BDW that references
854 * the DCR. Throw it all into an nfit_mem object. Note, that
855 * BDWs are optional.
856 */
857 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
858 int rc;
859
860 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
861 if (rc)
862 return rc;
863 }
864
865 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
866
867 return 0;
868}
869
Dan Williams45def222015-04-26 19:26:48 -0400870static ssize_t revision_show(struct device *dev,
871 struct device_attribute *attr, char *buf)
872{
873 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
874 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
875 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
876
Linda Knippers6b577c92015-11-20 19:05:49 -0500877 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400878}
879static DEVICE_ATTR_RO(revision);
880
Vishal Verma9ffd6352016-09-30 17:19:29 -0600881static ssize_t hw_error_scrub_show(struct device *dev,
882 struct device_attribute *attr, char *buf)
883{
884 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
885 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
886 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
887
888 return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
889}
890
891/*
892 * The 'hw_error_scrub' attribute can have the following values written to it:
893 * '0': Switch to the default mode where an exception will only insert
894 * the address of the memory error into the poison and badblocks lists.
895 * '1': Enable a full scrub to happen if an exception for a memory error is
896 * received.
897 */
898static ssize_t hw_error_scrub_store(struct device *dev,
899 struct device_attribute *attr, const char *buf, size_t size)
900{
901 struct nvdimm_bus_descriptor *nd_desc;
902 ssize_t rc;
903 long val;
904
905 rc = kstrtol(buf, 0, &val);
906 if (rc)
907 return rc;
908
909 device_lock(dev);
910 nd_desc = dev_get_drvdata(dev);
911 if (nd_desc) {
912 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
913
914 switch (val) {
915 case HW_ERROR_SCRUB_ON:
916 acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
917 break;
918 case HW_ERROR_SCRUB_OFF:
919 acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
920 break;
921 default:
922 rc = -EINVAL;
923 break;
924 }
925 }
926 device_unlock(dev);
927 if (rc)
928 return rc;
929 return size;
930}
931static DEVICE_ATTR_RW(hw_error_scrub);
932
Vishal Verma37b137f2016-07-23 21:51:42 -0700933/*
934 * This shows the number of full Address Range Scrubs that have been
935 * completed since driver load time. Userspace can wait on this using
936 * select/poll etc. A '+' at the end indicates an ARS is in progress
937 */
938static ssize_t scrub_show(struct device *dev,
939 struct device_attribute *attr, char *buf)
940{
941 struct nvdimm_bus_descriptor *nd_desc;
942 ssize_t rc = -ENXIO;
943
944 device_lock(dev);
945 nd_desc = dev_get_drvdata(dev);
946 if (nd_desc) {
947 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
948
949 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
950 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
951 }
952 device_unlock(dev);
953 return rc;
954}
955
Vishal Verma37b137f2016-07-23 21:51:42 -0700956static ssize_t scrub_store(struct device *dev,
957 struct device_attribute *attr, const char *buf, size_t size)
958{
959 struct nvdimm_bus_descriptor *nd_desc;
960 ssize_t rc;
961 long val;
962
963 rc = kstrtol(buf, 0, &val);
964 if (rc)
965 return rc;
966 if (val != 1)
967 return -EINVAL;
968
969 device_lock(dev);
970 nd_desc = dev_get_drvdata(dev);
971 if (nd_desc) {
972 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
973
974 rc = acpi_nfit_ars_rescan(acpi_desc);
975 }
976 device_unlock(dev);
977 if (rc)
978 return rc;
979 return size;
980}
981static DEVICE_ATTR_RW(scrub);
982
983static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
984{
985 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
986 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
987 | 1 << ND_CMD_ARS_STATUS;
988
989 return (nd_desc->cmd_mask & mask) == mask;
990}
991
992static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
993{
994 struct device *dev = container_of(kobj, struct device, kobj);
995 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
996
997 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
998 return 0;
999 return a->mode;
1000}
1001
Dan Williams45def222015-04-26 19:26:48 -04001002static struct attribute *acpi_nfit_attributes[] = {
1003 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -07001004 &dev_attr_scrub.attr,
Vishal Verma9ffd6352016-09-30 17:19:29 -06001005 &dev_attr_hw_error_scrub.attr,
Dan Williams45def222015-04-26 19:26:48 -04001006 NULL,
1007};
1008
1009static struct attribute_group acpi_nfit_attribute_group = {
1010 .name = "nfit",
1011 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -07001012 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -04001013};
1014
Dan Williamsa61fe6f2016-02-19 12:29:32 -08001015static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -04001016 &nvdimm_bus_attribute_group,
1017 &acpi_nfit_attribute_group,
1018 NULL,
1019};
1020
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001021static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1022{
1023 struct nvdimm *nvdimm = to_nvdimm(dev);
1024 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1025
1026 return __to_nfit_memdev(nfit_mem);
1027}
1028
1029static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1030{
1031 struct nvdimm *nvdimm = to_nvdimm(dev);
1032 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1033
1034 return nfit_mem->dcr;
1035}
1036
1037static ssize_t handle_show(struct device *dev,
1038 struct device_attribute *attr, char *buf)
1039{
1040 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1041
1042 return sprintf(buf, "%#x\n", memdev->device_handle);
1043}
1044static DEVICE_ATTR_RO(handle);
1045
1046static ssize_t phys_id_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->physical_id);
1052}
1053static DEVICE_ATTR_RO(phys_id);
1054
1055static ssize_t vendor_show(struct device *dev,
1056 struct device_attribute *attr, char *buf)
1057{
1058 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1059
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001060 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001061}
1062static DEVICE_ATTR_RO(vendor);
1063
1064static ssize_t rev_id_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->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001070}
1071static DEVICE_ATTR_RO(rev_id);
1072
1073static ssize_t device_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->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001079}
1080static DEVICE_ATTR_RO(device);
1081
Dan Williams6ca72082016-04-29 10:33:23 -07001082static ssize_t subsystem_vendor_show(struct device *dev,
1083 struct device_attribute *attr, char *buf)
1084{
1085 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1086
1087 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1088}
1089static DEVICE_ATTR_RO(subsystem_vendor);
1090
1091static ssize_t subsystem_rev_id_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",
1097 be16_to_cpu(dcr->subsystem_revision_id));
1098}
1099static DEVICE_ATTR_RO(subsystem_rev_id);
1100
1101static ssize_t subsystem_device_show(struct device *dev,
1102 struct device_attribute *attr, char *buf)
1103{
1104 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1105
1106 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1107}
1108static DEVICE_ATTR_RO(subsystem_device);
1109
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001110static int num_nvdimm_formats(struct nvdimm *nvdimm)
1111{
1112 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1113 int formats = 0;
1114
1115 if (nfit_mem->memdev_pmem)
1116 formats++;
1117 if (nfit_mem->memdev_bdw)
1118 formats++;
1119 return formats;
1120}
1121
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001122static ssize_t format_show(struct device *dev,
1123 struct device_attribute *attr, char *buf)
1124{
1125 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1126
Dan Williams1bcbf422016-06-29 11:19:32 -07001127 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001128}
1129static DEVICE_ATTR_RO(format);
1130
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001131static ssize_t format1_show(struct device *dev,
1132 struct device_attribute *attr, char *buf)
1133{
1134 u32 handle;
1135 ssize_t rc = -ENXIO;
1136 struct nfit_mem *nfit_mem;
1137 struct nfit_memdev *nfit_memdev;
1138 struct acpi_nfit_desc *acpi_desc;
1139 struct nvdimm *nvdimm = to_nvdimm(dev);
1140 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1141
1142 nfit_mem = nvdimm_provider_data(nvdimm);
1143 acpi_desc = nfit_mem->acpi_desc;
1144 handle = to_nfit_memdev(dev)->device_handle;
1145
1146 /* assumes DIMMs have at most 2 published interface codes */
1147 mutex_lock(&acpi_desc->init_mutex);
1148 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1149 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1150 struct nfit_dcr *nfit_dcr;
1151
1152 if (memdev->device_handle != handle)
1153 continue;
1154
1155 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1156 if (nfit_dcr->dcr->region_index != memdev->region_index)
1157 continue;
1158 if (nfit_dcr->dcr->code == dcr->code)
1159 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001160 rc = sprintf(buf, "0x%04x\n",
1161 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001162 break;
1163 }
1164 if (rc != ENXIO)
1165 break;
1166 }
1167 mutex_unlock(&acpi_desc->init_mutex);
1168 return rc;
1169}
1170static DEVICE_ATTR_RO(format1);
1171
1172static ssize_t formats_show(struct device *dev,
1173 struct device_attribute *attr, char *buf)
1174{
1175 struct nvdimm *nvdimm = to_nvdimm(dev);
1176
1177 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1178}
1179static DEVICE_ATTR_RO(formats);
1180
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001181static ssize_t serial_show(struct device *dev,
1182 struct device_attribute *attr, char *buf)
1183{
1184 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1185
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001186 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001187}
1188static DEVICE_ATTR_RO(serial);
1189
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001190static ssize_t family_show(struct device *dev,
1191 struct device_attribute *attr, char *buf)
1192{
1193 struct nvdimm *nvdimm = to_nvdimm(dev);
1194 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1195
1196 if (nfit_mem->family < 0)
1197 return -ENXIO;
1198 return sprintf(buf, "%d\n", nfit_mem->family);
1199}
1200static DEVICE_ATTR_RO(family);
1201
1202static ssize_t dsm_mask_show(struct device *dev,
1203 struct device_attribute *attr, char *buf)
1204{
1205 struct nvdimm *nvdimm = to_nvdimm(dev);
1206 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1207
1208 if (nfit_mem->family < 0)
1209 return -ENXIO;
1210 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1211}
1212static DEVICE_ATTR_RO(dsm_mask);
1213
Dan Williams58138822015-06-23 20:08:34 -04001214static ssize_t flags_show(struct device *dev,
1215 struct device_attribute *attr, char *buf)
1216{
1217 u16 flags = to_nfit_memdev(dev)->flags;
1218
1219 return sprintf(buf, "%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001220 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1221 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1222 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001223 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Toshi Kani402bae52015-08-26 10:20:23 -06001224 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "");
Dan Williams58138822015-06-23 20:08:34 -04001225}
1226static DEVICE_ATTR_RO(flags);
1227
Toshi Kani38a879b2016-04-25 15:34:59 -06001228static ssize_t id_show(struct device *dev,
1229 struct device_attribute *attr, char *buf)
1230{
1231 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1232
1233 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1234 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1235 be16_to_cpu(dcr->vendor_id),
1236 dcr->manufacturing_location,
1237 be16_to_cpu(dcr->manufacturing_date),
1238 be32_to_cpu(dcr->serial_number));
1239 else
1240 return sprintf(buf, "%04x-%08x\n",
1241 be16_to_cpu(dcr->vendor_id),
1242 be32_to_cpu(dcr->serial_number));
1243}
1244static DEVICE_ATTR_RO(id);
1245
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001246static struct attribute *acpi_nfit_dimm_attributes[] = {
1247 &dev_attr_handle.attr,
1248 &dev_attr_phys_id.attr,
1249 &dev_attr_vendor.attr,
1250 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001251 &dev_attr_rev_id.attr,
1252 &dev_attr_subsystem_vendor.attr,
1253 &dev_attr_subsystem_device.attr,
1254 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001255 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001256 &dev_attr_formats.attr,
1257 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001258 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001259 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001260 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001261 &dev_attr_family.attr,
1262 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001263 NULL,
1264};
1265
1266static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1267 struct attribute *a, int n)
1268{
1269 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001270 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001271
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001272 if (!to_nfit_dcr(dev))
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001273 return 0;
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001274 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1275 return 0;
1276 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001277}
1278
1279static struct attribute_group acpi_nfit_dimm_attribute_group = {
1280 .name = "nfit",
1281 .attrs = acpi_nfit_dimm_attributes,
1282 .is_visible = acpi_nfit_dimm_attr_visible,
1283};
1284
1285static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001286 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001287 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001288 &acpi_nfit_dimm_attribute_group,
1289 NULL,
1290};
1291
1292static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1293 u32 device_handle)
1294{
1295 struct nfit_mem *nfit_mem;
1296
1297 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1298 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1299 return nfit_mem->nvdimm;
1300
1301 return NULL;
1302}
1303
Dan Williams231bf112016-08-22 19:23:25 -07001304void __acpi_nvdimm_notify(struct device *dev, u32 event)
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001305{
1306 struct nfit_mem *nfit_mem;
1307 struct acpi_nfit_desc *acpi_desc;
1308
1309 dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1310 event);
1311
1312 if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1313 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1314 event);
1315 return;
1316 }
1317
1318 acpi_desc = dev_get_drvdata(dev->parent);
1319 if (!acpi_desc)
1320 return;
1321
1322 /*
1323 * If we successfully retrieved acpi_desc, then we know nfit_mem data
1324 * is still valid.
1325 */
1326 nfit_mem = dev_get_drvdata(dev);
1327 if (nfit_mem && nfit_mem->flags_attr)
1328 sysfs_notify_dirent(nfit_mem->flags_attr);
1329}
Dan Williams231bf112016-08-22 19:23:25 -07001330EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001331
1332static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1333{
1334 struct acpi_device *adev = data;
1335 struct device *dev = &adev->dev;
1336
1337 device_lock(dev->parent);
1338 __acpi_nvdimm_notify(dev, event);
1339 device_unlock(dev->parent);
1340}
1341
Dan Williams62232e452015-06-08 14:27:06 -04001342static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1343 struct nfit_mem *nfit_mem, u32 device_handle)
1344{
1345 struct acpi_device *adev, *adev_dimm;
1346 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001347 unsigned long dsm_mask;
1348 const u8 *uuid;
Linda Knippers60e95f42015-07-22 16:17:22 -04001349 int i;
Dan Williams62232e452015-06-08 14:27:06 -04001350
Dan Williamse3654ec2016-04-28 16:17:07 -07001351 /* nfit test assumes 1:1 relationship between commands and dsms */
1352 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001353 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001354 adev = to_acpi_dev(acpi_desc);
1355 if (!adev)
1356 return 0;
1357
1358 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1359 nfit_mem->adev = adev_dimm;
1360 if (!adev_dimm) {
1361 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1362 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001363 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001364 }
1365
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001366 if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1367 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1368 dev_err(dev, "%s: notification registration failed\n",
1369 dev_name(&adev_dimm->dev));
1370 return -ENXIO;
1371 }
1372
Dan Williams31eca762016-04-28 16:23:43 -07001373 /*
stuart hayese02fb722016-05-26 11:38:41 -05001374 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001375 * different command sets. Note, that checking for function0 (bit0)
1376 * tells us if any commands are reachable through this uuid.
Dan Williams31eca762016-04-28 16:23:43 -07001377 */
stuart hayese02fb722016-05-26 11:38:41 -05001378 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001379 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Dan Williams31eca762016-04-28 16:23:43 -07001380 break;
1381
1382 /* limit the supported commands to those that are publicly documented */
1383 nfit_mem->family = i;
Dan Williams87554092016-04-28 18:01:20 -07001384 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001385 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001386 if (disable_vendor_specific)
1387 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001388 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001389 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001390 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001391 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001392 if (disable_vendor_specific)
1393 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001394 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1395 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001396 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001397 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001398 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001399 /* DSMs are optional, continue loading the driver... */
1400 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001401 }
1402
1403 uuid = to_nfit_uuid(nfit_mem->family);
1404 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Dan Williams62232e452015-06-08 14:27:06 -04001405 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
1406 set_bit(i, &nfit_mem->dsm_mask);
1407
Linda Knippers60e95f42015-07-22 16:17:22 -04001408 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001409}
1410
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001411static void shutdown_dimm_notify(void *data)
1412{
1413 struct acpi_nfit_desc *acpi_desc = data;
1414 struct nfit_mem *nfit_mem;
1415
1416 mutex_lock(&acpi_desc->init_mutex);
1417 /*
1418 * Clear out the nfit_mem->flags_attr and shut down dimm event
1419 * notifications.
1420 */
1421 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williams231bf112016-08-22 19:23:25 -07001422 struct acpi_device *adev_dimm = nfit_mem->adev;
1423
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001424 if (nfit_mem->flags_attr) {
1425 sysfs_put(nfit_mem->flags_attr);
1426 nfit_mem->flags_attr = NULL;
1427 }
Dan Williams231bf112016-08-22 19:23:25 -07001428 if (adev_dimm)
1429 acpi_remove_notify_handler(adev_dimm->handle,
1430 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001431 }
1432 mutex_unlock(&acpi_desc->init_mutex);
1433}
1434
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001435static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1436{
1437 struct nfit_mem *nfit_mem;
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001438 int dimm_count = 0, rc;
1439 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001440
1441 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001442 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001443 unsigned long flags = 0, cmd_mask;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001444 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001445 u16 mem_flags;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001446
1447 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1448 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1449 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001450 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001451 continue;
1452 }
1453
1454 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1455 flags |= NDD_ALIASING;
1456
Dan Williams58138822015-06-23 20:08:34 -04001457 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001458 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams58138822015-06-23 20:08:34 -04001459 flags |= NDD_UNARMED;
1460
Dan Williams62232e452015-06-08 14:27:06 -04001461 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1462 if (rc)
1463 continue;
1464
Dan Williamse3654ec2016-04-28 16:17:07 -07001465 /*
Dan Williams31eca762016-04-28 16:23:43 -07001466 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1467 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1468 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001469 */
Dan Williams31eca762016-04-28 16:23:43 -07001470 cmd_mask = 1UL << ND_CMD_CALL;
1471 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1472 cmd_mask |= nfit_mem->dsm_mask;
1473
Dan Williamse5ae3b22016-06-07 17:00:04 -07001474 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1475 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001476 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001477 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001478 flags, cmd_mask, flush ? flush->hint_count : 0,
1479 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001480 if (!nvdimm)
1481 return -ENOMEM;
1482
1483 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001484 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001485
1486 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1487 continue;
1488
Toshi Kani402bae52015-08-26 10:20:23 -06001489 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001490 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001491 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1492 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1493 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001494 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
Dan Williams58138822015-06-23 20:08:34 -04001495
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001496 }
1497
Dan Williamsba9c8dd2016-08-22 19:28:37 -07001498 rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1499 if (rc)
1500 return rc;
1501
1502 /*
1503 * Now that dimms are successfully registered, and async registration
1504 * is flushed, attempt to enable event notification.
1505 */
1506 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1507 struct kernfs_node *nfit_kernfs;
1508
1509 nvdimm = nfit_mem->nvdimm;
1510 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1511 if (nfit_kernfs)
1512 nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1513 "flags");
1514 sysfs_put(nfit_kernfs);
1515 if (!nfit_mem->flags_attr)
1516 dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1517 nvdimm_name(nvdimm));
1518 }
1519
1520 return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1521 acpi_desc);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001522}
1523
Dan Williams62232e452015-06-08 14:27:06 -04001524static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1525{
1526 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1527 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1528 struct acpi_device *adev;
1529 int i;
1530
Dan Williamse3654ec2016-04-28 16:17:07 -07001531 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001532 adev = to_acpi_dev(acpi_desc);
1533 if (!adev)
1534 return;
1535
Dan Williamsd4f32362016-03-03 16:08:54 -08001536 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Dan Williams62232e452015-06-08 14:27:06 -04001537 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001538 set_bit(i, &nd_desc->cmd_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001539}
1540
Dan Williams1f7df6f2015-06-09 20:13:14 -04001541static ssize_t range_index_show(struct device *dev,
1542 struct device_attribute *attr, char *buf)
1543{
1544 struct nd_region *nd_region = to_nd_region(dev);
1545 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1546
1547 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1548}
1549static DEVICE_ATTR_RO(range_index);
1550
1551static struct attribute *acpi_nfit_region_attributes[] = {
1552 &dev_attr_range_index.attr,
1553 NULL,
1554};
1555
1556static struct attribute_group acpi_nfit_region_attribute_group = {
1557 .name = "nfit",
1558 .attrs = acpi_nfit_region_attributes,
1559};
1560
1561static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1562 &nd_region_attribute_group,
1563 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001564 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001565 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001566 &acpi_nfit_region_attribute_group,
1567 NULL,
1568};
1569
Dan Williamseaf96152015-05-01 13:11:27 -04001570/* enough info to uniquely specify an interleave set */
1571struct nfit_set_info {
1572 struct nfit_set_info_map {
1573 u64 region_offset;
1574 u32 serial_number;
1575 u32 pad;
1576 } mapping[0];
1577};
1578
1579static size_t sizeof_nfit_set_info(int num_mappings)
1580{
1581 return sizeof(struct nfit_set_info)
1582 + num_mappings * sizeof(struct nfit_set_info_map);
1583}
1584
1585static int cmp_map(const void *m0, const void *m1)
1586{
1587 const struct nfit_set_info_map *map0 = m0;
1588 const struct nfit_set_info_map *map1 = m1;
1589
1590 return memcmp(&map0->region_offset, &map1->region_offset,
1591 sizeof(u64));
1592}
1593
1594/* Retrieve the nth entry referencing this spa */
1595static struct acpi_nfit_memory_map *memdev_from_spa(
1596 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1597{
1598 struct nfit_memdev *nfit_memdev;
1599
1600 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1601 if (nfit_memdev->memdev->range_index == range_index)
1602 if (n-- == 0)
1603 return nfit_memdev->memdev;
1604 return NULL;
1605}
1606
1607static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1608 struct nd_region_desc *ndr_desc,
1609 struct acpi_nfit_system_address *spa)
1610{
1611 int i, spa_type = nfit_spa_type(spa);
1612 struct device *dev = acpi_desc->dev;
1613 struct nd_interleave_set *nd_set;
1614 u16 nr = ndr_desc->num_mappings;
1615 struct nfit_set_info *info;
1616
1617 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1618 /* pass */;
1619 else
1620 return 0;
1621
1622 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1623 if (!nd_set)
1624 return -ENOMEM;
1625
1626 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1627 if (!info)
1628 return -ENOMEM;
1629 for (i = 0; i < nr; i++) {
Dan Williams44c462e2016-09-19 16:38:50 -07001630 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
Dan Williamseaf96152015-05-01 13:11:27 -04001631 struct nfit_set_info_map *map = &info->mapping[i];
Dan Williams44c462e2016-09-19 16:38:50 -07001632 struct nvdimm *nvdimm = mapping->nvdimm;
Dan Williamseaf96152015-05-01 13:11:27 -04001633 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1634 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1635 spa->range_index, i);
1636
1637 if (!memdev || !nfit_mem->dcr) {
1638 dev_err(dev, "%s: failed to find DCR\n", __func__);
1639 return -ENODEV;
1640 }
1641
1642 map->region_offset = memdev->region_offset;
1643 map->serial_number = nfit_mem->dcr->serial_number;
1644 }
1645
1646 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1647 cmp_map, NULL);
1648 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1649 ndr_desc->nd_set = nd_set;
1650 devm_kfree(dev, info);
1651
1652 return 0;
1653}
1654
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001655static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1656{
1657 struct acpi_nfit_interleave *idt = mmio->idt;
1658 u32 sub_line_offset, line_index, line_offset;
1659 u64 line_no, table_skip_count, table_offset;
1660
1661 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1662 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1663 line_offset = idt->line_offset[line_index]
1664 * mmio->line_size;
1665 table_offset = table_skip_count * mmio->table_size;
1666
1667 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1668}
1669
Ross Zwislerde4a1962015-08-20 16:27:38 -06001670static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001671{
1672 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1673 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001674 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001675
1676 if (mmio->num_lines)
1677 offset = to_interleave_offset(offset, mmio);
1678
Ross Zwisler68202c92016-07-29 14:59:12 -06001679 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001680}
1681
1682static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1683 resource_size_t dpa, unsigned int len, unsigned int write)
1684{
1685 u64 cmd, offset;
1686 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1687
1688 enum {
1689 BCW_OFFSET_MASK = (1ULL << 48)-1,
1690 BCW_LEN_SHIFT = 48,
1691 BCW_LEN_MASK = (1ULL << 8) - 1,
1692 BCW_CMD_SHIFT = 56,
1693 };
1694
1695 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1696 len = len >> L1_CACHE_SHIFT;
1697 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1698 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1699
1700 offset = nfit_blk->cmd_offset + mmio->size * bw;
1701 if (mmio->num_lines)
1702 offset = to_interleave_offset(offset, mmio);
1703
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001704 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001705 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001706
Dan Williamsaef25332016-02-12 17:01:11 -08001707 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001708 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001709}
1710
1711static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1712 resource_size_t dpa, void *iobuf, size_t len, int rw,
1713 unsigned int lane)
1714{
1715 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1716 unsigned int copied = 0;
1717 u64 base_offset;
1718 int rc;
1719
1720 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1721 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001722 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1723 while (len) {
1724 unsigned int c;
1725 u64 offset;
1726
1727 if (mmio->num_lines) {
1728 u32 line_offset;
1729
1730 offset = to_interleave_offset(base_offset + copied,
1731 mmio);
1732 div_u64_rem(offset, mmio->line_size, &line_offset);
1733 c = min_t(size_t, len, mmio->line_size - line_offset);
1734 } else {
1735 offset = base_offset + nfit_blk->bdw_offset;
1736 c = len;
1737 }
1738
1739 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001740 memcpy_to_pmem(mmio->addr.aperture + offset,
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001741 iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001742 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001743 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001744 mmio_flush_range((void __force *)
1745 mmio->addr.aperture + offset, c);
1746
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001747 memcpy_from_pmem(iobuf + copied,
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001748 mmio->addr.aperture + offset, c);
1749 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001750
1751 copied += c;
1752 len -= c;
1753 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001754
1755 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001756 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001757
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001758 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1759 return rc;
1760}
1761
1762static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1763 resource_size_t dpa, void *iobuf, u64 len, int rw)
1764{
1765 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1766 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1767 struct nd_region *nd_region = nfit_blk->nd_region;
1768 unsigned int lane, copied = 0;
1769 int rc = 0;
1770
1771 lane = nd_region_acquire_lane(nd_region);
1772 while (len) {
1773 u64 c = min(len, mmio->size);
1774
1775 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1776 iobuf + copied, c, rw, lane);
1777 if (rc)
1778 break;
1779
1780 copied += c;
1781 len -= c;
1782 }
1783 nd_region_release_lane(nd_region, lane);
1784
1785 return rc;
1786}
1787
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001788static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1789 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1790{
1791 if (idt) {
1792 mmio->num_lines = idt->line_count;
1793 mmio->line_size = idt->line_size;
1794 if (interleave_ways == 0)
1795 return -ENXIO;
1796 mmio->table_size = mmio->num_lines * interleave_ways
1797 * mmio->line_size;
1798 }
1799
1800 return 0;
1801}
1802
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001803static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1804 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1805{
1806 struct nd_cmd_dimm_flags flags;
1807 int rc;
1808
1809 memset(&flags, 0, sizeof(flags));
1810 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08001811 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001812
1813 if (rc >= 0 && flags.status == 0)
1814 nfit_blk->dimm_flags = flags.flags;
1815 else if (rc == -ENOTTY) {
1816 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08001817 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001818 rc = 0;
1819 } else
1820 rc = -ENXIO;
1821
1822 return rc;
1823}
1824
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001825static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1826 struct device *dev)
1827{
1828 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001829 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1830 struct nfit_blk_mmio *mmio;
1831 struct nfit_blk *nfit_blk;
1832 struct nfit_mem *nfit_mem;
1833 struct nvdimm *nvdimm;
1834 int rc;
1835
1836 nvdimm = nd_blk_region_to_dimm(ndbr);
1837 nfit_mem = nvdimm_provider_data(nvdimm);
1838 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1839 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1840 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04001841 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1842 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001843 return -ENXIO;
1844 }
1845
1846 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1847 if (!nfit_blk)
1848 return -ENOMEM;
1849 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1850 nfit_blk->nd_region = to_nd_region(dev);
1851
1852 /* map block aperture memory */
1853 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1854 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07001855 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
1856 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001857 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001858 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1859 nvdimm_name(nvdimm));
1860 return -ENOMEM;
1861 }
1862 mmio->size = nfit_mem->bdw->size;
1863 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1864 mmio->idt = nfit_mem->idt_bdw;
1865 mmio->spa = nfit_mem->spa_bdw;
1866 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1867 nfit_mem->memdev_bdw->interleave_ways);
1868 if (rc) {
1869 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1870 __func__, nvdimm_name(nvdimm));
1871 return rc;
1872 }
1873
1874 /* map block control memory */
1875 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1876 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1877 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07001878 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1879 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001880 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001881 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1882 nvdimm_name(nvdimm));
1883 return -ENOMEM;
1884 }
1885 mmio->size = nfit_mem->dcr->window_size;
1886 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1887 mmio->idt = nfit_mem->idt_dcr;
1888 mmio->spa = nfit_mem->spa_dcr;
1889 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1890 nfit_mem->memdev_dcr->interleave_ways);
1891 if (rc) {
1892 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1893 __func__, nvdimm_name(nvdimm));
1894 return rc;
1895 }
1896
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001897 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1898 if (rc < 0) {
1899 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1900 __func__, nvdimm_name(nvdimm));
1901 return rc;
1902 }
1903
Dan Williamsf284a4f2016-07-07 19:44:50 -07001904 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001905 dev_warn(dev, "unable to guarantee persistence of writes\n");
1906
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001907 if (mmio->line_size == 0)
1908 return 0;
1909
1910 if ((u32) nfit_blk->cmd_offset % mmio->line_size
1911 + 8 > mmio->line_size) {
1912 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1913 return -ENXIO;
1914 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1915 + 8 > mmio->line_size) {
1916 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
1917 return -ENXIO;
1918 }
1919
1920 return 0;
1921}
1922
Dan Williamsaef25332016-02-12 17:01:11 -08001923static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08001924 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001925{
Dan Williamsaef25332016-02-12 17:01:11 -08001926 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001927 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08001928 int cmd_rc, rc;
1929
Dan Williams1cf03c02016-02-17 13:01:23 -08001930 cmd->address = spa->address;
1931 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08001932 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
1933 sizeof(*cmd), &cmd_rc);
1934 if (rc < 0)
1935 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001936 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001937}
1938
Dan Williams1cf03c02016-02-17 13:01:23 -08001939static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001940{
1941 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001942 int cmd_rc;
1943 struct nd_cmd_ars_start ars_start;
1944 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1945 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001946
Dan Williams1cf03c02016-02-17 13:01:23 -08001947 memset(&ars_start, 0, sizeof(ars_start));
1948 ars_start.address = spa->address;
1949 ars_start.length = spa->length;
1950 if (nfit_spa_type(spa) == NFIT_SPA_PM)
1951 ars_start.type = ND_ARS_PERSISTENT;
1952 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
1953 ars_start.type = ND_ARS_VOLATILE;
1954 else
1955 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07001956
Dan Williams1cf03c02016-02-17 13:01:23 -08001957 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1958 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08001959
Dan Williams1cf03c02016-02-17 13:01:23 -08001960 if (rc < 0)
1961 return rc;
1962 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001963}
1964
Dan Williams1cf03c02016-02-17 13:01:23 -08001965static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07001966{
Dan Williamsaef25332016-02-12 17:01:11 -08001967 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001968 struct nd_cmd_ars_start ars_start;
1969 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1970 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07001971
Dan Williams1cf03c02016-02-17 13:01:23 -08001972 memset(&ars_start, 0, sizeof(ars_start));
1973 ars_start.address = ars_status->restart_address;
1974 ars_start.length = ars_status->restart_length;
1975 ars_start.type = ars_status->type;
1976 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1977 sizeof(ars_start), &cmd_rc);
1978 if (rc < 0)
1979 return rc;
1980 return cmd_rc;
1981}
Dan Williamsaef25332016-02-12 17:01:11 -08001982
Dan Williams1cf03c02016-02-17 13:01:23 -08001983static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
1984{
1985 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1986 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
1987 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08001988
Dan Williams1cf03c02016-02-17 13:01:23 -08001989 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
1990 acpi_desc->ars_status_size, &cmd_rc);
1991 if (rc < 0)
1992 return rc;
1993 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001994}
1995
1996static int ars_status_process_records(struct nvdimm_bus *nvdimm_bus,
Dan Williams1cf03c02016-02-17 13:01:23 -08001997 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07001998{
1999 int rc;
2000 u32 i;
2001
Vishal Verma0caeef62015-12-24 19:21:43 -07002002 for (i = 0; i < ars_status->num_records; i++) {
2003 rc = nvdimm_bus_add_poison(nvdimm_bus,
2004 ars_status->records[i].err_address,
2005 ars_status->records[i].length);
2006 if (rc)
2007 return rc;
2008 }
2009
2010 return 0;
2011}
2012
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002013static void acpi_nfit_remove_resource(void *data)
2014{
2015 struct resource *res = data;
2016
2017 remove_resource(res);
2018}
2019
2020static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2021 struct nd_region_desc *ndr_desc)
2022{
2023 struct resource *res, *nd_res = ndr_desc->res;
2024 int is_pmem, ret;
2025
2026 /* No operation if the region is already registered as PMEM */
2027 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2028 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2029 if (is_pmem == REGION_INTERSECTS)
2030 return 0;
2031
2032 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2033 if (!res)
2034 return -ENOMEM;
2035
2036 res->name = "Persistent Memory";
2037 res->start = nd_res->start;
2038 res->end = nd_res->end;
2039 res->flags = IORESOURCE_MEM;
2040 res->desc = IORES_DESC_PERSISTENT_MEMORY;
2041
2042 ret = insert_resource(&iomem_resource, res);
2043 if (ret)
2044 return ret;
2045
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05302046 ret = devm_add_action_or_reset(acpi_desc->dev,
2047 acpi_nfit_remove_resource,
2048 res);
2049 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002050 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002051
2052 return 0;
2053}
2054
Dan Williams1f7df6f2015-06-09 20:13:14 -04002055static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
Dan Williams44c462e2016-09-19 16:38:50 -07002056 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
Dan Williams1f7df6f2015-06-09 20:13:14 -04002057 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08002058 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002059{
2060 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2061 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08002062 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002063 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002064 struct nfit_mem *nfit_mem;
2065 int blk_valid = 0;
2066
2067 if (!nvdimm) {
2068 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2069 spa->range_index, memdev->device_handle);
2070 return -ENODEV;
2071 }
2072
Dan Williams44c462e2016-09-19 16:38:50 -07002073 mapping->nvdimm = nvdimm;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002074 switch (nfit_spa_type(spa)) {
2075 case NFIT_SPA_PM:
2076 case NFIT_SPA_VOLATILE:
Dan Williams44c462e2016-09-19 16:38:50 -07002077 mapping->start = memdev->address;
2078 mapping->size = memdev->region_size;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002079 break;
2080 case NFIT_SPA_DCR:
2081 nfit_mem = nvdimm_provider_data(nvdimm);
2082 if (!nfit_mem || !nfit_mem->bdw) {
2083 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2084 spa->range_index, nvdimm_name(nvdimm));
2085 } else {
Dan Williams44c462e2016-09-19 16:38:50 -07002086 mapping->size = nfit_mem->bdw->capacity;
2087 mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04002088 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002089 blk_valid = 1;
2090 }
2091
Dan Williams44c462e2016-09-19 16:38:50 -07002092 ndr_desc->mapping = mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002093 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002094 ndbr_desc = to_blk_region_desc(ndr_desc);
2095 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04002096 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williams1cf03c02016-02-17 13:01:23 -08002097 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2098 ndr_desc);
2099 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04002100 return -ENOMEM;
2101 break;
2102 }
2103
2104 return 0;
2105}
2106
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002107static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2108{
2109 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2110 nfit_spa_type(spa) == NFIT_SPA_VCD ||
2111 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2112 nfit_spa_type(spa) == NFIT_SPA_PCD);
2113}
2114
Dan Williams1f7df6f2015-06-09 20:13:14 -04002115static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2116 struct nfit_spa *nfit_spa)
2117{
Dan Williams44c462e2016-09-19 16:38:50 -07002118 static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
Dan Williams1f7df6f2015-06-09 20:13:14 -04002119 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002120 struct nd_blk_region_desc ndbr_desc;
2121 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002122 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002123 struct nvdimm_bus *nvdimm_bus;
2124 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04002125 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002126
Dan Williams1cf03c02016-02-17 13:01:23 -08002127 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06002128 return 0;
2129
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002130 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04002131 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2132 __func__);
2133 return 0;
2134 }
2135
2136 memset(&res, 0, sizeof(res));
Dan Williams44c462e2016-09-19 16:38:50 -07002137 memset(&mappings, 0, sizeof(mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002138 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002139 res.start = spa->address;
2140 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002141 ndr_desc = &ndbr_desc.ndr_desc;
2142 ndr_desc->res = &res;
2143 ndr_desc->provider_data = nfit_spa;
2144 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002145 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2146 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2147 spa->proximity_domain);
2148 else
2149 ndr_desc->numa_node = NUMA_NO_NODE;
2150
Dan Williams1f7df6f2015-06-09 20:13:14 -04002151 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2152 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
Dan Williams44c462e2016-09-19 16:38:50 -07002153 struct nd_mapping_desc *mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002154
2155 if (memdev->range_index != spa->range_index)
2156 continue;
2157 if (count >= ND_MAX_MAPPINGS) {
2158 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2159 spa->range_index, ND_MAX_MAPPINGS);
2160 return -ENXIO;
2161 }
Dan Williams44c462e2016-09-19 16:38:50 -07002162 mapping = &mappings[count++];
2163 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002164 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002165 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002166 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002167 }
2168
Dan Williams44c462e2016-09-19 16:38:50 -07002169 ndr_desc->mapping = mappings;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002170 ndr_desc->num_mappings = count;
2171 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002172 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002173 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002174
Dan Williams1f7df6f2015-06-09 20:13:14 -04002175 nvdimm_bus = acpi_desc->nvdimm_bus;
2176 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002177 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002178 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002179 dev_warn(acpi_desc->dev,
2180 "failed to insert pmem resource to iomem: %d\n",
2181 rc);
Dan Williams48901162016-03-09 17:15:43 -08002182 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002183 }
Dan Williams48901162016-03-09 17:15:43 -08002184
Dan Williams1cf03c02016-02-17 13:01:23 -08002185 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2186 ndr_desc);
2187 if (!nfit_spa->nd_region)
2188 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002189 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002190 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2191 ndr_desc);
2192 if (!nfit_spa->nd_region)
2193 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002194 } else if (nfit_spa_is_virtual(spa)) {
2195 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2196 ndr_desc);
2197 if (!nfit_spa->nd_region)
2198 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002199 }
Vishal Verma20985162015-10-27 16:58:27 -06002200
Dan Williams1cf03c02016-02-17 13:01:23 -08002201 out:
2202 if (rc)
2203 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2204 nfit_spa->spa->range_index);
2205 return rc;
2206}
2207
2208static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2209 u32 max_ars)
2210{
2211 struct device *dev = acpi_desc->dev;
2212 struct nd_cmd_ars_status *ars_status;
2213
2214 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2215 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2216 return 0;
2217 }
2218
2219 if (acpi_desc->ars_status)
2220 devm_kfree(dev, acpi_desc->ars_status);
2221 acpi_desc->ars_status = NULL;
2222 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2223 if (!ars_status)
2224 return -ENOMEM;
2225 acpi_desc->ars_status = ars_status;
2226 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002227 return 0;
2228}
2229
Dan Williams1cf03c02016-02-17 13:01:23 -08002230static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2231 struct nfit_spa *nfit_spa)
2232{
2233 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2234 int rc;
2235
2236 if (!nfit_spa->max_ars) {
2237 struct nd_cmd_ars_cap ars_cap;
2238
2239 memset(&ars_cap, 0, sizeof(ars_cap));
2240 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2241 if (rc < 0)
2242 return rc;
2243 nfit_spa->max_ars = ars_cap.max_ars_out;
2244 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2245 /* check that the supported scrub types match the spa type */
2246 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2247 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2248 return -ENOTTY;
2249 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2250 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2251 return -ENOTTY;
2252 }
2253
2254 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2255 return -ENOMEM;
2256
2257 rc = ars_get_status(acpi_desc);
2258 if (rc < 0 && rc != -ENOSPC)
2259 return rc;
2260
2261 if (ars_status_process_records(acpi_desc->nvdimm_bus,
2262 acpi_desc->ars_status))
2263 return -ENOMEM;
2264
2265 return 0;
2266}
2267
2268static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2269 struct nfit_spa *nfit_spa)
2270{
2271 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2272 unsigned int overflow_retry = scrub_overflow_abort;
2273 u64 init_ars_start = 0, init_ars_len = 0;
2274 struct device *dev = acpi_desc->dev;
2275 unsigned int tmo = scrub_timeout;
2276 int rc;
2277
Vishal Verma37b137f2016-07-23 21:51:42 -07002278 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002279 return;
2280
2281 rc = ars_start(acpi_desc, nfit_spa);
2282 /*
2283 * If we timed out the initial scan we'll still be busy here,
2284 * and will wait another timeout before giving up permanently.
2285 */
2286 if (rc < 0 && rc != -EBUSY)
2287 return;
2288
2289 do {
2290 u64 ars_start, ars_len;
2291
2292 if (acpi_desc->cancel)
2293 break;
2294 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2295 if (rc == -ENOTTY)
2296 break;
2297 if (rc == -EBUSY && !tmo) {
2298 dev_warn(dev, "range %d ars timeout, aborting\n",
2299 spa->range_index);
2300 break;
2301 }
2302
2303 if (rc == -EBUSY) {
2304 /*
2305 * Note, entries may be appended to the list
2306 * while the lock is dropped, but the workqueue
2307 * being active prevents entries being deleted /
2308 * freed.
2309 */
2310 mutex_unlock(&acpi_desc->init_mutex);
2311 ssleep(1);
2312 tmo--;
2313 mutex_lock(&acpi_desc->init_mutex);
2314 continue;
2315 }
2316
2317 /* we got some results, but there are more pending... */
2318 if (rc == -ENOSPC && overflow_retry--) {
2319 if (!init_ars_len) {
2320 init_ars_len = acpi_desc->ars_status->length;
2321 init_ars_start = acpi_desc->ars_status->address;
2322 }
2323 rc = ars_continue(acpi_desc);
2324 }
2325
2326 if (rc < 0) {
2327 dev_warn(dev, "range %d ars continuation failed\n",
2328 spa->range_index);
2329 break;
2330 }
2331
2332 if (init_ars_len) {
2333 ars_start = init_ars_start;
2334 ars_len = init_ars_len;
2335 } else {
2336 ars_start = acpi_desc->ars_status->address;
2337 ars_len = acpi_desc->ars_status->length;
2338 }
2339 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2340 spa->range_index, ars_start, ars_len);
2341 /* notify the region about new poison entries */
2342 nvdimm_region_notify(nfit_spa->nd_region,
2343 NVDIMM_REVALIDATE_POISON);
2344 break;
2345 } while (1);
2346}
2347
2348static void acpi_nfit_scrub(struct work_struct *work)
2349{
2350 struct device *dev;
2351 u64 init_scrub_length = 0;
2352 struct nfit_spa *nfit_spa;
2353 u64 init_scrub_address = 0;
2354 bool init_ars_done = false;
2355 struct acpi_nfit_desc *acpi_desc;
2356 unsigned int tmo = scrub_timeout;
2357 unsigned int overflow_retry = scrub_overflow_abort;
2358
2359 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2360 dev = acpi_desc->dev;
2361
2362 /*
2363 * We scrub in 2 phases. The first phase waits for any platform
2364 * firmware initiated scrubs to complete and then we go search for the
2365 * affected spa regions to mark them scanned. In the second phase we
2366 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002367 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2368 * the first phase, but really only care about running phase 2, where
2369 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002370 */
2371
2372 /* process platform firmware initiated scrubs */
2373 retry:
2374 mutex_lock(&acpi_desc->init_mutex);
2375 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2376 struct nd_cmd_ars_status *ars_status;
2377 struct acpi_nfit_system_address *spa;
2378 u64 ars_start, ars_len;
2379 int rc;
2380
2381 if (acpi_desc->cancel)
2382 break;
2383
2384 if (nfit_spa->nd_region)
2385 continue;
2386
2387 if (init_ars_done) {
2388 /*
2389 * No need to re-query, we're now just
2390 * reconciling all the ranges covered by the
2391 * initial scrub
2392 */
2393 rc = 0;
2394 } else
2395 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2396
2397 if (rc == -ENOTTY) {
2398 /* no ars capability, just register spa and move on */
2399 acpi_nfit_register_region(acpi_desc, nfit_spa);
2400 continue;
2401 }
2402
2403 if (rc == -EBUSY && !tmo) {
2404 /* fallthrough to directed scrub in phase 2 */
2405 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2406 break;
2407 } else if (rc == -EBUSY) {
2408 mutex_unlock(&acpi_desc->init_mutex);
2409 ssleep(1);
2410 tmo--;
2411 goto retry;
2412 }
2413
2414 /* we got some results, but there are more pending... */
2415 if (rc == -ENOSPC && overflow_retry--) {
2416 ars_status = acpi_desc->ars_status;
2417 /*
2418 * Record the original scrub range, so that we
2419 * can recall all the ranges impacted by the
2420 * initial scrub.
2421 */
2422 if (!init_scrub_length) {
2423 init_scrub_length = ars_status->length;
2424 init_scrub_address = ars_status->address;
2425 }
2426 rc = ars_continue(acpi_desc);
2427 if (rc == 0) {
2428 mutex_unlock(&acpi_desc->init_mutex);
2429 goto retry;
2430 }
2431 }
2432
2433 if (rc < 0) {
2434 /*
2435 * Initial scrub failed, we'll give it one more
2436 * try below...
2437 */
2438 break;
2439 }
2440
2441 /* We got some final results, record completed ranges */
2442 ars_status = acpi_desc->ars_status;
2443 if (init_scrub_length) {
2444 ars_start = init_scrub_address;
2445 ars_len = ars_start + init_scrub_length;
2446 } else {
2447 ars_start = ars_status->address;
2448 ars_len = ars_status->length;
2449 }
2450 spa = nfit_spa->spa;
2451
2452 if (!init_ars_done) {
2453 init_ars_done = true;
2454 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2455 ars_start, ars_len);
2456 }
2457 if (ars_start <= spa->address && ars_start + ars_len
2458 >= spa->address + spa->length)
2459 acpi_nfit_register_region(acpi_desc, nfit_spa);
2460 }
2461
2462 /*
2463 * For all the ranges not covered by an initial scrub we still
2464 * want to see if there are errors, but it's ok to discover them
2465 * asynchronously.
2466 */
2467 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2468 /*
2469 * Flag all the ranges that still need scrubbing, but
2470 * register them now to make data available.
2471 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002472 if (!nfit_spa->nd_region) {
2473 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002474 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002475 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002476 }
2477
2478 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2479 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002480 acpi_desc->scrub_count++;
2481 if (acpi_desc->scrub_count_state)
2482 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002483 mutex_unlock(&acpi_desc->init_mutex);
2484}
2485
Dan Williams1f7df6f2015-06-09 20:13:14 -04002486static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2487{
2488 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002489 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002490
Dan Williams1cf03c02016-02-17 13:01:23 -08002491 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2492 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2493 /* BLK regions don't need to wait for ars results */
2494 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2495 if (rc)
2496 return rc;
2497 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002498
Dan Williams1cf03c02016-02-17 13:01:23 -08002499 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002500 return 0;
2501}
2502
Vishal Verma20985162015-10-27 16:58:27 -06002503static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2504 struct nfit_table_prev *prev)
2505{
2506 struct device *dev = acpi_desc->dev;
2507
2508 if (!list_empty(&prev->spas) ||
2509 !list_empty(&prev->memdevs) ||
2510 !list_empty(&prev->dcrs) ||
2511 !list_empty(&prev->bdws) ||
2512 !list_empty(&prev->idts) ||
2513 !list_empty(&prev->flushes)) {
2514 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2515 return -ENXIO;
2516 }
2517 return 0;
2518}
2519
Vishal Verma37b137f2016-07-23 21:51:42 -07002520static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2521{
2522 struct device *dev = acpi_desc->dev;
2523 struct kernfs_node *nfit;
2524 struct device *bus_dev;
2525
2526 if (!ars_supported(acpi_desc->nvdimm_bus))
2527 return 0;
2528
2529 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2530 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2531 if (!nfit) {
2532 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2533 return -ENODEV;
2534 }
2535 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2536 sysfs_put(nfit);
2537 if (!acpi_desc->scrub_count_state) {
2538 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2539 return -ENODEV;
2540 }
2541
2542 return 0;
2543}
2544
Dan Williams58cd71b2016-07-21 18:05:36 -07002545static void acpi_nfit_destruct(void *data)
2546{
2547 struct acpi_nfit_desc *acpi_desc = data;
Vishal Verma37b137f2016-07-23 21:51:42 -07002548 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002549
Vishal Verma6839a6d2016-07-23 21:51:21 -07002550 /*
2551 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2552 * race teardown
2553 */
2554 mutex_lock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002555 acpi_desc->cancel = 1;
Vishal Verma37b137f2016-07-23 21:51:42 -07002556 /*
2557 * Bounce the nvdimm bus lock to make sure any in-flight
2558 * acpi_nfit_ars_rescan() submissions have had a chance to
2559 * either submit or see ->cancel set.
2560 */
2561 device_lock(bus_dev);
2562 device_unlock(bus_dev);
2563
Dan Williams58cd71b2016-07-21 18:05:36 -07002564 flush_workqueue(nfit_wq);
Vishal Verma37b137f2016-07-23 21:51:42 -07002565 if (acpi_desc->scrub_count_state)
2566 sysfs_put(acpi_desc->scrub_count_state);
Dan Williams58cd71b2016-07-21 18:05:36 -07002567 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2568 acpi_desc->nvdimm_bus = NULL;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002569 list_del(&acpi_desc->list);
2570 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002571}
2572
Dan Williamse7a11b42016-07-14 16:19:55 -07002573int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002574{
2575 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002576 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002577 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002578 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002579
Dan Williams58cd71b2016-07-21 18:05:36 -07002580 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002581 acpi_nfit_init_dsms(acpi_desc);
2582
Dan Williams58cd71b2016-07-21 18:05:36 -07002583 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2584 &acpi_desc->nd_desc);
2585 if (!acpi_desc->nvdimm_bus)
2586 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002587
Dan Williams58cd71b2016-07-21 18:05:36 -07002588 rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
2589 acpi_desc);
2590 if (rc)
2591 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002592
2593 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2594 if (rc)
2595 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002596
2597 /* register this acpi_desc for mce notifications */
2598 mutex_lock(&acpi_desc_lock);
2599 list_add_tail(&acpi_desc->list, &acpi_descs);
2600 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002601 }
2602
Vishal Verma20985162015-10-27 16:58:27 -06002603 mutex_lock(&acpi_desc->init_mutex);
2604
2605 INIT_LIST_HEAD(&prev.spas);
2606 INIT_LIST_HEAD(&prev.memdevs);
2607 INIT_LIST_HEAD(&prev.dcrs);
2608 INIT_LIST_HEAD(&prev.bdws);
2609 INIT_LIST_HEAD(&prev.idts);
2610 INIT_LIST_HEAD(&prev.flushes);
2611
2612 list_cut_position(&prev.spas, &acpi_desc->spas,
2613 acpi_desc->spas.prev);
2614 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2615 acpi_desc->memdevs.prev);
2616 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2617 acpi_desc->dcrs.prev);
2618 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2619 acpi_desc->bdws.prev);
2620 list_cut_position(&prev.idts, &acpi_desc->idts,
2621 acpi_desc->idts.prev);
2622 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2623 acpi_desc->flushes.prev);
2624
Vishal Verma20985162015-10-27 16:58:27 -06002625 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002626 while (!IS_ERR_OR_NULL(data))
2627 data = add_table(acpi_desc, &prev, data, end);
2628
2629 if (IS_ERR(data)) {
2630 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2631 PTR_ERR(data));
2632 rc = PTR_ERR(data);
2633 goto out_unlock;
2634 }
2635
2636 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2637 if (rc)
2638 goto out_unlock;
2639
Dan Williams81ed4e32016-06-10 18:20:53 -07002640 rc = nfit_mem_init(acpi_desc);
2641 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002642 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002643
2644 rc = acpi_nfit_register_dimms(acpi_desc);
2645 if (rc)
2646 goto out_unlock;
2647
2648 rc = acpi_nfit_register_regions(acpi_desc);
2649
2650 out_unlock:
2651 mutex_unlock(&acpi_desc->init_mutex);
2652 return rc;
2653}
2654EXPORT_SYMBOL_GPL(acpi_nfit_init);
2655
Dan Williams7ae0fa432016-02-19 12:16:34 -08002656struct acpi_nfit_flush_work {
2657 struct work_struct work;
2658 struct completion cmp;
2659};
2660
2661static void flush_probe(struct work_struct *work)
2662{
2663 struct acpi_nfit_flush_work *flush;
2664
2665 flush = container_of(work, typeof(*flush), work);
2666 complete(&flush->cmp);
2667}
2668
2669static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2670{
2671 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2672 struct device *dev = acpi_desc->dev;
2673 struct acpi_nfit_flush_work flush;
2674
2675 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2676 device_lock(dev);
2677 device_unlock(dev);
2678
2679 /*
2680 * Scrub work could take 10s of seconds, userspace may give up so we
2681 * need to be interruptible while waiting.
2682 */
2683 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2684 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2685 queue_work(nfit_wq, &flush.work);
2686 return wait_for_completion_interruptible(&flush.cmp);
2687}
2688
Dan Williams87bf5722016-02-22 21:50:31 -08002689static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2690 struct nvdimm *nvdimm, unsigned int cmd)
2691{
2692 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2693
2694 if (nvdimm)
2695 return 0;
2696 if (cmd != ND_CMD_ARS_START)
2697 return 0;
2698
2699 /*
2700 * The kernel and userspace may race to initiate a scrub, but
2701 * the scrub thread is prepared to lose that initial race. It
2702 * just needs guarantees that any ars it initiates are not
2703 * interrupted by any intervening start reqeusts from userspace.
2704 */
2705 if (work_busy(&acpi_desc->work))
2706 return -EBUSY;
2707
2708 return 0;
2709}
2710
Vishal Verma6839a6d2016-07-23 21:51:21 -07002711int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
Vishal Verma37b137f2016-07-23 21:51:42 -07002712{
2713 struct device *dev = acpi_desc->dev;
2714 struct nfit_spa *nfit_spa;
2715
2716 if (work_busy(&acpi_desc->work))
2717 return -EBUSY;
2718
2719 if (acpi_desc->cancel)
2720 return 0;
2721
2722 mutex_lock(&acpi_desc->init_mutex);
2723 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2724 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2725
2726 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2727 continue;
2728
2729 nfit_spa->ars_required = 1;
2730 }
2731 queue_work(nfit_wq, &acpi_desc->work);
2732 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2733 mutex_unlock(&acpi_desc->init_mutex);
2734
2735 return 0;
2736}
2737
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002738void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002739{
2740 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002741
2742 dev_set_drvdata(dev, acpi_desc);
2743 acpi_desc->dev = dev;
2744 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2745 nd_desc = &acpi_desc->nd_desc;
2746 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002747 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002748 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002749 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002750 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002751 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2752
Dan Williamsb94d5232015-05-19 22:54:31 -04002753 INIT_LIST_HEAD(&acpi_desc->spas);
2754 INIT_LIST_HEAD(&acpi_desc->dcrs);
2755 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002756 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002757 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04002758 INIT_LIST_HEAD(&acpi_desc->memdevs);
2759 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002760 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06002761 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08002762 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04002763}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002764EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04002765
2766static int acpi_nfit_add(struct acpi_device *adev)
2767{
Vishal Verma20985162015-10-27 16:58:27 -06002768 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04002769 struct acpi_nfit_desc *acpi_desc;
2770 struct device *dev = &adev->dev;
2771 struct acpi_table_header *tbl;
2772 acpi_status status = AE_OK;
2773 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07002774 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002775
Lee, Chun-Yi82595422016-01-21 20:32:10 +08002776 status = acpi_get_table_with_size(ACPI_SIG_NFIT, 0, &tbl, &sz);
Dan Williamsb94d5232015-05-19 22:54:31 -04002777 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06002778 /* This is ok, we could have an nvdimm hotplugged later */
2779 dev_dbg(dev, "failed to find NFIT at startup\n");
2780 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002781 }
2782
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002783 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2784 if (!acpi_desc)
2785 return -ENOMEM;
2786 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04002787
Dan Williamse7a11b42016-07-14 16:19:55 -07002788 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05002789 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04002790
Vishal Verma20985162015-10-27 16:58:27 -06002791 /* Evaluate _FIT and override with that if present */
2792 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2793 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002794 union acpi_object *obj = buf.pointer;
2795
2796 if (obj->type == ACPI_TYPE_BUFFER)
2797 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2798 obj->buffer.length);
2799 else
Linda Knippers6b577c92015-11-20 19:05:49 -05002800 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2801 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07002802 kfree(buf.pointer);
2803 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07002804 /* skip over the lead-in header table */
2805 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2806 + sizeof(struct acpi_table_nfit),
2807 sz - sizeof(struct acpi_table_nfit));
Dan Williamse7a11b42016-07-14 16:19:55 -07002808 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002809}
2810
2811static int acpi_nfit_remove(struct acpi_device *adev)
2812{
Dan Williams58cd71b2016-07-21 18:05:36 -07002813 /* see acpi_nfit_destruct */
Dan Williamsb94d5232015-05-19 22:54:31 -04002814 return 0;
2815}
2816
Dan Williamsc14a8682016-08-18 22:15:04 -07002817void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
Vishal Verma20985162015-10-27 16:58:27 -06002818{
Dan Williamsc14a8682016-08-18 22:15:04 -07002819 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
Vishal Verma20985162015-10-27 16:58:27 -06002820 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamse7a11b42016-07-14 16:19:55 -07002821 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06002822 acpi_status status;
2823 int ret;
2824
2825 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2826
Vishal Vermac09f1212016-08-19 14:40:58 -06002827 if (event != NFIT_NOTIFY_UPDATE)
2828 return;
2829
Vishal Verma20985162015-10-27 16:58:27 -06002830 if (!dev->driver) {
2831 /* dev->driver may be null if we're being removed */
2832 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Dan Williamsc14a8682016-08-18 22:15:04 -07002833 return;
Vishal Verma20985162015-10-27 16:58:27 -06002834 }
2835
2836 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002837 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2838 if (!acpi_desc)
Dan Williamsc14a8682016-08-18 22:15:04 -07002839 return;
2840 acpi_nfit_desc_init(acpi_desc, dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002841 } else {
2842 /*
2843 * Finish previous registration before considering new
2844 * regions.
2845 */
2846 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06002847 }
2848
2849 /* Evaluate _FIT */
Dan Williamsc14a8682016-08-18 22:15:04 -07002850 status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
Vishal Verma20985162015-10-27 16:58:27 -06002851 if (ACPI_FAILURE(status)) {
2852 dev_err(dev, "failed to evaluate _FIT\n");
Dan Williamsc14a8682016-08-18 22:15:04 -07002853 return;
Vishal Verma20985162015-10-27 16:58:27 -06002854 }
2855
Linda Knippers6b577c92015-11-20 19:05:49 -05002856 obj = buf.pointer;
2857 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002858 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2859 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07002860 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05002861 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07002862 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05002863 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06002864 kfree(buf.pointer);
Dan Williamsc14a8682016-08-18 22:15:04 -07002865}
2866EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
Vishal Verma20985162015-10-27 16:58:27 -06002867
Dan Williamsc14a8682016-08-18 22:15:04 -07002868static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2869{
2870 device_lock(&adev->dev);
2871 __acpi_nfit_notify(&adev->dev, adev->handle, event);
2872 device_unlock(&adev->dev);
Vishal Verma20985162015-10-27 16:58:27 -06002873}
2874
Dan Williamsb94d5232015-05-19 22:54:31 -04002875static const struct acpi_device_id acpi_nfit_ids[] = {
2876 { "ACPI0012", 0 },
2877 { "", 0 },
2878};
2879MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
2880
2881static struct acpi_driver acpi_nfit_driver = {
2882 .name = KBUILD_MODNAME,
2883 .ids = acpi_nfit_ids,
2884 .ops = {
2885 .add = acpi_nfit_add,
2886 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06002887 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04002888 },
2889};
2890
2891static __init int nfit_init(void)
2892{
2893 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
2894 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
2895 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
2896 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
2897 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
2898 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
2899 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
2900
2901 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
2902 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
2903 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
2904 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
2905 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
2906 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
2907 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
2908 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
2909 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
2910 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
Dan Williams31eca762016-04-28 16:23:43 -07002911 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
2912 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
stuart hayese02fb722016-05-26 11:38:41 -05002913 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04002914
Dan Williams7ae0fa432016-02-19 12:16:34 -08002915 nfit_wq = create_singlethread_workqueue("nfit");
2916 if (!nfit_wq)
2917 return -ENOMEM;
2918
Vishal Verma6839a6d2016-07-23 21:51:21 -07002919 nfit_mce_register();
2920
Dan Williamsb94d5232015-05-19 22:54:31 -04002921 return acpi_bus_register_driver(&acpi_nfit_driver);
2922}
2923
2924static __exit void nfit_exit(void)
2925{
Vishal Verma6839a6d2016-07-23 21:51:21 -07002926 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04002927 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002928 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002929 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04002930}
2931
2932module_init(nfit_init);
2933module_exit(nfit_exit);
2934MODULE_LICENSE("GPL v2");
2935MODULE_AUTHOR("Intel Corporation");