blob: 262da6dd90568d3cb8cd73482f6a43ebc7fa02e6 [file] [log] [blame]
Matias Bjørling48add0f2015-10-28 19:54:56 +01001/*
2 * Copyright (C) 2015 Matias Bjorling <m@bjorling.me>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License version
6 * 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
16 * USA.
17 *
18 * Implementation of a generic nvm manager for Open-Channel SSDs.
19 */
20
21#include "gennvm.h"
22
23static void gennvm_blocks_free(struct nvm_dev *dev)
24{
25 struct gen_nvm *gn = dev->mp;
26 struct gen_lun *lun;
27 int i;
28
29 gennvm_for_each_lun(gn, lun, i) {
30 if (!lun->vlun.blocks)
31 break;
32 vfree(lun->vlun.blocks);
33 }
34}
35
36static void gennvm_luns_free(struct nvm_dev *dev)
37{
38 struct gen_nvm *gn = dev->mp;
39
40 kfree(gn->luns);
41}
42
43static int gennvm_luns_init(struct nvm_dev *dev, struct gen_nvm *gn)
44{
45 struct gen_lun *lun;
46 int i;
47
48 gn->luns = kcalloc(dev->nr_luns, sizeof(struct gen_lun), GFP_KERNEL);
49 if (!gn->luns)
50 return -ENOMEM;
51
52 gennvm_for_each_lun(gn, lun, i) {
53 spin_lock_init(&lun->vlun.lock);
54 INIT_LIST_HEAD(&lun->free_list);
55 INIT_LIST_HEAD(&lun->used_list);
56 INIT_LIST_HEAD(&lun->bb_list);
57
58 lun->reserved_blocks = 2; /* for GC only */
59 lun->vlun.id = i;
60 lun->vlun.lun_id = i % dev->luns_per_chnl;
61 lun->vlun.chnl_id = i / dev->luns_per_chnl;
62 lun->vlun.nr_free_blocks = dev->blks_per_lun;
Javier Gonzalez0b597332015-11-20 13:47:56 +010063 lun->vlun.nr_inuse_blocks = 0;
64 lun->vlun.nr_bad_blocks = 0;
Matias Bjørling48add0f2015-10-28 19:54:56 +010065 }
66 return 0;
67}
68
Matias Bjørling11450462015-11-16 15:34:37 +010069static int gennvm_block_bb(struct ppa_addr ppa, int nr_blocks, u8 *blks,
Matias Bjørling48add0f2015-10-28 19:54:56 +010070 void *private)
71{
72 struct gen_nvm *gn = private;
Matias Bjørling11450462015-11-16 15:34:37 +010073 struct nvm_dev *dev = gn->dev;
74 struct gen_lun *lun;
Matias Bjørling48add0f2015-10-28 19:54:56 +010075 struct nvm_block *blk;
76 int i;
77
Matias Bjørlingc3293a92015-12-29 14:37:56 +010078 lun = &gn->luns[(dev->luns_per_chnl * ppa.g.ch) + ppa.g.lun];
Matias Bjørling48add0f2015-10-28 19:54:56 +010079
Matias Bjørling11450462015-11-16 15:34:37 +010080 for (i = 0; i < nr_blocks; i++) {
81 if (blks[i] == 0)
82 continue;
83
Matias Bjørling48add0f2015-10-28 19:54:56 +010084 blk = &lun->vlun.blocks[i];
85 if (!blk) {
86 pr_err("gennvm: BB data is out of bounds.\n");
87 return -EINVAL;
88 }
89
90 list_move_tail(&blk->list, &lun->bb_list);
Javier Gonzalez0b597332015-11-20 13:47:56 +010091 lun->vlun.nr_bad_blocks++;
Chao Yubdded152016-01-12 07:49:16 +010092 lun->vlun.nr_free_blocks--;
Matias Bjørling48add0f2015-10-28 19:54:56 +010093 }
94
95 return 0;
96}
97
98static int gennvm_block_map(u64 slba, u32 nlb, __le64 *entries, void *private)
99{
100 struct nvm_dev *dev = private;
101 struct gen_nvm *gn = dev->mp;
102 sector_t max_pages = dev->total_pages * (dev->sec_size >> 9);
103 u64 elba = slba + nlb;
104 struct gen_lun *lun;
105 struct nvm_block *blk;
106 u64 i;
107 int lun_id;
108
109 if (unlikely(elba > dev->total_pages)) {
110 pr_err("gennvm: L2P data from device is out of bounds!\n");
111 return -EINVAL;
112 }
113
114 for (i = 0; i < nlb; i++) {
115 u64 pba = le64_to_cpu(entries[i]);
116
117 if (unlikely(pba >= max_pages && pba != U64_MAX)) {
118 pr_err("gennvm: L2P data entry is out of bounds!\n");
119 return -EINVAL;
120 }
121
122 /* Address zero is a special one. The first page on a disk is
123 * protected. It often holds internal device boot
124 * information.
125 */
126 if (!pba)
127 continue;
128
129 /* resolve block from physical address */
130 lun_id = div_u64(pba, dev->sec_per_lun);
131 lun = &gn->luns[lun_id];
132
133 /* Calculate block offset into lun */
134 pba = pba - (dev->sec_per_lun * lun_id);
135 blk = &lun->vlun.blocks[div_u64(pba, dev->sec_per_blk)];
136
137 if (!blk->type) {
138 /* at this point, we don't know anything about the
139 * block. It's up to the FTL on top to re-etablish the
140 * block state
141 */
142 list_move_tail(&blk->list, &lun->used_list);
143 blk->type = 1;
144 lun->vlun.nr_free_blocks--;
Javier Gonzalez0b597332015-11-20 13:47:56 +0100145 lun->vlun.nr_inuse_blocks++;
Matias Bjørling48add0f2015-10-28 19:54:56 +0100146 }
147 }
148
149 return 0;
150}
151
152static int gennvm_blocks_init(struct nvm_dev *dev, struct gen_nvm *gn)
153{
154 struct gen_lun *lun;
155 struct nvm_block *block;
156 sector_t lun_iter, blk_iter, cur_block_id = 0;
157 int ret;
158
159 gennvm_for_each_lun(gn, lun, lun_iter) {
160 lun->vlun.blocks = vzalloc(sizeof(struct nvm_block) *
161 dev->blks_per_lun);
162 if (!lun->vlun.blocks)
163 return -ENOMEM;
164
165 for (blk_iter = 0; blk_iter < dev->blks_per_lun; blk_iter++) {
166 block = &lun->vlun.blocks[blk_iter];
167
168 INIT_LIST_HEAD(&block->list);
169
170 block->lun = &lun->vlun;
171 block->id = cur_block_id++;
172
173 /* First block is reserved for device */
Javier Gonzalez0b597332015-11-20 13:47:56 +0100174 if (unlikely(lun_iter == 0 && blk_iter == 0)) {
175 lun->vlun.nr_free_blocks--;
Matias Bjørling48add0f2015-10-28 19:54:56 +0100176 continue;
Javier Gonzalez0b597332015-11-20 13:47:56 +0100177 }
Matias Bjørling48add0f2015-10-28 19:54:56 +0100178
179 list_add_tail(&block->list, &lun->free_list);
180 }
181
182 if (dev->ops->get_bb_tbl) {
Matias Bjørling11450462015-11-16 15:34:37 +0100183 struct ppa_addr ppa;
184
185 ppa.ppa = 0;
186 ppa.g.ch = lun->vlun.chnl_id;
187 ppa.g.lun = lun->vlun.id;
Matias Bjørling7386af22015-11-16 15:34:44 +0100188 ppa = generic_to_dev_addr(dev, ppa);
Matias Bjørling11450462015-11-16 15:34:37 +0100189
Matias Bjørling08236c62015-11-28 16:49:27 +0100190 ret = dev->ops->get_bb_tbl(dev, ppa,
Matias Bjørling11450462015-11-16 15:34:37 +0100191 dev->blks_per_lun,
192 gennvm_block_bb, gn);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100193 if (ret)
194 pr_err("gennvm: could not read BB table\n");
195 }
196 }
197
198 if (dev->ops->get_l2p_tbl) {
Matias Bjørling16f26c32015-12-06 11:25:48 +0100199 ret = dev->ops->get_l2p_tbl(dev, 0, dev->total_pages,
Matias Bjørling48add0f2015-10-28 19:54:56 +0100200 gennvm_block_map, dev);
201 if (ret) {
202 pr_err("gennvm: could not read L2P table.\n");
203 pr_warn("gennvm: default block initialization");
204 }
205 }
206
207 return 0;
208}
209
Wenwei Tao8261bd482015-11-28 16:49:23 +0100210static void gennvm_free(struct nvm_dev *dev)
211{
212 gennvm_blocks_free(dev);
213 gennvm_luns_free(dev);
214 kfree(dev->mp);
215 dev->mp = NULL;
216}
217
Matias Bjørling48add0f2015-10-28 19:54:56 +0100218static int gennvm_register(struct nvm_dev *dev)
219{
220 struct gen_nvm *gn;
221 int ret;
222
Matias Bjørling008b7442015-12-06 11:25:50 +0100223 if (!try_module_get(THIS_MODULE))
224 return -ENODEV;
225
Matias Bjørling48add0f2015-10-28 19:54:56 +0100226 gn = kzalloc(sizeof(struct gen_nvm), GFP_KERNEL);
227 if (!gn)
228 return -ENOMEM;
229
Matias Bjørling11450462015-11-16 15:34:37 +0100230 gn->dev = dev;
Matias Bjørling48add0f2015-10-28 19:54:56 +0100231 gn->nr_luns = dev->nr_luns;
232 dev->mp = gn;
233
234 ret = gennvm_luns_init(dev, gn);
235 if (ret) {
236 pr_err("gennvm: could not initialize luns\n");
237 goto err;
238 }
239
240 ret = gennvm_blocks_init(dev, gn);
241 if (ret) {
242 pr_err("gennvm: could not initialize blocks\n");
243 goto err;
244 }
245
246 return 1;
247err:
Wenwei Tao8261bd482015-11-28 16:49:23 +0100248 gennvm_free(dev);
Matias Bjørling008b7442015-12-06 11:25:50 +0100249 module_put(THIS_MODULE);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100250 return ret;
251}
252
253static void gennvm_unregister(struct nvm_dev *dev)
254{
Wenwei Tao8261bd482015-11-28 16:49:23 +0100255 gennvm_free(dev);
Matias Bjørling008b7442015-12-06 11:25:50 +0100256 module_put(THIS_MODULE);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100257}
258
259static struct nvm_block *gennvm_get_blk(struct nvm_dev *dev,
260 struct nvm_lun *vlun, unsigned long flags)
261{
262 struct gen_lun *lun = container_of(vlun, struct gen_lun, vlun);
263 struct nvm_block *blk = NULL;
264 int is_gc = flags & NVM_IOTYPE_GC;
265
266 spin_lock(&vlun->lock);
267
268 if (list_empty(&lun->free_list)) {
269 pr_err_ratelimited("gennvm: lun %u have no free pages available",
270 lun->vlun.id);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100271 goto out;
272 }
273
Wenwei Taoe9b76a82015-12-06 11:25:45 +0100274 if (!is_gc && lun->vlun.nr_free_blocks < lun->reserved_blocks)
Matias Bjørling48add0f2015-10-28 19:54:56 +0100275 goto out;
Matias Bjørling48add0f2015-10-28 19:54:56 +0100276
277 blk = list_first_entry(&lun->free_list, struct nvm_block, list);
278 list_move_tail(&blk->list, &lun->used_list);
279 blk->type = 1;
280
281 lun->vlun.nr_free_blocks--;
Javier Gonzalez0b597332015-11-20 13:47:56 +0100282 lun->vlun.nr_inuse_blocks++;
Matias Bjørling48add0f2015-10-28 19:54:56 +0100283
Matias Bjørling48add0f2015-10-28 19:54:56 +0100284out:
Wenwei Taoe9b76a82015-12-06 11:25:45 +0100285 spin_unlock(&vlun->lock);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100286 return blk;
287}
288
289static void gennvm_put_blk(struct nvm_dev *dev, struct nvm_block *blk)
290{
291 struct nvm_lun *vlun = blk->lun;
292 struct gen_lun *lun = container_of(vlun, struct gen_lun, vlun);
293
294 spin_lock(&vlun->lock);
295
296 switch (blk->type) {
297 case 1:
298 list_move_tail(&blk->list, &lun->free_list);
299 lun->vlun.nr_free_blocks++;
Javier Gonzalez0b597332015-11-20 13:47:56 +0100300 lun->vlun.nr_inuse_blocks--;
Matias Bjørling48add0f2015-10-28 19:54:56 +0100301 blk->type = 0;
302 break;
303 case 2:
304 list_move_tail(&blk->list, &lun->bb_list);
Javier Gonzalez0b597332015-11-20 13:47:56 +0100305 lun->vlun.nr_bad_blocks++;
306 lun->vlun.nr_inuse_blocks--;
Matias Bjørling48add0f2015-10-28 19:54:56 +0100307 break;
308 default:
309 WARN_ON_ONCE(1);
310 pr_err("gennvm: erroneous block type (%lu -> %u)\n",
311 blk->id, blk->type);
312 list_move_tail(&blk->list, &lun->bb_list);
Javier Gonzalez0b597332015-11-20 13:47:56 +0100313 lun->vlun.nr_bad_blocks++;
314 lun->vlun.nr_inuse_blocks--;
Matias Bjørling48add0f2015-10-28 19:54:56 +0100315 }
316
317 spin_unlock(&vlun->lock);
318}
319
Matias Bjørling48add0f2015-10-28 19:54:56 +0100320static void gennvm_blk_set_type(struct nvm_dev *dev, struct ppa_addr *ppa,
321 int type)
322{
323 struct gen_nvm *gn = dev->mp;
324 struct gen_lun *lun;
325 struct nvm_block *blk;
326
327 if (unlikely(ppa->g.ch > dev->nr_chnls ||
328 ppa->g.lun > dev->luns_per_chnl ||
329 ppa->g.blk > dev->blks_per_lun)) {
330 WARN_ON_ONCE(1);
331 pr_err("gennvm: ppa broken (ch: %u > %u lun: %u > %u blk: %u > %u",
332 ppa->g.ch, dev->nr_chnls,
333 ppa->g.lun, dev->luns_per_chnl,
334 ppa->g.blk, dev->blks_per_lun);
335 return;
336 }
337
338 lun = &gn->luns[ppa->g.lun * ppa->g.ch];
339 blk = &lun->vlun.blocks[ppa->g.blk];
340
341 /* will be moved to bb list on put_blk from target */
342 blk->type = type;
343}
344
345/* mark block bad. It is expected the target recover from the error. */
346static void gennvm_mark_blk_bad(struct nvm_dev *dev, struct nvm_rq *rqd)
347{
348 int i;
349
Matias Bjørling11450462015-11-16 15:34:37 +0100350 if (!dev->ops->set_bb_tbl)
Matias Bjørling48add0f2015-10-28 19:54:56 +0100351 return;
352
Matias Bjørling16f26c32015-12-06 11:25:48 +0100353 if (dev->ops->set_bb_tbl(dev, rqd, 1))
Matias Bjørling48add0f2015-10-28 19:54:56 +0100354 return;
355
Matias Bjørling069368e2016-01-12 07:49:19 +0100356 nvm_addr_to_generic_mode(dev, rqd);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100357
358 /* look up blocks and mark them as bad */
359 if (rqd->nr_pages > 1)
360 for (i = 0; i < rqd->nr_pages; i++)
361 gennvm_blk_set_type(dev, &rqd->ppa_list[i], 2);
362 else
363 gennvm_blk_set_type(dev, &rqd->ppa_addr, 2);
364}
365
Matias Bjørling912761622016-01-12 07:49:21 +0100366static void gennvm_end_io(struct nvm_rq *rqd, int error)
Matias Bjørling48add0f2015-10-28 19:54:56 +0100367{
368 struct nvm_tgt_instance *ins = rqd->ins;
Matias Bjørling48add0f2015-10-28 19:54:56 +0100369
370 switch (error) {
371 case NVM_RSP_SUCCESS:
Matias Bjørling48add0f2015-10-28 19:54:56 +0100372 case NVM_RSP_ERR_EMPTYPAGE:
373 break;
374 case NVM_RSP_ERR_FAILWRITE:
375 gennvm_mark_blk_bad(rqd->dev, rqd);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100376 }
377
Matias Bjørling912761622016-01-12 07:49:21 +0100378 ins->tt->end_io(rqd, error);
379}
Matias Bjørling48add0f2015-10-28 19:54:56 +0100380
Matias Bjørling912761622016-01-12 07:49:21 +0100381static int gennvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
382{
383 if (!dev->ops->submit_io)
384 return -ENODEV;
385
386 /* Convert address space */
387 nvm_generic_to_addr_mode(dev, rqd);
388
389 rqd->dev = dev;
390 rqd->end_io = gennvm_end_io;
391 return dev->ops->submit_io(dev, rqd);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100392}
393
394static int gennvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk,
395 unsigned long flags)
396{
Matias Bjørling069368e2016-01-12 07:49:19 +0100397 struct ppa_addr addr = block_to_ppa(dev, blk);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100398
Matias Bjørling81e681d2016-01-12 07:49:28 +0100399 return nvm_erase_ppa(dev, &addr, 1);
Matias Bjørling48add0f2015-10-28 19:54:56 +0100400}
401
402static struct nvm_lun *gennvm_get_lun(struct nvm_dev *dev, int lunid)
403{
404 struct gen_nvm *gn = dev->mp;
405
406 return &gn->luns[lunid].vlun;
407}
408
Javier Gonzalez2fde0e42015-11-20 13:47:57 +0100409static void gennvm_lun_info_print(struct nvm_dev *dev)
Matias Bjørling48add0f2015-10-28 19:54:56 +0100410{
411 struct gen_nvm *gn = dev->mp;
412 struct gen_lun *lun;
413 unsigned int i;
414
Javier Gonzalez2fde0e42015-11-20 13:47:57 +0100415
416 gennvm_for_each_lun(gn, lun, i) {
417 spin_lock(&lun->vlun.lock);
418
419 pr_info("%s: lun%8u\t%u\t%u\t%u\n",
420 dev->name, i,
421 lun->vlun.nr_free_blocks,
422 lun->vlun.nr_inuse_blocks,
423 lun->vlun.nr_bad_blocks);
424
425 spin_unlock(&lun->vlun.lock);
426 }
Matias Bjørling48add0f2015-10-28 19:54:56 +0100427}
428
429static struct nvmm_type gennvm = {
430 .name = "gennvm",
431 .version = {0, 1, 0},
432
433 .register_mgr = gennvm_register,
434 .unregister_mgr = gennvm_unregister,
435
436 .get_blk = gennvm_get_blk,
437 .put_blk = gennvm_put_blk,
438
439 .submit_io = gennvm_submit_io,
Matias Bjørling48add0f2015-10-28 19:54:56 +0100440 .erase_blk = gennvm_erase_blk,
441
442 .get_lun = gennvm_get_lun,
Javier Gonzalez2fde0e42015-11-20 13:47:57 +0100443 .lun_info_print = gennvm_lun_info_print,
Matias Bjørling48add0f2015-10-28 19:54:56 +0100444};
445
446static int __init gennvm_module_init(void)
447{
448 return nvm_register_mgr(&gennvm);
449}
450
451static void gennvm_module_exit(void)
452{
453 nvm_unregister_mgr(&gennvm);
454}
455
456module_init(gennvm_module_init);
457module_exit(gennvm_module_exit);
458MODULE_LICENSE("GPL v2");
459MODULE_DESCRIPTION("Generic media manager for Open-Channel SSDs");