blob: 964352720a0351175cc04063321ddf4390ea1647 [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;
Javier Gonzáleza40afad2018-03-30 00:05:14 +020039 int num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +010040 int *lun_offs;
41};
42
43struct nvm_dev_map {
44 struct nvm_ch_map *chnls;
Javier Gonzáleza40afad2018-03-30 00:05:14 +020045 int num_ch;
Matias Bjørlingade69e22017-01-31 13:17:09 +010046};
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
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200117 for (i = 0; i < dev_map->num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100118 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) {
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200123 for (j = 0; j < ch_map->num_lun; j++) {
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200124 int lun = j + lun_offs[j];
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200125 int lunid = (ch * dev->geo.num_lun) + 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;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200150 int num_lun = lun_end - lun_begin + 1;
151 int luns_left = num_lun;
152 int num_ch = num_lun / dev->geo.num_lun;
153 int num_ch_mod = num_lun % dev->geo.num_lun;
154 int bch = lun_begin / dev->geo.num_lun;
155 int blun = lun_begin % dev->geo.num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100156 int lunid = 0;
157 int lun_balanced = 1;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200158 int sec_per_lun, prev_num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100159 int i, j;
160
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200161 num_ch = (num_ch_mod == 0) ? num_ch : num_ch + 1;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100162
163 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
164 if (!dev_map)
165 goto err_dev;
166
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200167 dev_map->chnls = kcalloc(num_ch, sizeof(struct nvm_ch_map), GFP_KERNEL);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100168 if (!dev_map->chnls)
169 goto err_chnls;
170
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200171 luns = kcalloc(num_lun, sizeof(struct ppa_addr), GFP_KERNEL);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100172 if (!luns)
173 goto err_luns;
174
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200175 prev_num_lun = (luns_left > dev->geo.num_lun) ?
176 dev->geo.num_lun : luns_left;
177 for (i = 0; i < num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100178 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
179 int *lun_roffs = ch_rmap->lun_offs;
180 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
181 int *lun_offs;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200182 int luns_in_chnl = (luns_left > dev->geo.num_lun) ?
183 dev->geo.num_lun : luns_left;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100184
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200185 if (lun_balanced && prev_num_lun != luns_in_chnl)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100186 lun_balanced = 0;
187
188 ch_map->ch_off = ch_rmap->ch_off = bch;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200189 ch_map->num_lun = luns_in_chnl;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100190
191 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
192 if (!lun_offs)
193 goto err_ch;
194
195 for (j = 0; j < luns_in_chnl; j++) {
196 luns[lunid].ppa = 0;
Javier González69471512018-03-30 00:05:15 +0200197 luns[lunid].a.ch = i;
198 luns[lunid++].a.lun = j;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100199
200 lun_offs[j] = blun;
201 lun_roffs[j + blun] = blun;
202 }
203
204 ch_map->lun_offs = lun_offs;
205
206 /* when starting a new channel, lun offset is reset */
207 blun = 0;
208 luns_left -= luns_in_chnl;
209 }
210
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200211 dev_map->num_ch = num_ch;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100212
213 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
214 if (!tgt_dev)
215 goto err_ch;
216
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200217 /* Inherit device geometry from parent */
Matias Bjørlingade69e22017-01-31 13:17:09 +0100218 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200219
Matias Bjørlingade69e22017-01-31 13:17:09 +0100220 /* Target device only owns a portion of the physical device */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200221 tgt_dev->geo.num_ch = num_ch;
222 tgt_dev->geo.num_lun = (lun_balanced) ? prev_num_lun : -1;
223 tgt_dev->geo.all_luns = num_lun;
224 tgt_dev->geo.all_chunks = num_lun * dev->geo.num_chk;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200225
Javier Gonzáleze5392732018-01-05 14:16:14 +0100226 tgt_dev->geo.op = op;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200227
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200228 sec_per_lun = dev->geo.clba * dev->geo.num_chk;
229 tgt_dev->geo.total_secs = num_lun * sec_per_lun;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200230
Matias Bjørlingade69e22017-01-31 13:17:09 +0100231 tgt_dev->q = dev->q;
232 tgt_dev->map = dev_map;
233 tgt_dev->luns = luns;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100234 tgt_dev->parent = dev;
235
236 return tgt_dev;
237err_ch:
Javier González507f7d62017-05-03 11:19:05 +0200238 while (--i >= 0)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100239 kfree(dev_map->chnls[i].lun_offs);
240 kfree(luns);
241err_luns:
242 kfree(dev_map->chnls);
243err_chnls:
244 kfree(dev_map);
245err_dev:
246 return tgt_dev;
247}
248
249static const struct block_device_operations nvm_fops = {
250 .owner = THIS_MODULE,
251};
252
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100253static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200254{
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100255 struct nvm_tgt_type *tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200256
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100257 list_for_each_entry(tt, &nvm_tgt_types, list)
258 if (!strcmp(name, tt->name))
259 return tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200260
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100261 return NULL;
262}
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200263
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100264static struct nvm_tgt_type *nvm_find_target_type(const char *name)
265{
266 struct nvm_tgt_type *tt;
267
268 down_write(&nvm_tgtt_lock);
269 tt = __nvm_find_target_type(name);
270 up_write(&nvm_tgtt_lock);
271
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200272 return tt;
273}
274
Javier Gonzáleze5392732018-01-05 14:16:14 +0100275static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin,
276 int lun_end)
277{
278 if (lun_begin > lun_end || lun_end >= geo->all_luns) {
279 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
280 lun_begin, lun_end, geo->all_luns - 1);
281 return -EINVAL;
282 }
283
284 return 0;
285}
286
287static int __nvm_config_simple(struct nvm_dev *dev,
288 struct nvm_ioctl_create_simple *s)
289{
290 struct nvm_geo *geo = &dev->geo;
291
292 if (s->lun_begin == -1 && s->lun_end == -1) {
293 s->lun_begin = 0;
294 s->lun_end = geo->all_luns - 1;
295 }
296
297 return nvm_config_check_luns(geo, s->lun_begin, s->lun_end);
298}
299
300static int __nvm_config_extended(struct nvm_dev *dev,
301 struct nvm_ioctl_create_extended *e)
302{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100303 if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) {
304 e->lun_begin = 0;
305 e->lun_end = dev->geo.all_luns - 1;
306 }
307
308 /* op not set falls into target's default */
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200309 if (e->op == 0xFFFF) {
Javier Gonzáleze5392732018-01-05 14:16:14 +0100310 e->op = NVM_TARGET_DEFAULT_OP;
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200311 } else if (e->op < NVM_TARGET_MIN_OP || e->op > NVM_TARGET_MAX_OP) {
Javier Gonzáleze5392732018-01-05 14:16:14 +0100312 pr_err("nvm: invalid over provisioning value\n");
313 return -EINVAL;
314 }
315
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200316 return nvm_config_check_luns(&dev->geo, e->lun_begin, e->lun_end);
Javier Gonzáleze5392732018-01-05 14:16:14 +0100317}
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
Matias Bjørling656e33c2018-10-09 13:11:34 +0200358 if ((tt->flags & NVM_TGT_F_HOST_L2P) != (dev->geo.dom & NVM_RSP_L2P)) {
359 pr_err("nvm: device is incompatible with target L2P type.\n");
360 return -EINVAL;
361 }
362
Javier Gonzálezbd77b232018-01-05 14:16:05 +0100363 if (nvm_target_exists(create->tgtname)) {
364 pr_err("nvm: target name already exists (%s)\n",
365 create->tgtname);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100366 return -EINVAL;
367 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100368
Javier Gonzáleze5392732018-01-05 14:16:14 +0100369 ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end);
Rakesh Pandit12e9a6d62017-06-27 14:55:33 +0300370 if (ret)
371 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100372
373 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300374 if (!t) {
375 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100376 goto err_reserve;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300377 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100378
Javier Gonzáleze5392732018-01-05 14:16:14 +0100379 tgt_dev = nvm_create_tgt_dev(dev, e.lun_begin, e.lun_end, e.op);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100380 if (!tgt_dev) {
381 pr_err("nvm: could not create target device\n");
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300382 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100383 goto err_t;
384 }
385
Matias Bjørlingade69e22017-01-31 13:17:09 +0100386 tdisk = alloc_disk(0);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300387 if (!tdisk) {
388 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200389 goto err_dev;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300390 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200391
Bart Van Assche5ee05242018-02-28 10:15:31 -0800392 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node, NULL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300393 if (!tqueue) {
394 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200395 goto err_disk;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300396 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200397 blk_queue_make_request(tqueue, tt->make_rq);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100398
Javier González6eb08242017-04-15 20:55:49 +0200399 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100400 tdisk->flags = GENHD_FL_EXT_DEVT;
401 tdisk->major = 0;
402 tdisk->first_minor = 0;
403 tdisk->fops = &nvm_fops;
404 tdisk->queue = tqueue;
405
Javier González4af3f752017-04-15 20:55:45 +0200406 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300407 if (IS_ERR(targetdata)) {
408 ret = PTR_ERR(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100409 goto err_init;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300410 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100411
412 tdisk->private_data = targetdata;
413 tqueue->queuedata = targetdata;
414
Matias Bjørling89a09c52018-03-30 00:05:04 +0200415 blk_queue_max_hw_sectors(tqueue,
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200416 (dev->geo.csecs >> 9) * NVM_MAX_VLBA);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100417
418 set_capacity(tdisk, tt->capacity(targetdata));
419 add_disk(tdisk);
420
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300421 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
422 ret = -ENOMEM;
Javier González9a69b0e2017-01-31 13:17:20 +0100423 goto err_sysfs;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300424 }
Javier González9a69b0e2017-01-31 13:17:20 +0100425
Matias Bjørlingade69e22017-01-31 13:17:09 +0100426 t->type = tt;
427 t->disk = tdisk;
428 t->dev = tgt_dev;
429
430 mutex_lock(&dev->mlock);
431 list_add_tail(&t->list, &dev->targets);
432 mutex_unlock(&dev->mlock);
433
Rakesh Pandit90014822017-10-13 14:45:50 +0200434 __module_get(tt->owner);
435
Matias Bjørlingade69e22017-01-31 13:17:09 +0100436 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100437err_sysfs:
438 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200439 tt->exit(targetdata, true);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100440err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100441 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600442 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200443err_disk:
444 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100445err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200446 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100447err_t:
448 kfree(t);
449err_reserve:
Javier Gonzáleze5392732018-01-05 14:16:14 +0100450 nvm_release_luns_err(dev, e.lun_begin, e.lun_end);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300451 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100452}
453
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200454static void __nvm_remove_target(struct nvm_target *t, bool graceful)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100455{
456 struct nvm_tgt_type *tt = t->type;
457 struct gendisk *tdisk = t->disk;
458 struct request_queue *q = tdisk->queue;
459
460 del_gendisk(tdisk);
461 blk_cleanup_queue(q);
462
Javier González9a69b0e2017-01-31 13:17:20 +0100463 if (tt->sysfs_exit)
464 tt->sysfs_exit(tdisk);
465
Matias Bjørlingade69e22017-01-31 13:17:09 +0100466 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200467 tt->exit(tdisk->private_data, graceful);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100468
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200469 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100470 put_disk(tdisk);
Rakesh Pandit90014822017-10-13 14:45:50 +0200471 module_put(t->type->owner);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100472
473 list_del(&t->list);
474 kfree(t);
475}
476
477/**
478 * nvm_remove_tgt - Removes a target from the media manager
479 * @dev: device
480 * @remove: ioctl structure with target name to remove.
481 *
482 * Returns:
483 * 0: on success
484 * 1: on not found
485 * <0: on error
486 */
487static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
488{
489 struct nvm_target *t;
490
491 mutex_lock(&dev->mlock);
492 t = nvm_find_target(dev, remove->tgtname);
493 if (!t) {
494 mutex_unlock(&dev->mlock);
495 return 1;
496 }
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200497 __nvm_remove_target(t, true);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100498 mutex_unlock(&dev->mlock);
499
500 return 0;
501}
502
503static int nvm_register_map(struct nvm_dev *dev)
504{
505 struct nvm_dev_map *rmap;
506 int i, j;
507
508 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
509 if (!rmap)
510 goto err_rmap;
511
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200512 rmap->chnls = kcalloc(dev->geo.num_ch, sizeof(struct nvm_ch_map),
Matias Bjørlingade69e22017-01-31 13:17:09 +0100513 GFP_KERNEL);
514 if (!rmap->chnls)
515 goto err_chnls;
516
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200517 for (i = 0; i < dev->geo.num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100518 struct nvm_ch_map *ch_rmap;
519 int *lun_roffs;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200520 int luns_in_chnl = dev->geo.num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100521
522 ch_rmap = &rmap->chnls[i];
523
524 ch_rmap->ch_off = -1;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200525 ch_rmap->num_lun = luns_in_chnl;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100526
527 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
528 if (!lun_roffs)
529 goto err_ch;
530
531 for (j = 0; j < luns_in_chnl; j++)
532 lun_roffs[j] = -1;
533
534 ch_rmap->lun_offs = lun_roffs;
535 }
536
537 dev->rmap = rmap;
538
539 return 0;
540err_ch:
541 while (--i >= 0)
542 kfree(rmap->chnls[i].lun_offs);
543err_chnls:
544 kfree(rmap);
545err_rmap:
546 return -ENOMEM;
547}
548
Javier González7a3de2b2017-04-15 20:55:39 +0200549static void nvm_unregister_map(struct nvm_dev *dev)
550{
551 struct nvm_dev_map *rmap = dev->rmap;
552 int i;
553
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200554 for (i = 0; i < dev->geo.num_ch; i++)
Javier González7a3de2b2017-04-15 20:55:39 +0200555 kfree(rmap->chnls[i].lun_offs);
556
557 kfree(rmap->chnls);
558 kfree(rmap);
559}
560
Matias Bjørling61a561d2017-01-31 13:17:13 +0100561static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100562{
563 struct nvm_dev_map *dev_map = tgt_dev->map;
Javier González69471512018-03-30 00:05:15 +0200564 struct nvm_ch_map *ch_map = &dev_map->chnls[p->a.ch];
565 int lun_off = ch_map->lun_offs[p->a.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100566
Javier González69471512018-03-30 00:05:15 +0200567 p->a.ch += ch_map->ch_off;
568 p->a.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100569}
570
Matias Bjørling61a561d2017-01-31 13:17:13 +0100571static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100572{
573 struct nvm_dev *dev = tgt_dev->parent;
574 struct nvm_dev_map *dev_rmap = dev->rmap;
Javier González69471512018-03-30 00:05:15 +0200575 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->a.ch];
576 int lun_roff = ch_rmap->lun_offs[p->a.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100577
Javier González69471512018-03-30 00:05:15 +0200578 p->a.ch -= ch_rmap->ch_off;
579 p->a.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100580}
581
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100582static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
583 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100584{
585 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100586
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100587 for (i = 0; i < nr_ppas; i++) {
588 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
Javier González7100d502018-03-30 00:05:16 +0200589 ppa_list[i] = generic_to_dev_addr(tgt_dev->parent, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100590 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100591}
592
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100593static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
594 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100595{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100596 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100597
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100598 for (i = 0; i < nr_ppas; i++) {
Javier González7100d502018-03-30 00:05:16 +0200599 ppa_list[i] = dev_to_generic_addr(tgt_dev->parent, ppa_list[i]);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100600 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
601 }
602}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100603
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100604static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
605{
606 if (rqd->nr_ppas == 1) {
607 nvm_ppa_tgt_to_dev(tgt_dev, &rqd->ppa_addr, 1);
608 return;
609 }
610
611 nvm_ppa_tgt_to_dev(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
612}
613
614static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
615{
616 if (rqd->nr_ppas == 1) {
617 nvm_ppa_dev_to_tgt(tgt_dev, &rqd->ppa_addr, 1);
618 return;
619 }
620
621 nvm_ppa_dev_to_tgt(tgt_dev, rqd->ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100622}
623
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200624int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100625{
626 int ret = 0;
627
Matias Bjørling5cd90782016-07-07 09:54:17 +0200628 down_write(&nvm_tgtt_lock);
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100629 if (__nvm_find_target_type(tt->name))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100630 ret = -EEXIST;
631 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200632 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200633 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100634
635 return ret;
636}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200637EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100638
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200639void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100640{
641 if (!tt)
642 return;
643
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200644 down_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100645 list_del(&tt->list);
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200646 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100647}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200648EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100649
650void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
651 dma_addr_t *dma_handler)
652{
Javier González75b85642016-05-06 20:03:13 +0200653 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100654 dma_handler);
655}
656EXPORT_SYMBOL(nvm_dev_dma_alloc);
657
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100658void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100659{
Javier González75b85642016-05-06 20:03:13 +0200660 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100661}
662EXPORT_SYMBOL(nvm_dev_dma_free);
663
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100664static struct nvm_dev *nvm_find_nvm_dev(const char *name)
665{
666 struct nvm_dev *dev;
667
668 list_for_each_entry(dev, &nvm_devices, devices)
669 if (!strcmp(name, dev->name))
670 return dev;
671
672 return NULL;
673}
674
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200675static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
676 const struct ppa_addr *ppas, int nr_ppas)
677{
678 struct nvm_dev *dev = tgt_dev->parent;
679 struct nvm_geo *geo = &tgt_dev->geo;
680 int i, plane_cnt, pl_idx;
681 struct ppa_addr ppa;
682
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200683 if (geo->pln_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200684 rqd->nr_ppas = nr_ppas;
685 rqd->ppa_addr = ppas[0];
686
687 return 0;
688 }
689
690 rqd->nr_ppas = nr_ppas;
691 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
692 if (!rqd->ppa_list) {
693 pr_err("nvm: failed to allocate dma memory\n");
694 return -ENOMEM;
695 }
696
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200697 plane_cnt = geo->pln_mode;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200698 rqd->nr_ppas *= plane_cnt;
699
700 for (i = 0; i < nr_ppas; i++) {
701 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
702 ppa = ppas[i];
703 ppa.g.pl = pl_idx;
704 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
705 }
706 }
707
708 return 0;
709}
710
711static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
712 struct nvm_rq *rqd)
713{
714 if (!rqd->ppa_list)
715 return;
716
717 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
718}
719
Javier Gonzáleza294c192018-03-30 00:05:17 +0200720int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct nvm_chk_meta *meta,
721 struct ppa_addr ppa, int nchks)
722{
723 struct nvm_dev *dev = tgt_dev->parent;
724
725 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
726
727 return dev->ops->get_chk_meta(tgt_dev->parent, meta,
728 (sector_t)ppa.ppa, nchks);
729}
730EXPORT_SYMBOL(nvm_get_chunk_meta);
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200731
Javier González333ba052016-11-28 22:39:14 +0100732int nvm_set_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
733 int nr_ppas, int type)
734{
735 struct nvm_dev *dev = tgt_dev->parent;
736 struct nvm_rq rqd;
737 int ret;
738
Matias Bjørling89a09c52018-03-30 00:05:04 +0200739 if (nr_ppas > NVM_MAX_VLBA) {
Javier González333ba052016-11-28 22:39:14 +0100740 pr_err("nvm: unable to update all blocks atomically\n");
741 return -EINVAL;
742 }
743
744 memset(&rqd, 0, sizeof(struct nvm_rq));
745
Rakesh Panditef56b9c2017-10-13 14:46:30 +0200746 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100747 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100748
749 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
Javier González17912c42017-04-15 20:55:37 +0200750 nvm_free_rqd_ppalist(tgt_dev, &rqd);
Javier González333ba052016-11-28 22:39:14 +0100751 if (ret) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100752 pr_err("nvm: failed bb mark\n");
Javier González333ba052016-11-28 22:39:14 +0100753 return -EINVAL;
754 }
755
756 return 0;
757}
758EXPORT_SYMBOL(nvm_set_tgt_bb_tbl);
759
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200760static int nvm_set_flags(struct nvm_geo *geo, struct nvm_rq *rqd)
761{
762 int flags = 0;
763
764 if (geo->version == NVM_OCSSD_SPEC_20)
765 return 0;
766
767 if (rqd->is_seq)
768 flags |= geo->pln_mode >> 1;
769
770 if (rqd->opcode == NVM_OP_PREAD)
771 flags |= (NVM_IO_SCRAMBLE_ENABLE | NVM_IO_SUSPEND);
772 else if (rqd->opcode == NVM_OP_PWRITE)
773 flags |= NVM_IO_SCRAMBLE_ENABLE;
774
775 return flags;
776}
777
Javier González8e536242016-11-28 22:39:10 +0100778int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100779{
Javier González8e536242016-11-28 22:39:10 +0100780 struct nvm_dev *dev = tgt_dev->parent;
Javier González3e505af2017-06-26 11:57:10 +0200781 int ret;
Javier González8e536242016-11-28 22:39:10 +0100782
Matias Bjørlingade69e22017-01-31 13:17:09 +0100783 if (!dev->ops->submit_io)
784 return -ENODEV;
785
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100786 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100787
788 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200789 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González3e505af2017-06-26 11:57:10 +0200790
791 /* In case of error, fail with right address format */
792 ret = dev->ops->submit_io(dev, rqd);
793 if (ret)
794 nvm_rq_dev_to_tgt(tgt_dev, rqd);
795 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100796}
797EXPORT_SYMBOL(nvm_submit_io);
798
Javier González1a94b2d2017-10-13 14:46:47 +0200799int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100800{
Javier González1a94b2d2017-10-13 14:46:47 +0200801 struct nvm_dev *dev = tgt_dev->parent;
802 int ret;
Javier González17912c42017-04-15 20:55:37 +0200803
Javier González1a94b2d2017-10-13 14:46:47 +0200804 if (!dev->ops->submit_io_sync)
805 return -ENODEV;
806
807 nvm_rq_tgt_to_dev(tgt_dev, rqd);
808
809 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200810 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200811
812 /* In case of error, fail with right address format */
813 ret = dev->ops->submit_io_sync(dev, rqd);
814 nvm_rq_dev_to_tgt(tgt_dev, rqd);
815
816 return ret;
Javier González17912c42017-04-15 20:55:37 +0200817}
Javier González1a94b2d2017-10-13 14:46:47 +0200818EXPORT_SYMBOL(nvm_submit_io_sync);
Javier González17912c42017-04-15 20:55:37 +0200819
Matias Bjørling06894ef2017-01-31 13:17:17 +0100820void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100821{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100822 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100823
824 /* Convert address space */
825 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100826 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100827
Matias Bjørling06894ef2017-01-31 13:17:17 +0100828 if (rqd->end_io)
829 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100830}
831EXPORT_SYMBOL(nvm_end_io);
832
Matias Bjørling22e8c972016-05-06 20:02:58 +0200833/*
834 * folds a bad block list from its plane representation to its virtual
835 * block representation. The fold is done in place and reduced size is
836 * returned.
837 *
838 * If any of the planes status are bad or grown bad block, the virtual block
839 * is marked bad. If not bad, the first plane state acts as the block state.
840 */
841int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
842{
Javier González8e79b5c2016-11-28 22:39:06 +0100843 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200844 int blk, offset, pl, blktype;
845
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200846 if (nr_blks != geo->num_chk * geo->pln_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200847 return -EINVAL;
848
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200849 for (blk = 0; blk < geo->num_chk; blk++) {
850 offset = blk * geo->pln_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200851 blktype = blks[offset];
852
853 /* Bad blocks on any planes take precedence over other types */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200854 for (pl = 0; pl < geo->pln_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200855 if (blks[offset + pl] &
856 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
857 blktype = blks[offset + pl];
858 break;
859 }
860 }
861
862 blks[blk] = blktype;
863 }
864
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200865 return geo->num_chk;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200866}
867EXPORT_SYMBOL(nvm_bb_tbl_fold);
868
Javier González333ba052016-11-28 22:39:14 +0100869int nvm_get_tgt_bb_tbl(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
870 u8 *blks)
871{
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100872 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +0100873
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100874 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +0100875
876 return dev->ops->get_bb_tbl(dev, ppa, blks);
Javier González333ba052016-11-28 22:39:14 +0100877}
878EXPORT_SYMBOL(nvm_get_tgt_bb_tbl);
879
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100880static int nvm_core_init(struct nvm_dev *dev)
881{
Javier González8e79b5c2016-11-28 22:39:06 +0100882 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200883 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100884
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100885 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100886 sizeof(unsigned long), GFP_KERNEL);
887 if (!dev->lun_map)
888 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200889
Matias Bjørlingade69e22017-01-31 13:17:09 +0100890 INIT_LIST_HEAD(&dev->area_list);
891 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100892 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100893 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100894
Matias Bjørlingade69e22017-01-31 13:17:09 +0100895 ret = nvm_register_map(dev);
896 if (ret)
897 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200898
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100899 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200900err_fmtype:
901 kfree(dev->lun_map);
902 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100903}
904
Javier González46b160c2017-04-15 20:55:46 +0200905static void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100906{
907 if (!dev)
908 return;
909
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200910 if (dev->dma_pool)
911 dev->ops->destroy_dma_pool(dev->dma_pool);
912
Javier González7a3de2b2017-04-15 20:55:39 +0200913 nvm_unregister_map(dev);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200914 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200915 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100916}
917
918static int nvm_init(struct nvm_dev *dev)
919{
Javier González8e79b5c2016-11-28 22:39:06 +0100920 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100921 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100922
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200923 if (dev->ops->identity(dev)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100924 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100925 goto err;
926 }
927
Javier González3cb98f82018-03-30 00:05:11 +0200928 pr_debug("nvm: ver:%u.%u nvm_vendor:%x\n",
929 geo->major_ver_id, geo->minor_ver_id,
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200930 geo->vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100931
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
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200938 pr_info("nvm: registered %s [%u/%u/%u/%u/%u]\n",
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200939 dev->name, dev->geo.ws_min, dev->geo.ws_opt,
940 dev->geo.num_chk, dev->geo.all_luns,
941 dev->geo.num_ch);
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;
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200991 __nvm_remove_target(t, false);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100992 }
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
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001025 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1026 if (IS_ERR(info))
1027 return -EFAULT;
1028
1029 info->version[0] = NVM_VERSION_MAJOR;
1030 info->version[1] = NVM_VERSION_MINOR;
1031 info->version[2] = NVM_VERSION_PATCH;
1032
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001033 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001034 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001035 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1036
1037 tgt->version[0] = tt->version[0];
1038 tgt->version[1] = tt->version[1];
1039 tgt->version[2] = tt->version[2];
1040 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1041
1042 tgt_iter++;
1043 }
1044
1045 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001046 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001047
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001048 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1049 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001050 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001051 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001052
1053 kfree(info);
1054 return 0;
1055}
1056
1057static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1058{
1059 struct nvm_ioctl_get_devices *devices;
1060 struct nvm_dev *dev;
1061 int i = 0;
1062
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001063 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1064 if (!devices)
1065 return -ENOMEM;
1066
1067 down_write(&nvm_lock);
1068 list_for_each_entry(dev, &nvm_devices, devices) {
1069 struct nvm_ioctl_device_info *info = &devices->info[i];
1070
Javier González6eb08242017-04-15 20:55:49 +02001071 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001072
Matias Bjørlingade69e22017-01-31 13:17:09 +01001073 /* kept for compatibility */
1074 info->bmversion[0] = 1;
1075 info->bmversion[1] = 0;
1076 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001077 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001078 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001079
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001080 if (i > 31) {
1081 pr_err("nvm: max 31 devices can be reported.\n");
1082 break;
1083 }
1084 }
1085 up_write(&nvm_lock);
1086
1087 devices->nr_devices = i;
1088
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001089 if (copy_to_user(arg, devices,
1090 sizeof(struct nvm_ioctl_get_devices))) {
1091 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001092 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001093 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001094
1095 kfree(devices);
1096 return 0;
1097}
1098
1099static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1100{
1101 struct nvm_ioctl_create create;
1102
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001103 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1104 return -EFAULT;
1105
Javier Gonzáleze5392732018-01-05 14:16:14 +01001106 if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED &&
1107 create.conf.e.rsv != 0) {
1108 pr_err("nvm: reserved config field in use\n");
1109 return -EINVAL;
1110 }
1111
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001112 create.dev[DISK_NAME_LEN - 1] = '\0';
1113 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1114 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1115
1116 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001117 __u32 flags = create.flags;
1118
1119 /* Check for valid flags */
1120 if (flags & NVM_TARGET_FACTORY)
1121 flags &= ~NVM_TARGET_FACTORY;
1122
1123 if (flags) {
1124 pr_err("nvm: flag not supported\n");
1125 return -EINVAL;
1126 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001127 }
1128
1129 return __nvm_configure_create(&create);
1130}
1131
1132static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1133{
1134 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001135 struct nvm_dev *dev;
1136 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001137
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001138 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1139 return -EFAULT;
1140
1141 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1142
1143 if (remove.flags != 0) {
1144 pr_err("nvm: no flags supported\n");
1145 return -EINVAL;
1146 }
1147
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001148 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001149 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001150 if (!ret)
1151 break;
1152 }
1153
1154 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001155}
1156
Matias Bjørlingade69e22017-01-31 13:17:09 +01001157/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001158static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1159{
1160 struct nvm_ioctl_dev_init init;
1161
Matias Bjørling55696152016-01-12 07:49:37 +01001162 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1163 return -EFAULT;
1164
1165 if (init.flags != 0) {
1166 pr_err("nvm: no flags supported\n");
1167 return -EINVAL;
1168 }
1169
Matias Bjørlingade69e22017-01-31 13:17:09 +01001170 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001171}
1172
Matias Bjørlingade69e22017-01-31 13:17:09 +01001173/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001174static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1175{
1176 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001177
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001178 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1179 return -EFAULT;
1180
1181 fact.dev[DISK_NAME_LEN - 1] = '\0';
1182
1183 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1184 return -EINVAL;
1185
Matias Bjørlingbf643182016-02-04 15:13:27 +01001186 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001187}
1188
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001189static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1190{
1191 void __user *argp = (void __user *)arg;
1192
Johannes Thumshirn40f962d2018-03-30 00:05:07 +02001193 if (!capable(CAP_SYS_ADMIN))
1194 return -EPERM;
1195
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001196 switch (cmd) {
1197 case NVM_INFO:
1198 return nvm_ioctl_info(file, argp);
1199 case NVM_GET_DEVICES:
1200 return nvm_ioctl_get_devices(file, argp);
1201 case NVM_DEV_CREATE:
1202 return nvm_ioctl_dev_create(file, argp);
1203 case NVM_DEV_REMOVE:
1204 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001205 case NVM_DEV_INIT:
1206 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001207 case NVM_DEV_FACTORY:
1208 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001209 }
1210 return 0;
1211}
1212
1213static const struct file_operations _ctl_fops = {
1214 .open = nonseekable_open,
1215 .unlocked_ioctl = nvm_ctl_ioctl,
1216 .owner = THIS_MODULE,
1217 .llseek = noop_llseek,
1218};
1219
1220static struct miscdevice _nvm_misc = {
1221 .minor = MISC_DYNAMIC_MINOR,
1222 .name = "lightnvm",
1223 .nodename = "lightnvm/control",
1224 .fops = &_ctl_fops,
1225};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001226builtin_misc_device(_nvm_misc);