blob: 01d098fb96ac1cfcb0edc71e2228f9c06cf822e6 [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{
Geert Uytterhoeven2f5af4a2019-06-21 11:12:00 +0200481 struct nvm_target *t = NULL;
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
Hans Holmberg98d87f72019-07-31 11:41:33 +0200755static void nvm_sync_end_io(struct nvm_rq *rqd)
756{
757 struct completion *waiting = rqd->private;
758
759 complete(waiting);
760}
761
762static int nvm_submit_io_wait(struct nvm_dev *dev, struct nvm_rq *rqd)
763{
764 DECLARE_COMPLETION_ONSTACK(wait);
765 int ret = 0;
766
767 rqd->end_io = nvm_sync_end_io;
768 rqd->private = &wait;
769
770 ret = dev->ops->submit_io(dev, rqd);
771 if (ret)
772 return ret;
773
774 wait_for_completion_io(&wait);
775
776 return 0;
777}
778
Javier González1a94b2d2017-10-13 14:46:47 +0200779int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100780{
Javier González1a94b2d2017-10-13 14:46:47 +0200781 struct nvm_dev *dev = tgt_dev->parent;
782 int ret;
Javier González17912c42017-04-15 20:55:37 +0200783
Hans Holmberg98d87f72019-07-31 11:41:33 +0200784 if (!dev->ops->submit_io)
Javier González1a94b2d2017-10-13 14:46:47 +0200785 return -ENODEV;
786
787 nvm_rq_tgt_to_dev(tgt_dev, rqd);
788
789 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200790 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200791
Hans Holmberg98d87f72019-07-31 11:41:33 +0200792 ret = nvm_submit_io_wait(dev, rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200793
794 return ret;
Javier González17912c42017-04-15 20:55:37 +0200795}
Javier González1a94b2d2017-10-13 14:46:47 +0200796EXPORT_SYMBOL(nvm_submit_io_sync);
Javier González17912c42017-04-15 20:55:37 +0200797
Matias Bjørling06894ef2017-01-31 13:17:17 +0100798void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100799{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100800 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100801
802 /* Convert address space */
803 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100804 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100805
Matias Bjørling06894ef2017-01-31 13:17:17 +0100806 if (rqd->end_io)
807 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100808}
809EXPORT_SYMBOL(nvm_end_io);
810
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200811static int nvm_submit_io_sync_raw(struct nvm_dev *dev, struct nvm_rq *rqd)
812{
Hans Holmberg98d87f72019-07-31 11:41:33 +0200813 if (!dev->ops->submit_io)
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200814 return -ENODEV;
815
Hans Holmberg98d87f72019-07-31 11:41:33 +0200816 rqd->dev = NULL;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200817 rqd->flags = nvm_set_flags(&dev->geo, rqd);
818
Hans Holmberg98d87f72019-07-31 11:41:33 +0200819 return nvm_submit_io_wait(dev, rqd);
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200820}
821
822static int nvm_bb_chunk_sense(struct nvm_dev *dev, struct ppa_addr ppa)
823{
824 struct nvm_rq rqd = { NULL };
825 struct bio bio;
826 struct bio_vec bio_vec;
827 struct page *page;
828 int ret;
829
830 page = alloc_page(GFP_KERNEL);
831 if (!page)
832 return -ENOMEM;
833
834 bio_init(&bio, &bio_vec, 1);
835 bio_add_page(&bio, page, PAGE_SIZE, 0);
836 bio_set_op_attrs(&bio, REQ_OP_READ, 0);
837
838 rqd.bio = &bio;
839 rqd.opcode = NVM_OP_PREAD;
840 rqd.is_seq = 1;
841 rqd.nr_ppas = 1;
842 rqd.ppa_addr = generic_to_dev_addr(dev, ppa);
843
844 ret = nvm_submit_io_sync_raw(dev, &rqd);
845 if (ret)
846 return ret;
847
848 __free_page(page);
849
850 return rqd.error;
851}
852
Matias Bjørling22e8c972016-05-06 20:02:58 +0200853/*
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200854 * Scans a 1.2 chunk first and last page to determine if its state.
855 * If the chunk is found to be open, also scan it to update the write
856 * pointer.
Matias Bjørling22e8c972016-05-06 20:02:58 +0200857 */
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200858static int nvm_bb_chunk_scan(struct nvm_dev *dev, struct ppa_addr ppa,
859 struct nvm_chk_meta *meta)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200860{
Javier González8e79b5c2016-11-28 22:39:06 +0100861 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200862 int ret, pg, pl;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200863
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200864 /* sense first page */
865 ret = nvm_bb_chunk_sense(dev, ppa);
866 if (ret < 0) /* io error */
867 return ret;
868 else if (ret == 0) /* valid data */
869 meta->state = NVM_CHK_ST_OPEN;
870 else if (ret > 0) {
871 /*
872 * If empty page, the chunk is free, else it is an
873 * actual io error. In that case, mark it offline.
874 */
875 switch (ret) {
876 case NVM_RSP_ERR_EMPTYPAGE:
877 meta->state = NVM_CHK_ST_FREE;
878 return 0;
879 case NVM_RSP_ERR_FAILCRC:
880 case NVM_RSP_ERR_FAILECC:
881 case NVM_RSP_WARN_HIGHECC:
882 meta->state = NVM_CHK_ST_OPEN;
883 goto scan;
884 default:
885 return -ret; /* other io error */
886 }
887 }
888
889 /* sense last page */
890 ppa.g.pg = geo->num_pg - 1;
891 ppa.g.pl = geo->num_pln - 1;
892
893 ret = nvm_bb_chunk_sense(dev, ppa);
894 if (ret < 0) /* io error */
895 return ret;
896 else if (ret == 0) { /* Chunk fully written */
897 meta->state = NVM_CHK_ST_CLOSED;
898 meta->wp = geo->clba;
899 return 0;
900 } else if (ret > 0) {
901 switch (ret) {
902 case NVM_RSP_ERR_EMPTYPAGE:
903 case NVM_RSP_ERR_FAILCRC:
904 case NVM_RSP_ERR_FAILECC:
905 case NVM_RSP_WARN_HIGHECC:
906 meta->state = NVM_CHK_ST_OPEN;
907 break;
908 default:
909 return -ret; /* other io error */
910 }
911 }
912
913scan:
914 /*
915 * chunk is open, we scan sequentially to update the write pointer.
916 * We make the assumption that targets write data across all planes
917 * before moving to the next page.
918 */
919 for (pg = 0; pg < geo->num_pg; pg++) {
920 for (pl = 0; pl < geo->num_pln; pl++) {
921 ppa.g.pg = pg;
922 ppa.g.pl = pl;
923
924 ret = nvm_bb_chunk_sense(dev, ppa);
925 if (ret < 0) /* io error */
926 return ret;
927 else if (ret == 0) {
928 meta->wp += geo->ws_min;
929 } else if (ret > 0) {
930 switch (ret) {
931 case NVM_RSP_ERR_EMPTYPAGE:
932 return 0;
933 case NVM_RSP_ERR_FAILCRC:
934 case NVM_RSP_ERR_FAILECC:
935 case NVM_RSP_WARN_HIGHECC:
936 meta->wp += geo->ws_min;
937 break;
938 default:
939 return -ret; /* other io error */
940 }
941 }
942 }
943 }
944
945 return 0;
946}
947
948/*
949 * folds a bad block list from its plane representation to its
950 * chunk representation.
951 *
952 * If any of the planes status are bad or grown bad, the chunk is marked
953 * offline. If not bad, the first plane state acts as the chunk state.
954 */
955static int nvm_bb_to_chunk(struct nvm_dev *dev, struct ppa_addr ppa,
956 u8 *blks, int nr_blks, struct nvm_chk_meta *meta)
957{
958 struct nvm_geo *geo = &dev->geo;
959 int ret, blk, pl, offset, blktype;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200960
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200961 for (blk = 0; blk < geo->num_chk; blk++) {
962 offset = blk * geo->pln_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200963 blktype = blks[offset];
964
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200965 for (pl = 0; pl < geo->pln_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200966 if (blks[offset + pl] &
967 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
968 blktype = blks[offset + pl];
969 break;
970 }
971 }
972
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200973 ppa.g.blk = blk;
974
975 meta->wp = 0;
976 meta->type = NVM_CHK_TP_W_SEQ;
977 meta->wi = 0;
978 meta->slba = generic_to_dev_addr(dev, ppa).ppa;
979 meta->cnlb = dev->geo.clba;
980
981 if (blktype == NVM_BLK_T_FREE) {
982 ret = nvm_bb_chunk_scan(dev, ppa, meta);
983 if (ret)
984 return ret;
985 } else {
986 meta->state = NVM_CHK_ST_OFFLINE;
987 }
988
989 meta++;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200990 }
991
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200992 return 0;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200993}
Matias Bjørling22e8c972016-05-06 20:02:58 +0200994
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200995static int nvm_get_bb_meta(struct nvm_dev *dev, sector_t slba,
996 int nchks, struct nvm_chk_meta *meta)
997{
998 struct nvm_geo *geo = &dev->geo;
999 struct ppa_addr ppa;
1000 u8 *blks;
1001 int ch, lun, nr_blks;
Geert Uytterhoeven55e58c52018-12-11 20:16:08 +01001002 int ret = 0;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001003
1004 ppa.ppa = slba;
1005 ppa = dev_to_generic_addr(dev, ppa);
1006
1007 if (ppa.g.blk != 0)
1008 return -EINVAL;
1009
1010 if ((nchks % geo->num_chk) != 0)
1011 return -EINVAL;
1012
1013 nr_blks = geo->num_chk * geo->pln_mode;
1014
1015 blks = kmalloc(nr_blks, GFP_KERNEL);
1016 if (!blks)
1017 return -ENOMEM;
1018
1019 for (ch = ppa.g.ch; ch < geo->num_ch; ch++) {
1020 for (lun = ppa.g.lun; lun < geo->num_lun; lun++) {
1021 struct ppa_addr ppa_gen, ppa_dev;
1022
1023 if (!nchks)
1024 goto done;
1025
1026 ppa_gen.ppa = 0;
1027 ppa_gen.g.ch = ch;
1028 ppa_gen.g.lun = lun;
1029 ppa_dev = generic_to_dev_addr(dev, ppa_gen);
1030
1031 ret = dev->ops->get_bb_tbl(dev, ppa_dev, blks);
1032 if (ret)
1033 goto done;
1034
1035 ret = nvm_bb_to_chunk(dev, ppa_gen, blks, nr_blks,
1036 meta);
1037 if (ret)
1038 goto done;
1039
1040 meta += geo->num_chk;
1041 nchks -= geo->num_chk;
1042 }
1043 }
1044done:
1045 kfree(blks);
1046 return ret;
1047}
1048
1049int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
1050 int nchks, struct nvm_chk_meta *meta)
Javier González333ba052016-11-28 22:39:14 +01001051{
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001052 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +01001053
Matias Bjørlingdab8ee92017-01-31 13:17:14 +01001054 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001055
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001056 if (dev->geo.version == NVM_OCSSD_SPEC_12)
1057 return nvm_get_bb_meta(dev, (sector_t)ppa.ppa, nchks, meta);
1058
1059 return dev->ops->get_chk_meta(dev, (sector_t)ppa.ppa, nchks, meta);
Javier González333ba052016-11-28 22:39:14 +01001060}
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001061EXPORT_SYMBOL_GPL(nvm_get_chunk_meta);
1062
1063int nvm_set_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
1064 int nr_ppas, int type)
1065{
1066 struct nvm_dev *dev = tgt_dev->parent;
1067 struct nvm_rq rqd;
1068 int ret;
1069
1070 if (dev->geo.version == NVM_OCSSD_SPEC_20)
1071 return 0;
1072
1073 if (nr_ppas > NVM_MAX_VLBA) {
1074 pr_err("nvm: unable to update all blocks atomically\n");
1075 return -EINVAL;
1076 }
1077
1078 memset(&rqd, 0, sizeof(struct nvm_rq));
1079
1080 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
1081 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
1082
1083 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
1084 nvm_free_rqd_ppalist(tgt_dev, &rqd);
1085 if (ret)
1086 return -EINVAL;
1087
1088 return 0;
1089}
1090EXPORT_SYMBOL_GPL(nvm_set_chunk_meta);
Javier González333ba052016-11-28 22:39:14 +01001091
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001092static int nvm_core_init(struct nvm_dev *dev)
1093{
Javier González8e79b5c2016-11-28 22:39:06 +01001094 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001095 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001096
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001097 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +01001098 sizeof(unsigned long), GFP_KERNEL);
1099 if (!dev->lun_map)
1100 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001101
Matias Bjørlingade69e22017-01-31 13:17:09 +01001102 INIT_LIST_HEAD(&dev->area_list);
1103 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +01001104 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001105 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001106
Matias Bjørlingade69e22017-01-31 13:17:09 +01001107 ret = nvm_register_map(dev);
1108 if (ret)
1109 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001110
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001111 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001112err_fmtype:
1113 kfree(dev->lun_map);
1114 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001115}
1116
Igor Konopkoe69397e2019-05-04 20:38:08 +02001117static void nvm_free(struct kref *ref)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001118{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001119 struct nvm_dev *dev = container_of(ref, struct nvm_dev, ref);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001120
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001121 if (dev->dma_pool)
1122 dev->ops->destroy_dma_pool(dev->dma_pool);
1123
Igor Konopkoe69397e2019-05-04 20:38:08 +02001124 if (dev->rmap)
1125 nvm_unregister_map(dev);
1126
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001127 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001128 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001129}
1130
1131static int nvm_init(struct nvm_dev *dev)
1132{
Javier González8e79b5c2016-11-28 22:39:06 +01001133 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001134 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001135
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001136 if (dev->ops->identity(dev)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001137 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001138 goto err;
1139 }
1140
Javier González3cb98f82018-03-30 00:05:11 +02001141 pr_debug("nvm: ver:%u.%u nvm_vendor:%x\n",
1142 geo->major_ver_id, geo->minor_ver_id,
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001143 geo->vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001144
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001145 ret = nvm_core_init(dev);
1146 if (ret) {
1147 pr_err("nvm: could not initialize core structures.\n");
1148 goto err;
1149 }
1150
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001151 pr_info("nvm: registered %s [%u/%u/%u/%u/%u]\n",
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001152 dev->name, dev->geo.ws_min, dev->geo.ws_opt,
1153 dev->geo.num_chk, dev->geo.all_luns,
1154 dev->geo.num_ch);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001155 return 0;
1156err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001157 pr_err("nvm: failed to initialize nvm\n");
1158 return ret;
1159}
1160
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001161struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001162{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001163 struct nvm_dev *dev;
1164
1165 dev = kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1166 if (dev)
1167 kref_init(&dev->ref);
1168
1169 return dev;
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001170}
1171EXPORT_SYMBOL(nvm_alloc_dev);
1172
1173int nvm_register(struct nvm_dev *dev)
1174{
Igor Konopko24828d02018-12-11 20:16:24 +01001175 int ret, exp_pool_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001176
Igor Konopkoe69397e2019-05-04 20:38:08 +02001177 if (!dev->q || !dev->ops) {
1178 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001179 return -EINVAL;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001180 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001181
Matias Bjørling85136c02018-12-11 20:16:20 +01001182 ret = nvm_init(dev);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001183 if (ret) {
1184 kref_put(&dev->ref, nvm_free);
Matias Bjørling85136c02018-12-11 20:16:20 +01001185 return ret;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001186 }
Matias Bjørling85136c02018-12-11 20:16:20 +01001187
Igor Konopko24828d02018-12-11 20:16:24 +01001188 exp_pool_size = max_t(int, PAGE_SIZE,
1189 (NVM_MAX_VLBA * (sizeof(u64) + dev->geo.sos)));
1190 exp_pool_size = round_up(exp_pool_size, PAGE_SIZE);
1191
1192 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist",
1193 exp_pool_size);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001194 if (!dev->dma_pool) {
1195 pr_err("nvm: could not create dma pool\n");
Igor Konopkoe69397e2019-05-04 20:38:08 +02001196 kref_put(&dev->ref, nvm_free);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001197 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001198 }
1199
Matias Bjørling762796b2015-12-06 11:25:49 +01001200 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001201 down_write(&nvm_lock);
1202 list_add(&dev->devices, &nvm_devices);
1203 up_write(&nvm_lock);
1204
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001205 return 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001206}
1207EXPORT_SYMBOL(nvm_register);
1208
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001209void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001210{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001211 struct nvm_target *t, *tmp;
1212
1213 mutex_lock(&dev->mlock);
1214 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1215 if (t->dev->parent != dev)
1216 continue;
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001217 __nvm_remove_target(t, false);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001218 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001219 }
1220 mutex_unlock(&dev->mlock);
1221
Wenwei Taod0a712c2015-11-28 16:49:28 +01001222 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001223 list_del(&dev->devices);
1224 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001225
Igor Konopkoe69397e2019-05-04 20:38:08 +02001226 kref_put(&dev->ref, nvm_free);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001227}
1228EXPORT_SYMBOL(nvm_unregister);
1229
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001230static int __nvm_configure_create(struct nvm_ioctl_create *create)
1231{
1232 struct nvm_dev *dev;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001233 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001234
Wenwei Taod0a712c2015-11-28 16:49:28 +01001235 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001236 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001237 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001238
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001239 if (!dev) {
1240 pr_err("nvm: device not found\n");
1241 return -EINVAL;
1242 }
1243
Igor Konopkoe69397e2019-05-04 20:38:08 +02001244 kref_get(&dev->ref);
1245 ret = nvm_create_tgt(dev, create);
1246 if (ret)
1247 kref_put(&dev->ref, nvm_free);
1248
1249 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001250}
1251
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001252static long nvm_ioctl_info(struct file *file, void __user *arg)
1253{
1254 struct nvm_ioctl_info *info;
1255 struct nvm_tgt_type *tt;
1256 int tgt_iter = 0;
1257
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001258 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1259 if (IS_ERR(info))
1260 return -EFAULT;
1261
1262 info->version[0] = NVM_VERSION_MAJOR;
1263 info->version[1] = NVM_VERSION_MINOR;
1264 info->version[2] = NVM_VERSION_PATCH;
1265
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001266 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001267 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001268 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1269
1270 tgt->version[0] = tt->version[0];
1271 tgt->version[1] = tt->version[1];
1272 tgt->version[2] = tt->version[2];
1273 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1274
1275 tgt_iter++;
1276 }
1277
1278 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001279 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001280
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001281 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1282 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001283 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001284 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001285
1286 kfree(info);
1287 return 0;
1288}
1289
1290static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1291{
1292 struct nvm_ioctl_get_devices *devices;
1293 struct nvm_dev *dev;
1294 int i = 0;
1295
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001296 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1297 if (!devices)
1298 return -ENOMEM;
1299
1300 down_write(&nvm_lock);
1301 list_for_each_entry(dev, &nvm_devices, devices) {
1302 struct nvm_ioctl_device_info *info = &devices->info[i];
1303
Javier González6eb08242017-04-15 20:55:49 +02001304 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001305
Matias Bjørlingade69e22017-01-31 13:17:09 +01001306 /* kept for compatibility */
1307 info->bmversion[0] = 1;
1308 info->bmversion[1] = 0;
1309 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001310 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001311 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001312
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001313 if (i > 31) {
1314 pr_err("nvm: max 31 devices can be reported.\n");
1315 break;
1316 }
1317 }
1318 up_write(&nvm_lock);
1319
1320 devices->nr_devices = i;
1321
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001322 if (copy_to_user(arg, devices,
1323 sizeof(struct nvm_ioctl_get_devices))) {
1324 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001325 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001326 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001327
1328 kfree(devices);
1329 return 0;
1330}
1331
1332static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1333{
1334 struct nvm_ioctl_create create;
1335
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001336 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1337 return -EFAULT;
1338
Javier Gonzáleze5392732018-01-05 14:16:14 +01001339 if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED &&
1340 create.conf.e.rsv != 0) {
1341 pr_err("nvm: reserved config field in use\n");
1342 return -EINVAL;
1343 }
1344
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001345 create.dev[DISK_NAME_LEN - 1] = '\0';
1346 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1347 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1348
1349 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001350 __u32 flags = create.flags;
1351
1352 /* Check for valid flags */
1353 if (flags & NVM_TARGET_FACTORY)
1354 flags &= ~NVM_TARGET_FACTORY;
1355
1356 if (flags) {
1357 pr_err("nvm: flag not supported\n");
1358 return -EINVAL;
1359 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001360 }
1361
1362 return __nvm_configure_create(&create);
1363}
1364
1365static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1366{
1367 struct nvm_ioctl_remove remove;
1368
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001369 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1370 return -EFAULT;
1371
1372 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1373
1374 if (remove.flags != 0) {
1375 pr_err("nvm: no flags supported\n");
1376 return -EINVAL;
1377 }
1378
Igor Konopko843f2ed2019-05-04 20:38:09 +02001379 return nvm_remove_tgt(&remove);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001380}
1381
Matias Bjørlingade69e22017-01-31 13:17:09 +01001382/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001383static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1384{
1385 struct nvm_ioctl_dev_init init;
1386
Matias Bjørling55696152016-01-12 07:49:37 +01001387 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1388 return -EFAULT;
1389
1390 if (init.flags != 0) {
1391 pr_err("nvm: no flags supported\n");
1392 return -EINVAL;
1393 }
1394
Matias Bjørlingade69e22017-01-31 13:17:09 +01001395 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001396}
1397
Matias Bjørlingade69e22017-01-31 13:17:09 +01001398/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001399static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1400{
1401 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001402
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001403 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1404 return -EFAULT;
1405
1406 fact.dev[DISK_NAME_LEN - 1] = '\0';
1407
1408 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1409 return -EINVAL;
1410
Matias Bjørlingbf643182016-02-04 15:13:27 +01001411 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001412}
1413
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001414static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1415{
1416 void __user *argp = (void __user *)arg;
1417
Johannes Thumshirn40f962d2018-03-30 00:05:07 +02001418 if (!capable(CAP_SYS_ADMIN))
1419 return -EPERM;
1420
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001421 switch (cmd) {
1422 case NVM_INFO:
1423 return nvm_ioctl_info(file, argp);
1424 case NVM_GET_DEVICES:
1425 return nvm_ioctl_get_devices(file, argp);
1426 case NVM_DEV_CREATE:
1427 return nvm_ioctl_dev_create(file, argp);
1428 case NVM_DEV_REMOVE:
1429 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001430 case NVM_DEV_INIT:
1431 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001432 case NVM_DEV_FACTORY:
1433 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001434 }
1435 return 0;
1436}
1437
1438static const struct file_operations _ctl_fops = {
1439 .open = nonseekable_open,
1440 .unlocked_ioctl = nvm_ctl_ioctl,
1441 .owner = THIS_MODULE,
1442 .llseek = noop_llseek,
1443};
1444
1445static struct miscdevice _nvm_misc = {
1446 .minor = MISC_DYNAMIC_MINOR,
1447 .name = "lightnvm",
1448 .nodename = "lightnvm/control",
1449 .fops = &_ctl_fops,
1450};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001451builtin_misc_device(_nvm_misc);