arm64: futex: Avoid copying out uninitialised stack in failed cmpxchg()
[linux/fpc-iii.git] / drivers / lightnvm / core.c
blob60aa7bc5a6302fcc4bd4229076a50d94ae5e68c8
1 /*
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,
17 * USA.
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 */
37 struct nvm_ch_map {
38 int ch_off;
39 int num_lun;
40 int *lun_offs;
43 struct nvm_dev_map {
44 struct nvm_ch_map *chnls;
45 int num_ch;
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))
54 return tgt;
56 return NULL;
59 static bool nvm_target_exists(const char *name)
61 struct nvm_dev *dev;
62 struct nvm_target *tgt;
63 bool ret = false;
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)) {
70 ret = true;
71 mutex_unlock(&dev->mlock);
72 goto out;
75 mutex_unlock(&dev->mlock);
78 out:
79 up_write(&nvm_lock);
80 return ret;
83 static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
85 int i;
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);
90 goto err;
94 return 0;
95 err:
96 while (--i >= lun_begin)
97 clear_bit(i, dev->lun_map);
99 return -EBUSY;
102 static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
103 int lun_end)
105 int i;
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;
115 int i, j;
117 for (i = 0; i < dev_map->num_ch; 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;
122 if (clear) {
123 for (j = 0; j < ch_map->num_lun; j++) {
124 int lun = j + lun_offs[j];
125 int lunid = (ch * dev->geo.num_lun) + lun;
127 WARN_ON(!test_and_clear_bit(lunid,
128 dev->lun_map));
132 kfree(ch_map->lun_offs);
135 kfree(dev_map->chnls);
136 kfree(dev_map);
138 kfree(tgt_dev->luns);
139 kfree(tgt_dev);
142 static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
143 u16 lun_begin, u16 lun_end,
144 u16 op)
146 struct nvm_tgt_dev *tgt_dev = NULL;
147 struct nvm_dev_map *dev_rmap = dev->rmap;
148 struct nvm_dev_map *dev_map;
149 struct ppa_addr *luns;
150 int num_lun = lun_end - lun_begin + 1;
151 int luns_left = num_lun;
152 int num_ch = num_lun / dev->geo.num_lun;
153 int num_ch_mod = num_lun % dev->geo.num_lun;
154 int bch = lun_begin / dev->geo.num_lun;
155 int blun = lun_begin % dev->geo.num_lun;
156 int lunid = 0;
157 int lun_balanced = 1;
158 int sec_per_lun, prev_num_lun;
159 int i, j;
161 num_ch = (num_ch_mod == 0) ? num_ch : num_ch + 1;
163 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
164 if (!dev_map)
165 goto err_dev;
167 dev_map->chnls = kcalloc(num_ch, sizeof(struct nvm_ch_map), GFP_KERNEL);
168 if (!dev_map->chnls)
169 goto err_chnls;
171 luns = kcalloc(num_lun, sizeof(struct ppa_addr), GFP_KERNEL);
172 if (!luns)
173 goto err_luns;
175 prev_num_lun = (luns_left > dev->geo.num_lun) ?
176 dev->geo.num_lun : luns_left;
177 for (i = 0; i < num_ch; 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];
181 int *lun_offs;
182 int luns_in_chnl = (luns_left > dev->geo.num_lun) ?
183 dev->geo.num_lun : luns_left;
185 if (lun_balanced && prev_num_lun != luns_in_chnl)
186 lun_balanced = 0;
188 ch_map->ch_off = ch_rmap->ch_off = bch;
189 ch_map->num_lun = luns_in_chnl;
191 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
192 if (!lun_offs)
193 goto err_ch;
195 for (j = 0; j < luns_in_chnl; j++) {
196 luns[lunid].ppa = 0;
197 luns[lunid].a.ch = i;
198 luns[lunid++].a.lun = j;
200 lun_offs[j] = blun;
201 lun_roffs[j + blun] = blun;
204 ch_map->lun_offs = lun_offs;
206 /* when starting a new channel, lun offset is reset */
207 blun = 0;
208 luns_left -= luns_in_chnl;
211 dev_map->num_ch = num_ch;
213 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
214 if (!tgt_dev)
215 goto err_ch;
217 /* Inherit device geometry from parent */
218 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
220 /* Target device only owns a portion of the physical device */
221 tgt_dev->geo.num_ch = num_ch;
222 tgt_dev->geo.num_lun = (lun_balanced) ? prev_num_lun : -1;
223 tgt_dev->geo.all_luns = num_lun;
224 tgt_dev->geo.all_chunks = num_lun * dev->geo.num_chk;
226 tgt_dev->geo.op = op;
228 sec_per_lun = dev->geo.clba * dev->geo.num_chk;
229 tgt_dev->geo.total_secs = num_lun * sec_per_lun;
231 tgt_dev->q = dev->q;
232 tgt_dev->map = dev_map;
233 tgt_dev->luns = luns;
234 tgt_dev->parent = dev;
236 return tgt_dev;
237 err_ch:
238 while (--i >= 0)
239 kfree(dev_map->chnls[i].lun_offs);
240 kfree(luns);
241 err_luns:
242 kfree(dev_map->chnls);
243 err_chnls:
244 kfree(dev_map);
245 err_dev:
246 return tgt_dev;
249 static const struct block_device_operations nvm_fops = {
250 .owner = THIS_MODULE,
253 static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
255 struct nvm_tgt_type *tt;
257 list_for_each_entry(tt, &nvm_tgt_types, list)
258 if (!strcmp(name, tt->name))
259 return tt;
261 return NULL;
264 static struct nvm_tgt_type *nvm_find_target_type(const char *name)
266 struct nvm_tgt_type *tt;
268 down_write(&nvm_tgtt_lock);
269 tt = __nvm_find_target_type(name);
270 up_write(&nvm_tgtt_lock);
272 return tt;
275 static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin,
276 int lun_end)
278 if (lun_begin > lun_end || lun_end >= geo->all_luns) {
279 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
280 lun_begin, lun_end, geo->all_luns - 1);
281 return -EINVAL;
284 return 0;
287 static int __nvm_config_simple(struct nvm_dev *dev,
288 struct nvm_ioctl_create_simple *s)
290 struct nvm_geo *geo = &dev->geo;
292 if (s->lun_begin == -1 && s->lun_end == -1) {
293 s->lun_begin = 0;
294 s->lun_end = geo->all_luns - 1;
297 return nvm_config_check_luns(geo, s->lun_begin, s->lun_end);
300 static int __nvm_config_extended(struct nvm_dev *dev,
301 struct nvm_ioctl_create_extended *e)
303 if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) {
304 e->lun_begin = 0;
305 e->lun_end = dev->geo.all_luns - 1;
308 /* op not set falls into target's default */
309 if (e->op == 0xFFFF) {
310 e->op = NVM_TARGET_DEFAULT_OP;
311 } else if (e->op < NVM_TARGET_MIN_OP || e->op > NVM_TARGET_MAX_OP) {
312 pr_err("nvm: invalid over provisioning value\n");
313 return -EINVAL;
316 return nvm_config_check_luns(&dev->geo, e->lun_begin, e->lun_end);
319 static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
321 struct nvm_ioctl_create_extended e;
322 struct request_queue *tqueue;
323 struct gendisk *tdisk;
324 struct nvm_tgt_type *tt;
325 struct nvm_target *t;
326 struct nvm_tgt_dev *tgt_dev;
327 void *targetdata;
328 int ret;
330 switch (create->conf.type) {
331 case NVM_CONFIG_TYPE_SIMPLE:
332 ret = __nvm_config_simple(dev, &create->conf.s);
333 if (ret)
334 return ret;
336 e.lun_begin = create->conf.s.lun_begin;
337 e.lun_end = create->conf.s.lun_end;
338 e.op = NVM_TARGET_DEFAULT_OP;
339 break;
340 case NVM_CONFIG_TYPE_EXTENDED:
341 ret = __nvm_config_extended(dev, &create->conf.e);
342 if (ret)
343 return ret;
345 e = create->conf.e;
346 break;
347 default:
348 pr_err("nvm: config type not valid\n");
349 return -EINVAL;
352 tt = nvm_find_target_type(create->tgttype);
353 if (!tt) {
354 pr_err("nvm: target type %s not found\n", create->tgttype);
355 return -EINVAL;
358 if (nvm_target_exists(create->tgtname)) {
359 pr_err("nvm: target name already exists (%s)\n",
360 create->tgtname);
361 return -EINVAL;
364 ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end);
365 if (ret)
366 return ret;
368 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
369 if (!t) {
370 ret = -ENOMEM;
371 goto err_reserve;
374 tgt_dev = nvm_create_tgt_dev(dev, e.lun_begin, e.lun_end, e.op);
375 if (!tgt_dev) {
376 pr_err("nvm: could not create target device\n");
377 ret = -ENOMEM;
378 goto err_t;
381 tdisk = alloc_disk(0);
382 if (!tdisk) {
383 ret = -ENOMEM;
384 goto err_dev;
387 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node, NULL);
388 if (!tqueue) {
389 ret = -ENOMEM;
390 goto err_disk;
392 blk_queue_make_request(tqueue, tt->make_rq);
394 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
395 tdisk->flags = GENHD_FL_EXT_DEVT;
396 tdisk->major = 0;
397 tdisk->first_minor = 0;
398 tdisk->fops = &nvm_fops;
399 tdisk->queue = tqueue;
401 targetdata = tt->init(tgt_dev, tdisk, create->flags);
402 if (IS_ERR(targetdata)) {
403 ret = PTR_ERR(targetdata);
404 goto err_init;
407 tdisk->private_data = targetdata;
408 tqueue->queuedata = targetdata;
410 blk_queue_max_hw_sectors(tqueue,
411 (dev->geo.csecs >> 9) * NVM_MAX_VLBA);
413 set_capacity(tdisk, tt->capacity(targetdata));
414 add_disk(tdisk);
416 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
417 ret = -ENOMEM;
418 goto err_sysfs;
421 t->type = tt;
422 t->disk = tdisk;
423 t->dev = tgt_dev;
425 mutex_lock(&dev->mlock);
426 list_add_tail(&t->list, &dev->targets);
427 mutex_unlock(&dev->mlock);
429 __module_get(tt->owner);
431 return 0;
432 err_sysfs:
433 if (tt->exit)
434 tt->exit(targetdata, true);
435 err_init:
436 blk_cleanup_queue(tqueue);
437 tdisk->queue = NULL;
438 err_disk:
439 put_disk(tdisk);
440 err_dev:
441 nvm_remove_tgt_dev(tgt_dev, 0);
442 err_t:
443 kfree(t);
444 err_reserve:
445 nvm_release_luns_err(dev, e.lun_begin, e.lun_end);
446 return ret;
449 static void __nvm_remove_target(struct nvm_target *t, bool graceful)
451 struct nvm_tgt_type *tt = t->type;
452 struct gendisk *tdisk = t->disk;
453 struct request_queue *q = tdisk->queue;
455 del_gendisk(tdisk);
456 blk_cleanup_queue(q);
458 if (tt->sysfs_exit)
459 tt->sysfs_exit(tdisk);
461 if (tt->exit)
462 tt->exit(tdisk->private_data, graceful);
464 nvm_remove_tgt_dev(t->dev, 1);
465 put_disk(tdisk);
466 module_put(t->type->owner);
468 list_del(&t->list);
469 kfree(t);
473 * nvm_remove_tgt - Removes a target from the media manager
474 * @dev: device
475 * @remove: ioctl structure with target name to remove.
477 * Returns:
478 * 0: on success
479 * 1: on not found
480 * <0: on error
482 static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
484 struct nvm_target *t;
486 mutex_lock(&dev->mlock);
487 t = nvm_find_target(dev, remove->tgtname);
488 if (!t) {
489 mutex_unlock(&dev->mlock);
490 return 1;
492 __nvm_remove_target(t, true);
493 mutex_unlock(&dev->mlock);
495 return 0;
498 static int nvm_register_map(struct nvm_dev *dev)
500 struct nvm_dev_map *rmap;
501 int i, j;
503 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
504 if (!rmap)
505 goto err_rmap;
507 rmap->chnls = kcalloc(dev->geo.num_ch, sizeof(struct nvm_ch_map),
508 GFP_KERNEL);
509 if (!rmap->chnls)
510 goto err_chnls;
512 for (i = 0; i < dev->geo.num_ch; i++) {
513 struct nvm_ch_map *ch_rmap;
514 int *lun_roffs;
515 int luns_in_chnl = dev->geo.num_lun;
517 ch_rmap = &rmap->chnls[i];
519 ch_rmap->ch_off = -1;
520 ch_rmap->num_lun = luns_in_chnl;
522 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
523 if (!lun_roffs)
524 goto err_ch;
526 for (j = 0; j < luns_in_chnl; j++)
527 lun_roffs[j] = -1;
529 ch_rmap->lun_offs = lun_roffs;
532 dev->rmap = rmap;
534 return 0;
535 err_ch:
536 while (--i >= 0)
537 kfree(rmap->chnls[i].lun_offs);
538 err_chnls:
539 kfree(rmap);
540 err_rmap:
541 return -ENOMEM;
544 static void nvm_unregister_map(struct nvm_dev *dev)
546 struct nvm_dev_map *rmap = dev->rmap;
547 int i;
549 for (i = 0; i < dev->geo.num_ch; i++)
550 kfree(rmap->chnls[i].lun_offs);
552 kfree(rmap->chnls);
553 kfree(rmap);
556 static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
558 struct nvm_dev_map *dev_map = tgt_dev->map;
559 struct nvm_ch_map *ch_map = &dev_map->chnls[p->a.ch];
560 int lun_off = ch_map->lun_offs[p->a.lun];
562 p->a.ch += ch_map->ch_off;
563 p->a.lun += lun_off;
566 static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
568 struct nvm_dev *dev = tgt_dev->parent;
569 struct nvm_dev_map *dev_rmap = dev->rmap;
570 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->a.ch];
571 int lun_roff = ch_rmap->lun_offs[p->a.lun];
573 p->a.ch -= ch_rmap->ch_off;
574 p->a.lun -= lun_roff;
577 static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
578 struct ppa_addr *ppa_list, int nr_ppas)
580 int i;
582 for (i = 0; i < nr_ppas; i++) {
583 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
584 ppa_list[i] = generic_to_dev_addr(tgt_dev->parent, ppa_list[i]);
588 static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
589 struct ppa_addr *ppa_list, int nr_ppas)
591 int i;
593 for (i = 0; i < nr_ppas; i++) {
594 ppa_list[i] = dev_to_generic_addr(tgt_dev->parent, ppa_list[i]);
595 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
599 static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
601 if (rqd->nr_ppas == 1) {
602 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
603 return;
606 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
609 static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
611 if (rqd->nr_ppas == 1) {
612 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
613 return;
616 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
619 int nvm_register_tgt_type(struct nvm_tgt_type *tt)
621 int ret = 0;
623 down_write(&nvm_tgtt_lock);
624 if (__nvm_find_target_type(tt->name))
625 ret = -EEXIST;
626 else
627 list_add(&tt->list, &nvm_tgt_types);
628 up_write(&nvm_tgtt_lock);
630 return ret;
632 EXPORT_SYMBOL(nvm_register_tgt_type);
634 void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
636 if (!tt)
637 return;
639 down_write(&nvm_tgtt_lock);
640 list_del(&tt->list);
641 up_write(&nvm_tgtt_lock);
643 EXPORT_SYMBOL(nvm_unregister_tgt_type);
645 void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
646 dma_addr_t *dma_handler)
648 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
649 dma_handler);
651 EXPORT_SYMBOL(nvm_dev_dma_alloc);
653 void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
655 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
657 EXPORT_SYMBOL(nvm_dev_dma_free);
659 static struct nvm_dev *nvm_find_nvm_dev(const char *name)
661 struct nvm_dev *dev;
663 list_for_each_entry(dev, &nvm_devices, devices)
664 if (!strcmp(name, dev->name))
665 return dev;
667 return NULL;
670 static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
671 const struct ppa_addr *ppas, int nr_ppas)
673 struct nvm_dev *dev = tgt_dev->parent;
674 struct nvm_geo *geo = &tgt_dev->geo;
675 int i, plane_cnt, pl_idx;
676 struct ppa_addr ppa;
678 if (geo->pln_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
679 rqd->nr_ppas = nr_ppas;
680 rqd->ppa_addr = ppas[0];
682 return 0;
685 rqd->nr_ppas = nr_ppas;
686 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
687 if (!rqd->ppa_list) {
688 pr_err("nvm: failed to allocate dma memory\n");
689 return -ENOMEM;
692 plane_cnt = geo->pln_mode;
693 rqd->nr_ppas *= plane_cnt;
695 for (i = 0; i < nr_ppas; i++) {
696 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
697 ppa = ppas[i];
698 ppa.g.pl = pl_idx;
699 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
703 return 0;
706 static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
707 struct nvm_rq *rqd)
709 if (!rqd->ppa_list)
710 return;
712 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
715 int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct nvm_chk_meta *meta,
716 struct ppa_addr ppa, int nchks)
718 struct nvm_dev *dev = tgt_dev->parent;
720 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
722 return dev->ops->get_chk_meta(tgt_dev->parent, meta,
723 (sector_t)ppa.ppa, nchks);
725 EXPORT_SYMBOL(nvm_get_chunk_meta);
727 int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
728 int nr_ppas, int type)
730 struct nvm_dev *dev = tgt_dev->parent;
731 struct nvm_rq rqd;
732 int ret;
734 if (nr_ppas > NVM_MAX_VLBA) {
735 pr_err("nvm: unable to update all blocks atomically\n");
736 return -EINVAL;
739 memset(&rqd, 0, sizeof(struct nvm_rq));
741 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
742 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
744 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
745 nvm_free_rqd_ppalist(tgt_dev, &rqd);
746 if (ret) {
747 pr_err("nvm: failed bb mark\n");
748 return -EINVAL;
751 return 0;
753 EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
755 int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
757 struct nvm_dev *dev = tgt_dev->parent;
758 int ret;
760 if (!dev->ops->submit_io)
761 return -ENODEV;
763 nvm_rq_tgt_to_dev(tgt_dev, rqd);
765 rqd->dev = tgt_dev;
767 /* In case of error, fail with right address format */
768 ret = dev->ops->submit_io(dev, rqd);
769 if (ret)
770 nvm_rq_dev_to_tgt(tgt_dev, rqd);
771 return ret;
773 EXPORT_SYMBOL(nvm_submit_io);
775 int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
777 struct nvm_dev *dev = tgt_dev->parent;
778 int ret;
780 if (!dev->ops->submit_io_sync)
781 return -ENODEV;
783 nvm_rq_tgt_to_dev(tgt_dev, rqd);
785 rqd->dev = tgt_dev;
787 /* In case of error, fail with right address format */
788 ret = dev->ops->submit_io_sync(dev, rqd);
789 nvm_rq_dev_to_tgt(tgt_dev, rqd);
791 return ret;
793 EXPORT_SYMBOL(nvm_submit_io_sync);
795 void nvm_end_io(struct nvm_rq *rqd)
797 struct nvm_tgt_dev *tgt_dev = rqd->dev;
799 /* Convert address space */
800 if (tgt_dev)
801 nvm_rq_dev_to_tgt(tgt_dev, rqd);
803 if (rqd->end_io)
804 rqd->end_io(rqd);
806 EXPORT_SYMBOL(nvm_end_io);
809 * folds a bad block list from its plane representation to its virtual
810 * block representation. The fold is done in place and reduced size is
811 * returned.
813 * If any of the planes status are bad or grown bad block, the virtual block
814 * is marked bad. If not bad, the first plane state acts as the block state.
816 int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
818 struct nvm_geo *geo = &dev->geo;
819 int blk, offset, pl, blktype;
821 if (nr_blks != geo->num_chk * geo->pln_mode)
822 return -EINVAL;
824 for (blk = 0; blk < geo->num_chk; blk++) {
825 offset = blk * geo->pln_mode;
826 blktype = blks[offset];
828 /* Bad blocks on any planes take precedence over other types */
829 for (pl = 0; pl < geo->pln_mode; pl++) {
830 if (blks[offset + pl] &
831 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
832 blktype = blks[offset + pl];
833 break;
837 blks[blk] = blktype;
840 return geo->num_chk;
842 EXPORT_SYMBOL(nvm_bb_tbl_fold);
844 int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
845 u8 *blks)
847 struct nvm_dev *dev = tgt_dev->parent;
849 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
851 return dev->ops->get_bb_tbl(dev, ppa, blks);
853 EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
855 static int nvm_core_init(struct nvm_dev *dev)
857 struct nvm_geo *geo = &dev->geo;
858 int ret;
860 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
861 sizeof(unsigned long), GFP_KERNEL);
862 if (!dev->lun_map)
863 return -ENOMEM;
865 INIT_LIST_HEAD(&dev->area_list);
866 INIT_LIST_HEAD(&dev->targets);
867 mutex_init(&dev->mlock);
868 spin_lock_init(&dev->lock);
870 ret = nvm_register_map(dev);
871 if (ret)
872 goto err_fmtype;
874 return 0;
875 err_fmtype:
876 kfree(dev->lun_map);
877 return ret;
880 static void nvm_free(struct nvm_dev *dev)
882 if (!dev)
883 return;
885 if (dev->dma_pool)
886 dev->ops->destroy_dma_pool(dev->dma_pool);
888 nvm_unregister_map(dev);
889 kfree(dev->lun_map);
890 kfree(dev);
893 static int nvm_init(struct nvm_dev *dev)
895 struct nvm_geo *geo = &dev->geo;
896 int ret = -EINVAL;
898 if (dev->ops->identity(dev)) {
899 pr_err("nvm: device could not be identified\n");
900 goto err;
903 pr_debug("nvm: ver:%u.%u nvm_vendor:%x\n",
904 geo->major_ver_id, geo->minor_ver_id,
905 geo->vmnt);
907 ret = nvm_core_init(dev);
908 if (ret) {
909 pr_err("nvm: could not initialize core structures.\n");
910 goto err;
913 pr_info("nvm: registered %s [%u/%u/%u/%u/%u]\n",
914 dev->name, dev->geo.ws_min, dev->geo.ws_opt,
915 dev->geo.num_chk, dev->geo.all_luns,
916 dev->geo.num_ch);
917 return 0;
918 err:
919 pr_err("nvm: failed to initialize nvm\n");
920 return ret;
923 struct nvm_dev *nvm_alloc_dev(int node)
925 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
927 EXPORT_SYMBOL(nvm_alloc_dev);
929 int nvm_register(struct nvm_dev *dev)
931 int ret;
933 if (!dev->q || !dev->ops)
934 return -EINVAL;
936 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
937 if (!dev->dma_pool) {
938 pr_err("nvm: could not create dma pool\n");
939 return -ENOMEM;
942 ret = nvm_init(dev);
943 if (ret)
944 goto err_init;
946 /* register device with a supported media manager */
947 down_write(&nvm_lock);
948 list_add(&dev->devices, &nvm_devices);
949 up_write(&nvm_lock);
951 return 0;
952 err_init:
953 dev->ops->destroy_dma_pool(dev->dma_pool);
954 return ret;
956 EXPORT_SYMBOL(nvm_register);
958 void nvm_unregister(struct nvm_dev *dev)
960 struct nvm_target *t, *tmp;
962 mutex_lock(&dev->mlock);
963 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
964 if (t->dev->parent != dev)
965 continue;
966 __nvm_remove_target(t, false);
968 mutex_unlock(&dev->mlock);
970 down_write(&nvm_lock);
971 list_del(&dev->devices);
972 up_write(&nvm_lock);
974 nvm_free(dev);
976 EXPORT_SYMBOL(nvm_unregister);
978 static int __nvm_configure_create(struct nvm_ioctl_create *create)
980 struct nvm_dev *dev;
982 down_write(&nvm_lock);
983 dev = nvm_find_nvm_dev(create->dev);
984 up_write(&nvm_lock);
986 if (!dev) {
987 pr_err("nvm: device not found\n");
988 return -EINVAL;
991 return nvm_create_tgt(dev, create);
994 static long nvm_ioctl_info(struct file *file, void __user *arg)
996 struct nvm_ioctl_info *info;
997 struct nvm_tgt_type *tt;
998 int tgt_iter = 0;
1000 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1001 if (IS_ERR(info))
1002 return -EFAULT;
1004 info->version[0] = NVM_VERSION_MAJOR;
1005 info->version[1] = NVM_VERSION_MINOR;
1006 info->version[2] = NVM_VERSION_PATCH;
1008 down_write(&nvm_tgtt_lock);
1009 list_for_each_entry(tt, &nvm_tgt_types, list) {
1010 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1012 tgt->version[0] = tt->version[0];
1013 tgt->version[1] = tt->version[1];
1014 tgt->version[2] = tt->version[2];
1015 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1017 tgt_iter++;
1020 info->tgtsize = tgt_iter;
1021 up_write(&nvm_tgtt_lock);
1023 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1024 kfree(info);
1025 return -EFAULT;
1028 kfree(info);
1029 return 0;
1032 static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1034 struct nvm_ioctl_get_devices *devices;
1035 struct nvm_dev *dev;
1036 int i = 0;
1038 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1039 if (!devices)
1040 return -ENOMEM;
1042 down_write(&nvm_lock);
1043 list_for_each_entry(dev, &nvm_devices, devices) {
1044 struct nvm_ioctl_device_info *info = &devices->info[i];
1046 strlcpy(info->devname, dev->name, sizeof(info->devname));
1048 /* kept for compatibility */
1049 info->bmversion[0] = 1;
1050 info->bmversion[1] = 0;
1051 info->bmversion[2] = 0;
1052 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
1053 i++;
1055 if (i > 31) {
1056 pr_err("nvm: max 31 devices can be reported.\n");
1057 break;
1060 up_write(&nvm_lock);
1062 devices->nr_devices = i;
1064 if (copy_to_user(arg, devices,
1065 sizeof(struct nvm_ioctl_get_devices))) {
1066 kfree(devices);
1067 return -EFAULT;
1070 kfree(devices);
1071 return 0;
1074 static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1076 struct nvm_ioctl_create create;
1078 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1079 return -EFAULT;
1081 if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED &&
1082 create.conf.e.rsv != 0) {
1083 pr_err("nvm: reserved config field in use\n");
1084 return -EINVAL;
1087 create.dev[DISK_NAME_LEN - 1] = '\0';
1088 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1089 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1091 if (create.flags != 0) {
1092 __u32 flags = create.flags;
1094 /* Check for valid flags */
1095 if (flags & NVM_TARGET_FACTORY)
1096 flags &= ~NVM_TARGET_FACTORY;
1098 if (flags) {
1099 pr_err("nvm: flag not supported\n");
1100 return -EINVAL;
1104 return __nvm_configure_create(&create);
1107 static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1109 struct nvm_ioctl_remove remove;
1110 struct nvm_dev *dev;
1111 int ret = 0;
1113 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1114 return -EFAULT;
1116 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1118 if (remove.flags != 0) {
1119 pr_err("nvm: no flags supported\n");
1120 return -EINVAL;
1123 list_for_each_entry(dev, &nvm_devices, devices) {
1124 ret = nvm_remove_tgt(dev, &remove);
1125 if (!ret)
1126 break;
1129 return ret;
1132 /* kept for compatibility reasons */
1133 static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1135 struct nvm_ioctl_dev_init init;
1137 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1138 return -EFAULT;
1140 if (init.flags != 0) {
1141 pr_err("nvm: no flags supported\n");
1142 return -EINVAL;
1145 return 0;
1148 /* Kept for compatibility reasons */
1149 static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1151 struct nvm_ioctl_dev_factory fact;
1153 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1154 return -EFAULT;
1156 fact.dev[DISK_NAME_LEN - 1] = '\0';
1158 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1159 return -EINVAL;
1161 return 0;
1164 static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1166 void __user *argp = (void __user *)arg;
1168 if (!capable(CAP_SYS_ADMIN))
1169 return -EPERM;
1171 switch (cmd) {
1172 case NVM_INFO:
1173 return nvm_ioctl_info(file, argp);
1174 case NVM_GET_DEVICES:
1175 return nvm_ioctl_get_devices(file, argp);
1176 case NVM_DEV_CREATE:
1177 return nvm_ioctl_dev_create(file, argp);
1178 case NVM_DEV_REMOVE:
1179 return nvm_ioctl_dev_remove(file, argp);
1180 case NVM_DEV_INIT:
1181 return nvm_ioctl_dev_init(file, argp);
1182 case NVM_DEV_FACTORY:
1183 return nvm_ioctl_dev_factory(file, argp);
1185 return 0;
1188 static const struct file_operations _ctl_fops = {
1189 .open = nonseekable_open,
1190 .unlocked_ioctl = nvm_ctl_ioctl,
1191 .owner = THIS_MODULE,
1192 .llseek = noop_llseek,
1195 static struct miscdevice _nvm_misc = {
1196 .minor = MISC_DYNAMIC_MINOR,
1197 .name = "lightnvm",
1198 .nodename = "lightnvm/control",
1199 .fops = &_ctl_fops,
1201 builtin_misc_device(_nvm_misc);