2 * Copyright (C) 2015 IT University of Copenhagen. All rights reserved.
3 * Initial release: Matias Bjorling <m@bjorling.me>
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.
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.
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,
21 #include <linux/list.h>
22 #include <linux/types.h>
23 #include <linux/sem.h>
24 #include <linux/bitmap.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/miscdevice.h>
28 #include <linux/lightnvm.h>
29 #include <linux/sched/sysctl.h>
31 static LIST_HEAD(nvm_tgt_types);
32 static DECLARE_RWSEM(nvm_tgtt_lock);
33 static LIST_HEAD(nvm_devices);
34 static DECLARE_RWSEM(nvm_lock);
36 /* Map between virtual and physical channel and lun */
44 struct nvm_ch_map *chnls;
48 static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
50 struct nvm_target *tgt;
52 list_for_each_entry(tgt, &dev->targets, list)
53 if (!strcmp(name, tgt->disk->disk_name))
59 static bool nvm_target_exists(const char *name)
62 struct nvm_target *tgt;
65 down_write(&nvm_lock);
66 list_for_each_entry(dev, &nvm_devices, devices) {
67 mutex_lock(&dev->mlock);
68 list_for_each_entry(tgt, &dev->targets, list) {
69 if (!strcmp(name, tgt->disk->disk_name)) {
71 mutex_unlock(&dev->mlock);
75 mutex_unlock(&dev->mlock);
83 static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
87 for (i = lun_begin; i <= lun_end; i++) {
88 if (test_and_set_bit(i, dev->lun_map)) {
89 pr_err("nvm: lun %d already allocated\n", i);
96 while (--i >= lun_begin)
97 clear_bit(i, dev->lun_map);
102 static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
107 for (i = lun_begin; i <= lun_end; i++)
108 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
111 static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
113 struct nvm_dev *dev = tgt_dev->parent;
114 struct nvm_dev_map *dev_map = tgt_dev->map;
117 for (i = 0; i < dev_map->nr_chnls; i++) {
118 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
119 int *lun_offs = ch_map->lun_offs;
120 int ch = i + ch_map->ch_off;
123 for (j = 0; j < ch_map->nr_luns; j++) {
124 int lun = j + lun_offs[j];
125 int lunid = (ch * dev->geo.nr_luns) + lun;
127 WARN_ON(!test_and_clear_bit(lunid,
132 kfree(ch_map->lun_offs);
135 kfree(dev_map->chnls);
138 kfree(tgt_dev->luns);
142 static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
143 int lun_begin, int lun_end)
145 struct nvm_tgt_dev *tgt_dev = NULL;
146 struct nvm_dev_map *dev_rmap = dev->rmap;
147 struct nvm_dev_map *dev_map;
148 struct ppa_addr *luns;
149 int nr_luns = lun_end - lun_begin + 1;
150 int luns_left = nr_luns;
151 int nr_chnls = nr_luns / dev->geo.nr_luns;
152 int nr_chnls_mod = nr_luns % dev->geo.nr_luns;
153 int bch = lun_begin / dev->geo.nr_luns;
154 int blun = lun_begin % dev->geo.nr_luns;
156 int lun_balanced = 1;
160 nr_chnls = (nr_chnls_mod == 0) ? nr_chnls : nr_chnls + 1;
162 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
166 dev_map->chnls = kcalloc(nr_chnls, sizeof(struct nvm_ch_map),
171 luns = kcalloc(nr_luns, sizeof(struct ppa_addr), GFP_KERNEL);
175 prev_nr_luns = (luns_left > dev->geo.nr_luns) ?
176 dev->geo.nr_luns : luns_left;
177 for (i = 0; i < nr_chnls; i++) {
178 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
179 int *lun_roffs = ch_rmap->lun_offs;
180 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
182 int luns_in_chnl = (luns_left > dev->geo.nr_luns) ?
183 dev->geo.nr_luns : luns_left;
185 if (lun_balanced && prev_nr_luns != luns_in_chnl)
188 ch_map->ch_off = ch_rmap->ch_off = bch;
189 ch_map->nr_luns = luns_in_chnl;
191 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
195 for (j = 0; j < luns_in_chnl; j++) {
197 luns[lunid].g.ch = i;
198 luns[lunid++].g.lun = j;
201 lun_roffs[j + blun] = blun;
204 ch_map->lun_offs = lun_offs;
206 /* when starting a new channel, lun offset is reset */
208 luns_left -= luns_in_chnl;
211 dev_map->nr_chnls = nr_chnls;
213 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
217 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
218 /* Target device only owns a portion of the physical device */
219 tgt_dev->geo.nr_chnls = nr_chnls;
220 tgt_dev->geo.all_luns = nr_luns;
221 tgt_dev->geo.nr_luns = (lun_balanced) ? prev_nr_luns : -1;
222 tgt_dev->total_secs = nr_luns * tgt_dev->geo.sec_per_lun;
224 tgt_dev->map = dev_map;
225 tgt_dev->luns = luns;
226 memcpy(&tgt_dev->identity, &dev->identity, sizeof(struct nvm_id));
228 tgt_dev->parent = dev;
233 kfree(dev_map->chnls[i].lun_offs);
236 kfree(dev_map->chnls);
243 static const struct block_device_operations nvm_fops = {
244 .owner = THIS_MODULE,
247 static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
249 struct nvm_tgt_type *tt;
251 list_for_each_entry(tt, &nvm_tgt_types, list)
252 if (!strcmp(name, tt->name))
258 static struct nvm_tgt_type *nvm_find_target_type(const char *name)
260 struct nvm_tgt_type *tt;
262 down_write(&nvm_tgtt_lock);
263 tt = __nvm_find_target_type(name);
264 up_write(&nvm_tgtt_lock);
269 static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
271 struct nvm_ioctl_create_simple *s = &create->conf.s;
272 struct request_queue *tqueue;
273 struct gendisk *tdisk;
274 struct nvm_tgt_type *tt;
275 struct nvm_target *t;
276 struct nvm_tgt_dev *tgt_dev;
280 tt = nvm_find_target_type(create->tgttype);
282 pr_err("nvm: target type %s not found\n", create->tgttype);
286 if (nvm_target_exists(create->tgtname)) {
287 pr_err("nvm: target name already exists (%s)\n",
292 ret = nvm_reserve_luns(dev, s->lun_begin, s->lun_end);
296 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
302 tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end);
304 pr_err("nvm: could not create target device\n");
309 tdisk = alloc_disk(0);
315 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
320 blk_queue_make_request(tqueue, tt->make_rq);
322 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
323 tdisk->flags = GENHD_FL_EXT_DEVT;
325 tdisk->first_minor = 0;
326 tdisk->fops = &nvm_fops;
327 tdisk->queue = tqueue;
329 targetdata = tt->init(tgt_dev, tdisk, create->flags);
330 if (IS_ERR(targetdata)) {
331 ret = PTR_ERR(targetdata);
335 tdisk->private_data = targetdata;
336 tqueue->queuedata = targetdata;
338 blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
340 set_capacity(tdisk, tt->capacity(targetdata));
343 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
352 mutex_lock(&dev->mlock);
353 list_add_tail(&t->list, &dev->targets);
354 mutex_unlock(&dev->mlock);
356 __module_get(tt->owner);
361 tt->exit(targetdata);
363 blk_cleanup_queue(tqueue);
368 nvm_remove_tgt_dev(tgt_dev, 0);
372 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
376 static void __nvm_remove_target(struct nvm_target *t)
378 struct nvm_tgt_type *tt = t->type;
379 struct gendisk *tdisk = t->disk;
380 struct request_queue *q = tdisk->queue;
383 blk_cleanup_queue(q);
386 tt->sysfs_exit(tdisk);
389 tt->exit(tdisk->private_data);
391 nvm_remove_tgt_dev(t->dev, 1);
393 module_put(t->type->owner);
400 * nvm_remove_tgt - Removes a target from the media manager
402 * @remove: ioctl structure with target name to remove.
409 static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
411 struct nvm_target *t;
413 mutex_lock(&dev->mlock);
414 t = nvm_find_target(dev, remove->tgtname);
416 mutex_unlock(&dev->mlock);
419 __nvm_remove_target(t);
420 mutex_unlock(&dev->mlock);
425 static int nvm_register_map(struct nvm_dev *dev)
427 struct nvm_dev_map *rmap;
430 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
434 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
439 for (i = 0; i < dev->geo.nr_chnls; i++) {
440 struct nvm_ch_map *ch_rmap;
442 int luns_in_chnl = dev->geo.nr_luns;
444 ch_rmap = &rmap->chnls[i];
446 ch_rmap->ch_off = -1;
447 ch_rmap->nr_luns = luns_in_chnl;
449 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
453 for (j = 0; j < luns_in_chnl; j++)
456 ch_rmap->lun_offs = lun_roffs;
464 kfree(rmap->chnls[i].lun_offs);
471 static void nvm_unregister_map(struct nvm_dev *dev)
473 struct nvm_dev_map *rmap = dev->rmap;
476 for (i = 0; i < dev->geo.nr_chnls; i++)
477 kfree(rmap->chnls[i].lun_offs);
483 static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
485 struct nvm_dev_map *dev_map = tgt_dev->map;
486 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
487 int lun_off = ch_map->lun_offs[p->g.lun];
489 p->g.ch += ch_map->ch_off;
493 static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
495 struct nvm_dev *dev = tgt_dev->parent;
496 struct nvm_dev_map *dev_rmap = dev->rmap;
497 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
498 int lun_roff = ch_rmap->lun_offs[p->g.lun];
500 p->g.ch -= ch_rmap->ch_off;
501 p->g.lun -= lun_roff;
504 static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
505 struct ppa_addr *ppa_list, int nr_ppas)
509 for (i = 0; i < nr_ppas; i++) {
510 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
511 ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
515 static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
516 struct ppa_addr *ppa_list, int nr_ppas)
520 for (i = 0; i < nr_ppas; i++) {
521 ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
522 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
526 static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
528 if (rqd->nr_ppas == 1) {
529 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
533 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
536 static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
538 if (rqd->nr_ppas == 1) {
539 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
543 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
546 int nvm_register_tgt_type(struct nvm_tgt_type *tt)
550 down_write(&nvm_tgtt_lock);
551 if (__nvm_find_target_type(tt->name))
554 list_add(&tt->list, &nvm_tgt_types);
555 up_write(&nvm_tgtt_lock);
559 EXPORT_SYMBOL(nvm_register_tgt_type);
561 void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
566 down_write(&nvm_tgtt_lock);
568 up_write(&nvm_tgtt_lock);
570 EXPORT_SYMBOL(nvm_unregister_tgt_type);
572 void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
573 dma_addr_t *dma_handler)
575 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
578 EXPORT_SYMBOL(nvm_dev_dma_alloc);
580 void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
582 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
584 EXPORT_SYMBOL(nvm_dev_dma_free);
586 static struct nvm_dev *nvm_find_nvm_dev(const char *name)
590 list_for_each_entry(dev, &nvm_devices, devices)
591 if (!strcmp(name, dev->name))
597 static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
598 const struct ppa_addr *ppas, int nr_ppas)
600 struct nvm_dev *dev = tgt_dev->parent;
601 struct nvm_geo *geo = &tgt_dev->geo;
602 int i, plane_cnt, pl_idx;
605 if (geo->plane_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
606 rqd->nr_ppas = nr_ppas;
607 rqd->ppa_addr = ppas[0];
612 rqd->nr_ppas = nr_ppas;
613 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
614 if (!rqd->ppa_list) {
615 pr_err("nvm: failed to allocate dma memory\n");
619 plane_cnt = geo->plane_mode;
620 rqd->nr_ppas *= plane_cnt;
622 for (i = 0; i < nr_ppas; i++) {
623 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
626 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
633 static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
639 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
643 int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
644 int nr_ppas, int type)
646 struct nvm_dev *dev = tgt_dev->parent;
650 if (nr_ppas > dev->ops->max_phys_sect) {
651 pr_err("nvm: unable to update all blocks atomically\n");
655 memset(&rqd, 0, sizeof(struct nvm_rq));
657 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
658 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
660 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
661 nvm_free_rqd_ppalist(tgt_dev, &rqd);
663 pr_err("nvm: failed bb mark\n");
669 EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
671 int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
673 struct nvm_dev *dev = tgt_dev->parent;
675 return dev->ops->max_phys_sect;
677 EXPORT_SYMBOL(nvm_max_phys_sects);
679 int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
681 struct nvm_dev *dev = tgt_dev->parent;
684 if (!dev->ops->submit_io)
687 nvm_rq_tgt_to_dev(tgt_dev, rqd);
691 /* In case of error, fail with right address format */
692 ret = dev->ops->submit_io(dev, rqd);
694 nvm_rq_dev_to_tgt(tgt_dev, rqd);
697 EXPORT_SYMBOL(nvm_submit_io);
699 int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
701 struct nvm_dev *dev = tgt_dev->parent;
704 if (!dev->ops->submit_io_sync)
707 nvm_rq_tgt_to_dev(tgt_dev, rqd);
711 /* In case of error, fail with right address format */
712 ret = dev->ops->submit_io_sync(dev, rqd);
713 nvm_rq_dev_to_tgt(tgt_dev, rqd);
717 EXPORT_SYMBOL(nvm_submit_io_sync);
719 void nvm_end_io(struct nvm_rq *rqd)
721 struct nvm_tgt_dev *tgt_dev = rqd->dev;
723 /* Convert address space */
725 nvm_rq_dev_to_tgt(tgt_dev, rqd);
730 EXPORT_SYMBOL(nvm_end_io);
733 * folds a bad block list from its plane representation to its virtual
734 * block representation. The fold is done in place and reduced size is
737 * If any of the planes status are bad or grown bad block, the virtual block
738 * is marked bad. If not bad, the first plane state acts as the block state.
740 int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
742 struct nvm_geo *geo = &dev->geo;
743 int blk, offset, pl, blktype;
745 if (nr_blks != geo->nr_chks * geo->plane_mode)
748 for (blk = 0; blk < geo->nr_chks; blk++) {
749 offset = blk * geo->plane_mode;
750 blktype = blks[offset];
752 /* Bad blocks on any planes take precedence over other types */
753 for (pl = 0; pl < geo->plane_mode; pl++) {
754 if (blks[offset + pl] &
755 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
756 blktype = blks[offset + pl];
766 EXPORT_SYMBOL(nvm_bb_tbl_fold);
768 int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
771 struct nvm_dev *dev = tgt_dev->parent;
773 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
775 return dev->ops->get_bb_tbl(dev, ppa, blks);
777 EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
779 static int nvm_core_init(struct nvm_dev *dev)
781 struct nvm_id *id = &dev->identity;
782 struct nvm_id_group *grp = &id->grp;
783 struct nvm_geo *geo = &dev->geo;
786 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
788 if (grp->mtype != 0) {
789 pr_err("nvm: memory type not supported\n");
793 /* Whole device values */
794 geo->nr_chnls = grp->num_ch;
795 geo->nr_luns = grp->num_lun;
797 /* Generic device geometry values */
798 geo->ws_min = grp->ws_min;
799 geo->ws_opt = grp->ws_opt;
800 geo->ws_seq = grp->ws_seq;
801 geo->ws_per_chk = grp->ws_per_chk;
802 geo->nr_chks = grp->num_chk;
803 geo->sec_size = grp->csecs;
804 geo->oob_size = grp->sos;
805 geo->mccap = grp->mccap;
806 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
808 geo->sec_per_chk = grp->clba;
809 geo->sec_per_lun = geo->sec_per_chk * geo->nr_chks;
810 geo->all_luns = geo->nr_luns * geo->nr_chnls;
812 /* 1.2 spec device geometry values */
813 geo->plane_mode = 1 << geo->ws_seq;
814 geo->nr_planes = geo->ws_opt / geo->ws_min;
815 geo->sec_per_pg = geo->ws_min;
816 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
818 dev->total_secs = geo->all_luns * geo->sec_per_lun;
819 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
820 sizeof(unsigned long), GFP_KERNEL);
824 INIT_LIST_HEAD(&dev->area_list);
825 INIT_LIST_HEAD(&dev->targets);
826 mutex_init(&dev->mlock);
827 spin_lock_init(&dev->lock);
829 ret = nvm_register_map(dev);
833 blk_queue_logical_block_size(dev->q, geo->sec_size);
840 static void nvm_free(struct nvm_dev *dev)
846 dev->ops->destroy_dma_pool(dev->dma_pool);
848 nvm_unregister_map(dev);
853 static int nvm_init(struct nvm_dev *dev)
855 struct nvm_geo *geo = &dev->geo;
858 if (dev->ops->identity(dev, &dev->identity)) {
859 pr_err("nvm: device could not be identified\n");
863 pr_debug("nvm: ver:%x nvm_vendor:%x\n",
864 dev->identity.ver_id, dev->identity.vmnt);
866 if (dev->identity.ver_id != 1) {
867 pr_err("nvm: device not supported by kernel.");
871 ret = nvm_core_init(dev);
873 pr_err("nvm: could not initialize core structures.\n");
877 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
878 dev->name, geo->sec_per_pg, geo->nr_planes,
879 geo->ws_per_chk, geo->nr_chks,
880 geo->all_luns, geo->nr_chnls);
883 pr_err("nvm: failed to initialize nvm\n");
887 struct nvm_dev *nvm_alloc_dev(int node)
889 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
891 EXPORT_SYMBOL(nvm_alloc_dev);
893 int nvm_register(struct nvm_dev *dev)
897 if (!dev->q || !dev->ops)
900 if (dev->ops->max_phys_sect > 256) {
901 pr_info("nvm: max sectors supported is 256.\n");
905 if (dev->ops->max_phys_sect > 1) {
906 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
907 if (!dev->dma_pool) {
908 pr_err("nvm: could not create dma pool\n");
917 /* register device with a supported media manager */
918 down_write(&nvm_lock);
919 list_add(&dev->devices, &nvm_devices);
924 dev->ops->destroy_dma_pool(dev->dma_pool);
927 EXPORT_SYMBOL(nvm_register);
929 void nvm_unregister(struct nvm_dev *dev)
931 struct nvm_target *t, *tmp;
933 mutex_lock(&dev->mlock);
934 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
935 if (t->dev->parent != dev)
937 __nvm_remove_target(t);
939 mutex_unlock(&dev->mlock);
941 down_write(&nvm_lock);
942 list_del(&dev->devices);
947 EXPORT_SYMBOL(nvm_unregister);
949 static int __nvm_configure_create(struct nvm_ioctl_create *create)
952 struct nvm_ioctl_create_simple *s;
954 down_write(&nvm_lock);
955 dev = nvm_find_nvm_dev(create->dev);
959 pr_err("nvm: device not found\n");
963 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
964 pr_err("nvm: config type not valid\n");
969 if (s->lun_begin == -1 && s->lun_end == -1) {
971 s->lun_end = dev->geo.all_luns - 1;
974 if (s->lun_begin > s->lun_end || s->lun_end >= dev->geo.all_luns) {
975 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
976 s->lun_begin, s->lun_end, dev->geo.all_luns - 1);
980 return nvm_create_tgt(dev, create);
983 static long nvm_ioctl_info(struct file *file, void __user *arg)
985 struct nvm_ioctl_info *info;
986 struct nvm_tgt_type *tt;
989 if (!capable(CAP_SYS_ADMIN))
992 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
996 info->version[0] = NVM_VERSION_MAJOR;
997 info->version[1] = NVM_VERSION_MINOR;
998 info->version[2] = NVM_VERSION_PATCH;
1000 down_write(&nvm_tgtt_lock);
1001 list_for_each_entry(tt, &nvm_tgt_types, list) {
1002 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1004 tgt->version[0] = tt->version[0];
1005 tgt->version[1] = tt->version[1];
1006 tgt->version[2] = tt->version[2];
1007 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1012 info->tgtsize = tgt_iter;
1013 up_write(&nvm_tgtt_lock);
1015 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1024 static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1026 struct nvm_ioctl_get_devices *devices;
1027 struct nvm_dev *dev;
1030 if (!capable(CAP_SYS_ADMIN))
1033 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1037 down_write(&nvm_lock);
1038 list_for_each_entry(dev, &nvm_devices, devices) {
1039 struct nvm_ioctl_device_info *info = &devices->info[i];
1041 strlcpy(info->devname, dev->name, sizeof(info->devname));
1043 /* kept for compatibility */
1044 info->bmversion[0] = 1;
1045 info->bmversion[1] = 0;
1046 info->bmversion[2] = 0;
1047 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
1051 pr_err("nvm: max 31 devices can be reported.\n");
1055 up_write(&nvm_lock);
1057 devices->nr_devices = i;
1059 if (copy_to_user(arg, devices,
1060 sizeof(struct nvm_ioctl_get_devices))) {
1069 static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1071 struct nvm_ioctl_create create;
1073 if (!capable(CAP_SYS_ADMIN))
1076 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1079 create.dev[DISK_NAME_LEN - 1] = '\0';
1080 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1081 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1083 if (create.flags != 0) {
1084 __u32 flags = create.flags;
1086 /* Check for valid flags */
1087 if (flags & NVM_TARGET_FACTORY)
1088 flags &= ~NVM_TARGET_FACTORY;
1091 pr_err("nvm: flag not supported\n");
1096 return __nvm_configure_create(&create);
1099 static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1101 struct nvm_ioctl_remove remove;
1102 struct nvm_dev *dev;
1105 if (!capable(CAP_SYS_ADMIN))
1108 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1111 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1113 if (remove.flags != 0) {
1114 pr_err("nvm: no flags supported\n");
1118 list_for_each_entry(dev, &nvm_devices, devices) {
1119 ret = nvm_remove_tgt(dev, &remove);
1127 /* kept for compatibility reasons */
1128 static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1130 struct nvm_ioctl_dev_init init;
1132 if (!capable(CAP_SYS_ADMIN))
1135 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1138 if (init.flags != 0) {
1139 pr_err("nvm: no flags supported\n");
1146 /* Kept for compatibility reasons */
1147 static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1149 struct nvm_ioctl_dev_factory fact;
1151 if (!capable(CAP_SYS_ADMIN))
1154 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1157 fact.dev[DISK_NAME_LEN - 1] = '\0';
1159 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1165 static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1167 void __user *argp = (void __user *)arg;
1171 return nvm_ioctl_info(file, argp);
1172 case NVM_GET_DEVICES:
1173 return nvm_ioctl_get_devices(file, argp);
1174 case NVM_DEV_CREATE:
1175 return nvm_ioctl_dev_create(file, argp);
1176 case NVM_DEV_REMOVE:
1177 return nvm_ioctl_dev_remove(file, argp);
1179 return nvm_ioctl_dev_init(file, argp);
1180 case NVM_DEV_FACTORY:
1181 return nvm_ioctl_dev_factory(file, argp);
1186 static const struct file_operations _ctl_fops = {
1187 .open = nonseekable_open,
1188 .unlocked_ioctl = nvm_ctl_ioctl,
1189 .owner = THIS_MODULE,
1190 .llseek = noop_llseek,
1193 static struct miscdevice _nvm_misc = {
1194 .minor = MISC_DYNAMIC_MINOR,
1196 .nodename = "lightnvm/control",
1199 builtin_misc_device(_nvm_misc);