Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 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 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 24 | static u32 nsblk_meta_size(struct nd_namespace_blk *nsblk) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 25 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 26 | return nsblk->lbasize - ((nsblk->lbasize >= 4096) ? 4096 : 512); |
| 27 | } |
| 28 | |
| 29 | static u32 nsblk_internal_lbasize(struct nd_namespace_blk *nsblk) |
| 30 | { |
| 31 | return roundup(nsblk->lbasize, INT_LBASIZE_ALIGNMENT); |
| 32 | } |
| 33 | |
| 34 | static u32 nsblk_sector_size(struct nd_namespace_blk *nsblk) |
| 35 | { |
| 36 | return nsblk->lbasize - nsblk_meta_size(nsblk); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 37 | } |
| 38 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 39 | static resource_size_t to_dev_offset(struct nd_namespace_blk *nsblk, |
| 40 | resource_size_t ns_offset, unsigned int len) |
| 41 | { |
| 42 | int i; |
| 43 | |
| 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; |
| 50 | } |
| 51 | return nsblk->res[i]->start + ns_offset; |
| 52 | } |
| 53 | ns_offset -= resource_size(nsblk->res[i]); |
| 54 | } |
| 55 | |
| 56 | dev_WARN_ONCE(&nsblk->common.dev, 1, "request out of range\n"); |
| 57 | return SIZE_MAX; |
| 58 | } |
| 59 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 60 | static struct nd_blk_region *to_ndbr(struct nd_namespace_blk *nsblk) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 61 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 62 | struct nd_region *nd_region; |
| 63 | struct device *parent; |
| 64 | |
| 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); |
| 68 | } |
| 69 | |
| 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) |
| 73 | { |
| 74 | struct nd_blk_region *ndbr = to_ndbr(nsblk); |
| 75 | unsigned int len = nsblk_meta_size(nsblk); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 76 | resource_size_t dev_offset, ns_offset; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 77 | u32 internal_lbasize, sector_size; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 78 | int err = 0; |
| 79 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 80 | internal_lbasize = nsblk_internal_lbasize(nsblk); |
| 81 | sector_size = nsblk_sector_size(nsblk); |
| 82 | ns_offset = lba * internal_lbasize + sector_size; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 83 | dev_offset = to_dev_offset(nsblk, ns_offset, len); |
| 84 | if (dev_offset == SIZE_MAX) |
| 85 | return -EIO; |
| 86 | |
| 87 | while (len) { |
| 88 | unsigned int cur_len; |
| 89 | struct bio_vec bv; |
| 90 | void *iobuf; |
| 91 | |
| 92 | bv = bvec_iter_bvec(bip->bip_vec, bip->bip_iter); |
| 93 | /* |
| 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 |
| 97 | */ |
| 98 | |
| 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; |
| 106 | |
| 107 | len -= cur_len; |
| 108 | dev_offset += cur_len; |
Dmitry Monakhov | b1fb2c5 | 2017-06-29 11:31:13 -0700 | [diff] [blame] | 109 | if (!bvec_iter_advance(bip->bip_vec, &bip->bip_iter, cur_len)) |
| 110 | return -EIO; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | return err; |
| 114 | } |
| 115 | |
| 116 | #else /* CONFIG_BLK_DEV_INTEGRITY */ |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 117 | static int nd_blk_rw_integrity(struct nd_namespace_blk *nsblk, |
| 118 | struct bio_integrity_payload *bip, u64 lba, int rw) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 119 | { |
| 120 | return 0; |
| 121 | } |
| 122 | #endif |
| 123 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 124 | static int nsblk_do_bvec(struct nd_namespace_blk *nsblk, |
| 125 | struct bio_integrity_payload *bip, struct page *page, |
| 126 | unsigned int len, unsigned int off, int rw, sector_t sector) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 127 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 128 | struct nd_blk_region *ndbr = to_ndbr(nsblk); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 129 | resource_size_t dev_offset, ns_offset; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 130 | u32 internal_lbasize, sector_size; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 131 | int err = 0; |
| 132 | void *iobuf; |
| 133 | u64 lba; |
| 134 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 135 | internal_lbasize = nsblk_internal_lbasize(nsblk); |
| 136 | sector_size = nsblk_sector_size(nsblk); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 137 | while (len) { |
| 138 | unsigned int cur_len; |
| 139 | |
| 140 | /* |
| 141 | * If we don't have an integrity payload, we don't have to |
| 142 | * split the bvec into sectors, as this would cause unnecessary |
| 143 | * Block Window setup/move steps. the do_io routine is capable |
| 144 | * of handling len <= PAGE_SIZE. |
| 145 | */ |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 146 | cur_len = bip ? min(len, sector_size) : len; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 147 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 148 | lba = div_u64(sector << SECTOR_SHIFT, sector_size); |
| 149 | ns_offset = lba * internal_lbasize; |
| 150 | dev_offset = to_dev_offset(nsblk, ns_offset, cur_len); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 151 | if (dev_offset == SIZE_MAX) |
| 152 | return -EIO; |
| 153 | |
| 154 | iobuf = kmap_atomic(page); |
| 155 | err = ndbr->do_io(ndbr, dev_offset, iobuf + off, cur_len, rw); |
| 156 | kunmap_atomic(iobuf); |
| 157 | if (err) |
| 158 | return err; |
| 159 | |
| 160 | if (bip) { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 161 | err = nd_blk_rw_integrity(nsblk, bip, lba, rw); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 162 | if (err) |
| 163 | return err; |
| 164 | } |
| 165 | len -= cur_len; |
| 166 | off += cur_len; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 167 | sector += sector_size >> SECTOR_SHIFT; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | return err; |
| 171 | } |
| 172 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 173 | 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] | 174 | { |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 175 | struct bio_integrity_payload *bip; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 176 | struct nd_namespace_blk *nsblk; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 177 | struct bvec_iter iter; |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 178 | unsigned long start; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 179 | struct bio_vec bvec; |
| 180 | int err = 0, rw; |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 181 | bool do_acct; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 182 | |
Dmitry Monakhov | e23947b | 2017-06-29 11:31:11 -0700 | [diff] [blame] | 183 | if (!bio_integrity_prep(bio)) |
| 184 | return BLK_QC_T_NONE; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 185 | |
| 186 | bip = bio_integrity(bio); |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 187 | nsblk = q->queuedata; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 188 | rw = bio_data_dir(bio); |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 189 | do_acct = nd_iostat_start(bio, &start); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 190 | bio_for_each_segment(bvec, bio, iter) { |
| 191 | unsigned int len = bvec.bv_len; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 192 | |
| 193 | BUG_ON(len > PAGE_SIZE); |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 194 | err = nsblk_do_bvec(nsblk, bip, bvec.bv_page, len, |
| 195 | bvec.bv_offset, rw, iter.bi_sector); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 196 | if (err) { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 197 | dev_dbg(&nsblk->common.dev, |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 198 | "io error in %s sector %lld, len %d,\n", |
| 199 | (rw == READ) ? "READ" : "WRITE", |
| 200 | (unsigned long long) iter.bi_sector, len); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 201 | bio->bi_status = errno_to_blk_status(err); |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 202 | break; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 203 | } |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 204 | } |
Dan Williams | f0dc089 | 2015-05-16 12:28:53 -0400 | [diff] [blame] | 205 | if (do_acct) |
| 206 | nd_iostat_end(bio, start); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 207 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 208 | bio_endio(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 209 | return BLK_QC_T_NONE; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 212 | static int nsblk_rw_bytes(struct nd_namespace_common *ndns, |
Vishal Verma | 3ae3d67 | 2017-05-10 15:01:30 -0600 | [diff] [blame] | 213 | resource_size_t offset, void *iobuf, size_t n, int rw, |
| 214 | unsigned long flags) |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 215 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 216 | struct nd_namespace_blk *nsblk = to_nd_namespace_blk(&ndns->dev); |
| 217 | struct nd_blk_region *ndbr = to_ndbr(nsblk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 218 | resource_size_t dev_offset; |
| 219 | |
| 220 | dev_offset = to_dev_offset(nsblk, offset, n); |
| 221 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 222 | if (unlikely(offset + n > nsblk->size)) { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 223 | dev_WARN_ONCE(&ndns->dev, 1, "request out of range\n"); |
| 224 | return -EFAULT; |
| 225 | } |
| 226 | |
| 227 | if (dev_offset == SIZE_MAX) |
| 228 | return -EIO; |
| 229 | |
| 230 | return ndbr->do_io(ndbr, dev_offset, iobuf, n, rw); |
| 231 | } |
| 232 | |
| 233 | static const struct block_device_operations nd_blk_fops = { |
| 234 | .owner = THIS_MODULE, |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 235 | .revalidate_disk = nvdimm_revalidate_disk, |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 236 | }; |
| 237 | |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 238 | static void nd_blk_release_queue(void *q) |
| 239 | { |
| 240 | blk_cleanup_queue(q); |
| 241 | } |
| 242 | |
| 243 | static void nd_blk_release_disk(void *disk) |
| 244 | { |
| 245 | del_gendisk(disk); |
| 246 | put_disk(disk); |
| 247 | } |
| 248 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 249 | static int nsblk_attach_disk(struct nd_namespace_blk *nsblk) |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 250 | { |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 251 | struct device *dev = &nsblk->common.dev; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 252 | resource_size_t available_disk_size; |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 253 | struct request_queue *q; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 254 | struct gendisk *disk; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 255 | u64 internal_nlba; |
| 256 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 257 | internal_nlba = div_u64(nsblk->size, nsblk_internal_lbasize(nsblk)); |
| 258 | available_disk_size = internal_nlba * nsblk_sector_size(nsblk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 259 | |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 260 | q = blk_alloc_queue(GFP_KERNEL); |
| 261 | if (!q) |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 262 | return -ENOMEM; |
Dan Williams | f02716d | 2016-06-15 14:59:17 -0700 | [diff] [blame] | 263 | if (devm_add_action_or_reset(dev, nd_blk_release_queue, q)) |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 264 | return -ENOMEM; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 265 | |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 266 | blk_queue_make_request(q, nd_blk_make_request); |
| 267 | blk_queue_max_hw_sectors(q, UINT_MAX); |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 268 | blk_queue_logical_block_size(q, nsblk_sector_size(nsblk)); |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 269 | blk_queue_flag_set(QUEUE_FLAG_NONROT, q); |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 270 | q->queuedata = nsblk; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 271 | |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 272 | disk = alloc_disk(0); |
| 273 | if (!disk) |
| 274 | return -ENOMEM; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 275 | |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 276 | disk->first_minor = 0; |
| 277 | disk->fops = &nd_blk_fops; |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 278 | disk->queue = q; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 279 | disk->flags = GENHD_FL_EXT_DEVT; |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 280 | nvdimm_namespace_disk_name(&nsblk->common, disk->disk_name); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 281 | |
Dan Williams | f02716d | 2016-06-15 14:59:17 -0700 | [diff] [blame] | 282 | if (devm_add_action_or_reset(dev, nd_blk_release_disk, disk)) |
| 283 | return -ENOMEM; |
| 284 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 285 | if (nsblk_meta_size(nsblk)) { |
| 286 | int rc = nd_integrity_init(disk, nsblk_meta_size(nsblk)); |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 287 | |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 288 | if (rc) |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 289 | return rc; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | set_capacity(disk, available_disk_size >> SECTOR_SHIFT); |
Hannes Reinecke | fef912b | 2018-09-28 08:17:19 +0200 | [diff] [blame] | 293 | device_add_disk(dev, disk, NULL); |
Dan Williams | 5813882 | 2015-06-23 20:08:34 -0400 | [diff] [blame] | 294 | revalidate_disk(disk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int nd_blk_probe(struct device *dev) |
| 299 | { |
| 300 | struct nd_namespace_common *ndns; |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 301 | struct nd_namespace_blk *nsblk; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 302 | |
| 303 | ndns = nvdimm_namespace_common_probe(dev); |
| 304 | if (IS_ERR(ndns)) |
| 305 | return PTR_ERR(ndns); |
| 306 | |
Vishal Verma | fcae695 | 2015-06-25 04:22:39 -0400 | [diff] [blame] | 307 | nsblk = to_nd_namespace_blk(&ndns->dev); |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 308 | nsblk->size = nvdimm_namespace_capacity(ndns); |
| 309 | dev_set_drvdata(dev, nsblk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 310 | |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 311 | ndns->rw_bytes = nsblk_rw_bytes; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 312 | if (is_nd_btt(dev)) |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 313 | return nvdimm_namespace_attach_btt(ndns); |
Dan Williams | 200c79d | 2016-03-22 00:22:16 -0700 | [diff] [blame] | 314 | else if (nd_btt_probe(dev, ndns) == 0) { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 315 | /* we'll come back as btt-blk */ |
Dan Williams | d29cee1 | 2016-03-17 20:08:28 -0700 | [diff] [blame] | 316 | return -ENXIO; |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 317 | } else |
Dan Williams | 9d90725d | 2016-03-18 11:27:36 -0700 | [diff] [blame] | 318 | return nsblk_attach_disk(nsblk); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | static int nd_blk_remove(struct device *dev) |
| 322 | { |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 323 | if (is_nd_btt(dev)) |
Dan Williams | 298f2bc | 2016-03-15 16:41:04 -0700 | [diff] [blame] | 324 | nvdimm_namespace_detach_btt(to_nd_btt(dev)); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static struct nd_device_driver nd_blk_driver = { |
| 329 | .probe = nd_blk_probe, |
| 330 | .remove = nd_blk_remove, |
| 331 | .drv = { |
| 332 | .name = "nd_blk", |
| 333 | }, |
| 334 | .type = ND_DRIVER_NAMESPACE_BLK, |
| 335 | }; |
| 336 | |
| 337 | static int __init nd_blk_init(void) |
| 338 | { |
NeilBrown | ec56151 | 2016-03-10 08:59:28 +1100 | [diff] [blame] | 339 | return nd_driver_register(&nd_blk_driver); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static void __exit nd_blk_exit(void) |
| 343 | { |
| 344 | driver_unregister(&nd_blk_driver.drv); |
Ross Zwisler | 047fc8a | 2015-06-25 04:21:02 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | MODULE_AUTHOR("Ross Zwisler <ross.zwisler@linux.intel.com>"); |
| 348 | MODULE_LICENSE("GPL v2"); |
| 349 | MODULE_ALIAS_ND_DEVICE(ND_DEVICE_NAMESPACE_BLK); |
| 350 | module_init(nd_blk_init); |
| 351 | module_exit(nd_blk_exit); |