0e721c6fb1cfcb5d9a3c4d3f866c227bf44038e7
[cascardo/linux.git] / tools / testing / nvdimm / test / 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/platform_device.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/workqueue.h>
17 #include <linux/libnvdimm.h>
18 #include <linux/vmalloc.h>
19 #include <linux/device.h>
20 #include <linux/module.h>
21 #include <linux/mutex.h>
22 #include <linux/ndctl.h>
23 #include <linux/sizes.h>
24 #include <linux/list.h>
25 #include <linux/slab.h>
26 #include <nfit.h>
27 #include <nd.h>
28 #include "nfit_test.h"
29
30 /*
31  * Generate an NFIT table to describe the following topology:
32  *
33  * BUS0: Interleaved PMEM regions, and aliasing with BLK regions
34  *
35  *                     (a)                       (b)            DIMM   BLK-REGION
36  *           +----------+--------------+----------+---------+
37  * +------+  |  blk2.0  |     pm0.0    |  blk2.1  |  pm1.0  |    0      region2
38  * | imc0 +--+- - - - - region0 - - - -+----------+         +
39  * +--+---+  |  blk3.0  |     pm0.0    |  blk3.1  |  pm1.0  |    1      region3
40  *    |      +----------+--------------v----------v         v
41  * +--+---+                            |                    |
42  * | cpu0 |                                    region1
43  * +--+---+                            |                    |
44  *    |      +-------------------------^----------^         ^
45  * +--+---+  |                 blk4.0             |  pm1.0  |    2      region4
46  * | imc1 +--+-------------------------+----------+         +
47  * +------+  |                 blk5.0             |  pm1.0  |    3      region5
48  *           +-------------------------+----------+-+-------+
49  *
50  * +--+---+
51  * | cpu1 |
52  * +--+---+                   (Hotplug DIMM)
53  *    |      +----------------------------------------------+
54  * +--+---+  |                 blk6.0/pm7.0                 |    4      region6/7
55  * | imc0 +--+----------------------------------------------+
56  * +------+
57  *
58  *
59  * *) In this layout we have four dimms and two memory controllers in one
60  *    socket.  Each unique interface (BLK or PMEM) to DPA space
61  *    is identified by a region device with a dynamically assigned id.
62  *
63  * *) The first portion of dimm0 and dimm1 are interleaved as REGION0.
64  *    A single PMEM namespace "pm0.0" is created using half of the
65  *    REGION0 SPA-range.  REGION0 spans dimm0 and dimm1.  PMEM namespace
66  *    allocate from from the bottom of a region.  The unallocated
67  *    portion of REGION0 aliases with REGION2 and REGION3.  That
68  *    unallacted capacity is reclaimed as BLK namespaces ("blk2.0" and
69  *    "blk3.0") starting at the base of each DIMM to offset (a) in those
70  *    DIMMs.  "pm0.0", "blk2.0" and "blk3.0" are free-form readable
71  *    names that can be assigned to a namespace.
72  *
73  * *) In the last portion of dimm0 and dimm1 we have an interleaved
74  *    SPA range, REGION1, that spans those two dimms as well as dimm2
75  *    and dimm3.  Some of REGION1 allocated to a PMEM namespace named
76  *    "pm1.0" the rest is reclaimed in 4 BLK namespaces (for each
77  *    dimm in the interleave set), "blk2.1", "blk3.1", "blk4.0", and
78  *    "blk5.0".
79  *
80  * *) The portion of dimm2 and dimm3 that do not participate in the
81  *    REGION1 interleaved SPA range (i.e. the DPA address below offset
82  *    (b) are also included in the "blk4.0" and "blk5.0" namespaces.
83  *    Note, that BLK namespaces need not be contiguous in DPA-space, and
84  *    can consume aliased capacity from multiple interleave sets.
85  *
86  * BUS1: Legacy NVDIMM (single contiguous range)
87  *
88  *  region2
89  * +---------------------+
90  * |---------------------|
91  * ||       pm2.0       ||
92  * |---------------------|
93  * +---------------------+
94  *
95  * *) A NFIT-table may describe a simple system-physical-address range
96  *    with no BLK aliasing.  This type of region may optionally
97  *    reference an NVDIMM.
98  */
99 enum {
100         NUM_PM  = 3,
101         NUM_DCR = 5,
102         NUM_HINTS = 8,
103         NUM_BDW = NUM_DCR,
104         NUM_SPA = NUM_PM + NUM_DCR + NUM_BDW,
105         NUM_MEM = NUM_DCR + NUM_BDW + 2 /* spa0 iset */ + 4 /* spa1 iset */,
106         DIMM_SIZE = SZ_32M,
107         LABEL_SIZE = SZ_128K,
108         SPA_VCD_SIZE = SZ_4M,
109         SPA0_SIZE = DIMM_SIZE,
110         SPA1_SIZE = DIMM_SIZE*2,
111         SPA2_SIZE = DIMM_SIZE,
112         BDW_SIZE = 64 << 8,
113         DCR_SIZE = 12,
114         NUM_NFITS = 2, /* permit testing multiple NFITs per system */
115 };
116
117 struct nfit_test_dcr {
118         __le64 bdw_addr;
119         __le32 bdw_status;
120         __u8 aperature[BDW_SIZE];
121 };
122
123 #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm) \
124         (((node & 0xfff) << 16) | ((socket & 0xf) << 12) \
125          | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
126
127 static u32 handle[NUM_DCR] = {
128         [0] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
129         [1] = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
130         [2] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
131         [3] = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
132         [4] = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
133 };
134
135 static unsigned long dimm_fail_cmd_flags[NUM_DCR];
136
137 struct nfit_test {
138         struct acpi_nfit_desc acpi_desc;
139         struct platform_device pdev;
140         struct list_head resources;
141         void *nfit_buf;
142         dma_addr_t nfit_dma;
143         size_t nfit_size;
144         int num_dcr;
145         int num_pm;
146         void **dimm;
147         dma_addr_t *dimm_dma;
148         void **flush;
149         dma_addr_t *flush_dma;
150         void **label;
151         dma_addr_t *label_dma;
152         void **spa_set;
153         dma_addr_t *spa_set_dma;
154         struct nfit_test_dcr **dcr;
155         dma_addr_t *dcr_dma;
156         int (*alloc)(struct nfit_test *t);
157         void (*setup)(struct nfit_test *t);
158         int setup_hotplug;
159         union acpi_object **_fit;
160         dma_addr_t _fit_dma;
161         struct ars_state {
162                 struct nd_cmd_ars_status *ars_status;
163                 unsigned long deadline;
164                 spinlock_t lock;
165         } ars_state;
166         struct device *dimm_dev[NUM_DCR];
167 };
168
169 static struct nfit_test *to_nfit_test(struct device *dev)
170 {
171         struct platform_device *pdev = to_platform_device(dev);
172
173         return container_of(pdev, struct nfit_test, pdev);
174 }
175
176 static int nfit_test_cmd_get_config_size(struct nd_cmd_get_config_size *nd_cmd,
177                 unsigned int buf_len)
178 {
179         if (buf_len < sizeof(*nd_cmd))
180                 return -EINVAL;
181
182         nd_cmd->status = 0;
183         nd_cmd->config_size = LABEL_SIZE;
184         nd_cmd->max_xfer = SZ_4K;
185
186         return 0;
187 }
188
189 static int nfit_test_cmd_get_config_data(struct nd_cmd_get_config_data_hdr
190                 *nd_cmd, unsigned int buf_len, void *label)
191 {
192         unsigned int len, offset = nd_cmd->in_offset;
193         int rc;
194
195         if (buf_len < sizeof(*nd_cmd))
196                 return -EINVAL;
197         if (offset >= LABEL_SIZE)
198                 return -EINVAL;
199         if (nd_cmd->in_length + sizeof(*nd_cmd) > buf_len)
200                 return -EINVAL;
201
202         nd_cmd->status = 0;
203         len = min(nd_cmd->in_length, LABEL_SIZE - offset);
204         memcpy(nd_cmd->out_buf, label + offset, len);
205         rc = buf_len - sizeof(*nd_cmd) - len;
206
207         return rc;
208 }
209
210 static int nfit_test_cmd_set_config_data(struct nd_cmd_set_config_hdr *nd_cmd,
211                 unsigned int buf_len, void *label)
212 {
213         unsigned int len, offset = nd_cmd->in_offset;
214         u32 *status;
215         int rc;
216
217         if (buf_len < sizeof(*nd_cmd))
218                 return -EINVAL;
219         if (offset >= LABEL_SIZE)
220                 return -EINVAL;
221         if (nd_cmd->in_length + sizeof(*nd_cmd) + 4 > buf_len)
222                 return -EINVAL;
223
224         status = (void *)nd_cmd + nd_cmd->in_length + sizeof(*nd_cmd);
225         *status = 0;
226         len = min(nd_cmd->in_length, LABEL_SIZE - offset);
227         memcpy(label + offset, nd_cmd->in_buf, len);
228         rc = buf_len - sizeof(*nd_cmd) - (len + 4);
229
230         return rc;
231 }
232
233 #define NFIT_TEST_ARS_RECORDS 4
234 #define NFIT_TEST_CLEAR_ERR_UNIT 256
235
236 static int nfit_test_cmd_ars_cap(struct nd_cmd_ars_cap *nd_cmd,
237                 unsigned int buf_len)
238 {
239         if (buf_len < sizeof(*nd_cmd))
240                 return -EINVAL;
241
242         nd_cmd->max_ars_out = sizeof(struct nd_cmd_ars_status)
243                 + NFIT_TEST_ARS_RECORDS * sizeof(struct nd_ars_record);
244         nd_cmd->status = (ND_ARS_PERSISTENT | ND_ARS_VOLATILE) << 16;
245         nd_cmd->clear_err_unit = NFIT_TEST_CLEAR_ERR_UNIT;
246
247         return 0;
248 }
249
250 /*
251  * Initialize the ars_state to return an ars_result 1 second in the future with
252  * a 4K error range in the middle of the requested address range.
253  */
254 static void post_ars_status(struct ars_state *ars_state, u64 addr, u64 len)
255 {
256         struct nd_cmd_ars_status *ars_status;
257         struct nd_ars_record *ars_record;
258
259         ars_state->deadline = jiffies + 1*HZ;
260         ars_status = ars_state->ars_status;
261         ars_status->status = 0;
262         ars_status->out_length = sizeof(struct nd_cmd_ars_status)
263                 + sizeof(struct nd_ars_record);
264         ars_status->address = addr;
265         ars_status->length = len;
266         ars_status->type = ND_ARS_PERSISTENT;
267         ars_status->num_records = 1;
268         ars_record = &ars_status->records[0];
269         ars_record->handle = 0;
270         ars_record->err_address = addr + len / 2;
271         ars_record->length = SZ_4K;
272 }
273
274 static int nfit_test_cmd_ars_start(struct ars_state *ars_state,
275                 struct nd_cmd_ars_start *ars_start, unsigned int buf_len,
276                 int *cmd_rc)
277 {
278         if (buf_len < sizeof(*ars_start))
279                 return -EINVAL;
280
281         spin_lock(&ars_state->lock);
282         if (time_before(jiffies, ars_state->deadline)) {
283                 ars_start->status = NFIT_ARS_START_BUSY;
284                 *cmd_rc = -EBUSY;
285         } else {
286                 ars_start->status = 0;
287                 ars_start->scrub_time = 1;
288                 post_ars_status(ars_state, ars_start->address,
289                                 ars_start->length);
290                 *cmd_rc = 0;
291         }
292         spin_unlock(&ars_state->lock);
293
294         return 0;
295 }
296
297 static int nfit_test_cmd_ars_status(struct ars_state *ars_state,
298                 struct nd_cmd_ars_status *ars_status, unsigned int buf_len,
299                 int *cmd_rc)
300 {
301         if (buf_len < ars_state->ars_status->out_length)
302                 return -EINVAL;
303
304         spin_lock(&ars_state->lock);
305         if (time_before(jiffies, ars_state->deadline)) {
306                 memset(ars_status, 0, buf_len);
307                 ars_status->status = NFIT_ARS_STATUS_BUSY;
308                 ars_status->out_length = sizeof(*ars_status);
309                 *cmd_rc = -EBUSY;
310         } else {
311                 memcpy(ars_status, ars_state->ars_status,
312                                 ars_state->ars_status->out_length);
313                 *cmd_rc = 0;
314         }
315         spin_unlock(&ars_state->lock);
316         return 0;
317 }
318
319 static int nfit_test_cmd_clear_error(struct nd_cmd_clear_error *clear_err,
320                 unsigned int buf_len, int *cmd_rc)
321 {
322         const u64 mask = NFIT_TEST_CLEAR_ERR_UNIT - 1;
323         if (buf_len < sizeof(*clear_err))
324                 return -EINVAL;
325
326         if ((clear_err->address & mask) || (clear_err->length & mask))
327                 return -EINVAL;
328
329         /*
330          * Report 'all clear' success for all commands even though a new
331          * scrub will find errors again.  This is enough to have the
332          * error removed from the 'badblocks' tracking in the pmem
333          * driver.
334          */
335         clear_err->status = 0;
336         clear_err->cleared = clear_err->length;
337         *cmd_rc = 0;
338         return 0;
339 }
340
341 static int nfit_test_cmd_smart(struct nd_cmd_smart *smart, unsigned int buf_len)
342 {
343         static const struct nd_smart_payload smart_data = {
344                 .flags = ND_SMART_HEALTH_VALID | ND_SMART_TEMP_VALID
345                         | ND_SMART_SPARES_VALID | ND_SMART_ALARM_VALID
346                         | ND_SMART_USED_VALID | ND_SMART_SHUTDOWN_VALID,
347                 .health = ND_SMART_NON_CRITICAL_HEALTH,
348                 .temperature = 23 * 16,
349                 .spares = 75,
350                 .alarm_flags = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
351                 .life_used = 5,
352                 .shutdown_state = 0,
353                 .vendor_size = 0,
354         };
355
356         if (buf_len < sizeof(*smart))
357                 return -EINVAL;
358         memcpy(smart->data, &smart_data, sizeof(smart_data));
359         return 0;
360 }
361
362 static int nfit_test_cmd_smart_threshold(struct nd_cmd_smart_threshold *smart_t,
363                 unsigned int buf_len)
364 {
365         static const struct nd_smart_threshold_payload smart_t_data = {
366                 .alarm_control = ND_SMART_SPARE_TRIP | ND_SMART_TEMP_TRIP,
367                 .temperature = 40 * 16,
368                 .spares = 5,
369         };
370
371         if (buf_len < sizeof(*smart_t))
372                 return -EINVAL;
373         memcpy(smart_t->data, &smart_t_data, sizeof(smart_t_data));
374         return 0;
375 }
376
377 static int nfit_test_ctl(struct nvdimm_bus_descriptor *nd_desc,
378                 struct nvdimm *nvdimm, unsigned int cmd, void *buf,
379                 unsigned int buf_len, int *cmd_rc)
380 {
381         struct acpi_nfit_desc *acpi_desc = to_acpi_desc(nd_desc);
382         struct nfit_test *t = container_of(acpi_desc, typeof(*t), acpi_desc);
383         unsigned int func = cmd;
384         int i, rc = 0, __cmd_rc;
385
386         if (!cmd_rc)
387                 cmd_rc = &__cmd_rc;
388         *cmd_rc = 0;
389
390         if (nvdimm) {
391                 struct nfit_mem *nfit_mem = nvdimm_provider_data(nvdimm);
392                 unsigned long cmd_mask = nvdimm_cmd_mask(nvdimm);
393
394                 if (!nfit_mem)
395                         return -ENOTTY;
396
397                 if (cmd == ND_CMD_CALL) {
398                         struct nd_cmd_pkg *call_pkg = buf;
399
400                         buf_len = call_pkg->nd_size_in + call_pkg->nd_size_out;
401                         buf = (void *) call_pkg->nd_payload;
402                         func = call_pkg->nd_command;
403                         if (call_pkg->nd_family != nfit_mem->family)
404                                 return -ENOTTY;
405                 }
406
407                 if (!test_bit(cmd, &cmd_mask)
408                                 || !test_bit(func, &nfit_mem->dsm_mask))
409                         return -ENOTTY;
410
411                 /* lookup label space for the given dimm */
412                 for (i = 0; i < ARRAY_SIZE(handle); i++)
413                         if (__to_nfit_memdev(nfit_mem)->device_handle ==
414                                         handle[i])
415                                 break;
416                 if (i >= ARRAY_SIZE(handle))
417                         return -ENXIO;
418
419                 if ((1 << func) & dimm_fail_cmd_flags[i])
420                         return -EIO;
421
422                 switch (func) {
423                 case ND_CMD_GET_CONFIG_SIZE:
424                         rc = nfit_test_cmd_get_config_size(buf, buf_len);
425                         break;
426                 case ND_CMD_GET_CONFIG_DATA:
427                         rc = nfit_test_cmd_get_config_data(buf, buf_len,
428                                 t->label[i]);
429                         break;
430                 case ND_CMD_SET_CONFIG_DATA:
431                         rc = nfit_test_cmd_set_config_data(buf, buf_len,
432                                 t->label[i]);
433                         break;
434                 case ND_CMD_SMART:
435                         rc = nfit_test_cmd_smart(buf, buf_len);
436                         break;
437                 case ND_CMD_SMART_THRESHOLD:
438                         rc = nfit_test_cmd_smart_threshold(buf, buf_len);
439                         device_lock(&t->pdev.dev);
440                         __acpi_nvdimm_notify(t->dimm_dev[i], 0x81);
441                         device_unlock(&t->pdev.dev);
442                         break;
443                 default:
444                         return -ENOTTY;
445                 }
446         } else {
447                 struct ars_state *ars_state = &t->ars_state;
448
449                 if (!nd_desc || !test_bit(cmd, &nd_desc->cmd_mask))
450                         return -ENOTTY;
451
452                 switch (func) {
453                 case ND_CMD_ARS_CAP:
454                         rc = nfit_test_cmd_ars_cap(buf, buf_len);
455                         break;
456                 case ND_CMD_ARS_START:
457                         rc = nfit_test_cmd_ars_start(ars_state, buf, buf_len,
458                                         cmd_rc);
459                         break;
460                 case ND_CMD_ARS_STATUS:
461                         rc = nfit_test_cmd_ars_status(ars_state, buf, buf_len,
462                                         cmd_rc);
463                         break;
464                 case ND_CMD_CLEAR_ERROR:
465                         rc = nfit_test_cmd_clear_error(buf, buf_len, cmd_rc);
466                         break;
467                 default:
468                         return -ENOTTY;
469                 }
470         }
471
472         return rc;
473 }
474
475 static DEFINE_SPINLOCK(nfit_test_lock);
476 static struct nfit_test *instances[NUM_NFITS];
477
478 static void release_nfit_res(void *data)
479 {
480         struct nfit_test_resource *nfit_res = data;
481
482         spin_lock(&nfit_test_lock);
483         list_del(&nfit_res->list);
484         spin_unlock(&nfit_test_lock);
485
486         vfree(nfit_res->buf);
487         kfree(nfit_res);
488 }
489
490 static void *__test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma,
491                 void *buf)
492 {
493         struct device *dev = &t->pdev.dev;
494         struct nfit_test_resource *nfit_res = kzalloc(sizeof(*nfit_res),
495                         GFP_KERNEL);
496         int rc;
497
498         if (!buf || !nfit_res)
499                 goto err;
500         rc = devm_add_action(dev, release_nfit_res, nfit_res);
501         if (rc)
502                 goto err;
503         INIT_LIST_HEAD(&nfit_res->list);
504         memset(buf, 0, size);
505         nfit_res->dev = dev;
506         nfit_res->buf = buf;
507         nfit_res->res.start = *dma;
508         nfit_res->res.end = *dma + size - 1;
509         nfit_res->res.name = "NFIT";
510         spin_lock_init(&nfit_res->lock);
511         INIT_LIST_HEAD(&nfit_res->requests);
512         spin_lock(&nfit_test_lock);
513         list_add(&nfit_res->list, &t->resources);
514         spin_unlock(&nfit_test_lock);
515
516         return nfit_res->buf;
517  err:
518         if (buf)
519                 vfree(buf);
520         kfree(nfit_res);
521         return NULL;
522 }
523
524 static void *test_alloc(struct nfit_test *t, size_t size, dma_addr_t *dma)
525 {
526         void *buf = vmalloc(size);
527
528         *dma = (unsigned long) buf;
529         return __test_alloc(t, size, dma, buf);
530 }
531
532 static struct nfit_test_resource *nfit_test_lookup(resource_size_t addr)
533 {
534         int i;
535
536         for (i = 0; i < ARRAY_SIZE(instances); i++) {
537                 struct nfit_test_resource *n, *nfit_res = NULL;
538                 struct nfit_test *t = instances[i];
539
540                 if (!t)
541                         continue;
542                 spin_lock(&nfit_test_lock);
543                 list_for_each_entry(n, &t->resources, list) {
544                         if (addr >= n->res.start && (addr < n->res.start
545                                                 + resource_size(&n->res))) {
546                                 nfit_res = n;
547                                 break;
548                         } else if (addr >= (unsigned long) n->buf
549                                         && (addr < (unsigned long) n->buf
550                                                 + resource_size(&n->res))) {
551                                 nfit_res = n;
552                                 break;
553                         }
554                 }
555                 spin_unlock(&nfit_test_lock);
556                 if (nfit_res)
557                         return nfit_res;
558         }
559
560         return NULL;
561 }
562
563 static int ars_state_init(struct device *dev, struct ars_state *ars_state)
564 {
565         ars_state->ars_status = devm_kzalloc(dev,
566                         sizeof(struct nd_cmd_ars_status)
567                         + sizeof(struct nd_ars_record) * NFIT_TEST_ARS_RECORDS,
568                         GFP_KERNEL);
569         if (!ars_state->ars_status)
570                 return -ENOMEM;
571         spin_lock_init(&ars_state->lock);
572         return 0;
573 }
574
575 static void put_dimms(void *data)
576 {
577         struct device **dimm_dev = data;
578         int i;
579
580         for (i = 0; i < NUM_DCR; i++)
581                 if (dimm_dev[i])
582                         device_unregister(dimm_dev[i]);
583 }
584
585 static struct class *nfit_test_dimm;
586
587 static int dimm_name_to_id(struct device *dev)
588 {
589         int dimm;
590
591         if (sscanf(dev_name(dev), "test_dimm%d", &dimm) != 1
592                         || dimm >= NUM_DCR || dimm < 0)
593                 return -ENXIO;
594         return dimm;
595 }
596
597
598 static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
599                 char *buf)
600 {
601         int dimm = dimm_name_to_id(dev);
602
603         if (dimm < 0)
604                 return dimm;
605
606         return sprintf(buf, "%#x", handle[dimm]);
607 }
608 DEVICE_ATTR_RO(handle);
609
610 static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
611                 char *buf)
612 {
613         int dimm = dimm_name_to_id(dev);
614
615         if (dimm < 0)
616                 return dimm;
617
618         return sprintf(buf, "%#lx\n", dimm_fail_cmd_flags[dimm]);
619 }
620
621 static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
622                 const char *buf, size_t size)
623 {
624         int dimm = dimm_name_to_id(dev);
625         unsigned long val;
626         ssize_t rc;
627
628         if (dimm < 0)
629                 return dimm;
630
631         rc = kstrtol(buf, 0, &val);
632         if (rc)
633                 return rc;
634
635         dimm_fail_cmd_flags[dimm] = val;
636         return size;
637 }
638 static DEVICE_ATTR_RW(fail_cmd);
639
640 static struct attribute *nfit_test_dimm_attributes[] = {
641         &dev_attr_fail_cmd.attr,
642         &dev_attr_handle.attr,
643         NULL,
644 };
645
646 static struct attribute_group nfit_test_dimm_attribute_group = {
647         .attrs = nfit_test_dimm_attributes,
648 };
649
650 static const struct attribute_group *nfit_test_dimm_attribute_groups[] = {
651         &nfit_test_dimm_attribute_group,
652         NULL,
653 };
654
655 static int nfit_test0_alloc(struct nfit_test *t)
656 {
657         size_t nfit_size = sizeof(struct acpi_nfit_system_address) * NUM_SPA
658                         + sizeof(struct acpi_nfit_memory_map) * NUM_MEM
659                         + sizeof(struct acpi_nfit_control_region) * NUM_DCR
660                         + offsetof(struct acpi_nfit_control_region,
661                                         window_size) * NUM_DCR
662                         + sizeof(struct acpi_nfit_data_region) * NUM_BDW
663                         + (sizeof(struct acpi_nfit_flush_address)
664                                         + sizeof(u64) * NUM_HINTS) * NUM_DCR;
665         int i;
666
667         t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
668         if (!t->nfit_buf)
669                 return -ENOMEM;
670         t->nfit_size = nfit_size;
671
672         t->spa_set[0] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[0]);
673         if (!t->spa_set[0])
674                 return -ENOMEM;
675
676         t->spa_set[1] = test_alloc(t, SPA1_SIZE, &t->spa_set_dma[1]);
677         if (!t->spa_set[1])
678                 return -ENOMEM;
679
680         t->spa_set[2] = test_alloc(t, SPA0_SIZE, &t->spa_set_dma[2]);
681         if (!t->spa_set[2])
682                 return -ENOMEM;
683
684         for (i = 0; i < NUM_DCR; i++) {
685                 t->dimm[i] = test_alloc(t, DIMM_SIZE, &t->dimm_dma[i]);
686                 if (!t->dimm[i])
687                         return -ENOMEM;
688
689                 t->label[i] = test_alloc(t, LABEL_SIZE, &t->label_dma[i]);
690                 if (!t->label[i])
691                         return -ENOMEM;
692                 sprintf(t->label[i], "label%d", i);
693
694                 t->flush[i] = test_alloc(t, sizeof(u64) * NUM_HINTS,
695                                 &t->flush_dma[i]);
696                 if (!t->flush[i])
697                         return -ENOMEM;
698         }
699
700         for (i = 0; i < NUM_DCR; i++) {
701                 t->dcr[i] = test_alloc(t, LABEL_SIZE, &t->dcr_dma[i]);
702                 if (!t->dcr[i])
703                         return -ENOMEM;
704         }
705
706         t->_fit = test_alloc(t, sizeof(union acpi_object **), &t->_fit_dma);
707         if (!t->_fit)
708                 return -ENOMEM;
709
710         if (devm_add_action_or_reset(&t->pdev.dev, put_dimms, t->dimm_dev))
711                 return -ENOMEM;
712         for (i = 0; i < NUM_DCR; i++) {
713                 t->dimm_dev[i] = device_create_with_groups(nfit_test_dimm,
714                                 &t->pdev.dev, 0, NULL,
715                                 nfit_test_dimm_attribute_groups,
716                                 "test_dimm%d", i);
717                 if (!t->dimm_dev[i])
718                         return -ENOMEM;
719         }
720
721         return ars_state_init(&t->pdev.dev, &t->ars_state);
722 }
723
724 static int nfit_test1_alloc(struct nfit_test *t)
725 {
726         size_t nfit_size = sizeof(struct acpi_nfit_system_address) * 2
727                 + sizeof(struct acpi_nfit_memory_map)
728                 + offsetof(struct acpi_nfit_control_region, window_size);
729
730         t->nfit_buf = test_alloc(t, nfit_size, &t->nfit_dma);
731         if (!t->nfit_buf)
732                 return -ENOMEM;
733         t->nfit_size = nfit_size;
734
735         t->spa_set[0] = test_alloc(t, SPA2_SIZE, &t->spa_set_dma[0]);
736         if (!t->spa_set[0])
737                 return -ENOMEM;
738
739         t->spa_set[1] = test_alloc(t, SPA_VCD_SIZE, &t->spa_set_dma[1]);
740         if (!t->spa_set[1])
741                 return -ENOMEM;
742
743         return ars_state_init(&t->pdev.dev, &t->ars_state);
744 }
745
746 static void dcr_common_init(struct acpi_nfit_control_region *dcr)
747 {
748         dcr->vendor_id = 0xabcd;
749         dcr->device_id = 0;
750         dcr->revision_id = 1;
751         dcr->valid_fields = 1;
752         dcr->manufacturing_location = 0xa;
753         dcr->manufacturing_date = cpu_to_be16(2016);
754 }
755
756 static void nfit_test0_setup(struct nfit_test *t)
757 {
758         const int flush_hint_size = sizeof(struct acpi_nfit_flush_address)
759                 + (sizeof(u64) * NUM_HINTS);
760         struct acpi_nfit_desc *acpi_desc;
761         struct acpi_nfit_memory_map *memdev;
762         void *nfit_buf = t->nfit_buf;
763         struct acpi_nfit_system_address *spa;
764         struct acpi_nfit_control_region *dcr;
765         struct acpi_nfit_data_region *bdw;
766         struct acpi_nfit_flush_address *flush;
767         unsigned int offset, i;
768
769         /*
770          * spa0 (interleave first half of dimm0 and dimm1, note storage
771          * does not actually alias the related block-data-window
772          * regions)
773          */
774         spa = nfit_buf;
775         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
776         spa->header.length = sizeof(*spa);
777         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
778         spa->range_index = 0+1;
779         spa->address = t->spa_set_dma[0];
780         spa->length = SPA0_SIZE;
781
782         /*
783          * spa1 (interleave last half of the 4 DIMMS, note storage
784          * does not actually alias the related block-data-window
785          * regions)
786          */
787         spa = nfit_buf + sizeof(*spa);
788         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
789         spa->header.length = sizeof(*spa);
790         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
791         spa->range_index = 1+1;
792         spa->address = t->spa_set_dma[1];
793         spa->length = SPA1_SIZE;
794
795         /* spa2 (dcr0) dimm0 */
796         spa = nfit_buf + sizeof(*spa) * 2;
797         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
798         spa->header.length = sizeof(*spa);
799         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
800         spa->range_index = 2+1;
801         spa->address = t->dcr_dma[0];
802         spa->length = DCR_SIZE;
803
804         /* spa3 (dcr1) dimm1 */
805         spa = nfit_buf + sizeof(*spa) * 3;
806         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
807         spa->header.length = sizeof(*spa);
808         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
809         spa->range_index = 3+1;
810         spa->address = t->dcr_dma[1];
811         spa->length = DCR_SIZE;
812
813         /* spa4 (dcr2) dimm2 */
814         spa = nfit_buf + sizeof(*spa) * 4;
815         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
816         spa->header.length = sizeof(*spa);
817         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
818         spa->range_index = 4+1;
819         spa->address = t->dcr_dma[2];
820         spa->length = DCR_SIZE;
821
822         /* spa5 (dcr3) dimm3 */
823         spa = nfit_buf + sizeof(*spa) * 5;
824         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
825         spa->header.length = sizeof(*spa);
826         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
827         spa->range_index = 5+1;
828         spa->address = t->dcr_dma[3];
829         spa->length = DCR_SIZE;
830
831         /* spa6 (bdw for dcr0) dimm0 */
832         spa = nfit_buf + sizeof(*spa) * 6;
833         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
834         spa->header.length = sizeof(*spa);
835         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
836         spa->range_index = 6+1;
837         spa->address = t->dimm_dma[0];
838         spa->length = DIMM_SIZE;
839
840         /* spa7 (bdw for dcr1) dimm1 */
841         spa = nfit_buf + sizeof(*spa) * 7;
842         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
843         spa->header.length = sizeof(*spa);
844         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
845         spa->range_index = 7+1;
846         spa->address = t->dimm_dma[1];
847         spa->length = DIMM_SIZE;
848
849         /* spa8 (bdw for dcr2) dimm2 */
850         spa = nfit_buf + sizeof(*spa) * 8;
851         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
852         spa->header.length = sizeof(*spa);
853         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
854         spa->range_index = 8+1;
855         spa->address = t->dimm_dma[2];
856         spa->length = DIMM_SIZE;
857
858         /* spa9 (bdw for dcr3) dimm3 */
859         spa = nfit_buf + sizeof(*spa) * 9;
860         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
861         spa->header.length = sizeof(*spa);
862         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
863         spa->range_index = 9+1;
864         spa->address = t->dimm_dma[3];
865         spa->length = DIMM_SIZE;
866
867         offset = sizeof(*spa) * 10;
868         /* mem-region0 (spa0, dimm0) */
869         memdev = nfit_buf + offset;
870         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
871         memdev->header.length = sizeof(*memdev);
872         memdev->device_handle = handle[0];
873         memdev->physical_id = 0;
874         memdev->region_id = 0;
875         memdev->range_index = 0+1;
876         memdev->region_index = 4+1;
877         memdev->region_size = SPA0_SIZE/2;
878         memdev->region_offset = t->spa_set_dma[0];
879         memdev->address = 0;
880         memdev->interleave_index = 0;
881         memdev->interleave_ways = 2;
882
883         /* mem-region1 (spa0, dimm1) */
884         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map);
885         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
886         memdev->header.length = sizeof(*memdev);
887         memdev->device_handle = handle[1];
888         memdev->physical_id = 1;
889         memdev->region_id = 0;
890         memdev->range_index = 0+1;
891         memdev->region_index = 5+1;
892         memdev->region_size = SPA0_SIZE/2;
893         memdev->region_offset = t->spa_set_dma[0] + SPA0_SIZE/2;
894         memdev->address = 0;
895         memdev->interleave_index = 0;
896         memdev->interleave_ways = 2;
897
898         /* mem-region2 (spa1, dimm0) */
899         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 2;
900         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
901         memdev->header.length = sizeof(*memdev);
902         memdev->device_handle = handle[0];
903         memdev->physical_id = 0;
904         memdev->region_id = 1;
905         memdev->range_index = 1+1;
906         memdev->region_index = 4+1;
907         memdev->region_size = SPA1_SIZE/4;
908         memdev->region_offset = t->spa_set_dma[1];
909         memdev->address = SPA0_SIZE/2;
910         memdev->interleave_index = 0;
911         memdev->interleave_ways = 4;
912
913         /* mem-region3 (spa1, dimm1) */
914         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 3;
915         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
916         memdev->header.length = sizeof(*memdev);
917         memdev->device_handle = handle[1];
918         memdev->physical_id = 1;
919         memdev->region_id = 1;
920         memdev->range_index = 1+1;
921         memdev->region_index = 5+1;
922         memdev->region_size = SPA1_SIZE/4;
923         memdev->region_offset = t->spa_set_dma[1] + SPA1_SIZE/4;
924         memdev->address = SPA0_SIZE/2;
925         memdev->interleave_index = 0;
926         memdev->interleave_ways = 4;
927
928         /* mem-region4 (spa1, dimm2) */
929         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 4;
930         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
931         memdev->header.length = sizeof(*memdev);
932         memdev->device_handle = handle[2];
933         memdev->physical_id = 2;
934         memdev->region_id = 0;
935         memdev->range_index = 1+1;
936         memdev->region_index = 6+1;
937         memdev->region_size = SPA1_SIZE/4;
938         memdev->region_offset = t->spa_set_dma[1] + 2*SPA1_SIZE/4;
939         memdev->address = SPA0_SIZE/2;
940         memdev->interleave_index = 0;
941         memdev->interleave_ways = 4;
942
943         /* mem-region5 (spa1, dimm3) */
944         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 5;
945         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
946         memdev->header.length = sizeof(*memdev);
947         memdev->device_handle = handle[3];
948         memdev->physical_id = 3;
949         memdev->region_id = 0;
950         memdev->range_index = 1+1;
951         memdev->region_index = 7+1;
952         memdev->region_size = SPA1_SIZE/4;
953         memdev->region_offset = t->spa_set_dma[1] + 3*SPA1_SIZE/4;
954         memdev->address = SPA0_SIZE/2;
955         memdev->interleave_index = 0;
956         memdev->interleave_ways = 4;
957
958         /* mem-region6 (spa/dcr0, dimm0) */
959         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 6;
960         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
961         memdev->header.length = sizeof(*memdev);
962         memdev->device_handle = handle[0];
963         memdev->physical_id = 0;
964         memdev->region_id = 0;
965         memdev->range_index = 2+1;
966         memdev->region_index = 0+1;
967         memdev->region_size = 0;
968         memdev->region_offset = 0;
969         memdev->address = 0;
970         memdev->interleave_index = 0;
971         memdev->interleave_ways = 1;
972
973         /* mem-region7 (spa/dcr1, dimm1) */
974         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 7;
975         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
976         memdev->header.length = sizeof(*memdev);
977         memdev->device_handle = handle[1];
978         memdev->physical_id = 1;
979         memdev->region_id = 0;
980         memdev->range_index = 3+1;
981         memdev->region_index = 1+1;
982         memdev->region_size = 0;
983         memdev->region_offset = 0;
984         memdev->address = 0;
985         memdev->interleave_index = 0;
986         memdev->interleave_ways = 1;
987
988         /* mem-region8 (spa/dcr2, dimm2) */
989         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 8;
990         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
991         memdev->header.length = sizeof(*memdev);
992         memdev->device_handle = handle[2];
993         memdev->physical_id = 2;
994         memdev->region_id = 0;
995         memdev->range_index = 4+1;
996         memdev->region_index = 2+1;
997         memdev->region_size = 0;
998         memdev->region_offset = 0;
999         memdev->address = 0;
1000         memdev->interleave_index = 0;
1001         memdev->interleave_ways = 1;
1002
1003         /* mem-region9 (spa/dcr3, dimm3) */
1004         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 9;
1005         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1006         memdev->header.length = sizeof(*memdev);
1007         memdev->device_handle = handle[3];
1008         memdev->physical_id = 3;
1009         memdev->region_id = 0;
1010         memdev->range_index = 5+1;
1011         memdev->region_index = 3+1;
1012         memdev->region_size = 0;
1013         memdev->region_offset = 0;
1014         memdev->address = 0;
1015         memdev->interleave_index = 0;
1016         memdev->interleave_ways = 1;
1017
1018         /* mem-region10 (spa/bdw0, dimm0) */
1019         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 10;
1020         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1021         memdev->header.length = sizeof(*memdev);
1022         memdev->device_handle = handle[0];
1023         memdev->physical_id = 0;
1024         memdev->region_id = 0;
1025         memdev->range_index = 6+1;
1026         memdev->region_index = 0+1;
1027         memdev->region_size = 0;
1028         memdev->region_offset = 0;
1029         memdev->address = 0;
1030         memdev->interleave_index = 0;
1031         memdev->interleave_ways = 1;
1032
1033         /* mem-region11 (spa/bdw1, dimm1) */
1034         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 11;
1035         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1036         memdev->header.length = sizeof(*memdev);
1037         memdev->device_handle = handle[1];
1038         memdev->physical_id = 1;
1039         memdev->region_id = 0;
1040         memdev->range_index = 7+1;
1041         memdev->region_index = 1+1;
1042         memdev->region_size = 0;
1043         memdev->region_offset = 0;
1044         memdev->address = 0;
1045         memdev->interleave_index = 0;
1046         memdev->interleave_ways = 1;
1047
1048         /* mem-region12 (spa/bdw2, dimm2) */
1049         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 12;
1050         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1051         memdev->header.length = sizeof(*memdev);
1052         memdev->device_handle = handle[2];
1053         memdev->physical_id = 2;
1054         memdev->region_id = 0;
1055         memdev->range_index = 8+1;
1056         memdev->region_index = 2+1;
1057         memdev->region_size = 0;
1058         memdev->region_offset = 0;
1059         memdev->address = 0;
1060         memdev->interleave_index = 0;
1061         memdev->interleave_ways = 1;
1062
1063         /* mem-region13 (spa/dcr3, dimm3) */
1064         memdev = nfit_buf + offset + sizeof(struct acpi_nfit_memory_map) * 13;
1065         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1066         memdev->header.length = sizeof(*memdev);
1067         memdev->device_handle = handle[3];
1068         memdev->physical_id = 3;
1069         memdev->region_id = 0;
1070         memdev->range_index = 9+1;
1071         memdev->region_index = 3+1;
1072         memdev->region_size = 0;
1073         memdev->region_offset = 0;
1074         memdev->address = 0;
1075         memdev->interleave_index = 0;
1076         memdev->interleave_ways = 1;
1077
1078         offset = offset + sizeof(struct acpi_nfit_memory_map) * 14;
1079         /* dcr-descriptor0: blk */
1080         dcr = nfit_buf + offset;
1081         dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1082         dcr->header.length = sizeof(struct acpi_nfit_control_region);
1083         dcr->region_index = 0+1;
1084         dcr_common_init(dcr);
1085         dcr->serial_number = ~handle[0];
1086         dcr->code = NFIT_FIC_BLK;
1087         dcr->windows = 1;
1088         dcr->window_size = DCR_SIZE;
1089         dcr->command_offset = 0;
1090         dcr->command_size = 8;
1091         dcr->status_offset = 8;
1092         dcr->status_size = 4;
1093
1094         /* dcr-descriptor1: blk */
1095         dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region);
1096         dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1097         dcr->header.length = sizeof(struct acpi_nfit_control_region);
1098         dcr->region_index = 1+1;
1099         dcr_common_init(dcr);
1100         dcr->serial_number = ~handle[1];
1101         dcr->code = NFIT_FIC_BLK;
1102         dcr->windows = 1;
1103         dcr->window_size = DCR_SIZE;
1104         dcr->command_offset = 0;
1105         dcr->command_size = 8;
1106         dcr->status_offset = 8;
1107         dcr->status_size = 4;
1108
1109         /* dcr-descriptor2: blk */
1110         dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 2;
1111         dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1112         dcr->header.length = sizeof(struct acpi_nfit_control_region);
1113         dcr->region_index = 2+1;
1114         dcr_common_init(dcr);
1115         dcr->serial_number = ~handle[2];
1116         dcr->code = NFIT_FIC_BLK;
1117         dcr->windows = 1;
1118         dcr->window_size = DCR_SIZE;
1119         dcr->command_offset = 0;
1120         dcr->command_size = 8;
1121         dcr->status_offset = 8;
1122         dcr->status_size = 4;
1123
1124         /* dcr-descriptor3: blk */
1125         dcr = nfit_buf + offset + sizeof(struct acpi_nfit_control_region) * 3;
1126         dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1127         dcr->header.length = sizeof(struct acpi_nfit_control_region);
1128         dcr->region_index = 3+1;
1129         dcr_common_init(dcr);
1130         dcr->serial_number = ~handle[3];
1131         dcr->code = NFIT_FIC_BLK;
1132         dcr->windows = 1;
1133         dcr->window_size = DCR_SIZE;
1134         dcr->command_offset = 0;
1135         dcr->command_size = 8;
1136         dcr->status_offset = 8;
1137         dcr->status_size = 4;
1138
1139         offset = offset + sizeof(struct acpi_nfit_control_region) * 4;
1140         /* dcr-descriptor0: pmem */
1141         dcr = nfit_buf + offset;
1142         dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1143         dcr->header.length = offsetof(struct acpi_nfit_control_region,
1144                         window_size);
1145         dcr->region_index = 4+1;
1146         dcr_common_init(dcr);
1147         dcr->serial_number = ~handle[0];
1148         dcr->code = NFIT_FIC_BYTEN;
1149         dcr->windows = 0;
1150
1151         /* dcr-descriptor1: pmem */
1152         dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
1153                         window_size);
1154         dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1155         dcr->header.length = offsetof(struct acpi_nfit_control_region,
1156                         window_size);
1157         dcr->region_index = 5+1;
1158         dcr_common_init(dcr);
1159         dcr->serial_number = ~handle[1];
1160         dcr->code = NFIT_FIC_BYTEN;
1161         dcr->windows = 0;
1162
1163         /* dcr-descriptor2: pmem */
1164         dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
1165                         window_size) * 2;
1166         dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1167         dcr->header.length = offsetof(struct acpi_nfit_control_region,
1168                         window_size);
1169         dcr->region_index = 6+1;
1170         dcr_common_init(dcr);
1171         dcr->serial_number = ~handle[2];
1172         dcr->code = NFIT_FIC_BYTEN;
1173         dcr->windows = 0;
1174
1175         /* dcr-descriptor3: pmem */
1176         dcr = nfit_buf + offset + offsetof(struct acpi_nfit_control_region,
1177                         window_size) * 3;
1178         dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1179         dcr->header.length = offsetof(struct acpi_nfit_control_region,
1180                         window_size);
1181         dcr->region_index = 7+1;
1182         dcr_common_init(dcr);
1183         dcr->serial_number = ~handle[3];
1184         dcr->code = NFIT_FIC_BYTEN;
1185         dcr->windows = 0;
1186
1187         offset = offset + offsetof(struct acpi_nfit_control_region,
1188                         window_size) * 4;
1189         /* bdw0 (spa/dcr0, dimm0) */
1190         bdw = nfit_buf + offset;
1191         bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1192         bdw->header.length = sizeof(struct acpi_nfit_data_region);
1193         bdw->region_index = 0+1;
1194         bdw->windows = 1;
1195         bdw->offset = 0;
1196         bdw->size = BDW_SIZE;
1197         bdw->capacity = DIMM_SIZE;
1198         bdw->start_address = 0;
1199
1200         /* bdw1 (spa/dcr1, dimm1) */
1201         bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region);
1202         bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1203         bdw->header.length = sizeof(struct acpi_nfit_data_region);
1204         bdw->region_index = 1+1;
1205         bdw->windows = 1;
1206         bdw->offset = 0;
1207         bdw->size = BDW_SIZE;
1208         bdw->capacity = DIMM_SIZE;
1209         bdw->start_address = 0;
1210
1211         /* bdw2 (spa/dcr2, dimm2) */
1212         bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 2;
1213         bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1214         bdw->header.length = sizeof(struct acpi_nfit_data_region);
1215         bdw->region_index = 2+1;
1216         bdw->windows = 1;
1217         bdw->offset = 0;
1218         bdw->size = BDW_SIZE;
1219         bdw->capacity = DIMM_SIZE;
1220         bdw->start_address = 0;
1221
1222         /* bdw3 (spa/dcr3, dimm3) */
1223         bdw = nfit_buf + offset + sizeof(struct acpi_nfit_data_region) * 3;
1224         bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1225         bdw->header.length = sizeof(struct acpi_nfit_data_region);
1226         bdw->region_index = 3+1;
1227         bdw->windows = 1;
1228         bdw->offset = 0;
1229         bdw->size = BDW_SIZE;
1230         bdw->capacity = DIMM_SIZE;
1231         bdw->start_address = 0;
1232
1233         offset = offset + sizeof(struct acpi_nfit_data_region) * 4;
1234         /* flush0 (dimm0) */
1235         flush = nfit_buf + offset;
1236         flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1237         flush->header.length = flush_hint_size;
1238         flush->device_handle = handle[0];
1239         flush->hint_count = NUM_HINTS;
1240         for (i = 0; i < NUM_HINTS; i++)
1241                 flush->hint_address[i] = t->flush_dma[0] + i * sizeof(u64);
1242
1243         /* flush1 (dimm1) */
1244         flush = nfit_buf + offset + flush_hint_size * 1;
1245         flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1246         flush->header.length = flush_hint_size;
1247         flush->device_handle = handle[1];
1248         flush->hint_count = NUM_HINTS;
1249         for (i = 0; i < NUM_HINTS; i++)
1250                 flush->hint_address[i] = t->flush_dma[1] + i * sizeof(u64);
1251
1252         /* flush2 (dimm2) */
1253         flush = nfit_buf + offset + flush_hint_size  * 2;
1254         flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1255         flush->header.length = flush_hint_size;
1256         flush->device_handle = handle[2];
1257         flush->hint_count = NUM_HINTS;
1258         for (i = 0; i < NUM_HINTS; i++)
1259                 flush->hint_address[i] = t->flush_dma[2] + i * sizeof(u64);
1260
1261         /* flush3 (dimm3) */
1262         flush = nfit_buf + offset + flush_hint_size * 3;
1263         flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1264         flush->header.length = flush_hint_size;
1265         flush->device_handle = handle[3];
1266         flush->hint_count = NUM_HINTS;
1267         for (i = 0; i < NUM_HINTS; i++)
1268                 flush->hint_address[i] = t->flush_dma[3] + i * sizeof(u64);
1269
1270         if (t->setup_hotplug) {
1271                 offset = offset + flush_hint_size * 4;
1272                 /* dcr-descriptor4: blk */
1273                 dcr = nfit_buf + offset;
1274                 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1275                 dcr->header.length = sizeof(struct acpi_nfit_control_region);
1276                 dcr->region_index = 8+1;
1277                 dcr_common_init(dcr);
1278                 dcr->serial_number = ~handle[4];
1279                 dcr->code = NFIT_FIC_BLK;
1280                 dcr->windows = 1;
1281                 dcr->window_size = DCR_SIZE;
1282                 dcr->command_offset = 0;
1283                 dcr->command_size = 8;
1284                 dcr->status_offset = 8;
1285                 dcr->status_size = 4;
1286
1287                 offset = offset + sizeof(struct acpi_nfit_control_region);
1288                 /* dcr-descriptor4: pmem */
1289                 dcr = nfit_buf + offset;
1290                 dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1291                 dcr->header.length = offsetof(struct acpi_nfit_control_region,
1292                                 window_size);
1293                 dcr->region_index = 9+1;
1294                 dcr_common_init(dcr);
1295                 dcr->serial_number = ~handle[4];
1296                 dcr->code = NFIT_FIC_BYTEN;
1297                 dcr->windows = 0;
1298
1299                 offset = offset + offsetof(struct acpi_nfit_control_region,
1300                                 window_size);
1301                 /* bdw4 (spa/dcr4, dimm4) */
1302                 bdw = nfit_buf + offset;
1303                 bdw->header.type = ACPI_NFIT_TYPE_DATA_REGION;
1304                 bdw->header.length = sizeof(struct acpi_nfit_data_region);
1305                 bdw->region_index = 8+1;
1306                 bdw->windows = 1;
1307                 bdw->offset = 0;
1308                 bdw->size = BDW_SIZE;
1309                 bdw->capacity = DIMM_SIZE;
1310                 bdw->start_address = 0;
1311
1312                 offset = offset + sizeof(struct acpi_nfit_data_region);
1313                 /* spa10 (dcr4) dimm4 */
1314                 spa = nfit_buf + offset;
1315                 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1316                 spa->header.length = sizeof(*spa);
1317                 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_DCR), 16);
1318                 spa->range_index = 10+1;
1319                 spa->address = t->dcr_dma[4];
1320                 spa->length = DCR_SIZE;
1321
1322                 /*
1323                  * spa11 (single-dimm interleave for hotplug, note storage
1324                  * does not actually alias the related block-data-window
1325                  * regions)
1326                  */
1327                 spa = nfit_buf + offset + sizeof(*spa);
1328                 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1329                 spa->header.length = sizeof(*spa);
1330                 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
1331                 spa->range_index = 11+1;
1332                 spa->address = t->spa_set_dma[2];
1333                 spa->length = SPA0_SIZE;
1334
1335                 /* spa12 (bdw for dcr4) dimm4 */
1336                 spa = nfit_buf + offset + sizeof(*spa) * 2;
1337                 spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1338                 spa->header.length = sizeof(*spa);
1339                 memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_BDW), 16);
1340                 spa->range_index = 12+1;
1341                 spa->address = t->dimm_dma[4];
1342                 spa->length = DIMM_SIZE;
1343
1344                 offset = offset + sizeof(*spa) * 3;
1345                 /* mem-region14 (spa/dcr4, dimm4) */
1346                 memdev = nfit_buf + offset;
1347                 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1348                 memdev->header.length = sizeof(*memdev);
1349                 memdev->device_handle = handle[4];
1350                 memdev->physical_id = 4;
1351                 memdev->region_id = 0;
1352                 memdev->range_index = 10+1;
1353                 memdev->region_index = 8+1;
1354                 memdev->region_size = 0;
1355                 memdev->region_offset = 0;
1356                 memdev->address = 0;
1357                 memdev->interleave_index = 0;
1358                 memdev->interleave_ways = 1;
1359
1360                 /* mem-region15 (spa0, dimm4) */
1361                 memdev = nfit_buf + offset +
1362                                 sizeof(struct acpi_nfit_memory_map);
1363                 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1364                 memdev->header.length = sizeof(*memdev);
1365                 memdev->device_handle = handle[4];
1366                 memdev->physical_id = 4;
1367                 memdev->region_id = 0;
1368                 memdev->range_index = 11+1;
1369                 memdev->region_index = 9+1;
1370                 memdev->region_size = SPA0_SIZE;
1371                 memdev->region_offset = t->spa_set_dma[2];
1372                 memdev->address = 0;
1373                 memdev->interleave_index = 0;
1374                 memdev->interleave_ways = 1;
1375
1376                 /* mem-region16 (spa/bdw4, dimm4) */
1377                 memdev = nfit_buf + offset +
1378                                 sizeof(struct acpi_nfit_memory_map) * 2;
1379                 memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1380                 memdev->header.length = sizeof(*memdev);
1381                 memdev->device_handle = handle[4];
1382                 memdev->physical_id = 4;
1383                 memdev->region_id = 0;
1384                 memdev->range_index = 12+1;
1385                 memdev->region_index = 8+1;
1386                 memdev->region_size = 0;
1387                 memdev->region_offset = 0;
1388                 memdev->address = 0;
1389                 memdev->interleave_index = 0;
1390                 memdev->interleave_ways = 1;
1391
1392                 offset = offset + sizeof(struct acpi_nfit_memory_map) * 3;
1393                 /* flush3 (dimm4) */
1394                 flush = nfit_buf + offset;
1395                 flush->header.type = ACPI_NFIT_TYPE_FLUSH_ADDRESS;
1396                 flush->header.length = flush_hint_size;
1397                 flush->device_handle = handle[4];
1398                 flush->hint_count = NUM_HINTS;
1399                 for (i = 0; i < NUM_HINTS; i++)
1400                         flush->hint_address[i] = t->flush_dma[4]
1401                                 + i * sizeof(u64);
1402         }
1403
1404         post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA0_SIZE);
1405
1406         acpi_desc = &t->acpi_desc;
1407         set_bit(ND_CMD_GET_CONFIG_SIZE, &acpi_desc->dimm_cmd_force_en);
1408         set_bit(ND_CMD_GET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
1409         set_bit(ND_CMD_SET_CONFIG_DATA, &acpi_desc->dimm_cmd_force_en);
1410         set_bit(ND_CMD_SMART, &acpi_desc->dimm_cmd_force_en);
1411         set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1412         set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1413         set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1414         set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
1415         set_bit(ND_CMD_SMART_THRESHOLD, &acpi_desc->dimm_cmd_force_en);
1416 }
1417
1418 static void nfit_test1_setup(struct nfit_test *t)
1419 {
1420         size_t offset;
1421         void *nfit_buf = t->nfit_buf;
1422         struct acpi_nfit_memory_map *memdev;
1423         struct acpi_nfit_control_region *dcr;
1424         struct acpi_nfit_system_address *spa;
1425         struct acpi_nfit_desc *acpi_desc;
1426
1427         offset = 0;
1428         /* spa0 (flat range with no bdw aliasing) */
1429         spa = nfit_buf + offset;
1430         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1431         spa->header.length = sizeof(*spa);
1432         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_PM), 16);
1433         spa->range_index = 0+1;
1434         spa->address = t->spa_set_dma[0];
1435         spa->length = SPA2_SIZE;
1436
1437         /* virtual cd region */
1438         spa = nfit_buf + sizeof(*spa);
1439         spa->header.type = ACPI_NFIT_TYPE_SYSTEM_ADDRESS;
1440         spa->header.length = sizeof(*spa);
1441         memcpy(spa->range_guid, to_nfit_uuid(NFIT_SPA_VCD), 16);
1442         spa->range_index = 0;
1443         spa->address = t->spa_set_dma[1];
1444         spa->length = SPA_VCD_SIZE;
1445
1446         offset += sizeof(*spa) * 2;
1447         /* mem-region0 (spa0, dimm0) */
1448         memdev = nfit_buf + offset;
1449         memdev->header.type = ACPI_NFIT_TYPE_MEMORY_MAP;
1450         memdev->header.length = sizeof(*memdev);
1451         memdev->device_handle = 0;
1452         memdev->physical_id = 0;
1453         memdev->region_id = 0;
1454         memdev->range_index = 0+1;
1455         memdev->region_index = 0+1;
1456         memdev->region_size = SPA2_SIZE;
1457         memdev->region_offset = 0;
1458         memdev->address = 0;
1459         memdev->interleave_index = 0;
1460         memdev->interleave_ways = 1;
1461         memdev->flags = ACPI_NFIT_MEM_SAVE_FAILED | ACPI_NFIT_MEM_RESTORE_FAILED
1462                 | ACPI_NFIT_MEM_FLUSH_FAILED | ACPI_NFIT_MEM_HEALTH_OBSERVED
1463                 | ACPI_NFIT_MEM_NOT_ARMED;
1464
1465         offset += sizeof(*memdev);
1466         /* dcr-descriptor0 */
1467         dcr = nfit_buf + offset;
1468         dcr->header.type = ACPI_NFIT_TYPE_CONTROL_REGION;
1469         dcr->header.length = offsetof(struct acpi_nfit_control_region,
1470                         window_size);
1471         dcr->region_index = 0+1;
1472         dcr_common_init(dcr);
1473         dcr->serial_number = ~0;
1474         dcr->code = NFIT_FIC_BYTE;
1475         dcr->windows = 0;
1476
1477         post_ars_status(&t->ars_state, t->spa_set_dma[0], SPA2_SIZE);
1478
1479         acpi_desc = &t->acpi_desc;
1480         set_bit(ND_CMD_ARS_CAP, &acpi_desc->bus_cmd_force_en);
1481         set_bit(ND_CMD_ARS_START, &acpi_desc->bus_cmd_force_en);
1482         set_bit(ND_CMD_ARS_STATUS, &acpi_desc->bus_cmd_force_en);
1483         set_bit(ND_CMD_CLEAR_ERROR, &acpi_desc->bus_cmd_force_en);
1484 }
1485
1486 static int nfit_test_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
1487                 void *iobuf, u64 len, int rw)
1488 {
1489         struct nfit_blk *nfit_blk = ndbr->blk_provider_data;
1490         struct nfit_blk_mmio *mmio = &nfit_blk->mmio[BDW];
1491         struct nd_region *nd_region = &ndbr->nd_region;
1492         unsigned int lane;
1493
1494         lane = nd_region_acquire_lane(nd_region);
1495         if (rw)
1496                 memcpy(mmio->addr.base + dpa, iobuf, len);
1497         else {
1498                 memcpy(iobuf, mmio->addr.base + dpa, len);
1499
1500                 /* give us some some coverage of the mmio_flush_range() API */
1501                 mmio_flush_range(mmio->addr.base + dpa, len);
1502         }
1503         nd_region_release_lane(nd_region, lane);
1504
1505         return 0;
1506 }
1507
1508 static int nfit_test_probe(struct platform_device *pdev)
1509 {
1510         struct nvdimm_bus_descriptor *nd_desc;
1511         struct acpi_nfit_desc *acpi_desc;
1512         struct device *dev = &pdev->dev;
1513         struct nfit_test *nfit_test;
1514         struct nfit_mem *nfit_mem;
1515         union acpi_object *obj;
1516         int rc;
1517
1518         nfit_test = to_nfit_test(&pdev->dev);
1519
1520         /* common alloc */
1521         if (nfit_test->num_dcr) {
1522                 int num = nfit_test->num_dcr;
1523
1524                 nfit_test->dimm = devm_kcalloc(dev, num, sizeof(void *),
1525                                 GFP_KERNEL);
1526                 nfit_test->dimm_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
1527                                 GFP_KERNEL);
1528                 nfit_test->flush = devm_kcalloc(dev, num, sizeof(void *),
1529                                 GFP_KERNEL);
1530                 nfit_test->flush_dma = devm_kcalloc(dev, num, sizeof(dma_addr_t),
1531                                 GFP_KERNEL);
1532                 nfit_test->label = devm_kcalloc(dev, num, sizeof(void *),
1533                                 GFP_KERNEL);
1534                 nfit_test->label_dma = devm_kcalloc(dev, num,
1535                                 sizeof(dma_addr_t), GFP_KERNEL);
1536                 nfit_test->dcr = devm_kcalloc(dev, num,
1537                                 sizeof(struct nfit_test_dcr *), GFP_KERNEL);
1538                 nfit_test->dcr_dma = devm_kcalloc(dev, num,
1539                                 sizeof(dma_addr_t), GFP_KERNEL);
1540                 if (nfit_test->dimm && nfit_test->dimm_dma && nfit_test->label
1541                                 && nfit_test->label_dma && nfit_test->dcr
1542                                 && nfit_test->dcr_dma && nfit_test->flush
1543                                 && nfit_test->flush_dma)
1544                         /* pass */;
1545                 else
1546                         return -ENOMEM;
1547         }
1548
1549         if (nfit_test->num_pm) {
1550                 int num = nfit_test->num_pm;
1551
1552                 nfit_test->spa_set = devm_kcalloc(dev, num, sizeof(void *),
1553                                 GFP_KERNEL);
1554                 nfit_test->spa_set_dma = devm_kcalloc(dev, num,
1555                                 sizeof(dma_addr_t), GFP_KERNEL);
1556                 if (nfit_test->spa_set && nfit_test->spa_set_dma)
1557                         /* pass */;
1558                 else
1559                         return -ENOMEM;
1560         }
1561
1562         /* per-nfit specific alloc */
1563         if (nfit_test->alloc(nfit_test))
1564                 return -ENOMEM;
1565
1566         nfit_test->setup(nfit_test);
1567         acpi_desc = &nfit_test->acpi_desc;
1568         acpi_nfit_desc_init(acpi_desc, &pdev->dev);
1569         acpi_desc->blk_do_io = nfit_test_blk_do_io;
1570         nd_desc = &acpi_desc->nd_desc;
1571         nd_desc->provider_name = NULL;
1572         nd_desc->module = THIS_MODULE;
1573         nd_desc->ndctl = nfit_test_ctl;
1574
1575         rc = acpi_nfit_init(acpi_desc, nfit_test->nfit_buf,
1576                         nfit_test->nfit_size);
1577         if (rc)
1578                 return rc;
1579
1580         if (nfit_test->setup != nfit_test0_setup)
1581                 return 0;
1582
1583         nfit_test->setup_hotplug = 1;
1584         nfit_test->setup(nfit_test);
1585
1586         obj = kzalloc(sizeof(*obj), GFP_KERNEL);
1587         if (!obj)
1588                 return -ENOMEM;
1589         obj->type = ACPI_TYPE_BUFFER;
1590         obj->buffer.length = nfit_test->nfit_size;
1591         obj->buffer.pointer = nfit_test->nfit_buf;
1592         *(nfit_test->_fit) = obj;
1593         __acpi_nfit_notify(&pdev->dev, nfit_test, 0x80);
1594
1595         /* associate dimm devices with nfit_mem data for notification testing */
1596         mutex_lock(&acpi_desc->init_mutex);
1597         list_for_each_entry(nfit_mem, &acpi_desc->dimms, list) {
1598                 u32 nfit_handle = __to_nfit_memdev(nfit_mem)->device_handle;
1599                 int i;
1600
1601                 for (i = 0; i < NUM_DCR; i++)
1602                         if (nfit_handle == handle[i])
1603                                 dev_set_drvdata(nfit_test->dimm_dev[i],
1604                                                 nfit_mem);
1605         }
1606         mutex_unlock(&acpi_desc->init_mutex);
1607
1608         return 0;
1609 }
1610
1611 static int nfit_test_remove(struct platform_device *pdev)
1612 {
1613         return 0;
1614 }
1615
1616 static void nfit_test_release(struct device *dev)
1617 {
1618         struct nfit_test *nfit_test = to_nfit_test(dev);
1619
1620         kfree(nfit_test);
1621 }
1622
1623 static const struct platform_device_id nfit_test_id[] = {
1624         { KBUILD_MODNAME },
1625         { },
1626 };
1627
1628 static struct platform_driver nfit_test_driver = {
1629         .probe = nfit_test_probe,
1630         .remove = nfit_test_remove,
1631         .driver = {
1632                 .name = KBUILD_MODNAME,
1633         },
1634         .id_table = nfit_test_id,
1635 };
1636
1637 static __init int nfit_test_init(void)
1638 {
1639         int rc, i;
1640
1641         nfit_test_dimm = class_create(THIS_MODULE, "nfit_test_dimm");
1642         if (IS_ERR(nfit_test_dimm))
1643                 return PTR_ERR(nfit_test_dimm);
1644
1645         nfit_test_setup(nfit_test_lookup);
1646
1647         for (i = 0; i < NUM_NFITS; i++) {
1648                 struct nfit_test *nfit_test;
1649                 struct platform_device *pdev;
1650
1651                 nfit_test = kzalloc(sizeof(*nfit_test), GFP_KERNEL);
1652                 if (!nfit_test) {
1653                         rc = -ENOMEM;
1654                         goto err_register;
1655                 }
1656                 INIT_LIST_HEAD(&nfit_test->resources);
1657                 switch (i) {
1658                 case 0:
1659                         nfit_test->num_pm = NUM_PM;
1660                         nfit_test->num_dcr = NUM_DCR;
1661                         nfit_test->alloc = nfit_test0_alloc;
1662                         nfit_test->setup = nfit_test0_setup;
1663                         break;
1664                 case 1:
1665                         nfit_test->num_pm = 1;
1666                         nfit_test->alloc = nfit_test1_alloc;
1667                         nfit_test->setup = nfit_test1_setup;
1668                         break;
1669                 default:
1670                         rc = -EINVAL;
1671                         goto err_register;
1672                 }
1673                 pdev = &nfit_test->pdev;
1674                 pdev->name = KBUILD_MODNAME;
1675                 pdev->id = i;
1676                 pdev->dev.release = nfit_test_release;
1677                 rc = platform_device_register(pdev);
1678                 if (rc) {
1679                         put_device(&pdev->dev);
1680                         goto err_register;
1681                 }
1682
1683                 rc = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
1684                 if (rc)
1685                         goto err_register;
1686
1687                 instances[i] = nfit_test;
1688         }
1689
1690         rc = platform_driver_register(&nfit_test_driver);
1691         if (rc)
1692                 goto err_register;
1693         return 0;
1694
1695  err_register:
1696         for (i = 0; i < NUM_NFITS; i++)
1697                 if (instances[i])
1698                         platform_device_unregister(&instances[i]->pdev);
1699         nfit_test_teardown();
1700         return rc;
1701 }
1702
1703 static __exit void nfit_test_exit(void)
1704 {
1705         int i;
1706
1707         platform_driver_unregister(&nfit_test_driver);
1708         for (i = 0; i < NUM_NFITS; i++)
1709                 platform_device_unregister(&instances[i]->pdev);
1710         nfit_test_teardown();
1711         class_destroy(nfit_test_dimm);
1712 }
1713
1714 module_init(nfit_test_init);
1715 module_exit(nfit_test_exit);
1716 MODULE_LICENSE("GPL v2");
1717 MODULE_AUTHOR("Intel Corporation");