blob: 5c2d0f3a830b772195b9106d21d140c2a3538d3a [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
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010021#include <linux/list.h>
22#include <linux/types.h>
23#include <linux/sem.h>
24#include <linux/bitmap.h>
Rakesh Pandit90014822017-10-13 14:45:50 +020025#include <linux/module.h>
Paul Gortmaker389b2a12016-10-29 16:38:41 -040026#include <linux/moduleparam.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010027#include <linux/miscdevice.h>
28#include <linux/lightnvm.h>
Matias Bjørling912761622016-01-12 07:49:21 +010029#include <linux/sched/sysctl.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010030
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020031static LIST_HEAD(nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +020032static DECLARE_RWSEM(nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010033static LIST_HEAD(nvm_devices);
34static DECLARE_RWSEM(nvm_lock);
35
Matias Bjørlingade69e22017-01-31 13:17:09 +010036/* Map between virtual and physical channel and lun */
37struct nvm_ch_map {
38 int ch_off;
39 int nr_luns;
40 int *lun_offs;
41};
42
43struct nvm_dev_map {
44 struct nvm_ch_map *chnls;
45 int nr_chnls;
46};
47
Matias Bjørlingade69e22017-01-31 13:17:09 +010048static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
49{
50 struct nvm_target *tgt;
51
52 list_for_each_entry(tgt, &dev->targets, list)
53 if (!strcmp(name, tgt->disk->disk_name))
54 return tgt;
55
56 return NULL;
57}
58
59static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
60{
61 int i;
62
63 for (i = lun_begin; i <= lun_end; i++) {
64 if (test_and_set_bit(i, dev->lun_map)) {
65 pr_err("nvm: lun %d already allocated\n", i);
66 goto err;
67 }
68 }
69
70 return 0;
71err:
Javier González507f7d62017-05-03 11:19:05 +020072 while (--i >= lun_begin)
Matias Bjørlingade69e22017-01-31 13:17:09 +010073 clear_bit(i, dev->lun_map);
74
75 return -EBUSY;
76}
77
78static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
79 int lun_end)
80{
81 int i;
82
83 for (i = lun_begin; i <= lun_end; i++)
84 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
85}
86
Javier Gonzálezedee1bd2017-04-15 20:55:42 +020087static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
Matias Bjørlingade69e22017-01-31 13:17:09 +010088{
89 struct nvm_dev *dev = tgt_dev->parent;
90 struct nvm_dev_map *dev_map = tgt_dev->map;
91 int i, j;
92
93 for (i = 0; i < dev_map->nr_chnls; i++) {
94 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
95 int *lun_offs = ch_map->lun_offs;
96 int ch = i + ch_map->ch_off;
97
Javier Gonzálezedee1bd2017-04-15 20:55:42 +020098 if (clear) {
99 for (j = 0; j < ch_map->nr_luns; j++) {
100 int lun = j + lun_offs[j];
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100101 int lunid = (ch * dev->geo.nr_luns) + lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100102
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200103 WARN_ON(!test_and_clear_bit(lunid,
104 dev->lun_map));
105 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100106 }
107
108 kfree(ch_map->lun_offs);
109 }
110
111 kfree(dev_map->chnls);
112 kfree(dev_map);
113
114 kfree(tgt_dev->luns);
115 kfree(tgt_dev);
116}
117
118static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
119 int lun_begin, int lun_end)
120{
121 struct nvm_tgt_dev *tgt_dev = NULL;
122 struct nvm_dev_map *dev_rmap = dev->rmap;
123 struct nvm_dev_map *dev_map;
124 struct ppa_addr *luns;
125 int nr_luns = lun_end - lun_begin + 1;
126 int luns_left = nr_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100127 int nr_chnls = nr_luns / dev->geo.nr_luns;
128 int nr_chnls_mod = nr_luns % dev->geo.nr_luns;
129 int bch = lun_begin / dev->geo.nr_luns;
130 int blun = lun_begin % dev->geo.nr_luns;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100131 int lunid = 0;
132 int lun_balanced = 1;
133 int prev_nr_luns;
134 int i, j;
135
Matias Bjørlingade69e22017-01-31 13:17:09 +0100136 nr_chnls = (nr_chnls_mod == 0) ? nr_chnls : nr_chnls + 1;
137
138 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
139 if (!dev_map)
140 goto err_dev;
141
142 dev_map->chnls = kcalloc(nr_chnls, sizeof(struct nvm_ch_map),
143 GFP_KERNEL);
144 if (!dev_map->chnls)
145 goto err_chnls;
146
147 luns = kcalloc(nr_luns, sizeof(struct ppa_addr), GFP_KERNEL);
148 if (!luns)
149 goto err_luns;
150
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100151 prev_nr_luns = (luns_left > dev->geo.nr_luns) ?
152 dev->geo.nr_luns : luns_left;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100153 for (i = 0; i < nr_chnls; i++) {
154 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
155 int *lun_roffs = ch_rmap->lun_offs;
156 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
157 int *lun_offs;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100158 int luns_in_chnl = (luns_left > dev->geo.nr_luns) ?
159 dev->geo.nr_luns : luns_left;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100160
161 if (lun_balanced && prev_nr_luns != luns_in_chnl)
162 lun_balanced = 0;
163
164 ch_map->ch_off = ch_rmap->ch_off = bch;
165 ch_map->nr_luns = luns_in_chnl;
166
167 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
168 if (!lun_offs)
169 goto err_ch;
170
171 for (j = 0; j < luns_in_chnl; j++) {
172 luns[lunid].ppa = 0;
173 luns[lunid].g.ch = i;
174 luns[lunid++].g.lun = j;
175
176 lun_offs[j] = blun;
177 lun_roffs[j + blun] = blun;
178 }
179
180 ch_map->lun_offs = lun_offs;
181
182 /* when starting a new channel, lun offset is reset */
183 blun = 0;
184 luns_left -= luns_in_chnl;
185 }
186
187 dev_map->nr_chnls = nr_chnls;
188
189 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
190 if (!tgt_dev)
191 goto err_ch;
192
193 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
194 /* Target device only owns a portion of the physical device */
195 tgt_dev->geo.nr_chnls = nr_chnls;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100196 tgt_dev->geo.all_luns = nr_luns;
197 tgt_dev->geo.nr_luns = (lun_balanced) ? prev_nr_luns : -1;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100198 tgt_dev->total_secs = nr_luns * tgt_dev->geo.sec_per_lun;
199 tgt_dev->q = dev->q;
200 tgt_dev->map = dev_map;
201 tgt_dev->luns = luns;
202 memcpy(&tgt_dev->identity, &dev->identity, sizeof(struct nvm_id));
203
204 tgt_dev->parent = dev;
205
206 return tgt_dev;
207err_ch:
Javier González507f7d62017-05-03 11:19:05 +0200208 while (--i >= 0)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100209 kfree(dev_map->chnls[i].lun_offs);
210 kfree(luns);
211err_luns:
212 kfree(dev_map->chnls);
213err_chnls:
214 kfree(dev_map);
215err_dev:
216 return tgt_dev;
217}
218
219static const struct block_device_operations nvm_fops = {
220 .owner = THIS_MODULE,
221};
222
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100223static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200224{
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100225 struct nvm_tgt_type *tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200226
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100227 list_for_each_entry(tt, &nvm_tgt_types, list)
228 if (!strcmp(name, tt->name))
229 return tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200230
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100231 return NULL;
232}
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200233
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100234static struct nvm_tgt_type *nvm_find_target_type(const char *name)
235{
236 struct nvm_tgt_type *tt;
237
238 down_write(&nvm_tgtt_lock);
239 tt = __nvm_find_target_type(name);
240 up_write(&nvm_tgtt_lock);
241
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200242 return tt;
243}
244
Matias Bjørlingade69e22017-01-31 13:17:09 +0100245static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
246{
247 struct nvm_ioctl_create_simple *s = &create->conf.s;
248 struct request_queue *tqueue;
249 struct gendisk *tdisk;
250 struct nvm_tgt_type *tt;
251 struct nvm_target *t;
252 struct nvm_tgt_dev *tgt_dev;
253 void *targetdata;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300254 int ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100255
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100256 tt = nvm_find_target_type(create->tgttype);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100257 if (!tt) {
258 pr_err("nvm: target type %s not found\n", create->tgttype);
259 return -EINVAL;
260 }
261
262 mutex_lock(&dev->mlock);
263 t = nvm_find_target(dev, create->tgtname);
264 if (t) {
265 pr_err("nvm: target name already exists.\n");
266 mutex_unlock(&dev->mlock);
267 return -EINVAL;
268 }
269 mutex_unlock(&dev->mlock);
270
Rakesh Pandit12e9a6d62017-06-27 14:55:33 +0300271 ret = nvm_reserve_luns(dev, s->lun_begin, s->lun_end);
272 if (ret)
273 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100274
275 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300276 if (!t) {
277 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100278 goto err_reserve;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300279 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100280
281 tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end);
282 if (!tgt_dev) {
283 pr_err("nvm: could not create target device\n");
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300284 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100285 goto err_t;
286 }
287
Matias Bjørlingade69e22017-01-31 13:17:09 +0100288 tdisk = alloc_disk(0);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300289 if (!tdisk) {
290 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200291 goto err_dev;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300292 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200293
294 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300295 if (!tqueue) {
296 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200297 goto err_disk;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300298 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200299 blk_queue_make_request(tqueue, tt->make_rq);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100300
Javier González6eb08242017-04-15 20:55:49 +0200301 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100302 tdisk->flags = GENHD_FL_EXT_DEVT;
303 tdisk->major = 0;
304 tdisk->first_minor = 0;
305 tdisk->fops = &nvm_fops;
306 tdisk->queue = tqueue;
307
Javier González4af3f752017-04-15 20:55:45 +0200308 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300309 if (IS_ERR(targetdata)) {
310 ret = PTR_ERR(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100311 goto err_init;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300312 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100313
314 tdisk->private_data = targetdata;
315 tqueue->queuedata = targetdata;
316
317 blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
318
319 set_capacity(tdisk, tt->capacity(targetdata));
320 add_disk(tdisk);
321
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300322 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
323 ret = -ENOMEM;
Javier González9a69b0e2017-01-31 13:17:20 +0100324 goto err_sysfs;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300325 }
Javier González9a69b0e2017-01-31 13:17:20 +0100326
Matias Bjørlingade69e22017-01-31 13:17:09 +0100327 t->type = tt;
328 t->disk = tdisk;
329 t->dev = tgt_dev;
330
331 mutex_lock(&dev->mlock);
332 list_add_tail(&t->list, &dev->targets);
333 mutex_unlock(&dev->mlock);
334
Rakesh Pandit90014822017-10-13 14:45:50 +0200335 __module_get(tt->owner);
336
Matias Bjørlingade69e22017-01-31 13:17:09 +0100337 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100338err_sysfs:
339 if (tt->exit)
340 tt->exit(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100341err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100342 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600343 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200344err_disk:
345 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100346err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200347 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100348err_t:
349 kfree(t);
350err_reserve:
351 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300352 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100353}
354
355static void __nvm_remove_target(struct nvm_target *t)
356{
357 struct nvm_tgt_type *tt = t->type;
358 struct gendisk *tdisk = t->disk;
359 struct request_queue *q = tdisk->queue;
360
361 del_gendisk(tdisk);
362 blk_cleanup_queue(q);
363
Javier González9a69b0e2017-01-31 13:17:20 +0100364 if (tt->sysfs_exit)
365 tt->sysfs_exit(tdisk);
366
Matias Bjørlingade69e22017-01-31 13:17:09 +0100367 if (tt->exit)
368 tt->exit(tdisk->private_data);
369
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200370 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100371 put_disk(tdisk);
Rakesh Pandit90014822017-10-13 14:45:50 +0200372 module_put(t->type->owner);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100373
374 list_del(&t->list);
375 kfree(t);
376}
377
378/**
379 * nvm_remove_tgt - Removes a target from the media manager
380 * @dev: device
381 * @remove: ioctl structure with target name to remove.
382 *
383 * Returns:
384 * 0: on success
385 * 1: on not found
386 * <0: on error
387 */
388static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
389{
390 struct nvm_target *t;
391
392 mutex_lock(&dev->mlock);
393 t = nvm_find_target(dev, remove->tgtname);
394 if (!t) {
395 mutex_unlock(&dev->mlock);
396 return 1;
397 }
398 __nvm_remove_target(t);
399 mutex_unlock(&dev->mlock);
400
401 return 0;
402}
403
404static int nvm_register_map(struct nvm_dev *dev)
405{
406 struct nvm_dev_map *rmap;
407 int i, j;
408
409 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
410 if (!rmap)
411 goto err_rmap;
412
413 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
414 GFP_KERNEL);
415 if (!rmap->chnls)
416 goto err_chnls;
417
418 for (i = 0; i < dev->geo.nr_chnls; i++) {
419 struct nvm_ch_map *ch_rmap;
420 int *lun_roffs;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100421 int luns_in_chnl = dev->geo.nr_luns;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100422
423 ch_rmap = &rmap->chnls[i];
424
425 ch_rmap->ch_off = -1;
426 ch_rmap->nr_luns = luns_in_chnl;
427
428 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
429 if (!lun_roffs)
430 goto err_ch;
431
432 for (j = 0; j < luns_in_chnl; j++)
433 lun_roffs[j] = -1;
434
435 ch_rmap->lun_offs = lun_roffs;
436 }
437
438 dev->rmap = rmap;
439
440 return 0;
441err_ch:
442 while (--i >= 0)
443 kfree(rmap->chnls[i].lun_offs);
444err_chnls:
445 kfree(rmap);
446err_rmap:
447 return -ENOMEM;
448}
449
Javier González7a3de2b2017-04-15 20:55:39 +0200450static void nvm_unregister_map(struct nvm_dev *dev)
451{
452 struct nvm_dev_map *rmap = dev->rmap;
453 int i;
454
455 for (i = 0; i < dev->geo.nr_chnls; i++)
456 kfree(rmap->chnls[i].lun_offs);
457
458 kfree(rmap->chnls);
459 kfree(rmap);
460}
461
Matias Bjørling61a561d2017-01-31 13:17:13 +0100462static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100463{
464 struct nvm_dev_map *dev_map = tgt_dev->map;
465 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
466 int lun_off = ch_map->lun_offs[p->g.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100467
468 p->g.ch += ch_map->ch_off;
469 p->g.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100470}
471
Matias Bjørling61a561d2017-01-31 13:17:13 +0100472static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100473{
474 struct nvm_dev *dev = tgt_dev->parent;
475 struct nvm_dev_map *dev_rmap = dev->rmap;
476 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
477 int lun_roff = ch_rmap->lun_offs[p->g.lun];
478
479 p->g.ch -= ch_rmap->ch_off;
480 p->g.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100481}
482
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100483static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
484 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100485{
486 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100487
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100488 for (i = 0; i < nr_ppas; i++) {
489 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
490 ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100491 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100492}
493
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100494static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
495 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100496{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100497 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100498
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100499 for (i = 0; i < nr_ppas; i++) {
500 ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
501 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
502 }
503}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100504
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100505static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
506{
507 if (rqd->nr_ppas == 1) {
508 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
509 return;
510 }
511
512 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
513}
514
515static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
516{
517 if (rqd->nr_ppas == 1) {
518 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
519 return;
520 }
521
522 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100523}
524
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200525int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100526{
527 int ret = 0;
528
Matias Bjørling5cd90782016-07-07 09:54:17 +0200529 down_write(&nvm_tgtt_lock);
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100530 if (__nvm_find_target_type(tt->name))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100531 ret = -EEXIST;
532 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200533 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200534 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100535
536 return ret;
537}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200538EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100539
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200540void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100541{
542 if (!tt)
543 return;
544
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200545 down_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100546 list_del(&tt->list);
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200547 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100548}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200549EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100550
551void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
552 dma_addr_t *dma_handler)
553{
Javier González75b85642016-05-06 20:03:13 +0200554 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100555 dma_handler);
556}
557EXPORT_SYMBOL(nvm_dev_dma_alloc);
558
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100559void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100560{
Javier González75b85642016-05-06 20:03:13 +0200561 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100562}
563EXPORT_SYMBOL(nvm_dev_dma_free);
564
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100565static struct nvm_dev *nvm_find_nvm_dev(const char *name)
566{
567 struct nvm_dev *dev;
568
569 list_for_each_entry(dev, &nvm_devices, devices)
570 if (!strcmp(name, dev->name))
571 return dev;
572
573 return NULL;
574}
575
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200576static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
577 const struct ppa_addr *ppas, int nr_ppas)
578{
579 struct nvm_dev *dev = tgt_dev->parent;
580 struct nvm_geo *geo = &tgt_dev->geo;
581 int i, plane_cnt, pl_idx;
582 struct ppa_addr ppa;
583
584 if (geo->plane_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
585 rqd->nr_ppas = nr_ppas;
586 rqd->ppa_addr = ppas[0];
587
588 return 0;
589 }
590
591 rqd->nr_ppas = nr_ppas;
592 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
593 if (!rqd->ppa_list) {
594 pr_err("nvm: failed to allocate dma memory\n");
595 return -ENOMEM;
596 }
597
598 plane_cnt = geo->plane_mode;
599 rqd->nr_ppas *= plane_cnt;
600
601 for (i = 0; i < nr_ppas; i++) {
602 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
603 ppa = ppas[i];
604 ppa.g.pl = pl_idx;
605 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
606 }
607 }
608
609 return 0;
610}
611
612static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
613 struct nvm_rq *rqd)
614{
615 if (!rqd->ppa_list)
616 return;
617
618 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
619}
620
621
Javier González333ba052016-11-28 22:39:14 +0100622int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
623 int nr_ppas, int type)
624{
625 struct nvm_dev *dev = tgt_dev->parent;
626 struct nvm_rq rqd;
627 int ret;
628
629 if (nr_ppas > dev->ops->max_phys_sect) {
630 pr_err("nvm: unable to update all blocks atomically\n");
631 return -EINVAL;
632 }
633
634 memset(&rqd, 0, sizeof(struct nvm_rq));
635
Rakesh Panditef56b9c2017-10-13 14:46:30 +0200636 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100637 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100638
639 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
Javier González17912c42017-04-15 20:55:37 +0200640 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100641 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100642 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100643 return -EINVAL;
644 }
645
646 return 0;
647}
648EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
649
Javier Gonzáleza2790062016-11-28 22:39:12 +0100650int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
651{
652 struct nvm_dev *dev = tgt_dev->parent;
653
654 return dev->ops->max_phys_sect;
655}
656EXPORT_SYMBOL(nvm_max_phys_sects);
657
Javier González8e536242016-11-28 22:39:10 +0100658int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100659{
Javier González8e536242016-11-28 22:39:10 +0100660 struct nvm_dev *dev = tgt_dev->parent;
Javier González3e505af2017-06-26 11:57:10 +0200661 int ret;
Javier González8e536242016-11-28 22:39:10 +0100662
Matias Bjørlingade69e22017-01-31 13:17:09 +0100663 if (!dev->ops->submit_io)
664 return -ENODEV;
665
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100666 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100667
668 rqd->dev = tgt_dev;
Javier González3e505af2017-06-26 11:57:10 +0200669
670 /* In case of error, fail with right address format */
671 ret = dev->ops->submit_io(dev, rqd);
672 if (ret)
673 nvm_rq_dev_to_tgt(tgt_dev, rqd);
674 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100675}
676EXPORT_SYMBOL(nvm_submit_io);
677
Javier González1a94b2d2017-10-13 14:46:47 +0200678int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100679{
Javier González1a94b2d2017-10-13 14:46:47 +0200680 struct nvm_dev *dev = tgt_dev->parent;
681 int ret;
Javier González17912c42017-04-15 20:55:37 +0200682
Javier González1a94b2d2017-10-13 14:46:47 +0200683 if (!dev->ops->submit_io_sync)
684 return -ENODEV;
685
686 nvm_rq_tgt_to_dev(tgt_dev, rqd);
687
688 rqd->dev = tgt_dev;
689
690 /* In case of error, fail with right address format */
691 ret = dev->ops->submit_io_sync(dev, rqd);
692 nvm_rq_dev_to_tgt(tgt_dev, rqd);
693
694 return ret;
Javier González17912c42017-04-15 20:55:37 +0200695}
Javier González1a94b2d2017-10-13 14:46:47 +0200696EXPORT_SYMBOL(nvm_submit_io_sync);
Javier González17912c42017-04-15 20:55:37 +0200697
Matias Bjørling06894ef2017-01-31 13:17:17 +0100698void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100699{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100700 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100701
702 /* Convert address space */
703 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100704 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100705
Matias Bjørling06894ef2017-01-31 13:17:17 +0100706 if (rqd->end_io)
707 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100708}
709EXPORT_SYMBOL(nvm_end_io);
710
Matias Bjørling22e8c972016-05-06 20:02:58 +0200711/*
712 * folds a bad block list from its plane representation to its virtual
713 * block representation. The fold is done in place and reduced size is
714 * returned.
715 *
716 * If any of the planes status are bad or grown bad block, the virtual block
717 * is marked bad. If not bad, the first plane state acts as the block state.
718 */
719int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
720{
Javier González8e79b5c2016-11-28 22:39:06 +0100721 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200722 int blk, offset, pl, blktype;
723
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100724 if (nr_blks != geo->nr_chks * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200725 return -EINVAL;
726
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100727 for (blk = 0; blk < geo->nr_chks; blk++) {
Javier González8e79b5c2016-11-28 22:39:06 +0100728 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200729 blktype = blks[offset];
730
731 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100732 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200733 if (blks[offset + pl] &
734 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
735 blktype = blks[offset + pl];
736 break;
737 }
738 }
739
740 blks[blk] = blktype;
741 }
742
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100743 return geo->nr_chks;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200744}
745EXPORT_SYMBOL(nvm_bb_tbl_fold);
746
Javier González333ba052016-11-28 22:39:14 +0100747int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
748 u8 *blks)
749{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100750 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100751
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100752 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100753
754 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100755}
756EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
757
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100758static int nvm_core_init(struct nvm_dev *dev)
759{
760 struct nvm_id *id = &dev->identity;
Matias Bjørling19bd6fe2017-01-31 13:17:15 +0100761 struct nvm_id_group *grp = &id->grp;
Javier González8e79b5c2016-11-28 22:39:06 +0100762 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200763 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100764
Javier González8e79b5c2016-11-28 22:39:06 +0100765 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
766
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200767 if (grp->mtype != 0) {
768 pr_err("nvm: memory type not supported\n");
769 return -EINVAL;
770 }
771
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100772 /* Whole device values */
773 geo->nr_chnls = grp->num_ch;
774 geo->nr_luns = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100775
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100776 /* Generic device geometry values */
777 geo->ws_min = grp->ws_min;
778 geo->ws_opt = grp->ws_opt;
779 geo->ws_seq = grp->ws_seq;
780 geo->ws_per_chk = grp->ws_per_chk;
781 geo->nr_chks = grp->num_chk;
782 geo->sec_size = grp->csecs;
783 geo->oob_size = grp->sos;
784 geo->mccap = grp->mccap;
785 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
786
787 geo->sec_per_chk = grp->clba;
788 geo->sec_per_lun = geo->sec_per_chk * geo->nr_chks;
789 geo->all_luns = geo->nr_luns * geo->nr_chnls;
790
791 /* 1.2 spec device geometry values */
792 geo->plane_mode = 1 << geo->ws_seq;
793 geo->nr_planes = geo->ws_opt / geo->ws_min;
794 geo->sec_per_pg = geo->ws_min;
795 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
796
797 dev->total_secs = geo->all_luns * geo->sec_per_lun;
798 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100799 sizeof(unsigned long), GFP_KERNEL);
800 if (!dev->lun_map)
801 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200802
Matias Bjørlingade69e22017-01-31 13:17:09 +0100803 INIT_LIST_HEAD(&dev->area_list);
804 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100805 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100806 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100807
Matias Bjørlingade69e22017-01-31 13:17:09 +0100808 ret = nvm_register_map(dev);
809 if (ret)
810 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200811
Matias Bjørlingade69e22017-01-31 13:17:09 +0100812 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100813 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200814err_fmtype:
815 kfree(dev->lun_map);
816 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100817}
818
Javier González46b160c2017-04-15 20:55:46 +0200819static void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100820{
821 if (!dev)
822 return;
823
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200824 if (dev->dma_pool)
825 dev->ops->destroy_dma_pool(dev->dma_pool);
826
Javier González7a3de2b2017-04-15 20:55:39 +0200827 nvm_unregister_map(dev);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200828 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200829 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100830}
831
832static int nvm_init(struct nvm_dev *dev)
833{
Javier González8e79b5c2016-11-28 22:39:06 +0100834 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100835 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100836
Matias Bjørling16f26c32015-12-06 11:25:48 +0100837 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100838 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100839 goto err;
840 }
841
Matias Bjørling19bd6fe2017-01-31 13:17:15 +0100842 pr_debug("nvm: ver:%x nvm_vendor:%x\n",
843 dev->identity.ver_id, dev->identity.vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100844
845 if (dev->identity.ver_id != 1) {
846 pr_err("nvm: device not supported by kernel.");
847 goto err;
848 }
849
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100850 ret = nvm_core_init(dev);
851 if (ret) {
852 pr_err("nvm: could not initialize core structures.\n");
853 goto err;
854 }
855
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100856 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +0100857 dev->name, geo->sec_per_pg, geo->nr_planes,
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100858 geo->ws_per_chk, geo->nr_chks,
859 geo->all_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100860 return 0;
861err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100862 pr_err("nvm: failed to initialize nvm\n");
863 return ret;
864}
865
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200866struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100867{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200868 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
869}
870EXPORT_SYMBOL(nvm_alloc_dev);
871
872int nvm_register(struct nvm_dev *dev)
873{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100874 int ret;
875
Matias Bjørlingade69e22017-01-31 13:17:09 +0100876 if (!dev->q || !dev->ops)
877 return -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100878
Wenwei Taod1601472015-11-28 16:49:25 +0100879 if (dev->ops->max_phys_sect > 256) {
880 pr_info("nvm: max sectors supported is 256.\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +0100881 return -EINVAL;
Wenwei Taod1601472015-11-28 16:49:25 +0100882 }
883
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100884 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +0200885 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
886 if (!dev->dma_pool) {
887 pr_err("nvm: could not create dma pool\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +0100888 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100889 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100890 }
891
Matias Bjørlingade69e22017-01-31 13:17:09 +0100892 ret = nvm_init(dev);
893 if (ret)
894 goto err_init;
Matias Bjørlingb7692072016-01-12 07:49:38 +0100895
Matias Bjørling762796b2015-12-06 11:25:49 +0100896 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100897 down_write(&nvm_lock);
898 list_add(&dev->devices, &nvm_devices);
899 up_write(&nvm_lock);
900
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100901 return 0;
902err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100903 dev->ops->destroy_dma_pool(dev->dma_pool);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100904 return ret;
905}
906EXPORT_SYMBOL(nvm_register);
907
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200908void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100909{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100910 struct nvm_target *t, *tmp;
911
912 mutex_lock(&dev->mlock);
913 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
914 if (t->dev->parent != dev)
915 continue;
916 __nvm_remove_target(t);
917 }
918 mutex_unlock(&dev->mlock);
919
Wenwei Taod0a712c2015-11-28 16:49:28 +0100920 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100921 list_del(&dev->devices);
922 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +0100923
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100924 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100925}
926EXPORT_SYMBOL(nvm_unregister);
927
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100928static int __nvm_configure_create(struct nvm_ioctl_create *create)
929{
930 struct nvm_dev *dev;
931 struct nvm_ioctl_create_simple *s;
932
Wenwei Taod0a712c2015-11-28 16:49:28 +0100933 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100934 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100935 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200936
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100937 if (!dev) {
938 pr_err("nvm: device not found\n");
939 return -EINVAL;
940 }
941
942 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
943 pr_err("nvm: config type not valid\n");
944 return -EINVAL;
945 }
946 s = &create->conf.s;
947
Matias Bjørling6732c742017-02-15 16:25:33 +0100948 if (s->lun_begin == -1 && s->lun_end == -1) {
949 s->lun_begin = 0;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100950 s->lun_end = dev->geo.all_luns - 1;
Matias Bjørling6732c742017-02-15 16:25:33 +0100951 }
952
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100953 if (s->lun_begin > s->lun_end || s->lun_end >= dev->geo.all_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100954 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100955 s->lun_begin, s->lun_end, dev->geo.all_luns - 1);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100956 return -EINVAL;
957 }
958
Matias Bjørlingade69e22017-01-31 13:17:09 +0100959 return nvm_create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100960}
961
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100962static long nvm_ioctl_info(struct file *file, void __user *arg)
963{
964 struct nvm_ioctl_info *info;
965 struct nvm_tgt_type *tt;
966 int tgt_iter = 0;
967
968 if (!capable(CAP_SYS_ADMIN))
969 return -EPERM;
970
971 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
972 if (IS_ERR(info))
973 return -EFAULT;
974
975 info->version[0] = NVM_VERSION_MAJOR;
976 info->version[1] = NVM_VERSION_MINOR;
977 info->version[2] = NVM_VERSION_PATCH;
978
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200979 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200980 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100981 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
982
983 tgt->version[0] = tt->version[0];
984 tgt->version[1] = tt->version[1];
985 tgt->version[2] = tt->version[2];
986 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
987
988 tgt_iter++;
989 }
990
991 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200992 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100993
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100994 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
995 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100996 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100997 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100998
999 kfree(info);
1000 return 0;
1001}
1002
1003static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1004{
1005 struct nvm_ioctl_get_devices *devices;
1006 struct nvm_dev *dev;
1007 int i = 0;
1008
1009 if (!capable(CAP_SYS_ADMIN))
1010 return -EPERM;
1011
1012 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1013 if (!devices)
1014 return -ENOMEM;
1015
1016 down_write(&nvm_lock);
1017 list_for_each_entry(dev, &nvm_devices, devices) {
1018 struct nvm_ioctl_device_info *info = &devices->info[i];
1019
Javier González6eb08242017-04-15 20:55:49 +02001020 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001021
Matias Bjørlingade69e22017-01-31 13:17:09 +01001022 /* kept for compatibility */
1023 info->bmversion[0] = 1;
1024 info->bmversion[1] = 0;
1025 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001026 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001027 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001028
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001029 if (i > 31) {
1030 pr_err("nvm: max 31 devices can be reported.\n");
1031 break;
1032 }
1033 }
1034 up_write(&nvm_lock);
1035
1036 devices->nr_devices = i;
1037
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001038 if (copy_to_user(arg, devices,
1039 sizeof(struct nvm_ioctl_get_devices))) {
1040 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001041 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001042 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001043
1044 kfree(devices);
1045 return 0;
1046}
1047
1048static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1049{
1050 struct nvm_ioctl_create create;
1051
1052 if (!capable(CAP_SYS_ADMIN))
1053 return -EPERM;
1054
1055 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1056 return -EFAULT;
1057
1058 create.dev[DISK_NAME_LEN - 1] = '\0';
1059 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1060 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1061
1062 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001063 __u32 flags = create.flags;
1064
1065 /* Check for valid flags */
1066 if (flags & NVM_TARGET_FACTORY)
1067 flags &= ~NVM_TARGET_FACTORY;
1068
1069 if (flags) {
1070 pr_err("nvm: flag not supported\n");
1071 return -EINVAL;
1072 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001073 }
1074
1075 return __nvm_configure_create(&create);
1076}
1077
1078static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1079{
1080 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001081 struct nvm_dev *dev;
1082 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001083
1084 if (!capable(CAP_SYS_ADMIN))
1085 return -EPERM;
1086
1087 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1088 return -EFAULT;
1089
1090 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1091
1092 if (remove.flags != 0) {
1093 pr_err("nvm: no flags supported\n");
1094 return -EINVAL;
1095 }
1096
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001097 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001098 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001099 if (!ret)
1100 break;
1101 }
1102
1103 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001104}
1105
Matias Bjørlingade69e22017-01-31 13:17:09 +01001106/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001107static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1108{
1109 struct nvm_ioctl_dev_init init;
1110
1111 if (!capable(CAP_SYS_ADMIN))
1112 return -EPERM;
1113
1114 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1115 return -EFAULT;
1116
1117 if (init.flags != 0) {
1118 pr_err("nvm: no flags supported\n");
1119 return -EINVAL;
1120 }
1121
Matias Bjørlingade69e22017-01-31 13:17:09 +01001122 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001123}
1124
Matias Bjørlingade69e22017-01-31 13:17:09 +01001125/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001126static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1127{
1128 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001129
1130 if (!capable(CAP_SYS_ADMIN))
1131 return -EPERM;
1132
1133 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1134 return -EFAULT;
1135
1136 fact.dev[DISK_NAME_LEN - 1] = '\0';
1137
1138 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1139 return -EINVAL;
1140
Matias Bjørlingbf643182016-02-04 15:13:27 +01001141 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001142}
1143
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001144static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1145{
1146 void __user *argp = (void __user *)arg;
1147
1148 switch (cmd) {
1149 case NVM_INFO:
1150 return nvm_ioctl_info(file, argp);
1151 case NVM_GET_DEVICES:
1152 return nvm_ioctl_get_devices(file, argp);
1153 case NVM_DEV_CREATE:
1154 return nvm_ioctl_dev_create(file, argp);
1155 case NVM_DEV_REMOVE:
1156 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001157 case NVM_DEV_INIT:
1158 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001159 case NVM_DEV_FACTORY:
1160 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001161 }
1162 return 0;
1163}
1164
1165static const struct file_operations _ctl_fops = {
1166 .open = nonseekable_open,
1167 .unlocked_ioctl = nvm_ctl_ioctl,
1168 .owner = THIS_MODULE,
1169 .llseek = noop_llseek,
1170};
1171
1172static struct miscdevice _nvm_misc = {
1173 .minor = MISC_DYNAMIC_MINOR,
1174 .name = "lightnvm",
1175 .nodename = "lightnvm/control",
1176 .fops = &_ctl_fops,
1177};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001178builtin_misc_device(_nvm_misc);