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