Thomas Gleixner | 2025cf9 | 2019-05-29 07:18:02 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 2 | /* |
| 3 | * NVDIMM Block Window Driver |
| 4 | * Copyright (c) 2014, Intel Corporation. |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 5 | */ |
| 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 Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 16 | static u32 nsblk_meta_size(struct nd_namespace_blk *nsblk) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 17 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 18 | return nsblk->lbasize - ((nsblk->lbasize >= 4096) ? 4096 : 512); |
| 19 | } |
| 20 | |
| 21 | static u32 nsblk_internal_lbasize(struct nd_namespace_blk *nsblk) |
| 22 | { |
| 23 | return roundup(nsblk->lbasize, INT_LBASIZE_ALIGNMENT); |
| 24 | } |
| 25 | |
| 26 | static u32 nsblk_sector_size(struct nd_namespace_blk *nsblk) |
| 27 | { |
| 28 | return nsblk->lbasize - nsblk_meta_size(nsblk); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 29 | } |
| 30 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 31 | static 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 Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 52 | static struct nd_blk_region *to_ndbr(struct nd_namespace_blk *nsblk) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 53 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 54 | 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 |
| 63 | static 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 Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 68 | resource_size_t dev_offset, ns_offset; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 69 | u32 internal_lbasize, sector_size; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 70 | int err = 0; |
| 71 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 72 | internal_lbasize = nsblk_internal_lbasize(nsblk); |
| 73 | sector_size = nsblk_sector_size(nsblk); |
| 74 | ns_offset = lba * internal_lbasize + sector_size; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 75 | 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 Monakhov | b1fb2c5 | 2017-06-29 11:31:13 -0700 | [diff] [blame] | 101 | if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len)) |
| 102 | return -EIO; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return err; |
| 106 | } |
| 107 | |
| 108 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 109 | static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk, |
| 110 | struct bio_integrity_payload *bip, u64 lba, int rw) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 111 | { |
| 112 | return 0; |
| 113 | } |
| 114 | #endif |
| 115 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 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) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 119 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 120 | struct nd_blk_region *ndbr = to_ndbr(nsblk); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 121 | resource_size_t dev_offset, ns_offset; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 122 | u32 internal_lbasize, sector_size; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 123 | int err = 0; |
| 124 | void *iobuf; |
| 125 | u64 lba; |
| 126 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 127 | internal_lbasize = nsblk_internal_lbasize(nsblk); |
| 128 | sector_size = nsblk_sector_size(nsblk); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 129 | 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 Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 138 | cur_len = bip ? min(len, sector_size) : len; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 139 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 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); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 143 | 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 Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 153 | err = nd_blk_rw_integrity(nsblk, bip, lba, rw); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 154 | if (err) |
| 155 | return err; |
| 156 | } |
| 157 | len -= cur_len; |
| 158 | off += cur_len; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 159 | sector += sector_size >> SECTOR_SHIFT; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return err; |
| 163 | } |
| 164 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 165 | static blk_qc_t nd_blk_make_request(struct request_queue *q, struct bio *bio) |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 166 | { |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 167 | struct bio_integrity_payload *bip; |
Christoph Hellwig | daa2897 | 2020-05-08 18:15:15 +0200 | [diff] [blame] | 168 | struct nd_namespace_blk *nsblk = bio->bi_disk->private_data; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 169 | struct bvec_iter iter; |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 170 | unsigned long start; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 171 | struct bio_vec bvec; |
| 172 | int err = 0, rw; |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 173 | bool do_acct; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 174 | |
Dmitry Monakhov | e23947b | 2017-06-29 11:31:11 -0700 | [diff] [blame] | 175 | if (!bio_integrity_prep(bio)) |
| 176 | return BLK_QC_T_NONE; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 177 | |
| 178 | bip = bio_integrity(bio); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 179 | rw = bio_data_dir(bio); |
Christoph Hellwig | 0fd92f8 | 2020-05-27 07:24:10 +0200 | [diff] [blame] | 180 | do_acct = blk_queue_io_stat(bio->bi_disk->queue); |
| 181 | if (do_acct) |
| 182 | start = bio_start_io_acct(bio); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 183 | bio_for_each_segment(bvec, bio, iter) { |
| 184 | unsigned int len = bvec.bv_len; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 185 | |
| 186 | BUG_ON(len > PAGE_SIZE); |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 187 | err = nsblk_do_bvec(nsblk, bip, bvec.bv_page, len, |
| 188 | bvec.bv_offset, rw, iter.bi_sector); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 189 | if (err) { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 190 | dev_dbg(&nsblk->common.dev, |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 191 | "io error in %s sector %lld, len %d,\n", |
| 192 | (rw == READ) ? "READ" : "WRITE", |
| 193 | (unsigned long long) iter.bi_sector, len); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 194 | bio->bi_status = errno_to_blk_status(err); |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 195 | break; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 196 | } |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 197 | } |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 198 | if (do_acct) |
Christoph Hellwig | 0fd92f8 | 2020-05-27 07:24:10 +0200 | [diff] [blame] | 199 | bio_end_io_acct(bio, start); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 200 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 201 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 202 | return BLK_QC_T_NONE; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 205 | static int nsblk_rw_bytes(struct nd_namespace_common *ndns, |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 206 | resource_size_t offset, void *iobuf, size_t n, int rw, |
| 207 | unsigned long flags) |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 208 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 209 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev); |
| 210 | struct nd_blk_region *ndbr = to_ndbr(nsblk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 211 | resource_size_t dev_offset; |
| 212 | |
| 213 | dev_offset = to_dev_offset(nsblk, offset, n); |
| 214 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 215 | if (unlikely(offset + n > nsblk->size)) { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 216 | dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n"); |
| 217 | return -EFAULT; |
| 218 | } |
| 219 | |
| 220 | if (dev_offset == SIZE_MAX) |
| 221 | return -EIO; |
| 222 | |
| 223 | return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw); |
| 224 | } |
| 225 | |
| 226 | static const struct block_device_operations nd_blk_fops = { |
| 227 | .owner = THIS_MODULE, |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 228 | .revalidate_disk = nvdimm_revalidate_disk, |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 229 | }; |
| 230 | |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 231 | static void nd_blk_release_queue(void *q) |
| 232 | { |
| 233 | blk_cleanup_queue(q); |
| 234 | } |
| 235 | |
| 236 | static void nd_blk_release_disk(void *disk) |
| 237 | { |
| 238 | del_gendisk(disk); |
| 239 | put_disk(disk); |
| 240 | } |
| 241 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 242 | static int nsblk_attach_disk(struct nd_namespace_blk *nsblk) |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 243 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 244 | struct device *dev = &nsblk->common.dev; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 245 | resource_size_t available_disk_size; |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 246 | struct request_queue *q; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 247 | struct gendisk *disk; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 248 | u64 internal_nlba; |
| 249 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 250 | internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk)); |
| 251 | available_disk_size = internal_nlba * nsblk_sector_size(nsblk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 252 | |
Christoph Hellwig | 3d745ea | 2020-03-27 09:30:11 +0100 | [diff] [blame] | 253 | q = blk_alloc_queue(nd_blk_make_request, NUMA_NO_NODE); |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 254 | if (!q) |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 255 | return -ENOMEM; |
Dan Williams | f02716d | 2016-06-15 14:59:17 -0700 | [diff] [blame] | 256 | if (devm_add_action_or_reset(dev, nd_blk_release_queue, q)) |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 257 | return -ENOMEM; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 258 | |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 259 | blk_queue_max_hw_sectors(q, UINT_MAX); |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 260 | blk_queue_logical_block_size(q, nsblk_sector_size(nsblk)); |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 261 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 262 | |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 263 | disk = alloc_disk(0); |
| 264 | if (!disk) |
| 265 | return -ENOMEM; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 266 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 267 | disk->first_minor = 0; |
| 268 | disk->fops = &nd_blk_fops; |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 269 | disk->queue = q; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 270 | disk->flags = GENHD_FL_EXT_DEVT; |
Christoph Hellwig | daa2897 | 2020-05-08 18:15:15 +0200 | [diff] [blame] | 271 | disk->private_data = nsblk; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 272 | nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 273 | |
Dan Williams | f02716d | 2016-06-15 14:59:17 -0700 | [diff] [blame] | 274 | if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk)) |
| 275 | return -ENOMEM; |
| 276 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 277 | if (nsblk_meta_size(nsblk)) { |
| 278 | int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk)); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 279 | |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 280 | if (rc) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 281 | return rc; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | set_capacity(disk, available_disk_size >> SECTOR_SHIFT); |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 285 | device_add_disk(dev, disk, NULL); |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 286 | revalidate_disk(disk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | static int nd_blk_probe(struct device *dev) |
| 291 | { |
| 292 | struct nd_namespace_common *ndns; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 293 | struct nd_namespace_blk *nsblk; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 294 | |
| 295 | ndns = nvdimm_namespace_common_probe(dev); |
| 296 | if (IS_ERR(ndns)) |
| 297 | return PTR_ERR(ndns); |
| 298 | |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 299 | nsblk = to_nd_namespace_blk(&ndns->dev); |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 300 | nsblk->size = nvdimm_namespace_capacity(ndns); |
| 301 | dev_set_drvdata(dev, nsblk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 302 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 303 | ndns->rw_bytes = nsblk_rw_bytes; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 304 | if (is_nd_btt(dev)) |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 305 | return nvdimm_namespace_attach_btt(ndns); |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 306 | else if (nd_btt_probe(dev, ndns) == 0) { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 307 | /* we'll come back as btt-blk */ |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 308 | return -ENXIO; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 309 | } else |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 310 | return nsblk_attach_disk(nsblk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static int nd_blk_remove(struct device *dev) |
| 314 | { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 315 | if (is_nd_btt(dev)) |
Dan Williams | 298f2bc | 2016-03-15 16:41:04 -0700 | [diff] [blame] | 316 | nvdimm_namespace_detach_btt(to_nd_btt(dev)); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static struct nd_device_driver nd_blk_driver = { |
| 321 | .probe = nd_blk_probe, |
| 322 | .remove = nd_blk_remove, |
| 323 | .drv = { |
| 324 | .name = "nd_blk", |
| 325 | }, |
| 326 | .type = ND_DRIVER_NAMESPACE_BLK, |
| 327 | }; |
| 328 | |
| 329 | static int __init nd_blk_init(void) |
| 330 | { |
NeilBrown | ec56151 | 2016-03-10 08:59:28 +1100 | [diff] [blame] | 331 | return nd_driver_register(&nd_blk_driver); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | static void __exit nd_blk_exit(void) |
| 335 | { |
| 336 | driver_unregister(&nd_blk_driver.drv); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); |
| 340 | MODULE_LICENSE("GPL v2"); |
| 341 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK); |
| 342 | module_init(nd_blk_init); |
| 343 | module_exit(nd_blk_exit); |