ndtest: Add nvdimm control functions
[linux-2.6-microblaze.git] / tools / testing / nvdimm / test / ndtest.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3
4 #include <linux/platform_device.h>
5 #include <linux/device.h>
6 #include <linux/module.h>
7 #include <linux/genalloc.h>
8 #include <linux/vmalloc.h>
9 #include <linux/dma-mapping.h>
10 #include <linux/list_sort.h>
11 #include <linux/libnvdimm.h>
12 #include <linux/ndctl.h>
13 #include <nd-core.h>
14 #include <linux/printk.h>
15 #include <linux/seq_buf.h>
16
17 #include "../watermark.h"
18 #include "nfit_test.h"
19 #include "ndtest.h"
20
21 enum {
22         DIMM_SIZE = SZ_32M,
23         LABEL_SIZE = SZ_128K,
24         NUM_INSTANCES = 2,
25         NUM_DCR = 4,
26         NDTEST_MAX_MAPPING = 6,
27 };
28
29 #define NDTEST_SCM_DIMM_CMD_MASK           \
30         ((1ul << ND_CMD_GET_CONFIG_SIZE) | \
31          (1ul << ND_CMD_GET_CONFIG_DATA) | \
32          (1ul << ND_CMD_SET_CONFIG_DATA) | \
33          (1ul << ND_CMD_CALL))
34
35 #define NFIT_DIMM_HANDLE(node, socket, imc, chan, dimm)                 \
36         (((node & 0xfff) << 16) | ((socket & 0xf) << 12)                \
37          | ((imc & 0xf) << 8) | ((chan & 0xf) << 4) | (dimm & 0xf))
38
39 static DEFINE_SPINLOCK(ndtest_lock);
40 static struct ndtest_priv *instances[NUM_INSTANCES];
41 static struct class *ndtest_dimm_class;
42 static struct gen_pool *ndtest_pool;
43
44 static struct ndtest_dimm dimm_group1[] = {
45         {
46                 .size = DIMM_SIZE,
47                 .handle = NFIT_DIMM_HANDLE(0, 0, 0, 0, 0),
48                 .uuid_str = "1e5c75d2-b618-11ea-9aa3-507b9ddc0f72",
49                 .physical_id = 0,
50                 .num_formats = 2,
51         },
52         {
53                 .size = DIMM_SIZE,
54                 .handle = NFIT_DIMM_HANDLE(0, 0, 0, 0, 1),
55                 .uuid_str = "1c4d43ac-b618-11ea-be80-507b9ddc0f72",
56                 .physical_id = 1,
57                 .num_formats = 2,
58         },
59         {
60                 .size = DIMM_SIZE,
61                 .handle = NFIT_DIMM_HANDLE(0, 0, 1, 0, 0),
62                 .uuid_str = "a9f17ffc-b618-11ea-b36d-507b9ddc0f72",
63                 .physical_id = 2,
64                 .num_formats = 2,
65         },
66         {
67                 .size = DIMM_SIZE,
68                 .handle = NFIT_DIMM_HANDLE(0, 0, 1, 0, 1),
69                 .uuid_str = "b6b83b22-b618-11ea-8aae-507b9ddc0f72",
70                 .physical_id = 3,
71                 .num_formats = 2,
72         },
73         {
74                 .size = DIMM_SIZE,
75                 .handle = NFIT_DIMM_HANDLE(0, 1, 0, 0, 0),
76                 .uuid_str = "bf9baaee-b618-11ea-b181-507b9ddc0f72",
77                 .physical_id = 4,
78                 .num_formats = 2,
79         },
80 };
81
82 static struct ndtest_dimm dimm_group2[] = {
83         {
84                 .size = DIMM_SIZE,
85                 .handle = NFIT_DIMM_HANDLE(1, 0, 0, 0, 0),
86                 .uuid_str = "ca0817e2-b618-11ea-9db3-507b9ddc0f72",
87                 .physical_id = 0,
88                 .num_formats = 1,
89         },
90 };
91
92 static struct ndtest_mapping region0_mapping[] = {
93         {
94                 .dimm = 0,
95                 .position = 0,
96                 .start = 0,
97                 .size = SZ_16M,
98         },
99         {
100                 .dimm = 1,
101                 .position = 1,
102                 .start = 0,
103                 .size = SZ_16M,
104         }
105 };
106
107 static struct ndtest_mapping region1_mapping[] = {
108         {
109                 .dimm = 0,
110                 .position = 0,
111                 .start = SZ_16M,
112                 .size = SZ_16M,
113         },
114         {
115                 .dimm = 1,
116                 .position = 1,
117                 .start = SZ_16M,
118                 .size = SZ_16M,
119         },
120         {
121                 .dimm = 2,
122                 .position = 2,
123                 .start = SZ_16M,
124                 .size = SZ_16M,
125         },
126         {
127                 .dimm = 3,
128                 .position = 3,
129                 .start = SZ_16M,
130                 .size = SZ_16M,
131         },
132 };
133
134 static struct ndtest_mapping region2_mapping[] = {
135         {
136                 .dimm = 0,
137                 .position = 0,
138                 .start = 0,
139                 .size = DIMM_SIZE,
140         },
141 };
142
143 static struct ndtest_mapping region3_mapping[] = {
144         {
145                 .dimm = 1,
146                 .start = 0,
147                 .size = DIMM_SIZE,
148         }
149 };
150
151 static struct ndtest_mapping region4_mapping[] = {
152         {
153                 .dimm = 2,
154                 .start = 0,
155                 .size = DIMM_SIZE,
156         }
157 };
158
159 static struct ndtest_mapping region5_mapping[] = {
160         {
161                 .dimm = 3,
162                 .start = 0,
163                 .size = DIMM_SIZE,
164         }
165 };
166
167 static struct ndtest_region bus0_regions[] = {
168         {
169                 .type = ND_DEVICE_NAMESPACE_PMEM,
170                 .num_mappings = ARRAY_SIZE(region0_mapping),
171                 .mapping = region0_mapping,
172                 .size = DIMM_SIZE,
173                 .range_index = 1,
174         },
175         {
176                 .type = ND_DEVICE_NAMESPACE_PMEM,
177                 .num_mappings = ARRAY_SIZE(region1_mapping),
178                 .mapping = region1_mapping,
179                 .size = DIMM_SIZE * 2,
180                 .range_index = 2,
181         },
182         {
183                 .type = ND_DEVICE_NAMESPACE_BLK,
184                 .num_mappings = ARRAY_SIZE(region2_mapping),
185                 .mapping = region2_mapping,
186                 .size = DIMM_SIZE,
187                 .range_index = 3,
188         },
189         {
190                 .type = ND_DEVICE_NAMESPACE_BLK,
191                 .num_mappings = ARRAY_SIZE(region3_mapping),
192                 .mapping = region3_mapping,
193                 .size = DIMM_SIZE,
194                 .range_index = 4,
195         },
196         {
197                 .type = ND_DEVICE_NAMESPACE_BLK,
198                 .num_mappings = ARRAY_SIZE(region4_mapping),
199                 .mapping = region4_mapping,
200                 .size = DIMM_SIZE,
201                 .range_index = 5,
202         },
203         {
204                 .type = ND_DEVICE_NAMESPACE_BLK,
205                 .num_mappings = ARRAY_SIZE(region5_mapping),
206                 .mapping = region5_mapping,
207                 .size = DIMM_SIZE,
208                 .range_index = 6,
209         },
210 };
211
212 static struct ndtest_mapping region6_mapping[] = {
213         {
214                 .dimm = 0,
215                 .position = 0,
216                 .start = 0,
217                 .size = DIMM_SIZE,
218         },
219 };
220
221 static struct ndtest_region bus1_regions[] = {
222         {
223                 .type = ND_DEVICE_NAMESPACE_IO,
224                 .num_mappings = ARRAY_SIZE(region6_mapping),
225                 .mapping = region6_mapping,
226                 .size = DIMM_SIZE,
227                 .range_index = 1,
228         },
229 };
230
231 static struct ndtest_config bus_configs[NUM_INSTANCES] = {
232         /* bus 1 */
233         {
234                 .dimm_start = 0,
235                 .dimm_count = ARRAY_SIZE(dimm_group1),
236                 .dimms = dimm_group1,
237                 .regions = bus0_regions,
238                 .num_regions = ARRAY_SIZE(bus0_regions),
239         },
240         /* bus 2 */
241         {
242                 .dimm_start = ARRAY_SIZE(dimm_group1),
243                 .dimm_count = ARRAY_SIZE(dimm_group2),
244                 .dimms = dimm_group2,
245                 .regions = bus1_regions,
246                 .num_regions = ARRAY_SIZE(bus1_regions),
247         },
248 };
249
250 static inline struct ndtest_priv *to_ndtest_priv(struct device *dev)
251 {
252         struct platform_device *pdev = to_platform_device(dev);
253
254         return container_of(pdev, struct ndtest_priv, pdev);
255 }
256
257 static int ndtest_config_get(struct ndtest_dimm *p, unsigned int buf_len,
258                              struct nd_cmd_get_config_data_hdr *hdr)
259 {
260         unsigned int len;
261
262         if ((hdr->in_offset + hdr->in_length) > LABEL_SIZE)
263                 return -EINVAL;
264
265         hdr->status = 0;
266         len = min(hdr->in_length, LABEL_SIZE - hdr->in_offset);
267         memcpy(hdr->out_buf, p->label_area + hdr->in_offset, len);
268
269         return buf_len - len;
270 }
271
272 static int ndtest_config_set(struct ndtest_dimm *p, unsigned int buf_len,
273                              struct nd_cmd_set_config_hdr *hdr)
274 {
275         unsigned int len;
276
277         if ((hdr->in_offset + hdr->in_length) > LABEL_SIZE)
278                 return -EINVAL;
279
280         len = min(hdr->in_length, LABEL_SIZE - hdr->in_offset);
281         memcpy(p->label_area + hdr->in_offset, hdr->in_buf, len);
282
283         return buf_len - len;
284 }
285
286 static int ndtest_get_config_size(struct ndtest_dimm *dimm, unsigned int buf_len,
287                                   struct nd_cmd_get_config_size *size)
288 {
289         size->status = 0;
290         size->max_xfer = 8;
291         size->config_size = dimm->config_size;
292
293         return 0;
294 }
295
296 static int ndtest_ctl(struct nvdimm_bus_descriptor *nd_desc,
297                       struct nvdimm *nvdimm, unsigned int cmd, void *buf,
298                       unsigned int buf_len, int *cmd_rc)
299 {
300         struct ndtest_dimm *dimm;
301         int _cmd_rc;
302
303         if (!cmd_rc)
304                 cmd_rc = &_cmd_rc;
305
306         *cmd_rc = 0;
307
308         if (!nvdimm)
309                 return -EINVAL;
310
311         dimm = nvdimm_provider_data(nvdimm);
312         if (!dimm)
313                 return -EINVAL;
314
315         switch (cmd) {
316         case ND_CMD_GET_CONFIG_SIZE:
317                 *cmd_rc = ndtest_get_config_size(dimm, buf_len, buf);
318                 break;
319         case ND_CMD_GET_CONFIG_DATA:
320                 *cmd_rc = ndtest_config_get(dimm, buf_len, buf);
321                 break;
322         case ND_CMD_SET_CONFIG_DATA:
323                 *cmd_rc = ndtest_config_set(dimm, buf_len, buf);
324                 break;
325         default:
326                 return -EINVAL;
327         }
328
329         /* Failures for a DIMM can be injected using fail_cmd and
330          * fail_cmd_code, see the device attributes below
331          */
332         if ((1 << cmd) & dimm->fail_cmd)
333                 return dimm->fail_cmd_code ? dimm->fail_cmd_code : -EIO;
334
335         return 0;
336 }
337
338 static int ndtest_blk_do_io(struct nd_blk_region *ndbr, resource_size_t dpa,
339                 void *iobuf, u64 len, int rw)
340 {
341         struct ndtest_dimm *dimm = ndbr->blk_provider_data;
342         struct ndtest_blk_mmio *mmio = dimm->mmio;
343         struct nd_region *nd_region = &ndbr->nd_region;
344         unsigned int lane;
345
346         if (!mmio)
347                 return -ENOMEM;
348
349         lane = nd_region_acquire_lane(nd_region);
350         if (rw)
351                 memcpy(mmio->base + dpa, iobuf, len);
352         else {
353                 memcpy(iobuf, mmio->base + dpa, len);
354                 arch_invalidate_pmem(mmio->base + dpa, len);
355         }
356
357         nd_region_release_lane(nd_region, lane);
358
359         return 0;
360 }
361
362 static int ndtest_blk_region_enable(struct nvdimm_bus *nvdimm_bus,
363                                     struct device *dev)
364 {
365         struct nd_blk_region *ndbr = to_nd_blk_region(dev);
366         struct nvdimm *nvdimm;
367         struct ndtest_dimm *dimm;
368         struct ndtest_blk_mmio *mmio;
369
370         nvdimm = nd_blk_region_to_dimm(ndbr);
371         dimm = nvdimm_provider_data(nvdimm);
372
373         nd_blk_region_set_provider_data(ndbr, dimm);
374         dimm->blk_region = to_nd_region(dev);
375
376         mmio = devm_kzalloc(dev, sizeof(struct ndtest_blk_mmio), GFP_KERNEL);
377         if (!mmio)
378                 return -ENOMEM;
379
380         mmio->base = (void __iomem *) devm_nvdimm_memremap(
381                 dev, dimm->address, 12, nd_blk_memremap_flags(ndbr));
382         if (!mmio->base) {
383                 dev_err(dev, "%s failed to map blk dimm\n", nvdimm_name(nvdimm));
384                 return -ENOMEM;
385         }
386         mmio->size = dimm->size;
387         mmio->base_offset = 0;
388
389         dimm->mmio = mmio;
390
391         return 0;
392 }
393
394 static struct nfit_test_resource *ndtest_resource_lookup(resource_size_t addr)
395 {
396         int i;
397
398         for (i = 0; i < NUM_INSTANCES; i++) {
399                 struct nfit_test_resource *n, *nfit_res = NULL;
400                 struct ndtest_priv *t = instances[i];
401
402                 if (!t)
403                         continue;
404                 spin_lock(&ndtest_lock);
405                 list_for_each_entry(n, &t->resources, list) {
406                         if (addr >= n->res.start && (addr < n->res.start
407                                                 + resource_size(&n->res))) {
408                                 nfit_res = n;
409                                 break;
410                         } else if (addr >= (unsigned long) n->buf
411                                         && (addr < (unsigned long) n->buf
412                                                 + resource_size(&n->res))) {
413                                 nfit_res = n;
414                                 break;
415                         }
416                 }
417                 spin_unlock(&ndtest_lock);
418                 if (nfit_res)
419                         return nfit_res;
420         }
421
422         pr_warn("Failed to get resource\n");
423
424         return NULL;
425 }
426
427 static void ndtest_release_resource(void *data)
428 {
429         struct nfit_test_resource *res  = data;
430
431         spin_lock(&ndtest_lock);
432         list_del(&res->list);
433         spin_unlock(&ndtest_lock);
434
435         if (resource_size(&res->res) >= DIMM_SIZE)
436                 gen_pool_free(ndtest_pool, res->res.start,
437                                 resource_size(&res->res));
438         vfree(res->buf);
439         kfree(res);
440 }
441
442 static void *ndtest_alloc_resource(struct ndtest_priv *p, size_t size,
443                                    dma_addr_t *dma)
444 {
445         dma_addr_t __dma;
446         void *buf;
447         struct nfit_test_resource *res;
448         struct genpool_data_align data = {
449                 .align = SZ_128M,
450         };
451
452         res = kzalloc(sizeof(*res), GFP_KERNEL);
453         if (!res)
454                 return NULL;
455
456         buf = vmalloc(size);
457         if (size >= DIMM_SIZE)
458                 __dma = gen_pool_alloc_algo(ndtest_pool, size,
459                                             gen_pool_first_fit_align, &data);
460         else
461                 __dma = (unsigned long) buf;
462
463         if (!__dma)
464                 goto buf_err;
465
466         INIT_LIST_HEAD(&res->list);
467         res->dev = &p->pdev.dev;
468         res->buf = buf;
469         res->res.start = __dma;
470         res->res.end = __dma + size - 1;
471         res->res.name = "NFIT";
472         spin_lock_init(&res->lock);
473         INIT_LIST_HEAD(&res->requests);
474         spin_lock(&ndtest_lock);
475         list_add(&res->list, &p->resources);
476         spin_unlock(&ndtest_lock);
477
478         if (dma)
479                 *dma = __dma;
480
481         if (!devm_add_action(&p->pdev.dev, ndtest_release_resource, res))
482                 return res->buf;
483
484 buf_err:
485         if (__dma && size >= DIMM_SIZE)
486                 gen_pool_free(ndtest_pool, __dma, size);
487         if (buf)
488                 vfree(buf);
489         kfree(res);
490
491         return NULL;
492 }
493
494 static ssize_t range_index_show(struct device *dev,
495                 struct device_attribute *attr, char *buf)
496 {
497         struct nd_region *nd_region = to_nd_region(dev);
498         struct ndtest_region *region = nd_region_provider_data(nd_region);
499
500         return sprintf(buf, "%d\n", region->range_index);
501 }
502 static DEVICE_ATTR_RO(range_index);
503
504 static struct attribute *ndtest_region_attributes[] = {
505         &dev_attr_range_index.attr,
506         NULL,
507 };
508
509 static const struct attribute_group ndtest_region_attribute_group = {
510         .name = "papr",
511         .attrs = ndtest_region_attributes,
512 };
513
514 static const struct attribute_group *ndtest_region_attribute_groups[] = {
515         &ndtest_region_attribute_group,
516         NULL,
517 };
518
519 static int ndtest_create_region(struct ndtest_priv *p,
520                                 struct ndtest_region *region)
521 {
522         struct nd_mapping_desc mappings[NDTEST_MAX_MAPPING];
523         struct nd_blk_region_desc ndbr_desc;
524         struct nd_interleave_set *nd_set;
525         struct nd_region_desc *ndr_desc;
526         struct resource res;
527         int i, ndimm = region->mapping[0].dimm;
528         u64 uuid[2];
529
530         memset(&res, 0, sizeof(res));
531         memset(&mappings, 0, sizeof(mappings));
532         memset(&ndbr_desc, 0, sizeof(ndbr_desc));
533         ndr_desc = &ndbr_desc.ndr_desc;
534
535         if (!ndtest_alloc_resource(p, region->size, &res.start))
536                 return -ENOMEM;
537
538         res.end = res.start + region->size - 1;
539         ndr_desc->mapping = mappings;
540         ndr_desc->res = &res;
541         ndr_desc->provider_data = region;
542         ndr_desc->attr_groups = ndtest_region_attribute_groups;
543
544         if (uuid_parse(p->config->dimms[ndimm].uuid_str, (uuid_t *)uuid)) {
545                 pr_err("failed to parse UUID\n");
546                 return -ENXIO;
547         }
548
549         nd_set = devm_kzalloc(&p->pdev.dev, sizeof(*nd_set), GFP_KERNEL);
550         if (!nd_set)
551                 return -ENOMEM;
552
553         nd_set->cookie1 = cpu_to_le64(uuid[0]);
554         nd_set->cookie2 = cpu_to_le64(uuid[1]);
555         nd_set->altcookie = nd_set->cookie1;
556         ndr_desc->nd_set = nd_set;
557
558         if (region->type == ND_DEVICE_NAMESPACE_BLK) {
559                 mappings[0].start = 0;
560                 mappings[0].size = DIMM_SIZE;
561                 mappings[0].nvdimm = p->config->dimms[ndimm].nvdimm;
562
563                 ndr_desc->mapping = &mappings[0];
564                 ndr_desc->num_mappings = 1;
565                 ndr_desc->num_lanes = 1;
566                 ndbr_desc.enable = ndtest_blk_region_enable;
567                 ndbr_desc.do_io = ndtest_blk_do_io;
568                 region->region = nvdimm_blk_region_create(p->bus, ndr_desc);
569
570                 goto done;
571         }
572
573         for (i = 0; i < region->num_mappings; i++) {
574                 ndimm = region->mapping[i].dimm;
575                 mappings[i].start = region->mapping[i].start;
576                 mappings[i].size = region->mapping[i].size;
577                 mappings[i].position = region->mapping[i].position;
578                 mappings[i].nvdimm = p->config->dimms[ndimm].nvdimm;
579         }
580
581         ndr_desc->num_mappings = region->num_mappings;
582         region->region = nvdimm_pmem_region_create(p->bus, ndr_desc);
583
584 done:
585         if (!region->region) {
586                 dev_err(&p->pdev.dev, "Error registering region %pR\n",
587                         ndr_desc->res);
588                 return -ENXIO;
589         }
590
591         return 0;
592 }
593
594 static int ndtest_init_regions(struct ndtest_priv *p)
595 {
596         int i, ret = 0;
597
598         for (i = 0; i < p->config->num_regions; i++) {
599                 ret = ndtest_create_region(p, &p->config->regions[i]);
600                 if (ret)
601                         return ret;
602         }
603
604         return 0;
605 }
606
607 static void put_dimms(void *data)
608 {
609         struct ndtest_priv *p = data;
610         int i;
611
612         for (i = 0; i < p->config->dimm_count; i++)
613                 if (p->config->dimms[i].dev) {
614                         device_unregister(p->config->dimms[i].dev);
615                         p->config->dimms[i].dev = NULL;
616                 }
617 }
618
619 static ssize_t handle_show(struct device *dev, struct device_attribute *attr,
620                 char *buf)
621 {
622         struct ndtest_dimm *dimm = dev_get_drvdata(dev);
623
624         return sprintf(buf, "%#x\n", dimm->handle);
625 }
626 static DEVICE_ATTR_RO(handle);
627
628 static ssize_t fail_cmd_show(struct device *dev, struct device_attribute *attr,
629                 char *buf)
630 {
631         struct ndtest_dimm *dimm = dev_get_drvdata(dev);
632
633         return sprintf(buf, "%#x\n", dimm->fail_cmd);
634 }
635
636 static ssize_t fail_cmd_store(struct device *dev, struct device_attribute *attr,
637                 const char *buf, size_t size)
638 {
639         struct ndtest_dimm *dimm = dev_get_drvdata(dev);
640         unsigned long val;
641         ssize_t rc;
642
643         rc = kstrtol(buf, 0, &val);
644         if (rc)
645                 return rc;
646
647         dimm->fail_cmd = val;
648
649         return size;
650 }
651 static DEVICE_ATTR_RW(fail_cmd);
652
653 static ssize_t fail_cmd_code_show(struct device *dev, struct device_attribute *attr,
654                 char *buf)
655 {
656         struct ndtest_dimm *dimm = dev_get_drvdata(dev);
657
658         return sprintf(buf, "%d\n", dimm->fail_cmd_code);
659 }
660
661 static ssize_t fail_cmd_code_store(struct device *dev, struct device_attribute *attr,
662                 const char *buf, size_t size)
663 {
664         struct ndtest_dimm *dimm = dev_get_drvdata(dev);
665         unsigned long val;
666         ssize_t rc;
667
668         rc = kstrtol(buf, 0, &val);
669         if (rc)
670                 return rc;
671
672         dimm->fail_cmd_code = val;
673         return size;
674 }
675 static DEVICE_ATTR_RW(fail_cmd_code);
676
677 static struct attribute *dimm_attributes[] = {
678         &dev_attr_handle.attr,
679         &dev_attr_fail_cmd.attr,
680         &dev_attr_fail_cmd_code.attr,
681         NULL,
682 };
683
684 static struct attribute_group dimm_attribute_group = {
685         .attrs = dimm_attributes,
686 };
687
688 static const struct attribute_group *dimm_attribute_groups[] = {
689         &dimm_attribute_group,
690         NULL,
691 };
692
693 static ssize_t phys_id_show(struct device *dev,
694                 struct device_attribute *attr, char *buf)
695 {
696         struct nvdimm *nvdimm = to_nvdimm(dev);
697         struct ndtest_dimm *dimm = nvdimm_provider_data(nvdimm);
698
699         return sprintf(buf, "%#x\n", dimm->physical_id);
700 }
701 static DEVICE_ATTR_RO(phys_id);
702
703 static ssize_t vendor_show(struct device *dev,
704                            struct device_attribute *attr, char *buf)
705 {
706         return sprintf(buf, "0x1234567\n");
707 }
708 static DEVICE_ATTR_RO(vendor);
709
710 static ssize_t id_show(struct device *dev,
711                        struct device_attribute *attr, char *buf)
712 {
713         struct nvdimm *nvdimm = to_nvdimm(dev);
714         struct ndtest_dimm *dimm = nvdimm_provider_data(nvdimm);
715
716         return sprintf(buf, "%04x-%02x-%04x-%08x", 0xabcd,
717                        0xa, 2016, ~(dimm->handle));
718 }
719 static DEVICE_ATTR_RO(id);
720
721 static ssize_t nvdimm_handle_show(struct device *dev,
722                                   struct device_attribute *attr, char *buf)
723 {
724         struct nvdimm *nvdimm = to_nvdimm(dev);
725         struct ndtest_dimm *dimm = nvdimm_provider_data(nvdimm);
726
727         return sprintf(buf, "%#x\n", dimm->handle);
728 }
729
730 static struct device_attribute dev_attr_nvdimm_show_handle =  {
731         .attr   = { .name = "handle", .mode = 0444 },
732         .show   = nvdimm_handle_show,
733 };
734
735 static ssize_t subsystem_vendor_show(struct device *dev,
736                 struct device_attribute *attr, char *buf)
737 {
738         return sprintf(buf, "0x%04x\n", 0);
739 }
740 static DEVICE_ATTR_RO(subsystem_vendor);
741
742 static ssize_t dirty_shutdown_show(struct device *dev,
743                 struct device_attribute *attr, char *buf)
744 {
745         return sprintf(buf, "%d\n", 42);
746 }
747 static DEVICE_ATTR_RO(dirty_shutdown);
748
749 static ssize_t formats_show(struct device *dev,
750                 struct device_attribute *attr, char *buf)
751 {
752         struct nvdimm *nvdimm = to_nvdimm(dev);
753         struct ndtest_dimm *dimm = nvdimm_provider_data(nvdimm);
754
755         return sprintf(buf, "%d\n", dimm->num_formats);
756 }
757 static DEVICE_ATTR_RO(formats);
758
759 static ssize_t format_show(struct device *dev,
760                 struct device_attribute *attr, char *buf)
761 {
762         struct nvdimm *nvdimm = to_nvdimm(dev);
763         struct ndtest_dimm *dimm = nvdimm_provider_data(nvdimm);
764
765         if (dimm->num_formats > 1)
766                 return sprintf(buf, "0x201\n");
767
768         return sprintf(buf, "0x101\n");
769 }
770 static DEVICE_ATTR_RO(format);
771
772 static ssize_t format1_show(struct device *dev, struct device_attribute *attr,
773                             char *buf)
774 {
775         return sprintf(buf, "0x301\n");
776 }
777 static DEVICE_ATTR_RO(format1);
778
779 static umode_t ndtest_nvdimm_attr_visible(struct kobject *kobj,
780                                         struct attribute *a, int n)
781 {
782         struct device *dev = container_of(kobj, struct device, kobj);
783         struct nvdimm *nvdimm = to_nvdimm(dev);
784         struct ndtest_dimm *dimm = nvdimm_provider_data(nvdimm);
785
786         if (a == &dev_attr_format1.attr && dimm->num_formats <= 1)
787                 return 0;
788
789         return a->mode;
790 }
791
792 static struct attribute *ndtest_nvdimm_attributes[] = {
793         &dev_attr_nvdimm_show_handle.attr,
794         &dev_attr_vendor.attr,
795         &dev_attr_id.attr,
796         &dev_attr_phys_id.attr,
797         &dev_attr_subsystem_vendor.attr,
798         &dev_attr_dirty_shutdown.attr,
799         &dev_attr_formats.attr,
800         &dev_attr_format.attr,
801         &dev_attr_format1.attr,
802         NULL,
803 };
804
805 static const struct attribute_group ndtest_nvdimm_attribute_group = {
806         .name = "papr",
807         .attrs = ndtest_nvdimm_attributes,
808         .is_visible = ndtest_nvdimm_attr_visible,
809 };
810
811 static const struct attribute_group *ndtest_nvdimm_attribute_groups[] = {
812         &ndtest_nvdimm_attribute_group,
813         NULL,
814 };
815
816 static int ndtest_dimm_register(struct ndtest_priv *priv,
817                                 struct ndtest_dimm *dimm, int id)
818 {
819         struct device *dev = &priv->pdev.dev;
820         unsigned long dimm_flags = dimm->flags;
821
822         if (dimm->num_formats > 1) {
823                 set_bit(NDD_ALIASING, &dimm_flags);
824                 set_bit(NDD_LABELING, &dimm_flags);
825         }
826
827         dimm->nvdimm = nvdimm_create(priv->bus, dimm,
828                                     ndtest_nvdimm_attribute_groups, dimm_flags,
829                                     NDTEST_SCM_DIMM_CMD_MASK, 0, NULL);
830         if (!dimm->nvdimm) {
831                 dev_err(dev, "Error creating DIMM object for %pOF\n", priv->dn);
832                 return -ENXIO;
833         }
834
835         dimm->dev = device_create_with_groups(ndtest_dimm_class,
836                                              &priv->pdev.dev,
837                                              0, dimm, dimm_attribute_groups,
838                                              "test_dimm%d", id);
839         if (!dimm->dev) {
840                 pr_err("Could not create dimm device attributes\n");
841                 return -ENOMEM;
842         }
843
844         return 0;
845 }
846
847 static int ndtest_nvdimm_init(struct ndtest_priv *p)
848 {
849         struct ndtest_dimm *d;
850         void *res;
851         int i, id;
852
853         for (i = 0; i < p->config->dimm_count; i++) {
854                 d = &p->config->dimms[i];
855                 d->id = id = p->config->dimm_start + i;
856                 res = ndtest_alloc_resource(p, LABEL_SIZE, NULL);
857                 if (!res)
858                         return -ENOMEM;
859
860                 d->label_area = res;
861                 sprintf(d->label_area, "label%d", id);
862                 d->config_size = LABEL_SIZE;
863
864                 if (!ndtest_alloc_resource(p, d->size,
865                                            &p->dimm_dma[id]))
866                         return -ENOMEM;
867
868                 if (!ndtest_alloc_resource(p, LABEL_SIZE,
869                                            &p->label_dma[id]))
870                         return -ENOMEM;
871
872                 if (!ndtest_alloc_resource(p, LABEL_SIZE,
873                                            &p->dcr_dma[id]))
874                         return -ENOMEM;
875
876                 d->address = p->dimm_dma[id];
877
878                 ndtest_dimm_register(p, d, id);
879         }
880
881         return 0;
882 }
883
884 static ssize_t compatible_show(struct device *dev,
885                                struct device_attribute *attr, char *buf)
886 {
887         return sprintf(buf, "nvdimm_test");
888 }
889 static DEVICE_ATTR_RO(compatible);
890
891 static struct attribute *of_node_attributes[] = {
892         &dev_attr_compatible.attr,
893         NULL
894 };
895
896 static const struct attribute_group of_node_attribute_group = {
897         .name = "of_node",
898         .attrs = of_node_attributes,
899 };
900
901 static const struct attribute_group *ndtest_attribute_groups[] = {
902         &of_node_attribute_group,
903         NULL,
904 };
905
906 static int ndtest_bus_register(struct ndtest_priv *p)
907 {
908         p->config = &bus_configs[p->pdev.id];
909
910         p->bus_desc.ndctl = ndtest_ctl;
911         p->bus_desc.module = THIS_MODULE;
912         p->bus_desc.provider_name = NULL;
913         p->bus_desc.attr_groups = ndtest_attribute_groups;
914
915         p->bus = nvdimm_bus_register(&p->pdev.dev, &p->bus_desc);
916         if (!p->bus) {
917                 dev_err(&p->pdev.dev, "Error creating nvdimm bus %pOF\n", p->dn);
918                 return -ENOMEM;
919         }
920
921         return 0;
922 }
923
924 static int ndtest_remove(struct platform_device *pdev)
925 {
926         struct ndtest_priv *p = to_ndtest_priv(&pdev->dev);
927
928         nvdimm_bus_unregister(p->bus);
929         return 0;
930 }
931
932 static int ndtest_probe(struct platform_device *pdev)
933 {
934         struct ndtest_priv *p;
935         int rc;
936
937         p = to_ndtest_priv(&pdev->dev);
938         if (ndtest_bus_register(p))
939                 return -ENOMEM;
940
941         p->dcr_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
942                                  sizeof(dma_addr_t), GFP_KERNEL);
943         p->label_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
944                                    sizeof(dma_addr_t), GFP_KERNEL);
945         p->dimm_dma = devm_kcalloc(&p->pdev.dev, NUM_DCR,
946                                   sizeof(dma_addr_t), GFP_KERNEL);
947
948         rc = ndtest_nvdimm_init(p);
949         if (rc)
950                 goto err;
951
952         rc = ndtest_init_regions(p);
953         if (rc)
954                 goto err;
955
956         rc = devm_add_action_or_reset(&pdev->dev, put_dimms, p);
957         if (rc)
958                 goto err;
959
960         platform_set_drvdata(pdev, p);
961
962         return 0;
963
964 err:
965         pr_err("%s:%d Failed nvdimm init\n", __func__, __LINE__);
966         return rc;
967 }
968
969 static const struct platform_device_id ndtest_id[] = {
970         { KBUILD_MODNAME },
971         { },
972 };
973
974 static struct platform_driver ndtest_driver = {
975         .probe = ndtest_probe,
976         .remove = ndtest_remove,
977         .driver = {
978                 .name = KBUILD_MODNAME,
979         },
980         .id_table = ndtest_id,
981 };
982
983 static void ndtest_release(struct device *dev)
984 {
985         struct ndtest_priv *p = to_ndtest_priv(dev);
986
987         kfree(p);
988 }
989
990 static void cleanup_devices(void)
991 {
992         int i;
993
994         for (i = 0; i < NUM_INSTANCES; i++)
995                 if (instances[i])
996                         platform_device_unregister(&instances[i]->pdev);
997
998         nfit_test_teardown();
999
1000         if (ndtest_pool)
1001                 gen_pool_destroy(ndtest_pool);
1002
1003
1004         if (ndtest_dimm_class)
1005                 class_destroy(ndtest_dimm_class);
1006 }
1007
1008 static __init int ndtest_init(void)
1009 {
1010         int rc, i;
1011
1012         pmem_test();
1013         libnvdimm_test();
1014         device_dax_test();
1015         dax_pmem_test();
1016         dax_pmem_core_test();
1017 #ifdef CONFIG_DEV_DAX_PMEM_COMPAT
1018         dax_pmem_compat_test();
1019 #endif
1020
1021         nfit_test_setup(ndtest_resource_lookup, NULL);
1022
1023         ndtest_dimm_class = class_create(THIS_MODULE, "nfit_test_dimm");
1024         if (IS_ERR(ndtest_dimm_class)) {
1025                 rc = PTR_ERR(ndtest_dimm_class);
1026                 goto err_register;
1027         }
1028
1029         ndtest_pool = gen_pool_create(ilog2(SZ_4M), NUMA_NO_NODE);
1030         if (!ndtest_pool) {
1031                 rc = -ENOMEM;
1032                 goto err_register;
1033         }
1034
1035         if (gen_pool_add(ndtest_pool, SZ_4G, SZ_4G, NUMA_NO_NODE)) {
1036                 rc = -ENOMEM;
1037                 goto err_register;
1038         }
1039
1040         /* Each instance can be taken as a bus, which can have multiple dimms */
1041         for (i = 0; i < NUM_INSTANCES; i++) {
1042                 struct ndtest_priv *priv;
1043                 struct platform_device *pdev;
1044
1045                 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1046                 if (!priv) {
1047                         rc = -ENOMEM;
1048                         goto err_register;
1049                 }
1050
1051                 INIT_LIST_HEAD(&priv->resources);
1052                 pdev = &priv->pdev;
1053                 pdev->name = KBUILD_MODNAME;
1054                 pdev->id = i;
1055                 pdev->dev.release = ndtest_release;
1056                 rc = platform_device_register(pdev);
1057                 if (rc) {
1058                         put_device(&pdev->dev);
1059                         goto err_register;
1060                 }
1061                 get_device(&pdev->dev);
1062
1063                 instances[i] = priv;
1064         }
1065
1066         rc = platform_driver_register(&ndtest_driver);
1067         if (rc)
1068                 goto err_register;
1069
1070         return 0;
1071
1072 err_register:
1073         pr_err("Error registering platform device\n");
1074         cleanup_devices();
1075
1076         return rc;
1077 }
1078
1079 static __exit void ndtest_exit(void)
1080 {
1081         cleanup_devices();
1082         platform_driver_unregister(&ndtest_driver);
1083 }
1084
1085 module_init(ndtest_init);
1086 module_exit(ndtest_exit);
1087 MODULE_LICENSE("GPL");
1088 MODULE_AUTHOR("IBM Corporation");