nfit: export subsystem ids as attributes
[cascardo/linux.git] / drivers / acpi / nfit.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/delay.h>
19 #include <linux/list.h>
20 #include <linux/acpi.h>
21 #include <linux/sort.h>
22 #include <linux/pmem.h>
23 #include <linux/io.h>
24 #include <linux/nd.h>
25 #include <asm/cacheflush.h>
26 #include "nfit.h"
27
28 /*
29  * For readq() and writeq() on 32-bit builds, the hi-lo, lo-hi order is
30  * irrelevant.
31  */
32 #include <linux/io-64-nonatomic-hi-lo.h>
33
34 static bool force_enable_dimms;
35 module_param(force_enable_dimms, bool, S_IRUGO|S_IWUSR);
36 MODULE_PARM_DESC(force_enable_dimms, "Ignore _STA (ACPI DIMM device) status");
37
38 static unsigned int scrub_timeout = NFIT_ARS_TIMEOUT;
39 module_param(scrub_timeout, uint, S_IRUGO|S_IWUSR);
40 MODULE_PARM_DESC(scrub_timeout, "Initial scrub timeout in seconds");
41
42 /* after three payloads of overflow, it's dead jim */
43 static unsigned int scrub_overflow_abort = 3;
44 module_param(scrub_overflow_abort, uint, S_IRUGO|S_IWUSR);
45 MODULE_PARM_DESC(scrub_overflow_abort,
46                 "Number of times we overflow ARS results before abort");
47
48 static struct workqueue_struct *nfit_wq;
49
50 struct nfit_table_prev {
51         struct list_head spas;
52         struct list_head memdevs;
53         struct list_head dcrs;
54         struct list_head bdws;
55         struct list_head idts;
56         struct list_head flushes;
57 };
58
59 static u8 nfit_uuid[NFIT_UUID_MAX][16];
60
61 const u8 *to_nfit_uuid(enum nfit_uuids id)
62 {
63         return nfit_uuid[id];
64 }
65 EXPORT_SYMBOL(to_nfit_uuid);
66
67 static struct acpi_nfit_desc *to_acpi_nfit_desc(
68                 struct nvdimm_bus_descriptor *nd_desc)
69 {
70         return container_of(nd_desc, struct acpi_nfit_desc, nd_desc);
71 }
72
73 static struct acpi_device *to_acpi_dev(struct acpi_nfit_desc *acpi_desc)
74 {
75         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
76
77         /*
78          * If provider == 'ACPI.NFIT' we can assume 'dev' is a struct
79          * acpi_device.
80          */
81         if (!nd_desc->provider_name
82                         || strcmp(nd_desc->provider_name, "ACPI.NFIT") != 0)
83                 return NULL;
84
85         return to_acpi_device(acpi_desc->dev);
86 }
87
88 static int xlat_status(void *buf, unsigned int cmd)
89 {
90         struct nd_cmd_clear_error *clear_err;
91         struct nd_cmd_ars_status *ars_status;
92         struct nd_cmd_ars_start *ars_start;
93         struct nd_cmd_ars_cap *ars_cap;
94         u16 flags;
95
96         switch (cmd) {
97         case ND_CMD_ARS_CAP:
98                 ars_cap = buf;
99                 if ((ars_cap->status & 0xffff) == NFIT_ARS_CAP_NONE)
100                         return -ENOTTY;
101
102                 /* Command failed */
103                 if (ars_cap->status & 0xffff)
104                         return -EIO;
105
106                 /* No supported scan types for this range */
107                 flags = ND_ARS_PERSISTENT | ND_ARS_VOLATILE;
108                 if ((ars_cap->status >> 16 & flags) == 0)
109                         return -ENOTTY;
110                 break;
111         case ND_CMD_ARS_START:
112                 ars_start = buf;
113                 /* ARS is in progress */
114                 if ((ars_start->status & 0xffff) == NFIT_ARS_START_BUSY)
115                         return -EBUSY;
116
117                 /* Command failed */
118                 if (ars_start->status & 0xffff)
119                         return -EIO;
120                 break;
121         case ND_CMD_ARS_STATUS:
122                 ars_status = buf;
123                 /* Command failed */
124                 if (ars_status->status & 0xffff)
125                         return -EIO;
126                 /* Check extended status (Upper two bytes) */
127                 if (ars_status->status == NFIT_ARS_STATUS_DONE)
128                         return 0;
129
130                 /* ARS is in progress */
131                 if (ars_status->status == NFIT_ARS_STATUS_BUSY)
132                         return -EBUSY;
133
134                 /* No ARS performed for the current boot */
135                 if (ars_status->status == NFIT_ARS_STATUS_NONE)
136                         return -EAGAIN;
137
138                 /*
139                  * ARS interrupted, either we overflowed or some other
140                  * agent wants the scan to stop.  If we didn't overflow
141                  * then just continue with the returned results.
142                  */
143                 if (ars_status->status == NFIT_ARS_STATUS_INTR) {
144                         if (ars_status->flags & NFIT_ARS_F_OVERFLOW)
145                                 return -ENOSPC;
146                         return 0;
147                 }
148
149                 /* Unknown status */
150                 if (ars_status->status >> 16)
151                         return -EIO;
152                 break;
153         case ND_CMD_CLEAR_ERROR:
154                 clear_err = buf;
155                 if (clear_err->status & 0xffff)
156                         return -EIO;
157                 if (!clear_err->cleared)
158                         return -EIO;
159                 if (clear_err->length > clear_err->cleared)
160                         return clear_err->cleared;
161                 break;
162         default:
163                 break;
164         }
165
166         return 0;
167 }
168
169 static int acpi_nfit_ctl(struct nvdimm_bus_descriptor *nd_desc,
170                 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
171                 unsigned int buf_len, int *cmd_rc)
172 {
173         struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
174         const struct nd_cmd_desc *desc = NULL;
175         union acpi_object in_obj, in_buf, *out_obj;
176         struct device *dev = acpi_desc->dev;
177         const char *cmd_name, *dimm_name;
178         unsigned long dsm_mask;
179         acpi_handle handle;
180         const u8 *uuid;
181         u32 offset;
182         int rc, i;
183
184         if (nvdimm) {
185                 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
186                 struct acpi_device *adev = nfit_mem->adev;
187
188                 if (!adev)
189                         return -ENOTTY;
190                 dimm_name = nvdimm_name(nvdimm);
191                 cmd_name = nvdimm_cmd_name(cmd);
192                 dsm_mask = nfit_mem->dsm_mask;
193                 desc = nd_cmd_dimm_desc(cmd);
194                 uuid = to_nfit_uuid(NFIT_DEV_DIMM);
195                 handle = adev->handle;
196         } else {
197                 struct acpi_device *adev = to_acpi_dev(acpi_desc);
198
199                 cmd_name = nvdimm_bus_cmd_name(cmd);
200                 dsm_mask = nd_desc->dsm_mask;
201                 desc = nd_cmd_bus_desc(cmd);
202                 uuid = to_nfit_uuid(NFIT_DEV_BUS);
203                 handle = adev->handle;
204                 dimm_name = "bus";
205         }
206
207         if (!desc || (cmd && (desc->out_num + desc->in_num == 0)))
208                 return -ENOTTY;
209
210         if (!test_bit(cmd, &dsm_mask))
211                 return -ENOTTY;
212
213         in_obj.type = ACPI_TYPE_PACKAGE;
214         in_obj.package.count = 1;
215         in_obj.package.elements = &in_buf;
216         in_buf.type = ACPI_TYPE_BUFFER;
217         in_buf.buffer.pointer = buf;
218         in_buf.buffer.length = 0;
219
220         /* libnvdimm has already validated the input envelope */
221         for (i = 0; i < desc->in_num; i++)
222                 in_buf.buffer.length += nd_cmd_in_size(nvdimm, cmd, desc,
223                                 i, buf);
224
225         if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
226                 dev_dbg(dev, "%s:%s cmd: %s input length: %d\n", __func__,
227                                 dimm_name, cmd_name, in_buf.buffer.length);
228                 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
229                                 4, in_buf.buffer.pointer, min_t(u32, 128,
230                                         in_buf.buffer.length), true);
231         }
232
233         out_obj = acpi_evaluate_dsm(handle, uuid, 1, cmd, &in_obj);
234         if (!out_obj) {
235                 dev_dbg(dev, "%s:%s _DSM failed cmd: %s\n", __func__, dimm_name,
236                                 cmd_name);
237                 return -EINVAL;
238         }
239
240         if (out_obj->package.type != ACPI_TYPE_BUFFER) {
241                 dev_dbg(dev, "%s:%s unexpected output object type cmd: %s type: %d\n",
242                                 __func__, dimm_name, cmd_name, out_obj->type);
243                 rc = -EINVAL;
244                 goto out;
245         }
246
247         if (IS_ENABLED(CONFIG_ACPI_NFIT_DEBUG)) {
248                 dev_dbg(dev, "%s:%s cmd: %s output length: %d\n", __func__,
249                                 dimm_name, cmd_name, out_obj->buffer.length);
250                 print_hex_dump_debug(cmd_name, DUMP_PREFIX_OFFSET, 4,
251                                 4, out_obj->buffer.pointer, min_t(u32, 128,
252                                         out_obj->buffer.length), true);
253         }
254
255         for (i = 0, offset = 0; i < desc->out_num; i++) {
256                 u32 out_size = nd_cmd_out_size(nvdimm, cmd, desc, i, buf,
257                                 (u32 *) out_obj->buffer.pointer);
258
259                 if (offset + out_size > out_obj->buffer.length) {
260                         dev_dbg(dev, "%s:%s output object underflow cmd: %s field: %d\n",
261                                         __func__, dimm_name, cmd_name, i);
262                         break;
263                 }
264
265                 if (in_buf.buffer.length + offset + out_size > buf_len) {
266                         dev_dbg(dev, "%s:%s output overrun cmd: %s field: %d\n",
267                                         __func__, dimm_name, cmd_name, i);
268                         rc = -ENXIO;
269                         goto out;
270                 }
271                 memcpy(buf + in_buf.buffer.length + offset,
272                                 out_obj->buffer.pointer + offset, out_size);
273                 offset += out_size;
274         }
275         if (offset + in_buf.buffer.length < buf_len) {
276                 if (i >= 1) {
277                         /*
278                          * status valid, return the number of bytes left
279                          * unfilled in the output buffer
280                          */
281                         rc = buf_len - offset - in_buf.buffer.length;
282                         if (cmd_rc)
283                                 *cmd_rc = xlat_status(buf, cmd);
284                 } else {
285                         dev_err(dev, "%s:%s underrun cmd: %s buf_len: %d out_len: %d\n",
286                                         __func__, dimm_name, cmd_name, buf_len,
287                                         offset);
288                         rc = -ENXIO;
289                 }
290         } else
291                 rc = 0;
292
293  out:
294         ACPI_FREE(out_obj);
295
296         return rc;
297 }
298
299 static const char *spa_type_name(u16 type)
300 {
301         static const char *to_name[] = {
302                 [NFIT_SPA_VOLATILE] = "volatile",
303                 [NFIT_SPA_PM] = "pmem",
304                 [NFIT_SPA_DCR] = "dimm-control-region",
305                 [NFIT_SPA_BDW] = "block-data-window",
306                 [NFIT_SPA_VDISK] = "volatile-disk",
307                 [NFIT_SPA_VCD] = "volatile-cd",
308                 [NFIT_SPA_PDISK] = "persistent-disk",
309                 [NFIT_SPA_PCD] = "persistent-cd",
310
311         };
312
313         if (type > NFIT_SPA_PCD)
314                 return "unknown";
315
316         return to_name[type];
317 }
318
319 static int nfit_spa_type(struct acpi_nfit_system_address *spa)
320 {
321         int i;
322
323         for (i = 0; i < NFIT_UUID_MAX; i++)
324                 if (memcmp(to_nfit_uuid(i), spa->range_guid, 16) == 0)
325                         return i;
326         return -1;
327 }
328
329 static bool add_spa(struct acpi_nfit_desc *acpi_desc,
330                 struct nfit_table_prev *prev,
331                 struct acpi_nfit_system_address *spa)
332 {
333         size_t length = min_t(size_t, sizeof(*spa), spa->header.length);
334         struct device *dev = acpi_desc->dev;
335         struct nfit_spa *nfit_spa;
336
337         list_for_each_entry(nfit_spa, &prev->spas, list) {
338                 if (memcmp(nfit_spa->spa, spa, length) == 0) {
339                         list_move_tail(&nfit_spa->list, &acpi_desc->spas);
340                         return true;
341                 }
342         }
343
344         nfit_spa = devm_kzalloc(dev, sizeof(*nfit_spa), GFP_KERNEL);
345         if (!nfit_spa)
346                 return false;
347         INIT_LIST_HEAD(&nfit_spa->list);
348         nfit_spa->spa = spa;
349         list_add_tail(&nfit_spa->list, &acpi_desc->spas);
350         dev_dbg(dev, "%s: spa index: %d type: %s\n", __func__,
351                         spa->range_index,
352                         spa_type_name(nfit_spa_type(spa)));
353         return true;
354 }
355
356 static bool add_memdev(struct acpi_nfit_desc *acpi_desc,
357                 struct nfit_table_prev *prev,
358                 struct acpi_nfit_memory_map *memdev)
359 {
360         size_t length = min_t(size_t, sizeof(*memdev), memdev->header.length);
361         struct device *dev = acpi_desc->dev;
362         struct nfit_memdev *nfit_memdev;
363
364         list_for_each_entry(nfit_memdev, &prev->memdevs, list)
365                 if (memcmp(nfit_memdev->memdev, memdev, length) == 0) {
366                         list_move_tail(&nfit_memdev->list, &acpi_desc->memdevs);
367                         return true;
368                 }
369
370         nfit_memdev = devm_kzalloc(dev, sizeof(*nfit_memdev), GFP_KERNEL);
371         if (!nfit_memdev)
372                 return false;
373         INIT_LIST_HEAD(&nfit_memdev->list);
374         nfit_memdev->memdev = memdev;
375         list_add_tail(&nfit_memdev->list, &acpi_desc->memdevs);
376         dev_dbg(dev, "%s: memdev handle: %#x spa: %d dcr: %d\n",
377                         __func__, memdev->device_handle, memdev->range_index,
378                         memdev->region_index);
379         return true;
380 }
381
382 static bool add_dcr(struct acpi_nfit_desc *acpi_desc,
383                 struct nfit_table_prev *prev,
384                 struct acpi_nfit_control_region *dcr)
385 {
386         size_t length = min_t(size_t, sizeof(*dcr), dcr->header.length);
387         struct device *dev = acpi_desc->dev;
388         struct nfit_dcr *nfit_dcr;
389
390         list_for_each_entry(nfit_dcr, &prev->dcrs, list)
391                 if (memcmp(nfit_dcr->dcr, dcr, length) == 0) {
392                         list_move_tail(&nfit_dcr->list, &acpi_desc->dcrs);
393                         return true;
394                 }
395
396         nfit_dcr = devm_kzalloc(dev, sizeof(*nfit_dcr), GFP_KERNEL);
397         if (!nfit_dcr)
398                 return false;
399         INIT_LIST_HEAD(&nfit_dcr->list);
400         nfit_dcr->dcr = dcr;
401         list_add_tail(&nfit_dcr->list, &acpi_desc->dcrs);
402         dev_dbg(dev, "%s: dcr index: %d windows: %d\n", __func__,
403                         dcr->region_index, dcr->windows);
404         return true;
405 }
406
407 static bool add_bdw(struct acpi_nfit_desc *acpi_desc,
408                 struct nfit_table_prev *prev,
409                 struct acpi_nfit_data_region *bdw)
410 {
411         size_t length = min_t(size_t, sizeof(*bdw), bdw->header.length);
412         struct device *dev = acpi_desc->dev;
413         struct nfit_bdw *nfit_bdw;
414
415         list_for_each_entry(nfit_bdw, &prev->bdws, list)
416                 if (memcmp(nfit_bdw->bdw, bdw, length) == 0) {
417                         list_move_tail(&nfit_bdw->list, &acpi_desc->bdws);
418                         return true;
419                 }
420
421         nfit_bdw = devm_kzalloc(dev, sizeof(*nfit_bdw), GFP_KERNEL);
422         if (!nfit_bdw)
423                 return false;
424         INIT_LIST_HEAD(&nfit_bdw->list);
425         nfit_bdw->bdw = bdw;
426         list_add_tail(&nfit_bdw->list, &acpi_desc->bdws);
427         dev_dbg(dev, "%s: bdw dcr: %d windows: %d\n", __func__,
428                         bdw->region_index, bdw->windows);
429         return true;
430 }
431
432 static bool add_idt(struct acpi_nfit_desc *acpi_desc,
433                 struct nfit_table_prev *prev,
434                 struct acpi_nfit_interleave *idt)
435 {
436         size_t length = min_t(size_t, sizeof(*idt), idt->header.length);
437         struct device *dev = acpi_desc->dev;
438         struct nfit_idt *nfit_idt;
439
440         list_for_each_entry(nfit_idt, &prev->idts, list)
441                 if (memcmp(nfit_idt->idt, idt, length) == 0) {
442                         list_move_tail(&nfit_idt->list, &acpi_desc->idts);
443                         return true;
444                 }
445
446         nfit_idt = devm_kzalloc(dev, sizeof(*nfit_idt), GFP_KERNEL);
447         if (!nfit_idt)
448                 return false;
449         INIT_LIST_HEAD(&nfit_idt->list);
450         nfit_idt->idt = idt;
451         list_add_tail(&nfit_idt->list, &acpi_desc->idts);
452         dev_dbg(dev, "%s: idt index: %d num_lines: %d\n", __func__,
453                         idt->interleave_index, idt->line_count);
454         return true;
455 }
456
457 static bool add_flush(struct acpi_nfit_desc *acpi_desc,
458                 struct nfit_table_prev *prev,
459                 struct acpi_nfit_flush_address *flush)
460 {
461         size_t length = min_t(size_t, sizeof(*flush), flush->header.length);
462         struct device *dev = acpi_desc->dev;
463         struct nfit_flush *nfit_flush;
464
465         list_for_each_entry(nfit_flush, &prev->flushes, list)
466                 if (memcmp(nfit_flush->flush, flush, length) == 0) {
467                         list_move_tail(&nfit_flush->list, &acpi_desc->flushes);
468                         return true;
469                 }
470
471         nfit_flush = devm_kzalloc(dev, sizeof(*nfit_flush), GFP_KERNEL);
472         if (!nfit_flush)
473                 return false;
474         INIT_LIST_HEAD(&nfit_flush->list);
475         nfit_flush->flush = flush;
476         list_add_tail(&nfit_flush->list, &acpi_desc->flushes);
477         dev_dbg(dev, "%s: nfit_flush handle: %d hint_count: %d\n", __func__,
478                         flush->device_handle, flush->hint_count);
479         return true;
480 }
481
482 static void *add_table(struct acpi_nfit_desc *acpi_desc,
483                 struct nfit_table_prev *prev, void *table, const void *end)
484 {
485         struct device *dev = acpi_desc->dev;
486         struct acpi_nfit_header *hdr;
487         void *err = ERR_PTR(-ENOMEM);
488
489         if (table >= end)
490                 return NULL;
491
492         hdr = table;
493         if (!hdr->length) {
494                 dev_warn(dev, "found a zero length table '%d' parsing nfit\n",
495                         hdr->type);
496                 return NULL;
497         }
498
499         switch (hdr->type) {
500         case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
501                 if (!add_spa(acpi_desc, prev, table))
502                         return err;
503                 break;
504         case ACPI_NFIT_TYPE_MEMORY_MAP:
505                 if (!add_memdev(acpi_desc, prev, table))
506                         return err;
507                 break;
508         case ACPI_NFIT_TYPE_CONTROL_REGION:
509                 if (!add_dcr(acpi_desc, prev, table))
510                         return err;
511                 break;
512         case ACPI_NFIT_TYPE_DATA_REGION:
513                 if (!add_bdw(acpi_desc, prev, table))
514                         return err;
515                 break;
516         case ACPI_NFIT_TYPE_INTERLEAVE:
517                 if (!add_idt(acpi_desc, prev, table))
518                         return err;
519                 break;
520         case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
521                 if (!add_flush(acpi_desc, prev, table))
522                         return err;
523                 break;
524         case ACPI_NFIT_TYPE_SMBIOS:
525                 dev_dbg(dev, "%s: smbios\n", __func__);
526                 break;
527         default:
528                 dev_err(dev, "unknown table '%d' parsing nfit\n", hdr->type);
529                 break;
530         }
531
532         return table + hdr->length;
533 }
534
535 static void nfit_mem_find_spa_bdw(struct acpi_nfit_desc *acpi_desc,
536                 struct nfit_mem *nfit_mem)
537 {
538         u32 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
539         u16 dcr = nfit_mem->dcr->region_index;
540         struct nfit_spa *nfit_spa;
541
542         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
543                 u16 range_index = nfit_spa->spa->range_index;
544                 int type = nfit_spa_type(nfit_spa->spa);
545                 struct nfit_memdev *nfit_memdev;
546
547                 if (type != NFIT_SPA_BDW)
548                         continue;
549
550                 list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
551                         if (nfit_memdev->memdev->range_index != range_index)
552                                 continue;
553                         if (nfit_memdev->memdev->device_handle != device_handle)
554                                 continue;
555                         if (nfit_memdev->memdev->region_index != dcr)
556                                 continue;
557
558                         nfit_mem->spa_bdw = nfit_spa->spa;
559                         return;
560                 }
561         }
562
563         dev_dbg(acpi_desc->dev, "SPA-BDW not found for SPA-DCR %d\n",
564                         nfit_mem->spa_dcr->range_index);
565         nfit_mem->bdw = NULL;
566 }
567
568 static void nfit_mem_init_bdw(struct acpi_nfit_desc *acpi_desc,
569                 struct nfit_mem *nfit_mem, struct acpi_nfit_system_address *spa)
570 {
571         u16 dcr = __to_nfit_memdev(nfit_mem)->region_index;
572         struct nfit_memdev *nfit_memdev;
573         struct nfit_flush *nfit_flush;
574         struct nfit_bdw *nfit_bdw;
575         struct nfit_idt *nfit_idt;
576         u16 idt_idx, range_index;
577
578         list_for_each_entry(nfit_bdw, &acpi_desc->bdws, list) {
579                 if (nfit_bdw->bdw->region_index != dcr)
580                         continue;
581                 nfit_mem->bdw = nfit_bdw->bdw;
582                 break;
583         }
584
585         if (!nfit_mem->bdw)
586                 return;
587
588         nfit_mem_find_spa_bdw(acpi_desc, nfit_mem);
589
590         if (!nfit_mem->spa_bdw)
591                 return;
592
593         range_index = nfit_mem->spa_bdw->range_index;
594         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
595                 if (nfit_memdev->memdev->range_index != range_index ||
596                                 nfit_memdev->memdev->region_index != dcr)
597                         continue;
598                 nfit_mem->memdev_bdw = nfit_memdev->memdev;
599                 idt_idx = nfit_memdev->memdev->interleave_index;
600                 list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
601                         if (nfit_idt->idt->interleave_index != idt_idx)
602                                 continue;
603                         nfit_mem->idt_bdw = nfit_idt->idt;
604                         break;
605                 }
606
607                 list_for_each_entry(nfit_flush, &acpi_desc->flushes, list) {
608                         if (nfit_flush->flush->device_handle !=
609                                         nfit_memdev->memdev->device_handle)
610                                 continue;
611                         nfit_mem->nfit_flush = nfit_flush;
612                         break;
613                 }
614                 break;
615         }
616 }
617
618 static int nfit_mem_dcr_init(struct acpi_nfit_desc *acpi_desc,
619                 struct acpi_nfit_system_address *spa)
620 {
621         struct nfit_mem *nfit_mem, *found;
622         struct nfit_memdev *nfit_memdev;
623         int type = nfit_spa_type(spa);
624
625         switch (type) {
626         case NFIT_SPA_DCR:
627         case NFIT_SPA_PM:
628                 break;
629         default:
630                 return 0;
631         }
632
633         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
634                 struct nfit_dcr *nfit_dcr;
635                 u32 device_handle;
636                 u16 dcr;
637
638                 if (nfit_memdev->memdev->range_index != spa->range_index)
639                         continue;
640                 found = NULL;
641                 dcr = nfit_memdev->memdev->region_index;
642                 device_handle = nfit_memdev->memdev->device_handle;
643                 list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
644                         if (__to_nfit_memdev(nfit_mem)->device_handle
645                                         == device_handle) {
646                                 found = nfit_mem;
647                                 break;
648                         }
649
650                 if (found)
651                         nfit_mem = found;
652                 else {
653                         nfit_mem = devm_kzalloc(acpi_desc->dev,
654                                         sizeof(*nfit_mem), GFP_KERNEL);
655                         if (!nfit_mem)
656                                 return -ENOMEM;
657                         INIT_LIST_HEAD(&nfit_mem->list);
658                         list_add(&nfit_mem->list, &acpi_desc->dimms);
659                 }
660
661                 list_for_each_entry(nfit_dcr, &acpi_desc->dcrs, list) {
662                         if (nfit_dcr->dcr->region_index != dcr)
663                                 continue;
664                         /*
665                          * Record the control region for the dimm.  For
666                          * the ACPI 6.1 case, where there are separate
667                          * control regions for the pmem vs blk
668                          * interfaces, be sure to record the extended
669                          * blk details.
670                          */
671                         if (!nfit_mem->dcr)
672                                 nfit_mem->dcr = nfit_dcr->dcr;
673                         else if (nfit_mem->dcr->windows == 0
674                                         && nfit_dcr->dcr->windows)
675                                 nfit_mem->dcr = nfit_dcr->dcr;
676                         break;
677                 }
678
679                 if (dcr && !nfit_mem->dcr) {
680                         dev_err(acpi_desc->dev, "SPA %d missing DCR %d\n",
681                                         spa->range_index, dcr);
682                         return -ENODEV;
683                 }
684
685                 if (type == NFIT_SPA_DCR) {
686                         struct nfit_idt *nfit_idt;
687                         u16 idt_idx;
688
689                         /* multiple dimms may share a SPA when interleaved */
690                         nfit_mem->spa_dcr = spa;
691                         nfit_mem->memdev_dcr = nfit_memdev->memdev;
692                         idt_idx = nfit_memdev->memdev->interleave_index;
693                         list_for_each_entry(nfit_idt, &acpi_desc->idts, list) {
694                                 if (nfit_idt->idt->interleave_index != idt_idx)
695                                         continue;
696                                 nfit_mem->idt_dcr = nfit_idt->idt;
697                                 break;
698                         }
699                         nfit_mem_init_bdw(acpi_desc, nfit_mem, spa);
700                 } else {
701                         /*
702                          * A single dimm may belong to multiple SPA-PM
703                          * ranges, record at least one in addition to
704                          * any SPA-DCR range.
705                          */
706                         nfit_mem->memdev_pmem = nfit_memdev->memdev;
707                 }
708         }
709
710         return 0;
711 }
712
713 static int nfit_mem_cmp(void *priv, struct list_head *_a, struct list_head *_b)
714 {
715         struct nfit_mem *a = container_of(_a, typeof(*a), list);
716         struct nfit_mem *b = container_of(_b, typeof(*b), list);
717         u32 handleA, handleB;
718
719         handleA = __to_nfit_memdev(a)->device_handle;
720         handleB = __to_nfit_memdev(b)->device_handle;
721         if (handleA < handleB)
722                 return -1;
723         else if (handleA > handleB)
724                 return 1;
725         return 0;
726 }
727
728 static int nfit_mem_init(struct acpi_nfit_desc *acpi_desc)
729 {
730         struct nfit_spa *nfit_spa;
731
732         /*
733          * For each SPA-DCR or SPA-PMEM address range find its
734          * corresponding MEMDEV(s).  From each MEMDEV find the
735          * corresponding DCR.  Then, if we're operating on a SPA-DCR,
736          * try to find a SPA-BDW and a corresponding BDW that references
737          * the DCR.  Throw it all into an nfit_mem object.  Note, that
738          * BDWs are optional.
739          */
740         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
741                 int rc;
742
743                 rc = nfit_mem_dcr_init(acpi_desc, nfit_spa->spa);
744                 if (rc)
745                         return rc;
746         }
747
748         list_sort(NULL, &acpi_desc->dimms, nfit_mem_cmp);
749
750         return 0;
751 }
752
753 static ssize_t revision_show(struct device *dev,
754                 struct device_attribute *attr, char *buf)
755 {
756         struct nvdimm_bus *nvdimm_bus = to_nvdimm_bus(dev);
757         struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
758         struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
759
760         return sprintf(buf, "%d\n", acpi_desc->acpi_header.revision);
761 }
762 static DEVICE_ATTR_RO(revision);
763
764 static struct attribute *acpi_nfit_attributes[] = {
765         &dev_attr_revision.attr,
766         NULL,
767 };
768
769 static struct attribute_group acpi_nfit_attribute_group = {
770         .name = "nfit",
771         .attrs = acpi_nfit_attributes,
772 };
773
774 static const struct attribute_group *acpi_nfit_attribute_groups[] = {
775         &nvdimm_bus_attribute_group,
776         &acpi_nfit_attribute_group,
777         NULL,
778 };
779
780 static struct acpi_nfit_memory_map *to_nfit_memdev(struct device *dev)
781 {
782         struct nvdimm *nvdimm = to_nvdimm(dev);
783         struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
784
785         return __to_nfit_memdev(nfit_mem);
786 }
787
788 static struct acpi_nfit_control_region *to_nfit_dcr(struct device *dev)
789 {
790         struct nvdimm *nvdimm = to_nvdimm(dev);
791         struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
792
793         return nfit_mem->dcr;
794 }
795
796 static ssize_t handle_show(struct device *dev,
797                 struct device_attribute *attr, char *buf)
798 {
799         struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
800
801         return sprintf(buf, "%#x\n", memdev->device_handle);
802 }
803 static DEVICE_ATTR_RO(handle);
804
805 static ssize_t phys_id_show(struct device *dev,
806                 struct device_attribute *attr, char *buf)
807 {
808         struct acpi_nfit_memory_map *memdev = to_nfit_memdev(dev);
809
810         return sprintf(buf, "%#x\n", memdev->physical_id);
811 }
812 static DEVICE_ATTR_RO(phys_id);
813
814 static ssize_t vendor_show(struct device *dev,
815                 struct device_attribute *attr, char *buf)
816 {
817         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
818
819         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->vendor_id));
820 }
821 static DEVICE_ATTR_RO(vendor);
822
823 static ssize_t rev_id_show(struct device *dev,
824                 struct device_attribute *attr, char *buf)
825 {
826         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
827
828         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->revision_id));
829 }
830 static DEVICE_ATTR_RO(rev_id);
831
832 static ssize_t device_show(struct device *dev,
833                 struct device_attribute *attr, char *buf)
834 {
835         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
836
837         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->device_id));
838 }
839 static DEVICE_ATTR_RO(device);
840
841 static ssize_t format_show(struct device *dev,
842                 struct device_attribute *attr, char *buf)
843 {
844         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
845
846         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->code));
847 }
848 static DEVICE_ATTR_RO(format);
849
850 static ssize_t subsystem_vendor_show(struct device *dev,
851                 struct device_attribute *attr, char *buf)
852 {
853         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
854
855         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_vendor_id));
856 }
857 static DEVICE_ATTR_RO(subsystem_vendor);
858
859 static ssize_t subsystem_rev_id_show(struct device *dev,
860                 struct device_attribute *attr, char *buf)
861 {
862         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
863
864         return sprintf(buf, "0x%04x\n",
865                         be16_to_cpu(dcr->subsystem_revision_id));
866 }
867 static DEVICE_ATTR_RO(subsystem_rev_id);
868
869 static ssize_t subsystem_device_show(struct device *dev,
870                 struct device_attribute *attr, char *buf)
871 {
872         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
873
874         return sprintf(buf, "0x%04x\n", be16_to_cpu(dcr->subsystem_device_id));
875 }
876 static DEVICE_ATTR_RO(subsystem_device);
877
878 static ssize_t serial_show(struct device *dev,
879                 struct device_attribute *attr, char *buf)
880 {
881         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
882
883         return sprintf(buf, "0x%08x\n", be32_to_cpu(dcr->serial_number));
884 }
885 static DEVICE_ATTR_RO(serial);
886
887 static ssize_t flags_show(struct device *dev,
888                 struct device_attribute *attr, char *buf)
889 {
890         u16 flags = to_nfit_memdev(dev)->flags;
891
892         return sprintf(buf, "%s%s%s%s%s\n",
893                 flags & ACPI_NFIT_MEM_SAVE_FAILED ? "save_fail " : "",
894                 flags & ACPI_NFIT_MEM_RESTORE_FAILED ? "restore_fail " : "",
895                 flags & ACPI_NFIT_MEM_FLUSH_FAILED ? "flush_fail " : "",
896                 flags & ACPI_NFIT_MEM_NOT_ARMED ? "not_armed " : "",
897                 flags & ACPI_NFIT_MEM_HEALTH_OBSERVED ? "smart_event " : "");
898 }
899 static DEVICE_ATTR_RO(flags);
900
901 static ssize_t id_show(struct device *dev,
902                 struct device_attribute *attr, char *buf)
903 {
904         struct acpi_nfit_control_region *dcr = to_nfit_dcr(dev);
905
906         if (dcr->valid_fields & ACPI_NFIT_CONTROL_MFG_INFO_VALID)
907                 return sprintf(buf, "%04x-%02x-%04x-%08x\n",
908                                 be16_to_cpu(dcr->vendor_id),
909                                 dcr->manufacturing_location,
910                                 be16_to_cpu(dcr->manufacturing_date),
911                                 be32_to_cpu(dcr->serial_number));
912         else
913                 return sprintf(buf, "%04x-%08x\n",
914                                 be16_to_cpu(dcr->vendor_id),
915                                 be32_to_cpu(dcr->serial_number));
916 }
917 static DEVICE_ATTR_RO(id);
918
919 static struct attribute *acpi_nfit_dimm_attributes[] = {
920         &dev_attr_handle.attr,
921         &dev_attr_phys_id.attr,
922         &dev_attr_vendor.attr,
923         &dev_attr_device.attr,
924         &dev_attr_rev_id.attr,
925         &dev_attr_subsystem_vendor.attr,
926         &dev_attr_subsystem_device.attr,
927         &dev_attr_subsystem_rev_id.attr,
928         &dev_attr_format.attr,
929         &dev_attr_serial.attr,
930         &dev_attr_flags.attr,
931         &dev_attr_id.attr,
932         NULL,
933 };
934
935 static umode_t acpi_nfit_dimm_attr_visible(struct kobject *kobj,
936                 struct attribute *a, int n)
937 {
938         struct device *dev = container_of(kobj, struct device, kobj);
939
940         if (to_nfit_dcr(dev))
941                 return a->mode;
942         else
943                 return 0;
944 }
945
946 static struct attribute_group acpi_nfit_dimm_attribute_group = {
947         .name = "nfit",
948         .attrs = acpi_nfit_dimm_attributes,
949         .is_visible = acpi_nfit_dimm_attr_visible,
950 };
951
952 static const struct attribute_group *acpi_nfit_dimm_attribute_groups[] = {
953         &nvdimm_attribute_group,
954         &nd_device_attribute_group,
955         &acpi_nfit_dimm_attribute_group,
956         NULL,
957 };
958
959 static struct nvdimm *acpi_nfit_dimm_by_handle(struct acpi_nfit_desc *acpi_desc,
960                 u32 device_handle)
961 {
962         struct nfit_mem *nfit_mem;
963
964         list_for_each_entry(nfit_mem, &acpi_desc->dimms, list)
965                 if (__to_nfit_memdev(nfit_mem)->device_handle == device_handle)
966                         return nfit_mem->nvdimm;
967
968         return NULL;
969 }
970
971 static int acpi_nfit_add_dimm(struct acpi_nfit_desc *acpi_desc,
972                 struct nfit_mem *nfit_mem, u32 device_handle)
973 {
974         struct acpi_device *adev, *adev_dimm;
975         struct device *dev = acpi_desc->dev;
976         const u8 *uuid = to_nfit_uuid(NFIT_DEV_DIMM);
977         int i;
978
979         nfit_mem->dsm_mask = acpi_desc->dimm_dsm_force_en;
980         adev = to_acpi_dev(acpi_desc);
981         if (!adev)
982                 return 0;
983
984         adev_dimm = acpi_find_child_device(adev, device_handle, false);
985         nfit_mem->adev = adev_dimm;
986         if (!adev_dimm) {
987                 dev_err(dev, "no ACPI.NFIT device with _ADR %#x, disabling...\n",
988                                 device_handle);
989                 return force_enable_dimms ? 0 : -ENODEV;
990         }
991
992         for (i = ND_CMD_SMART; i <= ND_CMD_VENDOR; i++)
993                 if (acpi_check_dsm(adev_dimm->handle, uuid, 1, 1ULL << i))
994                         set_bit(i, &nfit_mem->dsm_mask);
995
996         return 0;
997 }
998
999 static int acpi_nfit_register_dimms(struct acpi_nfit_desc *acpi_desc)
1000 {
1001         struct nfit_mem *nfit_mem;
1002         int dimm_count = 0;
1003
1004         list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1005                 struct nvdimm *nvdimm;
1006                 unsigned long flags = 0;
1007                 u32 device_handle;
1008                 u16 mem_flags;
1009                 int rc;
1010
1011                 device_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1012                 nvdimm = acpi_nfit_dimm_by_handle(acpi_desc, device_handle);
1013                 if (nvdimm) {
1014                         dimm_count++;
1015                         continue;
1016                 }
1017
1018                 if (nfit_mem->bdw && nfit_mem->memdev_pmem)
1019                         flags |= NDD_ALIASING;
1020
1021                 mem_flags = __to_nfit_memdev(nfit_mem)->flags;
1022                 if (mem_flags & ACPI_NFIT_MEM_NOT_ARMED)
1023                         flags |= NDD_UNARMED;
1024
1025                 rc = acpi_nfit_add_dimm(acpi_desc, nfit_mem, device_handle);
1026                 if (rc)
1027                         continue;
1028
1029                 nvdimm = nvdimm_create(acpi_desc->nvdimm_bus, nfit_mem,
1030                                 acpi_nfit_dimm_attribute_groups,
1031                                 flags, &nfit_mem->dsm_mask);
1032                 if (!nvdimm)
1033                         return -ENOMEM;
1034
1035                 nfit_mem->nvdimm = nvdimm;
1036                 dimm_count++;
1037
1038                 if ((mem_flags & ACPI_NFIT_MEM_FAILED_MASK) == 0)
1039                         continue;
1040
1041                 dev_info(acpi_desc->dev, "%s flags:%s%s%s%s\n",
1042                                 nvdimm_name(nvdimm),
1043                   mem_flags & ACPI_NFIT_MEM_SAVE_FAILED ? " save_fail" : "",
1044                   mem_flags & ACPI_NFIT_MEM_RESTORE_FAILED ? " restore_fail":"",
1045                   mem_flags & ACPI_NFIT_MEM_FLUSH_FAILED ? " flush_fail" : "",
1046                   mem_flags & ACPI_NFIT_MEM_NOT_ARMED ? " not_armed" : "");
1047
1048         }
1049
1050         return nvdimm_bus_check_dimm_count(acpi_desc->nvdimm_bus, dimm_count);
1051 }
1052
1053 static void acpi_nfit_init_dsms(struct acpi_nfit_desc *acpi_desc)
1054 {
1055         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1056         const u8 *uuid = to_nfit_uuid(NFIT_DEV_BUS);
1057         struct acpi_device *adev;
1058         int i;
1059
1060         nd_desc->dsm_mask = acpi_desc->bus_dsm_force_en;
1061         adev = to_acpi_dev(acpi_desc);
1062         if (!adev)
1063                 return;
1064
1065         for (i = ND_CMD_ARS_CAP; i <= ND_CMD_CLEAR_ERROR; i++)
1066                 if (acpi_check_dsm(adev->handle, uuid, 1, 1ULL << i))
1067                         set_bit(i, &nd_desc->dsm_mask);
1068 }
1069
1070 static ssize_t range_index_show(struct device *dev,
1071                 struct device_attribute *attr, char *buf)
1072 {
1073         struct nd_region *nd_region = to_nd_region(dev);
1074         struct nfit_spa *nfit_spa = nd_region_provider_data(nd_region);
1075
1076         return sprintf(buf, "%d\n", nfit_spa->spa->range_index);
1077 }
1078 static DEVICE_ATTR_RO(range_index);
1079
1080 static struct attribute *acpi_nfit_region_attributes[] = {
1081         &dev_attr_range_index.attr,
1082         NULL,
1083 };
1084
1085 static struct attribute_group acpi_nfit_region_attribute_group = {
1086         .name = "nfit",
1087         .attrs = acpi_nfit_region_attributes,
1088 };
1089
1090 static const struct attribute_group *acpi_nfit_region_attribute_groups[] = {
1091         &nd_region_attribute_group,
1092         &nd_mapping_attribute_group,
1093         &nd_device_attribute_group,
1094         &nd_numa_attribute_group,
1095         &acpi_nfit_region_attribute_group,
1096         NULL,
1097 };
1098
1099 /* enough info to uniquely specify an interleave set */
1100 struct nfit_set_info {
1101         struct nfit_set_info_map {
1102                 u64 region_offset;
1103                 u32 serial_number;
1104                 u32 pad;
1105         } mapping[0];
1106 };
1107
1108 static size_t sizeof_nfit_set_info(int num_mappings)
1109 {
1110         return sizeof(struct nfit_set_info)
1111                 + num_mappings * sizeof(struct nfit_set_info_map);
1112 }
1113
1114 static int cmp_map(const void *m0, const void *m1)
1115 {
1116         const struct nfit_set_info_map *map0 = m0;
1117         const struct nfit_set_info_map *map1 = m1;
1118
1119         return memcmp(&map0->region_offset, &map1->region_offset,
1120                         sizeof(u64));
1121 }
1122
1123 /* Retrieve the nth entry referencing this spa */
1124 static struct acpi_nfit_memory_map *memdev_from_spa(
1125                 struct acpi_nfit_desc *acpi_desc, u16 range_index, int n)
1126 {
1127         struct nfit_memdev *nfit_memdev;
1128
1129         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list)
1130                 if (nfit_memdev->memdev->range_index == range_index)
1131                         if (n-- == 0)
1132                                 return nfit_memdev->memdev;
1133         return NULL;
1134 }
1135
1136 static int acpi_nfit_init_interleave_set(struct acpi_nfit_desc *acpi_desc,
1137                 struct nd_region_desc *ndr_desc,
1138                 struct acpi_nfit_system_address *spa)
1139 {
1140         int i, spa_type = nfit_spa_type(spa);
1141         struct device *dev = acpi_desc->dev;
1142         struct nd_interleave_set *nd_set;
1143         u16 nr = ndr_desc->num_mappings;
1144         struct nfit_set_info *info;
1145
1146         if (spa_type == NFIT_SPA_PM || spa_type == NFIT_SPA_VOLATILE)
1147                 /* pass */;
1148         else
1149                 return 0;
1150
1151         nd_set = devm_kzalloc(dev, sizeof(*nd_set), GFP_KERNEL);
1152         if (!nd_set)
1153                 return -ENOMEM;
1154
1155         info = devm_kzalloc(dev, sizeof_nfit_set_info(nr), GFP_KERNEL);
1156         if (!info)
1157                 return -ENOMEM;
1158         for (i = 0; i < nr; i++) {
1159                 struct nd_mapping *nd_mapping = &ndr_desc->nd_mapping[i];
1160                 struct nfit_set_info_map *map = &info->mapping[i];
1161                 struct nvdimm *nvdimm = nd_mapping->nvdimm;
1162                 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
1163                 struct acpi_nfit_memory_map *memdev = memdev_from_spa(acpi_desc,
1164                                 spa->range_index, i);
1165
1166                 if (!memdev || !nfit_mem->dcr) {
1167                         dev_err(dev, "%s: failed to find DCR\n", __func__);
1168                         return -ENODEV;
1169                 }
1170
1171                 map->region_offset = memdev->region_offset;
1172                 map->serial_number = nfit_mem->dcr->serial_number;
1173         }
1174
1175         sort(&info->mapping[0], nr, sizeof(struct nfit_set_info_map),
1176                         cmp_map, NULL);
1177         nd_set->cookie = nd_fletcher64(info, sizeof_nfit_set_info(nr), 0);
1178         ndr_desc->nd_set = nd_set;
1179         devm_kfree(dev, info);
1180
1181         return 0;
1182 }
1183
1184 static u64 to_interleave_offset(u64 offset, struct nfit_blk_mmio *mmio)
1185 {
1186         struct acpi_nfit_interleave *idt = mmio->idt;
1187         u32 sub_line_offset, line_index, line_offset;
1188         u64 line_no, table_skip_count, table_offset;
1189
1190         line_no = div_u64_rem(offset, mmio->line_size, &sub_line_offset);
1191         table_skip_count = div_u64_rem(line_no, mmio->num_lines, &line_index);
1192         line_offset = idt->line_offset[line_index]
1193                 * mmio->line_size;
1194         table_offset = table_skip_count * mmio->table_size;
1195
1196         return mmio->base_offset + line_offset + table_offset + sub_line_offset;
1197 }
1198
1199 static void wmb_blk(struct nfit_blk *nfit_blk)
1200 {
1201
1202         if (nfit_blk->nvdimm_flush) {
1203                 /*
1204                  * The first wmb() is needed to 'sfence' all previous writes
1205                  * such that they are architecturally visible for the platform
1206                  * buffer flush.  Note that we've already arranged for pmem
1207                  * writes to avoid the cache via arch_memcpy_to_pmem().  The
1208                  * final wmb() ensures ordering for the NVDIMM flush write.
1209                  */
1210                 wmb();
1211                 writeq(1, nfit_blk->nvdimm_flush);
1212                 wmb();
1213         } else
1214                 wmb_pmem();
1215 }
1216
1217 static u32 read_blk_stat(struct nfit_blk *nfit_blk, unsigned int bw)
1218 {
1219         struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1220         u64 offset = nfit_blk->stat_offset + mmio->size * bw;
1221
1222         if (mmio->num_lines)
1223                 offset = to_interleave_offset(offset, mmio);
1224
1225         return readl(mmio->addr.base + offset);
1226 }
1227
1228 static void write_blk_ctl(struct nfit_blk *nfit_blk, unsigned int bw,
1229                 resource_size_t dpa, unsigned int len, unsigned int write)
1230 {
1231         u64 cmd, offset;
1232         struct nfit_blk_mmio *mmio = &nfit_blk->mmio[DCR];
1233
1234         enum {
1235                 BCW_OFFSET_MASK = (1ULL << 48)-1,
1236                 BCW_LEN_SHIFT = 48,
1237                 BCW_LEN_MASK = (1ULL << 8) - 1,
1238                 BCW_CMD_SHIFT = 56,
1239         };
1240
1241         cmd = (dpa >> L1_CACHE_SHIFT) & BCW_OFFSET_MASK;
1242         len = len >> L1_CACHE_SHIFT;
1243         cmd |= ((u64) len & BCW_LEN_MASK) << BCW_LEN_SHIFT;
1244         cmd |= ((u64) write) << BCW_CMD_SHIFT;
1245
1246         offset = nfit_blk->cmd_offset + mmio->size * bw;
1247         if (mmio->num_lines)
1248                 offset = to_interleave_offset(offset, mmio);
1249
1250         writeq(cmd, mmio->addr.base + offset);
1251         wmb_blk(nfit_blk);
1252
1253         if (nfit_blk->dimm_flags & NFIT_BLK_DCR_LATCH)
1254                 readq(mmio->addr.base + offset);
1255 }
1256
1257 static int acpi_nfit_blk_single_io(struct nfit_blk *nfit_blk,
1258                 resource_size_t dpa, void *iobuf, size_t len, int rw,
1259                 unsigned int lane)
1260 {
1261         struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1262         unsigned int copied = 0;
1263         u64 base_offset;
1264         int rc;
1265
1266         base_offset = nfit_blk->bdw_offset + dpa % L1_CACHE_BYTES
1267                 + lane * mmio->size;
1268         write_blk_ctl(nfit_blk, lane, dpa, len, rw);
1269         while (len) {
1270                 unsigned int c;
1271                 u64 offset;
1272
1273                 if (mmio->num_lines) {
1274                         u32 line_offset;
1275
1276                         offset = to_interleave_offset(base_offset + copied,
1277                                         mmio);
1278                         div_u64_rem(offset, mmio->line_size, &line_offset);
1279                         c = min_t(size_t, len, mmio->line_size - line_offset);
1280                 } else {
1281                         offset = base_offset + nfit_blk->bdw_offset;
1282                         c = len;
1283                 }
1284
1285                 if (rw)
1286                         memcpy_to_pmem(mmio->addr.aperture + offset,
1287                                         iobuf + copied, c);
1288                 else {
1289                         if (nfit_blk->dimm_flags & NFIT_BLK_READ_FLUSH)
1290                                 mmio_flush_range((void __force *)
1291                                         mmio->addr.aperture + offset, c);
1292
1293                         memcpy_from_pmem(iobuf + copied,
1294                                         mmio->addr.aperture + offset, c);
1295                 }
1296
1297                 copied += c;
1298                 len -= c;
1299         }
1300
1301         if (rw)
1302                 wmb_blk(nfit_blk);
1303
1304         rc = read_blk_stat(nfit_blk, lane) ? -EIO : 0;
1305         return rc;
1306 }
1307
1308 static int acpi_nfit_blk_region_do_io(struct nd_blk_region *ndbr,
1309                 resource_size_t dpa, void *iobuf, u64 len, int rw)
1310 {
1311         struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1312         struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1313         struct nd_region *nd_region = nfit_blk->nd_region;
1314         unsigned int lane, copied = 0;
1315         int rc = 0;
1316
1317         lane = nd_region_acquire_lane(nd_region);
1318         while (len) {
1319                 u64 c = min(len, mmio->size);
1320
1321                 rc = acpi_nfit_blk_single_io(nfit_blk, dpa + copied,
1322                                 iobuf + copied, c, rw, lane);
1323                 if (rc)
1324                         break;
1325
1326                 copied += c;
1327                 len -= c;
1328         }
1329         nd_region_release_lane(nd_region, lane);
1330
1331         return rc;
1332 }
1333
1334 static void nfit_spa_mapping_release(struct kref *kref)
1335 {
1336         struct nfit_spa_mapping *spa_map = to_spa_map(kref);
1337         struct acpi_nfit_system_address *spa = spa_map->spa;
1338         struct acpi_nfit_desc *acpi_desc = spa_map->acpi_desc;
1339
1340         WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
1341         dev_dbg(acpi_desc->dev, "%s: SPA%d\n", __func__, spa->range_index);
1342         if (spa_map->type == SPA_MAP_APERTURE)
1343                 memunmap((void __force *)spa_map->addr.aperture);
1344         else
1345                 iounmap(spa_map->addr.base);
1346         release_mem_region(spa->address, spa->length);
1347         list_del(&spa_map->list);
1348         kfree(spa_map);
1349 }
1350
1351 static struct nfit_spa_mapping *find_spa_mapping(
1352                 struct acpi_nfit_desc *acpi_desc,
1353                 struct acpi_nfit_system_address *spa)
1354 {
1355         struct nfit_spa_mapping *spa_map;
1356
1357         WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
1358         list_for_each_entry(spa_map, &acpi_desc->spa_maps, list)
1359                 if (spa_map->spa == spa)
1360                         return spa_map;
1361
1362         return NULL;
1363 }
1364
1365 static void nfit_spa_unmap(struct acpi_nfit_desc *acpi_desc,
1366                 struct acpi_nfit_system_address *spa)
1367 {
1368         struct nfit_spa_mapping *spa_map;
1369
1370         mutex_lock(&acpi_desc->spa_map_mutex);
1371         spa_map = find_spa_mapping(acpi_desc, spa);
1372
1373         if (spa_map)
1374                 kref_put(&spa_map->kref, nfit_spa_mapping_release);
1375         mutex_unlock(&acpi_desc->spa_map_mutex);
1376 }
1377
1378 static void __iomem *__nfit_spa_map(struct acpi_nfit_desc *acpi_desc,
1379                 struct acpi_nfit_system_address *spa, enum spa_map_type type)
1380 {
1381         resource_size_t start = spa->address;
1382         resource_size_t n = spa->length;
1383         struct nfit_spa_mapping *spa_map;
1384         struct resource *res;
1385
1386         WARN_ON(!mutex_is_locked(&acpi_desc->spa_map_mutex));
1387
1388         spa_map = find_spa_mapping(acpi_desc, spa);
1389         if (spa_map) {
1390                 kref_get(&spa_map->kref);
1391                 return spa_map->addr.base;
1392         }
1393
1394         spa_map = kzalloc(sizeof(*spa_map), GFP_KERNEL);
1395         if (!spa_map)
1396                 return NULL;
1397
1398         INIT_LIST_HEAD(&spa_map->list);
1399         spa_map->spa = spa;
1400         kref_init(&spa_map->kref);
1401         spa_map->acpi_desc = acpi_desc;
1402
1403         res = request_mem_region(start, n, dev_name(acpi_desc->dev));
1404         if (!res)
1405                 goto err_mem;
1406
1407         spa_map->type = type;
1408         if (type == SPA_MAP_APERTURE)
1409                 spa_map->addr.aperture = (void __pmem *)memremap(start, n,
1410                                                         ARCH_MEMREMAP_PMEM);
1411         else
1412                 spa_map->addr.base = ioremap_nocache(start, n);
1413
1414
1415         if (!spa_map->addr.base)
1416                 goto err_map;
1417
1418         list_add_tail(&spa_map->list, &acpi_desc->spa_maps);
1419         return spa_map->addr.base;
1420
1421  err_map:
1422         release_mem_region(start, n);
1423  err_mem:
1424         kfree(spa_map);
1425         return NULL;
1426 }
1427
1428 /**
1429  * nfit_spa_map - interleave-aware managed-mappings of acpi_nfit_system_address ranges
1430  * @nvdimm_bus: NFIT-bus that provided the spa table entry
1431  * @nfit_spa: spa table to map
1432  * @type: aperture or control region
1433  *
1434  * In the case where block-data-window apertures and
1435  * dimm-control-regions are interleaved they will end up sharing a
1436  * single request_mem_region() + ioremap() for the address range.  In
1437  * the style of devm nfit_spa_map() mappings are automatically dropped
1438  * when all region devices referencing the same mapping are disabled /
1439  * unbound.
1440  */
1441 static void __iomem *nfit_spa_map(struct acpi_nfit_desc *acpi_desc,
1442                 struct acpi_nfit_system_address *spa, enum spa_map_type type)
1443 {
1444         void __iomem *iomem;
1445
1446         mutex_lock(&acpi_desc->spa_map_mutex);
1447         iomem = __nfit_spa_map(acpi_desc, spa, type);
1448         mutex_unlock(&acpi_desc->spa_map_mutex);
1449
1450         return iomem;
1451 }
1452
1453 static int nfit_blk_init_interleave(struct nfit_blk_mmio *mmio,
1454                 struct acpi_nfit_interleave *idt, u16 interleave_ways)
1455 {
1456         if (idt) {
1457                 mmio->num_lines = idt->line_count;
1458                 mmio->line_size = idt->line_size;
1459                 if (interleave_ways == 0)
1460                         return -ENXIO;
1461                 mmio->table_size = mmio->num_lines * interleave_ways
1462                         * mmio->line_size;
1463         }
1464
1465         return 0;
1466 }
1467
1468 static int acpi_nfit_blk_get_flags(struct nvdimm_bus_descriptor *nd_desc,
1469                 struct nvdimm *nvdimm, struct nfit_blk *nfit_blk)
1470 {
1471         struct nd_cmd_dimm_flags flags;
1472         int rc;
1473
1474         memset(&flags, 0, sizeof(flags));
1475         rc = nd_desc->ndctl(nd_desc, nvdimm, ND_CMD_DIMM_FLAGS, &flags,
1476                         sizeof(flags), NULL);
1477
1478         if (rc >= 0 && flags.status == 0)
1479                 nfit_blk->dimm_flags = flags.flags;
1480         else if (rc == -ENOTTY) {
1481                 /* fall back to a conservative default */
1482                 nfit_blk->dimm_flags = NFIT_BLK_DCR_LATCH | NFIT_BLK_READ_FLUSH;
1483                 rc = 0;
1484         } else
1485                 rc = -ENXIO;
1486
1487         return rc;
1488 }
1489
1490 static int acpi_nfit_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
1491                 struct device *dev)
1492 {
1493         struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1494         struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1495         struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1496         struct nfit_flush *nfit_flush;
1497         struct nfit_blk_mmio *mmio;
1498         struct nfit_blk *nfit_blk;
1499         struct nfit_mem *nfit_mem;
1500         struct nvdimm *nvdimm;
1501         int rc;
1502
1503         nvdimm = nd_blk_region_to_dimm(ndbr);
1504         nfit_mem = nvdimm_provider_data(nvdimm);
1505         if (!nfit_mem || !nfit_mem->dcr || !nfit_mem->bdw) {
1506                 dev_dbg(dev, "%s: missing%s%s%s\n", __func__,
1507                                 nfit_mem ? "" : " nfit_mem",
1508                                 (nfit_mem && nfit_mem->dcr) ? "" : " dcr",
1509                                 (nfit_mem && nfit_mem->bdw) ? "" : " bdw");
1510                 return -ENXIO;
1511         }
1512
1513         nfit_blk = devm_kzalloc(dev, sizeof(*nfit_blk), GFP_KERNEL);
1514         if (!nfit_blk)
1515                 return -ENOMEM;
1516         nd_blk_region_set_provider_data(ndbr, nfit_blk);
1517         nfit_blk->nd_region = to_nd_region(dev);
1518
1519         /* map block aperture memory */
1520         nfit_blk->bdw_offset = nfit_mem->bdw->offset;
1521         mmio = &nfit_blk->mmio[BDW];
1522         mmio->addr.base = nfit_spa_map(acpi_desc, nfit_mem->spa_bdw,
1523                         SPA_MAP_APERTURE);
1524         if (!mmio->addr.base) {
1525                 dev_dbg(dev, "%s: %s failed to map bdw\n", __func__,
1526                                 nvdimm_name(nvdimm));
1527                 return -ENOMEM;
1528         }
1529         mmio->size = nfit_mem->bdw->size;
1530         mmio->base_offset = nfit_mem->memdev_bdw->region_offset;
1531         mmio->idt = nfit_mem->idt_bdw;
1532         mmio->spa = nfit_mem->spa_bdw;
1533         rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_bdw,
1534                         nfit_mem->memdev_bdw->interleave_ways);
1535         if (rc) {
1536                 dev_dbg(dev, "%s: %s failed to init bdw interleave\n",
1537                                 __func__, nvdimm_name(nvdimm));
1538                 return rc;
1539         }
1540
1541         /* map block control memory */
1542         nfit_blk->cmd_offset = nfit_mem->dcr->command_offset;
1543         nfit_blk->stat_offset = nfit_mem->dcr->status_offset;
1544         mmio = &nfit_blk->mmio[DCR];
1545         mmio->addr.base = nfit_spa_map(acpi_desc, nfit_mem->spa_dcr,
1546                         SPA_MAP_CONTROL);
1547         if (!mmio->addr.base) {
1548                 dev_dbg(dev, "%s: %s failed to map dcr\n", __func__,
1549                                 nvdimm_name(nvdimm));
1550                 return -ENOMEM;
1551         }
1552         mmio->size = nfit_mem->dcr->window_size;
1553         mmio->base_offset = nfit_mem->memdev_dcr->region_offset;
1554         mmio->idt = nfit_mem->idt_dcr;
1555         mmio->spa = nfit_mem->spa_dcr;
1556         rc = nfit_blk_init_interleave(mmio, nfit_mem->idt_dcr,
1557                         nfit_mem->memdev_dcr->interleave_ways);
1558         if (rc) {
1559                 dev_dbg(dev, "%s: %s failed to init dcr interleave\n",
1560                                 __func__, nvdimm_name(nvdimm));
1561                 return rc;
1562         }
1563
1564         rc = acpi_nfit_blk_get_flags(nd_desc, nvdimm, nfit_blk);
1565         if (rc < 0) {
1566                 dev_dbg(dev, "%s: %s failed get DIMM flags\n",
1567                                 __func__, nvdimm_name(nvdimm));
1568                 return rc;
1569         }
1570
1571         nfit_flush = nfit_mem->nfit_flush;
1572         if (nfit_flush && nfit_flush->flush->hint_count != 0) {
1573                 nfit_blk->nvdimm_flush = devm_ioremap_nocache(dev,
1574                                 nfit_flush->flush->hint_address[0], 8);
1575                 if (!nfit_blk->nvdimm_flush)
1576                         return -ENOMEM;
1577         }
1578
1579         if (!arch_has_wmb_pmem() && !nfit_blk->nvdimm_flush)
1580                 dev_warn(dev, "unable to guarantee persistence of writes\n");
1581
1582         if (mmio->line_size == 0)
1583                 return 0;
1584
1585         if ((u32) nfit_blk->cmd_offset % mmio->line_size
1586                         + 8 > mmio->line_size) {
1587                 dev_dbg(dev, "cmd_offset crosses interleave boundary\n");
1588                 return -ENXIO;
1589         } else if ((u32) nfit_blk->stat_offset % mmio->line_size
1590                         + 8 > mmio->line_size) {
1591                 dev_dbg(dev, "stat_offset crosses interleave boundary\n");
1592                 return -ENXIO;
1593         }
1594
1595         return 0;
1596 }
1597
1598 static void acpi_nfit_blk_region_disable(struct nvdimm_bus *nvdimm_bus,
1599                 struct device *dev)
1600 {
1601         struct nvdimm_bus_descriptor *nd_desc = to_nd_desc(nvdimm_bus);
1602         struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
1603         struct nd_blk_region *ndbr = to_nd_blk_region(dev);
1604         struct nfit_blk *nfit_blk = nd_blk_region_provider_data(ndbr);
1605         int i;
1606
1607         if (!nfit_blk)
1608                 return; /* never enabled */
1609
1610         /* auto-free BLK spa mappings */
1611         for (i = 0; i < 2; i++) {
1612                 struct nfit_blk_mmio *mmio = &nfit_blk->mmio[i];
1613
1614                 if (mmio->addr.base)
1615                         nfit_spa_unmap(acpi_desc, mmio->spa);
1616         }
1617         nd_blk_region_set_provider_data(ndbr, NULL);
1618         /* devm will free nfit_blk */
1619 }
1620
1621 static int ars_get_cap(struct acpi_nfit_desc *acpi_desc,
1622                 struct nd_cmd_ars_cap *cmd, struct nfit_spa *nfit_spa)
1623 {
1624         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1625         struct acpi_nfit_system_address *spa = nfit_spa->spa;
1626         int cmd_rc, rc;
1627
1628         cmd->address = spa->address;
1629         cmd->length = spa->length;
1630         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_CAP, cmd,
1631                         sizeof(*cmd), &cmd_rc);
1632         if (rc < 0)
1633                 return rc;
1634         return cmd_rc;
1635 }
1636
1637 static int ars_start(struct acpi_nfit_desc *acpi_desc, struct nfit_spa *nfit_spa)
1638 {
1639         int rc;
1640         int cmd_rc;
1641         struct nd_cmd_ars_start ars_start;
1642         struct acpi_nfit_system_address *spa = nfit_spa->spa;
1643         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1644
1645         memset(&ars_start, 0, sizeof(ars_start));
1646         ars_start.address = spa->address;
1647         ars_start.length = spa->length;
1648         if (nfit_spa_type(spa) == NFIT_SPA_PM)
1649                 ars_start.type = ND_ARS_PERSISTENT;
1650         else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE)
1651                 ars_start.type = ND_ARS_VOLATILE;
1652         else
1653                 return -ENOTTY;
1654
1655         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1656                         sizeof(ars_start), &cmd_rc);
1657
1658         if (rc < 0)
1659                 return rc;
1660         return cmd_rc;
1661 }
1662
1663 static int ars_continue(struct acpi_nfit_desc *acpi_desc)
1664 {
1665         int rc, cmd_rc;
1666         struct nd_cmd_ars_start ars_start;
1667         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1668         struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
1669
1670         memset(&ars_start, 0, sizeof(ars_start));
1671         ars_start.address = ars_status->restart_address;
1672         ars_start.length = ars_status->restart_length;
1673         ars_start.type = ars_status->type;
1674         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_START, &ars_start,
1675                         sizeof(ars_start), &cmd_rc);
1676         if (rc < 0)
1677                 return rc;
1678         return cmd_rc;
1679 }
1680
1681 static int ars_get_status(struct acpi_nfit_desc *acpi_desc)
1682 {
1683         struct nvdimm_bus_descriptor *nd_desc = &acpi_desc->nd_desc;
1684         struct nd_cmd_ars_status *ars_status = acpi_desc->ars_status;
1685         int rc, cmd_rc;
1686
1687         rc = nd_desc->ndctl(nd_desc, NULL, ND_CMD_ARS_STATUS, ars_status,
1688                         acpi_desc->ars_status_size, &cmd_rc);
1689         if (rc < 0)
1690                 return rc;
1691         return cmd_rc;
1692 }
1693
1694 static int ars_status_process_records(struct nvdimm_bus *nvdimm_bus,
1695                 struct nd_cmd_ars_status *ars_status)
1696 {
1697         int rc;
1698         u32 i;
1699
1700         for (i = 0; i < ars_status->num_records; i++) {
1701                 rc = nvdimm_bus_add_poison(nvdimm_bus,
1702                                 ars_status->records[i].err_address,
1703                                 ars_status->records[i].length);
1704                 if (rc)
1705                         return rc;
1706         }
1707
1708         return 0;
1709 }
1710
1711 static void acpi_nfit_remove_resource(void *data)
1712 {
1713         struct resource *res = data;
1714
1715         remove_resource(res);
1716 }
1717
1718 static int acpi_nfit_insert_resource(struct acpi_nfit_desc *acpi_desc,
1719                 struct nd_region_desc *ndr_desc)
1720 {
1721         struct resource *res, *nd_res = ndr_desc->res;
1722         int is_pmem, ret;
1723
1724         /* No operation if the region is already registered as PMEM */
1725         is_pmem = region_intersects(nd_res->start, resource_size(nd_res),
1726                                 IORESOURCE_MEM, IORES_DESC_PERSISTENT_MEMORY);
1727         if (is_pmem == REGION_INTERSECTS)
1728                 return 0;
1729
1730         res = devm_kzalloc(acpi_desc->dev, sizeof(*res), GFP_KERNEL);
1731         if (!res)
1732                 return -ENOMEM;
1733
1734         res->name = "Persistent Memory";
1735         res->start = nd_res->start;
1736         res->end = nd_res->end;
1737         res->flags = IORESOURCE_MEM;
1738         res->desc = IORES_DESC_PERSISTENT_MEMORY;
1739
1740         ret = insert_resource(&iomem_resource, res);
1741         if (ret)
1742                 return ret;
1743
1744         ret = devm_add_action(acpi_desc->dev, acpi_nfit_remove_resource, res);
1745         if (ret) {
1746                 remove_resource(res);
1747                 return ret;
1748         }
1749
1750         return 0;
1751 }
1752
1753 static int acpi_nfit_init_mapping(struct acpi_nfit_desc *acpi_desc,
1754                 struct nd_mapping *nd_mapping, struct nd_region_desc *ndr_desc,
1755                 struct acpi_nfit_memory_map *memdev,
1756                 struct nfit_spa *nfit_spa)
1757 {
1758         struct nvdimm *nvdimm = acpi_nfit_dimm_by_handle(acpi_desc,
1759                         memdev->device_handle);
1760         struct acpi_nfit_system_address *spa = nfit_spa->spa;
1761         struct nd_blk_region_desc *ndbr_desc;
1762         struct nfit_mem *nfit_mem;
1763         int blk_valid = 0;
1764
1765         if (!nvdimm) {
1766                 dev_err(acpi_desc->dev, "spa%d dimm: %#x not found\n",
1767                                 spa->range_index, memdev->device_handle);
1768                 return -ENODEV;
1769         }
1770
1771         nd_mapping->nvdimm = nvdimm;
1772         switch (nfit_spa_type(spa)) {
1773         case NFIT_SPA_PM:
1774         case NFIT_SPA_VOLATILE:
1775                 nd_mapping->start = memdev->address;
1776                 nd_mapping->size = memdev->region_size;
1777                 break;
1778         case NFIT_SPA_DCR:
1779                 nfit_mem = nvdimm_provider_data(nvdimm);
1780                 if (!nfit_mem || !nfit_mem->bdw) {
1781                         dev_dbg(acpi_desc->dev, "spa%d %s missing bdw\n",
1782                                         spa->range_index, nvdimm_name(nvdimm));
1783                 } else {
1784                         nd_mapping->size = nfit_mem->bdw->capacity;
1785                         nd_mapping->start = nfit_mem->bdw->start_address;
1786                         ndr_desc->num_lanes = nfit_mem->bdw->windows;
1787                         blk_valid = 1;
1788                 }
1789
1790                 ndr_desc->nd_mapping = nd_mapping;
1791                 ndr_desc->num_mappings = blk_valid;
1792                 ndbr_desc = to_blk_region_desc(ndr_desc);
1793                 ndbr_desc->enable = acpi_nfit_blk_region_enable;
1794                 ndbr_desc->disable = acpi_nfit_blk_region_disable;
1795                 ndbr_desc->do_io = acpi_desc->blk_do_io;
1796                 nfit_spa->nd_region = nvdimm_blk_region_create(acpi_desc->nvdimm_bus,
1797                                 ndr_desc);
1798                 if (!nfit_spa->nd_region)
1799                         return -ENOMEM;
1800                 break;
1801         }
1802
1803         return 0;
1804 }
1805
1806 static int acpi_nfit_register_region(struct acpi_nfit_desc *acpi_desc,
1807                 struct nfit_spa *nfit_spa)
1808 {
1809         static struct nd_mapping nd_mappings[ND_MAX_MAPPINGS];
1810         struct acpi_nfit_system_address *spa = nfit_spa->spa;
1811         struct nd_blk_region_desc ndbr_desc;
1812         struct nd_region_desc *ndr_desc;
1813         struct nfit_memdev *nfit_memdev;
1814         struct nvdimm_bus *nvdimm_bus;
1815         struct resource res;
1816         int count = 0, rc;
1817
1818         if (nfit_spa->nd_region)
1819                 return 0;
1820
1821         if (spa->range_index == 0) {
1822                 dev_dbg(acpi_desc->dev, "%s: detected invalid spa index\n",
1823                                 __func__);
1824                 return 0;
1825         }
1826
1827         memset(&res, 0, sizeof(res));
1828         memset(&nd_mappings, 0, sizeof(nd_mappings));
1829         memset(&ndbr_desc, 0, sizeof(ndbr_desc));
1830         res.start = spa->address;
1831         res.end = res.start + spa->length - 1;
1832         ndr_desc = &ndbr_desc.ndr_desc;
1833         ndr_desc->res = &res;
1834         ndr_desc->provider_data = nfit_spa;
1835         ndr_desc->attr_groups = acpi_nfit_region_attribute_groups;
1836         if (spa->flags & ACPI_NFIT_PROXIMITY_VALID)
1837                 ndr_desc->numa_node = acpi_map_pxm_to_online_node(
1838                                                 spa->proximity_domain);
1839         else
1840                 ndr_desc->numa_node = NUMA_NO_NODE;
1841
1842         list_for_each_entry(nfit_memdev, &acpi_desc->memdevs, list) {
1843                 struct acpi_nfit_memory_map *memdev = nfit_memdev->memdev;
1844                 struct nd_mapping *nd_mapping;
1845
1846                 if (memdev->range_index != spa->range_index)
1847                         continue;
1848                 if (count >= ND_MAX_MAPPINGS) {
1849                         dev_err(acpi_desc->dev, "spa%d exceeds max mappings %d\n",
1850                                         spa->range_index, ND_MAX_MAPPINGS);
1851                         return -ENXIO;
1852                 }
1853                 nd_mapping = &nd_mappings[count++];
1854                 rc = acpi_nfit_init_mapping(acpi_desc, nd_mapping, ndr_desc,
1855                                 memdev, nfit_spa);
1856                 if (rc)
1857                         goto out;
1858         }
1859
1860         ndr_desc->nd_mapping = nd_mappings;
1861         ndr_desc->num_mappings = count;
1862         rc = acpi_nfit_init_interleave_set(acpi_desc, ndr_desc, spa);
1863         if (rc)
1864                 goto out;
1865
1866         nvdimm_bus = acpi_desc->nvdimm_bus;
1867         if (nfit_spa_type(spa) == NFIT_SPA_PM) {
1868                 rc = acpi_nfit_insert_resource(acpi_desc, ndr_desc);
1869                 if (rc) {
1870                         dev_warn(acpi_desc->dev,
1871                                 "failed to insert pmem resource to iomem: %d\n",
1872                                 rc);
1873                         goto out;
1874                 }
1875
1876                 nfit_spa->nd_region = nvdimm_pmem_region_create(nvdimm_bus,
1877                                 ndr_desc);
1878                 if (!nfit_spa->nd_region)
1879                         rc = -ENOMEM;
1880         } else if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE) {
1881                 nfit_spa->nd_region = nvdimm_volatile_region_create(nvdimm_bus,
1882                                 ndr_desc);
1883                 if (!nfit_spa->nd_region)
1884                         rc = -ENOMEM;
1885         }
1886
1887  out:
1888         if (rc)
1889                 dev_err(acpi_desc->dev, "failed to register spa range %d\n",
1890                                 nfit_spa->spa->range_index);
1891         return rc;
1892 }
1893
1894 static int ars_status_alloc(struct acpi_nfit_desc *acpi_desc,
1895                 u32 max_ars)
1896 {
1897         struct device *dev = acpi_desc->dev;
1898         struct nd_cmd_ars_status *ars_status;
1899
1900         if (acpi_desc->ars_status && acpi_desc->ars_status_size >= max_ars) {
1901                 memset(acpi_desc->ars_status, 0, acpi_desc->ars_status_size);
1902                 return 0;
1903         }
1904
1905         if (acpi_desc->ars_status)
1906                 devm_kfree(dev, acpi_desc->ars_status);
1907         acpi_desc->ars_status = NULL;
1908         ars_status = devm_kzalloc(dev, max_ars, GFP_KERNEL);
1909         if (!ars_status)
1910                 return -ENOMEM;
1911         acpi_desc->ars_status = ars_status;
1912         acpi_desc->ars_status_size = max_ars;
1913         return 0;
1914 }
1915
1916 static int acpi_nfit_query_poison(struct acpi_nfit_desc *acpi_desc,
1917                 struct nfit_spa *nfit_spa)
1918 {
1919         struct acpi_nfit_system_address *spa = nfit_spa->spa;
1920         int rc;
1921
1922         if (!nfit_spa->max_ars) {
1923                 struct nd_cmd_ars_cap ars_cap;
1924
1925                 memset(&ars_cap, 0, sizeof(ars_cap));
1926                 rc = ars_get_cap(acpi_desc, &ars_cap, nfit_spa);
1927                 if (rc < 0)
1928                         return rc;
1929                 nfit_spa->max_ars = ars_cap.max_ars_out;
1930                 nfit_spa->clear_err_unit = ars_cap.clear_err_unit;
1931                 /* check that the supported scrub types match the spa type */
1932                 if (nfit_spa_type(spa) == NFIT_SPA_VOLATILE &&
1933                                 ((ars_cap.status >> 16) & ND_ARS_VOLATILE) == 0)
1934                         return -ENOTTY;
1935                 else if (nfit_spa_type(spa) == NFIT_SPA_PM &&
1936                                 ((ars_cap.status >> 16) & ND_ARS_PERSISTENT) == 0)
1937                         return -ENOTTY;
1938         }
1939
1940         if (ars_status_alloc(acpi_desc, nfit_spa->max_ars))
1941                 return -ENOMEM;
1942
1943         rc = ars_get_status(acpi_desc);
1944         if (rc < 0 && rc != -ENOSPC)
1945                 return rc;
1946
1947         if (ars_status_process_records(acpi_desc->nvdimm_bus,
1948                                 acpi_desc->ars_status))
1949                 return -ENOMEM;
1950
1951         return 0;
1952 }
1953
1954 static void acpi_nfit_async_scrub(struct acpi_nfit_desc *acpi_desc,
1955                 struct nfit_spa *nfit_spa)
1956 {
1957         struct acpi_nfit_system_address *spa = nfit_spa->spa;
1958         unsigned int overflow_retry = scrub_overflow_abort;
1959         u64 init_ars_start = 0, init_ars_len = 0;
1960         struct device *dev = acpi_desc->dev;
1961         unsigned int tmo = scrub_timeout;
1962         int rc;
1963
1964         if (nfit_spa->ars_done || !nfit_spa->nd_region)
1965                 return;
1966
1967         rc = ars_start(acpi_desc, nfit_spa);
1968         /*
1969          * If we timed out the initial scan we'll still be busy here,
1970          * and will wait another timeout before giving up permanently.
1971          */
1972         if (rc < 0 && rc != -EBUSY)
1973                 return;
1974
1975         do {
1976                 u64 ars_start, ars_len;
1977
1978                 if (acpi_desc->cancel)
1979                         break;
1980                 rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
1981                 if (rc == -ENOTTY)
1982                         break;
1983                 if (rc == -EBUSY && !tmo) {
1984                         dev_warn(dev, "range %d ars timeout, aborting\n",
1985                                         spa->range_index);
1986                         break;
1987                 }
1988
1989                 if (rc == -EBUSY) {
1990                         /*
1991                          * Note, entries may be appended to the list
1992                          * while the lock is dropped, but the workqueue
1993                          * being active prevents entries being deleted /
1994                          * freed.
1995                          */
1996                         mutex_unlock(&acpi_desc->init_mutex);
1997                         ssleep(1);
1998                         tmo--;
1999                         mutex_lock(&acpi_desc->init_mutex);
2000                         continue;
2001                 }
2002
2003                 /* we got some results, but there are more pending... */
2004                 if (rc == -ENOSPC && overflow_retry--) {
2005                         if (!init_ars_len) {
2006                                 init_ars_len = acpi_desc->ars_status->length;
2007                                 init_ars_start = acpi_desc->ars_status->address;
2008                         }
2009                         rc = ars_continue(acpi_desc);
2010                 }
2011
2012                 if (rc < 0) {
2013                         dev_warn(dev, "range %d ars continuation failed\n",
2014                                         spa->range_index);
2015                         break;
2016                 }
2017
2018                 if (init_ars_len) {
2019                         ars_start = init_ars_start;
2020                         ars_len = init_ars_len;
2021                 } else {
2022                         ars_start = acpi_desc->ars_status->address;
2023                         ars_len = acpi_desc->ars_status->length;
2024                 }
2025                 dev_dbg(dev, "spa range: %d ars from %#llx + %#llx complete\n",
2026                                 spa->range_index, ars_start, ars_len);
2027                 /* notify the region about new poison entries */
2028                 nvdimm_region_notify(nfit_spa->nd_region,
2029                                 NVDIMM_REVALIDATE_POISON);
2030                 break;
2031         } while (1);
2032 }
2033
2034 static void acpi_nfit_scrub(struct work_struct *work)
2035 {
2036         struct device *dev;
2037         u64 init_scrub_length = 0;
2038         struct nfit_spa *nfit_spa;
2039         u64 init_scrub_address = 0;
2040         bool init_ars_done = false;
2041         struct acpi_nfit_desc *acpi_desc;
2042         unsigned int tmo = scrub_timeout;
2043         unsigned int overflow_retry = scrub_overflow_abort;
2044
2045         acpi_desc = container_of(work, typeof(*acpi_desc), work);
2046         dev = acpi_desc->dev;
2047
2048         /*
2049          * We scrub in 2 phases.  The first phase waits for any platform
2050          * firmware initiated scrubs to complete and then we go search for the
2051          * affected spa regions to mark them scanned.  In the second phase we
2052          * initiate a directed scrub for every range that was not scrubbed in
2053          * phase 1.
2054          */
2055
2056         /* process platform firmware initiated scrubs */
2057  retry:
2058         mutex_lock(&acpi_desc->init_mutex);
2059         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2060                 struct nd_cmd_ars_status *ars_status;
2061                 struct acpi_nfit_system_address *spa;
2062                 u64 ars_start, ars_len;
2063                 int rc;
2064
2065                 if (acpi_desc->cancel)
2066                         break;
2067
2068                 if (nfit_spa->nd_region)
2069                         continue;
2070
2071                 if (init_ars_done) {
2072                         /*
2073                          * No need to re-query, we're now just
2074                          * reconciling all the ranges covered by the
2075                          * initial scrub
2076                          */
2077                         rc = 0;
2078                 } else
2079                         rc = acpi_nfit_query_poison(acpi_desc, nfit_spa);
2080
2081                 if (rc == -ENOTTY) {
2082                         /* no ars capability, just register spa and move on */
2083                         acpi_nfit_register_region(acpi_desc, nfit_spa);
2084                         continue;
2085                 }
2086
2087                 if (rc == -EBUSY && !tmo) {
2088                         /* fallthrough to directed scrub in phase 2 */
2089                         dev_warn(dev, "timeout awaiting ars results, continuing...\n");
2090                         break;
2091                 } else if (rc == -EBUSY) {
2092                         mutex_unlock(&acpi_desc->init_mutex);
2093                         ssleep(1);
2094                         tmo--;
2095                         goto retry;
2096                 }
2097
2098                 /* we got some results, but there are more pending... */
2099                 if (rc == -ENOSPC && overflow_retry--) {
2100                         ars_status = acpi_desc->ars_status;
2101                         /*
2102                          * Record the original scrub range, so that we
2103                          * can recall all the ranges impacted by the
2104                          * initial scrub.
2105                          */
2106                         if (!init_scrub_length) {
2107                                 init_scrub_length = ars_status->length;
2108                                 init_scrub_address = ars_status->address;
2109                         }
2110                         rc = ars_continue(acpi_desc);
2111                         if (rc == 0) {
2112                                 mutex_unlock(&acpi_desc->init_mutex);
2113                                 goto retry;
2114                         }
2115                 }
2116
2117                 if (rc < 0) {
2118                         /*
2119                          * Initial scrub failed, we'll give it one more
2120                          * try below...
2121                          */
2122                         break;
2123                 }
2124
2125                 /* We got some final results, record completed ranges */
2126                 ars_status = acpi_desc->ars_status;
2127                 if (init_scrub_length) {
2128                         ars_start = init_scrub_address;
2129                         ars_len = ars_start + init_scrub_length;
2130                 } else {
2131                         ars_start = ars_status->address;
2132                         ars_len = ars_status->length;
2133                 }
2134                 spa = nfit_spa->spa;
2135
2136                 if (!init_ars_done) {
2137                         init_ars_done = true;
2138                         dev_dbg(dev, "init scrub %#llx + %#llx complete\n",
2139                                         ars_start, ars_len);
2140                 }
2141                 if (ars_start <= spa->address && ars_start + ars_len
2142                                 >= spa->address + spa->length)
2143                         acpi_nfit_register_region(acpi_desc, nfit_spa);
2144         }
2145
2146         /*
2147          * For all the ranges not covered by an initial scrub we still
2148          * want to see if there are errors, but it's ok to discover them
2149          * asynchronously.
2150          */
2151         list_for_each_entry(nfit_spa, &acpi_desc->spas, list) {
2152                 /*
2153                  * Flag all the ranges that still need scrubbing, but
2154                  * register them now to make data available.
2155                  */
2156                 if (nfit_spa->nd_region)
2157                         nfit_spa->ars_done = 1;
2158                 else
2159                         acpi_nfit_register_region(acpi_desc, nfit_spa);
2160         }
2161
2162         list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2163                 acpi_nfit_async_scrub(acpi_desc, nfit_spa);
2164         mutex_unlock(&acpi_desc->init_mutex);
2165 }
2166
2167 static int acpi_nfit_register_regions(struct acpi_nfit_desc *acpi_desc)
2168 {
2169         struct nfit_spa *nfit_spa;
2170         int rc;
2171
2172         list_for_each_entry(nfit_spa, &acpi_desc->spas, list)
2173                 if (nfit_spa_type(nfit_spa->spa) == NFIT_SPA_DCR) {
2174                         /* BLK regions don't need to wait for ars results */
2175                         rc = acpi_nfit_register_region(acpi_desc, nfit_spa);
2176                         if (rc)
2177                                 return rc;
2178                 }
2179
2180         queue_work(nfit_wq, &acpi_desc->work);
2181         return 0;
2182 }
2183
2184 static int acpi_nfit_check_deletions(struct acpi_nfit_desc *acpi_desc,
2185                 struct nfit_table_prev *prev)
2186 {
2187         struct device *dev = acpi_desc->dev;
2188
2189         if (!list_empty(&prev->spas) ||
2190                         !list_empty(&prev->memdevs) ||
2191                         !list_empty(&prev->dcrs) ||
2192                         !list_empty(&prev->bdws) ||
2193                         !list_empty(&prev->idts) ||
2194                         !list_empty(&prev->flushes)) {
2195                 dev_err(dev, "new nfit deletes entries (unsupported)\n");
2196                 return -ENXIO;
2197         }
2198         return 0;
2199 }
2200
2201 int acpi_nfit_init(struct acpi_nfit_desc *acpi_desc, acpi_size sz)
2202 {
2203         struct device *dev = acpi_desc->dev;
2204         struct nfit_table_prev prev;
2205         const void *end;
2206         u8 *data;
2207         int rc;
2208
2209         mutex_lock(&acpi_desc->init_mutex);
2210
2211         INIT_LIST_HEAD(&prev.spas);
2212         INIT_LIST_HEAD(&prev.memdevs);
2213         INIT_LIST_HEAD(&prev.dcrs);
2214         INIT_LIST_HEAD(&prev.bdws);
2215         INIT_LIST_HEAD(&prev.idts);
2216         INIT_LIST_HEAD(&prev.flushes);
2217
2218         list_cut_position(&prev.spas, &acpi_desc->spas,
2219                                 acpi_desc->spas.prev);
2220         list_cut_position(&prev.memdevs, &acpi_desc->memdevs,
2221                                 acpi_desc->memdevs.prev);
2222         list_cut_position(&prev.dcrs, &acpi_desc->dcrs,
2223                                 acpi_desc->dcrs.prev);
2224         list_cut_position(&prev.bdws, &acpi_desc->bdws,
2225                                 acpi_desc->bdws.prev);
2226         list_cut_position(&prev.idts, &acpi_desc->idts,
2227                                 acpi_desc->idts.prev);
2228         list_cut_position(&prev.flushes, &acpi_desc->flushes,
2229                                 acpi_desc->flushes.prev);
2230
2231         data = (u8 *) acpi_desc->nfit;
2232         end = data + sz;
2233         while (!IS_ERR_OR_NULL(data))
2234                 data = add_table(acpi_desc, &prev, data, end);
2235
2236         if (IS_ERR(data)) {
2237                 dev_dbg(dev, "%s: nfit table parsing error: %ld\n", __func__,
2238                                 PTR_ERR(data));
2239                 rc = PTR_ERR(data);
2240                 goto out_unlock;
2241         }
2242
2243         rc = acpi_nfit_check_deletions(acpi_desc, &prev);
2244         if (rc)
2245                 goto out_unlock;
2246
2247         if (nfit_mem_init(acpi_desc) != 0) {
2248                 rc = -ENOMEM;
2249                 goto out_unlock;
2250         }
2251
2252         acpi_nfit_init_dsms(acpi_desc);
2253
2254         rc = acpi_nfit_register_dimms(acpi_desc);
2255         if (rc)
2256                 goto out_unlock;
2257
2258         rc = acpi_nfit_register_regions(acpi_desc);
2259
2260  out_unlock:
2261         mutex_unlock(&acpi_desc->init_mutex);
2262         return rc;
2263 }
2264 EXPORT_SYMBOL_GPL(acpi_nfit_init);
2265
2266 struct acpi_nfit_flush_work {
2267         struct work_struct work;
2268         struct completion cmp;
2269 };
2270
2271 static void flush_probe(struct work_struct *work)
2272 {
2273         struct acpi_nfit_flush_work *flush;
2274
2275         flush = container_of(work, typeof(*flush), work);
2276         complete(&flush->cmp);
2277 }
2278
2279 static int acpi_nfit_flush_probe(struct nvdimm_bus_descriptor *nd_desc)
2280 {
2281         struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2282         struct device *dev = acpi_desc->dev;
2283         struct acpi_nfit_flush_work flush;
2284
2285         /* bounce the device lock to flush acpi_nfit_add / acpi_nfit_notify */
2286         device_lock(dev);
2287         device_unlock(dev);
2288
2289         /*
2290          * Scrub work could take 10s of seconds, userspace may give up so we
2291          * need to be interruptible while waiting.
2292          */
2293         INIT_WORK_ONSTACK(&flush.work, flush_probe);
2294         COMPLETION_INITIALIZER_ONSTACK(flush.cmp);
2295         queue_work(nfit_wq, &flush.work);
2296         return wait_for_completion_interruptible(&flush.cmp);
2297 }
2298
2299 static int acpi_nfit_clear_to_send(struct nvdimm_bus_descriptor *nd_desc,
2300                 struct nvdimm *nvdimm, unsigned int cmd)
2301 {
2302         struct acpi_nfit_desc *acpi_desc = to_acpi_nfit_desc(nd_desc);
2303
2304         if (nvdimm)
2305                 return 0;
2306         if (cmd != ND_CMD_ARS_START)
2307                 return 0;
2308
2309         /*
2310          * The kernel and userspace may race to initiate a scrub, but
2311          * the scrub thread is prepared to lose that initial race.  It
2312          * just needs guarantees that any ars it initiates are not
2313          * interrupted by any intervening start reqeusts from userspace.
2314          */
2315         if (work_busy(&acpi_desc->work))
2316                 return -EBUSY;
2317
2318         return 0;
2319 }
2320
2321 void acpi_nfit_desc_init(struct acpi_nfit_desc *acpi_desc, struct device *dev)
2322 {
2323         struct nvdimm_bus_descriptor *nd_desc;
2324
2325         dev_set_drvdata(dev, acpi_desc);
2326         acpi_desc->dev = dev;
2327         acpi_desc->blk_do_io = acpi_nfit_blk_region_do_io;
2328         nd_desc = &acpi_desc->nd_desc;
2329         nd_desc->provider_name = "ACPI.NFIT";
2330         nd_desc->ndctl = acpi_nfit_ctl;
2331         nd_desc->flush_probe = acpi_nfit_flush_probe;
2332         nd_desc->clear_to_send = acpi_nfit_clear_to_send;
2333         nd_desc->attr_groups = acpi_nfit_attribute_groups;
2334
2335         INIT_LIST_HEAD(&acpi_desc->spa_maps);
2336         INIT_LIST_HEAD(&acpi_desc->spas);
2337         INIT_LIST_HEAD(&acpi_desc->dcrs);
2338         INIT_LIST_HEAD(&acpi_desc->bdws);
2339         INIT_LIST_HEAD(&acpi_desc->idts);
2340         INIT_LIST_HEAD(&acpi_desc->flushes);
2341         INIT_LIST_HEAD(&acpi_desc->memdevs);
2342         INIT_LIST_HEAD(&acpi_desc->dimms);
2343         mutex_init(&acpi_desc->spa_map_mutex);
2344         mutex_init(&acpi_desc->init_mutex);
2345         INIT_WORK(&acpi_desc->work, acpi_nfit_scrub);
2346 }
2347 EXPORT_SYMBOL_GPL(acpi_nfit_desc_init);
2348
2349 static int acpi_nfit_add(struct acpi_device *adev)
2350 {
2351         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
2352         struct acpi_nfit_desc *acpi_desc;
2353         struct device *dev = &adev->dev;
2354         struct acpi_table_header *tbl;
2355         acpi_status status = AE_OK;
2356         acpi_size sz;
2357         int rc;
2358
2359         status = acpi_get_table_with_size("NFIT", 0, &tbl, &sz);
2360         if (ACPI_FAILURE(status)) {
2361                 /* This is ok, we could have an nvdimm hotplugged later */
2362                 dev_dbg(dev, "failed to find NFIT at startup\n");
2363                 return 0;
2364         }
2365
2366         acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2367         if (!acpi_desc)
2368                 return -ENOMEM;
2369         acpi_nfit_desc_init(acpi_desc, &adev->dev);
2370         acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, &acpi_desc->nd_desc);
2371         if (!acpi_desc->nvdimm_bus)
2372                 return -ENOMEM;
2373
2374         /*
2375          * Save the acpi header for later and then skip it,
2376          * making nfit point to the first nfit table header.
2377          */
2378         acpi_desc->acpi_header = *tbl;
2379         acpi_desc->nfit = (void *) tbl + sizeof(struct acpi_table_nfit);
2380         sz -= sizeof(struct acpi_table_nfit);
2381
2382         /* Evaluate _FIT and override with that if present */
2383         status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2384         if (ACPI_SUCCESS(status) && buf.length > 0) {
2385                 union acpi_object *obj;
2386                 /*
2387                  * Adjust for the acpi_object header of the _FIT
2388                  */
2389                 obj = buf.pointer;
2390                 if (obj->type == ACPI_TYPE_BUFFER) {
2391                         acpi_desc->nfit =
2392                                 (struct acpi_nfit_header *)obj->buffer.pointer;
2393                         sz = obj->buffer.length;
2394                 } else
2395                         dev_dbg(dev, "%s invalid type %d, ignoring _FIT\n",
2396                                  __func__, (int) obj->type);
2397         }
2398
2399         rc = acpi_nfit_init(acpi_desc, sz);
2400         if (rc) {
2401                 nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2402                 return rc;
2403         }
2404         return 0;
2405 }
2406
2407 static int acpi_nfit_remove(struct acpi_device *adev)
2408 {
2409         struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
2410
2411         acpi_desc->cancel = 1;
2412         flush_workqueue(nfit_wq);
2413         nvdimm_bus_unregister(acpi_desc->nvdimm_bus);
2414         return 0;
2415 }
2416
2417 static void acpi_nfit_notify(struct acpi_device *adev, u32 event)
2418 {
2419         struct acpi_nfit_desc *acpi_desc = dev_get_drvdata(&adev->dev);
2420         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
2421         struct acpi_nfit_header *nfit_saved;
2422         union acpi_object *obj;
2423         struct device *dev = &adev->dev;
2424         acpi_status status;
2425         int ret;
2426
2427         dev_dbg(dev, "%s: event: %d\n", __func__, event);
2428
2429         device_lock(dev);
2430         if (!dev->driver) {
2431                 /* dev->driver may be null if we're being removed */
2432                 dev_dbg(dev, "%s: no driver found for dev\n", __func__);
2433                 goto out_unlock;
2434         }
2435
2436         if (!acpi_desc) {
2437                 acpi_desc = devm_kzalloc(dev, sizeof(*acpi_desc), GFP_KERNEL);
2438                 if (!acpi_desc)
2439                         goto out_unlock;
2440                 acpi_nfit_desc_init(acpi_desc, &adev->dev);
2441                 acpi_desc->nvdimm_bus = nvdimm_bus_register(dev, &acpi_desc->nd_desc);
2442                 if (!acpi_desc->nvdimm_bus)
2443                         goto out_unlock;
2444         } else {
2445                 /*
2446                  * Finish previous registration before considering new
2447                  * regions.
2448                  */
2449                 flush_workqueue(nfit_wq);
2450         }
2451
2452         /* Evaluate _FIT */
2453         status = acpi_evaluate_object(adev->handle, "_FIT", NULL, &buf);
2454         if (ACPI_FAILURE(status)) {
2455                 dev_err(dev, "failed to evaluate _FIT\n");
2456                 goto out_unlock;
2457         }
2458
2459         nfit_saved = acpi_desc->nfit;
2460         obj = buf.pointer;
2461         if (obj->type == ACPI_TYPE_BUFFER) {
2462                 acpi_desc->nfit =
2463                         (struct acpi_nfit_header *)obj->buffer.pointer;
2464                 ret = acpi_nfit_init(acpi_desc, obj->buffer.length);
2465                 if (ret) {
2466                         /* Merge failed, restore old nfit, and exit */
2467                         acpi_desc->nfit = nfit_saved;
2468                         dev_err(dev, "failed to merge updated NFIT\n");
2469                 }
2470         } else {
2471                 /* Bad _FIT, restore old nfit */
2472                 dev_err(dev, "Invalid _FIT\n");
2473         }
2474         kfree(buf.pointer);
2475
2476  out_unlock:
2477         device_unlock(dev);
2478 }
2479
2480 static const struct acpi_device_id acpi_nfit_ids[] = {
2481         { "ACPI0012", 0 },
2482         { "", 0 },
2483 };
2484 MODULE_DEVICE_TABLE(acpi, acpi_nfit_ids);
2485
2486 static struct acpi_driver acpi_nfit_driver = {
2487         .name = KBUILD_MODNAME,
2488         .ids = acpi_nfit_ids,
2489         .ops = {
2490                 .add = acpi_nfit_add,
2491                 .remove = acpi_nfit_remove,
2492                 .notify = acpi_nfit_notify,
2493         },
2494 };
2495
2496 static __init int nfit_init(void)
2497 {
2498         BUILD_BUG_ON(sizeof(struct acpi_table_nfit) != 40);
2499         BUILD_BUG_ON(sizeof(struct acpi_nfit_system_address) != 56);
2500         BUILD_BUG_ON(sizeof(struct acpi_nfit_memory_map) != 48);
2501         BUILD_BUG_ON(sizeof(struct acpi_nfit_interleave) != 20);
2502         BUILD_BUG_ON(sizeof(struct acpi_nfit_smbios) != 9);
2503         BUILD_BUG_ON(sizeof(struct acpi_nfit_control_region) != 80);
2504         BUILD_BUG_ON(sizeof(struct acpi_nfit_data_region) != 40);
2505
2506         acpi_str_to_uuid(UUID_VOLATILE_MEMORY, nfit_uuid[NFIT_SPA_VOLATILE]);
2507         acpi_str_to_uuid(UUID_PERSISTENT_MEMORY, nfit_uuid[NFIT_SPA_PM]);
2508         acpi_str_to_uuid(UUID_CONTROL_REGION, nfit_uuid[NFIT_SPA_DCR]);
2509         acpi_str_to_uuid(UUID_DATA_REGION, nfit_uuid[NFIT_SPA_BDW]);
2510         acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_VDISK]);
2511         acpi_str_to_uuid(UUID_VOLATILE_VIRTUAL_CD, nfit_uuid[NFIT_SPA_VCD]);
2512         acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_DISK, nfit_uuid[NFIT_SPA_PDISK]);
2513         acpi_str_to_uuid(UUID_PERSISTENT_VIRTUAL_CD, nfit_uuid[NFIT_SPA_PCD]);
2514         acpi_str_to_uuid(UUID_NFIT_BUS, nfit_uuid[NFIT_DEV_BUS]);
2515         acpi_str_to_uuid(UUID_NFIT_DIMM, nfit_uuid[NFIT_DEV_DIMM]);
2516
2517         nfit_wq = create_singlethread_workqueue("nfit");
2518         if (!nfit_wq)
2519                 return -ENOMEM;
2520
2521         return acpi_bus_register_driver(&acpi_nfit_driver);
2522 }
2523
2524 static __exit void nfit_exit(void)
2525 {
2526         acpi_bus_unregister_driver(&acpi_nfit_driver);
2527         destroy_workqueue(nfit_wq);
2528 }
2529
2530 module_init(nfit_init);
2531 module_exit(nfit_exit);
2532 MODULE_LICENSE("GPL v2");
2533 MODULE_AUTHOR("Intel Corporation");