]>
Commit | Line | Data |
---|---|---|
cd9e9808 MB |
1 | /* |
2 | * Copyright (C) 2015 IT University of Copenhagen. All rights reserved. | |
3 | * Initial release: Matias Bjorling <[email protected]> | |
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 | ||
32 | static LIST_HEAD(nvm_targets); | |
33 | static LIST_HEAD(nvm_mgrs); | |
34 | static LIST_HEAD(nvm_devices); | |
35 | static DECLARE_RWSEM(nvm_lock); | |
36 | ||
37 | static 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 | ||
48 | int 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 | } | |
61 | EXPORT_SYMBOL(nvm_register_target); | |
62 | ||
63 | void 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 | } | |
72 | EXPORT_SYMBOL(nvm_unregister_target); | |
73 | ||
74 | void *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 | } | |
80 | EXPORT_SYMBOL(nvm_dev_dma_alloc); | |
81 | ||
82 | void 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 | } | |
87 | EXPORT_SYMBOL(nvm_dev_dma_free); | |
88 | ||
89 | static 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 | ||
100 | int 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 | } | |
113 | EXPORT_SYMBOL(nvm_register_mgr); | |
114 | ||
115 | void 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 | } | |
124 | EXPORT_SYMBOL(nvm_unregister_mgr); | |
125 | ||
126 | static struct nvm_dev *nvm_find_nvm_dev(const char *name) | |
127 | { | |
128 | struct nvm_dev *dev; | |
129 | ||
130 | list_for_each_entry(dev, &nvm_devices, devices) | |
131 | if (!strcmp(name, dev->name)) | |
132 | return dev; | |
133 | ||
134 | return NULL; | |
135 | } | |
136 | ||
137 | struct nvm_block *nvm_get_blk(struct nvm_dev *dev, struct nvm_lun *lun, | |
138 | unsigned long flags) | |
139 | { | |
140 | return dev->mt->get_blk(dev, lun, flags); | |
141 | } | |
142 | EXPORT_SYMBOL(nvm_get_blk); | |
143 | ||
144 | /* Assumes that all valid pages have already been moved on release to bm */ | |
145 | void nvm_put_blk(struct nvm_dev *dev, struct nvm_block *blk) | |
146 | { | |
147 | return dev->mt->put_blk(dev, blk); | |
148 | } | |
149 | EXPORT_SYMBOL(nvm_put_blk); | |
150 | ||
151 | int nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd) | |
152 | { | |
153 | return dev->mt->submit_io(dev, rqd); | |
154 | } | |
155 | EXPORT_SYMBOL(nvm_submit_io); | |
156 | ||
157 | int nvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk) | |
158 | { | |
159 | return dev->mt->erase_blk(dev, blk, 0); | |
160 | } | |
161 | EXPORT_SYMBOL(nvm_erase_blk); | |
162 | ||
163 | static void nvm_core_free(struct nvm_dev *dev) | |
164 | { | |
165 | kfree(dev); | |
166 | } | |
167 | ||
168 | static int nvm_core_init(struct nvm_dev *dev) | |
169 | { | |
170 | struct nvm_id *id = &dev->identity; | |
171 | struct nvm_id_group *grp = &id->groups[0]; | |
172 | ||
173 | /* device values */ | |
174 | dev->nr_chnls = grp->num_ch; | |
175 | dev->luns_per_chnl = grp->num_lun; | |
176 | dev->pgs_per_blk = grp->num_pg; | |
177 | dev->blks_per_lun = grp->num_blk; | |
178 | dev->nr_planes = grp->num_pln; | |
179 | dev->sec_size = grp->csecs; | |
180 | dev->oob_size = grp->sos; | |
181 | dev->sec_per_pg = grp->fpg_sz / grp->csecs; | |
182 | dev->addr_mode = id->ppat; | |
183 | dev->addr_format = id->ppaf; | |
184 | ||
185 | dev->plane_mode = NVM_PLANE_SINGLE; | |
186 | dev->max_rq_size = dev->ops->max_phys_sect * dev->sec_size; | |
187 | ||
4264c980 MB |
188 | if (grp->mtype != 0) { |
189 | pr_err("nvm: memory type not supported\n"); | |
190 | return -EINVAL; | |
191 | } | |
192 | ||
193 | if (grp->fmtype != 0 && grp->fmtype != 1) { | |
194 | pr_err("nvm: flash type not supported\n"); | |
195 | return -EINVAL; | |
196 | } | |
197 | ||
cd9e9808 MB |
198 | if (grp->mpos & 0x020202) |
199 | dev->plane_mode = NVM_PLANE_DOUBLE; | |
200 | if (grp->mpos & 0x040404) | |
201 | dev->plane_mode = NVM_PLANE_QUAD; | |
202 | ||
203 | /* calculated values */ | |
204 | dev->sec_per_pl = dev->sec_per_pg * dev->nr_planes; | |
205 | dev->sec_per_blk = dev->sec_per_pl * dev->pgs_per_blk; | |
206 | dev->sec_per_lun = dev->sec_per_blk * dev->blks_per_lun; | |
207 | dev->nr_luns = dev->luns_per_chnl * dev->nr_chnls; | |
208 | ||
209 | dev->total_blocks = dev->nr_planes * | |
210 | dev->blks_per_lun * | |
211 | dev->luns_per_chnl * | |
212 | dev->nr_chnls; | |
213 | dev->total_pages = dev->total_blocks * dev->pgs_per_blk; | |
214 | INIT_LIST_HEAD(&dev->online_targets); | |
215 | ||
216 | return 0; | |
217 | } | |
218 | ||
219 | static void nvm_free(struct nvm_dev *dev) | |
220 | { | |
221 | if (!dev) | |
222 | return; | |
223 | ||
224 | if (dev->mt) | |
225 | dev->mt->unregister_mgr(dev); | |
226 | ||
227 | nvm_core_free(dev); | |
228 | } | |
229 | ||
230 | static int nvm_init(struct nvm_dev *dev) | |
231 | { | |
232 | struct nvmm_type *mt; | |
233 | int ret = 0; | |
234 | ||
235 | if (!dev->q || !dev->ops) | |
236 | return -EINVAL; | |
237 | ||
238 | if (dev->ops->identity(dev->q, &dev->identity)) { | |
239 | pr_err("nvm: device could not be identified\n"); | |
240 | ret = -EINVAL; | |
241 | goto err; | |
242 | } | |
243 | ||
244 | pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n", | |
245 | dev->identity.ver_id, dev->identity.vmnt, | |
246 | dev->identity.cgrps); | |
247 | ||
248 | if (dev->identity.ver_id != 1) { | |
249 | pr_err("nvm: device not supported by kernel."); | |
250 | goto err; | |
251 | } | |
252 | ||
253 | if (dev->identity.cgrps != 1) { | |
254 | pr_err("nvm: only one group configuration supported."); | |
255 | goto err; | |
256 | } | |
257 | ||
258 | ret = nvm_core_init(dev); | |
259 | if (ret) { | |
260 | pr_err("nvm: could not initialize core structures.\n"); | |
261 | goto err; | |
262 | } | |
263 | ||
264 | /* register with device with a supported manager */ | |
265 | list_for_each_entry(mt, &nvm_mgrs, list) { | |
266 | ret = mt->register_mgr(dev); | |
267 | if (ret < 0) | |
268 | goto err; /* initialization failed */ | |
269 | if (ret > 0) { | |
270 | dev->mt = mt; | |
271 | break; /* successfully initialized */ | |
272 | } | |
273 | } | |
274 | ||
275 | if (!ret) { | |
276 | pr_info("nvm: no compatible manager found.\n"); | |
277 | return 0; | |
278 | } | |
279 | ||
280 | pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n", | |
281 | dev->name, dev->sec_per_pg, dev->nr_planes, | |
282 | dev->pgs_per_blk, dev->blks_per_lun, dev->nr_luns, | |
283 | dev->nr_chnls); | |
284 | return 0; | |
285 | err: | |
286 | nvm_free(dev); | |
287 | pr_err("nvm: failed to initialize nvm\n"); | |
288 | return ret; | |
289 | } | |
290 | ||
291 | static void nvm_exit(struct nvm_dev *dev) | |
292 | { | |
293 | if (dev->ppalist_pool) | |
294 | dev->ops->destroy_dma_pool(dev->ppalist_pool); | |
295 | nvm_free(dev); | |
296 | ||
297 | pr_info("nvm: successfully unloaded\n"); | |
298 | } | |
299 | ||
300 | int nvm_register(struct request_queue *q, char *disk_name, | |
301 | struct nvm_dev_ops *ops) | |
302 | { | |
303 | struct nvm_dev *dev; | |
304 | int ret; | |
305 | ||
306 | if (!ops->identity) | |
307 | return -EINVAL; | |
308 | ||
309 | dev = kzalloc(sizeof(struct nvm_dev), GFP_KERNEL); | |
310 | if (!dev) | |
311 | return -ENOMEM; | |
312 | ||
313 | dev->q = q; | |
314 | dev->ops = ops; | |
315 | strncpy(dev->name, disk_name, DISK_NAME_LEN); | |
316 | ||
317 | ret = nvm_init(dev); | |
318 | if (ret) | |
319 | goto err_init; | |
320 | ||
321 | down_write(&nvm_lock); | |
322 | list_add(&dev->devices, &nvm_devices); | |
323 | up_write(&nvm_lock); | |
324 | ||
325 | if (dev->ops->max_phys_sect > 1) { | |
326 | dev->ppalist_pool = dev->ops->create_dma_pool(dev->q, | |
327 | "ppalist"); | |
328 | if (!dev->ppalist_pool) { | |
329 | pr_err("nvm: could not create ppa pool\n"); | |
330 | return -ENOMEM; | |
331 | } | |
332 | } else if (dev->ops->max_phys_sect > 256) { | |
333 | pr_info("nvm: max sectors supported is 256.\n"); | |
334 | return -EINVAL; | |
335 | } | |
336 | ||
337 | return 0; | |
338 | err_init: | |
339 | kfree(dev); | |
340 | return ret; | |
341 | } | |
342 | EXPORT_SYMBOL(nvm_register); | |
343 | ||
344 | void nvm_unregister(char *disk_name) | |
345 | { | |
346 | struct nvm_dev *dev = nvm_find_nvm_dev(disk_name); | |
347 | ||
348 | if (!dev) { | |
349 | pr_err("nvm: could not find device %s to unregister\n", | |
350 | disk_name); | |
351 | return; | |
352 | } | |
353 | ||
354 | nvm_exit(dev); | |
355 | ||
356 | down_write(&nvm_lock); | |
357 | list_del(&dev->devices); | |
358 | up_write(&nvm_lock); | |
359 | } | |
360 | EXPORT_SYMBOL(nvm_unregister); | |
361 | ||
362 | static const struct block_device_operations nvm_fops = { | |
363 | .owner = THIS_MODULE, | |
364 | }; | |
365 | ||
366 | static int nvm_create_target(struct nvm_dev *dev, | |
367 | struct nvm_ioctl_create *create) | |
368 | { | |
369 | struct nvm_ioctl_create_simple *s = &create->conf.s; | |
370 | struct request_queue *tqueue; | |
371 | struct nvmm_type *mt; | |
372 | struct gendisk *tdisk; | |
373 | struct nvm_tgt_type *tt; | |
374 | struct nvm_target *t; | |
375 | void *targetdata; | |
376 | int ret = 0; | |
377 | ||
378 | if (!dev->mt) { | |
379 | /* register with device with a supported NVM manager */ | |
380 | list_for_each_entry(mt, &nvm_mgrs, list) { | |
381 | ret = mt->register_mgr(dev); | |
382 | if (ret < 0) | |
383 | return ret; /* initialization failed */ | |
384 | if (ret > 0) { | |
385 | dev->mt = mt; | |
386 | break; /* successfully initialized */ | |
387 | } | |
388 | } | |
389 | ||
390 | if (!ret) { | |
391 | pr_info("nvm: no compatible nvm manager found.\n"); | |
392 | return -ENODEV; | |
393 | } | |
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); | |
399 | return -EINVAL; | |
400 | } | |
401 | ||
402 | down_write(&nvm_lock); | |
403 | 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; | |
452 | err_init: | |
453 | put_disk(tdisk); | |
454 | err_queue: | |
455 | blk_cleanup_queue(tqueue); | |
456 | err_t: | |
457 | kfree(t); | |
458 | return -ENOMEM; | |
459 | } | |
460 | ||
461 | static 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); | |
470 | if (tt->exit) | |
471 | tt->exit(tdisk->private_data); | |
472 | ||
473 | blk_cleanup_queue(q); | |
474 | ||
475 | put_disk(tdisk); | |
476 | ||
477 | list_del(&t->list); | |
478 | kfree(t); | |
479 | } | |
480 | ||
481 | static int __nvm_configure_create(struct nvm_ioctl_create *create) | |
482 | { | |
483 | struct nvm_dev *dev; | |
484 | struct nvm_ioctl_create_simple *s; | |
485 | ||
486 | dev = nvm_find_nvm_dev(create->dev); | |
487 | if (!dev) { | |
488 | pr_err("nvm: device not found\n"); | |
489 | return -EINVAL; | |
490 | } | |
491 | ||
492 | if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) { | |
493 | pr_err("nvm: config type not valid\n"); | |
494 | return -EINVAL; | |
495 | } | |
496 | s = &create->conf.s; | |
497 | ||
498 | if (s->lun_begin > s->lun_end || s->lun_end > dev->nr_luns) { | |
499 | pr_err("nvm: lun out of bound (%u:%u > %u)\n", | |
500 | s->lun_begin, s->lun_end, dev->nr_luns); | |
501 | return -EINVAL; | |
502 | } | |
503 | ||
504 | return nvm_create_target(dev, create); | |
505 | } | |
506 | ||
507 | static int __nvm_configure_remove(struct nvm_ioctl_remove *remove) | |
508 | { | |
509 | struct nvm_target *t = NULL; | |
510 | struct nvm_dev *dev; | |
511 | int ret = -1; | |
512 | ||
513 | down_write(&nvm_lock); | |
514 | list_for_each_entry(dev, &nvm_devices, devices) | |
515 | list_for_each_entry(t, &dev->online_targets, list) { | |
516 | if (!strcmp(remove->tgtname, t->disk->disk_name)) { | |
517 | nvm_remove_target(t); | |
518 | ret = 0; | |
519 | break; | |
520 | } | |
521 | } | |
522 | up_write(&nvm_lock); | |
523 | ||
524 | if (ret) { | |
525 | pr_err("nvm: target \"%s\" doesn't exist.\n", remove->tgtname); | |
526 | return -EINVAL; | |
527 | } | |
528 | ||
529 | return 0; | |
530 | } | |
531 | ||
532 | #ifdef CONFIG_NVM_DEBUG | |
533 | static int nvm_configure_show(const char *val) | |
534 | { | |
535 | struct nvm_dev *dev; | |
536 | char opcode, devname[DISK_NAME_LEN]; | |
537 | int ret; | |
538 | ||
539 | ret = sscanf(val, "%c %32s", &opcode, devname); | |
540 | if (ret != 2) { | |
541 | pr_err("nvm: invalid command. Use \"opcode devicename\".\n"); | |
542 | return -EINVAL; | |
543 | } | |
544 | ||
545 | dev = nvm_find_nvm_dev(devname); | |
546 | if (!dev) { | |
547 | pr_err("nvm: device not found\n"); | |
548 | return -EINVAL; | |
549 | } | |
550 | ||
551 | if (!dev->mt) | |
552 | return 0; | |
553 | ||
554 | dev->mt->free_blocks_print(dev); | |
555 | ||
556 | return 0; | |
557 | } | |
558 | ||
559 | static int nvm_configure_remove(const char *val) | |
560 | { | |
561 | struct nvm_ioctl_remove remove; | |
562 | char opcode; | |
563 | int ret; | |
564 | ||
565 | ret = sscanf(val, "%c %256s", &opcode, remove.tgtname); | |
566 | if (ret != 2) { | |
567 | pr_err("nvm: invalid command. Use \"d targetname\".\n"); | |
568 | return -EINVAL; | |
569 | } | |
570 | ||
571 | remove.flags = 0; | |
572 | ||
573 | return __nvm_configure_remove(&remove); | |
574 | } | |
575 | ||
576 | static int nvm_configure_create(const char *val) | |
577 | { | |
578 | struct nvm_ioctl_create create; | |
579 | char opcode; | |
580 | int lun_begin, lun_end, ret; | |
581 | ||
582 | ret = sscanf(val, "%c %256s %256s %48s %u:%u", &opcode, create.dev, | |
583 | create.tgtname, create.tgttype, | |
584 | &lun_begin, &lun_end); | |
585 | if (ret != 6) { | |
586 | pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n"); | |
587 | return -EINVAL; | |
588 | } | |
589 | ||
590 | create.flags = 0; | |
591 | create.conf.type = NVM_CONFIG_TYPE_SIMPLE; | |
592 | create.conf.s.lun_begin = lun_begin; | |
593 | create.conf.s.lun_end = lun_end; | |
594 | ||
595 | return __nvm_configure_create(&create); | |
596 | } | |
597 | ||
598 | ||
599 | /* Exposes administrative interface through /sys/module/lnvm/configure_by_str */ | |
600 | static int nvm_configure_by_str_event(const char *val, | |
601 | const struct kernel_param *kp) | |
602 | { | |
603 | char opcode; | |
604 | int ret; | |
605 | ||
606 | ret = sscanf(val, "%c", &opcode); | |
607 | if (ret != 1) { | |
608 | pr_err("nvm: string must have the format of \"cmd ...\"\n"); | |
609 | return -EINVAL; | |
610 | } | |
611 | ||
612 | switch (opcode) { | |
613 | case 'a': | |
614 | return nvm_configure_create(val); | |
615 | case 'd': | |
616 | return nvm_configure_remove(val); | |
617 | case 's': | |
618 | return nvm_configure_show(val); | |
619 | default: | |
620 | pr_err("nvm: invalid command\n"); | |
621 | return -EINVAL; | |
622 | } | |
623 | ||
624 | return 0; | |
625 | } | |
626 | ||
627 | static int nvm_configure_get(char *buf, const struct kernel_param *kp) | |
628 | { | |
629 | int sz = 0; | |
630 | char *buf_start = buf; | |
631 | struct nvm_dev *dev; | |
632 | ||
633 | buf += sprintf(buf, "available devices:\n"); | |
634 | down_write(&nvm_lock); | |
635 | list_for_each_entry(dev, &nvm_devices, devices) { | |
636 | if (sz > 4095 - DISK_NAME_LEN) | |
637 | break; | |
638 | buf += sprintf(buf, " %32s\n", dev->name); | |
639 | } | |
640 | up_write(&nvm_lock); | |
641 | ||
642 | return buf - buf_start - 1; | |
643 | } | |
644 | ||
645 | static const struct kernel_param_ops nvm_configure_by_str_event_param_ops = { | |
646 | .set = nvm_configure_by_str_event, | |
647 | .get = nvm_configure_get, | |
648 | }; | |
649 | ||
650 | #undef MODULE_PARAM_PREFIX | |
651 | #define MODULE_PARAM_PREFIX "lnvm." | |
652 | ||
653 | module_param_cb(configure_debug, &nvm_configure_by_str_event_param_ops, NULL, | |
654 | 0644); | |
655 | ||
656 | #endif /* CONFIG_NVM_DEBUG */ | |
657 | ||
658 | static long nvm_ioctl_info(struct file *file, void __user *arg) | |
659 | { | |
660 | struct nvm_ioctl_info *info; | |
661 | struct nvm_tgt_type *tt; | |
662 | int tgt_iter = 0; | |
663 | ||
664 | if (!capable(CAP_SYS_ADMIN)) | |
665 | return -EPERM; | |
666 | ||
667 | info = memdup_user(arg, sizeof(struct nvm_ioctl_info)); | |
668 | if (IS_ERR(info)) | |
669 | return -EFAULT; | |
670 | ||
671 | info->version[0] = NVM_VERSION_MAJOR; | |
672 | info->version[1] = NVM_VERSION_MINOR; | |
673 | info->version[2] = NVM_VERSION_PATCH; | |
674 | ||
675 | down_write(&nvm_lock); | |
676 | list_for_each_entry(tt, &nvm_targets, list) { | |
677 | struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter]; | |
678 | ||
679 | tgt->version[0] = tt->version[0]; | |
680 | tgt->version[1] = tt->version[1]; | |
681 | tgt->version[2] = tt->version[2]; | |
682 | strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX); | |
683 | ||
684 | tgt_iter++; | |
685 | } | |
686 | ||
687 | info->tgtsize = tgt_iter; | |
688 | up_write(&nvm_lock); | |
689 | ||
690 | if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) | |
691 | return -EFAULT; | |
692 | ||
693 | kfree(info); | |
694 | return 0; | |
695 | } | |
696 | ||
697 | static long nvm_ioctl_get_devices(struct file *file, void __user *arg) | |
698 | { | |
699 | struct nvm_ioctl_get_devices *devices; | |
700 | struct nvm_dev *dev; | |
701 | int i = 0; | |
702 | ||
703 | if (!capable(CAP_SYS_ADMIN)) | |
704 | return -EPERM; | |
705 | ||
706 | devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL); | |
707 | if (!devices) | |
708 | return -ENOMEM; | |
709 | ||
710 | down_write(&nvm_lock); | |
711 | list_for_each_entry(dev, &nvm_devices, devices) { | |
712 | struct nvm_ioctl_device_info *info = &devices->info[i]; | |
713 | ||
714 | sprintf(info->devname, "%s", dev->name); | |
715 | if (dev->mt) { | |
716 | info->bmversion[0] = dev->mt->version[0]; | |
717 | info->bmversion[1] = dev->mt->version[1]; | |
718 | info->bmversion[2] = dev->mt->version[2]; | |
719 | sprintf(info->bmname, "%s", dev->mt->name); | |
720 | } else { | |
721 | sprintf(info->bmname, "none"); | |
722 | } | |
723 | ||
724 | i++; | |
725 | if (i > 31) { | |
726 | pr_err("nvm: max 31 devices can be reported.\n"); | |
727 | break; | |
728 | } | |
729 | } | |
730 | up_write(&nvm_lock); | |
731 | ||
732 | devices->nr_devices = i; | |
733 | ||
734 | if (copy_to_user(arg, devices, sizeof(struct nvm_ioctl_get_devices))) | |
735 | return -EFAULT; | |
736 | ||
737 | kfree(devices); | |
738 | return 0; | |
739 | } | |
740 | ||
741 | static long nvm_ioctl_dev_create(struct file *file, void __user *arg) | |
742 | { | |
743 | struct nvm_ioctl_create create; | |
744 | ||
745 | if (!capable(CAP_SYS_ADMIN)) | |
746 | return -EPERM; | |
747 | ||
748 | if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create))) | |
749 | return -EFAULT; | |
750 | ||
751 | create.dev[DISK_NAME_LEN - 1] = '\0'; | |
752 | create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0'; | |
753 | create.tgtname[DISK_NAME_LEN - 1] = '\0'; | |
754 | ||
755 | if (create.flags != 0) { | |
756 | pr_err("nvm: no flags supported\n"); | |
757 | return -EINVAL; | |
758 | } | |
759 | ||
760 | return __nvm_configure_create(&create); | |
761 | } | |
762 | ||
763 | static long nvm_ioctl_dev_remove(struct file *file, void __user *arg) | |
764 | { | |
765 | struct nvm_ioctl_remove remove; | |
766 | ||
767 | if (!capable(CAP_SYS_ADMIN)) | |
768 | return -EPERM; | |
769 | ||
770 | if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove))) | |
771 | return -EFAULT; | |
772 | ||
773 | remove.tgtname[DISK_NAME_LEN - 1] = '\0'; | |
774 | ||
775 | if (remove.flags != 0) { | |
776 | pr_err("nvm: no flags supported\n"); | |
777 | return -EINVAL; | |
778 | } | |
779 | ||
780 | return __nvm_configure_remove(&remove); | |
781 | } | |
782 | ||
783 | static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg) | |
784 | { | |
785 | void __user *argp = (void __user *)arg; | |
786 | ||
787 | switch (cmd) { | |
788 | case NVM_INFO: | |
789 | return nvm_ioctl_info(file, argp); | |
790 | case NVM_GET_DEVICES: | |
791 | return nvm_ioctl_get_devices(file, argp); | |
792 | case NVM_DEV_CREATE: | |
793 | return nvm_ioctl_dev_create(file, argp); | |
794 | case NVM_DEV_REMOVE: | |
795 | return nvm_ioctl_dev_remove(file, argp); | |
796 | } | |
797 | return 0; | |
798 | } | |
799 | ||
800 | static const struct file_operations _ctl_fops = { | |
801 | .open = nonseekable_open, | |
802 | .unlocked_ioctl = nvm_ctl_ioctl, | |
803 | .owner = THIS_MODULE, | |
804 | .llseek = noop_llseek, | |
805 | }; | |
806 | ||
807 | static struct miscdevice _nvm_misc = { | |
808 | .minor = MISC_DYNAMIC_MINOR, | |
809 | .name = "lightnvm", | |
810 | .nodename = "lightnvm/control", | |
811 | .fops = &_ctl_fops, | |
812 | }; | |
813 | ||
814 | MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR); | |
815 | ||
816 | static int __init nvm_mod_init(void) | |
817 | { | |
818 | int ret; | |
819 | ||
820 | ret = misc_register(&_nvm_misc); | |
821 | if (ret) | |
822 | pr_err("nvm: misc_register failed for control device"); | |
823 | ||
824 | return ret; | |
825 | } | |
826 | ||
827 | static void __exit nvm_mod_exit(void) | |
828 | { | |
829 | misc_deregister(&_nvm_misc); | |
830 | } | |
831 | ||
832 | MODULE_AUTHOR("Matias Bjorling <[email protected]>"); | |
833 | MODULE_LICENSE("GPL v2"); | |
834 | MODULE_VERSION("0.1"); | |
835 | module_init(nvm_mod_init); | |
836 | module_exit(nvm_mod_exit); |