1 // SPDX-License-Identifier: GPL-2.0-only
3 * NVDIMM Block Window Driver
4 * Copyright (c) 2014, Intel Corporation.
7 #include <linux/blkdev.h>
9 #include <linux/genhd.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
13 #include <linux/sizes.h>
16 static u32 nsblk_meta_size(struct nd_namespace_blk *nsblk)
18 return nsblk->lbasize - ((nsblk->lbasize >= 4096) ? 4096 : 512);
21 static u32 nsblk_internal_lbasize(struct nd_namespace_blk *nsblk)
23 return roundup(nsblk->lbasize, INT_LBASIZE_ALIGNMENT);
26 static u32 nsblk_sector_size(struct nd_namespace_blk *nsblk)
28 return nsblk->lbasize - nsblk_meta_size(nsblk);
31 static resource_size_t to_dev_offset(struct nd_namespace_blk *nsblk,
32 resource_size_t ns_offset, unsigned int len)
36 for (i = 0; i < nsblk->num_resources; i++) {
37 if (ns_offset < resource_size(nsblk->res[i])) {
38 if (ns_offset + len > resource_size(nsblk->res[i])) {
39 dev_WARN_ONCE(&nsblk->common.dev, 1,
43 return nsblk->res[i]->start + ns_offset;
45 ns_offset -= resource_size(nsblk->res[i]);
48 dev_WARN_ONCE(&nsblk->common.dev, 1, "request out of range\n");
52 static struct nd_blk_region *to_ndbr(struct nd_namespace_blk *nsblk)
54 struct nd_region *nd_region;
55 struct device *parent;
57 parent = nsblk->common.dev.parent;
58 nd_region = container_of(parent, struct nd_region, dev);
59 return container_of(nd_region, struct nd_blk_region, nd_region);
62 #ifdef CONFIG_BLK_DEV_INTEGRITY
63 static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
64 struct bio_integrity_payload *bip, u64 lba, int rw)
66 struct nd_blk_region *ndbr = to_ndbr(nsblk);
67 unsigned int len = nsblk_meta_size(nsblk);
68 resource_size_t dev_offset, ns_offset;
69 u32 internal_lbasize, sector_size;
72 internal_lbasize = nsblk_internal_lbasize(nsblk);
73 sector_size = nsblk_sector_size(nsblk);
74 ns_offset = lba * internal_lbasize + sector_size;
75 dev_offset = to_dev_offset(nsblk, ns_offset, len);
76 if (dev_offset == SIZE_MAX)
84 bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
86 * The 'bv' obtained from bvec_iter_bvec has its .bv_len and
87 * .bv_offset already adjusted for iter->bi_bvec_done, and we
88 * can use those directly
91 cur_len = min(len, bv.bv_len);
92 iobuf = kmap_atomic(bv.bv_page);
93 err = ndbr->do_io(ndbr, dev_offset, iobuf + bv.bv_offset,
100 dev_offset += cur_len;
101 if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len))
108 #else /* CONFIG_BLK_DEV_INTEGRITY */
109 static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
110 struct bio_integrity_payload *bip, u64 lba, int rw)
116 static int nsblk_do_bvec(struct nd_namespace_blk *nsblk,
117 struct bio_integrity_payload *bip, struct page *page,
118 unsigned int len, unsigned int off, int rw, sector_t sector)
120 struct nd_blk_region *ndbr = to_ndbr(nsblk);
121 resource_size_t dev_offset, ns_offset;
122 u32 internal_lbasize, sector_size;
127 internal_lbasize = nsblk_internal_lbasize(nsblk);
128 sector_size = nsblk_sector_size(nsblk);
130 unsigned int cur_len;
133 * If we don't have an integrity payload, we don't have to
134 * split the bvec into sectors, as this would cause unnecessary
135 * Block Window setup/move steps. the do_io routine is capable
136 * of handling len <= PAGE_SIZE.
138 cur_len = bip ? min(len, sector_size) : len;
140 lba = div_u64(sector << SECTOR_SHIFT, sector_size);
141 ns_offset = lba * internal_lbasize;
142 dev_offset = to_dev_offset(nsblk, ns_offset, cur_len);
143 if (dev_offset == SIZE_MAX)
146 iobuf = kmap_atomic(page);
147 err = ndbr->do_io(ndbr, dev_offset, iobuf + off, cur_len, rw);
148 kunmap_atomic(iobuf);
153 err = nd_blk_rw_integrity(nsblk, bip, lba, rw);
159 sector += sector_size >> SECTOR_SHIFT;
165 static blk_qc_t nd_blk_submit_bio(struct bio *bio)
167 struct bio_integrity_payload *bip;
168 struct nd_namespace_blk *nsblk = bio->bi_bdev->bd_disk->private_data;
169 struct bvec_iter iter;
175 if (!bio_integrity_prep(bio))
176 return BLK_QC_T_NONE;
178 bip = bio_integrity(bio);
179 rw = bio_data_dir(bio);
180 do_acct = blk_queue_io_stat(bio->bi_bdev->bd_disk->queue);
182 start = bio_start_io_acct(bio);
183 bio_for_each_segment(bvec, bio, iter) {
184 unsigned int len = bvec.bv_len;
186 BUG_ON(len > PAGE_SIZE);
187 err = nsblk_do_bvec(nsblk, bip, bvec.bv_page, len,
188 bvec.bv_offset, rw, iter.bi_sector);
190 dev_dbg(&nsblk->common.dev,
191 "io error in %s sector %lld, len %d,\n",
192 (rw == READ) ? "READ" : "WRITE",
193 (unsigned long long) iter.bi_sector, len);
194 bio->bi_status = errno_to_blk_status(err);
199 bio_end_io_acct(bio, start);
202 return BLK_QC_T_NONE;
205 static int nsblk_rw_bytes(struct nd_namespace_common *ndns,
206 resource_size_t offset, void *iobuf, size_t n, int rw,
209 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
210 struct nd_blk_region *ndbr = to_ndbr(nsblk);
211 resource_size_t dev_offset;
213 dev_offset = to_dev_offset(nsblk, offset, n);
215 if (unlikely(offset + n > nsblk->size)) {
216 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
220 if (dev_offset == SIZE_MAX)
223 return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
226 static const struct block_device_operations nd_blk_fops = {
227 .owner = THIS_MODULE,
228 .submit_bio = nd_blk_submit_bio,
231 static void nd_blk_release_disk(void *disk)
234 blk_cleanup_disk(disk);
237 static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
239 struct device *dev = &nsblk->common.dev;
240 resource_size_t available_disk_size;
241 struct gendisk *disk;
244 internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
245 available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
247 disk = blk_alloc_disk(NUMA_NO_NODE);
251 disk->fops = &nd_blk_fops;
252 disk->private_data = nsblk;
253 nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name);
255 blk_queue_max_hw_sectors(disk->queue, UINT_MAX);
256 blk_queue_logical_block_size(disk->queue, nsblk_sector_size(nsblk));
257 blk_queue_flag_set(QUEUE_FLAG_NONROT, disk->queue);
259 if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
262 if (nsblk_meta_size(nsblk)) {
263 int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
269 set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
270 device_add_disk(dev, disk, NULL);
271 nvdimm_check_and_set_ro(disk);
275 static int nd_blk_probe(struct device *dev)
277 struct nd_namespace_common *ndns;
278 struct nd_namespace_blk *nsblk;
280 ndns = nvdimm_namespace_common_probe(dev);
282 return PTR_ERR(ndns);
284 nsblk = to_nd_namespace_blk(&ndns->dev);
285 nsblk->size = nvdimm_namespace_capacity(ndns);
286 dev_set_drvdata(dev, nsblk);
288 ndns->rw_bytes = nsblk_rw_bytes;
290 return nvdimm_namespace_attach_btt(ndns);
291 else if (nd_btt_probe(dev, ndns) == 0) {
292 /* we'll come back as btt-blk */
295 return nsblk_attach_disk(nsblk);
298 static void nd_blk_remove(struct device *dev)
301 nvdimm_namespace_detach_btt(to_nd_btt(dev));
304 static struct nd_device_driver nd_blk_driver = {
305 .probe = nd_blk_probe,
306 .remove = nd_blk_remove,
310 .type = ND_DRIVER_NAMESPACE_BLK,
313 static int __init nd_blk_init(void)
315 return nd_driver_register(&nd_blk_driver);
318 static void __exit nd_blk_exit(void)
320 driver_unregister(&nd_blk_driver.drv);
323 MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
324 MODULE_LICENSE("GPL v2");
325 MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK);
326 module_init(nd_blk_init);
327 module_exit(nd_blk_exit);