2 * Copyright (C) 2015 IT University of Copenhagen. All rights reserved.
3 * Initial release: Matias Bjorling <m@bjorling.me>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version
7 * 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
21 #include <linux/list.h>
22 #include <linux/types.h>
23 #include <linux/sem.h>
24 #include <linux/bitmap.h>
25 #include <linux/module.h>
26 #include <linux/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_mgrs
);
33 static LIST_HEAD(nvm_devices
);
34 static DECLARE_RWSEM(nvm_lock
);
36 struct nvm_tgt_type
*nvm_find_target_type(const char *name
, int lock
)
38 struct nvm_tgt_type
*tmp
, *tt
= NULL
;
41 down_write(&nvm_tgtt_lock
);
43 list_for_each_entry(tmp
, &nvm_tgt_types
, list
)
44 if (!strcmp(name
, tmp
->name
)) {
50 up_write(&nvm_tgtt_lock
);
53 EXPORT_SYMBOL(nvm_find_target_type
);
55 int nvm_register_tgt_type(struct nvm_tgt_type
*tt
)
59 down_write(&nvm_tgtt_lock
);
60 if (nvm_find_target_type(tt
->name
, 0))
63 list_add(&tt
->list
, &nvm_tgt_types
);
64 up_write(&nvm_tgtt_lock
);
68 EXPORT_SYMBOL(nvm_register_tgt_type
);
70 void nvm_unregister_tgt_type(struct nvm_tgt_type
*tt
)
75 down_write(&nvm_lock
);
79 EXPORT_SYMBOL(nvm_unregister_tgt_type
);
81 void *nvm_dev_dma_alloc(struct nvm_dev
*dev
, gfp_t mem_flags
,
82 dma_addr_t
*dma_handler
)
84 return dev
->ops
->dev_dma_alloc(dev
, dev
->dma_pool
, mem_flags
,
87 EXPORT_SYMBOL(nvm_dev_dma_alloc
);
89 void nvm_dev_dma_free(struct nvm_dev
*dev
, void *addr
,
90 dma_addr_t dma_handler
)
92 dev
->ops
->dev_dma_free(dev
->dma_pool
, addr
, dma_handler
);
94 EXPORT_SYMBOL(nvm_dev_dma_free
);
96 static struct nvmm_type
*nvm_find_mgr_type(const char *name
)
100 list_for_each_entry(mt
, &nvm_mgrs
, list
)
101 if (!strcmp(name
, mt
->name
))
107 static struct nvmm_type
*nvm_init_mgr(struct nvm_dev
*dev
)
109 struct nvmm_type
*mt
;
112 lockdep_assert_held(&nvm_lock
);
114 list_for_each_entry(mt
, &nvm_mgrs
, list
) {
115 if (strncmp(dev
->sb
.mmtype
, mt
->name
, NVM_MMTYPE_LEN
))
118 ret
= mt
->register_mgr(dev
);
120 pr_err("nvm: media mgr failed to init (%d) on dev %s\n",
122 return NULL
; /* initialization failed */
130 int nvm_register_mgr(struct nvmm_type
*mt
)
135 down_write(&nvm_lock
);
136 if (nvm_find_mgr_type(mt
->name
)) {
140 list_add(&mt
->list
, &nvm_mgrs
);
143 /* try to register media mgr if any device have none configured */
144 list_for_each_entry(dev
, &nvm_devices
, devices
) {
148 dev
->mt
= nvm_init_mgr(dev
);
155 EXPORT_SYMBOL(nvm_register_mgr
);
157 void nvm_unregister_mgr(struct nvmm_type
*mt
)
162 down_write(&nvm_lock
);
166 EXPORT_SYMBOL(nvm_unregister_mgr
);
168 static struct nvm_dev
*nvm_find_nvm_dev(const char *name
)
172 list_for_each_entry(dev
, &nvm_devices
, devices
)
173 if (!strcmp(name
, dev
->name
))
179 struct nvm_block
*nvm_get_blk(struct nvm_dev
*dev
, struct nvm_lun
*lun
,
182 return dev
->mt
->get_blk(dev
, lun
, flags
);
184 EXPORT_SYMBOL(nvm_get_blk
);
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
)
189 return dev
->mt
->put_blk(dev
, blk
);
191 EXPORT_SYMBOL(nvm_put_blk
);
193 void nvm_mark_blk(struct nvm_dev
*dev
, struct ppa_addr ppa
, int type
)
195 return dev
->mt
->mark_blk(dev
, ppa
, type
);
197 EXPORT_SYMBOL(nvm_mark_blk
);
199 int nvm_submit_io(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
201 return dev
->mt
->submit_io(dev
, rqd
);
203 EXPORT_SYMBOL(nvm_submit_io
);
205 int nvm_erase_blk(struct nvm_dev
*dev
, struct nvm_block
*blk
)
207 return dev
->mt
->erase_blk(dev
, blk
, 0);
209 EXPORT_SYMBOL(nvm_erase_blk
);
211 void nvm_addr_to_generic_mode(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
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
,
220 rqd
->ppa_addr
= dev_to_generic_addr(dev
, rqd
->ppa_addr
);
223 EXPORT_SYMBOL(nvm_addr_to_generic_mode
);
225 void nvm_generic_to_addr_mode(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
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
,
234 rqd
->ppa_addr
= generic_to_dev_addr(dev
, rqd
->ppa_addr
);
237 EXPORT_SYMBOL(nvm_generic_to_addr_mode
);
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
)
242 int i
, plane_cnt
, pl_idx
;
245 if ((!vblk
|| dev
->plane_mode
== NVM_PLANE_SINGLE
) && nr_ppas
== 1) {
246 rqd
->nr_ppas
= nr_ppas
;
247 rqd
->ppa_addr
= ppas
[0];
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");
260 for (i
= 0; i
< nr_ppas
; i
++)
261 rqd
->ppa_list
[i
] = ppas
[i
];
263 plane_cnt
= dev
->plane_mode
;
264 rqd
->nr_ppas
*= plane_cnt
;
266 for (i
= 0; i
< nr_ppas
; i
++) {
267 for (pl_idx
= 0; pl_idx
< plane_cnt
; pl_idx
++) {
270 rqd
->ppa_list
[(pl_idx
* nr_ppas
) + i
] = ppa
;
277 EXPORT_SYMBOL(nvm_set_rqd_ppalist
);
279 void nvm_free_rqd_ppalist(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
284 nvm_dev_dma_free(dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
286 EXPORT_SYMBOL(nvm_free_rqd_ppalist
);
288 int nvm_erase_ppa(struct nvm_dev
*dev
, struct ppa_addr
*ppas
, int nr_ppas
)
293 if (!dev
->ops
->erase_block
)
296 memset(&rqd
, 0, sizeof(struct nvm_rq
));
298 ret
= nvm_set_rqd_ppalist(dev
, &rqd
, ppas
, nr_ppas
, 1);
302 nvm_generic_to_addr_mode(dev
, &rqd
);
304 ret
= dev
->ops
->erase_block(dev
, &rqd
);
306 nvm_free_rqd_ppalist(dev
, &rqd
);
310 EXPORT_SYMBOL(nvm_erase_ppa
);
312 void nvm_end_io(struct nvm_rq
*rqd
, int error
)
317 EXPORT_SYMBOL(nvm_end_io
);
319 static void nvm_end_io_sync(struct nvm_rq
*rqd
)
321 struct completion
*waiting
= rqd
->wait
;
328 static int __nvm_submit_ppa(struct nvm_dev
*dev
, struct nvm_rq
*rqd
, int opcode
,
329 int flags
, void *buf
, int len
)
331 DECLARE_COMPLETION_ONSTACK(wait
);
334 unsigned long hang_check
;
336 bio
= bio_map_kern(dev
->q
, buf
, len
, GFP_KERNEL
);
337 if (IS_ERR_OR_NULL(bio
))
340 nvm_generic_to_addr_mode(dev
, rqd
);
343 rqd
->opcode
= opcode
;
347 rqd
->end_io
= nvm_end_io_sync
;
349 ret
= dev
->ops
->submit_io(dev
, rqd
);
355 /* Prevent hang_check timer from firing at us during very long I/O */
356 hang_check
= sysctl_hung_task_timeout_secs
;
358 while (!wait_for_completion_io_timeout(&wait
,
359 hang_check
* (HZ
/2)))
362 wait_for_completion_io(&wait
);
368 * nvm_submit_ppa_list - submit user-defined ppa list to device. The user must
369 * take to free ppa list if necessary.
371 * @ppa_list: user created ppa_list
372 * @nr_ppas: length of ppa_list
373 * @opcode: device opcode
374 * @flags: device flags
376 * @len: data buffer length
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
)
383 if (dev
->ops
->max_phys_sect
< nr_ppas
)
386 memset(&rqd
, 0, sizeof(struct nvm_rq
));
388 rqd
.nr_ppas
= nr_ppas
;
390 rqd
.ppa_list
= ppa_list
;
392 rqd
.ppa_addr
= ppa_list
[0];
394 return __nvm_submit_ppa(dev
, &rqd
, opcode
, flags
, buf
, len
);
396 EXPORT_SYMBOL(nvm_submit_ppa_list
);
399 * nvm_submit_ppa - submit PPAs to device. PPAs will automatically be unfolded
400 * as single, dual, quad plane PPAs depending on device type.
402 * @ppa: user created ppa_list
403 * @nr_ppas: length of ppa_list
404 * @opcode: device opcode
405 * @flags: device flags
407 * @len: data buffer length
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
)
415 memset(&rqd
, 0, sizeof(struct nvm_rq
));
416 ret
= nvm_set_rqd_ppalist(dev
, &rqd
, ppa
, nr_ppas
, 1);
420 ret
= __nvm_submit_ppa(dev
, &rqd
, opcode
, flags
, buf
, len
);
422 nvm_free_rqd_ppalist(dev
, &rqd
);
426 EXPORT_SYMBOL(nvm_submit_ppa
);
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
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.
436 int nvm_bb_tbl_fold(struct nvm_dev
*dev
, u8
*blks
, int nr_blks
)
438 int blk
, offset
, pl
, blktype
;
440 if (nr_blks
!= dev
->blks_per_lun
* dev
->plane_mode
)
443 for (blk
= 0; blk
< dev
->blks_per_lun
; blk
++) {
444 offset
= blk
* dev
->plane_mode
;
445 blktype
= blks
[offset
];
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
];
459 return dev
->blks_per_lun
;
461 EXPORT_SYMBOL(nvm_bb_tbl_fold
);
463 int nvm_get_bb_tbl(struct nvm_dev
*dev
, struct ppa_addr ppa
, u8
*blks
)
465 ppa
= generic_to_dev_addr(dev
, ppa
);
467 return dev
->ops
->get_bb_tbl(dev
, ppa
, blks
);
469 EXPORT_SYMBOL(nvm_get_bb_tbl
);
471 static int nvm_init_slc_tbl(struct nvm_dev
*dev
, struct nvm_id_group
*grp
)
475 dev
->lps_per_blk
= dev
->pgs_per_blk
;
476 dev
->lptbl
= kcalloc(dev
->lps_per_blk
, sizeof(int), GFP_KERNEL
);
480 /* Just a linear array */
481 for (i
= 0; i
< dev
->lps_per_blk
; i
++)
487 static int nvm_init_mlc_tbl(struct nvm_dev
*dev
, struct nvm_id_group
*grp
)
490 struct nvm_id_lp_mlc
*mlc
= &grp
->lptbl
.mlc
;
495 dev
->lps_per_blk
= mlc
->num_pairs
;
496 dev
->lptbl
= kcalloc(dev
->lps_per_blk
, sizeof(int), GFP_KERNEL
);
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
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);
511 dev
->lptbl
[i
] = dev
->lptbl
[i
- 1] + (p
& 0xF);
517 static int nvm_core_init(struct nvm_dev
*dev
)
519 struct nvm_id
*id
= &dev
->identity
;
520 struct nvm_id_group
*grp
= &id
->groups
[0];
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
));
537 dev
->plane_mode
= NVM_PLANE_SINGLE
;
538 dev
->max_rq_size
= dev
->ops
->max_phys_sect
* dev
->sec_size
;
540 if (grp
->mpos
& 0x020202)
541 dev
->plane_mode
= NVM_PLANE_DOUBLE
;
542 if (grp
->mpos
& 0x040404)
543 dev
->plane_mode
= NVM_PLANE_QUAD
;
545 if (grp
->mtype
!= 0) {
546 pr_err("nvm: memory type not supported\n");
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
;
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
);
562 switch (grp
->fmtype
) {
563 case NVM_ID_FMTYPE_SLC
:
564 if (nvm_init_slc_tbl(dev
, grp
)) {
569 case NVM_ID_FMTYPE_MLC
:
570 if (nvm_init_mlc_tbl(dev
, grp
)) {
576 pr_err("nvm: flash type not supported\n");
581 mutex_init(&dev
->mlock
);
582 spin_lock_init(&dev
->lock
);
584 blk_queue_logical_block_size(dev
->q
, dev
->sec_size
);
592 static void nvm_free_mgr(struct nvm_dev
*dev
)
597 dev
->mt
->unregister_mgr(dev
);
601 static void nvm_free(struct nvm_dev
*dev
)
612 static int nvm_init(struct nvm_dev
*dev
)
616 if (!dev
->q
|| !dev
->ops
)
619 if (dev
->ops
->identity(dev
, &dev
->identity
)) {
620 pr_err("nvm: device could not be identified\n");
624 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
625 dev
->identity
.ver_id
, dev
->identity
.vmnt
,
626 dev
->identity
.cgrps
);
628 if (dev
->identity
.ver_id
!= 1) {
629 pr_err("nvm: device not supported by kernel.");
633 if (dev
->identity
.cgrps
!= 1) {
634 pr_err("nvm: only one group configuration supported.");
638 ret
= nvm_core_init(dev
);
640 pr_err("nvm: could not initialize core structures.\n");
644 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
645 dev
->name
, dev
->sec_per_pg
, dev
->nr_planes
,
646 dev
->pgs_per_blk
, dev
->blks_per_lun
, dev
->nr_luns
,
650 pr_err("nvm: failed to initialize nvm\n");
654 static void nvm_exit(struct nvm_dev
*dev
)
657 dev
->ops
->destroy_dma_pool(dev
->dma_pool
);
660 pr_info("nvm: successfully unloaded\n");
663 int nvm_register(struct request_queue
*q
, char *disk_name
,
664 struct nvm_dev_ops
*ops
)
672 dev
= kzalloc(sizeof(struct nvm_dev
), GFP_KERNEL
);
678 strncpy(dev
->name
, disk_name
, DISK_NAME_LEN
);
684 if (dev
->ops
->max_phys_sect
> 256) {
685 pr_info("nvm: max sectors supported is 256.\n");
690 if (dev
->ops
->max_phys_sect
> 1) {
691 dev
->dma_pool
= dev
->ops
->create_dma_pool(dev
, "ppalist");
692 if (!dev
->dma_pool
) {
693 pr_err("nvm: could not create dma pool\n");
699 if (dev
->identity
.cap
& NVM_ID_DCAP_BBLKMGMT
) {
700 ret
= nvm_get_sysblock(dev
, &dev
->sb
);
702 pr_err("nvm: device not initialized.\n");
704 pr_err("nvm: err (%d) on device initialization\n", ret
);
707 /* register device with a supported media manager */
708 down_write(&nvm_lock
);
710 dev
->mt
= nvm_init_mgr(dev
);
711 list_add(&dev
->devices
, &nvm_devices
);
720 EXPORT_SYMBOL(nvm_register
);
722 void nvm_unregister(char *disk_name
)
726 down_write(&nvm_lock
);
727 dev
= nvm_find_nvm_dev(disk_name
);
729 pr_err("nvm: could not find device %s to unregister\n",
735 list_del(&dev
->devices
);
741 EXPORT_SYMBOL(nvm_unregister
);
743 static int __nvm_configure_create(struct nvm_ioctl_create
*create
)
746 struct nvm_ioctl_create_simple
*s
;
748 down_write(&nvm_lock
);
749 dev
= nvm_find_nvm_dev(create
->dev
);
753 pr_err("nvm: device not found\n");
758 pr_info("nvm: device has no media manager registered.\n");
762 if (create
->conf
.type
!= NVM_CONFIG_TYPE_SIMPLE
) {
763 pr_err("nvm: config type not valid\n");
768 if (s
->lun_begin
> s
->lun_end
|| s
->lun_end
> dev
->nr_luns
) {
769 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
770 s
->lun_begin
, s
->lun_end
, dev
->nr_luns
);
774 return dev
->mt
->create_tgt(dev
, create
);
777 #ifdef CONFIG_NVM_DEBUG
778 static int nvm_configure_show(const char *val
)
781 char opcode
, devname
[DISK_NAME_LEN
];
784 ret
= sscanf(val
, "%c %32s", &opcode
, devname
);
786 pr_err("nvm: invalid command. Use \"opcode devicename\".\n");
790 down_write(&nvm_lock
);
791 dev
= nvm_find_nvm_dev(devname
);
794 pr_err("nvm: device not found\n");
801 dev
->mt
->lun_info_print(dev
);
806 static int nvm_configure_remove(const char *val
)
808 struct nvm_ioctl_remove remove
;
813 ret
= sscanf(val
, "%c %256s", &opcode
, remove
.tgtname
);
815 pr_err("nvm: invalid command. Use \"d targetname\".\n");
821 list_for_each_entry(dev
, &nvm_devices
, devices
) {
822 ret
= dev
->mt
->remove_tgt(dev
, &remove
);
830 static int nvm_configure_create(const char *val
)
832 struct nvm_ioctl_create create
;
834 int lun_begin
, lun_end
, ret
;
836 ret
= sscanf(val
, "%c %256s %256s %48s %u:%u", &opcode
, create
.dev
,
837 create
.tgtname
, create
.tgttype
,
838 &lun_begin
, &lun_end
);
840 pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n");
845 create
.conf
.type
= NVM_CONFIG_TYPE_SIMPLE
;
846 create
.conf
.s
.lun_begin
= lun_begin
;
847 create
.conf
.s
.lun_end
= lun_end
;
849 return __nvm_configure_create(&create
);
853 /* Exposes administrative interface through /sys/module/lnvm/configure_by_str */
854 static int nvm_configure_by_str_event(const char *val
,
855 const struct kernel_param
*kp
)
860 ret
= sscanf(val
, "%c", &opcode
);
862 pr_err("nvm: string must have the format of \"cmd ...\"\n");
868 return nvm_configure_create(val
);
870 return nvm_configure_remove(val
);
872 return nvm_configure_show(val
);
874 pr_err("nvm: invalid command\n");
881 static int nvm_configure_get(char *buf
, const struct kernel_param
*kp
)
886 sz
= sprintf(buf
, "available devices:\n");
887 down_write(&nvm_lock
);
888 list_for_each_entry(dev
, &nvm_devices
, devices
) {
889 if (sz
> 4095 - DISK_NAME_LEN
- 2)
891 sz
+= sprintf(buf
+ sz
, " %32s\n", dev
->name
);
898 static const struct kernel_param_ops nvm_configure_by_str_event_param_ops
= {
899 .set
= nvm_configure_by_str_event
,
900 .get
= nvm_configure_get
,
903 #undef MODULE_PARAM_PREFIX
904 #define MODULE_PARAM_PREFIX "lnvm."
906 module_param_cb(configure_debug
, &nvm_configure_by_str_event_param_ops
, NULL
,
909 #endif /* CONFIG_NVM_DEBUG */
911 static long nvm_ioctl_info(struct file
*file
, void __user
*arg
)
913 struct nvm_ioctl_info
*info
;
914 struct nvm_tgt_type
*tt
;
917 if (!capable(CAP_SYS_ADMIN
))
920 info
= memdup_user(arg
, sizeof(struct nvm_ioctl_info
));
924 info
->version
[0] = NVM_VERSION_MAJOR
;
925 info
->version
[1] = NVM_VERSION_MINOR
;
926 info
->version
[2] = NVM_VERSION_PATCH
;
928 down_write(&nvm_lock
);
929 list_for_each_entry(tt
, &nvm_tgt_types
, list
) {
930 struct nvm_ioctl_info_tgt
*tgt
= &info
->tgts
[tgt_iter
];
932 tgt
->version
[0] = tt
->version
[0];
933 tgt
->version
[1] = tt
->version
[1];
934 tgt
->version
[2] = tt
->version
[2];
935 strncpy(tgt
->tgtname
, tt
->name
, NVM_TTYPE_NAME_MAX
);
940 info
->tgtsize
= tgt_iter
;
943 if (copy_to_user(arg
, info
, sizeof(struct nvm_ioctl_info
))) {
952 static long nvm_ioctl_get_devices(struct file
*file
, void __user
*arg
)
954 struct nvm_ioctl_get_devices
*devices
;
958 if (!capable(CAP_SYS_ADMIN
))
961 devices
= kzalloc(sizeof(struct nvm_ioctl_get_devices
), GFP_KERNEL
);
965 down_write(&nvm_lock
);
966 list_for_each_entry(dev
, &nvm_devices
, devices
) {
967 struct nvm_ioctl_device_info
*info
= &devices
->info
[i
];
969 sprintf(info
->devname
, "%s", dev
->name
);
971 info
->bmversion
[0] = dev
->mt
->version
[0];
972 info
->bmversion
[1] = dev
->mt
->version
[1];
973 info
->bmversion
[2] = dev
->mt
->version
[2];
974 sprintf(info
->bmname
, "%s", dev
->mt
->name
);
976 sprintf(info
->bmname
, "none");
981 pr_err("nvm: max 31 devices can be reported.\n");
987 devices
->nr_devices
= i
;
989 if (copy_to_user(arg
, devices
,
990 sizeof(struct nvm_ioctl_get_devices
))) {
999 static long nvm_ioctl_dev_create(struct file
*file
, void __user
*arg
)
1001 struct nvm_ioctl_create create
;
1003 if (!capable(CAP_SYS_ADMIN
))
1006 if (copy_from_user(&create
, arg
, sizeof(struct nvm_ioctl_create
)))
1009 create
.dev
[DISK_NAME_LEN
- 1] = '\0';
1010 create
.tgttype
[NVM_TTYPE_NAME_MAX
- 1] = '\0';
1011 create
.tgtname
[DISK_NAME_LEN
- 1] = '\0';
1013 if (create
.flags
!= 0) {
1014 pr_err("nvm: no flags supported\n");
1018 return __nvm_configure_create(&create
);
1021 static long nvm_ioctl_dev_remove(struct file
*file
, void __user
*arg
)
1023 struct nvm_ioctl_remove remove
;
1024 struct nvm_dev
*dev
;
1027 if (!capable(CAP_SYS_ADMIN
))
1030 if (copy_from_user(&remove
, arg
, sizeof(struct nvm_ioctl_remove
)))
1033 remove
.tgtname
[DISK_NAME_LEN
- 1] = '\0';
1035 if (remove
.flags
!= 0) {
1036 pr_err("nvm: no flags supported\n");
1040 list_for_each_entry(dev
, &nvm_devices
, devices
) {
1041 ret
= dev
->mt
->remove_tgt(dev
, &remove
);
1049 static void nvm_setup_nvm_sb_info(struct nvm_sb_info
*info
)
1052 info
->erase_cnt
= 0;
1056 static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init
*init
)
1058 struct nvm_dev
*dev
;
1059 struct nvm_sb_info info
;
1062 down_write(&nvm_lock
);
1063 dev
= nvm_find_nvm_dev(init
->dev
);
1064 up_write(&nvm_lock
);
1066 pr_err("nvm: device not found\n");
1070 nvm_setup_nvm_sb_info(&info
);
1072 strncpy(info
.mmtype
, init
->mmtype
, NVM_MMTYPE_LEN
);
1073 info
.fs_ppa
.ppa
= -1;
1075 if (dev
->identity
.cap
& NVM_ID_DCAP_BBLKMGMT
) {
1076 ret
= nvm_init_sysblock(dev
, &info
);
1081 memcpy(&dev
->sb
, &info
, sizeof(struct nvm_sb_info
));
1083 down_write(&nvm_lock
);
1084 dev
->mt
= nvm_init_mgr(dev
);
1085 up_write(&nvm_lock
);
1090 static long nvm_ioctl_dev_init(struct file
*file
, void __user
*arg
)
1092 struct nvm_ioctl_dev_init init
;
1094 if (!capable(CAP_SYS_ADMIN
))
1097 if (copy_from_user(&init
, arg
, sizeof(struct nvm_ioctl_dev_init
)))
1100 if (init
.flags
!= 0) {
1101 pr_err("nvm: no flags supported\n");
1105 init
.dev
[DISK_NAME_LEN
- 1] = '\0';
1107 return __nvm_ioctl_dev_init(&init
);
1110 static long nvm_ioctl_dev_factory(struct file
*file
, void __user
*arg
)
1112 struct nvm_ioctl_dev_factory fact
;
1113 struct nvm_dev
*dev
;
1115 if (!capable(CAP_SYS_ADMIN
))
1118 if (copy_from_user(&fact
, arg
, sizeof(struct nvm_ioctl_dev_factory
)))
1121 fact
.dev
[DISK_NAME_LEN
- 1] = '\0';
1123 if (fact
.flags
& ~(NVM_FACTORY_NR_BITS
- 1))
1126 down_write(&nvm_lock
);
1127 dev
= nvm_find_nvm_dev(fact
.dev
);
1128 up_write(&nvm_lock
);
1130 pr_err("nvm: device not found\n");
1136 if (dev
->identity
.cap
& NVM_ID_DCAP_BBLKMGMT
)
1137 return nvm_dev_factory(dev
, fact
.flags
);
1142 static long nvm_ctl_ioctl(struct file
*file
, uint cmd
, unsigned long arg
)
1144 void __user
*argp
= (void __user
*)arg
;
1148 return nvm_ioctl_info(file
, argp
);
1149 case NVM_GET_DEVICES
:
1150 return nvm_ioctl_get_devices(file
, argp
);
1151 case NVM_DEV_CREATE
:
1152 return nvm_ioctl_dev_create(file
, argp
);
1153 case NVM_DEV_REMOVE
:
1154 return nvm_ioctl_dev_remove(file
, argp
);
1156 return nvm_ioctl_dev_init(file
, argp
);
1157 case NVM_DEV_FACTORY
:
1158 return nvm_ioctl_dev_factory(file
, argp
);
1163 static const struct file_operations _ctl_fops
= {
1164 .open
= nonseekable_open
,
1165 .unlocked_ioctl
= nvm_ctl_ioctl
,
1166 .owner
= THIS_MODULE
,
1167 .llseek
= noop_llseek
,
1170 static struct miscdevice _nvm_misc
= {
1171 .minor
= MISC_DYNAMIC_MINOR
,
1173 .nodename
= "lightnvm/control",
1177 MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR
);
1179 static int __init
nvm_mod_init(void)
1183 ret
= misc_register(&_nvm_misc
);
1185 pr_err("nvm: misc_register failed for control device");
1190 static void __exit
nvm_mod_exit(void)
1192 misc_deregister(&_nvm_misc
);
1195 MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
1196 MODULE_LICENSE("GPL v2");
1197 MODULE_VERSION("0.1");
1198 module_init(nvm_mod_init
);
1199 module_exit(nvm_mod_exit
);