blob: 7d555b110ecd6d8e4e5e6fa0d8a16a166c10adf2 [file] [log] [blame]
Thomas Gleixnera98c5b12019-06-01 10:08:25 +02001// SPDX-License-Identifier: GPL-2.0-only
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01002/*
3 * Copyright (C) 2015 IT University of Copenhagen. All rights reserved.
4 * Initial release: Matias Bjorling <m@bjorling.me>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01005 */
6
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01007#include <linux/list.h>
8#include <linux/types.h>
9#include <linux/sem.h>
10#include <linux/bitmap.h>
Rakesh Pandit90014822017-10-13 14:45:50 +020011#include <linux/module.h>
Paul Gortmaker389b2a12016-10-29 16:38:41 -040012#include <linux/moduleparam.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010013#include <linux/miscdevice.h>
14#include <linux/lightnvm.h>
Matias Bjørling912761622016-01-12 07:49:21 +010015#include <linux/sched/sysctl.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010016
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020017static LIST_HEAD(nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +020018static DECLARE_RWSEM(nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010019static LIST_HEAD(nvm_devices);
20static DECLARE_RWSEM(nvm_lock);
21
Matias Bjørlingade69e22017-01-31 13:17:09 +010022/* Map between virtual and physical channel and lun */
23struct nvm_ch_map {
24 int ch_off;
Javier Gonzáleza40afad2018-03-30 00:05:14 +020025 int num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +010026 int *lun_offs;
27};
28
29struct nvm_dev_map {
30 struct nvm_ch_map *chnls;
Javier Gonzáleza40afad2018-03-30 00:05:14 +020031 int num_ch;
Matias Bjørlingade69e22017-01-31 13:17:09 +010032};
33
Igor Konopkoe69397e2019-05-04 20:38:08 +020034static void nvm_free(struct kref *ref);
35
Matias Bjørlingade69e22017-01-31 13:17:09 +010036static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
37{
38 struct nvm_target *tgt;
39
40 list_for_each_entry(tgt, &dev->targets, list)
41 if (!strcmp(name, tgt->disk->disk_name))
42 return tgt;
43
44 return NULL;
45}
46
Javier Gonzálezbd77b232018-01-05 14:16:05 +010047static bool nvm_target_exists(const char *name)
48{
49 struct nvm_dev *dev;
50 struct nvm_target *tgt;
51 bool ret = false;
52
53 down_write(&nvm_lock);
54 list_for_each_entry(dev, &nvm_devices, devices) {
55 mutex_lock(&dev->mlock);
56 list_for_each_entry(tgt, &dev->targets, list) {
57 if (!strcmp(name, tgt->disk->disk_name)) {
58 ret = true;
59 mutex_unlock(&dev->mlock);
60 goto out;
61 }
62 }
63 mutex_unlock(&dev->mlock);
64 }
65
66out:
67 up_write(&nvm_lock);
68 return ret;
69}
70
Matias Bjørlingade69e22017-01-31 13:17:09 +010071static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
72{
73 int i;
74
75 for (i = lun_begin; i <= lun_end; i++) {
76 if (test_and_set_bit(i, dev->lun_map)) {
77 pr_err("nvm: lun %d already allocated\n", i);
78 goto err;
79 }
80 }
81
82 return 0;
83err:
Javier González507f7d62017-05-03 11:19:05 +020084 while (--i >= lun_begin)
Matias Bjørlingade69e22017-01-31 13:17:09 +010085 clear_bit(i, dev->lun_map);
86
87 return -EBUSY;
88}
89
90static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
91 int lun_end)
92{
93 int i;
94
95 for (i = lun_begin; i <= lun_end; i++)
96 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
97}
98
Javier Gonzálezedee1bd2017-04-15 20:55:42 +020099static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100100{
101 struct nvm_dev *dev = tgt_dev->parent;
102 struct nvm_dev_map *dev_map = tgt_dev->map;
103 int i, j;
104
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200105 for (i = 0; i < dev_map->num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100106 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
107 int *lun_offs = ch_map->lun_offs;
108 int ch = i + ch_map->ch_off;
109
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200110 if (clear) {
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200111 for (j = 0; j < ch_map->num_lun; j++) {
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200112 int lun = j + lun_offs[j];
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200113 int lunid = (ch * dev->geo.num_lun) + lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100114
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200115 WARN_ON(!test_and_clear_bit(lunid,
116 dev->lun_map));
117 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100118 }
119
120 kfree(ch_map->lun_offs);
121 }
122
123 kfree(dev_map->chnls);
124 kfree(dev_map);
125
126 kfree(tgt_dev->luns);
127 kfree(tgt_dev);
128}
129
130static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
Javier Gonzáleze5392732018-01-05 14:16:14 +0100131 u16 lun_begin, u16 lun_end,
132 u16 op)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100133{
134 struct nvm_tgt_dev *tgt_dev = NULL;
135 struct nvm_dev_map *dev_rmap = dev->rmap;
136 struct nvm_dev_map *dev_map;
137 struct ppa_addr *luns;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200138 int num_lun = lun_end - lun_begin + 1;
139 int luns_left = num_lun;
140 int num_ch = num_lun / dev->geo.num_lun;
141 int num_ch_mod = num_lun % dev->geo.num_lun;
142 int bch = lun_begin / dev->geo.num_lun;
143 int blun = lun_begin % dev->geo.num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100144 int lunid = 0;
145 int lun_balanced = 1;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200146 int sec_per_lun, prev_num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100147 int i, j;
148
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200149 num_ch = (num_ch_mod == 0) ? num_ch : num_ch + 1;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100150
151 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
152 if (!dev_map)
153 goto err_dev;
154
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200155 dev_map->chnls = kcalloc(num_ch, sizeof(struct nvm_ch_map), GFP_KERNEL);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100156 if (!dev_map->chnls)
157 goto err_chnls;
158
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200159 luns = kcalloc(num_lun, sizeof(struct ppa_addr), GFP_KERNEL);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100160 if (!luns)
161 goto err_luns;
162
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200163 prev_num_lun = (luns_left > dev->geo.num_lun) ?
164 dev->geo.num_lun : luns_left;
165 for (i = 0; i < num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100166 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
167 int *lun_roffs = ch_rmap->lun_offs;
168 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
169 int *lun_offs;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200170 int luns_in_chnl = (luns_left > dev->geo.num_lun) ?
171 dev->geo.num_lun : luns_left;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100172
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200173 if (lun_balanced && prev_num_lun != luns_in_chnl)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100174 lun_balanced = 0;
175
176 ch_map->ch_off = ch_rmap->ch_off = bch;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200177 ch_map->num_lun = luns_in_chnl;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100178
179 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
180 if (!lun_offs)
181 goto err_ch;
182
183 for (j = 0; j < luns_in_chnl; j++) {
184 luns[lunid].ppa = 0;
Javier González69471512018-03-30 00:05:15 +0200185 luns[lunid].a.ch = i;
186 luns[lunid++].a.lun = j;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100187
188 lun_offs[j] = blun;
189 lun_roffs[j + blun] = blun;
190 }
191
192 ch_map->lun_offs = lun_offs;
193
194 /* when starting a new channel, lun offset is reset */
195 blun = 0;
196 luns_left -= luns_in_chnl;
197 }
198
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200199 dev_map->num_ch = num_ch;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100200
201 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
202 if (!tgt_dev)
203 goto err_ch;
204
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200205 /* Inherit device geometry from parent */
Matias Bjørlingade69e22017-01-31 13:17:09 +0100206 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200207
Matias Bjørlingade69e22017-01-31 13:17:09 +0100208 /* Target device only owns a portion of the physical device */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200209 tgt_dev->geo.num_ch = num_ch;
210 tgt_dev->geo.num_lun = (lun_balanced) ? prev_num_lun : -1;
211 tgt_dev->geo.all_luns = num_lun;
212 tgt_dev->geo.all_chunks = num_lun * dev->geo.num_chk;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200213
Javier Gonzáleze5392732018-01-05 14:16:14 +0100214 tgt_dev->geo.op = op;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200215
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200216 sec_per_lun = dev->geo.clba * dev->geo.num_chk;
217 tgt_dev->geo.total_secs = num_lun * sec_per_lun;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200218
Matias Bjørlingade69e22017-01-31 13:17:09 +0100219 tgt_dev->q = dev->q;
220 tgt_dev->map = dev_map;
221 tgt_dev->luns = luns;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100222 tgt_dev->parent = dev;
223
224 return tgt_dev;
225err_ch:
Javier González507f7d62017-05-03 11:19:05 +0200226 while (--i >= 0)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100227 kfree(dev_map->chnls[i].lun_offs);
228 kfree(luns);
229err_luns:
230 kfree(dev_map->chnls);
231err_chnls:
232 kfree(dev_map);
233err_dev:
234 return tgt_dev;
235}
236
237static const struct block_device_operations nvm_fops = {
238 .owner = THIS_MODULE,
239};
240
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100241static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200242{
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100243 struct nvm_tgt_type *tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200244
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100245 list_for_each_entry(tt, &nvm_tgt_types, list)
246 if (!strcmp(name, tt->name))
247 return tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200248
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100249 return NULL;
250}
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200251
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100252static struct nvm_tgt_type *nvm_find_target_type(const char *name)
253{
254 struct nvm_tgt_type *tt;
255
256 down_write(&nvm_tgtt_lock);
257 tt = __nvm_find_target_type(name);
258 up_write(&nvm_tgtt_lock);
259
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200260 return tt;
261}
262
Javier Gonzáleze5392732018-01-05 14:16:14 +0100263static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin,
264 int lun_end)
265{
266 if (lun_begin > lun_end || lun_end >= geo->all_luns) {
267 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
268 lun_begin, lun_end, geo->all_luns - 1);
269 return -EINVAL;
270 }
271
272 return 0;
273}
274
275static int __nvm_config_simple(struct nvm_dev *dev,
276 struct nvm_ioctl_create_simple *s)
277{
278 struct nvm_geo *geo = &dev->geo;
279
280 if (s->lun_begin == -1 && s->lun_end == -1) {
281 s->lun_begin = 0;
282 s->lun_end = geo->all_luns - 1;
283 }
284
285 return nvm_config_check_luns(geo, s->lun_begin, s->lun_end);
286}
287
288static int __nvm_config_extended(struct nvm_dev *dev,
289 struct nvm_ioctl_create_extended *e)
290{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100291 if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) {
292 e->lun_begin = 0;
293 e->lun_end = dev->geo.all_luns - 1;
294 }
295
296 /* op not set falls into target's default */
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200297 if (e->op == 0xFFFF) {
Javier Gonzáleze5392732018-01-05 14:16:14 +0100298 e->op = NVM_TARGET_DEFAULT_OP;
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200299 } else if (e->op < NVM_TARGET_MIN_OP || e->op > NVM_TARGET_MAX_OP) {
Javier Gonzáleze5392732018-01-05 14:16:14 +0100300 pr_err("nvm: invalid over provisioning value\n");
301 return -EINVAL;
302 }
303
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200304 return nvm_config_check_luns(&dev->geo, e->lun_begin, e->lun_end);
Javier Gonzáleze5392732018-01-05 14:16:14 +0100305}
306
Matias Bjørlingade69e22017-01-31 13:17:09 +0100307static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
308{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100309 struct nvm_ioctl_create_extended e;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100310 struct request_queue *tqueue;
311 struct gendisk *tdisk;
312 struct nvm_tgt_type *tt;
313 struct nvm_target *t;
314 struct nvm_tgt_dev *tgt_dev;
315 void *targetdata;
Igor Konopkoa14669e2019-05-04 20:37:55 +0200316 unsigned int mdts;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300317 int ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100318
Javier Gonzáleze5392732018-01-05 14:16:14 +0100319 switch (create->conf.type) {
320 case NVM_CONFIG_TYPE_SIMPLE:
321 ret = __nvm_config_simple(dev, &create->conf.s);
322 if (ret)
323 return ret;
324
325 e.lun_begin = create->conf.s.lun_begin;
326 e.lun_end = create->conf.s.lun_end;
327 e.op = NVM_TARGET_DEFAULT_OP;
328 break;
329 case NVM_CONFIG_TYPE_EXTENDED:
330 ret = __nvm_config_extended(dev, &create->conf.e);
331 if (ret)
332 return ret;
333
334 e = create->conf.e;
335 break;
336 default:
337 pr_err("nvm: config type not valid\n");
338 return -EINVAL;
339 }
340
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100341 tt = nvm_find_target_type(create->tgttype);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100342 if (!tt) {
343 pr_err("nvm: target type %s not found\n", create->tgttype);
344 return -EINVAL;
345 }
346
Matias Bjørling656e33c2018-10-09 13:11:34 +0200347 if ((tt->flags & NVM_TGT_F_HOST_L2P) != (dev->geo.dom & NVM_RSP_L2P)) {
348 pr_err("nvm: device is incompatible with target L2P type.\n");
349 return -EINVAL;
350 }
351
Javier Gonzálezbd77b232018-01-05 14:16:05 +0100352 if (nvm_target_exists(create->tgtname)) {
353 pr_err("nvm: target name already exists (%s)\n",
354 create->tgtname);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100355 return -EINVAL;
356 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100357
Javier Gonzáleze5392732018-01-05 14:16:14 +0100358 ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end);
Rakesh Pandit12e9a6d62017-06-27 14:55:33 +0300359 if (ret)
360 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100361
362 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300363 if (!t) {
364 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100365 goto err_reserve;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300366 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100367
Javier Gonzáleze5392732018-01-05 14:16:14 +0100368 tgt_dev = nvm_create_tgt_dev(dev, e.lun_begin, e.lun_end, e.op);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100369 if (!tgt_dev) {
370 pr_err("nvm: could not create target device\n");
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300371 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100372 goto err_t;
373 }
374
Matias Bjørlingade69e22017-01-31 13:17:09 +0100375 tdisk = alloc_disk(0);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300376 if (!tdisk) {
377 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200378 goto err_dev;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300379 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200380
Christoph Hellwig6d469642018-11-14 17:02:18 +0100381 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300382 if (!tqueue) {
383 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200384 goto err_disk;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300385 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200386 blk_queue_make_request(tqueue, tt->make_rq);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100387
Javier González6eb08242017-04-15 20:55:49 +0200388 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100389 tdisk->flags = GENHD_FL_EXT_DEVT;
390 tdisk->major = 0;
391 tdisk->first_minor = 0;
392 tdisk->fops = &nvm_fops;
393 tdisk->queue = tqueue;
394
Javier González4af3f752017-04-15 20:55:45 +0200395 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300396 if (IS_ERR(targetdata)) {
397 ret = PTR_ERR(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100398 goto err_init;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300399 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100400
401 tdisk->private_data = targetdata;
402 tqueue->queuedata = targetdata;
403
Igor Konopkoa14669e2019-05-04 20:37:55 +0200404 mdts = (dev->geo.csecs >> 9) * NVM_MAX_VLBA;
405 if (dev->geo.mdts) {
406 mdts = min_t(u32, dev->geo.mdts,
407 (dev->geo.csecs >> 9) * NVM_MAX_VLBA);
408 }
409 blk_queue_max_hw_sectors(tqueue, mdts);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100410
411 set_capacity(tdisk, tt->capacity(targetdata));
412 add_disk(tdisk);
413
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300414 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
415 ret = -ENOMEM;
Javier González9a69b0e2017-01-31 13:17:20 +0100416 goto err_sysfs;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300417 }
Javier González9a69b0e2017-01-31 13:17:20 +0100418
Matias Bjørlingade69e22017-01-31 13:17:09 +0100419 t->type = tt;
420 t->disk = tdisk;
421 t->dev = tgt_dev;
422
423 mutex_lock(&dev->mlock);
424 list_add_tail(&t->list, &dev->targets);
425 mutex_unlock(&dev->mlock);
426
Rakesh Pandit90014822017-10-13 14:45:50 +0200427 __module_get(tt->owner);
428
Matias Bjørlingade69e22017-01-31 13:17:09 +0100429 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100430err_sysfs:
431 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200432 tt->exit(targetdata, true);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100433err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100434 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600435 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200436err_disk:
437 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100438err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200439 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100440err_t:
441 kfree(t);
442err_reserve:
Javier Gonzáleze5392732018-01-05 14:16:14 +0100443 nvm_release_luns_err(dev, e.lun_begin, e.lun_end);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300444 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100445}
446
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200447static void __nvm_remove_target(struct nvm_target *t, bool graceful)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100448{
449 struct nvm_tgt_type *tt = t->type;
450 struct gendisk *tdisk = t->disk;
451 struct request_queue *q = tdisk->queue;
452
453 del_gendisk(tdisk);
454 blk_cleanup_queue(q);
455
Javier González9a69b0e2017-01-31 13:17:20 +0100456 if (tt->sysfs_exit)
457 tt->sysfs_exit(tdisk);
458
Matias Bjørlingade69e22017-01-31 13:17:09 +0100459 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200460 tt->exit(tdisk->private_data, graceful);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100461
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200462 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100463 put_disk(tdisk);
Rakesh Pandit90014822017-10-13 14:45:50 +0200464 module_put(t->type->owner);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100465
466 list_del(&t->list);
467 kfree(t);
468}
469
470/**
471 * nvm_remove_tgt - Removes a target from the media manager
Matias Bjørlingade69e22017-01-31 13:17:09 +0100472 * @remove: ioctl structure with target name to remove.
473 *
474 * Returns:
475 * 0: on success
476 * 1: on not found
477 * <0: on error
478 */
Igor Konopko843f2ed2019-05-04 20:38:09 +0200479static int nvm_remove_tgt(struct nvm_ioctl_remove *remove)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100480{
481 struct nvm_target *t;
Igor Konopko843f2ed2019-05-04 20:38:09 +0200482 struct nvm_dev *dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100483
Igor Konopko843f2ed2019-05-04 20:38:09 +0200484 down_read(&nvm_lock);
485 list_for_each_entry(dev, &nvm_devices, devices) {
486 mutex_lock(&dev->mlock);
487 t = nvm_find_target(dev, remove->tgtname);
488 if (t) {
489 mutex_unlock(&dev->mlock);
490 break;
491 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100492 mutex_unlock(&dev->mlock);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100493 }
Igor Konopko843f2ed2019-05-04 20:38:09 +0200494 up_read(&nvm_lock);
495
496 if (!t)
497 return 1;
498
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200499 __nvm_remove_target(t, true);
Igor Konopkoe69397e2019-05-04 20:38:08 +0200500 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100501
502 return 0;
503}
504
505static int nvm_register_map(struct nvm_dev *dev)
506{
507 struct nvm_dev_map *rmap;
508 int i, j;
509
510 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
511 if (!rmap)
512 goto err_rmap;
513
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200514 rmap->chnls = kcalloc(dev->geo.num_ch, sizeof(struct nvm_ch_map),
Matias Bjørlingade69e22017-01-31 13:17:09 +0100515 GFP_KERNEL);
516 if (!rmap->chnls)
517 goto err_chnls;
518
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200519 for (i = 0; i < dev->geo.num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100520 struct nvm_ch_map *ch_rmap;
521 int *lun_roffs;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200522 int luns_in_chnl = dev->geo.num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100523
524 ch_rmap = &rmap->chnls[i];
525
526 ch_rmap->ch_off = -1;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200527 ch_rmap->num_lun = luns_in_chnl;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100528
529 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
530 if (!lun_roffs)
531 goto err_ch;
532
533 for (j = 0; j < luns_in_chnl; j++)
534 lun_roffs[j] = -1;
535
536 ch_rmap->lun_offs = lun_roffs;
537 }
538
539 dev->rmap = rmap;
540
541 return 0;
542err_ch:
543 while (--i >= 0)
544 kfree(rmap->chnls[i].lun_offs);
545err_chnls:
546 kfree(rmap);
547err_rmap:
548 return -ENOMEM;
549}
550
Javier González7a3de2b2017-04-15 20:55:39 +0200551static void nvm_unregister_map(struct nvm_dev *dev)
552{
553 struct nvm_dev_map *rmap = dev->rmap;
554 int i;
555
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200556 for (i = 0; i < dev->geo.num_ch; i++)
Javier González7a3de2b2017-04-15 20:55:39 +0200557 kfree(rmap->chnls[i].lun_offs);
558
559 kfree(rmap->chnls);
560 kfree(rmap);
561}
562
Matias Bjørling61a561d2017-01-31 13:17:13 +0100563static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100564{
565 struct nvm_dev_map *dev_map = tgt_dev->map;
Javier González69471512018-03-30 00:05:15 +0200566 struct nvm_ch_map *ch_map = &dev_map->chnls[p->a.ch];
567 int lun_off = ch_map->lun_offs[p->a.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100568
Javier González69471512018-03-30 00:05:15 +0200569 p->a.ch += ch_map->ch_off;
570 p->a.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100571}
572
Matias Bjørling61a561d2017-01-31 13:17:13 +0100573static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100574{
575 struct nvm_dev *dev = tgt_dev->parent;
576 struct nvm_dev_map *dev_rmap = dev->rmap;
Javier González69471512018-03-30 00:05:15 +0200577 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->a.ch];
578 int lun_roff = ch_rmap->lun_offs[p->a.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100579
Javier González69471512018-03-30 00:05:15 +0200580 p->a.ch -= ch_rmap->ch_off;
581 p->a.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100582}
583
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100584static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
585 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100586{
587 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100588
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100589 for (i = 0; i < nr_ppas; i++) {
590 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
Javier González7100d502018-03-30 00:05:16 +0200591 ppa_list[i] = generic_to_dev_addr(tgt_dev->parent, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100592 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100593}
594
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100595static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
596 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100597{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100598 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100599
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100600 for (i = 0; i < nr_ppas; i++) {
Javier González7100d502018-03-30 00:05:16 +0200601 ppa_list[i] = dev_to_generic_addr(tgt_dev->parent, ppa_list[i]);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100602 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
603 }
604}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100605
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100606static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
607{
Hans Holmbergd68a9342018-10-09 13:11:46 +0200608 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100609
Hans Holmbergd68a9342018-10-09 13:11:46 +0200610 nvm_ppa_tgt_to_dev(tgt_dev, ppa_list, rqd->nr_ppas);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100611}
612
613static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
614{
Hans Holmbergd68a9342018-10-09 13:11:46 +0200615 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100616
Hans Holmbergd68a9342018-10-09 13:11:46 +0200617 nvm_ppa_dev_to_tgt(tgt_dev, ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100618}
619
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200620int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100621{
622 int ret = 0;
623
Matias Bjørling5cd90782016-07-07 09:54:17 +0200624 down_write(&nvm_tgtt_lock);
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100625 if (__nvm_find_target_type(tt->name))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100626 ret = -EEXIST;
627 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200628 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200629 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100630
631 return ret;
632}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200633EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100634
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200635void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100636{
637 if (!tt)
638 return;
639
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200640 down_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100641 list_del(&tt->list);
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200642 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100643}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200644EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100645
646void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
647 dma_addr_t *dma_handler)
648{
Javier González75b85642016-05-06 20:03:13 +0200649 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100650 dma_handler);
651}
652EXPORT_SYMBOL(nvm_dev_dma_alloc);
653
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100654void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100655{
Javier González75b85642016-05-06 20:03:13 +0200656 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100657}
658EXPORT_SYMBOL(nvm_dev_dma_free);
659
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100660static struct nvm_dev *nvm_find_nvm_dev(const char *name)
661{
662 struct nvm_dev *dev;
663
664 list_for_each_entry(dev, &nvm_devices, devices)
665 if (!strcmp(name, dev->name))
666 return dev;
667
668 return NULL;
669}
670
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200671static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
672 const struct ppa_addr *ppas, int nr_ppas)
673{
674 struct nvm_dev *dev = tgt_dev->parent;
675 struct nvm_geo *geo = &tgt_dev->geo;
676 int i, plane_cnt, pl_idx;
677 struct ppa_addr ppa;
678
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200679 if (geo->pln_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200680 rqd->nr_ppas = nr_ppas;
681 rqd->ppa_addr = ppas[0];
682
683 return 0;
684 }
685
686 rqd->nr_ppas = nr_ppas;
687 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
688 if (!rqd->ppa_list) {
689 pr_err("nvm: failed to allocate dma memory\n");
690 return -ENOMEM;
691 }
692
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200693 plane_cnt = geo->pln_mode;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200694 rqd->nr_ppas *= plane_cnt;
695
696 for (i = 0; i < nr_ppas; i++) {
697 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
698 ppa = ppas[i];
699 ppa.g.pl = pl_idx;
700 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
701 }
702 }
703
704 return 0;
705}
706
707static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
708 struct nvm_rq *rqd)
709{
710 if (!rqd->ppa_list)
711 return;
712
713 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
714}
715
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200716static int nvm_set_flags(struct nvm_geo *geo, struct nvm_rq *rqd)
717{
718 int flags = 0;
719
720 if (geo->version == NVM_OCSSD_SPEC_20)
721 return 0;
722
723 if (rqd->is_seq)
724 flags |= geo->pln_mode >> 1;
725
726 if (rqd->opcode == NVM_OP_PREAD)
727 flags |= (NVM_IO_SCRAMBLE_ENABLE | NVM_IO_SUSPEND);
728 else if (rqd->opcode == NVM_OP_PWRITE)
729 flags |= NVM_IO_SCRAMBLE_ENABLE;
730
731 return flags;
732}
733
Javier González8e536242016-11-28 22:39:10 +0100734int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100735{
Javier González8e536242016-11-28 22:39:10 +0100736 struct nvm_dev *dev = tgt_dev->parent;
Javier González3e505af2017-06-26 11:57:10 +0200737 int ret;
Javier González8e536242016-11-28 22:39:10 +0100738
Matias Bjørlingade69e22017-01-31 13:17:09 +0100739 if (!dev->ops->submit_io)
740 return -ENODEV;
741
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100742 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100743
744 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200745 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González3e505af2017-06-26 11:57:10 +0200746
747 /* In case of error, fail with right address format */
748 ret = dev->ops->submit_io(dev, rqd);
749 if (ret)
750 nvm_rq_dev_to_tgt(tgt_dev, rqd);
751 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100752}
753EXPORT_SYMBOL(nvm_submit_io);
754
Javier González1a94b2d2017-10-13 14:46:47 +0200755int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100756{
Javier González1a94b2d2017-10-13 14:46:47 +0200757 struct nvm_dev *dev = tgt_dev->parent;
758 int ret;
Javier González17912c42017-04-15 20:55:37 +0200759
Javier González1a94b2d2017-10-13 14:46:47 +0200760 if (!dev->ops->submit_io_sync)
761 return -ENODEV;
762
763 nvm_rq_tgt_to_dev(tgt_dev, rqd);
764
765 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200766 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200767
768 /* In case of error, fail with right address format */
769 ret = dev->ops->submit_io_sync(dev, rqd);
770 nvm_rq_dev_to_tgt(tgt_dev, rqd);
771
772 return ret;
Javier González17912c42017-04-15 20:55:37 +0200773}
Javier González1a94b2d2017-10-13 14:46:47 +0200774EXPORT_SYMBOL(nvm_submit_io_sync);
Javier González17912c42017-04-15 20:55:37 +0200775
Matias Bjørling06894ef2017-01-31 13:17:17 +0100776void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100777{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100778 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100779
780 /* Convert address space */
781 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100782 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100783
Matias Bjørling06894ef2017-01-31 13:17:17 +0100784 if (rqd->end_io)
785 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100786}
787EXPORT_SYMBOL(nvm_end_io);
788
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200789static int nvm_submit_io_sync_raw(struct nvm_dev *dev, struct nvm_rq *rqd)
790{
791 if (!dev->ops->submit_io_sync)
792 return -ENODEV;
793
794 rqd->flags = nvm_set_flags(&dev->geo, rqd);
795
796 return dev->ops->submit_io_sync(dev, rqd);
797}
798
799static int nvm_bb_chunk_sense(struct nvm_dev *dev, struct ppa_addr ppa)
800{
801 struct nvm_rq rqd = { NULL };
802 struct bio bio;
803 struct bio_vec bio_vec;
804 struct page *page;
805 int ret;
806
807 page = alloc_page(GFP_KERNEL);
808 if (!page)
809 return -ENOMEM;
810
811 bio_init(&bio, &bio_vec, 1);
812 bio_add_page(&bio, page, PAGE_SIZE, 0);
813 bio_set_op_attrs(&bio, REQ_OP_READ, 0);
814
815 rqd.bio = &bio;
816 rqd.opcode = NVM_OP_PREAD;
817 rqd.is_seq = 1;
818 rqd.nr_ppas = 1;
819 rqd.ppa_addr = generic_to_dev_addr(dev, ppa);
820
821 ret = nvm_submit_io_sync_raw(dev, &rqd);
822 if (ret)
823 return ret;
824
825 __free_page(page);
826
827 return rqd.error;
828}
829
Matias Bjørling22e8c972016-05-06 20:02:58 +0200830/*
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200831 * Scans a 1.2 chunk first and last page to determine if its state.
832 * If the chunk is found to be open, also scan it to update the write
833 * pointer.
Matias Bjørling22e8c972016-05-06 20:02:58 +0200834 */
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200835static int nvm_bb_chunk_scan(struct nvm_dev *dev, struct ppa_addr ppa,
836 struct nvm_chk_meta *meta)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200837{
Javier González8e79b5c2016-11-28 22:39:06 +0100838 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200839 int ret, pg, pl;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200840
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200841 /* sense first page */
842 ret = nvm_bb_chunk_sense(dev, ppa);
843 if (ret < 0) /* io error */
844 return ret;
845 else if (ret == 0) /* valid data */
846 meta->state = NVM_CHK_ST_OPEN;
847 else if (ret > 0) {
848 /*
849 * If empty page, the chunk is free, else it is an
850 * actual io error. In that case, mark it offline.
851 */
852 switch (ret) {
853 case NVM_RSP_ERR_EMPTYPAGE:
854 meta->state = NVM_CHK_ST_FREE;
855 return 0;
856 case NVM_RSP_ERR_FAILCRC:
857 case NVM_RSP_ERR_FAILECC:
858 case NVM_RSP_WARN_HIGHECC:
859 meta->state = NVM_CHK_ST_OPEN;
860 goto scan;
861 default:
862 return -ret; /* other io error */
863 }
864 }
865
866 /* sense last page */
867 ppa.g.pg = geo->num_pg - 1;
868 ppa.g.pl = geo->num_pln - 1;
869
870 ret = nvm_bb_chunk_sense(dev, ppa);
871 if (ret < 0) /* io error */
872 return ret;
873 else if (ret == 0) { /* Chunk fully written */
874 meta->state = NVM_CHK_ST_CLOSED;
875 meta->wp = geo->clba;
876 return 0;
877 } else if (ret > 0) {
878 switch (ret) {
879 case NVM_RSP_ERR_EMPTYPAGE:
880 case NVM_RSP_ERR_FAILCRC:
881 case NVM_RSP_ERR_FAILECC:
882 case NVM_RSP_WARN_HIGHECC:
883 meta->state = NVM_CHK_ST_OPEN;
884 break;
885 default:
886 return -ret; /* other io error */
887 }
888 }
889
890scan:
891 /*
892 * chunk is open, we scan sequentially to update the write pointer.
893 * We make the assumption that targets write data across all planes
894 * before moving to the next page.
895 */
896 for (pg = 0; pg < geo->num_pg; pg++) {
897 for (pl = 0; pl < geo->num_pln; pl++) {
898 ppa.g.pg = pg;
899 ppa.g.pl = pl;
900
901 ret = nvm_bb_chunk_sense(dev, ppa);
902 if (ret < 0) /* io error */
903 return ret;
904 else if (ret == 0) {
905 meta->wp += geo->ws_min;
906 } else if (ret > 0) {
907 switch (ret) {
908 case NVM_RSP_ERR_EMPTYPAGE:
909 return 0;
910 case NVM_RSP_ERR_FAILCRC:
911 case NVM_RSP_ERR_FAILECC:
912 case NVM_RSP_WARN_HIGHECC:
913 meta->wp += geo->ws_min;
914 break;
915 default:
916 return -ret; /* other io error */
917 }
918 }
919 }
920 }
921
922 return 0;
923}
924
925/*
926 * folds a bad block list from its plane representation to its
927 * chunk representation.
928 *
929 * If any of the planes status are bad or grown bad, the chunk is marked
930 * offline. If not bad, the first plane state acts as the chunk state.
931 */
932static int nvm_bb_to_chunk(struct nvm_dev *dev, struct ppa_addr ppa,
933 u8 *blks, int nr_blks, struct nvm_chk_meta *meta)
934{
935 struct nvm_geo *geo = &dev->geo;
936 int ret, blk, pl, offset, blktype;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200937
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200938 for (blk = 0; blk < geo->num_chk; blk++) {
939 offset = blk * geo->pln_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200940 blktype = blks[offset];
941
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200942 for (pl = 0; pl < geo->pln_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200943 if (blks[offset + pl] &
944 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
945 blktype = blks[offset + pl];
946 break;
947 }
948 }
949
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200950 ppa.g.blk = blk;
951
952 meta->wp = 0;
953 meta->type = NVM_CHK_TP_W_SEQ;
954 meta->wi = 0;
955 meta->slba = generic_to_dev_addr(dev, ppa).ppa;
956 meta->cnlb = dev->geo.clba;
957
958 if (blktype == NVM_BLK_T_FREE) {
959 ret = nvm_bb_chunk_scan(dev, ppa, meta);
960 if (ret)
961 return ret;
962 } else {
963 meta->state = NVM_CHK_ST_OFFLINE;
964 }
965
966 meta++;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200967 }
968
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200969 return 0;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200970}
Matias Bjørling22e8c972016-05-06 20:02:58 +0200971
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200972static int nvm_get_bb_meta(struct nvm_dev *dev, sector_t slba,
973 int nchks, struct nvm_chk_meta *meta)
974{
975 struct nvm_geo *geo = &dev->geo;
976 struct ppa_addr ppa;
977 u8 *blks;
978 int ch, lun, nr_blks;
Geert Uytterhoeven55e58c52018-12-11 20:16:08 +0100979 int ret = 0;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200980
981 ppa.ppa = slba;
982 ppa = dev_to_generic_addr(dev, ppa);
983
984 if (ppa.g.blk != 0)
985 return -EINVAL;
986
987 if ((nchks % geo->num_chk) != 0)
988 return -EINVAL;
989
990 nr_blks = geo->num_chk * geo->pln_mode;
991
992 blks = kmalloc(nr_blks, GFP_KERNEL);
993 if (!blks)
994 return -ENOMEM;
995
996 for (ch = ppa.g.ch; ch < geo->num_ch; ch++) {
997 for (lun = ppa.g.lun; lun < geo->num_lun; lun++) {
998 struct ppa_addr ppa_gen, ppa_dev;
999
1000 if (!nchks)
1001 goto done;
1002
1003 ppa_gen.ppa = 0;
1004 ppa_gen.g.ch = ch;
1005 ppa_gen.g.lun = lun;
1006 ppa_dev = generic_to_dev_addr(dev, ppa_gen);
1007
1008 ret = dev->ops->get_bb_tbl(dev, ppa_dev, blks);
1009 if (ret)
1010 goto done;
1011
1012 ret = nvm_bb_to_chunk(dev, ppa_gen, blks, nr_blks,
1013 meta);
1014 if (ret)
1015 goto done;
1016
1017 meta += geo->num_chk;
1018 nchks -= geo->num_chk;
1019 }
1020 }
1021done:
1022 kfree(blks);
1023 return ret;
1024}
1025
1026int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
1027 int nchks, struct nvm_chk_meta *meta)
Javier González333ba052016-11-28 22:39:14 +01001028{
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001029 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +01001030
Matias Bjørlingdab8ee92017-01-31 13:17:14 +01001031 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001032
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001033 if (dev->geo.version == NVM_OCSSD_SPEC_12)
1034 return nvm_get_bb_meta(dev, (sector_t)ppa.ppa, nchks, meta);
1035
1036 return dev->ops->get_chk_meta(dev, (sector_t)ppa.ppa, nchks, meta);
Javier González333ba052016-11-28 22:39:14 +01001037}
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001038EXPORT_SYMBOL_GPL(nvm_get_chunk_meta);
1039
1040int nvm_set_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
1041 int nr_ppas, int type)
1042{
1043 struct nvm_dev *dev = tgt_dev->parent;
1044 struct nvm_rq rqd;
1045 int ret;
1046
1047 if (dev->geo.version == NVM_OCSSD_SPEC_20)
1048 return 0;
1049
1050 if (nr_ppas > NVM_MAX_VLBA) {
1051 pr_err("nvm: unable to update all blocks atomically\n");
1052 return -EINVAL;
1053 }
1054
1055 memset(&rqd, 0, sizeof(struct nvm_rq));
1056
1057 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
1058 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
1059
1060 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
1061 nvm_free_rqd_ppalist(tgt_dev, &rqd);
1062 if (ret)
1063 return -EINVAL;
1064
1065 return 0;
1066}
1067EXPORT_SYMBOL_GPL(nvm_set_chunk_meta);
Javier González333ba052016-11-28 22:39:14 +01001068
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001069static int nvm_core_init(struct nvm_dev *dev)
1070{
Javier González8e79b5c2016-11-28 22:39:06 +01001071 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001072 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001073
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001074 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +01001075 sizeof(unsigned long), GFP_KERNEL);
1076 if (!dev->lun_map)
1077 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001078
Matias Bjørlingade69e22017-01-31 13:17:09 +01001079 INIT_LIST_HEAD(&dev->area_list);
1080 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +01001081 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001082 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001083
Matias Bjørlingade69e22017-01-31 13:17:09 +01001084 ret = nvm_register_map(dev);
1085 if (ret)
1086 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001087
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001088 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001089err_fmtype:
1090 kfree(dev->lun_map);
1091 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001092}
1093
Igor Konopkoe69397e2019-05-04 20:38:08 +02001094static void nvm_free(struct kref *ref)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001095{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001096 struct nvm_dev *dev = container_of(ref, struct nvm_dev, ref);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001097
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001098 if (dev->dma_pool)
1099 dev->ops->destroy_dma_pool(dev->dma_pool);
1100
Igor Konopkoe69397e2019-05-04 20:38:08 +02001101 if (dev->rmap)
1102 nvm_unregister_map(dev);
1103
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001104 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001105 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001106}
1107
1108static int nvm_init(struct nvm_dev *dev)
1109{
Javier González8e79b5c2016-11-28 22:39:06 +01001110 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001111 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001112
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001113 if (dev->ops->identity(dev)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001114 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001115 goto err;
1116 }
1117
Javier González3cb98f82018-03-30 00:05:11 +02001118 pr_debug("nvm: ver:%u.%u nvm_vendor:%x\n",
1119 geo->major_ver_id, geo->minor_ver_id,
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001120 geo->vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001121
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001122 ret = nvm_core_init(dev);
1123 if (ret) {
1124 pr_err("nvm: could not initialize core structures.\n");
1125 goto err;
1126 }
1127
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001128 pr_info("nvm: registered %s [%u/%u/%u/%u/%u]\n",
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001129 dev->name, dev->geo.ws_min, dev->geo.ws_opt,
1130 dev->geo.num_chk, dev->geo.all_luns,
1131 dev->geo.num_ch);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001132 return 0;
1133err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001134 pr_err("nvm: failed to initialize nvm\n");
1135 return ret;
1136}
1137
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001138struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001139{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001140 struct nvm_dev *dev;
1141
1142 dev = kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1143 if (dev)
1144 kref_init(&dev->ref);
1145
1146 return dev;
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001147}
1148EXPORT_SYMBOL(nvm_alloc_dev);
1149
1150int nvm_register(struct nvm_dev *dev)
1151{
Igor Konopko24828d02018-12-11 20:16:24 +01001152 int ret, exp_pool_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001153
Igor Konopkoe69397e2019-05-04 20:38:08 +02001154 if (!dev->q || !dev->ops) {
1155 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001156 return -EINVAL;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001157 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001158
Matias Bjørling85136c02018-12-11 20:16:20 +01001159 ret = nvm_init(dev);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001160 if (ret) {
1161 kref_put(&dev->ref, nvm_free);
Matias Bjørling85136c02018-12-11 20:16:20 +01001162 return ret;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001163 }
Matias Bjørling85136c02018-12-11 20:16:20 +01001164
Igor Konopko24828d02018-12-11 20:16:24 +01001165 exp_pool_size = max_t(int, PAGE_SIZE,
1166 (NVM_MAX_VLBA * (sizeof(u64) + dev->geo.sos)));
1167 exp_pool_size = round_up(exp_pool_size, PAGE_SIZE);
1168
1169 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist",
1170 exp_pool_size);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001171 if (!dev->dma_pool) {
1172 pr_err("nvm: could not create dma pool\n");
Igor Konopkoe69397e2019-05-04 20:38:08 +02001173 kref_put(&dev->ref, nvm_free);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001174 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001175 }
1176
Matias Bjørling762796b2015-12-06 11:25:49 +01001177 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001178 down_write(&nvm_lock);
1179 list_add(&dev->devices, &nvm_devices);
1180 up_write(&nvm_lock);
1181
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001182 return 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001183}
1184EXPORT_SYMBOL(nvm_register);
1185
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001186void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001187{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001188 struct nvm_target *t, *tmp;
1189
1190 mutex_lock(&dev->mlock);
1191 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1192 if (t->dev->parent != dev)
1193 continue;
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001194 __nvm_remove_target(t, false);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001195 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001196 }
1197 mutex_unlock(&dev->mlock);
1198
Wenwei Taod0a712c2015-11-28 16:49:28 +01001199 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001200 list_del(&dev->devices);
1201 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001202
Igor Konopkoe69397e2019-05-04 20:38:08 +02001203 kref_put(&dev->ref, nvm_free);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001204}
1205EXPORT_SYMBOL(nvm_unregister);
1206
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001207static int __nvm_configure_create(struct nvm_ioctl_create *create)
1208{
1209 struct nvm_dev *dev;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001210 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001211
Wenwei Taod0a712c2015-11-28 16:49:28 +01001212 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001213 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001214 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001215
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001216 if (!dev) {
1217 pr_err("nvm: device not found\n");
1218 return -EINVAL;
1219 }
1220
Igor Konopkoe69397e2019-05-04 20:38:08 +02001221 kref_get(&dev->ref);
1222 ret = nvm_create_tgt(dev, create);
1223 if (ret)
1224 kref_put(&dev->ref, nvm_free);
1225
1226 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001227}
1228
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001229static long nvm_ioctl_info(struct file *file, void __user *arg)
1230{
1231 struct nvm_ioctl_info *info;
1232 struct nvm_tgt_type *tt;
1233 int tgt_iter = 0;
1234
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001235 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1236 if (IS_ERR(info))
1237 return -EFAULT;
1238
1239 info->version[0] = NVM_VERSION_MAJOR;
1240 info->version[1] = NVM_VERSION_MINOR;
1241 info->version[2] = NVM_VERSION_PATCH;
1242
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001243 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001244 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001245 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1246
1247 tgt->version[0] = tt->version[0];
1248 tgt->version[1] = tt->version[1];
1249 tgt->version[2] = tt->version[2];
1250 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1251
1252 tgt_iter++;
1253 }
1254
1255 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001256 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001257
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001258 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1259 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001260 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001261 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001262
1263 kfree(info);
1264 return 0;
1265}
1266
1267static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1268{
1269 struct nvm_ioctl_get_devices *devices;
1270 struct nvm_dev *dev;
1271 int i = 0;
1272
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001273 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1274 if (!devices)
1275 return -ENOMEM;
1276
1277 down_write(&nvm_lock);
1278 list_for_each_entry(dev, &nvm_devices, devices) {
1279 struct nvm_ioctl_device_info *info = &devices->info[i];
1280
Javier González6eb08242017-04-15 20:55:49 +02001281 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001282
Matias Bjørlingade69e22017-01-31 13:17:09 +01001283 /* kept for compatibility */
1284 info->bmversion[0] = 1;
1285 info->bmversion[1] = 0;
1286 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001287 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001288 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001289
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001290 if (i > 31) {
1291 pr_err("nvm: max 31 devices can be reported.\n");
1292 break;
1293 }
1294 }
1295 up_write(&nvm_lock);
1296
1297 devices->nr_devices = i;
1298
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001299 if (copy_to_user(arg, devices,
1300 sizeof(struct nvm_ioctl_get_devices))) {
1301 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001302 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001303 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001304
1305 kfree(devices);
1306 return 0;
1307}
1308
1309static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1310{
1311 struct nvm_ioctl_create create;
1312
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001313 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1314 return -EFAULT;
1315
Javier Gonzáleze5392732018-01-05 14:16:14 +01001316 if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED &&
1317 create.conf.e.rsv != 0) {
1318 pr_err("nvm: reserved config field in use\n");
1319 return -EINVAL;
1320 }
1321
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001322 create.dev[DISK_NAME_LEN - 1] = '\0';
1323 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1324 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1325
1326 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001327 __u32 flags = create.flags;
1328
1329 /* Check for valid flags */
1330 if (flags & NVM_TARGET_FACTORY)
1331 flags &= ~NVM_TARGET_FACTORY;
1332
1333 if (flags) {
1334 pr_err("nvm: flag not supported\n");
1335 return -EINVAL;
1336 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001337 }
1338
1339 return __nvm_configure_create(&create);
1340}
1341
1342static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1343{
1344 struct nvm_ioctl_remove remove;
1345
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001346 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1347 return -EFAULT;
1348
1349 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1350
1351 if (remove.flags != 0) {
1352 pr_err("nvm: no flags supported\n");
1353 return -EINVAL;
1354 }
1355
Igor Konopko843f2ed2019-05-04 20:38:09 +02001356 return nvm_remove_tgt(&remove);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001357}
1358
Matias Bjørlingade69e22017-01-31 13:17:09 +01001359/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001360static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1361{
1362 struct nvm_ioctl_dev_init init;
1363
Matias Bjørling55696152016-01-12 07:49:37 +01001364 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1365 return -EFAULT;
1366
1367 if (init.flags != 0) {
1368 pr_err("nvm: no flags supported\n");
1369 return -EINVAL;
1370 }
1371
Matias Bjørlingade69e22017-01-31 13:17:09 +01001372 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001373}
1374
Matias Bjørlingade69e22017-01-31 13:17:09 +01001375/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001376static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1377{
1378 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001379
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001380 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1381 return -EFAULT;
1382
1383 fact.dev[DISK_NAME_LEN - 1] = '\0';
1384
1385 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1386 return -EINVAL;
1387
Matias Bjørlingbf643182016-02-04 15:13:27 +01001388 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001389}
1390
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001391static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1392{
1393 void __user *argp = (void __user *)arg;
1394
Johannes Thumshirn40f962d2018-03-30 00:05:07 +02001395 if (!capable(CAP_SYS_ADMIN))
1396 return -EPERM;
1397
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001398 switch (cmd) {
1399 case NVM_INFO:
1400 return nvm_ioctl_info(file, argp);
1401 case NVM_GET_DEVICES:
1402 return nvm_ioctl_get_devices(file, argp);
1403 case NVM_DEV_CREATE:
1404 return nvm_ioctl_dev_create(file, argp);
1405 case NVM_DEV_REMOVE:
1406 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001407 case NVM_DEV_INIT:
1408 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001409 case NVM_DEV_FACTORY:
1410 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001411 }
1412 return 0;
1413}
1414
1415static const struct file_operations _ctl_fops = {
1416 .open = nonseekable_open,
1417 .unlocked_ioctl = nvm_ctl_ioctl,
1418 .owner = THIS_MODULE,
1419 .llseek = noop_llseek,
1420};
1421
1422static struct miscdevice _nvm_misc = {
1423 .minor = MISC_DYNAMIC_MINOR,
1424 .name = "lightnvm",
1425 .nodename = "lightnvm/control",
1426 .fops = &_ctl_fops,
1427};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001428builtin_misc_device(_nvm_misc);