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