mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / nvdimm / blk.c
blob495e06d9f7e7f39013d910e4459720b61ebd90f5
1 /*
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
12 * more details.
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"
24 static u32 nsblk_meta_size(struct nd_namespace_blk *nsblk)
26 return nsblk->lbasize - ((nsblk->lbasize >= 4096) ? 4096 : 512);
29 static u32 nsblk_internal_lbasize(struct nd_namespace_blk *nsblk)
31 return roundup(nsblk->lbasize, INT_LBASIZE_ALIGNMENT);
34 static u32 nsblk_sector_size(struct nd_namespace_blk *nsblk)
36 return nsblk->lbasize - nsblk_meta_size(nsblk);
39 static resource_size_t to_dev_offset(struct nd_namespace_blk *nsblk,
40 resource_size_t ns_offset, unsigned int len)
42 int i;
44 for (i = 0; i < nsblk->num_resources; i++) {
45 if (ns_offset < resource_size(nsblk->res[i])) {
46 if (ns_offset + len > resource_size(nsblk->res[i])) {
47 dev_WARN_ONCE(&nsblk->common.dev, 1,
48 "illegal request\n");
49 return SIZE_MAX;
51 return nsblk->res[i]->start + ns_offset;
53 ns_offset -= resource_size(nsblk->res[i]);
56 dev_WARN_ONCE(&nsblk->common.dev, 1, "request out of range\n");
57 return SIZE_MAX;
60 static struct nd_blk_region *to_ndbr(struct nd_namespace_blk *nsblk)
62 struct nd_region *nd_region;
63 struct device *parent;
65 parent = nsblk->common.dev.parent;
66 nd_region = container_of(parent, struct nd_region, dev);
67 return container_of(nd_region, struct nd_blk_region, nd_region);
70 #ifdef CONFIG_BLK_DEV_INTEGRITY
71 static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
72 struct bio_integrity_payload *bip, u64 lba, int rw)
74 struct nd_blk_region *ndbr = to_ndbr(nsblk);
75 unsigned int len = nsblk_meta_size(nsblk);
76 resource_size_t dev_offset, ns_offset;
77 u32 internal_lbasize, sector_size;
78 int err = 0;
80 internal_lbasize = nsblk_internal_lbasize(nsblk);
81 sector_size = nsblk_sector_size(nsblk);
82 ns_offset = lba * internal_lbasize + sector_size;
83 dev_offset = to_dev_offset(nsblk, ns_offset, len);
84 if (dev_offset == SIZE_MAX)
85 return -EIO;
87 while (len) {
88 unsigned int cur_len;
89 struct bio_vec bv;
90 void *iobuf;
92 bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
94 * The 'bv' obtained from bvec_iter_bvec has its .bv_len and
95 * .bv_offset already adjusted for iter->bi_bvec_done, and we
96 * can use those directly
99 cur_len = min(len, bv.bv_len);
100 iobuf = kmap_atomic(bv.bv_page);
101 err = ndbr->do_io(ndbr, dev_offset, iobuf + bv.bv_offset,
102 cur_len, rw);
103 kunmap_atomic(iobuf);
104 if (err)
105 return err;
107 len -= cur_len;
108 dev_offset += cur_len;
109 bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len);
112 return err;
115 #else /* CONFIG_BLK_DEV_INTEGRITY */
116 static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
117 struct bio_integrity_payload *bip, u64 lba, int rw)
119 return 0;
121 #endif
123 static int nsblk_do_bvec(struct nd_namespace_blk *nsblk,
124 struct bio_integrity_payload *bip, struct page *page,
125 unsigned int len, unsigned int off, int rw, sector_t sector)
127 struct nd_blk_region *ndbr = to_ndbr(nsblk);
128 resource_size_t dev_offset, ns_offset;
129 u32 internal_lbasize, sector_size;
130 int err = 0;
131 void *iobuf;
132 u64 lba;
134 internal_lbasize = nsblk_internal_lbasize(nsblk);
135 sector_size = nsblk_sector_size(nsblk);
136 while (len) {
137 unsigned int cur_len;
140 * If we don't have an integrity payload, we don't have to
141 * split the bvec into sectors, as this would cause unnecessary
142 * Block Window setup/move steps. the do_io routine is capable
143 * of handling len <= PAGE_SIZE.
145 cur_len = bip ? min(len, sector_size) : len;
147 lba = div_u64(sector << SECTOR_SHIFT, sector_size);
148 ns_offset = lba * internal_lbasize;
149 dev_offset = to_dev_offset(nsblk, ns_offset, cur_len);
150 if (dev_offset == SIZE_MAX)
151 return -EIO;
153 iobuf = kmap_atomic(page);
154 err = ndbr->do_io(ndbr, dev_offset, iobuf + off, cur_len, rw);
155 kunmap_atomic(iobuf);
156 if (err)
157 return err;
159 if (bip) {
160 err = nd_blk_rw_integrity(nsblk, bip, lba, rw);
161 if (err)
162 return err;
164 len -= cur_len;
165 off += cur_len;
166 sector += sector_size >> SECTOR_SHIFT;
169 return err;
172 static blk_qc_t nd_blk_make_request(struct request_queue *q, struct bio *bio)
174 struct bio_integrity_payload *bip;
175 struct nd_namespace_blk *nsblk;
176 struct bvec_iter iter;
177 unsigned long start;
178 struct bio_vec bvec;
179 int err = 0, rw;
180 bool do_acct;
183 * bio_integrity_enabled also checks if the bio already has an
184 * integrity payload attached. If it does, we *don't* do a
185 * bio_integrity_prep here - the payload has been generated by
186 * another kernel subsystem, and we just pass it through.
188 if (bio_integrity_enabled(bio) && bio_integrity_prep(bio)) {
189 bio->bi_error = -EIO;
190 goto out;
193 bip = bio_integrity(bio);
194 nsblk = q->queuedata;
195 rw = bio_data_dir(bio);
196 do_acct = nd_iostat_start(bio, &start);
197 bio_for_each_segment(bvec, bio, iter) {
198 unsigned int len = bvec.bv_len;
200 BUG_ON(len > PAGE_SIZE);
201 err = nsblk_do_bvec(nsblk, bip, bvec.bv_page, len,
202 bvec.bv_offset, rw, iter.bi_sector);
203 if (err) {
204 dev_dbg(&nsblk->common.dev,
205 "io error in %s sector %lld, len %d,\n",
206 (rw == READ) ? "READ" : "WRITE",
207 (unsigned long long) iter.bi_sector, len);
208 bio->bi_error = err;
209 break;
212 if (do_acct)
213 nd_iostat_end(bio, start);
215 out:
216 bio_endio(bio);
217 return BLK_QC_T_NONE;
220 static int nsblk_rw_bytes(struct nd_namespace_common *ndns,
221 resource_size_t offset, void *iobuf, size_t n, int rw)
223 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
224 struct nd_blk_region *ndbr = to_ndbr(nsblk);
225 resource_size_t dev_offset;
227 dev_offset = to_dev_offset(nsblk, offset, n);
229 if (unlikely(offset + n > nsblk->size)) {
230 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
231 return -EFAULT;
234 if (dev_offset == SIZE_MAX)
235 return -EIO;
237 return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
240 static const struct block_device_operations nd_blk_fops = {
241 .owner = THIS_MODULE,
242 .revalidate_disk = nvdimm_revalidate_disk,
245 static void nd_blk_release_queue(void *q)
247 blk_cleanup_queue(q);
250 static void nd_blk_release_disk(void *disk)
252 del_gendisk(disk);
253 put_disk(disk);
256 static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
258 struct device *dev = &nsblk->common.dev;
259 resource_size_t available_disk_size;
260 struct request_queue *q;
261 struct gendisk *disk;
262 u64 internal_nlba;
264 internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
265 available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
267 q = blk_alloc_queue(GFP_KERNEL);
268 if (!q)
269 return -ENOMEM;
270 if (devm_add_action(dev, nd_blk_release_queue, q)) {
271 blk_cleanup_queue(q);
272 return -ENOMEM;
275 blk_queue_make_request(q, nd_blk_make_request);
276 blk_queue_max_hw_sectors(q, UINT_MAX);
277 blk_queue_bounce_limit(q, BLK_BOUNCE_ANY);
278 blk_queue_logical_block_size(q, nsblk_sector_size(nsblk));
279 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, q);
280 q->queuedata = nsblk;
282 disk = alloc_disk(0);
283 if (!disk)
284 return -ENOMEM;
285 if (devm_add_action(dev, nd_blk_release_disk, disk)) {
286 put_disk(disk);
287 return -ENOMEM;
290 disk->driverfs_dev = dev;
291 disk->first_minor = 0;
292 disk->fops = &nd_blk_fops;
293 disk->queue = q;
294 disk->flags = GENHD_FL_EXT_DEVT;
295 nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name);
296 set_capacity(disk, 0);
297 add_disk(disk);
299 if (nsblk_meta_size(nsblk)) {
300 int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
302 if (rc)
303 return rc;
306 set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
307 revalidate_disk(disk);
308 return 0;
311 static int nd_blk_probe(struct device *dev)
313 struct nd_namespace_common *ndns;
314 struct nd_namespace_blk *nsblk;
316 ndns = nvdimm_namespace_common_probe(dev);
317 if (IS_ERR(ndns))
318 return PTR_ERR(ndns);
320 nsblk = to_nd_namespace_blk(&ndns->dev);
321 nsblk->size = nvdimm_namespace_capacity(ndns);
322 dev_set_drvdata(dev, nsblk);
324 ndns->rw_bytes = nsblk_rw_bytes;
325 if (is_nd_btt(dev))
326 return nvdimm_namespace_attach_btt(ndns);
327 else if (nd_btt_probe(dev, ndns) == 0) {
328 /* we'll come back as btt-blk */
329 return -ENXIO;
330 } else
331 return nsblk_attach_disk(nsblk);
334 static int nd_blk_remove(struct device *dev)
336 if (is_nd_btt(dev))
337 nvdimm_namespace_detach_btt(to_nd_btt(dev));
338 return 0;
341 static struct nd_device_driver nd_blk_driver = {
342 .probe = nd_blk_probe,
343 .remove = nd_blk_remove,
344 .drv = {
345 .name = "nd_blk",
347 .type = ND_DRIVER_NAMESPACE_BLK,
350 static int __init nd_blk_init(void)
352 return nd_driver_register(&nd_blk_driver);
355 static void __exit nd_blk_exit(void)
357 driver_unregister(&nd_blk_driver.drv);
360 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
361 MODULE_LICENSE("GPL v2");
362 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK);
363 module_init(nd_blk_init);
364 module_exit(nd_blk_exit);