blob: f88d4e57cff8f2dfe29fd81232697e4ba52abf57 [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
239static const struct block_device_operations nvm_fops = {
240 .owner = THIS_MODULE,
241};
242
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100243static struct nvm_tgt_type *__nvm_find_target_type(const char *name)
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200244{
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100245 struct nvm_tgt_type *tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200246
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100247 list_for_each_entry(tt, &nvm_tgt_types, list)
248 if (!strcmp(name, tt->name))
249 return tt;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200250
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100251 return NULL;
252}
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200253
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100254static struct nvm_tgt_type *nvm_find_target_type(const char *name)
255{
256 struct nvm_tgt_type *tt;
257
258 down_write(&nvm_tgtt_lock);
259 tt = __nvm_find_target_type(name);
260 up_write(&nvm_tgtt_lock);
261
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200262 return tt;
263}
264
Javier Gonzáleze5392732018-01-05 14:16:14 +0100265static int nvm_config_check_luns(struct nvm_geo *geo, int lun_begin,
266 int lun_end)
267{
268 if (lun_begin > lun_end || lun_end >= geo->all_luns) {
Minwoo Im43db0592019-09-05 21:04:32 +0200269 pr_err("lun out of bound (%u:%u > %u)\n",
Javier Gonzáleze5392732018-01-05 14:16:14 +0100270 lun_begin, lun_end, geo->all_luns - 1);
271 return -EINVAL;
272 }
273
274 return 0;
275}
276
277static int __nvm_config_simple(struct nvm_dev *dev,
278 struct nvm_ioctl_create_simple *s)
279{
280 struct nvm_geo *geo = &dev->geo;
281
282 if (s->lun_begin == -1 && s->lun_end == -1) {
283 s->lun_begin = 0;
284 s->lun_end = geo->all_luns - 1;
285 }
286
287 return nvm_config_check_luns(geo, s->lun_begin, s->lun_end);
288}
289
290static int __nvm_config_extended(struct nvm_dev *dev,
291 struct nvm_ioctl_create_extended *e)
292{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100293 if (e->lun_begin == 0xFFFF && e->lun_end == 0xFFFF) {
294 e->lun_begin = 0;
295 e->lun_end = dev->geo.all_luns - 1;
296 }
297
298 /* op not set falls into target's default */
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200299 if (e->op == 0xFFFF) {
Javier Gonzáleze5392732018-01-05 14:16:14 +0100300 e->op = NVM_TARGET_DEFAULT_OP;
Heiner Litz9d7aa4a2018-03-30 00:05:08 +0200301 } else if (e->op < NVM_TARGET_MIN_OP || e->op > NVM_TARGET_MAX_OP) {
Minwoo Im43db0592019-09-05 21:04:32 +0200302 pr_err("invalid over provisioning value\n");
Javier Gonzáleze5392732018-01-05 14:16:14 +0100303 return -EINVAL;
304 }
305
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200306 return nvm_config_check_luns(&dev->geo, e->lun_begin, e->lun_end);
Javier Gonzáleze5392732018-01-05 14:16:14 +0100307}
308
Matias Bjørlingade69e22017-01-31 13:17:09 +0100309static int nvm_create_tgt(struct nvm_dev *dev, struct nvm_ioctl_create *create)
310{
Javier Gonzáleze5392732018-01-05 14:16:14 +0100311 struct nvm_ioctl_create_extended e;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100312 struct request_queue *tqueue;
313 struct gendisk *tdisk;
314 struct nvm_tgt_type *tt;
315 struct nvm_target *t;
316 struct nvm_tgt_dev *tgt_dev;
317 void *targetdata;
Igor Konopkoa14669e2019-05-04 20:37:55 +0200318 unsigned int mdts;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300319 int ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100320
Javier Gonzáleze5392732018-01-05 14:16:14 +0100321 switch (create->conf.type) {
322 case NVM_CONFIG_TYPE_SIMPLE:
323 ret = __nvm_config_simple(dev, &create->conf.s);
324 if (ret)
325 return ret;
326
327 e.lun_begin = create->conf.s.lun_begin;
328 e.lun_end = create->conf.s.lun_end;
329 e.op = NVM_TARGET_DEFAULT_OP;
330 break;
331 case NVM_CONFIG_TYPE_EXTENDED:
332 ret = __nvm_config_extended(dev, &create->conf.e);
333 if (ret)
334 return ret;
335
336 e = create->conf.e;
337 break;
338 default:
Minwoo Im43db0592019-09-05 21:04:32 +0200339 pr_err("config type not valid\n");
Javier Gonzáleze5392732018-01-05 14:16:14 +0100340 return -EINVAL;
341 }
342
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100343 tt = nvm_find_target_type(create->tgttype);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100344 if (!tt) {
Minwoo Im43db0592019-09-05 21:04:32 +0200345 pr_err("target type %s not found\n", create->tgttype);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100346 return -EINVAL;
347 }
348
Matias Bjørling656e33c2018-10-09 13:11:34 +0200349 if ((tt->flags & NVM_TGT_F_HOST_L2P) != (dev->geo.dom & NVM_RSP_L2P)) {
Minwoo Im43db0592019-09-05 21:04:32 +0200350 pr_err("device is incompatible with target L2P type.\n");
Matias Bjørling656e33c2018-10-09 13:11:34 +0200351 return -EINVAL;
352 }
353
Javier Gonzálezbd77b232018-01-05 14:16:05 +0100354 if (nvm_target_exists(create->tgtname)) {
Minwoo Im43db0592019-09-05 21:04:32 +0200355 pr_err("target name already exists (%s)\n",
Javier Gonzálezbd77b232018-01-05 14:16:05 +0100356 create->tgtname);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100357 return -EINVAL;
358 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100359
Javier Gonzáleze5392732018-01-05 14:16:14 +0100360 ret = nvm_reserve_luns(dev, e.lun_begin, e.lun_end);
Rakesh Pandit12e9a6d62017-06-27 14:55:33 +0300361 if (ret)
362 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100363
364 t = kmalloc(sizeof(struct nvm_target), GFP_KERNEL);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300365 if (!t) {
366 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100367 goto err_reserve;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300368 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100369
Javier Gonzáleze5392732018-01-05 14:16:14 +0100370 tgt_dev = nvm_create_tgt_dev(dev, e.lun_begin, e.lun_end, e.op);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100371 if (!tgt_dev) {
Minwoo Im43db0592019-09-05 21:04:32 +0200372 pr_err("could not create target device\n");
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300373 ret = -ENOMEM;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100374 goto err_t;
375 }
376
Matias Bjørlingade69e22017-01-31 13:17:09 +0100377 tdisk = alloc_disk(0);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300378 if (!tdisk) {
379 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200380 goto err_dev;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300381 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200382
Christoph Hellwig6d469642018-11-14 17:02:18 +0100383 tqueue = blk_alloc_queue_node(GFP_KERNEL, dev->q->node);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300384 if (!tqueue) {
385 ret = -ENOMEM;
Javier González7d1ef2f2017-04-15 20:55:43 +0200386 goto err_disk;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300387 }
Javier González7d1ef2f2017-04-15 20:55:43 +0200388 blk_queue_make_request(tqueue, tt->make_rq);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100389
Javier González6eb08242017-04-15 20:55:49 +0200390 strlcpy(tdisk->disk_name, create->tgtname, sizeof(tdisk->disk_name));
Matias Bjørlingade69e22017-01-31 13:17:09 +0100391 tdisk->flags = GENHD_FL_EXT_DEVT;
392 tdisk->major = 0;
393 tdisk->first_minor = 0;
394 tdisk->fops = &nvm_fops;
395 tdisk->queue = tqueue;
396
Javier González4af3f752017-04-15 20:55:45 +0200397 targetdata = tt->init(tgt_dev, tdisk, create->flags);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300398 if (IS_ERR(targetdata)) {
399 ret = PTR_ERR(targetdata);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100400 goto err_init;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300401 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100402
403 tdisk->private_data = targetdata;
404 tqueue->queuedata = targetdata;
405
Igor Konopkoa14669e2019-05-04 20:37:55 +0200406 mdts = (dev->geo.csecs >> 9) * NVM_MAX_VLBA;
407 if (dev->geo.mdts) {
408 mdts = min_t(u32, dev->geo.mdts,
409 (dev->geo.csecs >> 9) * NVM_MAX_VLBA);
410 }
411 blk_queue_max_hw_sectors(tqueue, mdts);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100412
413 set_capacity(tdisk, tt->capacity(targetdata));
414 add_disk(tdisk);
415
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300416 if (tt->sysfs_init && tt->sysfs_init(tdisk)) {
417 ret = -ENOMEM;
Javier González9a69b0e2017-01-31 13:17:20 +0100418 goto err_sysfs;
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300419 }
Javier González9a69b0e2017-01-31 13:17:20 +0100420
Matias Bjørlingade69e22017-01-31 13:17:09 +0100421 t->type = tt;
422 t->disk = tdisk;
423 t->dev = tgt_dev;
424
425 mutex_lock(&dev->mlock);
426 list_add_tail(&t->list, &dev->targets);
427 mutex_unlock(&dev->mlock);
428
Rakesh Pandit90014822017-10-13 14:45:50 +0200429 __module_get(tt->owner);
430
Matias Bjørlingade69e22017-01-31 13:17:09 +0100431 return 0;
Javier González9a69b0e2017-01-31 13:17:20 +0100432err_sysfs:
433 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200434 tt->exit(targetdata, true);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100435err_init:
Matias Bjørlingade69e22017-01-31 13:17:09 +0100436 blk_cleanup_queue(tqueue);
Rakesh Pandit75ba4ad2017-04-20 08:17:47 -0600437 tdisk->queue = NULL;
Javier González7d1ef2f2017-04-15 20:55:43 +0200438err_disk:
439 put_disk(tdisk);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100440err_dev:
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200441 nvm_remove_tgt_dev(tgt_dev, 0);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100442err_t:
443 kfree(t);
444err_reserve:
Javier Gonzáleze5392732018-01-05 14:16:14 +0100445 nvm_release_luns_err(dev, e.lun_begin, e.lun_end);
Rakesh Pandit8d77bb82017-04-20 21:23:56 +0300446 return ret;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100447}
448
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200449static void __nvm_remove_target(struct nvm_target *t, bool graceful)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100450{
451 struct nvm_tgt_type *tt = t->type;
452 struct gendisk *tdisk = t->disk;
453 struct request_queue *q = tdisk->queue;
454
455 del_gendisk(tdisk);
456 blk_cleanup_queue(q);
457
Javier González9a69b0e2017-01-31 13:17:20 +0100458 if (tt->sysfs_exit)
459 tt->sysfs_exit(tdisk);
460
Matias Bjørlingade69e22017-01-31 13:17:09 +0100461 if (tt->exit)
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200462 tt->exit(tdisk->private_data, graceful);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100463
Javier Gonzálezedee1bd2017-04-15 20:55:42 +0200464 nvm_remove_tgt_dev(t->dev, 1);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100465 put_disk(tdisk);
Rakesh Pandit90014822017-10-13 14:45:50 +0200466 module_put(t->type->owner);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100467
468 list_del(&t->list);
469 kfree(t);
470}
471
472/**
473 * nvm_remove_tgt - Removes a target from the media manager
Matias Bjørlingade69e22017-01-31 13:17:09 +0100474 * @remove: ioctl structure with target name to remove.
475 *
476 * Returns:
477 * 0: on success
478 * 1: on not found
479 * <0: on error
480 */
Igor Konopko843f2ed2019-05-04 20:38:09 +0200481static int nvm_remove_tgt(struct nvm_ioctl_remove *remove)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100482{
Geert Uytterhoeven2f5af4a2019-06-21 11:12:00 +0200483 struct nvm_target *t = NULL;
Igor Konopko843f2ed2019-05-04 20:38:09 +0200484 struct nvm_dev *dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100485
Igor Konopko843f2ed2019-05-04 20:38:09 +0200486 down_read(&nvm_lock);
487 list_for_each_entry(dev, &nvm_devices, devices) {
488 mutex_lock(&dev->mlock);
489 t = nvm_find_target(dev, remove->tgtname);
490 if (t) {
491 mutex_unlock(&dev->mlock);
492 break;
493 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100494 mutex_unlock(&dev->mlock);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100495 }
Igor Konopko843f2ed2019-05-04 20:38:09 +0200496 up_read(&nvm_lock);
497
498 if (!t)
499 return 1;
500
Javier Gonzáleza7c9e912018-06-01 15:04:24 +0200501 __nvm_remove_target(t, true);
Igor Konopkoe69397e2019-05-04 20:38:08 +0200502 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100503
504 return 0;
505}
506
507static int nvm_register_map(struct nvm_dev *dev)
508{
509 struct nvm_dev_map *rmap;
510 int i, j;
511
512 rmap = kmalloc(sizeof(struct nvm_dev_map), GFP_KERNEL);
513 if (!rmap)
514 goto err_rmap;
515
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200516 rmap->chnls = kcalloc(dev->geo.num_ch, sizeof(struct nvm_ch_map),
Matias Bjørlingade69e22017-01-31 13:17:09 +0100517 GFP_KERNEL);
518 if (!rmap->chnls)
519 goto err_chnls;
520
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200521 for (i = 0; i < dev->geo.num_ch; i++) {
Matias Bjørlingade69e22017-01-31 13:17:09 +0100522 struct nvm_ch_map *ch_rmap;
523 int *lun_roffs;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200524 int luns_in_chnl = dev->geo.num_lun;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100525
526 ch_rmap = &rmap->chnls[i];
527
528 ch_rmap->ch_off = -1;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200529 ch_rmap->num_lun = luns_in_chnl;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100530
531 lun_roffs = kcalloc(luns_in_chnl, sizeof(int), GFP_KERNEL);
532 if (!lun_roffs)
533 goto err_ch;
534
535 for (j = 0; j < luns_in_chnl; j++)
536 lun_roffs[j] = -1;
537
538 ch_rmap->lun_offs = lun_roffs;
539 }
540
541 dev->rmap = rmap;
542
543 return 0;
544err_ch:
545 while (--i >= 0)
546 kfree(rmap->chnls[i].lun_offs);
547err_chnls:
548 kfree(rmap);
549err_rmap:
550 return -ENOMEM;
551}
552
Javier González7a3de2b2017-04-15 20:55:39 +0200553static void nvm_unregister_map(struct nvm_dev *dev)
554{
555 struct nvm_dev_map *rmap = dev->rmap;
556 int i;
557
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200558 for (i = 0; i < dev->geo.num_ch; i++)
Javier González7a3de2b2017-04-15 20:55:39 +0200559 kfree(rmap->chnls[i].lun_offs);
560
561 kfree(rmap->chnls);
562 kfree(rmap);
563}
564
Matias Bjørling61a561d2017-01-31 13:17:13 +0100565static void nvm_map_to_dev(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100566{
567 struct nvm_dev_map *dev_map = tgt_dev->map;
Javier González69471512018-03-30 00:05:15 +0200568 struct nvm_ch_map *ch_map = &dev_map->chnls[p->a.ch];
569 int lun_off = ch_map->lun_offs[p->a.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100570
Javier González69471512018-03-30 00:05:15 +0200571 p->a.ch += ch_map->ch_off;
572 p->a.lun += lun_off;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100573}
574
Matias Bjørling61a561d2017-01-31 13:17:13 +0100575static void nvm_map_to_tgt(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100576{
577 struct nvm_dev *dev = tgt_dev->parent;
578 struct nvm_dev_map *dev_rmap = dev->rmap;
Javier González69471512018-03-30 00:05:15 +0200579 struct nvm_ch_map *ch_rmap = &dev_rmap->chnls[p->a.ch];
580 int lun_roff = ch_rmap->lun_offs[p->a.lun];
Matias Bjørlingade69e22017-01-31 13:17:09 +0100581
Javier González69471512018-03-30 00:05:15 +0200582 p->a.ch -= ch_rmap->ch_off;
583 p->a.lun -= lun_roff;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100584}
585
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100586static void nvm_ppa_tgt_to_dev(struct nvm_tgt_dev *tgt_dev,
587 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100588{
589 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100590
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100591 for (i = 0; i < nr_ppas; i++) {
592 nvm_map_to_dev(tgt_dev, &ppa_list[i]);
Javier González7100d502018-03-30 00:05:16 +0200593 ppa_list[i] = generic_to_dev_addr(tgt_dev->parent, ppa_list[i]);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100594 }
Matias Bjørlingade69e22017-01-31 13:17:09 +0100595}
596
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100597static void nvm_ppa_dev_to_tgt(struct nvm_tgt_dev *tgt_dev,
598 struct ppa_addr *ppa_list, int nr_ppas)
Matias Bjørlingade69e22017-01-31 13:17:09 +0100599{
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100600 int i;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100601
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100602 for (i = 0; i < nr_ppas; i++) {
Javier González7100d502018-03-30 00:05:16 +0200603 ppa_list[i] = dev_to_generic_addr(tgt_dev->parent, ppa_list[i]);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100604 nvm_map_to_tgt(tgt_dev, &ppa_list[i]);
605 }
606}
Matias Bjørlingade69e22017-01-31 13:17:09 +0100607
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100608static void nvm_rq_tgt_to_dev(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
609{
Hans Holmbergd68a9342018-10-09 13:11:46 +0200610 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100611
Hans Holmbergd68a9342018-10-09 13:11:46 +0200612 nvm_ppa_tgt_to_dev(tgt_dev, ppa_list, rqd->nr_ppas);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100613}
614
615static void nvm_rq_dev_to_tgt(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
616{
Hans Holmbergd68a9342018-10-09 13:11:46 +0200617 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100618
Hans Holmbergd68a9342018-10-09 13:11:46 +0200619 nvm_ppa_dev_to_tgt(tgt_dev, ppa_list, rqd->nr_ppas);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100620}
621
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200622int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100623{
624 int ret = 0;
625
Matias Bjørling5cd90782016-07-07 09:54:17 +0200626 down_write(&nvm_tgtt_lock);
Javier Gonzáleze29c80e2018-01-05 14:16:04 +0100627 if (__nvm_find_target_type(tt->name))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100628 ret = -EEXIST;
629 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200630 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +0200631 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100632
633 return ret;
634}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200635EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100636
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200637void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100638{
639 if (!tt)
640 return;
641
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200642 down_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100643 list_del(&tt->list);
Rakesh Pandit88d31ea2017-10-13 14:45:52 +0200644 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100645}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200646EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100647
648void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
649 dma_addr_t *dma_handler)
650{
Javier González75b85642016-05-06 20:03:13 +0200651 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100652 dma_handler);
653}
654EXPORT_SYMBOL(nvm_dev_dma_alloc);
655
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100656void nvm_dev_dma_free(struct nvm_dev *dev, void *addr, dma_addr_t dma_handler)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100657{
Javier González75b85642016-05-06 20:03:13 +0200658 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100659}
660EXPORT_SYMBOL(nvm_dev_dma_free);
661
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100662static struct nvm_dev *nvm_find_nvm_dev(const char *name)
663{
664 struct nvm_dev *dev;
665
666 list_for_each_entry(dev, &nvm_devices, devices)
667 if (!strcmp(name, dev->name))
668 return dev;
669
670 return NULL;
671}
672
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200673static int nvm_set_rqd_ppalist(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
674 const struct ppa_addr *ppas, int nr_ppas)
675{
676 struct nvm_dev *dev = tgt_dev->parent;
677 struct nvm_geo *geo = &tgt_dev->geo;
678 int i, plane_cnt, pl_idx;
679 struct ppa_addr ppa;
680
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200681 if (geo->pln_mode == NVM_PLANE_SINGLE && nr_ppas == 1) {
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200682 rqd->nr_ppas = nr_ppas;
683 rqd->ppa_addr = ppas[0];
684
685 return 0;
686 }
687
688 rqd->nr_ppas = nr_ppas;
689 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
690 if (!rqd->ppa_list) {
Minwoo Im43db0592019-09-05 21:04:32 +0200691 pr_err("failed to allocate dma memory\n");
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200692 return -ENOMEM;
693 }
694
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200695 plane_cnt = geo->pln_mode;
Rakesh Panditeb6f1682017-10-13 14:46:31 +0200696 rqd->nr_ppas *= plane_cnt;
697
698 for (i = 0; i < nr_ppas; i++) {
699 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
700 ppa = ppas[i];
701 ppa.g.pl = pl_idx;
702 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
703 }
704 }
705
706 return 0;
707}
708
709static void nvm_free_rqd_ppalist(struct nvm_tgt_dev *tgt_dev,
710 struct nvm_rq *rqd)
711{
712 if (!rqd->ppa_list)
713 return;
714
715 nvm_dev_dma_free(tgt_dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
716}
717
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200718static int nvm_set_flags(struct nvm_geo *geo, struct nvm_rq *rqd)
719{
720 int flags = 0;
721
722 if (geo->version == NVM_OCSSD_SPEC_20)
723 return 0;
724
725 if (rqd->is_seq)
726 flags |= geo->pln_mode >> 1;
727
728 if (rqd->opcode == NVM_OP_PREAD)
729 flags |= (NVM_IO_SCRAMBLE_ENABLE | NVM_IO_SUSPEND);
730 else if (rqd->opcode == NVM_OP_PWRITE)
731 flags |= NVM_IO_SCRAMBLE_ENABLE;
732
733 return flags;
734}
735
Hans Holmberg48e5da722019-07-31 11:41:34 +0200736int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd, void *buf)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100737{
Javier González8e536242016-11-28 22:39:10 +0100738 struct nvm_dev *dev = tgt_dev->parent;
Javier González3e505af2017-06-26 11:57:10 +0200739 int ret;
Javier González8e536242016-11-28 22:39:10 +0100740
Matias Bjørlingade69e22017-01-31 13:17:09 +0100741 if (!dev->ops->submit_io)
742 return -ENODEV;
743
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100744 nvm_rq_tgt_to_dev(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100745
746 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200747 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González3e505af2017-06-26 11:57:10 +0200748
749 /* In case of error, fail with right address format */
Hans Holmberg48e5da722019-07-31 11:41:34 +0200750 ret = dev->ops->submit_io(dev, rqd, buf);
Javier González3e505af2017-06-26 11:57:10 +0200751 if (ret)
752 nvm_rq_dev_to_tgt(tgt_dev, rqd);
753 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100754}
755EXPORT_SYMBOL(nvm_submit_io);
756
Hans Holmberg98d87f72019-07-31 11:41:33 +0200757static void nvm_sync_end_io(struct nvm_rq *rqd)
758{
759 struct completion *waiting = rqd->private;
760
761 complete(waiting);
762}
763
Hans Holmberg48e5da722019-07-31 11:41:34 +0200764static int nvm_submit_io_wait(struct nvm_dev *dev, struct nvm_rq *rqd,
765 void *buf)
Hans Holmberg98d87f72019-07-31 11:41:33 +0200766{
767 DECLARE_COMPLETION_ONSTACK(wait);
768 int ret = 0;
769
770 rqd->end_io = nvm_sync_end_io;
771 rqd->private = &wait;
772
Hans Holmberg48e5da722019-07-31 11:41:34 +0200773 ret = dev->ops->submit_io(dev, rqd, buf);
Hans Holmberg98d87f72019-07-31 11:41:33 +0200774 if (ret)
775 return ret;
776
777 wait_for_completion_io(&wait);
778
779 return 0;
780}
781
Hans Holmberg48e5da722019-07-31 11:41:34 +0200782int nvm_submit_io_sync(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd,
783 void *buf)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100784{
Javier González1a94b2d2017-10-13 14:46:47 +0200785 struct nvm_dev *dev = tgt_dev->parent;
786 int ret;
Javier González17912c42017-04-15 20:55:37 +0200787
Hans Holmberg98d87f72019-07-31 11:41:33 +0200788 if (!dev->ops->submit_io)
Javier González1a94b2d2017-10-13 14:46:47 +0200789 return -ENODEV;
790
791 nvm_rq_tgt_to_dev(tgt_dev, rqd);
792
793 rqd->dev = tgt_dev;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200794 rqd->flags = nvm_set_flags(&tgt_dev->geo, rqd);
Javier González1a94b2d2017-10-13 14:46:47 +0200795
Hans Holmberg48e5da722019-07-31 11:41:34 +0200796 ret = nvm_submit_io_wait(dev, rqd, buf);
Javier González1a94b2d2017-10-13 14:46:47 +0200797
798 return ret;
Javier González17912c42017-04-15 20:55:37 +0200799}
Javier González1a94b2d2017-10-13 14:46:47 +0200800EXPORT_SYMBOL(nvm_submit_io_sync);
Javier González17912c42017-04-15 20:55:37 +0200801
Matias Bjørling06894ef2017-01-31 13:17:17 +0100802void nvm_end_io(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100803{
Matias Bjørlingade69e22017-01-31 13:17:09 +0100804 struct nvm_tgt_dev *tgt_dev = rqd->dev;
Matias Bjørlingade69e22017-01-31 13:17:09 +0100805
806 /* Convert address space */
807 if (tgt_dev)
Matias Bjørlingdab8ee92017-01-31 13:17:14 +0100808 nvm_rq_dev_to_tgt(tgt_dev, rqd);
Matias Bjørlingade69e22017-01-31 13:17:09 +0100809
Matias Bjørling06894ef2017-01-31 13:17:17 +0100810 if (rqd->end_io)
811 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100812}
813EXPORT_SYMBOL(nvm_end_io);
814
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200815static int nvm_submit_io_sync_raw(struct nvm_dev *dev, struct nvm_rq *rqd)
816{
Hans Holmberg98d87f72019-07-31 11:41:33 +0200817 if (!dev->ops->submit_io)
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200818 return -ENODEV;
819
Hans Holmberg98d87f72019-07-31 11:41:33 +0200820 rqd->dev = NULL;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200821 rqd->flags = nvm_set_flags(&dev->geo, rqd);
822
Hans Holmberg48e5da722019-07-31 11:41:34 +0200823 return nvm_submit_io_wait(dev, rqd, NULL);
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200824}
825
826static int nvm_bb_chunk_sense(struct nvm_dev *dev, struct ppa_addr ppa)
827{
828 struct nvm_rq rqd = { NULL };
829 struct bio bio;
830 struct bio_vec bio_vec;
831 struct page *page;
832 int ret;
833
834 page = alloc_page(GFP_KERNEL);
835 if (!page)
836 return -ENOMEM;
837
838 bio_init(&bio, &bio_vec, 1);
839 bio_add_page(&bio, page, PAGE_SIZE, 0);
840 bio_set_op_attrs(&bio, REQ_OP_READ, 0);
841
842 rqd.bio = &bio;
843 rqd.opcode = NVM_OP_PREAD;
844 rqd.is_seq = 1;
845 rqd.nr_ppas = 1;
846 rqd.ppa_addr = generic_to_dev_addr(dev, ppa);
847
848 ret = nvm_submit_io_sync_raw(dev, &rqd);
849 if (ret)
850 return ret;
851
852 __free_page(page);
853
854 return rqd.error;
855}
856
Matias Bjørling22e8c972016-05-06 20:02:58 +0200857/*
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200858 * Scans a 1.2 chunk first and last page to determine if its state.
859 * If the chunk is found to be open, also scan it to update the write
860 * pointer.
Matias Bjørling22e8c972016-05-06 20:02:58 +0200861 */
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200862static int nvm_bb_chunk_scan(struct nvm_dev *dev, struct ppa_addr ppa,
863 struct nvm_chk_meta *meta)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200864{
Javier González8e79b5c2016-11-28 22:39:06 +0100865 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200866 int ret, pg, pl;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200867
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200868 /* sense first page */
869 ret = nvm_bb_chunk_sense(dev, ppa);
870 if (ret < 0) /* io error */
871 return ret;
872 else if (ret == 0) /* valid data */
873 meta->state = NVM_CHK_ST_OPEN;
874 else if (ret > 0) {
875 /*
876 * If empty page, the chunk is free, else it is an
877 * actual io error. In that case, mark it offline.
878 */
879 switch (ret) {
880 case NVM_RSP_ERR_EMPTYPAGE:
881 meta->state = NVM_CHK_ST_FREE;
882 return 0;
883 case NVM_RSP_ERR_FAILCRC:
884 case NVM_RSP_ERR_FAILECC:
885 case NVM_RSP_WARN_HIGHECC:
886 meta->state = NVM_CHK_ST_OPEN;
887 goto scan;
888 default:
889 return -ret; /* other io error */
890 }
891 }
892
893 /* sense last page */
894 ppa.g.pg = geo->num_pg - 1;
895 ppa.g.pl = geo->num_pln - 1;
896
897 ret = nvm_bb_chunk_sense(dev, ppa);
898 if (ret < 0) /* io error */
899 return ret;
900 else if (ret == 0) { /* Chunk fully written */
901 meta->state = NVM_CHK_ST_CLOSED;
902 meta->wp = geo->clba;
903 return 0;
904 } else if (ret > 0) {
905 switch (ret) {
906 case NVM_RSP_ERR_EMPTYPAGE:
907 case NVM_RSP_ERR_FAILCRC:
908 case NVM_RSP_ERR_FAILECC:
909 case NVM_RSP_WARN_HIGHECC:
910 meta->state = NVM_CHK_ST_OPEN;
911 break;
912 default:
913 return -ret; /* other io error */
914 }
915 }
916
917scan:
918 /*
919 * chunk is open, we scan sequentially to update the write pointer.
920 * We make the assumption that targets write data across all planes
921 * before moving to the next page.
922 */
923 for (pg = 0; pg < geo->num_pg; pg++) {
924 for (pl = 0; pl < geo->num_pln; pl++) {
925 ppa.g.pg = pg;
926 ppa.g.pl = pl;
927
928 ret = nvm_bb_chunk_sense(dev, ppa);
929 if (ret < 0) /* io error */
930 return ret;
931 else if (ret == 0) {
932 meta->wp += geo->ws_min;
933 } else if (ret > 0) {
934 switch (ret) {
935 case NVM_RSP_ERR_EMPTYPAGE:
936 return 0;
937 case NVM_RSP_ERR_FAILCRC:
938 case NVM_RSP_ERR_FAILECC:
939 case NVM_RSP_WARN_HIGHECC:
940 meta->wp += geo->ws_min;
941 break;
942 default:
943 return -ret; /* other io error */
944 }
945 }
946 }
947 }
948
949 return 0;
950}
951
952/*
953 * folds a bad block list from its plane representation to its
954 * chunk representation.
955 *
956 * If any of the planes status are bad or grown bad, the chunk is marked
957 * offline. If not bad, the first plane state acts as the chunk state.
958 */
959static int nvm_bb_to_chunk(struct nvm_dev *dev, struct ppa_addr ppa,
960 u8 *blks, int nr_blks, struct nvm_chk_meta *meta)
961{
962 struct nvm_geo *geo = &dev->geo;
963 int ret, blk, pl, offset, blktype;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200964
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200965 for (blk = 0; blk < geo->num_chk; blk++) {
966 offset = blk * geo->pln_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200967 blktype = blks[offset];
968
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200969 for (pl = 0; pl < geo->pln_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200970 if (blks[offset + pl] &
971 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
972 blktype = blks[offset + pl];
973 break;
974 }
975 }
976
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200977 ppa.g.blk = blk;
978
979 meta->wp = 0;
980 meta->type = NVM_CHK_TP_W_SEQ;
981 meta->wi = 0;
982 meta->slba = generic_to_dev_addr(dev, ppa).ppa;
983 meta->cnlb = dev->geo.clba;
984
985 if (blktype == NVM_BLK_T_FREE) {
986 ret = nvm_bb_chunk_scan(dev, ppa, meta);
987 if (ret)
988 return ret;
989 } else {
990 meta->state = NVM_CHK_ST_OFFLINE;
991 }
992
993 meta++;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200994 }
995
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200996 return 0;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200997}
Matias Bjørling22e8c972016-05-06 20:02:58 +0200998
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200999static int nvm_get_bb_meta(struct nvm_dev *dev, sector_t slba,
1000 int nchks, struct nvm_chk_meta *meta)
1001{
1002 struct nvm_geo *geo = &dev->geo;
1003 struct ppa_addr ppa;
1004 u8 *blks;
1005 int ch, lun, nr_blks;
Geert Uytterhoeven55e58c52018-12-11 20:16:08 +01001006 int ret = 0;
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001007
1008 ppa.ppa = slba;
1009 ppa = dev_to_generic_addr(dev, ppa);
1010
1011 if (ppa.g.blk != 0)
1012 return -EINVAL;
1013
1014 if ((nchks % geo->num_chk) != 0)
1015 return -EINVAL;
1016
1017 nr_blks = geo->num_chk * geo->pln_mode;
1018
1019 blks = kmalloc(nr_blks, GFP_KERNEL);
1020 if (!blks)
1021 return -ENOMEM;
1022
1023 for (ch = ppa.g.ch; ch < geo->num_ch; ch++) {
1024 for (lun = ppa.g.lun; lun < geo->num_lun; lun++) {
1025 struct ppa_addr ppa_gen, ppa_dev;
1026
1027 if (!nchks)
1028 goto done;
1029
1030 ppa_gen.ppa = 0;
1031 ppa_gen.g.ch = ch;
1032 ppa_gen.g.lun = lun;
1033 ppa_dev = generic_to_dev_addr(dev, ppa_gen);
1034
1035 ret = dev->ops->get_bb_tbl(dev, ppa_dev, blks);
1036 if (ret)
1037 goto done;
1038
1039 ret = nvm_bb_to_chunk(dev, ppa_gen, blks, nr_blks,
1040 meta);
1041 if (ret)
1042 goto done;
1043
1044 meta += geo->num_chk;
1045 nchks -= geo->num_chk;
1046 }
1047 }
1048done:
1049 kfree(blks);
1050 return ret;
1051}
1052
1053int nvm_get_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr ppa,
1054 int nchks, struct nvm_chk_meta *meta)
Javier González333ba052016-11-28 22:39:14 +01001055{
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001056 struct nvm_dev *dev = tgt_dev->parent;
Javier González333ba052016-11-28 22:39:14 +01001057
Matias Bjørlingdab8ee92017-01-31 13:17:14 +01001058 nvm_ppa_tgt_to_dev(tgt_dev, &ppa, 1);
Matias Bjørling8f4fe002017-01-31 13:17:12 +01001059
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001060 if (dev->geo.version == NVM_OCSSD_SPEC_12)
1061 return nvm_get_bb_meta(dev, (sector_t)ppa.ppa, nchks, meta);
1062
1063 return dev->ops->get_chk_meta(dev, (sector_t)ppa.ppa, nchks, meta);
Javier González333ba052016-11-28 22:39:14 +01001064}
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001065EXPORT_SYMBOL_GPL(nvm_get_chunk_meta);
1066
1067int nvm_set_chunk_meta(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *ppas,
1068 int nr_ppas, int type)
1069{
1070 struct nvm_dev *dev = tgt_dev->parent;
1071 struct nvm_rq rqd;
1072 int ret;
1073
1074 if (dev->geo.version == NVM_OCSSD_SPEC_20)
1075 return 0;
1076
1077 if (nr_ppas > NVM_MAX_VLBA) {
Minwoo Im43db0592019-09-05 21:04:32 +02001078 pr_err("unable to update all blocks atomically\n");
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001079 return -EINVAL;
1080 }
1081
1082 memset(&rqd, 0, sizeof(struct nvm_rq));
1083
1084 nvm_set_rqd_ppalist(tgt_dev, &rqd, ppas, nr_ppas);
1085 nvm_rq_tgt_to_dev(tgt_dev, &rqd);
1086
1087 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
1088 nvm_free_rqd_ppalist(tgt_dev, &rqd);
1089 if (ret)
1090 return -EINVAL;
1091
1092 return 0;
1093}
1094EXPORT_SYMBOL_GPL(nvm_set_chunk_meta);
Javier González333ba052016-11-28 22:39:14 +01001095
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001096static int nvm_core_init(struct nvm_dev *dev)
1097{
Javier González8e79b5c2016-11-28 22:39:06 +01001098 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001099 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001100
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001101 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->all_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +01001102 sizeof(unsigned long), GFP_KERNEL);
1103 if (!dev->lun_map)
1104 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001105
Matias Bjørlingade69e22017-01-31 13:17:09 +01001106 INIT_LIST_HEAD(&dev->area_list);
1107 INIT_LIST_HEAD(&dev->targets);
Matias Bjørlinge3eb3792016-01-12 07:49:36 +01001108 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001109 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001110
Matias Bjørlingade69e22017-01-31 13:17:09 +01001111 ret = nvm_register_map(dev);
1112 if (ret)
1113 goto err_fmtype;
Matias Bjørlingac81bfa92016-09-16 14:25:04 +02001114
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001115 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001116err_fmtype:
1117 kfree(dev->lun_map);
1118 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001119}
1120
Igor Konopkoe69397e2019-05-04 20:38:08 +02001121static void nvm_free(struct kref *ref)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001122{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001123 struct nvm_dev *dev = container_of(ref, struct nvm_dev, ref);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001124
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001125 if (dev->dma_pool)
1126 dev->ops->destroy_dma_pool(dev->dma_pool);
1127
Igor Konopkoe69397e2019-05-04 20:38:08 +02001128 if (dev->rmap)
1129 nvm_unregister_map(dev);
1130
Matias Bjørling7f7c5d02016-05-06 20:02:59 +02001131 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +02001132 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001133}
1134
1135static int nvm_init(struct nvm_dev *dev)
1136{
Javier González8e79b5c2016-11-28 22:39:06 +01001137 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +01001138 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001139
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001140 if (dev->ops->identity(dev)) {
Minwoo Im43db0592019-09-05 21:04:32 +02001141 pr_err("device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001142 goto err;
1143 }
1144
Minwoo Im43db0592019-09-05 21:04:32 +02001145 pr_debug("ver:%u.%u nvm_vendor:%x\n", geo->major_ver_id,
1146 geo->minor_ver_id, geo->vmnt);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001147
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001148 ret = nvm_core_init(dev);
1149 if (ret) {
Minwoo Im43db0592019-09-05 21:04:32 +02001150 pr_err("could not initialize core structures.\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001151 goto err;
1152 }
1153
Minwoo Im43db0592019-09-05 21:04:32 +02001154 pr_info("registered %s [%u/%u/%u/%u/%u]\n",
Javier Gonzáleza40afad2018-03-30 00:05:14 +02001155 dev->name, dev->geo.ws_min, dev->geo.ws_opt,
1156 dev->geo.num_chk, dev->geo.all_luns,
1157 dev->geo.num_ch);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001158 return 0;
1159err:
Minwoo Im43db0592019-09-05 21:04:32 +02001160 pr_err("failed to initialize nvm\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001161 return ret;
1162}
1163
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001164struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001165{
Igor Konopkoe69397e2019-05-04 20:38:08 +02001166 struct nvm_dev *dev;
1167
1168 dev = kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
1169 if (dev)
1170 kref_init(&dev->ref);
1171
1172 return dev;
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001173}
1174EXPORT_SYMBOL(nvm_alloc_dev);
1175
1176int nvm_register(struct nvm_dev *dev)
1177{
Igor Konopko24828d02018-12-11 20:16:24 +01001178 int ret, exp_pool_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001179
Igor Konopkoe69397e2019-05-04 20:38:08 +02001180 if (!dev->q || !dev->ops) {
1181 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001182 return -EINVAL;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001183 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001184
Matias Bjørling85136c02018-12-11 20:16:20 +01001185 ret = nvm_init(dev);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001186 if (ret) {
1187 kref_put(&dev->ref, nvm_free);
Matias Bjørling85136c02018-12-11 20:16:20 +01001188 return ret;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001189 }
Matias Bjørling85136c02018-12-11 20:16:20 +01001190
Igor Konopko24828d02018-12-11 20:16:24 +01001191 exp_pool_size = max_t(int, PAGE_SIZE,
1192 (NVM_MAX_VLBA * (sizeof(u64) + dev->geo.sos)));
1193 exp_pool_size = round_up(exp_pool_size, PAGE_SIZE);
1194
1195 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist",
1196 exp_pool_size);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001197 if (!dev->dma_pool) {
Minwoo Im43db0592019-09-05 21:04:32 +02001198 pr_err("could not create dma pool\n");
Igor Konopkoe69397e2019-05-04 20:38:08 +02001199 kref_put(&dev->ref, nvm_free);
Matias Bjørling89a09c52018-03-30 00:05:04 +02001200 return -ENOMEM;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001201 }
1202
Matias Bjørling762796b2015-12-06 11:25:49 +01001203 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +01001204 down_write(&nvm_lock);
1205 list_add(&dev->devices, &nvm_devices);
1206 up_write(&nvm_lock);
1207
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001208 return 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001209}
1210EXPORT_SYMBOL(nvm_register);
1211
Matias Bjørlingb0b4e092016-09-16 14:25:07 +02001212void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001213{
Matias Bjørlingade69e22017-01-31 13:17:09 +01001214 struct nvm_target *t, *tmp;
1215
1216 mutex_lock(&dev->mlock);
1217 list_for_each_entry_safe(t, tmp, &dev->targets, list) {
1218 if (t->dev->parent != dev)
1219 continue;
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001220 __nvm_remove_target(t, false);
Igor Konopkoe69397e2019-05-04 20:38:08 +02001221 kref_put(&dev->ref, nvm_free);
Matias Bjørlingade69e22017-01-31 13:17:09 +01001222 }
1223 mutex_unlock(&dev->mlock);
1224
Wenwei Taod0a712c2015-11-28 16:49:28 +01001225 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001226 list_del(&dev->devices);
1227 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +01001228
Igor Konopkoe69397e2019-05-04 20:38:08 +02001229 kref_put(&dev->ref, nvm_free);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001230}
1231EXPORT_SYMBOL(nvm_unregister);
1232
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001233static int __nvm_configure_create(struct nvm_ioctl_create *create)
1234{
1235 struct nvm_dev *dev;
Igor Konopkoe69397e2019-05-04 20:38:08 +02001236 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001237
Wenwei Taod0a712c2015-11-28 16:49:28 +01001238 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001239 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +01001240 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001241
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001242 if (!dev) {
Minwoo Im43db0592019-09-05 21:04:32 +02001243 pr_err("device not found\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001244 return -EINVAL;
1245 }
1246
Igor Konopkoe69397e2019-05-04 20:38:08 +02001247 kref_get(&dev->ref);
1248 ret = nvm_create_tgt(dev, create);
1249 if (ret)
1250 kref_put(&dev->ref, nvm_free);
1251
1252 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001253}
1254
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001255static long nvm_ioctl_info(struct file *file, void __user *arg)
1256{
1257 struct nvm_ioctl_info *info;
1258 struct nvm_tgt_type *tt;
1259 int tgt_iter = 0;
1260
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001261 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
1262 if (IS_ERR(info))
1263 return -EFAULT;
1264
1265 info->version[0] = NVM_VERSION_MAJOR;
1266 info->version[1] = NVM_VERSION_MINOR;
1267 info->version[2] = NVM_VERSION_PATCH;
1268
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001269 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001270 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001271 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
1272
1273 tgt->version[0] = tt->version[0];
1274 tgt->version[1] = tt->version[1];
1275 tgt->version[2] = tt->version[2];
1276 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
1277
1278 tgt_iter++;
1279 }
1280
1281 info->tgtsize = tgt_iter;
Rakesh Pandit88d31ea2017-10-13 14:45:52 +02001282 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001283
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001284 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
1285 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001286 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001287 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001288
1289 kfree(info);
1290 return 0;
1291}
1292
1293static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
1294{
1295 struct nvm_ioctl_get_devices *devices;
1296 struct nvm_dev *dev;
1297 int i = 0;
1298
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001299 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
1300 if (!devices)
1301 return -ENOMEM;
1302
1303 down_write(&nvm_lock);
1304 list_for_each_entry(dev, &nvm_devices, devices) {
1305 struct nvm_ioctl_device_info *info = &devices->info[i];
1306
Javier González6eb08242017-04-15 20:55:49 +02001307 strlcpy(info->devname, dev->name, sizeof(info->devname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001308
Matias Bjørlingade69e22017-01-31 13:17:09 +01001309 /* kept for compatibility */
1310 info->bmversion[0] = 1;
1311 info->bmversion[1] = 0;
1312 info->bmversion[2] = 0;
Javier González6eb08242017-04-15 20:55:49 +02001313 strlcpy(info->bmname, "gennvm", sizeof(info->bmname));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001314 i++;
Matias Bjørlingade69e22017-01-31 13:17:09 +01001315
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001316 if (i > 31) {
Minwoo Im43db0592019-09-05 21:04:32 +02001317 pr_err("max 31 devices can be reported.\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001318 break;
1319 }
1320 }
1321 up_write(&nvm_lock);
1322
1323 devices->nr_devices = i;
1324
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001325 if (copy_to_user(arg, devices,
1326 sizeof(struct nvm_ioctl_get_devices))) {
1327 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001328 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +01001329 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001330
1331 kfree(devices);
1332 return 0;
1333}
1334
1335static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1336{
1337 struct nvm_ioctl_create create;
1338
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001339 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1340 return -EFAULT;
1341
Javier Gonzáleze5392732018-01-05 14:16:14 +01001342 if (create.conf.type == NVM_CONFIG_TYPE_EXTENDED &&
1343 create.conf.e.rsv != 0) {
Minwoo Im43db0592019-09-05 21:04:32 +02001344 pr_err("reserved config field in use\n");
Javier Gonzáleze5392732018-01-05 14:16:14 +01001345 return -EINVAL;
1346 }
1347
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001348 create.dev[DISK_NAME_LEN - 1] = '\0';
1349 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1350 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1351
1352 if (create.flags != 0) {
Javier González4af3f752017-04-15 20:55:45 +02001353 __u32 flags = create.flags;
1354
1355 /* Check for valid flags */
1356 if (flags & NVM_TARGET_FACTORY)
1357 flags &= ~NVM_TARGET_FACTORY;
1358
1359 if (flags) {
Minwoo Im43db0592019-09-05 21:04:32 +02001360 pr_err("flag not supported\n");
Javier González4af3f752017-04-15 20:55:45 +02001361 return -EINVAL;
1362 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001363 }
1364
1365 return __nvm_configure_create(&create);
1366}
1367
1368static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1369{
1370 struct nvm_ioctl_remove remove;
1371
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001372 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1373 return -EFAULT;
1374
1375 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1376
1377 if (remove.flags != 0) {
Minwoo Im43db0592019-09-05 21:04:32 +02001378 pr_err("no flags supported\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001379 return -EINVAL;
1380 }
1381
Igor Konopko843f2ed2019-05-04 20:38:09 +02001382 return nvm_remove_tgt(&remove);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001383}
1384
Matias Bjørlingade69e22017-01-31 13:17:09 +01001385/* kept for compatibility reasons */
Matias Bjørling55696152016-01-12 07:49:37 +01001386static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1387{
1388 struct nvm_ioctl_dev_init init;
1389
Matias Bjørling55696152016-01-12 07:49:37 +01001390 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1391 return -EFAULT;
1392
1393 if (init.flags != 0) {
Minwoo Im43db0592019-09-05 21:04:32 +02001394 pr_err("no flags supported\n");
Matias Bjørling55696152016-01-12 07:49:37 +01001395 return -EINVAL;
1396 }
1397
Matias Bjørlingade69e22017-01-31 13:17:09 +01001398 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001399}
1400
Matias Bjørlingade69e22017-01-31 13:17:09 +01001401/* Kept for compatibility reasons */
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001402static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1403{
1404 struct nvm_ioctl_dev_factory fact;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001405
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001406 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1407 return -EFAULT;
1408
1409 fact.dev[DISK_NAME_LEN - 1] = '\0';
1410
1411 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1412 return -EINVAL;
1413
Matias Bjørlingbf643182016-02-04 15:13:27 +01001414 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001415}
1416
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001417static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1418{
1419 void __user *argp = (void __user *)arg;
1420
Johannes Thumshirn40f962d2018-03-30 00:05:07 +02001421 if (!capable(CAP_SYS_ADMIN))
1422 return -EPERM;
1423
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001424 switch (cmd) {
1425 case NVM_INFO:
1426 return nvm_ioctl_info(file, argp);
1427 case NVM_GET_DEVICES:
1428 return nvm_ioctl_get_devices(file, argp);
1429 case NVM_DEV_CREATE:
1430 return nvm_ioctl_dev_create(file, argp);
1431 case NVM_DEV_REMOVE:
1432 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001433 case NVM_DEV_INIT:
1434 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001435 case NVM_DEV_FACTORY:
1436 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001437 }
1438 return 0;
1439}
1440
1441static const struct file_operations _ctl_fops = {
1442 .open = nonseekable_open,
1443 .unlocked_ioctl = nvm_ctl_ioctl,
1444 .owner = THIS_MODULE,
1445 .llseek = noop_llseek,
1446};
1447
1448static struct miscdevice _nvm_misc = {
1449 .minor = MISC_DYNAMIC_MINOR,
1450 .name = "lightnvm",
1451 .nodename = "lightnvm/control",
1452 .fops = &_ctl_fops,
1453};
Paul Gortmaker389b2a12016-10-29 16:38:41 -04001454builtin_misc_device(_nvm_misc);