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/blkdev.h>
22 #include <linux/blk-mq.h>
23 #include <linux/list.h>
24 #include <linux/types.h>
25 #include <linux/sem.h>
26 #include <linux/bitmap.h>
27 #include <linux/module.h>
28 #include <linux/miscdevice.h>
29 #include <linux/lightnvm.h>
30 #include <linux/sched/sysctl.h>
31 #include <uapi/linux/lightnvm.h>
33 static LIST_HEAD(nvm_tgt_types
);
34 static LIST_HEAD(nvm_mgrs
);
35 static LIST_HEAD(nvm_devices
);
36 static LIST_HEAD(nvm_targets
);
37 static DECLARE_RWSEM(nvm_lock
);
39 static struct nvm_target
*nvm_find_target(const char *name
)
41 struct nvm_target
*tgt
;
43 list_for_each_entry(tgt
, &nvm_targets
, list
)
44 if (!strcmp(name
, tgt
->disk
->disk_name
))
50 static struct nvm_tgt_type
*nvm_find_target_type(const char *name
)
52 struct nvm_tgt_type
*tt
;
54 list_for_each_entry(tt
, &nvm_tgt_types
, list
)
55 if (!strcmp(name
, tt
->name
))
61 int nvm_register_tgt_type(struct nvm_tgt_type
*tt
)
65 down_write(&nvm_lock
);
66 if (nvm_find_target_type(tt
->name
))
69 list_add(&tt
->list
, &nvm_tgt_types
);
74 EXPORT_SYMBOL(nvm_register_tgt_type
);
76 void nvm_unregister_tgt_type(struct nvm_tgt_type
*tt
)
81 down_write(&nvm_lock
);
85 EXPORT_SYMBOL(nvm_unregister_tgt_type
);
87 void *nvm_dev_dma_alloc(struct nvm_dev
*dev
, gfp_t mem_flags
,
88 dma_addr_t
*dma_handler
)
90 return dev
->ops
->dev_dma_alloc(dev
, dev
->dma_pool
, mem_flags
,
93 EXPORT_SYMBOL(nvm_dev_dma_alloc
);
95 void nvm_dev_dma_free(struct nvm_dev
*dev
, void *addr
,
96 dma_addr_t dma_handler
)
98 dev
->ops
->dev_dma_free(dev
->dma_pool
, addr
, dma_handler
);
100 EXPORT_SYMBOL(nvm_dev_dma_free
);
102 static struct nvmm_type
*nvm_find_mgr_type(const char *name
)
104 struct nvmm_type
*mt
;
106 list_for_each_entry(mt
, &nvm_mgrs
, list
)
107 if (!strcmp(name
, mt
->name
))
113 struct nvmm_type
*nvm_init_mgr(struct nvm_dev
*dev
)
115 struct nvmm_type
*mt
;
118 lockdep_assert_held(&nvm_lock
);
120 list_for_each_entry(mt
, &nvm_mgrs
, list
) {
121 if (strncmp(dev
->sb
.mmtype
, mt
->name
, NVM_MMTYPE_LEN
))
124 ret
= mt
->register_mgr(dev
);
126 pr_err("nvm: media mgr failed to init (%d) on dev %s\n",
128 return NULL
; /* initialization failed */
136 int nvm_register_mgr(struct nvmm_type
*mt
)
141 down_write(&nvm_lock
);
142 if (nvm_find_mgr_type(mt
->name
)) {
146 list_add(&mt
->list
, &nvm_mgrs
);
149 /* try to register media mgr if any device have none configured */
150 list_for_each_entry(dev
, &nvm_devices
, devices
) {
154 dev
->mt
= nvm_init_mgr(dev
);
161 EXPORT_SYMBOL(nvm_register_mgr
);
163 void nvm_unregister_mgr(struct nvmm_type
*mt
)
168 down_write(&nvm_lock
);
172 EXPORT_SYMBOL(nvm_unregister_mgr
);
174 static struct nvm_dev
*nvm_find_nvm_dev(const char *name
)
178 list_for_each_entry(dev
, &nvm_devices
, devices
)
179 if (!strcmp(name
, dev
->name
))
185 struct nvm_block
*nvm_get_blk_unlocked(struct nvm_dev
*dev
, struct nvm_lun
*lun
,
188 return dev
->mt
->get_blk_unlocked(dev
, lun
, flags
);
190 EXPORT_SYMBOL(nvm_get_blk_unlocked
);
192 /* Assumes that all valid pages have already been moved on release to bm */
193 void nvm_put_blk_unlocked(struct nvm_dev
*dev
, struct nvm_block
*blk
)
195 return dev
->mt
->put_blk_unlocked(dev
, blk
);
197 EXPORT_SYMBOL(nvm_put_blk_unlocked
);
199 struct nvm_block
*nvm_get_blk(struct nvm_dev
*dev
, struct nvm_lun
*lun
,
202 return dev
->mt
->get_blk(dev
, lun
, flags
);
204 EXPORT_SYMBOL(nvm_get_blk
);
206 /* Assumes that all valid pages have already been moved on release to bm */
207 void nvm_put_blk(struct nvm_dev
*dev
, struct nvm_block
*blk
)
209 return dev
->mt
->put_blk(dev
, blk
);
211 EXPORT_SYMBOL(nvm_put_blk
);
213 int nvm_submit_io(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
215 return dev
->mt
->submit_io(dev
, rqd
);
217 EXPORT_SYMBOL(nvm_submit_io
);
219 int nvm_erase_blk(struct nvm_dev
*dev
, struct nvm_block
*blk
)
221 return dev
->mt
->erase_blk(dev
, blk
, 0);
223 EXPORT_SYMBOL(nvm_erase_blk
);
225 void nvm_addr_to_generic_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
] = dev_to_generic_addr(dev
,
234 rqd
->ppa_addr
= dev_to_generic_addr(dev
, rqd
->ppa_addr
);
237 EXPORT_SYMBOL(nvm_addr_to_generic_mode
);
239 void nvm_generic_to_addr_mode(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
243 if (rqd
->nr_ppas
> 1) {
244 for (i
= 0; i
< rqd
->nr_ppas
; i
++)
245 rqd
->ppa_list
[i
] = generic_to_dev_addr(dev
,
248 rqd
->ppa_addr
= generic_to_dev_addr(dev
, rqd
->ppa_addr
);
251 EXPORT_SYMBOL(nvm_generic_to_addr_mode
);
253 int nvm_set_rqd_ppalist(struct nvm_dev
*dev
, struct nvm_rq
*rqd
,
254 struct ppa_addr
*ppas
, int nr_ppas
, int vblk
)
256 int i
, plane_cnt
, pl_idx
;
258 if ((!vblk
|| dev
->plane_mode
== NVM_PLANE_SINGLE
) && nr_ppas
== 1) {
259 rqd
->nr_ppas
= nr_ppas
;
260 rqd
->ppa_addr
= ppas
[0];
265 rqd
->nr_ppas
= nr_ppas
;
266 rqd
->ppa_list
= nvm_dev_dma_alloc(dev
, GFP_KERNEL
, &rqd
->dma_ppa_list
);
267 if (!rqd
->ppa_list
) {
268 pr_err("nvm: failed to allocate dma memory\n");
273 for (i
= 0; i
< nr_ppas
; i
++)
274 rqd
->ppa_list
[i
] = ppas
[i
];
276 plane_cnt
= dev
->plane_mode
;
277 rqd
->nr_ppas
*= plane_cnt
;
279 for (i
= 0; i
< nr_ppas
; i
++) {
280 for (pl_idx
= 0; pl_idx
< plane_cnt
; pl_idx
++) {
281 ppas
[i
].g
.pl
= pl_idx
;
282 rqd
->ppa_list
[(pl_idx
* nr_ppas
) + i
] = ppas
[i
];
289 EXPORT_SYMBOL(nvm_set_rqd_ppalist
);
291 void nvm_free_rqd_ppalist(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
296 nvm_dev_dma_free(dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
298 EXPORT_SYMBOL(nvm_free_rqd_ppalist
);
300 int nvm_erase_ppa(struct nvm_dev
*dev
, struct ppa_addr
*ppas
, int nr_ppas
)
305 if (!dev
->ops
->erase_block
)
308 memset(&rqd
, 0, sizeof(struct nvm_rq
));
310 ret
= nvm_set_rqd_ppalist(dev
, &rqd
, ppas
, nr_ppas
, 1);
314 nvm_generic_to_addr_mode(dev
, &rqd
);
316 ret
= dev
->ops
->erase_block(dev
, &rqd
);
318 nvm_free_rqd_ppalist(dev
, &rqd
);
322 EXPORT_SYMBOL(nvm_erase_ppa
);
324 void nvm_end_io(struct nvm_rq
*rqd
, int error
)
329 EXPORT_SYMBOL(nvm_end_io
);
331 static void nvm_end_io_sync(struct nvm_rq
*rqd
)
333 struct completion
*waiting
= rqd
->wait
;
340 int __nvm_submit_ppa(struct nvm_dev
*dev
, struct nvm_rq
*rqd
, int opcode
,
341 int flags
, void *buf
, int len
)
343 DECLARE_COMPLETION_ONSTACK(wait
);
346 unsigned long hang_check
;
348 bio
= bio_map_kern(dev
->q
, buf
, len
, GFP_KERNEL
);
349 if (IS_ERR_OR_NULL(bio
))
352 nvm_generic_to_addr_mode(dev
, rqd
);
355 rqd
->opcode
= opcode
;
359 rqd
->end_io
= nvm_end_io_sync
;
361 ret
= dev
->ops
->submit_io(dev
, rqd
);
367 /* Prevent hang_check timer from firing at us during very long I/O */
368 hang_check
= sysctl_hung_task_timeout_secs
;
370 while (!wait_for_completion_io_timeout(&wait
, hang_check
* (HZ
/2)));
372 wait_for_completion_io(&wait
);
378 * nvm_submit_ppa_list - submit user-defined ppa list to device. The user must
379 * take to free ppa list if necessary.
381 * @ppa_list: user created ppa_list
382 * @nr_ppas: length of ppa_list
383 * @opcode: device opcode
384 * @flags: device flags
386 * @len: data buffer length
388 int nvm_submit_ppa_list(struct nvm_dev
*dev
, struct ppa_addr
*ppa_list
,
389 int nr_ppas
, int opcode
, int flags
, void *buf
, int len
)
393 if (dev
->ops
->max_phys_sect
< nr_ppas
)
396 memset(&rqd
, 0, sizeof(struct nvm_rq
));
398 rqd
.nr_ppas
= nr_ppas
;
400 rqd
.ppa_list
= ppa_list
;
402 rqd
.ppa_addr
= ppa_list
[0];
404 return __nvm_submit_ppa(dev
, &rqd
, opcode
, flags
, buf
, len
);
406 EXPORT_SYMBOL(nvm_submit_ppa_list
);
409 * nvm_submit_ppa - submit PPAs to device. PPAs will automatically be unfolded
410 * as single, dual, quad plane PPAs depending on device type.
412 * @ppa: user created ppa_list
413 * @nr_ppas: length of ppa_list
414 * @opcode: device opcode
415 * @flags: device flags
417 * @len: data buffer length
419 int nvm_submit_ppa(struct nvm_dev
*dev
, struct ppa_addr
*ppa
, int nr_ppas
,
420 int opcode
, int flags
, void *buf
, int len
)
425 memset(&rqd
, 0, sizeof(struct nvm_rq
));
426 ret
= nvm_set_rqd_ppalist(dev
, &rqd
, ppa
, nr_ppas
, 1);
430 ret
= __nvm_submit_ppa(dev
, &rqd
, opcode
, flags
, buf
, len
);
432 nvm_free_rqd_ppalist(dev
, &rqd
);
436 EXPORT_SYMBOL(nvm_submit_ppa
);
439 * folds a bad block list from its plane representation to its virtual
440 * block representation. The fold is done in place and reduced size is
443 * If any of the planes status are bad or grown bad block, the virtual block
444 * is marked bad. If not bad, the first plane state acts as the block state.
446 int nvm_bb_tbl_fold(struct nvm_dev
*dev
, u8
*blks
, int nr_blks
)
448 int blk
, offset
, pl
, blktype
;
450 if (nr_blks
!= dev
->blks_per_lun
* dev
->plane_mode
)
453 for (blk
= 0; blk
< dev
->blks_per_lun
; blk
++) {
454 offset
= blk
* dev
->plane_mode
;
455 blktype
= blks
[offset
];
457 /* Bad blocks on any planes take precedence over other types */
458 for (pl
= 0; pl
< dev
->plane_mode
; pl
++) {
459 if (blks
[offset
+ pl
] &
460 (NVM_BLK_T_BAD
|NVM_BLK_T_GRWN_BAD
)) {
461 blktype
= blks
[offset
+ pl
];
469 return dev
->blks_per_lun
;
471 EXPORT_SYMBOL(nvm_bb_tbl_fold
);
473 int nvm_get_bb_tbl(struct nvm_dev
*dev
, struct ppa_addr ppa
, u8
*blks
)
475 ppa
= generic_to_dev_addr(dev
, ppa
);
477 return dev
->ops
->get_bb_tbl(dev
, ppa
, blks
);
479 EXPORT_SYMBOL(nvm_get_bb_tbl
);
481 static int nvm_init_slc_tbl(struct nvm_dev
*dev
, struct nvm_id_group
*grp
)
485 dev
->lps_per_blk
= dev
->pgs_per_blk
;
486 dev
->lptbl
= kcalloc(dev
->lps_per_blk
, sizeof(int), GFP_KERNEL
);
490 /* Just a linear array */
491 for (i
= 0; i
< dev
->lps_per_blk
; i
++)
497 static int nvm_init_mlc_tbl(struct nvm_dev
*dev
, struct nvm_id_group
*grp
)
500 struct nvm_id_lp_mlc
*mlc
= &grp
->lptbl
.mlc
;
505 dev
->lps_per_blk
= mlc
->num_pairs
;
506 dev
->lptbl
= kcalloc(dev
->lps_per_blk
, sizeof(int), GFP_KERNEL
);
510 /* The lower page table encoding consists of a list of bytes, where each
511 * has a lower and an upper half. The first half byte maintains the
512 * increment value and every value after is an offset added to the
513 * previous incrementation value */
514 dev
->lptbl
[0] = mlc
->pairs
[0] & 0xF;
515 for (i
= 1; i
< dev
->lps_per_blk
; i
++) {
516 p
= mlc
->pairs
[i
>> 1];
517 if (i
& 0x1) /* upper */
518 dev
->lptbl
[i
] = dev
->lptbl
[i
- 1] + ((p
& 0xF0) >> 4);
520 dev
->lptbl
[i
] = dev
->lptbl
[i
- 1] + (p
& 0xF);
526 static int nvm_core_init(struct nvm_dev
*dev
)
528 struct nvm_id
*id
= &dev
->identity
;
529 struct nvm_id_group
*grp
= &id
->groups
[0];
533 dev
->nr_chnls
= grp
->num_ch
;
534 dev
->luns_per_chnl
= grp
->num_lun
;
535 dev
->pgs_per_blk
= grp
->num_pg
;
536 dev
->blks_per_lun
= grp
->num_blk
;
537 dev
->nr_planes
= grp
->num_pln
;
538 dev
->fpg_size
= grp
->fpg_sz
;
539 dev
->pfpg_size
= grp
->fpg_sz
* grp
->num_pln
;
540 dev
->sec_size
= grp
->csecs
;
541 dev
->oob_size
= grp
->sos
;
542 dev
->sec_per_pg
= grp
->fpg_sz
/ grp
->csecs
;
543 dev
->mccap
= grp
->mccap
;
544 memcpy(&dev
->ppaf
, &id
->ppaf
, sizeof(struct nvm_addr_format
));
546 dev
->plane_mode
= NVM_PLANE_SINGLE
;
547 dev
->max_rq_size
= dev
->ops
->max_phys_sect
* dev
->sec_size
;
549 if (grp
->mpos
& 0x020202)
550 dev
->plane_mode
= NVM_PLANE_DOUBLE
;
551 if (grp
->mpos
& 0x040404)
552 dev
->plane_mode
= NVM_PLANE_QUAD
;
554 if (grp
->mtype
!= 0) {
555 pr_err("nvm: memory type not supported\n");
559 /* calculated values */
560 dev
->sec_per_pl
= dev
->sec_per_pg
* dev
->nr_planes
;
561 dev
->sec_per_blk
= dev
->sec_per_pl
* dev
->pgs_per_blk
;
562 dev
->sec_per_lun
= dev
->sec_per_blk
* dev
->blks_per_lun
;
563 dev
->nr_luns
= dev
->luns_per_chnl
* dev
->nr_chnls
;
565 dev
->total_secs
= dev
->nr_luns
* dev
->sec_per_lun
;
566 dev
->lun_map
= kcalloc(BITS_TO_LONGS(dev
->nr_luns
),
567 sizeof(unsigned long), GFP_KERNEL
);
571 switch (grp
->fmtype
) {
572 case NVM_ID_FMTYPE_SLC
:
573 if (nvm_init_slc_tbl(dev
, grp
)) {
578 case NVM_ID_FMTYPE_MLC
:
579 if (nvm_init_mlc_tbl(dev
, grp
)) {
585 pr_err("nvm: flash type not supported\n");
590 mutex_init(&dev
->mlock
);
591 spin_lock_init(&dev
->lock
);
599 static void nvm_remove_target(struct nvm_target
*t
)
601 struct nvm_tgt_type
*tt
= t
->type
;
602 struct gendisk
*tdisk
= t
->disk
;
603 struct request_queue
*q
= tdisk
->queue
;
605 lockdep_assert_held(&nvm_lock
);
608 blk_cleanup_queue(q
);
611 tt
->exit(tdisk
->private_data
);
619 static void nvm_free_mgr(struct nvm_dev
*dev
)
621 struct nvm_target
*tgt
, *tmp
;
626 down_write(&nvm_lock
);
627 list_for_each_entry_safe(tgt
, tmp
, &nvm_targets
, list
) {
631 nvm_remove_target(tgt
);
635 dev
->mt
->unregister_mgr(dev
);
639 static void nvm_free(struct nvm_dev
*dev
)
650 static int nvm_init(struct nvm_dev
*dev
)
654 if (!dev
->q
|| !dev
->ops
)
657 if (dev
->ops
->identity(dev
, &dev
->identity
)) {
658 pr_err("nvm: device could not be identified\n");
662 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
663 dev
->identity
.ver_id
, dev
->identity
.vmnt
,
664 dev
->identity
.cgrps
);
666 if (dev
->identity
.ver_id
!= 1) {
667 pr_err("nvm: device not supported by kernel.");
671 if (dev
->identity
.cgrps
!= 1) {
672 pr_err("nvm: only one group configuration supported.");
676 ret
= nvm_core_init(dev
);
678 pr_err("nvm: could not initialize core structures.\n");
682 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
683 dev
->name
, dev
->sec_per_pg
, dev
->nr_planes
,
684 dev
->pgs_per_blk
, dev
->blks_per_lun
, dev
->nr_luns
,
688 pr_err("nvm: failed to initialize nvm\n");
692 static void nvm_exit(struct nvm_dev
*dev
)
695 dev
->ops
->destroy_dma_pool(dev
->dma_pool
);
698 pr_info("nvm: successfully unloaded\n");
701 int nvm_register(struct request_queue
*q
, char *disk_name
,
702 struct nvm_dev_ops
*ops
)
710 dev
= kzalloc(sizeof(struct nvm_dev
), GFP_KERNEL
);
716 strncpy(dev
->name
, disk_name
, DISK_NAME_LEN
);
722 if (dev
->ops
->max_phys_sect
> 256) {
723 pr_info("nvm: max sectors supported is 256.\n");
728 if (dev
->ops
->max_phys_sect
> 1) {
729 dev
->dma_pool
= dev
->ops
->create_dma_pool(dev
, "ppalist");
730 if (!dev
->dma_pool
) {
731 pr_err("nvm: could not create dma pool\n");
737 if (dev
->identity
.cap
& NVM_ID_DCAP_BBLKMGMT
) {
738 ret
= nvm_get_sysblock(dev
, &dev
->sb
);
740 pr_err("nvm: device not initialized.\n");
742 pr_err("nvm: err (%d) on device initialization\n", ret
);
745 /* register device with a supported media manager */
746 down_write(&nvm_lock
);
748 dev
->mt
= nvm_init_mgr(dev
);
749 list_add(&dev
->devices
, &nvm_devices
);
758 EXPORT_SYMBOL(nvm_register
);
760 void nvm_unregister(char *disk_name
)
764 down_write(&nvm_lock
);
765 dev
= nvm_find_nvm_dev(disk_name
);
767 pr_err("nvm: could not find device %s to unregister\n",
773 list_del(&dev
->devices
);
779 EXPORT_SYMBOL(nvm_unregister
);
781 static const struct block_device_operations nvm_fops
= {
782 .owner
= THIS_MODULE
,
785 static int nvm_create_target(struct nvm_dev
*dev
,
786 struct nvm_ioctl_create
*create
)
788 struct nvm_ioctl_create_simple
*s
= &create
->conf
.s
;
789 struct request_queue
*tqueue
;
790 struct gendisk
*tdisk
;
791 struct nvm_tgt_type
*tt
;
792 struct nvm_target
*t
;
796 pr_info("nvm: device has no media manager registered.\n");
800 down_write(&nvm_lock
);
801 tt
= nvm_find_target_type(create
->tgttype
);
803 pr_err("nvm: target type %s not found\n", create
->tgttype
);
808 t
= nvm_find_target(create
->tgtname
);
810 pr_err("nvm: target name already exists.\n");
816 t
= kmalloc(sizeof(struct nvm_target
), GFP_KERNEL
);
820 tqueue
= blk_alloc_queue_node(GFP_KERNEL
, dev
->q
->node
);
823 blk_queue_make_request(tqueue
, tt
->make_rq
);
825 tdisk
= alloc_disk(0);
829 sprintf(tdisk
->disk_name
, "%s", create
->tgtname
);
830 tdisk
->flags
= GENHD_FL_EXT_DEVT
;
832 tdisk
->first_minor
= 0;
833 tdisk
->fops
= &nvm_fops
;
834 tdisk
->queue
= tqueue
;
836 targetdata
= tt
->init(dev
, tdisk
, s
->lun_begin
, s
->lun_end
);
837 if (IS_ERR(targetdata
))
840 tdisk
->private_data
= targetdata
;
841 tqueue
->queuedata
= targetdata
;
843 blk_queue_max_hw_sectors(tqueue
, 8 * dev
->ops
->max_phys_sect
);
845 set_capacity(tdisk
, tt
->capacity(targetdata
));
852 down_write(&nvm_lock
);
853 list_add_tail(&t
->list
, &nvm_targets
);
860 blk_cleanup_queue(tqueue
);
866 static int __nvm_configure_create(struct nvm_ioctl_create
*create
)
869 struct nvm_ioctl_create_simple
*s
;
871 down_write(&nvm_lock
);
872 dev
= nvm_find_nvm_dev(create
->dev
);
875 pr_err("nvm: device not found\n");
879 if (create
->conf
.type
!= NVM_CONFIG_TYPE_SIMPLE
) {
880 pr_err("nvm: config type not valid\n");
885 if (s
->lun_begin
> s
->lun_end
|| s
->lun_end
> dev
->nr_luns
) {
886 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
887 s
->lun_begin
, s
->lun_end
, dev
->nr_luns
);
891 return nvm_create_target(dev
, create
);
894 static int __nvm_configure_remove(struct nvm_ioctl_remove
*remove
)
896 struct nvm_target
*t
;
898 down_write(&nvm_lock
);
899 t
= nvm_find_target(remove
->tgtname
);
901 pr_err("nvm: target \"%s\" doesn't exist.\n", remove
->tgtname
);
906 nvm_remove_target(t
);
912 #ifdef CONFIG_NVM_DEBUG
913 static int nvm_configure_show(const char *val
)
916 char opcode
, devname
[DISK_NAME_LEN
];
919 ret
= sscanf(val
, "%c %32s", &opcode
, devname
);
921 pr_err("nvm: invalid command. Use \"opcode devicename\".\n");
925 down_write(&nvm_lock
);
926 dev
= nvm_find_nvm_dev(devname
);
929 pr_err("nvm: device not found\n");
936 dev
->mt
->lun_info_print(dev
);
941 static int nvm_configure_remove(const char *val
)
943 struct nvm_ioctl_remove remove
;
947 ret
= sscanf(val
, "%c %256s", &opcode
, remove
.tgtname
);
949 pr_err("nvm: invalid command. Use \"d targetname\".\n");
955 return __nvm_configure_remove(&remove
);
958 static int nvm_configure_create(const char *val
)
960 struct nvm_ioctl_create create
;
962 int lun_begin
, lun_end
, ret
;
964 ret
= sscanf(val
, "%c %256s %256s %48s %u:%u", &opcode
, create
.dev
,
965 create
.tgtname
, create
.tgttype
,
966 &lun_begin
, &lun_end
);
968 pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n");
973 create
.conf
.type
= NVM_CONFIG_TYPE_SIMPLE
;
974 create
.conf
.s
.lun_begin
= lun_begin
;
975 create
.conf
.s
.lun_end
= lun_end
;
977 return __nvm_configure_create(&create
);
981 /* Exposes administrative interface through /sys/module/lnvm/configure_by_str */
982 static int nvm_configure_by_str_event(const char *val
,
983 const struct kernel_param
*kp
)
988 ret
= sscanf(val
, "%c", &opcode
);
990 pr_err("nvm: string must have the format of \"cmd ...\"\n");
996 return nvm_configure_create(val
);
998 return nvm_configure_remove(val
);
1000 return nvm_configure_show(val
);
1002 pr_err("nvm: invalid command\n");
1009 static int nvm_configure_get(char *buf
, const struct kernel_param
*kp
)
1012 struct nvm_dev
*dev
;
1014 sz
= sprintf(buf
, "available devices:\n");
1015 down_write(&nvm_lock
);
1016 list_for_each_entry(dev
, &nvm_devices
, devices
) {
1017 if (sz
> 4095 - DISK_NAME_LEN
- 2)
1019 sz
+= sprintf(buf
+ sz
, " %32s\n", dev
->name
);
1021 up_write(&nvm_lock
);
1026 static const struct kernel_param_ops nvm_configure_by_str_event_param_ops
= {
1027 .set
= nvm_configure_by_str_event
,
1028 .get
= nvm_configure_get
,
1031 #undef MODULE_PARAM_PREFIX
1032 #define MODULE_PARAM_PREFIX "lnvm."
1034 module_param_cb(configure_debug
, &nvm_configure_by_str_event_param_ops
, NULL
,
1037 #endif /* CONFIG_NVM_DEBUG */
1039 static long nvm_ioctl_info(struct file
*file
, void __user
*arg
)
1041 struct nvm_ioctl_info
*info
;
1042 struct nvm_tgt_type
*tt
;
1045 if (!capable(CAP_SYS_ADMIN
))
1048 info
= memdup_user(arg
, sizeof(struct nvm_ioctl_info
));
1052 info
->version
[0] = NVM_VERSION_MAJOR
;
1053 info
->version
[1] = NVM_VERSION_MINOR
;
1054 info
->version
[2] = NVM_VERSION_PATCH
;
1056 down_write(&nvm_lock
);
1057 list_for_each_entry(tt
, &nvm_tgt_types
, list
) {
1058 struct nvm_ioctl_info_tgt
*tgt
= &info
->tgts
[tgt_iter
];
1060 tgt
->version
[0] = tt
->version
[0];
1061 tgt
->version
[1] = tt
->version
[1];
1062 tgt
->version
[2] = tt
->version
[2];
1063 strncpy(tgt
->tgtname
, tt
->name
, NVM_TTYPE_NAME_MAX
);
1068 info
->tgtsize
= tgt_iter
;
1069 up_write(&nvm_lock
);
1071 if (copy_to_user(arg
, info
, sizeof(struct nvm_ioctl_info
))) {
1080 static long nvm_ioctl_get_devices(struct file
*file
, void __user
*arg
)
1082 struct nvm_ioctl_get_devices
*devices
;
1083 struct nvm_dev
*dev
;
1086 if (!capable(CAP_SYS_ADMIN
))
1089 devices
= kzalloc(sizeof(struct nvm_ioctl_get_devices
), GFP_KERNEL
);
1093 down_write(&nvm_lock
);
1094 list_for_each_entry(dev
, &nvm_devices
, devices
) {
1095 struct nvm_ioctl_device_info
*info
= &devices
->info
[i
];
1097 sprintf(info
->devname
, "%s", dev
->name
);
1099 info
->bmversion
[0] = dev
->mt
->version
[0];
1100 info
->bmversion
[1] = dev
->mt
->version
[1];
1101 info
->bmversion
[2] = dev
->mt
->version
[2];
1102 sprintf(info
->bmname
, "%s", dev
->mt
->name
);
1104 sprintf(info
->bmname
, "none");
1109 pr_err("nvm: max 31 devices can be reported.\n");
1113 up_write(&nvm_lock
);
1115 devices
->nr_devices
= i
;
1117 if (copy_to_user(arg
, devices
,
1118 sizeof(struct nvm_ioctl_get_devices
))) {
1127 static long nvm_ioctl_dev_create(struct file
*file
, void __user
*arg
)
1129 struct nvm_ioctl_create create
;
1131 if (!capable(CAP_SYS_ADMIN
))
1134 if (copy_from_user(&create
, arg
, sizeof(struct nvm_ioctl_create
)))
1137 create
.dev
[DISK_NAME_LEN
- 1] = '\0';
1138 create
.tgttype
[NVM_TTYPE_NAME_MAX
- 1] = '\0';
1139 create
.tgtname
[DISK_NAME_LEN
- 1] = '\0';
1141 if (create
.flags
!= 0) {
1142 pr_err("nvm: no flags supported\n");
1146 return __nvm_configure_create(&create
);
1149 static long nvm_ioctl_dev_remove(struct file
*file
, void __user
*arg
)
1151 struct nvm_ioctl_remove remove
;
1153 if (!capable(CAP_SYS_ADMIN
))
1156 if (copy_from_user(&remove
, arg
, sizeof(struct nvm_ioctl_remove
)))
1159 remove
.tgtname
[DISK_NAME_LEN
- 1] = '\0';
1161 if (remove
.flags
!= 0) {
1162 pr_err("nvm: no flags supported\n");
1166 return __nvm_configure_remove(&remove
);
1169 static void nvm_setup_nvm_sb_info(struct nvm_sb_info
*info
)
1172 info
->erase_cnt
= 0;
1176 static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init
*init
)
1178 struct nvm_dev
*dev
;
1179 struct nvm_sb_info info
;
1182 down_write(&nvm_lock
);
1183 dev
= nvm_find_nvm_dev(init
->dev
);
1184 up_write(&nvm_lock
);
1186 pr_err("nvm: device not found\n");
1190 nvm_setup_nvm_sb_info(&info
);
1192 strncpy(info
.mmtype
, init
->mmtype
, NVM_MMTYPE_LEN
);
1193 info
.fs_ppa
.ppa
= -1;
1195 if (dev
->identity
.cap
& NVM_ID_DCAP_BBLKMGMT
) {
1196 ret
= nvm_init_sysblock(dev
, &info
);
1201 memcpy(&dev
->sb
, &info
, sizeof(struct nvm_sb_info
));
1203 down_write(&nvm_lock
);
1204 dev
->mt
= nvm_init_mgr(dev
);
1205 up_write(&nvm_lock
);
1210 static long nvm_ioctl_dev_init(struct file
*file
, void __user
*arg
)
1212 struct nvm_ioctl_dev_init init
;
1214 if (!capable(CAP_SYS_ADMIN
))
1217 if (copy_from_user(&init
, arg
, sizeof(struct nvm_ioctl_dev_init
)))
1220 if (init
.flags
!= 0) {
1221 pr_err("nvm: no flags supported\n");
1225 init
.dev
[DISK_NAME_LEN
- 1] = '\0';
1227 return __nvm_ioctl_dev_init(&init
);
1230 static long nvm_ioctl_dev_factory(struct file
*file
, void __user
*arg
)
1232 struct nvm_ioctl_dev_factory fact
;
1233 struct nvm_dev
*dev
;
1235 if (!capable(CAP_SYS_ADMIN
))
1238 if (copy_from_user(&fact
, arg
, sizeof(struct nvm_ioctl_dev_factory
)))
1241 fact
.dev
[DISK_NAME_LEN
- 1] = '\0';
1243 if (fact
.flags
& ~(NVM_FACTORY_NR_BITS
- 1))
1246 down_write(&nvm_lock
);
1247 dev
= nvm_find_nvm_dev(fact
.dev
);
1248 up_write(&nvm_lock
);
1250 pr_err("nvm: device not found\n");
1256 if (dev
->identity
.cap
& NVM_ID_DCAP_BBLKMGMT
)
1257 return nvm_dev_factory(dev
, fact
.flags
);
1262 static long nvm_ctl_ioctl(struct file
*file
, uint cmd
, unsigned long arg
)
1264 void __user
*argp
= (void __user
*)arg
;
1268 return nvm_ioctl_info(file
, argp
);
1269 case NVM_GET_DEVICES
:
1270 return nvm_ioctl_get_devices(file
, argp
);
1271 case NVM_DEV_CREATE
:
1272 return nvm_ioctl_dev_create(file
, argp
);
1273 case NVM_DEV_REMOVE
:
1274 return nvm_ioctl_dev_remove(file
, argp
);
1276 return nvm_ioctl_dev_init(file
, argp
);
1277 case NVM_DEV_FACTORY
:
1278 return nvm_ioctl_dev_factory(file
, argp
);
1283 static const struct file_operations _ctl_fops
= {
1284 .open
= nonseekable_open
,
1285 .unlocked_ioctl
= nvm_ctl_ioctl
,
1286 .owner
= THIS_MODULE
,
1287 .llseek
= noop_llseek
,
1290 static struct miscdevice _nvm_misc
= {
1291 .minor
= MISC_DYNAMIC_MINOR
,
1293 .nodename
= "lightnvm/control",
1297 MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR
);
1299 static int __init
nvm_mod_init(void)
1303 ret
= misc_register(&_nvm_misc
);
1305 pr_err("nvm: misc_register failed for control device");
1310 static void __exit
nvm_mod_exit(void)
1312 misc_deregister(&_nvm_misc
);
1315 MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
1316 MODULE_LICENSE("GPL v2");
1317 MODULE_VERSION("0.1");
1318 module_init(nvm_mod_init
);
1319 module_exit(nvm_mod_exit
);