blob: 86ce887b2ed6ab16bfe079300f46652fada2af13 [file] [log] [blame]
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001/*
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/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>
31
32static LIST_HEAD(nvm_targets);
33static LIST_HEAD(nvm_mgrs);
34static LIST_HEAD(nvm_devices);
35static DECLARE_RWSEM(nvm_lock);
36
37static struct nvm_tgt_type *nvm_find_target_type(const char *name)
38{
39 struct nvm_tgt_type *tt;
40
41 list_for_each_entry(tt, &nvm_targets, list)
42 if (!strcmp(name, tt->name))
43 return tt;
44
45 return NULL;
46}
47
48int nvm_register_target(struct nvm_tgt_type *tt)
49{
50 int ret = 0;
51
52 down_write(&nvm_lock);
53 if (nvm_find_target_type(tt->name))
54 ret = -EEXIST;
55 else
56 list_add(&tt->list, &nvm_targets);
57 up_write(&nvm_lock);
58
59 return ret;
60}
61EXPORT_SYMBOL(nvm_register_target);
62
63void nvm_unregister_target(struct nvm_tgt_type *tt)
64{
65 if (!tt)
66 return;
67
68 down_write(&nvm_lock);
69 list_del(&tt->list);
70 up_write(&nvm_lock);
71}
72EXPORT_SYMBOL(nvm_unregister_target);
73
74void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
75 dma_addr_t *dma_handler)
76{
77 return dev->ops->dev_dma_alloc(dev->q, dev->ppalist_pool, mem_flags,
78 dma_handler);
79}
80EXPORT_SYMBOL(nvm_dev_dma_alloc);
81
82void nvm_dev_dma_free(struct nvm_dev *dev, void *ppa_list,
83 dma_addr_t dma_handler)
84{
85 dev->ops->dev_dma_free(dev->ppalist_pool, ppa_list, dma_handler);
86}
87EXPORT_SYMBOL(nvm_dev_dma_free);
88
89static struct nvmm_type *nvm_find_mgr_type(const char *name)
90{
91 struct nvmm_type *mt;
92
93 list_for_each_entry(mt, &nvm_mgrs, list)
94 if (!strcmp(name, mt->name))
95 return mt;
96
97 return NULL;
98}
99
100int nvm_register_mgr(struct nvmm_type *mt)
101{
102 int ret = 0;
103
104 down_write(&nvm_lock);
105 if (nvm_find_mgr_type(mt->name))
106 ret = -EEXIST;
107 else
108 list_add(&mt->list, &nvm_mgrs);
109 up_write(&nvm_lock);
110
111 return ret;
112}
113EXPORT_SYMBOL(nvm_register_mgr);
114
115void nvm_unregister_mgr(struct nvmm_type *mt)
116{
117 if (!mt)
118 return;
119
120 down_write(&nvm_lock);
121 list_del(&mt->list);
122 up_write(&nvm_lock);
123}
124EXPORT_SYMBOL(nvm_unregister_mgr);
125
Wenwei Taod0a712c2015-11-28 16:49:28 +0100126/* register with device with a supported manager */
127static int register_mgr(struct nvm_dev *dev)
128{
129 struct nvmm_type *mt;
130 int ret = 0;
131
132 list_for_each_entry(mt, &nvm_mgrs, list) {
133 ret = mt->register_mgr(dev);
134 if (ret > 0) {
135 dev->mt = mt;
136 break; /* successfully initialized */
137 }
138 }
139
140 if (!ret)
141 pr_info("nvm: no compatible nvm manager found.\n");
142
143 return ret;
144}
145
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100146static struct nvm_dev *nvm_find_nvm_dev(const char *name)
147{
148 struct nvm_dev *dev;
149
150 list_for_each_entry(dev, &nvm_devices, devices)
151 if (!strcmp(name, dev->name))
152 return dev;
153
154 return NULL;
155}
156
157struct nvm_block *nvm_get_blk(struct nvm_dev *dev, struct nvm_lun *lun,
158 unsigned long flags)
159{
160 return dev->mt->get_blk(dev, lun, flags);
161}
162EXPORT_SYMBOL(nvm_get_blk);
163
164/* Assumes that all valid pages have already been moved on release to bm */
165void nvm_put_blk(struct nvm_dev *dev, struct nvm_block *blk)
166{
167 return dev->mt->put_blk(dev, blk);
168}
169EXPORT_SYMBOL(nvm_put_blk);
170
171int nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
172{
173 return dev->mt->submit_io(dev, rqd);
174}
175EXPORT_SYMBOL(nvm_submit_io);
176
177int nvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk)
178{
179 return dev->mt->erase_blk(dev, blk, 0);
180}
181EXPORT_SYMBOL(nvm_erase_blk);
182
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100183static int nvm_core_init(struct nvm_dev *dev)
184{
185 struct nvm_id *id = &dev->identity;
186 struct nvm_id_group *grp = &id->groups[0];
187
188 /* device values */
189 dev->nr_chnls = grp->num_ch;
190 dev->luns_per_chnl = grp->num_lun;
191 dev->pgs_per_blk = grp->num_pg;
192 dev->blks_per_lun = grp->num_blk;
193 dev->nr_planes = grp->num_pln;
194 dev->sec_size = grp->csecs;
195 dev->oob_size = grp->sos;
196 dev->sec_per_pg = grp->fpg_sz / grp->csecs;
Matias Bjørling7386af22015-11-16 15:34:44 +0100197 memcpy(&dev->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100198
199 dev->plane_mode = NVM_PLANE_SINGLE;
200 dev->max_rq_size = dev->ops->max_phys_sect * dev->sec_size;
201
Matias Bjørling4264c982015-11-16 15:34:41 +0100202 if (grp->mtype != 0) {
203 pr_err("nvm: memory type not supported\n");
204 return -EINVAL;
205 }
206
207 if (grp->fmtype != 0 && grp->fmtype != 1) {
208 pr_err("nvm: flash type not supported\n");
209 return -EINVAL;
210 }
211
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100212 if (grp->mpos & 0x020202)
213 dev->plane_mode = NVM_PLANE_DOUBLE;
214 if (grp->mpos & 0x040404)
215 dev->plane_mode = NVM_PLANE_QUAD;
216
217 /* calculated values */
218 dev->sec_per_pl = dev->sec_per_pg * dev->nr_planes;
219 dev->sec_per_blk = dev->sec_per_pl * dev->pgs_per_blk;
220 dev->sec_per_lun = dev->sec_per_blk * dev->blks_per_lun;
221 dev->nr_luns = dev->luns_per_chnl * dev->nr_chnls;
222
223 dev->total_blocks = dev->nr_planes *
224 dev->blks_per_lun *
225 dev->luns_per_chnl *
226 dev->nr_chnls;
227 dev->total_pages = dev->total_blocks * dev->pgs_per_blk;
228 INIT_LIST_HEAD(&dev->online_targets);
229
230 return 0;
231}
232
233static void nvm_free(struct nvm_dev *dev)
234{
235 if (!dev)
236 return;
237
238 if (dev->mt)
239 dev->mt->unregister_mgr(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100240}
241
242static int nvm_init(struct nvm_dev *dev)
243{
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100244 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100245
246 if (!dev->q || !dev->ops)
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100247 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100248
249 if (dev->ops->identity(dev->q, &dev->identity)) {
250 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100251 goto err;
252 }
253
254 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
255 dev->identity.ver_id, dev->identity.vmnt,
256 dev->identity.cgrps);
257
258 if (dev->identity.ver_id != 1) {
259 pr_err("nvm: device not supported by kernel.");
260 goto err;
261 }
262
263 if (dev->identity.cgrps != 1) {
264 pr_err("nvm: only one group configuration supported.");
265 goto err;
266 }
267
268 ret = nvm_core_init(dev);
269 if (ret) {
270 pr_err("nvm: could not initialize core structures.\n");
271 goto err;
272 }
273
Wenwei Taod0a712c2015-11-28 16:49:28 +0100274 down_write(&nvm_lock);
275 ret = register_mgr(dev);
276 up_write(&nvm_lock);
277 if (ret < 0)
278 goto err;
279 if (!ret)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100280 return 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100281
282 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
283 dev->name, dev->sec_per_pg, dev->nr_planes,
284 dev->pgs_per_blk, dev->blks_per_lun, dev->nr_luns,
285 dev->nr_chnls);
286 return 0;
287err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100288 pr_err("nvm: failed to initialize nvm\n");
289 return ret;
290}
291
292static void nvm_exit(struct nvm_dev *dev)
293{
294 if (dev->ppalist_pool)
295 dev->ops->destroy_dma_pool(dev->ppalist_pool);
296 nvm_free(dev);
297
298 pr_info("nvm: successfully unloaded\n");
299}
300
301int nvm_register(struct request_queue *q, char *disk_name,
302 struct nvm_dev_ops *ops)
303{
304 struct nvm_dev *dev;
305 int ret;
306
307 if (!ops->identity)
308 return -EINVAL;
309
310 dev = kzalloc(sizeof(struct nvm_dev), GFP_KERNEL);
311 if (!dev)
312 return -ENOMEM;
313
314 dev->q = q;
315 dev->ops = ops;
316 strncpy(dev->name, disk_name, DISK_NAME_LEN);
317
318 ret = nvm_init(dev);
319 if (ret)
320 goto err_init;
321
Wenwei Taod1601472015-11-28 16:49:25 +0100322 if (dev->ops->max_phys_sect > 256) {
323 pr_info("nvm: max sectors supported is 256.\n");
324 ret = -EINVAL;
325 goto err_init;
326 }
327
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100328 if (dev->ops->max_phys_sect > 1) {
329 dev->ppalist_pool = dev->ops->create_dma_pool(dev->q,
330 "ppalist");
331 if (!dev->ppalist_pool) {
332 pr_err("nvm: could not create ppa pool\n");
Matias Bjørling93e70c12015-11-20 13:47:54 +0100333 ret = -ENOMEM;
334 goto err_init;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100335 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100336 }
337
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100338 down_write(&nvm_lock);
339 list_add(&dev->devices, &nvm_devices);
340 up_write(&nvm_lock);
341
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100342 return 0;
343err_init:
344 kfree(dev);
345 return ret;
346}
347EXPORT_SYMBOL(nvm_register);
348
349void nvm_unregister(char *disk_name)
350{
Wenwei Taod0a712c2015-11-28 16:49:28 +0100351 struct nvm_dev *dev;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100352
Wenwei Taod0a712c2015-11-28 16:49:28 +0100353 down_write(&nvm_lock);
354 dev = nvm_find_nvm_dev(disk_name);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100355 if (!dev) {
356 pr_err("nvm: could not find device %s to unregister\n",
357 disk_name);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100358 up_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100359 return;
360 }
361
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100362 list_del(&dev->devices);
363 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +0100364
365 nvm_exit(dev);
366 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100367}
368EXPORT_SYMBOL(nvm_unregister);
369
370static const struct block_device_operations nvm_fops = {
371 .owner = THIS_MODULE,
372};
373
374static int nvm_create_target(struct nvm_dev *dev,
375 struct nvm_ioctl_create *create)
376{
377 struct nvm_ioctl_create_simple *s = &create->conf.s;
378 struct request_queue *tqueue;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100379 struct gendisk *tdisk;
380 struct nvm_tgt_type *tt;
381 struct nvm_target *t;
382 void *targetdata;
383 int ret = 0;
384
Wenwei Taod0a712c2015-11-28 16:49:28 +0100385 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100386 if (!dev->mt) {
Wenwei Taod0a712c2015-11-28 16:49:28 +0100387 ret = register_mgr(dev);
388 if (!ret)
389 ret = -ENODEV;
390 if (ret < 0) {
391 up_write(&nvm_lock);
392 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100393 }
394 }
395
396 tt = nvm_find_target_type(create->tgttype);
397 if (!tt) {
398 pr_err("nvm: target type %s not found\n", create->tgttype);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100399 up_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100400 return -EINVAL;
401 }
402
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100403 list_for_each_entry(t, &dev->online_targets, list) {
404 if (!strcmp(create->tgtname, t->disk->disk_name)) {
405 pr_err("nvm: target name already exists.\n");
406 up_write(&nvm_lock);
407 return -EINVAL;
408 }
409 }
410 up_write(&nvm_lock);
411
412 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
413 if (!t)
414 return -ENOMEM;
415
416 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
417 if (!tqueue)
418 goto err_t;
419 blk_queue_make_request(tqueue, tt->make_rq);
420
421 tdisk = alloc_disk(0);
422 if (!tdisk)
423 goto err_queue;
424
425 sprintf(tdisk->disk_name, "%s", create->tgtname);
426 tdisk->flags = GENHD_FL_EXT_DEVT;
427 tdisk->major = 0;
428 tdisk->first_minor = 0;
429 tdisk->fops = &nvm_fops;
430 tdisk->queue = tqueue;
431
432 targetdata = tt->init(dev, tdisk, s->lun_begin, s->lun_end);
433 if (IS_ERR(targetdata))
434 goto err_init;
435
436 tdisk->private_data = targetdata;
437 tqueue->queuedata = targetdata;
438
439 blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
440
441 set_capacity(tdisk, tt->capacity(targetdata));
442 add_disk(tdisk);
443
444 t->type = tt;
445 t->disk = tdisk;
446
447 down_write(&nvm_lock);
448 list_add_tail(&t->list, &dev->online_targets);
449 up_write(&nvm_lock);
450
451 return 0;
452err_init:
453 put_disk(tdisk);
454err_queue:
455 blk_cleanup_queue(tqueue);
456err_t:
457 kfree(t);
458 return -ENOMEM;
459}
460
461static void nvm_remove_target(struct nvm_target *t)
462{
463 struct nvm_tgt_type *tt = t->type;
464 struct gendisk *tdisk = t->disk;
465 struct request_queue *q = tdisk->queue;
466
467 lockdep_assert_held(&nvm_lock);
468
469 del_gendisk(tdisk);
Javier Gonzálezd09f9582015-11-16 15:34:47 +0100470 blk_cleanup_queue(q);
471
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100472 if (tt->exit)
473 tt->exit(tdisk->private_data);
474
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100475 put_disk(tdisk);
476
477 list_del(&t->list);
478 kfree(t);
479}
480
481static int __nvm_configure_create(struct nvm_ioctl_create *create)
482{
483 struct nvm_dev *dev;
484 struct nvm_ioctl_create_simple *s;
485
Wenwei Taod0a712c2015-11-28 16:49:28 +0100486 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100487 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100488 up_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100489 if (!dev) {
490 pr_err("nvm: device not found\n");
491 return -EINVAL;
492 }
493
494 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
495 pr_err("nvm: config type not valid\n");
496 return -EINVAL;
497 }
498 s = &create->conf.s;
499
500 if (s->lun_begin > s->lun_end || s->lun_end > dev->nr_luns) {
501 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
502 s->lun_begin, s->lun_end, dev->nr_luns);
503 return -EINVAL;
504 }
505
506 return nvm_create_target(dev, create);
507}
508
509static int __nvm_configure_remove(struct nvm_ioctl_remove *remove)
510{
511 struct nvm_target *t = NULL;
512 struct nvm_dev *dev;
513 int ret = -1;
514
515 down_write(&nvm_lock);
516 list_for_each_entry(dev, &nvm_devices, devices)
517 list_for_each_entry(t, &dev->online_targets, list) {
518 if (!strcmp(remove->tgtname, t->disk->disk_name)) {
519 nvm_remove_target(t);
520 ret = 0;
521 break;
522 }
523 }
524 up_write(&nvm_lock);
525
526 if (ret) {
527 pr_err("nvm: target \"%s\" doesn't exist.\n", remove->tgtname);
528 return -EINVAL;
529 }
530
531 return 0;
532}
533
534#ifdef CONFIG_NVM_DEBUG
535static int nvm_configure_show(const char *val)
536{
537 struct nvm_dev *dev;
538 char opcode, devname[DISK_NAME_LEN];
539 int ret;
540
541 ret = sscanf(val, "%c %32s", &opcode, devname);
542 if (ret != 2) {
543 pr_err("nvm: invalid command. Use \"opcode devicename\".\n");
544 return -EINVAL;
545 }
546
Wenwei Taod0a712c2015-11-28 16:49:28 +0100547 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100548 dev = nvm_find_nvm_dev(devname);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100549 up_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100550 if (!dev) {
551 pr_err("nvm: device not found\n");
552 return -EINVAL;
553 }
554
555 if (!dev->mt)
556 return 0;
557
Javier Gonzalez2fde0e42015-11-20 13:47:57 +0100558 dev->mt->lun_info_print(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100559
560 return 0;
561}
562
563static int nvm_configure_remove(const char *val)
564{
565 struct nvm_ioctl_remove remove;
566 char opcode;
567 int ret;
568
569 ret = sscanf(val, "%c %256s", &opcode, remove.tgtname);
570 if (ret != 2) {
571 pr_err("nvm: invalid command. Use \"d targetname\".\n");
572 return -EINVAL;
573 }
574
575 remove.flags = 0;
576
577 return __nvm_configure_remove(&remove);
578}
579
580static int nvm_configure_create(const char *val)
581{
582 struct nvm_ioctl_create create;
583 char opcode;
584 int lun_begin, lun_end, ret;
585
586 ret = sscanf(val, "%c %256s %256s %48s %u:%u", &opcode, create.dev,
587 create.tgtname, create.tgttype,
588 &lun_begin, &lun_end);
589 if (ret != 6) {
590 pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n");
591 return -EINVAL;
592 }
593
594 create.flags = 0;
595 create.conf.type = NVM_CONFIG_TYPE_SIMPLE;
596 create.conf.s.lun_begin = lun_begin;
597 create.conf.s.lun_end = lun_end;
598
599 return __nvm_configure_create(&create);
600}
601
602
603/* Exposes administrative interface through /sys/module/lnvm/configure_by_str */
604static int nvm_configure_by_str_event(const char *val,
605 const struct kernel_param *kp)
606{
607 char opcode;
608 int ret;
609
610 ret = sscanf(val, "%c", &opcode);
611 if (ret != 1) {
612 pr_err("nvm: string must have the format of \"cmd ...\"\n");
613 return -EINVAL;
614 }
615
616 switch (opcode) {
617 case 'a':
618 return nvm_configure_create(val);
619 case 'd':
620 return nvm_configure_remove(val);
621 case 's':
622 return nvm_configure_show(val);
623 default:
624 pr_err("nvm: invalid command\n");
625 return -EINVAL;
626 }
627
628 return 0;
629}
630
631static int nvm_configure_get(char *buf, const struct kernel_param *kp)
632{
633 int sz = 0;
634 char *buf_start = buf;
635 struct nvm_dev *dev;
636
637 buf += sprintf(buf, "available devices:\n");
638 down_write(&nvm_lock);
639 list_for_each_entry(dev, &nvm_devices, devices) {
640 if (sz > 4095 - DISK_NAME_LEN)
641 break;
642 buf += sprintf(buf, " %32s\n", dev->name);
643 }
644 up_write(&nvm_lock);
645
646 return buf - buf_start - 1;
647}
648
649static const struct kernel_param_ops nvm_configure_by_str_event_param_ops = {
650 .set = nvm_configure_by_str_event,
651 .get = nvm_configure_get,
652};
653
654#undef MODULE_PARAM_PREFIX
655#define MODULE_PARAM_PREFIX "lnvm."
656
657module_param_cb(configure_debug, &nvm_configure_by_str_event_param_ops, NULL,
658 0644);
659
660#endif /* CONFIG_NVM_DEBUG */
661
662static long nvm_ioctl_info(struct file *file, void __user *arg)
663{
664 struct nvm_ioctl_info *info;
665 struct nvm_tgt_type *tt;
666 int tgt_iter = 0;
667
668 if (!capable(CAP_SYS_ADMIN))
669 return -EPERM;
670
671 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
672 if (IS_ERR(info))
673 return -EFAULT;
674
675 info->version[0] = NVM_VERSION_MAJOR;
676 info->version[1] = NVM_VERSION_MINOR;
677 info->version[2] = NVM_VERSION_PATCH;
678
679 down_write(&nvm_lock);
680 list_for_each_entry(tt, &nvm_targets, list) {
681 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
682
683 tgt->version[0] = tt->version[0];
684 tgt->version[1] = tt->version[1];
685 tgt->version[2] = tt->version[2];
686 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
687
688 tgt_iter++;
689 }
690
691 info->tgtsize = tgt_iter;
692 up_write(&nvm_lock);
693
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100694 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
695 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100696 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100697 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100698
699 kfree(info);
700 return 0;
701}
702
703static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
704{
705 struct nvm_ioctl_get_devices *devices;
706 struct nvm_dev *dev;
707 int i = 0;
708
709 if (!capable(CAP_SYS_ADMIN))
710 return -EPERM;
711
712 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
713 if (!devices)
714 return -ENOMEM;
715
716 down_write(&nvm_lock);
717 list_for_each_entry(dev, &nvm_devices, devices) {
718 struct nvm_ioctl_device_info *info = &devices->info[i];
719
720 sprintf(info->devname, "%s", dev->name);
721 if (dev->mt) {
722 info->bmversion[0] = dev->mt->version[0];
723 info->bmversion[1] = dev->mt->version[1];
724 info->bmversion[2] = dev->mt->version[2];
725 sprintf(info->bmname, "%s", dev->mt->name);
726 } else {
727 sprintf(info->bmname, "none");
728 }
729
730 i++;
731 if (i > 31) {
732 pr_err("nvm: max 31 devices can be reported.\n");
733 break;
734 }
735 }
736 up_write(&nvm_lock);
737
738 devices->nr_devices = i;
739
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100740 if (copy_to_user(arg, devices,
741 sizeof(struct nvm_ioctl_get_devices))) {
742 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100743 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100744 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100745
746 kfree(devices);
747 return 0;
748}
749
750static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
751{
752 struct nvm_ioctl_create create;
753
754 if (!capable(CAP_SYS_ADMIN))
755 return -EPERM;
756
757 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
758 return -EFAULT;
759
760 create.dev[DISK_NAME_LEN - 1] = '\0';
761 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
762 create.tgtname[DISK_NAME_LEN - 1] = '\0';
763
764 if (create.flags != 0) {
765 pr_err("nvm: no flags supported\n");
766 return -EINVAL;
767 }
768
769 return __nvm_configure_create(&create);
770}
771
772static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
773{
774 struct nvm_ioctl_remove remove;
775
776 if (!capable(CAP_SYS_ADMIN))
777 return -EPERM;
778
779 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
780 return -EFAULT;
781
782 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
783
784 if (remove.flags != 0) {
785 pr_err("nvm: no flags supported\n");
786 return -EINVAL;
787 }
788
789 return __nvm_configure_remove(&remove);
790}
791
792static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
793{
794 void __user *argp = (void __user *)arg;
795
796 switch (cmd) {
797 case NVM_INFO:
798 return nvm_ioctl_info(file, argp);
799 case NVM_GET_DEVICES:
800 return nvm_ioctl_get_devices(file, argp);
801 case NVM_DEV_CREATE:
802 return nvm_ioctl_dev_create(file, argp);
803 case NVM_DEV_REMOVE:
804 return nvm_ioctl_dev_remove(file, argp);
805 }
806 return 0;
807}
808
809static const struct file_operations _ctl_fops = {
810 .open = nonseekable_open,
811 .unlocked_ioctl = nvm_ctl_ioctl,
812 .owner = THIS_MODULE,
813 .llseek = noop_llseek,
814};
815
816static struct miscdevice _nvm_misc = {
817 .minor = MISC_DYNAMIC_MINOR,
818 .name = "lightnvm",
819 .nodename = "lightnvm/control",
820 .fops = &_ctl_fops,
821};
822
823MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR);
824
825static int __init nvm_mod_init(void)
826{
827 int ret;
828
829 ret = misc_register(&_nvm_misc);
830 if (ret)
831 pr_err("nvm: misc_register failed for control device");
832
833 return ret;
834}
835
836static void __exit nvm_mod_exit(void)
837{
838 misc_deregister(&_nvm_misc);
839}
840
841MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
842MODULE_LICENSE("GPL v2");
843MODULE_VERSION("0.1");
844module_init(nvm_mod_init);
845module_exit(nvm_mod_exit);