blob: 0842c85d3b846180166b2681abac718919ccfcc4 [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
92static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev)
93{
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
103 for (j = 0; j < ch_map->nr_luns; j++) {
104 int lun = j + lun_offs[j];
105 int lunid = (ch * dev->geo.luns_per_chnl) + lun;
106
107 WARN_ON(!test_and_clear_bit(lunid, dev->lun_map));
108 }
109
110 kfree(ch_map->lun_offs);
111 }
112
113 kfree(dev_map->chnls);
114 kfree(dev_map);
115
116 kfree(tgt_dev->luns);
117 kfree(tgt_dev);
118}
119
120static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
121 int lun_begin, int lun_end)
122{
123 struct nvm_tgt_dev *tgt_dev = NULL;
124 struct nvm_dev_map *dev_rmap = dev->rmap;
125 struct nvm_dev_map *dev_map;
126 struct ppa_addr *luns;
127 int nr_luns = lun_end - lun_begin + 1;
128 int luns_left = nr_luns;
129 int nr_chnls = nr_luns / dev->geo.luns_per_chnl;
130 int nr_chnls_mod = nr_luns % dev->geo.luns_per_chnl;
131 int bch = lun_begin / dev->geo.luns_per_chnl;
132 int blun = lun_begin % dev->geo.luns_per_chnl;
133 int lunid = 0;
134 int lun_balanced = 1;
135 int prev_nr_luns;
136 int i, j;
137
138 nr_chnls = nr_luns / dev->geo.luns_per_chnl;
139 nr_chnls = (nr_chnls_mod == 0) ? nr_chnls : nr_chnls + 1;
140
141 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
142 if (!dev_map)
143 goto err_dev;
144
145 dev_map->chnls = kcalloc(nr_chnls, sizeof(struct nvm_ch_map),
146 GFP_KERNEL);
147 if (!dev_map->chnls)
148 goto err_chnls;
149
150 luns = kcalloc(nr_luns, sizeof(struct ppa_addr), GFP_KERNEL);
151 if (!luns)
152 goto err_luns;
153
154 prev_nr_luns = (luns_left > dev->geo.luns_per_chnl) ?
155 dev->geo.luns_per_chnl : luns_left;
156 for (i = 0; i < nr_chnls; i++) {
157 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
158 int *lun_roffs = ch_rmap->lun_offs;
159 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
160 int *lun_offs;
161 int luns_in_chnl = (luns_left > dev->geo.luns_per_chnl) ?
162 dev->geo.luns_per_chnl : luns_left;
163
164 if (lun_balanced && prev_nr_luns != luns_in_chnl)
165 lun_balanced = 0;
166
167 ch_map->ch_off = ch_rmap->ch_off = bch;
168 ch_map->nr_luns = luns_in_chnl;
169
170 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
171 if (!lun_offs)
172 goto err_ch;
173
174 for (j = 0; j < luns_in_chnl; j++) {
175 luns[lunid].ppa = 0;
176 luns[lunid].g.ch = i;
177 luns[lunid++].g.lun = j;
178
179 lun_offs[j] = blun;
180 lun_roffs[j + blun] = blun;
181 }
182
183 ch_map->lun_offs = lun_offs;
184
185 /* when starting a new channel, lun offset is reset */
186 blun = 0;
187 luns_left -= luns_in_chnl;
188 }
189
190 dev_map->nr_chnls = nr_chnls;
191
192 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
193 if (!tgt_dev)
194 goto err_ch;
195
196 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
197 /* Target device only owns a portion of the physical device */
198 tgt_dev->geo.nr_chnls = nr_chnls;
199 tgt_dev->geo.nr_luns = nr_luns;
200 tgt_dev->geo.luns_per_chnl = (lun_balanced) ? prev_nr_luns : -1;
201 tgt_dev->total_secs = nr_luns * tgt_dev->geo.sec_per_lun;
202 tgt_dev->q = dev->q;
203 tgt_dev->map = dev_map;
204 tgt_dev->luns = luns;
205 memcpy(&tgt_dev->identity, &dev->identity, sizeof(struct nvm_id));
206
207 tgt_dev->parent = dev;
208
209 return tgt_dev;
210err_ch:
211 while (--i > 0)
212 kfree(dev_map->chnls[i].lun_offs);
213 kfree(luns);
214err_luns:
215 kfree(dev_map->chnls);
216err_chnls:
217 kfree(dev_map);
218err_dev:
219 return tgt_dev;
220}
221
222static const struct block_device_operations nvm_fops = {
223 .owner = THIS_MODULE,
224};
225
226static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
227{
228 struct nvm_ioctl_create_simple *s = &create->conf.s;
229 struct request_queue *tqueue;
230 struct gendisk *tdisk;
231 struct nvm_tgt_type *tt;
232 struct nvm_target *t;
233 struct nvm_tgt_dev *tgt_dev;
234 void *targetdata;
235
236 tt = nvm_find_target_type(create->tgttype, 1);
237 if (!tt) {
238 pr_err("nvm: target type %s not found\n", create->tgttype);
239 return -EINVAL;
240 }
241
242 mutex_lock(&dev->mlock);
243 t = nvm_find_target(dev, create->tgtname);
244 if (t) {
245 pr_err("nvm: target name already exists.\n");
246 mutex_unlock(&dev->mlock);
247 return -EINVAL;
248 }
249 mutex_unlock(&dev->mlock);
250
251 if (nvm_reserve_luns(dev, s->lun_begin, s->lun_end))
252 return -ENOMEM;
253
254 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
255 if (!t)
256 goto err_reserve;
257
258 tgt_dev = nvm_create_tgt_dev(dev, s->lun_begin, s->lun_end);
259 if (!tgt_dev) {
260 pr_err("nvm: could not create target device\n");
261 goto err_t;
262 }
263
264 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
265 if (!tqueue)
266 goto err_dev;
267 blk_queue_make_request(tqueue, tt->make_rq);
268
269 tdisk = alloc_disk(0);
270 if (!tdisk)
271 goto err_queue;
272
273 sprintf(tdisk->disk_name, "%s", create->tgtname);
274 tdisk->flags = GENHD_FL_EXT_DEVT;
275 tdisk->major = 0;
276 tdisk->first_minor = 0;
277 tdisk->fops = &nvm_fops;
278 tdisk->queue = tqueue;
279
280 targetdata = tt->init(tgt_dev, tdisk);
281 if (IS_ERR(targetdata))
282 goto err_init;
283
284 tdisk->private_data = targetdata;
285 tqueue->queuedata = targetdata;
286
287 blk_queue_max_hw_sectors(tqueue, 8 * dev->ops->max_phys_sect);
288
289 set_capacity(tdisk, tt->capacity(targetdata));
290 add_disk(tdisk);
291
292 t->type = tt;
293 t->disk = tdisk;
294 t->dev = tgt_dev;
295
296 mutex_lock(&dev->mlock);
297 list_add_tail(&t->list, &dev->targets);
298 mutex_unlock(&dev->mlock);
299
300 return 0;
301err_init:
302 put_disk(tdisk);
303err_queue:
304 blk_cleanup_queue(tqueue);
305err_dev:
306 kfree(tgt_dev);
307err_t:
308 kfree(t);
309err_reserve:
310 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
311 return -ENOMEM;
312}
313
314static void __nvm_remove_target(struct nvm_target *t)
315{
316 struct nvm_tgt_type *tt = t->type;
317 struct gendisk *tdisk = t->disk;
318 struct request_queue *q = tdisk->queue;
319
320 del_gendisk(tdisk);
321 blk_cleanup_queue(q);
322
323 if (tt->exit)
324 tt->exit(tdisk->private_data);
325
326 nvm_remove_tgt_dev(t->dev);
327 put_disk(tdisk);
328
329 list_del(&t->list);
330 kfree(t);
331}
332
333/**
334 * nvm_remove_tgt - Removes a target from the media manager
335 * @dev: device
336 * @remove: ioctl structure with target name to remove.
337 *
338 * Returns:
339 * 0: on success
340 * 1: on not found
341 * <0: on error
342 */
343static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
344{
345 struct nvm_target *t;
346
347 mutex_lock(&dev->mlock);
348 t = nvm_find_target(dev, remove->tgtname);
349 if (!t) {
350 mutex_unlock(&dev->mlock);
351 return 1;
352 }
353 __nvm_remove_target(t);
354 mutex_unlock(&dev->mlock);
355
356 return 0;
357}
358
359static int nvm_register_map(struct nvm_dev *dev)
360{
361 struct nvm_dev_map *rmap;
362 int i, j;
363
364 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
365 if (!rmap)
366 goto err_rmap;
367
368 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
369 GFP_KERNEL);
370 if (!rmap->chnls)
371 goto err_chnls;
372
373 for (i = 0; i < dev->geo.nr_chnls; i++) {
374 struct nvm_ch_map *ch_rmap;
375 int *lun_roffs;
376 int luns_in_chnl = dev->geo.luns_per_chnl;
377
378 ch_rmap = &rmap->chnls[i];
379
380 ch_rmap->ch_off = -1;
381 ch_rmap->nr_luns = luns_in_chnl;
382
383 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
384 if (!lun_roffs)
385 goto err_ch;
386
387 for (j = 0; j < luns_in_chnl; j++)
388 lun_roffs[j] = -1;
389
390 ch_rmap->lun_offs = lun_roffs;
391 }
392
393 dev->rmap = rmap;
394
395 return 0;
396err_ch:
397 while (--i >= 0)
398 kfree(rmap->chnls[i].lun_offs);
399err_chnls:
400 kfree(rmap);
401err_rmap:
402 return -ENOMEM;
403}
404
Matias Bjørling61a561d2017-01-31 13:17:13 +0100405static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100406{
407 struct nvm_dev_map *dev_map = tgt_dev->map;
408 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
409 int lun_off = ch_map->lun_offs[p->g.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100410
411 p->g.ch += ch_map->ch_off;
412 p->g.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100413}
414
Matias Bjørling61a561d2017-01-31 13:17:13 +0100415static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100416{
417 struct nvm_dev *dev = tgt_dev->parent;
418 struct nvm_dev_map *dev_rmap = dev->rmap;
419 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
420 int lun_roff = ch_rmap->lun_offs[p->g.lun];
421
422 p->g.ch -= ch_rmap->ch_off;
423 p->g.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100424}
425
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100426static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
427 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100428{
429 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100430
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100431 for (i = 0; i < nr_ppas; i++) {
432 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
433 ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100434 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100435}
436
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100437static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
438 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100439{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100440 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100441
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100442 for (i = 0; i < nr_ppas; i++) {
443 ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
444 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
445 }
446}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100447
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100448static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
449{
450 if (rqd->nr_ppas == 1) {
451 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
452 return;
453 }
454
455 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
456}
457
458static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
459{
460 if (rqd->nr_ppas == 1) {
461 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
462 return;
463 }
464
465 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100466}
467
468void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries,
469 int len)
470{
471 struct nvm_geo *geo = &dev->geo;
472 struct nvm_dev_map *dev_rmap = dev->rmap;
473 u64 i;
474
475 for (i = 0; i < len; i++) {
476 struct nvm_ch_map *ch_rmap;
477 int *lun_roffs;
478 struct ppa_addr gaddr;
479 u64 pba = le64_to_cpu(entries[i]);
480 int off;
481 u64 diff;
482
483 if (!pba)
484 continue;
485
486 gaddr = linear_to_generic_addr(geo, pba);
487 ch_rmap = &dev_rmap->chnls[gaddr.g.ch];
488 lun_roffs = ch_rmap->lun_offs;
489
490 off = gaddr.g.ch * geo->luns_per_chnl + gaddr.g.lun;
491
492 diff = ((ch_rmap->ch_off * geo->luns_per_chnl) +
493 (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun;
494
495 entries[i] -= cpu_to_le64(diff);
496 }
497}
498EXPORT_SYMBOL(nvm_part_to_tgt);
499
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200500struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200501{
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200502 struct nvm_tgt_type *tmp, *tt = NULL;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200503
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200504 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200505 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200506
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200507 list_for_each_entry(tmp, &nvm_tgt_types, list)
508 if (!strcmp(name, tmp->name)) {
509 tt = tmp;
510 break;
511 }
512
513 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200514 up_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200515 return tt;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200516}
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200517EXPORT_SYMBOL(nvm_find_target_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100518
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200519int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100520{
521 int ret = 0;
522
Matias Bjørling5cd90782016-07-07 09:54:17 +0200523 down_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200524 if (nvm_find_target_type(tt->name, 0))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100525 ret = -EEXIST;
526 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200527 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200528 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100529
530 return ret;
531}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200532EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100533
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200534void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100535{
536 if (!tt)
537 return;
538
539 down_write(&nvm_lock);
540 list_del(&tt->list);
541 up_write(&nvm_lock);
542}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200543EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100544
545void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
546 dma_addr_t *dma_handler)
547{
Javier González75b85642016-05-06 20:03:13 +0200548 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100549 dma_handler);
550}
551EXPORT_SYMBOL(nvm_dev_dma_alloc);
552
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100553void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100554{
Javier González75b85642016-05-06 20:03:13 +0200555 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100556}
557EXPORT_SYMBOL(nvm_dev_dma_free);
558
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100559static struct nvm_dev *nvm_find_nvm_dev(const char *name)
560{
561 struct nvm_dev *dev;
562
563 list_for_each_entry(dev, &nvm_devices, devices)
564 if (!strcmp(name, dev->name))
565 return dev;
566
567 return NULL;
568}
569
Javier González333ba052016-11-28 22:39:14 +0100570int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
571 int nr_ppas, int type)
572{
573 struct nvm_dev *dev = tgt_dev->parent;
574 struct nvm_rq rqd;
575 int ret;
576
577 if (nr_ppas > dev->ops->max_phys_sect) {
578 pr_err("nvm: unable to update all blocks atomically\n");
579 return -EINVAL;
580 }
581
582 memset(&rqd, 0, sizeof(struct nvm_rq));
583
584 nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100585 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100586
587 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
588 nvm_free_rqd_ppalist(dev, &rqd);
589 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100590 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100591 return -EINVAL;
592 }
593
594 return 0;
595}
596EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
597
Javier Gonzáleza2790062016-11-28 22:39:12 +0100598int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
599{
600 struct nvm_dev *dev = tgt_dev->parent;
601
602 return dev->ops->max_phys_sect;
603}
604EXPORT_SYMBOL(nvm_max_phys_sects);
605
Javier González8e536242016-11-28 22:39:10 +0100606int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100607{
Javier González8e536242016-11-28 22:39:10 +0100608 struct nvm_dev *dev = tgt_dev->parent;
609
Matias Bjørlingade69e22017-01-31 13:17:09 +0100610 if (!dev->ops->submit_io)
611 return -ENODEV;
612
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100613 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100614
615 rqd->dev = tgt_dev;
616 return dev->ops->submit_io(dev, rqd);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100617}
618EXPORT_SYMBOL(nvm_submit_io);
619
Matias Bjørling10995c32017-01-31 13:17:10 +0100620int nvm_erase_blk(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas, int flags)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100621{
Matias Bjørling10995c32017-01-31 13:17:10 +0100622 struct nvm_dev *dev = tgt_dev->parent;
623 struct nvm_rq rqd;
624 int ret;
Javier González8e536242016-11-28 22:39:10 +0100625
Matias Bjørling10995c32017-01-31 13:17:10 +0100626 if (!dev->ops->erase_block)
627 return 0;
628
Matias Bjørling61a561d2017-01-31 13:17:13 +0100629 nvm_map_to_dev(tgt_dev, ppas);
Matias Bjørling10995c32017-01-31 13:17:10 +0100630
631 memset(&rqd, 0, sizeof(struct nvm_rq));
632
633 ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, 1, 1);
634 if (ret)
635 return ret;
636
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100637 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Matias Bjørling10995c32017-01-31 13:17:10 +0100638
639 rqd.flags = flags;
640
641 ret = dev->ops->erase_block(dev, &rqd);
642
643 nvm_free_rqd_ppalist(dev, &rqd);
644
645 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100646}
647EXPORT_SYMBOL(nvm_erase_blk);
648
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100649int nvm_get_l2p_tbl(struct nvm_tgt_dev *tgt_dev, u64 slba, u32 nlb,
Javier González959e9112016-11-28 22:39:11 +0100650 nvm_l2p_update_fn *update_l2p, void *priv)
651{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100652 struct nvm_dev *dev = tgt_dev->parent;
653
Javier González959e9112016-11-28 22:39:11 +0100654 if (!dev->ops->get_l2p_tbl)
655 return 0;
656
657 return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv);
658}
659EXPORT_SYMBOL(nvm_get_l2p_tbl);
660
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100661int nvm_get_area(struct nvm_tgt_dev *tgt_dev, sector_t *lba, sector_t len)
Javier González959e9112016-11-28 22:39:11 +0100662{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100663 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100664 struct nvm_geo *geo = &dev->geo;
665 struct nvm_area *area, *prev, *next;
666 sector_t begin = 0;
667 sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100668
Matias Bjørlingade69e22017-01-31 13:17:09 +0100669 if (len > max_sectors)
670 return -EINVAL;
671
672 area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL);
673 if (!area)
674 return -ENOMEM;
675
676 prev = NULL;
677
678 spin_lock(&dev->lock);
679 list_for_each_entry(next, &dev->area_list, list) {
680 if (begin + len > next->begin) {
681 begin = next->end;
682 prev = next;
683 continue;
684 }
685 break;
686 }
687
688 if ((begin + len) > max_sectors) {
689 spin_unlock(&dev->lock);
690 kfree(area);
691 return -EINVAL;
692 }
693
694 area->begin = *lba = begin;
695 area->end = begin + len;
696
697 if (prev) /* insert into sorted order */
698 list_add(&area->list, &prev->list);
699 else
700 list_add(&area->list, &dev->area_list);
701 spin_unlock(&dev->lock);
702
703 return 0;
Javier González959e9112016-11-28 22:39:11 +0100704}
705EXPORT_SYMBOL(nvm_get_area);
706
Matias Bjørlingade69e22017-01-31 13:17:09 +0100707void nvm_put_area(struct nvm_tgt_dev *tgt_dev, sector_t begin)
Javier González959e9112016-11-28 22:39:11 +0100708{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100709 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100710 struct nvm_area *area;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100711
Matias Bjørlingade69e22017-01-31 13:17:09 +0100712 spin_lock(&dev->lock);
713 list_for_each_entry(area, &dev->area_list, list) {
714 if (area->begin != begin)
715 continue;
716
717 list_del(&area->list);
718 spin_unlock(&dev->lock);
719 kfree(area);
720 return;
721 }
722 spin_unlock(&dev->lock);
Javier González959e9112016-11-28 22:39:11 +0100723}
724EXPORT_SYMBOL(nvm_put_area);
725
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100726int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd,
Matias Bjørling8680f162016-07-07 09:54:22 +0200727 const struct ppa_addr *ppas, int nr_ppas, int vblk)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100728{
Javier González8e79b5c2016-11-28 22:39:06 +0100729 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100730 int i, plane_cnt, pl_idx;
Matias Bjørling8680f162016-07-07 09:54:22 +0200731 struct ppa_addr ppa;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100732
Javier González8e79b5c2016-11-28 22:39:06 +0100733 if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
Javier González6d5be952016-05-06 20:03:20 +0200734 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100735 rqd->ppa_addr = ppas[0];
736
737 return 0;
738 }
739
Javier González6d5be952016-05-06 20:03:20 +0200740 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100741 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
742 if (!rqd->ppa_list) {
743 pr_err("nvm: failed to allocate dma memory\n");
744 return -ENOMEM;
745 }
746
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200747 if (!vblk) {
748 for (i = 0; i < nr_ppas; i++)
749 rqd->ppa_list[i] = ppas[i];
750 } else {
Javier González8e79b5c2016-11-28 22:39:06 +0100751 plane_cnt = geo->plane_mode;
Javier González6d5be952016-05-06 20:03:20 +0200752 rqd->nr_ppas *= plane_cnt;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200753
Matias Bjørling556755e2016-01-12 07:49:26 +0100754 for (i = 0; i < nr_ppas; i++) {
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200755 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
Matias Bjørling8680f162016-07-07 09:54:22 +0200756 ppa = ppas[i];
757 ppa.g.pl = pl_idx;
758 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200759 }
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100760 }
761 }
762
763 return 0;
764}
765EXPORT_SYMBOL(nvm_set_rqd_ppalist);
766
767void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd)
768{
769 if (!rqd->ppa_list)
770 return;
771
772 nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list);
773}
774EXPORT_SYMBOL(nvm_free_rqd_ppalist);
775
Matias Bjørling912761622016-01-12 07:49:21 +0100776void nvm_end_io(struct nvm_rq *rqd, int error)
777{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100778 struct nvm_tgt_dev *tgt_dev = rqd->dev;
779 struct nvm_tgt_instance *ins = rqd->ins;
780
781 /* Convert address space */
782 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100783 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100784
Matias Bjørling72d256e2016-01-12 07:49:29 +0100785 rqd->error = error;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100786 ins->tt->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100787}
788EXPORT_SYMBOL(nvm_end_io);
789
Matias Bjørling22e8c972016-05-06 20:02:58 +0200790/*
791 * folds a bad block list from its plane representation to its virtual
792 * block representation. The fold is done in place and reduced size is
793 * returned.
794 *
795 * If any of the planes status are bad or grown bad block, the virtual block
796 * is marked bad. If not bad, the first plane state acts as the block state.
797 */
798int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
799{
Javier González8e79b5c2016-11-28 22:39:06 +0100800 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200801 int blk, offset, pl, blktype;
802
Javier González8e79b5c2016-11-28 22:39:06 +0100803 if (nr_blks != geo->blks_per_lun * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200804 return -EINVAL;
805
Javier González8e79b5c2016-11-28 22:39:06 +0100806 for (blk = 0; blk < geo->blks_per_lun; blk++) {
807 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200808 blktype = blks[offset];
809
810 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100811 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200812 if (blks[offset + pl] &
813 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
814 blktype = blks[offset + pl];
815 break;
816 }
817 }
818
819 blks[blk] = blktype;
820 }
821
Javier González8e79b5c2016-11-28 22:39:06 +0100822 return geo->blks_per_lun;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200823}
824EXPORT_SYMBOL(nvm_bb_tbl_fold);
825
Javier González333ba052016-11-28 22:39:14 +0100826int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
827 u8 *blks)
828{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100829 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100830
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100831 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100832
833 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100834}
835EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
836
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100837static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
838{
Javier González8e79b5c2016-11-28 22:39:06 +0100839 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100840 int i;
841
Javier González8e79b5c2016-11-28 22:39:06 +0100842 dev->lps_per_blk = geo->pgs_per_blk;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100843 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
844 if (!dev->lptbl)
845 return -ENOMEM;
846
847 /* Just a linear array */
848 for (i = 0; i < dev->lps_per_blk; i++)
849 dev->lptbl[i] = i;
850
851 return 0;
852}
853
854static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
855{
856 int i, p;
857 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
858
859 if (!mlc->num_pairs)
860 return 0;
861
862 dev->lps_per_blk = mlc->num_pairs;
863 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
864 if (!dev->lptbl)
865 return -ENOMEM;
866
867 /* The lower page table encoding consists of a list of bytes, where each
868 * has a lower and an upper half. The first half byte maintains the
869 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200870 * previous incrementation value
871 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100872 dev->lptbl[0] = mlc->pairs[0] & 0xF;
873 for (i = 1; i < dev->lps_per_blk; i++) {
874 p = mlc->pairs[i >> 1];
875 if (i & 0x1) /* upper */
876 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
877 else /* lower */
878 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
879 }
880
881 return 0;
882}
883
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100884static int nvm_core_init(struct nvm_dev *dev)
885{
886 struct nvm_id *id = &dev->identity;
887 struct nvm_id_group *grp = &id->groups[0];
Javier González8e79b5c2016-11-28 22:39:06 +0100888 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200889 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100890
Javier González8e79b5c2016-11-28 22:39:06 +0100891 /* Whole device values */
892 geo->nr_chnls = grp->num_ch;
893 geo->luns_per_chnl = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100894
Javier González8e79b5c2016-11-28 22:39:06 +0100895 /* Generic device values */
896 geo->pgs_per_blk = grp->num_pg;
897 geo->blks_per_lun = grp->num_blk;
898 geo->nr_planes = grp->num_pln;
899 geo->fpg_size = grp->fpg_sz;
900 geo->pfpg_size = grp->fpg_sz * grp->num_pln;
901 geo->sec_size = grp->csecs;
902 geo->oob_size = grp->sos;
903 geo->sec_per_pg = grp->fpg_sz / grp->csecs;
904 geo->mccap = grp->mccap;
905 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
906
907 geo->plane_mode = NVM_PLANE_SINGLE;
908 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100909
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100910 if (grp->mpos & 0x020202)
Javier González8e79b5c2016-11-28 22:39:06 +0100911 geo->plane_mode = NVM_PLANE_DOUBLE;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100912 if (grp->mpos & 0x040404)
Javier González8e79b5c2016-11-28 22:39:06 +0100913 geo->plane_mode = NVM_PLANE_QUAD;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100914
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200915 if (grp->mtype != 0) {
916 pr_err("nvm: memory type not supported\n");
917 return -EINVAL;
918 }
919
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100920 /* calculated values */
Javier González8e79b5c2016-11-28 22:39:06 +0100921 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
922 geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
923 geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
924 geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100925
Javier González8e79b5c2016-11-28 22:39:06 +0100926 dev->total_secs = geo->nr_luns * geo->sec_per_lun;
927 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100928 sizeof(unsigned long), GFP_KERNEL);
929 if (!dev->lun_map)
930 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200931
932 switch (grp->fmtype) {
933 case NVM_ID_FMTYPE_SLC:
934 if (nvm_init_slc_tbl(dev, grp)) {
935 ret = -ENOMEM;
936 goto err_fmtype;
937 }
938 break;
939 case NVM_ID_FMTYPE_MLC:
940 if (nvm_init_mlc_tbl(dev, grp)) {
941 ret = -ENOMEM;
942 goto err_fmtype;
943 }
944 break;
945 default:
946 pr_err("nvm: flash type not supported\n");
947 ret = -EINVAL;
948 goto err_fmtype;
949 }
950
Matias Bjørlingade69e22017-01-31 13:17:09 +0100951 INIT_LIST_HEAD(&dev->area_list);
952 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100953 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100954 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100955
Matias Bjørlingade69e22017-01-31 13:17:09 +0100956 ret = nvm_register_map(dev);
957 if (ret)
958 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200959
Matias Bjørlingade69e22017-01-31 13:17:09 +0100960 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100961 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200962err_fmtype:
963 kfree(dev->lun_map);
964 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100965}
966
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200967void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100968{
969 if (!dev)
970 return;
971
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200972 if (dev->dma_pool)
973 dev->ops->destroy_dma_pool(dev->dma_pool);
974
Matias Bjørlingade69e22017-01-31 13:17:09 +0100975 kfree(dev->rmap);
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100976 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200977 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200978 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100979}
980
981static int nvm_init(struct nvm_dev *dev)
982{
Javier González8e79b5c2016-11-28 22:39:06 +0100983 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100984 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100985
Matias Bjørling16f26c32015-12-06 11:25:48 +0100986 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100987 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100988 goto err;
989 }
990
991 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
992 dev->identity.ver_id, dev->identity.vmnt,
993 dev->identity.cgrps);
994
995 if (dev->identity.ver_id != 1) {
996 pr_err("nvm: device not supported by kernel.");
997 goto err;
998 }
999
1000 if (dev->identity.cgrps != 1) {
1001 pr_err("nvm: only one group configuration supported.");
1002 goto err;
1003 }
1004
1005 ret = nvm_core_init(dev);
1006 if (ret) {
1007 pr_err("nvm: could not initialize core structures.\n");
1008 goto err;
1009 }
1010
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001011 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +01001012 dev->name, geo->sec_per_pg, geo->nr_planes,
1013 geo->pgs_per_blk, geo->blks_per_lun,
1014 geo->nr_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001015 return 0;
1016err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001017 pr_err("nvm: failed to initialize nvm\n");
1018 return ret;
1019}
1020
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001021struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001022{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001023 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1024}
1025EXPORT_SYMBOL(nvm_alloc_dev);
1026
1027int nvm_register(struct nvm_dev *dev)
1028{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001029 int ret;
1030
Matias Bjørlingade69e22017-01-31 13:17:09 +01001031 if (!dev->q || !dev->ops)
1032 return -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001033
Wenwei Taod1601472015-11-28 16:49:25 +01001034 if (dev->ops->max_phys_sect > 256) {
1035 pr_info("nvm: max sectors supported is 256.\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001036 return -EINVAL;
Wenwei Taod1601472015-11-28 16:49:25 +01001037 }
1038
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001039 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +02001040 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
1041 if (!dev->dma_pool) {
1042 pr_err("nvm: could not create dma pool\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001043 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001044 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001045 }
1046
Matias Bjørlingade69e22017-01-31 13:17:09 +01001047 ret = nvm_init(dev);
1048 if (ret)
1049 goto err_init;
Matias Bjørlingb7692072016-01-12 07:49:38 +01001050
Matias Bjørling762796b2015-12-06 11:25:49 +01001051 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001052 down_write(&nvm_lock);
1053 list_add(&dev->devices, &nvm_devices);
1054 up_write(&nvm_lock);
1055
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001056 return 0;
1057err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +01001058 dev->ops->destroy_dma_pool(dev->dma_pool);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001059 return ret;
1060}
1061EXPORT_SYMBOL(nvm_register);
1062
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001063void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001064{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001065 struct nvm_target *t, *tmp;
1066
1067 mutex_lock(&dev->mlock);
1068 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1069 if (t->dev->parent != dev)
1070 continue;
1071 __nvm_remove_target(t);
1072 }
1073 mutex_unlock(&dev->mlock);
1074
Wenwei Taod0a712c2015-11-28 16:49:28 +01001075 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001076 list_del(&dev->devices);
1077 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001078
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001079 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001080}
1081EXPORT_SYMBOL(nvm_unregister);
1082
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001083static int __nvm_configure_create(struct nvm_ioctl_create *create)
1084{
1085 struct nvm_dev *dev;
1086 struct nvm_ioctl_create_simple *s;
1087
Wenwei Taod0a712c2015-11-28 16:49:28 +01001088 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001089 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001090 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001091
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001092 if (!dev) {
1093 pr_err("nvm: device not found\n");
1094 return -EINVAL;
1095 }
1096
1097 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
1098 pr_err("nvm: config type not valid\n");
1099 return -EINVAL;
1100 }
1101 s = &create->conf.s;
1102
Javier González8e79b5c2016-11-28 22:39:06 +01001103 if (s->lun_begin > s->lun_end || s->lun_end > dev->geo.nr_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001104 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Javier González8e79b5c2016-11-28 22:39:06 +01001105 s->lun_begin, s->lun_end, dev->geo.nr_luns);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001106 return -EINVAL;
1107 }
1108
Matias Bjørlingade69e22017-01-31 13:17:09 +01001109 return nvm_create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001110}
1111
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001112static long nvm_ioctl_info(struct file *file, void __user *arg)
1113{
1114 struct nvm_ioctl_info *info;
1115 struct nvm_tgt_type *tt;
1116 int tgt_iter = 0;
1117
1118 if (!capable(CAP_SYS_ADMIN))
1119 return -EPERM;
1120
1121 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1122 if (IS_ERR(info))
1123 return -EFAULT;
1124
1125 info->version[0] = NVM_VERSION_MAJOR;
1126 info->version[1] = NVM_VERSION_MINOR;
1127 info->version[2] = NVM_VERSION_PATCH;
1128
1129 down_write(&nvm_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001130 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001131 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1132
1133 tgt->version[0] = tt->version[0];
1134 tgt->version[1] = tt->version[1];
1135 tgt->version[2] = tt->version[2];
1136 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1137
1138 tgt_iter++;
1139 }
1140
1141 info->tgtsize = tgt_iter;
1142 up_write(&nvm_lock);
1143
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001144 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1145 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001146 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001147 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001148
1149 kfree(info);
1150 return 0;
1151}
1152
1153static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1154{
1155 struct nvm_ioctl_get_devices *devices;
1156 struct nvm_dev *dev;
1157 int i = 0;
1158
1159 if (!capable(CAP_SYS_ADMIN))
1160 return -EPERM;
1161
1162 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1163 if (!devices)
1164 return -ENOMEM;
1165
1166 down_write(&nvm_lock);
1167 list_for_each_entry(dev, &nvm_devices, devices) {
1168 struct nvm_ioctl_device_info *info = &devices->info[i];
1169
1170 sprintf(info->devname, "%s", dev->name);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001171
Matias Bjørlingade69e22017-01-31 13:17:09 +01001172 /* kept for compatibility */
1173 info->bmversion[0] = 1;
1174 info->bmversion[1] = 0;
1175 info->bmversion[2] = 0;
1176 sprintf(info->bmname, "%s", "gennvm");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001177 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001178
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001179 if (i > 31) {
1180 pr_err("nvm: max 31 devices can be reported.\n");
1181 break;
1182 }
1183 }
1184 up_write(&nvm_lock);
1185
1186 devices->nr_devices = i;
1187
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001188 if (copy_to_user(arg, devices,
1189 sizeof(struct nvm_ioctl_get_devices))) {
1190 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001191 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001192 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001193
1194 kfree(devices);
1195 return 0;
1196}
1197
1198static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1199{
1200 struct nvm_ioctl_create create;
1201
1202 if (!capable(CAP_SYS_ADMIN))
1203 return -EPERM;
1204
1205 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1206 return -EFAULT;
1207
1208 create.dev[DISK_NAME_LEN - 1] = '\0';
1209 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1210 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1211
1212 if (create.flags != 0) {
1213 pr_err("nvm: no flags supported\n");
1214 return -EINVAL;
1215 }
1216
1217 return __nvm_configure_create(&create);
1218}
1219
1220static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1221{
1222 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001223 struct nvm_dev *dev;
1224 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001225
1226 if (!capable(CAP_SYS_ADMIN))
1227 return -EPERM;
1228
1229 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1230 return -EFAULT;
1231
1232 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1233
1234 if (remove.flags != 0) {
1235 pr_err("nvm: no flags supported\n");
1236 return -EINVAL;
1237 }
1238
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001239 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001240 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001241 if (!ret)
1242 break;
1243 }
1244
1245 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001246}
1247
Matias Bjørlingade69e22017-01-31 13:17:09 +01001248/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001249static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1250{
1251 struct nvm_ioctl_dev_init init;
1252
1253 if (!capable(CAP_SYS_ADMIN))
1254 return -EPERM;
1255
1256 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1257 return -EFAULT;
1258
1259 if (init.flags != 0) {
1260 pr_err("nvm: no flags supported\n");
1261 return -EINVAL;
1262 }
1263
Matias Bjørlingade69e22017-01-31 13:17:09 +01001264 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001265}
1266
Matias Bjørlingade69e22017-01-31 13:17:09 +01001267/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001268static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1269{
1270 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001271
1272 if (!capable(CAP_SYS_ADMIN))
1273 return -EPERM;
1274
1275 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1276 return -EFAULT;
1277
1278 fact.dev[DISK_NAME_LEN - 1] = '\0';
1279
1280 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1281 return -EINVAL;
1282
Matias Bjørlingbf643182016-02-04 15:13:27 +01001283 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001284}
1285
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001286static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1287{
1288 void __user *argp = (void __user *)arg;
1289
1290 switch (cmd) {
1291 case NVM_INFO:
1292 return nvm_ioctl_info(file, argp);
1293 case NVM_GET_DEVICES:
1294 return nvm_ioctl_get_devices(file, argp);
1295 case NVM_DEV_CREATE:
1296 return nvm_ioctl_dev_create(file, argp);
1297 case NVM_DEV_REMOVE:
1298 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001299 case NVM_DEV_INIT:
1300 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001301 case NVM_DEV_FACTORY:
1302 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001303 }
1304 return 0;
1305}
1306
1307static const struct file_operations _ctl_fops = {
1308 .open = nonseekable_open,
1309 .unlocked_ioctl = nvm_ctl_ioctl,
1310 .owner = THIS_MODULE,
1311 .llseek = noop_llseek,
1312};
1313
1314static struct miscdevice _nvm_misc = {
1315 .minor = MISC_DYNAMIC_MINOR,
1316 .name = "lightnvm",
1317 .nodename = "lightnvm/control",
1318 .fops = &_ctl_fops,
1319};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001320builtin_misc_device(_nvm_misc);