blob: 5d7aa45f2e042f86fa3b8a0d32c781c40d9f4ce4 [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>
Paul Gortmaker389b2a12016-10-29 16:38:41 -040025#include <linux/moduleparam.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010026#include <linux/miscdevice.h>
27#include <linux/lightnvm.h>
Matias Bjørling912761622016-01-12 07:49:21 +010028#include <linux/sched/sysctl.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010029
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020030static LIST_HEAD(nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +020031static DECLARE_RWSEM(nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010032static LIST_HEAD(nvm_devices);
33static DECLARE_RWSEM(nvm_lock);
34
Matias Bjørlingade69e22017-01-31 13:17:09 +010035/* Map between virtual and physical channel and lun */
36struct nvm_ch_map {
37 int ch_off;
38 int nr_luns;
39 int *lun_offs;
40};
41
42struct nvm_dev_map {
43 struct nvm_ch_map *chnls;
44 int nr_chnls;
45};
46
47struct nvm_area {
48 struct list_head list;
49 sector_t begin;
50 sector_t end; /* end is excluded */
51};
52
Matias Bjørlingade69e22017-01-31 13:17:09 +010053static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
54{
55 struct nvm_target *tgt;
56
57 list_for_each_entry(tgt, &dev->targets, list)
58 if (!strcmp(name, tgt->disk->disk_name))
59 return tgt;
60
61 return NULL;
62}
63
64static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
65{
66 int i;
67
68 for (i = lun_begin; i <= lun_end; i++) {
69 if (test_and_set_bit(i, dev->lun_map)) {
70 pr_err("nvm: lun %d already allocated\n", i);
71 goto err;
72 }
73 }
74
75 return 0;
76err:
77 while (--i > lun_begin)
78 clear_bit(i, dev->lun_map);
79
80 return -EBUSY;
81}
82
83static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
84 int lun_end)
85{
86 int i;
87
88 for (i = lun_begin; i <= lun_end; i++)
89 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
90}
91
Javier Gonzálezedee1bd2017-04-15 20:55:42 +020092static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
Matias Bjørlingade69e22017-01-31 13:17:09 +010093{
94 struct nvm_dev *dev = tgt_dev->parent;
95 struct nvm_dev_map *dev_map = tgt_dev->map;
96 int i, j;
97
98 for (i = 0; i < dev_map->nr_chnls; i++) {
99 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
100 int *lun_offs = ch_map->lun_offs;
101 int ch = i + ch_map->ch_off;
102
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200103 if (clear) {
104 for (j = 0; j < ch_map->nr_luns; j++) {
105 int lun = j + lun_offs[j];
106 int lunid = (ch * dev->geo.luns_per_chnl) + lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100107
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200108 WARN_ON(!test_and_clear_bit(lunid,
109 dev->lun_map));
110 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100111 }
112
113 kfree(ch_map->lun_offs);
114 }
115
116 kfree(dev_map->chnls);
117 kfree(dev_map);
118
119 kfree(tgt_dev->luns);
120 kfree(tgt_dev);
121}
122
123static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
124 int lun_begin, int lun_end)
125{
126 struct nvm_tgt_dev *tgt_dev = NULL;
127 struct nvm_dev_map *dev_rmap = dev->rmap;
128 struct nvm_dev_map *dev_map;
129 struct ppa_addr *luns;
130 int nr_luns = lun_end - lun_begin + 1;
131 int luns_left = nr_luns;
132 int nr_chnls = nr_luns / dev->geo.luns_per_chnl;
133 int nr_chnls_mod = nr_luns % dev->geo.luns_per_chnl;
134 int bch = lun_begin / dev->geo.luns_per_chnl;
135 int blun = lun_begin % dev->geo.luns_per_chnl;
136 int lunid = 0;
137 int lun_balanced = 1;
138 int prev_nr_luns;
139 int i, j;
140
141 nr_chnls = nr_luns / dev->geo.luns_per_chnl;
142 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:
214 while (--i > 0)
215 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
229static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
230{
231 struct nvm_ioctl_create_simple *s = &create->conf.s;
232 struct request_queue *tqueue;
233 struct gendisk *tdisk;
234 struct nvm_tgt_type *tt;
235 struct nvm_target *t;
236 struct nvm_tgt_dev *tgt_dev;
237 void *targetdata;
238
239 tt = nvm_find_target_type(create->tgttype, 1);
240 if (!tt) {
241 pr_err("nvm: target type %s not found\n", create->tgttype);
242 return -EINVAL;
243 }
244
245 mutex_lock(&dev->mlock);
246 t = nvm_find_target(dev, create->tgtname);
247 if (t) {
248 pr_err("nvm: target name already exists.\n");
249 mutex_unlock(&dev->mlock);
250 return -EINVAL;
251 }
252 mutex_unlock(&dev->mlock);
253
254 if (nvm_reserve_luns(dev, s->lun_begin, s->lun_end))
255 return -ENOMEM;
256
257 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
258 if (!t)
259 goto err_reserve;
260
261 tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end);
262 if (!tgt_dev) {
263 pr_err("nvm: could not create target device\n");
264 goto err_t;
265 }
266
Matias Bjørlingade69e22017-01-31 13:17:09 +0100267 tdisk = alloc_disk(0);
268 if (!tdisk)
Javier González7d1ef2f2017-04-15 20:55:43 +0200269 goto err_dev;
270
271 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
272 if (!tqueue)
273 goto err_disk;
274 blk_queue_make_request(tqueue, tt->make_rq);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100275
Javier González6eb08242017-04-15 20:55:49 +0200276 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100277 tdisk->flags = GENHD_FL_EXT_DEVT;
278 tdisk->major = 0;
279 tdisk->first_minor = 0;
280 tdisk->fops = &nvm_fops;
281 tdisk->queue = tqueue;
282
Javier González4af3f752017-04-15 20:55:45 +0200283 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100284 if (IS_ERR(targetdata))
285 goto err_init;
286
287 tdisk->private_data = targetdata;
288 tqueue->queuedata = targetdata;
289
290 blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
291
292 set_capacity(tdisk, tt->capacity(targetdata));
293 add_disk(tdisk);
294
Javier González9a69b0e2017-01-31 13:17:20 +0100295 if (tt->sysfs_init && tt->sysfs_init(tdisk))
296 goto err_sysfs;
297
Matias Bjørlingade69e22017-01-31 13:17:09 +0100298 t->type = tt;
299 t->disk = tdisk;
300 t->dev = tgt_dev;
301
302 mutex_lock(&dev->mlock);
303 list_add_tail(&t->list, &dev->targets);
304 mutex_unlock(&dev->mlock);
305
306 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100307err_sysfs:
308 if (tt->exit)
309 tt->exit(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100310err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100311 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600312 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200313err_disk:
314 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100315err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200316 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100317err_t:
318 kfree(t);
319err_reserve:
320 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
321 return -ENOMEM;
322}
323
324static void __nvm_remove_target(struct nvm_target *t)
325{
326 struct nvm_tgt_type *tt = t->type;
327 struct gendisk *tdisk = t->disk;
328 struct request_queue *q = tdisk->queue;
329
330 del_gendisk(tdisk);
331 blk_cleanup_queue(q);
332
Javier González9a69b0e2017-01-31 13:17:20 +0100333 if (tt->sysfs_exit)
334 tt->sysfs_exit(tdisk);
335
Matias Bjørlingade69e22017-01-31 13:17:09 +0100336 if (tt->exit)
337 tt->exit(tdisk->private_data);
338
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200339 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100340 put_disk(tdisk);
341
342 list_del(&t->list);
343 kfree(t);
344}
345
346/**
347 * nvm_remove_tgt - Removes a target from the media manager
348 * @dev: device
349 * @remove: ioctl structure with target name to remove.
350 *
351 * Returns:
352 * 0: on success
353 * 1: on not found
354 * <0: on error
355 */
356static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
357{
358 struct nvm_target *t;
359
360 mutex_lock(&dev->mlock);
361 t = nvm_find_target(dev, remove->tgtname);
362 if (!t) {
363 mutex_unlock(&dev->mlock);
364 return 1;
365 }
366 __nvm_remove_target(t);
367 mutex_unlock(&dev->mlock);
368
369 return 0;
370}
371
372static int nvm_register_map(struct nvm_dev *dev)
373{
374 struct nvm_dev_map *rmap;
375 int i, j;
376
377 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
378 if (!rmap)
379 goto err_rmap;
380
381 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
382 GFP_KERNEL);
383 if (!rmap->chnls)
384 goto err_chnls;
385
386 for (i = 0; i < dev->geo.nr_chnls; i++) {
387 struct nvm_ch_map *ch_rmap;
388 int *lun_roffs;
389 int luns_in_chnl = dev->geo.luns_per_chnl;
390
391 ch_rmap = &rmap->chnls[i];
392
393 ch_rmap->ch_off = -1;
394 ch_rmap->nr_luns = luns_in_chnl;
395
396 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
397 if (!lun_roffs)
398 goto err_ch;
399
400 for (j = 0; j < luns_in_chnl; j++)
401 lun_roffs[j] = -1;
402
403 ch_rmap->lun_offs = lun_roffs;
404 }
405
406 dev->rmap = rmap;
407
408 return 0;
409err_ch:
410 while (--i >= 0)
411 kfree(rmap->chnls[i].lun_offs);
412err_chnls:
413 kfree(rmap);
414err_rmap:
415 return -ENOMEM;
416}
417
Javier González7a3de2b2017-04-15 20:55:39 +0200418static void nvm_unregister_map(struct nvm_dev *dev)
419{
420 struct nvm_dev_map *rmap = dev->rmap;
421 int i;
422
423 for (i = 0; i < dev->geo.nr_chnls; i++)
424 kfree(rmap->chnls[i].lun_offs);
425
426 kfree(rmap->chnls);
427 kfree(rmap);
428}
429
Matias Bjørling61a561d2017-01-31 13:17:13 +0100430static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100431{
432 struct nvm_dev_map *dev_map = tgt_dev->map;
433 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
434 int lun_off = ch_map->lun_offs[p->g.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100435
436 p->g.ch += ch_map->ch_off;
437 p->g.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100438}
439
Matias Bjørling61a561d2017-01-31 13:17:13 +0100440static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100441{
442 struct nvm_dev *dev = tgt_dev->parent;
443 struct nvm_dev_map *dev_rmap = dev->rmap;
444 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
445 int lun_roff = ch_rmap->lun_offs[p->g.lun];
446
447 p->g.ch -= ch_rmap->ch_off;
448 p->g.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100449}
450
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100451static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
452 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100453{
454 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100455
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100456 for (i = 0; i < nr_ppas; i++) {
457 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
458 ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100459 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100460}
461
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100462static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
463 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100464{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100465 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100466
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100467 for (i = 0; i < nr_ppas; i++) {
468 ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
469 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
470 }
471}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100472
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100473static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
474{
475 if (rqd->nr_ppas == 1) {
476 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
477 return;
478 }
479
480 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
481}
482
483static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
484{
485 if (rqd->nr_ppas == 1) {
486 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
487 return;
488 }
489
490 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100491}
492
493void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries,
494 int len)
495{
496 struct nvm_geo *geo = &dev->geo;
497 struct nvm_dev_map *dev_rmap = dev->rmap;
498 u64 i;
499
500 for (i = 0; i < len; i++) {
501 struct nvm_ch_map *ch_rmap;
502 int *lun_roffs;
503 struct ppa_addr gaddr;
504 u64 pba = le64_to_cpu(entries[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100505 u64 diff;
506
507 if (!pba)
508 continue;
509
510 gaddr = linear_to_generic_addr(geo, pba);
511 ch_rmap = &dev_rmap->chnls[gaddr.g.ch];
512 lun_roffs = ch_rmap->lun_offs;
513
Matias Bjørlingade69e22017-01-31 13:17:09 +0100514 diff = ((ch_rmap->ch_off * geo->luns_per_chnl) +
515 (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun;
516
517 entries[i] -= cpu_to_le64(diff);
518 }
519}
520EXPORT_SYMBOL(nvm_part_to_tgt);
521
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200522struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200523{
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200524 struct nvm_tgt_type *tmp, *tt = NULL;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200525
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200526 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200527 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200528
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200529 list_for_each_entry(tmp, &nvm_tgt_types, list)
530 if (!strcmp(name, tmp->name)) {
531 tt = tmp;
532 break;
533 }
534
535 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200536 up_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200537 return tt;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200538}
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200539EXPORT_SYMBOL(nvm_find_target_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100540
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200541int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100542{
543 int ret = 0;
544
Matias Bjørling5cd90782016-07-07 09:54:17 +0200545 down_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200546 if (nvm_find_target_type(tt->name, 0))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100547 ret = -EEXIST;
548 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200549 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200550 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100551
552 return ret;
553}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200554EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100555
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200556void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100557{
558 if (!tt)
559 return;
560
561 down_write(&nvm_lock);
562 list_del(&tt->list);
563 up_write(&nvm_lock);
564}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200565EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100566
567void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
568 dma_addr_t *dma_handler)
569{
Javier González75b85642016-05-06 20:03:13 +0200570 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100571 dma_handler);
572}
573EXPORT_SYMBOL(nvm_dev_dma_alloc);
574
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100575void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100576{
Javier González75b85642016-05-06 20:03:13 +0200577 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100578}
579EXPORT_SYMBOL(nvm_dev_dma_free);
580
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100581static struct nvm_dev *nvm_find_nvm_dev(const char *name)
582{
583 struct nvm_dev *dev;
584
585 list_for_each_entry(dev, &nvm_devices, devices)
586 if (!strcmp(name, dev->name))
587 return dev;
588
589 return NULL;
590}
591
Javier González333ba052016-11-28 22:39:14 +0100592int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
593 int nr_ppas, int type)
594{
595 struct nvm_dev *dev = tgt_dev->parent;
596 struct nvm_rq rqd;
597 int ret;
598
599 if (nr_ppas > dev->ops->max_phys_sect) {
600 pr_err("nvm: unable to update all blocks atomically\n");
601 return -EINVAL;
602 }
603
604 memset(&rqd, 0, sizeof(struct nvm_rq));
605
Javier González17912c42017-04-15 20:55:37 +0200606 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100607 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100608
609 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
Javier González17912c42017-04-15 20:55:37 +0200610 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100611 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100612 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100613 return -EINVAL;
614 }
615
616 return 0;
617}
618EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
619
Javier Gonzáleza2790062016-11-28 22:39:12 +0100620int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
621{
622 struct nvm_dev *dev = tgt_dev->parent;
623
624 return dev->ops->max_phys_sect;
625}
626EXPORT_SYMBOL(nvm_max_phys_sects);
627
Javier González8e536242016-11-28 22:39:10 +0100628int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100629{
Javier González8e536242016-11-28 22:39:10 +0100630 struct nvm_dev *dev = tgt_dev->parent;
631
Matias Bjørlingade69e22017-01-31 13:17:09 +0100632 if (!dev->ops->submit_io)
633 return -ENODEV;
634
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100635 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100636
637 rqd->dev = tgt_dev;
638 return dev->ops->submit_io(dev, rqd);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100639}
640EXPORT_SYMBOL(nvm_submit_io);
641
Javier González17912c42017-04-15 20:55:37 +0200642static void nvm_end_io_sync(struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100643{
Javier González17912c42017-04-15 20:55:37 +0200644 struct completion *waiting = rqd->private;
645
646 complete(waiting);
647}
648
649int nvm_erase_sync(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
650 int nr_ppas)
651{
652 struct nvm_geo *geo = &tgt_dev->geo;
Matias Bjørling10995c32017-01-31 13:17:10 +0100653 struct nvm_rq rqd;
654 int ret;
Javier González17912c42017-04-15 20:55:37 +0200655 DECLARE_COMPLETION_ONSTACK(wait);
Matias Bjørling10995c32017-01-31 13:17:10 +0100656
657 memset(&rqd, 0, sizeof(struct nvm_rq));
658
Javier González17912c42017-04-15 20:55:37 +0200659 rqd.opcode = NVM_OP_ERASE;
660 rqd.end_io = nvm_end_io_sync;
661 rqd.private = &wait;
662 rqd.flags = geo->plane_mode >> 1;
663
664 ret = nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørling10995c32017-01-31 13:17:10 +0100665 if (ret)
666 return ret;
667
Javier González17912c42017-04-15 20:55:37 +0200668 ret = nvm_submit_io(tgt_dev, &rqd);
669 if (ret) {
670 pr_err("rrpr: erase I/O submission failed: %d\n", ret);
671 goto free_ppa_list;
672 }
673 wait_for_completion_io(&wait);
Matias Bjørling10995c32017-01-31 13:17:10 +0100674
Javier González17912c42017-04-15 20:55:37 +0200675free_ppa_list:
676 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Matias Bjørling10995c32017-01-31 13:17:10 +0100677
678 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100679}
Javier González17912c42017-04-15 20:55:37 +0200680EXPORT_SYMBOL(nvm_erase_sync);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100681
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100682int nvm_get_l2p_tbl(struct nvm_tgt_dev *tgt_dev, u64 slba, u32 nlb,
Javier González959e9112016-11-28 22:39:11 +0100683 nvm_l2p_update_fn *update_l2p, void *priv)
684{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100685 struct nvm_dev *dev = tgt_dev->parent;
686
Javier González959e9112016-11-28 22:39:11 +0100687 if (!dev->ops->get_l2p_tbl)
688 return 0;
689
690 return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv);
691}
692EXPORT_SYMBOL(nvm_get_l2p_tbl);
693
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100694int nvm_get_area(struct nvm_tgt_dev *tgt_dev, sector_t *lba, sector_t len)
Javier González959e9112016-11-28 22:39:11 +0100695{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100696 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100697 struct nvm_geo *geo = &dev->geo;
698 struct nvm_area *area, *prev, *next;
699 sector_t begin = 0;
700 sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100701
Matias Bjørlingade69e22017-01-31 13:17:09 +0100702 if (len > max_sectors)
703 return -EINVAL;
704
705 area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL);
706 if (!area)
707 return -ENOMEM;
708
709 prev = NULL;
710
711 spin_lock(&dev->lock);
712 list_for_each_entry(next, &dev->area_list, list) {
713 if (begin + len > next->begin) {
714 begin = next->end;
715 prev = next;
716 continue;
717 }
718 break;
719 }
720
721 if ((begin + len) > max_sectors) {
722 spin_unlock(&dev->lock);
723 kfree(area);
724 return -EINVAL;
725 }
726
727 area->begin = *lba = begin;
728 area->end = begin + len;
729
730 if (prev) /* insert into sorted order */
731 list_add(&area->list, &prev->list);
732 else
733 list_add(&area->list, &dev->area_list);
734 spin_unlock(&dev->lock);
735
736 return 0;
Javier González959e9112016-11-28 22:39:11 +0100737}
738EXPORT_SYMBOL(nvm_get_area);
739
Matias Bjørlingade69e22017-01-31 13:17:09 +0100740void nvm_put_area(struct nvm_tgt_dev *tgt_dev, sector_t begin)
Javier González959e9112016-11-28 22:39:11 +0100741{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100742 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100743 struct nvm_area *area;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100744
Matias Bjørlingade69e22017-01-31 13:17:09 +0100745 spin_lock(&dev->lock);
746 list_for_each_entry(area, &dev->area_list, list) {
747 if (area->begin != begin)
748 continue;
749
750 list_del(&area->list);
751 spin_unlock(&dev->lock);
752 kfree(area);
753 return;
754 }
755 spin_unlock(&dev->lock);
Javier González959e9112016-11-28 22:39:11 +0100756}
757EXPORT_SYMBOL(nvm_put_area);
758
Javier González17912c42017-04-15 20:55:37 +0200759int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
Matias Bjørling8680f162016-07-07 09:54:22 +0200760 const struct ppa_addr *ppas, int nr_ppas, int vblk)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100761{
Javier González17912c42017-04-15 20:55:37 +0200762 struct nvm_dev *dev = tgt_dev->parent;
763 struct nvm_geo *geo = &tgt_dev->geo;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100764 int i, plane_cnt, pl_idx;
Matias Bjørling8680f162016-07-07 09:54:22 +0200765 struct ppa_addr ppa;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100766
Javier González8e79b5c2016-11-28 22:39:06 +0100767 if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
Javier González6d5be952016-05-06 20:03:20 +0200768 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100769 rqd->ppa_addr = ppas[0];
770
771 return 0;
772 }
773
Javier González6d5be952016-05-06 20:03:20 +0200774 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100775 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
776 if (!rqd->ppa_list) {
777 pr_err("nvm: failed to allocate dma memory\n");
778 return -ENOMEM;
779 }
780
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200781 if (!vblk) {
782 for (i = 0; i < nr_ppas; i++)
783 rqd->ppa_list[i] = ppas[i];
784 } else {
Javier González8e79b5c2016-11-28 22:39:06 +0100785 plane_cnt = geo->plane_mode;
Javier González6d5be952016-05-06 20:03:20 +0200786 rqd->nr_ppas *= plane_cnt;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200787
Matias Bjørling556755e2016-01-12 07:49:26 +0100788 for (i = 0; i < nr_ppas; i++) {
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200789 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
Matias Bjørling8680f162016-07-07 09:54:22 +0200790 ppa = ppas[i];
791 ppa.g.pl = pl_idx;
792 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200793 }
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100794 }
795 }
796
797 return 0;
798}
799EXPORT_SYMBOL(nvm_set_rqd_ppalist);
800
Javier González17912c42017-04-15 20:55:37 +0200801void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100802{
803 if (!rqd->ppa_list)
804 return;
805
Javier González17912c42017-04-15 20:55:37 +0200806 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100807}
808EXPORT_SYMBOL(nvm_free_rqd_ppalist);
809
Matias Bjørling06894ef2017-01-31 13:17:17 +0100810void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100811{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100812 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100813
814 /* Convert address space */
815 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100816 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100817
Matias Bjørling06894ef2017-01-31 13:17:17 +0100818 if (rqd->end_io)
819 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100820}
821EXPORT_SYMBOL(nvm_end_io);
822
Matias Bjørling22e8c972016-05-06 20:02:58 +0200823/*
824 * folds a bad block list from its plane representation to its virtual
825 * block representation. The fold is done in place and reduced size is
826 * returned.
827 *
828 * If any of the planes status are bad or grown bad block, the virtual block
829 * is marked bad. If not bad, the first plane state acts as the block state.
830 */
831int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
832{
Javier González8e79b5c2016-11-28 22:39:06 +0100833 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200834 int blk, offset, pl, blktype;
835
Javier González8e79b5c2016-11-28 22:39:06 +0100836 if (nr_blks != geo->blks_per_lun * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200837 return -EINVAL;
838
Javier González8e79b5c2016-11-28 22:39:06 +0100839 for (blk = 0; blk < geo->blks_per_lun; blk++) {
840 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200841 blktype = blks[offset];
842
843 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100844 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200845 if (blks[offset + pl] &
846 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
847 blktype = blks[offset + pl];
848 break;
849 }
850 }
851
852 blks[blk] = blktype;
853 }
854
Javier González8e79b5c2016-11-28 22:39:06 +0100855 return geo->blks_per_lun;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200856}
857EXPORT_SYMBOL(nvm_bb_tbl_fold);
858
Javier González333ba052016-11-28 22:39:14 +0100859int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
860 u8 *blks)
861{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100862 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100863
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100864 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100865
866 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100867}
868EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
869
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100870static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
871{
Javier González8e79b5c2016-11-28 22:39:06 +0100872 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100873 int i;
874
Javier González8e79b5c2016-11-28 22:39:06 +0100875 dev->lps_per_blk = geo->pgs_per_blk;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100876 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
877 if (!dev->lptbl)
878 return -ENOMEM;
879
880 /* Just a linear array */
881 for (i = 0; i < dev->lps_per_blk; i++)
882 dev->lptbl[i] = i;
883
884 return 0;
885}
886
887static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
888{
889 int i, p;
890 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
891
892 if (!mlc->num_pairs)
893 return 0;
894
895 dev->lps_per_blk = mlc->num_pairs;
896 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
897 if (!dev->lptbl)
898 return -ENOMEM;
899
900 /* The lower page table encoding consists of a list of bytes, where each
901 * has a lower and an upper half. The first half byte maintains the
902 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200903 * previous incrementation value
904 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100905 dev->lptbl[0] = mlc->pairs[0] & 0xF;
906 for (i = 1; i < dev->lps_per_blk; i++) {
907 p = mlc->pairs[i >> 1];
908 if (i & 0x1) /* upper */
909 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
910 else /* lower */
911 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
912 }
913
914 return 0;
915}
916
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100917static int nvm_core_init(struct nvm_dev *dev)
918{
919 struct nvm_id *id = &dev->identity;
Matias Bjørling19bd6fe2017-01-31 13:17:15 +0100920 struct nvm_id_group *grp = &id->grp;
Javier González8e79b5c2016-11-28 22:39:06 +0100921 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200922 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100923
Javier González8e79b5c2016-11-28 22:39:06 +0100924 /* Whole device values */
925 geo->nr_chnls = grp->num_ch;
926 geo->luns_per_chnl = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100927
Javier González8e79b5c2016-11-28 22:39:06 +0100928 /* Generic device values */
929 geo->pgs_per_blk = grp->num_pg;
930 geo->blks_per_lun = grp->num_blk;
931 geo->nr_planes = grp->num_pln;
932 geo->fpg_size = grp->fpg_sz;
933 geo->pfpg_size = grp->fpg_sz * grp->num_pln;
934 geo->sec_size = grp->csecs;
935 geo->oob_size = grp->sos;
936 geo->sec_per_pg = grp->fpg_sz / grp->csecs;
937 geo->mccap = grp->mccap;
938 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
939
940 geo->plane_mode = NVM_PLANE_SINGLE;
941 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100942
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100943 if (grp->mpos & 0x020202)
Javier González8e79b5c2016-11-28 22:39:06 +0100944 geo->plane_mode = NVM_PLANE_DOUBLE;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100945 if (grp->mpos & 0x040404)
Javier González8e79b5c2016-11-28 22:39:06 +0100946 geo->plane_mode = NVM_PLANE_QUAD;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100947
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200948 if (grp->mtype != 0) {
949 pr_err("nvm: memory type not supported\n");
950 return -EINVAL;
951 }
952
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100953 /* calculated values */
Javier González8e79b5c2016-11-28 22:39:06 +0100954 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
955 geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
956 geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
957 geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100958
Javier González8e79b5c2016-11-28 22:39:06 +0100959 dev->total_secs = geo->nr_luns * geo->sec_per_lun;
960 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100961 sizeof(unsigned long), GFP_KERNEL);
962 if (!dev->lun_map)
963 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200964
965 switch (grp->fmtype) {
966 case NVM_ID_FMTYPE_SLC:
967 if (nvm_init_slc_tbl(dev, grp)) {
968 ret = -ENOMEM;
969 goto err_fmtype;
970 }
971 break;
972 case NVM_ID_FMTYPE_MLC:
973 if (nvm_init_mlc_tbl(dev, grp)) {
974 ret = -ENOMEM;
975 goto err_fmtype;
976 }
977 break;
978 default:
979 pr_err("nvm: flash type not supported\n");
980 ret = -EINVAL;
981 goto err_fmtype;
982 }
983
Matias Bjørlingade69e22017-01-31 13:17:09 +0100984 INIT_LIST_HEAD(&dev->area_list);
985 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100986 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100987 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100988
Matias Bjørlingade69e22017-01-31 13:17:09 +0100989 ret = nvm_register_map(dev);
990 if (ret)
991 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200992
Matias Bjørlingade69e22017-01-31 13:17:09 +0100993 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100994 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200995err_fmtype:
996 kfree(dev->lun_map);
997 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100998}
999
Javier González46b160c2017-04-15 20:55:46 +02001000static void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001001{
1002 if (!dev)
1003 return;
1004
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001005 if (dev->dma_pool)
1006 dev->ops->destroy_dma_pool(dev->dma_pool);
1007
Javier González7a3de2b2017-04-15 20:55:39 +02001008 nvm_unregister_map(dev);
Matias Bjørlingca5927e2016-01-12 07:49:35 +01001009 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001010 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001011 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001012}
1013
1014static int nvm_init(struct nvm_dev *dev)
1015{
Javier González8e79b5c2016-11-28 22:39:06 +01001016 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001017 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001018
Matias Bjørling16f26c32015-12-06 11:25:48 +01001019 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001020 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001021 goto err;
1022 }
1023
Matias Bjørling19bd6fe2017-01-31 13:17:15 +01001024 pr_debug("nvm: ver:%x nvm_vendor:%x\n",
1025 dev->identity.ver_id, dev->identity.vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001026
1027 if (dev->identity.ver_id != 1) {
1028 pr_err("nvm: device not supported by kernel.");
1029 goto err;
1030 }
1031
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001032 ret = nvm_core_init(dev);
1033 if (ret) {
1034 pr_err("nvm: could not initialize core structures.\n");
1035 goto err;
1036 }
1037
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001038 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +01001039 dev->name, geo->sec_per_pg, geo->nr_planes,
1040 geo->pgs_per_blk, geo->blks_per_lun,
1041 geo->nr_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001042 return 0;
1043err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001044 pr_err("nvm: failed to initialize nvm\n");
1045 return ret;
1046}
1047
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001048struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001049{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001050 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1051}
1052EXPORT_SYMBOL(nvm_alloc_dev);
1053
1054int nvm_register(struct nvm_dev *dev)
1055{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001056 int ret;
1057
Matias Bjørlingade69e22017-01-31 13:17:09 +01001058 if (!dev->q || !dev->ops)
1059 return -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001060
Wenwei Taod1601472015-11-28 16:49:25 +01001061 if (dev->ops->max_phys_sect > 256) {
1062 pr_info("nvm: max sectors supported is 256.\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001063 return -EINVAL;
Wenwei Taod1601472015-11-28 16:49:25 +01001064 }
1065
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001066 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +02001067 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
1068 if (!dev->dma_pool) {
1069 pr_err("nvm: could not create dma pool\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001070 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001071 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001072 }
1073
Matias Bjørlingade69e22017-01-31 13:17:09 +01001074 ret = nvm_init(dev);
1075 if (ret)
1076 goto err_init;
Matias Bjørlingb7692072016-01-12 07:49:38 +01001077
Matias Bjørling762796b2015-12-06 11:25:49 +01001078 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001079 down_write(&nvm_lock);
1080 list_add(&dev->devices, &nvm_devices);
1081 up_write(&nvm_lock);
1082
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001083 return 0;
1084err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +01001085 dev->ops->destroy_dma_pool(dev->dma_pool);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001086 return ret;
1087}
1088EXPORT_SYMBOL(nvm_register);
1089
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001090void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001091{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001092 struct nvm_target *t, *tmp;
1093
1094 mutex_lock(&dev->mlock);
1095 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1096 if (t->dev->parent != dev)
1097 continue;
1098 __nvm_remove_target(t);
1099 }
1100 mutex_unlock(&dev->mlock);
1101
Wenwei Taod0a712c2015-11-28 16:49:28 +01001102 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001103 list_del(&dev->devices);
1104 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001105
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001106 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001107}
1108EXPORT_SYMBOL(nvm_unregister);
1109
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001110static int __nvm_configure_create(struct nvm_ioctl_create *create)
1111{
1112 struct nvm_dev *dev;
1113 struct nvm_ioctl_create_simple *s;
1114
Wenwei Taod0a712c2015-11-28 16:49:28 +01001115 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001116 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001117 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001118
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001119 if (!dev) {
1120 pr_err("nvm: device not found\n");
1121 return -EINVAL;
1122 }
1123
1124 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
1125 pr_err("nvm: config type not valid\n");
1126 return -EINVAL;
1127 }
1128 s = &create->conf.s;
1129
Matias Bjørling6732c742017-02-15 16:25:33 +01001130 if (s->lun_begin == -1 && s->lun_end == -1) {
1131 s->lun_begin = 0;
1132 s->lun_end = dev->geo.nr_luns - 1;
1133 }
1134
Matias Bjørling0e5ffd12017-02-15 16:25:32 +01001135 if (s->lun_begin > s->lun_end || s->lun_end >= dev->geo.nr_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001136 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Matias Bjørling0e5ffd12017-02-15 16:25:32 +01001137 s->lun_begin, s->lun_end, dev->geo.nr_luns - 1);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001138 return -EINVAL;
1139 }
1140
Matias Bjørlingade69e22017-01-31 13:17:09 +01001141 return nvm_create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001142}
1143
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001144static long nvm_ioctl_info(struct file *file, void __user *arg)
1145{
1146 struct nvm_ioctl_info *info;
1147 struct nvm_tgt_type *tt;
1148 int tgt_iter = 0;
1149
1150 if (!capable(CAP_SYS_ADMIN))
1151 return -EPERM;
1152
1153 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1154 if (IS_ERR(info))
1155 return -EFAULT;
1156
1157 info->version[0] = NVM_VERSION_MAJOR;
1158 info->version[1] = NVM_VERSION_MINOR;
1159 info->version[2] = NVM_VERSION_PATCH;
1160
1161 down_write(&nvm_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001162 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001163 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1164
1165 tgt->version[0] = tt->version[0];
1166 tgt->version[1] = tt->version[1];
1167 tgt->version[2] = tt->version[2];
1168 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1169
1170 tgt_iter++;
1171 }
1172
1173 info->tgtsize = tgt_iter;
1174 up_write(&nvm_lock);
1175
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001176 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1177 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001178 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001179 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001180
1181 kfree(info);
1182 return 0;
1183}
1184
1185static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1186{
1187 struct nvm_ioctl_get_devices *devices;
1188 struct nvm_dev *dev;
1189 int i = 0;
1190
1191 if (!capable(CAP_SYS_ADMIN))
1192 return -EPERM;
1193
1194 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1195 if (!devices)
1196 return -ENOMEM;
1197
1198 down_write(&nvm_lock);
1199 list_for_each_entry(dev, &nvm_devices, devices) {
1200 struct nvm_ioctl_device_info *info = &devices->info[i];
1201
Javier González6eb08242017-04-15 20:55:49 +02001202 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001203
Matias Bjørlingade69e22017-01-31 13:17:09 +01001204 /* kept for compatibility */
1205 info->bmversion[0] = 1;
1206 info->bmversion[1] = 0;
1207 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001208 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001209 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001210
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001211 if (i > 31) {
1212 pr_err("nvm: max 31 devices can be reported.\n");
1213 break;
1214 }
1215 }
1216 up_write(&nvm_lock);
1217
1218 devices->nr_devices = i;
1219
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001220 if (copy_to_user(arg, devices,
1221 sizeof(struct nvm_ioctl_get_devices))) {
1222 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001223 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001224 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001225
1226 kfree(devices);
1227 return 0;
1228}
1229
1230static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1231{
1232 struct nvm_ioctl_create create;
1233
1234 if (!capable(CAP_SYS_ADMIN))
1235 return -EPERM;
1236
1237 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1238 return -EFAULT;
1239
1240 create.dev[DISK_NAME_LEN - 1] = '\0';
1241 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1242 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1243
1244 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001245 __u32 flags = create.flags;
1246
1247 /* Check for valid flags */
1248 if (flags & NVM_TARGET_FACTORY)
1249 flags &= ~NVM_TARGET_FACTORY;
1250
1251 if (flags) {
1252 pr_err("nvm: flag not supported\n");
1253 return -EINVAL;
1254 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001255 }
1256
1257 return __nvm_configure_create(&create);
1258}
1259
1260static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1261{
1262 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001263 struct nvm_dev *dev;
1264 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001265
1266 if (!capable(CAP_SYS_ADMIN))
1267 return -EPERM;
1268
1269 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1270 return -EFAULT;
1271
1272 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1273
1274 if (remove.flags != 0) {
1275 pr_err("nvm: no flags supported\n");
1276 return -EINVAL;
1277 }
1278
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001279 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001280 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001281 if (!ret)
1282 break;
1283 }
1284
1285 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001286}
1287
Matias Bjørlingade69e22017-01-31 13:17:09 +01001288/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001289static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1290{
1291 struct nvm_ioctl_dev_init init;
1292
1293 if (!capable(CAP_SYS_ADMIN))
1294 return -EPERM;
1295
1296 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1297 return -EFAULT;
1298
1299 if (init.flags != 0) {
1300 pr_err("nvm: no flags supported\n");
1301 return -EINVAL;
1302 }
1303
Matias Bjørlingade69e22017-01-31 13:17:09 +01001304 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001305}
1306
Matias Bjørlingade69e22017-01-31 13:17:09 +01001307/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001308static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1309{
1310 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001311
1312 if (!capable(CAP_SYS_ADMIN))
1313 return -EPERM;
1314
1315 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1316 return -EFAULT;
1317
1318 fact.dev[DISK_NAME_LEN - 1] = '\0';
1319
1320 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1321 return -EINVAL;
1322
Matias Bjørlingbf643182016-02-04 15:13:27 +01001323 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001324}
1325
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001326static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1327{
1328 void __user *argp = (void __user *)arg;
1329
1330 switch (cmd) {
1331 case NVM_INFO:
1332 return nvm_ioctl_info(file, argp);
1333 case NVM_GET_DEVICES:
1334 return nvm_ioctl_get_devices(file, argp);
1335 case NVM_DEV_CREATE:
1336 return nvm_ioctl_dev_create(file, argp);
1337 case NVM_DEV_REMOVE:
1338 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001339 case NVM_DEV_INIT:
1340 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001341 case NVM_DEV_FACTORY:
1342 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001343 }
1344 return 0;
1345}
1346
1347static const struct file_operations _ctl_fops = {
1348 .open = nonseekable_open,
1349 .unlocked_ioctl = nvm_ctl_ioctl,
1350 .owner = THIS_MODULE,
1351 .llseek = noop_llseek,
1352};
1353
1354static struct miscdevice _nvm_misc = {
1355 .minor = MISC_DYNAMIC_MINOR,
1356 .name = "lightnvm",
1357 .nodename = "lightnvm/control",
1358 .fops = &_ctl_fops,
1359};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001360builtin_misc_device(_nvm_misc);