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 <uapi/linux/lightnvm.h>
32 static LIST_HEAD(nvm_targets
);
33 static LIST_HEAD(nvm_mgrs
);
34 static LIST_HEAD(nvm_devices
);
35 static DECLARE_RWSEM(nvm_lock
);
37 static struct nvm_tgt_type
*nvm_find_target_type(const char *name
)
39 struct nvm_tgt_type
*tt
;
41 list_for_each_entry(tt
, &nvm_targets
, list
)
42 if (!strcmp(name
, tt
->name
))
48 int nvm_register_target(struct nvm_tgt_type
*tt
)
52 down_write(&nvm_lock
);
53 if (nvm_find_target_type(tt
->name
))
56 list_add(&tt
->list
, &nvm_targets
);
61 EXPORT_SYMBOL(nvm_register_target
);
63 void nvm_unregister_target(struct nvm_tgt_type
*tt
)
68 down_write(&nvm_lock
);
72 EXPORT_SYMBOL(nvm_unregister_target
);
74 void *nvm_dev_dma_alloc(struct nvm_dev
*dev
, gfp_t mem_flags
,
75 dma_addr_t
*dma_handler
)
77 return dev
->ops
->dev_dma_alloc(dev
, dev
->ppalist_pool
, mem_flags
,
80 EXPORT_SYMBOL(nvm_dev_dma_alloc
);
82 void nvm_dev_dma_free(struct nvm_dev
*dev
, void *ppa_list
,
83 dma_addr_t dma_handler
)
85 dev
->ops
->dev_dma_free(dev
->ppalist_pool
, ppa_list
, dma_handler
);
87 EXPORT_SYMBOL(nvm_dev_dma_free
);
89 static struct nvmm_type
*nvm_find_mgr_type(const char *name
)
93 list_for_each_entry(mt
, &nvm_mgrs
, list
)
94 if (!strcmp(name
, mt
->name
))
100 struct nvmm_type
*nvm_init_mgr(struct nvm_dev
*dev
)
102 struct nvmm_type
*mt
;
105 lockdep_assert_held(&nvm_lock
);
107 list_for_each_entry(mt
, &nvm_mgrs
, list
) {
108 ret
= mt
->register_mgr(dev
);
110 pr_err("nvm: media mgr failed to init (%d) on dev %s\n",
112 return NULL
; /* initialization failed */
120 int nvm_register_mgr(struct nvmm_type
*mt
)
125 down_write(&nvm_lock
);
126 if (nvm_find_mgr_type(mt
->name
)) {
130 list_add(&mt
->list
, &nvm_mgrs
);
133 /* try to register media mgr if any device have none configured */
134 list_for_each_entry(dev
, &nvm_devices
, devices
) {
138 dev
->mt
= nvm_init_mgr(dev
);
145 EXPORT_SYMBOL(nvm_register_mgr
);
147 void nvm_unregister_mgr(struct nvmm_type
*mt
)
152 down_write(&nvm_lock
);
156 EXPORT_SYMBOL(nvm_unregister_mgr
);
158 static struct nvm_dev
*nvm_find_nvm_dev(const char *name
)
162 list_for_each_entry(dev
, &nvm_devices
, devices
)
163 if (!strcmp(name
, dev
->name
))
169 struct nvm_block
*nvm_get_blk(struct nvm_dev
*dev
, struct nvm_lun
*lun
,
172 return dev
->mt
->get_blk(dev
, lun
, flags
);
174 EXPORT_SYMBOL(nvm_get_blk
);
176 /* Assumes that all valid pages have already been moved on release to bm */
177 void nvm_put_blk(struct nvm_dev
*dev
, struct nvm_block
*blk
)
179 return dev
->mt
->put_blk(dev
, blk
);
181 EXPORT_SYMBOL(nvm_put_blk
);
183 int nvm_submit_io(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
185 return dev
->mt
->submit_io(dev
, rqd
);
187 EXPORT_SYMBOL(nvm_submit_io
);
189 int nvm_erase_blk(struct nvm_dev
*dev
, struct nvm_block
*blk
)
191 return dev
->mt
->erase_blk(dev
, blk
, 0);
193 EXPORT_SYMBOL(nvm_erase_blk
);
195 static int nvm_core_init(struct nvm_dev
*dev
)
197 struct nvm_id
*id
= &dev
->identity
;
198 struct nvm_id_group
*grp
= &id
->groups
[0];
201 dev
->nr_chnls
= grp
->num_ch
;
202 dev
->luns_per_chnl
= grp
->num_lun
;
203 dev
->pgs_per_blk
= grp
->num_pg
;
204 dev
->blks_per_lun
= grp
->num_blk
;
205 dev
->nr_planes
= grp
->num_pln
;
206 dev
->sec_size
= grp
->csecs
;
207 dev
->oob_size
= grp
->sos
;
208 dev
->sec_per_pg
= grp
->fpg_sz
/ grp
->csecs
;
209 memcpy(&dev
->ppaf
, &id
->ppaf
, sizeof(struct nvm_addr_format
));
211 dev
->plane_mode
= NVM_PLANE_SINGLE
;
212 dev
->max_rq_size
= dev
->ops
->max_phys_sect
* dev
->sec_size
;
214 if (grp
->mtype
!= 0) {
215 pr_err("nvm: memory type not supported\n");
219 if (grp
->fmtype
!= 0 && grp
->fmtype
!= 1) {
220 pr_err("nvm: flash type not supported\n");
224 if (grp
->mpos
& 0x020202)
225 dev
->plane_mode
= NVM_PLANE_DOUBLE
;
226 if (grp
->mpos
& 0x040404)
227 dev
->plane_mode
= NVM_PLANE_QUAD
;
229 /* calculated values */
230 dev
->sec_per_pl
= dev
->sec_per_pg
* dev
->nr_planes
;
231 dev
->sec_per_blk
= dev
->sec_per_pl
* dev
->pgs_per_blk
;
232 dev
->sec_per_lun
= dev
->sec_per_blk
* dev
->blks_per_lun
;
233 dev
->nr_luns
= dev
->luns_per_chnl
* dev
->nr_chnls
;
235 dev
->total_blocks
= dev
->nr_planes
*
239 dev
->total_pages
= dev
->total_blocks
* dev
->pgs_per_blk
;
240 INIT_LIST_HEAD(&dev
->online_targets
);
245 static void nvm_free(struct nvm_dev
*dev
)
251 dev
->mt
->unregister_mgr(dev
);
254 static int nvm_init(struct nvm_dev
*dev
)
258 if (!dev
->q
|| !dev
->ops
)
261 if (dev
->ops
->identity(dev
, &dev
->identity
)) {
262 pr_err("nvm: device could not be identified\n");
266 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
267 dev
->identity
.ver_id
, dev
->identity
.vmnt
,
268 dev
->identity
.cgrps
);
270 if (dev
->identity
.ver_id
!= 1) {
271 pr_err("nvm: device not supported by kernel.");
275 if (dev
->identity
.cgrps
!= 1) {
276 pr_err("nvm: only one group configuration supported.");
280 ret
= nvm_core_init(dev
);
282 pr_err("nvm: could not initialize core structures.\n");
286 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
287 dev
->name
, dev
->sec_per_pg
, dev
->nr_planes
,
288 dev
->pgs_per_blk
, dev
->blks_per_lun
, dev
->nr_luns
,
292 pr_err("nvm: failed to initialize nvm\n");
296 static void nvm_exit(struct nvm_dev
*dev
)
298 if (dev
->ppalist_pool
)
299 dev
->ops
->destroy_dma_pool(dev
->ppalist_pool
);
302 pr_info("nvm: successfully unloaded\n");
305 int nvm_register(struct request_queue
*q
, char *disk_name
,
306 struct nvm_dev_ops
*ops
)
314 dev
= kzalloc(sizeof(struct nvm_dev
), GFP_KERNEL
);
320 strncpy(dev
->name
, disk_name
, DISK_NAME_LEN
);
326 if (dev
->ops
->max_phys_sect
> 256) {
327 pr_info("nvm: max sectors supported is 256.\n");
332 if (dev
->ops
->max_phys_sect
> 1) {
333 dev
->ppalist_pool
= dev
->ops
->create_dma_pool(dev
, "ppalist");
334 if (!dev
->ppalist_pool
) {
335 pr_err("nvm: could not create ppa pool\n");
341 /* register device with a supported media manager */
342 down_write(&nvm_lock
);
343 dev
->mt
= nvm_init_mgr(dev
);
344 list_add(&dev
->devices
, &nvm_devices
);
352 EXPORT_SYMBOL(nvm_register
);
354 void nvm_unregister(char *disk_name
)
358 down_write(&nvm_lock
);
359 dev
= nvm_find_nvm_dev(disk_name
);
361 pr_err("nvm: could not find device %s to unregister\n",
367 list_del(&dev
->devices
);
373 EXPORT_SYMBOL(nvm_unregister
);
375 static const struct block_device_operations nvm_fops
= {
376 .owner
= THIS_MODULE
,
379 static int nvm_create_target(struct nvm_dev
*dev
,
380 struct nvm_ioctl_create
*create
)
382 struct nvm_ioctl_create_simple
*s
= &create
->conf
.s
;
383 struct request_queue
*tqueue
;
384 struct gendisk
*tdisk
;
385 struct nvm_tgt_type
*tt
;
386 struct nvm_target
*t
;
390 pr_info("nvm: device has no media manager registered.\n");
394 down_write(&nvm_lock
);
395 tt
= nvm_find_target_type(create
->tgttype
);
397 pr_err("nvm: target type %s not found\n", create
->tgttype
);
402 list_for_each_entry(t
, &dev
->online_targets
, list
) {
403 if (!strcmp(create
->tgtname
, t
->disk
->disk_name
)) {
404 pr_err("nvm: target name already exists.\n");
411 t
= kmalloc(sizeof(struct nvm_target
), GFP_KERNEL
);
415 tqueue
= blk_alloc_queue_node(GFP_KERNEL
, dev
->q
->node
);
418 blk_queue_make_request(tqueue
, tt
->make_rq
);
420 tdisk
= alloc_disk(0);
424 sprintf(tdisk
->disk_name
, "%s", create
->tgtname
);
425 tdisk
->flags
= GENHD_FL_EXT_DEVT
;
427 tdisk
->first_minor
= 0;
428 tdisk
->fops
= &nvm_fops
;
429 tdisk
->queue
= tqueue
;
431 targetdata
= tt
->init(dev
, tdisk
, s
->lun_begin
, s
->lun_end
);
432 if (IS_ERR(targetdata
))
435 tdisk
->private_data
= targetdata
;
436 tqueue
->queuedata
= targetdata
;
438 blk_queue_max_hw_sectors(tqueue
, 8 * dev
->ops
->max_phys_sect
);
440 set_capacity(tdisk
, tt
->capacity(targetdata
));
446 down_write(&nvm_lock
);
447 list_add_tail(&t
->list
, &dev
->online_targets
);
454 blk_cleanup_queue(tqueue
);
460 static void nvm_remove_target(struct nvm_target
*t
)
462 struct nvm_tgt_type
*tt
= t
->type
;
463 struct gendisk
*tdisk
= t
->disk
;
464 struct request_queue
*q
= tdisk
->queue
;
466 lockdep_assert_held(&nvm_lock
);
469 blk_cleanup_queue(q
);
472 tt
->exit(tdisk
->private_data
);
480 static int __nvm_configure_create(struct nvm_ioctl_create
*create
)
483 struct nvm_ioctl_create_simple
*s
;
485 down_write(&nvm_lock
);
486 dev
= nvm_find_nvm_dev(create
->dev
);
489 pr_err("nvm: device not found\n");
493 if (create
->conf
.type
!= NVM_CONFIG_TYPE_SIMPLE
) {
494 pr_err("nvm: config type not valid\n");
499 if (s
->lun_begin
> s
->lun_end
|| s
->lun_end
> dev
->nr_luns
) {
500 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
501 s
->lun_begin
, s
->lun_end
, dev
->nr_luns
);
505 return nvm_create_target(dev
, create
);
508 static int __nvm_configure_remove(struct nvm_ioctl_remove
*remove
)
510 struct nvm_target
*t
= NULL
;
514 down_write(&nvm_lock
);
515 list_for_each_entry(dev
, &nvm_devices
, devices
)
516 list_for_each_entry(t
, &dev
->online_targets
, list
) {
517 if (!strcmp(remove
->tgtname
, t
->disk
->disk_name
)) {
518 nvm_remove_target(t
);
526 pr_err("nvm: target \"%s\" doesn't exist.\n", remove
->tgtname
);
533 #ifdef CONFIG_NVM_DEBUG
534 static int nvm_configure_show(const char *val
)
537 char opcode
, devname
[DISK_NAME_LEN
];
540 ret
= sscanf(val
, "%c %32s", &opcode
, devname
);
542 pr_err("nvm: invalid command. Use \"opcode devicename\".\n");
546 down_write(&nvm_lock
);
547 dev
= nvm_find_nvm_dev(devname
);
550 pr_err("nvm: device not found\n");
557 dev
->mt
->lun_info_print(dev
);
562 static int nvm_configure_remove(const char *val
)
564 struct nvm_ioctl_remove remove
;
568 ret
= sscanf(val
, "%c %256s", &opcode
, remove
.tgtname
);
570 pr_err("nvm: invalid command. Use \"d targetname\".\n");
576 return __nvm_configure_remove(&remove
);
579 static int nvm_configure_create(const char *val
)
581 struct nvm_ioctl_create create
;
583 int lun_begin
, lun_end
, ret
;
585 ret
= sscanf(val
, "%c %256s %256s %48s %u:%u", &opcode
, create
.dev
,
586 create
.tgtname
, create
.tgttype
,
587 &lun_begin
, &lun_end
);
589 pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n");
594 create
.conf
.type
= NVM_CONFIG_TYPE_SIMPLE
;
595 create
.conf
.s
.lun_begin
= lun_begin
;
596 create
.conf
.s
.lun_end
= lun_end
;
598 return __nvm_configure_create(&create
);
602 /* Exposes administrative interface through /sys/module/lnvm/configure_by_str */
603 static int nvm_configure_by_str_event(const char *val
,
604 const struct kernel_param
*kp
)
609 ret
= sscanf(val
, "%c", &opcode
);
611 pr_err("nvm: string must have the format of \"cmd ...\"\n");
617 return nvm_configure_create(val
);
619 return nvm_configure_remove(val
);
621 return nvm_configure_show(val
);
623 pr_err("nvm: invalid command\n");
630 static int nvm_configure_get(char *buf
, const struct kernel_param
*kp
)
633 char *buf_start
= buf
;
636 buf
+= sprintf(buf
, "available devices:\n");
637 down_write(&nvm_lock
);
638 list_for_each_entry(dev
, &nvm_devices
, devices
) {
639 if (sz
> 4095 - DISK_NAME_LEN
)
641 buf
+= sprintf(buf
, " %32s\n", dev
->name
);
645 return buf
- buf_start
- 1;
648 static const struct kernel_param_ops nvm_configure_by_str_event_param_ops
= {
649 .set
= nvm_configure_by_str_event
,
650 .get
= nvm_configure_get
,
653 #undef MODULE_PARAM_PREFIX
654 #define MODULE_PARAM_PREFIX "lnvm."
656 module_param_cb(configure_debug
, &nvm_configure_by_str_event_param_ops
, NULL
,
659 #endif /* CONFIG_NVM_DEBUG */
661 static long nvm_ioctl_info(struct file
*file
, void __user
*arg
)
663 struct nvm_ioctl_info
*info
;
664 struct nvm_tgt_type
*tt
;
667 if (!capable(CAP_SYS_ADMIN
))
670 info
= memdup_user(arg
, sizeof(struct nvm_ioctl_info
));
674 info
->version
[0] = NVM_VERSION_MAJOR
;
675 info
->version
[1] = NVM_VERSION_MINOR
;
676 info
->version
[2] = NVM_VERSION_PATCH
;
678 down_write(&nvm_lock
);
679 list_for_each_entry(tt
, &nvm_targets
, list
) {
680 struct nvm_ioctl_info_tgt
*tgt
= &info
->tgts
[tgt_iter
];
682 tgt
->version
[0] = tt
->version
[0];
683 tgt
->version
[1] = tt
->version
[1];
684 tgt
->version
[2] = tt
->version
[2];
685 strncpy(tgt
->tgtname
, tt
->name
, NVM_TTYPE_NAME_MAX
);
690 info
->tgtsize
= tgt_iter
;
693 if (copy_to_user(arg
, info
, sizeof(struct nvm_ioctl_info
))) {
702 static long nvm_ioctl_get_devices(struct file
*file
, void __user
*arg
)
704 struct nvm_ioctl_get_devices
*devices
;
708 if (!capable(CAP_SYS_ADMIN
))
711 devices
= kzalloc(sizeof(struct nvm_ioctl_get_devices
), GFP_KERNEL
);
715 down_write(&nvm_lock
);
716 list_for_each_entry(dev
, &nvm_devices
, devices
) {
717 struct nvm_ioctl_device_info
*info
= &devices
->info
[i
];
719 sprintf(info
->devname
, "%s", dev
->name
);
721 info
->bmversion
[0] = dev
->mt
->version
[0];
722 info
->bmversion
[1] = dev
->mt
->version
[1];
723 info
->bmversion
[2] = dev
->mt
->version
[2];
724 sprintf(info
->bmname
, "%s", dev
->mt
->name
);
726 sprintf(info
->bmname
, "none");
731 pr_err("nvm: max 31 devices can be reported.\n");
737 devices
->nr_devices
= i
;
739 if (copy_to_user(arg
, devices
,
740 sizeof(struct nvm_ioctl_get_devices
))) {
749 static long nvm_ioctl_dev_create(struct file
*file
, void __user
*arg
)
751 struct nvm_ioctl_create create
;
753 if (!capable(CAP_SYS_ADMIN
))
756 if (copy_from_user(&create
, arg
, sizeof(struct nvm_ioctl_create
)))
759 create
.dev
[DISK_NAME_LEN
- 1] = '\0';
760 create
.tgttype
[NVM_TTYPE_NAME_MAX
- 1] = '\0';
761 create
.tgtname
[DISK_NAME_LEN
- 1] = '\0';
763 if (create
.flags
!= 0) {
764 pr_err("nvm: no flags supported\n");
768 return __nvm_configure_create(&create
);
771 static long nvm_ioctl_dev_remove(struct file
*file
, void __user
*arg
)
773 struct nvm_ioctl_remove remove
;
775 if (!capable(CAP_SYS_ADMIN
))
778 if (copy_from_user(&remove
, arg
, sizeof(struct nvm_ioctl_remove
)))
781 remove
.tgtname
[DISK_NAME_LEN
- 1] = '\0';
783 if (remove
.flags
!= 0) {
784 pr_err("nvm: no flags supported\n");
788 return __nvm_configure_remove(&remove
);
791 static long nvm_ctl_ioctl(struct file
*file
, uint cmd
, unsigned long arg
)
793 void __user
*argp
= (void __user
*)arg
;
797 return nvm_ioctl_info(file
, argp
);
798 case NVM_GET_DEVICES
:
799 return nvm_ioctl_get_devices(file
, argp
);
801 return nvm_ioctl_dev_create(file
, argp
);
803 return nvm_ioctl_dev_remove(file
, argp
);
808 static const struct file_operations _ctl_fops
= {
809 .open
= nonseekable_open
,
810 .unlocked_ioctl
= nvm_ctl_ioctl
,
811 .owner
= THIS_MODULE
,
812 .llseek
= noop_llseek
,
815 static struct miscdevice _nvm_misc
= {
816 .minor
= MISC_DYNAMIC_MINOR
,
818 .nodename
= "lightnvm/control",
822 MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR
);
824 static int __init
nvm_mod_init(void)
828 ret
= misc_register(&_nvm_misc
);
830 pr_err("nvm: misc_register failed for control device");
835 static void __exit
nvm_mod_exit(void)
837 misc_deregister(&_nvm_misc
);
840 MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
841 MODULE_LICENSE("GPL v2");
842 MODULE_VERSION("0.1");
843 module_init(nvm_mod_init
);
844 module_exit(nvm_mod_exit
);