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