blob: 8664fe09cc82c9b8a80457dfb2f96f0bd3f0ff2b [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ález529435e812016-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
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100199int nvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
200{
201 return dev->mt->submit_io(dev, rqd);
202}
203EXPORT_SYMBOL(nvm_submit_io);
204
Javier Gonzálezbb314972016-11-28 22:38:54 +0100205int nvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk, int flags)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100206{
Javier Gonzálezbb314972016-11-28 22:38:54 +0100207 return dev->mt->erase_blk(dev, blk, flags);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100208}
209EXPORT_SYMBOL(nvm_erase_blk);
210
Matias Bjørling069368e2016-01-12 07:49:19 +0100211void nvm_addr_to_generic_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
212{
213 int i;
214
Javier González6d5be952016-05-06 20:03:20 +0200215 if (rqd->nr_ppas > 1) {
216 for (i = 0; i < rqd->nr_ppas; i++)
Matias Bjørling069368e2016-01-12 07:49:19 +0100217 rqd->ppa_list[i] = dev_to_generic_addr(dev,
218 rqd->ppa_list[i]);
219 } else {
220 rqd->ppa_addr = dev_to_generic_addr(dev, rqd->ppa_addr);
221 }
222}
223EXPORT_SYMBOL(nvm_addr_to_generic_mode);
224
225void nvm_generic_to_addr_mode(struct nvm_dev *dev, struct nvm_rq *rqd)
226{
227 int i;
228
Javier González6d5be952016-05-06 20:03:20 +0200229 if (rqd->nr_ppas > 1) {
230 for (i = 0; i < rqd->nr_ppas; i++)
Matias Bjørling069368e2016-01-12 07:49:19 +0100231 rqd->ppa_list[i] = generic_to_dev_addr(dev,
232 rqd->ppa_list[i]);
233 } else {
234 rqd->ppa_addr = generic_to_dev_addr(dev, rqd->ppa_addr);
235 }
236}
237EXPORT_SYMBOL(nvm_generic_to_addr_mode);
238
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100239int nvm_set_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd,
Matias Bjørling8680f162016-07-07 09:54:22 +0200240 const struct ppa_addr *ppas, int nr_ppas, int vblk)
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100241{
242 int i, plane_cnt, pl_idx;
Matias Bjørling8680f162016-07-07 09:54:22 +0200243 struct ppa_addr ppa;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100244
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200245 if ((!vblk || dev->plane_mode == NVM_PLANE_SINGLE) && nr_ppas == 1) {
Javier González6d5be952016-05-06 20:03:20 +0200246 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100247 rqd->ppa_addr = ppas[0];
248
249 return 0;
250 }
251
Javier González6d5be952016-05-06 20:03:20 +0200252 rqd->nr_ppas = nr_ppas;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100253 rqd->ppa_list = nvm_dev_dma_alloc(dev, GFP_KERNEL, &rqd->dma_ppa_list);
254 if (!rqd->ppa_list) {
255 pr_err("nvm: failed to allocate dma memory\n");
256 return -ENOMEM;
257 }
258
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200259 if (!vblk) {
260 for (i = 0; i < nr_ppas; i++)
261 rqd->ppa_list[i] = ppas[i];
262 } else {
263 plane_cnt = dev->plane_mode;
Javier González6d5be952016-05-06 20:03:20 +0200264 rqd->nr_ppas *= plane_cnt;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200265
Matias Bjørling556755e2016-01-12 07:49:26 +0100266 for (i = 0; i < nr_ppas; i++) {
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200267 for (pl_idx = 0; pl_idx < plane_cnt; pl_idx++) {
Matias Bjørling8680f162016-07-07 09:54:22 +0200268 ppa = ppas[i];
269 ppa.g.pl = pl_idx;
270 rqd->ppa_list[(pl_idx * nr_ppas) + i] = ppa;
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200271 }
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100272 }
273 }
274
275 return 0;
276}
277EXPORT_SYMBOL(nvm_set_rqd_ppalist);
278
279void nvm_free_rqd_ppalist(struct nvm_dev *dev, struct nvm_rq *rqd)
280{
281 if (!rqd->ppa_list)
282 return;
283
284 nvm_dev_dma_free(dev, rqd->ppa_list, rqd->dma_ppa_list);
285}
286EXPORT_SYMBOL(nvm_free_rqd_ppalist);
287
Javier Gonzálezbb314972016-11-28 22:38:54 +0100288int nvm_erase_ppa(struct nvm_dev *dev, struct ppa_addr *ppas, int nr_ppas,
289 int flags)
Matias Bjørling069368e2016-01-12 07:49:19 +0100290{
Matias Bjørling069368e2016-01-12 07:49:19 +0100291 struct nvm_rq rqd;
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100292 int ret;
Matias Bjørling069368e2016-01-12 07:49:19 +0100293
294 if (!dev->ops->erase_block)
295 return 0;
296
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100297 memset(&rqd, 0, sizeof(struct nvm_rq));
Matias Bjørling069368e2016-01-12 07:49:19 +0100298
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200299 ret = nvm_set_rqd_ppalist(dev, &rqd, ppas, nr_ppas, 1);
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100300 if (ret)
301 return ret;
Matias Bjørling069368e2016-01-12 07:49:19 +0100302
303 nvm_generic_to_addr_mode(dev, &rqd);
304
Javier Gonzálezbb314972016-11-28 22:38:54 +0100305 rqd.flags = flags;
306
Matias Bjørling069368e2016-01-12 07:49:19 +0100307 ret = dev->ops->erase_block(dev, &rqd);
308
Matias Bjørlingabd805e2016-01-12 07:49:20 +0100309 nvm_free_rqd_ppalist(dev, &rqd);
Matias Bjørling069368e2016-01-12 07:49:19 +0100310
311 return ret;
312}
313EXPORT_SYMBOL(nvm_erase_ppa);
314
Matias Bjørling912761622016-01-12 07:49:21 +0100315void nvm_end_io(struct nvm_rq *rqd, int error)
316{
Matias Bjørling72d256e2016-01-12 07:49:29 +0100317 rqd->error = error;
318 rqd->end_io(rqd);
Matias Bjørling912761622016-01-12 07:49:21 +0100319}
320EXPORT_SYMBOL(nvm_end_io);
321
Matias Bjørling09719b62016-01-12 07:49:30 +0100322static void nvm_end_io_sync(struct nvm_rq *rqd)
Matias Bjørling912761622016-01-12 07:49:21 +0100323{
324 struct completion *waiting = rqd->wait;
325
326 rqd->wait = NULL;
327
328 complete(waiting);
329}
330
Matias Bjørlingd9e46d52016-07-07 09:54:23 +0200331static int __nvm_submit_ppa(struct nvm_dev *dev, struct nvm_rq *rqd, int opcode,
Matias Bjørling1145e632016-05-06 20:02:56 +0200332 int flags, void *buf, int len)
Matias Bjørling09719b62016-01-12 07:49:30 +0100333{
334 DECLARE_COMPLETION_ONSTACK(wait);
Matias Bjørling09719b62016-01-12 07:49:30 +0100335 struct bio *bio;
336 int ret;
337 unsigned long hang_check;
338
339 bio = bio_map_kern(dev->q, buf, len, GFP_KERNEL);
340 if (IS_ERR_OR_NULL(bio))
341 return -ENOMEM;
342
Matias Bjørling1145e632016-05-06 20:02:56 +0200343 nvm_generic_to_addr_mode(dev, rqd);
Matias Bjørling09719b62016-01-12 07:49:30 +0100344
Matias Bjørling1145e632016-05-06 20:02:56 +0200345 rqd->dev = dev;
346 rqd->opcode = opcode;
347 rqd->flags = flags;
348 rqd->bio = bio;
349 rqd->wait = &wait;
350 rqd->end_io = nvm_end_io_sync;
Matias Bjørling09719b62016-01-12 07:49:30 +0100351
Matias Bjørling1145e632016-05-06 20:02:56 +0200352 ret = dev->ops->submit_io(dev, rqd);
Matias Bjørlingecfb40c2016-05-06 20:02:55 +0200353 if (ret) {
Matias Bjørlingecfb40c2016-05-06 20:02:55 +0200354 bio_put(bio);
355 return ret;
356 }
Matias Bjørling09719b62016-01-12 07:49:30 +0100357
358 /* Prevent hang_check timer from firing at us during very long I/O */
359 hang_check = sysctl_hung_task_timeout_secs;
360 if (hang_check)
Matias Bjørling12624af2016-07-07 09:54:13 +0200361 while (!wait_for_completion_io_timeout(&wait,
362 hang_check * (HZ/2)))
363 ;
Matias Bjørling09719b62016-01-12 07:49:30 +0100364 else
365 wait_for_completion_io(&wait);
366
Matias Bjørling1145e632016-05-06 20:02:56 +0200367 return rqd->error;
368}
369
370/**
371 * nvm_submit_ppa_list - submit user-defined ppa list to device. The user must
372 * take to free ppa list if necessary.
373 * @dev: device
374 * @ppa_list: user created ppa_list
375 * @nr_ppas: length of ppa_list
376 * @opcode: device opcode
377 * @flags: device flags
378 * @buf: data buffer
379 * @len: data buffer length
380 */
381int nvm_submit_ppa_list(struct nvm_dev *dev, struct ppa_addr *ppa_list,
382 int nr_ppas, int opcode, int flags, void *buf, int len)
383{
384 struct nvm_rq rqd;
385
386 if (dev->ops->max_phys_sect < nr_ppas)
387 return -EINVAL;
388
389 memset(&rqd, 0, sizeof(struct nvm_rq));
390
Javier González6d5be952016-05-06 20:03:20 +0200391 rqd.nr_ppas = nr_ppas;
Matias Bjørling1145e632016-05-06 20:02:56 +0200392 if (nr_ppas > 1)
393 rqd.ppa_list = ppa_list;
394 else
395 rqd.ppa_addr = ppa_list[0];
396
397 return __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
398}
399EXPORT_SYMBOL(nvm_submit_ppa_list);
400
401/**
402 * nvm_submit_ppa - submit PPAs to device. PPAs will automatically be unfolded
403 * as single, dual, quad plane PPAs depending on device type.
404 * @dev: device
405 * @ppa: user created ppa_list
406 * @nr_ppas: length of ppa_list
407 * @opcode: device opcode
408 * @flags: device flags
409 * @buf: data buffer
410 * @len: data buffer length
411 */
412int nvm_submit_ppa(struct nvm_dev *dev, struct ppa_addr *ppa, int nr_ppas,
413 int opcode, int flags, void *buf, int len)
414{
415 struct nvm_rq rqd;
416 int ret;
417
418 memset(&rqd, 0, sizeof(struct nvm_rq));
Matias Bjørling5ebc7d92016-05-06 20:03:07 +0200419 ret = nvm_set_rqd_ppalist(dev, &rqd, ppa, nr_ppas, 1);
Matias Bjørling1145e632016-05-06 20:02:56 +0200420 if (ret)
421 return ret;
422
423 ret = __nvm_submit_ppa(dev, &rqd, opcode, flags, buf, len);
424
Matias Bjørling09719b62016-01-12 07:49:30 +0100425 nvm_free_rqd_ppalist(dev, &rqd);
426
Matias Bjørling1145e632016-05-06 20:02:56 +0200427 return ret;
Matias Bjørling09719b62016-01-12 07:49:30 +0100428}
429EXPORT_SYMBOL(nvm_submit_ppa);
430
Matias Bjørling22e8c972016-05-06 20:02:58 +0200431/*
432 * folds a bad block list from its plane representation to its virtual
433 * block representation. The fold is done in place and reduced size is
434 * returned.
435 *
436 * If any of the planes status are bad or grown bad block, the virtual block
437 * is marked bad. If not bad, the first plane state acts as the block state.
438 */
439int nvm_bb_tbl_fold(struct nvm_dev *dev, u8 *blks, int nr_blks)
440{
441 int blk, offset, pl, blktype;
442
443 if (nr_blks != dev->blks_per_lun * dev->plane_mode)
444 return -EINVAL;
445
446 for (blk = 0; blk < dev->blks_per_lun; blk++) {
447 offset = blk * dev->plane_mode;
448 blktype = blks[offset];
449
450 /* Bad blocks on any planes take precedence over other types */
451 for (pl = 0; pl < dev->plane_mode; pl++) {
452 if (blks[offset + pl] &
453 (NVM_BLK_T_BAD|NVM_BLK_T_GRWN_BAD)) {
454 blktype = blks[offset + pl];
455 break;
456 }
457 }
458
459 blks[blk] = blktype;
460 }
461
462 return dev->blks_per_lun;
463}
464EXPORT_SYMBOL(nvm_bb_tbl_fold);
465
Matias Bjørlinge11903f2016-05-06 20:03:05 +0200466int nvm_get_bb_tbl(struct nvm_dev *dev, struct ppa_addr ppa, u8 *blks)
467{
468 ppa = generic_to_dev_addr(dev, ppa);
469
470 return dev->ops->get_bb_tbl(dev, ppa, blks);
471}
472EXPORT_SYMBOL(nvm_get_bb_tbl);
473
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100474static int nvm_init_slc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
475{
476 int i;
477
478 dev->lps_per_blk = dev->pgs_per_blk;
479 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
480 if (!dev->lptbl)
481 return -ENOMEM;
482
483 /* Just a linear array */
484 for (i = 0; i < dev->lps_per_blk; i++)
485 dev->lptbl[i] = i;
486
487 return 0;
488}
489
490static int nvm_init_mlc_tbl(struct nvm_dev *dev, struct nvm_id_group *grp)
491{
492 int i, p;
493 struct nvm_id_lp_mlc *mlc = &grp->lptbl.mlc;
494
495 if (!mlc->num_pairs)
496 return 0;
497
498 dev->lps_per_blk = mlc->num_pairs;
499 dev->lptbl = kcalloc(dev->lps_per_blk, sizeof(int), GFP_KERNEL);
500 if (!dev->lptbl)
501 return -ENOMEM;
502
503 /* The lower page table encoding consists of a list of bytes, where each
504 * has a lower and an upper half. The first half byte maintains the
505 * increment value and every value after is an offset added to the
Matias Bjørling12624af2016-07-07 09:54:13 +0200506 * previous incrementation value
507 */
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100508 dev->lptbl[0] = mlc->pairs[0] & 0xF;
509 for (i = 1; i < dev->lps_per_blk; i++) {
510 p = mlc->pairs[i >> 1];
511 if (i & 0x1) /* upper */
512 dev->lptbl[i] = dev->lptbl[i - 1] + ((p & 0xF0) >> 4);
513 else /* lower */
514 dev->lptbl[i] = dev->lptbl[i - 1] + (p & 0xF);
515 }
516
517 return 0;
518}
519
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100520static int nvm_core_init(struct nvm_dev *dev)
521{
522 struct nvm_id *id = &dev->identity;
523 struct nvm_id_group *grp = &id->groups[0];
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200524 int ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100525
526 /* device values */
527 dev->nr_chnls = grp->num_ch;
528 dev->luns_per_chnl = grp->num_lun;
529 dev->pgs_per_blk = grp->num_pg;
530 dev->blks_per_lun = grp->num_blk;
531 dev->nr_planes = grp->num_pln;
Matias Bjørling4891d122016-05-06 20:02:57 +0200532 dev->fpg_size = grp->fpg_sz;
533 dev->pfpg_size = grp->fpg_sz * grp->num_pln;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100534 dev->sec_size = grp->csecs;
535 dev->oob_size = grp->sos;
536 dev->sec_per_pg = grp->fpg_sz / grp->csecs;
Matias Bjørlingf9a99952016-01-12 07:49:34 +0100537 dev->mccap = grp->mccap;
Matias Bjørling7386af22015-11-16 15:34:44 +0100538 memcpy(&dev->ppaf, &id->ppaf, sizeof(struct nvm_addr_format));
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100539
540 dev->plane_mode = NVM_PLANE_SINGLE;
541 dev->max_rq_size = dev->ops->max_phys_sect * dev->sec_size;
542
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100543 if (grp->mpos & 0x020202)
544 dev->plane_mode = NVM_PLANE_DOUBLE;
545 if (grp->mpos & 0x040404)
546 dev->plane_mode = NVM_PLANE_QUAD;
547
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200548 if (grp->mtype != 0) {
549 pr_err("nvm: memory type not supported\n");
550 return -EINVAL;
551 }
552
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100553 /* calculated values */
554 dev->sec_per_pl = dev->sec_per_pg * dev->nr_planes;
555 dev->sec_per_blk = dev->sec_per_pl * dev->pgs_per_blk;
556 dev->sec_per_lun = dev->sec_per_blk * dev->blks_per_lun;
557 dev->nr_luns = dev->luns_per_chnl * dev->nr_chnls;
558
Matias Bjørlinged2a92a2016-02-20 08:52:42 +0100559 dev->total_secs = dev->nr_luns * dev->sec_per_lun;
Wenwei Taoda1e2842016-03-03 15:06:38 +0100560 dev->lun_map = kcalloc(BITS_TO_LONGS(dev->nr_luns),
561 sizeof(unsigned long), GFP_KERNEL);
562 if (!dev->lun_map)
563 return -ENOMEM;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200564
565 switch (grp->fmtype) {
566 case NVM_ID_FMTYPE_SLC:
567 if (nvm_init_slc_tbl(dev, grp)) {
568 ret = -ENOMEM;
569 goto err_fmtype;
570 }
571 break;
572 case NVM_ID_FMTYPE_MLC:
573 if (nvm_init_mlc_tbl(dev, grp)) {
574 ret = -ENOMEM;
575 goto err_fmtype;
576 }
577 break;
578 default:
579 pr_err("nvm: flash type not supported\n");
580 ret = -EINVAL;
581 goto err_fmtype;
582 }
583
Matias Bjørlinge3eb3792016-01-12 07:49:36 +0100584 mutex_init(&dev->mlock);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +0100585 spin_lock_init(&dev->lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100586
Matias Bjørlingac81bfa92016-09-16 14:25:04 +0200587 blk_queue_logical_block_size(dev->q, dev->sec_size);
588
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100589 return 0;
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200590err_fmtype:
591 kfree(dev->lun_map);
592 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100593}
594
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200595static void nvm_free_mgr(struct nvm_dev *dev)
596{
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200597 if (!dev->mt)
598 return;
599
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200600 dev->mt->unregister_mgr(dev);
601 dev->mt = NULL;
602}
603
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200604void nvm_free(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100605{
606 if (!dev)
607 return;
608
Matias Bjørling976bdfc2016-05-06 20:03:17 +0200609 nvm_free_mgr(dev);
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100610
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200611 if (dev->dma_pool)
612 dev->ops->destroy_dma_pool(dev->dma_pool);
613
Matias Bjørlingca5927e2016-01-12 07:49:35 +0100614 kfree(dev->lptbl);
Matias Bjørling7f7c5d02016-05-06 20:02:59 +0200615 kfree(dev->lun_map);
Simon A. F. Lund40267ef2016-09-16 14:25:08 +0200616 kfree(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100617}
618
619static int nvm_init(struct nvm_dev *dev)
620{
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100621 int ret = -EINVAL;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100622
623 if (!dev->q || !dev->ops)
Wenwei Tao480fc0d2015-11-20 13:47:53 +0100624 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100625
Matias Bjørling16f26c32015-12-06 11:25:48 +0100626 if (dev->ops->identity(dev, &dev->identity)) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100627 pr_err("nvm: device could not be identified\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100628 goto err;
629 }
630
631 pr_debug("nvm: ver:%x nvm_vendor:%x groups:%u\n",
632 dev->identity.ver_id, dev->identity.vmnt,
633 dev->identity.cgrps);
634
635 if (dev->identity.ver_id != 1) {
636 pr_err("nvm: device not supported by kernel.");
637 goto err;
638 }
639
640 if (dev->identity.cgrps != 1) {
641 pr_err("nvm: only one group configuration supported.");
642 goto err;
643 }
644
645 ret = nvm_core_init(dev);
646 if (ret) {
647 pr_err("nvm: could not initialize core structures.\n");
648 goto err;
649 }
650
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100651 pr_info("nvm: registered %s [%u/%u/%u/%u/%u/%u]\n",
652 dev->name, dev->sec_per_pg, dev->nr_planes,
653 dev->pgs_per_blk, dev->blks_per_lun, dev->nr_luns,
654 dev->nr_chnls);
655 return 0;
656err:
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100657 pr_err("nvm: failed to initialize nvm\n");
658 return ret;
659}
660
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200661struct nvm_dev *nvm_alloc_dev(int node)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100662{
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200663 return kzalloc_node(sizeof(struct nvm_dev), GFP_KERNEL, node);
664}
665EXPORT_SYMBOL(nvm_alloc_dev);
666
667int nvm_register(struct nvm_dev *dev)
668{
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100669 int ret;
670
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100671 ret = nvm_init(dev);
672 if (ret)
673 goto err_init;
674
Wenwei Taod1601472015-11-28 16:49:25 +0100675 if (dev->ops->max_phys_sect > 256) {
676 pr_info("nvm: max sectors supported is 256.\n");
677 ret = -EINVAL;
678 goto err_init;
679 }
680
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100681 if (dev->ops->max_phys_sect > 1) {
Javier González75b85642016-05-06 20:03:13 +0200682 dev->dma_pool = dev->ops->create_dma_pool(dev, "ppalist");
683 if (!dev->dma_pool) {
684 pr_err("nvm: could not create dma pool\n");
Matias Bjørling93e70c12015-11-20 13:47:54 +0100685 ret = -ENOMEM;
686 goto err_init;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100687 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100688 }
689
Matias Bjørlingbf643182016-02-04 15:13:27 +0100690 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
691 ret = nvm_get_sysblock(dev, &dev->sb);
692 if (!ret)
693 pr_err("nvm: device not initialized.\n");
694 else if (ret < 0)
695 pr_err("nvm: err (%d) on device initialization\n", ret);
696 }
Matias Bjørlingb7692072016-01-12 07:49:38 +0100697
Matias Bjørling762796b2015-12-06 11:25:49 +0100698 /* register device with a supported media manager */
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100699 down_write(&nvm_lock);
Matias Bjørlingb7692072016-01-12 07:49:38 +0100700 if (ret > 0)
701 dev->mt = nvm_init_mgr(dev);
Matias Bjørlingedad2e62015-11-16 15:34:42 +0100702 list_add(&dev->devices, &nvm_devices);
703 up_write(&nvm_lock);
704
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100705 return 0;
706err_init:
Wenwei Taoda1e2842016-03-03 15:06:38 +0100707 kfree(dev->lun_map);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100708 return ret;
709}
710EXPORT_SYMBOL(nvm_register);
711
Matias Bjørlingb0b4e092016-09-16 14:25:07 +0200712void nvm_unregister(struct nvm_dev *dev)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100713{
Wenwei Taod0a712c2015-11-28 16:49:28 +0100714 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100715 list_del(&dev->devices);
716 up_write(&nvm_lock);
Matias Bjørlingc1480ad2015-11-16 15:34:43 +0100717
Matias Bjørling3dc87dd2016-11-28 22:38:53 +0100718 nvm_free(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100719}
720EXPORT_SYMBOL(nvm_unregister);
721
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100722static int __nvm_configure_create(struct nvm_ioctl_create *create)
723{
724 struct nvm_dev *dev;
725 struct nvm_ioctl_create_simple *s;
726
Wenwei Taod0a712c2015-11-28 16:49:28 +0100727 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100728 dev = nvm_find_nvm_dev(create->dev);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100729 up_write(&nvm_lock);
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200730
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100731 if (!dev) {
732 pr_err("nvm: device not found\n");
733 return -EINVAL;
734 }
735
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200736 if (!dev->mt) {
737 pr_info("nvm: device has no media manager registered.\n");
738 return -ENODEV;
739 }
740
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100741 if (create->conf.type != NVM_CONFIG_TYPE_SIMPLE) {
742 pr_err("nvm: config type not valid\n");
743 return -EINVAL;
744 }
745 s = &create->conf.s;
746
747 if (s->lun_begin > s->lun_end || s->lun_end > dev->nr_luns) {
748 pr_err("nvm: lun out of bound (%u:%u > %u)\n",
749 s->lun_begin, s->lun_end, dev->nr_luns);
750 return -EINVAL;
751 }
752
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200753 return dev->mt->create_tgt(dev, create);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100754}
755
756#ifdef CONFIG_NVM_DEBUG
757static int nvm_configure_show(const char *val)
758{
759 struct nvm_dev *dev;
760 char opcode, devname[DISK_NAME_LEN];
761 int ret;
762
763 ret = sscanf(val, "%c %32s", &opcode, devname);
764 if (ret != 2) {
765 pr_err("nvm: invalid command. Use \"opcode devicename\".\n");
766 return -EINVAL;
767 }
768
Wenwei Taod0a712c2015-11-28 16:49:28 +0100769 down_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100770 dev = nvm_find_nvm_dev(devname);
Wenwei Taod0a712c2015-11-28 16:49:28 +0100771 up_write(&nvm_lock);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100772 if (!dev) {
773 pr_err("nvm: device not found\n");
774 return -EINVAL;
775 }
776
777 if (!dev->mt)
778 return 0;
779
Javier Gonzalez2fde0e42015-11-20 13:47:57 +0100780 dev->mt->lun_info_print(dev);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100781
782 return 0;
783}
784
785static int nvm_configure_remove(const char *val)
786{
787 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200788 struct nvm_dev *dev;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100789 char opcode;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200790 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100791
792 ret = sscanf(val, "%c %256s", &opcode, remove.tgtname);
793 if (ret != 2) {
794 pr_err("nvm: invalid command. Use \"d targetname\".\n");
795 return -EINVAL;
796 }
797
798 remove.flags = 0;
799
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +0200800 list_for_each_entry(dev, &nvm_devices, devices) {
801 ret = dev->mt->remove_tgt(dev, &remove);
802 if (!ret)
803 break;
804 }
805
806 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100807}
808
809static int nvm_configure_create(const char *val)
810{
811 struct nvm_ioctl_create create;
812 char opcode;
813 int lun_begin, lun_end, ret;
814
815 ret = sscanf(val, "%c %256s %256s %48s %u:%u", &opcode, create.dev,
816 create.tgtname, create.tgttype,
817 &lun_begin, &lun_end);
818 if (ret != 6) {
819 pr_err("nvm: invalid command. Use \"opcode device name tgttype lun_begin:lun_end\".\n");
820 return -EINVAL;
821 }
822
823 create.flags = 0;
824 create.conf.type = NVM_CONFIG_TYPE_SIMPLE;
825 create.conf.s.lun_begin = lun_begin;
826 create.conf.s.lun_end = lun_end;
827
828 return __nvm_configure_create(&create);
829}
830
831
832/* Exposes administrative interface through /sys/module/lnvm/configure_by_str */
833static int nvm_configure_by_str_event(const char *val,
834 const struct kernel_param *kp)
835{
836 char opcode;
837 int ret;
838
839 ret = sscanf(val, "%c", &opcode);
840 if (ret != 1) {
841 pr_err("nvm: string must have the format of \"cmd ...\"\n");
842 return -EINVAL;
843 }
844
845 switch (opcode) {
846 case 'a':
847 return nvm_configure_create(val);
848 case 'd':
849 return nvm_configure_remove(val);
850 case 's':
851 return nvm_configure_show(val);
852 default:
853 pr_err("nvm: invalid command\n");
854 return -EINVAL;
855 }
856
857 return 0;
858}
859
860static int nvm_configure_get(char *buf, const struct kernel_param *kp)
861{
Alan5e422cf2016-02-19 13:56:57 +0100862 int sz;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100863 struct nvm_dev *dev;
864
Alan5e422cf2016-02-19 13:56:57 +0100865 sz = sprintf(buf, "available devices:\n");
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100866 down_write(&nvm_lock);
867 list_for_each_entry(dev, &nvm_devices, devices) {
Alan5e422cf2016-02-19 13:56:57 +0100868 if (sz > 4095 - DISK_NAME_LEN - 2)
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100869 break;
Alan5e422cf2016-02-19 13:56:57 +0100870 sz += sprintf(buf + sz, " %32s\n", dev->name);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100871 }
872 up_write(&nvm_lock);
873
Alan5e422cf2016-02-19 13:56:57 +0100874 return sz;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100875}
876
877static const struct kernel_param_ops nvm_configure_by_str_event_param_ops = {
878 .set = nvm_configure_by_str_event,
879 .get = nvm_configure_get,
880};
881
882#undef MODULE_PARAM_PREFIX
883#define MODULE_PARAM_PREFIX "lnvm."
884
885module_param_cb(configure_debug, &nvm_configure_by_str_event_param_ops, NULL,
886 0644);
887
888#endif /* CONFIG_NVM_DEBUG */
889
890static long nvm_ioctl_info(struct file *file, void __user *arg)
891{
892 struct nvm_ioctl_info *info;
893 struct nvm_tgt_type *tt;
894 int tgt_iter = 0;
895
896 if (!capable(CAP_SYS_ADMIN))
897 return -EPERM;
898
899 info = memdup_user(arg, sizeof(struct nvm_ioctl_info));
900 if (IS_ERR(info))
901 return -EFAULT;
902
903 info->version[0] = NVM_VERSION_MAJOR;
904 info->version[1] = NVM_VERSION_MINOR;
905 info->version[2] = NVM_VERSION_PATCH;
906
907 down_write(&nvm_lock);
Simon A. F. Lund6063fe32016-05-06 20:03:02 +0200908 list_for_each_entry(tt, &nvm_tgt_types, list) {
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100909 struct nvm_ioctl_info_tgt *tgt = &info->tgts[tgt_iter];
910
911 tgt->version[0] = tt->version[0];
912 tgt->version[1] = tt->version[1];
913 tgt->version[2] = tt->version[2];
914 strncpy(tgt->tgtname, tt->name, NVM_TTYPE_NAME_MAX);
915
916 tgt_iter++;
917 }
918
919 info->tgtsize = tgt_iter;
920 up_write(&nvm_lock);
921
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100922 if (copy_to_user(arg, info, sizeof(struct nvm_ioctl_info))) {
923 kfree(info);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100924 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100925 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100926
927 kfree(info);
928 return 0;
929}
930
931static long nvm_ioctl_get_devices(struct file *file, void __user *arg)
932{
933 struct nvm_ioctl_get_devices *devices;
934 struct nvm_dev *dev;
935 int i = 0;
936
937 if (!capable(CAP_SYS_ADMIN))
938 return -EPERM;
939
940 devices = kzalloc(sizeof(struct nvm_ioctl_get_devices), GFP_KERNEL);
941 if (!devices)
942 return -ENOMEM;
943
944 down_write(&nvm_lock);
945 list_for_each_entry(dev, &nvm_devices, devices) {
946 struct nvm_ioctl_device_info *info = &devices->info[i];
947
948 sprintf(info->devname, "%s", dev->name);
949 if (dev->mt) {
950 info->bmversion[0] = dev->mt->version[0];
951 info->bmversion[1] = dev->mt->version[1];
952 info->bmversion[2] = dev->mt->version[2];
953 sprintf(info->bmname, "%s", dev->mt->name);
954 } else {
955 sprintf(info->bmname, "none");
956 }
957
958 i++;
959 if (i > 31) {
960 pr_err("nvm: max 31 devices can be reported.\n");
961 break;
962 }
963 }
964 up_write(&nvm_lock);
965
966 devices->nr_devices = i;
967
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100968 if (copy_to_user(arg, devices,
969 sizeof(struct nvm_ioctl_get_devices))) {
970 kfree(devices);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100971 return -EFAULT;
Sudip Mukherjee76e25082015-11-28 16:49:24 +0100972 }
Matias Bjørlingcd9e9802015-10-28 19:54:55 +0100973
974 kfree(devices);
975 return 0;
976}
977
978static long nvm_ioctl_dev_create(struct file *file, void __user *arg)
979{
980 struct nvm_ioctl_create create;
981
982 if (!capable(CAP_SYS_ADMIN))
983 return -EPERM;
984
985 if (copy_from_user(&create, arg, sizeof(struct nvm_ioctl_create)))
986 return -EFAULT;
987
988 create.dev[DISK_NAME_LEN - 1] = '\0';
989 create.tgttype[NVM_TTYPE_NAME_MAX - 1] = '\0';
990 create.tgtname[DISK_NAME_LEN - 1] = '\0';
991
992 if (create.flags != 0) {
993 pr_err("nvm: no flags supported\n");
994 return -EINVAL;
995 }
996
997 return __nvm_configure_create(&create);
998}
999
1000static long nvm_ioctl_dev_remove(struct file *file, void __user *arg)
1001{
1002 struct nvm_ioctl_remove remove;
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001003 struct nvm_dev *dev;
1004 int ret = 0;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001005
1006 if (!capable(CAP_SYS_ADMIN))
1007 return -EPERM;
1008
1009 if (copy_from_user(&remove, arg, sizeof(struct nvm_ioctl_remove)))
1010 return -EFAULT;
1011
1012 remove.tgtname[DISK_NAME_LEN - 1] = '\0';
1013
1014 if (remove.flags != 0) {
1015 pr_err("nvm: no flags supported\n");
1016 return -EINVAL;
1017 }
1018
Matias Bjørlingb76eb20b2016-07-07 09:54:16 +02001019 list_for_each_entry(dev, &nvm_devices, devices) {
1020 ret = dev->mt->remove_tgt(dev, &remove);
1021 if (!ret)
1022 break;
1023 }
1024
1025 return ret;
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001026}
1027
Matias Bjørling55696152016-01-12 07:49:37 +01001028static void nvm_setup_nvm_sb_info(struct nvm_sb_info *info)
1029{
1030 info->seqnr = 1;
1031 info->erase_cnt = 0;
1032 info->version = 1;
1033}
1034
1035static long __nvm_ioctl_dev_init(struct nvm_ioctl_dev_init *init)
1036{
1037 struct nvm_dev *dev;
1038 struct nvm_sb_info info;
Matias Bjørlingb7692072016-01-12 07:49:38 +01001039 int ret;
Matias Bjørling55696152016-01-12 07:49:37 +01001040
1041 down_write(&nvm_lock);
1042 dev = nvm_find_nvm_dev(init->dev);
1043 up_write(&nvm_lock);
1044 if (!dev) {
1045 pr_err("nvm: device not found\n");
1046 return -EINVAL;
1047 }
1048
1049 nvm_setup_nvm_sb_info(&info);
1050
1051 strncpy(info.mmtype, init->mmtype, NVM_MMTYPE_LEN);
1052 info.fs_ppa.ppa = -1;
1053
Matias Bjørlingbf643182016-02-04 15:13:27 +01001054 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT) {
1055 ret = nvm_init_sysblock(dev, &info);
1056 if (ret)
1057 return ret;
1058 }
Matias Bjørlingb7692072016-01-12 07:49:38 +01001059
1060 memcpy(&dev->sb, &info, sizeof(struct nvm_sb_info));
1061
1062 down_write(&nvm_lock);
1063 dev->mt = nvm_init_mgr(dev);
1064 up_write(&nvm_lock);
1065
1066 return 0;
Matias Bjørling55696152016-01-12 07:49:37 +01001067}
1068
1069static long nvm_ioctl_dev_init(struct file *file, void __user *arg)
1070{
1071 struct nvm_ioctl_dev_init init;
1072
1073 if (!capable(CAP_SYS_ADMIN))
1074 return -EPERM;
1075
1076 if (copy_from_user(&init, arg, sizeof(struct nvm_ioctl_dev_init)))
1077 return -EFAULT;
1078
1079 if (init.flags != 0) {
1080 pr_err("nvm: no flags supported\n");
1081 return -EINVAL;
1082 }
1083
1084 init.dev[DISK_NAME_LEN - 1] = '\0';
1085
1086 return __nvm_ioctl_dev_init(&init);
1087}
1088
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001089static long nvm_ioctl_dev_factory(struct file *file, void __user *arg)
1090{
1091 struct nvm_ioctl_dev_factory fact;
1092 struct nvm_dev *dev;
1093
1094 if (!capable(CAP_SYS_ADMIN))
1095 return -EPERM;
1096
1097 if (copy_from_user(&fact, arg, sizeof(struct nvm_ioctl_dev_factory)))
1098 return -EFAULT;
1099
1100 fact.dev[DISK_NAME_LEN - 1] = '\0';
1101
1102 if (fact.flags & ~(NVM_FACTORY_NR_BITS - 1))
1103 return -EINVAL;
1104
1105 down_write(&nvm_lock);
1106 dev = nvm_find_nvm_dev(fact.dev);
1107 up_write(&nvm_lock);
1108 if (!dev) {
1109 pr_err("nvm: device not found\n");
1110 return -EINVAL;
1111 }
1112
Matias Bjørling976bdfc2016-05-06 20:03:17 +02001113 nvm_free_mgr(dev);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001114
Matias Bjørlingbf643182016-02-04 15:13:27 +01001115 if (dev->identity.cap & NVM_ID_DCAP_BBLKMGMT)
1116 return nvm_dev_factory(dev, fact.flags);
1117
1118 return 0;
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001119}
1120
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001121static long nvm_ctl_ioctl(struct file *file, uint cmd, unsigned long arg)
1122{
1123 void __user *argp = (void __user *)arg;
1124
1125 switch (cmd) {
1126 case NVM_INFO:
1127 return nvm_ioctl_info(file, argp);
1128 case NVM_GET_DEVICES:
1129 return nvm_ioctl_get_devices(file, argp);
1130 case NVM_DEV_CREATE:
1131 return nvm_ioctl_dev_create(file, argp);
1132 case NVM_DEV_REMOVE:
1133 return nvm_ioctl_dev_remove(file, argp);
Matias Bjørling55696152016-01-12 07:49:37 +01001134 case NVM_DEV_INIT:
1135 return nvm_ioctl_dev_init(file, argp);
Matias Bjørling8b4970c2016-01-12 07:49:39 +01001136 case NVM_DEV_FACTORY:
1137 return nvm_ioctl_dev_factory(file, argp);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001138 }
1139 return 0;
1140}
1141
1142static const struct file_operations _ctl_fops = {
1143 .open = nonseekable_open,
1144 .unlocked_ioctl = nvm_ctl_ioctl,
1145 .owner = THIS_MODULE,
1146 .llseek = noop_llseek,
1147};
1148
1149static struct miscdevice _nvm_misc = {
1150 .minor = MISC_DYNAMIC_MINOR,
1151 .name = "lightnvm",
1152 .nodename = "lightnvm/control",
1153 .fops = &_ctl_fops,
1154};
PrasannaKumar Muralidharand6a38c02016-08-25 22:30:50 +05301155module_misc_device(_nvm_misc);
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001156
1157MODULE_ALIAS_MISCDEV(MISC_DYNAMIC_MINOR);
1158
Matias Bjørlingcd9e9802015-10-28 19:54:55 +01001159MODULE_AUTHOR("Matias Bjorling <m@bjorling.me>");
1160MODULE_LICENSE("GPL v2");
1161MODULE_VERSION("0.1");