2 * NVDIMM Block Window Driver
3 * Copyright (c) 2014, Intel Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 #include <linux/blkdev.h>
17 #include <linux/genhd.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
21 #include <linux/sizes.h>
24 struct nd_blk_device {
25 struct request_queue *queue;
27 struct nd_namespace_blk *nsblk;
28 struct nd_blk_region *ndbr;
32 static int nd_blk_major;
34 static resource_size_t to_dev_offset(struct nd_namespace_blk *nsblk,
35 resource_size_t ns_offset, unsigned int len)
39 for (i = 0; i < nsblk->num_resources; i++) {
40 if (ns_offset < resource_size(nsblk->res[i])) {
41 if (ns_offset + len > resource_size(nsblk->res[i])) {
42 dev_WARN_ONCE(&nsblk->common.dev, 1,
46 return nsblk->res[i]->start + ns_offset;
48 ns_offset -= resource_size(nsblk->res[i]);
51 dev_WARN_ONCE(&nsblk->common.dev, 1, "request out of range\n");
55 static void nd_blk_make_request(struct request_queue *q, struct bio *bio)
57 struct block_device *bdev = bio->bi_bdev;
58 struct gendisk *disk = bdev->bd_disk;
59 struct nd_namespace_blk *nsblk;
60 struct nd_blk_device *blk_dev;
61 struct nd_blk_region *ndbr;
62 struct bvec_iter iter;
66 blk_dev = disk->private_data;
67 nsblk = blk_dev->nsblk;
69 rw = bio_data_dir(bio);
70 bio_for_each_segment(bvec, bio, iter) {
71 unsigned int len = bvec.bv_len;
72 resource_size_t dev_offset;
75 BUG_ON(len > PAGE_SIZE);
77 dev_offset = to_dev_offset(nsblk,
78 iter.bi_sector << SECTOR_SHIFT, len);
79 if (dev_offset == SIZE_MAX) {
84 iobuf = kmap_atomic(bvec.bv_page);
85 err = ndbr->do_io(ndbr, dev_offset, iobuf + bvec.bv_offset,
96 static int nd_blk_rw_bytes(struct nd_namespace_common *ndns,
97 resource_size_t offset, void *iobuf, size_t n, int rw)
99 struct nd_blk_device *blk_dev = dev_get_drvdata(ndns->claim);
100 struct nd_namespace_blk *nsblk = blk_dev->nsblk;
101 struct nd_blk_region *ndbr = blk_dev->ndbr;
102 resource_size_t dev_offset;
104 dev_offset = to_dev_offset(nsblk, offset, n);
106 if (unlikely(offset + n > blk_dev->disk_size)) {
107 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
111 if (dev_offset == SIZE_MAX)
114 return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
117 static const struct block_device_operations nd_blk_fops = {
118 .owner = THIS_MODULE,
121 static int nd_blk_attach_disk(struct nd_namespace_common *ndns,
122 struct nd_blk_device *blk_dev)
124 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
125 struct gendisk *disk;
127 blk_dev->queue = blk_alloc_queue(GFP_KERNEL);
131 blk_queue_make_request(blk_dev->queue, nd_blk_make_request);
132 blk_queue_max_hw_sectors(blk_dev->queue, UINT_MAX);
133 blk_queue_bounce_limit(blk_dev->queue, BLK_BOUNCE_ANY);
134 blk_queue_logical_block_size(blk_dev->queue, nsblk->lbasize);
135 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, blk_dev->queue);
137 disk = blk_dev->disk = alloc_disk(0);
139 blk_cleanup_queue(blk_dev->queue);
143 disk->driverfs_dev = &ndns->dev;
144 disk->major = nd_blk_major;
145 disk->first_minor = 0;
146 disk->fops = &nd_blk_fops;
147 disk->private_data = blk_dev;
148 disk->queue = blk_dev->queue;
149 disk->flags = GENHD_FL_EXT_DEVT;
150 nvdimm_namespace_disk_name(ndns, disk->disk_name);
151 set_capacity(disk, blk_dev->disk_size >> SECTOR_SHIFT);
157 static int nd_blk_probe(struct device *dev)
159 struct nd_namespace_common *ndns;
160 struct nd_blk_device *blk_dev;
163 ndns = nvdimm_namespace_common_probe(dev);
165 return PTR_ERR(ndns);
167 blk_dev = kzalloc(sizeof(*blk_dev), GFP_KERNEL);
171 blk_dev->disk_size = nvdimm_namespace_capacity(ndns);
172 blk_dev->ndbr = to_nd_blk_region(dev->parent);
173 blk_dev->nsblk = to_nd_namespace_blk(&ndns->dev);
174 dev_set_drvdata(dev, blk_dev);
176 ndns->rw_bytes = nd_blk_rw_bytes;
178 rc = nvdimm_namespace_attach_btt(ndns);
179 else if (nd_btt_probe(ndns, blk_dev) == 0) {
180 /* we'll come back as btt-blk */
183 rc = nd_blk_attach_disk(ndns, blk_dev);
189 static void nd_blk_detach_disk(struct nd_blk_device *blk_dev)
191 del_gendisk(blk_dev->disk);
192 put_disk(blk_dev->disk);
193 blk_cleanup_queue(blk_dev->queue);
196 static int nd_blk_remove(struct device *dev)
198 struct nd_blk_device *blk_dev = dev_get_drvdata(dev);
201 nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns);
203 nd_blk_detach_disk(blk_dev);
209 static struct nd_device_driver nd_blk_driver = {
210 .probe = nd_blk_probe,
211 .remove = nd_blk_remove,
215 .type = ND_DRIVER_NAMESPACE_BLK,
218 static int __init nd_blk_init(void)
222 rc = register_blkdev(0, "nd_blk");
227 rc = nd_driver_register(&nd_blk_driver);
230 unregister_blkdev(nd_blk_major, "nd_blk");
235 static void __exit nd_blk_exit(void)
237 driver_unregister(&nd_blk_driver.drv);
238 unregister_blkdev(nd_blk_major, "nd_blk");
241 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
242 MODULE_LICENSE("GPL v2");
243 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK);
244 module_init(nd_blk_init);
245 module_exit(nd_blk_exit);