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