GNU Linux-libre 4.14.303-gnu1
[releases.git] / drivers / acpi / nfit / core.c
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>
16 #include <linux/mutex.h>
17 #include <linux/ndctl.h>
18 #include <linux/sysfs.h>
19 #include <linux/delay.h>
20 #include <linux/list.h>
21 #include <linux/acpi.h>
22 #include <linux/sort.h>
23 #include <linux/io.h>
24 #include <linux/nd.h>
25 #include <asm/cacheflush.h>
26 #include "nfit.h"
27
28 /*
29  * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
30  * irrelevant.
31  */
32 #include <linux/io-64-nonatomic-hi-lo.h>
33
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
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
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");
52
53 static unsigned long override_dsm_mask;
54 module_param(override_dsm_mask, ulong, S_IRUGO);
55 MODULE_PARM_DESC(override_dsm_mask, "Bitmask of allowed NVDIMM DSM functions");
56
57 static int default_dsm_family = -1;
58 module_param(default_dsm_family, int, S_IRUGO);
59 MODULE_PARM_DESC(default_dsm_family,
60                 "Try this DSM type first when identifying NVDIMM family");
61
62 LIST_HEAD(acpi_descs);
63 DEFINE_MUTEX(acpi_desc_lock);
64
65 static struct workqueue_struct *nfit_wq;
66
67 struct nfit_table_prev {
68         struct list_head spas;
69         struct list_head memdevs;
70         struct list_head dcrs;
71         struct list_head bdws;
72         struct list_head idts;
73         struct list_head flushes;
74 };
75
76 static guid_t nfit_uuid[NFIT_UUID_MAX];
77
78 const guid_t *to_nfit_uuid(enum nfit_uuids id)
79 {
80         return &nfit_uuid[id];
81 }
82 EXPORT_SYMBOL(to_nfit_uuid);
83
84 static struct acpi_nfit_desc *to_acpi_nfit_desc(
85                 struct nvdimm_bus_descriptor *nd_desc)
86 {
87         return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
88 }
89
90 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
91 {
92         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
93
94         /*
95          * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
96          * acpi_device.
97          */
98         if (!nd_desc->provider_name
99                         || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
100                 return NULL;
101
102         return to_acpi_device(acpi_desc->dev);
103 }
104
105 static int xlat_bus_status(void *buf, unsigned int cmd, u32 status)
106 {
107         struct nd_cmd_clear_error *clear_err;
108         struct nd_cmd_ars_status *ars_status;
109         u16 flags;
110
111         switch (cmd) {
112         case ND_CMD_ARS_CAP:
113                 if ((status & 0xffff) == NFIT_ARS_CAP_NONE)
114                         return -ENOTTY;
115
116                 /* Command failed */
117                 if (status & 0xffff)
118                         return -EIO;
119
120                 /* No supported scan types for this range */
121                 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
122                 if ((status >> 16 & flags) == 0)
123                         return -ENOTTY;
124                 return 0;
125         case ND_CMD_ARS_START:
126                 /* ARS is in progress */
127                 if ((status & 0xffff) == NFIT_ARS_START_BUSY)
128                         return -EBUSY;
129
130                 /* Command failed */
131                 if (status & 0xffff)
132                         return -EIO;
133                 return 0;
134         case ND_CMD_ARS_STATUS:
135                 ars_status = buf;
136                 /* Command failed */
137                 if (status & 0xffff)
138                         return -EIO;
139                 /* Check extended status (Upper two bytes) */
140                 if (status == NFIT_ARS_STATUS_DONE)
141                         return 0;
142
143                 /* ARS is in progress */
144                 if (status == NFIT_ARS_STATUS_BUSY)
145                         return -EBUSY;
146
147                 /* No ARS performed for the current boot */
148                 if (status == NFIT_ARS_STATUS_NONE)
149                         return -EAGAIN;
150
151                 /*
152                  * ARS interrupted, either we overflowed or some other
153                  * agent wants the scan to stop.  If we didn't overflow
154                  * then just continue with the returned results.
155                  */
156                 if (status == NFIT_ARS_STATUS_INTR) {
157                         if (ars_status->out_length >= 40 && (ars_status->flags
158                                                 & NFIT_ARS_F_OVERFLOW))
159                                 return -ENOSPC;
160                         return 0;
161                 }
162
163                 /* Unknown status */
164                 if (status >> 16)
165                         return -EIO;
166                 return 0;
167         case ND_CMD_CLEAR_ERROR:
168                 clear_err = buf;
169                 if (status & 0xffff)
170                         return -EIO;
171                 if (!clear_err->cleared)
172                         return -EIO;
173                 if (clear_err->length > clear_err->cleared)
174                         return clear_err->cleared;
175                 return 0;
176         default:
177                 break;
178         }
179
180         /* all other non-zero status results in an error */
181         if (status)
182                 return -EIO;
183         return 0;
184 }
185
186 static int xlat_nvdimm_status(void *buf, unsigned int cmd, u32 status)
187 {
188         switch (cmd) {
189         case ND_CMD_GET_CONFIG_SIZE:
190                 if (status >> 16 & ND_CONFIG_LOCKED)
191                         return -EACCES;
192                 break;
193         default:
194                 break;
195         }
196
197         /* all other non-zero status results in an error */
198         if (status)
199                 return -EIO;
200         return 0;
201 }
202
203 static int xlat_status(struct nvdimm *nvdimm, void *buf, unsigned int cmd,
204                 u32 status)
205 {
206         if (!nvdimm)
207                 return xlat_bus_status(buf, cmd, status);
208         return xlat_nvdimm_status(buf, cmd, status);
209 }
210
211 static int cmd_to_func(struct nfit_mem *nfit_mem, unsigned int cmd,
212                 struct nd_cmd_pkg *call_pkg)
213 {
214         if (call_pkg) {
215                 int i;
216
217                 if (nfit_mem && nfit_mem->family != call_pkg->nd_family)
218                         return -ENOTTY;
219
220                 for (i = 0; i < ARRAY_SIZE(call_pkg->nd_reserved2); i++)
221                         if (call_pkg->nd_reserved2[i])
222                                 return -EINVAL;
223                 return call_pkg->nd_command;
224         }
225
226         /* In the !call_pkg case, bus commands == bus functions */
227         if (!nfit_mem)
228                 return cmd;
229
230         /* Linux ND commands == NVDIMM_FAMILY_INTEL function numbers */
231         if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
232                 return cmd;
233
234         /*
235          * Force function number validation to fail since 0 is never
236          * published as a valid function in dsm_mask.
237          */
238         return 0;
239 }
240
241 int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc, struct nvdimm *nvdimm,
242                 unsigned int cmd, void *buf, unsigned int buf_len, int *cmd_rc)
243 {
244         struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
245         struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
246         union acpi_object in_obj, in_buf, *out_obj;
247         const struct nd_cmd_desc *desc = NULL;
248         struct device *dev = acpi_desc->dev;
249         struct nd_cmd_pkg *call_pkg = NULL;
250         const char *cmd_name, *dimm_name;
251         unsigned long cmd_mask, dsm_mask;
252         u32 offset, fw_status = 0;
253         acpi_handle handle;
254         const guid_t *guid;
255         int func, rc, i;
256
257         if (cmd_rc)
258                 *cmd_rc = -EINVAL;
259
260         if (cmd == ND_CMD_CALL)
261                 call_pkg = buf;
262         func = cmd_to_func(nfit_mem, cmd, call_pkg);
263         if (func < 0)
264                 return func;
265
266         if (nvdimm) {
267                 struct acpi_device *adev = nfit_mem->adev;
268
269                 if (!adev)
270                         return -ENOTTY;
271
272                 dimm_name = nvdimm_name(nvdimm);
273                 cmd_name = nvdimm_cmd_name(cmd);
274                 cmd_mask = nvdimm_cmd_mask(nvdimm);
275                 dsm_mask = nfit_mem->dsm_mask;
276                 desc = nd_cmd_dimm_desc(cmd);
277                 guid = to_nfit_uuid(nfit_mem->family);
278                 handle = adev->handle;
279         } else {
280                 struct acpi_device *adev = to_acpi_dev(acpi_desc);
281
282                 cmd_name = nvdimm_bus_cmd_name(cmd);
283                 cmd_mask = nd_desc->cmd_mask;
284                 dsm_mask = nd_desc->bus_dsm_mask;
285                 desc = nd_cmd_bus_desc(cmd);
286                 guid = to_nfit_uuid(NFIT_DEV_BUS);
287                 handle = adev->handle;
288                 dimm_name = "bus";
289         }
290
291         if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
292                 return -ENOTTY;
293
294         /*
295          * Check for a valid command.  For ND_CMD_CALL, we also have to
296          * make sure that the DSM function is supported.
297          */
298         if (cmd == ND_CMD_CALL && !test_bit(func, &dsm_mask))
299                 return -ENOTTY;
300         else if (!test_bit(cmd, &cmd_mask))
301                 return -ENOTTY;
302
303         in_obj.type = ACPI_TYPE_PACKAGE;
304         in_obj.package.count = 1;
305         in_obj.package.elements = &in_buf;
306         in_buf.type = ACPI_TYPE_BUFFER;
307         in_buf.buffer.pointer = buf;
308         in_buf.buffer.length = 0;
309
310         /* libnvdimm has already validated the input envelope */
311         for (i = 0; i < desc->in_num; i++)
312                 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
313                                 i, buf);
314
315         if (call_pkg) {
316                 /* skip over package wrapper */
317                 in_buf.buffer.pointer = (void *) &call_pkg->nd_payload;
318                 in_buf.buffer.length = call_pkg->nd_size_in;
319         }
320
321         dev_dbg(dev, "%s:%s cmd: %d: func: %d input length: %d\n",
322                         __func__, dimm_name, cmd, func, in_buf.buffer.length);
323         print_hex_dump_debug("nvdimm in  ", DUMP_PREFIX_OFFSET, 4, 4,
324                         in_buf.buffer.pointer,
325                         min_t(u32, 256, in_buf.buffer.length), true);
326
327         out_obj = acpi_evaluate_dsm(handle, guid, 1, func, &in_obj);
328         if (!out_obj) {
329                 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
330                                 cmd_name);
331                 return -EINVAL;
332         }
333
334         if (out_obj->type != ACPI_TYPE_BUFFER) {
335                 dev_dbg(dev, "%s unexpected output object type cmd: %s type: %d\n",
336                                 dimm_name, cmd_name, out_obj->type);
337                 rc = -EINVAL;
338                 goto out;
339         }
340
341         if (call_pkg) {
342                 call_pkg->nd_fw_size = out_obj->buffer.length;
343                 memcpy(call_pkg->nd_payload + call_pkg->nd_size_in,
344                         out_obj->buffer.pointer,
345                         min(call_pkg->nd_fw_size, call_pkg->nd_size_out));
346
347                 ACPI_FREE(out_obj);
348                 /*
349                  * Need to support FW function w/o known size in advance.
350                  * Caller can determine required size based upon nd_fw_size.
351                  * If we return an error (like elsewhere) then caller wouldn't
352                  * be able to rely upon data returned to make calculation.
353                  */
354                 if (cmd_rc)
355                         *cmd_rc = 0;
356                 return 0;
357         }
358
359         dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__, dimm_name,
360                         cmd_name, out_obj->buffer.length);
361         print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4, 4,
362                         out_obj->buffer.pointer,
363                         min_t(u32, 128, out_obj->buffer.length), true);
364
365         for (i = 0, offset = 0; i < desc->out_num; i++) {
366                 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
367                                 (u32 *) out_obj->buffer.pointer,
368                                 out_obj->buffer.length - offset);
369
370                 if (offset + out_size > out_obj->buffer.length) {
371                         dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
372                                         __func__, dimm_name, cmd_name, i);
373                         break;
374                 }
375
376                 if (in_buf.buffer.length + offset + out_size > buf_len) {
377                         dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
378                                         __func__, dimm_name, cmd_name, i);
379                         rc = -ENXIO;
380                         goto out;
381                 }
382                 memcpy(buf + in_buf.buffer.length + offset,
383                                 out_obj->buffer.pointer + offset, out_size);
384                 offset += out_size;
385         }
386
387         /*
388          * Set fw_status for all the commands with a known format to be
389          * later interpreted by xlat_status().
390          */
391         if (i >= 1 && ((cmd >= ND_CMD_ARS_CAP && cmd <= ND_CMD_CLEAR_ERROR)
392                         || (cmd >= ND_CMD_SMART && cmd <= ND_CMD_VENDOR)))
393                 fw_status = *(u32 *) out_obj->buffer.pointer;
394
395         if (offset + in_buf.buffer.length < buf_len) {
396                 if (i >= 1) {
397                         /*
398                          * status valid, return the number of bytes left
399                          * unfilled in the output buffer
400                          */
401                         rc = buf_len - offset - in_buf.buffer.length;
402                         if (cmd_rc)
403                                 *cmd_rc = xlat_status(nvdimm, buf, cmd,
404                                                 fw_status);
405                 } else {
406                         dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
407                                         __func__, dimm_name, cmd_name, buf_len,
408                                         offset);
409                         rc = -ENXIO;
410                 }
411         } else {
412                 rc = 0;
413                 if (cmd_rc)
414                         *cmd_rc = xlat_status(nvdimm, buf, cmd, fw_status);
415         }
416
417  out:
418         ACPI_FREE(out_obj);
419
420         return rc;
421 }
422 EXPORT_SYMBOL_GPL(acpi_nfit_ctl);
423
424 static const char *spa_type_name(u16 type)
425 {
426         static const char *to_name[] = {
427                 [NFIT_SPA_VOLATILE] = "volatile",
428                 [NFIT_SPA_PM] = "pmem",
429                 [NFIT_SPA_DCR] = "dimm-control-region",
430                 [NFIT_SPA_BDW] = "block-data-window",
431                 [NFIT_SPA_VDISK] = "volatile-disk",
432                 [NFIT_SPA_VCD] = "volatile-cd",
433                 [NFIT_SPA_PDISK] = "persistent-disk",
434                 [NFIT_SPA_PCD] = "persistent-cd",
435
436         };
437
438         if (type > NFIT_SPA_PCD)
439                 return "unknown";
440
441         return to_name[type];
442 }
443
444 int nfit_spa_type(struct acpi_nfit_system_address *spa)
445 {
446         int i;
447
448         for (i = 0; i < NFIT_UUID_MAX; i++)
449                 if (guid_equal(to_nfit_uuid(i), (guid_t *)&spa->range_guid))
450                         return i;
451         return -1;
452 }
453
454 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
455                 struct nfit_table_prev *prev,
456                 struct acpi_nfit_system_address *spa)
457 {
458         struct device *dev = acpi_desc->dev;
459         struct nfit_spa *nfit_spa;
460
461         if (spa->header.length != sizeof(*spa))
462                 return false;
463
464         list_for_each_entry(nfit_spa, &prev->spas, list) {
465                 if (memcmp(nfit_spa->spa, spa, sizeof(*spa)) == 0) {
466                         list_move_tail(&nfit_spa->list, &acpi_desc->spas);
467                         return true;
468                 }
469         }
470
471         nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa) + sizeof(*spa),
472                         GFP_KERNEL);
473         if (!nfit_spa)
474                 return false;
475         INIT_LIST_HEAD(&nfit_spa->list);
476         memcpy(nfit_spa->spa, spa, sizeof(*spa));
477         list_add_tail(&nfit_spa->list, &acpi_desc->spas);
478         dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
479                         spa->range_index,
480                         spa_type_name(nfit_spa_type(spa)));
481         return true;
482 }
483
484 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
485                 struct nfit_table_prev *prev,
486                 struct acpi_nfit_memory_map *memdev)
487 {
488         struct device *dev = acpi_desc->dev;
489         struct nfit_memdev *nfit_memdev;
490
491         if (memdev->header.length != sizeof(*memdev))
492                 return false;
493
494         list_for_each_entry(nfit_memdev, &prev->memdevs, list)
495                 if (memcmp(nfit_memdev->memdev, memdev, sizeof(*memdev)) == 0) {
496                         list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
497                         return true;
498                 }
499
500         nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev) + sizeof(*memdev),
501                         GFP_KERNEL);
502         if (!nfit_memdev)
503                 return false;
504         INIT_LIST_HEAD(&nfit_memdev->list);
505         memcpy(nfit_memdev->memdev, memdev, sizeof(*memdev));
506         list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
507         dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d flags: %#x\n",
508                         __func__, memdev->device_handle, memdev->range_index,
509                         memdev->region_index, memdev->flags);
510         return true;
511 }
512
513 /*
514  * An implementation may provide a truncated control region if no block windows
515  * are defined.
516  */
517 static size_t sizeof_dcr(struct acpi_nfit_control_region *dcr)
518 {
519         if (dcr->header.length < offsetof(struct acpi_nfit_control_region,
520                                 window_size))
521                 return 0;
522         if (dcr->windows)
523                 return sizeof(*dcr);
524         return offsetof(struct acpi_nfit_control_region, window_size);
525 }
526
527 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
528                 struct nfit_table_prev *prev,
529                 struct acpi_nfit_control_region *dcr)
530 {
531         struct device *dev = acpi_desc->dev;
532         struct nfit_dcr *nfit_dcr;
533
534         if (!sizeof_dcr(dcr))
535                 return false;
536
537         list_for_each_entry(nfit_dcr, &prev->dcrs, list)
538                 if (memcmp(nfit_dcr->dcr, dcr, sizeof_dcr(dcr)) == 0) {
539                         list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
540                         return true;
541                 }
542
543         nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr) + sizeof(*dcr),
544                         GFP_KERNEL);
545         if (!nfit_dcr)
546                 return false;
547         INIT_LIST_HEAD(&nfit_dcr->list);
548         memcpy(nfit_dcr->dcr, dcr, sizeof_dcr(dcr));
549         list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
550         dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
551                         dcr->region_index, dcr->windows);
552         return true;
553 }
554
555 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
556                 struct nfit_table_prev *prev,
557                 struct acpi_nfit_data_region *bdw)
558 {
559         struct device *dev = acpi_desc->dev;
560         struct nfit_bdw *nfit_bdw;
561
562         if (bdw->header.length != sizeof(*bdw))
563                 return false;
564         list_for_each_entry(nfit_bdw, &prev->bdws, list)
565                 if (memcmp(nfit_bdw->bdw, bdw, sizeof(*bdw)) == 0) {
566                         list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
567                         return true;
568                 }
569
570         nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw) + sizeof(*bdw),
571                         GFP_KERNEL);
572         if (!nfit_bdw)
573                 return false;
574         INIT_LIST_HEAD(&nfit_bdw->list);
575         memcpy(nfit_bdw->bdw, bdw, sizeof(*bdw));
576         list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
577         dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
578                         bdw->region_index, bdw->windows);
579         return true;
580 }
581
582 static size_t sizeof_idt(struct acpi_nfit_interleave *idt)
583 {
584         if (idt->header.length < sizeof(*idt))
585                 return 0;
586         return sizeof(*idt) + sizeof(u32) * (idt->line_count - 1);
587 }
588
589 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
590                 struct nfit_table_prev *prev,
591                 struct acpi_nfit_interleave *idt)
592 {
593         struct device *dev = acpi_desc->dev;
594         struct nfit_idt *nfit_idt;
595
596         if (!sizeof_idt(idt))
597                 return false;
598
599         list_for_each_entry(nfit_idt, &prev->idts, list) {
600                 if (sizeof_idt(nfit_idt->idt) != sizeof_idt(idt))
601                         continue;
602
603                 if (memcmp(nfit_idt->idt, idt, sizeof_idt(idt)) == 0) {
604                         list_move_tail(&nfit_idt->list, &acpi_desc->idts);
605                         return true;
606                 }
607         }
608
609         nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt) + sizeof_idt(idt),
610                         GFP_KERNEL);
611         if (!nfit_idt)
612                 return false;
613         INIT_LIST_HEAD(&nfit_idt->list);
614         memcpy(nfit_idt->idt, idt, sizeof_idt(idt));
615         list_add_tail(&nfit_idt->list, &acpi_desc->idts);
616         dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
617                         idt->interleave_index, idt->line_count);
618         return true;
619 }
620
621 static size_t sizeof_flush(struct acpi_nfit_flush_address *flush)
622 {
623         if (flush->header.length < sizeof(*flush))
624                 return 0;
625         return sizeof(*flush) + sizeof(u64) * (flush->hint_count - 1);
626 }
627
628 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
629                 struct nfit_table_prev *prev,
630                 struct acpi_nfit_flush_address *flush)
631 {
632         struct device *dev = acpi_desc->dev;
633         struct nfit_flush *nfit_flush;
634
635         if (!sizeof_flush(flush))
636                 return false;
637
638         list_for_each_entry(nfit_flush, &prev->flushes, list) {
639                 if (sizeof_flush(nfit_flush->flush) != sizeof_flush(flush))
640                         continue;
641
642                 if (memcmp(nfit_flush->flush, flush,
643                                         sizeof_flush(flush)) == 0) {
644                         list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
645                         return true;
646                 }
647         }
648
649         nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush)
650                         + sizeof_flush(flush), GFP_KERNEL);
651         if (!nfit_flush)
652                 return false;
653         INIT_LIST_HEAD(&nfit_flush->list);
654         memcpy(nfit_flush->flush, flush, sizeof_flush(flush));
655         list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
656         dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
657                         flush->device_handle, flush->hint_count);
658         return true;
659 }
660
661 static void *add_table(struct acpi_nfit_desc *acpi_desc,
662                 struct nfit_table_prev *prev, void *table, const void *end)
663 {
664         struct device *dev = acpi_desc->dev;
665         struct acpi_nfit_header *hdr;
666         void *err = ERR_PTR(-ENOMEM);
667
668         if (table >= end)
669                 return NULL;
670
671         hdr = table;
672         if (!hdr->length) {
673                 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
674                         hdr->type);
675                 return NULL;
676         }
677
678         switch (hdr->type) {
679         case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
680                 if (!add_spa(acpi_desc, prev, table))
681                         return err;
682                 break;
683         case ACPI_NFIT_TYPE_MEMORY_MAP:
684                 if (!add_memdev(acpi_desc, prev, table))
685                         return err;
686                 break;
687         case ACPI_NFIT_TYPE_CONTROL_REGION:
688                 if (!add_dcr(acpi_desc, prev, table))
689                         return err;
690                 break;
691         case ACPI_NFIT_TYPE_DATA_REGION:
692                 if (!add_bdw(acpi_desc, prev, table))
693                         return err;
694                 break;
695         case ACPI_NFIT_TYPE_INTERLEAVE:
696                 if (!add_idt(acpi_desc, prev, table))
697                         return err;
698                 break;
699         case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
700                 if (!add_flush(acpi_desc, prev, table))
701                         return err;
702                 break;
703         case ACPI_NFIT_TYPE_SMBIOS:
704                 dev_dbg(dev, "%s: smbios\n", __func__);
705                 break;
706         default:
707                 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
708                 break;
709         }
710
711         return table + hdr->length;
712 }
713
714 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
715                 struct nfit_mem *nfit_mem)
716 {
717         u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
718         u16 dcr = nfit_mem->dcr->region_index;
719         struct nfit_spa *nfit_spa;
720
721         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
722                 u16 range_index = nfit_spa->spa->range_index;
723                 int type = nfit_spa_type(nfit_spa->spa);
724                 struct nfit_memdev *nfit_memdev;
725
726                 if (type != NFIT_SPA_BDW)
727                         continue;
728
729                 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
730                         if (nfit_memdev->memdev->range_index != range_index)
731                                 continue;
732                         if (nfit_memdev->memdev->device_handle != device_handle)
733                                 continue;
734                         if (nfit_memdev->memdev->region_index != dcr)
735                                 continue;
736
737                         nfit_mem->spa_bdw = nfit_spa->spa;
738                         return;
739                 }
740         }
741
742         dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
743                         nfit_mem->spa_dcr->range_index);
744         nfit_mem->bdw = NULL;
745 }
746
747 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
748                 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
749 {
750         u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
751         struct nfit_memdev *nfit_memdev;
752         struct nfit_bdw *nfit_bdw;
753         struct nfit_idt *nfit_idt;
754         u16 idt_idx, range_index;
755
756         list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
757                 if (nfit_bdw->bdw->region_index != dcr)
758                         continue;
759                 nfit_mem->bdw = nfit_bdw->bdw;
760                 break;
761         }
762
763         if (!nfit_mem->bdw)
764                 return;
765
766         nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
767
768         if (!nfit_mem->spa_bdw)
769                 return;
770
771         range_index = nfit_mem->spa_bdw->range_index;
772         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
773                 if (nfit_memdev->memdev->range_index != range_index ||
774                                 nfit_memdev->memdev->region_index != dcr)
775                         continue;
776                 nfit_mem->memdev_bdw = nfit_memdev->memdev;
777                 idt_idx = nfit_memdev->memdev->interleave_index;
778                 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
779                         if (nfit_idt->idt->interleave_index != idt_idx)
780                                 continue;
781                         nfit_mem->idt_bdw = nfit_idt->idt;
782                         break;
783                 }
784                 break;
785         }
786 }
787
788 static int __nfit_mem_init(struct acpi_nfit_desc *acpi_desc,
789                 struct acpi_nfit_system_address *spa)
790 {
791         struct nfit_mem *nfit_mem, *found;
792         struct nfit_memdev *nfit_memdev;
793         int type = spa ? nfit_spa_type(spa) : 0;
794
795         switch (type) {
796         case NFIT_SPA_DCR:
797         case NFIT_SPA_PM:
798                 break;
799         default:
800                 if (spa)
801                         return 0;
802         }
803
804         /*
805          * This loop runs in two modes, when a dimm is mapped the loop
806          * adds memdev associations to an existing dimm, or creates a
807          * dimm. In the unmapped dimm case this loop sweeps for memdev
808          * instances with an invalid / zero range_index and adds those
809          * dimms without spa associations.
810          */
811         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
812                 struct nfit_flush *nfit_flush;
813                 struct nfit_dcr *nfit_dcr;
814                 u32 device_handle;
815                 u16 dcr;
816
817                 if (spa && nfit_memdev->memdev->range_index != spa->range_index)
818                         continue;
819                 if (!spa && nfit_memdev->memdev->range_index)
820                         continue;
821                 found = NULL;
822                 dcr = nfit_memdev->memdev->region_index;
823                 device_handle = nfit_memdev->memdev->device_handle;
824                 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
825                         if (__to_nfit_memdev(nfit_mem)->device_handle
826                                         == device_handle) {
827                                 found = nfit_mem;
828                                 break;
829                         }
830
831                 if (found)
832                         nfit_mem = found;
833                 else {
834                         nfit_mem = devm_kzalloc(acpi_desc->dev,
835                                         sizeof(*nfit_mem), GFP_KERNEL);
836                         if (!nfit_mem)
837                                 return -ENOMEM;
838                         INIT_LIST_HEAD(&nfit_mem->list);
839                         nfit_mem->acpi_desc = acpi_desc;
840                         list_add(&nfit_mem->list, &acpi_desc->dimms);
841                 }
842
843                 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
844                         if (nfit_dcr->dcr->region_index != dcr)
845                                 continue;
846                         /*
847                          * Record the control region for the dimm.  For
848                          * the ACPI 6.1 case, where there are separate
849                          * control regions for the pmem vs blk
850                          * interfaces, be sure to record the extended
851                          * blk details.
852                          */
853                         if (!nfit_mem->dcr)
854                                 nfit_mem->dcr = nfit_dcr->dcr;
855                         else if (nfit_mem->dcr->windows == 0
856                                         && nfit_dcr->dcr->windows)
857                                 nfit_mem->dcr = nfit_dcr->dcr;
858                         break;
859                 }
860
861                 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
862                         struct acpi_nfit_flush_address *flush;
863                         u16 i;
864
865                         if (nfit_flush->flush->device_handle != device_handle)
866                                 continue;
867                         nfit_mem->nfit_flush = nfit_flush;
868                         flush = nfit_flush->flush;
869                         nfit_mem->flush_wpq = devm_kzalloc(acpi_desc->dev,
870                                         flush->hint_count
871                                         * sizeof(struct resource), GFP_KERNEL);
872                         if (!nfit_mem->flush_wpq)
873                                 return -ENOMEM;
874                         for (i = 0; i < flush->hint_count; i++) {
875                                 struct resource *res = &nfit_mem->flush_wpq[i];
876
877                                 res->start = flush->hint_address[i];
878                                 res->end = res->start + 8 - 1;
879                         }
880                         break;
881                 }
882
883                 if (dcr && !nfit_mem->dcr) {
884                         dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
885                                         spa->range_index, dcr);
886                         return -ENODEV;
887                 }
888
889                 if (type == NFIT_SPA_DCR) {
890                         struct nfit_idt *nfit_idt;
891                         u16 idt_idx;
892
893                         /* multiple dimms may share a SPA when interleaved */
894                         nfit_mem->spa_dcr = spa;
895                         nfit_mem->memdev_dcr = nfit_memdev->memdev;
896                         idt_idx = nfit_memdev->memdev->interleave_index;
897                         list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
898                                 if (nfit_idt->idt->interleave_index != idt_idx)
899                                         continue;
900                                 nfit_mem->idt_dcr = nfit_idt->idt;
901                                 break;
902                         }
903                         nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
904                 } else if (type == NFIT_SPA_PM) {
905                         /*
906                          * A single dimm may belong to multiple SPA-PM
907                          * ranges, record at least one in addition to
908                          * any SPA-DCR range.
909                          */
910                         nfit_mem->memdev_pmem = nfit_memdev->memdev;
911                 } else
912                         nfit_mem->memdev_dcr = nfit_memdev->memdev;
913         }
914
915         return 0;
916 }
917
918 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
919 {
920         struct nfit_mem *a = container_of(_a, typeof(*a), list);
921         struct nfit_mem *b = container_of(_b, typeof(*b), list);
922         u32 handleA, handleB;
923
924         handleA = __to_nfit_memdev(a)->device_handle;
925         handleB = __to_nfit_memdev(b)->device_handle;
926         if (handleA < handleB)
927                 return -1;
928         else if (handleA > handleB)
929                 return 1;
930         return 0;
931 }
932
933 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
934 {
935         struct nfit_spa *nfit_spa;
936         int rc;
937
938
939         /*
940          * For each SPA-DCR or SPA-PMEM address range find its
941          * corresponding MEMDEV(s).  From each MEMDEV find the
942          * corresponding DCR.  Then, if we're operating on a SPA-DCR,
943          * try to find a SPA-BDW and a corresponding BDW that references
944          * the DCR.  Throw it all into an nfit_mem object.  Note, that
945          * BDWs are optional.
946          */
947         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
948                 rc = __nfit_mem_init(acpi_desc, nfit_spa->spa);
949                 if (rc)
950                         return rc;
951         }
952
953         /*
954          * If a DIMM has failed to be mapped into SPA there will be no
955          * SPA entries above. Find and register all the unmapped DIMMs
956          * for reporting and recovery purposes.
957          */
958         rc = __nfit_mem_init(acpi_desc, NULL);
959         if (rc)
960                 return rc;
961
962         list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
963
964         return 0;
965 }
966
967 static ssize_t bus_dsm_mask_show(struct device *dev,
968                 struct device_attribute *attr, char *buf)
969 {
970         struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
971         struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
972
973         return sprintf(buf, "%#lx\n", nd_desc->bus_dsm_mask);
974 }
975 static struct device_attribute dev_attr_bus_dsm_mask =
976                 __ATTR(dsm_mask, 0444, bus_dsm_mask_show, NULL);
977
978 static ssize_t revision_show(struct device *dev,
979                 struct device_attribute *attr, char *buf)
980 {
981         struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
982         struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
983         struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
984
985         return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
986 }
987 static DEVICE_ATTR_RO(revision);
988
989 static ssize_t hw_error_scrub_show(struct device *dev,
990                 struct device_attribute *attr, char *buf)
991 {
992         struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
993         struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
994         struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
995
996         return sprintf(buf, "%d\n", acpi_desc->scrub_mode);
997 }
998
999 /*
1000  * The 'hw_error_scrub' attribute can have the following values written to it:
1001  * '0': Switch to the default mode where an exception will only insert
1002  *      the address of the memory error into the poison and badblocks lists.
1003  * '1': Enable a full scrub to happen if an exception for a memory error is
1004  *      received.
1005  */
1006 static ssize_t hw_error_scrub_store(struct device *dev,
1007                 struct device_attribute *attr, const char *buf, size_t size)
1008 {
1009         struct nvdimm_bus_descriptor *nd_desc;
1010         ssize_t rc;
1011         long val;
1012
1013         rc = kstrtol(buf, 0, &val);
1014         if (rc)
1015                 return rc;
1016
1017         device_lock(dev);
1018         nd_desc = dev_get_drvdata(dev);
1019         if (nd_desc) {
1020                 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1021
1022                 switch (val) {
1023                 case HW_ERROR_SCRUB_ON:
1024                         acpi_desc->scrub_mode = HW_ERROR_SCRUB_ON;
1025                         break;
1026                 case HW_ERROR_SCRUB_OFF:
1027                         acpi_desc->scrub_mode = HW_ERROR_SCRUB_OFF;
1028                         break;
1029                 default:
1030                         rc = -EINVAL;
1031                         break;
1032                 }
1033         }
1034         device_unlock(dev);
1035         if (rc)
1036                 return rc;
1037         return size;
1038 }
1039 static DEVICE_ATTR_RW(hw_error_scrub);
1040
1041 /*
1042  * This shows the number of full Address Range Scrubs that have been
1043  * completed since driver load time. Userspace can wait on this using
1044  * select/poll etc. A '+' at the end indicates an ARS is in progress
1045  */
1046 static ssize_t scrub_show(struct device *dev,
1047                 struct device_attribute *attr, char *buf)
1048 {
1049         struct nvdimm_bus_descriptor *nd_desc;
1050         ssize_t rc = -ENXIO;
1051
1052         device_lock(dev);
1053         nd_desc = dev_get_drvdata(dev);
1054         if (nd_desc) {
1055                 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1056
1057                 mutex_lock(&acpi_desc->init_mutex);
1058                 rc = sprintf(buf, "%d%s", acpi_desc->scrub_count,
1059                                 work_busy(&acpi_desc->work)
1060                                 && !acpi_desc->cancel ? "+\n" : "\n");
1061                 mutex_unlock(&acpi_desc->init_mutex);
1062         }
1063         device_unlock(dev);
1064         return rc;
1065 }
1066
1067 static ssize_t scrub_store(struct device *dev,
1068                 struct device_attribute *attr, const char *buf, size_t size)
1069 {
1070         struct nvdimm_bus_descriptor *nd_desc;
1071         ssize_t rc;
1072         long val;
1073
1074         rc = kstrtol(buf, 0, &val);
1075         if (rc)
1076                 return rc;
1077         if (val != 1)
1078                 return -EINVAL;
1079
1080         device_lock(dev);
1081         nd_desc = dev_get_drvdata(dev);
1082         if (nd_desc) {
1083                 struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1084
1085                 rc = acpi_nfit_ars_rescan(acpi_desc, 0);
1086         }
1087         device_unlock(dev);
1088         if (rc)
1089                 return rc;
1090         return size;
1091 }
1092 static DEVICE_ATTR_RW(scrub);
1093
1094 static bool ars_supported(struct nvdimm_bus *nvdimm_bus)
1095 {
1096         struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1097         const unsigned long mask = 1 << ND_CMD_ARS_CAP | 1 << ND_CMD_ARS_START
1098                 | 1 << ND_CMD_ARS_STATUS;
1099
1100         return (nd_desc->cmd_mask & mask) == mask;
1101 }
1102
1103 static umode_t nfit_visible(struct kobject *kobj, struct attribute *a, int n)
1104 {
1105         struct device *dev = container_of(kobj, struct device, kobj);
1106         struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
1107
1108         if (a == &dev_attr_scrub.attr && !ars_supported(nvdimm_bus))
1109                 return 0;
1110         return a->mode;
1111 }
1112
1113 static struct attribute *acpi_nfit_attributes[] = {
1114         &dev_attr_revision.attr,
1115         &dev_attr_scrub.attr,
1116         &dev_attr_hw_error_scrub.attr,
1117         &dev_attr_bus_dsm_mask.attr,
1118         NULL,
1119 };
1120
1121 static const struct attribute_group acpi_nfit_attribute_group = {
1122         .name = "nfit",
1123         .attrs = acpi_nfit_attributes,
1124         .is_visible = nfit_visible,
1125 };
1126
1127 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
1128         &nvdimm_bus_attribute_group,
1129         &acpi_nfit_attribute_group,
1130         NULL,
1131 };
1132
1133 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
1134 {
1135         struct nvdimm *nvdimm = to_nvdimm(dev);
1136         struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1137
1138         return __to_nfit_memdev(nfit_mem);
1139 }
1140
1141 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
1142 {
1143         struct nvdimm *nvdimm = to_nvdimm(dev);
1144         struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1145
1146         return nfit_mem->dcr;
1147 }
1148
1149 static ssize_t handle_show(struct device *dev,
1150                 struct device_attribute *attr, char *buf)
1151 {
1152         struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1153
1154         return sprintf(buf, "%#x\n", memdev->device_handle);
1155 }
1156 static DEVICE_ATTR_RO(handle);
1157
1158 static ssize_t phys_id_show(struct device *dev,
1159                 struct device_attribute *attr, char *buf)
1160 {
1161         struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
1162
1163         return sprintf(buf, "%#x\n", memdev->physical_id);
1164 }
1165 static DEVICE_ATTR_RO(phys_id);
1166
1167 static ssize_t vendor_show(struct device *dev,
1168                 struct device_attribute *attr, char *buf)
1169 {
1170         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1171
1172         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
1173 }
1174 static DEVICE_ATTR_RO(vendor);
1175
1176 static ssize_t rev_id_show(struct device *dev,
1177                 struct device_attribute *attr, char *buf)
1178 {
1179         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1180
1181         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
1182 }
1183 static DEVICE_ATTR_RO(rev_id);
1184
1185 static ssize_t device_show(struct device *dev,
1186                 struct device_attribute *attr, char *buf)
1187 {
1188         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1189
1190         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
1191 }
1192 static DEVICE_ATTR_RO(device);
1193
1194 static ssize_t subsystem_vendor_show(struct device *dev,
1195                 struct device_attribute *attr, char *buf)
1196 {
1197         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1198
1199         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
1200 }
1201 static DEVICE_ATTR_RO(subsystem_vendor);
1202
1203 static ssize_t subsystem_rev_id_show(struct device *dev,
1204                 struct device_attribute *attr, char *buf)
1205 {
1206         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1207
1208         return sprintf(buf, "0x%04x\n",
1209                         be16_to_cpu(dcr->subsystem_revision_id));
1210 }
1211 static DEVICE_ATTR_RO(subsystem_rev_id);
1212
1213 static ssize_t subsystem_device_show(struct device *dev,
1214                 struct device_attribute *attr, char *buf)
1215 {
1216         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1217
1218         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
1219 }
1220 static DEVICE_ATTR_RO(subsystem_device);
1221
1222 static int num_nvdimm_formats(struct nvdimm *nvdimm)
1223 {
1224         struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1225         int formats = 0;
1226
1227         if (nfit_mem->memdev_pmem)
1228                 formats++;
1229         if (nfit_mem->memdev_bdw)
1230                 formats++;
1231         return formats;
1232 }
1233
1234 static ssize_t format_show(struct device *dev,
1235                 struct device_attribute *attr, char *buf)
1236 {
1237         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1238
1239         return sprintf(buf, "0x%04x\n", le16_to_cpu(dcr->code));
1240 }
1241 static DEVICE_ATTR_RO(format);
1242
1243 static ssize_t format1_show(struct device *dev,
1244                 struct device_attribute *attr, char *buf)
1245 {
1246         u32 handle;
1247         ssize_t rc = -ENXIO;
1248         struct nfit_mem *nfit_mem;
1249         struct nfit_memdev *nfit_memdev;
1250         struct acpi_nfit_desc *acpi_desc;
1251         struct nvdimm *nvdimm = to_nvdimm(dev);
1252         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1253
1254         nfit_mem = nvdimm_provider_data(nvdimm);
1255         acpi_desc = nfit_mem->acpi_desc;
1256         handle = to_nfit_memdev(dev)->device_handle;
1257
1258         /* assumes DIMMs have at most 2 published interface codes */
1259         mutex_lock(&acpi_desc->init_mutex);
1260         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1261                 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1262                 struct nfit_dcr *nfit_dcr;
1263
1264                 if (memdev->device_handle != handle)
1265                         continue;
1266
1267                 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
1268                         if (nfit_dcr->dcr->region_index != memdev->region_index)
1269                                 continue;
1270                         if (nfit_dcr->dcr->code == dcr->code)
1271                                 continue;
1272                         rc = sprintf(buf, "0x%04x\n",
1273                                         le16_to_cpu(nfit_dcr->dcr->code));
1274                         break;
1275                 }
1276                 if (rc != -ENXIO)
1277                         break;
1278         }
1279         mutex_unlock(&acpi_desc->init_mutex);
1280         return rc;
1281 }
1282 static DEVICE_ATTR_RO(format1);
1283
1284 static ssize_t formats_show(struct device *dev,
1285                 struct device_attribute *attr, char *buf)
1286 {
1287         struct nvdimm *nvdimm = to_nvdimm(dev);
1288
1289         return sprintf(buf, "%d\n", num_nvdimm_formats(nvdimm));
1290 }
1291 static DEVICE_ATTR_RO(formats);
1292
1293 static ssize_t serial_show(struct device *dev,
1294                 struct device_attribute *attr, char *buf)
1295 {
1296         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1297
1298         return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
1299 }
1300 static DEVICE_ATTR_RO(serial);
1301
1302 static ssize_t family_show(struct device *dev,
1303                 struct device_attribute *attr, char *buf)
1304 {
1305         struct nvdimm *nvdimm = to_nvdimm(dev);
1306         struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1307
1308         if (nfit_mem->family < 0)
1309                 return -ENXIO;
1310         return sprintf(buf, "%d\n", nfit_mem->family);
1311 }
1312 static DEVICE_ATTR_RO(family);
1313
1314 static ssize_t dsm_mask_show(struct device *dev,
1315                 struct device_attribute *attr, char *buf)
1316 {
1317         struct nvdimm *nvdimm = to_nvdimm(dev);
1318         struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1319
1320         if (nfit_mem->family < 0)
1321                 return -ENXIO;
1322         return sprintf(buf, "%#lx\n", nfit_mem->dsm_mask);
1323 }
1324 static DEVICE_ATTR_RO(dsm_mask);
1325
1326 static ssize_t flags_show(struct device *dev,
1327                 struct device_attribute *attr, char *buf)
1328 {
1329         u16 flags = to_nfit_memdev(dev)->flags;
1330
1331         return sprintf(buf, "%s%s%s%s%s%s%s\n",
1332                 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
1333                 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
1334                 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
1335                 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
1336                 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "",
1337                 flags & ACPI_NFIT_MEM_MAP_FAILED ? "map_fail " : "",
1338                 flags & ACPI_NFIT_MEM_HEALTH_ENABLED ? "smart_notify " : "");
1339 }
1340 static DEVICE_ATTR_RO(flags);
1341
1342 static ssize_t id_show(struct device *dev,
1343                 struct device_attribute *attr, char *buf)
1344 {
1345         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
1346
1347         if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
1348                 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
1349                                 be16_to_cpu(dcr->vendor_id),
1350                                 dcr->manufacturing_location,
1351                                 be16_to_cpu(dcr->manufacturing_date),
1352                                 be32_to_cpu(dcr->serial_number));
1353         else
1354                 return sprintf(buf, "%04x-%08x\n",
1355                                 be16_to_cpu(dcr->vendor_id),
1356                                 be32_to_cpu(dcr->serial_number));
1357 }
1358 static DEVICE_ATTR_RO(id);
1359
1360 static struct attribute *acpi_nfit_dimm_attributes[] = {
1361         &dev_attr_handle.attr,
1362         &dev_attr_phys_id.attr,
1363         &dev_attr_vendor.attr,
1364         &dev_attr_device.attr,
1365         &dev_attr_rev_id.attr,
1366         &dev_attr_subsystem_vendor.attr,
1367         &dev_attr_subsystem_device.attr,
1368         &dev_attr_subsystem_rev_id.attr,
1369         &dev_attr_format.attr,
1370         &dev_attr_formats.attr,
1371         &dev_attr_format1.attr,
1372         &dev_attr_serial.attr,
1373         &dev_attr_flags.attr,
1374         &dev_attr_id.attr,
1375         &dev_attr_family.attr,
1376         &dev_attr_dsm_mask.attr,
1377         NULL,
1378 };
1379
1380 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
1381                 struct attribute *a, int n)
1382 {
1383         struct device *dev = container_of(kobj, struct device, kobj);
1384         struct nvdimm *nvdimm = to_nvdimm(dev);
1385
1386         if (!to_nfit_dcr(dev)) {
1387                 /* Without a dcr only the memdev attributes can be surfaced */
1388                 if (a == &dev_attr_handle.attr || a == &dev_attr_phys_id.attr
1389                                 || a == &dev_attr_flags.attr
1390                                 || a == &dev_attr_family.attr
1391                                 || a == &dev_attr_dsm_mask.attr)
1392                         return a->mode;
1393                 return 0;
1394         }
1395
1396         if (a == &dev_attr_format1.attr && num_nvdimm_formats(nvdimm) <= 1)
1397                 return 0;
1398         return a->mode;
1399 }
1400
1401 static const struct attribute_group acpi_nfit_dimm_attribute_group = {
1402         .name = "nfit",
1403         .attrs = acpi_nfit_dimm_attributes,
1404         .is_visible = acpi_nfit_dimm_attr_visible,
1405 };
1406
1407 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
1408         &nvdimm_attribute_group,
1409         &nd_device_attribute_group,
1410         &acpi_nfit_dimm_attribute_group,
1411         NULL,
1412 };
1413
1414 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
1415                 u32 device_handle)
1416 {
1417         struct nfit_mem *nfit_mem;
1418
1419         list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
1420                 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
1421                         return nfit_mem->nvdimm;
1422
1423         return NULL;
1424 }
1425
1426 void __acpi_nvdimm_notify(struct device *dev, u32 event)
1427 {
1428         struct nfit_mem *nfit_mem;
1429         struct acpi_nfit_desc *acpi_desc;
1430
1431         dev_dbg(dev->parent, "%s: %s: event: %d\n", dev_name(dev), __func__,
1432                         event);
1433
1434         if (event != NFIT_NOTIFY_DIMM_HEALTH) {
1435                 dev_dbg(dev->parent, "%s: unknown event: %d\n", dev_name(dev),
1436                                 event);
1437                 return;
1438         }
1439
1440         acpi_desc = dev_get_drvdata(dev->parent);
1441         if (!acpi_desc)
1442                 return;
1443
1444         /*
1445          * If we successfully retrieved acpi_desc, then we know nfit_mem data
1446          * is still valid.
1447          */
1448         nfit_mem = dev_get_drvdata(dev);
1449         if (nfit_mem && nfit_mem->flags_attr)
1450                 sysfs_notify_dirent(nfit_mem->flags_attr);
1451 }
1452 EXPORT_SYMBOL_GPL(__acpi_nvdimm_notify);
1453
1454 static void acpi_nvdimm_notify(acpi_handle handle, u32 event, void *data)
1455 {
1456         struct acpi_device *adev = data;
1457         struct device *dev = &adev->dev;
1458
1459         device_lock(dev->parent);
1460         __acpi_nvdimm_notify(dev, event);
1461         device_unlock(dev->parent);
1462 }
1463
1464 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
1465                 struct nfit_mem *nfit_mem, u32 device_handle)
1466 {
1467         struct acpi_device *adev, *adev_dimm;
1468         struct device *dev = acpi_desc->dev;
1469         unsigned long dsm_mask;
1470         const guid_t *guid;
1471         int i;
1472         int family = -1;
1473
1474         /* nfit test assumes 1:1 relationship between commands and dsms */
1475         nfit_mem->dsm_mask = acpi_desc->dimm_cmd_force_en;
1476         nfit_mem->family = NVDIMM_FAMILY_INTEL;
1477         adev = to_acpi_dev(acpi_desc);
1478         if (!adev)
1479                 return 0;
1480
1481         adev_dimm = acpi_find_child_device(adev, device_handle, false);
1482         nfit_mem->adev = adev_dimm;
1483         if (!adev_dimm) {
1484                 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
1485                                 device_handle);
1486                 return force_enable_dimms ? 0 : -ENODEV;
1487         }
1488
1489         if (ACPI_FAILURE(acpi_install_notify_handler(adev_dimm->handle,
1490                 ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify, adev_dimm))) {
1491                 dev_err(dev, "%s: notification registration failed\n",
1492                                 dev_name(&adev_dimm->dev));
1493                 return -ENXIO;
1494         }
1495         /*
1496          * Record nfit_mem for the notification path to track back to
1497          * the nfit sysfs attributes for this dimm device object.
1498          */
1499         dev_set_drvdata(&adev_dimm->dev, nfit_mem);
1500
1501         /*
1502          * Until standardization materializes we need to consider 4
1503          * different command sets.  Note, that checking for function0 (bit0)
1504          * tells us if any commands are reachable through this GUID.
1505          */
1506         for (i = NVDIMM_FAMILY_INTEL; i <= NVDIMM_FAMILY_MSFT; i++)
1507                 if (acpi_check_dsm(adev_dimm->handle, to_nfit_uuid(i), 1, 1))
1508                         if (family < 0 || i == default_dsm_family)
1509                                 family = i;
1510
1511         /* limit the supported commands to those that are publicly documented */
1512         nfit_mem->family = family;
1513         if (override_dsm_mask && !disable_vendor_specific)
1514                 dsm_mask = override_dsm_mask;
1515         else if (nfit_mem->family == NVDIMM_FAMILY_INTEL) {
1516                 dsm_mask = 0x3fe;
1517                 if (disable_vendor_specific)
1518                         dsm_mask &= ~(1 << ND_CMD_VENDOR);
1519         } else if (nfit_mem->family == NVDIMM_FAMILY_HPE1) {
1520                 dsm_mask = 0x1c3c76;
1521         } else if (nfit_mem->family == NVDIMM_FAMILY_HPE2) {
1522                 dsm_mask = 0x1fe;
1523                 if (disable_vendor_specific)
1524                         dsm_mask &= ~(1 << 8);
1525         } else if (nfit_mem->family == NVDIMM_FAMILY_MSFT) {
1526                 dsm_mask = 0xffffffff;
1527         } else {
1528                 dev_dbg(dev, "unknown dimm command family\n");
1529                 nfit_mem->family = -1;
1530                 /* DSMs are optional, continue loading the driver... */
1531                 return 0;
1532         }
1533
1534         /*
1535          * Function 0 is the command interrogation function, don't
1536          * export it to potential userspace use, and enable it to be
1537          * used as an error value in acpi_nfit_ctl().
1538          */
1539         dsm_mask &= ~1UL;
1540
1541         guid = to_nfit_uuid(nfit_mem->family);
1542         for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1543                 if (acpi_check_dsm(adev_dimm->handle, guid, 1, 1ULL << i))
1544                         set_bit(i, &nfit_mem->dsm_mask);
1545
1546         return 0;
1547 }
1548
1549 static void shutdown_dimm_notify(void *data)
1550 {
1551         struct acpi_nfit_desc *acpi_desc = data;
1552         struct nfit_mem *nfit_mem;
1553
1554         mutex_lock(&acpi_desc->init_mutex);
1555         /*
1556          * Clear out the nfit_mem->flags_attr and shut down dimm event
1557          * notifications.
1558          */
1559         list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1560                 struct acpi_device *adev_dimm = nfit_mem->adev;
1561
1562                 if (nfit_mem->flags_attr) {
1563                         sysfs_put(nfit_mem->flags_attr);
1564                         nfit_mem->flags_attr = NULL;
1565                 }
1566                 if (adev_dimm) {
1567                         acpi_remove_notify_handler(adev_dimm->handle,
1568                                         ACPI_DEVICE_NOTIFY, acpi_nvdimm_notify);
1569                         dev_set_drvdata(&adev_dimm->dev, NULL);
1570                 }
1571         }
1572         mutex_unlock(&acpi_desc->init_mutex);
1573 }
1574
1575 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1576 {
1577         struct nfit_mem *nfit_mem;
1578         int dimm_count = 0, rc;
1579         struct nvdimm *nvdimm;
1580
1581         list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1582                 struct acpi_nfit_flush_address *flush;
1583                 unsigned long flags = 0, cmd_mask;
1584                 struct nfit_memdev *nfit_memdev;
1585                 u32 device_handle;
1586                 u16 mem_flags;
1587
1588                 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1589                 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1590                 if (nvdimm) {
1591                         dimm_count++;
1592                         continue;
1593                 }
1594
1595                 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1596                         set_bit(NDD_ALIASING, &flags);
1597
1598                 /* collate flags across all memdevs for this dimm */
1599                 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1600                         struct acpi_nfit_memory_map *dimm_memdev;
1601
1602                         dimm_memdev = __to_nfit_memdev(nfit_mem);
1603                         if (dimm_memdev->device_handle
1604                                         != nfit_memdev->memdev->device_handle)
1605                                 continue;
1606                         dimm_memdev->flags |= nfit_memdev->memdev->flags;
1607                 }
1608
1609                 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
1610                 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
1611                         set_bit(NDD_UNARMED, &flags);
1612
1613                 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1614                 if (rc)
1615                         continue;
1616
1617                 /*
1618                  * TODO: provide translation for non-NVDIMM_FAMILY_INTEL
1619                  * devices (i.e. from nd_cmd to acpi_dsm) to standardize the
1620                  * userspace interface.
1621                  */
1622                 cmd_mask = 1UL << ND_CMD_CALL;
1623                 if (nfit_mem->family == NVDIMM_FAMILY_INTEL)
1624                         cmd_mask |= nfit_mem->dsm_mask;
1625
1626                 flush = nfit_mem->nfit_flush ? nfit_mem->nfit_flush->flush
1627                         : NULL;
1628                 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
1629                                 acpi_nfit_dimm_attribute_groups,
1630                                 flags, cmd_mask, flush ? flush->hint_count : 0,
1631                                 nfit_mem->flush_wpq);
1632                 if (!nvdimm)
1633                         return -ENOMEM;
1634
1635                 nfit_mem->nvdimm = nvdimm;
1636                 dimm_count++;
1637
1638                 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1639                         continue;
1640
1641                 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s%s\n",
1642                                 nvdimm_name(nvdimm),
1643                   mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1644                   mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1645                   mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
1646                   mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "",
1647                   mem_flags & ACPI_NFIT_MEM_MAP_FAILED ? " map_fail" : "");
1648
1649         }
1650
1651         rc = nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1652         if (rc)
1653                 return rc;
1654
1655         /*
1656          * Now that dimms are successfully registered, and async registration
1657          * is flushed, attempt to enable event notification.
1658          */
1659         list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1660                 struct kernfs_node *nfit_kernfs;
1661
1662                 nvdimm = nfit_mem->nvdimm;
1663                 if (!nvdimm)
1664                         continue;
1665
1666                 nfit_kernfs = sysfs_get_dirent(nvdimm_kobj(nvdimm)->sd, "nfit");
1667                 if (nfit_kernfs)
1668                         nfit_mem->flags_attr = sysfs_get_dirent(nfit_kernfs,
1669                                         "flags");
1670                 sysfs_put(nfit_kernfs);
1671                 if (!nfit_mem->flags_attr)
1672                         dev_warn(acpi_desc->dev, "%s: notifications disabled\n",
1673                                         nvdimm_name(nvdimm));
1674         }
1675
1676         return devm_add_action_or_reset(acpi_desc->dev, shutdown_dimm_notify,
1677                         acpi_desc);
1678 }
1679
1680 /*
1681  * These constants are private because there are no kernel consumers of
1682  * these commands.
1683  */
1684 enum nfit_aux_cmds {
1685         NFIT_CMD_TRANSLATE_SPA = 5,
1686         NFIT_CMD_ARS_INJECT_SET = 7,
1687         NFIT_CMD_ARS_INJECT_CLEAR = 8,
1688         NFIT_CMD_ARS_INJECT_GET = 9,
1689 };
1690
1691 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1692 {
1693         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1694         const guid_t *guid = to_nfit_uuid(NFIT_DEV_BUS);
1695         struct acpi_device *adev;
1696         unsigned long dsm_mask;
1697         int i;
1698
1699         nd_desc->cmd_mask = acpi_desc->bus_cmd_force_en;
1700         adev = to_acpi_dev(acpi_desc);
1701         if (!adev)
1702                 return;
1703
1704         for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
1705                 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1706                         set_bit(i, &nd_desc->cmd_mask);
1707         set_bit(ND_CMD_CALL, &nd_desc->cmd_mask);
1708
1709         dsm_mask =
1710                 (1 << ND_CMD_ARS_CAP) |
1711                 (1 << ND_CMD_ARS_START) |
1712                 (1 << ND_CMD_ARS_STATUS) |
1713                 (1 << ND_CMD_CLEAR_ERROR) |
1714                 (1 << NFIT_CMD_TRANSLATE_SPA) |
1715                 (1 << NFIT_CMD_ARS_INJECT_SET) |
1716                 (1 << NFIT_CMD_ARS_INJECT_CLEAR) |
1717                 (1 << NFIT_CMD_ARS_INJECT_GET);
1718         for_each_set_bit(i, &dsm_mask, BITS_PER_LONG)
1719                 if (acpi_check_dsm(adev->handle, guid, 1, 1ULL << i))
1720                         set_bit(i, &nd_desc->bus_dsm_mask);
1721 }
1722
1723 static ssize_t range_index_show(struct device *dev,
1724                 struct device_attribute *attr, char *buf)
1725 {
1726         struct nd_region *nd_region = to_nd_region(dev);
1727         struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1728
1729         return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1730 }
1731 static DEVICE_ATTR_RO(range_index);
1732
1733 static ssize_t ecc_unit_size_show(struct device *dev,
1734                 struct device_attribute *attr, char *buf)
1735 {
1736         struct nd_region *nd_region = to_nd_region(dev);
1737         struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1738
1739         return sprintf(buf, "%d\n", nfit_spa->clear_err_unit);
1740 }
1741 static DEVICE_ATTR_RO(ecc_unit_size);
1742
1743 static struct attribute *acpi_nfit_region_attributes[] = {
1744         &dev_attr_range_index.attr,
1745         &dev_attr_ecc_unit_size.attr,
1746         NULL,
1747 };
1748
1749 static const struct attribute_group acpi_nfit_region_attribute_group = {
1750         .name = "nfit",
1751         .attrs = acpi_nfit_region_attributes,
1752 };
1753
1754 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1755         &nd_region_attribute_group,
1756         &nd_mapping_attribute_group,
1757         &nd_device_attribute_group,
1758         &nd_numa_attribute_group,
1759         &acpi_nfit_region_attribute_group,
1760         NULL,
1761 };
1762
1763 /* enough info to uniquely specify an interleave set */
1764 struct nfit_set_info {
1765         struct nfit_set_info_map {
1766                 u64 region_offset;
1767                 u32 serial_number;
1768                 u32 pad;
1769         } mapping[0];
1770 };
1771
1772 struct nfit_set_info2 {
1773         struct nfit_set_info_map2 {
1774                 u64 region_offset;
1775                 u32 serial_number;
1776                 u16 vendor_id;
1777                 u16 manufacturing_date;
1778                 u8  manufacturing_location;
1779                 u8  reserved[31];
1780         } mapping[0];
1781 };
1782
1783 static size_t sizeof_nfit_set_info(int num_mappings)
1784 {
1785         return sizeof(struct nfit_set_info)
1786                 + num_mappings * sizeof(struct nfit_set_info_map);
1787 }
1788
1789 static size_t sizeof_nfit_set_info2(int num_mappings)
1790 {
1791         return sizeof(struct nfit_set_info2)
1792                 + num_mappings * sizeof(struct nfit_set_info_map2);
1793 }
1794
1795 static int cmp_map_compat(const void *m0, const void *m1)
1796 {
1797         const struct nfit_set_info_map *map0 = m0;
1798         const struct nfit_set_info_map *map1 = m1;
1799
1800         return memcmp(&map0->region_offset, &map1->region_offset,
1801                         sizeof(u64));
1802 }
1803
1804 static int cmp_map(const void *m0, const void *m1)
1805 {
1806         const struct nfit_set_info_map *map0 = m0;
1807         const struct nfit_set_info_map *map1 = m1;
1808
1809         if (map0->region_offset < map1->region_offset)
1810                 return -1;
1811         else if (map0->region_offset > map1->region_offset)
1812                 return 1;
1813         return 0;
1814 }
1815
1816 static int cmp_map2(const void *m0, const void *m1)
1817 {
1818         const struct nfit_set_info_map2 *map0 = m0;
1819         const struct nfit_set_info_map2 *map1 = m1;
1820
1821         if (map0->region_offset < map1->region_offset)
1822                 return -1;
1823         else if (map0->region_offset > map1->region_offset)
1824                 return 1;
1825         return 0;
1826 }
1827
1828 /* Retrieve the nth entry referencing this spa */
1829 static struct acpi_nfit_memory_map *memdev_from_spa(
1830                 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1831 {
1832         struct nfit_memdev *nfit_memdev;
1833
1834         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1835                 if (nfit_memdev->memdev->range_index == range_index)
1836                         if (n-- == 0)
1837                                 return nfit_memdev->memdev;
1838         return NULL;
1839 }
1840
1841 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1842                 struct nd_region_desc *ndr_desc,
1843                 struct acpi_nfit_system_address *spa)
1844 {
1845         struct device *dev = acpi_desc->dev;
1846         struct nd_interleave_set *nd_set;
1847         u16 nr = ndr_desc->num_mappings;
1848         struct nfit_set_info2 *info2;
1849         struct nfit_set_info *info;
1850         int i;
1851
1852         nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1853         if (!nd_set)
1854                 return -ENOMEM;
1855         ndr_desc->nd_set = nd_set;
1856         guid_copy(&nd_set->type_guid, (guid_t *) spa->range_guid);
1857
1858         info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1859         if (!info)
1860                 return -ENOMEM;
1861
1862         info2 = devm_kzalloc(dev, sizeof_nfit_set_info2(nr), GFP_KERNEL);
1863         if (!info2)
1864                 return -ENOMEM;
1865
1866         for (i = 0; i < nr; i++) {
1867                 struct nd_mapping_desc *mapping = &ndr_desc->mapping[i];
1868                 struct nfit_set_info_map *map = &info->mapping[i];
1869                 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
1870                 struct nvdimm *nvdimm = mapping->nvdimm;
1871                 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1872                 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1873                                 spa->range_index, i);
1874                 struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1875
1876                 if (!memdev || !nfit_mem->dcr) {
1877                         dev_err(dev, "%s: failed to find DCR\n", __func__);
1878                         return -ENODEV;
1879                 }
1880
1881                 map->region_offset = memdev->region_offset;
1882                 map->serial_number = dcr->serial_number;
1883
1884                 map2->region_offset = memdev->region_offset;
1885                 map2->serial_number = dcr->serial_number;
1886                 map2->vendor_id = dcr->vendor_id;
1887                 map2->manufacturing_date = dcr->manufacturing_date;
1888                 map2->manufacturing_location = dcr->manufacturing_location;
1889         }
1890
1891         /* v1.1 namespaces */
1892         sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1893                         cmp_map, NULL);
1894         nd_set->cookie1 = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1895
1896         /* v1.2 namespaces */
1897         sort(&info2->mapping[0], nr, sizeof(struct nfit_set_info_map2),
1898                         cmp_map2, NULL);
1899         nd_set->cookie2 = nd_fletcher64(info2, sizeof_nfit_set_info2(nr), 0);
1900
1901         /* support v1.1 namespaces created with the wrong sort order */
1902         sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1903                         cmp_map_compat, NULL);
1904         nd_set->altcookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1905
1906         /* record the result of the sort for the mapping position */
1907         for (i = 0; i < nr; i++) {
1908                 struct nfit_set_info_map2 *map2 = &info2->mapping[i];
1909                 int j;
1910
1911                 for (j = 0; j < nr; j++) {
1912                         struct nd_mapping_desc *mapping = &ndr_desc->mapping[j];
1913                         struct nvdimm *nvdimm = mapping->nvdimm;
1914                         struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1915                         struct acpi_nfit_control_region *dcr = nfit_mem->dcr;
1916
1917                         if (map2->serial_number == dcr->serial_number &&
1918                             map2->vendor_id == dcr->vendor_id &&
1919                             map2->manufacturing_date == dcr->manufacturing_date &&
1920                             map2->manufacturing_location
1921                                     == dcr->manufacturing_location) {
1922                                 mapping->position = i;
1923                                 break;
1924                         }
1925                 }
1926         }
1927
1928         ndr_desc->nd_set = nd_set;
1929         devm_kfree(dev, info);
1930         devm_kfree(dev, info2);
1931
1932         return 0;
1933 }
1934
1935 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1936 {
1937         struct acpi_nfit_interleave *idt = mmio->idt;
1938         u32 sub_line_offset, line_index, line_offset;
1939         u64 line_no, table_skip_count, table_offset;
1940
1941         line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1942         table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1943         line_offset = idt->line_offset[line_index]
1944                 * mmio->line_size;
1945         table_offset = table_skip_count * mmio->table_size;
1946
1947         return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1948 }
1949
1950 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
1951 {
1952         struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1953         u64 offset = nfit_blk->stat_offset + mmio->size * bw;
1954         const u32 STATUS_MASK = 0x80000037;
1955
1956         if (mmio->num_lines)
1957                 offset = to_interleave_offset(offset, mmio);
1958
1959         return readl(mmio->addr.base + offset) & STATUS_MASK;
1960 }
1961
1962 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1963                 resource_size_t dpa, unsigned int len, unsigned int write)
1964 {
1965         u64 cmd, offset;
1966         struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1967
1968         enum {
1969                 BCW_OFFSET_MASK = (1ULL << 48)-1,
1970                 BCW_LEN_SHIFT = 48,
1971                 BCW_LEN_MASK = (1ULL << 8) - 1,
1972                 BCW_CMD_SHIFT = 56,
1973         };
1974
1975         cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1976         len = len >> L1_CACHE_SHIFT;
1977         cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1978         cmd |= ((u64) write) << BCW_CMD_SHIFT;
1979
1980         offset = nfit_blk->cmd_offset + mmio->size * bw;
1981         if (mmio->num_lines)
1982                 offset = to_interleave_offset(offset, mmio);
1983
1984         writeq(cmd, mmio->addr.base + offset);
1985         nvdimm_flush(nfit_blk->nd_region);
1986
1987         if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
1988                 readq(mmio->addr.base + offset);
1989 }
1990
1991 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1992                 resource_size_t dpa, void *iobuf, size_t len, int rw,
1993                 unsigned int lane)
1994 {
1995         struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1996         unsigned int copied = 0;
1997         u64 base_offset;
1998         int rc;
1999
2000         base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
2001                 + lane * mmio->size;
2002         write_blk_ctl(nfit_blk, lane, dpa, len, rw);
2003         while (len) {
2004                 unsigned int c;
2005                 u64 offset;
2006
2007                 if (mmio->num_lines) {
2008                         u32 line_offset;
2009
2010                         offset = to_interleave_offset(base_offset + copied,
2011                                         mmio);
2012                         div_u64_rem(offset, mmio->line_size, &line_offset);
2013                         c = min_t(size_t, len, mmio->line_size - line_offset);
2014                 } else {
2015                         offset = base_offset + nfit_blk->bdw_offset;
2016                         c = len;
2017                 }
2018
2019                 if (rw)
2020                         memcpy_flushcache(mmio->addr.aperture + offset, iobuf + copied, c);
2021                 else {
2022                         if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
2023                                 arch_invalidate_pmem((void __force *)
2024                                         mmio->addr.aperture + offset, c);
2025
2026                         memcpy(iobuf + copied, mmio->addr.aperture + offset, c);
2027                 }
2028
2029                 copied += c;
2030                 len -= c;
2031         }
2032
2033         if (rw)
2034                 nvdimm_flush(nfit_blk->nd_region);
2035
2036         rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
2037         return rc;
2038 }
2039
2040 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
2041                 resource_size_t dpa, void *iobuf, u64 len, int rw)
2042 {
2043         struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
2044         struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
2045         struct nd_region *nd_region = nfit_blk->nd_region;
2046         unsigned int lane, copied = 0;
2047         int rc = 0;
2048
2049         lane = nd_region_acquire_lane(nd_region);
2050         while (len) {
2051                 u64 c = min(len, mmio->size);
2052
2053                 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
2054                                 iobuf + copied, c, rw, lane);
2055                 if (rc)
2056                         break;
2057
2058                 copied += c;
2059                 len -= c;
2060         }
2061         nd_region_release_lane(nd_region, lane);
2062
2063         return rc;
2064 }
2065
2066 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
2067                 struct acpi_nfit_interleave *idt, u16 interleave_ways)
2068 {
2069         if (idt) {
2070                 mmio->num_lines = idt->line_count;
2071                 mmio->line_size = idt->line_size;
2072                 if (interleave_ways == 0)
2073                         return -ENXIO;
2074                 mmio->table_size = mmio->num_lines * interleave_ways
2075                         * mmio->line_size;
2076         }
2077
2078         return 0;
2079 }
2080
2081 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
2082                 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
2083 {
2084         struct nd_cmd_dimm_flags flags;
2085         int rc;
2086
2087         memset(&flags, 0, sizeof(flags));
2088         rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
2089                         sizeof(flags), NULL);
2090
2091         if (rc >= 0 && flags.status == 0)
2092                 nfit_blk->dimm_flags = flags.flags;
2093         else if (rc == -ENOTTY) {
2094                 /* fall back to a conservative default */
2095                 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
2096                 rc = 0;
2097         } else
2098                 rc = -ENXIO;
2099
2100         return rc;
2101 }
2102
2103 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
2104                 struct device *dev)
2105 {
2106         struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
2107         struct nd_blk_region *ndbr = to_nd_blk_region(dev);
2108         struct nfit_blk_mmio *mmio;
2109         struct nfit_blk *nfit_blk;
2110         struct nfit_mem *nfit_mem;
2111         struct nvdimm *nvdimm;
2112         int rc;
2113
2114         nvdimm = nd_blk_region_to_dimm(ndbr);
2115         nfit_mem = nvdimm_provider_data(nvdimm);
2116         if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
2117                 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
2118                                 nfit_mem ? "" : " nfit_mem",
2119                                 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
2120                                 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
2121                 return -ENXIO;
2122         }
2123
2124         nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
2125         if (!nfit_blk)
2126                 return -ENOMEM;
2127         nd_blk_region_set_provider_data(ndbr, nfit_blk);
2128         nfit_blk->nd_region = to_nd_region(dev);
2129
2130         /* map block aperture memory */
2131         nfit_blk->bdw_offset = nfit_mem->bdw->offset;
2132         mmio = &nfit_blk->mmio[BDW];
2133         mmio->addr.base = devm_nvdimm_memremap(dev, nfit_mem->spa_bdw->address,
2134                         nfit_mem->spa_bdw->length, nd_blk_memremap_flags(ndbr));
2135         if (!mmio->addr.base) {
2136                 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
2137                                 nvdimm_name(nvdimm));
2138                 return -ENOMEM;
2139         }
2140         mmio->size = nfit_mem->bdw->size;
2141         mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
2142         mmio->idt = nfit_mem->idt_bdw;
2143         mmio->spa = nfit_mem->spa_bdw;
2144         rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
2145                         nfit_mem->memdev_bdw->interleave_ways);
2146         if (rc) {
2147                 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
2148                                 __func__, nvdimm_name(nvdimm));
2149                 return rc;
2150         }
2151
2152         /* map block control memory */
2153         nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
2154         nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
2155         mmio = &nfit_blk->mmio[DCR];
2156         mmio->addr.base = devm_nvdimm_ioremap(dev, nfit_mem->spa_dcr->address,
2157                         nfit_mem->spa_dcr->length);
2158         if (!mmio->addr.base) {
2159                 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
2160                                 nvdimm_name(nvdimm));
2161                 return -ENOMEM;
2162         }
2163         mmio->size = nfit_mem->dcr->window_size;
2164         mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
2165         mmio->idt = nfit_mem->idt_dcr;
2166         mmio->spa = nfit_mem->spa_dcr;
2167         rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
2168                         nfit_mem->memdev_dcr->interleave_ways);
2169         if (rc) {
2170                 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
2171                                 __func__, nvdimm_name(nvdimm));
2172                 return rc;
2173         }
2174
2175         rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
2176         if (rc < 0) {
2177                 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
2178                                 __func__, nvdimm_name(nvdimm));
2179                 return rc;
2180         }
2181
2182         if (nvdimm_has_flush(nfit_blk->nd_region) < 0)
2183                 dev_warn(dev, "unable to guarantee persistence of writes\n");
2184
2185         if (mmio->line_size == 0)
2186                 return 0;
2187
2188         if ((u32) nfit_blk->cmd_offset % mmio->line_size
2189                         + 8 > mmio->line_size) {
2190                 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
2191                 return -ENXIO;
2192         } else if ((u32) nfit_blk->stat_offset % mmio->line_size
2193                         + 8 > mmio->line_size) {
2194                 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
2195                 return -ENXIO;
2196         }
2197
2198         return 0;
2199 }
2200
2201 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
2202                 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
2203 {
2204         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2205         struct acpi_nfit_system_address *spa = nfit_spa->spa;
2206         int cmd_rc, rc;
2207
2208         cmd->address = spa->address;
2209         cmd->length = spa->length;
2210         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
2211                         sizeof(*cmd), &cmd_rc);
2212         if (rc < 0)
2213                 return rc;
2214         return cmd_rc;
2215 }
2216
2217 static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
2218 {
2219         int rc;
2220         int cmd_rc;
2221         struct nd_cmd_ars_start ars_start;
2222         struct acpi_nfit_system_address *spa = nfit_spa->spa;
2223         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2224
2225         memset(&ars_start, 0, sizeof(ars_start));
2226         ars_start.address = spa->address;
2227         ars_start.length = spa->length;
2228         ars_start.flags = acpi_desc->ars_start_flags;
2229         if (nfit_spa_type(spa) == NFIT_SPA_PM)
2230                 ars_start.type = ND_ARS_PERSISTENT;
2231         else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
2232                 ars_start.type = ND_ARS_VOLATILE;
2233         else
2234                 return -ENOTTY;
2235
2236         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2237                         sizeof(ars_start), &cmd_rc);
2238
2239         if (rc < 0)
2240                 return rc;
2241         return cmd_rc;
2242 }
2243
2244 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
2245 {
2246         int rc, cmd_rc;
2247         struct nd_cmd_ars_start ars_start;
2248         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2249         struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2250
2251         memset(&ars_start, 0, sizeof(ars_start));
2252         ars_start.address = ars_status->restart_address;
2253         ars_start.length = ars_status->restart_length;
2254         ars_start.type = ars_status->type;
2255         ars_start.flags = acpi_desc->ars_start_flags;
2256         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
2257                         sizeof(ars_start), &cmd_rc);
2258         if (rc < 0)
2259                 return rc;
2260         return cmd_rc;
2261 }
2262
2263 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
2264 {
2265         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
2266         struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
2267         int rc, cmd_rc;
2268
2269         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
2270                         acpi_desc->ars_status_size, &cmd_rc);
2271         if (rc < 0)
2272                 return rc;
2273         return cmd_rc;
2274 }
2275
2276 static int ars_status_process_records(struct acpi_nfit_desc *acpi_desc,
2277                 struct nd_cmd_ars_status *ars_status)
2278 {
2279         struct nvdimm_bus *nvdimm_bus = acpi_desc->nvdimm_bus;
2280         int rc;
2281         u32 i;
2282
2283         /*
2284          * First record starts at 44 byte offset from the start of the
2285          * payload.
2286          */
2287         if (ars_status->out_length < 44)
2288                 return 0;
2289         for (i = 0; i < ars_status->num_records; i++) {
2290                 /* only process full records */
2291                 if (ars_status->out_length
2292                                 < 44 + sizeof(struct nd_ars_record) * (i + 1))
2293                         break;
2294                 rc = nvdimm_bus_add_poison(nvdimm_bus,
2295                                 ars_status->records[i].err_address,
2296                                 ars_status->records[i].length);
2297                 if (rc)
2298                         return rc;
2299         }
2300         if (i < ars_status->num_records)
2301                 dev_warn(acpi_desc->dev, "detected truncated ars results\n");
2302
2303         return 0;
2304 }
2305
2306 static void acpi_nfit_remove_resource(void *data)
2307 {
2308         struct resource *res = data;
2309
2310         remove_resource(res);
2311 }
2312
2313 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
2314                 struct nd_region_desc *ndr_desc)
2315 {
2316         struct resource *res, *nd_res = ndr_desc->res;
2317         int is_pmem, ret;
2318
2319         /* No operation if the region is already registered as PMEM */
2320         is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
2321                                 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
2322         if (is_pmem == REGION_INTERSECTS)
2323                 return 0;
2324
2325         res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
2326         if (!res)
2327                 return -ENOMEM;
2328
2329         res->name = "Persistent Memory";
2330         res->start = nd_res->start;
2331         res->end = nd_res->end;
2332         res->flags = IORESOURCE_MEM;
2333         res->desc = IORES_DESC_PERSISTENT_MEMORY;
2334
2335         ret = insert_resource(&iomem_resource, res);
2336         if (ret)
2337                 return ret;
2338
2339         ret = devm_add_action_or_reset(acpi_desc->dev,
2340                                         acpi_nfit_remove_resource,
2341                                         res);
2342         if (ret)
2343                 return ret;
2344
2345         return 0;
2346 }
2347
2348 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
2349                 struct nd_mapping_desc *mapping, struct nd_region_desc *ndr_desc,
2350                 struct acpi_nfit_memory_map *memdev,
2351                 struct nfit_spa *nfit_spa)
2352 {
2353         struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
2354                         memdev->device_handle);
2355         struct acpi_nfit_system_address *spa = nfit_spa->spa;
2356         struct nd_blk_region_desc *ndbr_desc;
2357         struct nfit_mem *nfit_mem;
2358         int rc;
2359
2360         if (!nvdimm) {
2361                 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
2362                                 spa->range_index, memdev->device_handle);
2363                 return -ENODEV;
2364         }
2365
2366         mapping->nvdimm = nvdimm;
2367         switch (nfit_spa_type(spa)) {
2368         case NFIT_SPA_PM:
2369         case NFIT_SPA_VOLATILE:
2370                 mapping->start = memdev->address;
2371                 mapping->size = memdev->region_size;
2372                 break;
2373         case NFIT_SPA_DCR:
2374                 nfit_mem = nvdimm_provider_data(nvdimm);
2375                 if (!nfit_mem || !nfit_mem->bdw) {
2376                         dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
2377                                         spa->range_index, nvdimm_name(nvdimm));
2378                         break;
2379                 }
2380
2381                 mapping->size = nfit_mem->bdw->capacity;
2382                 mapping->start = nfit_mem->bdw->start_address;
2383                 ndr_desc->num_lanes = nfit_mem->bdw->windows;
2384                 ndr_desc->mapping = mapping;
2385                 ndr_desc->num_mappings = 1;
2386                 ndbr_desc = to_blk_region_desc(ndr_desc);
2387                 ndbr_desc->enable = acpi_nfit_blk_region_enable;
2388                 ndbr_desc->do_io = acpi_desc->blk_do_io;
2389                 rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2390                 if (rc)
2391                         return rc;
2392                 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
2393                                 ndr_desc);
2394                 if (!nfit_spa->nd_region)
2395                         return -ENOMEM;
2396                 break;
2397         }
2398
2399         return 0;
2400 }
2401
2402 static bool nfit_spa_is_virtual(struct acpi_nfit_system_address *spa)
2403 {
2404         return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2405                 nfit_spa_type(spa) == NFIT_SPA_VCD   ||
2406                 nfit_spa_type(spa) == NFIT_SPA_PDISK ||
2407                 nfit_spa_type(spa) == NFIT_SPA_PCD);
2408 }
2409
2410 static bool nfit_spa_is_volatile(struct acpi_nfit_system_address *spa)
2411 {
2412         return (nfit_spa_type(spa) == NFIT_SPA_VDISK ||
2413                 nfit_spa_type(spa) == NFIT_SPA_VCD   ||
2414                 nfit_spa_type(spa) == NFIT_SPA_VOLATILE);
2415 }
2416
2417 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
2418                 struct nfit_spa *nfit_spa)
2419 {
2420         static struct nd_mapping_desc mappings[ND_MAX_MAPPINGS];
2421         struct acpi_nfit_system_address *spa = nfit_spa->spa;
2422         struct nd_blk_region_desc ndbr_desc;
2423         struct nd_region_desc *ndr_desc;
2424         struct nfit_memdev *nfit_memdev;
2425         struct nvdimm_bus *nvdimm_bus;
2426         struct resource res;
2427         int count = 0, rc;
2428
2429         if (nfit_spa->nd_region)
2430                 return 0;
2431
2432         if (spa->range_index == 0 && !nfit_spa_is_virtual(spa)) {
2433                 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
2434                                 __func__);
2435                 return 0;
2436         }
2437
2438         memset(&res, 0, sizeof(res));
2439         memset(&mappings, 0, sizeof(mappings));
2440         memset(&ndbr_desc, 0, sizeof(ndbr_desc));
2441         res.start = spa->address;
2442         res.end = res.start + spa->length - 1;
2443         ndr_desc = &ndbr_desc.ndr_desc;
2444         ndr_desc->res = &res;
2445         ndr_desc->provider_data = nfit_spa;
2446         ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
2447         if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
2448                 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
2449                                                 spa->proximity_domain);
2450         else
2451                 ndr_desc->numa_node = NUMA_NO_NODE;
2452
2453         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
2454                 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
2455                 struct nd_mapping_desc *mapping;
2456
2457                 /* range index 0 == unmapped in SPA or invalid-SPA */
2458                 if (memdev->range_index == 0 || spa->range_index == 0)
2459                         continue;
2460                 if (memdev->range_index != spa->range_index)
2461                         continue;
2462                 if (count >= ND_MAX_MAPPINGS) {
2463                         dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
2464                                         spa->range_index, ND_MAX_MAPPINGS);
2465                         return -ENXIO;
2466                 }
2467                 mapping = &mappings[count++];
2468                 rc = acpi_nfit_init_mapping(acpi_desc, mapping, ndr_desc,
2469                                 memdev, nfit_spa);
2470                 if (rc)
2471                         goto out;
2472         }
2473
2474         ndr_desc->mapping = mappings;
2475         ndr_desc->num_mappings = count;
2476         rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
2477         if (rc)
2478                 goto out;
2479
2480         nvdimm_bus = acpi_desc->nvdimm_bus;
2481         if (nfit_spa_type(spa) == NFIT_SPA_PM) {
2482                 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
2483                 if (rc) {
2484                         dev_warn(acpi_desc->dev,
2485                                 "failed to insert pmem resource to iomem: %d\n",
2486                                 rc);
2487                         goto out;
2488                 }
2489
2490                 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2491                                 ndr_desc);
2492                 if (!nfit_spa->nd_region)
2493                         rc = -ENOMEM;
2494         } else if (nfit_spa_is_volatile(spa)) {
2495                 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
2496                                 ndr_desc);
2497                 if (!nfit_spa->nd_region)
2498                         rc = -ENOMEM;
2499         } else if (nfit_spa_is_virtual(spa)) {
2500                 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
2501                                 ndr_desc);
2502                 if (!nfit_spa->nd_region)
2503                         rc = -ENOMEM;
2504         }
2505
2506  out:
2507         if (rc)
2508                 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
2509                                 nfit_spa->spa->range_index);
2510         return rc;
2511 }
2512
2513 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
2514                 u32 max_ars)
2515 {
2516         struct device *dev = acpi_desc->dev;
2517         struct nd_cmd_ars_status *ars_status;
2518
2519         if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
2520                 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
2521                 return 0;
2522         }
2523
2524         if (acpi_desc->ars_status)
2525                 devm_kfree(dev, acpi_desc->ars_status);
2526         acpi_desc->ars_status = NULL;
2527         ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
2528         if (!ars_status)
2529                 return -ENOMEM;
2530         acpi_desc->ars_status = ars_status;
2531         acpi_desc->ars_status_size = max_ars;
2532         return 0;
2533 }
2534
2535 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
2536                 struct nfit_spa *nfit_spa)
2537 {
2538         struct acpi_nfit_system_address *spa = nfit_spa->spa;
2539         int rc;
2540
2541         if (!nfit_spa->max_ars) {
2542                 struct nd_cmd_ars_cap ars_cap;
2543
2544                 memset(&ars_cap, 0, sizeof(ars_cap));
2545                 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
2546                 if (rc < 0)
2547                         return rc;
2548                 nfit_spa->max_ars = ars_cap.max_ars_out;
2549                 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
2550                 /* check that the supported scrub types match the spa type */
2551                 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
2552                                 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
2553                         return -ENOTTY;
2554                 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
2555                                 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
2556                         return -ENOTTY;
2557         }
2558
2559         if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
2560                 return -ENOMEM;
2561
2562         rc = ars_get_status(acpi_desc);
2563         if (rc < 0 && rc != -ENOSPC)
2564                 return rc;
2565
2566         if (ars_status_process_records(acpi_desc, acpi_desc->ars_status))
2567                 return -ENOMEM;
2568
2569         return 0;
2570 }
2571
2572 static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
2573                 struct nfit_spa *nfit_spa)
2574 {
2575         struct acpi_nfit_system_address *spa = nfit_spa->spa;
2576         unsigned int overflow_retry = scrub_overflow_abort;
2577         u64 init_ars_start = 0, init_ars_len = 0;
2578         struct device *dev = acpi_desc->dev;
2579         unsigned int tmo = scrub_timeout;
2580         int rc;
2581
2582         if (!nfit_spa->ars_required || !nfit_spa->nd_region)
2583                 return;
2584
2585         rc = ars_start(acpi_desc, nfit_spa);
2586         /*
2587          * If we timed out the initial scan we'll still be busy here,
2588          * and will wait another timeout before giving up permanently.
2589          */
2590         if (rc < 0 && rc != -EBUSY)
2591                 return;
2592
2593         do {
2594                 u64 ars_start, ars_len;
2595
2596                 if (acpi_desc->cancel)
2597                         break;
2598                 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2599                 if (rc == -ENOTTY)
2600                         break;
2601                 if (rc == -EBUSY && !tmo) {
2602                         dev_warn(dev, "range %d ars timeout, aborting\n",
2603                                         spa->range_index);
2604                         break;
2605                 }
2606
2607                 if (rc == -EBUSY) {
2608                         /*
2609                          * Note, entries may be appended to the list
2610                          * while the lock is dropped, but the workqueue
2611                          * being active prevents entries being deleted /
2612                          * freed.
2613                          */
2614                         mutex_unlock(&acpi_desc->init_mutex);
2615                         ssleep(1);
2616                         tmo--;
2617                         mutex_lock(&acpi_desc->init_mutex);
2618                         continue;
2619                 }
2620
2621                 /* we got some results, but there are more pending... */
2622                 if (rc == -ENOSPC && overflow_retry--) {
2623                         if (!init_ars_len) {
2624                                 init_ars_len = acpi_desc->ars_status->length;
2625                                 init_ars_start = acpi_desc->ars_status->address;
2626                         }
2627                         rc = ars_continue(acpi_desc);
2628                 }
2629
2630                 if (rc < 0) {
2631                         dev_warn(dev, "range %d ars continuation failed\n",
2632                                         spa->range_index);
2633                         break;
2634                 }
2635
2636                 if (init_ars_len) {
2637                         ars_start = init_ars_start;
2638                         ars_len = init_ars_len;
2639                 } else {
2640                         ars_start = acpi_desc->ars_status->address;
2641                         ars_len = acpi_desc->ars_status->length;
2642                 }
2643                 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2644                                 spa->range_index, ars_start, ars_len);
2645                 /* notify the region about new poison entries */
2646                 nvdimm_region_notify(nfit_spa->nd_region,
2647                                 NVDIMM_REVALIDATE_POISON);
2648                 break;
2649         } while (1);
2650 }
2651
2652 static void acpi_nfit_scrub(struct work_struct *work)
2653 {
2654         struct device *dev;
2655         u64 init_scrub_length = 0;
2656         struct nfit_spa *nfit_spa;
2657         u64 init_scrub_address = 0;
2658         bool init_ars_done = false;
2659         struct acpi_nfit_desc *acpi_desc;
2660         unsigned int tmo = scrub_timeout;
2661         unsigned int overflow_retry = scrub_overflow_abort;
2662
2663         acpi_desc = container_of(work, typeof(*acpi_desc), work);
2664         dev = acpi_desc->dev;
2665
2666         /*
2667          * We scrub in 2 phases.  The first phase waits for any platform
2668          * firmware initiated scrubs to complete and then we go search for the
2669          * affected spa regions to mark them scanned.  In the second phase we
2670          * initiate a directed scrub for every range that was not scrubbed in
2671          * phase 1. If we're called for a 'rescan', we harmlessly pass through
2672          * the first phase, but really only care about running phase 2, where
2673          * regions can be notified of new poison.
2674          */
2675
2676         /* process platform firmware initiated scrubs */
2677  retry:
2678         mutex_lock(&acpi_desc->init_mutex);
2679         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2680                 struct nd_cmd_ars_status *ars_status;
2681                 struct acpi_nfit_system_address *spa;
2682                 u64 ars_start, ars_len;
2683                 int rc;
2684
2685                 if (acpi_desc->cancel)
2686                         break;
2687
2688                 if (nfit_spa->nd_region)
2689                         continue;
2690
2691                 if (init_ars_done) {
2692                         /*
2693                          * No need to re-query, we're now just
2694                          * reconciling all the ranges covered by the
2695                          * initial scrub
2696                          */
2697                         rc = 0;
2698                 } else
2699                         rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2700
2701                 if (rc == -ENOTTY) {
2702                         /* no ars capability, just register spa and move on */
2703                         acpi_nfit_register_region(acpi_desc, nfit_spa);
2704                         continue;
2705                 }
2706
2707                 if (rc == -EBUSY && !tmo) {
2708                         /* fallthrough to directed scrub in phase 2 */
2709                         dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2710                         break;
2711                 } else if (rc == -EBUSY) {
2712                         mutex_unlock(&acpi_desc->init_mutex);
2713                         ssleep(1);
2714                         tmo--;
2715                         goto retry;
2716                 }
2717
2718                 /* we got some results, but there are more pending... */
2719                 if (rc == -ENOSPC && overflow_retry--) {
2720                         ars_status = acpi_desc->ars_status;
2721                         /*
2722                          * Record the original scrub range, so that we
2723                          * can recall all the ranges impacted by the
2724                          * initial scrub.
2725                          */
2726                         if (!init_scrub_length) {
2727                                 init_scrub_length = ars_status->length;
2728                                 init_scrub_address = ars_status->address;
2729                         }
2730                         rc = ars_continue(acpi_desc);
2731                         if (rc == 0) {
2732                                 mutex_unlock(&acpi_desc->init_mutex);
2733                                 goto retry;
2734                         }
2735                 }
2736
2737                 if (rc < 0) {
2738                         /*
2739                          * Initial scrub failed, we'll give it one more
2740                          * try below...
2741                          */
2742                         break;
2743                 }
2744
2745                 /* We got some final results, record completed ranges */
2746                 ars_status = acpi_desc->ars_status;
2747                 if (init_scrub_length) {
2748                         ars_start = init_scrub_address;
2749                         ars_len = ars_start + init_scrub_length;
2750                 } else {
2751                         ars_start = ars_status->address;
2752                         ars_len = ars_status->length;
2753                 }
2754                 spa = nfit_spa->spa;
2755
2756                 if (!init_ars_done) {
2757                         init_ars_done = true;
2758                         dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2759                                         ars_start, ars_len);
2760                 }
2761                 if (ars_start <= spa->address && ars_start + ars_len
2762                                 >= spa->address + spa->length)
2763                         acpi_nfit_register_region(acpi_desc, nfit_spa);
2764         }
2765
2766         /*
2767          * For all the ranges not covered by an initial scrub we still
2768          * want to see if there are errors, but it's ok to discover them
2769          * asynchronously.
2770          */
2771         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2772                 /*
2773                  * Flag all the ranges that still need scrubbing, but
2774                  * register them now to make data available.
2775                  */
2776                 if (!nfit_spa->nd_region) {
2777                         nfit_spa->ars_required = 1;
2778                         acpi_nfit_register_region(acpi_desc, nfit_spa);
2779                 }
2780         }
2781         acpi_desc->init_complete = 1;
2782
2783         list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2784                 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
2785         acpi_desc->scrub_count++;
2786         acpi_desc->ars_start_flags = 0;
2787         if (acpi_desc->scrub_count_state)
2788                 sysfs_notify_dirent(acpi_desc->scrub_count_state);
2789         mutex_unlock(&acpi_desc->init_mutex);
2790 }
2791
2792 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2793 {
2794         struct nfit_spa *nfit_spa;
2795
2796         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2797                 int rc, type = nfit_spa_type(nfit_spa->spa);
2798
2799                 /* PMEM and VMEM will be registered by the ARS workqueue */
2800                 if (type == NFIT_SPA_PM || type == NFIT_SPA_VOLATILE)
2801                         continue;
2802                 /* BLK apertures belong to BLK region registration below */
2803                 if (type == NFIT_SPA_BDW)
2804                         continue;
2805                 /* BLK regions don't need to wait for ARS results */
2806                 rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2807                 if (rc)
2808                         return rc;
2809         }
2810
2811         acpi_desc->ars_start_flags = 0;
2812         if (!acpi_desc->cancel)
2813                 queue_work(nfit_wq, &acpi_desc->work);
2814         return 0;
2815 }
2816
2817 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2818                 struct nfit_table_prev *prev)
2819 {
2820         struct device *dev = acpi_desc->dev;
2821
2822         if (!list_empty(&prev->spas) ||
2823                         !list_empty(&prev->memdevs) ||
2824                         !list_empty(&prev->dcrs) ||
2825                         !list_empty(&prev->bdws) ||
2826                         !list_empty(&prev->idts) ||
2827                         !list_empty(&prev->flushes)) {
2828                 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2829                 return -ENXIO;
2830         }
2831         return 0;
2832 }
2833
2834 static int acpi_nfit_desc_init_scrub_attr(struct acpi_nfit_desc *acpi_desc)
2835 {
2836         struct device *dev = acpi_desc->dev;
2837         struct kernfs_node *nfit;
2838         struct device *bus_dev;
2839
2840         if (!ars_supported(acpi_desc->nvdimm_bus))
2841                 return 0;
2842
2843         bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
2844         nfit = sysfs_get_dirent(bus_dev->kobj.sd, "nfit");
2845         if (!nfit) {
2846                 dev_err(dev, "sysfs_get_dirent 'nfit' failed\n");
2847                 return -ENODEV;
2848         }
2849         acpi_desc->scrub_count_state = sysfs_get_dirent(nfit, "scrub");
2850         sysfs_put(nfit);
2851         if (!acpi_desc->scrub_count_state) {
2852                 dev_err(dev, "sysfs_get_dirent 'scrub' failed\n");
2853                 return -ENODEV;
2854         }
2855
2856         return 0;
2857 }
2858
2859 static void acpi_nfit_unregister(void *data)
2860 {
2861         struct acpi_nfit_desc *acpi_desc = data;
2862
2863         nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2864 }
2865
2866 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, void *data, acpi_size sz)
2867 {
2868         struct device *dev = acpi_desc->dev;
2869         struct nfit_table_prev prev;
2870         const void *end;
2871         int rc;
2872
2873         if (!acpi_desc->nvdimm_bus) {
2874                 acpi_nfit_init_dsms(acpi_desc);
2875
2876                 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev,
2877                                 &acpi_desc->nd_desc);
2878                 if (!acpi_desc->nvdimm_bus)
2879                         return -ENOMEM;
2880
2881                 rc = devm_add_action_or_reset(dev, acpi_nfit_unregister,
2882                                 acpi_desc);
2883                 if (rc)
2884                         return rc;
2885
2886                 rc = acpi_nfit_desc_init_scrub_attr(acpi_desc);
2887                 if (rc)
2888                         return rc;
2889
2890                 /* register this acpi_desc for mce notifications */
2891                 mutex_lock(&acpi_desc_lock);
2892                 list_add_tail(&acpi_desc->list, &acpi_descs);
2893                 mutex_unlock(&acpi_desc_lock);
2894         }
2895
2896         mutex_lock(&acpi_desc->init_mutex);
2897
2898         INIT_LIST_HEAD(&prev.spas);
2899         INIT_LIST_HEAD(&prev.memdevs);
2900         INIT_LIST_HEAD(&prev.dcrs);
2901         INIT_LIST_HEAD(&prev.bdws);
2902         INIT_LIST_HEAD(&prev.idts);
2903         INIT_LIST_HEAD(&prev.flushes);
2904
2905         list_cut_position(&prev.spas, &acpi_desc->spas,
2906                                 acpi_desc->spas.prev);
2907         list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2908                                 acpi_desc->memdevs.prev);
2909         list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2910                                 acpi_desc->dcrs.prev);
2911         list_cut_position(&prev.bdws, &acpi_desc->bdws,
2912                                 acpi_desc->bdws.prev);
2913         list_cut_position(&prev.idts, &acpi_desc->idts,
2914                                 acpi_desc->idts.prev);
2915         list_cut_position(&prev.flushes, &acpi_desc->flushes,
2916                                 acpi_desc->flushes.prev);
2917
2918         end = data + sz;
2919         while (!IS_ERR_OR_NULL(data))
2920                 data = add_table(acpi_desc, &prev, data, end);
2921
2922         if (IS_ERR(data)) {
2923                 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2924                                 PTR_ERR(data));
2925                 rc = PTR_ERR(data);
2926                 goto out_unlock;
2927         }
2928
2929         rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2930         if (rc)
2931                 goto out_unlock;
2932
2933         rc = nfit_mem_init(acpi_desc);
2934         if (rc)
2935                 goto out_unlock;
2936
2937         rc = acpi_nfit_register_dimms(acpi_desc);
2938         if (rc)
2939                 goto out_unlock;
2940
2941         rc = acpi_nfit_register_regions(acpi_desc);
2942
2943  out_unlock:
2944         mutex_unlock(&acpi_desc->init_mutex);
2945         return rc;
2946 }
2947 EXPORT_SYMBOL_GPL(acpi_nfit_init);
2948
2949 struct acpi_nfit_flush_work {
2950         struct work_struct work;
2951         struct completion cmp;
2952 };
2953
2954 static void flush_probe(struct work_struct *work)
2955 {
2956         struct acpi_nfit_flush_work *flush;
2957
2958         flush = container_of(work, typeof(*flush), work);
2959         complete(&flush->cmp);
2960 }
2961
2962 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2963 {
2964         struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2965         struct device *dev = acpi_desc->dev;
2966         struct acpi_nfit_flush_work flush;
2967         int rc;
2968
2969         /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2970         device_lock(dev);
2971         device_unlock(dev);
2972
2973         /* bounce the init_mutex to make init_complete valid */
2974         mutex_lock(&acpi_desc->init_mutex);
2975         if (acpi_desc->cancel || acpi_desc->init_complete) {
2976                 mutex_unlock(&acpi_desc->init_mutex);
2977                 return 0;
2978         }
2979
2980         /*
2981          * Scrub work could take 10s of seconds, userspace may give up so we
2982          * need to be interruptible while waiting.
2983          */
2984         INIT_WORK_ONSTACK(&flush.work, flush_probe);
2985         init_completion(&flush.cmp);
2986         queue_work(nfit_wq, &flush.work);
2987         mutex_unlock(&acpi_desc->init_mutex);
2988
2989         rc = wait_for_completion_interruptible(&flush.cmp);
2990         cancel_work_sync(&flush.work);
2991         return rc;
2992 }
2993
2994 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2995                 struct nvdimm *nvdimm, unsigned int cmd)
2996 {
2997         struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2998
2999         if (nvdimm)
3000                 return 0;
3001         if (cmd != ND_CMD_ARS_START)
3002                 return 0;
3003
3004         /*
3005          * The kernel and userspace may race to initiate a scrub, but
3006          * the scrub thread is prepared to lose that initial race.  It
3007          * just needs guarantees that any ars it initiates are not
3008          * interrupted by any intervening start reqeusts from userspace.
3009          */
3010         if (work_busy(&acpi_desc->work))
3011                 return -EBUSY;
3012
3013         return 0;
3014 }
3015
3016 int acpi_nfit_ars_rescan(struct acpi_nfit_desc *acpi_desc, u8 flags)
3017 {
3018         struct device *dev = acpi_desc->dev;
3019         struct nfit_spa *nfit_spa;
3020
3021         if (work_busy(&acpi_desc->work))
3022                 return -EBUSY;
3023
3024         mutex_lock(&acpi_desc->init_mutex);
3025         if (acpi_desc->cancel) {
3026                 mutex_unlock(&acpi_desc->init_mutex);
3027                 return 0;
3028         }
3029
3030         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
3031                 struct acpi_nfit_system_address *spa = nfit_spa->spa;
3032
3033                 if (nfit_spa_type(spa) != NFIT_SPA_PM)
3034                         continue;
3035
3036                 nfit_spa->ars_required = 1;
3037         }
3038         acpi_desc->ars_start_flags = flags;
3039         queue_work(nfit_wq, &acpi_desc->work);
3040         dev_dbg(dev, "%s: ars_scan triggered\n", __func__);
3041         mutex_unlock(&acpi_desc->init_mutex);
3042
3043         return 0;
3044 }
3045
3046 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
3047 {
3048         struct nvdimm_bus_descriptor *nd_desc;
3049
3050         dev_set_drvdata(dev, acpi_desc);
3051         acpi_desc->dev = dev;
3052         acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
3053         nd_desc = &acpi_desc->nd_desc;
3054         nd_desc->provider_name = "ACPI.NFIT";
3055         nd_desc->module = THIS_MODULE;
3056         nd_desc->ndctl = acpi_nfit_ctl;
3057         nd_desc->flush_probe = acpi_nfit_flush_probe;
3058         nd_desc->clear_to_send = acpi_nfit_clear_to_send;
3059         nd_desc->attr_groups = acpi_nfit_attribute_groups;
3060
3061         INIT_LIST_HEAD(&acpi_desc->spas);
3062         INIT_LIST_HEAD(&acpi_desc->dcrs);
3063         INIT_LIST_HEAD(&acpi_desc->bdws);
3064         INIT_LIST_HEAD(&acpi_desc->idts);
3065         INIT_LIST_HEAD(&acpi_desc->flushes);
3066         INIT_LIST_HEAD(&acpi_desc->memdevs);
3067         INIT_LIST_HEAD(&acpi_desc->dimms);
3068         INIT_LIST_HEAD(&acpi_desc->list);
3069         mutex_init(&acpi_desc->init_mutex);
3070         INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
3071 }
3072 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
3073
3074 static void acpi_nfit_put_table(void *table)
3075 {
3076         acpi_put_table(table);
3077 }
3078
3079 void acpi_nfit_shutdown(void *data)
3080 {
3081         struct acpi_nfit_desc *acpi_desc = data;
3082         struct device *bus_dev = to_nvdimm_bus_dev(acpi_desc->nvdimm_bus);
3083
3084         /*
3085          * Destruct under acpi_desc_lock so that nfit_handle_mce does not
3086          * race teardown
3087          */
3088         mutex_lock(&acpi_desc_lock);
3089         list_del(&acpi_desc->list);
3090         mutex_unlock(&acpi_desc_lock);
3091
3092         mutex_lock(&acpi_desc->init_mutex);
3093         acpi_desc->cancel = 1;
3094         mutex_unlock(&acpi_desc->init_mutex);
3095
3096         /*
3097          * Bounce the nvdimm bus lock to make sure any in-flight
3098          * acpi_nfit_ars_rescan() submissions have had a chance to
3099          * either submit or see ->cancel set.
3100          */
3101         device_lock(bus_dev);
3102         device_unlock(bus_dev);
3103
3104         flush_workqueue(nfit_wq);
3105 }
3106 EXPORT_SYMBOL_GPL(acpi_nfit_shutdown);
3107
3108 static int acpi_nfit_add(struct acpi_device *adev)
3109 {
3110         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3111         struct acpi_nfit_desc *acpi_desc;
3112         struct device *dev = &adev->dev;
3113         struct acpi_table_header *tbl;
3114         acpi_status status = AE_OK;
3115         acpi_size sz;
3116         int rc = 0;
3117
3118         status = acpi_get_table(ACPI_SIG_NFIT, 0, &tbl);
3119         if (ACPI_FAILURE(status)) {
3120                 /* This is ok, we could have an nvdimm hotplugged later */
3121                 dev_dbg(dev, "failed to find NFIT at startup\n");
3122                 return 0;
3123         }
3124
3125         rc = devm_add_action_or_reset(dev, acpi_nfit_put_table, tbl);
3126         if (rc)
3127                 return rc;
3128         sz = tbl->length;
3129
3130         acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3131         if (!acpi_desc)
3132                 return -ENOMEM;
3133         acpi_nfit_desc_init(acpi_desc, &adev->dev);
3134
3135         /* Save the acpi header for exporting the revision via sysfs */
3136         acpi_desc->acpi_header = *tbl;
3137
3138         /* Evaluate _FIT and override with that if present */
3139         status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
3140         if (ACPI_SUCCESS(status) && buf.length > 0) {
3141                 union acpi_object *obj = buf.pointer;
3142
3143                 if (obj->type == ACPI_TYPE_BUFFER)
3144                         rc = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3145                                         obj->buffer.length);
3146                 else
3147                         dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
3148                                  __func__, (int) obj->type);
3149                 kfree(buf.pointer);
3150         } else
3151                 /* skip over the lead-in header table */
3152                 rc = acpi_nfit_init(acpi_desc, (void *) tbl
3153                                 + sizeof(struct acpi_table_nfit),
3154                                 sz - sizeof(struct acpi_table_nfit));
3155
3156         if (rc)
3157                 return rc;
3158         return devm_add_action_or_reset(dev, acpi_nfit_shutdown, acpi_desc);
3159 }
3160
3161 static int acpi_nfit_remove(struct acpi_device *adev)
3162 {
3163         /* see acpi_nfit_unregister */
3164         return 0;
3165 }
3166
3167 static void acpi_nfit_update_notify(struct device *dev, acpi_handle handle)
3168 {
3169         struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3170         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
3171         union acpi_object *obj;
3172         acpi_status status;
3173         int ret;
3174
3175         if (!dev->driver) {
3176                 /* dev->driver may be null if we're being removed */
3177                 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
3178                 return;
3179         }
3180
3181         if (!acpi_desc) {
3182                 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
3183                 if (!acpi_desc)
3184                         return;
3185                 acpi_nfit_desc_init(acpi_desc, dev);
3186         } else {
3187                 /*
3188                  * Finish previous registration before considering new
3189                  * regions.
3190                  */
3191                 flush_workqueue(nfit_wq);
3192         }
3193
3194         /* Evaluate _FIT */
3195         status = acpi_evaluate_object(handle, "_FIT", NULL, &buf);
3196         if (ACPI_FAILURE(status)) {
3197                 dev_err(dev, "failed to evaluate _FIT\n");
3198                 return;
3199         }
3200
3201         obj = buf.pointer;
3202         if (obj->type == ACPI_TYPE_BUFFER) {
3203                 ret = acpi_nfit_init(acpi_desc, obj->buffer.pointer,
3204                                 obj->buffer.length);
3205                 if (ret)
3206                         dev_err(dev, "failed to merge updated NFIT\n");
3207         } else
3208                 dev_err(dev, "Invalid _FIT\n");
3209         kfree(buf.pointer);
3210 }
3211
3212 static void acpi_nfit_uc_error_notify(struct device *dev, acpi_handle handle)
3213 {
3214         struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(dev);
3215         u8 flags = (acpi_desc->scrub_mode == HW_ERROR_SCRUB_ON) ?
3216                         0 : ND_ARS_RETURN_PREV_DATA;
3217
3218         acpi_nfit_ars_rescan(acpi_desc, flags);
3219 }
3220
3221 void __acpi_nfit_notify(struct device *dev, acpi_handle handle, u32 event)
3222 {
3223         dev_dbg(dev, "%s: event: 0x%x\n", __func__, event);
3224
3225         switch (event) {
3226         case NFIT_NOTIFY_UPDATE:
3227                 return acpi_nfit_update_notify(dev, handle);
3228         case NFIT_NOTIFY_UC_MEMORY_ERROR:
3229                 return acpi_nfit_uc_error_notify(dev, handle);
3230         default:
3231                 return;
3232         }
3233 }
3234 EXPORT_SYMBOL_GPL(__acpi_nfit_notify);
3235
3236 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
3237 {
3238         device_lock(&adev->dev);
3239         __acpi_nfit_notify(&adev->dev, adev->handle, event);
3240         device_unlock(&adev->dev);
3241 }
3242
3243 static const struct acpi_device_id acpi_nfit_ids[] = {
3244         { "ACPI0012", 0 },
3245         { "", 0 },
3246 };
3247 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
3248
3249 static struct acpi_driver acpi_nfit_driver = {
3250         .name = KBUILD_MODNAME,
3251         .ids = acpi_nfit_ids,
3252         .ops = {
3253                 .add = acpi_nfit_add,
3254                 .remove = acpi_nfit_remove,
3255                 .notify = acpi_nfit_notify,
3256         },
3257 };
3258
3259 static __init int nfit_init(void)
3260 {
3261         int ret;
3262
3263         BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
3264         BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
3265         BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
3266         BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
3267         BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
3268         BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
3269         BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
3270
3271         guid_parse(UUID_VOLATILE_MEMORY, &nfit_uuid[NFIT_SPA_VOLATILE]);
3272         guid_parse(UUID_PERSISTENT_MEMORY, &nfit_uuid[NFIT_SPA_PM]);
3273         guid_parse(UUID_CONTROL_REGION, &nfit_uuid[NFIT_SPA_DCR]);
3274         guid_parse(UUID_DATA_REGION, &nfit_uuid[NFIT_SPA_BDW]);
3275         guid_parse(UUID_VOLATILE_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_VDISK]);
3276         guid_parse(UUID_VOLATILE_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_VCD]);
3277         guid_parse(UUID_PERSISTENT_VIRTUAL_DISK, &nfit_uuid[NFIT_SPA_PDISK]);
3278         guid_parse(UUID_PERSISTENT_VIRTUAL_CD, &nfit_uuid[NFIT_SPA_PCD]);
3279         guid_parse(UUID_NFIT_BUS, &nfit_uuid[NFIT_DEV_BUS]);
3280         guid_parse(UUID_NFIT_DIMM, &nfit_uuid[NFIT_DEV_DIMM]);
3281         guid_parse(UUID_NFIT_DIMM_N_HPE1, &nfit_uuid[NFIT_DEV_DIMM_N_HPE1]);
3282         guid_parse(UUID_NFIT_DIMM_N_HPE2, &nfit_uuid[NFIT_DEV_DIMM_N_HPE2]);
3283         guid_parse(UUID_NFIT_DIMM_N_MSFT, &nfit_uuid[NFIT_DEV_DIMM_N_MSFT]);
3284
3285         nfit_wq = create_singlethread_workqueue("nfit");
3286         if (!nfit_wq)
3287                 return -ENOMEM;
3288
3289         nfit_mce_register();
3290         ret = acpi_bus_register_driver(&acpi_nfit_driver);
3291         if (ret) {
3292                 nfit_mce_unregister();
3293                 destroy_workqueue(nfit_wq);
3294         }
3295
3296         return ret;
3297
3298 }
3299
3300 static __exit void nfit_exit(void)
3301 {
3302         nfit_mce_unregister();
3303         acpi_bus_unregister_driver(&acpi_nfit_driver);
3304         destroy_workqueue(nfit_wq);
3305         WARN_ON(!list_empty(&acpi_descs));
3306 }
3307
3308 module_init(nfit_init);
3309 module_exit(nfit_exit);
3310 MODULE_LICENSE("GPL v2");
3311 MODULE_AUTHOR("Intel Corporation");