blob: 0e9f7996ff1d5a90c7a93157e48633f89d0805b6 [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
Igor Konopkoe69397e2019-05-04 20:38:08 +020048static void nvm_free(struct kref *ref);
49
Matias Bjørlingade69e22017-01-31 13:17:09 +010050static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
51{
52 struct nvm_target *tgt;
53
54 list_for_each_entry(tgt, &dev->targets, list)
55 if (!strcmp(name, tgt->disk->disk_name))
56 return tgt;
57
58 return NULL;
59}
60
Javier Gonzálezbd77b232018-01-05 14:16:05 +010061static bool nvm_target_exists(const char *name)
62{
63 struct nvm_dev *dev;
64 struct nvm_target *tgt;
65 bool ret = false;
66
67 down_write(&nvm_lock);
68 list_for_each_entry(dev, &nvm_devices, devices) {
69 mutex_lock(&dev->mlock);
70 list_for_each_entry(tgt, &dev->targets, list) {
71 if (!strcmp(name, tgt->disk->disk_name)) {
72 ret = true;
73 mutex_unlock(&dev->mlock);
74 goto out;
75 }
76 }
77 mutex_unlock(&dev->mlock);
78 }
79
80out:
81 up_write(&nvm_lock);
82 return ret;
83}
84
Matias Bjørlingade69e22017-01-31 13:17:09 +010085static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
86{
87 int i;
88
89 for (i = lun_begin; i <= lun_end; i++) {
90 if (test_and_set_bit(i, dev->lun_map)) {
91 pr_err("nvm: lun %d already allocated\n", i);
92 goto err;
93 }
94 }
95
96 return 0;
97err:
Javier González507f7d62017-05-03 11:19:05 +020098 while (--i >= lun_begin)
Matias Bjørlingade69e22017-01-31 13:17:09 +010099 clear_bit(i, dev->lun_map);
100
101 return -EBUSY;
102}
103
104static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
105 int lun_end)
106{
107 int i;
108
109 for (i = lun_begin; i <= lun_end; i++)
110 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
111}
112
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200113static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100114{
115 struct nvm_dev *dev = tgt_dev->parent;
116 struct nvm_dev_map *dev_map = tgt_dev->map;
117 int i, j;
118
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200119 for (i = 0; i < dev_map->num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100120 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
121 int *lun_offs = ch_map->lun_offs;
122 int ch = i + ch_map->ch_off;
123
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200124 if (clear) {
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200125 for (j = 0; j < ch_map->num_lun; j++) {
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200126 int lun = j + lun_offs[j];
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200127 int lunid = (ch * dev->geo.num_lun) + lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100128
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200129 WARN_ON(!test_and_clear_bit(lunid,
130 dev->lun_map));
131 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100132 }
133
134 kfree(ch_map->lun_offs);
135 }
136
137 kfree(dev_map->chnls);
138 kfree(dev_map);
139
140 kfree(tgt_dev->luns);
141 kfree(tgt_dev);
142}
143
144static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
Javier Gonzáleze5392732018-01-05 14:16:14 +0100145 u16 lun_begin, u16 lun_end,
146 u16 op)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100147{
148 struct nvm_tgt_dev *tgt_dev = NULL;
149 struct nvm_dev_map *dev_rmap = dev->rmap;
150 struct nvm_dev_map *dev_map;
151 struct ppa_addr *luns;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200152 int num_lun = lun_end - lun_begin + 1;
153 int luns_left = num_lun;
154 int num_ch = num_lun / dev->geo.num_lun;
155 int num_ch_mod = num_lun % dev->geo.num_lun;
156 int bch = lun_begin / dev->geo.num_lun;
157 int blun = lun_begin % dev->geo.num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100158 int lunid = 0;
159 int lun_balanced = 1;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200160 int sec_per_lun, prev_num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100161 int i, j;
162
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200163 num_ch = (num_ch_mod == 0) ? num_ch : num_ch + 1;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100164
165 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
166 if (!dev_map)
167 goto err_dev;
168
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200169 dev_map->chnls = kcalloc(num_ch, sizeof(struct nvm_ch_map), GFP_KERNEL);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100170 if (!dev_map->chnls)
171 goto err_chnls;
172
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200173 luns = kcalloc(num_lun, sizeof(struct ppa_addr), GFP_KERNEL);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100174 if (!luns)
175 goto err_luns;
176
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200177 prev_num_lun = (luns_left > dev->geo.num_lun) ?
178 dev->geo.num_lun : luns_left;
179 for (i = 0; i < num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100180 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
181 int *lun_roffs = ch_rmap->lun_offs;
182 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
183 int *lun_offs;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200184 int luns_in_chnl = (luns_left > dev->geo.num_lun) ?
185 dev->geo.num_lun : luns_left;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100186
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200187 if (lun_balanced && prev_num_lun != luns_in_chnl)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100188 lun_balanced = 0;
189
190 ch_map->ch_off = ch_rmap->ch_off = bch;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200191 ch_map->num_lun = luns_in_chnl;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100192
193 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
194 if (!lun_offs)
195 goto err_ch;
196
197 for (j = 0; j < luns_in_chnl; j++) {
198 luns[lunid].ppa = 0;
Javier González69471512018-03-30 00:05:15 +0200199 luns[lunid].a.ch = i;
200 luns[lunid++].a.lun = j;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100201
202 lun_offs[j] = blun;
203 lun_roffs[j + blun] = blun;
204 }
205
206 ch_map->lun_offs = lun_offs;
207
208 /* when starting a new channel, lun offset is reset */
209 blun = 0;
210 luns_left -= luns_in_chnl;
211 }
212
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200213 dev_map->num_ch = num_ch;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100214
215 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
216 if (!tgt_dev)
217 goto err_ch;
218
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200219 /* Inherit device geometry from parent */
Matias Bjørlingade69e22017-01-31 13:17:09 +0100220 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200221
Matias Bjørlingade69e22017-01-31 13:17:09 +0100222 /* Target device only owns a portion of the physical device */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200223 tgt_dev->geo.num_ch = num_ch;
224 tgt_dev->geo.num_lun = (lun_balanced) ? prev_num_lun : -1;
225 tgt_dev->geo.all_luns = num_lun;
226 tgt_dev->geo.all_chunks = num_lun * dev->geo.num_chk;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200227
Javier Gonzáleze5392732018-01-05 14:16:14 +0100228 tgt_dev->geo.op = op;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200229
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200230 sec_per_lun = dev->geo.clba * dev->geo.num_chk;
231 tgt_dev->geo.total_secs = num_lun * sec_per_lun;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200232
Matias Bjørlingade69e22017-01-31 13:17:09 +0100233 tgt_dev->q = dev->q;
234 tgt_dev->map = dev_map;
235 tgt_dev->luns = luns;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100236 tgt_dev->parent = dev;
237
238 return tgt_dev;
239err_ch:
Javier González507f7d62017-05-03 11:19:05 +0200240 while (--i >= 0)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100241 kfree(dev_map->chnls[i].lun_offs);
242 kfree(luns);
243err_luns:
244 kfree(dev_map->chnls);
245err_chnls:
246 kfree(dev_map);
247err_dev:
248 return tgt_dev;
249}
250
251static const struct block_device_operations nvm_fops = {
252 .owner = THIS_MODULE,
253};
254
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100255static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200256{
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100257 struct nvm_tgt_type *tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200258
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100259 list_for_each_entry(tt, &nvm_tgt_types, list)
260 if (!strcmp(name, tt->name))
261 return tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200262
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100263 return NULL;
264}
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200265
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100266static struct nvm_tgt_type *nvm_find_target_type(const char *name)
267{
268 struct nvm_tgt_type *tt;
269
270 down_write(&nvm_tgtt_lock);
271 tt = __nvm_find_target_type(name);
272 up_write(&nvm_tgtt_lock);
273
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200274 return tt;
275}
276
Javier Gonzáleze5392732018-01-05 14:16:14 +0100277static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin,
278 int lun_end)
279{
280 if (lun_begin > lun_end || lun_end >= geo->all_luns) {
281 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
282 lun_begin, lun_end, geo->all_luns - 1);
283 return -EINVAL;
284 }
285
286 return 0;
287}
288
289static int __nvm_config_simple(struct nvm_dev *dev,
290 struct nvm_ioctl_create_simple *s)
291{
292 struct nvm_geo *geo = &dev->geo;
293
294 if (s->lun_begin == -1 && s->lun_end == -1) {
295 s->lun_begin = 0;
296 s->lun_end = geo->all_luns - 1;
297 }
298
299 return nvm_config_check_luns(geo, s->lun_begin, s->lun_end);
300}
301
302static int __nvm_config_extended(struct nvm_dev *dev,
303 struct nvm_ioctl_create_extended *e)
304{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100305 if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) {
306 e->lun_begin = 0;
307 e->lun_end = dev->geo.all_luns - 1;
308 }
309
310 /* op not set falls into target's default */
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200311 if (e->op == 0xFFFF) {
Javier Gonzáleze5392732018-01-05 14:16:14 +0100312 e->op = NVM_TARGET_DEFAULT_OP;
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200313 } else if (e->op < NVM_TARGET_MIN_OP || e->op > NVM_TARGET_MAX_OP) {
Javier Gonzáleze5392732018-01-05 14:16:14 +0100314 pr_err("nvm: invalid over provisioning value\n");
315 return -EINVAL;
316 }
317
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200318 return nvm_config_check_luns(&dev->geo, e->lun_begin, e->lun_end);
Javier Gonzáleze5392732018-01-05 14:16:14 +0100319}
320
Matias Bjørlingade69e22017-01-31 13:17:09 +0100321static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
322{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100323 struct nvm_ioctl_create_extended e;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100324 struct request_queue *tqueue;
325 struct gendisk *tdisk;
326 struct nvm_tgt_type *tt;
327 struct nvm_target *t;
328 struct nvm_tgt_dev *tgt_dev;
329 void *targetdata;
Igor Konopkoa14669e2019-05-04 20:37:55 +0200330 unsigned int mdts;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300331 int ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100332
Javier Gonzáleze5392732018-01-05 14:16:14 +0100333 switch (create->conf.type) {
334 case NVM_CONFIG_TYPE_SIMPLE:
335 ret = __nvm_config_simple(dev, &create->conf.s);
336 if (ret)
337 return ret;
338
339 e.lun_begin = create->conf.s.lun_begin;
340 e.lun_end = create->conf.s.lun_end;
341 e.op = NVM_TARGET_DEFAULT_OP;
342 break;
343 case NVM_CONFIG_TYPE_EXTENDED:
344 ret = __nvm_config_extended(dev, &create->conf.e);
345 if (ret)
346 return ret;
347
348 e = create->conf.e;
349 break;
350 default:
351 pr_err("nvm: config type not valid\n");
352 return -EINVAL;
353 }
354
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100355 tt = nvm_find_target_type(create->tgttype);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100356 if (!tt) {
357 pr_err("nvm: target type %s not found\n", create->tgttype);
358 return -EINVAL;
359 }
360
Matias Bjørling656e33c2018-10-09 13:11:34 +0200361 if ((tt->flags & NVM_TGT_F_HOST_L2P) != (dev->geo.dom & NVM_RSP_L2P)) {
362 pr_err("nvm: device is incompatible with target L2P type.\n");
363 return -EINVAL;
364 }
365
Javier Gonzálezbd77b232018-01-05 14:16:05 +0100366 if (nvm_target_exists(create->tgtname)) {
367 pr_err("nvm: target name already exists (%s)\n",
368 create->tgtname);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100369 return -EINVAL;
370 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100371
Javier Gonzáleze5392732018-01-05 14:16:14 +0100372 ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end);
Rakesh Pandit12e9a6d62017-06-27 14:55:33 +0300373 if (ret)
374 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100375
376 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300377 if (!t) {
378 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100379 goto err_reserve;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300380 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100381
Javier Gonzáleze5392732018-01-05 14:16:14 +0100382 tgt_dev = nvm_create_tgt_dev(dev, e.lun_begin, e.lun_end, e.op);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100383 if (!tgt_dev) {
384 pr_err("nvm: could not create target device\n");
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300385 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100386 goto err_t;
387 }
388
Matias Bjørlingade69e22017-01-31 13:17:09 +0100389 tdisk = alloc_disk(0);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300390 if (!tdisk) {
391 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200392 goto err_dev;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300393 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200394
Christoph Hellwig6d469642018-11-14 17:02:18 +0100395 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300396 if (!tqueue) {
397 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200398 goto err_disk;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300399 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200400 blk_queue_make_request(tqueue, tt->make_rq);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100401
Javier González6eb08242017-04-15 20:55:49 +0200402 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100403 tdisk->flags = GENHD_FL_EXT_DEVT;
404 tdisk->major = 0;
405 tdisk->first_minor = 0;
406 tdisk->fops = &nvm_fops;
407 tdisk->queue = tqueue;
408
Javier González4af3f752017-04-15 20:55:45 +0200409 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300410 if (IS_ERR(targetdata)) {
411 ret = PTR_ERR(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100412 goto err_init;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300413 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100414
415 tdisk->private_data = targetdata;
416 tqueue->queuedata = targetdata;
417
Igor Konopkoa14669e2019-05-04 20:37:55 +0200418 mdts = (dev->geo.csecs >> 9) * NVM_MAX_VLBA;
419 if (dev->geo.mdts) {
420 mdts = min_t(u32, dev->geo.mdts,
421 (dev->geo.csecs >> 9) * NVM_MAX_VLBA);
422 }
423 blk_queue_max_hw_sectors(tqueue, mdts);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100424
425 set_capacity(tdisk, tt->capacity(targetdata));
426 add_disk(tdisk);
427
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300428 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
429 ret = -ENOMEM;
Javier González9a69b0e2017-01-31 13:17:20 +0100430 goto err_sysfs;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300431 }
Javier González9a69b0e2017-01-31 13:17:20 +0100432
Matias Bjørlingade69e22017-01-31 13:17:09 +0100433 t->type = tt;
434 t->disk = tdisk;
435 t->dev = tgt_dev;
436
437 mutex_lock(&dev->mlock);
438 list_add_tail(&t->list, &dev->targets);
439 mutex_unlock(&dev->mlock);
440
Rakesh Pandit90014822017-10-13 14:45:50 +0200441 __module_get(tt->owner);
442
Matias Bjørlingade69e22017-01-31 13:17:09 +0100443 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100444err_sysfs:
445 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200446 tt->exit(targetdata, true);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100447err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100448 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600449 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200450err_disk:
451 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100452err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200453 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100454err_t:
455 kfree(t);
456err_reserve:
Javier Gonzáleze5392732018-01-05 14:16:14 +0100457 nvm_release_luns_err(dev, e.lun_begin, e.lun_end);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300458 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100459}
460
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200461static void __nvm_remove_target(struct nvm_target *t, bool graceful)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100462{
463 struct nvm_tgt_type *tt = t->type;
464 struct gendisk *tdisk = t->disk;
465 struct request_queue *q = tdisk->queue;
466
467 del_gendisk(tdisk);
468 blk_cleanup_queue(q);
469
Javier González9a69b0e2017-01-31 13:17:20 +0100470 if (tt->sysfs_exit)
471 tt->sysfs_exit(tdisk);
472
Matias Bjørlingade69e22017-01-31 13:17:09 +0100473 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200474 tt->exit(tdisk->private_data, graceful);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100475
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200476 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100477 put_disk(tdisk);
Rakesh Pandit90014822017-10-13 14:45:50 +0200478 module_put(t->type->owner);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100479
480 list_del(&t->list);
481 kfree(t);
482}
483
484/**
485 * nvm_remove_tgt - Removes a target from the media manager
486 * @dev: device
487 * @remove: ioctl structure with target name to remove.
488 *
489 * Returns:
490 * 0: on success
491 * 1: on not found
492 * <0: on error
493 */
494static int nvm_remove_tgt(struct nvm_dev *dev, struct nvm_ioctl_remove *remove)
495{
496 struct nvm_target *t;
497
498 mutex_lock(&dev->mlock);
499 t = nvm_find_target(dev, remove->tgtname);
500 if (!t) {
501 mutex_unlock(&dev->mlock);
502 return 1;
503 }
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200504 __nvm_remove_target(t, true);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100505 mutex_unlock(&dev->mlock);
Igor Konopkoe69397e2019-05-04 20:38:08 +0200506 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100507
508 return 0;
509}
510
511static int nvm_register_map(struct nvm_dev *dev)
512{
513 struct nvm_dev_map *rmap;
514 int i, j;
515
516 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
517 if (!rmap)
518 goto err_rmap;
519
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200520 rmap->chnls = kcalloc(dev->geo.num_ch, sizeof(struct nvm_ch_map),
Matias Bjørlingade69e22017-01-31 13:17:09 +0100521 GFP_KERNEL);
522 if (!rmap->chnls)
523 goto err_chnls;
524
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200525 for (i = 0; i < dev->geo.num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100526 struct nvm_ch_map *ch_rmap;
527 int *lun_roffs;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200528 int luns_in_chnl = dev->geo.num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100529
530 ch_rmap = &rmap->chnls[i];
531
532 ch_rmap->ch_off = -1;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200533 ch_rmap->num_lun = luns_in_chnl;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100534
535 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
536 if (!lun_roffs)
537 goto err_ch;
538
539 for (j = 0; j < luns_in_chnl; j++)
540 lun_roffs[j] = -1;
541
542 ch_rmap->lun_offs = lun_roffs;
543 }
544
545 dev->rmap = rmap;
546
547 return 0;
548err_ch:
549 while (--i >= 0)
550 kfree(rmap->chnls[i].lun_offs);
551err_chnls:
552 kfree(rmap);
553err_rmap:
554 return -ENOMEM;
555}
556
Javier González7a3de2b2017-04-15 20:55:39 +0200557static void nvm_unregister_map(struct nvm_dev *dev)
558{
559 struct nvm_dev_map *rmap = dev->rmap;
560 int i;
561
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200562 for (i = 0; i < dev->geo.num_ch; i++)
Javier González7a3de2b2017-04-15 20:55:39 +0200563 kfree(rmap->chnls[i].lun_offs);
564
565 kfree(rmap->chnls);
566 kfree(rmap);
567}
568
Matias Bjørling61a561d2017-01-31 13:17:13 +0100569static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100570{
571 struct nvm_dev_map *dev_map = tgt_dev->map;
Javier González69471512018-03-30 00:05:15 +0200572 struct nvm_ch_map *ch_map = &dev_map->chnls[p->a.ch];
573 int lun_off = ch_map->lun_offs[p->a.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100574
Javier González69471512018-03-30 00:05:15 +0200575 p->a.ch += ch_map->ch_off;
576 p->a.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100577}
578
Matias Bjørling61a561d2017-01-31 13:17:13 +0100579static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100580{
581 struct nvm_dev *dev = tgt_dev->parent;
582 struct nvm_dev_map *dev_rmap = dev->rmap;
Javier González69471512018-03-30 00:05:15 +0200583 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->a.ch];
584 int lun_roff = ch_rmap->lun_offs[p->a.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100585
Javier González69471512018-03-30 00:05:15 +0200586 p->a.ch -= ch_rmap->ch_off;
587 p->a.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100588}
589
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100590static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
591 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100592{
593 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100594
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100595 for (i = 0; i < nr_ppas; i++) {
596 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
Javier González7100d502018-03-30 00:05:16 +0200597 ppa_list[i] = generic_to_dev_addr(tgt_dev->parent, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100598 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100599}
600
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100601static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
602 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100603{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100604 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100605
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100606 for (i = 0; i < nr_ppas; i++) {
Javier González7100d502018-03-30 00:05:16 +0200607 ppa_list[i] = dev_to_generic_addr(tgt_dev->parent, ppa_list[i]);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100608 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
609 }
610}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100611
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100612static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
613{
Hans Holmbergd68a9342018-10-09 13:11:46 +0200614 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100615
Hans Holmbergd68a9342018-10-09 13:11:46 +0200616 nvm_ppa_tgt_to_dev(tgt_dev, ppa_list, rqd->nr_ppas);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100617}
618
619static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
620{
Hans Holmbergd68a9342018-10-09 13:11:46 +0200621 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100622
Hans Holmbergd68a9342018-10-09 13:11:46 +0200623 nvm_ppa_dev_to_tgt(tgt_dev, ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100624}
625
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200626int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100627{
628 int ret = 0;
629
Matias Bjørling5cd90782016-07-07 09:54:17 +0200630 down_write(&nvm_tgtt_lock);
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100631 if (__nvm_find_target_type(tt->name))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100632 ret = -EEXIST;
633 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200634 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200635 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100636
637 return ret;
638}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200639EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100640
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200641void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100642{
643 if (!tt)
644 return;
645
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200646 down_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100647 list_del(&tt->list);
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200648 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100649}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200650EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100651
652void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
653 dma_addr_t *dma_handler)
654{
Javier González75b85642016-05-06 20:03:13 +0200655 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100656 dma_handler);
657}
658EXPORT_SYMBOL(nvm_dev_dma_alloc);
659
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100660void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100661{
Javier González75b85642016-05-06 20:03:13 +0200662 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100663}
664EXPORT_SYMBOL(nvm_dev_dma_free);
665
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100666static struct nvm_dev *nvm_find_nvm_dev(const char *name)
667{
668 struct nvm_dev *dev;
669
670 list_for_each_entry(dev, &nvm_devices, devices)
671 if (!strcmp(name, dev->name))
672 return dev;
673
674 return NULL;
675}
676
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200677static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
678 const struct ppa_addr *ppas, int nr_ppas)
679{
680 struct nvm_dev *dev = tgt_dev->parent;
681 struct nvm_geo *geo = &tgt_dev->geo;
682 int i, plane_cnt, pl_idx;
683 struct ppa_addr ppa;
684
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200685 if (geo->pln_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200686 rqd->nr_ppas = nr_ppas;
687 rqd->ppa_addr = ppas[0];
688
689 return 0;
690 }
691
692 rqd->nr_ppas = nr_ppas;
693 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
694 if (!rqd->ppa_list) {
695 pr_err("nvm: failed to allocate dma memory\n");
696 return -ENOMEM;
697 }
698
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200699 plane_cnt = geo->pln_mode;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200700 rqd->nr_ppas *= plane_cnt;
701
702 for (i = 0; i < nr_ppas; i++) {
703 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
704 ppa = ppas[i];
705 ppa.g.pl = pl_idx;
706 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
707 }
708 }
709
710 return 0;
711}
712
713static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
714 struct nvm_rq *rqd)
715{
716 if (!rqd->ppa_list)
717 return;
718
719 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
720}
721
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200722static int nvm_set_flags(struct nvm_geo *geo, struct nvm_rq *rqd)
723{
724 int flags = 0;
725
726 if (geo->version == NVM_OCSSD_SPEC_20)
727 return 0;
728
729 if (rqd->is_seq)
730 flags |= geo->pln_mode >> 1;
731
732 if (rqd->opcode == NVM_OP_PREAD)
733 flags |= (NVM_IO_SCRAMBLE_ENABLE | NVM_IO_SUSPEND);
734 else if (rqd->opcode == NVM_OP_PWRITE)
735 flags |= NVM_IO_SCRAMBLE_ENABLE;
736
737 return flags;
738}
739
Javier González8e536242016-11-28 22:39:10 +0100740int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100741{
Javier González8e536242016-11-28 22:39:10 +0100742 struct nvm_dev *dev = tgt_dev->parent;
Javier González3e505af2017-06-26 11:57:10 +0200743 int ret;
Javier González8e536242016-11-28 22:39:10 +0100744
Matias Bjørlingade69e22017-01-31 13:17:09 +0100745 if (!dev->ops->submit_io)
746 return -ENODEV;
747
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100748 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100749
750 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200751 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González3e505af2017-06-26 11:57:10 +0200752
753 /* In case of error, fail with right address format */
754 ret = dev->ops->submit_io(dev, rqd);
755 if (ret)
756 nvm_rq_dev_to_tgt(tgt_dev, rqd);
757 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100758}
759EXPORT_SYMBOL(nvm_submit_io);
760
Javier González1a94b2d2017-10-13 14:46:47 +0200761int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100762{
Javier González1a94b2d2017-10-13 14:46:47 +0200763 struct nvm_dev *dev = tgt_dev->parent;
764 int ret;
Javier González17912c42017-04-15 20:55:37 +0200765
Javier González1a94b2d2017-10-13 14:46:47 +0200766 if (!dev->ops->submit_io_sync)
767 return -ENODEV;
768
769 nvm_rq_tgt_to_dev(tgt_dev, rqd);
770
771 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200772 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200773
774 /* In case of error, fail with right address format */
775 ret = dev->ops->submit_io_sync(dev, rqd);
776 nvm_rq_dev_to_tgt(tgt_dev, rqd);
777
778 return ret;
Javier González17912c42017-04-15 20:55:37 +0200779}
Javier González1a94b2d2017-10-13 14:46:47 +0200780EXPORT_SYMBOL(nvm_submit_io_sync);
Javier González17912c42017-04-15 20:55:37 +0200781
Matias Bjørling06894ef2017-01-31 13:17:17 +0100782void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100783{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100784 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100785
786 /* Convert address space */
787 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100788 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100789
Matias Bjørling06894ef2017-01-31 13:17:17 +0100790 if (rqd->end_io)
791 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100792}
793EXPORT_SYMBOL(nvm_end_io);
794
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200795static int nvm_submit_io_sync_raw(struct nvm_dev *dev, struct nvm_rq *rqd)
796{
797 if (!dev->ops->submit_io_sync)
798 return -ENODEV;
799
800 rqd->flags = nvm_set_flags(&dev->geo, rqd);
801
802 return dev->ops->submit_io_sync(dev, rqd);
803}
804
805static int nvm_bb_chunk_sense(struct nvm_dev *dev, struct ppa_addr ppa)
806{
807 struct nvm_rq rqd = { NULL };
808 struct bio bio;
809 struct bio_vec bio_vec;
810 struct page *page;
811 int ret;
812
813 page = alloc_page(GFP_KERNEL);
814 if (!page)
815 return -ENOMEM;
816
817 bio_init(&bio, &bio_vec, 1);
818 bio_add_page(&bio, page, PAGE_SIZE, 0);
819 bio_set_op_attrs(&bio, REQ_OP_READ, 0);
820
821 rqd.bio = &bio;
822 rqd.opcode = NVM_OP_PREAD;
823 rqd.is_seq = 1;
824 rqd.nr_ppas = 1;
825 rqd.ppa_addr = generic_to_dev_addr(dev, ppa);
826
827 ret = nvm_submit_io_sync_raw(dev, &rqd);
828 if (ret)
829 return ret;
830
831 __free_page(page);
832
833 return rqd.error;
834}
835
Matias Bjørling22e8c972016-05-06 20:02:58 +0200836/*
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200837 * Scans a 1.2 chunk first and last page to determine if its state.
838 * If the chunk is found to be open, also scan it to update the write
839 * pointer.
Matias Bjørling22e8c972016-05-06 20:02:58 +0200840 */
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200841static int nvm_bb_chunk_scan(struct nvm_dev *dev, struct ppa_addr ppa,
842 struct nvm_chk_meta *meta)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200843{
Javier González8e79b5c2016-11-28 22:39:06 +0100844 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200845 int ret, pg, pl;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200846
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200847 /* sense first page */
848 ret = nvm_bb_chunk_sense(dev, ppa);
849 if (ret < 0) /* io error */
850 return ret;
851 else if (ret == 0) /* valid data */
852 meta->state = NVM_CHK_ST_OPEN;
853 else if (ret > 0) {
854 /*
855 * If empty page, the chunk is free, else it is an
856 * actual io error. In that case, mark it offline.
857 */
858 switch (ret) {
859 case NVM_RSP_ERR_EMPTYPAGE:
860 meta->state = NVM_CHK_ST_FREE;
861 return 0;
862 case NVM_RSP_ERR_FAILCRC:
863 case NVM_RSP_ERR_FAILECC:
864 case NVM_RSP_WARN_HIGHECC:
865 meta->state = NVM_CHK_ST_OPEN;
866 goto scan;
867 default:
868 return -ret; /* other io error */
869 }
870 }
871
872 /* sense last page */
873 ppa.g.pg = geo->num_pg - 1;
874 ppa.g.pl = geo->num_pln - 1;
875
876 ret = nvm_bb_chunk_sense(dev, ppa);
877 if (ret < 0) /* io error */
878 return ret;
879 else if (ret == 0) { /* Chunk fully written */
880 meta->state = NVM_CHK_ST_CLOSED;
881 meta->wp = geo->clba;
882 return 0;
883 } else if (ret > 0) {
884 switch (ret) {
885 case NVM_RSP_ERR_EMPTYPAGE:
886 case NVM_RSP_ERR_FAILCRC:
887 case NVM_RSP_ERR_FAILECC:
888 case NVM_RSP_WARN_HIGHECC:
889 meta->state = NVM_CHK_ST_OPEN;
890 break;
891 default:
892 return -ret; /* other io error */
893 }
894 }
895
896scan:
897 /*
898 * chunk is open, we scan sequentially to update the write pointer.
899 * We make the assumption that targets write data across all planes
900 * before moving to the next page.
901 */
902 for (pg = 0; pg < geo->num_pg; pg++) {
903 for (pl = 0; pl < geo->num_pln; pl++) {
904 ppa.g.pg = pg;
905 ppa.g.pl = pl;
906
907 ret = nvm_bb_chunk_sense(dev, ppa);
908 if (ret < 0) /* io error */
909 return ret;
910 else if (ret == 0) {
911 meta->wp += geo->ws_min;
912 } else if (ret > 0) {
913 switch (ret) {
914 case NVM_RSP_ERR_EMPTYPAGE:
915 return 0;
916 case NVM_RSP_ERR_FAILCRC:
917 case NVM_RSP_ERR_FAILECC:
918 case NVM_RSP_WARN_HIGHECC:
919 meta->wp += geo->ws_min;
920 break;
921 default:
922 return -ret; /* other io error */
923 }
924 }
925 }
926 }
927
928 return 0;
929}
930
931/*
932 * folds a bad block list from its plane representation to its
933 * chunk representation.
934 *
935 * If any of the planes status are bad or grown bad, the chunk is marked
936 * offline. If not bad, the first plane state acts as the chunk state.
937 */
938static int nvm_bb_to_chunk(struct nvm_dev *dev, struct ppa_addr ppa,
939 u8 *blks, int nr_blks, struct nvm_chk_meta *meta)
940{
941 struct nvm_geo *geo = &dev->geo;
942 int ret, blk, pl, offset, blktype;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200943
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200944 for (blk = 0; blk < geo->num_chk; blk++) {
945 offset = blk * geo->pln_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200946 blktype = blks[offset];
947
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200948 for (pl = 0; pl < geo->pln_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200949 if (blks[offset + pl] &
950 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
951 blktype = blks[offset + pl];
952 break;
953 }
954 }
955
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200956 ppa.g.blk = blk;
957
958 meta->wp = 0;
959 meta->type = NVM_CHK_TP_W_SEQ;
960 meta->wi = 0;
961 meta->slba = generic_to_dev_addr(dev, ppa).ppa;
962 meta->cnlb = dev->geo.clba;
963
964 if (blktype == NVM_BLK_T_FREE) {
965 ret = nvm_bb_chunk_scan(dev, ppa, meta);
966 if (ret)
967 return ret;
968 } else {
969 meta->state = NVM_CHK_ST_OFFLINE;
970 }
971
972 meta++;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200973 }
974
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200975 return 0;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200976}
Matias Bjørling22e8c972016-05-06 20:02:58 +0200977
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200978static int nvm_get_bb_meta(struct nvm_dev *dev, sector_t slba,
979 int nchks, struct nvm_chk_meta *meta)
980{
981 struct nvm_geo *geo = &dev->geo;
982 struct ppa_addr ppa;
983 u8 *blks;
984 int ch, lun, nr_blks;
Geert Uytterhoeven55e58c52018-12-11 20:16:08 +0100985 int ret = 0;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200986
987 ppa.ppa = slba;
988 ppa = dev_to_generic_addr(dev, ppa);
989
990 if (ppa.g.blk != 0)
991 return -EINVAL;
992
993 if ((nchks % geo->num_chk) != 0)
994 return -EINVAL;
995
996 nr_blks = geo->num_chk * geo->pln_mode;
997
998 blks = kmalloc(nr_blks, GFP_KERNEL);
999 if (!blks)
1000 return -ENOMEM;
1001
1002 for (ch = ppa.g.ch; ch < geo->num_ch; ch++) {
1003 for (lun = ppa.g.lun; lun < geo->num_lun; lun++) {
1004 struct ppa_addr ppa_gen, ppa_dev;
1005
1006 if (!nchks)
1007 goto done;
1008
1009 ppa_gen.ppa = 0;
1010 ppa_gen.g.ch = ch;
1011 ppa_gen.g.lun = lun;
1012 ppa_dev = generic_to_dev_addr(dev, ppa_gen);
1013
1014 ret = dev->ops->get_bb_tbl(dev, ppa_dev, blks);
1015 if (ret)
1016 goto done;
1017
1018 ret = nvm_bb_to_chunk(dev, ppa_gen, blks, nr_blks,
1019 meta);
1020 if (ret)
1021 goto done;
1022
1023 meta += geo->num_chk;
1024 nchks -= geo->num_chk;
1025 }
1026 }
1027done:
1028 kfree(blks);
1029 return ret;
1030}
1031
1032int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
1033 int nchks, struct nvm_chk_meta *meta)
Javier González333ba052016-11-28 22:39:14 +01001034{
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001035 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +01001036
Matias Bjørlingdab8ee92017-01-31 13:17:14 +01001037 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001038
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001039 if (dev->geo.version == NVM_OCSSD_SPEC_12)
1040 return nvm_get_bb_meta(dev, (sector_t)ppa.ppa, nchks, meta);
1041
1042 return dev->ops->get_chk_meta(dev, (sector_t)ppa.ppa, nchks, meta);
Javier González333ba052016-11-28 22:39:14 +01001043}
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001044EXPORT_SYMBOL_GPL(nvm_get_chunk_meta);
1045
1046int nvm_set_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
1047 int nr_ppas, int type)
1048{
1049 struct nvm_dev *dev = tgt_dev->parent;
1050 struct nvm_rq rqd;
1051 int ret;
1052
1053 if (dev->geo.version == NVM_OCSSD_SPEC_20)
1054 return 0;
1055
1056 if (nr_ppas > NVM_MAX_VLBA) {
1057 pr_err("nvm: unable to update all blocks atomically\n");
1058 return -EINVAL;
1059 }
1060
1061 memset(&rqd, 0, sizeof(struct nvm_rq));
1062
1063 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
1064 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
1065
1066 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
1067 nvm_free_rqd_ppalist(tgt_dev, &rqd);
1068 if (ret)
1069 return -EINVAL;
1070
1071 return 0;
1072}
1073EXPORT_SYMBOL_GPL(nvm_set_chunk_meta);
Javier González333ba052016-11-28 22:39:14 +01001074
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001075static int nvm_core_init(struct nvm_dev *dev)
1076{
Javier González8e79b5c2016-11-28 22:39:06 +01001077 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001078 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001079
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001080 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +01001081 sizeof(unsigned long), GFP_KERNEL);
1082 if (!dev->lun_map)
1083 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001084
Matias Bjørlingade69e22017-01-31 13:17:09 +01001085 INIT_LIST_HEAD(&dev->area_list);
1086 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +01001087 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001088 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001089
Matias Bjørlingade69e22017-01-31 13:17:09 +01001090 ret = nvm_register_map(dev);
1091 if (ret)
1092 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001093
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001094 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001095err_fmtype:
1096 kfree(dev->lun_map);
1097 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001098}
1099
Igor Konopkoe69397e2019-05-04 20:38:08 +02001100static void nvm_free(struct kref *ref)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001101{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001102 struct nvm_dev *dev = container_of(ref, struct nvm_dev, ref);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001103
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001104 if (dev->dma_pool)
1105 dev->ops->destroy_dma_pool(dev->dma_pool);
1106
Igor Konopkoe69397e2019-05-04 20:38:08 +02001107 if (dev->rmap)
1108 nvm_unregister_map(dev);
1109
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001110 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001111 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001112}
1113
1114static int nvm_init(struct nvm_dev *dev)
1115{
Javier González8e79b5c2016-11-28 22:39:06 +01001116 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001117 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001118
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001119 if (dev->ops->identity(dev)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001120 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001121 goto err;
1122 }
1123
Javier González3cb98f82018-03-30 00:05:11 +02001124 pr_debug("nvm: ver:%u.%u nvm_vendor:%x\n",
1125 geo->major_ver_id, geo->minor_ver_id,
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001126 geo->vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001127
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001128 ret = nvm_core_init(dev);
1129 if (ret) {
1130 pr_err("nvm: could not initialize core structures.\n");
1131 goto err;
1132 }
1133
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001134 pr_info("nvm: registered %s [%u/%u/%u/%u/%u]\n",
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001135 dev->name, dev->geo.ws_min, dev->geo.ws_opt,
1136 dev->geo.num_chk, dev->geo.all_luns,
1137 dev->geo.num_ch);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001138 return 0;
1139err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001140 pr_err("nvm: failed to initialize nvm\n");
1141 return ret;
1142}
1143
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001144struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001145{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001146 struct nvm_dev *dev;
1147
1148 dev = kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1149 if (dev)
1150 kref_init(&dev->ref);
1151
1152 return dev;
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001153}
1154EXPORT_SYMBOL(nvm_alloc_dev);
1155
1156int nvm_register(struct nvm_dev *dev)
1157{
Igor Konopko24828d02018-12-11 20:16:24 +01001158 int ret, exp_pool_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001159
Igor Konopkoe69397e2019-05-04 20:38:08 +02001160 if (!dev->q || !dev->ops) {
1161 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001162 return -EINVAL;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001163 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001164
Matias Bjørling85136c02018-12-11 20:16:20 +01001165 ret = nvm_init(dev);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001166 if (ret) {
1167 kref_put(&dev->ref, nvm_free);
Matias Bjørling85136c02018-12-11 20:16:20 +01001168 return ret;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001169 }
Matias Bjørling85136c02018-12-11 20:16:20 +01001170
Igor Konopko24828d02018-12-11 20:16:24 +01001171 exp_pool_size = max_t(int, PAGE_SIZE,
1172 (NVM_MAX_VLBA * (sizeof(u64) + dev->geo.sos)));
1173 exp_pool_size = round_up(exp_pool_size, PAGE_SIZE);
1174
1175 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist",
1176 exp_pool_size);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001177 if (!dev->dma_pool) {
1178 pr_err("nvm: could not create dma pool\n");
Igor Konopkoe69397e2019-05-04 20:38:08 +02001179 kref_put(&dev->ref, nvm_free);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001180 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001181 }
1182
Matias Bjørling762796b2015-12-06 11:25:49 +01001183 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001184 down_write(&nvm_lock);
1185 list_add(&dev->devices, &nvm_devices);
1186 up_write(&nvm_lock);
1187
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001188 return 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001189}
1190EXPORT_SYMBOL(nvm_register);
1191
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001192void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001193{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001194 struct nvm_target *t, *tmp;
1195
1196 mutex_lock(&dev->mlock);
1197 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1198 if (t->dev->parent != dev)
1199 continue;
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001200 __nvm_remove_target(t, false);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001201 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001202 }
1203 mutex_unlock(&dev->mlock);
1204
Wenwei Taod0a712c2015-11-28 16:49:28 +01001205 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001206 list_del(&dev->devices);
1207 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001208
Igor Konopkoe69397e2019-05-04 20:38:08 +02001209 kref_put(&dev->ref, nvm_free);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001210}
1211EXPORT_SYMBOL(nvm_unregister);
1212
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001213static int __nvm_configure_create(struct nvm_ioctl_create *create)
1214{
1215 struct nvm_dev *dev;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001216 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001217
Wenwei Taod0a712c2015-11-28 16:49:28 +01001218 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001219 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001220 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001221
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001222 if (!dev) {
1223 pr_err("nvm: device not found\n");
1224 return -EINVAL;
1225 }
1226
Igor Konopkoe69397e2019-05-04 20:38:08 +02001227 kref_get(&dev->ref);
1228 ret = nvm_create_tgt(dev, create);
1229 if (ret)
1230 kref_put(&dev->ref, nvm_free);
1231
1232 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001233}
1234
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001235static long nvm_ioctl_info(struct file *file, void __user *arg)
1236{
1237 struct nvm_ioctl_info *info;
1238 struct nvm_tgt_type *tt;
1239 int tgt_iter = 0;
1240
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001241 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1242 if (IS_ERR(info))
1243 return -EFAULT;
1244
1245 info->version[0] = NVM_VERSION_MAJOR;
1246 info->version[1] = NVM_VERSION_MINOR;
1247 info->version[2] = NVM_VERSION_PATCH;
1248
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001249 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001250 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001251 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1252
1253 tgt->version[0] = tt->version[0];
1254 tgt->version[1] = tt->version[1];
1255 tgt->version[2] = tt->version[2];
1256 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1257
1258 tgt_iter++;
1259 }
1260
1261 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001262 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001263
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001264 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1265 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001266 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001267 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001268
1269 kfree(info);
1270 return 0;
1271}
1272
1273static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1274{
1275 struct nvm_ioctl_get_devices *devices;
1276 struct nvm_dev *dev;
1277 int i = 0;
1278
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001279 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1280 if (!devices)
1281 return -ENOMEM;
1282
1283 down_write(&nvm_lock);
1284 list_for_each_entry(dev, &nvm_devices, devices) {
1285 struct nvm_ioctl_device_info *info = &devices->info[i];
1286
Javier González6eb08242017-04-15 20:55:49 +02001287 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001288
Matias Bjørlingade69e22017-01-31 13:17:09 +01001289 /* kept for compatibility */
1290 info->bmversion[0] = 1;
1291 info->bmversion[1] = 0;
1292 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001293 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001294 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001295
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001296 if (i > 31) {
1297 pr_err("nvm: max 31 devices can be reported.\n");
1298 break;
1299 }
1300 }
1301 up_write(&nvm_lock);
1302
1303 devices->nr_devices = i;
1304
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001305 if (copy_to_user(arg, devices,
1306 sizeof(struct nvm_ioctl_get_devices))) {
1307 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001308 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001309 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001310
1311 kfree(devices);
1312 return 0;
1313}
1314
1315static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1316{
1317 struct nvm_ioctl_create create;
1318
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001319 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1320 return -EFAULT;
1321
Javier Gonzáleze5392732018-01-05 14:16:14 +01001322 if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED &&
1323 create.conf.e.rsv != 0) {
1324 pr_err("nvm: reserved config field in use\n");
1325 return -EINVAL;
1326 }
1327
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001328 create.dev[DISK_NAME_LEN - 1] = '\0';
1329 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1330 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1331
1332 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001333 __u32 flags = create.flags;
1334
1335 /* Check for valid flags */
1336 if (flags & NVM_TARGET_FACTORY)
1337 flags &= ~NVM_TARGET_FACTORY;
1338
1339 if (flags) {
1340 pr_err("nvm: flag not supported\n");
1341 return -EINVAL;
1342 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001343 }
1344
1345 return __nvm_configure_create(&create);
1346}
1347
1348static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1349{
1350 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001351 struct nvm_dev *dev;
1352 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001353
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001354 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1355 return -EFAULT;
1356
1357 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1358
1359 if (remove.flags != 0) {
1360 pr_err("nvm: no flags supported\n");
1361 return -EINVAL;
1362 }
1363
Marcin Dziegielewskif41d4272019-05-04 20:37:58 +02001364 down_read(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001365 list_for_each_entry(dev, &nvm_devices, devices) {
Matias Bjørlingade69e22017-01-31 13:17:09 +01001366 ret = nvm_remove_tgt(dev, &remove);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001367 if (!ret)
1368 break;
1369 }
Marcin Dziegielewskif41d4272019-05-04 20:37:58 +02001370 up_read(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001371
1372 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001373}
1374
Matias Bjørlingade69e22017-01-31 13:17:09 +01001375/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001376static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1377{
1378 struct nvm_ioctl_dev_init init;
1379
Matias Bjørling55696152016-01-12 07:49:37 +01001380 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1381 return -EFAULT;
1382
1383 if (init.flags != 0) {
1384 pr_err("nvm: no flags supported\n");
1385 return -EINVAL;
1386 }
1387
Matias Bjørlingade69e22017-01-31 13:17:09 +01001388 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001389}
1390
Matias Bjørlingade69e22017-01-31 13:17:09 +01001391/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001392static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1393{
1394 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001395
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001396 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1397 return -EFAULT;
1398
1399 fact.dev[DISK_NAME_LEN - 1] = '\0';
1400
1401 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1402 return -EINVAL;
1403
Matias Bjørlingbf643182016-02-04 15:13:27 +01001404 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001405}
1406
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001407static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1408{
1409 void __user *argp = (void __user *)arg;
1410
Johannes Thumshirn40f962d2018-03-30 00:05:07 +02001411 if (!capable(CAP_SYS_ADMIN))
1412 return -EPERM;
1413
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001414 switch (cmd) {
1415 case NVM_INFO:
1416 return nvm_ioctl_info(file, argp);
1417 case NVM_GET_DEVICES:
1418 return nvm_ioctl_get_devices(file, argp);
1419 case NVM_DEV_CREATE:
1420 return nvm_ioctl_dev_create(file, argp);
1421 case NVM_DEV_REMOVE:
1422 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001423 case NVM_DEV_INIT:
1424 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001425 case NVM_DEV_FACTORY:
1426 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001427 }
1428 return 0;
1429}
1430
1431static const struct file_operations _ctl_fops = {
1432 .open = nonseekable_open,
1433 .unlocked_ioctl = nvm_ctl_ioctl,
1434 .owner = THIS_MODULE,
1435 .llseek = noop_llseek,
1436};
1437
1438static struct miscdevice _nvm_misc = {
1439 .minor = MISC_DYNAMIC_MINOR,
1440 .name = "lightnvm",
1441 .nodename = "lightnvm/control",
1442 .fops = &_ctl_fops,
1443};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001444builtin_misc_device(_nvm_misc);