blob: 0e5f77234c79aa0832dac85fd772a503195efcc3 [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
48struct nvm_area {
49 struct list_head list;
50 sector_t begin;
51 sector_t end; /* end is excluded */
52};
53
Matias Bjørlingade69e22017-01-31 13:17:09 +010054static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
55{
56 struct nvm_target *tgt;
57
58 list_for_each_entry(tgt, &dev->targets, list)
59 if (!strcmp(name, tgt->disk->disk_name))
60 return tgt;
61
62 return NULL;
63}
64
65static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
66{
67 int i;
68
69 for (i = lun_begin; i <= lun_end; i++) {
70 if (test_and_set_bit(i, dev->lun_map)) {
71 pr_err("nvm: lun %d already allocated\n", i);
72 goto err;
73 }
74 }
75
76 return 0;
77err:
Javier González507f7d62017-05-03 11:19:05 +020078 while (--i >= lun_begin)
Matias Bjørlingade69e22017-01-31 13:17:09 +010079 clear_bit(i, dev->lun_map);
80
81 return -EBUSY;
82}
83
84static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
85 int lun_end)
86{
87 int i;
88
89 for (i = lun_begin; i <= lun_end; i++)
90 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
91}
92
Javier Gonzálezedee1bd2017-04-15 20:55:42 +020093static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
Matias Bjørlingade69e22017-01-31 13:17:09 +010094{
95 struct nvm_dev *dev = tgt_dev->parent;
96 struct nvm_dev_map *dev_map = tgt_dev->map;
97 int i, j;
98
99 for (i = 0; i < dev_map->nr_chnls; i++) {
100 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
101 int *lun_offs = ch_map->lun_offs;
102 int ch = i + ch_map->ch_off;
103
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200104 if (clear) {
105 for (j = 0; j < ch_map->nr_luns; j++) {
106 int lun = j + lun_offs[j];
107 int lunid = (ch * dev->geo.luns_per_chnl) + lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100108
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200109 WARN_ON(!test_and_clear_bit(lunid,
110 dev->lun_map));
111 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100112 }
113
114 kfree(ch_map->lun_offs);
115 }
116
117 kfree(dev_map->chnls);
118 kfree(dev_map);
119
120 kfree(tgt_dev->luns);
121 kfree(tgt_dev);
122}
123
124static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
125 int lun_begin, int lun_end)
126{
127 struct nvm_tgt_dev *tgt_dev = NULL;
128 struct nvm_dev_map *dev_rmap = dev->rmap;
129 struct nvm_dev_map *dev_map;
130 struct ppa_addr *luns;
131 int nr_luns = lun_end - lun_begin + 1;
132 int luns_left = nr_luns;
133 int nr_chnls = nr_luns / dev->geo.luns_per_chnl;
134 int nr_chnls_mod = nr_luns % dev->geo.luns_per_chnl;
135 int bch = lun_begin / dev->geo.luns_per_chnl;
136 int blun = lun_begin % dev->geo.luns_per_chnl;
137 int lunid = 0;
138 int lun_balanced = 1;
139 int prev_nr_luns;
140 int i, j;
141
Matias Bjørlingade69e22017-01-31 13:17:09 +0100142 nr_chnls = (nr_chnls_mod == 0) ? nr_chnls : nr_chnls + 1;
143
144 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
145 if (!dev_map)
146 goto err_dev;
147
148 dev_map->chnls = kcalloc(nr_chnls, sizeof(struct nvm_ch_map),
149 GFP_KERNEL);
150 if (!dev_map->chnls)
151 goto err_chnls;
152
153 luns = kcalloc(nr_luns, sizeof(struct ppa_addr), GFP_KERNEL);
154 if (!luns)
155 goto err_luns;
156
157 prev_nr_luns = (luns_left > dev->geo.luns_per_chnl) ?
158 dev->geo.luns_per_chnl : luns_left;
159 for (i = 0; i < nr_chnls; i++) {
160 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
161 int *lun_roffs = ch_rmap->lun_offs;
162 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
163 int *lun_offs;
164 int luns_in_chnl = (luns_left > dev->geo.luns_per_chnl) ?
165 dev->geo.luns_per_chnl : luns_left;
166
167 if (lun_balanced && prev_nr_luns != luns_in_chnl)
168 lun_balanced = 0;
169
170 ch_map->ch_off = ch_rmap->ch_off = bch;
171 ch_map->nr_luns = luns_in_chnl;
172
173 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
174 if (!lun_offs)
175 goto err_ch;
176
177 for (j = 0; j < luns_in_chnl; j++) {
178 luns[lunid].ppa = 0;
179 luns[lunid].g.ch = i;
180 luns[lunid++].g.lun = j;
181
182 lun_offs[j] = blun;
183 lun_roffs[j + blun] = blun;
184 }
185
186 ch_map->lun_offs = lun_offs;
187
188 /* when starting a new channel, lun offset is reset */
189 blun = 0;
190 luns_left -= luns_in_chnl;
191 }
192
193 dev_map->nr_chnls = nr_chnls;
194
195 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
196 if (!tgt_dev)
197 goto err_ch;
198
199 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
200 /* Target device only owns a portion of the physical device */
201 tgt_dev->geo.nr_chnls = nr_chnls;
202 tgt_dev->geo.nr_luns = nr_luns;
203 tgt_dev->geo.luns_per_chnl = (lun_balanced) ? prev_nr_luns : -1;
204 tgt_dev->total_secs = nr_luns * tgt_dev->geo.sec_per_lun;
205 tgt_dev->q = dev->q;
206 tgt_dev->map = dev_map;
207 tgt_dev->luns = luns;
208 memcpy(&tgt_dev->identity, &dev->identity, sizeof(struct nvm_id));
209
210 tgt_dev->parent = dev;
211
212 return tgt_dev;
213err_ch:
Javier González507f7d62017-05-03 11:19:05 +0200214 while (--i >= 0)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100215 kfree(dev_map->chnls[i].lun_offs);
216 kfree(luns);
217err_luns:
218 kfree(dev_map->chnls);
219err_chnls:
220 kfree(dev_map);
221err_dev:
222 return tgt_dev;
223}
224
225static const struct block_device_operations nvm_fops = {
226 .owner = THIS_MODULE,
227};
228
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200229static struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
230{
231 struct nvm_tgt_type *tmp, *tt = NULL;
232
233 if (lock)
234 down_write(&nvm_tgtt_lock);
235
236 list_for_each_entry(tmp, &nvm_tgt_types, list)
237 if (!strcmp(name, tmp->name)) {
238 tt = tmp;
239 break;
240 }
241
242 if (lock)
243 up_write(&nvm_tgtt_lock);
244 return tt;
245}
246
Matias Bjørlingade69e22017-01-31 13:17:09 +0100247static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
248{
249 struct nvm_ioctl_create_simple *s = &create->conf.s;
250 struct request_queue *tqueue;
251 struct gendisk *tdisk;
252 struct nvm_tgt_type *tt;
253 struct nvm_target *t;
254 struct nvm_tgt_dev *tgt_dev;
255 void *targetdata;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300256 int ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100257
258 tt = nvm_find_target_type(create->tgttype, 1);
259 if (!tt) {
260 pr_err("nvm: target type %s not found\n", create->tgttype);
261 return -EINVAL;
262 }
263
264 mutex_lock(&dev->mlock);
265 t = nvm_find_target(dev, create->tgtname);
266 if (t) {
267 pr_err("nvm: target name already exists.\n");
268 mutex_unlock(&dev->mlock);
269 return -EINVAL;
270 }
271 mutex_unlock(&dev->mlock);
272
Rakesh Pandit12e9a6d62017-06-27 14:55:33 +0300273 ret = nvm_reserve_luns(dev, s->lun_begin, s->lun_end);
274 if (ret)
275 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100276
277 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300278 if (!t) {
279 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100280 goto err_reserve;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300281 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100282
283 tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end);
284 if (!tgt_dev) {
285 pr_err("nvm: could not create target device\n");
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300286 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100287 goto err_t;
288 }
289
Matias Bjørlingade69e22017-01-31 13:17:09 +0100290 tdisk = alloc_disk(0);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300291 if (!tdisk) {
292 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200293 goto err_dev;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300294 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200295
296 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300297 if (!tqueue) {
298 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200299 goto err_disk;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300300 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200301 blk_queue_make_request(tqueue, tt->make_rq);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100302
Javier González6eb08242017-04-15 20:55:49 +0200303 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100304 tdisk->flags = GENHD_FL_EXT_DEVT;
305 tdisk->major = 0;
306 tdisk->first_minor = 0;
307 tdisk->fops = &nvm_fops;
308 tdisk->queue = tqueue;
309
Javier González4af3f752017-04-15 20:55:45 +0200310 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300311 if (IS_ERR(targetdata)) {
312 ret = PTR_ERR(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100313 goto err_init;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300314 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100315
316 tdisk->private_data = targetdata;
317 tqueue->queuedata = targetdata;
318
319 blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
320
321 set_capacity(tdisk, tt->capacity(targetdata));
322 add_disk(tdisk);
323
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300324 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
325 ret = -ENOMEM;
Javier González9a69b0e2017-01-31 13:17:20 +0100326 goto err_sysfs;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300327 }
Javier González9a69b0e2017-01-31 13:17:20 +0100328
Matias Bjørlingade69e22017-01-31 13:17:09 +0100329 t->type = tt;
330 t->disk = tdisk;
331 t->dev = tgt_dev;
332
333 mutex_lock(&dev->mlock);
334 list_add_tail(&t->list, &dev->targets);
335 mutex_unlock(&dev->mlock);
336
Rakesh Pandit90014822017-10-13 14:45:50 +0200337 __module_get(tt->owner);
338
Matias Bjørlingade69e22017-01-31 13:17:09 +0100339 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100340err_sysfs:
341 if (tt->exit)
342 tt->exit(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100343err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100344 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600345 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200346err_disk:
347 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100348err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200349 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100350err_t:
351 kfree(t);
352err_reserve:
353 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300354 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100355}
356
357static void __nvm_remove_target(struct nvm_target *t)
358{
359 struct nvm_tgt_type *tt = t->type;
360 struct gendisk *tdisk = t->disk;
361 struct request_queue *q = tdisk->queue;
362
363 del_gendisk(tdisk);
364 blk_cleanup_queue(q);
365
Javier González9a69b0e2017-01-31 13:17:20 +0100366 if (tt->sysfs_exit)
367 tt->sysfs_exit(tdisk);
368
Matias Bjørlingade69e22017-01-31 13:17:09 +0100369 if (tt->exit)
370 tt->exit(tdisk->private_data);
371
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200372 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100373 put_disk(tdisk);
Rakesh Pandit90014822017-10-13 14:45:50 +0200374 module_put(t->type->owner);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100375
376 list_del(&t->list);
377 kfree(t);
378}
379
380/**
381 * nvm_remove_tgt - Removes a target from the media manager
382 * @dev: device
383 * @remove: ioctl structure with target name to remove.
384 *
385 * Returns:
386 * 0: on success
387 * 1: on not found
388 * <0: on error
389 */
390static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
391{
392 struct nvm_target *t;
Rakesh Panditbb6aa6f2017-10-13 14:45:51 +0200393 struct block_device *bdev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100394
395 mutex_lock(&dev->mlock);
396 t = nvm_find_target(dev, remove->tgtname);
397 if (!t) {
398 mutex_unlock(&dev->mlock);
399 return 1;
400 }
Rakesh Panditbb6aa6f2017-10-13 14:45:51 +0200401 bdev = bdget_disk(t->disk, 0);
402 if (!bdev) {
403 pr_err("nvm: removal failed, allocating bd failed\n");
404 mutex_unlock(&dev->mlock);
405 return -ENOMEM;
406 }
407 if (bdev->bd_super || bdev->bd_part_count) {
408 pr_err("nvm: removal failed, block device busy\n");
409 bdput(bdev);
410 mutex_unlock(&dev->mlock);
411 return -EBUSY;
412 }
413 bdput(bdev);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100414 __nvm_remove_target(t);
415 mutex_unlock(&dev->mlock);
416
417 return 0;
418}
419
420static int nvm_register_map(struct nvm_dev *dev)
421{
422 struct nvm_dev_map *rmap;
423 int i, j;
424
425 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
426 if (!rmap)
427 goto err_rmap;
428
429 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
430 GFP_KERNEL);
431 if (!rmap->chnls)
432 goto err_chnls;
433
434 for (i = 0; i < dev->geo.nr_chnls; i++) {
435 struct nvm_ch_map *ch_rmap;
436 int *lun_roffs;
437 int luns_in_chnl = dev->geo.luns_per_chnl;
438
439 ch_rmap = &rmap->chnls[i];
440
441 ch_rmap->ch_off = -1;
442 ch_rmap->nr_luns = luns_in_chnl;
443
444 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
445 if (!lun_roffs)
446 goto err_ch;
447
448 for (j = 0; j < luns_in_chnl; j++)
449 lun_roffs[j] = -1;
450
451 ch_rmap->lun_offs = lun_roffs;
452 }
453
454 dev->rmap = rmap;
455
456 return 0;
457err_ch:
458 while (--i >= 0)
459 kfree(rmap->chnls[i].lun_offs);
460err_chnls:
461 kfree(rmap);
462err_rmap:
463 return -ENOMEM;
464}
465
Javier González7a3de2b2017-04-15 20:55:39 +0200466static void nvm_unregister_map(struct nvm_dev *dev)
467{
468 struct nvm_dev_map *rmap = dev->rmap;
469 int i;
470
471 for (i = 0; i < dev->geo.nr_chnls; i++)
472 kfree(rmap->chnls[i].lun_offs);
473
474 kfree(rmap->chnls);
475 kfree(rmap);
476}
477
Matias Bjørling61a561d2017-01-31 13:17:13 +0100478static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100479{
480 struct nvm_dev_map *dev_map = tgt_dev->map;
481 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
482 int lun_off = ch_map->lun_offs[p->g.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100483
484 p->g.ch += ch_map->ch_off;
485 p->g.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100486}
487
Matias Bjørling61a561d2017-01-31 13:17:13 +0100488static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100489{
490 struct nvm_dev *dev = tgt_dev->parent;
491 struct nvm_dev_map *dev_rmap = dev->rmap;
492 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
493 int lun_roff = ch_rmap->lun_offs[p->g.lun];
494
495 p->g.ch -= ch_rmap->ch_off;
496 p->g.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100497}
498
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100499static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
500 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100501{
502 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100503
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100504 for (i = 0; i < nr_ppas; i++) {
505 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
506 ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100507 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100508}
509
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100510static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
511 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100512{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100513 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100514
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100515 for (i = 0; i < nr_ppas; i++) {
516 ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
517 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
518 }
519}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100520
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100521static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
522{
523 if (rqd->nr_ppas == 1) {
524 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
525 return;
526 }
527
528 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
529}
530
531static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
532{
533 if (rqd->nr_ppas == 1) {
534 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
535 return;
536 }
537
538 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100539}
540
541void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries,
542 int len)
543{
544 struct nvm_geo *geo = &dev->geo;
545 struct nvm_dev_map *dev_rmap = dev->rmap;
546 u64 i;
547
548 for (i = 0; i < len; i++) {
549 struct nvm_ch_map *ch_rmap;
550 int *lun_roffs;
551 struct ppa_addr gaddr;
552 u64 pba = le64_to_cpu(entries[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100553 u64 diff;
554
555 if (!pba)
556 continue;
557
558 gaddr = linear_to_generic_addr(geo, pba);
559 ch_rmap = &dev_rmap->chnls[gaddr.g.ch];
560 lun_roffs = ch_rmap->lun_offs;
561
Matias Bjørlingade69e22017-01-31 13:17:09 +0100562 diff = ((ch_rmap->ch_off * geo->luns_per_chnl) +
563 (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun;
564
565 entries[i] -= cpu_to_le64(diff);
566 }
567}
568EXPORT_SYMBOL(nvm_part_to_tgt);
569
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200570int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100571{
572 int ret = 0;
573
Matias Bjørling5cd90782016-07-07 09:54:17 +0200574 down_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200575 if (nvm_find_target_type(tt->name, 0))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100576 ret = -EEXIST;
577 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200578 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200579 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100580
581 return ret;
582}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200583EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100584
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200585void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100586{
587 if (!tt)
588 return;
589
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200590 down_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100591 list_del(&tt->list);
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200592 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100593}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200594EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100595
596void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
597 dma_addr_t *dma_handler)
598{
Javier González75b85642016-05-06 20:03:13 +0200599 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100600 dma_handler);
601}
602EXPORT_SYMBOL(nvm_dev_dma_alloc);
603
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100604void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100605{
Javier González75b85642016-05-06 20:03:13 +0200606 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100607}
608EXPORT_SYMBOL(nvm_dev_dma_free);
609
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100610static struct nvm_dev *nvm_find_nvm_dev(const char *name)
611{
612 struct nvm_dev *dev;
613
614 list_for_each_entry(dev, &nvm_devices, devices)
615 if (!strcmp(name, dev->name))
616 return dev;
617
618 return NULL;
619}
620
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200621static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
622 const struct ppa_addr *ppas, int nr_ppas)
623{
624 struct nvm_dev *dev = tgt_dev->parent;
625 struct nvm_geo *geo = &tgt_dev->geo;
626 int i, plane_cnt, pl_idx;
627 struct ppa_addr ppa;
628
629 if (geo->plane_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
630 rqd->nr_ppas = nr_ppas;
631 rqd->ppa_addr = ppas[0];
632
633 return 0;
634 }
635
636 rqd->nr_ppas = nr_ppas;
637 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
638 if (!rqd->ppa_list) {
639 pr_err("nvm: failed to allocate dma memory\n");
640 return -ENOMEM;
641 }
642
643 plane_cnt = geo->plane_mode;
644 rqd->nr_ppas *= plane_cnt;
645
646 for (i = 0; i < nr_ppas; i++) {
647 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
648 ppa = ppas[i];
649 ppa.g.pl = pl_idx;
650 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
651 }
652 }
653
654 return 0;
655}
656
657static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
658 struct nvm_rq *rqd)
659{
660 if (!rqd->ppa_list)
661 return;
662
663 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
664}
665
666
Javier González333ba052016-11-28 22:39:14 +0100667int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
668 int nr_ppas, int type)
669{
670 struct nvm_dev *dev = tgt_dev->parent;
671 struct nvm_rq rqd;
672 int ret;
673
674 if (nr_ppas > dev->ops->max_phys_sect) {
675 pr_err("nvm: unable to update all blocks atomically\n");
676 return -EINVAL;
677 }
678
679 memset(&rqd, 0, sizeof(struct nvm_rq));
680
Rakesh Panditef56b9c2017-10-13 14:46:30 +0200681 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100682 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100683
684 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
Javier González17912c42017-04-15 20:55:37 +0200685 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100686 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100687 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100688 return -EINVAL;
689 }
690
691 return 0;
692}
693EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
694
Javier Gonzáleza2790062016-11-28 22:39:12 +0100695int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
696{
697 struct nvm_dev *dev = tgt_dev->parent;
698
699 return dev->ops->max_phys_sect;
700}
701EXPORT_SYMBOL(nvm_max_phys_sects);
702
Javier González8e536242016-11-28 22:39:10 +0100703int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100704{
Javier González8e536242016-11-28 22:39:10 +0100705 struct nvm_dev *dev = tgt_dev->parent;
Javier González3e505af2017-06-26 11:57:10 +0200706 int ret;
Javier González8e536242016-11-28 22:39:10 +0100707
Matias Bjørlingade69e22017-01-31 13:17:09 +0100708 if (!dev->ops->submit_io)
709 return -ENODEV;
710
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100711 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100712
713 rqd->dev = tgt_dev;
Javier González3e505af2017-06-26 11:57:10 +0200714
715 /* In case of error, fail with right address format */
716 ret = dev->ops->submit_io(dev, rqd);
717 if (ret)
718 nvm_rq_dev_to_tgt(tgt_dev, rqd);
719 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100720}
721EXPORT_SYMBOL(nvm_submit_io);
722
Javier González17912c42017-04-15 20:55:37 +0200723static void nvm_end_io_sync(struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100724{
Javier González17912c42017-04-15 20:55:37 +0200725 struct completion *waiting = rqd->private;
726
727 complete(waiting);
728}
729
730int nvm_erase_sync(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
731 int nr_ppas)
732{
733 struct nvm_geo *geo = &tgt_dev->geo;
Matias Bjørling10995c32017-01-31 13:17:10 +0100734 struct nvm_rq rqd;
735 int ret;
Javier González17912c42017-04-15 20:55:37 +0200736 DECLARE_COMPLETION_ONSTACK(wait);
Matias Bjørling10995c32017-01-31 13:17:10 +0100737
738 memset(&rqd, 0, sizeof(struct nvm_rq));
739
Javier González17912c42017-04-15 20:55:37 +0200740 rqd.opcode = NVM_OP_ERASE;
741 rqd.end_io = nvm_end_io_sync;
742 rqd.private = &wait;
743 rqd.flags = geo->plane_mode >> 1;
744
Rakesh Panditef56b9c2017-10-13 14:46:30 +0200745 ret = nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
Matias Bjørling10995c32017-01-31 13:17:10 +0100746 if (ret)
747 return ret;
748
Javier González17912c42017-04-15 20:55:37 +0200749 ret = nvm_submit_io(tgt_dev, &rqd);
750 if (ret) {
751 pr_err("rrpr: erase I/O submission failed: %d\n", ret);
752 goto free_ppa_list;
753 }
754 wait_for_completion_io(&wait);
Matias Bjørling10995c32017-01-31 13:17:10 +0100755
Javier González17912c42017-04-15 20:55:37 +0200756free_ppa_list:
757 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Matias Bjørling10995c32017-01-31 13:17:10 +0100758
759 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100760}
Javier González17912c42017-04-15 20:55:37 +0200761EXPORT_SYMBOL(nvm_erase_sync);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100762
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100763int nvm_get_l2p_tbl(struct nvm_tgt_dev *tgt_dev, u64 slba, u32 nlb,
Javier González959e9112016-11-28 22:39:11 +0100764 nvm_l2p_update_fn *update_l2p, void *priv)
765{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100766 struct nvm_dev *dev = tgt_dev->parent;
767
Javier González959e9112016-11-28 22:39:11 +0100768 if (!dev->ops->get_l2p_tbl)
769 return 0;
770
771 return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv);
772}
773EXPORT_SYMBOL(nvm_get_l2p_tbl);
774
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100775int nvm_get_area(struct nvm_tgt_dev *tgt_dev, sector_t *lba, sector_t len)
Javier González959e9112016-11-28 22:39:11 +0100776{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100777 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100778 struct nvm_geo *geo = &dev->geo;
779 struct nvm_area *area, *prev, *next;
780 sector_t begin = 0;
781 sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100782
Matias Bjørlingade69e22017-01-31 13:17:09 +0100783 if (len > max_sectors)
784 return -EINVAL;
785
786 area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL);
787 if (!area)
788 return -ENOMEM;
789
790 prev = NULL;
791
792 spin_lock(&dev->lock);
793 list_for_each_entry(next, &dev->area_list, list) {
794 if (begin + len > next->begin) {
795 begin = next->end;
796 prev = next;
797 continue;
798 }
799 break;
800 }
801
802 if ((begin + len) > max_sectors) {
803 spin_unlock(&dev->lock);
804 kfree(area);
805 return -EINVAL;
806 }
807
808 area->begin = *lba = begin;
809 area->end = begin + len;
810
811 if (prev) /* insert into sorted order */
812 list_add(&area->list, &prev->list);
813 else
814 list_add(&area->list, &dev->area_list);
815 spin_unlock(&dev->lock);
816
817 return 0;
Javier González959e9112016-11-28 22:39:11 +0100818}
819EXPORT_SYMBOL(nvm_get_area);
820
Matias Bjørlingade69e22017-01-31 13:17:09 +0100821void nvm_put_area(struct nvm_tgt_dev *tgt_dev, sector_t begin)
Javier González959e9112016-11-28 22:39:11 +0100822{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100823 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100824 struct nvm_area *area;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100825
Matias Bjørlingade69e22017-01-31 13:17:09 +0100826 spin_lock(&dev->lock);
827 list_for_each_entry(area, &dev->area_list, list) {
828 if (area->begin != begin)
829 continue;
830
831 list_del(&area->list);
832 spin_unlock(&dev->lock);
833 kfree(area);
834 return;
835 }
836 spin_unlock(&dev->lock);
Javier González959e9112016-11-28 22:39:11 +0100837}
838EXPORT_SYMBOL(nvm_put_area);
839
Matias Bjørling06894ef2017-01-31 13:17:17 +0100840void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100841{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100842 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100843
844 /* Convert address space */
845 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100846 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100847
Matias Bjørling06894ef2017-01-31 13:17:17 +0100848 if (rqd->end_io)
849 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100850}
851EXPORT_SYMBOL(nvm_end_io);
852
Matias Bjørling22e8c972016-05-06 20:02:58 +0200853/*
854 * folds a bad block list from its plane representation to its virtual
855 * block representation. The fold is done in place and reduced size is
856 * returned.
857 *
858 * If any of the planes status are bad or grown bad block, the virtual block
859 * is marked bad. If not bad, the first plane state acts as the block state.
860 */
861int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
862{
Javier González8e79b5c2016-11-28 22:39:06 +0100863 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200864 int blk, offset, pl, blktype;
865
Javier González8e79b5c2016-11-28 22:39:06 +0100866 if (nr_blks != geo->blks_per_lun * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200867 return -EINVAL;
868
Javier González8e79b5c2016-11-28 22:39:06 +0100869 for (blk = 0; blk < geo->blks_per_lun; blk++) {
870 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200871 blktype = blks[offset];
872
873 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100874 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200875 if (blks[offset + pl] &
876 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
877 blktype = blks[offset + pl];
878 break;
879 }
880 }
881
882 blks[blk] = blktype;
883 }
884
Javier González8e79b5c2016-11-28 22:39:06 +0100885 return geo->blks_per_lun;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200886}
887EXPORT_SYMBOL(nvm_bb_tbl_fold);
888
Javier González333ba052016-11-28 22:39:14 +0100889int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
890 u8 *blks)
891{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100892 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100893
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100894 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100895
896 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100897}
898EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
899
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100900static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
901{
Javier González8e79b5c2016-11-28 22:39:06 +0100902 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100903 int i;
904
Javier González8e79b5c2016-11-28 22:39:06 +0100905 dev->lps_per_blk = geo->pgs_per_blk;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100906 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
907 if (!dev->lptbl)
908 return -ENOMEM;
909
910 /* Just a linear array */
911 for (i = 0; i < dev->lps_per_blk; i++)
912 dev->lptbl[i] = i;
913
914 return 0;
915}
916
917static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
918{
919 int i, p;
920 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
921
922 if (!mlc->num_pairs)
923 return 0;
924
925 dev->lps_per_blk = mlc->num_pairs;
926 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
927 if (!dev->lptbl)
928 return -ENOMEM;
929
930 /* The lower page table encoding consists of a list of bytes, where each
931 * has a lower and an upper half. The first half byte maintains the
932 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200933 * previous incrementation value
934 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100935 dev->lptbl[0] = mlc->pairs[0] & 0xF;
936 for (i = 1; i < dev->lps_per_blk; i++) {
937 p = mlc->pairs[i >> 1];
938 if (i & 0x1) /* upper */
939 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
940 else /* lower */
941 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
942 }
943
944 return 0;
945}
946
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100947static int nvm_core_init(struct nvm_dev *dev)
948{
949 struct nvm_id *id = &dev->identity;
Matias Bjørling19bd6fe2017-01-31 13:17:15 +0100950 struct nvm_id_group *grp = &id->grp;
Javier González8e79b5c2016-11-28 22:39:06 +0100951 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200952 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100953
Javier González8e79b5c2016-11-28 22:39:06 +0100954 /* Whole device values */
955 geo->nr_chnls = grp->num_ch;
956 geo->luns_per_chnl = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100957
Javier González8e79b5c2016-11-28 22:39:06 +0100958 /* Generic device values */
959 geo->pgs_per_blk = grp->num_pg;
960 geo->blks_per_lun = grp->num_blk;
961 geo->nr_planes = grp->num_pln;
962 geo->fpg_size = grp->fpg_sz;
963 geo->pfpg_size = grp->fpg_sz * grp->num_pln;
964 geo->sec_size = grp->csecs;
965 geo->oob_size = grp->sos;
966 geo->sec_per_pg = grp->fpg_sz / grp->csecs;
967 geo->mccap = grp->mccap;
968 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
969
970 geo->plane_mode = NVM_PLANE_SINGLE;
971 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100972
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100973 if (grp->mpos & 0x020202)
Javier González8e79b5c2016-11-28 22:39:06 +0100974 geo->plane_mode = NVM_PLANE_DOUBLE;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100975 if (grp->mpos & 0x040404)
Javier González8e79b5c2016-11-28 22:39:06 +0100976 geo->plane_mode = NVM_PLANE_QUAD;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100977
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200978 if (grp->mtype != 0) {
979 pr_err("nvm: memory type not supported\n");
980 return -EINVAL;
981 }
982
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100983 /* calculated values */
Javier González8e79b5c2016-11-28 22:39:06 +0100984 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
985 geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
986 geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
987 geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100988
Javier González8e79b5c2016-11-28 22:39:06 +0100989 dev->total_secs = geo->nr_luns * geo->sec_per_lun;
990 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100991 sizeof(unsigned long), GFP_KERNEL);
992 if (!dev->lun_map)
993 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200994
995 switch (grp->fmtype) {
996 case NVM_ID_FMTYPE_SLC:
997 if (nvm_init_slc_tbl(dev, grp)) {
998 ret = -ENOMEM;
999 goto err_fmtype;
1000 }
1001 break;
1002 case NVM_ID_FMTYPE_MLC:
1003 if (nvm_init_mlc_tbl(dev, grp)) {
1004 ret = -ENOMEM;
1005 goto err_fmtype;
1006 }
1007 break;
1008 default:
1009 pr_err("nvm: flash type not supported\n");
1010 ret = -EINVAL;
1011 goto err_fmtype;
1012 }
1013
Matias Bjørlingade69e22017-01-31 13:17:09 +01001014 INIT_LIST_HEAD(&dev->area_list);
1015 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +01001016 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001017 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001018
Matias Bjørlingade69e22017-01-31 13:17:09 +01001019 ret = nvm_register_map(dev);
1020 if (ret)
1021 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001022
Matias Bjørlingade69e22017-01-31 13:17:09 +01001023 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001024 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001025err_fmtype:
1026 kfree(dev->lun_map);
1027 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001028}
1029
Javier González46b160c2017-04-15 20:55:46 +02001030static void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001031{
1032 if (!dev)
1033 return;
1034
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001035 if (dev->dma_pool)
1036 dev->ops->destroy_dma_pool(dev->dma_pool);
1037
Javier González7a3de2b2017-04-15 20:55:39 +02001038 nvm_unregister_map(dev);
Matias Bjørlingca5927e2016-01-12 07:49:35 +01001039 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001040 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001041 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001042}
1043
1044static int nvm_init(struct nvm_dev *dev)
1045{
Javier González8e79b5c2016-11-28 22:39:06 +01001046 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001047 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001048
Matias Bjørling16f26c32015-12-06 11:25:48 +01001049 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001050 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001051 goto err;
1052 }
1053
Matias Bjørling19bd6fe2017-01-31 13:17:15 +01001054 pr_debug("nvm: ver:%x nvm_vendor:%x\n",
1055 dev->identity.ver_id, dev->identity.vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001056
1057 if (dev->identity.ver_id != 1) {
1058 pr_err("nvm: device not supported by kernel.");
1059 goto err;
1060 }
1061
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001062 ret = nvm_core_init(dev);
1063 if (ret) {
1064 pr_err("nvm: could not initialize core structures.\n");
1065 goto err;
1066 }
1067
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001068 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +01001069 dev->name, geo->sec_per_pg, geo->nr_planes,
1070 geo->pgs_per_blk, geo->blks_per_lun,
1071 geo->nr_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001072 return 0;
1073err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001074 pr_err("nvm: failed to initialize nvm\n");
1075 return ret;
1076}
1077
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001078struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001079{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001080 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1081}
1082EXPORT_SYMBOL(nvm_alloc_dev);
1083
1084int nvm_register(struct nvm_dev *dev)
1085{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001086 int ret;
1087
Matias Bjørlingade69e22017-01-31 13:17:09 +01001088 if (!dev->q || !dev->ops)
1089 return -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001090
Wenwei Taod1601472015-11-28 16:49:25 +01001091 if (dev->ops->max_phys_sect > 256) {
1092 pr_info("nvm: max sectors supported is 256.\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001093 return -EINVAL;
Wenwei Taod1601472015-11-28 16:49:25 +01001094 }
1095
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001096 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +02001097 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
1098 if (!dev->dma_pool) {
1099 pr_err("nvm: could not create dma pool\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001100 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001101 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001102 }
1103
Matias Bjørlingade69e22017-01-31 13:17:09 +01001104 ret = nvm_init(dev);
1105 if (ret)
1106 goto err_init;
Matias Bjørlingb7692072016-01-12 07:49:38 +01001107
Matias Bjørling762796b2015-12-06 11:25:49 +01001108 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001109 down_write(&nvm_lock);
1110 list_add(&dev->devices, &nvm_devices);
1111 up_write(&nvm_lock);
1112
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001113 return 0;
1114err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +01001115 dev->ops->destroy_dma_pool(dev->dma_pool);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001116 return ret;
1117}
1118EXPORT_SYMBOL(nvm_register);
1119
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001120void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001121{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001122 struct nvm_target *t, *tmp;
1123
1124 mutex_lock(&dev->mlock);
1125 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1126 if (t->dev->parent != dev)
1127 continue;
1128 __nvm_remove_target(t);
1129 }
1130 mutex_unlock(&dev->mlock);
1131
Wenwei Taod0a712c2015-11-28 16:49:28 +01001132 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001133 list_del(&dev->devices);
1134 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001135
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001136 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001137}
1138EXPORT_SYMBOL(nvm_unregister);
1139
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001140static int __nvm_configure_create(struct nvm_ioctl_create *create)
1141{
1142 struct nvm_dev *dev;
1143 struct nvm_ioctl_create_simple *s;
1144
Wenwei Taod0a712c2015-11-28 16:49:28 +01001145 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001146 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001147 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001148
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001149 if (!dev) {
1150 pr_err("nvm: device not found\n");
1151 return -EINVAL;
1152 }
1153
1154 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
1155 pr_err("nvm: config type not valid\n");
1156 return -EINVAL;
1157 }
1158 s = &create->conf.s;
1159
Matias Bjørling6732c742017-02-15 16:25:33 +01001160 if (s->lun_begin == -1 && s->lun_end == -1) {
1161 s->lun_begin = 0;
1162 s->lun_end = dev->geo.nr_luns - 1;
1163 }
1164
Matias Bjørling0e5ffd12017-02-15 16:25:32 +01001165 if (s->lun_begin > s->lun_end || s->lun_end >= dev->geo.nr_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001166 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Matias Bjørling0e5ffd12017-02-15 16:25:32 +01001167 s->lun_begin, s->lun_end, dev->geo.nr_luns - 1);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001168 return -EINVAL;
1169 }
1170
Matias Bjørlingade69e22017-01-31 13:17:09 +01001171 return nvm_create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001172}
1173
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001174static long nvm_ioctl_info(struct file *file, void __user *arg)
1175{
1176 struct nvm_ioctl_info *info;
1177 struct nvm_tgt_type *tt;
1178 int tgt_iter = 0;
1179
1180 if (!capable(CAP_SYS_ADMIN))
1181 return -EPERM;
1182
1183 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1184 if (IS_ERR(info))
1185 return -EFAULT;
1186
1187 info->version[0] = NVM_VERSION_MAJOR;
1188 info->version[1] = NVM_VERSION_MINOR;
1189 info->version[2] = NVM_VERSION_PATCH;
1190
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001191 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001192 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001193 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1194
1195 tgt->version[0] = tt->version[0];
1196 tgt->version[1] = tt->version[1];
1197 tgt->version[2] = tt->version[2];
1198 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1199
1200 tgt_iter++;
1201 }
1202
1203 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001204 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001205
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001206 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1207 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001208 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001209 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001210
1211 kfree(info);
1212 return 0;
1213}
1214
1215static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1216{
1217 struct nvm_ioctl_get_devices *devices;
1218 struct nvm_dev *dev;
1219 int i = 0;
1220
1221 if (!capable(CAP_SYS_ADMIN))
1222 return -EPERM;
1223
1224 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1225 if (!devices)
1226 return -ENOMEM;
1227
1228 down_write(&nvm_lock);
1229 list_for_each_entry(dev, &nvm_devices, devices) {
1230 struct nvm_ioctl_device_info *info = &devices->info[i];
1231
Javier González6eb08242017-04-15 20:55:49 +02001232 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001233
Matias Bjørlingade69e22017-01-31 13:17:09 +01001234 /* kept for compatibility */
1235 info->bmversion[0] = 1;
1236 info->bmversion[1] = 0;
1237 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001238 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001239 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001240
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001241 if (i > 31) {
1242 pr_err("nvm: max 31 devices can be reported.\n");
1243 break;
1244 }
1245 }
1246 up_write(&nvm_lock);
1247
1248 devices->nr_devices = i;
1249
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001250 if (copy_to_user(arg, devices,
1251 sizeof(struct nvm_ioctl_get_devices))) {
1252 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001253 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001254 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001255
1256 kfree(devices);
1257 return 0;
1258}
1259
1260static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1261{
1262 struct nvm_ioctl_create create;
1263
1264 if (!capable(CAP_SYS_ADMIN))
1265 return -EPERM;
1266
1267 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1268 return -EFAULT;
1269
1270 create.dev[DISK_NAME_LEN - 1] = '\0';
1271 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1272 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1273
1274 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001275 __u32 flags = create.flags;
1276
1277 /* Check for valid flags */
1278 if (flags & NVM_TARGET_FACTORY)
1279 flags &= ~NVM_TARGET_FACTORY;
1280
1281 if (flags) {
1282 pr_err("nvm: flag not supported\n");
1283 return -EINVAL;
1284 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001285 }
1286
1287 return __nvm_configure_create(&create);
1288}
1289
1290static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1291{
1292 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001293 struct nvm_dev *dev;
1294 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001295
1296 if (!capable(CAP_SYS_ADMIN))
1297 return -EPERM;
1298
1299 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1300 return -EFAULT;
1301
1302 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1303
1304 if (remove.flags != 0) {
1305 pr_err("nvm: no flags supported\n");
1306 return -EINVAL;
1307 }
1308
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001309 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001310 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001311 if (!ret)
1312 break;
1313 }
1314
1315 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001316}
1317
Matias Bjørlingade69e22017-01-31 13:17:09 +01001318/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001319static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1320{
1321 struct nvm_ioctl_dev_init init;
1322
1323 if (!capable(CAP_SYS_ADMIN))
1324 return -EPERM;
1325
1326 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1327 return -EFAULT;
1328
1329 if (init.flags != 0) {
1330 pr_err("nvm: no flags supported\n");
1331 return -EINVAL;
1332 }
1333
Matias Bjørlingade69e22017-01-31 13:17:09 +01001334 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001335}
1336
Matias Bjørlingade69e22017-01-31 13:17:09 +01001337/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001338static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1339{
1340 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001341
1342 if (!capable(CAP_SYS_ADMIN))
1343 return -EPERM;
1344
1345 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1346 return -EFAULT;
1347
1348 fact.dev[DISK_NAME_LEN - 1] = '\0';
1349
1350 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1351 return -EINVAL;
1352
Matias Bjørlingbf643182016-02-04 15:13:27 +01001353 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001354}
1355
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001356static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1357{
1358 void __user *argp = (void __user *)arg;
1359
1360 switch (cmd) {
1361 case NVM_INFO:
1362 return nvm_ioctl_info(file, argp);
1363 case NVM_GET_DEVICES:
1364 return nvm_ioctl_get_devices(file, argp);
1365 case NVM_DEV_CREATE:
1366 return nvm_ioctl_dev_create(file, argp);
1367 case NVM_DEV_REMOVE:
1368 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001369 case NVM_DEV_INIT:
1370 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001371 case NVM_DEV_FACTORY:
1372 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001373 }
1374 return 0;
1375}
1376
1377static const struct file_operations _ctl_fops = {
1378 .open = nonseekable_open,
1379 .unlocked_ioctl = nvm_ctl_ioctl,
1380 .owner = THIS_MODULE,
1381 .llseek = noop_llseek,
1382};
1383
1384static struct miscdevice _nvm_misc = {
1385 .minor = MISC_DYNAMIC_MINOR,
1386 .name = "lightnvm",
1387 .nodename = "lightnvm/control",
1388 .fops = &_ctl_fops,
1389};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001390builtin_misc_device(_nvm_misc);