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