blob: 95105c47e0822dec524eba1909a8aa4cc70960de [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
Javier González9a69b0e2017-01-31 13:17:20 +0100292 if (tt->sysfs_init && tt->sysfs_init(tdisk))
293 goto err_sysfs;
294
Matias Bjørlingade69e22017-01-31 13:17:09 +0100295 t->type = tt;
296 t->disk = tdisk;
297 t->dev = tgt_dev;
298
299 mutex_lock(&dev->mlock);
300 list_add_tail(&t->list, &dev->targets);
301 mutex_unlock(&dev->mlock);
302
303 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100304err_sysfs:
305 if (tt->exit)
306 tt->exit(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100307err_init:
308 put_disk(tdisk);
309err_queue:
310 blk_cleanup_queue(tqueue);
311err_dev:
Javier Gonzálezdeccf5a2017-01-31 13:17:19 +0100312 nvm_remove_tgt_dev(tgt_dev);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100313err_t:
314 kfree(t);
315err_reserve:
316 nvm_release_luns_err(dev, s->lun_begin, s->lun_end);
317 return -ENOMEM;
318}
319
320static void __nvm_remove_target(struct nvm_target *t)
321{
322 struct nvm_tgt_type *tt = t->type;
323 struct gendisk *tdisk = t->disk;
324 struct request_queue *q = tdisk->queue;
325
326 del_gendisk(tdisk);
327 blk_cleanup_queue(q);
328
Javier González9a69b0e2017-01-31 13:17:20 +0100329 if (tt->sysfs_exit)
330 tt->sysfs_exit(tdisk);
331
Matias Bjørlingade69e22017-01-31 13:17:09 +0100332 if (tt->exit)
333 tt->exit(tdisk->private_data);
334
335 nvm_remove_tgt_dev(t->dev);
336 put_disk(tdisk);
337
338 list_del(&t->list);
339 kfree(t);
340}
341
342/**
343 * nvm_remove_tgt - Removes a target from the media manager
344 * @dev: device
345 * @remove: ioctl structure with target name to remove.
346 *
347 * Returns:
348 * 0: on success
349 * 1: on not found
350 * <0: on error
351 */
352static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
353{
354 struct nvm_target *t;
355
356 mutex_lock(&dev->mlock);
357 t = nvm_find_target(dev, remove->tgtname);
358 if (!t) {
359 mutex_unlock(&dev->mlock);
360 return 1;
361 }
362 __nvm_remove_target(t);
363 mutex_unlock(&dev->mlock);
364
365 return 0;
366}
367
368static int nvm_register_map(struct nvm_dev *dev)
369{
370 struct nvm_dev_map *rmap;
371 int i, j;
372
373 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
374 if (!rmap)
375 goto err_rmap;
376
377 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
378 GFP_KERNEL);
379 if (!rmap->chnls)
380 goto err_chnls;
381
382 for (i = 0; i < dev->geo.nr_chnls; i++) {
383 struct nvm_ch_map *ch_rmap;
384 int *lun_roffs;
385 int luns_in_chnl = dev->geo.luns_per_chnl;
386
387 ch_rmap = &rmap->chnls[i];
388
389 ch_rmap->ch_off = -1;
390 ch_rmap->nr_luns = luns_in_chnl;
391
392 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
393 if (!lun_roffs)
394 goto err_ch;
395
396 for (j = 0; j < luns_in_chnl; j++)
397 lun_roffs[j] = -1;
398
399 ch_rmap->lun_offs = lun_roffs;
400 }
401
402 dev->rmap = rmap;
403
404 return 0;
405err_ch:
406 while (--i >= 0)
407 kfree(rmap->chnls[i].lun_offs);
408err_chnls:
409 kfree(rmap);
410err_rmap:
411 return -ENOMEM;
412}
413
Matias Bjørling61a561d2017-01-31 13:17:13 +0100414static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100415{
416 struct nvm_dev_map *dev_map = tgt_dev->map;
417 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
418 int lun_off = ch_map->lun_offs[p->g.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100419
420 p->g.ch += ch_map->ch_off;
421 p->g.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100422}
423
Matias Bjørling61a561d2017-01-31 13:17:13 +0100424static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100425{
426 struct nvm_dev *dev = tgt_dev->parent;
427 struct nvm_dev_map *dev_rmap = dev->rmap;
428 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
429 int lun_roff = ch_rmap->lun_offs[p->g.lun];
430
431 p->g.ch -= ch_rmap->ch_off;
432 p->g.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100433}
434
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100435static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
436 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100437{
438 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100439
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100440 for (i = 0; i < nr_ppas; i++) {
441 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
442 ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100443 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100444}
445
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100446static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
447 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100448{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100449 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100450
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100451 for (i = 0; i < nr_ppas; i++) {
452 ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
453 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
454 }
455}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100456
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100457static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
458{
459 if (rqd->nr_ppas == 1) {
460 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
461 return;
462 }
463
464 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
465}
466
467static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
468{
469 if (rqd->nr_ppas == 1) {
470 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
471 return;
472 }
473
474 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100475}
476
477void nvm_part_to_tgt(struct nvm_dev *dev, sector_t *entries,
478 int len)
479{
480 struct nvm_geo *geo = &dev->geo;
481 struct nvm_dev_map *dev_rmap = dev->rmap;
482 u64 i;
483
484 for (i = 0; i < len; i++) {
485 struct nvm_ch_map *ch_rmap;
486 int *lun_roffs;
487 struct ppa_addr gaddr;
488 u64 pba = le64_to_cpu(entries[i]);
489 int off;
490 u64 diff;
491
492 if (!pba)
493 continue;
494
495 gaddr = linear_to_generic_addr(geo, pba);
496 ch_rmap = &dev_rmap->chnls[gaddr.g.ch];
497 lun_roffs = ch_rmap->lun_offs;
498
499 off = gaddr.g.ch * geo->luns_per_chnl + gaddr.g.lun;
500
501 diff = ((ch_rmap->ch_off * geo->luns_per_chnl) +
502 (lun_roffs[gaddr.g.lun])) * geo->sec_per_lun;
503
504 entries[i] -= cpu_to_le64(diff);
505 }
506}
507EXPORT_SYMBOL(nvm_part_to_tgt);
508
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200509struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200510{
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200511 struct nvm_tgt_type *tmp, *tt = NULL;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200512
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200513 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200514 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200515
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200516 list_for_each_entry(tmp, &nvm_tgt_types, list)
517 if (!strcmp(name, tmp->name)) {
518 tt = tmp;
519 break;
520 }
521
522 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +0200523 up_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200524 return tt;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +0200525}
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200526EXPORT_SYMBOL(nvm_find_target_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100527
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200528int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100529{
530 int ret = 0;
531
Matias Bjørling5cd90782016-07-07 09:54:17 +0200532 down_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200533 if (nvm_find_target_type(tt->name, 0))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100534 ret = -EEXIST;
535 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200536 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200537 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100538
539 return ret;
540}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200541EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100542
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200543void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100544{
545 if (!tt)
546 return;
547
548 down_write(&nvm_lock);
549 list_del(&tt->list);
550 up_write(&nvm_lock);
551}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200552EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100553
554void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
555 dma_addr_t *dma_handler)
556{
Javier González75b85642016-05-06 20:03:13 +0200557 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100558 dma_handler);
559}
560EXPORT_SYMBOL(nvm_dev_dma_alloc);
561
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100562void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100563{
Javier González75b85642016-05-06 20:03:13 +0200564 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100565}
566EXPORT_SYMBOL(nvm_dev_dma_free);
567
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100568static struct nvm_dev *nvm_find_nvm_dev(const char *name)
569{
570 struct nvm_dev *dev;
571
572 list_for_each_entry(dev, &nvm_devices, devices)
573 if (!strcmp(name, dev->name))
574 return dev;
575
576 return NULL;
577}
578
Javier González333ba052016-11-28 22:39:14 +0100579int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
580 int nr_ppas, int type)
581{
582 struct nvm_dev *dev = tgt_dev->parent;
583 struct nvm_rq rqd;
584 int ret;
585
586 if (nr_ppas > dev->ops->max_phys_sect) {
587 pr_err("nvm: unable to update all blocks atomically\n");
588 return -EINVAL;
589 }
590
591 memset(&rqd, 0, sizeof(struct nvm_rq));
592
Javier González17912c42017-04-15 20:55:37 +0200593 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100594 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100595
596 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
Javier González17912c42017-04-15 20:55:37 +0200597 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100598 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100599 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100600 return -EINVAL;
601 }
602
603 return 0;
604}
605EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
606
Javier Gonzáleza2790062016-11-28 22:39:12 +0100607int nvm_max_phys_sects(struct nvm_tgt_dev *tgt_dev)
608{
609 struct nvm_dev *dev = tgt_dev->parent;
610
611 return dev->ops->max_phys_sect;
612}
613EXPORT_SYMBOL(nvm_max_phys_sects);
614
Javier González8e536242016-11-28 22:39:10 +0100615int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100616{
Javier González8e536242016-11-28 22:39:10 +0100617 struct nvm_dev *dev = tgt_dev->parent;
618
Matias Bjørlingade69e22017-01-31 13:17:09 +0100619 if (!dev->ops->submit_io)
620 return -ENODEV;
621
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100622 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100623
624 rqd->dev = tgt_dev;
625 return dev->ops->submit_io(dev, rqd);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100626}
627EXPORT_SYMBOL(nvm_submit_io);
628
Javier González17912c42017-04-15 20:55:37 +0200629static void nvm_end_io_sync(struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100630{
Javier González17912c42017-04-15 20:55:37 +0200631 struct completion *waiting = rqd->private;
632
633 complete(waiting);
634}
635
636int nvm_erase_sync(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
637 int nr_ppas)
638{
639 struct nvm_geo *geo = &tgt_dev->geo;
Matias Bjørling10995c32017-01-31 13:17:10 +0100640 struct nvm_rq rqd;
641 int ret;
Javier González17912c42017-04-15 20:55:37 +0200642 DECLARE_COMPLETION_ONSTACK(wait);
Matias Bjørling10995c32017-01-31 13:17:10 +0100643
644 memset(&rqd, 0, sizeof(struct nvm_rq));
645
Javier González17912c42017-04-15 20:55:37 +0200646 rqd.opcode = NVM_OP_ERASE;
647 rqd.end_io = nvm_end_io_sync;
648 rqd.private = &wait;
649 rqd.flags = geo->plane_mode >> 1;
650
651 ret = nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørling10995c32017-01-31 13:17:10 +0100652 if (ret)
653 return ret;
654
Javier González17912c42017-04-15 20:55:37 +0200655 ret = nvm_submit_io(tgt_dev, &rqd);
656 if (ret) {
657 pr_err("rrpr: erase I/O submission failed: %d\n", ret);
658 goto free_ppa_list;
659 }
660 wait_for_completion_io(&wait);
Matias Bjørling10995c32017-01-31 13:17:10 +0100661
Javier González17912c42017-04-15 20:55:37 +0200662free_ppa_list:
663 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Matias Bjørling10995c32017-01-31 13:17:10 +0100664
665 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100666}
Javier González17912c42017-04-15 20:55:37 +0200667EXPORT_SYMBOL(nvm_erase_sync);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100668
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100669int nvm_get_l2p_tbl(struct nvm_tgt_dev *tgt_dev, u64 slba, u32 nlb,
Javier González959e9112016-11-28 22:39:11 +0100670 nvm_l2p_update_fn *update_l2p, void *priv)
671{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100672 struct nvm_dev *dev = tgt_dev->parent;
673
Javier González959e9112016-11-28 22:39:11 +0100674 if (!dev->ops->get_l2p_tbl)
675 return 0;
676
677 return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv);
678}
679EXPORT_SYMBOL(nvm_get_l2p_tbl);
680
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100681int nvm_get_area(struct nvm_tgt_dev *tgt_dev, sector_t *lba, sector_t len)
Javier González959e9112016-11-28 22:39:11 +0100682{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100683 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100684 struct nvm_geo *geo = &dev->geo;
685 struct nvm_area *area, *prev, *next;
686 sector_t begin = 0;
687 sector_t max_sectors = (geo->sec_size * dev->total_secs) >> 9;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100688
Matias Bjørlingade69e22017-01-31 13:17:09 +0100689 if (len > max_sectors)
690 return -EINVAL;
691
692 area = kmalloc(sizeof(struct nvm_area), GFP_KERNEL);
693 if (!area)
694 return -ENOMEM;
695
696 prev = NULL;
697
698 spin_lock(&dev->lock);
699 list_for_each_entry(next, &dev->area_list, list) {
700 if (begin + len > next->begin) {
701 begin = next->end;
702 prev = next;
703 continue;
704 }
705 break;
706 }
707
708 if ((begin + len) > max_sectors) {
709 spin_unlock(&dev->lock);
710 kfree(area);
711 return -EINVAL;
712 }
713
714 area->begin = *lba = begin;
715 area->end = begin + len;
716
717 if (prev) /* insert into sorted order */
718 list_add(&area->list, &prev->list);
719 else
720 list_add(&area->list, &dev->area_list);
721 spin_unlock(&dev->lock);
722
723 return 0;
Javier González959e9112016-11-28 22:39:11 +0100724}
725EXPORT_SYMBOL(nvm_get_area);
726
Matias Bjørlingade69e22017-01-31 13:17:09 +0100727void nvm_put_area(struct nvm_tgt_dev *tgt_dev, sector_t begin)
Javier González959e9112016-11-28 22:39:11 +0100728{
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100729 struct nvm_dev *dev = tgt_dev->parent;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100730 struct nvm_area *area;
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100731
Matias Bjørlingade69e22017-01-31 13:17:09 +0100732 spin_lock(&dev->lock);
733 list_for_each_entry(area, &dev->area_list, list) {
734 if (area->begin != begin)
735 continue;
736
737 list_del(&area->list);
738 spin_unlock(&dev->lock);
739 kfree(area);
740 return;
741 }
742 spin_unlock(&dev->lock);
Javier González959e9112016-11-28 22:39:11 +0100743}
744EXPORT_SYMBOL(nvm_put_area);
745
Javier González17912c42017-04-15 20:55:37 +0200746int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
Matias Bjørling8680f162016-07-07 09:54:22 +0200747 const struct ppa_addr *ppas, int nr_ppas, int vblk)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100748{
Javier González17912c42017-04-15 20:55:37 +0200749 struct nvm_dev *dev = tgt_dev->parent;
750 struct nvm_geo *geo = &tgt_dev->geo;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100751 int i, plane_cnt, pl_idx;
Matias Bjørling8680f162016-07-07 09:54:22 +0200752 struct ppa_addr ppa;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100753
Javier González8e79b5c2016-11-28 22:39:06 +0100754 if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
Javier González6d5be952016-05-06 20:03:20 +0200755 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100756 rqd->ppa_addr = ppas[0];
757
758 return 0;
759 }
760
Javier González6d5be952016-05-06 20:03:20 +0200761 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100762 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
763 if (!rqd->ppa_list) {
764 pr_err("nvm: failed to allocate dma memory\n");
765 return -ENOMEM;
766 }
767
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200768 if (!vblk) {
769 for (i = 0; i < nr_ppas; i++)
770 rqd->ppa_list[i] = ppas[i];
771 } else {
Javier González8e79b5c2016-11-28 22:39:06 +0100772 plane_cnt = geo->plane_mode;
Javier González6d5be952016-05-06 20:03:20 +0200773 rqd->nr_ppas *= plane_cnt;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200774
Matias Bjørling556755e2016-01-12 07:49:26 +0100775 for (i = 0; i < nr_ppas; i++) {
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200776 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
Matias Bjørling8680f162016-07-07 09:54:22 +0200777 ppa = ppas[i];
778 ppa.g.pl = pl_idx;
779 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200780 }
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100781 }
782 }
783
784 return 0;
785}
786EXPORT_SYMBOL(nvm_set_rqd_ppalist);
787
Javier González17912c42017-04-15 20:55:37 +0200788void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100789{
790 if (!rqd->ppa_list)
791 return;
792
Javier González17912c42017-04-15 20:55:37 +0200793 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100794}
795EXPORT_SYMBOL(nvm_free_rqd_ppalist);
796
Matias Bjørling06894ef2017-01-31 13:17:17 +0100797void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100798{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100799 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100800
801 /* Convert address space */
802 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100803 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100804
Matias Bjørling06894ef2017-01-31 13:17:17 +0100805 if (rqd->end_io)
806 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100807}
808EXPORT_SYMBOL(nvm_end_io);
809
Matias Bjørling22e8c972016-05-06 20:02:58 +0200810/*
811 * folds a bad block list from its plane representation to its virtual
812 * block representation. The fold is done in place and reduced size is
813 * returned.
814 *
815 * If any of the planes status are bad or grown bad block, the virtual block
816 * is marked bad. If not bad, the first plane state acts as the block state.
817 */
818int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
819{
Javier González8e79b5c2016-11-28 22:39:06 +0100820 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200821 int blk, offset, pl, blktype;
822
Javier González8e79b5c2016-11-28 22:39:06 +0100823 if (nr_blks != geo->blks_per_lun * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200824 return -EINVAL;
825
Javier González8e79b5c2016-11-28 22:39:06 +0100826 for (blk = 0; blk < geo->blks_per_lun; blk++) {
827 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200828 blktype = blks[offset];
829
830 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100831 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200832 if (blks[offset + pl] &
833 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
834 blktype = blks[offset + pl];
835 break;
836 }
837 }
838
839 blks[blk] = blktype;
840 }
841
Javier González8e79b5c2016-11-28 22:39:06 +0100842 return geo->blks_per_lun;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200843}
844EXPORT_SYMBOL(nvm_bb_tbl_fold);
845
Javier González333ba052016-11-28 22:39:14 +0100846int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
847 u8 *blks)
848{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100849 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100850
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100851 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100852
853 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100854}
855EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
856
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100857static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
858{
Javier González8e79b5c2016-11-28 22:39:06 +0100859 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100860 int i;
861
Javier González8e79b5c2016-11-28 22:39:06 +0100862 dev->lps_per_blk = geo->pgs_per_blk;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100863 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
864 if (!dev->lptbl)
865 return -ENOMEM;
866
867 /* Just a linear array */
868 for (i = 0; i < dev->lps_per_blk; i++)
869 dev->lptbl[i] = i;
870
871 return 0;
872}
873
874static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
875{
876 int i, p;
877 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
878
879 if (!mlc->num_pairs)
880 return 0;
881
882 dev->lps_per_blk = mlc->num_pairs;
883 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
884 if (!dev->lptbl)
885 return -ENOMEM;
886
887 /* The lower page table encoding consists of a list of bytes, where each
888 * has a lower and an upper half. The first half byte maintains the
889 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200890 * previous incrementation value
891 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100892 dev->lptbl[0] = mlc->pairs[0] & 0xF;
893 for (i = 1; i < dev->lps_per_blk; i++) {
894 p = mlc->pairs[i >> 1];
895 if (i & 0x1) /* upper */
896 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
897 else /* lower */
898 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
899 }
900
901 return 0;
902}
903
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100904static int nvm_core_init(struct nvm_dev *dev)
905{
906 struct nvm_id *id = &dev->identity;
Matias Bjørling19bd6fe2017-01-31 13:17:15 +0100907 struct nvm_id_group *grp = &id->grp;
Javier González8e79b5c2016-11-28 22:39:06 +0100908 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200909 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100910
Javier González8e79b5c2016-11-28 22:39:06 +0100911 /* Whole device values */
912 geo->nr_chnls = grp->num_ch;
913 geo->luns_per_chnl = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100914
Javier González8e79b5c2016-11-28 22:39:06 +0100915 /* Generic device values */
916 geo->pgs_per_blk = grp->num_pg;
917 geo->blks_per_lun = grp->num_blk;
918 geo->nr_planes = grp->num_pln;
919 geo->fpg_size = grp->fpg_sz;
920 geo->pfpg_size = grp->fpg_sz * grp->num_pln;
921 geo->sec_size = grp->csecs;
922 geo->oob_size = grp->sos;
923 geo->sec_per_pg = grp->fpg_sz / grp->csecs;
924 geo->mccap = grp->mccap;
925 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
926
927 geo->plane_mode = NVM_PLANE_SINGLE;
928 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100929
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100930 if (grp->mpos & 0x020202)
Javier González8e79b5c2016-11-28 22:39:06 +0100931 geo->plane_mode = NVM_PLANE_DOUBLE;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100932 if (grp->mpos & 0x040404)
Javier González8e79b5c2016-11-28 22:39:06 +0100933 geo->plane_mode = NVM_PLANE_QUAD;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100934
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200935 if (grp->mtype != 0) {
936 pr_err("nvm: memory type not supported\n");
937 return -EINVAL;
938 }
939
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100940 /* calculated values */
Javier González8e79b5c2016-11-28 22:39:06 +0100941 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
942 geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
943 geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
944 geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100945
Javier González8e79b5c2016-11-28 22:39:06 +0100946 dev->total_secs = geo->nr_luns * geo->sec_per_lun;
947 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100948 sizeof(unsigned long), GFP_KERNEL);
949 if (!dev->lun_map)
950 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200951
952 switch (grp->fmtype) {
953 case NVM_ID_FMTYPE_SLC:
954 if (nvm_init_slc_tbl(dev, grp)) {
955 ret = -ENOMEM;
956 goto err_fmtype;
957 }
958 break;
959 case NVM_ID_FMTYPE_MLC:
960 if (nvm_init_mlc_tbl(dev, grp)) {
961 ret = -ENOMEM;
962 goto err_fmtype;
963 }
964 break;
965 default:
966 pr_err("nvm: flash type not supported\n");
967 ret = -EINVAL;
968 goto err_fmtype;
969 }
970
Matias Bjørlingade69e22017-01-31 13:17:09 +0100971 INIT_LIST_HEAD(&dev->area_list);
972 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100973 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100974 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100975
Matias Bjørlingade69e22017-01-31 13:17:09 +0100976 ret = nvm_register_map(dev);
977 if (ret)
978 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200979
Matias Bjørlingade69e22017-01-31 13:17:09 +0100980 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100981 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200982err_fmtype:
983 kfree(dev->lun_map);
984 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100985}
986
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200987void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100988{
989 if (!dev)
990 return;
991
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200992 if (dev->dma_pool)
993 dev->ops->destroy_dma_pool(dev->dma_pool);
994
Matias Bjørlingade69e22017-01-31 13:17:09 +0100995 kfree(dev->rmap);
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100996 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200997 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200998 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100999}
1000
1001static int nvm_init(struct nvm_dev *dev)
1002{
Javier González8e79b5c2016-11-28 22:39:06 +01001003 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001004 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001005
Matias Bjørling16f26c32015-12-06 11:25:48 +01001006 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001007 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001008 goto err;
1009 }
1010
Matias Bjørling19bd6fe2017-01-31 13:17:15 +01001011 pr_debug("nvm: ver:%x nvm_vendor:%x\n",
1012 dev->identity.ver_id, dev->identity.vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001013
1014 if (dev->identity.ver_id != 1) {
1015 pr_err("nvm: device not supported by kernel.");
1016 goto err;
1017 }
1018
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001019 ret = nvm_core_init(dev);
1020 if (ret) {
1021 pr_err("nvm: could not initialize core structures.\n");
1022 goto err;
1023 }
1024
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001025 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +01001026 dev->name, geo->sec_per_pg, geo->nr_planes,
1027 geo->pgs_per_blk, geo->blks_per_lun,
1028 geo->nr_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001029 return 0;
1030err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001031 pr_err("nvm: failed to initialize nvm\n");
1032 return ret;
1033}
1034
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001035struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001036{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001037 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1038}
1039EXPORT_SYMBOL(nvm_alloc_dev);
1040
1041int nvm_register(struct nvm_dev *dev)
1042{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001043 int ret;
1044
Matias Bjørlingade69e22017-01-31 13:17:09 +01001045 if (!dev->q || !dev->ops)
1046 return -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001047
Wenwei Taod1601472015-11-28 16:49:25 +01001048 if (dev->ops->max_phys_sect > 256) {
1049 pr_info("nvm: max sectors supported is 256.\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001050 return -EINVAL;
Wenwei Taod1601472015-11-28 16:49:25 +01001051 }
1052
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001053 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +02001054 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
1055 if (!dev->dma_pool) {
1056 pr_err("nvm: could not create dma pool\n");
Matias Bjørlingade69e22017-01-31 13:17:09 +01001057 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001058 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001059 }
1060
Matias Bjørlingade69e22017-01-31 13:17:09 +01001061 ret = nvm_init(dev);
1062 if (ret)
1063 goto err_init;
Matias Bjørlingb7692072016-01-12 07:49:38 +01001064
Matias Bjørling762796b2015-12-06 11:25:49 +01001065 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001066 down_write(&nvm_lock);
1067 list_add(&dev->devices, &nvm_devices);
1068 up_write(&nvm_lock);
1069
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001070 return 0;
1071err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +01001072 dev->ops->destroy_dma_pool(dev->dma_pool);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001073 return ret;
1074}
1075EXPORT_SYMBOL(nvm_register);
1076
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001077void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001078{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001079 struct nvm_target *t, *tmp;
1080
1081 mutex_lock(&dev->mlock);
1082 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1083 if (t->dev->parent != dev)
1084 continue;
1085 __nvm_remove_target(t);
1086 }
1087 mutex_unlock(&dev->mlock);
1088
Wenwei Taod0a712c2015-11-28 16:49:28 +01001089 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001090 list_del(&dev->devices);
1091 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001092
Matias Bjørling3dc87dd2016-11-28 22:38:53 +01001093 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001094}
1095EXPORT_SYMBOL(nvm_unregister);
1096
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001097static int __nvm_configure_create(struct nvm_ioctl_create *create)
1098{
1099 struct nvm_dev *dev;
1100 struct nvm_ioctl_create_simple *s;
1101
Wenwei Taod0a712c2015-11-28 16:49:28 +01001102 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001103 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001104 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001105
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001106 if (!dev) {
1107 pr_err("nvm: device not found\n");
1108 return -EINVAL;
1109 }
1110
1111 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
1112 pr_err("nvm: config type not valid\n");
1113 return -EINVAL;
1114 }
1115 s = &create->conf.s;
1116
Matias Bjørling6732c742017-02-15 16:25:33 +01001117 if (s->lun_begin == -1 && s->lun_end == -1) {
1118 s->lun_begin = 0;
1119 s->lun_end = dev->geo.nr_luns - 1;
1120 }
1121
Matias Bjørling0e5ffd12017-02-15 16:25:32 +01001122 if (s->lun_begin > s->lun_end || s->lun_end >= dev->geo.nr_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001123 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Matias Bjørling0e5ffd12017-02-15 16:25:32 +01001124 s->lun_begin, s->lun_end, dev->geo.nr_luns - 1);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001125 return -EINVAL;
1126 }
1127
Matias Bjørlingade69e22017-01-31 13:17:09 +01001128 return nvm_create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001129}
1130
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001131static long nvm_ioctl_info(struct file *file, void __user *arg)
1132{
1133 struct nvm_ioctl_info *info;
1134 struct nvm_tgt_type *tt;
1135 int tgt_iter = 0;
1136
1137 if (!capable(CAP_SYS_ADMIN))
1138 return -EPERM;
1139
1140 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1141 if (IS_ERR(info))
1142 return -EFAULT;
1143
1144 info->version[0] = NVM_VERSION_MAJOR;
1145 info->version[1] = NVM_VERSION_MINOR;
1146 info->version[2] = NVM_VERSION_PATCH;
1147
1148 down_write(&nvm_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001149 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001150 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1151
1152 tgt->version[0] = tt->version[0];
1153 tgt->version[1] = tt->version[1];
1154 tgt->version[2] = tt->version[2];
1155 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1156
1157 tgt_iter++;
1158 }
1159
1160 info->tgtsize = tgt_iter;
1161 up_write(&nvm_lock);
1162
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001163 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1164 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001165 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001166 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001167
1168 kfree(info);
1169 return 0;
1170}
1171
1172static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1173{
1174 struct nvm_ioctl_get_devices *devices;
1175 struct nvm_dev *dev;
1176 int i = 0;
1177
1178 if (!capable(CAP_SYS_ADMIN))
1179 return -EPERM;
1180
1181 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1182 if (!devices)
1183 return -ENOMEM;
1184
1185 down_write(&nvm_lock);
1186 list_for_each_entry(dev, &nvm_devices, devices) {
1187 struct nvm_ioctl_device_info *info = &devices->info[i];
1188
1189 sprintf(info->devname, "%s", dev->name);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001190
Matias Bjørlingade69e22017-01-31 13:17:09 +01001191 /* kept for compatibility */
1192 info->bmversion[0] = 1;
1193 info->bmversion[1] = 0;
1194 info->bmversion[2] = 0;
1195 sprintf(info->bmname, "%s", "gennvm");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001196 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001197
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001198 if (i > 31) {
1199 pr_err("nvm: max 31 devices can be reported.\n");
1200 break;
1201 }
1202 }
1203 up_write(&nvm_lock);
1204
1205 devices->nr_devices = i;
1206
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001207 if (copy_to_user(arg, devices,
1208 sizeof(struct nvm_ioctl_get_devices))) {
1209 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001210 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001211 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001212
1213 kfree(devices);
1214 return 0;
1215}
1216
1217static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1218{
1219 struct nvm_ioctl_create create;
1220
1221 if (!capable(CAP_SYS_ADMIN))
1222 return -EPERM;
1223
1224 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1225 return -EFAULT;
1226
1227 create.dev[DISK_NAME_LEN - 1] = '\0';
1228 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1229 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1230
1231 if (create.flags != 0) {
1232 pr_err("nvm: no flags supported\n");
1233 return -EINVAL;
1234 }
1235
1236 return __nvm_configure_create(&create);
1237}
1238
1239static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1240{
1241 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001242 struct nvm_dev *dev;
1243 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001244
1245 if (!capable(CAP_SYS_ADMIN))
1246 return -EPERM;
1247
1248 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1249 return -EFAULT;
1250
1251 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1252
1253 if (remove.flags != 0) {
1254 pr_err("nvm: no flags supported\n");
1255 return -EINVAL;
1256 }
1257
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001258 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001259 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001260 if (!ret)
1261 break;
1262 }
1263
1264 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001265}
1266
Matias Bjørlingade69e22017-01-31 13:17:09 +01001267/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001268static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1269{
1270 struct nvm_ioctl_dev_init init;
1271
1272 if (!capable(CAP_SYS_ADMIN))
1273 return -EPERM;
1274
1275 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1276 return -EFAULT;
1277
1278 if (init.flags != 0) {
1279 pr_err("nvm: no flags supported\n");
1280 return -EINVAL;
1281 }
1282
Matias Bjørlingade69e22017-01-31 13:17:09 +01001283 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001284}
1285
Matias Bjørlingade69e22017-01-31 13:17:09 +01001286/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001287static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1288{
1289 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001290
1291 if (!capable(CAP_SYS_ADMIN))
1292 return -EPERM;
1293
1294 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1295 return -EFAULT;
1296
1297 fact.dev[DISK_NAME_LEN - 1] = '\0';
1298
1299 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1300 return -EINVAL;
1301
Matias Bjørlingbf643182016-02-04 15:13:27 +01001302 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001303}
1304
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001305static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1306{
1307 void __user *argp = (void __user *)arg;
1308
1309 switch (cmd) {
1310 case NVM_INFO:
1311 return nvm_ioctl_info(file, argp);
1312 case NVM_GET_DEVICES:
1313 return nvm_ioctl_get_devices(file, argp);
1314 case NVM_DEV_CREATE:
1315 return nvm_ioctl_dev_create(file, argp);
1316 case NVM_DEV_REMOVE:
1317 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001318 case NVM_DEV_INIT:
1319 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001320 case NVM_DEV_FACTORY:
1321 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001322 }
1323 return 0;
1324}
1325
1326static const struct file_operations _ctl_fops = {
1327 .open = nonseekable_open,
1328 .unlocked_ioctl = nvm_ctl_ioctl,
1329 .owner = THIS_MODULE,
1330 .llseek = noop_llseek,
1331};
1332
1333static struct miscdevice _nvm_misc = {
1334 .minor = MISC_DYNAMIC_MINOR,
1335 .name = "lightnvm",
1336 .nodename = "lightnvm/control",
1337 .fops = &_ctl_fops,
1338};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001339builtin_misc_device(_nvm_misc);