blob: 5b95043443a39ebbeb77392169292172b1382052 [file] [log] [blame]
Ross Zwisler9e853f22015-04-01 09:12:19 +02001/*
2 * Persistent Memory Driver
3 *
Dan Williams9f53f9f2015-06-09 15:33:45 -04004 * Copyright (c) 2014-2015, Intel Corporation.
Ross Zwisler9e853f22015-04-01 09:12:19 +02005 * 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>
Dan Williams32ab0a3f2015-08-01 02:16:37 -040024#include <linux/memory_hotplug.h>
Ross Zwisler9e853f22015-04-01 09:12:19 +020025#include <linux/moduleparam.h>
Dan Williams32ab0a3f2015-08-01 02:16:37 -040026#include <linux/vmalloc.h>
Ross Zwisler9e853f22015-04-01 09:12:19 +020027#include <linux/slab.h>
Ross Zwisler61031952015-06-25 03:08:39 -040028#include <linux/pmem.h>
Dan Williams9f53f9f2015-06-09 15:33:45 -040029#include <linux/nd.h>
Vishal Verma0caeef62015-12-24 19:21:43 -070030#include "nd-core.h"
Dan Williams32ab0a3f2015-08-01 02:16:37 -040031#include "pfn.h"
Dan Williams9f53f9f2015-06-09 15:33:45 -040032#include "nd.h"
Ross Zwisler9e853f22015-04-01 09:12:19 +020033
34struct pmem_device {
35 struct request_queue *pmem_queue;
36 struct gendisk *pmem_disk;
Dan Williams32ab0a3f2015-08-01 02:16:37 -040037 struct nd_namespace_common *ndns;
Ross Zwisler9e853f22015-04-01 09:12:19 +020038
39 /* One contiguous memory region per device */
40 phys_addr_t phys_addr;
Dan Williams32ab0a3f2015-08-01 02:16:37 -040041 /* when non-zero this device is hosting a 'pfn' instance */
42 phys_addr_t data_offset;
Ross Zwisler61031952015-06-25 03:08:39 -040043 void __pmem *virt_addr;
Ross Zwisler9e853f22015-04-01 09:12:19 +020044 size_t size;
45};
46
47static int pmem_major;
Ross Zwisler9e853f22015-04-01 09:12:19 +020048
49static void pmem_do_bvec(struct pmem_device *pmem, struct page *page,
50 unsigned int len, unsigned int off, int rw,
51 sector_t sector)
52{
53 void *mem = kmap_atomic(page);
Dan Williams32ab0a3f2015-08-01 02:16:37 -040054 phys_addr_t pmem_off = sector * 512 + pmem->data_offset;
Ross Zwisler61031952015-06-25 03:08:39 -040055 void __pmem *pmem_addr = pmem->virt_addr + pmem_off;
Ross Zwisler9e853f22015-04-01 09:12:19 +020056
57 if (rw == READ) {
Ross Zwisler61031952015-06-25 03:08:39 -040058 memcpy_from_pmem(mem + off, pmem_addr, len);
Ross Zwisler9e853f22015-04-01 09:12:19 +020059 flush_dcache_page(page);
60 } else {
61 flush_dcache_page(page);
Ross Zwisler61031952015-06-25 03:08:39 -040062 memcpy_to_pmem(pmem_addr, mem + off, len);
Ross Zwisler9e853f22015-04-01 09:12:19 +020063 }
64
65 kunmap_atomic(mem);
66}
67
Jens Axboedece1632015-11-05 10:41:16 -070068static blk_qc_t pmem_make_request(struct request_queue *q, struct bio *bio)
Ross Zwisler9e853f22015-04-01 09:12:19 +020069{
Dan Williamsf0dc0892015-05-16 12:28:53 -040070 bool do_acct;
71 unsigned long start;
Dan Williamsedc870e2015-05-16 12:28:51 -040072 struct bio_vec bvec;
73 struct bvec_iter iter;
Ross Zwisler9e853f22015-04-01 09:12:19 +020074 struct block_device *bdev = bio->bi_bdev;
75 struct pmem_device *pmem = bdev->bd_disk->private_data;
Ross Zwisler9e853f22015-04-01 09:12:19 +020076
Dan Williamsf0dc0892015-05-16 12:28:53 -040077 do_acct = nd_iostat_start(bio, &start);
Dan Williamsedc870e2015-05-16 12:28:51 -040078 bio_for_each_segment(bvec, bio, iter)
Ross Zwisler9e853f22015-04-01 09:12:19 +020079 pmem_do_bvec(pmem, bvec.bv_page, bvec.bv_len, bvec.bv_offset,
Dan Williamsedc870e2015-05-16 12:28:51 -040080 bio_data_dir(bio), iter.bi_sector);
Dan Williamsf0dc0892015-05-16 12:28:53 -040081 if (do_acct)
82 nd_iostat_end(bio, start);
Ross Zwisler61031952015-06-25 03:08:39 -040083
84 if (bio_data_dir(bio))
85 wmb_pmem();
86
Christoph Hellwig4246a0b2015-07-20 15:29:37 +020087 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -070088 return BLK_QC_T_NONE;
Ross Zwisler9e853f22015-04-01 09:12:19 +020089}
90
91static int pmem_rw_page(struct block_device *bdev, sector_t sector,
92 struct page *page, int rw)
93{
94 struct pmem_device *pmem = bdev->bd_disk->private_data;
95
96 pmem_do_bvec(pmem, page, PAGE_CACHE_SIZE, 0, rw, sector);
Ross Zwislerba8fe0f2015-09-16 14:52:21 -060097 if (rw & WRITE)
98 wmb_pmem();
Ross Zwisler9e853f22015-04-01 09:12:19 +020099 page_endio(page, rw & WRITE, 0);
100
101 return 0;
102}
103
104static long pmem_direct_access(struct block_device *bdev, sector_t sector,
Dan Williamscb389b92015-08-07 17:41:00 -0400105 void __pmem **kaddr, unsigned long *pfn)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200106{
107 struct pmem_device *pmem = bdev->bd_disk->private_data;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400108 resource_size_t offset = sector * 512 + pmem->data_offset;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200109
Ross Zwislere2e05392015-08-18 13:55:41 -0600110 *kaddr = pmem->virt_addr + offset;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200111 *pfn = (pmem->phys_addr + offset) >> PAGE_SHIFT;
112
Dan Williams589e75d2015-10-24 19:55:58 -0700113 return pmem->size - offset;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200114}
115
116static const struct block_device_operations pmem_fops = {
117 .owner = THIS_MODULE,
118 .rw_page = pmem_rw_page,
119 .direct_access = pmem_direct_access,
Dan Williams58138822015-06-23 20:08:34 -0400120 .revalidate_disk = nvdimm_revalidate_disk,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200121};
122
Dan Williams9f53f9f2015-06-09 15:33:45 -0400123static struct pmem_device *pmem_alloc(struct device *dev,
124 struct resource *res, int id)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200125{
126 struct pmem_device *pmem;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200127
Christoph Hellwig708ab622015-08-10 23:07:08 -0400128 pmem = devm_kzalloc(dev, sizeof(*pmem), GFP_KERNEL);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200129 if (!pmem)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400130 return ERR_PTR(-ENOMEM);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200131
132 pmem->phys_addr = res->start;
133 pmem->size = resource_size(res);
Dan Williams96601ad2015-08-24 18:29:38 -0400134 if (!arch_has_wmb_pmem())
Ross Zwisler61031952015-06-25 03:08:39 -0400135 dev_warn(dev, "unable to guarantee persistence of writes\n");
Ross Zwisler9e853f22015-04-01 09:12:19 +0200136
Christoph Hellwig708ab622015-08-10 23:07:08 -0400137 if (!devm_request_mem_region(dev, pmem->phys_addr, pmem->size,
138 dev_name(dev))) {
Dan Williams9f53f9f2015-06-09 15:33:45 -0400139 dev_warn(dev, "could not reserve region [0x%pa:0x%zx]\n",
140 &pmem->phys_addr, pmem->size);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400141 return ERR_PTR(-EBUSY);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200142 }
143
Dan Williamsb36f4762015-09-15 02:42:20 -0400144 if (pmem_should_map_pages(dev))
145 pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, res);
146 else
Dan Williamsa6393152015-09-15 02:14:03 -0400147 pmem->virt_addr = (void __pmem *) devm_memremap(dev,
148 pmem->phys_addr, pmem->size,
149 ARCH_MEMREMAP_PMEM);
Dan Williamsb36f4762015-09-15 02:42:20 -0400150
151 if (IS_ERR(pmem->virt_addr))
152 return (void __force *) pmem->virt_addr;
Dan Williams8c2f7e82015-06-25 04:20:04 -0400153
154 return pmem;
155}
156
157static void pmem_detach_disk(struct pmem_device *pmem)
158{
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400159 if (!pmem->pmem_disk)
160 return;
161
Dan Williams8c2f7e82015-06-25 04:20:04 -0400162 del_gendisk(pmem->pmem_disk);
163 put_disk(pmem->pmem_disk);
164 blk_cleanup_queue(pmem->pmem_queue);
165}
166
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400167static int pmem_attach_disk(struct device *dev,
168 struct nd_namespace_common *ndns, struct pmem_device *pmem)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400169{
Dan Williams538ea4a2015-10-05 20:35:56 -0400170 int nid = dev_to_node(dev);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400171 struct gendisk *disk;
Vishal Verma0caeef62015-12-24 19:21:43 -0700172 int ret;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200173
Dan Williams538ea4a2015-10-05 20:35:56 -0400174 pmem->pmem_queue = blk_alloc_queue_node(GFP_KERNEL, nid);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200175 if (!pmem->pmem_queue)
Dan Williams8c2f7e82015-06-25 04:20:04 -0400176 return -ENOMEM;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200177
178 blk_queue_make_request(pmem->pmem_queue, pmem_make_request);
Vishal Verma6b474962015-07-23 11:58:48 -0600179 blk_queue_physical_block_size(pmem->pmem_queue, PAGE_SIZE);
Dan Williams43d3fa32015-05-16 12:28:50 -0400180 blk_queue_max_hw_sectors(pmem->pmem_queue, UINT_MAX);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200181 blk_queue_bounce_limit(pmem->pmem_queue, BLK_BOUNCE_ANY);
Dan Williams0f51c4f2015-05-16 12:28:54 -0400182 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, pmem->pmem_queue);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200183
Dan Williams538ea4a2015-10-05 20:35:56 -0400184 disk = alloc_disk_node(0, nid);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400185 if (!disk) {
186 blk_cleanup_queue(pmem->pmem_queue);
187 return -ENOMEM;
188 }
Ross Zwisler9e853f22015-04-01 09:12:19 +0200189
Ross Zwisler9e853f22015-04-01 09:12:19 +0200190 disk->major = pmem_major;
Dan Williams9f53f9f2015-06-09 15:33:45 -0400191 disk->first_minor = 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200192 disk->fops = &pmem_fops;
193 disk->private_data = pmem;
194 disk->queue = pmem->pmem_queue;
195 disk->flags = GENHD_FL_EXT_DEVT;
Vishal Verma5212e112015-06-25 04:20:32 -0400196 nvdimm_namespace_disk_name(ndns, disk->disk_name);
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400197 disk->driverfs_dev = dev;
198 set_capacity(disk, (pmem->size - pmem->data_offset) / 512);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200199 pmem->pmem_disk = disk;
200
Vishal Verma0caeef62015-12-24 19:21:43 -0700201 ret = nvdimm_namespace_add_poison(disk, pmem->data_offset, ndns);
202 if (ret)
203 return ret;
204
Ross Zwisler9e853f22015-04-01 09:12:19 +0200205 add_disk(disk);
Dan Williams58138822015-06-23 20:08:34 -0400206 revalidate_disk(disk);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200207
Dan Williams8c2f7e82015-06-25 04:20:04 -0400208 return 0;
209}
Ross Zwisler9e853f22015-04-01 09:12:19 +0200210
Dan Williams8c2f7e82015-06-25 04:20:04 -0400211static int pmem_rw_bytes(struct nd_namespace_common *ndns,
212 resource_size_t offset, void *buf, size_t size, int rw)
213{
214 struct pmem_device *pmem = dev_get_drvdata(ndns->claim);
215
216 if (unlikely(offset + size > pmem->size)) {
217 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
218 return -EFAULT;
219 }
220
221 if (rw == READ)
Ross Zwisler61031952015-06-25 03:08:39 -0400222 memcpy_from_pmem(buf, pmem->virt_addr + offset, size);
223 else {
224 memcpy_to_pmem(pmem->virt_addr + offset, buf, size);
225 wmb_pmem();
226 }
Dan Williams8c2f7e82015-06-25 04:20:04 -0400227
228 return 0;
229}
230
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400231static int nd_pfn_init(struct nd_pfn *nd_pfn)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200232{
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400233 struct nd_pfn_sb *pfn_sb = kzalloc(sizeof(*pfn_sb), GFP_KERNEL);
234 struct pmem_device *pmem = dev_get_drvdata(&nd_pfn->dev);
235 struct nd_namespace_common *ndns = nd_pfn->ndns;
236 struct nd_region *nd_region;
237 unsigned long npfns;
238 phys_addr_t offset;
239 u64 checksum;
240 int rc;
241
242 if (!pfn_sb)
243 return -ENOMEM;
244
245 nd_pfn->pfn_sb = pfn_sb;
246 rc = nd_pfn_validate(nd_pfn);
247 if (rc == 0 || rc == -EBUSY)
248 return rc;
249
250 /* section alignment for simple hotplug */
251 if (nvdimm_namespace_capacity(ndns) < ND_PFN_ALIGN
252 || pmem->phys_addr & ND_PFN_MASK)
253 return -ENODEV;
254
255 nd_region = to_nd_region(nd_pfn->dev.parent);
256 if (nd_region->ro) {
257 dev_info(&nd_pfn->dev,
258 "%s is read-only, unable to init metadata\n",
259 dev_name(&nd_region->dev));
260 goto err;
261 }
262
263 memset(pfn_sb, 0, sizeof(*pfn_sb));
264 npfns = (pmem->size - SZ_8K) / SZ_4K;
265 /*
266 * Note, we use 64 here for the standard size of struct page,
267 * debugging options may cause it to be larger in which case the
268 * implementation will limit the pfns advertised through
269 * ->direct_access() to those that are included in the memmap.
270 */
271 if (nd_pfn->mode == PFN_MODE_PMEM)
272 offset = ALIGN(SZ_8K + 64 * npfns, PMD_SIZE);
273 else if (nd_pfn->mode == PFN_MODE_RAM)
274 offset = SZ_8K;
275 else
276 goto err;
277
278 npfns = (pmem->size - offset) / SZ_4K;
279 pfn_sb->mode = cpu_to_le32(nd_pfn->mode);
280 pfn_sb->dataoff = cpu_to_le64(offset);
281 pfn_sb->npfns = cpu_to_le64(npfns);
282 memcpy(pfn_sb->signature, PFN_SIG, PFN_SIG_LEN);
283 memcpy(pfn_sb->uuid, nd_pfn->uuid, 16);
284 pfn_sb->version_major = cpu_to_le16(1);
285 checksum = nd_sb_checksum((struct nd_gen_sb *) pfn_sb);
286 pfn_sb->checksum = cpu_to_le64(checksum);
287
288 rc = nvdimm_write_bytes(ndns, SZ_4K, pfn_sb, sizeof(*pfn_sb));
289 if (rc)
290 goto err;
291
292 return 0;
293 err:
294 nd_pfn->pfn_sb = NULL;
295 kfree(pfn_sb);
296 return -ENXIO;
297}
298
299static int nvdimm_namespace_detach_pfn(struct nd_namespace_common *ndns)
300{
301 struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim);
302 struct pmem_device *pmem;
303
304 /* free pmem disk */
305 pmem = dev_get_drvdata(&nd_pfn->dev);
306 pmem_detach_disk(pmem);
307
308 /* release nd_pfn resources */
309 kfree(nd_pfn->pfn_sb);
310 nd_pfn->pfn_sb = NULL;
311
312 return 0;
313}
314
315static int nvdimm_namespace_attach_pfn(struct nd_namespace_common *ndns)
316{
317 struct nd_namespace_io *nsio = to_nd_namespace_io(&ndns->dev);
318 struct nd_pfn *nd_pfn = to_nd_pfn(ndns->claim);
319 struct device *dev = &nd_pfn->dev;
320 struct vmem_altmap *altmap;
321 struct nd_region *nd_region;
322 struct nd_pfn_sb *pfn_sb;
323 struct pmem_device *pmem;
324 phys_addr_t offset;
325 int rc;
326
327 if (!nd_pfn->uuid || !nd_pfn->ndns)
328 return -ENODEV;
329
330 nd_region = to_nd_region(dev->parent);
331 rc = nd_pfn_init(nd_pfn);
332 if (rc)
333 return rc;
334
335 if (PAGE_SIZE != SZ_4K) {
336 dev_err(dev, "only supported on systems with 4K PAGE_SIZE\n");
337 return -ENXIO;
338 }
339 if (nsio->res.start & ND_PFN_MASK) {
340 dev_err(dev, "%s not memory hotplug section aligned\n",
341 dev_name(&ndns->dev));
342 return -ENXIO;
343 }
344
345 pfn_sb = nd_pfn->pfn_sb;
346 offset = le64_to_cpu(pfn_sb->dataoff);
347 nd_pfn->mode = le32_to_cpu(nd_pfn->pfn_sb->mode);
348 if (nd_pfn->mode == PFN_MODE_RAM) {
349 if (offset != SZ_8K)
350 return -EINVAL;
351 nd_pfn->npfns = le64_to_cpu(pfn_sb->npfns);
352 altmap = NULL;
353 } else {
354 rc = -ENXIO;
355 goto err;
356 }
357
358 /* establish pfn range for lookup, and switch to direct map */
359 pmem = dev_get_drvdata(dev);
Dan Williamsa6393152015-09-15 02:14:03 -0400360 devm_memunmap(dev, (void __force *) pmem->virt_addr);
361 pmem->virt_addr = (void __pmem *) devm_memremap_pages(dev, &nsio->res);
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400362 if (IS_ERR(pmem->virt_addr)) {
363 rc = PTR_ERR(pmem->virt_addr);
364 goto err;
365 }
366
367 /* attach pmem disk in "pfn-mode" */
368 pmem->data_offset = offset;
369 rc = pmem_attach_disk(dev, ndns, pmem);
370 if (rc)
371 goto err;
372
373 return rc;
374 err:
375 nvdimm_namespace_detach_pfn(ndns);
376 return rc;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200377}
378
Dan Williams9f53f9f2015-06-09 15:33:45 -0400379static int nd_pmem_probe(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200380{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400381 struct nd_region *nd_region = to_nd_region(dev->parent);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400382 struct nd_namespace_common *ndns;
383 struct nd_namespace_io *nsio;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200384 struct pmem_device *pmem;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200385
Dan Williams8c2f7e82015-06-25 04:20:04 -0400386 ndns = nvdimm_namespace_common_probe(dev);
387 if (IS_ERR(ndns))
388 return PTR_ERR(ndns);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400389
Dan Williams8c2f7e82015-06-25 04:20:04 -0400390 nsio = to_nd_namespace_io(&ndns->dev);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400391 pmem = pmem_alloc(dev, &nsio->res, nd_region->id);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200392 if (IS_ERR(pmem))
393 return PTR_ERR(pmem);
394
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400395 pmem->ndns = ndns;
Dan Williams9f53f9f2015-06-09 15:33:45 -0400396 dev_set_drvdata(dev, pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400397 ndns->rw_bytes = pmem_rw_bytes;
Christoph Hellwig708ab622015-08-10 23:07:08 -0400398
Dan Williams8c2f7e82015-06-25 04:20:04 -0400399 if (is_nd_btt(dev))
Christoph Hellwig708ab622015-08-10 23:07:08 -0400400 return nvdimm_namespace_attach_btt(ndns);
401
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400402 if (is_nd_pfn(dev))
403 return nvdimm_namespace_attach_pfn(ndns);
404
405 if (nd_btt_probe(ndns, pmem) == 0) {
Dan Williams8c2f7e82015-06-25 04:20:04 -0400406 /* we'll come back as btt-pmem */
Christoph Hellwig708ab622015-08-10 23:07:08 -0400407 return -ENXIO;
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400408 }
409
410 if (nd_pfn_probe(ndns, pmem) == 0) {
411 /* we'll come back as pfn-pmem */
412 return -ENXIO;
413 }
414
415 return pmem_attach_disk(dev, ndns, pmem);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200416}
417
Dan Williams9f53f9f2015-06-09 15:33:45 -0400418static int nd_pmem_remove(struct device *dev)
Ross Zwisler9e853f22015-04-01 09:12:19 +0200419{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400420 struct pmem_device *pmem = dev_get_drvdata(dev);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200421
Dan Williams8c2f7e82015-06-25 04:20:04 -0400422 if (is_nd_btt(dev))
Dan Williams32ab0a3f2015-08-01 02:16:37 -0400423 nvdimm_namespace_detach_btt(pmem->ndns);
424 else if (is_nd_pfn(dev))
425 nvdimm_namespace_detach_pfn(pmem->ndns);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400426 else
427 pmem_detach_disk(pmem);
Dan Williams8c2f7e82015-06-25 04:20:04 -0400428
Ross Zwisler9e853f22015-04-01 09:12:19 +0200429 return 0;
430}
431
Dan Williams9f53f9f2015-06-09 15:33:45 -0400432MODULE_ALIAS("pmem");
433MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_IO);
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400434MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_PMEM);
Dan Williams9f53f9f2015-06-09 15:33:45 -0400435static struct nd_device_driver nd_pmem_driver = {
436 .probe = nd_pmem_probe,
437 .remove = nd_pmem_remove,
438 .drv = {
439 .name = "nd_pmem",
Ross Zwisler9e853f22015-04-01 09:12:19 +0200440 },
Dan Williamsbf9bccc2015-06-17 17:14:46 -0400441 .type = ND_DRIVER_NAMESPACE_IO | ND_DRIVER_NAMESPACE_PMEM,
Ross Zwisler9e853f22015-04-01 09:12:19 +0200442};
443
444static int __init pmem_init(void)
445{
446 int error;
447
448 pmem_major = register_blkdev(0, "pmem");
449 if (pmem_major < 0)
450 return pmem_major;
451
Dan Williams9f53f9f2015-06-09 15:33:45 -0400452 error = nd_driver_register(&nd_pmem_driver);
453 if (error) {
Ross Zwisler9e853f22015-04-01 09:12:19 +0200454 unregister_blkdev(pmem_major, "pmem");
Dan Williams9f53f9f2015-06-09 15:33:45 -0400455 return error;
456 }
457
458 return 0;
Ross Zwisler9e853f22015-04-01 09:12:19 +0200459}
460module_init(pmem_init);
461
462static void pmem_exit(void)
463{
Dan Williams9f53f9f2015-06-09 15:33:45 -0400464 driver_unregister(&nd_pmem_driver.drv);
Ross Zwisler9e853f22015-04-01 09:12:19 +0200465 unregister_blkdev(pmem_major, "pmem");
466}
467module_exit(pmem_exit);
468
469MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
470MODULE_LICENSE("GPL v2");