Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 IT University of Copenhagen. All rights reserved. |
| 3 | * Initial release: Matias Bjorling <m@bjorling.me> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License version |
| 7 | * 2 as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; see the file COPYING. If not, write to |
| 16 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, |
| 17 | * USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <linux/blkdev.h> |
| 22 | #include <linux/blk-mq.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/sem.h> |
| 26 | #include <linux/bitmap.h> |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/miscdevice.h> |
| 29 | #include <linux/lightnvm.h> |
| 30 | #include <uapi/linux/lightnvm.h> |
| 31 | |
| 32 | static LIST_HEAD(nvm_targets); |
| 33 | static LIST_HEAD(nvm_mgrs); |
| 34 | static LIST_HEAD(nvm_devices); |
| 35 | static DECLARE_RWSEM(nvm_lock); |
| 36 | |
| 37 | static struct nvm_tgt_type *nvm_find_target_type(const char *name) |
| 38 | { |
| 39 | struct nvm_tgt_type *tt; |
| 40 | |
| 41 | list_for_each_entry(tt, &nvm_targets, list) |
| 42 | if (!strcmp(name, tt->name)) |
| 43 | return tt; |
| 44 | |
| 45 | return NULL; |
| 46 | } |
| 47 | |
| 48 | int nvm_register_target(struct nvm_tgt_type *tt) |
| 49 | { |
| 50 | int ret = 0; |
| 51 | |
| 52 | down_write(&nvm_lock); |
| 53 | if (nvm_find_target_type(tt->name)) |
| 54 | ret = -EEXIST; |
| 55 | else |
| 56 | list_add(&tt->list, &nvm_targets); |
| 57 | up_write(&nvm_lock); |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | EXPORT_SYMBOL(nvm_register_target); |
| 62 | |
| 63 | void nvm_unregister_target(struct nvm_tgt_type *tt) |
| 64 | { |
| 65 | if (!tt) |
| 66 | return; |
| 67 | |
| 68 | down_write(&nvm_lock); |
| 69 | list_del(&tt->list); |
| 70 | up_write(&nvm_lock); |
| 71 | } |
| 72 | EXPORT_SYMBOL(nvm_unregister_target); |
| 73 | |
| 74 | void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags, |
| 75 | dma_addr_t *dma_handler) |
| 76 | { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 77 | return dev->ops->dev_dma_alloc(dev, dev->ppalist_pool, mem_flags, |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 78 | dma_handler); |
| 79 | } |
| 80 | EXPORT_SYMBOL(nvm_dev_dma_alloc); |
| 81 | |
| 82 | void nvm_dev_dma_free(struct nvm_dev *dev, void *ppa_list, |
| 83 | dma_addr_t dma_handler) |
| 84 | { |
| 85 | dev->ops->dev_dma_free(dev->ppalist_pool, ppa_list, dma_handler); |
| 86 | } |
| 87 | EXPORT_SYMBOL(nvm_dev_dma_free); |
| 88 | |
| 89 | static struct nvmm_type *nvm_find_mgr_type(const char *name) |
| 90 | { |
| 91 | struct nvmm_type *mt; |
| 92 | |
| 93 | list_for_each_entry(mt, &nvm_mgrs, list) |
| 94 | if (!strcmp(name, mt->name)) |
| 95 | return mt; |
| 96 | |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 100 | struct nvmm_type *nvm_init_mgr(struct nvm_dev *dev) |
| 101 | { |
| 102 | struct nvmm_type *mt; |
| 103 | int ret; |
| 104 | |
| 105 | lockdep_assert_held(&nvm_lock); |
| 106 | |
| 107 | list_for_each_entry(mt, &nvm_mgrs, list) { |
| 108 | ret = mt->register_mgr(dev); |
| 109 | if (ret < 0) { |
| 110 | pr_err("nvm: media mgr failed to init (%d) on dev %s\n", |
| 111 | ret, dev->name); |
| 112 | return NULL; /* initialization failed */ |
| 113 | } else if (ret > 0) |
| 114 | return mt; |
| 115 | } |
| 116 | |
| 117 | return NULL; |
| 118 | } |
| 119 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 120 | int nvm_register_mgr(struct nvmm_type *mt) |
| 121 | { |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 122 | struct nvm_dev *dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 123 | int ret = 0; |
| 124 | |
| 125 | down_write(&nvm_lock); |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 126 | if (nvm_find_mgr_type(mt->name)) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 127 | ret = -EEXIST; |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 128 | goto finish; |
| 129 | } else { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 130 | list_add(&mt->list, &nvm_mgrs); |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /* try to register media mgr if any device have none configured */ |
| 134 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 135 | if (dev->mt) |
| 136 | continue; |
| 137 | |
| 138 | dev->mt = nvm_init_mgr(dev); |
| 139 | } |
| 140 | finish: |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 141 | up_write(&nvm_lock); |
| 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | EXPORT_SYMBOL(nvm_register_mgr); |
| 146 | |
| 147 | void nvm_unregister_mgr(struct nvmm_type *mt) |
| 148 | { |
| 149 | if (!mt) |
| 150 | return; |
| 151 | |
| 152 | down_write(&nvm_lock); |
| 153 | list_del(&mt->list); |
| 154 | up_write(&nvm_lock); |
| 155 | } |
| 156 | EXPORT_SYMBOL(nvm_unregister_mgr); |
| 157 | |
| 158 | static struct nvm_dev *nvm_find_nvm_dev(const char *name) |
| 159 | { |
| 160 | struct nvm_dev *dev; |
| 161 | |
| 162 | list_for_each_entry(dev, &nvm_devices, devices) |
| 163 | if (!strcmp(name, dev->name)) |
| 164 | return dev; |
| 165 | |
| 166 | return NULL; |
| 167 | } |
| 168 | |
| 169 | struct nvm_block *nvm_get_blk(struct nvm_dev *dev, struct nvm_lun *lun, |
| 170 | unsigned long flags) |
| 171 | { |
| 172 | return dev->mt->get_blk(dev, lun, flags); |
| 173 | } |
| 174 | EXPORT_SYMBOL(nvm_get_blk); |
| 175 | |
| 176 | /* Assumes that all valid pages have already been moved on release to bm */ |
| 177 | void nvm_put_blk(struct nvm_dev *dev, struct nvm_block *blk) |
| 178 | { |
| 179 | return dev->mt->put_blk(dev, blk); |
| 180 | } |
| 181 | EXPORT_SYMBOL(nvm_put_blk); |
| 182 | |
| 183 | int nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd) |
| 184 | { |
| 185 | return dev->mt->submit_io(dev, rqd); |
| 186 | } |
| 187 | EXPORT_SYMBOL(nvm_submit_io); |
| 188 | |
| 189 | int nvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk) |
| 190 | { |
| 191 | return dev->mt->erase_blk(dev, blk, 0); |
| 192 | } |
| 193 | EXPORT_SYMBOL(nvm_erase_blk); |
| 194 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 195 | static int nvm_core_init(struct nvm_dev *dev) |
| 196 | { |
| 197 | struct nvm_id *id = &dev->identity; |
| 198 | struct nvm_id_group *grp = &id->groups[0]; |
| 199 | |
| 200 | /* device values */ |
| 201 | dev->nr_chnls = grp->num_ch; |
| 202 | dev->luns_per_chnl = grp->num_lun; |
| 203 | dev->pgs_per_blk = grp->num_pg; |
| 204 | dev->blks_per_lun = grp->num_blk; |
| 205 | dev->nr_planes = grp->num_pln; |
| 206 | dev->sec_size = grp->csecs; |
| 207 | dev->oob_size = grp->sos; |
| 208 | dev->sec_per_pg = grp->fpg_sz / grp->csecs; |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 209 | memcpy(&dev->ppaf, &id->ppaf, sizeof(struct nvm_addr_format)); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 210 | |
| 211 | dev->plane_mode = NVM_PLANE_SINGLE; |
| 212 | dev->max_rq_size = dev->ops->max_phys_sect * dev->sec_size; |
| 213 | |
Matias Bjørling | 4264c98 | 2015-11-16 15:34:41 +0100 | [diff] [blame] | 214 | if (grp->mtype != 0) { |
| 215 | pr_err("nvm: memory type not supported\n"); |
| 216 | return -EINVAL; |
| 217 | } |
| 218 | |
| 219 | if (grp->fmtype != 0 && grp->fmtype != 1) { |
| 220 | pr_err("nvm: flash type not supported\n"); |
| 221 | return -EINVAL; |
| 222 | } |
| 223 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 224 | if (grp->mpos & 0x020202) |
| 225 | dev->plane_mode = NVM_PLANE_DOUBLE; |
| 226 | if (grp->mpos & 0x040404) |
| 227 | dev->plane_mode = NVM_PLANE_QUAD; |
| 228 | |
| 229 | /* calculated values */ |
| 230 | dev->sec_per_pl = dev->sec_per_pg * dev->nr_planes; |
| 231 | dev->sec_per_blk = dev->sec_per_pl * dev->pgs_per_blk; |
| 232 | dev->sec_per_lun = dev->sec_per_blk * dev->blks_per_lun; |
| 233 | dev->nr_luns = dev->luns_per_chnl * dev->nr_chnls; |
| 234 | |
| 235 | dev->total_blocks = dev->nr_planes * |
| 236 | dev->blks_per_lun * |
| 237 | dev->luns_per_chnl * |
| 238 | dev->nr_chnls; |
| 239 | dev->total_pages = dev->total_blocks * dev->pgs_per_blk; |
| 240 | INIT_LIST_HEAD(&dev->online_targets); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | static void nvm_free(struct nvm_dev *dev) |
| 246 | { |
| 247 | if (!dev) |
| 248 | return; |
| 249 | |
| 250 | if (dev->mt) |
| 251 | dev->mt->unregister_mgr(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static int nvm_init(struct nvm_dev *dev) |
| 255 | { |
Wenwei Tao | 480fc0d | 2015-11-20 13:47:53 +0100 | [diff] [blame] | 256 | int ret = -EINVAL; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 257 | |
| 258 | if (!dev->q || !dev->ops) |
Wenwei Tao | 480fc0d | 2015-11-20 13:47:53 +0100 | [diff] [blame] | 259 | return ret; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 260 | |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 261 | if (dev->ops->identity(dev, &dev->identity)) { |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 262 | pr_err("nvm: device could not be identified\n"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 263 | goto err; |
| 264 | } |
| 265 | |
| 266 | pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n", |
| 267 | dev->identity.ver_id, dev->identity.vmnt, |
| 268 | dev->identity.cgrps); |
| 269 | |
| 270 | if (dev->identity.ver_id != 1) { |
| 271 | pr_err("nvm: device not supported by kernel."); |
| 272 | goto err; |
| 273 | } |
| 274 | |
| 275 | if (dev->identity.cgrps != 1) { |
| 276 | pr_err("nvm: only one group configuration supported."); |
| 277 | goto err; |
| 278 | } |
| 279 | |
| 280 | ret = nvm_core_init(dev); |
| 281 | if (ret) { |
| 282 | pr_err("nvm: could not initialize core structures.\n"); |
| 283 | goto err; |
| 284 | } |
| 285 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 286 | pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n", |
| 287 | dev->name, dev->sec_per_pg, dev->nr_planes, |
| 288 | dev->pgs_per_blk, dev->blks_per_lun, dev->nr_luns, |
| 289 | dev->nr_chnls); |
| 290 | return 0; |
| 291 | err: |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 292 | pr_err("nvm: failed to initialize nvm\n"); |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static void nvm_exit(struct nvm_dev *dev) |
| 297 | { |
| 298 | if (dev->ppalist_pool) |
| 299 | dev->ops->destroy_dma_pool(dev->ppalist_pool); |
| 300 | nvm_free(dev); |
| 301 | |
| 302 | pr_info("nvm: successfully unloaded\n"); |
| 303 | } |
| 304 | |
| 305 | int nvm_register(struct request_queue *q, char *disk_name, |
| 306 | struct nvm_dev_ops *ops) |
| 307 | { |
| 308 | struct nvm_dev *dev; |
| 309 | int ret; |
| 310 | |
| 311 | if (!ops->identity) |
| 312 | return -EINVAL; |
| 313 | |
| 314 | dev = kzalloc(sizeof(struct nvm_dev), GFP_KERNEL); |
| 315 | if (!dev) |
| 316 | return -ENOMEM; |
| 317 | |
| 318 | dev->q = q; |
| 319 | dev->ops = ops; |
| 320 | strncpy(dev->name, disk_name, DISK_NAME_LEN); |
| 321 | |
| 322 | ret = nvm_init(dev); |
| 323 | if (ret) |
| 324 | goto err_init; |
| 325 | |
Wenwei Tao | d160147 | 2015-11-28 16:49:25 +0100 | [diff] [blame] | 326 | if (dev->ops->max_phys_sect > 256) { |
| 327 | pr_info("nvm: max sectors supported is 256.\n"); |
| 328 | ret = -EINVAL; |
| 329 | goto err_init; |
| 330 | } |
| 331 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 332 | if (dev->ops->max_phys_sect > 1) { |
Matias Bjørling | 16f26c3 | 2015-12-06 11:25:48 +0100 | [diff] [blame] | 333 | dev->ppalist_pool = dev->ops->create_dma_pool(dev, "ppalist"); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 334 | if (!dev->ppalist_pool) { |
| 335 | pr_err("nvm: could not create ppa pool\n"); |
Matias Bjørling | 93e70c1 | 2015-11-20 13:47:54 +0100 | [diff] [blame] | 336 | ret = -ENOMEM; |
| 337 | goto err_init; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 338 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 341 | /* register device with a supported media manager */ |
Matias Bjørling | edad2e6 | 2015-11-16 15:34:42 +0100 | [diff] [blame] | 342 | down_write(&nvm_lock); |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 343 | dev->mt = nvm_init_mgr(dev); |
Matias Bjørling | edad2e6 | 2015-11-16 15:34:42 +0100 | [diff] [blame] | 344 | list_add(&dev->devices, &nvm_devices); |
| 345 | up_write(&nvm_lock); |
| 346 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | err_init: |
| 349 | kfree(dev); |
| 350 | return ret; |
| 351 | } |
| 352 | EXPORT_SYMBOL(nvm_register); |
| 353 | |
| 354 | void nvm_unregister(char *disk_name) |
| 355 | { |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 356 | struct nvm_dev *dev; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 357 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 358 | down_write(&nvm_lock); |
| 359 | dev = nvm_find_nvm_dev(disk_name); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 360 | if (!dev) { |
| 361 | pr_err("nvm: could not find device %s to unregister\n", |
| 362 | disk_name); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 363 | up_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 364 | return; |
| 365 | } |
| 366 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 367 | list_del(&dev->devices); |
| 368 | up_write(&nvm_lock); |
Matias Bjørling | c1480ad | 2015-11-16 15:34:43 +0100 | [diff] [blame] | 369 | |
| 370 | nvm_exit(dev); |
| 371 | kfree(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 372 | } |
| 373 | EXPORT_SYMBOL(nvm_unregister); |
| 374 | |
| 375 | static const struct block_device_operations nvm_fops = { |
| 376 | .owner = THIS_MODULE, |
| 377 | }; |
| 378 | |
| 379 | static int nvm_create_target(struct nvm_dev *dev, |
| 380 | struct nvm_ioctl_create *create) |
| 381 | { |
| 382 | struct nvm_ioctl_create_simple *s = &create->conf.s; |
| 383 | struct request_queue *tqueue; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 384 | struct gendisk *tdisk; |
| 385 | struct nvm_tgt_type *tt; |
| 386 | struct nvm_target *t; |
| 387 | void *targetdata; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 388 | |
| 389 | if (!dev->mt) { |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 390 | pr_info("nvm: device has no media manager registered.\n"); |
| 391 | return -ENODEV; |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Matias Bjørling | 762796b | 2015-12-06 11:25:49 +0100 | [diff] [blame] | 394 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 395 | tt = nvm_find_target_type(create->tgttype); |
| 396 | if (!tt) { |
| 397 | pr_err("nvm: target type %s not found\n", create->tgttype); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 398 | up_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 399 | return -EINVAL; |
| 400 | } |
| 401 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 402 | list_for_each_entry(t, &dev->online_targets, list) { |
| 403 | if (!strcmp(create->tgtname, t->disk->disk_name)) { |
| 404 | pr_err("nvm: target name already exists.\n"); |
| 405 | up_write(&nvm_lock); |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | } |
| 409 | up_write(&nvm_lock); |
| 410 | |
| 411 | t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL); |
| 412 | if (!t) |
| 413 | return -ENOMEM; |
| 414 | |
| 415 | tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node); |
| 416 | if (!tqueue) |
| 417 | goto err_t; |
| 418 | blk_queue_make_request(tqueue, tt->make_rq); |
| 419 | |
| 420 | tdisk = alloc_disk(0); |
| 421 | if (!tdisk) |
| 422 | goto err_queue; |
| 423 | |
| 424 | sprintf(tdisk->disk_name, "%s", create->tgtname); |
| 425 | tdisk->flags = GENHD_FL_EXT_DEVT; |
| 426 | tdisk->major = 0; |
| 427 | tdisk->first_minor = 0; |
| 428 | tdisk->fops = &nvm_fops; |
| 429 | tdisk->queue = tqueue; |
| 430 | |
| 431 | targetdata = tt->init(dev, tdisk, s->lun_begin, s->lun_end); |
| 432 | if (IS_ERR(targetdata)) |
| 433 | goto err_init; |
| 434 | |
| 435 | tdisk->private_data = targetdata; |
| 436 | tqueue->queuedata = targetdata; |
| 437 | |
| 438 | blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect); |
| 439 | |
| 440 | set_capacity(tdisk, tt->capacity(targetdata)); |
| 441 | add_disk(tdisk); |
| 442 | |
| 443 | t->type = tt; |
| 444 | t->disk = tdisk; |
| 445 | |
| 446 | down_write(&nvm_lock); |
| 447 | list_add_tail(&t->list, &dev->online_targets); |
| 448 | up_write(&nvm_lock); |
| 449 | |
| 450 | return 0; |
| 451 | err_init: |
| 452 | put_disk(tdisk); |
| 453 | err_queue: |
| 454 | blk_cleanup_queue(tqueue); |
| 455 | err_t: |
| 456 | kfree(t); |
| 457 | return -ENOMEM; |
| 458 | } |
| 459 | |
| 460 | static void nvm_remove_target(struct nvm_target *t) |
| 461 | { |
| 462 | struct nvm_tgt_type *tt = t->type; |
| 463 | struct gendisk *tdisk = t->disk; |
| 464 | struct request_queue *q = tdisk->queue; |
| 465 | |
| 466 | lockdep_assert_held(&nvm_lock); |
| 467 | |
| 468 | del_gendisk(tdisk); |
Javier González | d09f958 | 2015-11-16 15:34:47 +0100 | [diff] [blame] | 469 | blk_cleanup_queue(q); |
| 470 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 471 | if (tt->exit) |
| 472 | tt->exit(tdisk->private_data); |
| 473 | |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 474 | put_disk(tdisk); |
| 475 | |
| 476 | list_del(&t->list); |
| 477 | kfree(t); |
| 478 | } |
| 479 | |
| 480 | static int __nvm_configure_create(struct nvm_ioctl_create *create) |
| 481 | { |
| 482 | struct nvm_dev *dev; |
| 483 | struct nvm_ioctl_create_simple *s; |
| 484 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 485 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 486 | dev = nvm_find_nvm_dev(create->dev); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 487 | up_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 488 | if (!dev) { |
| 489 | pr_err("nvm: device not found\n"); |
| 490 | return -EINVAL; |
| 491 | } |
| 492 | |
| 493 | if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) { |
| 494 | pr_err("nvm: config type not valid\n"); |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | s = &create->conf.s; |
| 498 | |
| 499 | if (s->lun_begin > s->lun_end || s->lun_end > dev->nr_luns) { |
| 500 | pr_err("nvm: lun out of bound (%u:%u > %u)\n", |
| 501 | s->lun_begin, s->lun_end, dev->nr_luns); |
| 502 | return -EINVAL; |
| 503 | } |
| 504 | |
| 505 | return nvm_create_target(dev, create); |
| 506 | } |
| 507 | |
| 508 | static int __nvm_configure_remove(struct nvm_ioctl_remove *remove) |
| 509 | { |
| 510 | struct nvm_target *t = NULL; |
| 511 | struct nvm_dev *dev; |
| 512 | int ret = -1; |
| 513 | |
| 514 | down_write(&nvm_lock); |
| 515 | list_for_each_entry(dev, &nvm_devices, devices) |
| 516 | list_for_each_entry(t, &dev->online_targets, list) { |
| 517 | if (!strcmp(remove->tgtname, t->disk->disk_name)) { |
| 518 | nvm_remove_target(t); |
| 519 | ret = 0; |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | up_write(&nvm_lock); |
| 524 | |
| 525 | if (ret) { |
| 526 | pr_err("nvm: target \"%s\" doesn't exist.\n", remove->tgtname); |
| 527 | return -EINVAL; |
| 528 | } |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | #ifdef CONFIG_NVM_DEBUG |
| 534 | static int nvm_configure_show(const char *val) |
| 535 | { |
| 536 | struct nvm_dev *dev; |
| 537 | char opcode, devname[DISK_NAME_LEN]; |
| 538 | int ret; |
| 539 | |
| 540 | ret = sscanf(val, "%c %32s", &opcode, devname); |
| 541 | if (ret != 2) { |
| 542 | pr_err("nvm: invalid command. Use \"opcode devicename\".\n"); |
| 543 | return -EINVAL; |
| 544 | } |
| 545 | |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 546 | down_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 547 | dev = nvm_find_nvm_dev(devname); |
Wenwei Tao | d0a712c | 2015-11-28 16:49:28 +0100 | [diff] [blame] | 548 | up_write(&nvm_lock); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 549 | if (!dev) { |
| 550 | pr_err("nvm: device not found\n"); |
| 551 | return -EINVAL; |
| 552 | } |
| 553 | |
| 554 | if (!dev->mt) |
| 555 | return 0; |
| 556 | |
Javier Gonzalez | 2fde0e4 | 2015-11-20 13:47:57 +0100 | [diff] [blame] | 557 | dev->mt->lun_info_print(dev); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 558 | |
| 559 | return 0; |
| 560 | } |
| 561 | |
| 562 | static int nvm_configure_remove(const char *val) |
| 563 | { |
| 564 | struct nvm_ioctl_remove remove; |
| 565 | char opcode; |
| 566 | int ret; |
| 567 | |
| 568 | ret = sscanf(val, "%c %256s", &opcode, remove.tgtname); |
| 569 | if (ret != 2) { |
| 570 | pr_err("nvm: invalid command. Use \"d targetname\".\n"); |
| 571 | return -EINVAL; |
| 572 | } |
| 573 | |
| 574 | remove.flags = 0; |
| 575 | |
| 576 | return __nvm_configure_remove(&remove); |
| 577 | } |
| 578 | |
| 579 | static int nvm_configure_create(const char *val) |
| 580 | { |
| 581 | struct nvm_ioctl_create create; |
| 582 | char opcode; |
| 583 | int lun_begin, lun_end, ret; |
| 584 | |
| 585 | ret = sscanf(val, "%c %256s %256s %48s %u:%u", &opcode, create.dev, |
| 586 | create.tgtname, create.tgttype, |
| 587 | &lun_begin, &lun_end); |
| 588 | if (ret != 6) { |
| 589 | pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n"); |
| 590 | return -EINVAL; |
| 591 | } |
| 592 | |
| 593 | create.flags = 0; |
| 594 | create.conf.type = NVM_CONFIG_TYPE_SIMPLE; |
| 595 | create.conf.s.lun_begin = lun_begin; |
| 596 | create.conf.s.lun_end = lun_end; |
| 597 | |
| 598 | return __nvm_configure_create(&create); |
| 599 | } |
| 600 | |
| 601 | |
| 602 | /* Exposes administrative interface through /sys/module/lnvm/configure_by_str */ |
| 603 | static int nvm_configure_by_str_event(const char *val, |
| 604 | const struct kernel_param *kp) |
| 605 | { |
| 606 | char opcode; |
| 607 | int ret; |
| 608 | |
| 609 | ret = sscanf(val, "%c", &opcode); |
| 610 | if (ret != 1) { |
| 611 | pr_err("nvm: string must have the format of \"cmd ...\"\n"); |
| 612 | return -EINVAL; |
| 613 | } |
| 614 | |
| 615 | switch (opcode) { |
| 616 | case 'a': |
| 617 | return nvm_configure_create(val); |
| 618 | case 'd': |
| 619 | return nvm_configure_remove(val); |
| 620 | case 's': |
| 621 | return nvm_configure_show(val); |
| 622 | default: |
| 623 | pr_err("nvm: invalid command\n"); |
| 624 | return -EINVAL; |
| 625 | } |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | static int nvm_configure_get(char *buf, const struct kernel_param *kp) |
| 631 | { |
| 632 | int sz = 0; |
| 633 | char *buf_start = buf; |
| 634 | struct nvm_dev *dev; |
| 635 | |
| 636 | buf += sprintf(buf, "available devices:\n"); |
| 637 | down_write(&nvm_lock); |
| 638 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 639 | if (sz > 4095 - DISK_NAME_LEN) |
| 640 | break; |
| 641 | buf += sprintf(buf, " %32s\n", dev->name); |
| 642 | } |
| 643 | up_write(&nvm_lock); |
| 644 | |
| 645 | return buf - buf_start - 1; |
| 646 | } |
| 647 | |
| 648 | static const struct kernel_param_ops nvm_configure_by_str_event_param_ops = { |
| 649 | .set = nvm_configure_by_str_event, |
| 650 | .get = nvm_configure_get, |
| 651 | }; |
| 652 | |
| 653 | #undef MODULE_PARAM_PREFIX |
| 654 | #define MODULE_PARAM_PREFIX "lnvm." |
| 655 | |
| 656 | module_param_cb(configure_debug, &nvm_configure_by_str_event_param_ops, NULL, |
| 657 | 0644); |
| 658 | |
| 659 | #endif /* CONFIG_NVM_DEBUG */ |
| 660 | |
| 661 | static long nvm_ioctl_info(struct file *file, void __user *arg) |
| 662 | { |
| 663 | struct nvm_ioctl_info *info; |
| 664 | struct nvm_tgt_type *tt; |
| 665 | int tgt_iter = 0; |
| 666 | |
| 667 | if (!capable(CAP_SYS_ADMIN)) |
| 668 | return -EPERM; |
| 669 | |
| 670 | info = memdup_user(arg, sizeof(struct nvm_ioctl_info)); |
| 671 | if (IS_ERR(info)) |
| 672 | return -EFAULT; |
| 673 | |
| 674 | info->version[0] = NVM_VERSION_MAJOR; |
| 675 | info->version[1] = NVM_VERSION_MINOR; |
| 676 | info->version[2] = NVM_VERSION_PATCH; |
| 677 | |
| 678 | down_write(&nvm_lock); |
| 679 | list_for_each_entry(tt, &nvm_targets, list) { |
| 680 | struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter]; |
| 681 | |
| 682 | tgt->version[0] = tt->version[0]; |
| 683 | tgt->version[1] = tt->version[1]; |
| 684 | tgt->version[2] = tt->version[2]; |
| 685 | strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX); |
| 686 | |
| 687 | tgt_iter++; |
| 688 | } |
| 689 | |
| 690 | info->tgtsize = tgt_iter; |
| 691 | up_write(&nvm_lock); |
| 692 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 693 | if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) { |
| 694 | kfree(info); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 695 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 696 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 697 | |
| 698 | kfree(info); |
| 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | static long nvm_ioctl_get_devices(struct file *file, void __user *arg) |
| 703 | { |
| 704 | struct nvm_ioctl_get_devices *devices; |
| 705 | struct nvm_dev *dev; |
| 706 | int i = 0; |
| 707 | |
| 708 | if (!capable(CAP_SYS_ADMIN)) |
| 709 | return -EPERM; |
| 710 | |
| 711 | devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL); |
| 712 | if (!devices) |
| 713 | return -ENOMEM; |
| 714 | |
| 715 | down_write(&nvm_lock); |
| 716 | list_for_each_entry(dev, &nvm_devices, devices) { |
| 717 | struct nvm_ioctl_device_info *info = &devices->info[i]; |
| 718 | |
| 719 | sprintf(info->devname, "%s", dev->name); |
| 720 | if (dev->mt) { |
| 721 | info->bmversion[0] = dev->mt->version[0]; |
| 722 | info->bmversion[1] = dev->mt->version[1]; |
| 723 | info->bmversion[2] = dev->mt->version[2]; |
| 724 | sprintf(info->bmname, "%s", dev->mt->name); |
| 725 | } else { |
| 726 | sprintf(info->bmname, "none"); |
| 727 | } |
| 728 | |
| 729 | i++; |
| 730 | if (i > 31) { |
| 731 | pr_err("nvm: max 31 devices can be reported.\n"); |
| 732 | break; |
| 733 | } |
| 734 | } |
| 735 | up_write(&nvm_lock); |
| 736 | |
| 737 | devices->nr_devices = i; |
| 738 | |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 739 | if (copy_to_user(arg, devices, |
| 740 | sizeof(struct nvm_ioctl_get_devices))) { |
| 741 | kfree(devices); |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 742 | return -EFAULT; |
Sudip Mukherjee | 76e2508 | 2015-11-28 16:49:24 +0100 | [diff] [blame] | 743 | } |
Matias Bjørling | cd9e980 | 2015-10-28 19:54:55 +0100 | [diff] [blame] | 744 | |
| 745 | kfree(devices); |
| 746 | return 0; |
| 747 | } |
| 748 | |
| 749 | static long nvm_ioctl_dev_create(struct file *file, void __user *arg) |
| 750 | { |
| 751 | struct nvm_ioctl_create create; |
| 752 | |
| 753 | if (!capable(CAP_SYS_ADMIN)) |
| 754 | return -EPERM; |
| 755 | |
| 756 | if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create))) |
| 757 | return -EFAULT; |
| 758 | |
| 759 | create.dev[DISK_NAME_LEN - 1] = '\0'; |
| 760 | create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0'; |
| 761 | create.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 762 | |
| 763 | if (create.flags != 0) { |
| 764 | pr_err("nvm: no flags supported\n"); |
| 765 | return -EINVAL; |
| 766 | } |
| 767 | |
| 768 | return __nvm_configure_create(&create); |
| 769 | } |
| 770 | |
| 771 | static long nvm_ioctl_dev_remove(struct file *file, void __user *arg) |
| 772 | { |
| 773 | struct nvm_ioctl_remove remove; |
| 774 | |
| 775 | if (!capable(CAP_SYS_ADMIN)) |
| 776 | return -EPERM; |
| 777 | |
| 778 | if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove))) |
| 779 | return -EFAULT; |
| 780 | |
| 781 | remove.tgtname[DISK_NAME_LEN - 1] = '\0'; |
| 782 | |
| 783 | if (remove.flags != 0) { |
| 784 | pr_err("nvm: no flags supported\n"); |
| 785 | return -EINVAL; |
| 786 | } |
| 787 | |
| 788 | return __nvm_configure_remove(&remove); |
| 789 | } |
| 790 | |
| 791 | static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg) |
| 792 | { |
| 793 | void __user *argp = (void __user *)arg; |
| 794 | |
| 795 | switch (cmd) { |
| 796 | case NVM_INFO: |
| 797 | return nvm_ioctl_info(file, argp); |
| 798 | case NVM_GET_DEVICES: |
| 799 | return nvm_ioctl_get_devices(file, argp); |
| 800 | case NVM_DEV_CREATE: |
| 801 | return nvm_ioctl_dev_create(file, argp); |
| 802 | case NVM_DEV_REMOVE: |
| 803 | return nvm_ioctl_dev_remove(file, argp); |
| 804 | } |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static const struct file_operations _ctl_fops = { |
| 809 | .open = nonseekable_open, |
| 810 | .unlocked_ioctl = nvm_ctl_ioctl, |
| 811 | .owner = THIS_MODULE, |
| 812 | .llseek = noop_llseek, |
| 813 | }; |
| 814 | |
| 815 | static struct miscdevice _nvm_misc = { |
| 816 | .minor = MISC_DYNAMIC_MINOR, |
| 817 | .name = "lightnvm", |
| 818 | .nodename = "lightnvm/control", |
| 819 | .fops = &_ctl_fops, |
| 820 | }; |
| 821 | |
| 822 | MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR); |
| 823 | |
| 824 | static int __init nvm_mod_init(void) |
| 825 | { |
| 826 | int ret; |
| 827 | |
| 828 | ret = misc_register(&_nvm_misc); |
| 829 | if (ret) |
| 830 | pr_err("nvm: misc_register failed for control device"); |
| 831 | |
| 832 | return ret; |
| 833 | } |
| 834 | |
| 835 | static void __exit nvm_mod_exit(void) |
| 836 | { |
| 837 | misc_deregister(&_nvm_misc); |
| 838 | } |
| 839 | |
| 840 | MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>"); |
| 841 | MODULE_LICENSE("GPL v2"); |
| 842 | MODULE_VERSION("0.1"); |
| 843 | module_init(nvm_mod_init); |
| 844 | module_exit(nvm_mod_exit); |