blob: 493ce034e687694bf4c8c41f815815f113fe7336 [file] [log] [blame]
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001/*
2 * Copyright (C) 2015 IT University of Copenhagen. All rights reserved.
3 * Initial release: Matias Bjorling <m@bjorling.me>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version
7 * 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
17 * USA.
18 *
19 */
20
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010021#include <linux/list.h>
22#include <linux/types.h>
23#include <linux/sem.h>
24#include <linux/bitmap.h>
25#include <linux/module.h>
26#include <linux/miscdevice.h>
27#include <linux/lightnvm.h>
Matias Bjørling912761622016-01-12 07:49:21 +010028#include <linux/sched/sysctl.h>
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010029
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020030static LIST_HEAD(nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +020031static DECLARE_RWSEM(nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010032static LIST_HEAD(nvm_mgrs);
33static LIST_HEAD(nvm_devices);
34static DECLARE_RWSEM(nvm_lock);
35
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +020036struct nvm_tgt_type *nvm_find_target_type(const char *name, int lock)
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +020037{
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +020038 struct nvm_tgt_type *tmp, *tt = NULL;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +020039
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +020040 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +020041 down_write(&nvm_tgtt_lock);
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +020042
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +020043 list_for_each_entry(tmp, &nvm_tgt_types, list)
44 if (!strcmp(name, tmp->name)) {
45 tt = tmp;
46 break;
47 }
48
49 if (lock)
Matias Bjørling5cd90782016-07-07 09:54:17 +020050 up_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +020051 return tt;
Simon A. F. Lund6f8645c2016-05-06 20:03:03 +020052}
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +020053EXPORT_SYMBOL(nvm_find_target_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010054
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020055int nvm_register_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010056{
57 int ret = 0;
58
Matias Bjørling5cd90782016-07-07 09:54:17 +020059 down_write(&nvm_tgtt_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +020060 if (nvm_find_target_type(tt->name, 0))
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010061 ret = -EEXIST;
62 else
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020063 list_add(&tt->list, &nvm_tgt_types);
Matias Bjørling5cd90782016-07-07 09:54:17 +020064 up_write(&nvm_tgtt_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010065
66 return ret;
67}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020068EXPORT_SYMBOL(nvm_register_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010069
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020070void nvm_unregister_tgt_type(struct nvm_tgt_type *tt)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010071{
72 if (!tt)
73 return;
74
75 down_write(&nvm_lock);
76 list_del(&tt->list);
77 up_write(&nvm_lock);
78}
Simon A. F. Lund6063fe32016-05-06 20:03:02 +020079EXPORT_SYMBOL(nvm_unregister_tgt_type);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010080
81void *nvm_dev_dma_alloc(struct nvm_dev *dev, gfp_t mem_flags,
82 dma_addr_t *dma_handler)
83{
Javier González75b85642016-05-06 20:03:13 +020084 return dev->ops->dev_dma_alloc(dev, dev->dma_pool, mem_flags,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010085 dma_handler);
86}
87EXPORT_SYMBOL(nvm_dev_dma_alloc);
88
Javier González75b85642016-05-06 20:03:13 +020089void nvm_dev_dma_free(struct nvm_dev *dev, void *addr,
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010090 dma_addr_t dma_handler)
91{
Javier González75b85642016-05-06 20:03:13 +020092 dev->ops->dev_dma_free(dev->dma_pool, addr, dma_handler);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +010093}
94EXPORT_SYMBOL(nvm_dev_dma_free);
95
96static struct nvmm_type *nvm_find_mgr_type(const char *name)
97{
98 struct nvmm_type *mt;
99
100 list_for_each_entry(mt, &nvm_mgrs, list)
101 if (!strcmp(name, mt->name))
102 return mt;
103
104 return NULL;
105}
106
Johannes Thumshirn58eaaf92016-07-07 09:54:11 +0200107static struct nvmm_type *nvm_init_mgr(struct nvm_dev *dev)
Matias Bjørling762796b2015-12-06 11:25:49 +0100108{
109 struct nvmm_type *mt;
110 int ret;
111
112 lockdep_assert_held(&nvm_lock);
113
114 list_for_each_entry(mt, &nvm_mgrs, list) {
Matias Bjørlingb7692072016-01-12 07:49:38 +0100115 if (strncmp(dev->sb.mmtype, mt->name, NVM_MMTYPE_LEN))
116 continue;
117
Matias Bjørling762796b2015-12-06 11:25:49 +0100118 ret = mt->register_mgr(dev);
119 if (ret < 0) {
120 pr_err("nvm: media mgr failed to init (%d) on dev %s\n",
121 ret, dev->name);
122 return NULL; /* initialization failed */
123 } else if (ret > 0)
124 return mt;
125 }
126
127 return NULL;
128}
129
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100130int nvm_register_mgr(struct nvmm_type *mt)
131{
Matias Bjørling762796b2015-12-06 11:25:49 +0100132 struct nvm_dev *dev;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100133 int ret = 0;
134
135 down_write(&nvm_lock);
Matias Bjørling762796b2015-12-06 11:25:49 +0100136 if (nvm_find_mgr_type(mt->name)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100137 ret = -EEXIST;
Matias Bjørling762796b2015-12-06 11:25:49 +0100138 goto finish;
139 } else {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100140 list_add(&mt->list, &nvm_mgrs);
Matias Bjørling762796b2015-12-06 11:25:49 +0100141 }
142
143 /* try to register media mgr if any device have none configured */
144 list_for_each_entry(dev, &nvm_devices, devices) {
145 if (dev->mt)
146 continue;
147
148 dev->mt = nvm_init_mgr(dev);
149 }
150finish:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100151 up_write(&nvm_lock);
152
153 return ret;
154}
155EXPORT_SYMBOL(nvm_register_mgr);
156
157void nvm_unregister_mgr(struct nvmm_type *mt)
158{
159 if (!mt)
160 return;
161
162 down_write(&nvm_lock);
163 list_del(&mt->list);
164 up_write(&nvm_lock);
165}
166EXPORT_SYMBOL(nvm_unregister_mgr);
167
168static struct nvm_dev *nvm_find_nvm_dev(const char *name)
169{
170 struct nvm_dev *dev;
171
172 list_for_each_entry(dev, &nvm_devices, devices)
173 if (!strcmp(name, dev->name))
174 return dev;
175
176 return NULL;
177}
178
Javier González529435e812016-07-07 09:54:08 +0200179void nvm_mark_blk(struct nvm_dev *dev, struct ppa_addr ppa, int type)
180{
181 return dev->mt->mark_blk(dev, ppa, type);
182}
183EXPORT_SYMBOL(nvm_mark_blk);
184
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100185int nvm_set_bb_tbl(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas,
186 int type)
187{
188 struct nvm_rq rqd;
189 int ret;
190
191 if (nr_ppas > dev->ops->max_phys_sect) {
192 pr_err("nvm: unable to update all sysblocks atomically\n");
193 return -EINVAL;
194 }
195
196 memset(&rqd, 0, sizeof(struct nvm_rq));
197
198 nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
199 nvm_generic_to_addr_mode(dev, &rqd);
200
201 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
202 nvm_free_rqd_ppalist(dev, &rqd);
203 if (ret) {
204 pr_err("nvm: sysblk failed bb mark\n");
205 return -EINVAL;
206 }
207
208 return 0;
209}
210EXPORT_SYMBOL(nvm_set_bb_tbl);
211
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100212int nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
213{
214 return dev->mt->submit_io(dev, rqd);
215}
216EXPORT_SYMBOL(nvm_submit_io);
217
Javier Gonzálezbb314972016-11-28 22:38:54 +0100218int nvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk, int flags)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100219{
Javier Gonzálezbb314972016-11-28 22:38:54 +0100220 return dev->mt->erase_blk(dev, blk, flags);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100221}
222EXPORT_SYMBOL(nvm_erase_blk);
223
Matias Bjørling069368e2016-01-12 07:49:19 +0100224void nvm_addr_to_generic_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
225{
226 int i;
227
Javier González6d5be952016-05-06 20:03:20 +0200228 if (rqd->nr_ppas > 1) {
229 for (i = 0; i < rqd->nr_ppas; i++)
Matias Bjørling069368e2016-01-12 07:49:19 +0100230 rqd->ppa_list[i] = dev_to_generic_addr(dev,
231 rqd->ppa_list[i]);
232 } else {
233 rqd->ppa_addr = dev_to_generic_addr(dev, rqd->ppa_addr);
234 }
235}
236EXPORT_SYMBOL(nvm_addr_to_generic_mode);
237
238void nvm_generic_to_addr_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
239{
240 int i;
241
Javier González6d5be952016-05-06 20:03:20 +0200242 if (rqd->nr_ppas > 1) {
243 for (i = 0; i < rqd->nr_ppas; i++)
Matias Bjørling069368e2016-01-12 07:49:19 +0100244 rqd->ppa_list[i] = generic_to_dev_addr(dev,
245 rqd->ppa_list[i]);
246 } else {
247 rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr);
248 }
249}
250EXPORT_SYMBOL(nvm_generic_to_addr_mode);
251
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100252int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd,
Matias Bjørling8680f162016-07-07 09:54:22 +0200253 const struct ppa_addr *ppas, int nr_ppas, int vblk)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100254{
Javier González8e79b5c2016-11-28 22:39:06 +0100255 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100256 int i, plane_cnt, pl_idx;
Matias Bjørling8680f162016-07-07 09:54:22 +0200257 struct ppa_addr ppa;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100258
Javier González8e79b5c2016-11-28 22:39:06 +0100259 if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
Javier González6d5be952016-05-06 20:03:20 +0200260 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100261 rqd->ppa_addr = ppas[0];
262
263 return 0;
264 }
265
Javier González6d5be952016-05-06 20:03:20 +0200266 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100267 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
268 if (!rqd->ppa_list) {
269 pr_err("nvm: failed to allocate dma memory\n");
270 return -ENOMEM;
271 }
272
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200273 if (!vblk) {
274 for (i = 0; i < nr_ppas; i++)
275 rqd->ppa_list[i] = ppas[i];
276 } else {
Javier González8e79b5c2016-11-28 22:39:06 +0100277 plane_cnt = geo->plane_mode;
Javier González6d5be952016-05-06 20:03:20 +0200278 rqd->nr_ppas *= plane_cnt;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200279
Matias Bjørling556755e2016-01-12 07:49:26 +0100280 for (i = 0; i < nr_ppas; i++) {
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200281 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
Matias Bjørling8680f162016-07-07 09:54:22 +0200282 ppa = ppas[i];
283 ppa.g.pl = pl_idx;
284 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200285 }
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100286 }
287 }
288
289 return 0;
290}
291EXPORT_SYMBOL(nvm_set_rqd_ppalist);
292
293void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd)
294{
295 if (!rqd->ppa_list)
296 return;
297
298 nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list);
299}
300EXPORT_SYMBOL(nvm_free_rqd_ppalist);
301
Javier Gonzálezbb314972016-11-28 22:38:54 +0100302int nvm_erase_ppa(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas,
303 int flags)
Matias Bjørling069368e2016-01-12 07:49:19 +0100304{
Matias Bjørling069368e2016-01-12 07:49:19 +0100305 struct nvm_rq rqd;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100306 int ret;
Matias Bjørling069368e2016-01-12 07:49:19 +0100307
308 if (!dev->ops->erase_block)
309 return 0;
310
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100311 memset(&rqd, 0, sizeof(struct nvm_rq));
Matias Bjørling069368e2016-01-12 07:49:19 +0100312
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200313 ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100314 if (ret)
315 return ret;
Matias Bjørling069368e2016-01-12 07:49:19 +0100316
317 nvm_generic_to_addr_mode(dev, &rqd);
318
Javier Gonzálezbb314972016-11-28 22:38:54 +0100319 rqd.flags = flags;
320
Matias Bjørling069368e2016-01-12 07:49:19 +0100321 ret = dev->ops->erase_block(dev, &rqd);
322
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100323 nvm_free_rqd_ppalist(dev, &rqd);
Matias Bjørling069368e2016-01-12 07:49:19 +0100324
325 return ret;
326}
327EXPORT_SYMBOL(nvm_erase_ppa);
328
Matias Bjørling912761622016-01-12 07:49:21 +0100329void nvm_end_io(struct nvm_rq *rqd, int error)
330{
Matias Bjørling72d256e2016-01-12 07:49:29 +0100331 rqd->error = error;
332 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100333}
334EXPORT_SYMBOL(nvm_end_io);
335
Matias Bjørling09719b62016-01-12 07:49:30 +0100336static void nvm_end_io_sync(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100337{
338 struct completion *waiting = rqd->wait;
339
340 rqd->wait = NULL;
341
342 complete(waiting);
343}
344
Matias Bjørlingd9e46d52016-07-07 09:54:23 +0200345static int __nvm_submit_ppa(struct nvm_dev *dev, struct nvm_rq *rqd, int opcode,
Matias Bjørling1145e632016-05-06 20:02:56 +0200346 int flags, void *buf, int len)
Matias Bjørling09719b62016-01-12 07:49:30 +0100347{
348 DECLARE_COMPLETION_ONSTACK(wait);
Matias Bjørling09719b62016-01-12 07:49:30 +0100349 struct bio *bio;
350 int ret;
351 unsigned long hang_check;
352
353 bio = bio_map_kern(dev->q, buf, len, GFP_KERNEL);
354 if (IS_ERR_OR_NULL(bio))
355 return -ENOMEM;
356
Matias Bjørling1145e632016-05-06 20:02:56 +0200357 nvm_generic_to_addr_mode(dev, rqd);
Matias Bjørling09719b62016-01-12 07:49:30 +0100358
Matias Bjørling1145e632016-05-06 20:02:56 +0200359 rqd->dev = dev;
360 rqd->opcode = opcode;
361 rqd->flags = flags;
362 rqd->bio = bio;
363 rqd->wait = &wait;
364 rqd->end_io = nvm_end_io_sync;
Matias Bjørling09719b62016-01-12 07:49:30 +0100365
Matias Bjørling1145e632016-05-06 20:02:56 +0200366 ret = dev->ops->submit_io(dev, rqd);
Matias Bjørlingecfb40c2016-05-06 20:02:55 +0200367 if (ret) {
Matias Bjørlingecfb40c2016-05-06 20:02:55 +0200368 bio_put(bio);
369 return ret;
370 }
Matias Bjørling09719b62016-01-12 07:49:30 +0100371
372 /* Prevent hang_check timer from firing at us during very long I/O */
373 hang_check = sysctl_hung_task_timeout_secs;
374 if (hang_check)
Matias Bjørling12624af2016-07-07 09:54:13 +0200375 while (!wait_for_completion_io_timeout(&wait,
376 hang_check * (HZ/2)))
377 ;
Matias Bjørling09719b62016-01-12 07:49:30 +0100378 else
379 wait_for_completion_io(&wait);
380
Matias Bjørling1145e632016-05-06 20:02:56 +0200381 return rqd->error;
382}
383
384/**
385 * nvm_submit_ppa_list - submit user-defined ppa list to device. The user must
386 * take to free ppa list if necessary.
387 * @dev: device
388 * @ppa_list: user created ppa_list
389 * @nr_ppas: length of ppa_list
390 * @opcode: device opcode
391 * @flags: device flags
392 * @buf: data buffer
393 * @len: data buffer length
394 */
395int nvm_submit_ppa_list(struct nvm_dev *dev, struct ppa_addr *ppa_list,
396 int nr_ppas, int opcode, int flags, void *buf, int len)
397{
398 struct nvm_rq rqd;
399
400 if (dev->ops->max_phys_sect < nr_ppas)
401 return -EINVAL;
402
403 memset(&rqd, 0, sizeof(struct nvm_rq));
404
Javier González6d5be952016-05-06 20:03:20 +0200405 rqd.nr_ppas = nr_ppas;
Matias Bjørling1145e632016-05-06 20:02:56 +0200406 if (nr_ppas > 1)
407 rqd.ppa_list = ppa_list;
408 else
409 rqd.ppa_addr = ppa_list[0];
410
411 return __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
412}
413EXPORT_SYMBOL(nvm_submit_ppa_list);
414
415/**
416 * nvm_submit_ppa - submit PPAs to device. PPAs will automatically be unfolded
417 * as single, dual, quad plane PPAs depending on device type.
418 * @dev: device
419 * @ppa: user created ppa_list
420 * @nr_ppas: length of ppa_list
421 * @opcode: device opcode
422 * @flags: device flags
423 * @buf: data buffer
424 * @len: data buffer length
425 */
426int nvm_submit_ppa(struct nvm_dev *dev, struct ppa_addr *ppa, int nr_ppas,
427 int opcode, int flags, void *buf, int len)
428{
429 struct nvm_rq rqd;
430 int ret;
431
432 memset(&rqd, 0, sizeof(struct nvm_rq));
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200433 ret = nvm_set_rqd_ppalist(dev, &rqd, ppa, nr_ppas, 1);
Matias Bjørling1145e632016-05-06 20:02:56 +0200434 if (ret)
435 return ret;
436
437 ret = __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
438
Matias Bjørling09719b62016-01-12 07:49:30 +0100439 nvm_free_rqd_ppalist(dev, &rqd);
440
Matias Bjørling1145e632016-05-06 20:02:56 +0200441 return ret;
Matias Bjørling09719b62016-01-12 07:49:30 +0100442}
443EXPORT_SYMBOL(nvm_submit_ppa);
444
Matias Bjørling22e8c972016-05-06 20:02:58 +0200445/*
446 * folds a bad block list from its plane representation to its virtual
447 * block representation. The fold is done in place and reduced size is
448 * returned.
449 *
450 * If any of the planes status are bad or grown bad block, the virtual block
451 * is marked bad. If not bad, the first plane state acts as the block state.
452 */
453int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
454{
Javier González8e79b5c2016-11-28 22:39:06 +0100455 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200456 int blk, offset, pl, blktype;
457
Javier González8e79b5c2016-11-28 22:39:06 +0100458 if (nr_blks != geo->blks_per_lun * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200459 return -EINVAL;
460
Javier González8e79b5c2016-11-28 22:39:06 +0100461 for (blk = 0; blk < geo->blks_per_lun; blk++) {
462 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200463 blktype = blks[offset];
464
465 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100466 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200467 if (blks[offset + pl] &
468 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
469 blktype = blks[offset + pl];
470 break;
471 }
472 }
473
474 blks[blk] = blktype;
475 }
476
Javier González8e79b5c2016-11-28 22:39:06 +0100477 return geo->blks_per_lun;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200478}
479EXPORT_SYMBOL(nvm_bb_tbl_fold);
480
Matias Bjørlinge11903f2016-05-06 20:03:05 +0200481int nvm_get_bb_tbl(struct nvm_dev *dev, struct ppa_addr ppa, u8 *blks)
482{
483 ppa = generic_to_dev_addr(dev, ppa);
484
485 return dev->ops->get_bb_tbl(dev, ppa, blks);
486}
487EXPORT_SYMBOL(nvm_get_bb_tbl);
488
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100489static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
490{
Javier González8e79b5c2016-11-28 22:39:06 +0100491 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100492 int i;
493
Javier González8e79b5c2016-11-28 22:39:06 +0100494 dev->lps_per_blk = geo->pgs_per_blk;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100495 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
496 if (!dev->lptbl)
497 return -ENOMEM;
498
499 /* Just a linear array */
500 for (i = 0; i < dev->lps_per_blk; i++)
501 dev->lptbl[i] = i;
502
503 return 0;
504}
505
506static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
507{
508 int i, p;
509 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
510
511 if (!mlc->num_pairs)
512 return 0;
513
514 dev->lps_per_blk = mlc->num_pairs;
515 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
516 if (!dev->lptbl)
517 return -ENOMEM;
518
519 /* The lower page table encoding consists of a list of bytes, where each
520 * has a lower and an upper half. The first half byte maintains the
521 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200522 * previous incrementation value
523 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100524 dev->lptbl[0] = mlc->pairs[0] & 0xF;
525 for (i = 1; i < dev->lps_per_blk; i++) {
526 p = mlc->pairs[i >> 1];
527 if (i & 0x1) /* upper */
528 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
529 else /* lower */
530 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
531 }
532
533 return 0;
534}
535
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100536static int nvm_core_init(struct nvm_dev *dev)
537{
538 struct nvm_id *id = &dev->identity;
539 struct nvm_id_group *grp = &id->groups[0];
Javier González8e79b5c2016-11-28 22:39:06 +0100540 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200541 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100542
Javier González8e79b5c2016-11-28 22:39:06 +0100543 /* Whole device values */
544 geo->nr_chnls = grp->num_ch;
545 geo->luns_per_chnl = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100546
Javier González8e79b5c2016-11-28 22:39:06 +0100547 /* Generic device values */
548 geo->pgs_per_blk = grp->num_pg;
549 geo->blks_per_lun = grp->num_blk;
550 geo->nr_planes = grp->num_pln;
551 geo->fpg_size = grp->fpg_sz;
552 geo->pfpg_size = grp->fpg_sz * grp->num_pln;
553 geo->sec_size = grp->csecs;
554 geo->oob_size = grp->sos;
555 geo->sec_per_pg = grp->fpg_sz / grp->csecs;
556 geo->mccap = grp->mccap;
557 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
558
559 geo->plane_mode = NVM_PLANE_SINGLE;
560 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100561
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100562 if (grp->mpos & 0x020202)
Javier González8e79b5c2016-11-28 22:39:06 +0100563 geo->plane_mode = NVM_PLANE_DOUBLE;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100564 if (grp->mpos & 0x040404)
Javier González8e79b5c2016-11-28 22:39:06 +0100565 geo->plane_mode = NVM_PLANE_QUAD;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100566
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200567 if (grp->mtype != 0) {
568 pr_err("nvm: memory type not supported\n");
569 return -EINVAL;
570 }
571
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100572 /* calculated values */
Javier González8e79b5c2016-11-28 22:39:06 +0100573 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
574 geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
575 geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
576 geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100577
Javier González8e79b5c2016-11-28 22:39:06 +0100578 dev->total_secs = geo->nr_luns * geo->sec_per_lun;
579 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100580 sizeof(unsigned long), GFP_KERNEL);
581 if (!dev->lun_map)
582 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200583
584 switch (grp->fmtype) {
585 case NVM_ID_FMTYPE_SLC:
586 if (nvm_init_slc_tbl(dev, grp)) {
587 ret = -ENOMEM;
588 goto err_fmtype;
589 }
590 break;
591 case NVM_ID_FMTYPE_MLC:
592 if (nvm_init_mlc_tbl(dev, grp)) {
593 ret = -ENOMEM;
594 goto err_fmtype;
595 }
596 break;
597 default:
598 pr_err("nvm: flash type not supported\n");
599 ret = -EINVAL;
600 goto err_fmtype;
601 }
602
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100603 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100604 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100605
Javier González8e79b5c2016-11-28 22:39:06 +0100606 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200607
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100608 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200609err_fmtype:
610 kfree(dev->lun_map);
611 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100612}
613
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200614static void nvm_free_mgr(struct nvm_dev *dev)
615{
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200616 if (!dev->mt)
617 return;
618
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200619 dev->mt->unregister_mgr(dev);
620 dev->mt = NULL;
621}
622
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200623void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100624{
625 if (!dev)
626 return;
627
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200628 nvm_free_mgr(dev);
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100629
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200630 if (dev->dma_pool)
631 dev->ops->destroy_dma_pool(dev->dma_pool);
632
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100633 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200634 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200635 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100636}
637
638static int nvm_init(struct nvm_dev *dev)
639{
Javier González8e79b5c2016-11-28 22:39:06 +0100640 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100641 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100642
643 if (!dev->q || !dev->ops)
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100644 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100645
Matias Bjørling16f26c32015-12-06 11:25:48 +0100646 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100647 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100648 goto err;
649 }
650
651 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
652 dev->identity.ver_id, dev->identity.vmnt,
653 dev->identity.cgrps);
654
655 if (dev->identity.ver_id != 1) {
656 pr_err("nvm: device not supported by kernel.");
657 goto err;
658 }
659
660 if (dev->identity.cgrps != 1) {
661 pr_err("nvm: only one group configuration supported.");
662 goto err;
663 }
664
665 ret = nvm_core_init(dev);
666 if (ret) {
667 pr_err("nvm: could not initialize core structures.\n");
668 goto err;
669 }
670
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100671 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +0100672 dev->name, geo->sec_per_pg, geo->nr_planes,
673 geo->pgs_per_blk, geo->blks_per_lun,
674 geo->nr_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100675 return 0;
676err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100677 pr_err("nvm: failed to initialize nvm\n");
678 return ret;
679}
680
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200681struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100682{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200683 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
684}
685EXPORT_SYMBOL(nvm_alloc_dev);
686
687int nvm_register(struct nvm_dev *dev)
688{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100689 int ret;
690
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100691 ret = nvm_init(dev);
692 if (ret)
693 goto err_init;
694
Wenwei Taod1601472015-11-28 16:49:25 +0100695 if (dev->ops->max_phys_sect > 256) {
696 pr_info("nvm: max sectors supported is 256.\n");
697 ret = -EINVAL;
698 goto err_init;
699 }
700
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100701 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +0200702 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
703 if (!dev->dma_pool) {
704 pr_err("nvm: could not create dma pool\n");
Matias Bjørling93e70c12015-11-20 13:47:54 +0100705 ret = -ENOMEM;
706 goto err_init;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100707 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100708 }
709
Matias Bjørlingbf643182016-02-04 15:13:27 +0100710 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
711 ret = nvm_get_sysblock(dev, &dev->sb);
712 if (!ret)
713 pr_err("nvm: device not initialized.\n");
714 else if (ret < 0)
715 pr_err("nvm: err (%d) on device initialization\n", ret);
716 }
Matias Bjørlingb7692072016-01-12 07:49:38 +0100717
Matias Bjørling762796b2015-12-06 11:25:49 +0100718 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100719 down_write(&nvm_lock);
Matias Bjørlingb7692072016-01-12 07:49:38 +0100720 if (ret > 0)
721 dev->mt = nvm_init_mgr(dev);
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100722 list_add(&dev->devices, &nvm_devices);
723 up_write(&nvm_lock);
724
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100725 return 0;
726err_init:
Wenwei Taoda1e2842016-03-03 15:06:38 +0100727 kfree(dev->lun_map);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100728 return ret;
729}
730EXPORT_SYMBOL(nvm_register);
731
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200732void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100733{
Wenwei Taod0a712c2015-11-28 16:49:28 +0100734 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100735 list_del(&dev->devices);
736 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +0100737
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100738 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100739}
740EXPORT_SYMBOL(nvm_unregister);
741
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100742static int __nvm_configure_create(struct nvm_ioctl_create *create)
743{
744 struct nvm_dev *dev;
745 struct nvm_ioctl_create_simple *s;
746
Wenwei Taod0a712c2015-11-28 16:49:28 +0100747 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100748 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100749 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200750
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100751 if (!dev) {
752 pr_err("nvm: device not found\n");
753 return -EINVAL;
754 }
755
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200756 if (!dev->mt) {
757 pr_info("nvm: device has no media manager registered.\n");
758 return -ENODEV;
759 }
760
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100761 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
762 pr_err("nvm: config type not valid\n");
763 return -EINVAL;
764 }
765 s = &create->conf.s;
766
Javier González8e79b5c2016-11-28 22:39:06 +0100767 if (s->lun_begin > s->lun_end || s->lun_end > dev->geo.nr_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100768 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Javier González8e79b5c2016-11-28 22:39:06 +0100769 s->lun_begin, s->lun_end, dev->geo.nr_luns);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100770 return -EINVAL;
771 }
772
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200773 return dev->mt->create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100774}
775
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100776static long nvm_ioctl_info(struct file *file, void __user *arg)
777{
778 struct nvm_ioctl_info *info;
779 struct nvm_tgt_type *tt;
780 int tgt_iter = 0;
781
782 if (!capable(CAP_SYS_ADMIN))
783 return -EPERM;
784
785 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
786 if (IS_ERR(info))
787 return -EFAULT;
788
789 info->version[0] = NVM_VERSION_MAJOR;
790 info->version[1] = NVM_VERSION_MINOR;
791 info->version[2] = NVM_VERSION_PATCH;
792
793 down_write(&nvm_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200794 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100795 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
796
797 tgt->version[0] = tt->version[0];
798 tgt->version[1] = tt->version[1];
799 tgt->version[2] = tt->version[2];
800 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
801
802 tgt_iter++;
803 }
804
805 info->tgtsize = tgt_iter;
806 up_write(&nvm_lock);
807
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100808 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
809 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100810 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100811 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100812
813 kfree(info);
814 return 0;
815}
816
817static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
818{
819 struct nvm_ioctl_get_devices *devices;
820 struct nvm_dev *dev;
821 int i = 0;
822
823 if (!capable(CAP_SYS_ADMIN))
824 return -EPERM;
825
826 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
827 if (!devices)
828 return -ENOMEM;
829
830 down_write(&nvm_lock);
831 list_for_each_entry(dev, &nvm_devices, devices) {
832 struct nvm_ioctl_device_info *info = &devices->info[i];
833
834 sprintf(info->devname, "%s", dev->name);
835 if (dev->mt) {
836 info->bmversion[0] = dev->mt->version[0];
837 info->bmversion[1] = dev->mt->version[1];
838 info->bmversion[2] = dev->mt->version[2];
839 sprintf(info->bmname, "%s", dev->mt->name);
840 } else {
841 sprintf(info->bmname, "none");
842 }
843
844 i++;
845 if (i > 31) {
846 pr_err("nvm: max 31 devices can be reported.\n");
847 break;
848 }
849 }
850 up_write(&nvm_lock);
851
852 devices->nr_devices = i;
853
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100854 if (copy_to_user(arg, devices,
855 sizeof(struct nvm_ioctl_get_devices))) {
856 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100857 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100858 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100859
860 kfree(devices);
861 return 0;
862}
863
864static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
865{
866 struct nvm_ioctl_create create;
867
868 if (!capable(CAP_SYS_ADMIN))
869 return -EPERM;
870
871 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
872 return -EFAULT;
873
874 create.dev[DISK_NAME_LEN - 1] = '\0';
875 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
876 create.tgtname[DISK_NAME_LEN - 1] = '\0';
877
878 if (create.flags != 0) {
879 pr_err("nvm: no flags supported\n");
880 return -EINVAL;
881 }
882
883 return __nvm_configure_create(&create);
884}
885
886static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
887{
888 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200889 struct nvm_dev *dev;
890 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100891
892 if (!capable(CAP_SYS_ADMIN))
893 return -EPERM;
894
895 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
896 return -EFAULT;
897
898 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
899
900 if (remove.flags != 0) {
901 pr_err("nvm: no flags supported\n");
902 return -EINVAL;
903 }
904
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200905 list_for_each_entry(dev, &nvm_devices, devices) {
906 ret = dev->mt->remove_tgt(dev, &remove);
907 if (!ret)
908 break;
909 }
910
911 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100912}
913
Matias Bjørling55696152016-01-12 07:49:37 +0100914static void nvm_setup_nvm_sb_info(struct nvm_sb_info *info)
915{
916 info->seqnr = 1;
917 info->erase_cnt = 0;
918 info->version = 1;
919}
920
921static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init *init)
922{
923 struct nvm_dev *dev;
924 struct nvm_sb_info info;
Matias Bjørlingb7692072016-01-12 07:49:38 +0100925 int ret;
Matias Bjørling55696152016-01-12 07:49:37 +0100926
927 down_write(&nvm_lock);
928 dev = nvm_find_nvm_dev(init->dev);
929 up_write(&nvm_lock);
930 if (!dev) {
931 pr_err("nvm: device not found\n");
932 return -EINVAL;
933 }
934
935 nvm_setup_nvm_sb_info(&info);
936
937 strncpy(info.mmtype, init->mmtype, NVM_MMTYPE_LEN);
938 info.fs_ppa.ppa = -1;
939
Matias Bjørlingbf643182016-02-04 15:13:27 +0100940 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
941 ret = nvm_init_sysblock(dev, &info);
942 if (ret)
943 return ret;
944 }
Matias Bjørlingb7692072016-01-12 07:49:38 +0100945
946 memcpy(&dev->sb, &info, sizeof(struct nvm_sb_info));
947
948 down_write(&nvm_lock);
949 dev->mt = nvm_init_mgr(dev);
950 up_write(&nvm_lock);
951
952 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +0100953}
954
955static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
956{
957 struct nvm_ioctl_dev_init init;
958
959 if (!capable(CAP_SYS_ADMIN))
960 return -EPERM;
961
962 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
963 return -EFAULT;
964
965 if (init.flags != 0) {
966 pr_err("nvm: no flags supported\n");
967 return -EINVAL;
968 }
969
970 init.dev[DISK_NAME_LEN - 1] = '\0';
971
972 return __nvm_ioctl_dev_init(&init);
973}
974
Matias Bjørling8b4970c2016-01-12 07:49:39 +0100975static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
976{
977 struct nvm_ioctl_dev_factory fact;
978 struct nvm_dev *dev;
979
980 if (!capable(CAP_SYS_ADMIN))
981 return -EPERM;
982
983 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
984 return -EFAULT;
985
986 fact.dev[DISK_NAME_LEN - 1] = '\0';
987
988 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
989 return -EINVAL;
990
991 down_write(&nvm_lock);
992 dev = nvm_find_nvm_dev(fact.dev);
993 up_write(&nvm_lock);
994 if (!dev) {
995 pr_err("nvm: device not found\n");
996 return -EINVAL;
997 }
998
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200999 nvm_free_mgr(dev);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001000
Matias Bjørlingbf643182016-02-04 15:13:27 +01001001 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT)
1002 return nvm_dev_factory(dev, fact.flags);
1003
1004 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001005}
1006
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001007static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1008{
1009 void __user *argp = (void __user *)arg;
1010
1011 switch (cmd) {
1012 case NVM_INFO:
1013 return nvm_ioctl_info(file, argp);
1014 case NVM_GET_DEVICES:
1015 return nvm_ioctl_get_devices(file, argp);
1016 case NVM_DEV_CREATE:
1017 return nvm_ioctl_dev_create(file, argp);
1018 case NVM_DEV_REMOVE:
1019 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001020 case NVM_DEV_INIT:
1021 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001022 case NVM_DEV_FACTORY:
1023 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001024 }
1025 return 0;
1026}
1027
1028static const struct file_operations _ctl_fops = {
1029 .open = nonseekable_open,
1030 .unlocked_ioctl = nvm_ctl_ioctl,
1031 .owner = THIS_MODULE,
1032 .llseek = noop_llseek,
1033};
1034
1035static struct miscdevice _nvm_misc = {
1036 .minor = MISC_DYNAMIC_MINOR,
1037 .name = "lightnvm",
1038 .nodename = "lightnvm/control",
1039 .fops = &_ctl_fops,
1040};
PrasannaKumar Muralidharand6a38c02016-08-25 22:30:50 +05301041module_misc_device(_nvm_misc);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001042
1043MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR);
1044
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001045MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
1046MODULE_LICENSE("GPL v2");
1047MODULE_VERSION("0.1");