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