blob: 5eea3d5ecc30d395cd20625ffa7beb011d899263 [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
267 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
268 if (!tqueue)
269 goto err_dev;
270 blk_queue_make_request(tqueue, tt->make_rq);
271
272 tdisk = alloc_disk(0);
273 if (!tdisk)
274 goto err_queue;
275
276 sprintf(tdisk->disk_name, "%s", create->tgtname);
277 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
283 targetdata = tt->init(tgt_dev, tdisk);
284 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:
311 put_disk(tdisk);
312err_queue:
313 blk_cleanup_queue(tqueue);
314err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200315 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100316err_t:
317 kfree(t);
318err_reserve:
319 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
320 return -ENOMEM;
321}
322
323static void __nvm_remove_target(struct nvm_target *t)
324{
325 struct nvm_tgt_type *tt = t->type;
326 struct gendisk *tdisk = t->disk;
327 struct request_queue *q = tdisk->queue;
328
329 del_gendisk(tdisk);
330 blk_cleanup_queue(q);
331
Javier González9a69b0e2017-01-31 13:17:20 +0100332 if (tt->sysfs_exit)
333 tt->sysfs_exit(tdisk);
334
Matias Bjørlingade69e22017-01-31 13:17:09 +0100335 if (tt->exit)
336 tt->exit(tdisk->private_data);
337
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200338 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100339 put_disk(tdisk);
340
341 list_del(&t->list);
342 kfree(t);
343}
344
345/**
346 * nvm_remove_tgt - Removes a target from the media manager
347 * @dev: device
348 * @remove: ioctl structure with target name to remove.
349 *
350 * Returns:
351 * 0: on success
352 * 1: on not found
353 * <0: on error
354 */
355static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
356{
357 struct nvm_target *t;
358
359 mutex_lock(&dev->mlock);
360 t = nvm_find_target(dev, remove->tgtname);
361 if (!t) {
362 mutex_unlock(&dev->mlock);
363 return 1;
364 }
365 __nvm_remove_target(t);
366 mutex_unlock(&dev->mlock);
367
368 return 0;
369}
370
371static int nvm_register_map(struct nvm_dev *dev)
372{
373 struct nvm_dev_map *rmap;
374 int i, j;
375
376 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
377 if (!rmap)
378 goto err_rmap;
379
380 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
381 GFP_KERNEL);
382 if (!rmap->chnls)
383 goto err_chnls;
384
385 for (i = 0; i < dev->geo.nr_chnls; i++) {
386 struct nvm_ch_map *ch_rmap;
387 int *lun_roffs;
388 int luns_in_chnl = dev->geo.luns_per_chnl;
389
390 ch_rmap = &rmap->chnls[i];
391
392 ch_rmap->ch_off = -1;
393 ch_rmap->nr_luns = luns_in_chnl;
394
395 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
396 if (!lun_roffs)
397 goto err_ch;
398
399 for (j = 0; j < luns_in_chnl; j++)
400 lun_roffs[j] = -1;
401
402 ch_rmap->lun_offs = lun_roffs;
403 }
404
405 dev->rmap = rmap;
406
407 return 0;
408err_ch:
409 while (--i >= 0)
410 kfree(rmap->chnls[i].lun_offs);
411err_chnls:
412 kfree(rmap);
413err_rmap:
414 return -ENOMEM;
415}
416
Javier González7a3de2b2017-04-15 20:55:39 +0200417static void nvm_unregister_map(struct nvm_dev *dev)
418{
419 struct nvm_dev_map *rmap = dev->rmap;
420 int i;
421
422 for (i = 0; i < dev->geo.nr_chnls; i++)
423 kfree(rmap->chnls[i].lun_offs);
424
425 kfree(rmap->chnls);
426 kfree(rmap);
427}
428
Matias Bjørling61a561d2017-01-31 13:17:13 +0100429static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100430{
431 struct nvm_dev_map *dev_map = tgt_dev->map;
432 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
433 int lun_off = ch_map->lun_offs[p->g.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100434
435 p->g.ch += ch_map->ch_off;
436 p->g.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100437}
438
Matias Bjørling61a561d2017-01-31 13:17:13 +0100439static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100440{
441 struct nvm_dev *dev = tgt_dev->parent;
442 struct nvm_dev_map *dev_rmap = dev->rmap;
443 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
444 int lun_roff = ch_rmap->lun_offs[p->g.lun];
445
446 p->g.ch -= ch_rmap->ch_off;
447 p->g.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100448}
449
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100450static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
451 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100452{
453 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100454
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100455 for (i = 0; i < nr_ppas; i++) {
456 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
457 ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100458 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100459}
460
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100461static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
462 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100463{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100464 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100465
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100466 for (i = 0; i < nr_ppas; i++) {
467 ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
468 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
469 }
470}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100471
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100472static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
473{
474 if (rqd->nr_ppas == 1) {
475 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
476 return;
477 }
478
479 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
480}
481
482static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
483{
484 if (rqd->nr_ppas == 1) {
485 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
486 return;
487 }
488
489 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100490}
491
492void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries,
493 int len)
494{
495 struct nvm_geo *geo = &dev->geo;
496 struct nvm_dev_map *dev_rmap = dev->rmap;
497 u64 i;
498
499 for (i = 0; i < len; i++) {
500 struct nvm_ch_map *ch_rmap;
501 int *lun_roffs;
502 struct ppa_addr gaddr;
503 u64 pba = le64_to_cpu(entries[i]);
504 int off;
505 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
514 off = gaddr.g.ch * geo->luns_per_chnl + gaddr.g.lun;
515
516 diff = ((ch_rmap->ch_off * geo->luns_per_chnl) +
517 (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun;
518
519 entries[i] -= cpu_to_le64(diff);
520 }
521}
522EXPORT_SYMBOL(nvm_part_to_tgt);
523
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200524struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200525{
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200526 struct nvm_tgt_type *tmp, *tt = NULL;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200527
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200528 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200529 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200530
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200531 list_for_each_entry(tmp, &nvm_tgt_types, list)
532 if (!strcmp(name, tmp->name)) {
533 tt = tmp;
534 break;
535 }
536
537 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200538 up_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200539 return tt;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200540}
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200541EXPORT_SYMBOL(nvm_find_target_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100542
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200543int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100544{
545 int ret = 0;
546
Matias Bjørling5cd90782016-07-07 09:54:17 +0200547 down_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200548 if (nvm_find_target_type(tt->name, 0))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100549 ret = -EEXIST;
550 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200551 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200552 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100553
554 return ret;
555}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200556EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100557
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200558void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100559{
560 if (!tt)
561 return;
562
563 down_write(&nvm_lock);
564 list_del(&tt->list);
565 up_write(&nvm_lock);
566}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200567EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100568
569void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
570 dma_addr_t *dma_handler)
571{
Javier González75b85642016-05-06 20:03:13 +0200572 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100573 dma_handler);
574}
575EXPORT_SYMBOL(nvm_dev_dma_alloc);
576
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100577void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100578{
Javier González75b85642016-05-06 20:03:13 +0200579 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100580}
581EXPORT_SYMBOL(nvm_dev_dma_free);
582
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100583static struct nvm_dev *nvm_find_nvm_dev(const char *name)
584{
585 struct nvm_dev *dev;
586
587 list_for_each_entry(dev, &nvm_devices, devices)
588 if (!strcmp(name, dev->name))
589 return dev;
590
591 return NULL;
592}
593
Javier González333ba052016-11-28 22:39:14 +0100594int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
595 int nr_ppas, int type)
596{
597 struct nvm_dev *dev = tgt_dev->parent;
598 struct nvm_rq rqd;
599 int ret;
600
601 if (nr_ppas > dev->ops->max_phys_sect) {
602 pr_err("nvm: unable to update all blocks atomically\n");
603 return -EINVAL;
604 }
605
606 memset(&rqd, 0, sizeof(struct nvm_rq));
607
Javier González17912c42017-04-15 20:55:37 +0200608 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100609 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100610
611 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
Javier González17912c42017-04-15 20:55:37 +0200612 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100613 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100614 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100615 return -EINVAL;
616 }
617
618 return 0;
619}
620EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
621
Javier Gonzáleza2790062016-11-28 22:39:12 +0100622int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
623{
624 struct nvm_dev *dev = tgt_dev->parent;
625
626 return dev->ops->max_phys_sect;
627}
628EXPORT_SYMBOL(nvm_max_phys_sects);
629
Javier González8e536242016-11-28 22:39:10 +0100630int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100631{
Javier González8e536242016-11-28 22:39:10 +0100632 struct nvm_dev *dev = tgt_dev->parent;
633
Matias Bjørlingade69e22017-01-31 13:17:09 +0100634 if (!dev->ops->submit_io)
635 return -ENODEV;
636
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100637 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100638
639 rqd->dev = tgt_dev;
640 return dev->ops->submit_io(dev, rqd);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100641}
642EXPORT_SYMBOL(nvm_submit_io);
643
Javier González17912c42017-04-15 20:55:37 +0200644static void nvm_end_io_sync(struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100645{
Javier González17912c42017-04-15 20:55:37 +0200646 struct completion *waiting = rqd->private;
647
648 complete(waiting);
649}
650
651int nvm_erase_sync(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
652 int nr_ppas)
653{
654 struct nvm_geo *geo = &tgt_dev->geo;
Matias Bjørling10995c32017-01-31 13:17:10 +0100655 struct nvm_rq rqd;
656 int ret;
Javier González17912c42017-04-15 20:55:37 +0200657 DECLARE_COMPLETION_ONSTACK(wait);
Matias Bjørling10995c32017-01-31 13:17:10 +0100658
659 memset(&rqd, 0, sizeof(struct nvm_rq));
660
Javier González17912c42017-04-15 20:55:37 +0200661 rqd.opcode = NVM_OP_ERASE;
662 rqd.end_io = nvm_end_io_sync;
663 rqd.private = &wait;
664 rqd.flags = geo->plane_mode >> 1;
665
666 ret = nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørling10995c32017-01-31 13:17:10 +0100667 if (ret)
668 return ret;
669
Javier González17912c42017-04-15 20:55:37 +0200670 ret = nvm_submit_io(tgt_dev, &rqd);
671 if (ret) {
672 pr_err("rrpr: erase I/O submission failed: %d\n", ret);
673 goto free_ppa_list;
674 }
675 wait_for_completion_io(&wait);
Matias Bjørling10995c32017-01-31 13:17:10 +0100676
Javier González17912c42017-04-15 20:55:37 +0200677free_ppa_list:
678 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Matias Bjørling10995c32017-01-31 13:17:10 +0100679
680 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100681}
Javier González17912c42017-04-15 20:55:37 +0200682EXPORT_SYMBOL(nvm_erase_sync);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100683
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100684int nvm_get_l2p_tbl(struct nvm_tgt_dev *tgt_dev, u64 slba, u32 nlb,
Javier González959e9112016-11-28 22:39:11 +0100685 nvm_l2p_update_fn *update_l2p, void *priv)
686{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100687 struct nvm_dev *dev = tgt_dev->parent;
688
Javier González959e9112016-11-28 22:39:11 +0100689 if (!dev->ops->get_l2p_tbl)
690 return 0;
691
692 return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv);
693}
694EXPORT_SYMBOL(nvm_get_l2p_tbl);
695
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100696int nvm_get_area(struct nvm_tgt_dev *tgt_dev, sector_t *lba, sector_t len)
Javier González959e9112016-11-28 22:39:11 +0100697{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100698 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100699 struct nvm_geo *geo = &dev->geo;
700 struct nvm_area *area, *prev, *next;
701 sector_t begin = 0;
702 sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100703
Matias Bjørlingade69e22017-01-31 13:17:09 +0100704 if (len > max_sectors)
705 return -EINVAL;
706
707 area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL);
708 if (!area)
709 return -ENOMEM;
710
711 prev = NULL;
712
713 spin_lock(&dev->lock);
714 list_for_each_entry(next, &dev->area_list, list) {
715 if (begin + len > next->begin) {
716 begin = next->end;
717 prev = next;
718 continue;
719 }
720 break;
721 }
722
723 if ((begin + len) > max_sectors) {
724 spin_unlock(&dev->lock);
725 kfree(area);
726 return -EINVAL;
727 }
728
729 area->begin = *lba = begin;
730 area->end = begin + len;
731
732 if (prev) /* insert into sorted order */
733 list_add(&area->list, &prev->list);
734 else
735 list_add(&area->list, &dev->area_list);
736 spin_unlock(&dev->lock);
737
738 return 0;
Javier González959e9112016-11-28 22:39:11 +0100739}
740EXPORT_SYMBOL(nvm_get_area);
741
Matias Bjørlingade69e22017-01-31 13:17:09 +0100742void nvm_put_area(struct nvm_tgt_dev *tgt_dev, sector_t begin)
Javier González959e9112016-11-28 22:39:11 +0100743{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100744 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100745 struct nvm_area *area;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100746
Matias Bjørlingade69e22017-01-31 13:17:09 +0100747 spin_lock(&dev->lock);
748 list_for_each_entry(area, &dev->area_list, list) {
749 if (area->begin != begin)
750 continue;
751
752 list_del(&area->list);
753 spin_unlock(&dev->lock);
754 kfree(area);
755 return;
756 }
757 spin_unlock(&dev->lock);
Javier González959e9112016-11-28 22:39:11 +0100758}
759EXPORT_SYMBOL(nvm_put_area);
760
Javier González17912c42017-04-15 20:55:37 +0200761int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
Matias Bjørling8680f162016-07-07 09:54:22 +0200762 const struct ppa_addr *ppas, int nr_ppas, int vblk)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100763{
Javier González17912c42017-04-15 20:55:37 +0200764 struct nvm_dev *dev = tgt_dev->parent;
765 struct nvm_geo *geo = &tgt_dev->geo;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100766 int i, plane_cnt, pl_idx;
Matias Bjørling8680f162016-07-07 09:54:22 +0200767 struct ppa_addr ppa;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100768
Javier González8e79b5c2016-11-28 22:39:06 +0100769 if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
Javier González6d5be952016-05-06 20:03:20 +0200770 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100771 rqd->ppa_addr = ppas[0];
772
773 return 0;
774 }
775
Javier González6d5be952016-05-06 20:03:20 +0200776 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100777 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
778 if (!rqd->ppa_list) {
779 pr_err("nvm: failed to allocate dma memory\n");
780 return -ENOMEM;
781 }
782
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200783 if (!vblk) {
784 for (i = 0; i < nr_ppas; i++)
785 rqd->ppa_list[i] = ppas[i];
786 } else {
Javier González8e79b5c2016-11-28 22:39:06 +0100787 plane_cnt = geo->plane_mode;
Javier González6d5be952016-05-06 20:03:20 +0200788 rqd->nr_ppas *= plane_cnt;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200789
Matias Bjørling556755e2016-01-12 07:49:26 +0100790 for (i = 0; i < nr_ppas; i++) {
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200791 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
Matias Bjørling8680f162016-07-07 09:54:22 +0200792 ppa = ppas[i];
793 ppa.g.pl = pl_idx;
794 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200795 }
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100796 }
797 }
798
799 return 0;
800}
801EXPORT_SYMBOL(nvm_set_rqd_ppalist);
802
Javier González17912c42017-04-15 20:55:37 +0200803void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100804{
805 if (!rqd->ppa_list)
806 return;
807
Javier González17912c42017-04-15 20:55:37 +0200808 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100809}
810EXPORT_SYMBOL(nvm_free_rqd_ppalist);
811
Matias Bjørling06894ef2017-01-31 13:17:17 +0100812void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100813{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100814 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100815
816 /* Convert address space */
817 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100818 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100819
Matias Bjørling06894ef2017-01-31 13:17:17 +0100820 if (rqd->end_io)
821 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100822}
823EXPORT_SYMBOL(nvm_end_io);
824
Matias Bjørling22e8c972016-05-06 20:02:58 +0200825/*
826 * folds a bad block list from its plane representation to its virtual
827 * block representation. The fold is done in place and reduced size is
828 * returned.
829 *
830 * If any of the planes status are bad or grown bad block, the virtual block
831 * is marked bad. If not bad, the first plane state acts as the block state.
832 */
833int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
834{
Javier González8e79b5c2016-11-28 22:39:06 +0100835 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200836 int blk, offset, pl, blktype;
837
Javier González8e79b5c2016-11-28 22:39:06 +0100838 if (nr_blks != geo->blks_per_lun * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200839 return -EINVAL;
840
Javier González8e79b5c2016-11-28 22:39:06 +0100841 for (blk = 0; blk < geo->blks_per_lun; blk++) {
842 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200843 blktype = blks[offset];
844
845 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100846 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200847 if (blks[offset + pl] &
848 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
849 blktype = blks[offset + pl];
850 break;
851 }
852 }
853
854 blks[blk] = blktype;
855 }
856
Javier González8e79b5c2016-11-28 22:39:06 +0100857 return geo->blks_per_lun;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200858}
859EXPORT_SYMBOL(nvm_bb_tbl_fold);
860
Javier González333ba052016-11-28 22:39:14 +0100861int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
862 u8 *blks)
863{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100864 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100865
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100866 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100867
868 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100869}
870EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
871
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100872static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
873{
Javier González8e79b5c2016-11-28 22:39:06 +0100874 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100875 int i;
876
Javier González8e79b5c2016-11-28 22:39:06 +0100877 dev->lps_per_blk = geo->pgs_per_blk;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100878 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
879 if (!dev->lptbl)
880 return -ENOMEM;
881
882 /* Just a linear array */
883 for (i = 0; i < dev->lps_per_blk; i++)
884 dev->lptbl[i] = i;
885
886 return 0;
887}
888
889static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
890{
891 int i, p;
892 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
893
894 if (!mlc->num_pairs)
895 return 0;
896
897 dev->lps_per_blk = mlc->num_pairs;
898 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
899 if (!dev->lptbl)
900 return -ENOMEM;
901
902 /* The lower page table encoding consists of a list of bytes, where each
903 * has a lower and an upper half. The first half byte maintains the
904 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200905 * previous incrementation value
906 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100907 dev->lptbl[0] = mlc->pairs[0] & 0xF;
908 for (i = 1; i < dev->lps_per_blk; i++) {
909 p = mlc->pairs[i >> 1];
910 if (i & 0x1) /* upper */
911 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
912 else /* lower */
913 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
914 }
915
916 return 0;
917}
918
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100919static int nvm_core_init(struct nvm_dev *dev)
920{
921 struct nvm_id *id = &dev->identity;
Matias Bjørling19bd6fe2017-01-31 13:17:15 +0100922 struct nvm_id_group *grp = &id->grp;
Javier González8e79b5c2016-11-28 22:39:06 +0100923 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200924 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100925
Javier González8e79b5c2016-11-28 22:39:06 +0100926 /* Whole device values */
927 geo->nr_chnls = grp->num_ch;
928 geo->luns_per_chnl = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100929
Javier González8e79b5c2016-11-28 22:39:06 +0100930 /* Generic device values */
931 geo->pgs_per_blk = grp->num_pg;
932 geo->blks_per_lun = grp->num_blk;
933 geo->nr_planes = grp->num_pln;
934 geo->fpg_size = grp->fpg_sz;
935 geo->pfpg_size = grp->fpg_sz * grp->num_pln;
936 geo->sec_size = grp->csecs;
937 geo->oob_size = grp->sos;
938 geo->sec_per_pg = grp->fpg_sz / grp->csecs;
939 geo->mccap = grp->mccap;
940 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
941
942 geo->plane_mode = NVM_PLANE_SINGLE;
943 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100944
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100945 if (grp->mpos & 0x020202)
Javier González8e79b5c2016-11-28 22:39:06 +0100946 geo->plane_mode = NVM_PLANE_DOUBLE;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100947 if (grp->mpos & 0x040404)
Javier González8e79b5c2016-11-28 22:39:06 +0100948 geo->plane_mode = NVM_PLANE_QUAD;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100949
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200950 if (grp->mtype != 0) {
951 pr_err("nvm: memory type not supported\n");
952 return -EINVAL;
953 }
954
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100955 /* calculated values */
Javier González8e79b5c2016-11-28 22:39:06 +0100956 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
957 geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
958 geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
959 geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100960
Javier González8e79b5c2016-11-28 22:39:06 +0100961 dev->total_secs = geo->nr_luns * geo->sec_per_lun;
962 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100963 sizeof(unsigned long), GFP_KERNEL);
964 if (!dev->lun_map)
965 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200966
967 switch (grp->fmtype) {
968 case NVM_ID_FMTYPE_SLC:
969 if (nvm_init_slc_tbl(dev, grp)) {
970 ret = -ENOMEM;
971 goto err_fmtype;
972 }
973 break;
974 case NVM_ID_FMTYPE_MLC:
975 if (nvm_init_mlc_tbl(dev, grp)) {
976 ret = -ENOMEM;
977 goto err_fmtype;
978 }
979 break;
980 default:
981 pr_err("nvm: flash type not supported\n");
982 ret = -EINVAL;
983 goto err_fmtype;
984 }
985
Matias Bjørlingade69e22017-01-31 13:17:09 +0100986 INIT_LIST_HEAD(&dev->area_list);
987 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100988 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100989 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100990
Matias Bjørlingade69e22017-01-31 13:17:09 +0100991 ret = nvm_register_map(dev);
992 if (ret)
993 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200994
Matias Bjørlingade69e22017-01-31 13:17:09 +0100995 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100996 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200997err_fmtype:
998 kfree(dev->lun_map);
999 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001000}
1001
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001002void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001003{
1004 if (!dev)
1005 return;
1006
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001007 if (dev->dma_pool)
1008 dev->ops->destroy_dma_pool(dev->dma_pool);
1009
Javier González7a3de2b2017-04-15 20:55:39 +02001010 nvm_unregister_map(dev);
Matias Bjørlingca5927e2016-01-12 07:49:35 +01001011 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001012 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001013 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001014}
1015
1016static int nvm_init(struct nvm_dev *dev)
1017{
Javier González8e79b5c2016-11-28 22:39:06 +01001018 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001019 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001020
Matias Bjørling16f26c32015-12-06 11:25:48 +01001021 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001022 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001023 goto err;
1024 }
1025
Matias Bjørling19bd6fe2017-01-31 13:17:15 +01001026 pr_debug("nvm: ver:%x nvm_vendor:%x\n",
1027 dev->identity.ver_id, dev->identity.vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001028
1029 if (dev->identity.ver_id != 1) {
1030 pr_err("nvm: device not supported by kernel.");
1031 goto err;
1032 }
1033
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001034 ret = nvm_core_init(dev);
1035 if (ret) {
1036 pr_err("nvm: could not initialize core structures.\n");
1037 goto err;
1038 }
1039
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001040 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +01001041 dev->name, geo->sec_per_pg, geo->nr_planes,
1042 geo->pgs_per_blk, geo->blks_per_lun,
1043 geo->nr_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001044 return 0;
1045err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001046 pr_err("nvm: failed to initialize nvm\n");
1047 return ret;
1048}
1049
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001050struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001051{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001052 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1053}
1054EXPORT_SYMBOL(nvm_alloc_dev);
1055
1056int nvm_register(struct nvm_dev *dev)
1057{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001058 int ret;
1059
Matias Bjørlingade69e22017-01-31 13:17:09 +01001060 if (!dev->q || !dev->ops)
1061 return -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001062
Wenwei Taod1601472015-11-28 16:49:25 +01001063 if (dev->ops->max_phys_sect > 256) {
1064 pr_info("nvm: max sectors supported is 256.\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001065 return -EINVAL;
Wenwei Taod1601472015-11-28 16:49:25 +01001066 }
1067
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001068 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +02001069 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
1070 if (!dev->dma_pool) {
1071 pr_err("nvm: could not create dma pool\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001072 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001073 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001074 }
1075
Matias Bjørlingade69e22017-01-31 13:17:09 +01001076 ret = nvm_init(dev);
1077 if (ret)
1078 goto err_init;
Matias Bjørlingb7692072016-01-12 07:49:38 +01001079
Matias Bjørling762796b2015-12-06 11:25:49 +01001080 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001081 down_write(&nvm_lock);
1082 list_add(&dev->devices, &nvm_devices);
1083 up_write(&nvm_lock);
1084
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001085 return 0;
1086err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +01001087 dev->ops->destroy_dma_pool(dev->dma_pool);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001088 return ret;
1089}
1090EXPORT_SYMBOL(nvm_register);
1091
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001092void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001093{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001094 struct nvm_target *t, *tmp;
1095
1096 mutex_lock(&dev->mlock);
1097 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1098 if (t->dev->parent != dev)
1099 continue;
1100 __nvm_remove_target(t);
1101 }
1102 mutex_unlock(&dev->mlock);
1103
Wenwei Taod0a712c2015-11-28 16:49:28 +01001104 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001105 list_del(&dev->devices);
1106 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001107
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001108 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001109}
1110EXPORT_SYMBOL(nvm_unregister);
1111
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001112static int __nvm_configure_create(struct nvm_ioctl_create *create)
1113{
1114 struct nvm_dev *dev;
1115 struct nvm_ioctl_create_simple *s;
1116
Wenwei Taod0a712c2015-11-28 16:49:28 +01001117 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001118 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001119 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001120
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001121 if (!dev) {
1122 pr_err("nvm: device not found\n");
1123 return -EINVAL;
1124 }
1125
1126 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
1127 pr_err("nvm: config type not valid\n");
1128 return -EINVAL;
1129 }
1130 s = &create->conf.s;
1131
Matias Bjørling6732c742017-02-15 16:25:33 +01001132 if (s->lun_begin == -1 && s->lun_end == -1) {
1133 s->lun_begin = 0;
1134 s->lun_end = dev->geo.nr_luns - 1;
1135 }
1136
Matias Bjørling0e5ffd12017-02-15 16:25:32 +01001137 if (s->lun_begin > s->lun_end || s->lun_end >= dev->geo.nr_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001138 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Matias Bjørling0e5ffd12017-02-15 16:25:32 +01001139 s->lun_begin, s->lun_end, dev->geo.nr_luns - 1);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001140 return -EINVAL;
1141 }
1142
Matias Bjørlingade69e22017-01-31 13:17:09 +01001143 return nvm_create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001144}
1145
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001146static long nvm_ioctl_info(struct file *file, void __user *arg)
1147{
1148 struct nvm_ioctl_info *info;
1149 struct nvm_tgt_type *tt;
1150 int tgt_iter = 0;
1151
1152 if (!capable(CAP_SYS_ADMIN))
1153 return -EPERM;
1154
1155 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1156 if (IS_ERR(info))
1157 return -EFAULT;
1158
1159 info->version[0] = NVM_VERSION_MAJOR;
1160 info->version[1] = NVM_VERSION_MINOR;
1161 info->version[2] = NVM_VERSION_PATCH;
1162
1163 down_write(&nvm_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001164 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001165 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1166
1167 tgt->version[0] = tt->version[0];
1168 tgt->version[1] = tt->version[1];
1169 tgt->version[2] = tt->version[2];
1170 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1171
1172 tgt_iter++;
1173 }
1174
1175 info->tgtsize = tgt_iter;
1176 up_write(&nvm_lock);
1177
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001178 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1179 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001180 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001181 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001182
1183 kfree(info);
1184 return 0;
1185}
1186
1187static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1188{
1189 struct nvm_ioctl_get_devices *devices;
1190 struct nvm_dev *dev;
1191 int i = 0;
1192
1193 if (!capable(CAP_SYS_ADMIN))
1194 return -EPERM;
1195
1196 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1197 if (!devices)
1198 return -ENOMEM;
1199
1200 down_write(&nvm_lock);
1201 list_for_each_entry(dev, &nvm_devices, devices) {
1202 struct nvm_ioctl_device_info *info = &devices->info[i];
1203
1204 sprintf(info->devname, "%s", dev->name);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001205
Matias Bjørlingade69e22017-01-31 13:17:09 +01001206 /* kept for compatibility */
1207 info->bmversion[0] = 1;
1208 info->bmversion[1] = 0;
1209 info->bmversion[2] = 0;
1210 sprintf(info->bmname, "%s", "gennvm");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001211 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001212
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001213 if (i > 31) {
1214 pr_err("nvm: max 31 devices can be reported.\n");
1215 break;
1216 }
1217 }
1218 up_write(&nvm_lock);
1219
1220 devices->nr_devices = i;
1221
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001222 if (copy_to_user(arg, devices,
1223 sizeof(struct nvm_ioctl_get_devices))) {
1224 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001225 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001226 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001227
1228 kfree(devices);
1229 return 0;
1230}
1231
1232static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1233{
1234 struct nvm_ioctl_create create;
1235
1236 if (!capable(CAP_SYS_ADMIN))
1237 return -EPERM;
1238
1239 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1240 return -EFAULT;
1241
1242 create.dev[DISK_NAME_LEN - 1] = '\0';
1243 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1244 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1245
1246 if (create.flags != 0) {
1247 pr_err("nvm: no flags supported\n");
1248 return -EINVAL;
1249 }
1250
1251 return __nvm_configure_create(&create);
1252}
1253
1254static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1255{
1256 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001257 struct nvm_dev *dev;
1258 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001259
1260 if (!capable(CAP_SYS_ADMIN))
1261 return -EPERM;
1262
1263 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1264 return -EFAULT;
1265
1266 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1267
1268 if (remove.flags != 0) {
1269 pr_err("nvm: no flags supported\n");
1270 return -EINVAL;
1271 }
1272
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001273 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001274 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001275 if (!ret)
1276 break;
1277 }
1278
1279 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001280}
1281
Matias Bjørlingade69e22017-01-31 13:17:09 +01001282/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001283static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1284{
1285 struct nvm_ioctl_dev_init init;
1286
1287 if (!capable(CAP_SYS_ADMIN))
1288 return -EPERM;
1289
1290 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1291 return -EFAULT;
1292
1293 if (init.flags != 0) {
1294 pr_err("nvm: no flags supported\n");
1295 return -EINVAL;
1296 }
1297
Matias Bjørlingade69e22017-01-31 13:17:09 +01001298 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001299}
1300
Matias Bjørlingade69e22017-01-31 13:17:09 +01001301/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001302static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1303{
1304 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001305
1306 if (!capable(CAP_SYS_ADMIN))
1307 return -EPERM;
1308
1309 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1310 return -EFAULT;
1311
1312 fact.dev[DISK_NAME_LEN - 1] = '\0';
1313
1314 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1315 return -EINVAL;
1316
Matias Bjørlingbf643182016-02-04 15:13:27 +01001317 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001318}
1319
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001320static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1321{
1322 void __user *argp = (void __user *)arg;
1323
1324 switch (cmd) {
1325 case NVM_INFO:
1326 return nvm_ioctl_info(file, argp);
1327 case NVM_GET_DEVICES:
1328 return nvm_ioctl_get_devices(file, argp);
1329 case NVM_DEV_CREATE:
1330 return nvm_ioctl_dev_create(file, argp);
1331 case NVM_DEV_REMOVE:
1332 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001333 case NVM_DEV_INIT:
1334 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001335 case NVM_DEV_FACTORY:
1336 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001337 }
1338 return 0;
1339}
1340
1341static const struct file_operations _ctl_fops = {
1342 .open = nonseekable_open,
1343 .unlocked_ioctl = nvm_ctl_ioctl,
1344 .owner = THIS_MODULE,
1345 .llseek = noop_llseek,
1346};
1347
1348static struct miscdevice _nvm_misc = {
1349 .minor = MISC_DYNAMIC_MINOR,
1350 .name = "lightnvm",
1351 .nodename = "lightnvm/control",
1352 .fops = &_ctl_fops,
1353};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001354builtin_misc_device(_nvm_misc);