libnvdimm, btt: add support for blk integrity
[linux-2.6-block.git] / drivers / nvdimm / blk.c
1 /*
2  * NVDIMM Block Window Driver
3  * Copyright (c) 2014, Intel Corporation.
4  *
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.
8  *
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
12  * more details.
13  */
14
15 #include <linux/blkdev.h>
16 #include <linux/fs.h>
17 #include <linux/genhd.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/nd.h>
21 #include <linux/sizes.h>
22 #include "nd.h"
23
24 struct nd_blk_device {
25         struct request_queue *queue;
26         struct gendisk *disk;
27         struct nd_namespace_blk *nsblk;
28         struct nd_blk_region *ndbr;
29         size_t disk_size;
30 };
31
32 static int nd_blk_major;
33
34 static resource_size_t to_dev_offset(struct nd_namespace_blk *nsblk,
35                                 resource_size_t ns_offset, unsigned int len)
36 {
37         int i;
38
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,
43                                         "illegal request\n");
44                                 return SIZE_MAX;
45                         }
46                         return nsblk->res[i]->start + ns_offset;
47                 }
48                 ns_offset -= resource_size(nsblk->res[i]);
49         }
50
51         dev_WARN_ONCE(&nsblk->common.dev, 1, "request out of range\n");
52         return SIZE_MAX;
53 }
54
55 static void nd_blk_make_request(struct request_queue *q, struct bio *bio)
56 {
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;
63         struct bio_vec bvec;
64         int err = 0, rw;
65
66         blk_dev = disk->private_data;
67         nsblk = blk_dev->nsblk;
68         ndbr = blk_dev->ndbr;
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;
73                 void *iobuf;
74
75                 BUG_ON(len > PAGE_SIZE);
76
77                 dev_offset = to_dev_offset(nsblk,
78                                 iter.bi_sector << SECTOR_SHIFT, len);
79                 if (dev_offset == SIZE_MAX) {
80                         err = -EIO;
81                         goto out;
82                 }
83
84                 iobuf = kmap_atomic(bvec.bv_page);
85                 err = ndbr->do_io(ndbr, dev_offset, iobuf + bvec.bv_offset,
86                                 len, rw);
87                 kunmap_atomic(iobuf);
88                 if (err)
89                         goto out;
90         }
91
92  out:
93         bio_endio(bio, err);
94 }
95
96 static int nd_blk_rw_bytes(struct nd_namespace_common *ndns,
97                 resource_size_t offset, void *iobuf, size_t n, int rw)
98 {
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;
103
104         dev_offset = to_dev_offset(nsblk, offset, n);
105
106         if (unlikely(offset + n > blk_dev->disk_size)) {
107                 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
108                 return -EFAULT;
109         }
110
111         if (dev_offset == SIZE_MAX)
112                 return -EIO;
113
114         return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
115 }
116
117 static const struct block_device_operations nd_blk_fops = {
118         .owner = THIS_MODULE,
119 };
120
121 static int nd_blk_attach_disk(struct nd_namespace_common *ndns,
122                 struct nd_blk_device *blk_dev)
123 {
124         struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
125         struct gendisk *disk;
126
127         blk_dev->queue = blk_alloc_queue(GFP_KERNEL);
128         if (!blk_dev->queue)
129                 return -ENOMEM;
130
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);
136
137         disk = blk_dev->disk = alloc_disk(0);
138         if (!disk) {
139                 blk_cleanup_queue(blk_dev->queue);
140                 return -ENOMEM;
141         }
142
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);
152         add_disk(disk);
153
154         return 0;
155 }
156
157 static int nd_blk_probe(struct device *dev)
158 {
159         struct nd_namespace_common *ndns;
160         struct nd_blk_device *blk_dev;
161         int rc;
162
163         ndns = nvdimm_namespace_common_probe(dev);
164         if (IS_ERR(ndns))
165                 return PTR_ERR(ndns);
166
167         blk_dev = kzalloc(sizeof(*blk_dev), GFP_KERNEL);
168         if (!blk_dev)
169                 return -ENOMEM;
170
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);
175
176         ndns->rw_bytes = nd_blk_rw_bytes;
177         if (is_nd_btt(dev))
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 */
181                 rc = -ENXIO;
182         } else
183                 rc = nd_blk_attach_disk(ndns, blk_dev);
184         if (rc)
185                 kfree(blk_dev);
186         return rc;
187 }
188
189 static void nd_blk_detach_disk(struct nd_blk_device *blk_dev)
190 {
191         del_gendisk(blk_dev->disk);
192         put_disk(blk_dev->disk);
193         blk_cleanup_queue(blk_dev->queue);
194 }
195
196 static int nd_blk_remove(struct device *dev)
197 {
198         struct nd_blk_device *blk_dev = dev_get_drvdata(dev);
199
200         if (is_nd_btt(dev))
201                 nvdimm_namespace_detach_btt(to_nd_btt(dev)->ndns);
202         else
203                 nd_blk_detach_disk(blk_dev);
204         kfree(blk_dev);
205
206         return 0;
207 }
208
209 static struct nd_device_driver nd_blk_driver = {
210         .probe = nd_blk_probe,
211         .remove = nd_blk_remove,
212         .drv = {
213                 .name = "nd_blk",
214         },
215         .type = ND_DRIVER_NAMESPACE_BLK,
216 };
217
218 static int __init nd_blk_init(void)
219 {
220         int rc;
221
222         rc = register_blkdev(0, "nd_blk");
223         if (rc < 0)
224                 return rc;
225
226         nd_blk_major = rc;
227         rc = nd_driver_register(&nd_blk_driver);
228
229         if (rc < 0)
230                 unregister_blkdev(nd_blk_major, "nd_blk");
231
232         return rc;
233 }
234
235 static void __exit nd_blk_exit(void)
236 {
237         driver_unregister(&nd_blk_driver.drv);
238         unregister_blkdev(nd_blk_major, "nd_blk");
239 }
240
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);