blob: 6d6d2c12ff5bbb7af8289ef03b8c21b6dc896b86 [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
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200223static struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
224{
225 struct nvm_tgt_type *tmp, *tt = NULL;
226
227 if (lock)
228 down_write(&nvm_tgtt_lock);
229
230 list_for_each_entry(tmp, &nvm_tgt_types, list)
231 if (!strcmp(name, tmp->name)) {
232 tt = tmp;
233 break;
234 }
235
236 if (lock)
237 up_write(&nvm_tgtt_lock);
238 return tt;
239}
240
Matias Bjørlingade69e22017-01-31 13:17:09 +0100241static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
242{
243 struct nvm_ioctl_create_simple *s = &create->conf.s;
244 struct request_queue *tqueue;
245 struct gendisk *tdisk;
246 struct nvm_tgt_type *tt;
247 struct nvm_target *t;
248 struct nvm_tgt_dev *tgt_dev;
249 void *targetdata;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300250 int ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100251
252 tt = nvm_find_target_type(create->tgttype, 1);
253 if (!tt) {
254 pr_err("nvm: target type %s not found\n", create->tgttype);
255 return -EINVAL;
256 }
257
258 mutex_lock(&dev->mlock);
259 t = nvm_find_target(dev, create->tgtname);
260 if (t) {
261 pr_err("nvm: target name already exists.\n");
262 mutex_unlock(&dev->mlock);
263 return -EINVAL;
264 }
265 mutex_unlock(&dev->mlock);
266
Rakesh Pandit12e9a6d62017-06-27 14:55:33 +0300267 ret = nvm_reserve_luns(dev, s->lun_begin, s->lun_end);
268 if (ret)
269 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100270
271 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300272 if (!t) {
273 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100274 goto err_reserve;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300275 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100276
277 tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end);
278 if (!tgt_dev) {
279 pr_err("nvm: could not create target device\n");
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300280 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100281 goto err_t;
282 }
283
Matias Bjørlingade69e22017-01-31 13:17:09 +0100284 tdisk = alloc_disk(0);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300285 if (!tdisk) {
286 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200287 goto err_dev;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300288 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200289
290 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300291 if (!tqueue) {
292 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200293 goto err_disk;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300294 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200295 blk_queue_make_request(tqueue, tt->make_rq);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100296
Javier González6eb08242017-04-15 20:55:49 +0200297 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100298 tdisk->flags = GENHD_FL_EXT_DEVT;
299 tdisk->major = 0;
300 tdisk->first_minor = 0;
301 tdisk->fops = &nvm_fops;
302 tdisk->queue = tqueue;
303
Javier González4af3f752017-04-15 20:55:45 +0200304 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300305 if (IS_ERR(targetdata)) {
306 ret = PTR_ERR(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100307 goto err_init;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300308 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100309
310 tdisk->private_data = targetdata;
311 tqueue->queuedata = targetdata;
312
313 blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
314
315 set_capacity(tdisk, tt->capacity(targetdata));
316 add_disk(tdisk);
317
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300318 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
319 ret = -ENOMEM;
Javier González9a69b0e2017-01-31 13:17:20 +0100320 goto err_sysfs;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300321 }
Javier González9a69b0e2017-01-31 13:17:20 +0100322
Matias Bjørlingade69e22017-01-31 13:17:09 +0100323 t->type = tt;
324 t->disk = tdisk;
325 t->dev = tgt_dev;
326
327 mutex_lock(&dev->mlock);
328 list_add_tail(&t->list, &dev->targets);
329 mutex_unlock(&dev->mlock);
330
Rakesh Pandit90014822017-10-13 14:45:50 +0200331 __module_get(tt->owner);
332
Matias Bjørlingade69e22017-01-31 13:17:09 +0100333 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100334err_sysfs:
335 if (tt->exit)
336 tt->exit(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100337err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100338 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600339 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200340err_disk:
341 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100342err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200343 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100344err_t:
345 kfree(t);
346err_reserve:
347 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300348 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100349}
350
351static void __nvm_remove_target(struct nvm_target *t)
352{
353 struct nvm_tgt_type *tt = t->type;
354 struct gendisk *tdisk = t->disk;
355 struct request_queue *q = tdisk->queue;
356
357 del_gendisk(tdisk);
358 blk_cleanup_queue(q);
359
Javier González9a69b0e2017-01-31 13:17:20 +0100360 if (tt->sysfs_exit)
361 tt->sysfs_exit(tdisk);
362
Matias Bjørlingade69e22017-01-31 13:17:09 +0100363 if (tt->exit)
364 tt->exit(tdisk->private_data);
365
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200366 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100367 put_disk(tdisk);
Rakesh Pandit90014822017-10-13 14:45:50 +0200368 module_put(t->type->owner);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100369
370 list_del(&t->list);
371 kfree(t);
372}
373
374/**
375 * nvm_remove_tgt - Removes a target from the media manager
376 * @dev: device
377 * @remove: ioctl structure with target name to remove.
378 *
379 * Returns:
380 * 0: on success
381 * 1: on not found
382 * <0: on error
383 */
384static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
385{
386 struct nvm_target *t;
387
388 mutex_lock(&dev->mlock);
389 t = nvm_find_target(dev, remove->tgtname);
390 if (!t) {
391 mutex_unlock(&dev->mlock);
392 return 1;
393 }
394 __nvm_remove_target(t);
395 mutex_unlock(&dev->mlock);
396
397 return 0;
398}
399
400static int nvm_register_map(struct nvm_dev *dev)
401{
402 struct nvm_dev_map *rmap;
403 int i, j;
404
405 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
406 if (!rmap)
407 goto err_rmap;
408
409 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
410 GFP_KERNEL);
411 if (!rmap->chnls)
412 goto err_chnls;
413
414 for (i = 0; i < dev->geo.nr_chnls; i++) {
415 struct nvm_ch_map *ch_rmap;
416 int *lun_roffs;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100417 int luns_in_chnl = dev->geo.nr_luns;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100418
419 ch_rmap = &rmap->chnls[i];
420
421 ch_rmap->ch_off = -1;
422 ch_rmap->nr_luns = luns_in_chnl;
423
424 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
425 if (!lun_roffs)
426 goto err_ch;
427
428 for (j = 0; j < luns_in_chnl; j++)
429 lun_roffs[j] = -1;
430
431 ch_rmap->lun_offs = lun_roffs;
432 }
433
434 dev->rmap = rmap;
435
436 return 0;
437err_ch:
438 while (--i >= 0)
439 kfree(rmap->chnls[i].lun_offs);
440err_chnls:
441 kfree(rmap);
442err_rmap:
443 return -ENOMEM;
444}
445
Javier González7a3de2b2017-04-15 20:55:39 +0200446static void nvm_unregister_map(struct nvm_dev *dev)
447{
448 struct nvm_dev_map *rmap = dev->rmap;
449 int i;
450
451 for (i = 0; i < dev->geo.nr_chnls; i++)
452 kfree(rmap->chnls[i].lun_offs);
453
454 kfree(rmap->chnls);
455 kfree(rmap);
456}
457
Matias Bjørling61a561d2017-01-31 13:17:13 +0100458static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100459{
460 struct nvm_dev_map *dev_map = tgt_dev->map;
461 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
462 int lun_off = ch_map->lun_offs[p->g.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100463
464 p->g.ch += ch_map->ch_off;
465 p->g.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100466}
467
Matias Bjørling61a561d2017-01-31 13:17:13 +0100468static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100469{
470 struct nvm_dev *dev = tgt_dev->parent;
471 struct nvm_dev_map *dev_rmap = dev->rmap;
472 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
473 int lun_roff = ch_rmap->lun_offs[p->g.lun];
474
475 p->g.ch -= ch_rmap->ch_off;
476 p->g.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100477}
478
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100479static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
480 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100481{
482 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100483
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100484 for (i = 0; i < nr_ppas; i++) {
485 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
486 ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100487 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100488}
489
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100490static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
491 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100492{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100493 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100494
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100495 for (i = 0; i < nr_ppas; i++) {
496 ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
497 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
498 }
499}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100500
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100501static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
502{
503 if (rqd->nr_ppas == 1) {
504 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
505 return;
506 }
507
508 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
509}
510
511static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
512{
513 if (rqd->nr_ppas == 1) {
514 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
515 return;
516 }
517
518 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100519}
520
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200521int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100522{
523 int ret = 0;
524
Matias Bjørling5cd90782016-07-07 09:54:17 +0200525 down_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200526 if (nvm_find_target_type(tt->name, 0))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100527 ret = -EEXIST;
528 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200529 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200530 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100531
532 return ret;
533}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200534EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100535
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200536void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100537{
538 if (!tt)
539 return;
540
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200541 down_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100542 list_del(&tt->list);
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200543 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100544}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200545EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100546
547void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
548 dma_addr_t *dma_handler)
549{
Javier González75b85642016-05-06 20:03:13 +0200550 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100551 dma_handler);
552}
553EXPORT_SYMBOL(nvm_dev_dma_alloc);
554
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100555void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100556{
Javier González75b85642016-05-06 20:03:13 +0200557 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100558}
559EXPORT_SYMBOL(nvm_dev_dma_free);
560
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100561static struct nvm_dev *nvm_find_nvm_dev(const char *name)
562{
563 struct nvm_dev *dev;
564
565 list_for_each_entry(dev, &nvm_devices, devices)
566 if (!strcmp(name, dev->name))
567 return dev;
568
569 return NULL;
570}
571
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200572static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
573 const struct ppa_addr *ppas, int nr_ppas)
574{
575 struct nvm_dev *dev = tgt_dev->parent;
576 struct nvm_geo *geo = &tgt_dev->geo;
577 int i, plane_cnt, pl_idx;
578 struct ppa_addr ppa;
579
580 if (geo->plane_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
581 rqd->nr_ppas = nr_ppas;
582 rqd->ppa_addr = ppas[0];
583
584 return 0;
585 }
586
587 rqd->nr_ppas = nr_ppas;
588 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
589 if (!rqd->ppa_list) {
590 pr_err("nvm: failed to allocate dma memory\n");
591 return -ENOMEM;
592 }
593
594 plane_cnt = geo->plane_mode;
595 rqd->nr_ppas *= plane_cnt;
596
597 for (i = 0; i < nr_ppas; i++) {
598 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
599 ppa = ppas[i];
600 ppa.g.pl = pl_idx;
601 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
602 }
603 }
604
605 return 0;
606}
607
608static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
609 struct nvm_rq *rqd)
610{
611 if (!rqd->ppa_list)
612 return;
613
614 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
615}
616
617
Javier González333ba052016-11-28 22:39:14 +0100618int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
619 int nr_ppas, int type)
620{
621 struct nvm_dev *dev = tgt_dev->parent;
622 struct nvm_rq rqd;
623 int ret;
624
625 if (nr_ppas > dev->ops->max_phys_sect) {
626 pr_err("nvm: unable to update all blocks atomically\n");
627 return -EINVAL;
628 }
629
630 memset(&rqd, 0, sizeof(struct nvm_rq));
631
Rakesh Panditef56b9c2017-10-13 14:46:30 +0200632 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100633 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100634
635 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
Javier González17912c42017-04-15 20:55:37 +0200636 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100637 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100638 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100639 return -EINVAL;
640 }
641
642 return 0;
643}
644EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
645
Javier Gonzáleza2790062016-11-28 22:39:12 +0100646int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
647{
648 struct nvm_dev *dev = tgt_dev->parent;
649
650 return dev->ops->max_phys_sect;
651}
652EXPORT_SYMBOL(nvm_max_phys_sects);
653
Javier González8e536242016-11-28 22:39:10 +0100654int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100655{
Javier González8e536242016-11-28 22:39:10 +0100656 struct nvm_dev *dev = tgt_dev->parent;
Javier González3e505af2017-06-26 11:57:10 +0200657 int ret;
Javier González8e536242016-11-28 22:39:10 +0100658
Matias Bjørlingade69e22017-01-31 13:17:09 +0100659 if (!dev->ops->submit_io)
660 return -ENODEV;
661
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100662 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100663
664 rqd->dev = tgt_dev;
Javier González3e505af2017-06-26 11:57:10 +0200665
666 /* In case of error, fail with right address format */
667 ret = dev->ops->submit_io(dev, rqd);
668 if (ret)
669 nvm_rq_dev_to_tgt(tgt_dev, rqd);
670 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100671}
672EXPORT_SYMBOL(nvm_submit_io);
673
Javier González1a94b2d2017-10-13 14:46:47 +0200674int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100675{
Javier González1a94b2d2017-10-13 14:46:47 +0200676 struct nvm_dev *dev = tgt_dev->parent;
677 int ret;
Javier González17912c42017-04-15 20:55:37 +0200678
Javier González1a94b2d2017-10-13 14:46:47 +0200679 if (!dev->ops->submit_io_sync)
680 return -ENODEV;
681
682 nvm_rq_tgt_to_dev(tgt_dev, rqd);
683
684 rqd->dev = tgt_dev;
685
686 /* In case of error, fail with right address format */
687 ret = dev->ops->submit_io_sync(dev, rqd);
688 nvm_rq_dev_to_tgt(tgt_dev, rqd);
689
690 return ret;
Javier González17912c42017-04-15 20:55:37 +0200691}
Javier González1a94b2d2017-10-13 14:46:47 +0200692EXPORT_SYMBOL(nvm_submit_io_sync);
Javier González17912c42017-04-15 20:55:37 +0200693
Matias Bjørling06894ef2017-01-31 13:17:17 +0100694void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100695{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100696 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100697
698 /* Convert address space */
699 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100700 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100701
Matias Bjørling06894ef2017-01-31 13:17:17 +0100702 if (rqd->end_io)
703 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100704}
705EXPORT_SYMBOL(nvm_end_io);
706
Matias Bjørling22e8c972016-05-06 20:02:58 +0200707/*
708 * folds a bad block list from its plane representation to its virtual
709 * block representation. The fold is done in place and reduced size is
710 * returned.
711 *
712 * If any of the planes status are bad or grown bad block, the virtual block
713 * is marked bad. If not bad, the first plane state acts as the block state.
714 */
715int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
716{
Javier González8e79b5c2016-11-28 22:39:06 +0100717 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200718 int blk, offset, pl, blktype;
719
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100720 if (nr_blks != geo->nr_chks * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200721 return -EINVAL;
722
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100723 for (blk = 0; blk < geo->nr_chks; blk++) {
Javier González8e79b5c2016-11-28 22:39:06 +0100724 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200725 blktype = blks[offset];
726
727 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100728 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200729 if (blks[offset + pl] &
730 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
731 blktype = blks[offset + pl];
732 break;
733 }
734 }
735
736 blks[blk] = blktype;
737 }
738
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100739 return geo->nr_chks;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200740}
741EXPORT_SYMBOL(nvm_bb_tbl_fold);
742
Javier González333ba052016-11-28 22:39:14 +0100743int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
744 u8 *blks)
745{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100746 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100747
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100748 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100749
750 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100751}
752EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
753
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100754static int nvm_core_init(struct nvm_dev *dev)
755{
756 struct nvm_id *id = &dev->identity;
Matias Bjørling19bd6fe2017-01-31 13:17:15 +0100757 struct nvm_id_group *grp = &id->grp;
Javier González8e79b5c2016-11-28 22:39:06 +0100758 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200759 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100760
Javier González8e79b5c2016-11-28 22:39:06 +0100761 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
762
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200763 if (grp->mtype != 0) {
764 pr_err("nvm: memory type not supported\n");
765 return -EINVAL;
766 }
767
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100768 /* Whole device values */
769 geo->nr_chnls = grp->num_ch;
770 geo->nr_luns = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100771
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100772 /* Generic device geometry values */
773 geo->ws_min = grp->ws_min;
774 geo->ws_opt = grp->ws_opt;
775 geo->ws_seq = grp->ws_seq;
776 geo->ws_per_chk = grp->ws_per_chk;
777 geo->nr_chks = grp->num_chk;
778 geo->sec_size = grp->csecs;
779 geo->oob_size = grp->sos;
780 geo->mccap = grp->mccap;
781 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
782
783 geo->sec_per_chk = grp->clba;
784 geo->sec_per_lun = geo->sec_per_chk * geo->nr_chks;
785 geo->all_luns = geo->nr_luns * geo->nr_chnls;
786
787 /* 1.2 spec device geometry values */
788 geo->plane_mode = 1 << geo->ws_seq;
789 geo->nr_planes = geo->ws_opt / geo->ws_min;
790 geo->sec_per_pg = geo->ws_min;
791 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
792
793 dev->total_secs = geo->all_luns * geo->sec_per_lun;
794 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100795 sizeof(unsigned long), GFP_KERNEL);
796 if (!dev->lun_map)
797 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200798
Matias Bjørlingade69e22017-01-31 13:17:09 +0100799 INIT_LIST_HEAD(&dev->area_list);
800 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100801 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100802 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100803
Matias Bjørlingade69e22017-01-31 13:17:09 +0100804 ret = nvm_register_map(dev);
805 if (ret)
806 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200807
Matias Bjørlingade69e22017-01-31 13:17:09 +0100808 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100809 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200810err_fmtype:
811 kfree(dev->lun_map);
812 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100813}
814
Javier González46b160c2017-04-15 20:55:46 +0200815static void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100816{
817 if (!dev)
818 return;
819
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200820 if (dev->dma_pool)
821 dev->ops->destroy_dma_pool(dev->dma_pool);
822
Javier González7a3de2b2017-04-15 20:55:39 +0200823 nvm_unregister_map(dev);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200824 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200825 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100826}
827
828static int nvm_init(struct nvm_dev *dev)
829{
Javier González8e79b5c2016-11-28 22:39:06 +0100830 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100831 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100832
Matias Bjørling16f26c32015-12-06 11:25:48 +0100833 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100834 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100835 goto err;
836 }
837
Matias Bjørling19bd6fe2017-01-31 13:17:15 +0100838 pr_debug("nvm: ver:%x nvm_vendor:%x\n",
839 dev->identity.ver_id, dev->identity.vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100840
841 if (dev->identity.ver_id != 1) {
842 pr_err("nvm: device not supported by kernel.");
843 goto err;
844 }
845
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100846 ret = nvm_core_init(dev);
847 if (ret) {
848 pr_err("nvm: could not initialize core structures.\n");
849 goto err;
850 }
851
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100852 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +0100853 dev->name, geo->sec_per_pg, geo->nr_planes,
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100854 geo->ws_per_chk, geo->nr_chks,
855 geo->all_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100856 return 0;
857err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100858 pr_err("nvm: failed to initialize nvm\n");
859 return ret;
860}
861
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200862struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100863{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200864 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
865}
866EXPORT_SYMBOL(nvm_alloc_dev);
867
868int nvm_register(struct nvm_dev *dev)
869{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100870 int ret;
871
Matias Bjørlingade69e22017-01-31 13:17:09 +0100872 if (!dev->q || !dev->ops)
873 return -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100874
Wenwei Taod1601472015-11-28 16:49:25 +0100875 if (dev->ops->max_phys_sect > 256) {
876 pr_info("nvm: max sectors supported is 256.\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +0100877 return -EINVAL;
Wenwei Taod1601472015-11-28 16:49:25 +0100878 }
879
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100880 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +0200881 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
882 if (!dev->dma_pool) {
883 pr_err("nvm: could not create dma pool\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +0100884 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100885 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100886 }
887
Matias Bjørlingade69e22017-01-31 13:17:09 +0100888 ret = nvm_init(dev);
889 if (ret)
890 goto err_init;
Matias Bjørlingb7692072016-01-12 07:49:38 +0100891
Matias Bjørling762796b2015-12-06 11:25:49 +0100892 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100893 down_write(&nvm_lock);
894 list_add(&dev->devices, &nvm_devices);
895 up_write(&nvm_lock);
896
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100897 return 0;
898err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100899 dev->ops->destroy_dma_pool(dev->dma_pool);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100900 return ret;
901}
902EXPORT_SYMBOL(nvm_register);
903
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200904void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100905{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100906 struct nvm_target *t, *tmp;
907
908 mutex_lock(&dev->mlock);
909 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
910 if (t->dev->parent != dev)
911 continue;
912 __nvm_remove_target(t);
913 }
914 mutex_unlock(&dev->mlock);
915
Wenwei Taod0a712c2015-11-28 16:49:28 +0100916 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100917 list_del(&dev->devices);
918 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +0100919
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100920 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100921}
922EXPORT_SYMBOL(nvm_unregister);
923
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100924static int __nvm_configure_create(struct nvm_ioctl_create *create)
925{
926 struct nvm_dev *dev;
927 struct nvm_ioctl_create_simple *s;
928
Wenwei Taod0a712c2015-11-28 16:49:28 +0100929 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100930 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100931 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200932
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100933 if (!dev) {
934 pr_err("nvm: device not found\n");
935 return -EINVAL;
936 }
937
938 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
939 pr_err("nvm: config type not valid\n");
940 return -EINVAL;
941 }
942 s = &create->conf.s;
943
Matias Bjørling6732c742017-02-15 16:25:33 +0100944 if (s->lun_begin == -1 && s->lun_end == -1) {
945 s->lun_begin = 0;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100946 s->lun_end = dev->geo.all_luns - 1;
Matias Bjørling6732c742017-02-15 16:25:33 +0100947 }
948
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100949 if (s->lun_begin > s->lun_end || s->lun_end >= dev->geo.all_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100950 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100951 s->lun_begin, s->lun_end, dev->geo.all_luns - 1);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100952 return -EINVAL;
953 }
954
Matias Bjørlingade69e22017-01-31 13:17:09 +0100955 return nvm_create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100956}
957
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100958static long nvm_ioctl_info(struct file *file, void __user *arg)
959{
960 struct nvm_ioctl_info *info;
961 struct nvm_tgt_type *tt;
962 int tgt_iter = 0;
963
964 if (!capable(CAP_SYS_ADMIN))
965 return -EPERM;
966
967 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
968 if (IS_ERR(info))
969 return -EFAULT;
970
971 info->version[0] = NVM_VERSION_MAJOR;
972 info->version[1] = NVM_VERSION_MINOR;
973 info->version[2] = NVM_VERSION_PATCH;
974
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200975 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200976 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100977 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
978
979 tgt->version[0] = tt->version[0];
980 tgt->version[1] = tt->version[1];
981 tgt->version[2] = tt->version[2];
982 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
983
984 tgt_iter++;
985 }
986
987 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200988 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100989
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100990 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
991 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100992 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100993 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100994
995 kfree(info);
996 return 0;
997}
998
999static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1000{
1001 struct nvm_ioctl_get_devices *devices;
1002 struct nvm_dev *dev;
1003 int i = 0;
1004
1005 if (!capable(CAP_SYS_ADMIN))
1006 return -EPERM;
1007
1008 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1009 if (!devices)
1010 return -ENOMEM;
1011
1012 down_write(&nvm_lock);
1013 list_for_each_entry(dev, &nvm_devices, devices) {
1014 struct nvm_ioctl_device_info *info = &devices->info[i];
1015
Javier González6eb08242017-04-15 20:55:49 +02001016 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001017
Matias Bjørlingade69e22017-01-31 13:17:09 +01001018 /* kept for compatibility */
1019 info->bmversion[0] = 1;
1020 info->bmversion[1] = 0;
1021 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001022 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001023 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001024
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001025 if (i > 31) {
1026 pr_err("nvm: max 31 devices can be reported.\n");
1027 break;
1028 }
1029 }
1030 up_write(&nvm_lock);
1031
1032 devices->nr_devices = i;
1033
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001034 if (copy_to_user(arg, devices,
1035 sizeof(struct nvm_ioctl_get_devices))) {
1036 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001037 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001038 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001039
1040 kfree(devices);
1041 return 0;
1042}
1043
1044static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1045{
1046 struct nvm_ioctl_create create;
1047
1048 if (!capable(CAP_SYS_ADMIN))
1049 return -EPERM;
1050
1051 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1052 return -EFAULT;
1053
1054 create.dev[DISK_NAME_LEN - 1] = '\0';
1055 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1056 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1057
1058 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001059 __u32 flags = create.flags;
1060
1061 /* Check for valid flags */
1062 if (flags & NVM_TARGET_FACTORY)
1063 flags &= ~NVM_TARGET_FACTORY;
1064
1065 if (flags) {
1066 pr_err("nvm: flag not supported\n");
1067 return -EINVAL;
1068 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001069 }
1070
1071 return __nvm_configure_create(&create);
1072}
1073
1074static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1075{
1076 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001077 struct nvm_dev *dev;
1078 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001079
1080 if (!capable(CAP_SYS_ADMIN))
1081 return -EPERM;
1082
1083 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1084 return -EFAULT;
1085
1086 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1087
1088 if (remove.flags != 0) {
1089 pr_err("nvm: no flags supported\n");
1090 return -EINVAL;
1091 }
1092
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001093 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001094 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001095 if (!ret)
1096 break;
1097 }
1098
1099 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001100}
1101
Matias Bjørlingade69e22017-01-31 13:17:09 +01001102/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001103static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1104{
1105 struct nvm_ioctl_dev_init init;
1106
1107 if (!capable(CAP_SYS_ADMIN))
1108 return -EPERM;
1109
1110 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1111 return -EFAULT;
1112
1113 if (init.flags != 0) {
1114 pr_err("nvm: no flags supported\n");
1115 return -EINVAL;
1116 }
1117
Matias Bjørlingade69e22017-01-31 13:17:09 +01001118 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001119}
1120
Matias Bjørlingade69e22017-01-31 13:17:09 +01001121/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001122static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1123{
1124 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001125
1126 if (!capable(CAP_SYS_ADMIN))
1127 return -EPERM;
1128
1129 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1130 return -EFAULT;
1131
1132 fact.dev[DISK_NAME_LEN - 1] = '\0';
1133
1134 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1135 return -EINVAL;
1136
Matias Bjørlingbf643182016-02-04 15:13:27 +01001137 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001138}
1139
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001140static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1141{
1142 void __user *argp = (void __user *)arg;
1143
1144 switch (cmd) {
1145 case NVM_INFO:
1146 return nvm_ioctl_info(file, argp);
1147 case NVM_GET_DEVICES:
1148 return nvm_ioctl_get_devices(file, argp);
1149 case NVM_DEV_CREATE:
1150 return nvm_ioctl_dev_create(file, argp);
1151 case NVM_DEV_REMOVE:
1152 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001153 case NVM_DEV_INIT:
1154 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001155 case NVM_DEV_FACTORY:
1156 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001157 }
1158 return 0;
1159}
1160
1161static const struct file_operations _ctl_fops = {
1162 .open = nonseekable_open,
1163 .unlocked_ioctl = nvm_ctl_ioctl,
1164 .owner = THIS_MODULE,
1165 .llseek = noop_llseek,
1166};
1167
1168static struct miscdevice _nvm_misc = {
1169 .minor = MISC_DYNAMIC_MINOR,
1170 .name = "lightnvm",
1171 .nodename = "lightnvm/control",
1172 .fops = &_ctl_fops,
1173};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001174builtin_misc_device(_nvm_misc);