blob: 18d48732fe74565cef3df3735f1784be77b736fe [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
53enum {
54 TRANS_TGT_TO_DEV = 0x0,
55 TRANS_DEV_TO_TGT = 0x1,
56};
57
58static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
59{
60 struct nvm_target *tgt;
61
62 list_for_each_entry(tgt, &dev->targets, list)
63 if (!strcmp(name, tgt->disk->disk_name))
64 return tgt;
65
66 return NULL;
67}
68
69static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
70{
71 int i;
72
73 for (i = lun_begin; i <= lun_end; i++) {
74 if (test_and_set_bit(i, dev->lun_map)) {
75 pr_err("nvm: lun %d already allocated\n", i);
76 goto err;
77 }
78 }
79
80 return 0;
81err:
82 while (--i > lun_begin)
83 clear_bit(i, dev->lun_map);
84
85 return -EBUSY;
86}
87
88static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
89 int lun_end)
90{
91 int i;
92
93 for (i = lun_begin; i <= lun_end; i++)
94 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
95}
96
97static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev)
98{
99 struct nvm_dev *dev = tgt_dev->parent;
100 struct nvm_dev_map *dev_map = tgt_dev->map;
101 int i, j;
102
103 for (i = 0; i < dev_map->nr_chnls; i++) {
104 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
105 int *lun_offs = ch_map->lun_offs;
106 int ch = i + ch_map->ch_off;
107
108 for (j = 0; j < ch_map->nr_luns; j++) {
109 int lun = j + lun_offs[j];
110 int lunid = (ch * dev->geo.luns_per_chnl) + lun;
111
112 WARN_ON(!test_and_clear_bit(lunid, dev->lun_map));
113 }
114
115 kfree(ch_map->lun_offs);
116 }
117
118 kfree(dev_map->chnls);
119 kfree(dev_map);
120
121 kfree(tgt_dev->luns);
122 kfree(tgt_dev);
123}
124
125static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
126 int lun_begin, int lun_end)
127{
128 struct nvm_tgt_dev *tgt_dev = NULL;
129 struct nvm_dev_map *dev_rmap = dev->rmap;
130 struct nvm_dev_map *dev_map;
131 struct ppa_addr *luns;
132 int nr_luns = lun_end - lun_begin + 1;
133 int luns_left = nr_luns;
134 int nr_chnls = nr_luns / dev->geo.luns_per_chnl;
135 int nr_chnls_mod = nr_luns % dev->geo.luns_per_chnl;
136 int bch = lun_begin / dev->geo.luns_per_chnl;
137 int blun = lun_begin % dev->geo.luns_per_chnl;
138 int lunid = 0;
139 int lun_balanced = 1;
140 int prev_nr_luns;
141 int i, j;
142
143 nr_chnls = nr_luns / dev->geo.luns_per_chnl;
144 nr_chnls = (nr_chnls_mod == 0) ? nr_chnls : nr_chnls + 1;
145
146 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
147 if (!dev_map)
148 goto err_dev;
149
150 dev_map->chnls = kcalloc(nr_chnls, sizeof(struct nvm_ch_map),
151 GFP_KERNEL);
152 if (!dev_map->chnls)
153 goto err_chnls;
154
155 luns = kcalloc(nr_luns, sizeof(struct ppa_addr), GFP_KERNEL);
156 if (!luns)
157 goto err_luns;
158
159 prev_nr_luns = (luns_left > dev->geo.luns_per_chnl) ?
160 dev->geo.luns_per_chnl : luns_left;
161 for (i = 0; i < nr_chnls; i++) {
162 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
163 int *lun_roffs = ch_rmap->lun_offs;
164 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
165 int *lun_offs;
166 int luns_in_chnl = (luns_left > dev->geo.luns_per_chnl) ?
167 dev->geo.luns_per_chnl : luns_left;
168
169 if (lun_balanced && prev_nr_luns != luns_in_chnl)
170 lun_balanced = 0;
171
172 ch_map->ch_off = ch_rmap->ch_off = bch;
173 ch_map->nr_luns = luns_in_chnl;
174
175 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
176 if (!lun_offs)
177 goto err_ch;
178
179 for (j = 0; j < luns_in_chnl; j++) {
180 luns[lunid].ppa = 0;
181 luns[lunid].g.ch = i;
182 luns[lunid++].g.lun = j;
183
184 lun_offs[j] = blun;
185 lun_roffs[j + blun] = blun;
186 }
187
188 ch_map->lun_offs = lun_offs;
189
190 /* when starting a new channel, lun offset is reset */
191 blun = 0;
192 luns_left -= luns_in_chnl;
193 }
194
195 dev_map->nr_chnls = nr_chnls;
196
197 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
198 if (!tgt_dev)
199 goto err_ch;
200
201 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
202 /* Target device only owns a portion of the physical device */
203 tgt_dev->geo.nr_chnls = nr_chnls;
204 tgt_dev->geo.nr_luns = nr_luns;
205 tgt_dev->geo.luns_per_chnl = (lun_balanced) ? prev_nr_luns : -1;
206 tgt_dev->total_secs = nr_luns * tgt_dev->geo.sec_per_lun;
207 tgt_dev->q = dev->q;
208 tgt_dev->map = dev_map;
209 tgt_dev->luns = luns;
210 memcpy(&tgt_dev->identity, &dev->identity, sizeof(struct nvm_id));
211
212 tgt_dev->parent = dev;
213
214 return tgt_dev;
215err_ch:
216 while (--i > 0)
217 kfree(dev_map->chnls[i].lun_offs);
218 kfree(luns);
219err_luns:
220 kfree(dev_map->chnls);
221err_chnls:
222 kfree(dev_map);
223err_dev:
224 return tgt_dev;
225}
226
227static const struct block_device_operations nvm_fops = {
228 .owner = THIS_MODULE,
229};
230
231static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
232{
233 struct nvm_ioctl_create_simple *s = &create->conf.s;
234 struct request_queue *tqueue;
235 struct gendisk *tdisk;
236 struct nvm_tgt_type *tt;
237 struct nvm_target *t;
238 struct nvm_tgt_dev *tgt_dev;
239 void *targetdata;
240
241 tt = nvm_find_target_type(create->tgttype, 1);
242 if (!tt) {
243 pr_err("nvm: target type %s not found\n", create->tgttype);
244 return -EINVAL;
245 }
246
247 mutex_lock(&dev->mlock);
248 t = nvm_find_target(dev, create->tgtname);
249 if (t) {
250 pr_err("nvm: target name already exists.\n");
251 mutex_unlock(&dev->mlock);
252 return -EINVAL;
253 }
254 mutex_unlock(&dev->mlock);
255
256 if (nvm_reserve_luns(dev, s->lun_begin, s->lun_end))
257 return -ENOMEM;
258
259 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
260 if (!t)
261 goto err_reserve;
262
263 tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end);
264 if (!tgt_dev) {
265 pr_err("nvm: could not create target device\n");
266 goto err_t;
267 }
268
269 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
270 if (!tqueue)
271 goto err_dev;
272 blk_queue_make_request(tqueue, tt->make_rq);
273
274 tdisk = alloc_disk(0);
275 if (!tdisk)
276 goto err_queue;
277
278 sprintf(tdisk->disk_name, "%s", create->tgtname);
279 tdisk->flags = GENHD_FL_EXT_DEVT;
280 tdisk->major = 0;
281 tdisk->first_minor = 0;
282 tdisk->fops = &nvm_fops;
283 tdisk->queue = tqueue;
284
285 targetdata = tt->init(tgt_dev, tdisk);
286 if (IS_ERR(targetdata))
287 goto err_init;
288
289 tdisk->private_data = targetdata;
290 tqueue->queuedata = targetdata;
291
292 blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
293
294 set_capacity(tdisk, tt->capacity(targetdata));
295 add_disk(tdisk);
296
297 t->type = tt;
298 t->disk = tdisk;
299 t->dev = tgt_dev;
300
301 mutex_lock(&dev->mlock);
302 list_add_tail(&t->list, &dev->targets);
303 mutex_unlock(&dev->mlock);
304
305 return 0;
306err_init:
307 put_disk(tdisk);
308err_queue:
309 blk_cleanup_queue(tqueue);
310err_dev:
311 kfree(tgt_dev);
312err_t:
313 kfree(t);
314err_reserve:
315 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
316 return -ENOMEM;
317}
318
319static void __nvm_remove_target(struct nvm_target *t)
320{
321 struct nvm_tgt_type *tt = t->type;
322 struct gendisk *tdisk = t->disk;
323 struct request_queue *q = tdisk->queue;
324
325 del_gendisk(tdisk);
326 blk_cleanup_queue(q);
327
328 if (tt->exit)
329 tt->exit(tdisk->private_data);
330
331 nvm_remove_tgt_dev(t->dev);
332 put_disk(tdisk);
333
334 list_del(&t->list);
335 kfree(t);
336}
337
338/**
339 * nvm_remove_tgt - Removes a target from the media manager
340 * @dev: device
341 * @remove: ioctl structure with target name to remove.
342 *
343 * Returns:
344 * 0: on success
345 * 1: on not found
346 * <0: on error
347 */
348static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
349{
350 struct nvm_target *t;
351
352 mutex_lock(&dev->mlock);
353 t = nvm_find_target(dev, remove->tgtname);
354 if (!t) {
355 mutex_unlock(&dev->mlock);
356 return 1;
357 }
358 __nvm_remove_target(t);
359 mutex_unlock(&dev->mlock);
360
361 return 0;
362}
363
364static int nvm_register_map(struct nvm_dev *dev)
365{
366 struct nvm_dev_map *rmap;
367 int i, j;
368
369 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
370 if (!rmap)
371 goto err_rmap;
372
373 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
374 GFP_KERNEL);
375 if (!rmap->chnls)
376 goto err_chnls;
377
378 for (i = 0; i < dev->geo.nr_chnls; i++) {
379 struct nvm_ch_map *ch_rmap;
380 int *lun_roffs;
381 int luns_in_chnl = dev->geo.luns_per_chnl;
382
383 ch_rmap = &rmap->chnls[i];
384
385 ch_rmap->ch_off = -1;
386 ch_rmap->nr_luns = luns_in_chnl;
387
388 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
389 if (!lun_roffs)
390 goto err_ch;
391
392 for (j = 0; j < luns_in_chnl; j++)
393 lun_roffs[j] = -1;
394
395 ch_rmap->lun_offs = lun_roffs;
396 }
397
398 dev->rmap = rmap;
399
400 return 0;
401err_ch:
402 while (--i >= 0)
403 kfree(rmap->chnls[i].lun_offs);
404err_chnls:
405 kfree(rmap);
406err_rmap:
407 return -ENOMEM;
408}
409
410static int nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
411{
412 struct nvm_dev_map *dev_map = tgt_dev->map;
413 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
414 int lun_off = ch_map->lun_offs[p->g.lun];
415 struct nvm_dev *dev = tgt_dev->parent;
416 struct nvm_dev_map *dev_rmap = dev->rmap;
417 struct nvm_ch_map *ch_rmap;
418 int lun_roff;
419
420 p->g.ch += ch_map->ch_off;
421 p->g.lun += lun_off;
422
423 ch_rmap = &dev_rmap->chnls[p->g.ch];
424 lun_roff = ch_rmap->lun_offs[p->g.lun];
425
426 if (unlikely(ch_rmap->ch_off < 0 || lun_roff < 0)) {
427 pr_err("nvm: corrupted device partition table\n");
428 return -EINVAL;
429 }
430
431 return 0;
432}
433
434static int nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
435{
436 struct nvm_dev *dev = tgt_dev->parent;
437 struct nvm_dev_map *dev_rmap = dev->rmap;
438 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
439 int lun_roff = ch_rmap->lun_offs[p->g.lun];
440
441 p->g.ch -= ch_rmap->ch_off;
442 p->g.lun -= lun_roff;
443
444 return 0;
445}
446
447static int nvm_trans_rq(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
448 int flag)
449{
450 int i;
451 int ret;
452
453 if (rqd->nr_ppas == 1) {
454 if (flag == TRANS_TGT_TO_DEV)
455 return nvm_map_to_dev(tgt_dev, &rqd->ppa_addr);
456 else
457 return nvm_map_to_tgt(tgt_dev, &rqd->ppa_addr);
458 }
459
460 for (i = 0; i < rqd->nr_ppas; i++) {
461 if (flag == TRANS_TGT_TO_DEV)
462 ret = nvm_map_to_dev(tgt_dev, &rqd->ppa_list[i]);
463 else
464 ret = nvm_map_to_tgt(tgt_dev, &rqd->ppa_list[i]);
465
466 if (ret)
467 break;
468 }
469
470 return ret;
471}
472
473static struct ppa_addr nvm_trans_ppa(struct nvm_tgt_dev *tgt_dev,
474 struct ppa_addr p, int dir)
475{
476 struct ppa_addr ppa = p;
477
478 if (dir == TRANS_TGT_TO_DEV)
479 nvm_map_to_dev(tgt_dev, &ppa);
480 else
481 nvm_map_to_tgt(tgt_dev, &ppa);
482
483 return ppa;
484}
485
486void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries,
487 int len)
488{
489 struct nvm_geo *geo = &dev->geo;
490 struct nvm_dev_map *dev_rmap = dev->rmap;
491 u64 i;
492
493 for (i = 0; i < len; i++) {
494 struct nvm_ch_map *ch_rmap;
495 int *lun_roffs;
496 struct ppa_addr gaddr;
497 u64 pba = le64_to_cpu(entries[i]);
498 int off;
499 u64 diff;
500
501 if (!pba)
502 continue;
503
504 gaddr = linear_to_generic_addr(geo, pba);
505 ch_rmap = &dev_rmap->chnls[gaddr.g.ch];
506 lun_roffs = ch_rmap->lun_offs;
507
508 off = gaddr.g.ch * geo->luns_per_chnl + gaddr.g.lun;
509
510 diff = ((ch_rmap->ch_off * geo->luns_per_chnl) +
511 (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun;
512
513 entries[i] -= cpu_to_le64(diff);
514 }
515}
516EXPORT_SYMBOL(nvm_part_to_tgt);
517
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200518struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200519{
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200520 struct nvm_tgt_type *tmp, *tt = NULL;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200521
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200522 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200523 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200524
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200525 list_for_each_entry(tmp, &nvm_tgt_types, list)
526 if (!strcmp(name, tmp->name)) {
527 tt = tmp;
528 break;
529 }
530
531 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200532 up_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200533 return tt;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200534}
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200535EXPORT_SYMBOL(nvm_find_target_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100536
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200537int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100538{
539 int ret = 0;
540
Matias Bjørling5cd90782016-07-07 09:54:17 +0200541 down_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200542 if (nvm_find_target_type(tt->name, 0))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100543 ret = -EEXIST;
544 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200545 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200546 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100547
548 return ret;
549}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200550EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100551
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200552void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100553{
554 if (!tt)
555 return;
556
557 down_write(&nvm_lock);
558 list_del(&tt->list);
559 up_write(&nvm_lock);
560}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200561EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100562
563void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
564 dma_addr_t *dma_handler)
565{
Javier González75b85642016-05-06 20:03:13 +0200566 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100567 dma_handler);
568}
569EXPORT_SYMBOL(nvm_dev_dma_alloc);
570
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100571void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100572{
Javier González75b85642016-05-06 20:03:13 +0200573 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100574}
575EXPORT_SYMBOL(nvm_dev_dma_free);
576
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100577static struct nvm_dev *nvm_find_nvm_dev(const char *name)
578{
579 struct nvm_dev *dev;
580
581 list_for_each_entry(dev, &nvm_devices, devices)
582 if (!strcmp(name, dev->name))
583 return dev;
584
585 return NULL;
586}
587
Javier González333ba052016-11-28 22:39:14 +0100588static void nvm_tgt_generic_to_addr_mode(struct nvm_tgt_dev *tgt_dev,
589 struct nvm_rq *rqd)
590{
591 struct nvm_dev *dev = tgt_dev->parent;
592 int i;
593
594 if (rqd->nr_ppas > 1) {
595 for (i = 0; i < rqd->nr_ppas; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100596 rqd->ppa_list[i] = nvm_trans_ppa(tgt_dev,
Javier González333ba052016-11-28 22:39:14 +0100597 rqd->ppa_list[i], TRANS_TGT_TO_DEV);
598 rqd->ppa_list[i] = generic_to_dev_addr(dev,
599 rqd->ppa_list[i]);
600 }
601 } else {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100602 rqd->ppa_addr = nvm_trans_ppa(tgt_dev, rqd->ppa_addr,
Javier González333ba052016-11-28 22:39:14 +0100603 TRANS_TGT_TO_DEV);
604 rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr);
605 }
606}
607
Javier González333ba052016-11-28 22:39:14 +0100608int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
609 int nr_ppas, int type)
610{
611 struct nvm_dev *dev = tgt_dev->parent;
612 struct nvm_rq rqd;
613 int ret;
614
615 if (nr_ppas > dev->ops->max_phys_sect) {
616 pr_err("nvm: unable to update all blocks atomically\n");
617 return -EINVAL;
618 }
619
620 memset(&rqd, 0, sizeof(struct nvm_rq));
621
622 nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
623 nvm_tgt_generic_to_addr_mode(tgt_dev, &rqd);
624
625 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
626 nvm_free_rqd_ppalist(dev, &rqd);
627 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100628 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100629 return -EINVAL;
630 }
631
632 return 0;
633}
634EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
635
Javier Gonzáleza2790062016-11-28 22:39:12 +0100636int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
637{
638 struct nvm_dev *dev = tgt_dev->parent;
639
640 return dev->ops->max_phys_sect;
641}
642EXPORT_SYMBOL(nvm_max_phys_sects);
643
Javier González8e536242016-11-28 22:39:10 +0100644int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100645{
Javier González8e536242016-11-28 22:39:10 +0100646 struct nvm_dev *dev = tgt_dev->parent;
647
Matias Bjørlingade69e22017-01-31 13:17:09 +0100648 if (!dev->ops->submit_io)
649 return -ENODEV;
650
651 /* Convert address space */
652 nvm_generic_to_addr_mode(dev, rqd);
653
654 rqd->dev = tgt_dev;
655 return dev->ops->submit_io(dev, rqd);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100656}
657EXPORT_SYMBOL(nvm_submit_io);
658
Matias Bjørling10995c32017-01-31 13:17:10 +0100659int nvm_erase_blk(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas, int flags)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100660{
Matias Bjørling10995c32017-01-31 13:17:10 +0100661 struct nvm_dev *dev = tgt_dev->parent;
662 struct nvm_rq rqd;
663 int ret;
Javier González8e536242016-11-28 22:39:10 +0100664
Matias Bjørling10995c32017-01-31 13:17:10 +0100665 if (!dev->ops->erase_block)
666 return 0;
667
668 ret = nvm_map_to_dev(tgt_dev, ppas);
669 if (ret)
670 return ret;
671
672 memset(&rqd, 0, sizeof(struct nvm_rq));
673
674 ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, 1, 1);
675 if (ret)
676 return ret;
677
678 nvm_generic_to_addr_mode(dev, &rqd);
679
680 rqd.flags = flags;
681
682 ret = dev->ops->erase_block(dev, &rqd);
683
684 nvm_free_rqd_ppalist(dev, &rqd);
685
686 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100687}
688EXPORT_SYMBOL(nvm_erase_blk);
689
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100690int nvm_get_l2p_tbl(struct nvm_tgt_dev *tgt_dev, u64 slba, u32 nlb,
Javier González959e9112016-11-28 22:39:11 +0100691 nvm_l2p_update_fn *update_l2p, void *priv)
692{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100693 struct nvm_dev *dev = tgt_dev->parent;
694
Javier González959e9112016-11-28 22:39:11 +0100695 if (!dev->ops->get_l2p_tbl)
696 return 0;
697
698 return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv);
699}
700EXPORT_SYMBOL(nvm_get_l2p_tbl);
701
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100702int nvm_get_area(struct nvm_tgt_dev *tgt_dev, sector_t *lba, sector_t len)
Javier González959e9112016-11-28 22:39:11 +0100703{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100704 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100705 struct nvm_geo *geo = &dev->geo;
706 struct nvm_area *area, *prev, *next;
707 sector_t begin = 0;
708 sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100709
Matias Bjørlingade69e22017-01-31 13:17:09 +0100710 if (len > max_sectors)
711 return -EINVAL;
712
713 area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL);
714 if (!area)
715 return -ENOMEM;
716
717 prev = NULL;
718
719 spin_lock(&dev->lock);
720 list_for_each_entry(next, &dev->area_list, list) {
721 if (begin + len > next->begin) {
722 begin = next->end;
723 prev = next;
724 continue;
725 }
726 break;
727 }
728
729 if ((begin + len) > max_sectors) {
730 spin_unlock(&dev->lock);
731 kfree(area);
732 return -EINVAL;
733 }
734
735 area->begin = *lba = begin;
736 area->end = begin + len;
737
738 if (prev) /* insert into sorted order */
739 list_add(&area->list, &prev->list);
740 else
741 list_add(&area->list, &dev->area_list);
742 spin_unlock(&dev->lock);
743
744 return 0;
Javier González959e9112016-11-28 22:39:11 +0100745}
746EXPORT_SYMBOL(nvm_get_area);
747
Matias Bjørlingade69e22017-01-31 13:17:09 +0100748void nvm_put_area(struct nvm_tgt_dev *tgt_dev, sector_t begin)
Javier González959e9112016-11-28 22:39:11 +0100749{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100750 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100751 struct nvm_area *area;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100752
Matias Bjørlingade69e22017-01-31 13:17:09 +0100753 spin_lock(&dev->lock);
754 list_for_each_entry(area, &dev->area_list, list) {
755 if (area->begin != begin)
756 continue;
757
758 list_del(&area->list);
759 spin_unlock(&dev->lock);
760 kfree(area);
761 return;
762 }
763 spin_unlock(&dev->lock);
Javier González959e9112016-11-28 22:39:11 +0100764}
765EXPORT_SYMBOL(nvm_put_area);
766
Matias Bjørling069368e2016-01-12 07:49:19 +0100767void nvm_addr_to_generic_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
768{
769 int i;
770
Javier González6d5be952016-05-06 20:03:20 +0200771 if (rqd->nr_ppas > 1) {
772 for (i = 0; i < rqd->nr_ppas; i++)
Matias Bjørling069368e2016-01-12 07:49:19 +0100773 rqd->ppa_list[i] = dev_to_generic_addr(dev,
774 rqd->ppa_list[i]);
775 } else {
776 rqd->ppa_addr = dev_to_generic_addr(dev, rqd->ppa_addr);
777 }
778}
779EXPORT_SYMBOL(nvm_addr_to_generic_mode);
780
781void nvm_generic_to_addr_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
782{
783 int i;
784
Javier González6d5be952016-05-06 20:03:20 +0200785 if (rqd->nr_ppas > 1) {
786 for (i = 0; i < rqd->nr_ppas; i++)
Matias Bjørling069368e2016-01-12 07:49:19 +0100787 rqd->ppa_list[i] = generic_to_dev_addr(dev,
788 rqd->ppa_list[i]);
789 } else {
790 rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr);
791 }
792}
793EXPORT_SYMBOL(nvm_generic_to_addr_mode);
794
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100795int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd,
Matias Bjørling8680f162016-07-07 09:54:22 +0200796 const struct ppa_addr *ppas, int nr_ppas, int vblk)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100797{
Javier González8e79b5c2016-11-28 22:39:06 +0100798 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100799 int i, plane_cnt, pl_idx;
Matias Bjørling8680f162016-07-07 09:54:22 +0200800 struct ppa_addr ppa;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100801
Javier González8e79b5c2016-11-28 22:39:06 +0100802 if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
Javier González6d5be952016-05-06 20:03:20 +0200803 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100804 rqd->ppa_addr = ppas[0];
805
806 return 0;
807 }
808
Javier González6d5be952016-05-06 20:03:20 +0200809 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100810 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
811 if (!rqd->ppa_list) {
812 pr_err("nvm: failed to allocate dma memory\n");
813 return -ENOMEM;
814 }
815
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200816 if (!vblk) {
817 for (i = 0; i < nr_ppas; i++)
818 rqd->ppa_list[i] = ppas[i];
819 } else {
Javier González8e79b5c2016-11-28 22:39:06 +0100820 plane_cnt = geo->plane_mode;
Javier González6d5be952016-05-06 20:03:20 +0200821 rqd->nr_ppas *= plane_cnt;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200822
Matias Bjørling556755e2016-01-12 07:49:26 +0100823 for (i = 0; i < nr_ppas; i++) {
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200824 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
Matias Bjørling8680f162016-07-07 09:54:22 +0200825 ppa = ppas[i];
826 ppa.g.pl = pl_idx;
827 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200828 }
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100829 }
830 }
831
832 return 0;
833}
834EXPORT_SYMBOL(nvm_set_rqd_ppalist);
835
836void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd)
837{
838 if (!rqd->ppa_list)
839 return;
840
841 nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list);
842}
843EXPORT_SYMBOL(nvm_free_rqd_ppalist);
844
Matias Bjørling912761622016-01-12 07:49:21 +0100845void nvm_end_io(struct nvm_rq *rqd, int error)
846{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100847 struct nvm_tgt_dev *tgt_dev = rqd->dev;
848 struct nvm_tgt_instance *ins = rqd->ins;
849
850 /* Convert address space */
851 if (tgt_dev)
852 nvm_trans_rq(tgt_dev, rqd, TRANS_DEV_TO_TGT);
853
Matias Bjørling72d256e2016-01-12 07:49:29 +0100854 rqd->error = error;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100855 ins->tt->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100856}
857EXPORT_SYMBOL(nvm_end_io);
858
Matias Bjørling22e8c972016-05-06 20:02:58 +0200859/*
860 * folds a bad block list from its plane representation to its virtual
861 * block representation. The fold is done in place and reduced size is
862 * returned.
863 *
864 * If any of the planes status are bad or grown bad block, the virtual block
865 * is marked bad. If not bad, the first plane state acts as the block state.
866 */
867int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
868{
Javier González8e79b5c2016-11-28 22:39:06 +0100869 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200870 int blk, offset, pl, blktype;
871
Javier González8e79b5c2016-11-28 22:39:06 +0100872 if (nr_blks != geo->blks_per_lun * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200873 return -EINVAL;
874
Javier González8e79b5c2016-11-28 22:39:06 +0100875 for (blk = 0; blk < geo->blks_per_lun; blk++) {
876 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200877 blktype = blks[offset];
878
879 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100880 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200881 if (blks[offset + pl] &
882 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
883 blktype = blks[offset + pl];
884 break;
885 }
886 }
887
888 blks[blk] = blktype;
889 }
890
Javier González8e79b5c2016-11-28 22:39:06 +0100891 return geo->blks_per_lun;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200892}
893EXPORT_SYMBOL(nvm_bb_tbl_fold);
894
Javier González333ba052016-11-28 22:39:14 +0100895int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
896 u8 *blks)
897{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100898 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100899
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100900 ppa = nvm_trans_ppa(tgt_dev, ppa, TRANS_TGT_TO_DEV);
901 ppa = generic_to_dev_addr(dev, ppa);
902
903 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100904}
905EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
906
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100907static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
908{
Javier González8e79b5c2016-11-28 22:39:06 +0100909 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100910 int i;
911
Javier González8e79b5c2016-11-28 22:39:06 +0100912 dev->lps_per_blk = geo->pgs_per_blk;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100913 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
914 if (!dev->lptbl)
915 return -ENOMEM;
916
917 /* Just a linear array */
918 for (i = 0; i < dev->lps_per_blk; i++)
919 dev->lptbl[i] = i;
920
921 return 0;
922}
923
924static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
925{
926 int i, p;
927 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
928
929 if (!mlc->num_pairs)
930 return 0;
931
932 dev->lps_per_blk = mlc->num_pairs;
933 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
934 if (!dev->lptbl)
935 return -ENOMEM;
936
937 /* The lower page table encoding consists of a list of bytes, where each
938 * has a lower and an upper half. The first half byte maintains the
939 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200940 * previous incrementation value
941 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100942 dev->lptbl[0] = mlc->pairs[0] & 0xF;
943 for (i = 1; i < dev->lps_per_blk; i++) {
944 p = mlc->pairs[i >> 1];
945 if (i & 0x1) /* upper */
946 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
947 else /* lower */
948 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
949 }
950
951 return 0;
952}
953
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100954static int nvm_core_init(struct nvm_dev *dev)
955{
956 struct nvm_id *id = &dev->identity;
957 struct nvm_id_group *grp = &id->groups[0];
Javier González8e79b5c2016-11-28 22:39:06 +0100958 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200959 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100960
Javier González8e79b5c2016-11-28 22:39:06 +0100961 /* Whole device values */
962 geo->nr_chnls = grp->num_ch;
963 geo->luns_per_chnl = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100964
Javier González8e79b5c2016-11-28 22:39:06 +0100965 /* Generic device values */
966 geo->pgs_per_blk = grp->num_pg;
967 geo->blks_per_lun = grp->num_blk;
968 geo->nr_planes = grp->num_pln;
969 geo->fpg_size = grp->fpg_sz;
970 geo->pfpg_size = grp->fpg_sz * grp->num_pln;
971 geo->sec_size = grp->csecs;
972 geo->oob_size = grp->sos;
973 geo->sec_per_pg = grp->fpg_sz / grp->csecs;
974 geo->mccap = grp->mccap;
975 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
976
977 geo->plane_mode = NVM_PLANE_SINGLE;
978 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100979
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100980 if (grp->mpos & 0x020202)
Javier González8e79b5c2016-11-28 22:39:06 +0100981 geo->plane_mode = NVM_PLANE_DOUBLE;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100982 if (grp->mpos & 0x040404)
Javier González8e79b5c2016-11-28 22:39:06 +0100983 geo->plane_mode = NVM_PLANE_QUAD;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100984
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200985 if (grp->mtype != 0) {
986 pr_err("nvm: memory type not supported\n");
987 return -EINVAL;
988 }
989
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100990 /* calculated values */
Javier González8e79b5c2016-11-28 22:39:06 +0100991 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
992 geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
993 geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
994 geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100995
Javier González8e79b5c2016-11-28 22:39:06 +0100996 dev->total_secs = geo->nr_luns * geo->sec_per_lun;
997 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100998 sizeof(unsigned long), GFP_KERNEL);
999 if (!dev->lun_map)
1000 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001001
1002 switch (grp->fmtype) {
1003 case NVM_ID_FMTYPE_SLC:
1004 if (nvm_init_slc_tbl(dev, grp)) {
1005 ret = -ENOMEM;
1006 goto err_fmtype;
1007 }
1008 break;
1009 case NVM_ID_FMTYPE_MLC:
1010 if (nvm_init_mlc_tbl(dev, grp)) {
1011 ret = -ENOMEM;
1012 goto err_fmtype;
1013 }
1014 break;
1015 default:
1016 pr_err("nvm: flash type not supported\n");
1017 ret = -EINVAL;
1018 goto err_fmtype;
1019 }
1020
Matias Bjørlingade69e22017-01-31 13:17:09 +01001021 INIT_LIST_HEAD(&dev->area_list);
1022 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +01001023 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001024 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001025
Matias Bjørlingade69e22017-01-31 13:17:09 +01001026 ret = nvm_register_map(dev);
1027 if (ret)
1028 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001029
Matias Bjørlingade69e22017-01-31 13:17:09 +01001030 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001031 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001032err_fmtype:
1033 kfree(dev->lun_map);
1034 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001035}
1036
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001037void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001038{
1039 if (!dev)
1040 return;
1041
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001042 if (dev->dma_pool)
1043 dev->ops->destroy_dma_pool(dev->dma_pool);
1044
Matias Bjørlingade69e22017-01-31 13:17:09 +01001045 kfree(dev->rmap);
Matias Bjørlingca5927e2016-01-12 07:49:35 +01001046 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001047 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001048 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001049}
1050
1051static int nvm_init(struct nvm_dev *dev)
1052{
Javier González8e79b5c2016-11-28 22:39:06 +01001053 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001054 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001055
Matias Bjørling16f26c32015-12-06 11:25:48 +01001056 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001057 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001058 goto err;
1059 }
1060
1061 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
1062 dev->identity.ver_id, dev->identity.vmnt,
1063 dev->identity.cgrps);
1064
1065 if (dev->identity.ver_id != 1) {
1066 pr_err("nvm: device not supported by kernel.");
1067 goto err;
1068 }
1069
1070 if (dev->identity.cgrps != 1) {
1071 pr_err("nvm: only one group configuration supported.");
1072 goto err;
1073 }
1074
1075 ret = nvm_core_init(dev);
1076 if (ret) {
1077 pr_err("nvm: could not initialize core structures.\n");
1078 goto err;
1079 }
1080
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001081 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +01001082 dev->name, geo->sec_per_pg, geo->nr_planes,
1083 geo->pgs_per_blk, geo->blks_per_lun,
1084 geo->nr_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001085 return 0;
1086err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001087 pr_err("nvm: failed to initialize nvm\n");
1088 return ret;
1089}
1090
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001091struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001092{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001093 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1094}
1095EXPORT_SYMBOL(nvm_alloc_dev);
1096
1097int nvm_register(struct nvm_dev *dev)
1098{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001099 int ret;
1100
Matias Bjørlingade69e22017-01-31 13:17:09 +01001101 if (!dev->q || !dev->ops)
1102 return -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001103
Wenwei Taod1601472015-11-28 16:49:25 +01001104 if (dev->ops->max_phys_sect > 256) {
1105 pr_info("nvm: max sectors supported is 256.\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001106 return -EINVAL;
Wenwei Taod1601472015-11-28 16:49:25 +01001107 }
1108
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001109 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +02001110 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
1111 if (!dev->dma_pool) {
1112 pr_err("nvm: could not create dma pool\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001113 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001114 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001115 }
1116
Matias Bjørlingade69e22017-01-31 13:17:09 +01001117 ret = nvm_init(dev);
1118 if (ret)
1119 goto err_init;
Matias Bjørlingb7692072016-01-12 07:49:38 +01001120
Matias Bjørling762796b2015-12-06 11:25:49 +01001121 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001122 down_write(&nvm_lock);
1123 list_add(&dev->devices, &nvm_devices);
1124 up_write(&nvm_lock);
1125
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001126 return 0;
1127err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +01001128 dev->ops->destroy_dma_pool(dev->dma_pool);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001129 return ret;
1130}
1131EXPORT_SYMBOL(nvm_register);
1132
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001133void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001134{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001135 struct nvm_target *t, *tmp;
1136
1137 mutex_lock(&dev->mlock);
1138 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1139 if (t->dev->parent != dev)
1140 continue;
1141 __nvm_remove_target(t);
1142 }
1143 mutex_unlock(&dev->mlock);
1144
Wenwei Taod0a712c2015-11-28 16:49:28 +01001145 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001146 list_del(&dev->devices);
1147 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001148
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001149 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001150}
1151EXPORT_SYMBOL(nvm_unregister);
1152
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001153static int __nvm_configure_create(struct nvm_ioctl_create *create)
1154{
1155 struct nvm_dev *dev;
1156 struct nvm_ioctl_create_simple *s;
1157
Wenwei Taod0a712c2015-11-28 16:49:28 +01001158 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001159 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001160 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001161
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001162 if (!dev) {
1163 pr_err("nvm: device not found\n");
1164 return -EINVAL;
1165 }
1166
1167 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
1168 pr_err("nvm: config type not valid\n");
1169 return -EINVAL;
1170 }
1171 s = &create->conf.s;
1172
Javier González8e79b5c2016-11-28 22:39:06 +01001173 if (s->lun_begin > s->lun_end || s->lun_end > dev->geo.nr_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001174 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Javier González8e79b5c2016-11-28 22:39:06 +01001175 s->lun_begin, s->lun_end, dev->geo.nr_luns);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001176 return -EINVAL;
1177 }
1178
Matias Bjørlingade69e22017-01-31 13:17:09 +01001179 return nvm_create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001180}
1181
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001182static long nvm_ioctl_info(struct file *file, void __user *arg)
1183{
1184 struct nvm_ioctl_info *info;
1185 struct nvm_tgt_type *tt;
1186 int tgt_iter = 0;
1187
1188 if (!capable(CAP_SYS_ADMIN))
1189 return -EPERM;
1190
1191 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1192 if (IS_ERR(info))
1193 return -EFAULT;
1194
1195 info->version[0] = NVM_VERSION_MAJOR;
1196 info->version[1] = NVM_VERSION_MINOR;
1197 info->version[2] = NVM_VERSION_PATCH;
1198
1199 down_write(&nvm_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001200 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001201 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1202
1203 tgt->version[0] = tt->version[0];
1204 tgt->version[1] = tt->version[1];
1205 tgt->version[2] = tt->version[2];
1206 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1207
1208 tgt_iter++;
1209 }
1210
1211 info->tgtsize = tgt_iter;
1212 up_write(&nvm_lock);
1213
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001214 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1215 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001216 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001217 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001218
1219 kfree(info);
1220 return 0;
1221}
1222
1223static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1224{
1225 struct nvm_ioctl_get_devices *devices;
1226 struct nvm_dev *dev;
1227 int i = 0;
1228
1229 if (!capable(CAP_SYS_ADMIN))
1230 return -EPERM;
1231
1232 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1233 if (!devices)
1234 return -ENOMEM;
1235
1236 down_write(&nvm_lock);
1237 list_for_each_entry(dev, &nvm_devices, devices) {
1238 struct nvm_ioctl_device_info *info = &devices->info[i];
1239
1240 sprintf(info->devname, "%s", dev->name);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001241
Matias Bjørlingade69e22017-01-31 13:17:09 +01001242 /* kept for compatibility */
1243 info->bmversion[0] = 1;
1244 info->bmversion[1] = 0;
1245 info->bmversion[2] = 0;
1246 sprintf(info->bmname, "%s", "gennvm");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001247 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001248
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001249 if (i > 31) {
1250 pr_err("nvm: max 31 devices can be reported.\n");
1251 break;
1252 }
1253 }
1254 up_write(&nvm_lock);
1255
1256 devices->nr_devices = i;
1257
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001258 if (copy_to_user(arg, devices,
1259 sizeof(struct nvm_ioctl_get_devices))) {
1260 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001261 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001262 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001263
1264 kfree(devices);
1265 return 0;
1266}
1267
1268static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1269{
1270 struct nvm_ioctl_create create;
1271
1272 if (!capable(CAP_SYS_ADMIN))
1273 return -EPERM;
1274
1275 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1276 return -EFAULT;
1277
1278 create.dev[DISK_NAME_LEN - 1] = '\0';
1279 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1280 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1281
1282 if (create.flags != 0) {
1283 pr_err("nvm: no flags supported\n");
1284 return -EINVAL;
1285 }
1286
1287 return __nvm_configure_create(&create);
1288}
1289
1290static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1291{
1292 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001293 struct nvm_dev *dev;
1294 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001295
1296 if (!capable(CAP_SYS_ADMIN))
1297 return -EPERM;
1298
1299 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1300 return -EFAULT;
1301
1302 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1303
1304 if (remove.flags != 0) {
1305 pr_err("nvm: no flags supported\n");
1306 return -EINVAL;
1307 }
1308
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001309 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001310 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001311 if (!ret)
1312 break;
1313 }
1314
1315 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001316}
1317
Matias Bjørlingade69e22017-01-31 13:17:09 +01001318/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001319static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1320{
1321 struct nvm_ioctl_dev_init init;
1322
1323 if (!capable(CAP_SYS_ADMIN))
1324 return -EPERM;
1325
1326 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1327 return -EFAULT;
1328
1329 if (init.flags != 0) {
1330 pr_err("nvm: no flags supported\n");
1331 return -EINVAL;
1332 }
1333
Matias Bjørlingade69e22017-01-31 13:17:09 +01001334 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001335}
1336
Matias Bjørlingade69e22017-01-31 13:17:09 +01001337/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001338static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1339{
1340 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001341
1342 if (!capable(CAP_SYS_ADMIN))
1343 return -EPERM;
1344
1345 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1346 return -EFAULT;
1347
1348 fact.dev[DISK_NAME_LEN - 1] = '\0';
1349
1350 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1351 return -EINVAL;
1352
Matias Bjørlingbf643182016-02-04 15:13:27 +01001353 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001354}
1355
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001356static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1357{
1358 void __user *argp = (void __user *)arg;
1359
1360 switch (cmd) {
1361 case NVM_INFO:
1362 return nvm_ioctl_info(file, argp);
1363 case NVM_GET_DEVICES:
1364 return nvm_ioctl_get_devices(file, argp);
1365 case NVM_DEV_CREATE:
1366 return nvm_ioctl_dev_create(file, argp);
1367 case NVM_DEV_REMOVE:
1368 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001369 case NVM_DEV_INIT:
1370 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001371 case NVM_DEV_FACTORY:
1372 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001373 }
1374 return 0;
1375}
1376
1377static const struct file_operations _ctl_fops = {
1378 .open = nonseekable_open,
1379 .unlocked_ioctl = nvm_ctl_ioctl,
1380 .owner = THIS_MODULE,
1381 .llseek = noop_llseek,
1382};
1383
1384static struct miscdevice _nvm_misc = {
1385 .minor = MISC_DYNAMIC_MINOR,
1386 .name = "lightnvm",
1387 .nodename = "lightnvm/control",
1388 .fops = &_ctl_fops,
1389};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001390builtin_misc_device(_nvm_misc);