blob: 69f261e650bcad0ac8965ae151b411a2f2f89ffe [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áleza24ba462016-11-28 22:38:56 +0100179int nvm_set_bb_tbl(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas,
180 int type)
181{
182 struct nvm_rq rqd;
183 int ret;
184
185 if (nr_ppas > dev->ops->max_phys_sect) {
186 pr_err("nvm: unable to update all sysblocks atomically\n");
187 return -EINVAL;
188 }
189
190 memset(&rqd, 0, sizeof(struct nvm_rq));
191
192 nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
193 nvm_generic_to_addr_mode(dev, &rqd);
194
195 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
196 nvm_free_rqd_ppalist(dev, &rqd);
197 if (ret) {
198 pr_err("nvm: sysblk failed bb mark\n");
199 return -EINVAL;
200 }
201
202 return 0;
203}
204EXPORT_SYMBOL(nvm_set_bb_tbl);
205
Javier González8e536242016-11-28 22:39:10 +0100206int nvm_submit_io(struct nvm_tgt_dev *tgt_dev, struct nvm_rq *rqd)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100207{
Javier González8e536242016-11-28 22:39:10 +0100208 struct nvm_dev *dev = tgt_dev->parent;
209
210 return dev->mt->submit_io(tgt_dev, rqd);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100211}
212EXPORT_SYMBOL(nvm_submit_io);
213
Javier González8e536242016-11-28 22:39:10 +0100214int nvm_erase_blk(struct nvm_tgt_dev *tgt_dev, struct ppa_addr *p, int flags)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100215{
Javier González8e536242016-11-28 22:39:10 +0100216 struct nvm_dev *dev = tgt_dev->parent;
217
218 return dev->mt->erase_blk(tgt_dev, p, flags);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100219}
220EXPORT_SYMBOL(nvm_erase_blk);
221
Javier González959e9112016-11-28 22:39:11 +0100222int nvm_get_l2p_tbl(struct nvm_dev *dev, u64 slba, u32 nlb,
223 nvm_l2p_update_fn *update_l2p, void *priv)
224{
225 if (!dev->ops->get_l2p_tbl)
226 return 0;
227
228 return dev->ops->get_l2p_tbl(dev, slba, nlb, update_l2p, priv);
229}
230EXPORT_SYMBOL(nvm_get_l2p_tbl);
231
232int nvm_get_area(struct nvm_dev *dev, sector_t *lba, sector_t len)
233{
234 return dev->mt->get_area(dev, lba, len);
235}
236EXPORT_SYMBOL(nvm_get_area);
237
238void nvm_put_area(struct nvm_dev *dev, sector_t lba)
239{
240 dev->mt->put_area(dev, lba);
241}
242EXPORT_SYMBOL(nvm_put_area);
243
Matias Bjørling069368e2016-01-12 07:49:19 +0100244void nvm_addr_to_generic_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
245{
246 int i;
247
Javier González6d5be952016-05-06 20:03:20 +0200248 if (rqd->nr_ppas > 1) {
249 for (i = 0; i < rqd->nr_ppas; i++)
Matias Bjørling069368e2016-01-12 07:49:19 +0100250 rqd->ppa_list[i] = dev_to_generic_addr(dev,
251 rqd->ppa_list[i]);
252 } else {
253 rqd->ppa_addr = dev_to_generic_addr(dev, rqd->ppa_addr);
254 }
255}
256EXPORT_SYMBOL(nvm_addr_to_generic_mode);
257
258void nvm_generic_to_addr_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
259{
260 int i;
261
Javier González6d5be952016-05-06 20:03:20 +0200262 if (rqd->nr_ppas > 1) {
263 for (i = 0; i < rqd->nr_ppas; i++)
Matias Bjørling069368e2016-01-12 07:49:19 +0100264 rqd->ppa_list[i] = generic_to_dev_addr(dev,
265 rqd->ppa_list[i]);
266 } else {
267 rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr);
268 }
269}
270EXPORT_SYMBOL(nvm_generic_to_addr_mode);
271
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100272int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd,
Matias Bjørling8680f162016-07-07 09:54:22 +0200273 const struct ppa_addr *ppas, int nr_ppas, int vblk)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100274{
Javier González8e79b5c2016-11-28 22:39:06 +0100275 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100276 int i, plane_cnt, pl_idx;
Matias Bjørling8680f162016-07-07 09:54:22 +0200277 struct ppa_addr ppa;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100278
Javier González8e79b5c2016-11-28 22:39:06 +0100279 if ((!vblk || geo->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
Javier González6d5be952016-05-06 20:03:20 +0200280 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100281 rqd->ppa_addr = ppas[0];
282
283 return 0;
284 }
285
Javier González6d5be952016-05-06 20:03:20 +0200286 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100287 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
288 if (!rqd->ppa_list) {
289 pr_err("nvm: failed to allocate dma memory\n");
290 return -ENOMEM;
291 }
292
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200293 if (!vblk) {
294 for (i = 0; i < nr_ppas; i++)
295 rqd->ppa_list[i] = ppas[i];
296 } else {
Javier González8e79b5c2016-11-28 22:39:06 +0100297 plane_cnt = geo->plane_mode;
Javier González6d5be952016-05-06 20:03:20 +0200298 rqd->nr_ppas *= plane_cnt;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200299
Matias Bjørling556755e2016-01-12 07:49:26 +0100300 for (i = 0; i < nr_ppas; i++) {
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200301 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
Matias Bjørling8680f162016-07-07 09:54:22 +0200302 ppa = ppas[i];
303 ppa.g.pl = pl_idx;
304 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200305 }
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100306 }
307 }
308
309 return 0;
310}
311EXPORT_SYMBOL(nvm_set_rqd_ppalist);
312
313void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd)
314{
315 if (!rqd->ppa_list)
316 return;
317
318 nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list);
319}
320EXPORT_SYMBOL(nvm_free_rqd_ppalist);
321
Javier Gonzálezbb314972016-11-28 22:38:54 +0100322int nvm_erase_ppa(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas,
323 int flags)
Matias Bjørling069368e2016-01-12 07:49:19 +0100324{
Matias Bjørling069368e2016-01-12 07:49:19 +0100325 struct nvm_rq rqd;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100326 int ret;
Matias Bjørling069368e2016-01-12 07:49:19 +0100327
328 if (!dev->ops->erase_block)
329 return 0;
330
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100331 memset(&rqd, 0, sizeof(struct nvm_rq));
Matias Bjørling069368e2016-01-12 07:49:19 +0100332
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200333 ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100334 if (ret)
335 return ret;
Matias Bjørling069368e2016-01-12 07:49:19 +0100336
337 nvm_generic_to_addr_mode(dev, &rqd);
338
Javier Gonzálezbb314972016-11-28 22:38:54 +0100339 rqd.flags = flags;
340
Matias Bjørling069368e2016-01-12 07:49:19 +0100341 ret = dev->ops->erase_block(dev, &rqd);
342
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100343 nvm_free_rqd_ppalist(dev, &rqd);
Matias Bjørling069368e2016-01-12 07:49:19 +0100344
345 return ret;
346}
347EXPORT_SYMBOL(nvm_erase_ppa);
348
Matias Bjørling912761622016-01-12 07:49:21 +0100349void nvm_end_io(struct nvm_rq *rqd, int error)
350{
Matias Bjørling72d256e2016-01-12 07:49:29 +0100351 rqd->error = error;
352 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100353}
354EXPORT_SYMBOL(nvm_end_io);
355
Matias Bjørling09719b62016-01-12 07:49:30 +0100356static void nvm_end_io_sync(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100357{
358 struct completion *waiting = rqd->wait;
359
360 rqd->wait = NULL;
361
362 complete(waiting);
363}
364
Matias Bjørlingd9e46d52016-07-07 09:54:23 +0200365static int __nvm_submit_ppa(struct nvm_dev *dev, struct nvm_rq *rqd, int opcode,
Matias Bjørling1145e632016-05-06 20:02:56 +0200366 int flags, void *buf, int len)
Matias Bjørling09719b62016-01-12 07:49:30 +0100367{
368 DECLARE_COMPLETION_ONSTACK(wait);
Matias Bjørling09719b62016-01-12 07:49:30 +0100369 struct bio *bio;
370 int ret;
371 unsigned long hang_check;
372
373 bio = bio_map_kern(dev->q, buf, len, GFP_KERNEL);
374 if (IS_ERR_OR_NULL(bio))
375 return -ENOMEM;
376
Matias Bjørling1145e632016-05-06 20:02:56 +0200377 nvm_generic_to_addr_mode(dev, rqd);
Matias Bjørling09719b62016-01-12 07:49:30 +0100378
Javier González8e536242016-11-28 22:39:10 +0100379 rqd->dev = NULL;
Matias Bjørling1145e632016-05-06 20:02:56 +0200380 rqd->opcode = opcode;
381 rqd->flags = flags;
382 rqd->bio = bio;
383 rqd->wait = &wait;
384 rqd->end_io = nvm_end_io_sync;
Matias Bjørling09719b62016-01-12 07:49:30 +0100385
Matias Bjørling1145e632016-05-06 20:02:56 +0200386 ret = dev->ops->submit_io(dev, rqd);
Matias Bjørlingecfb40c2016-05-06 20:02:55 +0200387 if (ret) {
Matias Bjørlingecfb40c2016-05-06 20:02:55 +0200388 bio_put(bio);
389 return ret;
390 }
Matias Bjørling09719b62016-01-12 07:49:30 +0100391
392 /* Prevent hang_check timer from firing at us during very long I/O */
393 hang_check = sysctl_hung_task_timeout_secs;
394 if (hang_check)
Matias Bjørling12624af2016-07-07 09:54:13 +0200395 while (!wait_for_completion_io_timeout(&wait,
396 hang_check * (HZ/2)))
397 ;
Matias Bjørling09719b62016-01-12 07:49:30 +0100398 else
399 wait_for_completion_io(&wait);
400
Matias Bjørling1145e632016-05-06 20:02:56 +0200401 return rqd->error;
402}
403
404/**
405 * nvm_submit_ppa_list - submit user-defined ppa list to device. The user must
406 * take to free ppa list if necessary.
407 * @dev: device
408 * @ppa_list: user created ppa_list
409 * @nr_ppas: length of ppa_list
410 * @opcode: device opcode
411 * @flags: device flags
412 * @buf: data buffer
413 * @len: data buffer length
414 */
415int nvm_submit_ppa_list(struct nvm_dev *dev, struct ppa_addr *ppa_list,
416 int nr_ppas, int opcode, int flags, void *buf, int len)
417{
418 struct nvm_rq rqd;
419
420 if (dev->ops->max_phys_sect < nr_ppas)
421 return -EINVAL;
422
423 memset(&rqd, 0, sizeof(struct nvm_rq));
424
Javier González6d5be952016-05-06 20:03:20 +0200425 rqd.nr_ppas = nr_ppas;
Matias Bjørling1145e632016-05-06 20:02:56 +0200426 if (nr_ppas > 1)
427 rqd.ppa_list = ppa_list;
428 else
429 rqd.ppa_addr = ppa_list[0];
430
431 return __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
432}
433EXPORT_SYMBOL(nvm_submit_ppa_list);
434
435/**
436 * nvm_submit_ppa - submit PPAs to device. PPAs will automatically be unfolded
437 * as single, dual, quad plane PPAs depending on device type.
438 * @dev: device
439 * @ppa: user created ppa_list
440 * @nr_ppas: length of ppa_list
441 * @opcode: device opcode
442 * @flags: device flags
443 * @buf: data buffer
444 * @len: data buffer length
445 */
446int nvm_submit_ppa(struct nvm_dev *dev, struct ppa_addr *ppa, int nr_ppas,
447 int opcode, int flags, void *buf, int len)
448{
449 struct nvm_rq rqd;
450 int ret;
451
452 memset(&rqd, 0, sizeof(struct nvm_rq));
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200453 ret = nvm_set_rqd_ppalist(dev, &rqd, ppa, nr_ppas, 1);
Matias Bjørling1145e632016-05-06 20:02:56 +0200454 if (ret)
455 return ret;
456
457 ret = __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
458
Matias Bjørling09719b62016-01-12 07:49:30 +0100459 nvm_free_rqd_ppalist(dev, &rqd);
460
Matias Bjørling1145e632016-05-06 20:02:56 +0200461 return ret;
Matias Bjørling09719b62016-01-12 07:49:30 +0100462}
463EXPORT_SYMBOL(nvm_submit_ppa);
464
Matias Bjørling22e8c972016-05-06 20:02:58 +0200465/*
466 * folds a bad block list from its plane representation to its virtual
467 * block representation. The fold is done in place and reduced size is
468 * returned.
469 *
470 * If any of the planes status are bad or grown bad block, the virtual block
471 * is marked bad. If not bad, the first plane state acts as the block state.
472 */
473int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
474{
Javier González8e79b5c2016-11-28 22:39:06 +0100475 struct nvm_geo *geo = &dev->geo;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200476 int blk, offset, pl, blktype;
477
Javier González8e79b5c2016-11-28 22:39:06 +0100478 if (nr_blks != geo->blks_per_lun * geo->plane_mode)
Matias Bjørling22e8c972016-05-06 20:02:58 +0200479 return -EINVAL;
480
Javier González8e79b5c2016-11-28 22:39:06 +0100481 for (blk = 0; blk < geo->blks_per_lun; blk++) {
482 offset = blk * geo->plane_mode;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200483 blktype = blks[offset];
484
485 /* Bad blocks on any planes take precedence over other types */
Javier González8e79b5c2016-11-28 22:39:06 +0100486 for (pl = 0; pl < geo->plane_mode; pl++) {
Matias Bjørling22e8c972016-05-06 20:02:58 +0200487 if (blks[offset + pl] &
488 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
489 blktype = blks[offset + pl];
490 break;
491 }
492 }
493
494 blks[blk] = blktype;
495 }
496
Javier González8e79b5c2016-11-28 22:39:06 +0100497 return geo->blks_per_lun;
Matias Bjørling22e8c972016-05-06 20:02:58 +0200498}
499EXPORT_SYMBOL(nvm_bb_tbl_fold);
500
Matias Bjørlinge11903f2016-05-06 20:03:05 +0200501int nvm_get_bb_tbl(struct nvm_dev *dev, struct ppa_addr ppa, u8 *blks)
502{
503 ppa = generic_to_dev_addr(dev, ppa);
504
505 return dev->ops->get_bb_tbl(dev, ppa, blks);
506}
507EXPORT_SYMBOL(nvm_get_bb_tbl);
508
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100509static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
510{
Javier González8e79b5c2016-11-28 22:39:06 +0100511 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100512 int i;
513
Javier González8e79b5c2016-11-28 22:39:06 +0100514 dev->lps_per_blk = geo->pgs_per_blk;
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100515 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
516 if (!dev->lptbl)
517 return -ENOMEM;
518
519 /* Just a linear array */
520 for (i = 0; i < dev->lps_per_blk; i++)
521 dev->lptbl[i] = i;
522
523 return 0;
524}
525
526static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
527{
528 int i, p;
529 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
530
531 if (!mlc->num_pairs)
532 return 0;
533
534 dev->lps_per_blk = mlc->num_pairs;
535 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
536 if (!dev->lptbl)
537 return -ENOMEM;
538
539 /* The lower page table encoding consists of a list of bytes, where each
540 * has a lower and an upper half. The first half byte maintains the
541 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200542 * previous incrementation value
543 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100544 dev->lptbl[0] = mlc->pairs[0] & 0xF;
545 for (i = 1; i < dev->lps_per_blk; i++) {
546 p = mlc->pairs[i >> 1];
547 if (i & 0x1) /* upper */
548 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
549 else /* lower */
550 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
551 }
552
553 return 0;
554}
555
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100556static int nvm_core_init(struct nvm_dev *dev)
557{
558 struct nvm_id *id = &dev->identity;
559 struct nvm_id_group *grp = &id->groups[0];
Javier González8e79b5c2016-11-28 22:39:06 +0100560 struct nvm_geo *geo = &dev->geo;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200561 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100562
Javier González8e79b5c2016-11-28 22:39:06 +0100563 /* Whole device values */
564 geo->nr_chnls = grp->num_ch;
565 geo->luns_per_chnl = grp->num_lun;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100566
Javier González8e79b5c2016-11-28 22:39:06 +0100567 /* Generic device values */
568 geo->pgs_per_blk = grp->num_pg;
569 geo->blks_per_lun = grp->num_blk;
570 geo->nr_planes = grp->num_pln;
571 geo->fpg_size = grp->fpg_sz;
572 geo->pfpg_size = grp->fpg_sz * grp->num_pln;
573 geo->sec_size = grp->csecs;
574 geo->oob_size = grp->sos;
575 geo->sec_per_pg = grp->fpg_sz / grp->csecs;
576 geo->mccap = grp->mccap;
577 memcpy(&geo->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
578
579 geo->plane_mode = NVM_PLANE_SINGLE;
580 geo->max_rq_size = dev->ops->max_phys_sect * geo->sec_size;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100581
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100582 if (grp->mpos & 0x020202)
Javier González8e79b5c2016-11-28 22:39:06 +0100583 geo->plane_mode = NVM_PLANE_DOUBLE;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100584 if (grp->mpos & 0x040404)
Javier González8e79b5c2016-11-28 22:39:06 +0100585 geo->plane_mode = NVM_PLANE_QUAD;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100586
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200587 if (grp->mtype != 0) {
588 pr_err("nvm: memory type not supported\n");
589 return -EINVAL;
590 }
591
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100592 /* calculated values */
Javier González8e79b5c2016-11-28 22:39:06 +0100593 geo->sec_per_pl = geo->sec_per_pg * geo->nr_planes;
594 geo->sec_per_blk = geo->sec_per_pl * geo->pgs_per_blk;
595 geo->sec_per_lun = geo->sec_per_blk * geo->blks_per_lun;
596 geo->nr_luns = geo->luns_per_chnl * geo->nr_chnls;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100597
Javier González8e79b5c2016-11-28 22:39:06 +0100598 dev->total_secs = geo->nr_luns * geo->sec_per_lun;
599 dev->lun_map = kcalloc(BITS_TO_LONGS(geo->nr_luns),
Wenwei Taoda1e2842016-03-03 15:06:38 +0100600 sizeof(unsigned long), GFP_KERNEL);
601 if (!dev->lun_map)
602 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200603
604 switch (grp->fmtype) {
605 case NVM_ID_FMTYPE_SLC:
606 if (nvm_init_slc_tbl(dev, grp)) {
607 ret = -ENOMEM;
608 goto err_fmtype;
609 }
610 break;
611 case NVM_ID_FMTYPE_MLC:
612 if (nvm_init_mlc_tbl(dev, grp)) {
613 ret = -ENOMEM;
614 goto err_fmtype;
615 }
616 break;
617 default:
618 pr_err("nvm: flash type not supported\n");
619 ret = -EINVAL;
620 goto err_fmtype;
621 }
622
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100623 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100624 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100625
Javier González8e79b5c2016-11-28 22:39:06 +0100626 blk_queue_logical_block_size(dev->q, geo->sec_size);
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200627
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100628 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200629err_fmtype:
630 kfree(dev->lun_map);
631 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100632}
633
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200634static void nvm_free_mgr(struct nvm_dev *dev)
635{
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200636 if (!dev->mt)
637 return;
638
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200639 dev->mt->unregister_mgr(dev);
640 dev->mt = NULL;
641}
642
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200643void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100644{
645 if (!dev)
646 return;
647
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200648 nvm_free_mgr(dev);
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100649
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200650 if (dev->dma_pool)
651 dev->ops->destroy_dma_pool(dev->dma_pool);
652
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100653 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200654 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200655 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100656}
657
658static int nvm_init(struct nvm_dev *dev)
659{
Javier González8e79b5c2016-11-28 22:39:06 +0100660 struct nvm_geo *geo = &dev->geo;
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100661 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100662
663 if (!dev->q || !dev->ops)
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100664 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100665
Matias Bjørling16f26c32015-12-06 11:25:48 +0100666 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100667 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100668 goto err;
669 }
670
671 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
672 dev->identity.ver_id, dev->identity.vmnt,
673 dev->identity.cgrps);
674
675 if (dev->identity.ver_id != 1) {
676 pr_err("nvm: device not supported by kernel.");
677 goto err;
678 }
679
680 if (dev->identity.cgrps != 1) {
681 pr_err("nvm: only one group configuration supported.");
682 goto err;
683 }
684
685 ret = nvm_core_init(dev);
686 if (ret) {
687 pr_err("nvm: could not initialize core structures.\n");
688 goto err;
689 }
690
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100691 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
Javier González8e79b5c2016-11-28 22:39:06 +0100692 dev->name, geo->sec_per_pg, geo->nr_planes,
693 geo->pgs_per_blk, geo->blks_per_lun,
694 geo->nr_luns, geo->nr_chnls);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100695 return 0;
696err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100697 pr_err("nvm: failed to initialize nvm\n");
698 return ret;
699}
700
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200701struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100702{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200703 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
704}
705EXPORT_SYMBOL(nvm_alloc_dev);
706
707int nvm_register(struct nvm_dev *dev)
708{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100709 int ret;
710
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100711 ret = nvm_init(dev);
712 if (ret)
713 goto err_init;
714
Wenwei Taod1601472015-11-28 16:49:25 +0100715 if (dev->ops->max_phys_sect > 256) {
716 pr_info("nvm: max sectors supported is 256.\n");
717 ret = -EINVAL;
718 goto err_init;
719 }
720
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100721 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +0200722 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
723 if (!dev->dma_pool) {
724 pr_err("nvm: could not create dma pool\n");
Matias Bjørling93e70c12015-11-20 13:47:54 +0100725 ret = -ENOMEM;
726 goto err_init;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100727 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100728 }
729
Matias Bjørlingbf643182016-02-04 15:13:27 +0100730 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
731 ret = nvm_get_sysblock(dev, &dev->sb);
732 if (!ret)
733 pr_err("nvm: device not initialized.\n");
734 else if (ret < 0)
735 pr_err("nvm: err (%d) on device initialization\n", ret);
736 }
Matias Bjørlingb7692072016-01-12 07:49:38 +0100737
Matias Bjørling762796b2015-12-06 11:25:49 +0100738 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100739 down_write(&nvm_lock);
Matias Bjørlingb7692072016-01-12 07:49:38 +0100740 if (ret > 0)
741 dev->mt = nvm_init_mgr(dev);
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100742 list_add(&dev->devices, &nvm_devices);
743 up_write(&nvm_lock);
744
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100745 return 0;
746err_init:
Wenwei Taoda1e2842016-03-03 15:06:38 +0100747 kfree(dev->lun_map);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100748 return ret;
749}
750EXPORT_SYMBOL(nvm_register);
751
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200752void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100753{
Wenwei Taod0a712c2015-11-28 16:49:28 +0100754 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100755 list_del(&dev->devices);
756 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +0100757
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100758 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100759}
760EXPORT_SYMBOL(nvm_unregister);
761
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100762static int __nvm_configure_create(struct nvm_ioctl_create *create)
763{
764 struct nvm_dev *dev;
765 struct nvm_ioctl_create_simple *s;
766
Wenwei Taod0a712c2015-11-28 16:49:28 +0100767 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100768 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100769 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200770
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100771 if (!dev) {
772 pr_err("nvm: device not found\n");
773 return -EINVAL;
774 }
775
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200776 if (!dev->mt) {
777 pr_info("nvm: device has no media manager registered.\n");
778 return -ENODEV;
779 }
780
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100781 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
782 pr_err("nvm: config type not valid\n");
783 return -EINVAL;
784 }
785 s = &create->conf.s;
786
Javier González8e79b5c2016-11-28 22:39:06 +0100787 if (s->lun_begin > s->lun_end || s->lun_end > dev->geo.nr_luns) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100788 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
Javier González8e79b5c2016-11-28 22:39:06 +0100789 s->lun_begin, s->lun_end, dev->geo.nr_luns);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100790 return -EINVAL;
791 }
792
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200793 return dev->mt->create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100794}
795
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100796static long nvm_ioctl_info(struct file *file, void __user *arg)
797{
798 struct nvm_ioctl_info *info;
799 struct nvm_tgt_type *tt;
800 int tgt_iter = 0;
801
802 if (!capable(CAP_SYS_ADMIN))
803 return -EPERM;
804
805 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
806 if (IS_ERR(info))
807 return -EFAULT;
808
809 info->version[0] = NVM_VERSION_MAJOR;
810 info->version[1] = NVM_VERSION_MINOR;
811 info->version[2] = NVM_VERSION_PATCH;
812
813 down_write(&nvm_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200814 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100815 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
816
817 tgt->version[0] = tt->version[0];
818 tgt->version[1] = tt->version[1];
819 tgt->version[2] = tt->version[2];
820 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
821
822 tgt_iter++;
823 }
824
825 info->tgtsize = tgt_iter;
826 up_write(&nvm_lock);
827
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100828 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
829 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100830 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100831 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100832
833 kfree(info);
834 return 0;
835}
836
837static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
838{
839 struct nvm_ioctl_get_devices *devices;
840 struct nvm_dev *dev;
841 int i = 0;
842
843 if (!capable(CAP_SYS_ADMIN))
844 return -EPERM;
845
846 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
847 if (!devices)
848 return -ENOMEM;
849
850 down_write(&nvm_lock);
851 list_for_each_entry(dev, &nvm_devices, devices) {
852 struct nvm_ioctl_device_info *info = &devices->info[i];
853
854 sprintf(info->devname, "%s", dev->name);
855 if (dev->mt) {
856 info->bmversion[0] = dev->mt->version[0];
857 info->bmversion[1] = dev->mt->version[1];
858 info->bmversion[2] = dev->mt->version[2];
859 sprintf(info->bmname, "%s", dev->mt->name);
860 } else {
861 sprintf(info->bmname, "none");
862 }
863
864 i++;
865 if (i > 31) {
866 pr_err("nvm: max 31 devices can be reported.\n");
867 break;
868 }
869 }
870 up_write(&nvm_lock);
871
872 devices->nr_devices = i;
873
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100874 if (copy_to_user(arg, devices,
875 sizeof(struct nvm_ioctl_get_devices))) {
876 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100877 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100878 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100879
880 kfree(devices);
881 return 0;
882}
883
884static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
885{
886 struct nvm_ioctl_create create;
887
888 if (!capable(CAP_SYS_ADMIN))
889 return -EPERM;
890
891 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
892 return -EFAULT;
893
894 create.dev[DISK_NAME_LEN - 1] = '\0';
895 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
896 create.tgtname[DISK_NAME_LEN - 1] = '\0';
897
898 if (create.flags != 0) {
899 pr_err("nvm: no flags supported\n");
900 return -EINVAL;
901 }
902
903 return __nvm_configure_create(&create);
904}
905
906static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
907{
908 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200909 struct nvm_dev *dev;
910 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100911
912 if (!capable(CAP_SYS_ADMIN))
913 return -EPERM;
914
915 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
916 return -EFAULT;
917
918 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
919
920 if (remove.flags != 0) {
921 pr_err("nvm: no flags supported\n");
922 return -EINVAL;
923 }
924
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200925 list_for_each_entry(dev, &nvm_devices, devices) {
926 ret = dev->mt->remove_tgt(dev, &remove);
927 if (!ret)
928 break;
929 }
930
931 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100932}
933
Matias Bjørling55696152016-01-12 07:49:37 +0100934static void nvm_setup_nvm_sb_info(struct nvm_sb_info *info)
935{
936 info->seqnr = 1;
937 info->erase_cnt = 0;
938 info->version = 1;
939}
940
941static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init *init)
942{
943 struct nvm_dev *dev;
944 struct nvm_sb_info info;
Matias Bjørlingb7692072016-01-12 07:49:38 +0100945 int ret;
Matias Bjørling55696152016-01-12 07:49:37 +0100946
947 down_write(&nvm_lock);
948 dev = nvm_find_nvm_dev(init->dev);
949 up_write(&nvm_lock);
950 if (!dev) {
951 pr_err("nvm: device not found\n");
952 return -EINVAL;
953 }
954
955 nvm_setup_nvm_sb_info(&info);
956
957 strncpy(info.mmtype, init->mmtype, NVM_MMTYPE_LEN);
958 info.fs_ppa.ppa = -1;
959
Matias Bjørlingbf643182016-02-04 15:13:27 +0100960 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
961 ret = nvm_init_sysblock(dev, &info);
962 if (ret)
963 return ret;
964 }
Matias Bjørlingb7692072016-01-12 07:49:38 +0100965
966 memcpy(&dev->sb, &info, sizeof(struct nvm_sb_info));
967
968 down_write(&nvm_lock);
969 dev->mt = nvm_init_mgr(dev);
970 up_write(&nvm_lock);
971
972 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +0100973}
974
975static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
976{
977 struct nvm_ioctl_dev_init init;
978
979 if (!capable(CAP_SYS_ADMIN))
980 return -EPERM;
981
982 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
983 return -EFAULT;
984
985 if (init.flags != 0) {
986 pr_err("nvm: no flags supported\n");
987 return -EINVAL;
988 }
989
990 init.dev[DISK_NAME_LEN - 1] = '\0';
991
992 return __nvm_ioctl_dev_init(&init);
993}
994
Matias Bjørling8b4970c2016-01-12 07:49:39 +0100995static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
996{
997 struct nvm_ioctl_dev_factory fact;
998 struct nvm_dev *dev;
999
1000 if (!capable(CAP_SYS_ADMIN))
1001 return -EPERM;
1002
1003 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1004 return -EFAULT;
1005
1006 fact.dev[DISK_NAME_LEN - 1] = '\0';
1007
1008 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1009 return -EINVAL;
1010
1011 down_write(&nvm_lock);
1012 dev = nvm_find_nvm_dev(fact.dev);
1013 up_write(&nvm_lock);
1014 if (!dev) {
1015 pr_err("nvm: device not found\n");
1016 return -EINVAL;
1017 }
1018
Matias Bjørling976bdfc2016-05-06 20:03:17 +02001019 nvm_free_mgr(dev);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001020
Matias Bjørlingbf643182016-02-04 15:13:27 +01001021 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT)
1022 return nvm_dev_factory(dev, fact.flags);
1023
1024 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001025}
1026
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001027static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1028{
1029 void __user *argp = (void __user *)arg;
1030
1031 switch (cmd) {
1032 case NVM_INFO:
1033 return nvm_ioctl_info(file, argp);
1034 case NVM_GET_DEVICES:
1035 return nvm_ioctl_get_devices(file, argp);
1036 case NVM_DEV_CREATE:
1037 return nvm_ioctl_dev_create(file, argp);
1038 case NVM_DEV_REMOVE:
1039 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001040 case NVM_DEV_INIT:
1041 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001042 case NVM_DEV_FACTORY:
1043 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001044 }
1045 return 0;
1046}
1047
1048static const struct file_operations _ctl_fops = {
1049 .open = nonseekable_open,
1050 .unlocked_ioctl = nvm_ctl_ioctl,
1051 .owner = THIS_MODULE,
1052 .llseek = noop_llseek,
1053};
1054
1055static struct miscdevice _nvm_misc = {
1056 .minor = MISC_DYNAMIC_MINOR,
1057 .name = "lightnvm",
1058 .nodename = "lightnvm/control",
1059 .fops = &_ctl_fops,
1060};
PrasannaKumar Muralidharand6a38c02016-08-25 22:30:50 +05301061module_misc_device(_nvm_misc);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001062
1063MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR);
1064
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001065MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
1066MODULE_LICENSE("GPL v2");
1067MODULE_VERSION("0.1");