]> git.karo-electronics.de Git - linux-beck.git/blob
1111740
[linux-beck.git] /
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/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/miscdevice.h>
27 #include <linux/lightnvm.h>
28 #include <linux/sched/sysctl.h>
29
30 static LIST_HEAD(nvm_tgt_types);
31 static DECLARE_RWSEM(nvm_tgtt_lock);
32 static LIST_HEAD(nvm_mgrs);
33 static LIST_HEAD(nvm_devices);
34 static DECLARE_RWSEM(nvm_lock);
35
36 struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
37 {
38         struct nvm_tgt_type *tmp, *tt = NULL;
39
40         if (lock)
41                 down_write(&nvm_tgtt_lock);
42
43         list_for_each_entry(tmp, &nvm_tgt_types, list)
44                 if (!strcmp(name, tmp->name)) {
45                         tt = tmp;
46                         break;
47                 }
48
49         if (lock)
50                 up_write(&nvm_tgtt_lock);
51         return tt;
52 }
53 EXPORT_SYMBOL(nvm_find_target_type);
54
55 int nvm_register_tgt_type(struct nvm_tgt_type *tt)
56 {
57         int ret = 0;
58
59         down_write(&nvm_tgtt_lock);
60         if (nvm_find_target_type(tt->name, 0))
61                 ret = -EEXIST;
62         else
63                 list_add(&tt->list, &nvm_tgt_types);
64         up_write(&nvm_tgtt_lock);
65
66         return ret;
67 }
68 EXPORT_SYMBOL(nvm_register_tgt_type);
69
70 void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
71 {
72         if (!tt)
73                 return;
74
75         down_write(&nvm_lock);
76         list_del(&tt->list);
77         up_write(&nvm_lock);
78 }
79 EXPORT_SYMBOL(nvm_unregister_tgt_type);
80
81 void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
82                                                         dma_addr_t *dma_handler)
83 {
84         return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
85                                                                 dma_handler);
86 }
87 EXPORT_SYMBOL(nvm_dev_dma_alloc);
88
89 void nvm_dev_dma_free(struct nvm_dev *dev, void *addr,
90                                                         dma_addr_t dma_handler)
91 {
92         dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
93 }
94 EXPORT_SYMBOL(nvm_dev_dma_free);
95
96 static struct nvmm_type *nvm_find_mgr_type(const char *name)
97 {
98         struct nvmm_type *mt;
99
100         list_for_each_entry(mt, &nvm_mgrs, list)
101                 if (!strcmp(name, mt->name))
102                         return mt;
103
104         return NULL;
105 }
106
107 static struct nvmm_type *nvm_init_mgr(struct nvm_dev *dev)
108 {
109         struct nvmm_type *mt;
110         int ret;
111
112         lockdep_assert_held(&nvm_lock);
113
114         list_for_each_entry(mt, &nvm_mgrs, list) {
115                 if (strncmp(dev->sb.mmtype, mt->name, NVM_MMTYPE_LEN))
116                         continue;
117
118                 ret = mt->register_mgr(dev);
119                 if (ret < 0) {
120                         pr_err("nvm: media mgr failed to init (%d) on dev %s\n",
121                                                                 ret, dev->name);
122                         return NULL; /* initialization failed */
123                 } else if (ret > 0)
124                         return mt;
125         }
126
127         return NULL;
128 }
129
130 int nvm_register_mgr(struct nvmm_type *mt)
131 {
132         struct nvm_dev *dev;
133         int ret = 0;
134
135         down_write(&nvm_lock);
136         if (nvm_find_mgr_type(mt->name)) {
137                 ret = -EEXIST;
138                 goto finish;
139         } else {
140                 list_add(&mt->list, &nvm_mgrs);
141         }
142
143         /* try to register media mgr if any device have none configured */
144         list_for_each_entry(dev, &nvm_devices, devices) {
145                 if (dev->mt)
146                         continue;
147
148                 dev->mt = nvm_init_mgr(dev);
149         }
150 finish:
151         up_write(&nvm_lock);
152
153         return ret;
154 }
155 EXPORT_SYMBOL(nvm_register_mgr);
156
157 void nvm_unregister_mgr(struct nvmm_type *mt)
158 {
159         if (!mt)
160                 return;
161
162         down_write(&nvm_lock);
163         list_del(&mt->list);
164         up_write(&nvm_lock);
165 }
166 EXPORT_SYMBOL(nvm_unregister_mgr);
167
168 static struct nvm_dev *nvm_find_nvm_dev(const char *name)
169 {
170         struct nvm_dev *dev;
171
172         list_for_each_entry(dev, &nvm_devices, devices)
173                 if (!strcmp(name, dev->name))
174                         return dev;
175
176         return NULL;
177 }
178
179 struct nvm_block *nvm_get_blk(struct nvm_dev *dev, struct nvm_lun *lun,
180                                                         unsigned long flags)
181 {
182         return dev->mt->get_blk(dev, lun, flags);
183 }
184 EXPORT_SYMBOL(nvm_get_blk);
185
186 /* Assumes that all valid pages have already been moved on release to bm */
187 void nvm_put_blk(struct nvm_dev *dev, struct nvm_block *blk)
188 {
189         return dev->mt->put_blk(dev, blk);
190 }
191 EXPORT_SYMBOL(nvm_put_blk);
192
193 void nvm_mark_blk(struct nvm_dev *dev, struct ppa_addr ppa, int type)
194 {
195         return dev->mt->mark_blk(dev, ppa, type);
196 }
197 EXPORT_SYMBOL(nvm_mark_blk);
198
199 int nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
200 {
201         return dev->mt->submit_io(dev, rqd);
202 }
203 EXPORT_SYMBOL(nvm_submit_io);
204
205 int nvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk)
206 {
207         return dev->mt->erase_blk(dev, blk, 0);
208 }
209 EXPORT_SYMBOL(nvm_erase_blk);
210
211 void nvm_addr_to_generic_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
212 {
213         int i;
214
215         if (rqd->nr_ppas > 1) {
216                 for (i = 0; i < rqd->nr_ppas; i++)
217                         rqd->ppa_list[i] = dev_to_generic_addr(dev,
218                                                         rqd->ppa_list[i]);
219         } else {
220                 rqd->ppa_addr = dev_to_generic_addr(dev, rqd->ppa_addr);
221         }
222 }
223 EXPORT_SYMBOL(nvm_addr_to_generic_mode);
224
225 void nvm_generic_to_addr_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
226 {
227         int i;
228
229         if (rqd->nr_ppas > 1) {
230                 for (i = 0; i < rqd->nr_ppas; i++)
231                         rqd->ppa_list[i] = generic_to_dev_addr(dev,
232                                                         rqd->ppa_list[i]);
233         } else {
234                 rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr);
235         }
236 }
237 EXPORT_SYMBOL(nvm_generic_to_addr_mode);
238
239 int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd,
240                         const struct ppa_addr *ppas, int nr_ppas, int vblk)
241 {
242         int i, plane_cnt, pl_idx;
243         struct ppa_addr ppa;
244
245         if ((!vblk || dev->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
246                 rqd->nr_ppas = nr_ppas;
247                 rqd->ppa_addr = ppas[0];
248
249                 return 0;
250         }
251
252         rqd->nr_ppas = nr_ppas;
253         rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
254         if (!rqd->ppa_list) {
255                 pr_err("nvm: failed to allocate dma memory\n");
256                 return -ENOMEM;
257         }
258
259         if (!vblk) {
260                 for (i = 0; i < nr_ppas; i++)
261                         rqd->ppa_list[i] = ppas[i];
262         } else {
263                 plane_cnt = dev->plane_mode;
264                 rqd->nr_ppas *= plane_cnt;
265
266                 for (i = 0; i < nr_ppas; i++) {
267                         for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
268                                 ppa = ppas[i];
269                                 ppa.g.pl = pl_idx;
270                                 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
271                         }
272                 }
273         }
274
275         return 0;
276 }
277 EXPORT_SYMBOL(nvm_set_rqd_ppalist);
278
279 void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd)
280 {
281         if (!rqd->ppa_list)
282                 return;
283
284         nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list);
285 }
286 EXPORT_SYMBOL(nvm_free_rqd_ppalist);
287
288 int nvm_erase_ppa(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas)
289 {
290         struct nvm_rq rqd;
291         int ret;
292
293         if (!dev->ops->erase_block)
294                 return 0;
295
296         memset(&rqd, 0, sizeof(struct nvm_rq));
297
298         ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
299         if (ret)
300                 return ret;
301
302         nvm_generic_to_addr_mode(dev, &rqd);
303
304         ret = dev->ops->erase_block(dev, &rqd);
305
306         nvm_free_rqd_ppalist(dev, &rqd);
307
308         return ret;
309 }
310 EXPORT_SYMBOL(nvm_erase_ppa);
311
312 void nvm_end_io(struct nvm_rq *rqd, int error)
313 {
314         rqd->error = error;
315         rqd->end_io(rqd);
316 }
317 EXPORT_SYMBOL(nvm_end_io);
318
319 static void nvm_end_io_sync(struct nvm_rq *rqd)
320 {
321         struct completion *waiting = rqd->wait;
322
323         rqd->wait = NULL;
324
325         complete(waiting);
326 }
327
328 static int __nvm_submit_ppa(struct nvm_dev *dev, struct nvm_rq *rqd, int opcode,
329                                                 int flags, void *buf, int len)
330 {
331         DECLARE_COMPLETION_ONSTACK(wait);
332         struct bio *bio;
333         int ret;
334         unsigned long hang_check;
335
336         bio = bio_map_kern(dev->q, buf, len, GFP_KERNEL);
337         if (IS_ERR_OR_NULL(bio))
338                 return -ENOMEM;
339
340         nvm_generic_to_addr_mode(dev, rqd);
341
342         rqd->dev = dev;
343         rqd->opcode = opcode;
344         rqd->flags = flags;
345         rqd->bio = bio;
346         rqd->wait = &wait;
347         rqd->end_io = nvm_end_io_sync;
348
349         ret = dev->ops->submit_io(dev, rqd);
350         if (ret) {
351                 bio_put(bio);
352                 return ret;
353         }
354
355         /* Prevent hang_check timer from firing at us during very long I/O */
356         hang_check = sysctl_hung_task_timeout_secs;
357         if (hang_check)
358                 while (!wait_for_completion_io_timeout(&wait,
359                                                         hang_check * (HZ/2)))
360                         ;
361         else
362                 wait_for_completion_io(&wait);
363
364         return rqd->error;
365 }
366
367 /**
368  * nvm_submit_ppa_list - submit user-defined ppa list to device. The user must
369  *                       take to free ppa list if necessary.
370  * @dev:        device
371  * @ppa_list:   user created ppa_list
372  * @nr_ppas:    length of ppa_list
373  * @opcode:     device opcode
374  * @flags:      device flags
375  * @buf:        data buffer
376  * @len:        data buffer length
377  */
378 int nvm_submit_ppa_list(struct nvm_dev *dev, struct ppa_addr *ppa_list,
379                         int nr_ppas, int opcode, int flags, void *buf, int len)
380 {
381         struct nvm_rq rqd;
382
383         if (dev->ops->max_phys_sect < nr_ppas)
384                 return -EINVAL;
385
386         memset(&rqd, 0, sizeof(struct nvm_rq));
387
388         rqd.nr_ppas = nr_ppas;
389         if (nr_ppas > 1)
390                 rqd.ppa_list = ppa_list;
391         else
392                 rqd.ppa_addr = ppa_list[0];
393
394         return __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
395 }
396 EXPORT_SYMBOL(nvm_submit_ppa_list);
397
398 /**
399  * nvm_submit_ppa - submit PPAs to device. PPAs will automatically be unfolded
400  *                  as single, dual, quad plane PPAs depending on device type.
401  * @dev:        device
402  * @ppa:        user created ppa_list
403  * @nr_ppas:    length of ppa_list
404  * @opcode:     device opcode
405  * @flags:      device flags
406  * @buf:        data buffer
407  * @len:        data buffer length
408  */
409 int nvm_submit_ppa(struct nvm_dev *dev, struct ppa_addr *ppa, int nr_ppas,
410                                 int opcode, int flags, void *buf, int len)
411 {
412         struct nvm_rq rqd;
413         int ret;
414
415         memset(&rqd, 0, sizeof(struct nvm_rq));
416         ret = nvm_set_rqd_ppalist(dev, &rqd, ppa, nr_ppas, 1);
417         if (ret)
418                 return ret;
419
420         ret = __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
421
422         nvm_free_rqd_ppalist(dev, &rqd);
423
424         return ret;
425 }
426 EXPORT_SYMBOL(nvm_submit_ppa);
427
428 /*
429  * folds a bad block list from its plane representation to its virtual
430  * block representation. The fold is done in place and reduced size is
431  * returned.
432  *
433  * If any of the planes status are bad or grown bad block, the virtual block
434  * is marked bad. If not bad, the first plane state acts as the block state.
435  */
436 int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
437 {
438         int blk, offset, pl, blktype;
439
440         if (nr_blks != dev->blks_per_lun * dev->plane_mode)
441                 return -EINVAL;
442
443         for (blk = 0; blk < dev->blks_per_lun; blk++) {
444                 offset = blk * dev->plane_mode;
445                 blktype = blks[offset];
446
447                 /* Bad blocks on any planes take precedence over other types */
448                 for (pl = 0; pl < dev->plane_mode; pl++) {
449                         if (blks[offset + pl] &
450                                         (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
451                                 blktype = blks[offset + pl];
452                                 break;
453                         }
454                 }
455
456                 blks[blk] = blktype;
457         }
458
459         return dev->blks_per_lun;
460 }
461 EXPORT_SYMBOL(nvm_bb_tbl_fold);
462
463 int nvm_get_bb_tbl(struct nvm_dev *dev, struct ppa_addr ppa, u8 *blks)
464 {
465         ppa = generic_to_dev_addr(dev, ppa);
466
467         return dev->ops->get_bb_tbl(dev, ppa, blks);
468 }
469 EXPORT_SYMBOL(nvm_get_bb_tbl);
470
471 static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
472 {
473         int i;
474
475         dev->lps_per_blk = dev->pgs_per_blk;
476         dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
477         if (!dev->lptbl)
478                 return -ENOMEM;
479
480         /* Just a linear array */
481         for (i = 0; i < dev->lps_per_blk; i++)
482                 dev->lptbl[i] = i;
483
484         return 0;
485 }
486
487 static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
488 {
489         int i, p;
490         struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
491
492         if (!mlc->num_pairs)
493                 return 0;
494
495         dev->lps_per_blk = mlc->num_pairs;
496         dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
497         if (!dev->lptbl)
498                 return -ENOMEM;
499
500         /* The lower page table encoding consists of a list of bytes, where each
501          * has a lower and an upper half. The first half byte maintains the
502          * increment value and every value after is an offset added to the
503          * previous incrementation value
504          */
505         dev->lptbl[0] = mlc->pairs[0] & 0xF;
506         for (i = 1; i < dev->lps_per_blk; i++) {
507                 p = mlc->pairs[i >> 1];
508                 if (i & 0x1) /* upper */
509                         dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
510                 else /* lower */
511                         dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
512         }
513
514         return 0;
515 }
516
517 static int nvm_core_init(struct nvm_dev *dev)
518 {
519         struct nvm_id *id = &dev->identity;
520         struct nvm_id_group *grp = &id->groups[0];
521         int ret;
522
523         /* device values */
524         dev->nr_chnls = grp->num_ch;
525         dev->luns_per_chnl = grp->num_lun;
526         dev->pgs_per_blk = grp->num_pg;
527         dev->blks_per_lun = grp->num_blk;
528         dev->nr_planes = grp->num_pln;
529         dev->fpg_size = grp->fpg_sz;
530         dev->pfpg_size = grp->fpg_sz * grp->num_pln;
531         dev->sec_size = grp->csecs;
532         dev->oob_size = grp->sos;
533         dev->sec_per_pg = grp->fpg_sz / grp->csecs;
534         dev->mccap = grp->mccap;
535         memcpy(&dev->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
536
537         dev->plane_mode = NVM_PLANE_SINGLE;
538         dev->max_rq_size = dev->ops->max_phys_sect * dev->sec_size;
539
540         if (grp->mpos & 0x020202)
541                 dev->plane_mode = NVM_PLANE_DOUBLE;
542         if (grp->mpos & 0x040404)
543                 dev->plane_mode = NVM_PLANE_QUAD;
544
545         if (grp->mtype != 0) {
546                 pr_err("nvm: memory type not supported\n");
547                 return -EINVAL;
548         }
549
550         /* calculated values */
551         dev->sec_per_pl = dev->sec_per_pg * dev->nr_planes;
552         dev->sec_per_blk = dev->sec_per_pl * dev->pgs_per_blk;
553         dev->sec_per_lun = dev->sec_per_blk * dev->blks_per_lun;
554         dev->nr_luns = dev->luns_per_chnl * dev->nr_chnls;
555
556         dev->total_secs = dev->nr_luns * dev->sec_per_lun;
557         dev->lun_map = kcalloc(BITS_TO_LONGS(dev->nr_luns),
558                                         sizeof(unsigned long), GFP_KERNEL);
559         if (!dev->lun_map)
560                 return -ENOMEM;
561
562         switch (grp->fmtype) {
563         case NVM_ID_FMTYPE_SLC:
564                 if (nvm_init_slc_tbl(dev, grp)) {
565                         ret = -ENOMEM;
566                         goto err_fmtype;
567                 }
568                 break;
569         case NVM_ID_FMTYPE_MLC:
570                 if (nvm_init_mlc_tbl(dev, grp)) {
571                         ret = -ENOMEM;
572                         goto err_fmtype;
573                 }
574                 break;
575         default:
576                 pr_err("nvm: flash type not supported\n");
577                 ret = -EINVAL;
578                 goto err_fmtype;
579         }
580
581         mutex_init(&dev->mlock);
582         spin_lock_init(&dev->lock);
583
584         blk_queue_logical_block_size(dev->q, dev->sec_size);
585
586         return 0;
587 err_fmtype:
588         kfree(dev->lun_map);
589         return ret;
590 }
591
592 static void nvm_free_mgr(struct nvm_dev *dev)
593 {
594         if (!dev->mt)
595                 return;
596
597         dev->mt->unregister_mgr(dev);
598         dev->mt = NULL;
599 }
600
601 void nvm_free(struct nvm_dev *dev)
602 {
603         if (!dev)
604                 return;
605
606         nvm_free_mgr(dev);
607
608         if (dev->dma_pool)
609                 dev->ops->destroy_dma_pool(dev->dma_pool);
610
611         kfree(dev->lptbl);
612         kfree(dev->lun_map);
613         kfree(dev);
614 }
615
616 static int nvm_init(struct nvm_dev *dev)
617 {
618         int ret = -EINVAL;
619
620         if (!dev->q || !dev->ops)
621                 return ret;
622
623         if (dev->ops->identity(dev, &dev->identity)) {
624                 pr_err("nvm: device could not be identified\n");
625                 goto err;
626         }
627
628         pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
629                         dev->identity.ver_id, dev->identity.vmnt,
630                                                         dev->identity.cgrps);
631
632         if (dev->identity.ver_id != 1) {
633                 pr_err("nvm: device not supported by kernel.");
634                 goto err;
635         }
636
637         if (dev->identity.cgrps != 1) {
638                 pr_err("nvm: only one group configuration supported.");
639                 goto err;
640         }
641
642         ret = nvm_core_init(dev);
643         if (ret) {
644                 pr_err("nvm: could not initialize core structures.\n");
645                 goto err;
646         }
647
648         pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
649                         dev->name, dev->sec_per_pg, dev->nr_planes,
650                         dev->pgs_per_blk, dev->blks_per_lun, dev->nr_luns,
651                         dev->nr_chnls);
652         return 0;
653 err:
654         pr_err("nvm: failed to initialize nvm\n");
655         return ret;
656 }
657
658 struct nvm_dev *nvm_alloc_dev(int node)
659 {
660         return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
661 }
662 EXPORT_SYMBOL(nvm_alloc_dev);
663
664 int nvm_register(struct nvm_dev *dev)
665 {
666         int ret;
667
668         ret = nvm_init(dev);
669         if (ret)
670                 goto err_init;
671
672         if (dev->ops->max_phys_sect > 256) {
673                 pr_info("nvm: max sectors supported is 256.\n");
674                 ret = -EINVAL;
675                 goto err_init;
676         }
677
678         if (dev->ops->max_phys_sect > 1) {
679                 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
680                 if (!dev->dma_pool) {
681                         pr_err("nvm: could not create dma pool\n");
682                         ret = -ENOMEM;
683                         goto err_init;
684                 }
685         }
686
687         if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
688                 ret = nvm_get_sysblock(dev, &dev->sb);
689                 if (!ret)
690                         pr_err("nvm: device not initialized.\n");
691                 else if (ret < 0)
692                         pr_err("nvm: err (%d) on device initialization\n", ret);
693         }
694
695         /* register device with a supported media manager */
696         down_write(&nvm_lock);
697         if (ret > 0)
698                 dev->mt = nvm_init_mgr(dev);
699         list_add(&dev->devices, &nvm_devices);
700         up_write(&nvm_lock);
701
702         return 0;
703 err_init:
704         kfree(dev->lun_map);
705         return ret;
706 }
707 EXPORT_SYMBOL(nvm_register);
708
709 void nvm_unregister(struct nvm_dev *dev)
710 {
711         down_write(&nvm_lock);
712         list_del(&dev->devices);
713         up_write(&nvm_lock);
714
715         nvm_free(dev);
716 }
717 EXPORT_SYMBOL(nvm_unregister);
718
719 static int __nvm_configure_create(struct nvm_ioctl_create *create)
720 {
721         struct nvm_dev *dev;
722         struct nvm_ioctl_create_simple *s;
723
724         down_write(&nvm_lock);
725         dev = nvm_find_nvm_dev(create->dev);
726         up_write(&nvm_lock);
727
728         if (!dev) {
729                 pr_err("nvm: device not found\n");
730                 return -EINVAL;
731         }
732
733         if (!dev->mt) {
734                 pr_info("nvm: device has no media manager registered.\n");
735                 return -ENODEV;
736         }
737
738         if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
739                 pr_err("nvm: config type not valid\n");
740                 return -EINVAL;
741         }
742         s = &create->conf.s;
743
744         if (s->lun_begin > s->lun_end || s->lun_end > dev->nr_luns) {
745                 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
746                         s->lun_begin, s->lun_end, dev->nr_luns);
747                 return -EINVAL;
748         }
749
750         return dev->mt->create_tgt(dev, create);
751 }
752
753 #ifdef CONFIG_NVM_DEBUG
754 static int nvm_configure_show(const char *val)
755 {
756         struct nvm_dev *dev;
757         char opcode, devname[DISK_NAME_LEN];
758         int ret;
759
760         ret = sscanf(val, "%c %32s", &opcode, devname);
761         if (ret != 2) {
762                 pr_err("nvm: invalid command. Use \"opcode devicename\".\n");
763                 return -EINVAL;
764         }
765
766         down_write(&nvm_lock);
767         dev = nvm_find_nvm_dev(devname);
768         up_write(&nvm_lock);
769         if (!dev) {
770                 pr_err("nvm: device not found\n");
771                 return -EINVAL;
772         }
773
774         if (!dev->mt)
775                 return 0;
776
777         dev->mt->lun_info_print(dev);
778
779         return 0;
780 }
781
782 static int nvm_configure_remove(const char *val)
783 {
784         struct nvm_ioctl_remove remove;
785         struct nvm_dev *dev;
786         char opcode;
787         int ret = 0;
788
789         ret = sscanf(val, "%c %256s", &opcode, remove.tgtname);
790         if (ret != 2) {
791                 pr_err("nvm: invalid command. Use \"d targetname\".\n");
792                 return -EINVAL;
793         }
794
795         remove.flags = 0;
796
797         list_for_each_entry(dev, &nvm_devices, devices) {
798                 ret = dev->mt->remove_tgt(dev, &remove);
799                 if (!ret)
800                         break;
801         }
802
803         return ret;
804 }
805
806 static int nvm_configure_create(const char *val)
807 {
808         struct nvm_ioctl_create create;
809         char opcode;
810         int lun_begin, lun_end, ret;
811
812         ret = sscanf(val, "%c %256s %256s %48s %u:%u", &opcode, create.dev,
813                                                 create.tgtname, create.tgttype,
814                                                 &lun_begin, &lun_end);
815         if (ret != 6) {
816                 pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n");
817                 return -EINVAL;
818         }
819
820         create.flags = 0;
821         create.conf.type = NVM_CONFIG_TYPE_SIMPLE;
822         create.conf.s.lun_begin = lun_begin;
823         create.conf.s.lun_end = lun_end;
824
825         return __nvm_configure_create(&create);
826 }
827
828
829 /* Exposes administrative interface through /sys/module/lnvm/configure_by_str */
830 static int nvm_configure_by_str_event(const char *val,
831                                         const struct kernel_param *kp)
832 {
833         char opcode;
834         int ret;
835
836         ret = sscanf(val, "%c", &opcode);
837         if (ret != 1) {
838                 pr_err("nvm: string must have the format of \"cmd ...\"\n");
839                 return -EINVAL;
840         }
841
842         switch (opcode) {
843         case 'a':
844                 return nvm_configure_create(val);
845         case 'd':
846                 return nvm_configure_remove(val);
847         case 's':
848                 return nvm_configure_show(val);
849         default:
850                 pr_err("nvm: invalid command\n");
851                 return -EINVAL;
852         }
853
854         return 0;
855 }
856
857 static int nvm_configure_get(char *buf, const struct kernel_param *kp)
858 {
859         int sz;
860         struct nvm_dev *dev;
861
862         sz = sprintf(buf, "available devices:\n");
863         down_write(&nvm_lock);
864         list_for_each_entry(dev, &nvm_devices, devices) {
865                 if (sz > 4095 - DISK_NAME_LEN - 2)
866                         break;
867                 sz += sprintf(buf + sz, " %32s\n", dev->name);
868         }
869         up_write(&nvm_lock);
870
871         return sz;
872 }
873
874 static const struct kernel_param_ops nvm_configure_by_str_event_param_ops = {
875         .set    = nvm_configure_by_str_event,
876         .get    = nvm_configure_get,
877 };
878
879 #undef MODULE_PARAM_PREFIX
880 #define MODULE_PARAM_PREFIX     "lnvm."
881
882 module_param_cb(configure_debug, &nvm_configure_by_str_event_param_ops, NULL,
883                                                                         0644);
884
885 #endif /* CONFIG_NVM_DEBUG */
886
887 static long nvm_ioctl_info(struct file *file, void __user *arg)
888 {
889         struct nvm_ioctl_info *info;
890         struct nvm_tgt_type *tt;
891         int tgt_iter = 0;
892
893         if (!capable(CAP_SYS_ADMIN))
894                 return -EPERM;
895
896         info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
897         if (IS_ERR(info))
898                 return -EFAULT;
899
900         info->version[0] = NVM_VERSION_MAJOR;
901         info->version[1] = NVM_VERSION_MINOR;
902         info->version[2] = NVM_VERSION_PATCH;
903
904         down_write(&nvm_lock);
905         list_for_each_entry(tt, &nvm_tgt_types, list) {
906                 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
907
908                 tgt->version[0] = tt->version[0];
909                 tgt->version[1] = tt->version[1];
910                 tgt->version[2] = tt->version[2];
911                 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
912
913                 tgt_iter++;
914         }
915
916         info->tgtsize = tgt_iter;
917         up_write(&nvm_lock);
918
919         if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
920                 kfree(info);
921                 return -EFAULT;
922         }
923
924         kfree(info);
925         return 0;
926 }
927
928 static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
929 {
930         struct nvm_ioctl_get_devices *devices;
931         struct nvm_dev *dev;
932         int i = 0;
933
934         if (!capable(CAP_SYS_ADMIN))
935                 return -EPERM;
936
937         devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
938         if (!devices)
939                 return -ENOMEM;
940
941         down_write(&nvm_lock);
942         list_for_each_entry(dev, &nvm_devices, devices) {
943                 struct nvm_ioctl_device_info *info = &devices->info[i];
944
945                 sprintf(info->devname, "%s", dev->name);
946                 if (dev->mt) {
947                         info->bmversion[0] = dev->mt->version[0];
948                         info->bmversion[1] = dev->mt->version[1];
949                         info->bmversion[2] = dev->mt->version[2];
950                         sprintf(info->bmname, "%s", dev->mt->name);
951                 } else {
952                         sprintf(info->bmname, "none");
953                 }
954
955                 i++;
956                 if (i > 31) {
957                         pr_err("nvm: max 31 devices can be reported.\n");
958                         break;
959                 }
960         }
961         up_write(&nvm_lock);
962
963         devices->nr_devices = i;
964
965         if (copy_to_user(arg, devices,
966                          sizeof(struct nvm_ioctl_get_devices))) {
967                 kfree(devices);
968                 return -EFAULT;
969         }
970
971         kfree(devices);
972         return 0;
973 }
974
975 static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
976 {
977         struct nvm_ioctl_create create;
978
979         if (!capable(CAP_SYS_ADMIN))
980                 return -EPERM;
981
982         if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
983                 return -EFAULT;
984
985         create.dev[DISK_NAME_LEN - 1] = '\0';
986         create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
987         create.tgtname[DISK_NAME_LEN - 1] = '\0';
988
989         if (create.flags != 0) {
990                 pr_err("nvm: no flags supported\n");
991                 return -EINVAL;
992         }
993
994         return __nvm_configure_create(&create);
995 }
996
997 static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
998 {
999         struct nvm_ioctl_remove remove;
1000         struct nvm_dev *dev;
1001         int ret = 0;
1002
1003         if (!capable(CAP_SYS_ADMIN))
1004                 return -EPERM;
1005
1006         if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1007                 return -EFAULT;
1008
1009         remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1010
1011         if (remove.flags != 0) {
1012                 pr_err("nvm: no flags supported\n");
1013                 return -EINVAL;
1014         }
1015
1016         list_for_each_entry(dev, &nvm_devices, devices) {
1017                 ret = dev->mt->remove_tgt(dev, &remove);
1018                 if (!ret)
1019                         break;
1020         }
1021
1022         return ret;
1023 }
1024
1025 static void nvm_setup_nvm_sb_info(struct nvm_sb_info *info)
1026 {
1027         info->seqnr = 1;
1028         info->erase_cnt = 0;
1029         info->version = 1;
1030 }
1031
1032 static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init *init)
1033 {
1034         struct nvm_dev *dev;
1035         struct nvm_sb_info info;
1036         int ret;
1037
1038         down_write(&nvm_lock);
1039         dev = nvm_find_nvm_dev(init->dev);
1040         up_write(&nvm_lock);
1041         if (!dev) {
1042                 pr_err("nvm: device not found\n");
1043                 return -EINVAL;
1044         }
1045
1046         nvm_setup_nvm_sb_info(&info);
1047
1048         strncpy(info.mmtype, init->mmtype, NVM_MMTYPE_LEN);
1049         info.fs_ppa.ppa = -1;
1050
1051         if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
1052                 ret = nvm_init_sysblock(dev, &info);
1053                 if (ret)
1054                         return ret;
1055         }
1056
1057         memcpy(&dev->sb, &info, sizeof(struct nvm_sb_info));
1058
1059         down_write(&nvm_lock);
1060         dev->mt = nvm_init_mgr(dev);
1061         up_write(&nvm_lock);
1062
1063         return 0;
1064 }
1065
1066 static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1067 {
1068         struct nvm_ioctl_dev_init init;
1069
1070         if (!capable(CAP_SYS_ADMIN))
1071                 return -EPERM;
1072
1073         if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1074                 return -EFAULT;
1075
1076         if (init.flags != 0) {
1077                 pr_err("nvm: no flags supported\n");
1078                 return -EINVAL;
1079         }
1080
1081         init.dev[DISK_NAME_LEN - 1] = '\0';
1082
1083         return __nvm_ioctl_dev_init(&init);
1084 }
1085
1086 static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1087 {
1088         struct nvm_ioctl_dev_factory fact;
1089         struct nvm_dev *dev;
1090
1091         if (!capable(CAP_SYS_ADMIN))
1092                 return -EPERM;
1093
1094         if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1095                 return -EFAULT;
1096
1097         fact.dev[DISK_NAME_LEN - 1] = '\0';
1098
1099         if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1100                 return -EINVAL;
1101
1102         down_write(&nvm_lock);
1103         dev = nvm_find_nvm_dev(fact.dev);
1104         up_write(&nvm_lock);
1105         if (!dev) {
1106                 pr_err("nvm: device not found\n");
1107                 return -EINVAL;
1108         }
1109
1110         nvm_free_mgr(dev);
1111
1112         if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT)
1113                 return nvm_dev_factory(dev, fact.flags);
1114
1115         return 0;
1116 }
1117
1118 static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1119 {
1120         void __user *argp = (void __user *)arg;
1121
1122         switch (cmd) {
1123         case NVM_INFO:
1124                 return nvm_ioctl_info(file, argp);
1125         case NVM_GET_DEVICES:
1126                 return nvm_ioctl_get_devices(file, argp);
1127         case NVM_DEV_CREATE:
1128                 return nvm_ioctl_dev_create(file, argp);
1129         case NVM_DEV_REMOVE:
1130                 return nvm_ioctl_dev_remove(file, argp);
1131         case NVM_DEV_INIT:
1132                 return nvm_ioctl_dev_init(file, argp);
1133         case NVM_DEV_FACTORY:
1134                 return nvm_ioctl_dev_factory(file, argp);
1135         }
1136         return 0;
1137 }
1138
1139 static const struct file_operations _ctl_fops = {
1140         .open = nonseekable_open,
1141         .unlocked_ioctl = nvm_ctl_ioctl,
1142         .owner = THIS_MODULE,
1143         .llseek  = noop_llseek,
1144 };
1145
1146 static struct miscdevice _nvm_misc = {
1147         .minor          = MISC_DYNAMIC_MINOR,
1148         .name           = "lightnvm",
1149         .nodename       = "lightnvm/control",
1150         .fops           = &_ctl_fops,
1151 };
1152 module_misc_device(_nvm_misc);
1153
1154 MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR);
1155
1156 MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
1157 MODULE_LICENSE("GPL v2");
1158 MODULE_VERSION("0.1");