libnvdimm, pmem: use ->queuedata for driver private data
[cascardo/linux.git] / drivers / nvdimm / pmem.c
1 /*
2  * Persistent Memory Driver
3  *
4  * Copyright (c) 2014-2015, Intel Corporation.
5  * Copyright (c) 2015, Christoph Hellwig <hch@lst.de>.
6  * Copyright (c) 2015, Boaz Harrosh <boaz@plexistor.com>.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  */
17
18 #include <asm/cacheflush.h>
19 #include <linux/blkdev.h>
20 #include <linux/hdreg.h>
21 #include <linux/init.h>
22 #include <linux/platform_device.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/badblocks.h>
26 #include <linux/memremap.h>
27 #include <linux/vmalloc.h>
28 #include <linux/pfn_t.h>
29 #include <linux/slab.h>
30 #include <linux/pmem.h>
31 #include <linux/nd.h>
32 #include "pfn.h"
33 #include "nd.h"
34
35 struct pmem_device {
36         struct request_queue    *pmem_queue;
37         struct gendisk          *pmem_disk;
38
39         /* One contiguous memory region per device */
40         phys_addr_t             phys_addr;
41         /* when non-zero this device is hosting a 'pfn' instance */
42         phys_addr_t             data_offset;
43         u64                     pfn_flags;
44         void __pmem             *virt_addr;
45         /* immutable base size of the namespace */
46         size_t                  size;
47         /* trim size when namespace capacity has been section aligned */
48         u32                     pfn_pad;
49         struct badblocks        bb;
50 };
51
52 static bool is_bad_pmem(struct badblocks *bb, sector_t sector, unsigned int len)
53 {
54         if (bb->count) {
55                 sector_t first_bad;
56                 int num_bad;
57
58                 return !!badblocks_check(bb, sector, len / 512, &first_bad,
59                                 &num_bad);
60         }
61
62         return false;
63 }
64
65 static void pmem_clear_poison(struct pmem_device *pmem, phys_addr_t offset,
66                 unsigned int len)
67 {
68         struct device *dev = disk_to_dev(pmem->pmem_disk);
69         sector_t sector;
70         long cleared;
71
72         sector = (offset - pmem->data_offset) / 512;
73         cleared = nvdimm_clear_poison(dev, pmem->phys_addr + offset, len);
74
75         if (cleared > 0 && cleared / 512) {
76                 dev_dbg(dev, "%s: %llx clear %ld sector%s\n",
77                                 __func__, (unsigned long long) sector,
78                                 cleared / 512, cleared / 512 > 1 ? "s" : "");
79                 badblocks_clear(&pmem->bb, sector, cleared / 512);
80         }
81         invalidate_pmem(pmem->virt_addr + offset, len);
82 }
83
84 static int pmem_do_bvec(struct pmem_device *pmem, struct page *page,
85                         unsigned int len, unsigned int off, int rw,
86                         sector_t sector)
87 {
88         int rc = 0;
89         bool bad_pmem = false;
90         void *mem = kmap_atomic(page);
91         phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
92         void __pmem *pmem_addr = pmem->virt_addr + pmem_off;
93
94         if (unlikely(is_bad_pmem(&pmem->bb, sector, len)))
95                 bad_pmem = true;
96
97         if (rw == READ) {
98                 if (unlikely(bad_pmem))
99                         rc = -EIO;
100                 else {
101                         rc = memcpy_from_pmem(mem + off, pmem_addr, len);
102                         flush_dcache_page(page);
103                 }
104         } else {
105                 /*
106                  * Note that we write the data both before and after
107                  * clearing poison.  The write before clear poison
108                  * handles situations where the latest written data is
109                  * preserved and the clear poison operation simply marks
110                  * the address range as valid without changing the data.
111                  * In this case application software can assume that an
112                  * interrupted write will either return the new good
113                  * data or an error.
114                  *
115                  * However, if pmem_clear_poison() leaves the data in an
116                  * indeterminate state we need to perform the write
117                  * after clear poison.
118                  */
119                 flush_dcache_page(page);
120                 memcpy_to_pmem(pmem_addr, mem + off, len);
121                 if (unlikely(bad_pmem)) {
122                         pmem_clear_poison(pmem, pmem_off, len);
123                         memcpy_to_pmem(pmem_addr, mem + off, len);
124                 }
125         }
126
127         kunmap_atomic(mem);
128         return rc;
129 }
130
131 static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
132 {
133         int rc = 0;
134         bool do_acct;
135         unsigned long start;
136         struct bio_vec bvec;
137         struct bvec_iter iter;
138         struct pmem_device *pmem = q->queuedata;
139
140         do_acct = nd_iostat_start(bio, &start);
141         bio_for_each_segment(bvec, bio, iter) {
142                 rc = pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len,
143                                 bvec.bv_offset, bio_data_dir(bio),
144                                 iter.bi_sector);
145                 if (rc) {
146                         bio->bi_error = rc;
147                         break;
148                 }
149         }
150         if (do_acct)
151                 nd_iostat_end(bio, start);
152
153         if (bio_data_dir(bio))
154                 wmb_pmem();
155
156         bio_endio(bio);
157         return BLK_QC_T_NONE;
158 }
159
160 static int pmem_rw_page(struct block_device *bdev, sector_t sector,
161                        struct page *page, int rw)
162 {
163         struct pmem_device *pmem = bdev->bd_queue->queuedata;
164         int rc;
165
166         rc = pmem_do_bvec(pmem, page, PAGE_SIZE, 0, rw, sector);
167         if (rw & WRITE)
168                 wmb_pmem();
169
170         /*
171          * The ->rw_page interface is subtle and tricky.  The core
172          * retries on any error, so we can only invoke page_endio() in
173          * the successful completion case.  Otherwise, we'll see crashes
174          * caused by double completion.
175          */
176         if (rc == 0)
177                 page_endio(page, rw & WRITE, 0);
178
179         return rc;
180 }
181
182 static long pmem_direct_access(struct block_device *bdev, sector_t sector,
183                       void __pmem **kaddr, pfn_t *pfn)
184 {
185         struct pmem_device *pmem = bdev->bd_queue->queuedata;
186         resource_size_t offset = sector * 512 + pmem->data_offset;
187
188         *kaddr = pmem->virt_addr + offset;
189         *pfn = phys_to_pfn_t(pmem->phys_addr + offset, pmem->pfn_flags);
190
191         return pmem->size - pmem->pfn_pad - offset;
192 }
193
194 static const struct block_device_operations pmem_fops = {
195         .owner =                THIS_MODULE,
196         .rw_page =              pmem_rw_page,
197         .direct_access =        pmem_direct_access,
198         .revalidate_disk =      nvdimm_revalidate_disk,
199 };
200
201 static struct pmem_device *pmem_alloc(struct device *dev,
202                 struct resource *res, int id)
203 {
204         struct pmem_device *pmem;
205         struct request_queue *q;
206
207         pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
208         if (!pmem)
209                 return ERR_PTR(-ENOMEM);
210
211         pmem->phys_addr = res->start;
212         pmem->size = resource_size(res);
213         if (!arch_has_wmb_pmem())
214                 dev_warn(dev, "unable to guarantee persistence of writes\n");
215
216         if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size,
217                         dev_name(dev))) {
218                 dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
219                                 &pmem->phys_addr, pmem->size);
220                 return ERR_PTR(-EBUSY);
221         }
222
223         q = blk_alloc_queue_node(GFP_KERNEL, dev_to_node(dev));
224         if (!q)
225                 return ERR_PTR(-ENOMEM);
226
227         pmem->pfn_flags = PFN_DEV;
228         if (pmem_should_map_pages(dev)) {
229                 pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, res,
230                                 &q->q_usage_counter, NULL);
231                 pmem->pfn_flags |= PFN_MAP;
232         } else
233                 pmem->virt_addr = (void __pmem *) devm_memremap(dev,
234                                 pmem->phys_addr, pmem->size,
235                                 ARCH_MEMREMAP_PMEM);
236
237         if (IS_ERR(pmem->virt_addr)) {
238                 blk_cleanup_queue(q);
239                 return (void __force *) pmem->virt_addr;
240         }
241
242         pmem->pmem_queue = q;
243         return pmem;
244 }
245
246 static void pmem_detach_disk(struct pmem_device *pmem)
247 {
248         if (!pmem->pmem_disk)
249                 return;
250
251         del_gendisk(pmem->pmem_disk);
252         put_disk(pmem->pmem_disk);
253         blk_cleanup_queue(pmem->pmem_queue);
254 }
255
256 static int pmem_attach_disk(struct device *dev,
257                 struct nd_namespace_common *ndns, struct pmem_device *pmem)
258 {
259         struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
260         int nid = dev_to_node(dev);
261         struct resource bb_res;
262         struct gendisk *disk;
263
264         blk_queue_make_request(pmem->pmem_queue, pmem_make_request);
265         blk_queue_physical_block_size(pmem->pmem_queue, PAGE_SIZE);
266         blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX);
267         blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY);
268         queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue);
269         pmem->pmem_queue->queuedata = pmem;
270
271         disk = alloc_disk_node(0, nid);
272         if (!disk) {
273                 blk_cleanup_queue(pmem->pmem_queue);
274                 return -ENOMEM;
275         }
276
277         disk->fops              = &pmem_fops;
278         disk->queue             = pmem->pmem_queue;
279         disk->flags             = GENHD_FL_EXT_DEVT;
280         nvdimm_namespace_disk_name(ndns, disk->disk_name);
281         disk->driverfs_dev = dev;
282         set_capacity(disk, (pmem->size - pmem->pfn_pad - pmem->data_offset)
283                         / 512);
284         pmem->pmem_disk = disk;
285         devm_exit_badblocks(dev, &pmem->bb);
286         if (devm_init_badblocks(dev, &pmem->bb))
287                 return -ENOMEM;
288         bb_res.start = nsio->res.start + pmem->data_offset;
289         bb_res.end = nsio->res.end;
290         if (is_nd_pfn(dev)) {
291                 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
292                 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
293
294                 bb_res.start += __le32_to_cpu(pfn_sb->start_pad);
295                 bb_res.end -= __le32_to_cpu(pfn_sb->end_trunc);
296         }
297         nvdimm_badblocks_populate(to_nd_region(dev->parent), &pmem->bb,
298                         &bb_res);
299         disk->bb = &pmem->bb;
300         add_disk(disk);
301         revalidate_disk(disk);
302
303         return 0;
304 }
305
306 static int pmem_rw_bytes(struct nd_namespace_common *ndns,
307                 resource_size_t offset, void *buf, size_t size, int rw)
308 {
309         struct pmem_device *pmem = dev_get_drvdata(ndns->claim);
310
311         if (unlikely(offset + size > pmem->size)) {
312                 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
313                 return -EFAULT;
314         }
315
316         if (rw == READ) {
317                 unsigned int sz_align = ALIGN(size + (offset & (512 - 1)), 512);
318
319                 if (unlikely(is_bad_pmem(&pmem->bb, offset / 512, sz_align)))
320                         return -EIO;
321                 return memcpy_from_pmem(buf, pmem->virt_addr + offset, size);
322         } else {
323                 memcpy_to_pmem(pmem->virt_addr + offset, buf, size);
324                 wmb_pmem();
325         }
326
327         return 0;
328 }
329
330 static int nd_pfn_init(struct nd_pfn *nd_pfn)
331 {
332         struct pmem_device *pmem = dev_get_drvdata(&nd_pfn->dev);
333         struct nd_namespace_common *ndns = nd_pfn->ndns;
334         u32 start_pad = 0, end_trunc = 0;
335         resource_size_t start, size;
336         struct nd_namespace_io *nsio;
337         struct nd_region *nd_region;
338         struct nd_pfn_sb *pfn_sb;
339         unsigned long npfns;
340         phys_addr_t offset;
341         u64 checksum;
342         int rc;
343
344         pfn_sb = devm_kzalloc(&nd_pfn->dev, sizeof(*pfn_sb), GFP_KERNEL);
345         if (!pfn_sb)
346                 return -ENOMEM;
347
348         nd_pfn->pfn_sb = pfn_sb;
349         rc = nd_pfn_validate(nd_pfn);
350         if (rc == -ENODEV)
351                 /* no info block, do init */;
352         else
353                 return rc;
354
355         nd_region = to_nd_region(nd_pfn->dev.parent);
356         if (nd_region->ro) {
357                 dev_info(&nd_pfn->dev,
358                                 "%s is read-only, unable to init metadata\n",
359                                 dev_name(&nd_region->dev));
360                 return -ENXIO;
361         }
362
363         memset(pfn_sb, 0, sizeof(*pfn_sb));
364
365         /*
366          * Check if pmem collides with 'System RAM' when section aligned and
367          * trim it accordingly
368          */
369         nsio = to_nd_namespace_io(&ndns->dev);
370         start = PHYS_SECTION_ALIGN_DOWN(nsio->res.start);
371         size = resource_size(&nsio->res);
372         if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
373                                 IORES_DESC_NONE) == REGION_MIXED) {
374
375                 start = nsio->res.start;
376                 start_pad = PHYS_SECTION_ALIGN_UP(start) - start;
377         }
378
379         start = nsio->res.start;
380         size = PHYS_SECTION_ALIGN_UP(start + size) - start;
381         if (region_intersects(start, size, IORESOURCE_SYSTEM_RAM,
382                                 IORES_DESC_NONE) == REGION_MIXED) {
383                 size = resource_size(&nsio->res);
384                 end_trunc = start + size - PHYS_SECTION_ALIGN_DOWN(start + size);
385         }
386
387         if (start_pad + end_trunc)
388                 dev_info(&nd_pfn->dev, "%s section collision, truncate %d bytes\n",
389                                 dev_name(&ndns->dev), start_pad + end_trunc);
390
391         /*
392          * Note, we use 64 here for the standard size of struct page,
393          * debugging options may cause it to be larger in which case the
394          * implementation will limit the pfns advertised through
395          * ->direct_access() to those that are included in the memmap.
396          */
397         start += start_pad;
398         npfns = (pmem->size - start_pad - end_trunc - SZ_8K) / SZ_4K;
399         if (nd_pfn->mode == PFN_MODE_PMEM)
400                 offset = ALIGN(start + SZ_8K + 64 * npfns, nd_pfn->align)
401                         - start;
402         else if (nd_pfn->mode == PFN_MODE_RAM)
403                 offset = ALIGN(start + SZ_8K, nd_pfn->align) - start;
404         else
405                 return -ENXIO;
406
407         if (offset + start_pad + end_trunc >= pmem->size) {
408                 dev_err(&nd_pfn->dev, "%s unable to satisfy requested alignment\n",
409                                 dev_name(&ndns->dev));
410                 return -ENXIO;
411         }
412
413         npfns = (pmem->size - offset - start_pad - end_trunc) / SZ_4K;
414         pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
415         pfn_sb->dataoff = cpu_to_le64(offset);
416         pfn_sb->npfns = cpu_to_le64(npfns);
417         memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN);
418         memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
419         memcpy(pfn_sb->parent_uuid, nd_dev_to_uuid(&ndns->dev), 16);
420         pfn_sb->version_major = cpu_to_le16(1);
421         pfn_sb->version_minor = cpu_to_le16(1);
422         pfn_sb->start_pad = cpu_to_le32(start_pad);
423         pfn_sb->end_trunc = cpu_to_le32(end_trunc);
424         checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
425         pfn_sb->checksum = cpu_to_le64(checksum);
426
427         return nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
428 }
429
430 static void nvdimm_namespace_detach_pfn(struct nd_pfn *nd_pfn)
431 {
432         struct pmem_device *pmem;
433
434         /* free pmem disk */
435         pmem = dev_get_drvdata(&nd_pfn->dev);
436         pmem_detach_disk(pmem);
437 }
438
439 /*
440  * We hotplug memory at section granularity, pad the reserved area from
441  * the previous section base to the namespace base address.
442  */
443 static unsigned long init_altmap_base(resource_size_t base)
444 {
445         unsigned long base_pfn = PHYS_PFN(base);
446
447         return PFN_SECTION_ALIGN_DOWN(base_pfn);
448 }
449
450 static unsigned long init_altmap_reserve(resource_size_t base)
451 {
452         unsigned long reserve = PHYS_PFN(SZ_8K);
453         unsigned long base_pfn = PHYS_PFN(base);
454
455         reserve += base_pfn - PFN_SECTION_ALIGN_DOWN(base_pfn);
456         return reserve;
457 }
458
459 static int __nvdimm_namespace_attach_pfn(struct nd_pfn *nd_pfn)
460 {
461         int rc;
462         struct resource res;
463         struct request_queue *q;
464         struct pmem_device *pmem;
465         struct vmem_altmap *altmap;
466         struct device *dev = &nd_pfn->dev;
467         struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
468         struct nd_namespace_common *ndns = nd_pfn->ndns;
469         u32 start_pad = __le32_to_cpu(pfn_sb->start_pad);
470         u32 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
471         struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
472         resource_size_t base = nsio->res.start + start_pad;
473         struct vmem_altmap __altmap = {
474                 .base_pfn = init_altmap_base(base),
475                 .reserve = init_altmap_reserve(base),
476         };
477
478         pmem = dev_get_drvdata(dev);
479         pmem->data_offset = le64_to_cpu(pfn_sb->dataoff);
480         pmem->pfn_pad = start_pad + end_trunc;
481         nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode);
482         if (nd_pfn->mode == PFN_MODE_RAM) {
483                 if (pmem->data_offset < SZ_8K)
484                         return -EINVAL;
485                 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
486                 altmap = NULL;
487         } else if (nd_pfn->mode == PFN_MODE_PMEM) {
488                 nd_pfn->npfns = (pmem->size - pmem->pfn_pad - pmem->data_offset)
489                         / PAGE_SIZE;
490                 if (le64_to_cpu(nd_pfn->pfn_sb->npfns) > nd_pfn->npfns)
491                         dev_info(&nd_pfn->dev,
492                                         "number of pfns truncated from %lld to %ld\n",
493                                         le64_to_cpu(nd_pfn->pfn_sb->npfns),
494                                         nd_pfn->npfns);
495                 altmap = & __altmap;
496                 altmap->free = PHYS_PFN(pmem->data_offset - SZ_8K);
497                 altmap->alloc = 0;
498         } else {
499                 rc = -ENXIO;
500                 goto err;
501         }
502
503         /* establish pfn range for lookup, and switch to direct map */
504         q = pmem->pmem_queue;
505         memcpy(&res, &nsio->res, sizeof(res));
506         res.start += start_pad;
507         res.end -= end_trunc;
508         devm_memunmap(dev, (void __force *) pmem->virt_addr);
509         pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, &res,
510                         &q->q_usage_counter, altmap);
511         pmem->pfn_flags |= PFN_MAP;
512         if (IS_ERR(pmem->virt_addr)) {
513                 rc = PTR_ERR(pmem->virt_addr);
514                 goto err;
515         }
516
517         /* attach pmem disk in "pfn-mode" */
518         rc = pmem_attach_disk(dev, ndns, pmem);
519         if (rc)
520                 goto err;
521
522         return rc;
523  err:
524         nvdimm_namespace_detach_pfn(nd_pfn);
525         return rc;
526
527 }
528
529 static int nvdimm_namespace_attach_pfn(struct nd_namespace_common *ndns)
530 {
531         struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim);
532         int rc;
533
534         if (!nd_pfn->uuid || !nd_pfn->ndns)
535                 return -ENODEV;
536
537         rc = nd_pfn_init(nd_pfn);
538         if (rc)
539                 return rc;
540         /* we need a valid pfn_sb before we can init a vmem_altmap */
541         return __nvdimm_namespace_attach_pfn(nd_pfn);
542 }
543
544 static int nd_pmem_probe(struct device *dev)
545 {
546         struct nd_region *nd_region = to_nd_region(dev->parent);
547         struct nd_namespace_common *ndns;
548         struct nd_namespace_io *nsio;
549         struct pmem_device *pmem;
550
551         ndns = nvdimm_namespace_common_probe(dev);
552         if (IS_ERR(ndns))
553                 return PTR_ERR(ndns);
554
555         nsio = to_nd_namespace_io(&ndns->dev);
556         pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
557         if (IS_ERR(pmem))
558                 return PTR_ERR(pmem);
559
560         dev_set_drvdata(dev, pmem);
561         ndns->rw_bytes = pmem_rw_bytes;
562         if (devm_init_badblocks(dev, &pmem->bb))
563                 return -ENOMEM;
564         nvdimm_badblocks_populate(nd_region, &pmem->bb, &nsio->res);
565
566         if (is_nd_btt(dev)) {
567                 /* btt allocates its own request_queue */
568                 blk_cleanup_queue(pmem->pmem_queue);
569                 pmem->pmem_queue = NULL;
570                 return nvdimm_namespace_attach_btt(ndns);
571         }
572
573         if (is_nd_pfn(dev))
574                 return nvdimm_namespace_attach_pfn(ndns);
575
576         if (nd_btt_probe(dev, ndns, pmem) == 0
577                         || nd_pfn_probe(dev, ndns, pmem) == 0) {
578                 /*
579                  * We'll come back as either btt-pmem, or pfn-pmem, so
580                  * drop the queue allocation for now.
581                  */
582                 blk_cleanup_queue(pmem->pmem_queue);
583                 return -ENXIO;
584         }
585
586         return pmem_attach_disk(dev, ndns, pmem);
587 }
588
589 static int nd_pmem_remove(struct device *dev)
590 {
591         struct pmem_device *pmem = dev_get_drvdata(dev);
592
593         if (is_nd_btt(dev))
594                 nvdimm_namespace_detach_btt(to_nd_btt(dev));
595         else if (is_nd_pfn(dev))
596                 nvdimm_namespace_detach_pfn(to_nd_pfn(dev));
597         else
598                 pmem_detach_disk(pmem);
599
600         return 0;
601 }
602
603 static void nd_pmem_notify(struct device *dev, enum nvdimm_event event)
604 {
605         struct nd_region *nd_region = to_nd_region(dev->parent);
606         struct pmem_device *pmem = dev_get_drvdata(dev);
607         resource_size_t offset = 0, end_trunc = 0;
608         struct nd_namespace_common *ndns;
609         struct nd_namespace_io *nsio;
610         struct resource res;
611
612         if (event != NVDIMM_REVALIDATE_POISON)
613                 return;
614
615         if (is_nd_btt(dev)) {
616                 struct nd_btt *nd_btt = to_nd_btt(dev);
617
618                 ndns = nd_btt->ndns;
619         } else if (is_nd_pfn(dev)) {
620                 struct nd_pfn *nd_pfn = to_nd_pfn(dev);
621                 struct nd_pfn_sb *pfn_sb = nd_pfn->pfn_sb;
622
623                 ndns = nd_pfn->ndns;
624                 offset = pmem->data_offset + __le32_to_cpu(pfn_sb->start_pad);
625                 end_trunc = __le32_to_cpu(pfn_sb->end_trunc);
626         } else
627                 ndns = to_ndns(dev);
628
629         nsio = to_nd_namespace_io(&ndns->dev);
630         res.start = nsio->res.start + offset;
631         res.end = nsio->res.end - end_trunc;
632         nvdimm_badblocks_populate(nd_region, &pmem->bb, &res);
633 }
634
635 MODULE_ALIAS("pmem");
636 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
637 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
638 static struct nd_device_driver nd_pmem_driver = {
639         .probe = nd_pmem_probe,
640         .remove = nd_pmem_remove,
641         .notify = nd_pmem_notify,
642         .drv = {
643                 .name = "nd_pmem",
644         },
645         .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
646 };
647
648 static int __init pmem_init(void)
649 {
650         return nd_driver_register(&nd_pmem_driver);
651 }
652 module_init(pmem_init);
653
654 static void pmem_exit(void)
655 {
656         driver_unregister(&nd_pmem_driver.drv);
657 }
658 module_exit(pmem_exit);
659
660 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
661 MODULE_LICENSE("GPL v2");