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