blob: 43751fab9d36a886ec871db07c96d09db27fd81e [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Ross Zwisler047fc8a2015-06-25 04:21:02 -04002/*
3 * NVDIMM Block Window Driver
4 * Copyright (c) 2014, Intel Corporation.
Ross Zwisler047fc8a2015-06-25 04:21:02 -04005 */
6
7#include <linux/blkdev.h>
8#include <linux/fs.h>
9#include <linux/genhd.h>
10#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/nd.h>
13#include <linux/sizes.h>
14#include "nd.h"
15
Dan Williams9d90725d2016-03-18 11:27:36 -070016static u32 nsblk_meta_size(struct nd_namespace_blk *nsblk)
Vishal Vermafcae6952015-06-25 04:22:39 -040017{
Dan Williams9d90725d2016-03-18 11:27:36 -070018 return nsblk->lbasize - ((nsblk->lbasize >= 4096) ? 4096 : 512);
19}
20
21static u32 nsblk_internal_lbasize(struct nd_namespace_blk *nsblk)
22{
23 return roundup(nsblk->lbasize, INT_LBASIZE_ALIGNMENT);
24}
25
26static u32 nsblk_sector_size(struct nd_namespace_blk *nsblk)
27{
28 return nsblk->lbasize - nsblk_meta_size(nsblk);
Vishal Vermafcae6952015-06-25 04:22:39 -040029}
30
Ross Zwisler047fc8a2015-06-25 04:21:02 -040031static resource_size_t to_dev_offset(struct nd_namespace_blk *nsblk,
32 resource_size_t ns_offset, unsigned int len)
33{
34 int i;
35
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,
40 "illegal request\n");
41 return SIZE_MAX;
42 }
43 return nsblk->res[i]->start + ns_offset;
44 }
45 ns_offset -= resource_size(nsblk->res[i]);
46 }
47
48 dev_WARN_ONCE(&nsblk->common.dev, 1, "request out of range\n");
49 return SIZE_MAX;
50}
51
Dan Williams9d90725d2016-03-18 11:27:36 -070052static struct nd_blk_region *to_ndbr(struct nd_namespace_blk *nsblk)
Vishal Vermafcae6952015-06-25 04:22:39 -040053{
Dan Williams9d90725d2016-03-18 11:27:36 -070054 struct nd_region *nd_region;
55 struct device *parent;
56
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);
60}
61
62#ifdef CONFIG_BLK_DEV_INTEGRITY
63static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
64 struct bio_integrity_payload *bip, u64 lba, int rw)
65{
66 struct nd_blk_region *ndbr = to_ndbr(nsblk);
67 unsigned int len = nsblk_meta_size(nsblk);
Vishal Vermafcae6952015-06-25 04:22:39 -040068 resource_size_t dev_offset, ns_offset;
Dan Williams9d90725d2016-03-18 11:27:36 -070069 u32 internal_lbasize, sector_size;
Vishal Vermafcae6952015-06-25 04:22:39 -040070 int err = 0;
71
Dan Williams9d90725d2016-03-18 11:27:36 -070072 internal_lbasize = nsblk_internal_lbasize(nsblk);
73 sector_size = nsblk_sector_size(nsblk);
74 ns_offset = lba * internal_lbasize + sector_size;
Vishal Vermafcae6952015-06-25 04:22:39 -040075 dev_offset = to_dev_offset(nsblk, ns_offset, len);
76 if (dev_offset == SIZE_MAX)
77 return -EIO;
78
79 while (len) {
80 unsigned int cur_len;
81 struct bio_vec bv;
82 void *iobuf;
83
84 bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter);
85 /*
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
89 */
90
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,
94 cur_len, rw);
95 kunmap_atomic(iobuf);
96 if (err)
97 return err;
98
99 len -= cur_len;
100 dev_offset += cur_len;
Dmitry Monakhovb1fb2c52017-06-29 11:31:13 -0700101 if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len))
102 return -EIO;
Vishal Vermafcae6952015-06-25 04:22:39 -0400103 }
104
105 return err;
106}
107
108#else /* CONFIG_BLK_DEV_INTEGRITY */
Dan Williams9d90725d2016-03-18 11:27:36 -0700109static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk,
110 struct bio_integrity_payload *bip, u64 lba, int rw)
Vishal Vermafcae6952015-06-25 04:22:39 -0400111{
112 return 0;
113}
114#endif
115
Dan Williams9d90725d2016-03-18 11:27:36 -0700116static 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)
Vishal Vermafcae6952015-06-25 04:22:39 -0400119{
Dan Williams9d90725d2016-03-18 11:27:36 -0700120 struct nd_blk_region *ndbr = to_ndbr(nsblk);
Vishal Vermafcae6952015-06-25 04:22:39 -0400121 resource_size_t dev_offset, ns_offset;
Dan Williams9d90725d2016-03-18 11:27:36 -0700122 u32 internal_lbasize, sector_size;
Vishal Vermafcae6952015-06-25 04:22:39 -0400123 int err = 0;
124 void *iobuf;
125 u64 lba;
126
Dan Williams9d90725d2016-03-18 11:27:36 -0700127 internal_lbasize = nsblk_internal_lbasize(nsblk);
128 sector_size = nsblk_sector_size(nsblk);
Vishal Vermafcae6952015-06-25 04:22:39 -0400129 while (len) {
130 unsigned int cur_len;
131
132 /*
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.
137 */
Dan Williams9d90725d2016-03-18 11:27:36 -0700138 cur_len = bip ? min(len, sector_size) : len;
Vishal Vermafcae6952015-06-25 04:22:39 -0400139
Dan Williams9d90725d2016-03-18 11:27:36 -0700140 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);
Vishal Vermafcae6952015-06-25 04:22:39 -0400143 if (dev_offset == SIZE_MAX)
144 return -EIO;
145
146 iobuf = kmap_atomic(page);
147 err = ndbr->do_io(ndbr, dev_offset, iobuf + off, cur_len, rw);
148 kunmap_atomic(iobuf);
149 if (err)
150 return err;
151
152 if (bip) {
Dan Williams9d90725d2016-03-18 11:27:36 -0700153 err = nd_blk_rw_integrity(nsblk, bip, lba, rw);
Vishal Vermafcae6952015-06-25 04:22:39 -0400154 if (err)
155 return err;
156 }
157 len -= cur_len;
158 off += cur_len;
Dan Williams9d90725d2016-03-18 11:27:36 -0700159 sector += sector_size >> SECTOR_SHIFT;
Vishal Vermafcae6952015-06-25 04:22:39 -0400160 }
161
162 return err;
163}
164
Jens Axboedece1632015-11-05 10:41:16 -0700165static blk_qc_t nd_blk_make_request(struct request_queue *q, struct bio *bio)
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400166{
Vishal Vermafcae6952015-06-25 04:22:39 -0400167 struct bio_integrity_payload *bip;
Dan Williams9d90725d2016-03-18 11:27:36 -0700168 struct nd_namespace_blk *nsblk;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400169 struct bvec_iter iter;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400170 unsigned long start;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400171 struct bio_vec bvec;
172 int err = 0, rw;
Dan Williamsf0dc0892015-05-16 12:28:53 -0400173 bool do_acct;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400174
Dmitry Monakhove23947b2017-06-29 11:31:11 -0700175 if (!bio_integrity_prep(bio))
176 return BLK_QC_T_NONE;
Vishal Vermafcae6952015-06-25 04:22:39 -0400177
178 bip = bio_integrity(bio);
Dan Williams9d90725d2016-03-18 11:27:36 -0700179 nsblk = q->queuedata;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400180 rw = bio_data_dir(bio);
Dan Williamsf0dc0892015-05-16 12:28:53 -0400181 do_acct = nd_iostat_start(bio, &start);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400182 bio_for_each_segment(bvec, bio, iter) {
183 unsigned int len = bvec.bv_len;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400184
185 BUG_ON(len > PAGE_SIZE);
Dan Williams9d90725d2016-03-18 11:27:36 -0700186 err = nsblk_do_bvec(nsblk, bip, bvec.bv_page, len,
187 bvec.bv_offset, rw, iter.bi_sector);
Vishal Vermafcae6952015-06-25 04:22:39 -0400188 if (err) {
Dan Williams9d90725d2016-03-18 11:27:36 -0700189 dev_dbg(&nsblk->common.dev,
Vishal Vermafcae6952015-06-25 04:22:39 -0400190 "io error in %s sector %lld, len %d,\n",
191 (rw == READ) ? "READ" : "WRITE",
192 (unsigned long long) iter.bi_sector, len);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200193 bio->bi_status = errno_to_blk_status(err);
Dan Williamsf0dc0892015-05-16 12:28:53 -0400194 break;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400195 }
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400196 }
Dan Williamsf0dc0892015-05-16 12:28:53 -0400197 if (do_acct)
198 nd_iostat_end(bio, start);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400199
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200200 bio_endio(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700201 return BLK_QC_T_NONE;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400202}
203
Dan Williams9d90725d2016-03-18 11:27:36 -0700204static int nsblk_rw_bytes(struct nd_namespace_common *ndns,
Vishal Verma3ae3d672017-05-10 15:01:30 -0600205 resource_size_t offset, void *iobuf, size_t n, int rw,
206 unsigned long flags)
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400207{
Dan Williams9d90725d2016-03-18 11:27:36 -0700208 struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev);
209 struct nd_blk_region *ndbr = to_ndbr(nsblk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400210 resource_size_t dev_offset;
211
212 dev_offset = to_dev_offset(nsblk, offset, n);
213
Dan Williams9d90725d2016-03-18 11:27:36 -0700214 if (unlikely(offset + n > nsblk->size)) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400215 dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n");
216 return -EFAULT;
217 }
218
219 if (dev_offset == SIZE_MAX)
220 return -EIO;
221
222 return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw);
223}
224
225static const struct block_device_operations nd_blk_fops = {
226 .owner = THIS_MODULE,
Dan Williams58138822015-06-23 20:08:34 -0400227 .revalidate_disk = nvdimm_revalidate_disk,
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400228};
229
Dan Williamsd29cee12016-03-17 20:08:28 -0700230static void nd_blk_release_queue(void *q)
231{
232 blk_cleanup_queue(q);
233}
234
235static void nd_blk_release_disk(void *disk)
236{
237 del_gendisk(disk);
238 put_disk(disk);
239}
240
Dan Williams9d90725d2016-03-18 11:27:36 -0700241static int nsblk_attach_disk(struct nd_namespace_blk *nsblk)
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400242{
Dan Williams9d90725d2016-03-18 11:27:36 -0700243 struct device *dev = &nsblk->common.dev;
Vishal Vermafcae6952015-06-25 04:22:39 -0400244 resource_size_t available_disk_size;
Dan Williamsd29cee12016-03-17 20:08:28 -0700245 struct request_queue *q;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400246 struct gendisk *disk;
Vishal Vermafcae6952015-06-25 04:22:39 -0400247 u64 internal_nlba;
248
Dan Williams9d90725d2016-03-18 11:27:36 -0700249 internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk));
250 available_disk_size = internal_nlba * nsblk_sector_size(nsblk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400251
Christoph Hellwig3d745ea2020-03-27 09:30:11 +0100252 q = blk_alloc_queue(nd_blk_make_request, NUMA_NO_NODE);
Dan Williamsd29cee12016-03-17 20:08:28 -0700253 if (!q)
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400254 return -ENOMEM;
Dan Williamsf02716d2016-06-15 14:59:17 -0700255 if (devm_add_action_or_reset(dev, nd_blk_release_queue, q))
Dan Williamsd29cee12016-03-17 20:08:28 -0700256 return -ENOMEM;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400257
Dan Williamsd29cee12016-03-17 20:08:28 -0700258 blk_queue_max_hw_sectors(q, UINT_MAX);
Dan Williams9d90725d2016-03-18 11:27:36 -0700259 blk_queue_logical_block_size(q, nsblk_sector_size(nsblk));
Bart Van Assche8b904b52018-03-07 17:10:10 -0800260 blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
Dan Williams9d90725d2016-03-18 11:27:36 -0700261 q->queuedata = nsblk;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400262
Dan Williamsd29cee12016-03-17 20:08:28 -0700263 disk = alloc_disk(0);
264 if (!disk)
265 return -ENOMEM;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400266
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400267 disk->first_minor = 0;
268 disk->fops = &nd_blk_fops;
Dan Williamsd29cee12016-03-17 20:08:28 -0700269 disk->queue = q;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400270 disk->flags = GENHD_FL_EXT_DEVT;
Dan Williams9d90725d2016-03-18 11:27:36 -0700271 nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400272
Dan Williamsf02716d2016-06-15 14:59:17 -0700273 if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk))
274 return -ENOMEM;
275
Dan Williams9d90725d2016-03-18 11:27:36 -0700276 if (nsblk_meta_size(nsblk)) {
277 int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk));
Vishal Vermafcae6952015-06-25 04:22:39 -0400278
Dan Williamsd29cee12016-03-17 20:08:28 -0700279 if (rc)
Vishal Vermafcae6952015-06-25 04:22:39 -0400280 return rc;
Vishal Vermafcae6952015-06-25 04:22:39 -0400281 }
282
283 set_capacity(disk, available_disk_size >> SECTOR_SHIFT);
Hannes Reineckefef912b2018-09-28 08:17:19 +0200284 device_add_disk(dev, disk, NULL);
Dan Williams58138822015-06-23 20:08:34 -0400285 revalidate_disk(disk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400286 return 0;
287}
288
289static int nd_blk_probe(struct device *dev)
290{
291 struct nd_namespace_common *ndns;
Vishal Vermafcae6952015-06-25 04:22:39 -0400292 struct nd_namespace_blk *nsblk;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400293
294 ndns = nvdimm_namespace_common_probe(dev);
295 if (IS_ERR(ndns))
296 return PTR_ERR(ndns);
297
Vishal Vermafcae6952015-06-25 04:22:39 -0400298 nsblk = to_nd_namespace_blk(&ndns->dev);
Dan Williams9d90725d2016-03-18 11:27:36 -0700299 nsblk->size = nvdimm_namespace_capacity(ndns);
300 dev_set_drvdata(dev, nsblk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400301
Dan Williams9d90725d2016-03-18 11:27:36 -0700302 ndns->rw_bytes = nsblk_rw_bytes;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400303 if (is_nd_btt(dev))
Dan Williamsd29cee12016-03-17 20:08:28 -0700304 return nvdimm_namespace_attach_btt(ndns);
Dan Williams200c79d2016-03-22 00:22:16 -0700305 else if (nd_btt_probe(dev, ndns) == 0) {
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400306 /* we'll come back as btt-blk */
Dan Williamsd29cee12016-03-17 20:08:28 -0700307 return -ENXIO;
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400308 } else
Dan Williams9d90725d2016-03-18 11:27:36 -0700309 return nsblk_attach_disk(nsblk);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400310}
311
312static int nd_blk_remove(struct device *dev)
313{
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400314 if (is_nd_btt(dev))
Dan Williams298f2bc2016-03-15 16:41:04 -0700315 nvdimm_namespace_detach_btt(to_nd_btt(dev));
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400316 return 0;
317}
318
319static struct nd_device_driver nd_blk_driver = {
320 .probe = nd_blk_probe,
321 .remove = nd_blk_remove,
322 .drv = {
323 .name = "nd_blk",
324 },
325 .type = ND_DRIVER_NAMESPACE_BLK,
326};
327
328static int __init nd_blk_init(void)
329{
NeilBrownec561512016-03-10 08:59:28 +1100330 return nd_driver_register(&nd_blk_driver);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400331}
332
333static void __exit nd_blk_exit(void)
334{
335 driver_unregister(&nd_blk_driver.drv);
Ross Zwisler047fc8a2015-06-25 04:21:02 -0400336}
337
338MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>");
339MODULE_LICENSE("GPL v2");
340MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK);
341module_init(nd_blk_init);
342module_exit(nd_blk_exit);