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/moduleparam.h>
26 #include <linux/miscdevice.h>
27 #include <linux/lightnvm.h>
28 #include <linux/sched/sysctl.h>
30 static LIST_HEAD(nvm_tgt_types);
31 static DECLARE_RWSEM(nvm_tgtt_lock);
32 static LIST_HEAD(nvm_devices);
33 static DECLARE_RWSEM(nvm_lock);
35 /* Map between virtual and physical channel and lun */
43 struct nvm_ch_map *chnls;
48 struct list_head list;
50 sector_t end; /* end is excluded */
54 TRANS_TGT_TO_DEV = 0x0,
55 TRANS_DEV_TO_TGT = 0x1,
58 static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
60 struct nvm_target *tgt;
62 list_for_each_entry(tgt, &dev->targets, list)
63 if (!strcmp(name, tgt->disk->disk_name))
69 static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
73 for (i = lun_begin; i <= lun_end; i++) {
74 if (test_and_set_bit(i, dev->lun_map)) {
75 pr_err("nvm: lun %d already allocated\n", i);
82 while (--i > lun_begin)
83 clear_bit(i, dev->lun_map);
88 static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
93 for (i = lun_begin; i <= lun_end; i++)
94 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
97 static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev)
99 struct nvm_dev *dev = tgt_dev->parent;
100 struct nvm_dev_map *dev_map = tgt_dev->map;
103 for (i = 0; i < dev_map->nr_chnls; i++) {
104 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
105 int *lun_offs = ch_map->lun_offs;
106 int ch = i + ch_map->ch_off;
108 for (j = 0; j < ch_map->nr_luns; j++) {
109 int lun = j + lun_offs[j];
110 int lunid = (ch * dev->geo.luns_per_chnl) + lun;
112 WARN_ON(!test_and_clear_bit(lunid, dev->lun_map));
115 kfree(ch_map->lun_offs);
118 kfree(dev_map->chnls);
121 kfree(tgt_dev->luns);
125 static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
126 int lun_begin, int lun_end)
128 struct nvm_tgt_dev *tgt_dev = NULL;
129 struct nvm_dev_map *dev_rmap = dev->rmap;
130 struct nvm_dev_map *dev_map;
131 struct ppa_addr *luns;
132 int nr_luns = lun_end - lun_begin + 1;
133 int luns_left = nr_luns;
134 int nr_chnls = nr_luns / dev->geo.luns_per_chnl;
135 int nr_chnls_mod = nr_luns % dev->geo.luns_per_chnl;
136 int bch = lun_begin / dev->geo.luns_per_chnl;
137 int blun = lun_begin % dev->geo.luns_per_chnl;
139 int lun_balanced = 1;
143 nr_chnls = nr_luns / dev->geo.luns_per_chnl;
144 nr_chnls = (nr_chnls_mod == 0) ? nr_chnls : nr_chnls + 1;
146 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
150 dev_map->chnls = kcalloc(nr_chnls, sizeof(struct nvm_ch_map),
155 luns = kcalloc(nr_luns, sizeof(struct ppa_addr), GFP_KERNEL);
159 prev_nr_luns = (luns_left > dev->geo.luns_per_chnl) ?
160 dev->geo.luns_per_chnl : luns_left;
161 for (i = 0; i < nr_chnls; i++) {
162 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
163 int *lun_roffs = ch_rmap->lun_offs;
164 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
166 int luns_in_chnl = (luns_left > dev->geo.luns_per_chnl) ?
167 dev->geo.luns_per_chnl : luns_left;
169 if (lun_balanced && prev_nr_luns != luns_in_chnl)
172 ch_map->ch_off = ch_rmap->ch_off = bch;
173 ch_map->nr_luns = luns_in_chnl;
175 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
179 for (j = 0; j < luns_in_chnl; j++) {
181 luns[lunid].g.ch = i;
182 luns[lunid++].g.lun = j;
185 lun_roffs[j + blun] = blun;
188 ch_map->lun_offs = lun_offs;
190 /* when starting a new channel, lun offset is reset */
192 luns_left -= luns_in_chnl;
195 dev_map->nr_chnls = nr_chnls;
197 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
201 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
202 /* Target device only owns a portion of the physical device */
203 tgt_dev->geo.nr_chnls = nr_chnls;
204 tgt_dev->geo.nr_luns = nr_luns;
205 tgt_dev->geo.luns_per_chnl = (lun_balanced) ? prev_nr_luns : -1;
206 tgt_dev->total_secs = nr_luns * tgt_dev->geo.sec_per_lun;
208 tgt_dev->map = dev_map;
209 tgt_dev->luns = luns;
210 memcpy(&tgt_dev->identity, &dev->identity, sizeof(struct nvm_id));
212 tgt_dev->parent = dev;
217 kfree(dev_map->chnls[i].lun_offs);
220 kfree(dev_map->chnls);
227 static const struct block_device_operations nvm_fops = {
228 .owner = THIS_MODULE,
231 static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
233 struct nvm_ioctl_create_simple *s = &create->conf.s;
234 struct request_queue *tqueue;
235 struct gendisk *tdisk;
236 struct nvm_tgt_type *tt;
237 struct nvm_target *t;
238 struct nvm_tgt_dev *tgt_dev;
241 tt = nvm_find_target_type(create->tgttype, 1);
243 pr_err("nvm: target type %s not found\n", create->tgttype);
247 mutex_lock(&dev->mlock);
248 t = nvm_find_target(dev, create->tgtname);
250 pr_err("nvm: target name already exists.\n");
251 mutex_unlock(&dev->mlock);
254 mutex_unlock(&dev->mlock);
256 if (nvm_reserve_luns(dev, s->lun_begin, s->lun_end))
259 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
263 tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end);
265 pr_err("nvm: could not create target device\n");
269 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
272 blk_queue_make_request(tqueue, tt->make_rq);
274 tdisk = alloc_disk(0);
278 sprintf(tdisk->disk_name, "%s", create->tgtname);
279 tdisk->flags = GENHD_FL_EXT_DEVT;
281 tdisk->first_minor = 0;
282 tdisk->fops = &nvm_fops;
283 tdisk->queue = tqueue;
285 targetdata = tt->init(tgt_dev, tdisk);
286 if (IS_ERR(targetdata))
289 tdisk->private_data = targetdata;
290 tqueue->queuedata = targetdata;
292 blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
294 set_capacity(tdisk, tt->capacity(targetdata));
301 mutex_lock(&dev->mlock);
302 list_add_tail(&t->list, &dev->targets);
303 mutex_unlock(&dev->mlock);
309 blk_cleanup_queue(tqueue);
315 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
319 static void __nvm_remove_target(struct nvm_target *t)
321 struct nvm_tgt_type *tt = t->type;
322 struct gendisk *tdisk = t->disk;
323 struct request_queue *q = tdisk->queue;
326 blk_cleanup_queue(q);
329 tt->exit(tdisk->private_data);
331 nvm_remove_tgt_dev(t->dev);
339 * nvm_remove_tgt - Removes a target from the media manager
341 * @remove: ioctl structure with target name to remove.
348 static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
350 struct nvm_target *t;
352 mutex_lock(&dev->mlock);
353 t = nvm_find_target(dev, remove->tgtname);
355 mutex_unlock(&dev->mlock);
358 __nvm_remove_target(t);
359 mutex_unlock(&dev->mlock);
364 static int nvm_register_map(struct nvm_dev *dev)
366 struct nvm_dev_map *rmap;
369 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
373 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
378 for (i = 0; i < dev->geo.nr_chnls; i++) {
379 struct nvm_ch_map *ch_rmap;
381 int luns_in_chnl = dev->geo.luns_per_chnl;
383 ch_rmap = &rmap->chnls[i];
385 ch_rmap->ch_off = -1;
386 ch_rmap->nr_luns = luns_in_chnl;
388 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
392 for (j = 0; j < luns_in_chnl; j++)
395 ch_rmap->lun_offs = lun_roffs;
403 kfree(rmap->chnls[i].lun_offs);
410 static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
412 struct nvm_dev_map *dev_map = tgt_dev->map;
413 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
414 int lun_off = ch_map->lun_offs[p->g.lun];
416 p->g.ch += ch_map->ch_off;
420 static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
422 struct nvm_dev *dev = tgt_dev->parent;
423 struct nvm_dev_map *dev_rmap = dev->rmap;
424 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
425 int lun_roff = ch_rmap->lun_offs[p->g.lun];
427 p->g.ch -= ch_rmap->ch_off;
428 p->g.lun -= lun_roff;
431 static void nvm_trans_rq(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
436 if (rqd->nr_ppas == 1) {
437 if (flag == TRANS_TGT_TO_DEV)
438 nvm_map_to_dev(tgt_dev, &rqd->ppa_addr);
440 nvm_map_to_tgt(tgt_dev, &rqd->ppa_addr);
444 for (i = 0; i < rqd->nr_ppas; i++) {
445 if (flag == TRANS_TGT_TO_DEV)
446 nvm_map_to_dev(tgt_dev, &rqd->ppa_list[i]);
448 nvm_map_to_tgt(tgt_dev, &rqd->ppa_list[i]);
452 static struct ppa_addr nvm_trans_ppa(struct nvm_tgt_dev *tgt_dev,
453 struct ppa_addr p, int dir)
455 struct ppa_addr ppa = p;
457 if (dir == TRANS_TGT_TO_DEV)
458 nvm_map_to_dev(tgt_dev, &ppa);
460 nvm_map_to_tgt(tgt_dev, &ppa);
465 void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries,
468 struct nvm_geo *geo = &dev->geo;
469 struct nvm_dev_map *dev_rmap = dev->rmap;
472 for (i = 0; i < len; i++) {
473 struct nvm_ch_map *ch_rmap;
475 struct ppa_addr gaddr;
476 u64 pba = le64_to_cpu(entries[i]);
483 gaddr = linear_to_generic_addr(geo, pba);
484 ch_rmap = &dev_rmap->chnls[gaddr.g.ch];
485 lun_roffs = ch_rmap->lun_offs;
487 off = gaddr.g.ch * geo->luns_per_chnl + gaddr.g.lun;
489 diff = ((ch_rmap->ch_off * geo->luns_per_chnl) +
490 (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun;
492 entries[i] -= cpu_to_le64(diff);
495 EXPORT_SYMBOL(nvm_part_to_tgt);
497 struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
499 struct nvm_tgt_type *tmp, *tt = NULL;
502 down_write(&nvm_tgtt_lock);
504 list_for_each_entry(tmp, &nvm_tgt_types, list)
505 if (!strcmp(name, tmp->name)) {
511 up_write(&nvm_tgtt_lock);
514 EXPORT_SYMBOL(nvm_find_target_type);
516 int nvm_register_tgt_type(struct nvm_tgt_type *tt)
520 down_write(&nvm_tgtt_lock);
521 if (nvm_find_target_type(tt->name, 0))
524 list_add(&tt->list, &nvm_tgt_types);
525 up_write(&nvm_tgtt_lock);
529 EXPORT_SYMBOL(nvm_register_tgt_type);
531 void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
536 down_write(&nvm_lock);
540 EXPORT_SYMBOL(nvm_unregister_tgt_type);
542 void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
543 dma_addr_t *dma_handler)
545 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
548 EXPORT_SYMBOL(nvm_dev_dma_alloc);
550 void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
552 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
554 EXPORT_SYMBOL(nvm_dev_dma_free);
556 static struct nvm_dev *nvm_find_nvm_dev(const char *name)
560 list_for_each_entry(dev, &nvm_devices, devices)
561 if (!strcmp(name, dev->name))
567 static void nvm_tgt_generic_to_addr_mode(struct nvm_tgt_dev *tgt_dev,
570 struct nvm_dev *dev = tgt_dev->parent;
573 if (rqd->nr_ppas > 1) {
574 for (i = 0; i < rqd->nr_ppas; i++) {
575 rqd->ppa_list[i] = nvm_trans_ppa(tgt_dev,
576 rqd->ppa_list[i], TRANS_TGT_TO_DEV);
577 rqd->ppa_list[i] = generic_to_dev_addr(dev,
581 rqd->ppa_addr = nvm_trans_ppa(tgt_dev, rqd->ppa_addr,
583 rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr);
587 int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
588 int nr_ppas, int type)
590 struct nvm_dev *dev = tgt_dev->parent;
594 if (nr_ppas > dev->ops->max_phys_sect) {
595 pr_err("nvm: unable to update all blocks atomically\n");
599 memset(&rqd, 0, sizeof(struct nvm_rq));
601 nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
602 nvm_tgt_generic_to_addr_mode(tgt_dev, &rqd);
604 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
605 nvm_free_rqd_ppalist(dev, &rqd);
607 pr_err("nvm: failed bb mark\n");
613 EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
615 int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
617 struct nvm_dev *dev = tgt_dev->parent;
619 return dev->ops->max_phys_sect;
621 EXPORT_SYMBOL(nvm_max_phys_sects);
623 int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
625 struct nvm_dev *dev = tgt_dev->parent;
627 if (!dev->ops->submit_io)
630 /* Convert address space */
631 nvm_generic_to_addr_mode(dev, rqd);
634 return dev->ops->submit_io(dev, rqd);
636 EXPORT_SYMBOL(nvm_submit_io);
638 int nvm_erase_blk(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas, int flags)
640 struct nvm_dev *dev = tgt_dev->parent;
644 if (!dev->ops->erase_block)
647 nvm_map_to_dev(tgt_dev, ppas);
649 memset(&rqd, 0, sizeof(struct nvm_rq));
651 ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, 1, 1);
655 nvm_generic_to_addr_mode(dev, &rqd);
659 ret = dev->ops->erase_block(dev, &rqd);
661 nvm_free_rqd_ppalist(dev, &rqd);
665 EXPORT_SYMBOL(nvm_erase_blk);
667 int nvm_get_l2p_tbl(struct nvm_tgt_dev *tgt_dev, u64 slba, u32 nlb,
668 nvm_l2p_update_fn *update_l2p, void *priv)
670 struct nvm_dev *dev = tgt_dev->parent;
672 if (!dev->ops->get_l2p_tbl)
675 return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv);
677 EXPORT_SYMBOL(nvm_get_l2p_tbl);
679 int nvm_get_area(struct nvm_tgt_dev *tgt_dev, sector_t *lba, sector_t len)
681 struct nvm_dev *dev = tgt_dev->parent;
682 struct nvm_geo *geo = &dev->geo;
683 struct nvm_area *area, *prev, *next;
685 sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9;
687 if (len > max_sectors)
690 area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL);
696 spin_lock(&dev->lock);
697 list_for_each_entry(next, &dev->area_list, list) {
698 if (begin + len > next->begin) {
706 if ((begin + len) > max_sectors) {
707 spin_unlock(&dev->lock);
712 area->begin = *lba = begin;
713 area->end = begin + len;
715 if (prev) /* insert into sorted order */
716 list_add(&area->list, &prev->list);
718 list_add(&area->list, &dev->area_list);
719 spin_unlock(&dev->lock);
723 EXPORT_SYMBOL(nvm_get_area);
725 void nvm_put_area(struct nvm_tgt_dev *tgt_dev, sector_t begin)
727 struct nvm_dev *dev = tgt_dev->parent;
728 struct nvm_area *area;
730 spin_lock(&dev->lock);
731 list_for_each_entry(area, &dev->area_list, list) {
732 if (area->begin != begin)
735 list_del(&area->list);
736 spin_unlock(&dev->lock);
740 spin_unlock(&dev->lock);
742 EXPORT_SYMBOL(nvm_put_area);
744 void nvm_addr_to_generic_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
748 if (rqd->nr_ppas > 1) {
749 for (i = 0; i < rqd->nr_ppas; i++)
750 rqd->ppa_list[i] = dev_to_generic_addr(dev,
753 rqd->ppa_addr = dev_to_generic_addr(dev, rqd->ppa_addr);
756 EXPORT_SYMBOL(nvm_addr_to_generic_mode);
758 void nvm_generic_to_addr_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
762 if (rqd->nr_ppas > 1) {
763 for (i = 0; i < rqd->nr_ppas; i++)
764 rqd->ppa_list[i] = generic_to_dev_addr(dev,
767 rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr);
770 EXPORT_SYMBOL(nvm_generic_to_addr_mode);
772 int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd,
773 const struct ppa_addr *ppas, int nr_ppas, int vblk)
775 struct nvm_geo *geo = &dev->geo;
776 int i, plane_cnt, pl_idx;
779 if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
780 rqd->nr_ppas = nr_ppas;
781 rqd->ppa_addr = ppas[0];
786 rqd->nr_ppas = nr_ppas;
787 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
788 if (!rqd->ppa_list) {
789 pr_err("nvm: failed to allocate dma memory\n");
794 for (i = 0; i < nr_ppas; i++)
795 rqd->ppa_list[i] = ppas[i];
797 plane_cnt = geo->plane_mode;
798 rqd->nr_ppas *= plane_cnt;
800 for (i = 0; i < nr_ppas; i++) {
801 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
804 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
811 EXPORT_SYMBOL(nvm_set_rqd_ppalist);
813 void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd)
818 nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list);
820 EXPORT_SYMBOL(nvm_free_rqd_ppalist);
822 void nvm_end_io(struct nvm_rq *rqd, int error)
824 struct nvm_tgt_dev *tgt_dev = rqd->dev;
825 struct nvm_tgt_instance *ins = rqd->ins;
827 /* Convert address space */
829 nvm_trans_rq(tgt_dev, rqd, TRANS_DEV_TO_TGT);
832 ins->tt->end_io(rqd);
834 EXPORT_SYMBOL(nvm_end_io);
837 * folds a bad block list from its plane representation to its virtual
838 * block representation. The fold is done in place and reduced size is
841 * If any of the planes status are bad or grown bad block, the virtual block
842 * is marked bad. If not bad, the first plane state acts as the block state.
844 int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
846 struct nvm_geo *geo = &dev->geo;
847 int blk, offset, pl, blktype;
849 if (nr_blks != geo->blks_per_lun * geo->plane_mode)
852 for (blk = 0; blk < geo->blks_per_lun; blk++) {
853 offset = blk * geo->plane_mode;
854 blktype = blks[offset];
856 /* Bad blocks on any planes take precedence over other types */
857 for (pl = 0; pl < geo->plane_mode; pl++) {
858 if (blks[offset + pl] &
859 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
860 blktype = blks[offset + pl];
868 return geo->blks_per_lun;
870 EXPORT_SYMBOL(nvm_bb_tbl_fold);
872 int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
875 struct nvm_dev *dev = tgt_dev->parent;
877 ppa = nvm_trans_ppa(tgt_dev, ppa, TRANS_TGT_TO_DEV);
878 ppa = generic_to_dev_addr(dev, ppa);
880 return dev->ops->get_bb_tbl(dev, ppa, blks);
882 EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
884 static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
886 struct nvm_geo *geo = &dev->geo;
889 dev->lps_per_blk = geo->pgs_per_blk;
890 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
894 /* Just a linear array */
895 for (i = 0; i < dev->lps_per_blk; i++)
901 static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
904 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
909 dev->lps_per_blk = mlc->num_pairs;
910 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
914 /* The lower page table encoding consists of a list of bytes, where each
915 * has a lower and an upper half. The first half byte maintains the
916 * increment value and every value after is an offset added to the
917 * previous incrementation value
919 dev->lptbl[0] = mlc->pairs[0] & 0xF;
920 for (i = 1; i < dev->lps_per_blk; i++) {
921 p = mlc->pairs[i >> 1];
922 if (i & 0x1) /* upper */
923 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
925 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
931 static int nvm_core_init(struct nvm_dev *dev)
933 struct nvm_id *id = &dev->identity;
934 struct nvm_id_group *grp = &id->groups[0];
935 struct nvm_geo *geo = &dev->geo;
938 /* Whole device values */
939 geo->nr_chnls = grp->num_ch;
940 geo->luns_per_chnl = grp->num_lun;
942 /* Generic device values */
943 geo->pgs_per_blk = grp->num_pg;
944 geo->blks_per_lun = grp->num_blk;
945 geo->nr_planes = grp->num_pln;
946 geo->fpg_size = grp->fpg_sz;
947 geo->pfpg_size = grp->fpg_sz * grp->num_pln;
948 geo->sec_size = grp->csecs;
949 geo->oob_size = grp->sos;
950 geo->sec_per_pg = grp->fpg_sz / grp->csecs;
951 geo->mccap = grp->mccap;
952 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
954 geo->plane_mode = NVM_PLANE_SINGLE;
955 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
957 if (grp->mpos & 0x020202)
958 geo->plane_mode = NVM_PLANE_DOUBLE;
959 if (grp->mpos & 0x040404)
960 geo->plane_mode = NVM_PLANE_QUAD;
962 if (grp->mtype != 0) {
963 pr_err("nvm: memory type not supported\n");
967 /* calculated values */
968 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
969 geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
970 geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
971 geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
973 dev->total_secs = geo->nr_luns * geo->sec_per_lun;
974 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
975 sizeof(unsigned long), GFP_KERNEL);
979 switch (grp->fmtype) {
980 case NVM_ID_FMTYPE_SLC:
981 if (nvm_init_slc_tbl(dev, grp)) {
986 case NVM_ID_FMTYPE_MLC:
987 if (nvm_init_mlc_tbl(dev, grp)) {
993 pr_err("nvm: flash type not supported\n");
998 INIT_LIST_HEAD(&dev->area_list);
999 INIT_LIST_HEAD(&dev->targets);
1000 mutex_init(&dev->mlock);
1001 spin_lock_init(&dev->lock);
1003 ret = nvm_register_map(dev);
1007 blk_queue_logical_block_size(dev->q, geo->sec_size);
1010 kfree(dev->lun_map);
1014 void nvm_free(struct nvm_dev *dev)
1020 dev->ops->destroy_dma_pool(dev->dma_pool);
1024 kfree(dev->lun_map);
1028 static int nvm_init(struct nvm_dev *dev)
1030 struct nvm_geo *geo = &dev->geo;
1033 if (dev->ops->identity(dev, &dev->identity)) {
1034 pr_err("nvm: device could not be identified\n");
1038 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
1039 dev->identity.ver_id, dev->identity.vmnt,
1040 dev->identity.cgrps);
1042 if (dev->identity.ver_id != 1) {
1043 pr_err("nvm: device not supported by kernel.");
1047 if (dev->identity.cgrps != 1) {
1048 pr_err("nvm: only one group configuration supported.");
1052 ret = nvm_core_init(dev);
1054 pr_err("nvm: could not initialize core structures.\n");
1058 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
1059 dev->name, geo->sec_per_pg, geo->nr_planes,
1060 geo->pgs_per_blk, geo->blks_per_lun,
1061 geo->nr_luns, geo->nr_chnls);
1064 pr_err("nvm: failed to initialize nvm\n");
1068 struct nvm_dev *nvm_alloc_dev(int node)
1070 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1072 EXPORT_SYMBOL(nvm_alloc_dev);
1074 int nvm_register(struct nvm_dev *dev)
1078 if (!dev->q || !dev->ops)
1081 if (dev->ops->max_phys_sect > 256) {
1082 pr_info("nvm: max sectors supported is 256.\n");
1086 if (dev->ops->max_phys_sect > 1) {
1087 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
1088 if (!dev->dma_pool) {
1089 pr_err("nvm: could not create dma pool\n");
1094 ret = nvm_init(dev);
1098 /* register device with a supported media manager */
1099 down_write(&nvm_lock);
1100 list_add(&dev->devices, &nvm_devices);
1101 up_write(&nvm_lock);
1105 dev->ops->destroy_dma_pool(dev->dma_pool);
1108 EXPORT_SYMBOL(nvm_register);
1110 void nvm_unregister(struct nvm_dev *dev)
1112 struct nvm_target *t, *tmp;
1114 mutex_lock(&dev->mlock);
1115 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1116 if (t->dev->parent != dev)
1118 __nvm_remove_target(t);
1120 mutex_unlock(&dev->mlock);
1122 down_write(&nvm_lock);
1123 list_del(&dev->devices);
1124 up_write(&nvm_lock);
1128 EXPORT_SYMBOL(nvm_unregister);
1130 static int __nvm_configure_create(struct nvm_ioctl_create *create)
1132 struct nvm_dev *dev;
1133 struct nvm_ioctl_create_simple *s;
1135 down_write(&nvm_lock);
1136 dev = nvm_find_nvm_dev(create->dev);
1137 up_write(&nvm_lock);
1140 pr_err("nvm: device not found\n");
1144 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
1145 pr_err("nvm: config type not valid\n");
1148 s = &create->conf.s;
1150 if (s->lun_begin > s->lun_end || s->lun_end > dev->geo.nr_luns) {
1151 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
1152 s->lun_begin, s->lun_end, dev->geo.nr_luns);
1156 return nvm_create_tgt(dev, create);
1159 static long nvm_ioctl_info(struct file *file, void __user *arg)
1161 struct nvm_ioctl_info *info;
1162 struct nvm_tgt_type *tt;
1165 if (!capable(CAP_SYS_ADMIN))
1168 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1172 info->version[0] = NVM_VERSION_MAJOR;
1173 info->version[1] = NVM_VERSION_MINOR;
1174 info->version[2] = NVM_VERSION_PATCH;
1176 down_write(&nvm_lock);
1177 list_for_each_entry(tt, &nvm_tgt_types, list) {
1178 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1180 tgt->version[0] = tt->version[0];
1181 tgt->version[1] = tt->version[1];
1182 tgt->version[2] = tt->version[2];
1183 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1188 info->tgtsize = tgt_iter;
1189 up_write(&nvm_lock);
1191 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1200 static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1202 struct nvm_ioctl_get_devices *devices;
1203 struct nvm_dev *dev;
1206 if (!capable(CAP_SYS_ADMIN))
1209 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1213 down_write(&nvm_lock);
1214 list_for_each_entry(dev, &nvm_devices, devices) {
1215 struct nvm_ioctl_device_info *info = &devices->info[i];
1217 sprintf(info->devname, "%s", dev->name);
1219 /* kept for compatibility */
1220 info->bmversion[0] = 1;
1221 info->bmversion[1] = 0;
1222 info->bmversion[2] = 0;
1223 sprintf(info->bmname, "%s", "gennvm");
1227 pr_err("nvm: max 31 devices can be reported.\n");
1231 up_write(&nvm_lock);
1233 devices->nr_devices = i;
1235 if (copy_to_user(arg, devices,
1236 sizeof(struct nvm_ioctl_get_devices))) {
1245 static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1247 struct nvm_ioctl_create create;
1249 if (!capable(CAP_SYS_ADMIN))
1252 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1255 create.dev[DISK_NAME_LEN - 1] = '\0';
1256 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1257 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1259 if (create.flags != 0) {
1260 pr_err("nvm: no flags supported\n");
1264 return __nvm_configure_create(&create);
1267 static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1269 struct nvm_ioctl_remove remove;
1270 struct nvm_dev *dev;
1273 if (!capable(CAP_SYS_ADMIN))
1276 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1279 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1281 if (remove.flags != 0) {
1282 pr_err("nvm: no flags supported\n");
1286 list_for_each_entry(dev, &nvm_devices, devices) {
1287 ret = nvm_remove_tgt(dev, &remove);
1295 /* kept for compatibility reasons */
1296 static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1298 struct nvm_ioctl_dev_init init;
1300 if (!capable(CAP_SYS_ADMIN))
1303 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1306 if (init.flags != 0) {
1307 pr_err("nvm: no flags supported\n");
1314 /* Kept for compatibility reasons */
1315 static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1317 struct nvm_ioctl_dev_factory fact;
1319 if (!capable(CAP_SYS_ADMIN))
1322 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1325 fact.dev[DISK_NAME_LEN - 1] = '\0';
1327 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1333 static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1335 void __user *argp = (void __user *)arg;
1339 return nvm_ioctl_info(file, argp);
1340 case NVM_GET_DEVICES:
1341 return nvm_ioctl_get_devices(file, argp);
1342 case NVM_DEV_CREATE:
1343 return nvm_ioctl_dev_create(file, argp);
1344 case NVM_DEV_REMOVE:
1345 return nvm_ioctl_dev_remove(file, argp);
1347 return nvm_ioctl_dev_init(file, argp);
1348 case NVM_DEV_FACTORY:
1349 return nvm_ioctl_dev_factory(file, argp);
1354 static const struct file_operations _ctl_fops = {
1355 .open = nonseekable_open,
1356 .unlocked_ioctl = nvm_ctl_ioctl,
1357 .owner = THIS_MODULE,
1358 .llseek = noop_llseek,
1361 static struct miscdevice _nvm_misc = {
1362 .minor = MISC_DYNAMIC_MINOR,
1364 .nodename = "lightnvm/control",
1367 builtin_misc_device(_nvm_misc);