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