blob: e1d5ea6d5e404a151b3f362926402e1168f8cacd [file] [log] [blame]
Dan Williamsb94d5232015-05-19 22:54:31 -04001/*
2 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13#include <linux/list_sort.h>
14#include <linux/libnvdimm.h>
15#include <linux/module.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040016#include <linux/mutex.h>
Dan Williams62232e452015-06-08 14:27:06 -040017#include <linux/ndctl.h>
Vishal Verma37b137f2016-07-23 21:51:42 -070018#include <linux/sysfs.h>
Vishal Verma0caeef62015-12-24 19:21:43 -070019#include <linux/delay.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040020#include <linux/list.h>
21#include <linux/acpi.h>
Dan Williamseaf96152015-05-01 13:11:27 -040022#include <linux/sort.h>
Ross Zwislerc2ad2952015-07-10 11:06:13 -060023#include <linux/pmem.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040024#include <linux/io.h>
Dan Williams1cf03c02016-02-17 13:01:23 -080025#include <linux/nd.h>
Dan Williams96601ad2015-08-24 18:29:38 -040026#include <asm/cacheflush.h>
Dan Williamsb94d5232015-05-19 22:54:31 -040027#include "nfit.h"
28
Ross Zwisler047fc8a2015-06-25 04:21:02 -040029/*
30 * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
31 * irrelevant.
32 */
Christoph Hellwig2f8e2c82015-08-28 09:27:14 +020033#include <linux/io-64-nonatomic-hi-lo.h>
Ross Zwisler047fc8a2015-06-25 04:21:02 -040034
Dan Williams4d88a972015-05-31 14:41:48 -040035static bool force_enable_dimms;
36module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
37MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
38
Dan Williams1cf03c02016-02-17 13:01:23 -080039static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
40module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
41MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
42
43/* after three payloads of overflow, it's dead jim */
44static unsigned int scrub_overflow_abort = 3;
45module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
46MODULE_PARM_DESC(scrub_overflow_abort,
47 "Number of times we overflow ARS results before abort");
48
Dan Williams87554092016-04-28 18:01:20 -070049static bool disable_vendor_specific;
50module_param(disable_vendor_specific, bool, S_IRUGO);
51MODULE_PARM_DESC(disable_vendor_specific,
52 "Limit commands to the publicly specified set\n");
53
Vishal Verma6839a6d2016-07-23 21:51:21 -070054LIST_HEAD(acpi_descs);
55DEFINE_MUTEX(acpi_desc_lock);
56
Dan Williams7ae0fa432016-02-19 12:16:34 -080057static struct workqueue_struct *nfit_wq;
58
Vishal Verma20985162015-10-27 16:58:27 -060059struct nfit_table_prev {
60 struct list_head spas;
61 struct list_head memdevs;
62 struct list_head dcrs;
63 struct list_head bdws;
64 struct list_head idts;
65 struct list_head flushes;
66};
67
Dan Williamsb94d5232015-05-19 22:54:31 -040068static u8 nfit_uuid[NFIT_UUID_MAX][16];
69
Dan Williams6bc75612015-06-17 17:23:32 -040070const u8 *to_nfit_uuid(enum nfit_uuids id)
Dan Williamsb94d5232015-05-19 22:54:31 -040071{
72 return nfit_uuid[id];
73}
Dan Williams6bc75612015-06-17 17:23:32 -040074EXPORT_SYMBOL(to_nfit_uuid);
Dan Williamsb94d5232015-05-19 22:54:31 -040075
Dan Williams62232e452015-06-08 14:27:06 -040076static struct acpi_nfit_desc *to_acpi_nfit_desc(
77 struct nvdimm_bus_descriptor *nd_desc)
78{
79 return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
80}
81
82static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
83{
84 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
85
86 /*
87 * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
88 * acpi_device.
89 */
90 if (!nd_desc->provider_name
91 || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
92 return NULL;
93
94 return to_acpi_device(acpi_desc->dev);
95}
96
Dan Williams11294d62016-09-21 09:21:26 -070097static int xlat_status(void *buf, unsigned int cmd, u32 status)
Dan Williamsaef25332016-02-12 17:01:11 -080098{
Dan Williamsd4f32362016-03-03 16:08:54 -080099 struct nd_cmd_clear_error *clear_err;
Dan Williamsaef25332016-02-12 17:01:11 -0800100 struct nd_cmd_ars_status *ars_status;
Dan Williamsaef25332016-02-12 17:01:11 -0800101 u16 flags;
102
103 switch (cmd) {
104 case ND_CMD_ARS_CAP:
Dan Williams11294d62016-09-21 09:21:26 -0700105 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800106 return -ENOTTY;
107
108 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700109 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800110 return -EIO;
111
112 /* No supported scan types for this range */
113 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
Dan Williams11294d62016-09-21 09:21:26 -0700114 if ((status >> 16 & flags) == 0)
Dan Williamsaef25332016-02-12 17:01:11 -0800115 return -ENOTTY;
116 break;
117 case ND_CMD_ARS_START:
Dan Williamsaef25332016-02-12 17:01:11 -0800118 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700119 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800120 return -EBUSY;
121
122 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700123 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800124 return -EIO;
125 break;
126 case ND_CMD_ARS_STATUS:
127 ars_status = buf;
128 /* Command failed */
Dan Williams11294d62016-09-21 09:21:26 -0700129 if (status & 0xffff)
Dan Williamsaef25332016-02-12 17:01:11 -0800130 return -EIO;
131 /* Check extended status (Upper two bytes) */
Dan Williams11294d62016-09-21 09:21:26 -0700132 if (status == NFIT_ARS_STATUS_DONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800133 return 0;
134
135 /* ARS is in progress */
Dan Williams11294d62016-09-21 09:21:26 -0700136 if (status == NFIT_ARS_STATUS_BUSY)
Dan Williamsaef25332016-02-12 17:01:11 -0800137 return -EBUSY;
138
139 /* No ARS performed for the current boot */
Dan Williams11294d62016-09-21 09:21:26 -0700140 if (status == NFIT_ARS_STATUS_NONE)
Dan Williamsaef25332016-02-12 17:01:11 -0800141 return -EAGAIN;
142
143 /*
144 * ARS interrupted, either we overflowed or some other
145 * agent wants the scan to stop. If we didn't overflow
146 * then just continue with the returned results.
147 */
Dan Williams11294d62016-09-21 09:21:26 -0700148 if (status == NFIT_ARS_STATUS_INTR) {
Dan Williamsaef25332016-02-12 17:01:11 -0800149 if (ars_status->flags & NFIT_ARS_F_OVERFLOW)
150 return -ENOSPC;
151 return 0;
152 }
153
154 /* Unknown status */
Dan Williams11294d62016-09-21 09:21:26 -0700155 if (status >> 16)
Dan Williamsaef25332016-02-12 17:01:11 -0800156 return -EIO;
157 break;
Dan Williamsd4f32362016-03-03 16:08:54 -0800158 case ND_CMD_CLEAR_ERROR:
159 clear_err = buf;
Dan Williams11294d62016-09-21 09:21:26 -0700160 if (status & 0xffff)
Dan Williamsd4f32362016-03-03 16:08:54 -0800161 return -EIO;
162 if (!clear_err->cleared)
163 return -EIO;
164 if (clear_err->length > clear_err->cleared)
165 return clear_err->cleared;
166 break;
Dan Williamsaef25332016-02-12 17:01:11 -0800167 default:
168 break;
169 }
170
Dan Williams11294d62016-09-21 09:21:26 -0700171 /* all other non-zero status results in an error */
172 if (status)
173 return -EIO;
Dan Williamsaef25332016-02-12 17:01:11 -0800174 return 0;
175}
176
Dan Williamsb94d5232015-05-19 22:54:31 -0400177static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
178 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
Dan Williamsaef25332016-02-12 17:01:11 -0800179 unsigned int buf_len, int *cmd_rc)
Dan Williamsb94d5232015-05-19 22:54:31 -0400180{
Dan Williams62232e452015-06-08 14:27:06 -0400181 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
Dan Williams62232e452015-06-08 14:27:06 -0400182 union acpi_object in_obj, in_buf, *out_obj;
Dan Williams31eca762016-04-28 16:23:43 -0700183 const struct nd_cmd_desc *desc = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400184 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -0700185 struct nd_cmd_pkg *call_pkg = NULL;
Dan Williams62232e452015-06-08 14:27:06 -0400186 const char *cmd_name, *dimm_name;
Dan Williams31eca762016-04-28 16:23:43 -0700187 unsigned long cmd_mask, dsm_mask;
Dan Williams11294d62016-09-21 09:21:26 -0700188 u32 offset, fw_status = 0;
Dan Williams62232e452015-06-08 14:27:06 -0400189 acpi_handle handle;
Dan Williams31eca762016-04-28 16:23:43 -0700190 unsigned int func;
Dan Williams62232e452015-06-08 14:27:06 -0400191 const u8 *uuid;
Dan Williams62232e452015-06-08 14:27:06 -0400192 int rc, i;
193
Dan Williams31eca762016-04-28 16:23:43 -0700194 func = cmd;
195 if (cmd == ND_CMD_CALL) {
196 call_pkg = buf;
197 func = call_pkg->nd_command;
198 }
199
Dan Williams62232e452015-06-08 14:27:06 -0400200 if (nvdimm) {
201 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
202 struct acpi_device *adev = nfit_mem->adev;
203
204 if (!adev)
205 return -ENOTTY;
Dan Williams31eca762016-04-28 16:23:43 -0700206 if (call_pkg && nfit_mem->family != call_pkg->nd_family)
207 return -ENOTTY;
208
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400209 dimm_name = nvdimm_name(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400210 cmd_name = nvdimm_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700211 cmd_mask = nvdimm_cmd_mask(nvdimm);
Dan Williams62232e452015-06-08 14:27:06 -0400212 dsm_mask = nfit_mem->dsm_mask;
213 desc = nd_cmd_dimm_desc(cmd);
Dan Williams31eca762016-04-28 16:23:43 -0700214 uuid = to_nfit_uuid(nfit_mem->family);
Dan Williams62232e452015-06-08 14:27:06 -0400215 handle = adev->handle;
216 } else {
217 struct acpi_device *adev = to_acpi_dev(acpi_desc);
218
219 cmd_name = nvdimm_bus_cmd_name(cmd);
Dan Williamse3654ec2016-04-28 16:17:07 -0700220 cmd_mask = nd_desc->cmd_mask;
Dan Williams31eca762016-04-28 16:23:43 -0700221 dsm_mask = cmd_mask;
Dan Williams62232e452015-06-08 14:27:06 -0400222 desc = nd_cmd_bus_desc(cmd);
223 uuid = to_nfit_uuid(NFIT_DEV_BUS);
224 handle = adev->handle;
225 dimm_name = "bus";
226 }
227
228 if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
229 return -ENOTTY;
230
Dan Williams31eca762016-04-28 16:23:43 -0700231 if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask))
Dan Williams62232e452015-06-08 14:27:06 -0400232 return -ENOTTY;
233
234 in_obj.type = ACPI_TYPE_PACKAGE;
235 in_obj.package.count = 1;
236 in_obj.package.elements = &in_buf;
237 in_buf.type = ACPI_TYPE_BUFFER;
238 in_buf.buffer.pointer = buf;
239 in_buf.buffer.length = 0;
240
241 /* libnvdimm has already validated the input envelope */
242 for (i = 0; i < desc->in_num; i++)
243 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
244 i, buf);
245
Dan Williams31eca762016-04-28 16:23:43 -0700246 if (call_pkg) {
247 /* skip over package wrapper */
248 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
249 in_buf.buffer.length = call_pkg->nd_size_in;
Dan Williams62232e452015-06-08 14:27:06 -0400250 }
251
Dan Williams31eca762016-04-28 16:23:43 -0700252 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
253 dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
254 __func__, dimm_name, cmd, func,
255 in_buf.buffer.length);
256 print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4,
257 in_buf.buffer.pointer,
258 min_t(u32, 256, in_buf.buffer.length), true);
259 }
260
261 out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj);
Dan Williams62232e452015-06-08 14:27:06 -0400262 if (!out_obj) {
263 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
264 cmd_name);
265 return -EINVAL;
266 }
267
Dan Williams31eca762016-04-28 16:23:43 -0700268 if (call_pkg) {
269 call_pkg->nd_fw_size = out_obj->buffer.length;
270 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
271 out_obj->buffer.pointer,
272 min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
273
274 ACPI_FREE(out_obj);
275 /*
276 * Need to support FW function w/o known size in advance.
277 * Caller can determine required size based upon nd_fw_size.
278 * If we return an error (like elsewhere) then caller wouldn't
279 * be able to rely upon data returned to make calculation.
280 */
281 return 0;
282 }
283
Dan Williams62232e452015-06-08 14:27:06 -0400284 if (out_obj->package.type != ACPI_TYPE_BUFFER) {
285 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
286 __func__, dimm_name, cmd_name, out_obj->type);
287 rc = -EINVAL;
288 goto out;
289 }
290
291 if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
292 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
293 dimm_name, cmd_name, out_obj->buffer.length);
294 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
295 4, out_obj->buffer.pointer, min_t(u32, 128,
296 out_obj->buffer.length), true);
297 }
298
299 for (i = 0, offset = 0; i < desc->out_num; i++) {
300 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
301 (u32 *) out_obj->buffer.pointer);
302
303 if (offset + out_size > out_obj->buffer.length) {
304 dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
305 __func__, dimm_name, cmd_name, i);
306 break;
307 }
308
309 if (in_buf.buffer.length + offset + out_size > buf_len) {
310 dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
311 __func__, dimm_name, cmd_name, i);
312 rc = -ENXIO;
313 goto out;
314 }
315 memcpy(buf + in_buf.buffer.length + offset,
316 out_obj->buffer.pointer + offset, out_size);
317 offset += out_size;
318 }
Dan Williams11294d62016-09-21 09:21:26 -0700319
320 /*
321 * Set fw_status for all the commands with a known format to be
322 * later interpreted by xlat_status().
323 */
324 if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
325 || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
326 fw_status = *(u32 *) out_obj->buffer.pointer;
327
Dan Williams62232e452015-06-08 14:27:06 -0400328 if (offset + in_buf.buffer.length < buf_len) {
329 if (i >= 1) {
330 /*
331 * status valid, return the number of bytes left
332 * unfilled in the output buffer
333 */
334 rc = buf_len - offset - in_buf.buffer.length;
Dan Williamsaef25332016-02-12 17:01:11 -0800335 if (cmd_rc)
Dan Williams11294d62016-09-21 09:21:26 -0700336 *cmd_rc = xlat_status(buf, cmd, fw_status);
Dan Williams62232e452015-06-08 14:27:06 -0400337 } else {
338 dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
339 __func__, dimm_name, cmd_name, buf_len,
340 offset);
341 rc = -ENXIO;
342 }
Dan Williams2eea6582016-05-02 09:11:53 -0700343 } else {
Dan Williams62232e452015-06-08 14:27:06 -0400344 rc = 0;
Dan Williams2eea6582016-05-02 09:11:53 -0700345 if (cmd_rc)
Dan Williams11294d62016-09-21 09:21:26 -0700346 *cmd_rc = xlat_status(buf, cmd, fw_status);
Dan Williams2eea6582016-05-02 09:11:53 -0700347 }
Dan Williams62232e452015-06-08 14:27:06 -0400348
349 out:
350 ACPI_FREE(out_obj);
351
352 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -0400353}
354
355static const char *spa_type_name(u16 type)
356{
357 static const char *to_name[] = {
358 [NFIT_SPA_VOLATILE] = "volatile",
359 [NFIT_SPA_PM] = "pmem",
360 [NFIT_SPA_DCR] = "dimm-control-region",
361 [NFIT_SPA_BDW] = "block-data-window",
362 [NFIT_SPA_VDISK] = "volatile-disk",
363 [NFIT_SPA_VCD] = "volatile-cd",
364 [NFIT_SPA_PDISK] = "persistent-disk",
365 [NFIT_SPA_PCD] = "persistent-cd",
366
367 };
368
369 if (type > NFIT_SPA_PCD)
370 return "unknown";
371
372 return to_name[type];
373}
374
Vishal Verma6839a6d2016-07-23 21:51:21 -0700375int nfit_spa_type(struct acpi_nfit_system_address *spa)
Dan Williamsb94d5232015-05-19 22:54:31 -0400376{
377 int i;
378
379 for (i = 0; i < NFIT_UUID_MAX; i++)
380 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
381 return i;
382 return -1;
383}
384
385static bool add_spa(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600386 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400387 struct acpi_nfit_system_address *spa)
388{
389 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600390 struct nfit_spa *nfit_spa;
Dan Williamsb94d5232015-05-19 22:54:31 -0400391
Dan Williams31932042016-07-14 17:22:48 -0700392 if (spa->header.length != sizeof(*spa))
393 return false;
394
Vishal Verma20985162015-10-27 16:58:27 -0600395 list_for_each_entry(nfit_spa, &prev->spas, list) {
Dan Williams31932042016-07-14 17:22:48 -0700396 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600397 list_move_tail(&nfit_spa->list, &acpi_desc->spas);
398 return true;
399 }
400 }
401
Dan Williams31932042016-07-14 17:22:48 -0700402 nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
403 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400404 if (!nfit_spa)
405 return false;
406 INIT_LIST_HEAD(&nfit_spa->list);
Dan Williams31932042016-07-14 17:22:48 -0700407 memcpy(nfit_spa->spa, spa, sizeof(*spa));
Dan Williamsb94d5232015-05-19 22:54:31 -0400408 list_add_tail(&nfit_spa->list, &acpi_desc->spas);
409 dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
410 spa->range_index,
411 spa_type_name(nfit_spa_type(spa)));
412 return true;
413}
414
415static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600416 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400417 struct acpi_nfit_memory_map *memdev)
418{
419 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600420 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400421
Dan Williams31932042016-07-14 17:22:48 -0700422 if (memdev->header.length != sizeof(*memdev))
423 return false;
424
Vishal Verma20985162015-10-27 16:58:27 -0600425 list_for_each_entry(nfit_memdev, &prev->memdevs, list)
Dan Williams31932042016-07-14 17:22:48 -0700426 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600427 list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
428 return true;
429 }
430
Dan Williams31932042016-07-14 17:22:48 -0700431 nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
432 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400433 if (!nfit_memdev)
434 return false;
435 INIT_LIST_HEAD(&nfit_memdev->list);
Dan Williams31932042016-07-14 17:22:48 -0700436 memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
Dan Williamsb94d5232015-05-19 22:54:31 -0400437 list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
438 dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
439 __func__, memdev->device_handle, memdev->range_index,
440 memdev->region_index);
441 return true;
442}
443
Dan Williams31932042016-07-14 17:22:48 -0700444/*
445 * An implementation may provide a truncated control region if no block windows
446 * are defined.
447 */
448static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
449{
450 if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
451 window_size))
452 return 0;
453 if (dcr->windows)
454 return sizeof(*dcr);
455 return offsetof(struct acpi_nfit_control_region, window_size);
456}
457
Dan Williamsb94d5232015-05-19 22:54:31 -0400458static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600459 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400460 struct acpi_nfit_control_region *dcr)
461{
462 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600463 struct nfit_dcr *nfit_dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400464
Dan Williams31932042016-07-14 17:22:48 -0700465 if (!sizeof_dcr(dcr))
466 return false;
467
Vishal Verma20985162015-10-27 16:58:27 -0600468 list_for_each_entry(nfit_dcr, &prev->dcrs, list)
Dan Williams31932042016-07-14 17:22:48 -0700469 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600470 list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
471 return true;
472 }
473
Dan Williams31932042016-07-14 17:22:48 -0700474 nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
475 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400476 if (!nfit_dcr)
477 return false;
478 INIT_LIST_HEAD(&nfit_dcr->list);
Dan Williams31932042016-07-14 17:22:48 -0700479 memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
Dan Williamsb94d5232015-05-19 22:54:31 -0400480 list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
481 dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
482 dcr->region_index, dcr->windows);
483 return true;
484}
485
486static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600487 struct nfit_table_prev *prev,
Dan Williamsb94d5232015-05-19 22:54:31 -0400488 struct acpi_nfit_data_region *bdw)
489{
490 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600491 struct nfit_bdw *nfit_bdw;
Dan Williamsb94d5232015-05-19 22:54:31 -0400492
Dan Williams31932042016-07-14 17:22:48 -0700493 if (bdw->header.length != sizeof(*bdw))
494 return false;
Vishal Verma20985162015-10-27 16:58:27 -0600495 list_for_each_entry(nfit_bdw, &prev->bdws, list)
Dan Williams31932042016-07-14 17:22:48 -0700496 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600497 list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
498 return true;
499 }
500
Dan Williams31932042016-07-14 17:22:48 -0700501 nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
502 GFP_KERNEL);
Dan Williamsb94d5232015-05-19 22:54:31 -0400503 if (!nfit_bdw)
504 return false;
505 INIT_LIST_HEAD(&nfit_bdw->list);
Dan Williams31932042016-07-14 17:22:48 -0700506 memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
Dan Williamsb94d5232015-05-19 22:54:31 -0400507 list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
508 dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
509 bdw->region_index, bdw->windows);
510 return true;
511}
512
Dan Williams31932042016-07-14 17:22:48 -0700513static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
514{
515 if (idt->header.length < sizeof(*idt))
516 return 0;
517 return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
518}
519
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400520static bool add_idt(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600521 struct nfit_table_prev *prev,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400522 struct acpi_nfit_interleave *idt)
523{
524 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600525 struct nfit_idt *nfit_idt;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400526
Dan Williams31932042016-07-14 17:22:48 -0700527 if (!sizeof_idt(idt))
528 return false;
529
530 list_for_each_entry(nfit_idt, &prev->idts, list) {
531 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
532 continue;
533
534 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600535 list_move_tail(&nfit_idt->list, &acpi_desc->idts);
536 return true;
537 }
Dan Williams31932042016-07-14 17:22:48 -0700538 }
Vishal Verma20985162015-10-27 16:58:27 -0600539
Dan Williams31932042016-07-14 17:22:48 -0700540 nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
541 GFP_KERNEL);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400542 if (!nfit_idt)
543 return false;
544 INIT_LIST_HEAD(&nfit_idt->list);
Dan Williams31932042016-07-14 17:22:48 -0700545 memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400546 list_add_tail(&nfit_idt->list, &acpi_desc->idts);
547 dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
548 idt->interleave_index, idt->line_count);
549 return true;
550}
551
Dan Williams31932042016-07-14 17:22:48 -0700552static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
553{
554 if (flush->header.length < sizeof(*flush))
555 return 0;
556 return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
557}
558
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600559static bool add_flush(struct acpi_nfit_desc *acpi_desc,
Vishal Verma20985162015-10-27 16:58:27 -0600560 struct nfit_table_prev *prev,
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600561 struct acpi_nfit_flush_address *flush)
562{
563 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -0600564 struct nfit_flush *nfit_flush;
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600565
Dan Williams31932042016-07-14 17:22:48 -0700566 if (!sizeof_flush(flush))
567 return false;
568
569 list_for_each_entry(nfit_flush, &prev->flushes, list) {
570 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
571 continue;
572
573 if (memcmp(nfit_flush->flush, flush,
574 sizeof_flush(flush)) == 0) {
Vishal Verma20985162015-10-27 16:58:27 -0600575 list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
576 return true;
577 }
Dan Williams31932042016-07-14 17:22:48 -0700578 }
Vishal Verma20985162015-10-27 16:58:27 -0600579
Dan Williams31932042016-07-14 17:22:48 -0700580 nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
581 + sizeof_flush(flush), GFP_KERNEL);
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600582 if (!nfit_flush)
583 return false;
584 INIT_LIST_HEAD(&nfit_flush->list);
Dan Williams31932042016-07-14 17:22:48 -0700585 memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600586 list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
587 dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
588 flush->device_handle, flush->hint_count);
589 return true;
590}
591
Vishal Verma20985162015-10-27 16:58:27 -0600592static void *add_table(struct acpi_nfit_desc *acpi_desc,
593 struct nfit_table_prev *prev, void *table, const void *end)
Dan Williamsb94d5232015-05-19 22:54:31 -0400594{
595 struct device *dev = acpi_desc->dev;
596 struct acpi_nfit_header *hdr;
597 void *err = ERR_PTR(-ENOMEM);
598
599 if (table >= end)
600 return NULL;
601
602 hdr = table;
Vishal Verma564d5012015-10-27 16:58:26 -0600603 if (!hdr->length) {
604 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
605 hdr->type);
606 return NULL;
607 }
608
Dan Williamsb94d5232015-05-19 22:54:31 -0400609 switch (hdr->type) {
610 case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600611 if (!add_spa(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400612 return err;
613 break;
614 case ACPI_NFIT_TYPE_MEMORY_MAP:
Vishal Verma20985162015-10-27 16:58:27 -0600615 if (!add_memdev(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400616 return err;
617 break;
618 case ACPI_NFIT_TYPE_CONTROL_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600619 if (!add_dcr(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400620 return err;
621 break;
622 case ACPI_NFIT_TYPE_DATA_REGION:
Vishal Verma20985162015-10-27 16:58:27 -0600623 if (!add_bdw(acpi_desc, prev, table))
Dan Williamsb94d5232015-05-19 22:54:31 -0400624 return err;
625 break;
Dan Williamsb94d5232015-05-19 22:54:31 -0400626 case ACPI_NFIT_TYPE_INTERLEAVE:
Vishal Verma20985162015-10-27 16:58:27 -0600627 if (!add_idt(acpi_desc, prev, table))
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400628 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400629 break;
630 case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
Vishal Verma20985162015-10-27 16:58:27 -0600631 if (!add_flush(acpi_desc, prev, table))
Ross Zwislerc2ad2952015-07-10 11:06:13 -0600632 return err;
Dan Williamsb94d5232015-05-19 22:54:31 -0400633 break;
634 case ACPI_NFIT_TYPE_SMBIOS:
635 dev_dbg(dev, "%s: smbios\n", __func__);
636 break;
637 default:
638 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
639 break;
640 }
641
642 return table + hdr->length;
643}
644
645static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
646 struct nfit_mem *nfit_mem)
647{
648 u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
649 u16 dcr = nfit_mem->dcr->region_index;
650 struct nfit_spa *nfit_spa;
651
652 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
653 u16 range_index = nfit_spa->spa->range_index;
654 int type = nfit_spa_type(nfit_spa->spa);
655 struct nfit_memdev *nfit_memdev;
656
657 if (type != NFIT_SPA_BDW)
658 continue;
659
660 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
661 if (nfit_memdev->memdev->range_index != range_index)
662 continue;
663 if (nfit_memdev->memdev->device_handle != device_handle)
664 continue;
665 if (nfit_memdev->memdev->region_index != dcr)
666 continue;
667
668 nfit_mem->spa_bdw = nfit_spa->spa;
669 return;
670 }
671 }
672
673 dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
674 nfit_mem->spa_dcr->range_index);
675 nfit_mem->bdw = NULL;
676}
677
Dan Williams6697b2c2016-02-04 16:51:00 -0800678static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
Dan Williamsb94d5232015-05-19 22:54:31 -0400679 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
680{
681 u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400682 struct nfit_memdev *nfit_memdev;
Dan Williamsb94d5232015-05-19 22:54:31 -0400683 struct nfit_bdw *nfit_bdw;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400684 struct nfit_idt *nfit_idt;
685 u16 idt_idx, range_index;
Dan Williamsb94d5232015-05-19 22:54:31 -0400686
Dan Williamsb94d5232015-05-19 22:54:31 -0400687 list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
688 if (nfit_bdw->bdw->region_index != dcr)
689 continue;
690 nfit_mem->bdw = nfit_bdw->bdw;
691 break;
692 }
693
694 if (!nfit_mem->bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800695 return;
Dan Williamsb94d5232015-05-19 22:54:31 -0400696
697 nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400698
699 if (!nfit_mem->spa_bdw)
Dan Williams6697b2c2016-02-04 16:51:00 -0800700 return;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400701
702 range_index = nfit_mem->spa_bdw->range_index;
703 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
704 if (nfit_memdev->memdev->range_index != range_index ||
705 nfit_memdev->memdev->region_index != dcr)
706 continue;
707 nfit_mem->memdev_bdw = nfit_memdev->memdev;
708 idt_idx = nfit_memdev->memdev->interleave_index;
709 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
710 if (nfit_idt->idt->interleave_index != idt_idx)
711 continue;
712 nfit_mem->idt_bdw = nfit_idt->idt;
713 break;
714 }
715 break;
716 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400717}
718
719static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
720 struct acpi_nfit_system_address *spa)
721{
722 struct nfit_mem *nfit_mem, *found;
723 struct nfit_memdev *nfit_memdev;
724 int type = nfit_spa_type(spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400725
726 switch (type) {
727 case NFIT_SPA_DCR:
728 case NFIT_SPA_PM:
729 break;
730 default:
731 return 0;
732 }
733
734 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700735 struct nfit_flush *nfit_flush;
Dan Williams6697b2c2016-02-04 16:51:00 -0800736 struct nfit_dcr *nfit_dcr;
737 u32 device_handle;
738 u16 dcr;
Dan Williamsb94d5232015-05-19 22:54:31 -0400739
740 if (nfit_memdev->memdev->range_index != spa->range_index)
741 continue;
742 found = NULL;
743 dcr = nfit_memdev->memdev->region_index;
Dan Williams6697b2c2016-02-04 16:51:00 -0800744 device_handle = nfit_memdev->memdev->device_handle;
Dan Williamsb94d5232015-05-19 22:54:31 -0400745 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
Dan Williams6697b2c2016-02-04 16:51:00 -0800746 if (__to_nfit_memdev(nfit_mem)->device_handle
747 == device_handle) {
Dan Williamsb94d5232015-05-19 22:54:31 -0400748 found = nfit_mem;
749 break;
750 }
751
752 if (found)
753 nfit_mem = found;
754 else {
755 nfit_mem = devm_kzalloc(acpi_desc->dev,
756 sizeof(*nfit_mem), GFP_KERNEL);
757 if (!nfit_mem)
758 return -ENOMEM;
759 INIT_LIST_HEAD(&nfit_mem->list);
Dan Williams8cc6ddf2016-04-05 15:26:50 -0700760 nfit_mem->acpi_desc = acpi_desc;
Dan Williams6697b2c2016-02-04 16:51:00 -0800761 list_add(&nfit_mem->list, &acpi_desc->dimms);
762 }
763
764 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
765 if (nfit_dcr->dcr->region_index != dcr)
766 continue;
767 /*
768 * Record the control region for the dimm. For
769 * the ACPI 6.1 case, where there are separate
770 * control regions for the pmem vs blk
771 * interfaces, be sure to record the extended
772 * blk details.
773 */
774 if (!nfit_mem->dcr)
775 nfit_mem->dcr = nfit_dcr->dcr;
776 else if (nfit_mem->dcr->windows == 0
777 && nfit_dcr->dcr->windows)
778 nfit_mem->dcr = nfit_dcr->dcr;
779 break;
780 }
781
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700782 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -0700783 struct acpi_nfit_flush_address *flush;
784 u16 i;
785
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700786 if (nfit_flush->flush->device_handle != device_handle)
787 continue;
788 nfit_mem->nfit_flush = nfit_flush;
Dan Williamse5ae3b22016-06-07 17:00:04 -0700789 flush = nfit_flush->flush;
790 nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
791 flush->hint_count
792 * sizeof(struct resource), GFP_KERNEL);
793 if (!nfit_mem->flush_wpq)
794 return -ENOMEM;
795 for (i = 0; i < flush->hint_count; i++) {
796 struct resource *res = &nfit_mem->flush_wpq[i];
797
798 res->start = flush->hint_address[i];
799 res->end = res->start + 8 - 1;
800 }
Dan Williamsad9ac5e2016-05-26 11:38:08 -0700801 break;
802 }
803
Dan Williams6697b2c2016-02-04 16:51:00 -0800804 if (dcr && !nfit_mem->dcr) {
805 dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
806 spa->range_index, dcr);
807 return -ENODEV;
Dan Williamsb94d5232015-05-19 22:54:31 -0400808 }
809
810 if (type == NFIT_SPA_DCR) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400811 struct nfit_idt *nfit_idt;
812 u16 idt_idx;
813
Dan Williamsb94d5232015-05-19 22:54:31 -0400814 /* multiple dimms may share a SPA when interleaved */
815 nfit_mem->spa_dcr = spa;
816 nfit_mem->memdev_dcr = nfit_memdev->memdev;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400817 idt_idx = nfit_memdev->memdev->interleave_index;
818 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
819 if (nfit_idt->idt->interleave_index != idt_idx)
820 continue;
821 nfit_mem->idt_dcr = nfit_idt->idt;
822 break;
823 }
Dan Williams6697b2c2016-02-04 16:51:00 -0800824 nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
Dan Williamsb94d5232015-05-19 22:54:31 -0400825 } else {
826 /*
827 * A single dimm may belong to multiple SPA-PM
828 * ranges, record at least one in addition to
829 * any SPA-DCR range.
830 */
831 nfit_mem->memdev_pmem = nfit_memdev->memdev;
832 }
Dan Williamsb94d5232015-05-19 22:54:31 -0400833 }
834
835 return 0;
836}
837
838static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
839{
840 struct nfit_mem *a = container_of(_a, typeof(*a), list);
841 struct nfit_mem *b = container_of(_b, typeof(*b), list);
842 u32 handleA, handleB;
843
844 handleA = __to_nfit_memdev(a)->device_handle;
845 handleB = __to_nfit_memdev(b)->device_handle;
846 if (handleA < handleB)
847 return -1;
848 else if (handleA > handleB)
849 return 1;
850 return 0;
851}
852
853static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
854{
855 struct nfit_spa *nfit_spa;
856
857 /*
858 * For each SPA-DCR or SPA-PMEM address range find its
859 * corresponding MEMDEV(s). From each MEMDEV find the
860 * corresponding DCR. Then, if we're operating on a SPA-DCR,
861 * try to find a SPA-BDW and a corresponding BDW that references
862 * the DCR. Throw it all into an nfit_mem object. Note, that
863 * BDWs are optional.
864 */
865 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
866 int rc;
867
868 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
869 if (rc)
870 return rc;
871 }
872
873 list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
874
875 return 0;
876}
877
Dan Williams45def222015-04-26 19:26:48 -0400878static ssize_t revision_show(struct device *dev,
879 struct device_attribute *attr, char *buf)
880{
881 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
882 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
883 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
884
Linda Knippers6b577c92015-11-20 19:05:49 -0500885 return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
Dan Williams45def222015-04-26 19:26:48 -0400886}
887static DEVICE_ATTR_RO(revision);
888
Vishal Verma37b137f2016-07-23 21:51:42 -0700889/*
890 * This shows the number of full Address Range Scrubs that have been
891 * completed since driver load time. Userspace can wait on this using
892 * select/poll etc. A '+' at the end indicates an ARS is in progress
893 */
894static ssize_t scrub_show(struct device *dev,
895 struct device_attribute *attr, char *buf)
896{
897 struct nvdimm_bus_descriptor *nd_desc;
898 ssize_t rc = -ENXIO;
899
900 device_lock(dev);
901 nd_desc = dev_get_drvdata(dev);
902 if (nd_desc) {
903 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
904
905 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
906 (work_busy(&acpi_desc->work)) ? "+\n" : "\n");
907 }
908 device_unlock(dev);
909 return rc;
910}
911
Vishal Verma37b137f2016-07-23 21:51:42 -0700912static ssize_t scrub_store(struct device *dev,
913 struct device_attribute *attr, const char *buf, size_t size)
914{
915 struct nvdimm_bus_descriptor *nd_desc;
916 ssize_t rc;
917 long val;
918
919 rc = kstrtol(buf, 0, &val);
920 if (rc)
921 return rc;
922 if (val != 1)
923 return -EINVAL;
924
925 device_lock(dev);
926 nd_desc = dev_get_drvdata(dev);
927 if (nd_desc) {
928 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
929
930 rc = acpi_nfit_ars_rescan(acpi_desc);
931 }
932 device_unlock(dev);
933 if (rc)
934 return rc;
935 return size;
936}
937static DEVICE_ATTR_RW(scrub);
938
939static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
940{
941 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
942 const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
943 | 1 << ND_CMD_ARS_STATUS;
944
945 return (nd_desc->cmd_mask & mask) == mask;
946}
947
948static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
949{
950 struct device *dev = container_of(kobj, struct device, kobj);
951 struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
952
953 if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
954 return 0;
955 return a->mode;
956}
957
Dan Williams45def222015-04-26 19:26:48 -0400958static struct attribute *acpi_nfit_attributes[] = {
959 &dev_attr_revision.attr,
Vishal Verma37b137f2016-07-23 21:51:42 -0700960 &dev_attr_scrub.attr,
Dan Williams45def222015-04-26 19:26:48 -0400961 NULL,
962};
963
964static struct attribute_group acpi_nfit_attribute_group = {
965 .name = "nfit",
966 .attrs = acpi_nfit_attributes,
Vishal Verma37b137f2016-07-23 21:51:42 -0700967 .is_visible = nfit_visible,
Dan Williams45def222015-04-26 19:26:48 -0400968};
969
Dan Williamsa61fe6f2016-02-19 12:29:32 -0800970static const struct attribute_group *acpi_nfit_attribute_groups[] = {
Dan Williams45def222015-04-26 19:26:48 -0400971 &nvdimm_bus_attribute_group,
972 &acpi_nfit_attribute_group,
973 NULL,
974};
975
Dan Williamse6dfb2d2015-04-25 03:56:17 -0400976static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
977{
978 struct nvdimm *nvdimm = to_nvdimm(dev);
979 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
980
981 return __to_nfit_memdev(nfit_mem);
982}
983
984static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
985{
986 struct nvdimm *nvdimm = to_nvdimm(dev);
987 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
988
989 return nfit_mem->dcr;
990}
991
992static ssize_t handle_show(struct device *dev,
993 struct device_attribute *attr, char *buf)
994{
995 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
996
997 return sprintf(buf, "%#x\n", memdev->device_handle);
998}
999static DEVICE_ATTR_RO(handle);
1000
1001static ssize_t phys_id_show(struct device *dev,
1002 struct device_attribute *attr, char *buf)
1003{
1004 struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1005
1006 return sprintf(buf, "%#x\n", memdev->physical_id);
1007}
1008static DEVICE_ATTR_RO(phys_id);
1009
1010static ssize_t vendor_show(struct device *dev,
1011 struct device_attribute *attr, char *buf)
1012{
1013 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1014
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001015 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001016}
1017static DEVICE_ATTR_RO(vendor);
1018
1019static ssize_t rev_id_show(struct device *dev,
1020 struct device_attribute *attr, char *buf)
1021{
1022 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1023
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001024 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001025}
1026static DEVICE_ATTR_RO(rev_id);
1027
1028static ssize_t device_show(struct device *dev,
1029 struct device_attribute *attr, char *buf)
1030{
1031 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1032
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001033 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001034}
1035static DEVICE_ATTR_RO(device);
1036
Dan Williams6ca72082016-04-29 10:33:23 -07001037static ssize_t subsystem_vendor_show(struct device *dev,
1038 struct device_attribute *attr, char *buf)
1039{
1040 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1041
1042 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1043}
1044static DEVICE_ATTR_RO(subsystem_vendor);
1045
1046static ssize_t subsystem_rev_id_show(struct device *dev,
1047 struct device_attribute *attr, char *buf)
1048{
1049 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1050
1051 return sprintf(buf, "0x%04x\n",
1052 be16_to_cpu(dcr->subsystem_revision_id));
1053}
1054static DEVICE_ATTR_RO(subsystem_rev_id);
1055
1056static ssize_t subsystem_device_show(struct device *dev,
1057 struct device_attribute *attr, char *buf)
1058{
1059 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1060
1061 return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1062}
1063static DEVICE_ATTR_RO(subsystem_device);
1064
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001065static int num_nvdimm_formats(struct nvdimm *nvdimm)
1066{
1067 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1068 int formats = 0;
1069
1070 if (nfit_mem->memdev_pmem)
1071 formats++;
1072 if (nfit_mem->memdev_bdw)
1073 formats++;
1074 return formats;
1075}
1076
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001077static ssize_t format_show(struct device *dev,
1078 struct device_attribute *attr, char *buf)
1079{
1080 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1081
Dan Williams1bcbf422016-06-29 11:19:32 -07001082 return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001083}
1084static DEVICE_ATTR_RO(format);
1085
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001086static ssize_t format1_show(struct device *dev,
1087 struct device_attribute *attr, char *buf)
1088{
1089 u32 handle;
1090 ssize_t rc = -ENXIO;
1091 struct nfit_mem *nfit_mem;
1092 struct nfit_memdev *nfit_memdev;
1093 struct acpi_nfit_desc *acpi_desc;
1094 struct nvdimm *nvdimm = to_nvdimm(dev);
1095 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1096
1097 nfit_mem = nvdimm_provider_data(nvdimm);
1098 acpi_desc = nfit_mem->acpi_desc;
1099 handle = to_nfit_memdev(dev)->device_handle;
1100
1101 /* assumes DIMMs have at most 2 published interface codes */
1102 mutex_lock(&acpi_desc->init_mutex);
1103 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1104 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1105 struct nfit_dcr *nfit_dcr;
1106
1107 if (memdev->device_handle != handle)
1108 continue;
1109
1110 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1111 if (nfit_dcr->dcr->region_index != memdev->region_index)
1112 continue;
1113 if (nfit_dcr->dcr->code == dcr->code)
1114 continue;
Dan Williams1bcbf422016-06-29 11:19:32 -07001115 rc = sprintf(buf, "0x%04x\n",
1116 le16_to_cpu(nfit_dcr->dcr->code));
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001117 break;
1118 }
1119 if (rc != ENXIO)
1120 break;
1121 }
1122 mutex_unlock(&acpi_desc->init_mutex);
1123 return rc;
1124}
1125static DEVICE_ATTR_RO(format1);
1126
1127static ssize_t formats_show(struct device *dev,
1128 struct device_attribute *attr, char *buf)
1129{
1130 struct nvdimm *nvdimm = to_nvdimm(dev);
1131
1132 return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1133}
1134static DEVICE_ATTR_RO(formats);
1135
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001136static ssize_t serial_show(struct device *dev,
1137 struct device_attribute *attr, char *buf)
1138{
1139 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1140
Toshi Kani5ad9a7f2016-04-25 15:34:58 -06001141 return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001142}
1143static DEVICE_ATTR_RO(serial);
1144
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001145static ssize_t family_show(struct device *dev,
1146 struct device_attribute *attr, char *buf)
1147{
1148 struct nvdimm *nvdimm = to_nvdimm(dev);
1149 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1150
1151 if (nfit_mem->family < 0)
1152 return -ENXIO;
1153 return sprintf(buf, "%d\n", nfit_mem->family);
1154}
1155static DEVICE_ATTR_RO(family);
1156
1157static ssize_t dsm_mask_show(struct device *dev,
1158 struct device_attribute *attr, char *buf)
1159{
1160 struct nvdimm *nvdimm = to_nvdimm(dev);
1161 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1162
1163 if (nfit_mem->family < 0)
1164 return -ENXIO;
1165 return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1166}
1167static DEVICE_ATTR_RO(dsm_mask);
1168
Dan Williams58138822015-06-23 20:08:34 -04001169static ssize_t flags_show(struct device *dev,
1170 struct device_attribute *attr, char *buf)
1171{
1172 u16 flags = to_nfit_memdev(dev)->flags;
1173
1174 return sprintf(buf, "%s%s%s%s%s\n",
Toshi Kani402bae52015-08-26 10:20:23 -06001175 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1176 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1177 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001178 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
Toshi Kani402bae52015-08-26 10:20:23 -06001179 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "");
Dan Williams58138822015-06-23 20:08:34 -04001180}
1181static DEVICE_ATTR_RO(flags);
1182
Toshi Kani38a879b2016-04-25 15:34:59 -06001183static ssize_t id_show(struct device *dev,
1184 struct device_attribute *attr, char *buf)
1185{
1186 struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1187
1188 if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1189 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1190 be16_to_cpu(dcr->vendor_id),
1191 dcr->manufacturing_location,
1192 be16_to_cpu(dcr->manufacturing_date),
1193 be32_to_cpu(dcr->serial_number));
1194 else
1195 return sprintf(buf, "%04x-%08x\n",
1196 be16_to_cpu(dcr->vendor_id),
1197 be32_to_cpu(dcr->serial_number));
1198}
1199static DEVICE_ATTR_RO(id);
1200
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001201static struct attribute *acpi_nfit_dimm_attributes[] = {
1202 &dev_attr_handle.attr,
1203 &dev_attr_phys_id.attr,
1204 &dev_attr_vendor.attr,
1205 &dev_attr_device.attr,
Dan Williams6ca72082016-04-29 10:33:23 -07001206 &dev_attr_rev_id.attr,
1207 &dev_attr_subsystem_vendor.attr,
1208 &dev_attr_subsystem_device.attr,
1209 &dev_attr_subsystem_rev_id.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001210 &dev_attr_format.attr,
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001211 &dev_attr_formats.attr,
1212 &dev_attr_format1.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001213 &dev_attr_serial.attr,
Dan Williams58138822015-06-23 20:08:34 -04001214 &dev_attr_flags.attr,
Toshi Kani38a879b2016-04-25 15:34:59 -06001215 &dev_attr_id.attr,
Dan Williamsa94e3fb2016-04-28 18:18:05 -07001216 &dev_attr_family.attr,
1217 &dev_attr_dsm_mask.attr,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001218 NULL,
1219};
1220
1221static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1222 struct attribute *a, int n)
1223{
1224 struct device *dev = container_of(kobj, struct device, kobj);
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001225 struct nvdimm *nvdimm = to_nvdimm(dev);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001226
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001227 if (!to_nfit_dcr(dev))
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001228 return 0;
Dan Williams8cc6ddf2016-04-05 15:26:50 -07001229 if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1230 return 0;
1231 return a->mode;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001232}
1233
1234static struct attribute_group acpi_nfit_dimm_attribute_group = {
1235 .name = "nfit",
1236 .attrs = acpi_nfit_dimm_attributes,
1237 .is_visible = acpi_nfit_dimm_attr_visible,
1238};
1239
1240static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
Dan Williams62232e452015-06-08 14:27:06 -04001241 &nvdimm_attribute_group,
Dan Williams4d88a972015-05-31 14:41:48 -04001242 &nd_device_attribute_group,
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001243 &acpi_nfit_dimm_attribute_group,
1244 NULL,
1245};
1246
1247static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1248 u32 device_handle)
1249{
1250 struct nfit_mem *nfit_mem;
1251
1252 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1253 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1254 return nfit_mem->nvdimm;
1255
1256 return NULL;
1257}
1258
Dan Williams62232e452015-06-08 14:27:06 -04001259static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1260 struct nfit_mem *nfit_mem, u32 device_handle)
1261{
1262 struct acpi_device *adev, *adev_dimm;
1263 struct device *dev = acpi_desc->dev;
Dan Williams31eca762016-04-28 16:23:43 -07001264 unsigned long dsm_mask;
1265 const u8 *uuid;
Linda Knippers60e95f432015-07-22 16:17:22 -04001266 int i;
Dan Williams62232e452015-06-08 14:27:06 -04001267
Dan Williamse3654ec2016-04-28 16:17:07 -07001268 /* nfit test assumes 1:1 relationship between commands and dsms */
1269 nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
Dan Williams31eca762016-04-28 16:23:43 -07001270 nfit_mem->family = NVDIMM_FAMILY_INTEL;
Dan Williams62232e452015-06-08 14:27:06 -04001271 adev = to_acpi_dev(acpi_desc);
1272 if (!adev)
1273 return 0;
1274
1275 adev_dimm = acpi_find_child_device(adev, device_handle, false);
1276 nfit_mem->adev = adev_dimm;
1277 if (!adev_dimm) {
1278 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1279 device_handle);
Dan Williams4d88a972015-05-31 14:41:48 -04001280 return force_enable_dimms ? 0 : -ENODEV;
Dan Williams62232e452015-06-08 14:27:06 -04001281 }
1282
Dan Williams31eca762016-04-28 16:23:43 -07001283 /*
stuart hayese02fb722016-05-26 11:38:41 -05001284 * Until standardization materializes we need to consider 4
Dan Williamsa7225592016-07-19 12:32:39 -07001285 * different command sets. Note, that checking for function0 (bit0)
1286 * tells us if any commands are reachable through this uuid.
Dan Williams31eca762016-04-28 16:23:43 -07001287 */
stuart hayese02fb722016-05-26 11:38:41 -05001288 for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
Dan Williamsa7225592016-07-19 12:32:39 -07001289 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
Dan Williams31eca762016-04-28 16:23:43 -07001290 break;
1291
1292 /* limit the supported commands to those that are publicly documented */
1293 nfit_mem->family = i;
Dan Williams87554092016-04-28 18:01:20 -07001294 if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
Dan Williams31eca762016-04-28 16:23:43 -07001295 dsm_mask = 0x3fe;
Dan Williams87554092016-04-28 18:01:20 -07001296 if (disable_vendor_specific)
1297 dsm_mask &= ~(1 << ND_CMD_VENDOR);
stuart hayese02fb722016-05-26 11:38:41 -05001298 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
Dan Williams31eca762016-04-28 16:23:43 -07001299 dsm_mask = 0x1c3c76;
stuart hayese02fb722016-05-26 11:38:41 -05001300 } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
Dan Williams31eca762016-04-28 16:23:43 -07001301 dsm_mask = 0x1fe;
Dan Williams87554092016-04-28 18:01:20 -07001302 if (disable_vendor_specific)
1303 dsm_mask &= ~(1 << 8);
stuart hayese02fb722016-05-26 11:38:41 -05001304 } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1305 dsm_mask = 0xffffffff;
Dan Williams87554092016-04-28 18:01:20 -07001306 } else {
Dan Williamsa7225592016-07-19 12:32:39 -07001307 dev_dbg(dev, "unknown dimm command family\n");
Dan Williams31eca762016-04-28 16:23:43 -07001308 nfit_mem->family = -1;
Dan Williamsa7225592016-07-19 12:32:39 -07001309 /* DSMs are optional, continue loading the driver... */
1310 return 0;
Dan Williams31eca762016-04-28 16:23:43 -07001311 }
1312
1313 uuid = to_nfit_uuid(nfit_mem->family);
1314 for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
Dan Williams62232e452015-06-08 14:27:06 -04001315 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
1316 set_bit(i, &nfit_mem->dsm_mask);
1317
Linda Knippers60e95f432015-07-22 16:17:22 -04001318 return 0;
Dan Williams62232e452015-06-08 14:27:06 -04001319}
1320
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001321static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1322{
1323 struct nfit_mem *nfit_mem;
Dan Williams4d88a972015-05-31 14:41:48 -04001324 int dimm_count = 0;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001325
1326 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
Dan Williamse5ae3b22016-06-07 17:00:04 -07001327 struct acpi_nfit_flush_address *flush;
Dan Williams31eca762016-04-28 16:23:43 -07001328 unsigned long flags = 0, cmd_mask;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001329 struct nvdimm *nvdimm;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001330 u32 device_handle;
Dan Williams58138822015-06-23 20:08:34 -04001331 u16 mem_flags;
Dan Williams62232e452015-06-08 14:27:06 -04001332 int rc;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001333
1334 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1335 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1336 if (nvdimm) {
Vishal Verma20985162015-10-27 16:58:27 -06001337 dimm_count++;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001338 continue;
1339 }
1340
1341 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1342 flags |= NDD_ALIASING;
1343
Dan Williams58138822015-06-23 20:08:34 -04001344 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
Bob Mooreca321d12015-10-19 10:24:52 +08001345 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
Dan Williams58138822015-06-23 20:08:34 -04001346 flags |= NDD_UNARMED;
1347
Dan Williams62232e452015-06-08 14:27:06 -04001348 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1349 if (rc)
1350 continue;
1351
Dan Williamse3654ec2016-04-28 16:17:07 -07001352 /*
Dan Williams31eca762016-04-28 16:23:43 -07001353 * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1354 * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1355 * userspace interface.
Dan Williamse3654ec2016-04-28 16:17:07 -07001356 */
Dan Williams31eca762016-04-28 16:23:43 -07001357 cmd_mask = 1UL << ND_CMD_CALL;
1358 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1359 cmd_mask |= nfit_mem->dsm_mask;
1360
Dan Williamse5ae3b22016-06-07 17:00:04 -07001361 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1362 : NULL;
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001363 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
Dan Williams62232e452015-06-08 14:27:06 -04001364 acpi_nfit_dimm_attribute_groups,
Dan Williamse5ae3b22016-06-07 17:00:04 -07001365 flags, cmd_mask, flush ? flush->hint_count : 0,
1366 nfit_mem->flush_wpq);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001367 if (!nvdimm)
1368 return -ENOMEM;
1369
1370 nfit_mem->nvdimm = nvdimm;
Dan Williams4d88a972015-05-31 14:41:48 -04001371 dimm_count++;
Dan Williams58138822015-06-23 20:08:34 -04001372
1373 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1374 continue;
1375
Toshi Kani402bae52015-08-26 10:20:23 -06001376 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
Dan Williams58138822015-06-23 20:08:34 -04001377 nvdimm_name(nvdimm),
Toshi Kani402bae52015-08-26 10:20:23 -06001378 mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1379 mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1380 mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
Bob Mooreca321d12015-10-19 10:24:52 +08001381 mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
Dan Williams58138822015-06-23 20:08:34 -04001382
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001383 }
1384
Dan Williams4d88a972015-05-31 14:41:48 -04001385 return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
Dan Williamse6dfb2d2015-04-25 03:56:17 -04001386}
1387
Dan Williams62232e452015-06-08 14:27:06 -04001388static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1389{
1390 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1391 const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1392 struct acpi_device *adev;
1393 int i;
1394
Dan Williamse3654ec2016-04-28 16:17:07 -07001395 nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
Dan Williams62232e452015-06-08 14:27:06 -04001396 adev = to_acpi_dev(acpi_desc);
1397 if (!adev)
1398 return;
1399
Dan Williamsd4f32362016-03-03 16:08:54 -08001400 for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
Dan Williams62232e452015-06-08 14:27:06 -04001401 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
Dan Williamse3654ec2016-04-28 16:17:07 -07001402 set_bit(i, &nd_desc->cmd_mask);
Dan Williams62232e452015-06-08 14:27:06 -04001403}
1404
Dan Williams1f7df6f2015-06-09 20:13:14 -04001405static ssize_t range_index_show(struct device *dev,
1406 struct device_attribute *attr, char *buf)
1407{
1408 struct nd_region *nd_region = to_nd_region(dev);
1409 struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1410
1411 return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1412}
1413static DEVICE_ATTR_RO(range_index);
1414
1415static struct attribute *acpi_nfit_region_attributes[] = {
1416 &dev_attr_range_index.attr,
1417 NULL,
1418};
1419
1420static struct attribute_group acpi_nfit_region_attribute_group = {
1421 .name = "nfit",
1422 .attrs = acpi_nfit_region_attributes,
1423};
1424
1425static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1426 &nd_region_attribute_group,
1427 &nd_mapping_attribute_group,
Dan Williams3d880022015-05-31 15:02:11 -04001428 &nd_device_attribute_group,
Toshi Kani74ae66c2015-06-19 12:18:34 -06001429 &nd_numa_attribute_group,
Dan Williams1f7df6f2015-06-09 20:13:14 -04001430 &acpi_nfit_region_attribute_group,
1431 NULL,
1432};
1433
Dan Williamseaf96152015-05-01 13:11:27 -04001434/* enough info to uniquely specify an interleave set */
1435struct nfit_set_info {
1436 struct nfit_set_info_map {
1437 u64 region_offset;
1438 u32 serial_number;
1439 u32 pad;
1440 } mapping[0];
1441};
1442
1443static size_t sizeof_nfit_set_info(int num_mappings)
1444{
1445 return sizeof(struct nfit_set_info)
1446 + num_mappings * sizeof(struct nfit_set_info_map);
1447}
1448
1449static int cmp_map(const void *m0, const void *m1)
1450{
1451 const struct nfit_set_info_map *map0 = m0;
1452 const struct nfit_set_info_map *map1 = m1;
1453
1454 return memcmp(&map0->region_offset, &map1->region_offset,
1455 sizeof(u64));
1456}
1457
1458/* Retrieve the nth entry referencing this spa */
1459static struct acpi_nfit_memory_map *memdev_from_spa(
1460 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1461{
1462 struct nfit_memdev *nfit_memdev;
1463
1464 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1465 if (nfit_memdev->memdev->range_index == range_index)
1466 if (n-- == 0)
1467 return nfit_memdev->memdev;
1468 return NULL;
1469}
1470
1471static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1472 struct nd_region_desc *ndr_desc,
1473 struct acpi_nfit_system_address *spa)
1474{
1475 int i, spa_type = nfit_spa_type(spa);
1476 struct device *dev = acpi_desc->dev;
1477 struct nd_interleave_set *nd_set;
1478 u16 nr = ndr_desc->num_mappings;
1479 struct nfit_set_info *info;
1480
1481 if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1482 /* pass */;
1483 else
1484 return 0;
1485
1486 nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1487 if (!nd_set)
1488 return -ENOMEM;
1489
1490 info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1491 if (!info)
1492 return -ENOMEM;
1493 for (i = 0; i < nr; i++) {
1494 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
1495 struct nfit_set_info_map *map = &info->mapping[i];
1496 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1497 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1498 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1499 spa->range_index, i);
1500
1501 if (!memdev || !nfit_mem->dcr) {
1502 dev_err(dev, "%s: failed to find DCR\n", __func__);
1503 return -ENODEV;
1504 }
1505
1506 map->region_offset = memdev->region_offset;
1507 map->serial_number = nfit_mem->dcr->serial_number;
1508 }
1509
1510 sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1511 cmp_map, NULL);
1512 nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1513 ndr_desc->nd_set = nd_set;
1514 devm_kfree(dev, info);
1515
1516 return 0;
1517}
1518
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001519static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1520{
1521 struct acpi_nfit_interleave *idt = mmio->idt;
1522 u32 sub_line_offset, line_index, line_offset;
1523 u64 line_no, table_skip_count, table_offset;
1524
1525 line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1526 table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1527 line_offset = idt->line_offset[line_index]
1528 * mmio->line_size;
1529 table_offset = table_skip_count * mmio->table_size;
1530
1531 return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1532}
1533
Ross Zwislerde4a1962015-08-20 16:27:38 -06001534static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001535{
1536 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1537 u64 offset = nfit_blk->stat_offset + mmio->size * bw;
Ross Zwisler68202c92016-07-29 14:59:12 -06001538 const u32 STATUS_MASK = 0x80000037;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001539
1540 if (mmio->num_lines)
1541 offset = to_interleave_offset(offset, mmio);
1542
Ross Zwisler68202c92016-07-29 14:59:12 -06001543 return readl(mmio->addr.base + offset) & STATUS_MASK;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001544}
1545
1546static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1547 resource_size_t dpa, unsigned int len, unsigned int write)
1548{
1549 u64 cmd, offset;
1550 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1551
1552 enum {
1553 BCW_OFFSET_MASK = (1ULL << 48)-1,
1554 BCW_LEN_SHIFT = 48,
1555 BCW_LEN_MASK = (1ULL << 8) - 1,
1556 BCW_CMD_SHIFT = 56,
1557 };
1558
1559 cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1560 len = len >> L1_CACHE_SHIFT;
1561 cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1562 cmd |= ((u64) write) << BCW_CMD_SHIFT;
1563
1564 offset = nfit_blk->cmd_offset + mmio->size * bw;
1565 if (mmio->num_lines)
1566 offset = to_interleave_offset(offset, mmio);
1567
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001568 writeq(cmd, mmio->addr.base + offset);
Dan Williamsf284a4f2016-07-07 19:44:50 -07001569 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001570
Dan Williamsaef25332016-02-12 17:01:11 -08001571 if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001572 readq(mmio->addr.base + offset);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001573}
1574
1575static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1576 resource_size_t dpa, void *iobuf, size_t len, int rw,
1577 unsigned int lane)
1578{
1579 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1580 unsigned int copied = 0;
1581 u64 base_offset;
1582 int rc;
1583
1584 base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1585 + lane * mmio->size;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001586 write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1587 while (len) {
1588 unsigned int c;
1589 u64 offset;
1590
1591 if (mmio->num_lines) {
1592 u32 line_offset;
1593
1594 offset = to_interleave_offset(base_offset + copied,
1595 mmio);
1596 div_u64_rem(offset, mmio->line_size, &line_offset);
1597 c = min_t(size_t, len, mmio->line_size - line_offset);
1598 } else {
1599 offset = base_offset + nfit_blk->bdw_offset;
1600 c = len;
1601 }
1602
1603 if (rw)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001604 memcpy_to_pmem(mmio->addr.aperture + offset,
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001605 iobuf + copied, c);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001606 else {
Dan Williamsaef25332016-02-12 17:01:11 -08001607 if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001608 mmio_flush_range((void __force *)
1609 mmio->addr.aperture + offset, c);
1610
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001611 memcpy_from_pmem(iobuf + copied,
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001612 mmio->addr.aperture + offset, c);
1613 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001614
1615 copied += c;
1616 len -= c;
1617 }
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001618
1619 if (rw)
Dan Williamsf284a4f2016-07-07 19:44:50 -07001620 nvdimm_flush(nfit_blk->nd_region);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001621
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001622 rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1623 return rc;
1624}
1625
1626static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1627 resource_size_t dpa, void *iobuf, u64 len, int rw)
1628{
1629 struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1630 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1631 struct nd_region *nd_region = nfit_blk->nd_region;
1632 unsigned int lane, copied = 0;
1633 int rc = 0;
1634
1635 lane = nd_region_acquire_lane(nd_region);
1636 while (len) {
1637 u64 c = min(len, mmio->size);
1638
1639 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1640 iobuf + copied, c, rw, lane);
1641 if (rc)
1642 break;
1643
1644 copied += c;
1645 len -= c;
1646 }
1647 nd_region_release_lane(nd_region, lane);
1648
1649 return rc;
1650}
1651
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001652static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1653 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1654{
1655 if (idt) {
1656 mmio->num_lines = idt->line_count;
1657 mmio->line_size = idt->line_size;
1658 if (interleave_ways == 0)
1659 return -ENXIO;
1660 mmio->table_size = mmio->num_lines * interleave_ways
1661 * mmio->line_size;
1662 }
1663
1664 return 0;
1665}
1666
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001667static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1668 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1669{
1670 struct nd_cmd_dimm_flags flags;
1671 int rc;
1672
1673 memset(&flags, 0, sizeof(flags));
1674 rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
Dan Williamsaef25332016-02-12 17:01:11 -08001675 sizeof(flags), NULL);
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001676
1677 if (rc >= 0 && flags.status == 0)
1678 nfit_blk->dimm_flags = flags.flags;
1679 else if (rc == -ENOTTY) {
1680 /* fall back to a conservative default */
Dan Williamsaef25332016-02-12 17:01:11 -08001681 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001682 rc = 0;
1683 } else
1684 rc = -ENXIO;
1685
1686 return rc;
1687}
1688
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001689static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1690 struct device *dev)
1691{
1692 struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001693 struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1694 struct nfit_blk_mmio *mmio;
1695 struct nfit_blk *nfit_blk;
1696 struct nfit_mem *nfit_mem;
1697 struct nvdimm *nvdimm;
1698 int rc;
1699
1700 nvdimm = nd_blk_region_to_dimm(ndbr);
1701 nfit_mem = nvdimm_provider_data(nvdimm);
1702 if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1703 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1704 nfit_mem ? "" : " nfit_mem",
Dan Williams193ccca2015-06-30 16:09:39 -04001705 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1706 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001707 return -ENXIO;
1708 }
1709
1710 nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1711 if (!nfit_blk)
1712 return -ENOMEM;
1713 nd_blk_region_set_provider_data(ndbr, nfit_blk);
1714 nfit_blk->nd_region = to_nd_region(dev);
1715
1716 /* map block aperture memory */
1717 nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1718 mmio = &nfit_blk->mmio[BDW];
Dan Williams29b9aa02016-06-06 17:42:38 -07001719 mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
1720 nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001721 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001722 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1723 nvdimm_name(nvdimm));
1724 return -ENOMEM;
1725 }
1726 mmio->size = nfit_mem->bdw->size;
1727 mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1728 mmio->idt = nfit_mem->idt_bdw;
1729 mmio->spa = nfit_mem->spa_bdw;
1730 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1731 nfit_mem->memdev_bdw->interleave_ways);
1732 if (rc) {
1733 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1734 __func__, nvdimm_name(nvdimm));
1735 return rc;
1736 }
1737
1738 /* map block control memory */
1739 nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1740 nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1741 mmio = &nfit_blk->mmio[DCR];
Dan Williams29b9aa02016-06-06 17:42:38 -07001742 mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
1743 nfit_mem->spa_dcr->length);
Ross Zwisler67a3e8f2015-08-27 13:14:20 -06001744 if (!mmio->addr.base) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001745 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1746 nvdimm_name(nvdimm));
1747 return -ENOMEM;
1748 }
1749 mmio->size = nfit_mem->dcr->window_size;
1750 mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1751 mmio->idt = nfit_mem->idt_dcr;
1752 mmio->spa = nfit_mem->spa_dcr;
1753 rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1754 nfit_mem->memdev_dcr->interleave_ways);
1755 if (rc) {
1756 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1757 __func__, nvdimm_name(nvdimm));
1758 return rc;
1759 }
1760
Ross Zwislerf0f2c072015-07-10 11:06:14 -06001761 rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1762 if (rc < 0) {
1763 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1764 __func__, nvdimm_name(nvdimm));
1765 return rc;
1766 }
1767
Dan Williamsf284a4f2016-07-07 19:44:50 -07001768 if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
Ross Zwislerc2ad2952015-07-10 11:06:13 -06001769 dev_warn(dev, "unable to guarantee persistence of writes\n");
1770
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001771 if (mmio->line_size == 0)
1772 return 0;
1773
1774 if ((u32) nfit_blk->cmd_offset % mmio->line_size
1775 + 8 > mmio->line_size) {
1776 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1777 return -ENXIO;
1778 } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1779 + 8 > mmio->line_size) {
1780 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
1781 return -ENXIO;
1782 }
1783
1784 return 0;
1785}
1786
Dan Williamsaef25332016-02-12 17:01:11 -08001787static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08001788 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001789{
Dan Williamsaef25332016-02-12 17:01:11 -08001790 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001791 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Dan Williamsaef25332016-02-12 17:01:11 -08001792 int cmd_rc, rc;
1793
Dan Williams1cf03c02016-02-17 13:01:23 -08001794 cmd->address = spa->address;
1795 cmd->length = spa->length;
Dan Williamsaef25332016-02-12 17:01:11 -08001796 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
1797 sizeof(*cmd), &cmd_rc);
1798 if (rc < 0)
1799 return rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001800 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001801}
1802
Dan Williams1cf03c02016-02-17 13:01:23 -08001803static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
Vishal Verma0caeef62015-12-24 19:21:43 -07001804{
1805 int rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001806 int cmd_rc;
1807 struct nd_cmd_ars_start ars_start;
1808 struct acpi_nfit_system_address *spa = nfit_spa->spa;
1809 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001810
Dan Williams1cf03c02016-02-17 13:01:23 -08001811 memset(&ars_start, 0, sizeof(ars_start));
1812 ars_start.address = spa->address;
1813 ars_start.length = spa->length;
1814 if (nfit_spa_type(spa) == NFIT_SPA_PM)
1815 ars_start.type = ND_ARS_PERSISTENT;
1816 else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
1817 ars_start.type = ND_ARS_VOLATILE;
1818 else
1819 return -ENOTTY;
Vishal Verma0caeef62015-12-24 19:21:43 -07001820
Dan Williams1cf03c02016-02-17 13:01:23 -08001821 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1822 sizeof(ars_start), &cmd_rc);
Dan Williamsaef25332016-02-12 17:01:11 -08001823
Dan Williams1cf03c02016-02-17 13:01:23 -08001824 if (rc < 0)
1825 return rc;
1826 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001827}
1828
Dan Williams1cf03c02016-02-17 13:01:23 -08001829static int ars_continue(struct acpi_nfit_desc *acpi_desc)
Vishal Verma0caeef62015-12-24 19:21:43 -07001830{
Dan Williamsaef25332016-02-12 17:01:11 -08001831 int rc, cmd_rc;
Dan Williams1cf03c02016-02-17 13:01:23 -08001832 struct nd_cmd_ars_start ars_start;
1833 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1834 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
Vishal Verma0caeef62015-12-24 19:21:43 -07001835
Dan Williams1cf03c02016-02-17 13:01:23 -08001836 memset(&ars_start, 0, sizeof(ars_start));
1837 ars_start.address = ars_status->restart_address;
1838 ars_start.length = ars_status->restart_length;
1839 ars_start.type = ars_status->type;
1840 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1841 sizeof(ars_start), &cmd_rc);
1842 if (rc < 0)
1843 return rc;
1844 return cmd_rc;
1845}
Dan Williamsaef25332016-02-12 17:01:11 -08001846
Dan Williams1cf03c02016-02-17 13:01:23 -08001847static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
1848{
1849 struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1850 struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
1851 int rc, cmd_rc;
Dan Williamsaef25332016-02-12 17:01:11 -08001852
Dan Williams1cf03c02016-02-17 13:01:23 -08001853 rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
1854 acpi_desc->ars_status_size, &cmd_rc);
1855 if (rc < 0)
1856 return rc;
1857 return cmd_rc;
Vishal Verma0caeef62015-12-24 19:21:43 -07001858}
1859
1860static int ars_status_process_records(struct nvdimm_bus *nvdimm_bus,
Dan Williams1cf03c02016-02-17 13:01:23 -08001861 struct nd_cmd_ars_status *ars_status)
Vishal Verma0caeef62015-12-24 19:21:43 -07001862{
1863 int rc;
1864 u32 i;
1865
Vishal Verma0caeef62015-12-24 19:21:43 -07001866 for (i = 0; i < ars_status->num_records; i++) {
1867 rc = nvdimm_bus_add_poison(nvdimm_bus,
1868 ars_status->records[i].err_address,
1869 ars_status->records[i].length);
1870 if (rc)
1871 return rc;
1872 }
1873
1874 return 0;
1875}
1876
Toshi Kaniaf1996e2016-03-09 12:47:06 -07001877static void acpi_nfit_remove_resource(void *data)
1878{
1879 struct resource *res = data;
1880
1881 remove_resource(res);
1882}
1883
1884static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
1885 struct nd_region_desc *ndr_desc)
1886{
1887 struct resource *res, *nd_res = ndr_desc->res;
1888 int is_pmem, ret;
1889
1890 /* No operation if the region is already registered as PMEM */
1891 is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
1892 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
1893 if (is_pmem == REGION_INTERSECTS)
1894 return 0;
1895
1896 res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
1897 if (!res)
1898 return -ENOMEM;
1899
1900 res->name = "Persistent Memory";
1901 res->start = nd_res->start;
1902 res->end = nd_res->end;
1903 res->flags = IORESOURCE_MEM;
1904 res->desc = IORES_DESC_PERSISTENT_MEMORY;
1905
1906 ret = insert_resource(&iomem_resource, res);
1907 if (ret)
1908 return ret;
1909
Sajjan, Vikas Cd932dd22016-07-04 10:02:51 +05301910 ret = devm_add_action_or_reset(acpi_desc->dev,
1911 acpi_nfit_remove_resource,
1912 res);
1913 if (ret)
Toshi Kaniaf1996e2016-03-09 12:47:06 -07001914 return ret;
Toshi Kaniaf1996e2016-03-09 12:47:06 -07001915
1916 return 0;
1917}
1918
Dan Williams1f7df6f2015-06-09 20:13:14 -04001919static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
1920 struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
1921 struct acpi_nfit_memory_map *memdev,
Dan Williams1cf03c02016-02-17 13:01:23 -08001922 struct nfit_spa *nfit_spa)
Dan Williams1f7df6f2015-06-09 20:13:14 -04001923{
1924 struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
1925 memdev->device_handle);
Dan Williams1cf03c02016-02-17 13:01:23 -08001926 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001927 struct nd_blk_region_desc *ndbr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001928 struct nfit_mem *nfit_mem;
1929 int blk_valid = 0;
1930
1931 if (!nvdimm) {
1932 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
1933 spa->range_index, memdev->device_handle);
1934 return -ENODEV;
1935 }
1936
1937 nd_mapping->nvdimm = nvdimm;
1938 switch (nfit_spa_type(spa)) {
1939 case NFIT_SPA_PM:
1940 case NFIT_SPA_VOLATILE:
1941 nd_mapping->start = memdev->address;
1942 nd_mapping->size = memdev->region_size;
1943 break;
1944 case NFIT_SPA_DCR:
1945 nfit_mem = nvdimm_provider_data(nvdimm);
1946 if (!nfit_mem || !nfit_mem->bdw) {
1947 dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
1948 spa->range_index, nvdimm_name(nvdimm));
1949 } else {
1950 nd_mapping->size = nfit_mem->bdw->capacity;
1951 nd_mapping->start = nfit_mem->bdw->start_address;
Vishal Verma5212e112015-06-25 04:20:32 -04001952 ndr_desc->num_lanes = nfit_mem->bdw->windows;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001953 blk_valid = 1;
1954 }
1955
1956 ndr_desc->nd_mapping = nd_mapping;
1957 ndr_desc->num_mappings = blk_valid;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001958 ndbr_desc = to_blk_region_desc(ndr_desc);
1959 ndbr_desc->enable = acpi_nfit_blk_region_enable;
Dan Williams6bc75612015-06-17 17:23:32 -04001960 ndbr_desc->do_io = acpi_desc->blk_do_io;
Dan Williams1cf03c02016-02-17 13:01:23 -08001961 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
1962 ndr_desc);
1963 if (!nfit_spa->nd_region)
Dan Williams1f7df6f2015-06-09 20:13:14 -04001964 return -ENOMEM;
1965 break;
1966 }
1967
1968 return 0;
1969}
1970
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08001971static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
1972{
1973 return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
1974 nfit_spa_type(spa) == NFIT_SPA_VCD ||
1975 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
1976 nfit_spa_type(spa) == NFIT_SPA_PCD);
1977}
1978
Dan Williams1f7df6f2015-06-09 20:13:14 -04001979static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
1980 struct nfit_spa *nfit_spa)
1981{
1982 static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
1983 struct acpi_nfit_system_address *spa = nfit_spa->spa;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04001984 struct nd_blk_region_desc ndbr_desc;
1985 struct nd_region_desc *ndr_desc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001986 struct nfit_memdev *nfit_memdev;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001987 struct nvdimm_bus *nvdimm_bus;
1988 struct resource res;
Dan Williamseaf96152015-05-01 13:11:27 -04001989 int count = 0, rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04001990
Dan Williams1cf03c02016-02-17 13:01:23 -08001991 if (nfit_spa->nd_region)
Vishal Verma20985162015-10-27 16:58:27 -06001992 return 0;
1993
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08001994 if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
Dan Williams1f7df6f2015-06-09 20:13:14 -04001995 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
1996 __func__);
1997 return 0;
1998 }
1999
2000 memset(&res, 0, sizeof(res));
2001 memset(&nd_mappings, 0, sizeof(nd_mappings));
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002002 memset(&ndbr_desc, 0, sizeof(ndbr_desc));
Dan Williams1f7df6f2015-06-09 20:13:14 -04002003 res.start = spa->address;
2004 res.end = res.start + spa->length - 1;
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002005 ndr_desc = &ndbr_desc.ndr_desc;
2006 ndr_desc->res = &res;
2007 ndr_desc->provider_data = nfit_spa;
2008 ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
Toshi Kani41d7a6d2015-06-19 12:18:33 -06002009 if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2010 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2011 spa->proximity_domain);
2012 else
2013 ndr_desc->numa_node = NUMA_NO_NODE;
2014
Dan Williams1f7df6f2015-06-09 20:13:14 -04002015 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2016 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2017 struct nd_mapping *nd_mapping;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002018
2019 if (memdev->range_index != spa->range_index)
2020 continue;
2021 if (count >= ND_MAX_MAPPINGS) {
2022 dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2023 spa->range_index, ND_MAX_MAPPINGS);
2024 return -ENXIO;
2025 }
2026 nd_mapping = &nd_mappings[count++];
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002027 rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc,
Dan Williams1cf03c02016-02-17 13:01:23 -08002028 memdev, nfit_spa);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002029 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002030 goto out;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002031 }
2032
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002033 ndr_desc->nd_mapping = nd_mappings;
2034 ndr_desc->num_mappings = count;
2035 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
Dan Williamseaf96152015-05-01 13:11:27 -04002036 if (rc)
Dan Williams1cf03c02016-02-17 13:01:23 -08002037 goto out;
Dan Williamseaf96152015-05-01 13:11:27 -04002038
Dan Williams1f7df6f2015-06-09 20:13:14 -04002039 nvdimm_bus = acpi_desc->nvdimm_bus;
2040 if (nfit_spa_type(spa) == NFIT_SPA_PM) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002041 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
Dan Williams48901162016-03-09 17:15:43 -08002042 if (rc) {
Toshi Kaniaf1996e2016-03-09 12:47:06 -07002043 dev_warn(acpi_desc->dev,
2044 "failed to insert pmem resource to iomem: %d\n",
2045 rc);
Dan Williams48901162016-03-09 17:15:43 -08002046 goto out;
Vishal Verma0caeef62015-12-24 19:21:43 -07002047 }
Dan Williams48901162016-03-09 17:15:43 -08002048
Dan Williams1cf03c02016-02-17 13:01:23 -08002049 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2050 ndr_desc);
2051 if (!nfit_spa->nd_region)
2052 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002053 } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
Dan Williams1cf03c02016-02-17 13:01:23 -08002054 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2055 ndr_desc);
2056 if (!nfit_spa->nd_region)
2057 rc = -ENOMEM;
Lee, Chun-Yic2f32ac2016-07-15 12:05:35 +08002058 } else if (nfit_spa_is_virtual(spa)) {
2059 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2060 ndr_desc);
2061 if (!nfit_spa->nd_region)
2062 rc = -ENOMEM;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002063 }
Vishal Verma20985162015-10-27 16:58:27 -06002064
Dan Williams1cf03c02016-02-17 13:01:23 -08002065 out:
2066 if (rc)
2067 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2068 nfit_spa->spa->range_index);
2069 return rc;
2070}
2071
2072static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2073 u32 max_ars)
2074{
2075 struct device *dev = acpi_desc->dev;
2076 struct nd_cmd_ars_status *ars_status;
2077
2078 if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2079 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2080 return 0;
2081 }
2082
2083 if (acpi_desc->ars_status)
2084 devm_kfree(dev, acpi_desc->ars_status);
2085 acpi_desc->ars_status = NULL;
2086 ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2087 if (!ars_status)
2088 return -ENOMEM;
2089 acpi_desc->ars_status = ars_status;
2090 acpi_desc->ars_status_size = max_ars;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002091 return 0;
2092}
2093
Dan Williams1cf03c02016-02-17 13:01:23 -08002094static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2095 struct nfit_spa *nfit_spa)
2096{
2097 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2098 int rc;
2099
2100 if (!nfit_spa->max_ars) {
2101 struct nd_cmd_ars_cap ars_cap;
2102
2103 memset(&ars_cap, 0, sizeof(ars_cap));
2104 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2105 if (rc < 0)
2106 return rc;
2107 nfit_spa->max_ars = ars_cap.max_ars_out;
2108 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2109 /* check that the supported scrub types match the spa type */
2110 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2111 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2112 return -ENOTTY;
2113 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2114 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2115 return -ENOTTY;
2116 }
2117
2118 if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2119 return -ENOMEM;
2120
2121 rc = ars_get_status(acpi_desc);
2122 if (rc < 0 && rc != -ENOSPC)
2123 return rc;
2124
2125 if (ars_status_process_records(acpi_desc->nvdimm_bus,
2126 acpi_desc->ars_status))
2127 return -ENOMEM;
2128
2129 return 0;
2130}
2131
2132static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2133 struct nfit_spa *nfit_spa)
2134{
2135 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2136 unsigned int overflow_retry = scrub_overflow_abort;
2137 u64 init_ars_start = 0, init_ars_len = 0;
2138 struct device *dev = acpi_desc->dev;
2139 unsigned int tmo = scrub_timeout;
2140 int rc;
2141
Vishal Verma37b137f2016-07-23 21:51:42 -07002142 if (!nfit_spa->ars_required || !nfit_spa->nd_region)
Dan Williams1cf03c02016-02-17 13:01:23 -08002143 return;
2144
2145 rc = ars_start(acpi_desc, nfit_spa);
2146 /*
2147 * If we timed out the initial scan we'll still be busy here,
2148 * and will wait another timeout before giving up permanently.
2149 */
2150 if (rc < 0 && rc != -EBUSY)
2151 return;
2152
2153 do {
2154 u64 ars_start, ars_len;
2155
2156 if (acpi_desc->cancel)
2157 break;
2158 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2159 if (rc == -ENOTTY)
2160 break;
2161 if (rc == -EBUSY && !tmo) {
2162 dev_warn(dev, "range %d ars timeout, aborting\n",
2163 spa->range_index);
2164 break;
2165 }
2166
2167 if (rc == -EBUSY) {
2168 /*
2169 * Note, entries may be appended to the list
2170 * while the lock is dropped, but the workqueue
2171 * being active prevents entries being deleted /
2172 * freed.
2173 */
2174 mutex_unlock(&acpi_desc->init_mutex);
2175 ssleep(1);
2176 tmo--;
2177 mutex_lock(&acpi_desc->init_mutex);
2178 continue;
2179 }
2180
2181 /* we got some results, but there are more pending... */
2182 if (rc == -ENOSPC && overflow_retry--) {
2183 if (!init_ars_len) {
2184 init_ars_len = acpi_desc->ars_status->length;
2185 init_ars_start = acpi_desc->ars_status->address;
2186 }
2187 rc = ars_continue(acpi_desc);
2188 }
2189
2190 if (rc < 0) {
2191 dev_warn(dev, "range %d ars continuation failed\n",
2192 spa->range_index);
2193 break;
2194 }
2195
2196 if (init_ars_len) {
2197 ars_start = init_ars_start;
2198 ars_len = init_ars_len;
2199 } else {
2200 ars_start = acpi_desc->ars_status->address;
2201 ars_len = acpi_desc->ars_status->length;
2202 }
2203 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2204 spa->range_index, ars_start, ars_len);
2205 /* notify the region about new poison entries */
2206 nvdimm_region_notify(nfit_spa->nd_region,
2207 NVDIMM_REVALIDATE_POISON);
2208 break;
2209 } while (1);
2210}
2211
2212static void acpi_nfit_scrub(struct work_struct *work)
2213{
2214 struct device *dev;
2215 u64 init_scrub_length = 0;
2216 struct nfit_spa *nfit_spa;
2217 u64 init_scrub_address = 0;
2218 bool init_ars_done = false;
2219 struct acpi_nfit_desc *acpi_desc;
2220 unsigned int tmo = scrub_timeout;
2221 unsigned int overflow_retry = scrub_overflow_abort;
2222
2223 acpi_desc = container_of(work, typeof(*acpi_desc), work);
2224 dev = acpi_desc->dev;
2225
2226 /*
2227 * We scrub in 2 phases. The first phase waits for any platform
2228 * firmware initiated scrubs to complete and then we go search for the
2229 * affected spa regions to mark them scanned. In the second phase we
2230 * initiate a directed scrub for every range that was not scrubbed in
Vishal Verma37b137f2016-07-23 21:51:42 -07002231 * phase 1. If we're called for a 'rescan', we harmlessly pass through
2232 * the first phase, but really only care about running phase 2, where
2233 * regions can be notified of new poison.
Dan Williams1cf03c02016-02-17 13:01:23 -08002234 */
2235
2236 /* process platform firmware initiated scrubs */
2237 retry:
2238 mutex_lock(&acpi_desc->init_mutex);
2239 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2240 struct nd_cmd_ars_status *ars_status;
2241 struct acpi_nfit_system_address *spa;
2242 u64 ars_start, ars_len;
2243 int rc;
2244
2245 if (acpi_desc->cancel)
2246 break;
2247
2248 if (nfit_spa->nd_region)
2249 continue;
2250
2251 if (init_ars_done) {
2252 /*
2253 * No need to re-query, we're now just
2254 * reconciling all the ranges covered by the
2255 * initial scrub
2256 */
2257 rc = 0;
2258 } else
2259 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2260
2261 if (rc == -ENOTTY) {
2262 /* no ars capability, just register spa and move on */
2263 acpi_nfit_register_region(acpi_desc, nfit_spa);
2264 continue;
2265 }
2266
2267 if (rc == -EBUSY && !tmo) {
2268 /* fallthrough to directed scrub in phase 2 */
2269 dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2270 break;
2271 } else if (rc == -EBUSY) {
2272 mutex_unlock(&acpi_desc->init_mutex);
2273 ssleep(1);
2274 tmo--;
2275 goto retry;
2276 }
2277
2278 /* we got some results, but there are more pending... */
2279 if (rc == -ENOSPC && overflow_retry--) {
2280 ars_status = acpi_desc->ars_status;
2281 /*
2282 * Record the original scrub range, so that we
2283 * can recall all the ranges impacted by the
2284 * initial scrub.
2285 */
2286 if (!init_scrub_length) {
2287 init_scrub_length = ars_status->length;
2288 init_scrub_address = ars_status->address;
2289 }
2290 rc = ars_continue(acpi_desc);
2291 if (rc == 0) {
2292 mutex_unlock(&acpi_desc->init_mutex);
2293 goto retry;
2294 }
2295 }
2296
2297 if (rc < 0) {
2298 /*
2299 * Initial scrub failed, we'll give it one more
2300 * try below...
2301 */
2302 break;
2303 }
2304
2305 /* We got some final results, record completed ranges */
2306 ars_status = acpi_desc->ars_status;
2307 if (init_scrub_length) {
2308 ars_start = init_scrub_address;
2309 ars_len = ars_start + init_scrub_length;
2310 } else {
2311 ars_start = ars_status->address;
2312 ars_len = ars_status->length;
2313 }
2314 spa = nfit_spa->spa;
2315
2316 if (!init_ars_done) {
2317 init_ars_done = true;
2318 dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2319 ars_start, ars_len);
2320 }
2321 if (ars_start <= spa->address && ars_start + ars_len
2322 >= spa->address + spa->length)
2323 acpi_nfit_register_region(acpi_desc, nfit_spa);
2324 }
2325
2326 /*
2327 * For all the ranges not covered by an initial scrub we still
2328 * want to see if there are errors, but it's ok to discover them
2329 * asynchronously.
2330 */
2331 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2332 /*
2333 * Flag all the ranges that still need scrubbing, but
2334 * register them now to make data available.
2335 */
Vishal Verma37b137f2016-07-23 21:51:42 -07002336 if (!nfit_spa->nd_region) {
2337 nfit_spa->ars_required = 1;
Dan Williams1cf03c02016-02-17 13:01:23 -08002338 acpi_nfit_register_region(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002339 }
Dan Williams1cf03c02016-02-17 13:01:23 -08002340 }
2341
2342 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2343 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
Vishal Verma37b137f2016-07-23 21:51:42 -07002344 acpi_desc->scrub_count++;
2345 if (acpi_desc->scrub_count_state)
2346 sysfs_notify_dirent(acpi_desc->scrub_count_state);
Dan Williams1cf03c02016-02-17 13:01:23 -08002347 mutex_unlock(&acpi_desc->init_mutex);
2348}
2349
Dan Williams1f7df6f2015-06-09 20:13:14 -04002350static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2351{
2352 struct nfit_spa *nfit_spa;
Dan Williams1cf03c02016-02-17 13:01:23 -08002353 int rc;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002354
Dan Williams1cf03c02016-02-17 13:01:23 -08002355 list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2356 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2357 /* BLK regions don't need to wait for ars results */
2358 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2359 if (rc)
2360 return rc;
2361 }
Dan Williams1f7df6f2015-06-09 20:13:14 -04002362
Dan Williams1cf03c02016-02-17 13:01:23 -08002363 queue_work(nfit_wq, &acpi_desc->work);
Dan Williams1f7df6f2015-06-09 20:13:14 -04002364 return 0;
2365}
2366
Vishal Verma20985162015-10-27 16:58:27 -06002367static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2368 struct nfit_table_prev *prev)
2369{
2370 struct device *dev = acpi_desc->dev;
2371
2372 if (!list_empty(&prev->spas) ||
2373 !list_empty(&prev->memdevs) ||
2374 !list_empty(&prev->dcrs) ||
2375 !list_empty(&prev->bdws) ||
2376 !list_empty(&prev->idts) ||
2377 !list_empty(&prev->flushes)) {
2378 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2379 return -ENXIO;
2380 }
2381 return 0;
2382}
2383
Vishal Verma37b137f2016-07-23 21:51:42 -07002384static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2385{
2386 struct device *dev = acpi_desc->dev;
2387 struct kernfs_node *nfit;
2388 struct device *bus_dev;
2389
2390 if (!ars_supported(acpi_desc->nvdimm_bus))
2391 return 0;
2392
2393 bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2394 nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2395 if (!nfit) {
2396 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2397 return -ENODEV;
2398 }
2399 acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2400 sysfs_put(nfit);
2401 if (!acpi_desc->scrub_count_state) {
2402 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2403 return -ENODEV;
2404 }
2405
2406 return 0;
2407}
2408
Dan Williams58cd71b2016-07-21 18:05:36 -07002409static void acpi_nfit_destruct(void *data)
2410{
2411 struct acpi_nfit_desc *acpi_desc = data;
Vishal Verma37b137f2016-07-23 21:51:42 -07002412 struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
Dan Williams58cd71b2016-07-21 18:05:36 -07002413
Vishal Verma6839a6d2016-07-23 21:51:21 -07002414 /*
2415 * Destruct under acpi_desc_lock so that nfit_handle_mce does not
2416 * race teardown
2417 */
2418 mutex_lock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002419 acpi_desc->cancel = 1;
Vishal Verma37b137f2016-07-23 21:51:42 -07002420 /*
2421 * Bounce the nvdimm bus lock to make sure any in-flight
2422 * acpi_nfit_ars_rescan() submissions have had a chance to
2423 * either submit or see ->cancel set.
2424 */
2425 device_lock(bus_dev);
2426 device_unlock(bus_dev);
2427
Dan Williams58cd71b2016-07-21 18:05:36 -07002428 flush_workqueue(nfit_wq);
Vishal Verma37b137f2016-07-23 21:51:42 -07002429 if (acpi_desc->scrub_count_state)
2430 sysfs_put(acpi_desc->scrub_count_state);
Dan Williams58cd71b2016-07-21 18:05:36 -07002431 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2432 acpi_desc->nvdimm_bus = NULL;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002433 list_del(&acpi_desc->list);
2434 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002435}
2436
Dan Williamse7a11b42016-07-14 16:19:55 -07002437int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
Dan Williamsb94d5232015-05-19 22:54:31 -04002438{
2439 struct device *dev = acpi_desc->dev;
Vishal Verma20985162015-10-27 16:58:27 -06002440 struct nfit_table_prev prev;
Dan Williamsb94d5232015-05-19 22:54:31 -04002441 const void *end;
Dan Williams1f7df6f2015-06-09 20:13:14 -04002442 int rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002443
Dan Williams58cd71b2016-07-21 18:05:36 -07002444 if (!acpi_desc->nvdimm_bus) {
Vishal Verma37b137f2016-07-23 21:51:42 -07002445 acpi_nfit_init_dsms(acpi_desc);
2446
Dan Williams58cd71b2016-07-21 18:05:36 -07002447 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2448 &acpi_desc->nd_desc);
2449 if (!acpi_desc->nvdimm_bus)
2450 return -ENOMEM;
Vishal Verma37b137f2016-07-23 21:51:42 -07002451
Dan Williams58cd71b2016-07-21 18:05:36 -07002452 rc = devm_add_action_or_reset(dev, acpi_nfit_destruct,
2453 acpi_desc);
2454 if (rc)
2455 return rc;
Vishal Verma37b137f2016-07-23 21:51:42 -07002456
2457 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2458 if (rc)
2459 return rc;
Vishal Verma6839a6d2016-07-23 21:51:21 -07002460
2461 /* register this acpi_desc for mce notifications */
2462 mutex_lock(&acpi_desc_lock);
2463 list_add_tail(&acpi_desc->list, &acpi_descs);
2464 mutex_unlock(&acpi_desc_lock);
Dan Williams58cd71b2016-07-21 18:05:36 -07002465 }
2466
Vishal Verma20985162015-10-27 16:58:27 -06002467 mutex_lock(&acpi_desc->init_mutex);
2468
2469 INIT_LIST_HEAD(&prev.spas);
2470 INIT_LIST_HEAD(&prev.memdevs);
2471 INIT_LIST_HEAD(&prev.dcrs);
2472 INIT_LIST_HEAD(&prev.bdws);
2473 INIT_LIST_HEAD(&prev.idts);
2474 INIT_LIST_HEAD(&prev.flushes);
2475
2476 list_cut_position(&prev.spas, &acpi_desc->spas,
2477 acpi_desc->spas.prev);
2478 list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2479 acpi_desc->memdevs.prev);
2480 list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2481 acpi_desc->dcrs.prev);
2482 list_cut_position(&prev.bdws, &acpi_desc->bdws,
2483 acpi_desc->bdws.prev);
2484 list_cut_position(&prev.idts, &acpi_desc->idts,
2485 acpi_desc->idts.prev);
2486 list_cut_position(&prev.flushes, &acpi_desc->flushes,
2487 acpi_desc->flushes.prev);
2488
Vishal Verma20985162015-10-27 16:58:27 -06002489 end = data + sz;
Vishal Verma20985162015-10-27 16:58:27 -06002490 while (!IS_ERR_OR_NULL(data))
2491 data = add_table(acpi_desc, &prev, data, end);
2492
2493 if (IS_ERR(data)) {
2494 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2495 PTR_ERR(data));
2496 rc = PTR_ERR(data);
2497 goto out_unlock;
2498 }
2499
2500 rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2501 if (rc)
2502 goto out_unlock;
2503
Dan Williams81ed4e32016-06-10 18:20:53 -07002504 rc = nfit_mem_init(acpi_desc);
2505 if (rc)
Vishal Verma20985162015-10-27 16:58:27 -06002506 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002507
2508 rc = acpi_nfit_register_dimms(acpi_desc);
2509 if (rc)
2510 goto out_unlock;
2511
2512 rc = acpi_nfit_register_regions(acpi_desc);
2513
2514 out_unlock:
2515 mutex_unlock(&acpi_desc->init_mutex);
2516 return rc;
2517}
2518EXPORT_SYMBOL_GPL(acpi_nfit_init);
2519
Dan Williams7ae0fa432016-02-19 12:16:34 -08002520struct acpi_nfit_flush_work {
2521 struct work_struct work;
2522 struct completion cmp;
2523};
2524
2525static void flush_probe(struct work_struct *work)
2526{
2527 struct acpi_nfit_flush_work *flush;
2528
2529 flush = container_of(work, typeof(*flush), work);
2530 complete(&flush->cmp);
2531}
2532
2533static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2534{
2535 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2536 struct device *dev = acpi_desc->dev;
2537 struct acpi_nfit_flush_work flush;
2538
2539 /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2540 device_lock(dev);
2541 device_unlock(dev);
2542
2543 /*
2544 * Scrub work could take 10s of seconds, userspace may give up so we
2545 * need to be interruptible while waiting.
2546 */
2547 INIT_WORK_ONSTACK(&flush.work, flush_probe);
2548 COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2549 queue_work(nfit_wq, &flush.work);
2550 return wait_for_completion_interruptible(&flush.cmp);
2551}
2552
Dan Williams87bf5722016-02-22 21:50:31 -08002553static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2554 struct nvdimm *nvdimm, unsigned int cmd)
2555{
2556 struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2557
2558 if (nvdimm)
2559 return 0;
2560 if (cmd != ND_CMD_ARS_START)
2561 return 0;
2562
2563 /*
2564 * The kernel and userspace may race to initiate a scrub, but
2565 * the scrub thread is prepared to lose that initial race. It
2566 * just needs guarantees that any ars it initiates are not
2567 * interrupted by any intervening start reqeusts from userspace.
2568 */
2569 if (work_busy(&acpi_desc->work))
2570 return -EBUSY;
2571
2572 return 0;
2573}
2574
Vishal Verma6839a6d2016-07-23 21:51:21 -07002575int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc)
Vishal Verma37b137f2016-07-23 21:51:42 -07002576{
2577 struct device *dev = acpi_desc->dev;
2578 struct nfit_spa *nfit_spa;
2579
2580 if (work_busy(&acpi_desc->work))
2581 return -EBUSY;
2582
2583 if (acpi_desc->cancel)
2584 return 0;
2585
2586 mutex_lock(&acpi_desc->init_mutex);
2587 list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2588 struct acpi_nfit_system_address *spa = nfit_spa->spa;
2589
2590 if (nfit_spa_type(spa) != NFIT_SPA_PM)
2591 continue;
2592
2593 nfit_spa->ars_required = 1;
2594 }
2595 queue_work(nfit_wq, &acpi_desc->work);
2596 dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
2597 mutex_unlock(&acpi_desc->init_mutex);
2598
2599 return 0;
2600}
2601
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002602void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
Vishal Verma20985162015-10-27 16:58:27 -06002603{
2604 struct nvdimm_bus_descriptor *nd_desc;
Vishal Verma20985162015-10-27 16:58:27 -06002605
2606 dev_set_drvdata(dev, acpi_desc);
2607 acpi_desc->dev = dev;
2608 acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2609 nd_desc = &acpi_desc->nd_desc;
2610 nd_desc->provider_name = "ACPI.NFIT";
Dan Williamsbc9775d2016-07-21 20:03:19 -07002611 nd_desc->module = THIS_MODULE;
Vishal Verma20985162015-10-27 16:58:27 -06002612 nd_desc->ndctl = acpi_nfit_ctl;
Dan Williams7ae0fa432016-02-19 12:16:34 -08002613 nd_desc->flush_probe = acpi_nfit_flush_probe;
Dan Williams87bf5722016-02-22 21:50:31 -08002614 nd_desc->clear_to_send = acpi_nfit_clear_to_send;
Vishal Verma20985162015-10-27 16:58:27 -06002615 nd_desc->attr_groups = acpi_nfit_attribute_groups;
2616
Dan Williamsb94d5232015-05-19 22:54:31 -04002617 INIT_LIST_HEAD(&acpi_desc->spas);
2618 INIT_LIST_HEAD(&acpi_desc->dcrs);
2619 INIT_LIST_HEAD(&acpi_desc->bdws);
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002620 INIT_LIST_HEAD(&acpi_desc->idts);
Ross Zwislerc2ad2952015-07-10 11:06:13 -06002621 INIT_LIST_HEAD(&acpi_desc->flushes);
Dan Williamsb94d5232015-05-19 22:54:31 -04002622 INIT_LIST_HEAD(&acpi_desc->memdevs);
2623 INIT_LIST_HEAD(&acpi_desc->dimms);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002624 INIT_LIST_HEAD(&acpi_desc->list);
Vishal Verma20985162015-10-27 16:58:27 -06002625 mutex_init(&acpi_desc->init_mutex);
Dan Williams1cf03c02016-02-17 13:01:23 -08002626 INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
Dan Williamsb94d5232015-05-19 22:54:31 -04002627}
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002628EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
Dan Williamsb94d5232015-05-19 22:54:31 -04002629
2630static int acpi_nfit_add(struct acpi_device *adev)
2631{
Vishal Verma20985162015-10-27 16:58:27 -06002632 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Dan Williamsb94d5232015-05-19 22:54:31 -04002633 struct acpi_nfit_desc *acpi_desc;
2634 struct device *dev = &adev->dev;
2635 struct acpi_table_header *tbl;
2636 acpi_status status = AE_OK;
2637 acpi_size sz;
Dan Williams31932042016-07-14 17:22:48 -07002638 int rc = 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002639
Lee, Chun-Yi82595422016-01-21 20:32:10 +08002640 status = acpi_get_table_with_size(ACPI_SIG_NFIT, 0, &tbl, &sz);
Dan Williamsb94d5232015-05-19 22:54:31 -04002641 if (ACPI_FAILURE(status)) {
Vishal Verma20985162015-10-27 16:58:27 -06002642 /* This is ok, we could have an nvdimm hotplugged later */
2643 dev_dbg(dev, "failed to find NFIT at startup\n");
2644 return 0;
Dan Williamsb94d5232015-05-19 22:54:31 -04002645 }
2646
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002647 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2648 if (!acpi_desc)
2649 return -ENOMEM;
2650 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williamsb94d5232015-05-19 22:54:31 -04002651
Dan Williamse7a11b42016-07-14 16:19:55 -07002652 /* Save the acpi header for exporting the revision via sysfs */
Linda Knippers6b577c92015-11-20 19:05:49 -05002653 acpi_desc->acpi_header = *tbl;
Dan Williamsb94d5232015-05-19 22:54:31 -04002654
Vishal Verma20985162015-10-27 16:58:27 -06002655 /* Evaluate _FIT and override with that if present */
2656 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2657 if (ACPI_SUCCESS(status) && buf.length > 0) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002658 union acpi_object *obj = buf.pointer;
2659
2660 if (obj->type == ACPI_TYPE_BUFFER)
2661 rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2662 obj->buffer.length);
2663 else
Linda Knippers6b577c92015-11-20 19:05:49 -05002664 dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2665 __func__, (int) obj->type);
Dan Williams31932042016-07-14 17:22:48 -07002666 kfree(buf.pointer);
2667 } else
Dan Williamse7a11b42016-07-14 16:19:55 -07002668 /* skip over the lead-in header table */
2669 rc = acpi_nfit_init(acpi_desc, (void *) tbl
2670 + sizeof(struct acpi_table_nfit),
2671 sz - sizeof(struct acpi_table_nfit));
Dan Williamse7a11b42016-07-14 16:19:55 -07002672 return rc;
Dan Williamsb94d5232015-05-19 22:54:31 -04002673}
2674
2675static int acpi_nfit_remove(struct acpi_device *adev)
2676{
Dan Williams58cd71b2016-07-21 18:05:36 -07002677 /* see acpi_nfit_destruct */
Dan Williamsb94d5232015-05-19 22:54:31 -04002678 return 0;
2679}
2680
Vishal Verma20985162015-10-27 16:58:27 -06002681static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2682{
2683 struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
2684 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
Vishal Verma20985162015-10-27 16:58:27 -06002685 struct device *dev = &adev->dev;
Dan Williamse7a11b42016-07-14 16:19:55 -07002686 union acpi_object *obj;
Vishal Verma20985162015-10-27 16:58:27 -06002687 acpi_status status;
2688 int ret;
2689
2690 dev_dbg(dev, "%s: event: %d\n", __func__, event);
2691
2692 device_lock(dev);
2693 if (!dev->driver) {
2694 /* dev->driver may be null if we're being removed */
2695 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
Alexey Khoroshilovd91e8922015-12-11 23:24:10 +03002696 goto out_unlock;
Vishal Verma20985162015-10-27 16:58:27 -06002697 }
2698
2699 if (!acpi_desc) {
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002700 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2701 if (!acpi_desc)
Vishal Verma20985162015-10-27 16:58:27 -06002702 goto out_unlock;
Dan Williamsa61fe6f2016-02-19 12:29:32 -08002703 acpi_nfit_desc_init(acpi_desc, &adev->dev);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002704 } else {
2705 /*
2706 * Finish previous registration before considering new
2707 * regions.
2708 */
2709 flush_workqueue(nfit_wq);
Vishal Verma20985162015-10-27 16:58:27 -06002710 }
2711
2712 /* Evaluate _FIT */
2713 status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2714 if (ACPI_FAILURE(status)) {
2715 dev_err(dev, "failed to evaluate _FIT\n");
2716 goto out_unlock;
2717 }
2718
Linda Knippers6b577c92015-11-20 19:05:49 -05002719 obj = buf.pointer;
2720 if (obj->type == ACPI_TYPE_BUFFER) {
Dan Williamse7a11b42016-07-14 16:19:55 -07002721 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
2722 obj->buffer.length);
Dan Williams31932042016-07-14 17:22:48 -07002723 if (ret)
Linda Knippers6b577c92015-11-20 19:05:49 -05002724 dev_err(dev, "failed to merge updated NFIT\n");
Dan Williams31932042016-07-14 17:22:48 -07002725 } else
Linda Knippers6b577c92015-11-20 19:05:49 -05002726 dev_err(dev, "Invalid _FIT\n");
Vishal Verma20985162015-10-27 16:58:27 -06002727 kfree(buf.pointer);
2728
2729 out_unlock:
2730 device_unlock(dev);
2731}
2732
Dan Williamsb94d5232015-05-19 22:54:31 -04002733static const struct acpi_device_id acpi_nfit_ids[] = {
2734 { "ACPI0012", 0 },
2735 { "", 0 },
2736};
2737MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
2738
2739static struct acpi_driver acpi_nfit_driver = {
2740 .name = KBUILD_MODNAME,
2741 .ids = acpi_nfit_ids,
2742 .ops = {
2743 .add = acpi_nfit_add,
2744 .remove = acpi_nfit_remove,
Vishal Verma20985162015-10-27 16:58:27 -06002745 .notify = acpi_nfit_notify,
Dan Williamsb94d5232015-05-19 22:54:31 -04002746 },
2747};
2748
2749static __init int nfit_init(void)
2750{
2751 BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
2752 BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
2753 BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
2754 BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
2755 BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
2756 BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
2757 BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
2758
2759 acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
2760 acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
2761 acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
2762 acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
2763 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
2764 acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
2765 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
2766 acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
2767 acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
2768 acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
Dan Williams31eca762016-04-28 16:23:43 -07002769 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
2770 acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
stuart hayese02fb722016-05-26 11:38:41 -05002771 acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
Dan Williamsb94d5232015-05-19 22:54:31 -04002772
Dan Williams7ae0fa432016-02-19 12:16:34 -08002773 nfit_wq = create_singlethread_workqueue("nfit");
2774 if (!nfit_wq)
2775 return -ENOMEM;
2776
Vishal Verma6839a6d2016-07-23 21:51:21 -07002777 nfit_mce_register();
2778
Dan Williamsb94d5232015-05-19 22:54:31 -04002779 return acpi_bus_register_driver(&acpi_nfit_driver);
2780}
2781
2782static __exit void nfit_exit(void)
2783{
Vishal Verma6839a6d2016-07-23 21:51:21 -07002784 nfit_mce_unregister();
Dan Williamsb94d5232015-05-19 22:54:31 -04002785 acpi_bus_unregister_driver(&acpi_nfit_driver);
Dan Williams7ae0fa432016-02-19 12:16:34 -08002786 destroy_workqueue(nfit_wq);
Vishal Verma6839a6d2016-07-23 21:51:21 -07002787 WARN_ON(!list_empty(&acpi_descs));
Dan Williamsb94d5232015-05-19 22:54:31 -04002788}
2789
2790module_init(nfit_init);
2791module_exit(nfit_exit);
2792MODULE_LICENSE("GPL v2");
2793MODULE_AUTHOR("Intel Corporation");