blob: 9704db219866ae4b86573bec8ef82991f1f5f941 [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>
Rakesh Pandit90014822017-10-13 14:45:50 +020025#include <linux/module.h>
Paul Gortmaker389b2a12016-10-29 16:38:41 -040026#include <linux/moduleparam.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010027#include <linux/miscdevice.h>
28#include <linux/lightnvm.h>
Matias Bjørling912761622016-01-12 07:49:21 +010029#include <linux/sched/sysctl.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010030
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020031static LIST_HEAD(nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +020032static DECLARE_RWSEM(nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010033static LIST_HEAD(nvm_devices);
34static DECLARE_RWSEM(nvm_lock);
35
Matias Bjørlingade69e22017-01-31 13:17:09 +010036/* Map between virtual and physical channel and lun */
37struct nvm_ch_map {
38 int ch_off;
39 int nr_luns;
40 int *lun_offs;
41};
42
43struct nvm_dev_map {
44 struct nvm_ch_map *chnls;
45 int nr_chnls;
46};
47
Matias Bjørlingade69e22017-01-31 13:17:09 +010048static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
49{
50 struct nvm_target *tgt;
51
52 list_for_each_entry(tgt, &dev->targets, list)
53 if (!strcmp(name, tgt->disk->disk_name))
54 return tgt;
55
56 return NULL;
57}
58
Javier Gonzálezbd77b232018-01-05 14:16:05 +010059static bool nvm_target_exists(const char *name)
60{
61 struct nvm_dev *dev;
62 struct nvm_target *tgt;
63 bool ret = false;
64
65 down_write(&nvm_lock);
66 list_for_each_entry(dev, &nvm_devices, devices) {
67 mutex_lock(&dev->mlock);
68 list_for_each_entry(tgt, &dev->targets, list) {
69 if (!strcmp(name, tgt->disk->disk_name)) {
70 ret = true;
71 mutex_unlock(&dev->mlock);
72 goto out;
73 }
74 }
75 mutex_unlock(&dev->mlock);
76 }
77
78out:
79 up_write(&nvm_lock);
80 return ret;
81}
82
Matias Bjørlingade69e22017-01-31 13:17:09 +010083static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
84{
85 int i;
86
87 for (i = lun_begin; i <= lun_end; i++) {
88 if (test_and_set_bit(i, dev->lun_map)) {
89 pr_err("nvm: lun %d already allocated\n", i);
90 goto err;
91 }
92 }
93
94 return 0;
95err:
Javier González507f7d62017-05-03 11:19:05 +020096 while (--i >= lun_begin)
Matias Bjørlingade69e22017-01-31 13:17:09 +010097 clear_bit(i, dev->lun_map);
98
99 return -EBUSY;
100}
101
102static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
103 int lun_end)
104{
105 int i;
106
107 for (i = lun_begin; i <= lun_end; i++)
108 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
109}
110
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200111static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100112{
113 struct nvm_dev *dev = tgt_dev->parent;
114 struct nvm_dev_map *dev_map = tgt_dev->map;
115 int i, j;
116
117 for (i = 0; i < dev_map->nr_chnls; i++) {
118 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
119 int *lun_offs = ch_map->lun_offs;
120 int ch = i + ch_map->ch_off;
121
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200122 if (clear) {
123 for (j = 0; j < ch_map->nr_luns; j++) {
124 int lun = j + lun_offs[j];
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100125 int lunid = (ch * dev->geo.nr_luns) + lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100126
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200127 WARN_ON(!test_and_clear_bit(lunid,
128 dev->lun_map));
129 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100130 }
131
132 kfree(ch_map->lun_offs);
133 }
134
135 kfree(dev_map->chnls);
136 kfree(dev_map);
137
138 kfree(tgt_dev->luns);
139 kfree(tgt_dev);
140}
141
142static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
Javier Gonzáleze5392732018-01-05 14:16:14 +0100143 u16 lun_begin, u16 lun_end,
144 u16 op)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100145{
146 struct nvm_tgt_dev *tgt_dev = NULL;
147 struct nvm_dev_map *dev_rmap = dev->rmap;
148 struct nvm_dev_map *dev_map;
149 struct ppa_addr *luns;
150 int nr_luns = lun_end - lun_begin + 1;
151 int luns_left = nr_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100152 int nr_chnls = nr_luns / dev->geo.nr_luns;
153 int nr_chnls_mod = nr_luns % dev->geo.nr_luns;
154 int bch = lun_begin / dev->geo.nr_luns;
155 int blun = lun_begin % dev->geo.nr_luns;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100156 int lunid = 0;
157 int lun_balanced = 1;
158 int prev_nr_luns;
159 int i, j;
160
Matias Bjørlingade69e22017-01-31 13:17:09 +0100161 nr_chnls = (nr_chnls_mod == 0) ? nr_chnls : nr_chnls + 1;
162
163 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
164 if (!dev_map)
165 goto err_dev;
166
167 dev_map->chnls = kcalloc(nr_chnls, sizeof(struct nvm_ch_map),
168 GFP_KERNEL);
169 if (!dev_map->chnls)
170 goto err_chnls;
171
172 luns = kcalloc(nr_luns, sizeof(struct ppa_addr), GFP_KERNEL);
173 if (!luns)
174 goto err_luns;
175
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100176 prev_nr_luns = (luns_left > dev->geo.nr_luns) ?
177 dev->geo.nr_luns : luns_left;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100178 for (i = 0; i < nr_chnls; i++) {
179 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
180 int *lun_roffs = ch_rmap->lun_offs;
181 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
182 int *lun_offs;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100183 int luns_in_chnl = (luns_left > dev->geo.nr_luns) ?
184 dev->geo.nr_luns : luns_left;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100185
186 if (lun_balanced && prev_nr_luns != luns_in_chnl)
187 lun_balanced = 0;
188
189 ch_map->ch_off = ch_rmap->ch_off = bch;
190 ch_map->nr_luns = luns_in_chnl;
191
192 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
193 if (!lun_offs)
194 goto err_ch;
195
196 for (j = 0; j < luns_in_chnl; j++) {
197 luns[lunid].ppa = 0;
198 luns[lunid].g.ch = i;
199 luns[lunid++].g.lun = j;
200
201 lun_offs[j] = blun;
202 lun_roffs[j + blun] = blun;
203 }
204
205 ch_map->lun_offs = lun_offs;
206
207 /* when starting a new channel, lun offset is reset */
208 blun = 0;
209 luns_left -= luns_in_chnl;
210 }
211
212 dev_map->nr_chnls = nr_chnls;
213
214 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
215 if (!tgt_dev)
216 goto err_ch;
217
218 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
219 /* Target device only owns a portion of the physical device */
220 tgt_dev->geo.nr_chnls = nr_chnls;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100221 tgt_dev->geo.all_luns = nr_luns;
222 tgt_dev->geo.nr_luns = (lun_balanced) ? prev_nr_luns : -1;
Javier Gonzáleze5392732018-01-05 14:16:14 +0100223 tgt_dev->geo.op = op;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100224 tgt_dev->total_secs = nr_luns * tgt_dev->geo.sec_per_lun;
225 tgt_dev->q = dev->q;
226 tgt_dev->map = dev_map;
227 tgt_dev->luns = luns;
228 memcpy(&tgt_dev->identity, &dev->identity, sizeof(struct nvm_id));
229
230 tgt_dev->parent = dev;
231
232 return tgt_dev;
233err_ch:
Javier González507f7d62017-05-03 11:19:05 +0200234 while (--i >= 0)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100235 kfree(dev_map->chnls[i].lun_offs);
236 kfree(luns);
237err_luns:
238 kfree(dev_map->chnls);
239err_chnls:
240 kfree(dev_map);
241err_dev:
242 return tgt_dev;
243}
244
245static const struct block_device_operations nvm_fops = {
246 .owner = THIS_MODULE,
247};
248
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100249static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200250{
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100251 struct nvm_tgt_type *tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200252
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100253 list_for_each_entry(tt, &nvm_tgt_types, list)
254 if (!strcmp(name, tt->name))
255 return tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200256
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100257 return NULL;
258}
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200259
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100260static struct nvm_tgt_type *nvm_find_target_type(const char *name)
261{
262 struct nvm_tgt_type *tt;
263
264 down_write(&nvm_tgtt_lock);
265 tt = __nvm_find_target_type(name);
266 up_write(&nvm_tgtt_lock);
267
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200268 return tt;
269}
270
Javier Gonzáleze5392732018-01-05 14:16:14 +0100271static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin,
272 int lun_end)
273{
274 if (lun_begin > lun_end || lun_end >= geo->all_luns) {
275 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
276 lun_begin, lun_end, geo->all_luns - 1);
277 return -EINVAL;
278 }
279
280 return 0;
281}
282
283static int __nvm_config_simple(struct nvm_dev *dev,
284 struct nvm_ioctl_create_simple *s)
285{
286 struct nvm_geo *geo = &dev->geo;
287
288 if (s->lun_begin == -1 && s->lun_end == -1) {
289 s->lun_begin = 0;
290 s->lun_end = geo->all_luns - 1;
291 }
292
293 return nvm_config_check_luns(geo, s->lun_begin, s->lun_end);
294}
295
296static int __nvm_config_extended(struct nvm_dev *dev,
297 struct nvm_ioctl_create_extended *e)
298{
299 struct nvm_geo *geo = &dev->geo;
300
301 if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) {
302 e->lun_begin = 0;
303 e->lun_end = dev->geo.all_luns - 1;
304 }
305
306 /* op not set falls into target's default */
307 if (e->op == 0xFFFF)
308 e->op = NVM_TARGET_DEFAULT_OP;
309
310 if (e->op < NVM_TARGET_MIN_OP ||
311 e->op > NVM_TARGET_MAX_OP) {
312 pr_err("nvm: invalid over provisioning value\n");
313 return -EINVAL;
314 }
315
316 return nvm_config_check_luns(geo, e->lun_begin, e->lun_end);
317}
318
Matias Bjørlingade69e22017-01-31 13:17:09 +0100319static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
320{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100321 struct nvm_ioctl_create_extended e;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100322 struct request_queue *tqueue;
323 struct gendisk *tdisk;
324 struct nvm_tgt_type *tt;
325 struct nvm_target *t;
326 struct nvm_tgt_dev *tgt_dev;
327 void *targetdata;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300328 int ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100329
Javier Gonzáleze5392732018-01-05 14:16:14 +0100330 switch (create->conf.type) {
331 case NVM_CONFIG_TYPE_SIMPLE:
332 ret = __nvm_config_simple(dev, &create->conf.s);
333 if (ret)
334 return ret;
335
336 e.lun_begin = create->conf.s.lun_begin;
337 e.lun_end = create->conf.s.lun_end;
338 e.op = NVM_TARGET_DEFAULT_OP;
339 break;
340 case NVM_CONFIG_TYPE_EXTENDED:
341 ret = __nvm_config_extended(dev, &create->conf.e);
342 if (ret)
343 return ret;
344
345 e = create->conf.e;
346 break;
347 default:
348 pr_err("nvm: config type not valid\n");
349 return -EINVAL;
350 }
351
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100352 tt = nvm_find_target_type(create->tgttype);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100353 if (!tt) {
354 pr_err("nvm: target type %s not found\n", create->tgttype);
355 return -EINVAL;
356 }
357
Javier Gonzálezbd77b232018-01-05 14:16:05 +0100358 if (nvm_target_exists(create->tgtname)) {
359 pr_err("nvm: target name already exists (%s)\n",
360 create->tgtname);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100361 return -EINVAL;
362 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100363
Javier Gonzáleze5392732018-01-05 14:16:14 +0100364 ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end);
Rakesh Pandit12e9a6d62017-06-27 14:55:33 +0300365 if (ret)
366 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100367
368 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300369 if (!t) {
370 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100371 goto err_reserve;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300372 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100373
Javier Gonzáleze5392732018-01-05 14:16:14 +0100374 tgt_dev = nvm_create_tgt_dev(dev, e.lun_begin, e.lun_end, e.op);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100375 if (!tgt_dev) {
376 pr_err("nvm: could not create target device\n");
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300377 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100378 goto err_t;
379 }
380
Matias Bjørlingade69e22017-01-31 13:17:09 +0100381 tdisk = alloc_disk(0);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300382 if (!tdisk) {
383 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200384 goto err_dev;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300385 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200386
Bart Van Assche5ee05242018-02-28 10:15:31 -0800387 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node, NULL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300388 if (!tqueue) {
389 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200390 goto err_disk;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300391 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200392 blk_queue_make_request(tqueue, tt->make_rq);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100393
Javier González6eb08242017-04-15 20:55:49 +0200394 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100395 tdisk->flags = GENHD_FL_EXT_DEVT;
396 tdisk->major = 0;
397 tdisk->first_minor = 0;
398 tdisk->fops = &nvm_fops;
399 tdisk->queue = tqueue;
400
Javier González4af3f752017-04-15 20:55:45 +0200401 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300402 if (IS_ERR(targetdata)) {
403 ret = PTR_ERR(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100404 goto err_init;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300405 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100406
407 tdisk->private_data = targetdata;
408 tqueue->queuedata = targetdata;
409
Matias Bjørling89a09c52018-03-30 00:05:04 +0200410 blk_queue_max_hw_sectors(tqueue,
411 (dev->geo.sec_size >> 9) * NVM_MAX_VLBA);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100412
413 set_capacity(tdisk, tt->capacity(targetdata));
414 add_disk(tdisk);
415
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300416 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
417 ret = -ENOMEM;
Javier González9a69b0e2017-01-31 13:17:20 +0100418 goto err_sysfs;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300419 }
Javier González9a69b0e2017-01-31 13:17:20 +0100420
Matias Bjørlingade69e22017-01-31 13:17:09 +0100421 t->type = tt;
422 t->disk = tdisk;
423 t->dev = tgt_dev;
424
425 mutex_lock(&dev->mlock);
426 list_add_tail(&t->list, &dev->targets);
427 mutex_unlock(&dev->mlock);
428
Rakesh Pandit90014822017-10-13 14:45:50 +0200429 __module_get(tt->owner);
430
Matias Bjørlingade69e22017-01-31 13:17:09 +0100431 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100432err_sysfs:
433 if (tt->exit)
434 tt->exit(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100435err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100436 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600437 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200438err_disk:
439 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100440err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200441 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100442err_t:
443 kfree(t);
444err_reserve:
Javier Gonzáleze5392732018-01-05 14:16:14 +0100445 nvm_release_luns_err(dev, e.lun_begin, e.lun_end);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300446 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100447}
448
449static void __nvm_remove_target(struct nvm_target *t)
450{
451 struct nvm_tgt_type *tt = t->type;
452 struct gendisk *tdisk = t->disk;
453 struct request_queue *q = tdisk->queue;
454
455 del_gendisk(tdisk);
456 blk_cleanup_queue(q);
457
Javier González9a69b0e2017-01-31 13:17:20 +0100458 if (tt->sysfs_exit)
459 tt->sysfs_exit(tdisk);
460
Matias Bjørlingade69e22017-01-31 13:17:09 +0100461 if (tt->exit)
462 tt->exit(tdisk->private_data);
463
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200464 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100465 put_disk(tdisk);
Rakesh Pandit90014822017-10-13 14:45:50 +0200466 module_put(t->type->owner);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100467
468 list_del(&t->list);
469 kfree(t);
470}
471
472/**
473 * nvm_remove_tgt - Removes a target from the media manager
474 * @dev: device
475 * @remove: ioctl structure with target name to remove.
476 *
477 * Returns:
478 * 0: on success
479 * 1: on not found
480 * <0: on error
481 */
482static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
483{
484 struct nvm_target *t;
485
486 mutex_lock(&dev->mlock);
487 t = nvm_find_target(dev, remove->tgtname);
488 if (!t) {
489 mutex_unlock(&dev->mlock);
490 return 1;
491 }
492 __nvm_remove_target(t);
493 mutex_unlock(&dev->mlock);
494
495 return 0;
496}
497
498static int nvm_register_map(struct nvm_dev *dev)
499{
500 struct nvm_dev_map *rmap;
501 int i, j;
502
503 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
504 if (!rmap)
505 goto err_rmap;
506
507 rmap->chnls = kcalloc(dev->geo.nr_chnls, sizeof(struct nvm_ch_map),
508 GFP_KERNEL);
509 if (!rmap->chnls)
510 goto err_chnls;
511
512 for (i = 0; i < dev->geo.nr_chnls; i++) {
513 struct nvm_ch_map *ch_rmap;
514 int *lun_roffs;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100515 int luns_in_chnl = dev->geo.nr_luns;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100516
517 ch_rmap = &rmap->chnls[i];
518
519 ch_rmap->ch_off = -1;
520 ch_rmap->nr_luns = luns_in_chnl;
521
522 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
523 if (!lun_roffs)
524 goto err_ch;
525
526 for (j = 0; j < luns_in_chnl; j++)
527 lun_roffs[j] = -1;
528
529 ch_rmap->lun_offs = lun_roffs;
530 }
531
532 dev->rmap = rmap;
533
534 return 0;
535err_ch:
536 while (--i >= 0)
537 kfree(rmap->chnls[i].lun_offs);
538err_chnls:
539 kfree(rmap);
540err_rmap:
541 return -ENOMEM;
542}
543
Javier González7a3de2b2017-04-15 20:55:39 +0200544static void nvm_unregister_map(struct nvm_dev *dev)
545{
546 struct nvm_dev_map *rmap = dev->rmap;
547 int i;
548
549 for (i = 0; i < dev->geo.nr_chnls; i++)
550 kfree(rmap->chnls[i].lun_offs);
551
552 kfree(rmap->chnls);
553 kfree(rmap);
554}
555
Matias Bjørling61a561d2017-01-31 13:17:13 +0100556static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100557{
558 struct nvm_dev_map *dev_map = tgt_dev->map;
559 struct nvm_ch_map *ch_map = &dev_map->chnls[p->g.ch];
560 int lun_off = ch_map->lun_offs[p->g.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100561
562 p->g.ch += ch_map->ch_off;
563 p->g.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100564}
565
Matias Bjørling61a561d2017-01-31 13:17:13 +0100566static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100567{
568 struct nvm_dev *dev = tgt_dev->parent;
569 struct nvm_dev_map *dev_rmap = dev->rmap;
570 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->g.ch];
571 int lun_roff = ch_rmap->lun_offs[p->g.lun];
572
573 p->g.ch -= ch_rmap->ch_off;
574 p->g.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100575}
576
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100577static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
578 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100579{
580 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100581
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100582 for (i = 0; i < nr_ppas; i++) {
583 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
584 ppa_list[i] = generic_to_dev_addr(tgt_dev, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100585 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100586}
587
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100588static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
589 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100590{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100591 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100592
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100593 for (i = 0; i < nr_ppas; i++) {
594 ppa_list[i] = dev_to_generic_addr(tgt_dev, ppa_list[i]);
595 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
596 }
597}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100598
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100599static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
600{
601 if (rqd->nr_ppas == 1) {
602 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
603 return;
604 }
605
606 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
607}
608
609static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
610{
611 if (rqd->nr_ppas == 1) {
612 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
613 return;
614 }
615
616 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100617}
618
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200619int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100620{
621 int ret = 0;
622
Matias Bjørling5cd90782016-07-07 09:54:17 +0200623 down_write(&nvm_tgtt_lock);
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100624 if (__nvm_find_target_type(tt->name))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100625 ret = -EEXIST;
626 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200627 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200628 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100629
630 return ret;
631}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200632EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100633
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200634void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100635{
636 if (!tt)
637 return;
638
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200639 down_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100640 list_del(&tt->list);
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200641 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100642}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200643EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100644
645void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
646 dma_addr_t *dma_handler)
647{
Javier González75b85642016-05-06 20:03:13 +0200648 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100649 dma_handler);
650}
651EXPORT_SYMBOL(nvm_dev_dma_alloc);
652
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100653void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100654{
Javier González75b85642016-05-06 20:03:13 +0200655 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100656}
657EXPORT_SYMBOL(nvm_dev_dma_free);
658
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100659static struct nvm_dev *nvm_find_nvm_dev(const char *name)
660{
661 struct nvm_dev *dev;
662
663 list_for_each_entry(dev, &nvm_devices, devices)
664 if (!strcmp(name, dev->name))
665 return dev;
666
667 return NULL;
668}
669
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200670static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
671 const struct ppa_addr *ppas, int nr_ppas)
672{
673 struct nvm_dev *dev = tgt_dev->parent;
674 struct nvm_geo *geo = &tgt_dev->geo;
675 int i, plane_cnt, pl_idx;
676 struct ppa_addr ppa;
677
678 if (geo->plane_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
679 rqd->nr_ppas = nr_ppas;
680 rqd->ppa_addr = ppas[0];
681
682 return 0;
683 }
684
685 rqd->nr_ppas = nr_ppas;
686 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
687 if (!rqd->ppa_list) {
688 pr_err("nvm: failed to allocate dma memory\n");
689 return -ENOMEM;
690 }
691
692 plane_cnt = geo->plane_mode;
693 rqd->nr_ppas *= plane_cnt;
694
695 for (i = 0; i < nr_ppas; i++) {
696 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
697 ppa = ppas[i];
698 ppa.g.pl = pl_idx;
699 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
700 }
701 }
702
703 return 0;
704}
705
706static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
707 struct nvm_rq *rqd)
708{
709 if (!rqd->ppa_list)
710 return;
711
712 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
713}
714
715
Javier González333ba052016-11-28 22:39:14 +0100716int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
717 int nr_ppas, int type)
718{
719 struct nvm_dev *dev = tgt_dev->parent;
720 struct nvm_rq rqd;
721 int ret;
722
Matias Bjørling89a09c52018-03-30 00:05:04 +0200723 if (nr_ppas > NVM_MAX_VLBA) {
Javier González333ba052016-11-28 22:39:14 +0100724 pr_err("nvm: unable to update all blocks atomically\n");
725 return -EINVAL;
726 }
727
728 memset(&rqd, 0, sizeof(struct nvm_rq));
729
Rakesh Panditef56b9c2017-10-13 14:46:30 +0200730 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100731 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100732
733 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
Javier González17912c42017-04-15 20:55:37 +0200734 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100735 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100736 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100737 return -EINVAL;
738 }
739
740 return 0;
741}
742EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
743
Javier González8e536242016-11-28 22:39:10 +0100744int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100745{
Javier González8e536242016-11-28 22:39:10 +0100746 struct nvm_dev *dev = tgt_dev->parent;
Javier González3e505af2017-06-26 11:57:10 +0200747 int ret;
Javier González8e536242016-11-28 22:39:10 +0100748
Matias Bjørlingade69e22017-01-31 13:17:09 +0100749 if (!dev->ops->submit_io)
750 return -ENODEV;
751
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100752 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100753
754 rqd->dev = tgt_dev;
Javier González3e505af2017-06-26 11:57:10 +0200755
756 /* In case of error, fail with right address format */
757 ret = dev->ops->submit_io(dev, rqd);
758 if (ret)
759 nvm_rq_dev_to_tgt(tgt_dev, rqd);
760 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100761}
762EXPORT_SYMBOL(nvm_submit_io);
763
Javier González1a94b2d2017-10-13 14:46:47 +0200764int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100765{
Javier González1a94b2d2017-10-13 14:46:47 +0200766 struct nvm_dev *dev = tgt_dev->parent;
767 int ret;
Javier González17912c42017-04-15 20:55:37 +0200768
Javier González1a94b2d2017-10-13 14:46:47 +0200769 if (!dev->ops->submit_io_sync)
770 return -ENODEV;
771
772 nvm_rq_tgt_to_dev(tgt_dev, rqd);
773
774 rqd->dev = tgt_dev;
775
776 /* In case of error, fail with right address format */
777 ret = dev->ops->submit_io_sync(dev, rqd);
778 nvm_rq_dev_to_tgt(tgt_dev, rqd);
779
780 return ret;
Javier González17912c42017-04-15 20:55:37 +0200781}
Javier González1a94b2d2017-10-13 14:46:47 +0200782EXPORT_SYMBOL(nvm_submit_io_sync);
Javier González17912c42017-04-15 20:55:37 +0200783
Matias Bjørling06894ef2017-01-31 13:17:17 +0100784void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100785{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100786 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100787
788 /* Convert address space */
789 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100790 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100791
Matias Bjørling06894ef2017-01-31 13:17:17 +0100792 if (rqd->end_io)
793 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100794}
795EXPORT_SYMBOL(nvm_end_io);
796
Matias Bjørling22e8c972016-05-06 20:02:58 +0200797/*
798 * folds a bad block list from its plane representation to its virtual
799 * block representation. The fold is done in place and reduced size is
800 * returned.
801 *
802 * If any of the planes status are bad or grown bad block, the virtual block
803 * is marked bad. If not bad, the first plane state acts as the block state.
804 */
805int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
806{
Javier González8e79b5c2016-11-28 22:39:06 +0100807 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200808 int blk, offset, pl, blktype;
809
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100810 if (nr_blks != geo->nr_chks * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200811 return -EINVAL;
812
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100813 for (blk = 0; blk < geo->nr_chks; blk++) {
Javier González8e79b5c2016-11-28 22:39:06 +0100814 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200815 blktype = blks[offset];
816
817 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100818 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200819 if (blks[offset + pl] &
820 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
821 blktype = blks[offset + pl];
822 break;
823 }
824 }
825
826 blks[blk] = blktype;
827 }
828
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100829 return geo->nr_chks;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200830}
831EXPORT_SYMBOL(nvm_bb_tbl_fold);
832
Javier González333ba052016-11-28 22:39:14 +0100833int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
834 u8 *blks)
835{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100836 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100837
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100838 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100839
840 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100841}
842EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
843
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100844static int nvm_core_init(struct nvm_dev *dev)
845{
846 struct nvm_id *id = &dev->identity;
Javier González8e79b5c2016-11-28 22:39:06 +0100847 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200848 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100849
Javier González8e79b5c2016-11-28 22:39:06 +0100850 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
851
Matias Bjørlingc6ac3f32018-03-30 00:05:01 +0200852 if (id->mtype != 0) {
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200853 pr_err("nvm: memory type not supported\n");
854 return -EINVAL;
855 }
856
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100857 /* Whole device values */
Matias Bjørlingc6ac3f32018-03-30 00:05:01 +0200858 geo->nr_chnls = id->num_ch;
859 geo->nr_luns = id->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100860
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100861 /* Generic device geometry values */
Matias Bjørlingc6ac3f32018-03-30 00:05:01 +0200862 geo->ws_min = id->ws_min;
863 geo->ws_opt = id->ws_opt;
864 geo->ws_seq = id->ws_seq;
865 geo->ws_per_chk = id->ws_per_chk;
866 geo->nr_chks = id->num_chk;
867 geo->sec_size = id->csecs;
868 geo->oob_size = id->sos;
869 geo->mccap = id->mccap;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100870
Matias Bjørlingc6ac3f32018-03-30 00:05:01 +0200871 geo->sec_per_chk = id->clba;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100872 geo->sec_per_lun = geo->sec_per_chk * geo->nr_chks;
873 geo->all_luns = geo->nr_luns * geo->nr_chnls;
874
875 /* 1.2 spec device geometry values */
876 geo->plane_mode = 1 << geo->ws_seq;
877 geo->nr_planes = geo->ws_opt / geo->ws_min;
878 geo->sec_per_pg = geo->ws_min;
879 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
880
881 dev->total_secs = geo->all_luns * geo->sec_per_lun;
882 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100883 sizeof(unsigned long), GFP_KERNEL);
884 if (!dev->lun_map)
885 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200886
Matias Bjørlingade69e22017-01-31 13:17:09 +0100887 INIT_LIST_HEAD(&dev->area_list);
888 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100889 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100890 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100891
Matias Bjørlingade69e22017-01-31 13:17:09 +0100892 ret = nvm_register_map(dev);
893 if (ret)
894 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200895
Matias Bjørlingade69e22017-01-31 13:17:09 +0100896 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100897 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200898err_fmtype:
899 kfree(dev->lun_map);
900 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100901}
902
Javier González46b160c2017-04-15 20:55:46 +0200903static void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100904{
905 if (!dev)
906 return;
907
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200908 if (dev->dma_pool)
909 dev->ops->destroy_dma_pool(dev->dma_pool);
910
Javier González7a3de2b2017-04-15 20:55:39 +0200911 nvm_unregister_map(dev);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200912 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200913 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100914}
915
916static int nvm_init(struct nvm_dev *dev)
917{
Javier González8e79b5c2016-11-28 22:39:06 +0100918 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100919 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100920
Matias Bjørling16f26c32015-12-06 11:25:48 +0100921 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100922 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100923 goto err;
924 }
925
Matias Bjørling62771fe02018-03-30 00:05:02 +0200926 if (dev->identity.ver_id != 1 && dev->identity.ver_id != 2) {
927 pr_err("nvm: device ver_id %d not supported by kernel.\n",
928 dev->identity.ver_id);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100929 goto err;
930 }
931
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100932 ret = nvm_core_init(dev);
933 if (ret) {
934 pr_err("nvm: could not initialize core structures.\n");
935 goto err;
936 }
937
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100938 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +0100939 dev->name, geo->sec_per_pg, geo->nr_planes,
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100940 geo->ws_per_chk, geo->nr_chks,
941 geo->all_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100942 return 0;
943err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100944 pr_err("nvm: failed to initialize nvm\n");
945 return ret;
946}
947
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200948struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100949{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200950 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
951}
952EXPORT_SYMBOL(nvm_alloc_dev);
953
954int nvm_register(struct nvm_dev *dev)
955{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100956 int ret;
957
Matias Bjørlingade69e22017-01-31 13:17:09 +0100958 if (!dev->q || !dev->ops)
959 return -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100960
Matias Bjørling89a09c52018-03-30 00:05:04 +0200961 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
962 if (!dev->dma_pool) {
963 pr_err("nvm: could not create dma pool\n");
964 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100965 }
966
Matias Bjørlingade69e22017-01-31 13:17:09 +0100967 ret = nvm_init(dev);
968 if (ret)
969 goto err_init;
Matias Bjørlingb7692072016-01-12 07:49:38 +0100970
Matias Bjørling762796b2015-12-06 11:25:49 +0100971 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100972 down_write(&nvm_lock);
973 list_add(&dev->devices, &nvm_devices);
974 up_write(&nvm_lock);
975
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100976 return 0;
977err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100978 dev->ops->destroy_dma_pool(dev->dma_pool);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100979 return ret;
980}
981EXPORT_SYMBOL(nvm_register);
982
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200983void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100984{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100985 struct nvm_target *t, *tmp;
986
987 mutex_lock(&dev->mlock);
988 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
989 if (t->dev->parent != dev)
990 continue;
991 __nvm_remove_target(t);
992 }
993 mutex_unlock(&dev->mlock);
994
Wenwei Taod0a712c2015-11-28 16:49:28 +0100995 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100996 list_del(&dev->devices);
997 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +0100998
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100999 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001000}
1001EXPORT_SYMBOL(nvm_unregister);
1002
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001003static int __nvm_configure_create(struct nvm_ioctl_create *create)
1004{
1005 struct nvm_dev *dev;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001006
Wenwei Taod0a712c2015-11-28 16:49:28 +01001007 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001008 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001009 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001010
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001011 if (!dev) {
1012 pr_err("nvm: device not found\n");
1013 return -EINVAL;
1014 }
1015
Matias Bjørlingade69e22017-01-31 13:17:09 +01001016 return nvm_create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001017}
1018
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001019static long nvm_ioctl_info(struct file *file, void __user *arg)
1020{
1021 struct nvm_ioctl_info *info;
1022 struct nvm_tgt_type *tt;
1023 int tgt_iter = 0;
1024
1025 if (!capable(CAP_SYS_ADMIN))
1026 return -EPERM;
1027
1028 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1029 if (IS_ERR(info))
1030 return -EFAULT;
1031
1032 info->version[0] = NVM_VERSION_MAJOR;
1033 info->version[1] = NVM_VERSION_MINOR;
1034 info->version[2] = NVM_VERSION_PATCH;
1035
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001036 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001037 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001038 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1039
1040 tgt->version[0] = tt->version[0];
1041 tgt->version[1] = tt->version[1];
1042 tgt->version[2] = tt->version[2];
1043 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1044
1045 tgt_iter++;
1046 }
1047
1048 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001049 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001050
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001051 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1052 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001053 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001054 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001055
1056 kfree(info);
1057 return 0;
1058}
1059
1060static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1061{
1062 struct nvm_ioctl_get_devices *devices;
1063 struct nvm_dev *dev;
1064 int i = 0;
1065
1066 if (!capable(CAP_SYS_ADMIN))
1067 return -EPERM;
1068
1069 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1070 if (!devices)
1071 return -ENOMEM;
1072
1073 down_write(&nvm_lock);
1074 list_for_each_entry(dev, &nvm_devices, devices) {
1075 struct nvm_ioctl_device_info *info = &devices->info[i];
1076
Javier González6eb08242017-04-15 20:55:49 +02001077 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001078
Matias Bjørlingade69e22017-01-31 13:17:09 +01001079 /* kept for compatibility */
1080 info->bmversion[0] = 1;
1081 info->bmversion[1] = 0;
1082 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001083 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001084 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001085
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001086 if (i > 31) {
1087 pr_err("nvm: max 31 devices can be reported.\n");
1088 break;
1089 }
1090 }
1091 up_write(&nvm_lock);
1092
1093 devices->nr_devices = i;
1094
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001095 if (copy_to_user(arg, devices,
1096 sizeof(struct nvm_ioctl_get_devices))) {
1097 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001098 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001099 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001100
1101 kfree(devices);
1102 return 0;
1103}
1104
1105static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1106{
1107 struct nvm_ioctl_create create;
1108
1109 if (!capable(CAP_SYS_ADMIN))
1110 return -EPERM;
1111
1112 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1113 return -EFAULT;
1114
Javier Gonzáleze5392732018-01-05 14:16:14 +01001115 if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED &&
1116 create.conf.e.rsv != 0) {
1117 pr_err("nvm: reserved config field in use\n");
1118 return -EINVAL;
1119 }
1120
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001121 create.dev[DISK_NAME_LEN - 1] = '\0';
1122 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1123 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1124
1125 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001126 __u32 flags = create.flags;
1127
1128 /* Check for valid flags */
1129 if (flags & NVM_TARGET_FACTORY)
1130 flags &= ~NVM_TARGET_FACTORY;
1131
1132 if (flags) {
1133 pr_err("nvm: flag not supported\n");
1134 return -EINVAL;
1135 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001136 }
1137
1138 return __nvm_configure_create(&create);
1139}
1140
1141static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1142{
1143 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001144 struct nvm_dev *dev;
1145 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001146
1147 if (!capable(CAP_SYS_ADMIN))
1148 return -EPERM;
1149
1150 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1151 return -EFAULT;
1152
1153 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1154
1155 if (remove.flags != 0) {
1156 pr_err("nvm: no flags supported\n");
1157 return -EINVAL;
1158 }
1159
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001160 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001161 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001162 if (!ret)
1163 break;
1164 }
1165
1166 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001167}
1168
Matias Bjørlingade69e22017-01-31 13:17:09 +01001169/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001170static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1171{
1172 struct nvm_ioctl_dev_init init;
1173
1174 if (!capable(CAP_SYS_ADMIN))
1175 return -EPERM;
1176
1177 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1178 return -EFAULT;
1179
1180 if (init.flags != 0) {
1181 pr_err("nvm: no flags supported\n");
1182 return -EINVAL;
1183 }
1184
Matias Bjørlingade69e22017-01-31 13:17:09 +01001185 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001186}
1187
Matias Bjørlingade69e22017-01-31 13:17:09 +01001188/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001189static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1190{
1191 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001192
1193 if (!capable(CAP_SYS_ADMIN))
1194 return -EPERM;
1195
1196 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1197 return -EFAULT;
1198
1199 fact.dev[DISK_NAME_LEN - 1] = '\0';
1200
1201 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1202 return -EINVAL;
1203
Matias Bjørlingbf643182016-02-04 15:13:27 +01001204 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001205}
1206
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001207static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1208{
1209 void __user *argp = (void __user *)arg;
1210
1211 switch (cmd) {
1212 case NVM_INFO:
1213 return nvm_ioctl_info(file, argp);
1214 case NVM_GET_DEVICES:
1215 return nvm_ioctl_get_devices(file, argp);
1216 case NVM_DEV_CREATE:
1217 return nvm_ioctl_dev_create(file, argp);
1218 case NVM_DEV_REMOVE:
1219 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001220 case NVM_DEV_INIT:
1221 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001222 case NVM_DEV_FACTORY:
1223 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001224 }
1225 return 0;
1226}
1227
1228static const struct file_operations _ctl_fops = {
1229 .open = nonseekable_open,
1230 .unlocked_ioctl = nvm_ctl_ioctl,
1231 .owner = THIS_MODULE,
1232 .llseek = noop_llseek,
1233};
1234
1235static struct miscdevice _nvm_misc = {
1236 .minor = MISC_DYNAMIC_MINOR,
1237 .name = "lightnvm",
1238 .nodename = "lightnvm/control",
1239 .fops = &_ctl_fops,
1240};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001241builtin_misc_device(_nvm_misc);