blob: c1bcac71008c67374ad76e9ea455cc220f8bba31 [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
Minwoo Im43db0592019-09-05 21:04:32 +02007#define pr_fmt(fmt) "nvm: " fmt
8
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01009#include <linux/list.h>
10#include <linux/types.h>
11#include <linux/sem.h>
12#include <linux/bitmap.h>
Rakesh Pandit90014822017-10-13 14:45:50 +020013#include <linux/module.h>
Paul Gortmaker389b2a12016-10-29 16:38:41 -040014#include <linux/moduleparam.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010015#include <linux/miscdevice.h>
16#include <linux/lightnvm.h>
Matias Bjørling912761622016-01-12 07:49:21 +010017#include <linux/sched/sysctl.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010018
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020019static LIST_HEAD(nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +020020static DECLARE_RWSEM(nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010021static LIST_HEAD(nvm_devices);
22static DECLARE_RWSEM(nvm_lock);
23
Matias Bjørlingade69e22017-01-31 13:17:09 +010024/* Map between virtual and physical channel and lun */
25struct nvm_ch_map {
26 int ch_off;
Javier Gonzáleza40afad2018-03-30 00:05:14 +020027 int num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +010028 int *lun_offs;
29};
30
31struct nvm_dev_map {
32 struct nvm_ch_map *chnls;
Javier Gonzáleza40afad2018-03-30 00:05:14 +020033 int num_ch;
Matias Bjørlingade69e22017-01-31 13:17:09 +010034};
35
Igor Konopkoe69397e2019-05-04 20:38:08 +020036static void nvm_free(struct kref *ref);
37
Matias Bjørlingade69e22017-01-31 13:17:09 +010038static struct nvm_target *nvm_find_target(struct nvm_dev *dev, const char *name)
39{
40 struct nvm_target *tgt;
41
42 list_for_each_entry(tgt, &dev->targets, list)
43 if (!strcmp(name, tgt->disk->disk_name))
44 return tgt;
45
46 return NULL;
47}
48
Javier Gonzálezbd77b232018-01-05 14:16:05 +010049static bool nvm_target_exists(const char *name)
50{
51 struct nvm_dev *dev;
52 struct nvm_target *tgt;
53 bool ret = false;
54
55 down_write(&nvm_lock);
56 list_for_each_entry(dev, &nvm_devices, devices) {
57 mutex_lock(&dev->mlock);
58 list_for_each_entry(tgt, &dev->targets, list) {
59 if (!strcmp(name, tgt->disk->disk_name)) {
60 ret = true;
61 mutex_unlock(&dev->mlock);
62 goto out;
63 }
64 }
65 mutex_unlock(&dev->mlock);
66 }
67
68out:
69 up_write(&nvm_lock);
70 return ret;
71}
72
Matias Bjørlingade69e22017-01-31 13:17:09 +010073static int nvm_reserve_luns(struct nvm_dev *dev, int lun_begin, int lun_end)
74{
75 int i;
76
77 for (i = lun_begin; i <= lun_end; i++) {
78 if (test_and_set_bit(i, dev->lun_map)) {
Minwoo Im43db0592019-09-05 21:04:32 +020079 pr_err("lun %d already allocated\n", i);
Matias Bjørlingade69e22017-01-31 13:17:09 +010080 goto err;
81 }
82 }
83
84 return 0;
85err:
Javier González507f7d62017-05-03 11:19:05 +020086 while (--i >= lun_begin)
Matias Bjørlingade69e22017-01-31 13:17:09 +010087 clear_bit(i, dev->lun_map);
88
89 return -EBUSY;
90}
91
92static void nvm_release_luns_err(struct nvm_dev *dev, int lun_begin,
93 int lun_end)
94{
95 int i;
96
97 for (i = lun_begin; i <= lun_end; i++)
98 WARN_ON(!test_and_clear_bit(i, dev->lun_map));
99}
100
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200101static void nvm_remove_tgt_dev(struct nvm_tgt_dev *tgt_dev, int clear)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100102{
103 struct nvm_dev *dev = tgt_dev->parent;
104 struct nvm_dev_map *dev_map = tgt_dev->map;
105 int i, j;
106
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200107 for (i = 0; i < dev_map->num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100108 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
109 int *lun_offs = ch_map->lun_offs;
110 int ch = i + ch_map->ch_off;
111
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200112 if (clear) {
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200113 for (j = 0; j < ch_map->num_lun; j++) {
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200114 int lun = j + lun_offs[j];
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200115 int lunid = (ch * dev->geo.num_lun) + lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100116
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200117 WARN_ON(!test_and_clear_bit(lunid,
118 dev->lun_map));
119 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100120 }
121
122 kfree(ch_map->lun_offs);
123 }
124
125 kfree(dev_map->chnls);
126 kfree(dev_map);
127
128 kfree(tgt_dev->luns);
129 kfree(tgt_dev);
130}
131
132static struct nvm_tgt_dev *nvm_create_tgt_dev(struct nvm_dev *dev,
Javier Gonzáleze5392732018-01-05 14:16:14 +0100133 u16 lun_begin, u16 lun_end,
134 u16 op)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100135{
136 struct nvm_tgt_dev *tgt_dev = NULL;
137 struct nvm_dev_map *dev_rmap = dev->rmap;
138 struct nvm_dev_map *dev_map;
139 struct ppa_addr *luns;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200140 int num_lun = lun_end - lun_begin + 1;
141 int luns_left = num_lun;
142 int num_ch = num_lun / dev->geo.num_lun;
143 int num_ch_mod = num_lun % dev->geo.num_lun;
144 int bch = lun_begin / dev->geo.num_lun;
145 int blun = lun_begin % dev->geo.num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100146 int lunid = 0;
147 int lun_balanced = 1;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200148 int sec_per_lun, prev_num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100149 int i, j;
150
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200151 num_ch = (num_ch_mod == 0) ? num_ch : num_ch + 1;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100152
153 dev_map = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
154 if (!dev_map)
155 goto err_dev;
156
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200157 dev_map->chnls = kcalloc(num_ch, sizeof(struct nvm_ch_map), GFP_KERNEL);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100158 if (!dev_map->chnls)
159 goto err_chnls;
160
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200161 luns = kcalloc(num_lun, sizeof(struct ppa_addr), GFP_KERNEL);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100162 if (!luns)
163 goto err_luns;
164
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200165 prev_num_lun = (luns_left > dev->geo.num_lun) ?
166 dev->geo.num_lun : luns_left;
167 for (i = 0; i < num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100168 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[i + bch];
169 int *lun_roffs = ch_rmap->lun_offs;
170 struct nvm_ch_map *ch_map = &dev_map->chnls[i];
171 int *lun_offs;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200172 int luns_in_chnl = (luns_left > dev->geo.num_lun) ?
173 dev->geo.num_lun : luns_left;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100174
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200175 if (lun_balanced && prev_num_lun != luns_in_chnl)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100176 lun_balanced = 0;
177
178 ch_map->ch_off = ch_rmap->ch_off = bch;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200179 ch_map->num_lun = luns_in_chnl;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100180
181 lun_offs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
182 if (!lun_offs)
183 goto err_ch;
184
185 for (j = 0; j < luns_in_chnl; j++) {
186 luns[lunid].ppa = 0;
Javier González69471512018-03-30 00:05:15 +0200187 luns[lunid].a.ch = i;
188 luns[lunid++].a.lun = j;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100189
190 lun_offs[j] = blun;
191 lun_roffs[j + blun] = blun;
192 }
193
194 ch_map->lun_offs = lun_offs;
195
196 /* when starting a new channel, lun offset is reset */
197 blun = 0;
198 luns_left -= luns_in_chnl;
199 }
200
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200201 dev_map->num_ch = num_ch;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100202
203 tgt_dev = kmalloc(sizeof(struct nvm_tgt_dev), GFP_KERNEL);
204 if (!tgt_dev)
205 goto err_ch;
206
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200207 /* Inherit device geometry from parent */
Matias Bjørlingade69e22017-01-31 13:17:09 +0100208 memcpy(&tgt_dev->geo, &dev->geo, sizeof(struct nvm_geo));
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200209
Matias Bjørlingade69e22017-01-31 13:17:09 +0100210 /* Target device only owns a portion of the physical device */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200211 tgt_dev->geo.num_ch = num_ch;
212 tgt_dev->geo.num_lun = (lun_balanced) ? prev_num_lun : -1;
213 tgt_dev->geo.all_luns = num_lun;
214 tgt_dev->geo.all_chunks = num_lun * dev->geo.num_chk;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200215
Javier Gonzáleze5392732018-01-05 14:16:14 +0100216 tgt_dev->geo.op = op;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200217
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200218 sec_per_lun = dev->geo.clba * dev->geo.num_chk;
219 tgt_dev->geo.total_secs = num_lun * sec_per_lun;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200220
Matias Bjørlingade69e22017-01-31 13:17:09 +0100221 tgt_dev->q = dev->q;
222 tgt_dev->map = dev_map;
223 tgt_dev->luns = luns;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100224 tgt_dev->parent = dev;
225
226 return tgt_dev;
227err_ch:
Javier González507f7d62017-05-03 11:19:05 +0200228 while (--i >= 0)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100229 kfree(dev_map->chnls[i].lun_offs);
230 kfree(luns);
231err_luns:
232 kfree(dev_map->chnls);
233err_chnls:
234 kfree(dev_map);
235err_dev:
236 return tgt_dev;
237}
238
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100239static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200240{
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100241 struct nvm_tgt_type *tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200242
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100243 list_for_each_entry(tt, &nvm_tgt_types, list)
244 if (!strcmp(name, tt->name))
245 return tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200246
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100247 return NULL;
248}
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200249
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100250static struct nvm_tgt_type *nvm_find_target_type(const char *name)
251{
252 struct nvm_tgt_type *tt;
253
254 down_write(&nvm_tgtt_lock);
255 tt = __nvm_find_target_type(name);
256 up_write(&nvm_tgtt_lock);
257
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200258 return tt;
259}
260
Javier Gonzáleze5392732018-01-05 14:16:14 +0100261static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin,
262 int lun_end)
263{
264 if (lun_begin > lun_end || lun_end >= geo->all_luns) {
Minwoo Im43db0592019-09-05 21:04:32 +0200265 pr_err("lun out of bound (%u:%u > %u)\n",
Javier Gonzáleze5392732018-01-05 14:16:14 +0100266 lun_begin, lun_end, geo->all_luns - 1);
267 return -EINVAL;
268 }
269
270 return 0;
271}
272
273static int __nvm_config_simple(struct nvm_dev *dev,
274 struct nvm_ioctl_create_simple *s)
275{
276 struct nvm_geo *geo = &dev->geo;
277
278 if (s->lun_begin == -1 && s->lun_end == -1) {
279 s->lun_begin = 0;
280 s->lun_end = geo->all_luns - 1;
281 }
282
283 return nvm_config_check_luns(geo, s->lun_begin, s->lun_end);
284}
285
286static int __nvm_config_extended(struct nvm_dev *dev,
287 struct nvm_ioctl_create_extended *e)
288{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100289 if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) {
290 e->lun_begin = 0;
291 e->lun_end = dev->geo.all_luns - 1;
292 }
293
294 /* op not set falls into target's default */
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200295 if (e->op == 0xFFFF) {
Javier Gonzáleze5392732018-01-05 14:16:14 +0100296 e->op = NVM_TARGET_DEFAULT_OP;
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200297 } else if (e->op < NVM_TARGET_MIN_OP || e->op > NVM_TARGET_MAX_OP) {
Minwoo Im43db0592019-09-05 21:04:32 +0200298 pr_err("invalid over provisioning value\n");
Javier Gonzáleze5392732018-01-05 14:16:14 +0100299 return -EINVAL;
300 }
301
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200302 return nvm_config_check_luns(&dev->geo, e->lun_begin, e->lun_end);
Javier Gonzáleze5392732018-01-05 14:16:14 +0100303}
304
Matias Bjørlingade69e22017-01-31 13:17:09 +0100305static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
306{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100307 struct nvm_ioctl_create_extended e;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100308 struct request_queue *tqueue;
309 struct gendisk *tdisk;
310 struct nvm_tgt_type *tt;
311 struct nvm_target *t;
312 struct nvm_tgt_dev *tgt_dev;
313 void *targetdata;
Igor Konopkoa14669e2019-05-04 20:37:55 +0200314 unsigned int mdts;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300315 int ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100316
Javier Gonzáleze5392732018-01-05 14:16:14 +0100317 switch (create->conf.type) {
318 case NVM_CONFIG_TYPE_SIMPLE:
319 ret = __nvm_config_simple(dev, &create->conf.s);
320 if (ret)
321 return ret;
322
323 e.lun_begin = create->conf.s.lun_begin;
324 e.lun_end = create->conf.s.lun_end;
325 e.op = NVM_TARGET_DEFAULT_OP;
326 break;
327 case NVM_CONFIG_TYPE_EXTENDED:
328 ret = __nvm_config_extended(dev, &create->conf.e);
329 if (ret)
330 return ret;
331
332 e = create->conf.e;
333 break;
334 default:
Minwoo Im43db0592019-09-05 21:04:32 +0200335 pr_err("config type not valid\n");
Javier Gonzáleze5392732018-01-05 14:16:14 +0100336 return -EINVAL;
337 }
338
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100339 tt = nvm_find_target_type(create->tgttype);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100340 if (!tt) {
Minwoo Im43db0592019-09-05 21:04:32 +0200341 pr_err("target type %s not found\n", create->tgttype);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100342 return -EINVAL;
343 }
344
Matias Bjørling656e33c2018-10-09 13:11:34 +0200345 if ((tt->flags & NVM_TGT_F_HOST_L2P) != (dev->geo.dom & NVM_RSP_L2P)) {
Minwoo Im43db0592019-09-05 21:04:32 +0200346 pr_err("device is incompatible with target L2P type.\n");
Matias Bjørling656e33c2018-10-09 13:11:34 +0200347 return -EINVAL;
348 }
349
Javier Gonzálezbd77b232018-01-05 14:16:05 +0100350 if (nvm_target_exists(create->tgtname)) {
Minwoo Im43db0592019-09-05 21:04:32 +0200351 pr_err("target name already exists (%s)\n",
Javier Gonzálezbd77b232018-01-05 14:16:05 +0100352 create->tgtname);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100353 return -EINVAL;
354 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100355
Javier Gonzáleze5392732018-01-05 14:16:14 +0100356 ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end);
Rakesh Pandit12e9a6d62017-06-27 14:55:33 +0300357 if (ret)
358 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100359
360 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300361 if (!t) {
362 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100363 goto err_reserve;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300364 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100365
Javier Gonzáleze5392732018-01-05 14:16:14 +0100366 tgt_dev = nvm_create_tgt_dev(dev, e.lun_begin, e.lun_end, e.op);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100367 if (!tgt_dev) {
Minwoo Im43db0592019-09-05 21:04:32 +0200368 pr_err("could not create target device\n");
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300369 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100370 goto err_t;
371 }
372
Matias Bjørlingade69e22017-01-31 13:17:09 +0100373 tdisk = alloc_disk(0);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300374 if (!tdisk) {
375 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200376 goto err_dev;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300377 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200378
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200379 tqueue = blk_alloc_queue(dev->q->node);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300380 if (!tqueue) {
381 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200382 goto err_disk;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300383 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100384
Javier González6eb08242017-04-15 20:55:49 +0200385 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100386 tdisk->flags = GENHD_FL_EXT_DEVT;
387 tdisk->major = 0;
388 tdisk->first_minor = 0;
Christoph Hellwigc62b37d2020-07-01 10:59:43 +0200389 tdisk->fops = tt->bops;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100390 tdisk->queue = tqueue;
391
Javier González4af3f752017-04-15 20:55:45 +0200392 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300393 if (IS_ERR(targetdata)) {
394 ret = PTR_ERR(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100395 goto err_init;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300396 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100397
398 tdisk->private_data = targetdata;
399 tqueue->queuedata = targetdata;
400
Igor Konopkoa14669e2019-05-04 20:37:55 +0200401 mdts = (dev->geo.csecs >> 9) * NVM_MAX_VLBA;
402 if (dev->geo.mdts) {
403 mdts = min_t(u32, dev->geo.mdts,
404 (dev->geo.csecs >> 9) * NVM_MAX_VLBA);
405 }
406 blk_queue_max_hw_sectors(tqueue, mdts);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100407
408 set_capacity(tdisk, tt->capacity(targetdata));
409 add_disk(tdisk);
410
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300411 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
412 ret = -ENOMEM;
Javier González9a69b0e2017-01-31 13:17:20 +0100413 goto err_sysfs;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300414 }
Javier González9a69b0e2017-01-31 13:17:20 +0100415
Matias Bjørlingade69e22017-01-31 13:17:09 +0100416 t->type = tt;
417 t->disk = tdisk;
418 t->dev = tgt_dev;
419
420 mutex_lock(&dev->mlock);
421 list_add_tail(&t->list, &dev->targets);
422 mutex_unlock(&dev->mlock);
423
Rakesh Pandit90014822017-10-13 14:45:50 +0200424 __module_get(tt->owner);
425
Matias Bjørlingade69e22017-01-31 13:17:09 +0100426 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100427err_sysfs:
428 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200429 tt->exit(targetdata, true);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100430err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100431 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600432 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200433err_disk:
434 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100435err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200436 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100437err_t:
438 kfree(t);
439err_reserve:
Javier Gonzáleze5392732018-01-05 14:16:14 +0100440 nvm_release_luns_err(dev, e.lun_begin, e.lun_end);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300441 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100442}
443
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200444static void __nvm_remove_target(struct nvm_target *t, bool graceful)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100445{
446 struct nvm_tgt_type *tt = t->type;
447 struct gendisk *tdisk = t->disk;
448 struct request_queue *q = tdisk->queue;
449
450 del_gendisk(tdisk);
451 blk_cleanup_queue(q);
452
Javier González9a69b0e2017-01-31 13:17:20 +0100453 if (tt->sysfs_exit)
454 tt->sysfs_exit(tdisk);
455
Matias Bjørlingade69e22017-01-31 13:17:09 +0100456 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200457 tt->exit(tdisk->private_data, graceful);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100458
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200459 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100460 put_disk(tdisk);
Rakesh Pandit90014822017-10-13 14:45:50 +0200461 module_put(t->type->owner);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100462
463 list_del(&t->list);
464 kfree(t);
465}
466
467/**
468 * nvm_remove_tgt - Removes a target from the media manager
Matias Bjørlingade69e22017-01-31 13:17:09 +0100469 * @remove: ioctl structure with target name to remove.
470 *
471 * Returns:
472 * 0: on success
473 * 1: on not found
474 * <0: on error
475 */
Igor Konopko843f2ed2019-05-04 20:38:09 +0200476static int nvm_remove_tgt(struct nvm_ioctl_remove *remove)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100477{
Geert Uytterhoeven2f5af4a2019-06-21 11:12:00 +0200478 struct nvm_target *t = NULL;
Igor Konopko843f2ed2019-05-04 20:38:09 +0200479 struct nvm_dev *dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100480
Igor Konopko843f2ed2019-05-04 20:38:09 +0200481 down_read(&nvm_lock);
482 list_for_each_entry(dev, &nvm_devices, devices) {
483 mutex_lock(&dev->mlock);
484 t = nvm_find_target(dev, remove->tgtname);
485 if (t) {
486 mutex_unlock(&dev->mlock);
487 break;
488 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100489 mutex_unlock(&dev->mlock);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100490 }
Igor Konopko843f2ed2019-05-04 20:38:09 +0200491 up_read(&nvm_lock);
492
Minwoo Im362cd2b2019-09-05 21:04:33 +0200493 if (!t) {
494 pr_err("failed to remove target %s\n",
495 remove->tgtname);
Igor Konopko843f2ed2019-05-04 20:38:09 +0200496 return 1;
Minwoo Im362cd2b2019-09-05 21:04:33 +0200497 }
Igor Konopko843f2ed2019-05-04 20:38:09 +0200498
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) {
Minwoo Im43db0592019-09-05 21:04:32 +0200689 pr_err("failed to allocate dma memory\n");
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200690 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
Hans Holmberg48e5da722019-07-31 11:41:34 +0200734int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd, void *buf)
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 */
Hans Holmberg48e5da722019-07-31 11:41:34 +0200748 ret = dev->ops->submit_io(dev, rqd, buf);
Javier González3e505af2017-06-26 11:57:10 +0200749 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
Hans Holmberg48e5da722019-07-31 11:41:34 +0200762static int nvm_submit_io_wait(struct nvm_dev *dev, struct nvm_rq *rqd,
763 void *buf)
Hans Holmberg98d87f72019-07-31 11:41:33 +0200764{
765 DECLARE_COMPLETION_ONSTACK(wait);
766 int ret = 0;
767
768 rqd->end_io = nvm_sync_end_io;
769 rqd->private = &wait;
770
Hans Holmberg48e5da722019-07-31 11:41:34 +0200771 ret = dev->ops->submit_io(dev, rqd, buf);
Hans Holmberg98d87f72019-07-31 11:41:33 +0200772 if (ret)
773 return ret;
774
775 wait_for_completion_io(&wait);
776
777 return 0;
778}
779
Hans Holmberg48e5da722019-07-31 11:41:34 +0200780int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
781 void *buf)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100782{
Javier González1a94b2d2017-10-13 14:46:47 +0200783 struct nvm_dev *dev = tgt_dev->parent;
784 int ret;
Javier González17912c42017-04-15 20:55:37 +0200785
Hans Holmberg98d87f72019-07-31 11:41:33 +0200786 if (!dev->ops->submit_io)
Javier González1a94b2d2017-10-13 14:46:47 +0200787 return -ENODEV;
788
789 nvm_rq_tgt_to_dev(tgt_dev, rqd);
790
791 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200792 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200793
Hans Holmberg48e5da722019-07-31 11:41:34 +0200794 ret = nvm_submit_io_wait(dev, rqd, buf);
Javier González1a94b2d2017-10-13 14:46:47 +0200795
796 return ret;
Javier González17912c42017-04-15 20:55:37 +0200797}
Javier González1a94b2d2017-10-13 14:46:47 +0200798EXPORT_SYMBOL(nvm_submit_io_sync);
Javier González17912c42017-04-15 20:55:37 +0200799
Matias Bjørling06894ef2017-01-31 13:17:17 +0100800void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100801{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100802 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100803
804 /* Convert address space */
805 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100806 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100807
Matias Bjørling06894ef2017-01-31 13:17:17 +0100808 if (rqd->end_io)
809 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100810}
811EXPORT_SYMBOL(nvm_end_io);
812
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200813static int nvm_submit_io_sync_raw(struct nvm_dev *dev, struct nvm_rq *rqd)
814{
Hans Holmberg98d87f72019-07-31 11:41:33 +0200815 if (!dev->ops->submit_io)
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200816 return -ENODEV;
817
Hans Holmberg98d87f72019-07-31 11:41:33 +0200818 rqd->dev = NULL;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200819 rqd->flags = nvm_set_flags(&dev->geo, rqd);
820
Hans Holmberg48e5da722019-07-31 11:41:34 +0200821 return nvm_submit_io_wait(dev, rqd, NULL);
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200822}
823
824static int nvm_bb_chunk_sense(struct nvm_dev *dev, struct ppa_addr ppa)
825{
826 struct nvm_rq rqd = { NULL };
827 struct bio bio;
828 struct bio_vec bio_vec;
829 struct page *page;
830 int ret;
831
832 page = alloc_page(GFP_KERNEL);
833 if (!page)
834 return -ENOMEM;
835
836 bio_init(&bio, &bio_vec, 1);
837 bio_add_page(&bio, page, PAGE_SIZE, 0);
838 bio_set_op_attrs(&bio, REQ_OP_READ, 0);
839
840 rqd.bio = &bio;
841 rqd.opcode = NVM_OP_PREAD;
842 rqd.is_seq = 1;
843 rqd.nr_ppas = 1;
844 rqd.ppa_addr = generic_to_dev_addr(dev, ppa);
845
846 ret = nvm_submit_io_sync_raw(dev, &rqd);
847 if (ret)
848 return ret;
849
850 __free_page(page);
851
852 return rqd.error;
853}
854
Matias Bjørling22e8c972016-05-06 20:02:58 +0200855/*
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200856 * Scans a 1.2 chunk first and last page to determine if its state.
857 * If the chunk is found to be open, also scan it to update the write
858 * pointer.
Matias Bjørling22e8c972016-05-06 20:02:58 +0200859 */
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200860static int nvm_bb_chunk_scan(struct nvm_dev *dev, struct ppa_addr ppa,
861 struct nvm_chk_meta *meta)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200862{
Javier González8e79b5c2016-11-28 22:39:06 +0100863 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200864 int ret, pg, pl;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200865
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200866 /* sense first page */
867 ret = nvm_bb_chunk_sense(dev, ppa);
868 if (ret < 0) /* io error */
869 return ret;
870 else if (ret == 0) /* valid data */
871 meta->state = NVM_CHK_ST_OPEN;
872 else if (ret > 0) {
873 /*
874 * If empty page, the chunk is free, else it is an
875 * actual io error. In that case, mark it offline.
876 */
877 switch (ret) {
878 case NVM_RSP_ERR_EMPTYPAGE:
879 meta->state = NVM_CHK_ST_FREE;
880 return 0;
881 case NVM_RSP_ERR_FAILCRC:
882 case NVM_RSP_ERR_FAILECC:
883 case NVM_RSP_WARN_HIGHECC:
884 meta->state = NVM_CHK_ST_OPEN;
885 goto scan;
886 default:
887 return -ret; /* other io error */
888 }
889 }
890
891 /* sense last page */
892 ppa.g.pg = geo->num_pg - 1;
893 ppa.g.pl = geo->num_pln - 1;
894
895 ret = nvm_bb_chunk_sense(dev, ppa);
896 if (ret < 0) /* io error */
897 return ret;
898 else if (ret == 0) { /* Chunk fully written */
899 meta->state = NVM_CHK_ST_CLOSED;
900 meta->wp = geo->clba;
901 return 0;
902 } else if (ret > 0) {
903 switch (ret) {
904 case NVM_RSP_ERR_EMPTYPAGE:
905 case NVM_RSP_ERR_FAILCRC:
906 case NVM_RSP_ERR_FAILECC:
907 case NVM_RSP_WARN_HIGHECC:
908 meta->state = NVM_CHK_ST_OPEN;
909 break;
910 default:
911 return -ret; /* other io error */
912 }
913 }
914
915scan:
916 /*
917 * chunk is open, we scan sequentially to update the write pointer.
918 * We make the assumption that targets write data across all planes
919 * before moving to the next page.
920 */
921 for (pg = 0; pg < geo->num_pg; pg++) {
922 for (pl = 0; pl < geo->num_pln; pl++) {
923 ppa.g.pg = pg;
924 ppa.g.pl = pl;
925
926 ret = nvm_bb_chunk_sense(dev, ppa);
927 if (ret < 0) /* io error */
928 return ret;
929 else if (ret == 0) {
930 meta->wp += geo->ws_min;
931 } else if (ret > 0) {
932 switch (ret) {
933 case NVM_RSP_ERR_EMPTYPAGE:
934 return 0;
935 case NVM_RSP_ERR_FAILCRC:
936 case NVM_RSP_ERR_FAILECC:
937 case NVM_RSP_WARN_HIGHECC:
938 meta->wp += geo->ws_min;
939 break;
940 default:
941 return -ret; /* other io error */
942 }
943 }
944 }
945 }
946
947 return 0;
948}
949
950/*
951 * folds a bad block list from its plane representation to its
952 * chunk representation.
953 *
954 * If any of the planes status are bad or grown bad, the chunk is marked
955 * offline. If not bad, the first plane state acts as the chunk state.
956 */
957static int nvm_bb_to_chunk(struct nvm_dev *dev, struct ppa_addr ppa,
958 u8 *blks, int nr_blks, struct nvm_chk_meta *meta)
959{
960 struct nvm_geo *geo = &dev->geo;
961 int ret, blk, pl, offset, blktype;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200962
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200963 for (blk = 0; blk < geo->num_chk; blk++) {
964 offset = blk * geo->pln_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200965 blktype = blks[offset];
966
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200967 for (pl = 0; pl < geo->pln_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200968 if (blks[offset + pl] &
969 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
970 blktype = blks[offset + pl];
971 break;
972 }
973 }
974
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200975 ppa.g.blk = blk;
976
977 meta->wp = 0;
978 meta->type = NVM_CHK_TP_W_SEQ;
979 meta->wi = 0;
980 meta->slba = generic_to_dev_addr(dev, ppa).ppa;
981 meta->cnlb = dev->geo.clba;
982
983 if (blktype == NVM_BLK_T_FREE) {
984 ret = nvm_bb_chunk_scan(dev, ppa, meta);
985 if (ret)
986 return ret;
987 } else {
988 meta->state = NVM_CHK_ST_OFFLINE;
989 }
990
991 meta++;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200992 }
993
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200994 return 0;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200995}
Matias Bjørling22e8c972016-05-06 20:02:58 +0200996
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200997static int nvm_get_bb_meta(struct nvm_dev *dev, sector_t slba,
998 int nchks, struct nvm_chk_meta *meta)
999{
1000 struct nvm_geo *geo = &dev->geo;
1001 struct ppa_addr ppa;
1002 u8 *blks;
1003 int ch, lun, nr_blks;
Geert Uytterhoeven55e58c52018-12-11 20:16:08 +01001004 int ret = 0;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001005
1006 ppa.ppa = slba;
1007 ppa = dev_to_generic_addr(dev, ppa);
1008
1009 if (ppa.g.blk != 0)
1010 return -EINVAL;
1011
1012 if ((nchks % geo->num_chk) != 0)
1013 return -EINVAL;
1014
1015 nr_blks = geo->num_chk * geo->pln_mode;
1016
1017 blks = kmalloc(nr_blks, GFP_KERNEL);
1018 if (!blks)
1019 return -ENOMEM;
1020
1021 for (ch = ppa.g.ch; ch < geo->num_ch; ch++) {
1022 for (lun = ppa.g.lun; lun < geo->num_lun; lun++) {
1023 struct ppa_addr ppa_gen, ppa_dev;
1024
1025 if (!nchks)
1026 goto done;
1027
1028 ppa_gen.ppa = 0;
1029 ppa_gen.g.ch = ch;
1030 ppa_gen.g.lun = lun;
1031 ppa_dev = generic_to_dev_addr(dev, ppa_gen);
1032
1033 ret = dev->ops->get_bb_tbl(dev, ppa_dev, blks);
1034 if (ret)
1035 goto done;
1036
1037 ret = nvm_bb_to_chunk(dev, ppa_gen, blks, nr_blks,
1038 meta);
1039 if (ret)
1040 goto done;
1041
1042 meta += geo->num_chk;
1043 nchks -= geo->num_chk;
1044 }
1045 }
1046done:
1047 kfree(blks);
1048 return ret;
1049}
1050
1051int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
1052 int nchks, struct nvm_chk_meta *meta)
Javier González333ba052016-11-28 22:39:14 +01001053{
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001054 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +01001055
Matias Bjørlingdab8ee92017-01-31 13:17:14 +01001056 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001057
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001058 if (dev->geo.version == NVM_OCSSD_SPEC_12)
1059 return nvm_get_bb_meta(dev, (sector_t)ppa.ppa, nchks, meta);
1060
1061 return dev->ops->get_chk_meta(dev, (sector_t)ppa.ppa, nchks, meta);
Javier González333ba052016-11-28 22:39:14 +01001062}
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001063EXPORT_SYMBOL_GPL(nvm_get_chunk_meta);
1064
1065int nvm_set_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
1066 int nr_ppas, int type)
1067{
1068 struct nvm_dev *dev = tgt_dev->parent;
1069 struct nvm_rq rqd;
1070 int ret;
1071
1072 if (dev->geo.version == NVM_OCSSD_SPEC_20)
1073 return 0;
1074
1075 if (nr_ppas > NVM_MAX_VLBA) {
Minwoo Im43db0592019-09-05 21:04:32 +02001076 pr_err("unable to update all blocks atomically\n");
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001077 return -EINVAL;
1078 }
1079
1080 memset(&rqd, 0, sizeof(struct nvm_rq));
1081
1082 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
1083 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
1084
1085 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
1086 nvm_free_rqd_ppalist(tgt_dev, &rqd);
1087 if (ret)
1088 return -EINVAL;
1089
1090 return 0;
1091}
1092EXPORT_SYMBOL_GPL(nvm_set_chunk_meta);
Javier González333ba052016-11-28 22:39:14 +01001093
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001094static int nvm_core_init(struct nvm_dev *dev)
1095{
Javier González8e79b5c2016-11-28 22:39:06 +01001096 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001097 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001098
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001099 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +01001100 sizeof(unsigned long), GFP_KERNEL);
1101 if (!dev->lun_map)
1102 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001103
Matias Bjørlingade69e22017-01-31 13:17:09 +01001104 INIT_LIST_HEAD(&dev->area_list);
1105 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +01001106 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001107 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001108
Matias Bjørlingade69e22017-01-31 13:17:09 +01001109 ret = nvm_register_map(dev);
1110 if (ret)
1111 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001112
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001113 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001114err_fmtype:
1115 kfree(dev->lun_map);
1116 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001117}
1118
Igor Konopkoe69397e2019-05-04 20:38:08 +02001119static void nvm_free(struct kref *ref)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001120{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001121 struct nvm_dev *dev = container_of(ref, struct nvm_dev, ref);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001122
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001123 if (dev->dma_pool)
1124 dev->ops->destroy_dma_pool(dev->dma_pool);
1125
Igor Konopkoe69397e2019-05-04 20:38:08 +02001126 if (dev->rmap)
1127 nvm_unregister_map(dev);
1128
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001129 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001130 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001131}
1132
1133static int nvm_init(struct nvm_dev *dev)
1134{
Javier González8e79b5c2016-11-28 22:39:06 +01001135 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001136 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001137
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001138 if (dev->ops->identity(dev)) {
Minwoo Im43db0592019-09-05 21:04:32 +02001139 pr_err("device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001140 goto err;
1141 }
1142
Minwoo Im43db0592019-09-05 21:04:32 +02001143 pr_debug("ver:%u.%u nvm_vendor:%x\n", geo->major_ver_id,
1144 geo->minor_ver_id, geo->vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001145
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001146 ret = nvm_core_init(dev);
1147 if (ret) {
Minwoo Im43db0592019-09-05 21:04:32 +02001148 pr_err("could not initialize core structures.\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001149 goto err;
1150 }
1151
Minwoo Im43db0592019-09-05 21:04:32 +02001152 pr_info("registered %s [%u/%u/%u/%u/%u]\n",
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001153 dev->name, dev->geo.ws_min, dev->geo.ws_opt,
1154 dev->geo.num_chk, dev->geo.all_luns,
1155 dev->geo.num_ch);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001156 return 0;
1157err:
Minwoo Im43db0592019-09-05 21:04:32 +02001158 pr_err("failed to initialize nvm\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001159 return ret;
1160}
1161
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001162struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001163{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001164 struct nvm_dev *dev;
1165
1166 dev = kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1167 if (dev)
1168 kref_init(&dev->ref);
1169
1170 return dev;
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001171}
1172EXPORT_SYMBOL(nvm_alloc_dev);
1173
1174int nvm_register(struct nvm_dev *dev)
1175{
Igor Konopko24828d02018-12-11 20:16:24 +01001176 int ret, exp_pool_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001177
Igor Konopkoe69397e2019-05-04 20:38:08 +02001178 if (!dev->q || !dev->ops) {
1179 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001180 return -EINVAL;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001181 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001182
Matias Bjørling85136c02018-12-11 20:16:20 +01001183 ret = nvm_init(dev);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001184 if (ret) {
1185 kref_put(&dev->ref, nvm_free);
Matias Bjørling85136c02018-12-11 20:16:20 +01001186 return ret;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001187 }
Matias Bjørling85136c02018-12-11 20:16:20 +01001188
Igor Konopko24828d02018-12-11 20:16:24 +01001189 exp_pool_size = max_t(int, PAGE_SIZE,
1190 (NVM_MAX_VLBA * (sizeof(u64) + dev->geo.sos)));
1191 exp_pool_size = round_up(exp_pool_size, PAGE_SIZE);
1192
1193 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist",
1194 exp_pool_size);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001195 if (!dev->dma_pool) {
Minwoo Im43db0592019-09-05 21:04:32 +02001196 pr_err("could not create dma pool\n");
Igor Konopkoe69397e2019-05-04 20:38:08 +02001197 kref_put(&dev->ref, nvm_free);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001198 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001199 }
1200
Matias Bjørling762796b2015-12-06 11:25:49 +01001201 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001202 down_write(&nvm_lock);
1203 list_add(&dev->devices, &nvm_devices);
1204 up_write(&nvm_lock);
1205
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001206 return 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001207}
1208EXPORT_SYMBOL(nvm_register);
1209
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001210void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001211{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001212 struct nvm_target *t, *tmp;
1213
1214 mutex_lock(&dev->mlock);
1215 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1216 if (t->dev->parent != dev)
1217 continue;
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001218 __nvm_remove_target(t, false);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001219 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001220 }
1221 mutex_unlock(&dev->mlock);
1222
Wenwei Taod0a712c2015-11-28 16:49:28 +01001223 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001224 list_del(&dev->devices);
1225 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001226
Igor Konopkoe69397e2019-05-04 20:38:08 +02001227 kref_put(&dev->ref, nvm_free);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001228}
1229EXPORT_SYMBOL(nvm_unregister);
1230
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001231static int __nvm_configure_create(struct nvm_ioctl_create *create)
1232{
1233 struct nvm_dev *dev;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001234 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001235
Wenwei Taod0a712c2015-11-28 16:49:28 +01001236 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001237 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001238 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001239
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001240 if (!dev) {
Minwoo Im43db0592019-09-05 21:04:32 +02001241 pr_err("device not found\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001242 return -EINVAL;
1243 }
1244
Igor Konopkoe69397e2019-05-04 20:38:08 +02001245 kref_get(&dev->ref);
1246 ret = nvm_create_tgt(dev, create);
1247 if (ret)
1248 kref_put(&dev->ref, nvm_free);
1249
1250 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001251}
1252
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001253static long nvm_ioctl_info(struct file *file, void __user *arg)
1254{
1255 struct nvm_ioctl_info *info;
1256 struct nvm_tgt_type *tt;
1257 int tgt_iter = 0;
1258
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001259 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1260 if (IS_ERR(info))
1261 return -EFAULT;
1262
1263 info->version[0] = NVM_VERSION_MAJOR;
1264 info->version[1] = NVM_VERSION_MINOR;
1265 info->version[2] = NVM_VERSION_PATCH;
1266
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001267 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001268 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001269 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1270
1271 tgt->version[0] = tt->version[0];
1272 tgt->version[1] = tt->version[1];
1273 tgt->version[2] = tt->version[2];
1274 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1275
1276 tgt_iter++;
1277 }
1278
1279 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001280 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001281
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001282 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1283 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001284 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001285 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001286
1287 kfree(info);
1288 return 0;
1289}
1290
1291static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1292{
1293 struct nvm_ioctl_get_devices *devices;
1294 struct nvm_dev *dev;
1295 int i = 0;
1296
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001297 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1298 if (!devices)
1299 return -ENOMEM;
1300
1301 down_write(&nvm_lock);
1302 list_for_each_entry(dev, &nvm_devices, devices) {
1303 struct nvm_ioctl_device_info *info = &devices->info[i];
1304
Javier González6eb08242017-04-15 20:55:49 +02001305 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001306
Matias Bjørlingade69e22017-01-31 13:17:09 +01001307 /* kept for compatibility */
1308 info->bmversion[0] = 1;
1309 info->bmversion[1] = 0;
1310 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001311 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001312 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001313
Colin Ian Kinga48faeb2020-10-16 15:33:51 +01001314 if (i >= ARRAY_SIZE(devices->info)) {
1315 pr_err("max %zd devices can be reported.\n",
1316 ARRAY_SIZE(devices->info));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001317 break;
1318 }
1319 }
1320 up_write(&nvm_lock);
1321
1322 devices->nr_devices = i;
1323
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001324 if (copy_to_user(arg, devices,
1325 sizeof(struct nvm_ioctl_get_devices))) {
1326 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001327 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001328 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001329
1330 kfree(devices);
1331 return 0;
1332}
1333
1334static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1335{
1336 struct nvm_ioctl_create create;
1337
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001338 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1339 return -EFAULT;
1340
Javier Gonzáleze5392732018-01-05 14:16:14 +01001341 if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED &&
1342 create.conf.e.rsv != 0) {
Minwoo Im43db0592019-09-05 21:04:32 +02001343 pr_err("reserved config field in use\n");
Javier Gonzáleze5392732018-01-05 14:16:14 +01001344 return -EINVAL;
1345 }
1346
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001347 create.dev[DISK_NAME_LEN - 1] = '\0';
1348 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1349 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1350
1351 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001352 __u32 flags = create.flags;
1353
1354 /* Check for valid flags */
1355 if (flags & NVM_TARGET_FACTORY)
1356 flags &= ~NVM_TARGET_FACTORY;
1357
1358 if (flags) {
Minwoo Im43db0592019-09-05 21:04:32 +02001359 pr_err("flag not supported\n");
Javier González4af3f752017-04-15 20:55:45 +02001360 return -EINVAL;
1361 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001362 }
1363
1364 return __nvm_configure_create(&create);
1365}
1366
1367static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1368{
1369 struct nvm_ioctl_remove remove;
1370
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001371 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1372 return -EFAULT;
1373
1374 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1375
1376 if (remove.flags != 0) {
Minwoo Im43db0592019-09-05 21:04:32 +02001377 pr_err("no flags supported\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001378 return -EINVAL;
1379 }
1380
Igor Konopko843f2ed2019-05-04 20:38:09 +02001381 return nvm_remove_tgt(&remove);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001382}
1383
Matias Bjørlingade69e22017-01-31 13:17:09 +01001384/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001385static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1386{
1387 struct nvm_ioctl_dev_init init;
1388
Matias Bjørling55696152016-01-12 07:49:37 +01001389 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1390 return -EFAULT;
1391
1392 if (init.flags != 0) {
Minwoo Im43db0592019-09-05 21:04:32 +02001393 pr_err("no flags supported\n");
Matias Bjørling55696152016-01-12 07:49:37 +01001394 return -EINVAL;
1395 }
1396
Matias Bjørlingade69e22017-01-31 13:17:09 +01001397 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001398}
1399
Matias Bjørlingade69e22017-01-31 13:17:09 +01001400/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001401static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1402{
1403 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001404
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001405 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1406 return -EFAULT;
1407
1408 fact.dev[DISK_NAME_LEN - 1] = '\0';
1409
1410 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1411 return -EINVAL;
1412
Matias Bjørlingbf643182016-02-04 15:13:27 +01001413 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001414}
1415
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001416static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1417{
1418 void __user *argp = (void __user *)arg;
1419
Johannes Thumshirn40f962d2018-03-30 00:05:07 +02001420 if (!capable(CAP_SYS_ADMIN))
1421 return -EPERM;
1422
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001423 switch (cmd) {
1424 case NVM_INFO:
1425 return nvm_ioctl_info(file, argp);
1426 case NVM_GET_DEVICES:
1427 return nvm_ioctl_get_devices(file, argp);
1428 case NVM_DEV_CREATE:
1429 return nvm_ioctl_dev_create(file, argp);
1430 case NVM_DEV_REMOVE:
1431 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001432 case NVM_DEV_INIT:
1433 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001434 case NVM_DEV_FACTORY:
1435 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001436 }
1437 return 0;
1438}
1439
1440static const struct file_operations _ctl_fops = {
1441 .open = nonseekable_open,
1442 .unlocked_ioctl = nvm_ctl_ioctl,
1443 .owner = THIS_MODULE,
1444 .llseek = noop_llseek,
1445};
1446
1447static struct miscdevice _nvm_misc = {
1448 .minor = MISC_DYNAMIC_MINOR,
1449 .name = "lightnvm",
1450 .nodename = "lightnvm/control",
1451 .fops = &_ctl_fops,
1452};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001453builtin_misc_device(_nvm_misc);