Commit | Line | Data |
---|---|---|
b94d5230 DW |
1 | /* |
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> | |
047fc8a1 | 16 | #include <linux/mutex.h> |
62232e45 | 17 | #include <linux/ndctl.h> |
0caeef63 | 18 | #include <linux/delay.h> |
b94d5230 DW |
19 | #include <linux/list.h> |
20 | #include <linux/acpi.h> | |
eaf96153 | 21 | #include <linux/sort.h> |
c2ad2954 | 22 | #include <linux/pmem.h> |
047fc8a1 | 23 | #include <linux/io.h> |
1cf03c00 | 24 | #include <linux/nd.h> |
96601adb | 25 | #include <asm/cacheflush.h> |
b94d5230 DW |
26 | #include "nfit.h" |
27 | ||
047fc8a1 RZ |
28 | /* |
29 | * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is | |
30 | * irrelevant. | |
31 | */ | |
2f8e2c87 | 32 | #include <linux/io-64-nonatomic-hi-lo.h> |
047fc8a1 | 33 | |
4d88a97a DW |
34 | static bool force_enable_dimms; |
35 | module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR); | |
36 | MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status"); | |
37 | ||
1cf03c00 DW |
38 | static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT; |
39 | module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR); | |
40 | MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds"); | |
41 | ||
42 | /* after three payloads of overflow, it's dead jim */ | |
43 | static unsigned int scrub_overflow_abort = 3; | |
44 | module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR); | |
45 | MODULE_PARM_DESC(scrub_overflow_abort, | |
46 | "Number of times we overflow ARS results before abort"); | |
47 | ||
87554098 DW |
48 | static bool disable_vendor_specific; |
49 | module_param(disable_vendor_specific, bool, S_IRUGO); | |
50 | MODULE_PARM_DESC(disable_vendor_specific, | |
51 | "Limit commands to the publicly specified set\n"); | |
52 | ||
7ae0fa43 DW |
53 | static struct workqueue_struct *nfit_wq; |
54 | ||
20985164 VV |
55 | struct nfit_table_prev { |
56 | struct list_head spas; | |
57 | struct list_head memdevs; | |
58 | struct list_head dcrs; | |
59 | struct list_head bdws; | |
60 | struct list_head idts; | |
61 | struct list_head flushes; | |
62 | }; | |
63 | ||
b94d5230 DW |
64 | static u8 nfit_uuid[NFIT_UUID_MAX][16]; |
65 | ||
6bc75619 | 66 | const u8 *to_nfit_uuid(enum nfit_uuids id) |
b94d5230 DW |
67 | { |
68 | return nfit_uuid[id]; | |
69 | } | |
6bc75619 | 70 | EXPORT_SYMBOL(to_nfit_uuid); |
b94d5230 | 71 | |
62232e45 DW |
72 | static struct acpi_nfit_desc *to_acpi_nfit_desc( |
73 | struct nvdimm_bus_descriptor *nd_desc) | |
74 | { | |
75 | return container_of(nd_desc, struct acpi_nfit_desc, nd_desc); | |
76 | } | |
77 | ||
78 | static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc) | |
79 | { | |
80 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; | |
81 | ||
82 | /* | |
83 | * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct | |
84 | * acpi_device. | |
85 | */ | |
86 | if (!nd_desc->provider_name | |
87 | || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0) | |
88 | return NULL; | |
89 | ||
90 | return to_acpi_device(acpi_desc->dev); | |
91 | } | |
92 | ||
aef25338 DW |
93 | static int xlat_status(void *buf, unsigned int cmd) |
94 | { | |
d4f32367 | 95 | struct nd_cmd_clear_error *clear_err; |
aef25338 DW |
96 | struct nd_cmd_ars_status *ars_status; |
97 | struct nd_cmd_ars_start *ars_start; | |
98 | struct nd_cmd_ars_cap *ars_cap; | |
99 | u16 flags; | |
100 | ||
101 | switch (cmd) { | |
102 | case ND_CMD_ARS_CAP: | |
103 | ars_cap = buf; | |
104 | if ((ars_cap->status & 0xffff) == NFIT_ARS_CAP_NONE) | |
105 | return -ENOTTY; | |
106 | ||
107 | /* Command failed */ | |
108 | if (ars_cap->status & 0xffff) | |
109 | return -EIO; | |
110 | ||
111 | /* No supported scan types for this range */ | |
112 | flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE; | |
113 | if ((ars_cap->status >> 16 & flags) == 0) | |
114 | return -ENOTTY; | |
115 | break; | |
116 | case ND_CMD_ARS_START: | |
117 | ars_start = buf; | |
118 | /* ARS is in progress */ | |
119 | if ((ars_start->status & 0xffff) == NFIT_ARS_START_BUSY) | |
120 | return -EBUSY; | |
121 | ||
122 | /* Command failed */ | |
123 | if (ars_start->status & 0xffff) | |
124 | return -EIO; | |
125 | break; | |
126 | case ND_CMD_ARS_STATUS: | |
127 | ars_status = buf; | |
128 | /* Command failed */ | |
129 | if (ars_status->status & 0xffff) | |
130 | return -EIO; | |
131 | /* Check extended status (Upper two bytes) */ | |
132 | if (ars_status->status == NFIT_ARS_STATUS_DONE) | |
133 | return 0; | |
134 | ||
135 | /* ARS is in progress */ | |
136 | if (ars_status->status == NFIT_ARS_STATUS_BUSY) | |
137 | return -EBUSY; | |
138 | ||
139 | /* No ARS performed for the current boot */ | |
140 | if (ars_status->status == NFIT_ARS_STATUS_NONE) | |
141 | 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 | */ | |
148 | if (ars_status->status == NFIT_ARS_STATUS_INTR) { | |
149 | if (ars_status->flags & NFIT_ARS_F_OVERFLOW) | |
150 | return -ENOSPC; | |
151 | return 0; | |
152 | } | |
153 | ||
154 | /* Unknown status */ | |
155 | if (ars_status->status >> 16) | |
156 | return -EIO; | |
157 | break; | |
d4f32367 DW |
158 | case ND_CMD_CLEAR_ERROR: |
159 | clear_err = buf; | |
160 | if (clear_err->status & 0xffff) | |
161 | 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; | |
aef25338 DW |
167 | default: |
168 | break; | |
169 | } | |
170 | ||
171 | return 0; | |
172 | } | |
173 | ||
b94d5230 DW |
174 | static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, |
175 | struct nvdimm *nvdimm, unsigned int cmd, void *buf, | |
aef25338 | 176 | unsigned int buf_len, int *cmd_rc) |
b94d5230 | 177 | { |
62232e45 | 178 | struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc); |
62232e45 | 179 | union acpi_object in_obj, in_buf, *out_obj; |
31eca76b | 180 | const struct nd_cmd_desc *desc = NULL; |
62232e45 | 181 | struct device *dev = acpi_desc->dev; |
31eca76b | 182 | struct nd_cmd_pkg *call_pkg = NULL; |
62232e45 | 183 | const char *cmd_name, *dimm_name; |
31eca76b | 184 | unsigned long cmd_mask, dsm_mask; |
62232e45 | 185 | acpi_handle handle; |
31eca76b | 186 | unsigned int func; |
62232e45 DW |
187 | const u8 *uuid; |
188 | u32 offset; | |
189 | int rc, i; | |
190 | ||
31eca76b DW |
191 | func = cmd; |
192 | if (cmd == ND_CMD_CALL) { | |
193 | call_pkg = buf; | |
194 | func = call_pkg->nd_command; | |
195 | } | |
196 | ||
62232e45 DW |
197 | if (nvdimm) { |
198 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
199 | struct acpi_device *adev = nfit_mem->adev; | |
200 | ||
201 | if (!adev) | |
202 | return -ENOTTY; | |
31eca76b DW |
203 | if (call_pkg && nfit_mem->family != call_pkg->nd_family) |
204 | return -ENOTTY; | |
205 | ||
047fc8a1 | 206 | dimm_name = nvdimm_name(nvdimm); |
62232e45 | 207 | cmd_name = nvdimm_cmd_name(cmd); |
e3654eca | 208 | cmd_mask = nvdimm_cmd_mask(nvdimm); |
62232e45 DW |
209 | dsm_mask = nfit_mem->dsm_mask; |
210 | desc = nd_cmd_dimm_desc(cmd); | |
31eca76b | 211 | uuid = to_nfit_uuid(nfit_mem->family); |
62232e45 DW |
212 | handle = adev->handle; |
213 | } else { | |
214 | struct acpi_device *adev = to_acpi_dev(acpi_desc); | |
215 | ||
216 | cmd_name = nvdimm_bus_cmd_name(cmd); | |
e3654eca | 217 | cmd_mask = nd_desc->cmd_mask; |
31eca76b | 218 | dsm_mask = cmd_mask; |
62232e45 DW |
219 | desc = nd_cmd_bus_desc(cmd); |
220 | uuid = to_nfit_uuid(NFIT_DEV_BUS); | |
221 | handle = adev->handle; | |
222 | dimm_name = "bus"; | |
223 | } | |
224 | ||
225 | if (!desc || (cmd && (desc->out_num + desc->in_num == 0))) | |
226 | return -ENOTTY; | |
227 | ||
31eca76b | 228 | if (!test_bit(cmd, &cmd_mask) || !test_bit(func, &dsm_mask)) |
62232e45 DW |
229 | return -ENOTTY; |
230 | ||
231 | in_obj.type = ACPI_TYPE_PACKAGE; | |
232 | in_obj.package.count = 1; | |
233 | in_obj.package.elements = &in_buf; | |
234 | in_buf.type = ACPI_TYPE_BUFFER; | |
235 | in_buf.buffer.pointer = buf; | |
236 | in_buf.buffer.length = 0; | |
237 | ||
238 | /* libnvdimm has already validated the input envelope */ | |
239 | for (i = 0; i < desc->in_num; i++) | |
240 | in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc, | |
241 | i, buf); | |
242 | ||
31eca76b DW |
243 | if (call_pkg) { |
244 | /* skip over package wrapper */ | |
245 | in_buf.buffer.pointer = (void *) &call_pkg->nd_payload; | |
246 | in_buf.buffer.length = call_pkg->nd_size_in; | |
247 | } | |
248 | ||
62232e45 | 249 | if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) { |
31eca76b DW |
250 | dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n", |
251 | __func__, dimm_name, cmd, func, | |
252 | in_buf.buffer.length); | |
253 | print_hex_dump_debug("nvdimm in ", DUMP_PREFIX_OFFSET, 4, 4, | |
254 | in_buf.buffer.pointer, | |
255 | min_t(u32, 256, in_buf.buffer.length), true); | |
62232e45 DW |
256 | } |
257 | ||
31eca76b | 258 | out_obj = acpi_evaluate_dsm(handle, uuid, 1, func, &in_obj); |
62232e45 DW |
259 | if (!out_obj) { |
260 | dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name, | |
261 | cmd_name); | |
262 | return -EINVAL; | |
263 | } | |
264 | ||
31eca76b DW |
265 | if (call_pkg) { |
266 | call_pkg->nd_fw_size = out_obj->buffer.length; | |
267 | memcpy(call_pkg->nd_payload + call_pkg->nd_size_in, | |
268 | out_obj->buffer.pointer, | |
269 | min(call_pkg->nd_fw_size, call_pkg->nd_size_out)); | |
270 | ||
271 | ACPI_FREE(out_obj); | |
272 | /* | |
273 | * Need to support FW function w/o known size in advance. | |
274 | * Caller can determine required size based upon nd_fw_size. | |
275 | * If we return an error (like elsewhere) then caller wouldn't | |
276 | * be able to rely upon data returned to make calculation. | |
277 | */ | |
278 | return 0; | |
279 | } | |
280 | ||
62232e45 DW |
281 | if (out_obj->package.type != ACPI_TYPE_BUFFER) { |
282 | dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n", | |
283 | __func__, dimm_name, cmd_name, out_obj->type); | |
284 | rc = -EINVAL; | |
285 | goto out; | |
286 | } | |
287 | ||
288 | if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) { | |
289 | dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, | |
290 | dimm_name, cmd_name, out_obj->buffer.length); | |
291 | print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, | |
292 | 4, out_obj->buffer.pointer, min_t(u32, 128, | |
293 | out_obj->buffer.length), true); | |
294 | } | |
295 | ||
296 | for (i = 0, offset = 0; i < desc->out_num; i++) { | |
297 | u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf, | |
298 | (u32 *) out_obj->buffer.pointer); | |
299 | ||
300 | if (offset + out_size > out_obj->buffer.length) { | |
301 | dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n", | |
302 | __func__, dimm_name, cmd_name, i); | |
303 | break; | |
304 | } | |
305 | ||
306 | if (in_buf.buffer.length + offset + out_size > buf_len) { | |
307 | dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n", | |
308 | __func__, dimm_name, cmd_name, i); | |
309 | rc = -ENXIO; | |
310 | goto out; | |
311 | } | |
312 | memcpy(buf + in_buf.buffer.length + offset, | |
313 | out_obj->buffer.pointer + offset, out_size); | |
314 | offset += out_size; | |
315 | } | |
316 | if (offset + in_buf.buffer.length < buf_len) { | |
317 | if (i >= 1) { | |
318 | /* | |
319 | * status valid, return the number of bytes left | |
320 | * unfilled in the output buffer | |
321 | */ | |
322 | rc = buf_len - offset - in_buf.buffer.length; | |
aef25338 DW |
323 | if (cmd_rc) |
324 | *cmd_rc = xlat_status(buf, cmd); | |
62232e45 DW |
325 | } else { |
326 | dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n", | |
327 | __func__, dimm_name, cmd_name, buf_len, | |
328 | offset); | |
329 | rc = -ENXIO; | |
330 | } | |
2eea6582 | 331 | } else { |
62232e45 | 332 | rc = 0; |
2eea6582 DW |
333 | if (cmd_rc) |
334 | *cmd_rc = xlat_status(buf, cmd); | |
335 | } | |
62232e45 DW |
336 | |
337 | out: | |
338 | ACPI_FREE(out_obj); | |
339 | ||
340 | return rc; | |
b94d5230 DW |
341 | } |
342 | ||
343 | static const char *spa_type_name(u16 type) | |
344 | { | |
345 | static const char *to_name[] = { | |
346 | [NFIT_SPA_VOLATILE] = "volatile", | |
347 | [NFIT_SPA_PM] = "pmem", | |
348 | [NFIT_SPA_DCR] = "dimm-control-region", | |
349 | [NFIT_SPA_BDW] = "block-data-window", | |
350 | [NFIT_SPA_VDISK] = "volatile-disk", | |
351 | [NFIT_SPA_VCD] = "volatile-cd", | |
352 | [NFIT_SPA_PDISK] = "persistent-disk", | |
353 | [NFIT_SPA_PCD] = "persistent-cd", | |
354 | ||
355 | }; | |
356 | ||
357 | if (type > NFIT_SPA_PCD) | |
358 | return "unknown"; | |
359 | ||
360 | return to_name[type]; | |
361 | } | |
362 | ||
363 | static int nfit_spa_type(struct acpi_nfit_system_address *spa) | |
364 | { | |
365 | int i; | |
366 | ||
367 | for (i = 0; i < NFIT_UUID_MAX; i++) | |
368 | if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0) | |
369 | return i; | |
370 | return -1; | |
371 | } | |
372 | ||
373 | static bool add_spa(struct acpi_nfit_desc *acpi_desc, | |
20985164 | 374 | struct nfit_table_prev *prev, |
b94d5230 DW |
375 | struct acpi_nfit_system_address *spa) |
376 | { | |
377 | struct device *dev = acpi_desc->dev; | |
20985164 VV |
378 | struct nfit_spa *nfit_spa; |
379 | ||
31932041 DW |
380 | if (spa->header.length != sizeof(*spa)) |
381 | return false; | |
382 | ||
20985164 | 383 | list_for_each_entry(nfit_spa, &prev->spas, list) { |
31932041 | 384 | if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) { |
20985164 VV |
385 | list_move_tail(&nfit_spa->list, &acpi_desc->spas); |
386 | return true; | |
387 | } | |
388 | } | |
b94d5230 | 389 | |
31932041 DW |
390 | nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa), |
391 | GFP_KERNEL); | |
b94d5230 DW |
392 | if (!nfit_spa) |
393 | return false; | |
394 | INIT_LIST_HEAD(&nfit_spa->list); | |
31932041 | 395 | memcpy(nfit_spa->spa, spa, sizeof(*spa)); |
b94d5230 DW |
396 | list_add_tail(&nfit_spa->list, &acpi_desc->spas); |
397 | dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__, | |
398 | spa->range_index, | |
399 | spa_type_name(nfit_spa_type(spa))); | |
400 | return true; | |
401 | } | |
402 | ||
403 | static bool add_memdev(struct acpi_nfit_desc *acpi_desc, | |
20985164 | 404 | struct nfit_table_prev *prev, |
b94d5230 DW |
405 | struct acpi_nfit_memory_map *memdev) |
406 | { | |
407 | struct device *dev = acpi_desc->dev; | |
20985164 | 408 | struct nfit_memdev *nfit_memdev; |
b94d5230 | 409 | |
31932041 DW |
410 | if (memdev->header.length != sizeof(*memdev)) |
411 | return false; | |
412 | ||
20985164 | 413 | list_for_each_entry(nfit_memdev, &prev->memdevs, list) |
31932041 | 414 | if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) { |
20985164 VV |
415 | list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs); |
416 | return true; | |
417 | } | |
418 | ||
31932041 DW |
419 | nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev), |
420 | GFP_KERNEL); | |
b94d5230 DW |
421 | if (!nfit_memdev) |
422 | return false; | |
423 | INIT_LIST_HEAD(&nfit_memdev->list); | |
31932041 | 424 | memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev)); |
b94d5230 DW |
425 | list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs); |
426 | dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n", | |
427 | __func__, memdev->device_handle, memdev->range_index, | |
428 | memdev->region_index); | |
429 | return true; | |
430 | } | |
431 | ||
31932041 DW |
432 | /* |
433 | * An implementation may provide a truncated control region if no block windows | |
434 | * are defined. | |
435 | */ | |
436 | static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr) | |
437 | { | |
438 | if (dcr->header.length < offsetof(struct acpi_nfit_control_region, | |
439 | window_size)) | |
440 | return 0; | |
441 | if (dcr->windows) | |
442 | return sizeof(*dcr); | |
443 | return offsetof(struct acpi_nfit_control_region, window_size); | |
444 | } | |
445 | ||
b94d5230 | 446 | static bool add_dcr(struct acpi_nfit_desc *acpi_desc, |
20985164 | 447 | struct nfit_table_prev *prev, |
b94d5230 DW |
448 | struct acpi_nfit_control_region *dcr) |
449 | { | |
450 | struct device *dev = acpi_desc->dev; | |
20985164 VV |
451 | struct nfit_dcr *nfit_dcr; |
452 | ||
31932041 DW |
453 | if (!sizeof_dcr(dcr)) |
454 | return false; | |
455 | ||
20985164 | 456 | list_for_each_entry(nfit_dcr, &prev->dcrs, list) |
31932041 | 457 | if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) { |
20985164 VV |
458 | list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs); |
459 | return true; | |
460 | } | |
b94d5230 | 461 | |
31932041 DW |
462 | nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr), |
463 | GFP_KERNEL); | |
b94d5230 DW |
464 | if (!nfit_dcr) |
465 | return false; | |
466 | INIT_LIST_HEAD(&nfit_dcr->list); | |
31932041 | 467 | memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)); |
b94d5230 DW |
468 | list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs); |
469 | dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__, | |
470 | dcr->region_index, dcr->windows); | |
471 | return true; | |
472 | } | |
473 | ||
474 | static bool add_bdw(struct acpi_nfit_desc *acpi_desc, | |
20985164 | 475 | struct nfit_table_prev *prev, |
b94d5230 DW |
476 | struct acpi_nfit_data_region *bdw) |
477 | { | |
478 | struct device *dev = acpi_desc->dev; | |
20985164 VV |
479 | struct nfit_bdw *nfit_bdw; |
480 | ||
31932041 DW |
481 | if (bdw->header.length != sizeof(*bdw)) |
482 | return false; | |
20985164 | 483 | list_for_each_entry(nfit_bdw, &prev->bdws, list) |
31932041 | 484 | if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) { |
20985164 VV |
485 | list_move_tail(&nfit_bdw->list, &acpi_desc->bdws); |
486 | return true; | |
487 | } | |
b94d5230 | 488 | |
31932041 DW |
489 | nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw), |
490 | GFP_KERNEL); | |
b94d5230 DW |
491 | if (!nfit_bdw) |
492 | return false; | |
493 | INIT_LIST_HEAD(&nfit_bdw->list); | |
31932041 | 494 | memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw)); |
b94d5230 DW |
495 | list_add_tail(&nfit_bdw->list, &acpi_desc->bdws); |
496 | dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__, | |
497 | bdw->region_index, bdw->windows); | |
498 | return true; | |
499 | } | |
500 | ||
31932041 DW |
501 | static size_t sizeof_idt(struct acpi_nfit_interleave *idt) |
502 | { | |
503 | if (idt->header.length < sizeof(*idt)) | |
504 | return 0; | |
505 | return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1); | |
506 | } | |
507 | ||
047fc8a1 | 508 | static bool add_idt(struct acpi_nfit_desc *acpi_desc, |
20985164 | 509 | struct nfit_table_prev *prev, |
047fc8a1 RZ |
510 | struct acpi_nfit_interleave *idt) |
511 | { | |
512 | struct device *dev = acpi_desc->dev; | |
20985164 VV |
513 | struct nfit_idt *nfit_idt; |
514 | ||
31932041 DW |
515 | if (!sizeof_idt(idt)) |
516 | return false; | |
517 | ||
518 | list_for_each_entry(nfit_idt, &prev->idts, list) { | |
519 | if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt)) | |
520 | continue; | |
521 | ||
522 | if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) { | |
20985164 VV |
523 | list_move_tail(&nfit_idt->list, &acpi_desc->idts); |
524 | return true; | |
525 | } | |
31932041 | 526 | } |
047fc8a1 | 527 | |
31932041 DW |
528 | nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt), |
529 | GFP_KERNEL); | |
047fc8a1 RZ |
530 | if (!nfit_idt) |
531 | return false; | |
532 | INIT_LIST_HEAD(&nfit_idt->list); | |
31932041 | 533 | memcpy(nfit_idt->idt, idt, sizeof_idt(idt)); |
047fc8a1 RZ |
534 | list_add_tail(&nfit_idt->list, &acpi_desc->idts); |
535 | dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__, | |
536 | idt->interleave_index, idt->line_count); | |
537 | return true; | |
538 | } | |
539 | ||
31932041 DW |
540 | static size_t sizeof_flush(struct acpi_nfit_flush_address *flush) |
541 | { | |
542 | if (flush->header.length < sizeof(*flush)) | |
543 | return 0; | |
544 | return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1); | |
545 | } | |
546 | ||
c2ad2954 | 547 | static bool add_flush(struct acpi_nfit_desc *acpi_desc, |
20985164 | 548 | struct nfit_table_prev *prev, |
c2ad2954 RZ |
549 | struct acpi_nfit_flush_address *flush) |
550 | { | |
551 | struct device *dev = acpi_desc->dev; | |
20985164 | 552 | struct nfit_flush *nfit_flush; |
c2ad2954 | 553 | |
31932041 DW |
554 | if (!sizeof_flush(flush)) |
555 | return false; | |
556 | ||
557 | list_for_each_entry(nfit_flush, &prev->flushes, list) { | |
558 | if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush)) | |
559 | continue; | |
560 | ||
561 | if (memcmp(nfit_flush->flush, flush, | |
562 | sizeof_flush(flush)) == 0) { | |
20985164 VV |
563 | list_move_tail(&nfit_flush->list, &acpi_desc->flushes); |
564 | return true; | |
565 | } | |
31932041 | 566 | } |
20985164 | 567 | |
31932041 DW |
568 | nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush) |
569 | + sizeof_flush(flush), GFP_KERNEL); | |
c2ad2954 RZ |
570 | if (!nfit_flush) |
571 | return false; | |
572 | INIT_LIST_HEAD(&nfit_flush->list); | |
31932041 | 573 | memcpy(nfit_flush->flush, flush, sizeof_flush(flush)); |
c2ad2954 RZ |
574 | list_add_tail(&nfit_flush->list, &acpi_desc->flushes); |
575 | dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__, | |
576 | flush->device_handle, flush->hint_count); | |
577 | return true; | |
578 | } | |
579 | ||
20985164 VV |
580 | static void *add_table(struct acpi_nfit_desc *acpi_desc, |
581 | struct nfit_table_prev *prev, void *table, const void *end) | |
b94d5230 DW |
582 | { |
583 | struct device *dev = acpi_desc->dev; | |
584 | struct acpi_nfit_header *hdr; | |
585 | void *err = ERR_PTR(-ENOMEM); | |
586 | ||
587 | if (table >= end) | |
588 | return NULL; | |
589 | ||
590 | hdr = table; | |
564d5011 VV |
591 | if (!hdr->length) { |
592 | dev_warn(dev, "found a zero length table '%d' parsing nfit\n", | |
593 | hdr->type); | |
594 | return NULL; | |
595 | } | |
596 | ||
b94d5230 DW |
597 | switch (hdr->type) { |
598 | case ACPI_NFIT_TYPE_SYSTEM_ADDRESS: | |
20985164 | 599 | if (!add_spa(acpi_desc, prev, table)) |
b94d5230 DW |
600 | return err; |
601 | break; | |
602 | case ACPI_NFIT_TYPE_MEMORY_MAP: | |
20985164 | 603 | if (!add_memdev(acpi_desc, prev, table)) |
b94d5230 DW |
604 | return err; |
605 | break; | |
606 | case ACPI_NFIT_TYPE_CONTROL_REGION: | |
20985164 | 607 | if (!add_dcr(acpi_desc, prev, table)) |
b94d5230 DW |
608 | return err; |
609 | break; | |
610 | case ACPI_NFIT_TYPE_DATA_REGION: | |
20985164 | 611 | if (!add_bdw(acpi_desc, prev, table)) |
b94d5230 DW |
612 | return err; |
613 | break; | |
b94d5230 | 614 | case ACPI_NFIT_TYPE_INTERLEAVE: |
20985164 | 615 | if (!add_idt(acpi_desc, prev, table)) |
047fc8a1 | 616 | return err; |
b94d5230 DW |
617 | break; |
618 | case ACPI_NFIT_TYPE_FLUSH_ADDRESS: | |
20985164 | 619 | if (!add_flush(acpi_desc, prev, table)) |
c2ad2954 | 620 | return err; |
b94d5230 DW |
621 | break; |
622 | case ACPI_NFIT_TYPE_SMBIOS: | |
623 | dev_dbg(dev, "%s: smbios\n", __func__); | |
624 | break; | |
625 | default: | |
626 | dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type); | |
627 | break; | |
628 | } | |
629 | ||
630 | return table + hdr->length; | |
631 | } | |
632 | ||
633 | static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc, | |
634 | struct nfit_mem *nfit_mem) | |
635 | { | |
636 | u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle; | |
637 | u16 dcr = nfit_mem->dcr->region_index; | |
638 | struct nfit_spa *nfit_spa; | |
639 | ||
640 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { | |
641 | u16 range_index = nfit_spa->spa->range_index; | |
642 | int type = nfit_spa_type(nfit_spa->spa); | |
643 | struct nfit_memdev *nfit_memdev; | |
644 | ||
645 | if (type != NFIT_SPA_BDW) | |
646 | continue; | |
647 | ||
648 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { | |
649 | if (nfit_memdev->memdev->range_index != range_index) | |
650 | continue; | |
651 | if (nfit_memdev->memdev->device_handle != device_handle) | |
652 | continue; | |
653 | if (nfit_memdev->memdev->region_index != dcr) | |
654 | continue; | |
655 | ||
656 | nfit_mem->spa_bdw = nfit_spa->spa; | |
657 | return; | |
658 | } | |
659 | } | |
660 | ||
661 | dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n", | |
662 | nfit_mem->spa_dcr->range_index); | |
663 | nfit_mem->bdw = NULL; | |
664 | } | |
665 | ||
6697b2cf | 666 | static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc, |
b94d5230 DW |
667 | struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa) |
668 | { | |
669 | u16 dcr = __to_nfit_memdev(nfit_mem)->region_index; | |
047fc8a1 | 670 | struct nfit_memdev *nfit_memdev; |
b94d5230 | 671 | struct nfit_bdw *nfit_bdw; |
047fc8a1 RZ |
672 | struct nfit_idt *nfit_idt; |
673 | u16 idt_idx, range_index; | |
b94d5230 | 674 | |
b94d5230 DW |
675 | list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) { |
676 | if (nfit_bdw->bdw->region_index != dcr) | |
677 | continue; | |
678 | nfit_mem->bdw = nfit_bdw->bdw; | |
679 | break; | |
680 | } | |
681 | ||
682 | if (!nfit_mem->bdw) | |
6697b2cf | 683 | return; |
b94d5230 DW |
684 | |
685 | nfit_mem_find_spa_bdw(acpi_desc, nfit_mem); | |
047fc8a1 RZ |
686 | |
687 | if (!nfit_mem->spa_bdw) | |
6697b2cf | 688 | return; |
047fc8a1 RZ |
689 | |
690 | range_index = nfit_mem->spa_bdw->range_index; | |
691 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { | |
692 | if (nfit_memdev->memdev->range_index != range_index || | |
693 | nfit_memdev->memdev->region_index != dcr) | |
694 | continue; | |
695 | nfit_mem->memdev_bdw = nfit_memdev->memdev; | |
696 | idt_idx = nfit_memdev->memdev->interleave_index; | |
697 | list_for_each_entry(nfit_idt, &acpi_desc->idts, list) { | |
698 | if (nfit_idt->idt->interleave_index != idt_idx) | |
699 | continue; | |
700 | nfit_mem->idt_bdw = nfit_idt->idt; | |
701 | break; | |
702 | } | |
703 | break; | |
704 | } | |
b94d5230 DW |
705 | } |
706 | ||
707 | static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc, | |
708 | struct acpi_nfit_system_address *spa) | |
709 | { | |
710 | struct nfit_mem *nfit_mem, *found; | |
711 | struct nfit_memdev *nfit_memdev; | |
712 | int type = nfit_spa_type(spa); | |
b94d5230 DW |
713 | |
714 | switch (type) { | |
715 | case NFIT_SPA_DCR: | |
716 | case NFIT_SPA_PM: | |
717 | break; | |
718 | default: | |
719 | return 0; | |
720 | } | |
721 | ||
722 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { | |
ad9ac5e1 | 723 | struct nfit_flush *nfit_flush; |
6697b2cf DW |
724 | struct nfit_dcr *nfit_dcr; |
725 | u32 device_handle; | |
726 | u16 dcr; | |
b94d5230 DW |
727 | |
728 | if (nfit_memdev->memdev->range_index != spa->range_index) | |
729 | continue; | |
730 | found = NULL; | |
731 | dcr = nfit_memdev->memdev->region_index; | |
6697b2cf | 732 | device_handle = nfit_memdev->memdev->device_handle; |
b94d5230 | 733 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) |
6697b2cf DW |
734 | if (__to_nfit_memdev(nfit_mem)->device_handle |
735 | == device_handle) { | |
b94d5230 DW |
736 | found = nfit_mem; |
737 | break; | |
738 | } | |
739 | ||
740 | if (found) | |
741 | nfit_mem = found; | |
742 | else { | |
743 | nfit_mem = devm_kzalloc(acpi_desc->dev, | |
744 | sizeof(*nfit_mem), GFP_KERNEL); | |
745 | if (!nfit_mem) | |
746 | return -ENOMEM; | |
747 | INIT_LIST_HEAD(&nfit_mem->list); | |
8cc6ddfc | 748 | nfit_mem->acpi_desc = acpi_desc; |
6697b2cf DW |
749 | list_add(&nfit_mem->list, &acpi_desc->dimms); |
750 | } | |
751 | ||
752 | list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) { | |
753 | if (nfit_dcr->dcr->region_index != dcr) | |
754 | continue; | |
755 | /* | |
756 | * Record the control region for the dimm. For | |
757 | * the ACPI 6.1 case, where there are separate | |
758 | * control regions for the pmem vs blk | |
759 | * interfaces, be sure to record the extended | |
760 | * blk details. | |
761 | */ | |
762 | if (!nfit_mem->dcr) | |
763 | nfit_mem->dcr = nfit_dcr->dcr; | |
764 | else if (nfit_mem->dcr->windows == 0 | |
765 | && nfit_dcr->dcr->windows) | |
766 | nfit_mem->dcr = nfit_dcr->dcr; | |
767 | break; | |
768 | } | |
769 | ||
ad9ac5e1 | 770 | list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) { |
e5ae3b25 DW |
771 | struct acpi_nfit_flush_address *flush; |
772 | u16 i; | |
773 | ||
ad9ac5e1 DW |
774 | if (nfit_flush->flush->device_handle != device_handle) |
775 | continue; | |
776 | nfit_mem->nfit_flush = nfit_flush; | |
e5ae3b25 DW |
777 | flush = nfit_flush->flush; |
778 | nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev, | |
779 | flush->hint_count | |
780 | * sizeof(struct resource), GFP_KERNEL); | |
781 | if (!nfit_mem->flush_wpq) | |
782 | return -ENOMEM; | |
783 | for (i = 0; i < flush->hint_count; i++) { | |
784 | struct resource *res = &nfit_mem->flush_wpq[i]; | |
785 | ||
786 | res->start = flush->hint_address[i]; | |
787 | res->end = res->start + 8 - 1; | |
788 | } | |
ad9ac5e1 DW |
789 | break; |
790 | } | |
791 | ||
6697b2cf DW |
792 | if (dcr && !nfit_mem->dcr) { |
793 | dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n", | |
794 | spa->range_index, dcr); | |
795 | return -ENODEV; | |
b94d5230 DW |
796 | } |
797 | ||
798 | if (type == NFIT_SPA_DCR) { | |
047fc8a1 RZ |
799 | struct nfit_idt *nfit_idt; |
800 | u16 idt_idx; | |
801 | ||
b94d5230 DW |
802 | /* multiple dimms may share a SPA when interleaved */ |
803 | nfit_mem->spa_dcr = spa; | |
804 | nfit_mem->memdev_dcr = nfit_memdev->memdev; | |
047fc8a1 RZ |
805 | idt_idx = nfit_memdev->memdev->interleave_index; |
806 | list_for_each_entry(nfit_idt, &acpi_desc->idts, list) { | |
807 | if (nfit_idt->idt->interleave_index != idt_idx) | |
808 | continue; | |
809 | nfit_mem->idt_dcr = nfit_idt->idt; | |
810 | break; | |
811 | } | |
6697b2cf | 812 | nfit_mem_init_bdw(acpi_desc, nfit_mem, spa); |
b94d5230 DW |
813 | } else { |
814 | /* | |
815 | * A single dimm may belong to multiple SPA-PM | |
816 | * ranges, record at least one in addition to | |
817 | * any SPA-DCR range. | |
818 | */ | |
819 | nfit_mem->memdev_pmem = nfit_memdev->memdev; | |
820 | } | |
b94d5230 DW |
821 | } |
822 | ||
823 | return 0; | |
824 | } | |
825 | ||
826 | static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b) | |
827 | { | |
828 | struct nfit_mem *a = container_of(_a, typeof(*a), list); | |
829 | struct nfit_mem *b = container_of(_b, typeof(*b), list); | |
830 | u32 handleA, handleB; | |
831 | ||
832 | handleA = __to_nfit_memdev(a)->device_handle; | |
833 | handleB = __to_nfit_memdev(b)->device_handle; | |
834 | if (handleA < handleB) | |
835 | return -1; | |
836 | else if (handleA > handleB) | |
837 | return 1; | |
838 | return 0; | |
839 | } | |
840 | ||
841 | static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc) | |
842 | { | |
843 | struct nfit_spa *nfit_spa; | |
844 | ||
845 | /* | |
846 | * For each SPA-DCR or SPA-PMEM address range find its | |
847 | * corresponding MEMDEV(s). From each MEMDEV find the | |
848 | * corresponding DCR. Then, if we're operating on a SPA-DCR, | |
849 | * try to find a SPA-BDW and a corresponding BDW that references | |
850 | * the DCR. Throw it all into an nfit_mem object. Note, that | |
851 | * BDWs are optional. | |
852 | */ | |
853 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { | |
854 | int rc; | |
855 | ||
856 | rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa); | |
857 | if (rc) | |
858 | return rc; | |
859 | } | |
860 | ||
861 | list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp); | |
862 | ||
863 | return 0; | |
864 | } | |
865 | ||
45def22c DW |
866 | static ssize_t revision_show(struct device *dev, |
867 | struct device_attribute *attr, char *buf) | |
868 | { | |
869 | struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev); | |
870 | struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus); | |
871 | struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc); | |
872 | ||
6b577c9d | 873 | return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision); |
45def22c DW |
874 | } |
875 | static DEVICE_ATTR_RO(revision); | |
876 | ||
877 | static struct attribute *acpi_nfit_attributes[] = { | |
878 | &dev_attr_revision.attr, | |
879 | NULL, | |
880 | }; | |
881 | ||
882 | static struct attribute_group acpi_nfit_attribute_group = { | |
883 | .name = "nfit", | |
884 | .attrs = acpi_nfit_attributes, | |
885 | }; | |
886 | ||
a61fe6f7 | 887 | static const struct attribute_group *acpi_nfit_attribute_groups[] = { |
45def22c DW |
888 | &nvdimm_bus_attribute_group, |
889 | &acpi_nfit_attribute_group, | |
890 | NULL, | |
891 | }; | |
892 | ||
e6dfb2de DW |
893 | static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev) |
894 | { | |
895 | struct nvdimm *nvdimm = to_nvdimm(dev); | |
896 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
897 | ||
898 | return __to_nfit_memdev(nfit_mem); | |
899 | } | |
900 | ||
901 | static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev) | |
902 | { | |
903 | struct nvdimm *nvdimm = to_nvdimm(dev); | |
904 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
905 | ||
906 | return nfit_mem->dcr; | |
907 | } | |
908 | ||
909 | static ssize_t handle_show(struct device *dev, | |
910 | struct device_attribute *attr, char *buf) | |
911 | { | |
912 | struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev); | |
913 | ||
914 | return sprintf(buf, "%#x\n", memdev->device_handle); | |
915 | } | |
916 | static DEVICE_ATTR_RO(handle); | |
917 | ||
918 | static ssize_t phys_id_show(struct device *dev, | |
919 | struct device_attribute *attr, char *buf) | |
920 | { | |
921 | struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev); | |
922 | ||
923 | return sprintf(buf, "%#x\n", memdev->physical_id); | |
924 | } | |
925 | static DEVICE_ATTR_RO(phys_id); | |
926 | ||
927 | static ssize_t vendor_show(struct device *dev, | |
928 | struct device_attribute *attr, char *buf) | |
929 | { | |
930 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
931 | ||
5ad9a7fd | 932 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id)); |
e6dfb2de DW |
933 | } |
934 | static DEVICE_ATTR_RO(vendor); | |
935 | ||
936 | static ssize_t rev_id_show(struct device *dev, | |
937 | struct device_attribute *attr, char *buf) | |
938 | { | |
939 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
940 | ||
5ad9a7fd | 941 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id)); |
e6dfb2de DW |
942 | } |
943 | static DEVICE_ATTR_RO(rev_id); | |
944 | ||
945 | static ssize_t device_show(struct device *dev, | |
946 | struct device_attribute *attr, char *buf) | |
947 | { | |
948 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
949 | ||
5ad9a7fd | 950 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id)); |
e6dfb2de DW |
951 | } |
952 | static DEVICE_ATTR_RO(device); | |
953 | ||
6ca72085 DW |
954 | static ssize_t subsystem_vendor_show(struct device *dev, |
955 | struct device_attribute *attr, char *buf) | |
956 | { | |
957 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
958 | ||
959 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id)); | |
960 | } | |
961 | static DEVICE_ATTR_RO(subsystem_vendor); | |
962 | ||
963 | static ssize_t subsystem_rev_id_show(struct device *dev, | |
964 | struct device_attribute *attr, char *buf) | |
965 | { | |
966 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
967 | ||
968 | return sprintf(buf, "0x%04x\n", | |
969 | be16_to_cpu(dcr->subsystem_revision_id)); | |
970 | } | |
971 | static DEVICE_ATTR_RO(subsystem_rev_id); | |
972 | ||
973 | static ssize_t subsystem_device_show(struct device *dev, | |
974 | struct device_attribute *attr, char *buf) | |
975 | { | |
976 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
977 | ||
978 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id)); | |
979 | } | |
980 | static DEVICE_ATTR_RO(subsystem_device); | |
981 | ||
8cc6ddfc DW |
982 | static int num_nvdimm_formats(struct nvdimm *nvdimm) |
983 | { | |
984 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
985 | int formats = 0; | |
986 | ||
987 | if (nfit_mem->memdev_pmem) | |
988 | formats++; | |
989 | if (nfit_mem->memdev_bdw) | |
990 | formats++; | |
991 | return formats; | |
992 | } | |
993 | ||
e6dfb2de DW |
994 | static ssize_t format_show(struct device *dev, |
995 | struct device_attribute *attr, char *buf) | |
996 | { | |
997 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
998 | ||
1b982baf | 999 | return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->code)); |
e6dfb2de DW |
1000 | } |
1001 | static DEVICE_ATTR_RO(format); | |
1002 | ||
8cc6ddfc DW |
1003 | static ssize_t format1_show(struct device *dev, |
1004 | struct device_attribute *attr, char *buf) | |
1005 | { | |
1006 | u32 handle; | |
1007 | ssize_t rc = -ENXIO; | |
1008 | struct nfit_mem *nfit_mem; | |
1009 | struct nfit_memdev *nfit_memdev; | |
1010 | struct acpi_nfit_desc *acpi_desc; | |
1011 | struct nvdimm *nvdimm = to_nvdimm(dev); | |
1012 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
1013 | ||
1014 | nfit_mem = nvdimm_provider_data(nvdimm); | |
1015 | acpi_desc = nfit_mem->acpi_desc; | |
1016 | handle = to_nfit_memdev(dev)->device_handle; | |
1017 | ||
1018 | /* assumes DIMMs have at most 2 published interface codes */ | |
1019 | mutex_lock(&acpi_desc->init_mutex); | |
1020 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { | |
1021 | struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev; | |
1022 | struct nfit_dcr *nfit_dcr; | |
1023 | ||
1024 | if (memdev->device_handle != handle) | |
1025 | continue; | |
1026 | ||
1027 | list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) { | |
1028 | if (nfit_dcr->dcr->region_index != memdev->region_index) | |
1029 | continue; | |
1030 | if (nfit_dcr->dcr->code == dcr->code) | |
1031 | continue; | |
1b982baf DW |
1032 | rc = sprintf(buf, "%#x\n", |
1033 | be16_to_cpu(nfit_dcr->dcr->code)); | |
8cc6ddfc DW |
1034 | break; |
1035 | } | |
1036 | if (rc != ENXIO) | |
1037 | break; | |
1038 | } | |
1039 | mutex_unlock(&acpi_desc->init_mutex); | |
1040 | return rc; | |
1041 | } | |
1042 | static DEVICE_ATTR_RO(format1); | |
1043 | ||
1044 | static ssize_t formats_show(struct device *dev, | |
1045 | struct device_attribute *attr, char *buf) | |
1046 | { | |
1047 | struct nvdimm *nvdimm = to_nvdimm(dev); | |
1048 | ||
1049 | return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm)); | |
1050 | } | |
1051 | static DEVICE_ATTR_RO(formats); | |
1052 | ||
e6dfb2de DW |
1053 | static ssize_t serial_show(struct device *dev, |
1054 | struct device_attribute *attr, char *buf) | |
1055 | { | |
1056 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
1057 | ||
5ad9a7fd | 1058 | return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number)); |
e6dfb2de DW |
1059 | } |
1060 | static DEVICE_ATTR_RO(serial); | |
1061 | ||
a94e3fbe DW |
1062 | static ssize_t family_show(struct device *dev, |
1063 | struct device_attribute *attr, char *buf) | |
1064 | { | |
1065 | struct nvdimm *nvdimm = to_nvdimm(dev); | |
1066 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
1067 | ||
1068 | if (nfit_mem->family < 0) | |
1069 | return -ENXIO; | |
1070 | return sprintf(buf, "%d\n", nfit_mem->family); | |
1071 | } | |
1072 | static DEVICE_ATTR_RO(family); | |
1073 | ||
1074 | static ssize_t dsm_mask_show(struct device *dev, | |
1075 | struct device_attribute *attr, char *buf) | |
1076 | { | |
1077 | struct nvdimm *nvdimm = to_nvdimm(dev); | |
1078 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
1079 | ||
1080 | if (nfit_mem->family < 0) | |
1081 | return -ENXIO; | |
1082 | return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask); | |
1083 | } | |
1084 | static DEVICE_ATTR_RO(dsm_mask); | |
1085 | ||
58138820 DW |
1086 | static ssize_t flags_show(struct device *dev, |
1087 | struct device_attribute *attr, char *buf) | |
1088 | { | |
1089 | u16 flags = to_nfit_memdev(dev)->flags; | |
1090 | ||
1091 | return sprintf(buf, "%s%s%s%s%s\n", | |
402bae59 TK |
1092 | flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "", |
1093 | flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "", | |
1094 | flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "", | |
ca321d1c | 1095 | flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "", |
402bae59 | 1096 | flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : ""); |
58138820 DW |
1097 | } |
1098 | static DEVICE_ATTR_RO(flags); | |
1099 | ||
38a879ba TK |
1100 | static ssize_t id_show(struct device *dev, |
1101 | struct device_attribute *attr, char *buf) | |
1102 | { | |
1103 | struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev); | |
1104 | ||
1105 | if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID) | |
1106 | return sprintf(buf, "%04x-%02x-%04x-%08x\n", | |
1107 | be16_to_cpu(dcr->vendor_id), | |
1108 | dcr->manufacturing_location, | |
1109 | be16_to_cpu(dcr->manufacturing_date), | |
1110 | be32_to_cpu(dcr->serial_number)); | |
1111 | else | |
1112 | return sprintf(buf, "%04x-%08x\n", | |
1113 | be16_to_cpu(dcr->vendor_id), | |
1114 | be32_to_cpu(dcr->serial_number)); | |
1115 | } | |
1116 | static DEVICE_ATTR_RO(id); | |
1117 | ||
e6dfb2de DW |
1118 | static struct attribute *acpi_nfit_dimm_attributes[] = { |
1119 | &dev_attr_handle.attr, | |
1120 | &dev_attr_phys_id.attr, | |
1121 | &dev_attr_vendor.attr, | |
1122 | &dev_attr_device.attr, | |
6ca72085 DW |
1123 | &dev_attr_rev_id.attr, |
1124 | &dev_attr_subsystem_vendor.attr, | |
1125 | &dev_attr_subsystem_device.attr, | |
1126 | &dev_attr_subsystem_rev_id.attr, | |
e6dfb2de | 1127 | &dev_attr_format.attr, |
8cc6ddfc DW |
1128 | &dev_attr_formats.attr, |
1129 | &dev_attr_format1.attr, | |
e6dfb2de | 1130 | &dev_attr_serial.attr, |
58138820 | 1131 | &dev_attr_flags.attr, |
38a879ba | 1132 | &dev_attr_id.attr, |
a94e3fbe DW |
1133 | &dev_attr_family.attr, |
1134 | &dev_attr_dsm_mask.attr, | |
e6dfb2de DW |
1135 | NULL, |
1136 | }; | |
1137 | ||
1138 | static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj, | |
1139 | struct attribute *a, int n) | |
1140 | { | |
1141 | struct device *dev = container_of(kobj, struct device, kobj); | |
8cc6ddfc | 1142 | struct nvdimm *nvdimm = to_nvdimm(dev); |
e6dfb2de | 1143 | |
8cc6ddfc DW |
1144 | if (!to_nfit_dcr(dev)) |
1145 | return 0; | |
1146 | if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1) | |
e6dfb2de | 1147 | return 0; |
8cc6ddfc | 1148 | return a->mode; |
e6dfb2de DW |
1149 | } |
1150 | ||
1151 | static struct attribute_group acpi_nfit_dimm_attribute_group = { | |
1152 | .name = "nfit", | |
1153 | .attrs = acpi_nfit_dimm_attributes, | |
1154 | .is_visible = acpi_nfit_dimm_attr_visible, | |
1155 | }; | |
1156 | ||
1157 | static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = { | |
62232e45 | 1158 | &nvdimm_attribute_group, |
4d88a97a | 1159 | &nd_device_attribute_group, |
e6dfb2de DW |
1160 | &acpi_nfit_dimm_attribute_group, |
1161 | NULL, | |
1162 | }; | |
1163 | ||
1164 | static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc, | |
1165 | u32 device_handle) | |
1166 | { | |
1167 | struct nfit_mem *nfit_mem; | |
1168 | ||
1169 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) | |
1170 | if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle) | |
1171 | return nfit_mem->nvdimm; | |
1172 | ||
1173 | return NULL; | |
1174 | } | |
1175 | ||
62232e45 DW |
1176 | static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc, |
1177 | struct nfit_mem *nfit_mem, u32 device_handle) | |
1178 | { | |
1179 | struct acpi_device *adev, *adev_dimm; | |
1180 | struct device *dev = acpi_desc->dev; | |
31eca76b DW |
1181 | unsigned long dsm_mask; |
1182 | const u8 *uuid; | |
60e95f43 | 1183 | int i; |
62232e45 | 1184 | |
e3654eca DW |
1185 | /* nfit test assumes 1:1 relationship between commands and dsms */ |
1186 | nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en; | |
31eca76b | 1187 | nfit_mem->family = NVDIMM_FAMILY_INTEL; |
62232e45 DW |
1188 | adev = to_acpi_dev(acpi_desc); |
1189 | if (!adev) | |
1190 | return 0; | |
1191 | ||
1192 | adev_dimm = acpi_find_child_device(adev, device_handle, false); | |
1193 | nfit_mem->adev = adev_dimm; | |
1194 | if (!adev_dimm) { | |
1195 | dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n", | |
1196 | device_handle); | |
4d88a97a | 1197 | return force_enable_dimms ? 0 : -ENODEV; |
62232e45 DW |
1198 | } |
1199 | ||
31eca76b | 1200 | /* |
e02fb726 | 1201 | * Until standardization materializes we need to consider 4 |
31eca76b DW |
1202 | * different command sets. Note, that checking for function0 (bit0) |
1203 | * tells us if any commands are reachable through this uuid. | |
1204 | */ | |
e02fb726 | 1205 | for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++) |
31eca76b DW |
1206 | if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1)) |
1207 | break; | |
1208 | ||
1209 | /* limit the supported commands to those that are publicly documented */ | |
1210 | nfit_mem->family = i; | |
87554098 | 1211 | if (nfit_mem->family == NVDIMM_FAMILY_INTEL) { |
31eca76b | 1212 | dsm_mask = 0x3fe; |
87554098 DW |
1213 | if (disable_vendor_specific) |
1214 | dsm_mask &= ~(1 << ND_CMD_VENDOR); | |
e02fb726 | 1215 | } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) { |
31eca76b | 1216 | dsm_mask = 0x1c3c76; |
e02fb726 | 1217 | } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) { |
31eca76b | 1218 | dsm_mask = 0x1fe; |
87554098 DW |
1219 | if (disable_vendor_specific) |
1220 | dsm_mask &= ~(1 << 8); | |
e02fb726 | 1221 | } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) { |
1222 | dsm_mask = 0xffffffff; | |
87554098 | 1223 | } else { |
31eca76b DW |
1224 | dev_err(dev, "unknown dimm command family\n"); |
1225 | nfit_mem->family = -1; | |
1226 | return force_enable_dimms ? 0 : -ENODEV; | |
1227 | } | |
1228 | ||
1229 | uuid = to_nfit_uuid(nfit_mem->family); | |
1230 | for_each_set_bit(i, &dsm_mask, BITS_PER_LONG) | |
62232e45 DW |
1231 | if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i)) |
1232 | set_bit(i, &nfit_mem->dsm_mask); | |
1233 | ||
60e95f43 | 1234 | return 0; |
62232e45 DW |
1235 | } |
1236 | ||
e6dfb2de DW |
1237 | static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc) |
1238 | { | |
1239 | struct nfit_mem *nfit_mem; | |
4d88a97a | 1240 | int dimm_count = 0; |
e6dfb2de DW |
1241 | |
1242 | list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) { | |
e5ae3b25 | 1243 | struct acpi_nfit_flush_address *flush; |
31eca76b | 1244 | unsigned long flags = 0, cmd_mask; |
e6dfb2de | 1245 | struct nvdimm *nvdimm; |
e6dfb2de | 1246 | u32 device_handle; |
58138820 | 1247 | u16 mem_flags; |
62232e45 | 1248 | int rc; |
e6dfb2de DW |
1249 | |
1250 | device_handle = __to_nfit_memdev(nfit_mem)->device_handle; | |
1251 | nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle); | |
1252 | if (nvdimm) { | |
20985164 | 1253 | dimm_count++; |
e6dfb2de DW |
1254 | continue; |
1255 | } | |
1256 | ||
1257 | if (nfit_mem->bdw && nfit_mem->memdev_pmem) | |
1258 | flags |= NDD_ALIASING; | |
1259 | ||
58138820 | 1260 | mem_flags = __to_nfit_memdev(nfit_mem)->flags; |
ca321d1c | 1261 | if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED) |
58138820 DW |
1262 | flags |= NDD_UNARMED; |
1263 | ||
62232e45 DW |
1264 | rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle); |
1265 | if (rc) | |
1266 | continue; | |
1267 | ||
e3654eca | 1268 | /* |
31eca76b DW |
1269 | * TODO: provide translation for non-NVDIMM_FAMILY_INTEL |
1270 | * devices (i.e. from nd_cmd to acpi_dsm) to standardize the | |
1271 | * userspace interface. | |
e3654eca | 1272 | */ |
31eca76b DW |
1273 | cmd_mask = 1UL << ND_CMD_CALL; |
1274 | if (nfit_mem->family == NVDIMM_FAMILY_INTEL) | |
1275 | cmd_mask |= nfit_mem->dsm_mask; | |
1276 | ||
e5ae3b25 DW |
1277 | flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush |
1278 | : NULL; | |
e6dfb2de | 1279 | nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem, |
62232e45 | 1280 | acpi_nfit_dimm_attribute_groups, |
e5ae3b25 DW |
1281 | flags, cmd_mask, flush ? flush->hint_count : 0, |
1282 | nfit_mem->flush_wpq); | |
e6dfb2de DW |
1283 | if (!nvdimm) |
1284 | return -ENOMEM; | |
1285 | ||
1286 | nfit_mem->nvdimm = nvdimm; | |
4d88a97a | 1287 | dimm_count++; |
58138820 DW |
1288 | |
1289 | if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0) | |
1290 | continue; | |
1291 | ||
402bae59 | 1292 | dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n", |
58138820 | 1293 | nvdimm_name(nvdimm), |
402bae59 TK |
1294 | mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "", |
1295 | mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"", | |
1296 | mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "", | |
ca321d1c | 1297 | mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : ""); |
58138820 | 1298 | |
e6dfb2de DW |
1299 | } |
1300 | ||
4d88a97a | 1301 | return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count); |
e6dfb2de DW |
1302 | } |
1303 | ||
62232e45 DW |
1304 | static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc) |
1305 | { | |
1306 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; | |
1307 | const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS); | |
1308 | struct acpi_device *adev; | |
1309 | int i; | |
1310 | ||
e3654eca | 1311 | nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en; |
62232e45 DW |
1312 | adev = to_acpi_dev(acpi_desc); |
1313 | if (!adev) | |
1314 | return; | |
1315 | ||
d4f32367 | 1316 | for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++) |
62232e45 | 1317 | if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i)) |
e3654eca | 1318 | set_bit(i, &nd_desc->cmd_mask); |
62232e45 DW |
1319 | } |
1320 | ||
1f7df6f8 DW |
1321 | static ssize_t range_index_show(struct device *dev, |
1322 | struct device_attribute *attr, char *buf) | |
1323 | { | |
1324 | struct nd_region *nd_region = to_nd_region(dev); | |
1325 | struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region); | |
1326 | ||
1327 | return sprintf(buf, "%d\n", nfit_spa->spa->range_index); | |
1328 | } | |
1329 | static DEVICE_ATTR_RO(range_index); | |
1330 | ||
1331 | static struct attribute *acpi_nfit_region_attributes[] = { | |
1332 | &dev_attr_range_index.attr, | |
1333 | NULL, | |
1334 | }; | |
1335 | ||
1336 | static struct attribute_group acpi_nfit_region_attribute_group = { | |
1337 | .name = "nfit", | |
1338 | .attrs = acpi_nfit_region_attributes, | |
1339 | }; | |
1340 | ||
1341 | static const struct attribute_group *acpi_nfit_region_attribute_groups[] = { | |
1342 | &nd_region_attribute_group, | |
1343 | &nd_mapping_attribute_group, | |
3d88002e | 1344 | &nd_device_attribute_group, |
74ae66c3 | 1345 | &nd_numa_attribute_group, |
1f7df6f8 DW |
1346 | &acpi_nfit_region_attribute_group, |
1347 | NULL, | |
1348 | }; | |
1349 | ||
eaf96153 DW |
1350 | /* enough info to uniquely specify an interleave set */ |
1351 | struct nfit_set_info { | |
1352 | struct nfit_set_info_map { | |
1353 | u64 region_offset; | |
1354 | u32 serial_number; | |
1355 | u32 pad; | |
1356 | } mapping[0]; | |
1357 | }; | |
1358 | ||
1359 | static size_t sizeof_nfit_set_info(int num_mappings) | |
1360 | { | |
1361 | return sizeof(struct nfit_set_info) | |
1362 | + num_mappings * sizeof(struct nfit_set_info_map); | |
1363 | } | |
1364 | ||
1365 | static int cmp_map(const void *m0, const void *m1) | |
1366 | { | |
1367 | const struct nfit_set_info_map *map0 = m0; | |
1368 | const struct nfit_set_info_map *map1 = m1; | |
1369 | ||
1370 | return memcmp(&map0->region_offset, &map1->region_offset, | |
1371 | sizeof(u64)); | |
1372 | } | |
1373 | ||
1374 | /* Retrieve the nth entry referencing this spa */ | |
1375 | static struct acpi_nfit_memory_map *memdev_from_spa( | |
1376 | struct acpi_nfit_desc *acpi_desc, u16 range_index, int n) | |
1377 | { | |
1378 | struct nfit_memdev *nfit_memdev; | |
1379 | ||
1380 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) | |
1381 | if (nfit_memdev->memdev->range_index == range_index) | |
1382 | if (n-- == 0) | |
1383 | return nfit_memdev->memdev; | |
1384 | return NULL; | |
1385 | } | |
1386 | ||
1387 | static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc, | |
1388 | struct nd_region_desc *ndr_desc, | |
1389 | struct acpi_nfit_system_address *spa) | |
1390 | { | |
1391 | int i, spa_type = nfit_spa_type(spa); | |
1392 | struct device *dev = acpi_desc->dev; | |
1393 | struct nd_interleave_set *nd_set; | |
1394 | u16 nr = ndr_desc->num_mappings; | |
1395 | struct nfit_set_info *info; | |
1396 | ||
1397 | if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE) | |
1398 | /* pass */; | |
1399 | else | |
1400 | return 0; | |
1401 | ||
1402 | nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL); | |
1403 | if (!nd_set) | |
1404 | return -ENOMEM; | |
1405 | ||
1406 | info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL); | |
1407 | if (!info) | |
1408 | return -ENOMEM; | |
1409 | for (i = 0; i < nr; i++) { | |
1410 | struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i]; | |
1411 | struct nfit_set_info_map *map = &info->mapping[i]; | |
1412 | struct nvdimm *nvdimm = nd_mapping->nvdimm; | |
1413 | struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm); | |
1414 | struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc, | |
1415 | spa->range_index, i); | |
1416 | ||
1417 | if (!memdev || !nfit_mem->dcr) { | |
1418 | dev_err(dev, "%s: failed to find DCR\n", __func__); | |
1419 | return -ENODEV; | |
1420 | } | |
1421 | ||
1422 | map->region_offset = memdev->region_offset; | |
1423 | map->serial_number = nfit_mem->dcr->serial_number; | |
1424 | } | |
1425 | ||
1426 | sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map), | |
1427 | cmp_map, NULL); | |
1428 | nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0); | |
1429 | ndr_desc->nd_set = nd_set; | |
1430 | devm_kfree(dev, info); | |
1431 | ||
1432 | return 0; | |
1433 | } | |
1434 | ||
047fc8a1 RZ |
1435 | static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio) |
1436 | { | |
1437 | struct acpi_nfit_interleave *idt = mmio->idt; | |
1438 | u32 sub_line_offset, line_index, line_offset; | |
1439 | u64 line_no, table_skip_count, table_offset; | |
1440 | ||
1441 | line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset); | |
1442 | table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index); | |
1443 | line_offset = idt->line_offset[line_index] | |
1444 | * mmio->line_size; | |
1445 | table_offset = table_skip_count * mmio->table_size; | |
1446 | ||
1447 | return mmio->base_offset + line_offset + table_offset + sub_line_offset; | |
1448 | } | |
1449 | ||
de4a196c | 1450 | static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw) |
047fc8a1 RZ |
1451 | { |
1452 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR]; | |
1453 | u64 offset = nfit_blk->stat_offset + mmio->size * bw; | |
1454 | ||
1455 | if (mmio->num_lines) | |
1456 | offset = to_interleave_offset(offset, mmio); | |
1457 | ||
12f03ee6 | 1458 | return readl(mmio->addr.base + offset); |
047fc8a1 RZ |
1459 | } |
1460 | ||
1461 | static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw, | |
1462 | resource_size_t dpa, unsigned int len, unsigned int write) | |
1463 | { | |
1464 | u64 cmd, offset; | |
1465 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR]; | |
1466 | ||
1467 | enum { | |
1468 | BCW_OFFSET_MASK = (1ULL << 48)-1, | |
1469 | BCW_LEN_SHIFT = 48, | |
1470 | BCW_LEN_MASK = (1ULL << 8) - 1, | |
1471 | BCW_CMD_SHIFT = 56, | |
1472 | }; | |
1473 | ||
1474 | cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK; | |
1475 | len = len >> L1_CACHE_SHIFT; | |
1476 | cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT; | |
1477 | cmd |= ((u64) write) << BCW_CMD_SHIFT; | |
1478 | ||
1479 | offset = nfit_blk->cmd_offset + mmio->size * bw; | |
1480 | if (mmio->num_lines) | |
1481 | offset = to_interleave_offset(offset, mmio); | |
1482 | ||
67a3e8fe | 1483 | writeq(cmd, mmio->addr.base + offset); |
f284a4f2 | 1484 | nvdimm_flush(nfit_blk->nd_region); |
f0f2c072 | 1485 | |
aef25338 | 1486 | if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH) |
67a3e8fe | 1487 | readq(mmio->addr.base + offset); |
047fc8a1 RZ |
1488 | } |
1489 | ||
1490 | static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk, | |
1491 | resource_size_t dpa, void *iobuf, size_t len, int rw, | |
1492 | unsigned int lane) | |
1493 | { | |
1494 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW]; | |
1495 | unsigned int copied = 0; | |
1496 | u64 base_offset; | |
1497 | int rc; | |
1498 | ||
1499 | base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES | |
1500 | + lane * mmio->size; | |
047fc8a1 RZ |
1501 | write_blk_ctl(nfit_blk, lane, dpa, len, rw); |
1502 | while (len) { | |
1503 | unsigned int c; | |
1504 | u64 offset; | |
1505 | ||
1506 | if (mmio->num_lines) { | |
1507 | u32 line_offset; | |
1508 | ||
1509 | offset = to_interleave_offset(base_offset + copied, | |
1510 | mmio); | |
1511 | div_u64_rem(offset, mmio->line_size, &line_offset); | |
1512 | c = min_t(size_t, len, mmio->line_size - line_offset); | |
1513 | } else { | |
1514 | offset = base_offset + nfit_blk->bdw_offset; | |
1515 | c = len; | |
1516 | } | |
1517 | ||
1518 | if (rw) | |
67a3e8fe | 1519 | memcpy_to_pmem(mmio->addr.aperture + offset, |
c2ad2954 | 1520 | iobuf + copied, c); |
67a3e8fe | 1521 | else { |
aef25338 | 1522 | if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH) |
67a3e8fe RZ |
1523 | mmio_flush_range((void __force *) |
1524 | mmio->addr.aperture + offset, c); | |
1525 | ||
c2ad2954 | 1526 | memcpy_from_pmem(iobuf + copied, |
67a3e8fe RZ |
1527 | mmio->addr.aperture + offset, c); |
1528 | } | |
047fc8a1 RZ |
1529 | |
1530 | copied += c; | |
1531 | len -= c; | |
1532 | } | |
c2ad2954 RZ |
1533 | |
1534 | if (rw) | |
f284a4f2 | 1535 | nvdimm_flush(nfit_blk->nd_region); |
c2ad2954 | 1536 | |
047fc8a1 RZ |
1537 | rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0; |
1538 | return rc; | |
1539 | } | |
1540 | ||
1541 | static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr, | |
1542 | resource_size_t dpa, void *iobuf, u64 len, int rw) | |
1543 | { | |
1544 | struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr); | |
1545 | struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW]; | |
1546 | struct nd_region *nd_region = nfit_blk->nd_region; | |
1547 | unsigned int lane, copied = 0; | |
1548 | int rc = 0; | |
1549 | ||
1550 | lane = nd_region_acquire_lane(nd_region); | |
1551 | while (len) { | |
1552 | u64 c = min(len, mmio->size); | |
1553 | ||
1554 | rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied, | |
1555 | iobuf + copied, c, rw, lane); | |
1556 | if (rc) | |
1557 | break; | |
1558 | ||
1559 | copied += c; | |
1560 | len -= c; | |
1561 | } | |
1562 | nd_region_release_lane(nd_region, lane); | |
1563 | ||
1564 | return rc; | |
1565 | } | |
1566 | ||
047fc8a1 RZ |
1567 | static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio, |
1568 | struct acpi_nfit_interleave *idt, u16 interleave_ways) | |
1569 | { | |
1570 | if (idt) { | |
1571 | mmio->num_lines = idt->line_count; | |
1572 | mmio->line_size = idt->line_size; | |
1573 | if (interleave_ways == 0) | |
1574 | return -ENXIO; | |
1575 | mmio->table_size = mmio->num_lines * interleave_ways | |
1576 | * mmio->line_size; | |
1577 | } | |
1578 | ||
1579 | return 0; | |
1580 | } | |
1581 | ||
f0f2c072 RZ |
1582 | static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc, |
1583 | struct nvdimm *nvdimm, struct nfit_blk *nfit_blk) | |
1584 | { | |
1585 | struct nd_cmd_dimm_flags flags; | |
1586 | int rc; | |
1587 | ||
1588 | memset(&flags, 0, sizeof(flags)); | |
1589 | rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags, | |
aef25338 | 1590 | sizeof(flags), NULL); |
f0f2c072 RZ |
1591 | |
1592 | if (rc >= 0 && flags.status == 0) | |
1593 | nfit_blk->dimm_flags = flags.flags; | |
1594 | else if (rc == -ENOTTY) { | |
1595 | /* fall back to a conservative default */ | |
aef25338 | 1596 | nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH; |
f0f2c072 RZ |
1597 | rc = 0; |
1598 | } else | |
1599 | rc = -ENXIO; | |
1600 | ||
1601 | return rc; | |
1602 | } | |
1603 | ||
047fc8a1 RZ |
1604 | static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus, |
1605 | struct device *dev) | |
1606 | { | |
1607 | struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus); | |
047fc8a1 RZ |
1608 | struct nd_blk_region *ndbr = to_nd_blk_region(dev); |
1609 | struct nfit_blk_mmio *mmio; | |
1610 | struct nfit_blk *nfit_blk; | |
1611 | struct nfit_mem *nfit_mem; | |
1612 | struct nvdimm *nvdimm; | |
1613 | int rc; | |
1614 | ||
1615 | nvdimm = nd_blk_region_to_dimm(ndbr); | |
1616 | nfit_mem = nvdimm_provider_data(nvdimm); | |
1617 | if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) { | |
1618 | dev_dbg(dev, "%s: missing%s%s%s\n", __func__, | |
1619 | nfit_mem ? "" : " nfit_mem", | |
193ccca4 DW |
1620 | (nfit_mem && nfit_mem->dcr) ? "" : " dcr", |
1621 | (nfit_mem && nfit_mem->bdw) ? "" : " bdw"); | |
047fc8a1 RZ |
1622 | return -ENXIO; |
1623 | } | |
1624 | ||
1625 | nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL); | |
1626 | if (!nfit_blk) | |
1627 | return -ENOMEM; | |
1628 | nd_blk_region_set_provider_data(ndbr, nfit_blk); | |
1629 | nfit_blk->nd_region = to_nd_region(dev); | |
1630 | ||
1631 | /* map block aperture memory */ | |
1632 | nfit_blk->bdw_offset = nfit_mem->bdw->offset; | |
1633 | mmio = &nfit_blk->mmio[BDW]; | |
29b9aa0a DW |
1634 | mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address, |
1635 | nfit_mem->spa_bdw->length, ARCH_MEMREMAP_PMEM); | |
67a3e8fe | 1636 | if (!mmio->addr.base) { |
047fc8a1 RZ |
1637 | dev_dbg(dev, "%s: %s failed to map bdw\n", __func__, |
1638 | nvdimm_name(nvdimm)); | |
1639 | return -ENOMEM; | |
1640 | } | |
1641 | mmio->size = nfit_mem->bdw->size; | |
1642 | mmio->base_offset = nfit_mem->memdev_bdw->region_offset; | |
1643 | mmio->idt = nfit_mem->idt_bdw; | |
1644 | mmio->spa = nfit_mem->spa_bdw; | |
1645 | rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw, | |
1646 | nfit_mem->memdev_bdw->interleave_ways); | |
1647 | if (rc) { | |
1648 | dev_dbg(dev, "%s: %s failed to init bdw interleave\n", | |
1649 | __func__, nvdimm_name(nvdimm)); | |
1650 | return rc; | |
1651 | } | |
1652 | ||
1653 | /* map block control memory */ | |
1654 | nfit_blk->cmd_offset = nfit_mem->dcr->command_offset; | |
1655 | nfit_blk->stat_offset = nfit_mem->dcr->status_offset; | |
1656 | mmio = &nfit_blk->mmio[DCR]; | |
29b9aa0a DW |
1657 | mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address, |
1658 | nfit_mem->spa_dcr->length); | |
67a3e8fe | 1659 | if (!mmio->addr.base) { |
047fc8a1 RZ |
1660 | dev_dbg(dev, "%s: %s failed to map dcr\n", __func__, |
1661 | nvdimm_name(nvdimm)); | |
1662 | return -ENOMEM; | |
1663 | } | |
1664 | mmio->size = nfit_mem->dcr->window_size; | |
1665 | mmio->base_offset = nfit_mem->memdev_dcr->region_offset; | |
1666 | mmio->idt = nfit_mem->idt_dcr; | |
1667 | mmio->spa = nfit_mem->spa_dcr; | |
1668 | rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr, | |
1669 | nfit_mem->memdev_dcr->interleave_ways); | |
1670 | if (rc) { | |
1671 | dev_dbg(dev, "%s: %s failed to init dcr interleave\n", | |
1672 | __func__, nvdimm_name(nvdimm)); | |
1673 | return rc; | |
1674 | } | |
1675 | ||
f0f2c072 RZ |
1676 | rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk); |
1677 | if (rc < 0) { | |
1678 | dev_dbg(dev, "%s: %s failed get DIMM flags\n", | |
1679 | __func__, nvdimm_name(nvdimm)); | |
1680 | return rc; | |
1681 | } | |
1682 | ||
f284a4f2 | 1683 | if (nvdimm_has_flush(nfit_blk->nd_region) < 0) |
c2ad2954 RZ |
1684 | dev_warn(dev, "unable to guarantee persistence of writes\n"); |
1685 | ||
047fc8a1 RZ |
1686 | if (mmio->line_size == 0) |
1687 | return 0; | |
1688 | ||
1689 | if ((u32) nfit_blk->cmd_offset % mmio->line_size | |
1690 | + 8 > mmio->line_size) { | |
1691 | dev_dbg(dev, "cmd_offset crosses interleave boundary\n"); | |
1692 | return -ENXIO; | |
1693 | } else if ((u32) nfit_blk->stat_offset % mmio->line_size | |
1694 | + 8 > mmio->line_size) { | |
1695 | dev_dbg(dev, "stat_offset crosses interleave boundary\n"); | |
1696 | return -ENXIO; | |
1697 | } | |
1698 | ||
1699 | return 0; | |
1700 | } | |
1701 | ||
aef25338 | 1702 | static int ars_get_cap(struct acpi_nfit_desc *acpi_desc, |
1cf03c00 | 1703 | struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa) |
0caeef63 | 1704 | { |
aef25338 | 1705 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; |
1cf03c00 | 1706 | struct acpi_nfit_system_address *spa = nfit_spa->spa; |
aef25338 DW |
1707 | int cmd_rc, rc; |
1708 | ||
1cf03c00 DW |
1709 | cmd->address = spa->address; |
1710 | cmd->length = spa->length; | |
aef25338 DW |
1711 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd, |
1712 | sizeof(*cmd), &cmd_rc); | |
1713 | if (rc < 0) | |
1714 | return rc; | |
1cf03c00 | 1715 | return cmd_rc; |
0caeef63 VV |
1716 | } |
1717 | ||
1cf03c00 | 1718 | static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa) |
0caeef63 VV |
1719 | { |
1720 | int rc; | |
1cf03c00 DW |
1721 | int cmd_rc; |
1722 | struct nd_cmd_ars_start ars_start; | |
1723 | struct acpi_nfit_system_address *spa = nfit_spa->spa; | |
1724 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; | |
0caeef63 | 1725 | |
1cf03c00 DW |
1726 | memset(&ars_start, 0, sizeof(ars_start)); |
1727 | ars_start.address = spa->address; | |
1728 | ars_start.length = spa->length; | |
1729 | if (nfit_spa_type(spa) == NFIT_SPA_PM) | |
1730 | ars_start.type = ND_ARS_PERSISTENT; | |
1731 | else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) | |
1732 | ars_start.type = ND_ARS_VOLATILE; | |
1733 | else | |
1734 | return -ENOTTY; | |
aef25338 | 1735 | |
1cf03c00 DW |
1736 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start, |
1737 | sizeof(ars_start), &cmd_rc); | |
aef25338 | 1738 | |
1cf03c00 DW |
1739 | if (rc < 0) |
1740 | return rc; | |
1741 | return cmd_rc; | |
0caeef63 VV |
1742 | } |
1743 | ||
1cf03c00 | 1744 | static int ars_continue(struct acpi_nfit_desc *acpi_desc) |
0caeef63 | 1745 | { |
aef25338 | 1746 | int rc, cmd_rc; |
1cf03c00 DW |
1747 | struct nd_cmd_ars_start ars_start; |
1748 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; | |
1749 | struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status; | |
1750 | ||
1751 | memset(&ars_start, 0, sizeof(ars_start)); | |
1752 | ars_start.address = ars_status->restart_address; | |
1753 | ars_start.length = ars_status->restart_length; | |
1754 | ars_start.type = ars_status->type; | |
1755 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start, | |
1756 | sizeof(ars_start), &cmd_rc); | |
1757 | if (rc < 0) | |
1758 | return rc; | |
1759 | return cmd_rc; | |
1760 | } | |
0caeef63 | 1761 | |
1cf03c00 DW |
1762 | static int ars_get_status(struct acpi_nfit_desc *acpi_desc) |
1763 | { | |
1764 | struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc; | |
1765 | struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status; | |
1766 | int rc, cmd_rc; | |
aef25338 | 1767 | |
1cf03c00 DW |
1768 | rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status, |
1769 | acpi_desc->ars_status_size, &cmd_rc); | |
1770 | if (rc < 0) | |
1771 | return rc; | |
1772 | return cmd_rc; | |
0caeef63 VV |
1773 | } |
1774 | ||
1775 | static int ars_status_process_records(struct nvdimm_bus *nvdimm_bus, | |
1cf03c00 | 1776 | struct nd_cmd_ars_status *ars_status) |
0caeef63 VV |
1777 | { |
1778 | int rc; | |
1779 | u32 i; | |
1780 | ||
0caeef63 VV |
1781 | for (i = 0; i < ars_status->num_records; i++) { |
1782 | rc = nvdimm_bus_add_poison(nvdimm_bus, | |
1783 | ars_status->records[i].err_address, | |
1784 | ars_status->records[i].length); | |
1785 | if (rc) | |
1786 | return rc; | |
1787 | } | |
1788 | ||
1789 | return 0; | |
1790 | } | |
1791 | ||
af1996ef TK |
1792 | static void acpi_nfit_remove_resource(void *data) |
1793 | { | |
1794 | struct resource *res = data; | |
1795 | ||
1796 | remove_resource(res); | |
1797 | } | |
1798 | ||
1799 | static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc, | |
1800 | struct nd_region_desc *ndr_desc) | |
1801 | { | |
1802 | struct resource *res, *nd_res = ndr_desc->res; | |
1803 | int is_pmem, ret; | |
1804 | ||
1805 | /* No operation if the region is already registered as PMEM */ | |
1806 | is_pmem = region_intersects(nd_res->start, resource_size(nd_res), | |
1807 | IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY); | |
1808 | if (is_pmem == REGION_INTERSECTS) | |
1809 | return 0; | |
1810 | ||
1811 | res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL); | |
1812 | if (!res) | |
1813 | return -ENOMEM; | |
1814 | ||
1815 | res->name = "Persistent Memory"; | |
1816 | res->start = nd_res->start; | |
1817 | res->end = nd_res->end; | |
1818 | res->flags = IORESOURCE_MEM; | |
1819 | res->desc = IORES_DESC_PERSISTENT_MEMORY; | |
1820 | ||
1821 | ret = insert_resource(&iomem_resource, res); | |
1822 | if (ret) | |
1823 | return ret; | |
1824 | ||
d932dd2c SV |
1825 | ret = devm_add_action_or_reset(acpi_desc->dev, |
1826 | acpi_nfit_remove_resource, | |
1827 | res); | |
1828 | if (ret) | |
af1996ef | 1829 | return ret; |
af1996ef TK |
1830 | |
1831 | return 0; | |
1832 | } | |
1833 | ||
1f7df6f8 DW |
1834 | static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc, |
1835 | struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc, | |
1836 | struct acpi_nfit_memory_map *memdev, | |
1cf03c00 | 1837 | struct nfit_spa *nfit_spa) |
1f7df6f8 DW |
1838 | { |
1839 | struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, | |
1840 | memdev->device_handle); | |
1cf03c00 | 1841 | struct acpi_nfit_system_address *spa = nfit_spa->spa; |
047fc8a1 | 1842 | struct nd_blk_region_desc *ndbr_desc; |
1f7df6f8 DW |
1843 | struct nfit_mem *nfit_mem; |
1844 | int blk_valid = 0; | |
1845 | ||
1846 | if (!nvdimm) { | |
1847 | dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n", | |
1848 | spa->range_index, memdev->device_handle); | |
1849 | return -ENODEV; | |
1850 | } | |
1851 | ||
1852 | nd_mapping->nvdimm = nvdimm; | |
1853 | switch (nfit_spa_type(spa)) { | |
1854 | case NFIT_SPA_PM: | |
1855 | case NFIT_SPA_VOLATILE: | |
1856 | nd_mapping->start = memdev->address; | |
1857 | nd_mapping->size = memdev->region_size; | |
1858 | break; | |
1859 | case NFIT_SPA_DCR: | |
1860 | nfit_mem = nvdimm_provider_data(nvdimm); | |
1861 | if (!nfit_mem || !nfit_mem->bdw) { | |
1862 | dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n", | |
1863 | spa->range_index, nvdimm_name(nvdimm)); | |
1864 | } else { | |
1865 | nd_mapping->size = nfit_mem->bdw->capacity; | |
1866 | nd_mapping->start = nfit_mem->bdw->start_address; | |
5212e11f | 1867 | ndr_desc->num_lanes = nfit_mem->bdw->windows; |
1f7df6f8 DW |
1868 | blk_valid = 1; |
1869 | } | |
1870 | ||
1871 | ndr_desc->nd_mapping = nd_mapping; | |
1872 | ndr_desc->num_mappings = blk_valid; | |
047fc8a1 RZ |
1873 | ndbr_desc = to_blk_region_desc(ndr_desc); |
1874 | ndbr_desc->enable = acpi_nfit_blk_region_enable; | |
6bc75619 | 1875 | ndbr_desc->do_io = acpi_desc->blk_do_io; |
1cf03c00 DW |
1876 | nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus, |
1877 | ndr_desc); | |
1878 | if (!nfit_spa->nd_region) | |
1f7df6f8 DW |
1879 | return -ENOMEM; |
1880 | break; | |
1881 | } | |
1882 | ||
1883 | return 0; | |
1884 | } | |
1885 | ||
c2f32acd LCY |
1886 | static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa) |
1887 | { | |
1888 | return (nfit_spa_type(spa) == NFIT_SPA_VDISK || | |
1889 | nfit_spa_type(spa) == NFIT_SPA_VCD || | |
1890 | nfit_spa_type(spa) == NFIT_SPA_PDISK || | |
1891 | nfit_spa_type(spa) == NFIT_SPA_PCD); | |
1892 | } | |
1893 | ||
1f7df6f8 DW |
1894 | static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc, |
1895 | struct nfit_spa *nfit_spa) | |
1896 | { | |
1897 | static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS]; | |
1898 | struct acpi_nfit_system_address *spa = nfit_spa->spa; | |
047fc8a1 RZ |
1899 | struct nd_blk_region_desc ndbr_desc; |
1900 | struct nd_region_desc *ndr_desc; | |
1f7df6f8 | 1901 | struct nfit_memdev *nfit_memdev; |
1f7df6f8 DW |
1902 | struct nvdimm_bus *nvdimm_bus; |
1903 | struct resource res; | |
eaf96153 | 1904 | int count = 0, rc; |
1f7df6f8 | 1905 | |
1cf03c00 | 1906 | if (nfit_spa->nd_region) |
20985164 VV |
1907 | return 0; |
1908 | ||
c2f32acd | 1909 | if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) { |
1f7df6f8 DW |
1910 | dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n", |
1911 | __func__); | |
1912 | return 0; | |
1913 | } | |
1914 | ||
1915 | memset(&res, 0, sizeof(res)); | |
1916 | memset(&nd_mappings, 0, sizeof(nd_mappings)); | |
047fc8a1 | 1917 | memset(&ndbr_desc, 0, sizeof(ndbr_desc)); |
1f7df6f8 DW |
1918 | res.start = spa->address; |
1919 | res.end = res.start + spa->length - 1; | |
047fc8a1 RZ |
1920 | ndr_desc = &ndbr_desc.ndr_desc; |
1921 | ndr_desc->res = &res; | |
1922 | ndr_desc->provider_data = nfit_spa; | |
1923 | ndr_desc->attr_groups = acpi_nfit_region_attribute_groups; | |
41d7a6d6 TK |
1924 | if (spa->flags & ACPI_NFIT_PROXIMITY_VALID) |
1925 | ndr_desc->numa_node = acpi_map_pxm_to_online_node( | |
1926 | spa->proximity_domain); | |
1927 | else | |
1928 | ndr_desc->numa_node = NUMA_NO_NODE; | |
1929 | ||
1f7df6f8 DW |
1930 | list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) { |
1931 | struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev; | |
1932 | struct nd_mapping *nd_mapping; | |
1f7df6f8 DW |
1933 | |
1934 | if (memdev->range_index != spa->range_index) | |
1935 | continue; | |
1936 | if (count >= ND_MAX_MAPPINGS) { | |
1937 | dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n", | |
1938 | spa->range_index, ND_MAX_MAPPINGS); | |
1939 | return -ENXIO; | |
1940 | } | |
1941 | nd_mapping = &nd_mappings[count++]; | |
047fc8a1 | 1942 | rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc, |
1cf03c00 | 1943 | memdev, nfit_spa); |
1f7df6f8 | 1944 | if (rc) |
1cf03c00 | 1945 | goto out; |
1f7df6f8 DW |
1946 | } |
1947 | ||
047fc8a1 RZ |
1948 | ndr_desc->nd_mapping = nd_mappings; |
1949 | ndr_desc->num_mappings = count; | |
1950 | rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa); | |
eaf96153 | 1951 | if (rc) |
1cf03c00 | 1952 | goto out; |
eaf96153 | 1953 | |
1f7df6f8 DW |
1954 | nvdimm_bus = acpi_desc->nvdimm_bus; |
1955 | if (nfit_spa_type(spa) == NFIT_SPA_PM) { | |
af1996ef | 1956 | rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc); |
48901165 | 1957 | if (rc) { |
af1996ef TK |
1958 | dev_warn(acpi_desc->dev, |
1959 | "failed to insert pmem resource to iomem: %d\n", | |
1960 | rc); | |
48901165 | 1961 | goto out; |
0caeef63 | 1962 | } |
48901165 | 1963 | |
1cf03c00 DW |
1964 | nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus, |
1965 | ndr_desc); | |
1966 | if (!nfit_spa->nd_region) | |
1967 | rc = -ENOMEM; | |
1f7df6f8 | 1968 | } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) { |
1cf03c00 DW |
1969 | nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus, |
1970 | ndr_desc); | |
1971 | if (!nfit_spa->nd_region) | |
1972 | rc = -ENOMEM; | |
c2f32acd LCY |
1973 | } else if (nfit_spa_is_virtual(spa)) { |
1974 | nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus, | |
1975 | ndr_desc); | |
1976 | if (!nfit_spa->nd_region) | |
1977 | rc = -ENOMEM; | |
1f7df6f8 | 1978 | } |
20985164 | 1979 | |
1cf03c00 DW |
1980 | out: |
1981 | if (rc) | |
1982 | dev_err(acpi_desc->dev, "failed to register spa range %d\n", | |
1983 | nfit_spa->spa->range_index); | |
1984 | return rc; | |
1985 | } | |
1986 | ||
1987 | static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc, | |
1988 | u32 max_ars) | |
1989 | { | |
1990 | struct device *dev = acpi_desc->dev; | |
1991 | struct nd_cmd_ars_status *ars_status; | |
1992 | ||
1993 | if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) { | |
1994 | memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size); | |
1995 | return 0; | |
1996 | } | |
1997 | ||
1998 | if (acpi_desc->ars_status) | |
1999 | devm_kfree(dev, acpi_desc->ars_status); | |
2000 | acpi_desc->ars_status = NULL; | |
2001 | ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL); | |
2002 | if (!ars_status) | |
2003 | return -ENOMEM; | |
2004 | acpi_desc->ars_status = ars_status; | |
2005 | acpi_desc->ars_status_size = max_ars; | |
1f7df6f8 DW |
2006 | return 0; |
2007 | } | |
2008 | ||
1cf03c00 DW |
2009 | static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc, |
2010 | struct nfit_spa *nfit_spa) | |
2011 | { | |
2012 | struct acpi_nfit_system_address *spa = nfit_spa->spa; | |
2013 | int rc; | |
2014 | ||
2015 | if (!nfit_spa->max_ars) { | |
2016 | struct nd_cmd_ars_cap ars_cap; | |
2017 | ||
2018 | memset(&ars_cap, 0, sizeof(ars_cap)); | |
2019 | rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa); | |
2020 | if (rc < 0) | |
2021 | return rc; | |
2022 | nfit_spa->max_ars = ars_cap.max_ars_out; | |
2023 | nfit_spa->clear_err_unit = ars_cap.clear_err_unit; | |
2024 | /* check that the supported scrub types match the spa type */ | |
2025 | if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE && | |
2026 | ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0) | |
2027 | return -ENOTTY; | |
2028 | else if (nfit_spa_type(spa) == NFIT_SPA_PM && | |
2029 | ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0) | |
2030 | return -ENOTTY; | |
2031 | } | |
2032 | ||
2033 | if (ars_status_alloc(acpi_desc, nfit_spa->max_ars)) | |
2034 | return -ENOMEM; | |
2035 | ||
2036 | rc = ars_get_status(acpi_desc); | |
2037 | if (rc < 0 && rc != -ENOSPC) | |
2038 | return rc; | |
2039 | ||
2040 | if (ars_status_process_records(acpi_desc->nvdimm_bus, | |
2041 | acpi_desc->ars_status)) | |
2042 | return -ENOMEM; | |
2043 | ||
2044 | return 0; | |
2045 | } | |
2046 | ||
2047 | static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc, | |
2048 | struct nfit_spa *nfit_spa) | |
2049 | { | |
2050 | struct acpi_nfit_system_address *spa = nfit_spa->spa; | |
2051 | unsigned int overflow_retry = scrub_overflow_abort; | |
2052 | u64 init_ars_start = 0, init_ars_len = 0; | |
2053 | struct device *dev = acpi_desc->dev; | |
2054 | unsigned int tmo = scrub_timeout; | |
2055 | int rc; | |
2056 | ||
2057 | if (nfit_spa->ars_done || !nfit_spa->nd_region) | |
2058 | return; | |
2059 | ||
2060 | rc = ars_start(acpi_desc, nfit_spa); | |
2061 | /* | |
2062 | * If we timed out the initial scan we'll still be busy here, | |
2063 | * and will wait another timeout before giving up permanently. | |
2064 | */ | |
2065 | if (rc < 0 && rc != -EBUSY) | |
2066 | return; | |
2067 | ||
2068 | do { | |
2069 | u64 ars_start, ars_len; | |
2070 | ||
2071 | if (acpi_desc->cancel) | |
2072 | break; | |
2073 | rc = acpi_nfit_query_poison(acpi_desc, nfit_spa); | |
2074 | if (rc == -ENOTTY) | |
2075 | break; | |
2076 | if (rc == -EBUSY && !tmo) { | |
2077 | dev_warn(dev, "range %d ars timeout, aborting\n", | |
2078 | spa->range_index); | |
2079 | break; | |
2080 | } | |
2081 | ||
2082 | if (rc == -EBUSY) { | |
2083 | /* | |
2084 | * Note, entries may be appended to the list | |
2085 | * while the lock is dropped, but the workqueue | |
2086 | * being active prevents entries being deleted / | |
2087 | * freed. | |
2088 | */ | |
2089 | mutex_unlock(&acpi_desc->init_mutex); | |
2090 | ssleep(1); | |
2091 | tmo--; | |
2092 | mutex_lock(&acpi_desc->init_mutex); | |
2093 | continue; | |
2094 | } | |
2095 | ||
2096 | /* we got some results, but there are more pending... */ | |
2097 | if (rc == -ENOSPC && overflow_retry--) { | |
2098 | if (!init_ars_len) { | |
2099 | init_ars_len = acpi_desc->ars_status->length; | |
2100 | init_ars_start = acpi_desc->ars_status->address; | |
2101 | } | |
2102 | rc = ars_continue(acpi_desc); | |
2103 | } | |
2104 | ||
2105 | if (rc < 0) { | |
2106 | dev_warn(dev, "range %d ars continuation failed\n", | |
2107 | spa->range_index); | |
2108 | break; | |
2109 | } | |
2110 | ||
2111 | if (init_ars_len) { | |
2112 | ars_start = init_ars_start; | |
2113 | ars_len = init_ars_len; | |
2114 | } else { | |
2115 | ars_start = acpi_desc->ars_status->address; | |
2116 | ars_len = acpi_desc->ars_status->length; | |
2117 | } | |
2118 | dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n", | |
2119 | spa->range_index, ars_start, ars_len); | |
2120 | /* notify the region about new poison entries */ | |
2121 | nvdimm_region_notify(nfit_spa->nd_region, | |
2122 | NVDIMM_REVALIDATE_POISON); | |
2123 | break; | |
2124 | } while (1); | |
2125 | } | |
2126 | ||
2127 | static void acpi_nfit_scrub(struct work_struct *work) | |
1f7df6f8 | 2128 | { |
1cf03c00 DW |
2129 | struct device *dev; |
2130 | u64 init_scrub_length = 0; | |
1f7df6f8 | 2131 | struct nfit_spa *nfit_spa; |
1cf03c00 DW |
2132 | u64 init_scrub_address = 0; |
2133 | bool init_ars_done = false; | |
2134 | struct acpi_nfit_desc *acpi_desc; | |
2135 | unsigned int tmo = scrub_timeout; | |
2136 | unsigned int overflow_retry = scrub_overflow_abort; | |
2137 | ||
2138 | acpi_desc = container_of(work, typeof(*acpi_desc), work); | |
2139 | dev = acpi_desc->dev; | |
1f7df6f8 | 2140 | |
1cf03c00 DW |
2141 | /* |
2142 | * We scrub in 2 phases. The first phase waits for any platform | |
2143 | * firmware initiated scrubs to complete and then we go search for the | |
2144 | * affected spa regions to mark them scanned. In the second phase we | |
2145 | * initiate a directed scrub for every range that was not scrubbed in | |
2146 | * phase 1. | |
2147 | */ | |
2148 | ||
2149 | /* process platform firmware initiated scrubs */ | |
2150 | retry: | |
2151 | mutex_lock(&acpi_desc->init_mutex); | |
1f7df6f8 | 2152 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { |
1cf03c00 DW |
2153 | struct nd_cmd_ars_status *ars_status; |
2154 | struct acpi_nfit_system_address *spa; | |
2155 | u64 ars_start, ars_len; | |
2156 | int rc; | |
1f7df6f8 | 2157 | |
1cf03c00 DW |
2158 | if (acpi_desc->cancel) |
2159 | break; | |
2160 | ||
2161 | if (nfit_spa->nd_region) | |
2162 | continue; | |
2163 | ||
2164 | if (init_ars_done) { | |
2165 | /* | |
2166 | * No need to re-query, we're now just | |
2167 | * reconciling all the ranges covered by the | |
2168 | * initial scrub | |
2169 | */ | |
2170 | rc = 0; | |
2171 | } else | |
2172 | rc = acpi_nfit_query_poison(acpi_desc, nfit_spa); | |
2173 | ||
2174 | if (rc == -ENOTTY) { | |
2175 | /* no ars capability, just register spa and move on */ | |
2176 | acpi_nfit_register_region(acpi_desc, nfit_spa); | |
2177 | continue; | |
2178 | } | |
2179 | ||
2180 | if (rc == -EBUSY && !tmo) { | |
2181 | /* fallthrough to directed scrub in phase 2 */ | |
2182 | dev_warn(dev, "timeout awaiting ars results, continuing...\n"); | |
2183 | break; | |
2184 | } else if (rc == -EBUSY) { | |
2185 | mutex_unlock(&acpi_desc->init_mutex); | |
2186 | ssleep(1); | |
2187 | tmo--; | |
2188 | goto retry; | |
2189 | } | |
2190 | ||
2191 | /* we got some results, but there are more pending... */ | |
2192 | if (rc == -ENOSPC && overflow_retry--) { | |
2193 | ars_status = acpi_desc->ars_status; | |
2194 | /* | |
2195 | * Record the original scrub range, so that we | |
2196 | * can recall all the ranges impacted by the | |
2197 | * initial scrub. | |
2198 | */ | |
2199 | if (!init_scrub_length) { | |
2200 | init_scrub_length = ars_status->length; | |
2201 | init_scrub_address = ars_status->address; | |
2202 | } | |
2203 | rc = ars_continue(acpi_desc); | |
2204 | if (rc == 0) { | |
2205 | mutex_unlock(&acpi_desc->init_mutex); | |
2206 | goto retry; | |
2207 | } | |
2208 | } | |
2209 | ||
2210 | if (rc < 0) { | |
2211 | /* | |
2212 | * Initial scrub failed, we'll give it one more | |
2213 | * try below... | |
2214 | */ | |
2215 | break; | |
2216 | } | |
2217 | ||
2218 | /* We got some final results, record completed ranges */ | |
2219 | ars_status = acpi_desc->ars_status; | |
2220 | if (init_scrub_length) { | |
2221 | ars_start = init_scrub_address; | |
2222 | ars_len = ars_start + init_scrub_length; | |
2223 | } else { | |
2224 | ars_start = ars_status->address; | |
2225 | ars_len = ars_status->length; | |
2226 | } | |
2227 | spa = nfit_spa->spa; | |
2228 | ||
2229 | if (!init_ars_done) { | |
2230 | init_ars_done = true; | |
2231 | dev_dbg(dev, "init scrub %#llx + %#llx complete\n", | |
2232 | ars_start, ars_len); | |
2233 | } | |
2234 | if (ars_start <= spa->address && ars_start + ars_len | |
2235 | >= spa->address + spa->length) | |
2236 | acpi_nfit_register_region(acpi_desc, nfit_spa); | |
1f7df6f8 | 2237 | } |
1cf03c00 DW |
2238 | |
2239 | /* | |
2240 | * For all the ranges not covered by an initial scrub we still | |
2241 | * want to see if there are errors, but it's ok to discover them | |
2242 | * asynchronously. | |
2243 | */ | |
2244 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) { | |
2245 | /* | |
2246 | * Flag all the ranges that still need scrubbing, but | |
2247 | * register them now to make data available. | |
2248 | */ | |
2249 | if (nfit_spa->nd_region) | |
2250 | nfit_spa->ars_done = 1; | |
2251 | else | |
2252 | acpi_nfit_register_region(acpi_desc, nfit_spa); | |
2253 | } | |
2254 | ||
2255 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) | |
2256 | acpi_nfit_async_scrub(acpi_desc, nfit_spa); | |
2257 | mutex_unlock(&acpi_desc->init_mutex); | |
2258 | } | |
2259 | ||
2260 | static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc) | |
2261 | { | |
2262 | struct nfit_spa *nfit_spa; | |
2263 | int rc; | |
2264 | ||
2265 | list_for_each_entry(nfit_spa, &acpi_desc->spas, list) | |
2266 | if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) { | |
2267 | /* BLK regions don't need to wait for ars results */ | |
2268 | rc = acpi_nfit_register_region(acpi_desc, nfit_spa); | |
2269 | if (rc) | |
2270 | return rc; | |
2271 | } | |
2272 | ||
2273 | queue_work(nfit_wq, &acpi_desc->work); | |
1f7df6f8 DW |
2274 | return 0; |
2275 | } | |
2276 | ||
20985164 VV |
2277 | static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc, |
2278 | struct nfit_table_prev *prev) | |
2279 | { | |
2280 | struct device *dev = acpi_desc->dev; | |
2281 | ||
2282 | if (!list_empty(&prev->spas) || | |
2283 | !list_empty(&prev->memdevs) || | |
2284 | !list_empty(&prev->dcrs) || | |
2285 | !list_empty(&prev->bdws) || | |
2286 | !list_empty(&prev->idts) || | |
2287 | !list_empty(&prev->flushes)) { | |
2288 | dev_err(dev, "new nfit deletes entries (unsupported)\n"); | |
2289 | return -ENXIO; | |
2290 | } | |
2291 | return 0; | |
2292 | } | |
2293 | ||
e7a11b44 | 2294 | int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz) |
b94d5230 DW |
2295 | { |
2296 | struct device *dev = acpi_desc->dev; | |
20985164 | 2297 | struct nfit_table_prev prev; |
b94d5230 | 2298 | const void *end; |
1f7df6f8 | 2299 | int rc; |
b94d5230 | 2300 | |
20985164 VV |
2301 | mutex_lock(&acpi_desc->init_mutex); |
2302 | ||
2303 | INIT_LIST_HEAD(&prev.spas); | |
2304 | INIT_LIST_HEAD(&prev.memdevs); | |
2305 | INIT_LIST_HEAD(&prev.dcrs); | |
2306 | INIT_LIST_HEAD(&prev.bdws); | |
2307 | INIT_LIST_HEAD(&prev.idts); | |
2308 | INIT_LIST_HEAD(&prev.flushes); | |
2309 | ||
2310 | list_cut_position(&prev.spas, &acpi_desc->spas, | |
2311 | acpi_desc->spas.prev); | |
2312 | list_cut_position(&prev.memdevs, &acpi_desc->memdevs, | |
2313 | acpi_desc->memdevs.prev); | |
2314 | list_cut_position(&prev.dcrs, &acpi_desc->dcrs, | |
2315 | acpi_desc->dcrs.prev); | |
2316 | list_cut_position(&prev.bdws, &acpi_desc->bdws, | |
2317 | acpi_desc->bdws.prev); | |
2318 | list_cut_position(&prev.idts, &acpi_desc->idts, | |
2319 | acpi_desc->idts.prev); | |
2320 | list_cut_position(&prev.flushes, &acpi_desc->flushes, | |
2321 | acpi_desc->flushes.prev); | |
b94d5230 | 2322 | |
b94d5230 | 2323 | end = data + sz; |
b94d5230 | 2324 | while (!IS_ERR_OR_NULL(data)) |
20985164 | 2325 | data = add_table(acpi_desc, &prev, data, end); |
b94d5230 DW |
2326 | |
2327 | if (IS_ERR(data)) { | |
2328 | dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__, | |
2329 | PTR_ERR(data)); | |
20985164 VV |
2330 | rc = PTR_ERR(data); |
2331 | goto out_unlock; | |
b94d5230 DW |
2332 | } |
2333 | ||
20985164 VV |
2334 | rc = acpi_nfit_check_deletions(acpi_desc, &prev); |
2335 | if (rc) | |
2336 | goto out_unlock; | |
2337 | ||
81ed4e36 DW |
2338 | rc = nfit_mem_init(acpi_desc); |
2339 | if (rc) | |
20985164 | 2340 | goto out_unlock; |
b94d5230 | 2341 | |
62232e45 DW |
2342 | acpi_nfit_init_dsms(acpi_desc); |
2343 | ||
1f7df6f8 DW |
2344 | rc = acpi_nfit_register_dimms(acpi_desc); |
2345 | if (rc) | |
20985164 VV |
2346 | goto out_unlock; |
2347 | ||
2348 | rc = acpi_nfit_register_regions(acpi_desc); | |
1f7df6f8 | 2349 | |
20985164 VV |
2350 | out_unlock: |
2351 | mutex_unlock(&acpi_desc->init_mutex); | |
2352 | return rc; | |
b94d5230 | 2353 | } |
6bc75619 | 2354 | EXPORT_SYMBOL_GPL(acpi_nfit_init); |
b94d5230 | 2355 | |
7ae0fa43 DW |
2356 | struct acpi_nfit_flush_work { |
2357 | struct work_struct work; | |
2358 | struct completion cmp; | |
2359 | }; | |
2360 | ||
2361 | static void flush_probe(struct work_struct *work) | |
2362 | { | |
2363 | struct acpi_nfit_flush_work *flush; | |
2364 | ||
2365 | flush = container_of(work, typeof(*flush), work); | |
2366 | complete(&flush->cmp); | |
2367 | } | |
2368 | ||
2369 | static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc) | |
2370 | { | |
2371 | struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc); | |
2372 | struct device *dev = acpi_desc->dev; | |
2373 | struct acpi_nfit_flush_work flush; | |
2374 | ||
2375 | /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */ | |
2376 | device_lock(dev); | |
2377 | device_unlock(dev); | |
2378 | ||
2379 | /* | |
2380 | * Scrub work could take 10s of seconds, userspace may give up so we | |
2381 | * need to be interruptible while waiting. | |
2382 | */ | |
2383 | INIT_WORK_ONSTACK(&flush.work, flush_probe); | |
2384 | COMPLETION_INITIALIZER_ONSTACK(flush.cmp); | |
2385 | queue_work(nfit_wq, &flush.work); | |
2386 | return wait_for_completion_interruptible(&flush.cmp); | |
2387 | } | |
2388 | ||
87bf572e DW |
2389 | static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc, |
2390 | struct nvdimm *nvdimm, unsigned int cmd) | |
2391 | { | |
2392 | struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc); | |
2393 | ||
2394 | if (nvdimm) | |
2395 | return 0; | |
2396 | if (cmd != ND_CMD_ARS_START) | |
2397 | return 0; | |
2398 | ||
2399 | /* | |
2400 | * The kernel and userspace may race to initiate a scrub, but | |
2401 | * the scrub thread is prepared to lose that initial race. It | |
2402 | * just needs guarantees that any ars it initiates are not | |
2403 | * interrupted by any intervening start reqeusts from userspace. | |
2404 | */ | |
2405 | if (work_busy(&acpi_desc->work)) | |
2406 | return -EBUSY; | |
2407 | ||
2408 | return 0; | |
2409 | } | |
2410 | ||
a61fe6f7 | 2411 | void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev) |
b94d5230 DW |
2412 | { |
2413 | struct nvdimm_bus_descriptor *nd_desc; | |
b94d5230 DW |
2414 | |
2415 | dev_set_drvdata(dev, acpi_desc); | |
2416 | acpi_desc->dev = dev; | |
6bc75619 | 2417 | acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io; |
b94d5230 DW |
2418 | nd_desc = &acpi_desc->nd_desc; |
2419 | nd_desc->provider_name = "ACPI.NFIT"; | |
bc9775d8 | 2420 | nd_desc->module = THIS_MODULE; |
b94d5230 | 2421 | nd_desc->ndctl = acpi_nfit_ctl; |
7ae0fa43 | 2422 | nd_desc->flush_probe = acpi_nfit_flush_probe; |
87bf572e | 2423 | nd_desc->clear_to_send = acpi_nfit_clear_to_send; |
45def22c | 2424 | nd_desc->attr_groups = acpi_nfit_attribute_groups; |
b94d5230 | 2425 | |
20985164 VV |
2426 | INIT_LIST_HEAD(&acpi_desc->spas); |
2427 | INIT_LIST_HEAD(&acpi_desc->dcrs); | |
2428 | INIT_LIST_HEAD(&acpi_desc->bdws); | |
2429 | INIT_LIST_HEAD(&acpi_desc->idts); | |
2430 | INIT_LIST_HEAD(&acpi_desc->flushes); | |
2431 | INIT_LIST_HEAD(&acpi_desc->memdevs); | |
2432 | INIT_LIST_HEAD(&acpi_desc->dimms); | |
20985164 | 2433 | mutex_init(&acpi_desc->init_mutex); |
1cf03c00 | 2434 | INIT_WORK(&acpi_desc->work, acpi_nfit_scrub); |
20985164 | 2435 | } |
a61fe6f7 | 2436 | EXPORT_SYMBOL_GPL(acpi_nfit_desc_init); |
20985164 VV |
2437 | |
2438 | static int acpi_nfit_add(struct acpi_device *adev) | |
2439 | { | |
2440 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; | |
2441 | struct acpi_nfit_desc *acpi_desc; | |
2442 | struct device *dev = &adev->dev; | |
2443 | struct acpi_table_header *tbl; | |
2444 | acpi_status status = AE_OK; | |
2445 | acpi_size sz; | |
31932041 | 2446 | int rc = 0; |
20985164 | 2447 | |
82595423 | 2448 | status = acpi_get_table_with_size(ACPI_SIG_NFIT, 0, &tbl, &sz); |
20985164 VV |
2449 | if (ACPI_FAILURE(status)) { |
2450 | /* This is ok, we could have an nvdimm hotplugged later */ | |
2451 | dev_dbg(dev, "failed to find NFIT at startup\n"); | |
2452 | return 0; | |
2453 | } | |
2454 | ||
a61fe6f7 DW |
2455 | acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL); |
2456 | if (!acpi_desc) | |
2457 | return -ENOMEM; | |
2458 | acpi_nfit_desc_init(acpi_desc, &adev->dev); | |
2459 | acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, &acpi_desc->nd_desc); | |
2460 | if (!acpi_desc->nvdimm_bus) | |
2461 | return -ENOMEM; | |
20985164 | 2462 | |
e7a11b44 | 2463 | /* Save the acpi header for exporting the revision via sysfs */ |
6b577c9d | 2464 | acpi_desc->acpi_header = *tbl; |
20985164 VV |
2465 | |
2466 | /* Evaluate _FIT and override with that if present */ | |
2467 | status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf); | |
2468 | if (ACPI_SUCCESS(status) && buf.length > 0) { | |
e7a11b44 DW |
2469 | union acpi_object *obj = buf.pointer; |
2470 | ||
2471 | if (obj->type == ACPI_TYPE_BUFFER) | |
2472 | rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer, | |
2473 | obj->buffer.length); | |
2474 | else | |
6b577c9d LK |
2475 | dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n", |
2476 | __func__, (int) obj->type); | |
31932041 DW |
2477 | kfree(buf.pointer); |
2478 | } else | |
e7a11b44 DW |
2479 | /* skip over the lead-in header table */ |
2480 | rc = acpi_nfit_init(acpi_desc, (void *) tbl | |
2481 | + sizeof(struct acpi_table_nfit), | |
2482 | sz - sizeof(struct acpi_table_nfit)); | |
b94d5230 | 2483 | |
e7a11b44 | 2484 | if (rc) |
b94d5230 | 2485 | nvdimm_bus_unregister(acpi_desc->nvdimm_bus); |
e7a11b44 | 2486 | return rc; |
b94d5230 DW |
2487 | } |
2488 | ||
2489 | static int acpi_nfit_remove(struct acpi_device *adev) | |
2490 | { | |
2491 | struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev); | |
2492 | ||
7ae0fa43 DW |
2493 | acpi_desc->cancel = 1; |
2494 | flush_workqueue(nfit_wq); | |
b94d5230 DW |
2495 | nvdimm_bus_unregister(acpi_desc->nvdimm_bus); |
2496 | return 0; | |
2497 | } | |
2498 | ||
20985164 VV |
2499 | static void acpi_nfit_notify(struct acpi_device *adev, u32 event) |
2500 | { | |
2501 | struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev); | |
2502 | struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL }; | |
20985164 | 2503 | struct device *dev = &adev->dev; |
e7a11b44 | 2504 | union acpi_object *obj; |
20985164 VV |
2505 | acpi_status status; |
2506 | int ret; | |
2507 | ||
2508 | dev_dbg(dev, "%s: event: %d\n", __func__, event); | |
2509 | ||
2510 | device_lock(dev); | |
2511 | if (!dev->driver) { | |
2512 | /* dev->driver may be null if we're being removed */ | |
2513 | dev_dbg(dev, "%s: no driver found for dev\n", __func__); | |
d91e8928 | 2514 | goto out_unlock; |
20985164 VV |
2515 | } |
2516 | ||
2517 | if (!acpi_desc) { | |
a61fe6f7 DW |
2518 | acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL); |
2519 | if (!acpi_desc) | |
2520 | goto out_unlock; | |
2521 | acpi_nfit_desc_init(acpi_desc, &adev->dev); | |
2522 | acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, &acpi_desc->nd_desc); | |
2523 | if (!acpi_desc->nvdimm_bus) | |
20985164 | 2524 | goto out_unlock; |
7ae0fa43 DW |
2525 | } else { |
2526 | /* | |
2527 | * Finish previous registration before considering new | |
2528 | * regions. | |
2529 | */ | |
2530 | flush_workqueue(nfit_wq); | |
20985164 VV |
2531 | } |
2532 | ||
2533 | /* Evaluate _FIT */ | |
2534 | status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf); | |
2535 | if (ACPI_FAILURE(status)) { | |
2536 | dev_err(dev, "failed to evaluate _FIT\n"); | |
2537 | goto out_unlock; | |
2538 | } | |
2539 | ||
6b577c9d LK |
2540 | obj = buf.pointer; |
2541 | if (obj->type == ACPI_TYPE_BUFFER) { | |
e7a11b44 DW |
2542 | ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer, |
2543 | obj->buffer.length); | |
31932041 | 2544 | if (ret) |
6b577c9d | 2545 | dev_err(dev, "failed to merge updated NFIT\n"); |
31932041 | 2546 | } else |
6b577c9d | 2547 | dev_err(dev, "Invalid _FIT\n"); |
20985164 VV |
2548 | kfree(buf.pointer); |
2549 | ||
2550 | out_unlock: | |
2551 | device_unlock(dev); | |
2552 | } | |
2553 | ||
b94d5230 DW |
2554 | static const struct acpi_device_id acpi_nfit_ids[] = { |
2555 | { "ACPI0012", 0 }, | |
2556 | { "", 0 }, | |
2557 | }; | |
2558 | MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids); | |
2559 | ||
2560 | static struct acpi_driver acpi_nfit_driver = { | |
2561 | .name = KBUILD_MODNAME, | |
2562 | .ids = acpi_nfit_ids, | |
2563 | .ops = { | |
2564 | .add = acpi_nfit_add, | |
2565 | .remove = acpi_nfit_remove, | |
20985164 | 2566 | .notify = acpi_nfit_notify, |
b94d5230 DW |
2567 | }, |
2568 | }; | |
2569 | ||
2570 | static __init int nfit_init(void) | |
2571 | { | |
2572 | BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40); | |
2573 | BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56); | |
2574 | BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48); | |
2575 | BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20); | |
2576 | BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9); | |
2577 | BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80); | |
2578 | BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40); | |
2579 | ||
2580 | acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]); | |
2581 | acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]); | |
2582 | acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]); | |
2583 | acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]); | |
2584 | acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]); | |
2585 | acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]); | |
2586 | acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]); | |
2587 | acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]); | |
2588 | acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]); | |
2589 | acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]); | |
31eca76b DW |
2590 | acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE1, nfit_uuid[NFIT_DEV_DIMM_N_HPE1]); |
2591 | acpi_str_to_uuid(UUID_NFIT_DIMM_N_HPE2, nfit_uuid[NFIT_DEV_DIMM_N_HPE2]); | |
e02fb726 | 2592 | acpi_str_to_uuid(UUID_NFIT_DIMM_N_MSFT, nfit_uuid[NFIT_DEV_DIMM_N_MSFT]); |
b94d5230 | 2593 | |
7ae0fa43 DW |
2594 | nfit_wq = create_singlethread_workqueue("nfit"); |
2595 | if (!nfit_wq) | |
2596 | return -ENOMEM; | |
2597 | ||
b94d5230 DW |
2598 | return acpi_bus_register_driver(&acpi_nfit_driver); |
2599 | } | |
2600 | ||
2601 | static __exit void nfit_exit(void) | |
2602 | { | |
2603 | acpi_bus_unregister_driver(&acpi_nfit_driver); | |
7ae0fa43 | 2604 | destroy_workqueue(nfit_wq); |
b94d5230 DW |
2605 | } |
2606 | ||
2607 | module_init(nfit_init); | |
2608 | module_exit(nfit_exit); | |
2609 | MODULE_LICENSE("GPL v2"); | |
2610 | MODULE_AUTHOR("Intel Corporation"); |