blob: 6527cf6862fa09436a46211f87dcd9d408fc8753 [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
179struct nvm_block *nvm_get_blk(struct nvm_dev *dev, struct nvm_lun *lun,
180 unsigned long flags)
181{
182 return dev->mt->get_blk(dev, lun, flags);
183}
184EXPORT_SYMBOL(nvm_get_blk);
185
186/* Assumes that all valid pages have already been moved on release to bm */
187void nvm_put_blk(struct nvm_dev *dev, struct nvm_block *blk)
188{
189 return dev->mt->put_blk(dev, blk);
190}
191EXPORT_SYMBOL(nvm_put_blk);
192
Javier González529435e2016-07-07 09:54:08 +0200193void nvm_mark_blk(struct nvm_dev *dev, struct ppa_addr ppa, int type)
194{
195 return dev->mt->mark_blk(dev, ppa, type);
196}
197EXPORT_SYMBOL(nvm_mark_blk);
198
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100199int nvm_set_bb_tbl(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas,
200 int type)
201{
202 struct nvm_rq rqd;
203 int ret;
204
205 if (nr_ppas > dev->ops->max_phys_sect) {
206 pr_err("nvm: unable to update all sysblocks atomically\n");
207 return -EINVAL;
208 }
209
210 memset(&rqd, 0, sizeof(struct nvm_rq));
211
212 nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
213 nvm_generic_to_addr_mode(dev, &rqd);
214
215 ret = dev->ops->set_bb_tbl(dev, &rqd.ppa_addr, rqd.nr_ppas, type);
216 nvm_free_rqd_ppalist(dev, &rqd);
217 if (ret) {
218 pr_err("nvm: sysblk failed bb mark\n");
219 return -EINVAL;
220 }
221
222 return 0;
223}
224EXPORT_SYMBOL(nvm_set_bb_tbl);
225
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100226int nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
227{
228 return dev->mt->submit_io(dev, rqd);
229}
230EXPORT_SYMBOL(nvm_submit_io);
231
Javier Gonzálezbb314972016-11-28 22:38:54 +0100232int nvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk, int flags)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100233{
Javier Gonzálezbb314972016-11-28 22:38:54 +0100234 return dev->mt->erase_blk(dev, blk, flags);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100235}
236EXPORT_SYMBOL(nvm_erase_blk);
237
Matias Bjørling069368e2016-01-12 07:49:19 +0100238void nvm_addr_to_generic_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] = dev_to_generic_addr(dev,
245 rqd->ppa_list[i]);
246 } else {
247 rqd->ppa_addr = dev_to_generic_addr(dev, rqd->ppa_addr);
248 }
249}
250EXPORT_SYMBOL(nvm_addr_to_generic_mode);
251
252void nvm_generic_to_addr_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
253{
254 int i;
255
Javier González6d5be952016-05-06 20:03:20 +0200256 if (rqd->nr_ppas > 1) {
257 for (i = 0; i < rqd->nr_ppas; i++)
Matias Bjørling069368e2016-01-12 07:49:19 +0100258 rqd->ppa_list[i] = generic_to_dev_addr(dev,
259 rqd->ppa_list[i]);
260 } else {
261 rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr);
262 }
263}
264EXPORT_SYMBOL(nvm_generic_to_addr_mode);
265
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100266int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd,
Matias Bjørling8680f162016-07-07 09:54:22 +0200267 const struct ppa_addr *ppas, int nr_ppas, int vblk)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100268{
269 int i, plane_cnt, pl_idx;
Matias Bjørling8680f162016-07-07 09:54:22 +0200270 struct ppa_addr ppa;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100271
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200272 if ((!vblk || dev->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
Javier González6d5be952016-05-06 20:03:20 +0200273 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100274 rqd->ppa_addr = ppas[0];
275
276 return 0;
277 }
278
Javier González6d5be952016-05-06 20:03:20 +0200279 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100280 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
281 if (!rqd->ppa_list) {
282 pr_err("nvm: failed to allocate dma memory\n");
283 return -ENOMEM;
284 }
285
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200286 if (!vblk) {
287 for (i = 0; i < nr_ppas; i++)
288 rqd->ppa_list[i] = ppas[i];
289 } else {
290 plane_cnt = dev->plane_mode;
Javier González6d5be952016-05-06 20:03:20 +0200291 rqd->nr_ppas *= plane_cnt;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200292
Matias Bjørling556755e2016-01-12 07:49:26 +0100293 for (i = 0; i < nr_ppas; i++) {
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200294 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
Matias Bjørling8680f162016-07-07 09:54:22 +0200295 ppa = ppas[i];
296 ppa.g.pl = pl_idx;
297 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200298 }
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100299 }
300 }
301
302 return 0;
303}
304EXPORT_SYMBOL(nvm_set_rqd_ppalist);
305
306void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd)
307{
308 if (!rqd->ppa_list)
309 return;
310
311 nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list);
312}
313EXPORT_SYMBOL(nvm_free_rqd_ppalist);
314
Javier Gonzálezbb314972016-11-28 22:38:54 +0100315int nvm_erase_ppa(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas,
316 int flags)
Matias Bjørling069368e2016-01-12 07:49:19 +0100317{
Matias Bjørling069368e2016-01-12 07:49:19 +0100318 struct nvm_rq rqd;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100319 int ret;
Matias Bjørling069368e2016-01-12 07:49:19 +0100320
321 if (!dev->ops->erase_block)
322 return 0;
323
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100324 memset(&rqd, 0, sizeof(struct nvm_rq));
Matias Bjørling069368e2016-01-12 07:49:19 +0100325
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200326 ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100327 if (ret)
328 return ret;
Matias Bjørling069368e2016-01-12 07:49:19 +0100329
330 nvm_generic_to_addr_mode(dev, &rqd);
331
Javier Gonzálezbb314972016-11-28 22:38:54 +0100332 rqd.flags = flags;
333
Matias Bjørling069368e2016-01-12 07:49:19 +0100334 ret = dev->ops->erase_block(dev, &rqd);
335
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100336 nvm_free_rqd_ppalist(dev, &rqd);
Matias Bjørling069368e2016-01-12 07:49:19 +0100337
338 return ret;
339}
340EXPORT_SYMBOL(nvm_erase_ppa);
341
Matias Bjørling912761622016-01-12 07:49:21 +0100342void nvm_end_io(struct nvm_rq *rqd, int error)
343{
Matias Bjørling72d256e2016-01-12 07:49:29 +0100344 rqd->error = error;
345 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100346}
347EXPORT_SYMBOL(nvm_end_io);
348
Matias Bjørling09719b62016-01-12 07:49:30 +0100349static void nvm_end_io_sync(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100350{
351 struct completion *waiting = rqd->wait;
352
353 rqd->wait = NULL;
354
355 complete(waiting);
356}
357
Matias Bjørlingd9e46d52016-07-07 09:54:23 +0200358static int __nvm_submit_ppa(struct nvm_dev *dev, struct nvm_rq *rqd, int opcode,
Matias Bjørling1145e632016-05-06 20:02:56 +0200359 int flags, void *buf, int len)
Matias Bjørling09719b62016-01-12 07:49:30 +0100360{
361 DECLARE_COMPLETION_ONSTACK(wait);
Matias Bjørling09719b62016-01-12 07:49:30 +0100362 struct bio *bio;
363 int ret;
364 unsigned long hang_check;
365
366 bio = bio_map_kern(dev->q, buf, len, GFP_KERNEL);
367 if (IS_ERR_OR_NULL(bio))
368 return -ENOMEM;
369
Matias Bjørling1145e632016-05-06 20:02:56 +0200370 nvm_generic_to_addr_mode(dev, rqd);
Matias Bjørling09719b62016-01-12 07:49:30 +0100371
Matias Bjørling1145e632016-05-06 20:02:56 +0200372 rqd->dev = dev;
373 rqd->opcode = opcode;
374 rqd->flags = flags;
375 rqd->bio = bio;
376 rqd->wait = &wait;
377 rqd->end_io = nvm_end_io_sync;
Matias Bjørling09719b62016-01-12 07:49:30 +0100378
Matias Bjørling1145e632016-05-06 20:02:56 +0200379 ret = dev->ops->submit_io(dev, rqd);
Matias Bjørlingecfb40c2016-05-06 20:02:55 +0200380 if (ret) {
Matias Bjørlingecfb40c2016-05-06 20:02:55 +0200381 bio_put(bio);
382 return ret;
383 }
Matias Bjørling09719b62016-01-12 07:49:30 +0100384
385 /* Prevent hang_check timer from firing at us during very long I/O */
386 hang_check = sysctl_hung_task_timeout_secs;
387 if (hang_check)
Matias Bjørling12624af2016-07-07 09:54:13 +0200388 while (!wait_for_completion_io_timeout(&wait,
389 hang_check * (HZ/2)))
390 ;
Matias Bjørling09719b62016-01-12 07:49:30 +0100391 else
392 wait_for_completion_io(&wait);
393
Matias Bjørling1145e632016-05-06 20:02:56 +0200394 return rqd->error;
395}
396
397/**
398 * nvm_submit_ppa_list - submit user-defined ppa list to device. The user must
399 * take to free ppa list if necessary.
400 * @dev: device
401 * @ppa_list: user created ppa_list
402 * @nr_ppas: length of ppa_list
403 * @opcode: device opcode
404 * @flags: device flags
405 * @buf: data buffer
406 * @len: data buffer length
407 */
408int nvm_submit_ppa_list(struct nvm_dev *dev, struct ppa_addr *ppa_list,
409 int nr_ppas, int opcode, int flags, void *buf, int len)
410{
411 struct nvm_rq rqd;
412
413 if (dev->ops->max_phys_sect < nr_ppas)
414 return -EINVAL;
415
416 memset(&rqd, 0, sizeof(struct nvm_rq));
417
Javier González6d5be952016-05-06 20:03:20 +0200418 rqd.nr_ppas = nr_ppas;
Matias Bjørling1145e632016-05-06 20:02:56 +0200419 if (nr_ppas > 1)
420 rqd.ppa_list = ppa_list;
421 else
422 rqd.ppa_addr = ppa_list[0];
423
424 return __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
425}
426EXPORT_SYMBOL(nvm_submit_ppa_list);
427
428/**
429 * nvm_submit_ppa - submit PPAs to device. PPAs will automatically be unfolded
430 * as single, dual, quad plane PPAs depending on device type.
431 * @dev: device
432 * @ppa: user created ppa_list
433 * @nr_ppas: length of ppa_list
434 * @opcode: device opcode
435 * @flags: device flags
436 * @buf: data buffer
437 * @len: data buffer length
438 */
439int nvm_submit_ppa(struct nvm_dev *dev, struct ppa_addr *ppa, int nr_ppas,
440 int opcode, int flags, void *buf, int len)
441{
442 struct nvm_rq rqd;
443 int ret;
444
445 memset(&rqd, 0, sizeof(struct nvm_rq));
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200446 ret = nvm_set_rqd_ppalist(dev, &rqd, ppa, nr_ppas, 1);
Matias Bjørling1145e632016-05-06 20:02:56 +0200447 if (ret)
448 return ret;
449
450 ret = __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
451
Matias Bjørling09719b62016-01-12 07:49:30 +0100452 nvm_free_rqd_ppalist(dev, &rqd);
453
Matias Bjørling1145e632016-05-06 20:02:56 +0200454 return ret;
Matias Bjørling09719b62016-01-12 07:49:30 +0100455}
456EXPORT_SYMBOL(nvm_submit_ppa);
457
Matias Bjørling22e8c972016-05-06 20:02:58 +0200458/*
459 * folds a bad block list from its plane representation to its virtual
460 * block representation. The fold is done in place and reduced size is
461 * returned.
462 *
463 * If any of the planes status are bad or grown bad block, the virtual block
464 * is marked bad. If not bad, the first plane state acts as the block state.
465 */
466int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
467{
468 int blk, offset, pl, blktype;
469
470 if (nr_blks != dev->blks_per_lun * dev->plane_mode)
471 return -EINVAL;
472
473 for (blk = 0; blk < dev->blks_per_lun; blk++) {
474 offset = blk * dev->plane_mode;
475 blktype = blks[offset];
476
477 /* Bad blocks on any planes take precedence over other types */
478 for (pl = 0; pl < dev->plane_mode; pl++) {
479 if (blks[offset + pl] &
480 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
481 blktype = blks[offset + pl];
482 break;
483 }
484 }
485
486 blks[blk] = blktype;
487 }
488
489 return dev->blks_per_lun;
490}
491EXPORT_SYMBOL(nvm_bb_tbl_fold);
492
Matias Bjørlinge11903f2016-05-06 20:03:05 +0200493int nvm_get_bb_tbl(struct nvm_dev *dev, struct ppa_addr ppa, u8 *blks)
494{
495 ppa = generic_to_dev_addr(dev, ppa);
496
497 return dev->ops->get_bb_tbl(dev, ppa, blks);
498}
499EXPORT_SYMBOL(nvm_get_bb_tbl);
500
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100501static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
502{
503 int i;
504
505 dev->lps_per_blk = dev->pgs_per_blk;
506 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
507 if (!dev->lptbl)
508 return -ENOMEM;
509
510 /* Just a linear array */
511 for (i = 0; i < dev->lps_per_blk; i++)
512 dev->lptbl[i] = i;
513
514 return 0;
515}
516
517static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
518{
519 int i, p;
520 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
521
522 if (!mlc->num_pairs)
523 return 0;
524
525 dev->lps_per_blk = mlc->num_pairs;
526 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
527 if (!dev->lptbl)
528 return -ENOMEM;
529
530 /* The lower page table encoding consists of a list of bytes, where each
531 * has a lower and an upper half. The first half byte maintains the
532 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200533 * previous incrementation value
534 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100535 dev->lptbl[0] = mlc->pairs[0] & 0xF;
536 for (i = 1; i < dev->lps_per_blk; i++) {
537 p = mlc->pairs[i >> 1];
538 if (i & 0x1) /* upper */
539 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
540 else /* lower */
541 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
542 }
543
544 return 0;
545}
546
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100547static int nvm_core_init(struct nvm_dev *dev)
548{
549 struct nvm_id *id = &dev->identity;
550 struct nvm_id_group *grp = &id->groups[0];
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200551 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100552
553 /* device values */
554 dev->nr_chnls = grp->num_ch;
555 dev->luns_per_chnl = grp->num_lun;
556 dev->pgs_per_blk = grp->num_pg;
557 dev->blks_per_lun = grp->num_blk;
558 dev->nr_planes = grp->num_pln;
Matias Bjørling4891d122016-05-06 20:02:57 +0200559 dev->fpg_size = grp->fpg_sz;
560 dev->pfpg_size = grp->fpg_sz * grp->num_pln;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100561 dev->sec_size = grp->csecs;
562 dev->oob_size = grp->sos;
563 dev->sec_per_pg = grp->fpg_sz / grp->csecs;
Matias Bjørlingf9a99952016-01-12 07:49:34 +0100564 dev->mccap = grp->mccap;
Matias Bjørling7386af22015-11-16 15:34:44 +0100565 memcpy(&dev->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100566
567 dev->plane_mode = NVM_PLANE_SINGLE;
568 dev->max_rq_size = dev->ops->max_phys_sect * dev->sec_size;
569
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100570 if (grp->mpos & 0x020202)
571 dev->plane_mode = NVM_PLANE_DOUBLE;
572 if (grp->mpos & 0x040404)
573 dev->plane_mode = NVM_PLANE_QUAD;
574
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200575 if (grp->mtype != 0) {
576 pr_err("nvm: memory type not supported\n");
577 return -EINVAL;
578 }
579
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100580 /* calculated values */
581 dev->sec_per_pl = dev->sec_per_pg * dev->nr_planes;
582 dev->sec_per_blk = dev->sec_per_pl * dev->pgs_per_blk;
583 dev->sec_per_lun = dev->sec_per_blk * dev->blks_per_lun;
584 dev->nr_luns = dev->luns_per_chnl * dev->nr_chnls;
585
Matias Bjørlinged2a92a2016-02-20 08:52:42 +0100586 dev->total_secs = dev->nr_luns * dev->sec_per_lun;
Wenwei Taoda1e2842016-03-03 15:06:38 +0100587 dev->lun_map = kcalloc(BITS_TO_LONGS(dev->nr_luns),
588 sizeof(unsigned long), GFP_KERNEL);
589 if (!dev->lun_map)
590 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200591
592 switch (grp->fmtype) {
593 case NVM_ID_FMTYPE_SLC:
594 if (nvm_init_slc_tbl(dev, grp)) {
595 ret = -ENOMEM;
596 goto err_fmtype;
597 }
598 break;
599 case NVM_ID_FMTYPE_MLC:
600 if (nvm_init_mlc_tbl(dev, grp)) {
601 ret = -ENOMEM;
602 goto err_fmtype;
603 }
604 break;
605 default:
606 pr_err("nvm: flash type not supported\n");
607 ret = -EINVAL;
608 goto err_fmtype;
609 }
610
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100611 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100612 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100613
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200614 blk_queue_logical_block_size(dev->q, dev->sec_size);
615
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100616 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200617err_fmtype:
618 kfree(dev->lun_map);
619 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100620}
621
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200622static void nvm_free_mgr(struct nvm_dev *dev)
623{
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200624 if (!dev->mt)
625 return;
626
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200627 dev->mt->unregister_mgr(dev);
628 dev->mt = NULL;
629}
630
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200631void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100632{
633 if (!dev)
634 return;
635
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200636 nvm_free_mgr(dev);
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100637
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200638 if (dev->dma_pool)
639 dev->ops->destroy_dma_pool(dev->dma_pool);
640
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100641 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200642 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200643 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100644}
645
646static int nvm_init(struct nvm_dev *dev)
647{
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100648 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100649
650 if (!dev->q || !dev->ops)
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100651 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100652
Matias Bjørling16f26c32015-12-06 11:25:48 +0100653 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100654 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100655 goto err;
656 }
657
658 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
659 dev->identity.ver_id, dev->identity.vmnt,
660 dev->identity.cgrps);
661
662 if (dev->identity.ver_id != 1) {
663 pr_err("nvm: device not supported by kernel.");
664 goto err;
665 }
666
667 if (dev->identity.cgrps != 1) {
668 pr_err("nvm: only one group configuration supported.");
669 goto err;
670 }
671
672 ret = nvm_core_init(dev);
673 if (ret) {
674 pr_err("nvm: could not initialize core structures.\n");
675 goto err;
676 }
677
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100678 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
679 dev->name, dev->sec_per_pg, dev->nr_planes,
680 dev->pgs_per_blk, dev->blks_per_lun, dev->nr_luns,
681 dev->nr_chnls);
682 return 0;
683err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100684 pr_err("nvm: failed to initialize nvm\n");
685 return ret;
686}
687
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200688struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100689{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200690 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
691}
692EXPORT_SYMBOL(nvm_alloc_dev);
693
694int nvm_register(struct nvm_dev *dev)
695{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100696 int ret;
697
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100698 ret = nvm_init(dev);
699 if (ret)
700 goto err_init;
701
Wenwei Taod1601472015-11-28 16:49:25 +0100702 if (dev->ops->max_phys_sect > 256) {
703 pr_info("nvm: max sectors supported is 256.\n");
704 ret = -EINVAL;
705 goto err_init;
706 }
707
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100708 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +0200709 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
710 if (!dev->dma_pool) {
711 pr_err("nvm: could not create dma pool\n");
Matias Bjørling93e70c12015-11-20 13:47:54 +0100712 ret = -ENOMEM;
713 goto err_init;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100714 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100715 }
716
Matias Bjørlingbf643182016-02-04 15:13:27 +0100717 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
718 ret = nvm_get_sysblock(dev, &dev->sb);
719 if (!ret)
720 pr_err("nvm: device not initialized.\n");
721 else if (ret < 0)
722 pr_err("nvm: err (%d) on device initialization\n", ret);
723 }
Matias Bjørlingb7692072016-01-12 07:49:38 +0100724
Matias Bjørling762796b2015-12-06 11:25:49 +0100725 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100726 down_write(&nvm_lock);
Matias Bjørlingb7692072016-01-12 07:49:38 +0100727 if (ret > 0)
728 dev->mt = nvm_init_mgr(dev);
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100729 list_add(&dev->devices, &nvm_devices);
730 up_write(&nvm_lock);
731
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100732 return 0;
733err_init:
Wenwei Taoda1e2842016-03-03 15:06:38 +0100734 kfree(dev->lun_map);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100735 return ret;
736}
737EXPORT_SYMBOL(nvm_register);
738
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200739void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100740{
Wenwei Taod0a712c2015-11-28 16:49:28 +0100741 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100742 list_del(&dev->devices);
743 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +0100744
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100745 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100746}
747EXPORT_SYMBOL(nvm_unregister);
748
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100749static int __nvm_configure_create(struct nvm_ioctl_create *create)
750{
751 struct nvm_dev *dev;
752 struct nvm_ioctl_create_simple *s;
753
Wenwei Taod0a712c2015-11-28 16:49:28 +0100754 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100755 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100756 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200757
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100758 if (!dev) {
759 pr_err("nvm: device not found\n");
760 return -EINVAL;
761 }
762
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200763 if (!dev->mt) {
764 pr_info("nvm: device has no media manager registered.\n");
765 return -ENODEV;
766 }
767
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100768 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
769 pr_err("nvm: config type not valid\n");
770 return -EINVAL;
771 }
772 s = &create->conf.s;
773
774 if (s->lun_begin > s->lun_end || s->lun_end > dev->nr_luns) {
775 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
776 s->lun_begin, s->lun_end, dev->nr_luns);
777 return -EINVAL;
778 }
779
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200780 return dev->mt->create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100781}
782
783#ifdef CONFIG_NVM_DEBUG
784static int nvm_configure_show(const char *val)
785{
786 struct nvm_dev *dev;
787 char opcode, devname[DISK_NAME_LEN];
788 int ret;
789
790 ret = sscanf(val, "%c %32s", &opcode, devname);
791 if (ret != 2) {
792 pr_err("nvm: invalid command. Use \"opcode devicename\".\n");
793 return -EINVAL;
794 }
795
Wenwei Taod0a712c2015-11-28 16:49:28 +0100796 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100797 dev = nvm_find_nvm_dev(devname);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100798 up_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100799 if (!dev) {
800 pr_err("nvm: device not found\n");
801 return -EINVAL;
802 }
803
804 if (!dev->mt)
805 return 0;
806
Javier Gonzalez2fde0e42015-11-20 13:47:57 +0100807 dev->mt->lun_info_print(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100808
809 return 0;
810}
811
812static int nvm_configure_remove(const char *val)
813{
814 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200815 struct nvm_dev *dev;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100816 char opcode;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200817 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100818
819 ret = sscanf(val, "%c %256s", &opcode, remove.tgtname);
820 if (ret != 2) {
821 pr_err("nvm: invalid command. Use \"d targetname\".\n");
822 return -EINVAL;
823 }
824
825 remove.flags = 0;
826
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200827 list_for_each_entry(dev, &nvm_devices, devices) {
828 ret = dev->mt->remove_tgt(dev, &remove);
829 if (!ret)
830 break;
831 }
832
833 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100834}
835
836static int nvm_configure_create(const char *val)
837{
838 struct nvm_ioctl_create create;
839 char opcode;
840 int lun_begin, lun_end, ret;
841
842 ret = sscanf(val, "%c %256s %256s %48s %u:%u", &opcode, create.dev,
843 create.tgtname, create.tgttype,
844 &lun_begin, &lun_end);
845 if (ret != 6) {
846 pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n");
847 return -EINVAL;
848 }
849
850 create.flags = 0;
851 create.conf.type = NVM_CONFIG_TYPE_SIMPLE;
852 create.conf.s.lun_begin = lun_begin;
853 create.conf.s.lun_end = lun_end;
854
855 return __nvm_configure_create(&create);
856}
857
858
859/* Exposes administrative interface through /sys/module/lnvm/configure_by_str */
860static int nvm_configure_by_str_event(const char *val,
861 const struct kernel_param *kp)
862{
863 char opcode;
864 int ret;
865
866 ret = sscanf(val, "%c", &opcode);
867 if (ret != 1) {
868 pr_err("nvm: string must have the format of \"cmd ...\"\n");
869 return -EINVAL;
870 }
871
872 switch (opcode) {
873 case 'a':
874 return nvm_configure_create(val);
875 case 'd':
876 return nvm_configure_remove(val);
877 case 's':
878 return nvm_configure_show(val);
879 default:
880 pr_err("nvm: invalid command\n");
881 return -EINVAL;
882 }
883
884 return 0;
885}
886
887static int nvm_configure_get(char *buf, const struct kernel_param *kp)
888{
Alan5e422cf2016-02-19 13:56:57 +0100889 int sz;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100890 struct nvm_dev *dev;
891
Alan5e422cf2016-02-19 13:56:57 +0100892 sz = sprintf(buf, "available devices:\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100893 down_write(&nvm_lock);
894 list_for_each_entry(dev, &nvm_devices, devices) {
Alan5e422cf2016-02-19 13:56:57 +0100895 if (sz > 4095 - DISK_NAME_LEN - 2)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100896 break;
Alan5e422cf2016-02-19 13:56:57 +0100897 sz += sprintf(buf + sz, " %32s\n", dev->name);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100898 }
899 up_write(&nvm_lock);
900
Alan5e422cf2016-02-19 13:56:57 +0100901 return sz;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100902}
903
904static const struct kernel_param_ops nvm_configure_by_str_event_param_ops = {
905 .set = nvm_configure_by_str_event,
906 .get = nvm_configure_get,
907};
908
909#undef MODULE_PARAM_PREFIX
910#define MODULE_PARAM_PREFIX "lnvm."
911
912module_param_cb(configure_debug, &nvm_configure_by_str_event_param_ops, NULL,
913 0644);
914
915#endif /* CONFIG_NVM_DEBUG */
916
917static long nvm_ioctl_info(struct file *file, void __user *arg)
918{
919 struct nvm_ioctl_info *info;
920 struct nvm_tgt_type *tt;
921 int tgt_iter = 0;
922
923 if (!capable(CAP_SYS_ADMIN))
924 return -EPERM;
925
926 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
927 if (IS_ERR(info))
928 return -EFAULT;
929
930 info->version[0] = NVM_VERSION_MAJOR;
931 info->version[1] = NVM_VERSION_MINOR;
932 info->version[2] = NVM_VERSION_PATCH;
933
934 down_write(&nvm_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200935 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100936 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
937
938 tgt->version[0] = tt->version[0];
939 tgt->version[1] = tt->version[1];
940 tgt->version[2] = tt->version[2];
941 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
942
943 tgt_iter++;
944 }
945
946 info->tgtsize = tgt_iter;
947 up_write(&nvm_lock);
948
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100949 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
950 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100951 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100952 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100953
954 kfree(info);
955 return 0;
956}
957
958static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
959{
960 struct nvm_ioctl_get_devices *devices;
961 struct nvm_dev *dev;
962 int i = 0;
963
964 if (!capable(CAP_SYS_ADMIN))
965 return -EPERM;
966
967 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
968 if (!devices)
969 return -ENOMEM;
970
971 down_write(&nvm_lock);
972 list_for_each_entry(dev, &nvm_devices, devices) {
973 struct nvm_ioctl_device_info *info = &devices->info[i];
974
975 sprintf(info->devname, "%s", dev->name);
976 if (dev->mt) {
977 info->bmversion[0] = dev->mt->version[0];
978 info->bmversion[1] = dev->mt->version[1];
979 info->bmversion[2] = dev->mt->version[2];
980 sprintf(info->bmname, "%s", dev->mt->name);
981 } else {
982 sprintf(info->bmname, "none");
983 }
984
985 i++;
986 if (i > 31) {
987 pr_err("nvm: max 31 devices can be reported.\n");
988 break;
989 }
990 }
991 up_write(&nvm_lock);
992
993 devices->nr_devices = i;
994
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100995 if (copy_to_user(arg, devices,
996 sizeof(struct nvm_ioctl_get_devices))) {
997 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100998 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100999 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001000
1001 kfree(devices);
1002 return 0;
1003}
1004
1005static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
1006{
1007 struct nvm_ioctl_create create;
1008
1009 if (!capable(CAP_SYS_ADMIN))
1010 return -EPERM;
1011
1012 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
1013 return -EFAULT;
1014
1015 create.dev[DISK_NAME_LEN - 1] = '\0';
1016 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
1017 create.tgtname[DISK_NAME_LEN - 1] = '\0';
1018
1019 if (create.flags != 0) {
1020 pr_err("nvm: no flags supported\n");
1021 return -EINVAL;
1022 }
1023
1024 return __nvm_configure_create(&create);
1025}
1026
1027static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1028{
1029 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001030 struct nvm_dev *dev;
1031 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001032
1033 if (!capable(CAP_SYS_ADMIN))
1034 return -EPERM;
1035
1036 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1037 return -EFAULT;
1038
1039 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1040
1041 if (remove.flags != 0) {
1042 pr_err("nvm: no flags supported\n");
1043 return -EINVAL;
1044 }
1045
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001046 list_for_each_entry(dev, &nvm_devices, devices) {
1047 ret = dev->mt->remove_tgt(dev, &remove);
1048 if (!ret)
1049 break;
1050 }
1051
1052 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001053}
1054
Matias Bjørling55696152016-01-12 07:49:37 +01001055static void nvm_setup_nvm_sb_info(struct nvm_sb_info *info)
1056{
1057 info->seqnr = 1;
1058 info->erase_cnt = 0;
1059 info->version = 1;
1060}
1061
1062static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init *init)
1063{
1064 struct nvm_dev *dev;
1065 struct nvm_sb_info info;
Matias Bjørlingb7692072016-01-12 07:49:38 +01001066 int ret;
Matias Bjørling55696152016-01-12 07:49:37 +01001067
1068 down_write(&nvm_lock);
1069 dev = nvm_find_nvm_dev(init->dev);
1070 up_write(&nvm_lock);
1071 if (!dev) {
1072 pr_err("nvm: device not found\n");
1073 return -EINVAL;
1074 }
1075
1076 nvm_setup_nvm_sb_info(&info);
1077
1078 strncpy(info.mmtype, init->mmtype, NVM_MMTYPE_LEN);
1079 info.fs_ppa.ppa = -1;
1080
Matias Bjørlingbf643182016-02-04 15:13:27 +01001081 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
1082 ret = nvm_init_sysblock(dev, &info);
1083 if (ret)
1084 return ret;
1085 }
Matias Bjørlingb7692072016-01-12 07:49:38 +01001086
1087 memcpy(&dev->sb, &info, sizeof(struct nvm_sb_info));
1088
1089 down_write(&nvm_lock);
1090 dev->mt = nvm_init_mgr(dev);
1091 up_write(&nvm_lock);
1092
1093 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001094}
1095
1096static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1097{
1098 struct nvm_ioctl_dev_init init;
1099
1100 if (!capable(CAP_SYS_ADMIN))
1101 return -EPERM;
1102
1103 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1104 return -EFAULT;
1105
1106 if (init.flags != 0) {
1107 pr_err("nvm: no flags supported\n");
1108 return -EINVAL;
1109 }
1110
1111 init.dev[DISK_NAME_LEN - 1] = '\0';
1112
1113 return __nvm_ioctl_dev_init(&init);
1114}
1115
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001116static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1117{
1118 struct nvm_ioctl_dev_factory fact;
1119 struct nvm_dev *dev;
1120
1121 if (!capable(CAP_SYS_ADMIN))
1122 return -EPERM;
1123
1124 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1125 return -EFAULT;
1126
1127 fact.dev[DISK_NAME_LEN - 1] = '\0';
1128
1129 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1130 return -EINVAL;
1131
1132 down_write(&nvm_lock);
1133 dev = nvm_find_nvm_dev(fact.dev);
1134 up_write(&nvm_lock);
1135 if (!dev) {
1136 pr_err("nvm: device not found\n");
1137 return -EINVAL;
1138 }
1139
Matias Bjørling976bdfc2016-05-06 20:03:17 +02001140 nvm_free_mgr(dev);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001141
Matias Bjørlingbf643182016-02-04 15:13:27 +01001142 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT)
1143 return nvm_dev_factory(dev, fact.flags);
1144
1145 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001146}
1147
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001148static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1149{
1150 void __user *argp = (void __user *)arg;
1151
1152 switch (cmd) {
1153 case NVM_INFO:
1154 return nvm_ioctl_info(file, argp);
1155 case NVM_GET_DEVICES:
1156 return nvm_ioctl_get_devices(file, argp);
1157 case NVM_DEV_CREATE:
1158 return nvm_ioctl_dev_create(file, argp);
1159 case NVM_DEV_REMOVE:
1160 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001161 case NVM_DEV_INIT:
1162 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001163 case NVM_DEV_FACTORY:
1164 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001165 }
1166 return 0;
1167}
1168
1169static const struct file_operations _ctl_fops = {
1170 .open = nonseekable_open,
1171 .unlocked_ioctl = nvm_ctl_ioctl,
1172 .owner = THIS_MODULE,
1173 .llseek = noop_llseek,
1174};
1175
1176static struct miscdevice _nvm_misc = {
1177 .minor = MISC_DYNAMIC_MINOR,
1178 .name = "lightnvm",
1179 .nodename = "lightnvm/control",
1180 .fops = &_ctl_fops,
1181};
PrasannaKumar Muralidharand6a38c02016-08-25 22:30:50 +05301182module_misc_device(_nvm_misc);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001183
1184MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR);
1185
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001186MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
1187MODULE_LICENSE("GPL v2");
1188MODULE_VERSION("0.1");