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>
32 static LIST_HEAD(nvm_tgt_types
);
33 static DECLARE_RWSEM(nvm_tgtt_lock
);
34 static LIST_HEAD(nvm_mgrs
);
35 static LIST_HEAD(nvm_devices
);
36 static DECLARE_RWSEM(nvm_lock
);
38 struct nvm_tgt_type
*nvm_find_target_type(const char *name
, int lock
)
40 struct nvm_tgt_type
*tmp
, *tt
= NULL
;
43 down_write(&nvm_tgtt_lock
);
45 list_for_each_entry(tmp
, &nvm_tgt_types
, list
)
46 if (!strcmp(name
, tmp
->name
)) {
52 up_write(&nvm_tgtt_lock
);
55 EXPORT_SYMBOL(nvm_find_target_type
);
57 int nvm_register_tgt_type(struct nvm_tgt_type
*tt
)
61 down_write(&nvm_tgtt_lock
);
62 if (nvm_find_target_type(tt
->name
, 0))
65 list_add(&tt
->list
, &nvm_tgt_types
);
66 up_write(&nvm_tgtt_lock
);
70 EXPORT_SYMBOL(nvm_register_tgt_type
);
72 void nvm_unregister_tgt_type(struct nvm_tgt_type
*tt
)
77 down_write(&nvm_lock
);
81 EXPORT_SYMBOL(nvm_unregister_tgt_type
);
83 void *nvm_dev_dma_alloc(struct nvm_dev
*dev
, gfp_t mem_flags
,
84 dma_addr_t
*dma_handler
)
86 return dev
->ops
->dev_dma_alloc(dev
, dev
->dma_pool
, mem_flags
,
89 EXPORT_SYMBOL(nvm_dev_dma_alloc
);
91 void nvm_dev_dma_free(struct nvm_dev
*dev
, void *addr
,
92 dma_addr_t dma_handler
)
94 dev
->ops
->dev_dma_free(dev
->dma_pool
, addr
, dma_handler
);
96 EXPORT_SYMBOL(nvm_dev_dma_free
);
98 static struct nvmm_type
*nvm_find_mgr_type(const char *name
)
100 struct nvmm_type
*mt
;
102 list_for_each_entry(mt
, &nvm_mgrs
, list
)
103 if (!strcmp(name
, mt
->name
))
109 static struct nvmm_type
*nvm_init_mgr(struct nvm_dev
*dev
)
111 struct nvmm_type
*mt
;
114 lockdep_assert_held(&nvm_lock
);
116 list_for_each_entry(mt
, &nvm_mgrs
, list
) {
117 if (strncmp(dev
->sb
.mmtype
, mt
->name
, NVM_MMTYPE_LEN
))
120 ret
= mt
->register_mgr(dev
);
122 pr_err("nvm: media mgr failed to init (%d) on dev %s\n",
124 return NULL
; /* initialization failed */
132 int nvm_register_mgr(struct nvmm_type
*mt
)
137 down_write(&nvm_lock
);
138 if (nvm_find_mgr_type(mt
->name
)) {
142 list_add(&mt
->list
, &nvm_mgrs
);
145 /* try to register media mgr if any device have none configured */
146 list_for_each_entry(dev
, &nvm_devices
, devices
) {
150 dev
->mt
= nvm_init_mgr(dev
);
157 EXPORT_SYMBOL(nvm_register_mgr
);
159 void nvm_unregister_mgr(struct nvmm_type
*mt
)
164 down_write(&nvm_lock
);
168 EXPORT_SYMBOL(nvm_unregister_mgr
);
170 static struct nvm_dev
*nvm_find_nvm_dev(const char *name
)
174 list_for_each_entry(dev
, &nvm_devices
, devices
)
175 if (!strcmp(name
, dev
->name
))
181 struct nvm_block
*nvm_get_blk(struct nvm_dev
*dev
, struct nvm_lun
*lun
,
184 return dev
->mt
->get_blk(dev
, lun
, flags
);
186 EXPORT_SYMBOL(nvm_get_blk
);
188 /* Assumes that all valid pages have already been moved on release to bm */
189 void nvm_put_blk(struct nvm_dev
*dev
, struct nvm_block
*blk
)
191 return dev
->mt
->put_blk(dev
, blk
);
193 EXPORT_SYMBOL(nvm_put_blk
);
195 void nvm_mark_blk(struct nvm_dev
*dev
, struct ppa_addr ppa
, int type
)
197 return dev
->mt
->mark_blk(dev
, ppa
, type
);
199 EXPORT_SYMBOL(nvm_mark_blk
);
201 int nvm_submit_io(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
203 return dev
->mt
->submit_io(dev
, rqd
);
205 EXPORT_SYMBOL(nvm_submit_io
);
207 int nvm_erase_blk(struct nvm_dev
*dev
, struct nvm_block
*blk
)
209 return dev
->mt
->erase_blk(dev
, blk
, 0);
211 EXPORT_SYMBOL(nvm_erase_blk
);
213 void nvm_addr_to_generic_mode(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
217 if (rqd
->nr_ppas
> 1) {
218 for (i
= 0; i
< rqd
->nr_ppas
; i
++)
219 rqd
->ppa_list
[i
] = dev_to_generic_addr(dev
,
222 rqd
->ppa_addr
= dev_to_generic_addr(dev
, rqd
->ppa_addr
);
225 EXPORT_SYMBOL(nvm_addr_to_generic_mode
);
227 void nvm_generic_to_addr_mode(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
231 if (rqd
->nr_ppas
> 1) {
232 for (i
= 0; i
< rqd
->nr_ppas
; i
++)
233 rqd
->ppa_list
[i
] = generic_to_dev_addr(dev
,
236 rqd
->ppa_addr
= generic_to_dev_addr(dev
, rqd
->ppa_addr
);
239 EXPORT_SYMBOL(nvm_generic_to_addr_mode
);
241 int nvm_set_rqd_ppalist(struct nvm_dev
*dev
, struct nvm_rq
*rqd
,
242 const struct ppa_addr
*ppas
, int nr_ppas
, int vblk
)
244 int i
, plane_cnt
, pl_idx
;
247 if ((!vblk
|| dev
->plane_mode
== NVM_PLANE_SINGLE
) && nr_ppas
== 1) {
248 rqd
->nr_ppas
= nr_ppas
;
249 rqd
->ppa_addr
= ppas
[0];
254 rqd
->nr_ppas
= nr_ppas
;
255 rqd
->ppa_list
= nvm_dev_dma_alloc(dev
, GFP_KERNEL
, &rqd
->dma_ppa_list
);
256 if (!rqd
->ppa_list
) {
257 pr_err("nvm: failed to allocate dma memory\n");
262 for (i
= 0; i
< nr_ppas
; i
++)
263 rqd
->ppa_list
[i
] = ppas
[i
];
265 plane_cnt
= dev
->plane_mode
;
266 rqd
->nr_ppas
*= plane_cnt
;
268 for (i
= 0; i
< nr_ppas
; i
++) {
269 for (pl_idx
= 0; pl_idx
< plane_cnt
; pl_idx
++) {
272 rqd
->ppa_list
[(pl_idx
* nr_ppas
) + i
] = ppa
;
279 EXPORT_SYMBOL(nvm_set_rqd_ppalist
);
281 void nvm_free_rqd_ppalist(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
286 nvm_dev_dma_free(dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
288 EXPORT_SYMBOL(nvm_free_rqd_ppalist
);
290 int nvm_erase_ppa(struct nvm_dev
*dev
, struct ppa_addr
*ppas
, int nr_ppas
)
295 if (!dev
->ops
->erase_block
)
298 memset(&rqd
, 0, sizeof(struct nvm_rq
));
300 ret
= nvm_set_rqd_ppalist(dev
, &rqd
, ppas
, nr_ppas
, 1);
304 nvm_generic_to_addr_mode(dev
, &rqd
);
306 ret
= dev
->ops
->erase_block(dev
, &rqd
);
308 nvm_free_rqd_ppalist(dev
, &rqd
);
312 EXPORT_SYMBOL(nvm_erase_ppa
);
314 void nvm_end_io(struct nvm_rq
*rqd
, int error
)
319 EXPORT_SYMBOL(nvm_end_io
);
321 static void nvm_end_io_sync(struct nvm_rq
*rqd
)
323 struct completion
*waiting
= rqd
->wait
;
330 static int __nvm_submit_ppa(struct nvm_dev
*dev
, struct nvm_rq
*rqd
, int opcode
,
331 int flags
, void *buf
, int len
)
333 DECLARE_COMPLETION_ONSTACK(wait
);
336 unsigned long hang_check
;
338 bio
= bio_map_kern(dev
->q
, buf
, len
, GFP_KERNEL
);
339 if (IS_ERR_OR_NULL(bio
))
342 nvm_generic_to_addr_mode(dev
, rqd
);
345 rqd
->opcode
= opcode
;
349 rqd
->end_io
= nvm_end_io_sync
;
351 ret
= dev
->ops
->submit_io(dev
, rqd
);
357 /* Prevent hang_check timer from firing at us during very long I/O */
358 hang_check
= sysctl_hung_task_timeout_secs
;
360 while (!wait_for_completion_io_timeout(&wait
,
361 hang_check
* (HZ
/2)))
364 wait_for_completion_io(&wait
);
370 * nvm_submit_ppa_list - submit user-defined ppa list to device. The user must
371 * take to free ppa list if necessary.
373 * @ppa_list: user created ppa_list
374 * @nr_ppas: length of ppa_list
375 * @opcode: device opcode
376 * @flags: device flags
378 * @len: data buffer length
380 int nvm_submit_ppa_list(struct nvm_dev
*dev
, struct ppa_addr
*ppa_list
,
381 int nr_ppas
, int opcode
, int flags
, void *buf
, int len
)
385 if (dev
->ops
->max_phys_sect
< nr_ppas
)
388 memset(&rqd
, 0, sizeof(struct nvm_rq
));
390 rqd
.nr_ppas
= nr_ppas
;
392 rqd
.ppa_list
= ppa_list
;
394 rqd
.ppa_addr
= ppa_list
[0];
396 return __nvm_submit_ppa(dev
, &rqd
, opcode
, flags
, buf
, len
);
398 EXPORT_SYMBOL(nvm_submit_ppa_list
);
401 * nvm_submit_ppa - submit PPAs to device. PPAs will automatically be unfolded
402 * as single, dual, quad plane PPAs depending on device type.
404 * @ppa: user created ppa_list
405 * @nr_ppas: length of ppa_list
406 * @opcode: device opcode
407 * @flags: device flags
409 * @len: data buffer length
411 int nvm_submit_ppa(struct nvm_dev
*dev
, struct ppa_addr
*ppa
, int nr_ppas
,
412 int opcode
, int flags
, void *buf
, int len
)
417 memset(&rqd
, 0, sizeof(struct nvm_rq
));
418 ret
= nvm_set_rqd_ppalist(dev
, &rqd
, ppa
, nr_ppas
, 1);
422 ret
= __nvm_submit_ppa(dev
, &rqd
, opcode
, flags
, buf
, len
);
424 nvm_free_rqd_ppalist(dev
, &rqd
);
428 EXPORT_SYMBOL(nvm_submit_ppa
);
431 * folds a bad block list from its plane representation to its virtual
432 * block representation. The fold is done in place and reduced size is
435 * If any of the planes status are bad or grown bad block, the virtual block
436 * is marked bad. If not bad, the first plane state acts as the block state.
438 int nvm_bb_tbl_fold(struct nvm_dev
*dev
, u8
*blks
, int nr_blks
)
440 int blk
, offset
, pl
, blktype
;
442 if (nr_blks
!= dev
->blks_per_lun
* dev
->plane_mode
)
445 for (blk
= 0; blk
< dev
->blks_per_lun
; blk
++) {
446 offset
= blk
* dev
->plane_mode
;
447 blktype
= blks
[offset
];
449 /* Bad blocks on any planes take precedence over other types */
450 for (pl
= 0; pl
< dev
->plane_mode
; pl
++) {
451 if (blks
[offset
+ pl
] &
452 (NVM_BLK_T_BAD
|NVM_BLK_T_GRWN_BAD
)) {
453 blktype
= blks
[offset
+ pl
];
461 return dev
->blks_per_lun
;
463 EXPORT_SYMBOL(nvm_bb_tbl_fold
);
465 int nvm_get_bb_tbl(struct nvm_dev
*dev
, struct ppa_addr ppa
, u8
*blks
)
467 ppa
= generic_to_dev_addr(dev
, ppa
);
469 return dev
->ops
->get_bb_tbl(dev
, ppa
, blks
);
471 EXPORT_SYMBOL(nvm_get_bb_tbl
);
473 static int nvm_init_slc_tbl(struct nvm_dev
*dev
, struct nvm_id_group
*grp
)
477 dev
->lps_per_blk
= dev
->pgs_per_blk
;
478 dev
->lptbl
= kcalloc(dev
->lps_per_blk
, sizeof(int), GFP_KERNEL
);
482 /* Just a linear array */
483 for (i
= 0; i
< dev
->lps_per_blk
; i
++)
489 static int nvm_init_mlc_tbl(struct nvm_dev
*dev
, struct nvm_id_group
*grp
)
492 struct nvm_id_lp_mlc
*mlc
= &grp
->lptbl
.mlc
;
497 dev
->lps_per_blk
= mlc
->num_pairs
;
498 dev
->lptbl
= kcalloc(dev
->lps_per_blk
, sizeof(int), GFP_KERNEL
);
502 /* The lower page table encoding consists of a list of bytes, where each
503 * has a lower and an upper half. The first half byte maintains the
504 * increment value and every value after is an offset added to the
505 * previous incrementation value
507 dev
->lptbl
[0] = mlc
->pairs
[0] & 0xF;
508 for (i
= 1; i
< dev
->lps_per_blk
; i
++) {
509 p
= mlc
->pairs
[i
>> 1];
510 if (i
& 0x1) /* upper */
511 dev
->lptbl
[i
] = dev
->lptbl
[i
- 1] + ((p
& 0xF0) >> 4);
513 dev
->lptbl
[i
] = dev
->lptbl
[i
- 1] + (p
& 0xF);
519 static int nvm_core_init(struct nvm_dev
*dev
)
521 struct nvm_id
*id
= &dev
->identity
;
522 struct nvm_id_group
*grp
= &id
->groups
[0];
526 dev
->nr_chnls
= grp
->num_ch
;
527 dev
->luns_per_chnl
= grp
->num_lun
;
528 dev
->pgs_per_blk
= grp
->num_pg
;
529 dev
->blks_per_lun
= grp
->num_blk
;
530 dev
->nr_planes
= grp
->num_pln
;
531 dev
->fpg_size
= grp
->fpg_sz
;
532 dev
->pfpg_size
= grp
->fpg_sz
* grp
->num_pln
;
533 dev
->sec_size
= grp
->csecs
;
534 dev
->oob_size
= grp
->sos
;
535 dev
->sec_per_pg
= grp
->fpg_sz
/ grp
->csecs
;
536 dev
->mccap
= grp
->mccap
;
537 memcpy(&dev
->ppaf
, &id
->ppaf
, sizeof(struct nvm_addr_format
));
539 dev
->plane_mode
= NVM_PLANE_SINGLE
;
540 dev
->max_rq_size
= dev
->ops
->max_phys_sect
* dev
->sec_size
;
542 if (grp
->mpos
& 0x020202)
543 dev
->plane_mode
= NVM_PLANE_DOUBLE
;
544 if (grp
->mpos
& 0x040404)
545 dev
->plane_mode
= NVM_PLANE_QUAD
;
547 if (grp
->mtype
!= 0) {
548 pr_err("nvm: memory type not supported\n");
552 /* calculated values */
553 dev
->sec_per_pl
= dev
->sec_per_pg
* dev
->nr_planes
;
554 dev
->sec_per_blk
= dev
->sec_per_pl
* dev
->pgs_per_blk
;
555 dev
->sec_per_lun
= dev
->sec_per_blk
* dev
->blks_per_lun
;
556 dev
->nr_luns
= dev
->luns_per_chnl
* dev
->nr_chnls
;
558 dev
->total_secs
= dev
->nr_luns
* dev
->sec_per_lun
;
559 dev
->lun_map
= kcalloc(BITS_TO_LONGS(dev
->nr_luns
),
560 sizeof(unsigned long), GFP_KERNEL
);
564 switch (grp
->fmtype
) {
565 case NVM_ID_FMTYPE_SLC
:
566 if (nvm_init_slc_tbl(dev
, grp
)) {
571 case NVM_ID_FMTYPE_MLC
:
572 if (nvm_init_mlc_tbl(dev
, grp
)) {
578 pr_err("nvm: flash type not supported\n");
583 mutex_init(&dev
->mlock
);
584 spin_lock_init(&dev
->lock
);
586 blk_queue_logical_block_size(dev
->q
, dev
->sec_size
);
594 static void nvm_free_mgr(struct nvm_dev
*dev
)
599 dev
->mt
->unregister_mgr(dev
);
603 void nvm_free(struct nvm_dev
*dev
)
611 dev
->ops
->destroy_dma_pool(dev
->dma_pool
);
618 static int nvm_init(struct nvm_dev
*dev
)
622 if (!dev
->q
|| !dev
->ops
)
625 if (dev
->ops
->identity(dev
, &dev
->identity
)) {
626 pr_err("nvm: device could not be identified\n");
630 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
631 dev
->identity
.ver_id
, dev
->identity
.vmnt
,
632 dev
->identity
.cgrps
);
634 if (dev
->identity
.ver_id
!= 1) {
635 pr_err("nvm: device not supported by kernel.");
639 if (dev
->identity
.cgrps
!= 1) {
640 pr_err("nvm: only one group configuration supported.");
644 ret
= nvm_core_init(dev
);
646 pr_err("nvm: could not initialize core structures.\n");
650 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
651 dev
->name
, dev
->sec_per_pg
, dev
->nr_planes
,
652 dev
->pgs_per_blk
, dev
->blks_per_lun
, dev
->nr_luns
,
656 pr_err("nvm: failed to initialize nvm\n");
660 static void nvm_exit(struct nvm_dev
*dev
)
662 nvm_sysfs_unregister_dev(dev
);
665 struct nvm_dev
*nvm_alloc_dev(int node
)
667 return kzalloc_node(sizeof(struct nvm_dev
), GFP_KERNEL
, node
);
669 EXPORT_SYMBOL(nvm_alloc_dev
);
671 int nvm_register(struct nvm_dev
*dev
)
679 if (dev
->ops
->max_phys_sect
> 256) {
680 pr_info("nvm: max sectors supported is 256.\n");
685 if (dev
->ops
->max_phys_sect
> 1) {
686 dev
->dma_pool
= dev
->ops
->create_dma_pool(dev
, "ppalist");
687 if (!dev
->dma_pool
) {
688 pr_err("nvm: could not create dma pool\n");
694 ret
= nvm_sysfs_register_dev(dev
);
698 if (dev
->identity
.cap
& NVM_ID_DCAP_BBLKMGMT
) {
699 ret
= nvm_get_sysblock(dev
, &dev
->sb
);
701 pr_err("nvm: device not initialized.\n");
703 pr_err("nvm: err (%d) on device initialization\n", ret
);
706 /* register device with a supported media manager */
707 down_write(&nvm_lock
);
709 dev
->mt
= nvm_init_mgr(dev
);
710 list_add(&dev
->devices
, &nvm_devices
);
715 dev
->ops
->destroy_dma_pool(dev
->dma_pool
);
720 EXPORT_SYMBOL(nvm_register
);
722 void nvm_unregister(struct nvm_dev
*dev
)
724 down_write(&nvm_lock
);
725 list_del(&dev
->devices
);
730 EXPORT_SYMBOL(nvm_unregister
);
732 static int __nvm_configure_create(struct nvm_ioctl_create
*create
)
735 struct nvm_ioctl_create_simple
*s
;
737 down_write(&nvm_lock
);
738 dev
= nvm_find_nvm_dev(create
->dev
);
742 pr_err("nvm: device not found\n");
747 pr_info("nvm: device has no media manager registered.\n");
751 if (create
->conf
.type
!= NVM_CONFIG_TYPE_SIMPLE
) {
752 pr_err("nvm: config type not valid\n");
757 if (s
->lun_begin
> s
->lun_end
|| s
->lun_end
> dev
->nr_luns
) {
758 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
759 s
->lun_begin
, s
->lun_end
, dev
->nr_luns
);
763 return dev
->mt
->create_tgt(dev
, create
);
766 #ifdef CONFIG_NVM_DEBUG
767 static int nvm_configure_show(const char *val
)
770 char opcode
, devname
[DISK_NAME_LEN
];
773 ret
= sscanf(val
, "%c %32s", &opcode
, devname
);
775 pr_err("nvm: invalid command. Use \"opcode devicename\".\n");
779 down_write(&nvm_lock
);
780 dev
= nvm_find_nvm_dev(devname
);
783 pr_err("nvm: device not found\n");
790 dev
->mt
->lun_info_print(dev
);
795 static int nvm_configure_remove(const char *val
)
797 struct nvm_ioctl_remove remove
;
802 ret
= sscanf(val
, "%c %256s", &opcode
, remove
.tgtname
);
804 pr_err("nvm: invalid command. Use \"d targetname\".\n");
810 list_for_each_entry(dev
, &nvm_devices
, devices
) {
811 ret
= dev
->mt
->remove_tgt(dev
, &remove
);
819 static int nvm_configure_create(const char *val
)
821 struct nvm_ioctl_create create
;
823 int lun_begin
, lun_end
, ret
;
825 ret
= sscanf(val
, "%c %256s %256s %48s %u:%u", &opcode
, create
.dev
,
826 create
.tgtname
, create
.tgttype
,
827 &lun_begin
, &lun_end
);
829 pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n");
834 create
.conf
.type
= NVM_CONFIG_TYPE_SIMPLE
;
835 create
.conf
.s
.lun_begin
= lun_begin
;
836 create
.conf
.s
.lun_end
= lun_end
;
838 return __nvm_configure_create(&create
);
842 /* Exposes administrative interface through /sys/module/lnvm/configure_by_str */
843 static int nvm_configure_by_str_event(const char *val
,
844 const struct kernel_param
*kp
)
849 ret
= sscanf(val
, "%c", &opcode
);
851 pr_err("nvm: string must have the format of \"cmd ...\"\n");
857 return nvm_configure_create(val
);
859 return nvm_configure_remove(val
);
861 return nvm_configure_show(val
);
863 pr_err("nvm: invalid command\n");
870 static int nvm_configure_get(char *buf
, const struct kernel_param
*kp
)
875 sz
= sprintf(buf
, "available devices:\n");
876 down_write(&nvm_lock
);
877 list_for_each_entry(dev
, &nvm_devices
, devices
) {
878 if (sz
> 4095 - DISK_NAME_LEN
- 2)
880 sz
+= sprintf(buf
+ sz
, " %32s\n", dev
->name
);
887 static const struct kernel_param_ops nvm_configure_by_str_event_param_ops
= {
888 .set
= nvm_configure_by_str_event
,
889 .get
= nvm_configure_get
,
892 #undef MODULE_PARAM_PREFIX
893 #define MODULE_PARAM_PREFIX "lnvm."
895 module_param_cb(configure_debug
, &nvm_configure_by_str_event_param_ops
, NULL
,
898 #endif /* CONFIG_NVM_DEBUG */
900 static long nvm_ioctl_info(struct file
*file
, void __user
*arg
)
902 struct nvm_ioctl_info
*info
;
903 struct nvm_tgt_type
*tt
;
906 if (!capable(CAP_SYS_ADMIN
))
909 info
= memdup_user(arg
, sizeof(struct nvm_ioctl_info
));
913 info
->version
[0] = NVM_VERSION_MAJOR
;
914 info
->version
[1] = NVM_VERSION_MINOR
;
915 info
->version
[2] = NVM_VERSION_PATCH
;
917 down_write(&nvm_lock
);
918 list_for_each_entry(tt
, &nvm_tgt_types
, list
) {
919 struct nvm_ioctl_info_tgt
*tgt
= &info
->tgts
[tgt_iter
];
921 tgt
->version
[0] = tt
->version
[0];
922 tgt
->version
[1] = tt
->version
[1];
923 tgt
->version
[2] = tt
->version
[2];
924 strncpy(tgt
->tgtname
, tt
->name
, NVM_TTYPE_NAME_MAX
);
929 info
->tgtsize
= tgt_iter
;
932 if (copy_to_user(arg
, info
, sizeof(struct nvm_ioctl_info
))) {
941 static long nvm_ioctl_get_devices(struct file
*file
, void __user
*arg
)
943 struct nvm_ioctl_get_devices
*devices
;
947 if (!capable(CAP_SYS_ADMIN
))
950 devices
= kzalloc(sizeof(struct nvm_ioctl_get_devices
), GFP_KERNEL
);
954 down_write(&nvm_lock
);
955 list_for_each_entry(dev
, &nvm_devices
, devices
) {
956 struct nvm_ioctl_device_info
*info
= &devices
->info
[i
];
958 sprintf(info
->devname
, "%s", dev
->name
);
960 info
->bmversion
[0] = dev
->mt
->version
[0];
961 info
->bmversion
[1] = dev
->mt
->version
[1];
962 info
->bmversion
[2] = dev
->mt
->version
[2];
963 sprintf(info
->bmname
, "%s", dev
->mt
->name
);
965 sprintf(info
->bmname
, "none");
970 pr_err("nvm: max 31 devices can be reported.\n");
976 devices
->nr_devices
= i
;
978 if (copy_to_user(arg
, devices
,
979 sizeof(struct nvm_ioctl_get_devices
))) {
988 static long nvm_ioctl_dev_create(struct file
*file
, void __user
*arg
)
990 struct nvm_ioctl_create create
;
992 if (!capable(CAP_SYS_ADMIN
))
995 if (copy_from_user(&create
, arg
, sizeof(struct nvm_ioctl_create
)))
998 create
.dev
[DISK_NAME_LEN
- 1] = '\0';
999 create
.tgttype
[NVM_TTYPE_NAME_MAX
- 1] = '\0';
1000 create
.tgtname
[DISK_NAME_LEN
- 1] = '\0';
1002 if (create
.flags
!= 0) {
1003 pr_err("nvm: no flags supported\n");
1007 return __nvm_configure_create(&create
);
1010 static long nvm_ioctl_dev_remove(struct file
*file
, void __user
*arg
)
1012 struct nvm_ioctl_remove remove
;
1013 struct nvm_dev
*dev
;
1016 if (!capable(CAP_SYS_ADMIN
))
1019 if (copy_from_user(&remove
, arg
, sizeof(struct nvm_ioctl_remove
)))
1022 remove
.tgtname
[DISK_NAME_LEN
- 1] = '\0';
1024 if (remove
.flags
!= 0) {
1025 pr_err("nvm: no flags supported\n");
1029 list_for_each_entry(dev
, &nvm_devices
, devices
) {
1030 ret
= dev
->mt
->remove_tgt(dev
, &remove
);
1038 static void nvm_setup_nvm_sb_info(struct nvm_sb_info
*info
)
1041 info
->erase_cnt
= 0;
1045 static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init
*init
)
1047 struct nvm_dev
*dev
;
1048 struct nvm_sb_info info
;
1051 down_write(&nvm_lock
);
1052 dev
= nvm_find_nvm_dev(init
->dev
);
1053 up_write(&nvm_lock
);
1055 pr_err("nvm: device not found\n");
1059 nvm_setup_nvm_sb_info(&info
);
1061 strncpy(info
.mmtype
, init
->mmtype
, NVM_MMTYPE_LEN
);
1062 info
.fs_ppa
.ppa
= -1;
1064 if (dev
->identity
.cap
& NVM_ID_DCAP_BBLKMGMT
) {
1065 ret
= nvm_init_sysblock(dev
, &info
);
1070 memcpy(&dev
->sb
, &info
, sizeof(struct nvm_sb_info
));
1072 down_write(&nvm_lock
);
1073 dev
->mt
= nvm_init_mgr(dev
);
1074 up_write(&nvm_lock
);
1079 static long nvm_ioctl_dev_init(struct file
*file
, void __user
*arg
)
1081 struct nvm_ioctl_dev_init init
;
1083 if (!capable(CAP_SYS_ADMIN
))
1086 if (copy_from_user(&init
, arg
, sizeof(struct nvm_ioctl_dev_init
)))
1089 if (init
.flags
!= 0) {
1090 pr_err("nvm: no flags supported\n");
1094 init
.dev
[DISK_NAME_LEN
- 1] = '\0';
1096 return __nvm_ioctl_dev_init(&init
);
1099 static long nvm_ioctl_dev_factory(struct file
*file
, void __user
*arg
)
1101 struct nvm_ioctl_dev_factory fact
;
1102 struct nvm_dev
*dev
;
1104 if (!capable(CAP_SYS_ADMIN
))
1107 if (copy_from_user(&fact
, arg
, sizeof(struct nvm_ioctl_dev_factory
)))
1110 fact
.dev
[DISK_NAME_LEN
- 1] = '\0';
1112 if (fact
.flags
& ~(NVM_FACTORY_NR_BITS
- 1))
1115 down_write(&nvm_lock
);
1116 dev
= nvm_find_nvm_dev(fact
.dev
);
1117 up_write(&nvm_lock
);
1119 pr_err("nvm: device not found\n");
1125 if (dev
->identity
.cap
& NVM_ID_DCAP_BBLKMGMT
)
1126 return nvm_dev_factory(dev
, fact
.flags
);
1131 static long nvm_ctl_ioctl(struct file
*file
, uint cmd
, unsigned long arg
)
1133 void __user
*argp
= (void __user
*)arg
;
1137 return nvm_ioctl_info(file
, argp
);
1138 case NVM_GET_DEVICES
:
1139 return nvm_ioctl_get_devices(file
, argp
);
1140 case NVM_DEV_CREATE
:
1141 return nvm_ioctl_dev_create(file
, argp
);
1142 case NVM_DEV_REMOVE
:
1143 return nvm_ioctl_dev_remove(file
, argp
);
1145 return nvm_ioctl_dev_init(file
, argp
);
1146 case NVM_DEV_FACTORY
:
1147 return nvm_ioctl_dev_factory(file
, argp
);
1152 static const struct file_operations _ctl_fops
= {
1153 .open
= nonseekable_open
,
1154 .unlocked_ioctl
= nvm_ctl_ioctl
,
1155 .owner
= THIS_MODULE
,
1156 .llseek
= noop_llseek
,
1159 static struct miscdevice _nvm_misc
= {
1160 .minor
= MISC_DYNAMIC_MINOR
,
1162 .nodename
= "lightnvm/control",
1165 module_misc_device(_nvm_misc
);
1167 MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR
);
1169 MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
1170 MODULE_LICENSE("GPL v2");
1171 MODULE_VERSION("0.1");