2 * Copyright (C) 2015 Matias Bjorling <m@bjorling.me>
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.
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.
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,
18 * Implementation of a generic nvm manager for Open-Channel SSDs.
23 static void gennvm_blocks_free(struct nvm_dev
*dev
)
25 struct gen_nvm
*gn
= dev
->mp
;
29 gennvm_for_each_lun(gn
, lun
, i
) {
30 if (!lun
->vlun
.blocks
)
32 vfree(lun
->vlun
.blocks
);
36 static void gennvm_luns_free(struct nvm_dev
*dev
)
38 struct gen_nvm
*gn
= dev
->mp
;
43 static int gennvm_luns_init(struct nvm_dev
*dev
, struct gen_nvm
*gn
)
48 gn
->luns
= kcalloc(dev
->nr_luns
, sizeof(struct gen_lun
), GFP_KERNEL
);
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
);
58 lun
->reserved_blocks
= 2; /* for GC only */
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
;
63 lun
->vlun
.nr_inuse_blocks
= 0;
64 lun
->vlun
.nr_bad_blocks
= 0;
69 static int gennvm_block_bb(struct ppa_addr ppa
, int nr_blocks
, u8
*blks
,
72 struct gen_nvm
*gn
= private;
73 struct nvm_dev
*dev
= gn
->dev
;
75 struct nvm_block
*blk
;
78 lun
= &gn
->luns
[(dev
->luns_per_chnl
* ppa
.g
.ch
) + ppa
.g
.lun
];
80 for (i
= 0; i
< nr_blocks
; i
++) {
84 blk
= &lun
->vlun
.blocks
[i
];
86 pr_err("gennvm: BB data is out of bounds.\n");
90 list_move_tail(&blk
->list
, &lun
->bb_list
);
91 lun
->vlun
.nr_bad_blocks
++;
97 static int gennvm_block_map(u64 slba
, u32 nlb
, __le64
*entries
, void *private)
99 struct nvm_dev
*dev
= private;
100 struct gen_nvm
*gn
= dev
->mp
;
101 sector_t max_pages
= dev
->total_pages
* (dev
->sec_size
>> 9);
102 u64 elba
= slba
+ nlb
;
104 struct nvm_block
*blk
;
108 if (unlikely(elba
> dev
->total_pages
)) {
109 pr_err("gennvm: L2P data from device is out of bounds!\n");
113 for (i
= 0; i
< nlb
; i
++) {
114 u64 pba
= le64_to_cpu(entries
[i
]);
116 if (unlikely(pba
>= max_pages
&& pba
!= U64_MAX
)) {
117 pr_err("gennvm: L2P data entry is out of bounds!\n");
121 /* Address zero is a special one. The first page on a disk is
122 * protected. It often holds internal device boot
128 /* resolve block from physical address */
129 lun_id
= div_u64(pba
, dev
->sec_per_lun
);
130 lun
= &gn
->luns
[lun_id
];
132 /* Calculate block offset into lun */
133 pba
= pba
- (dev
->sec_per_lun
* lun_id
);
134 blk
= &lun
->vlun
.blocks
[div_u64(pba
, dev
->sec_per_blk
)];
137 /* at this point, we don't know anything about the
138 * block. It's up to the FTL on top to re-etablish the
141 list_move_tail(&blk
->list
, &lun
->used_list
);
143 lun
->vlun
.nr_free_blocks
--;
144 lun
->vlun
.nr_inuse_blocks
++;
151 static int gennvm_blocks_init(struct nvm_dev
*dev
, struct gen_nvm
*gn
)
154 struct nvm_block
*block
;
155 sector_t lun_iter
, blk_iter
, cur_block_id
= 0;
158 gennvm_for_each_lun(gn
, lun
, lun_iter
) {
159 lun
->vlun
.blocks
= vzalloc(sizeof(struct nvm_block
) *
161 if (!lun
->vlun
.blocks
)
164 for (blk_iter
= 0; blk_iter
< dev
->blks_per_lun
; blk_iter
++) {
165 block
= &lun
->vlun
.blocks
[blk_iter
];
167 INIT_LIST_HEAD(&block
->list
);
169 block
->lun
= &lun
->vlun
;
170 block
->id
= cur_block_id
++;
172 /* First block is reserved for device */
173 if (unlikely(lun_iter
== 0 && blk_iter
== 0)) {
174 lun
->vlun
.nr_free_blocks
--;
178 list_add_tail(&block
->list
, &lun
->free_list
);
181 if (dev
->ops
->get_bb_tbl
) {
185 ppa
.g
.ch
= lun
->vlun
.chnl_id
;
186 ppa
.g
.lun
= lun
->vlun
.id
;
187 ppa
= generic_to_dev_addr(dev
, ppa
);
189 ret
= dev
->ops
->get_bb_tbl(dev
, ppa
,
191 gennvm_block_bb
, gn
);
193 pr_err("gennvm: could not read BB table\n");
197 if (dev
->ops
->get_l2p_tbl
) {
198 ret
= dev
->ops
->get_l2p_tbl(dev
, 0, dev
->total_pages
,
199 gennvm_block_map
, dev
);
201 pr_err("gennvm: could not read L2P table.\n");
202 pr_warn("gennvm: default block initialization");
209 static void gennvm_free(struct nvm_dev
*dev
)
211 gennvm_blocks_free(dev
);
212 gennvm_luns_free(dev
);
217 static int gennvm_register(struct nvm_dev
*dev
)
222 if (!try_module_get(THIS_MODULE
))
225 gn
= kzalloc(sizeof(struct gen_nvm
), GFP_KERNEL
);
230 gn
->nr_luns
= dev
->nr_luns
;
233 ret
= gennvm_luns_init(dev
, gn
);
235 pr_err("gennvm: could not initialize luns\n");
239 ret
= gennvm_blocks_init(dev
, gn
);
241 pr_err("gennvm: could not initialize blocks\n");
248 module_put(THIS_MODULE
);
252 static void gennvm_unregister(struct nvm_dev
*dev
)
255 module_put(THIS_MODULE
);
258 static struct nvm_block
*gennvm_get_blk(struct nvm_dev
*dev
,
259 struct nvm_lun
*vlun
, unsigned long flags
)
261 struct gen_lun
*lun
= container_of(vlun
, struct gen_lun
, vlun
);
262 struct nvm_block
*blk
= NULL
;
263 int is_gc
= flags
& NVM_IOTYPE_GC
;
265 spin_lock(&vlun
->lock
);
267 if (list_empty(&lun
->free_list
)) {
268 pr_err_ratelimited("gennvm: lun %u have no free pages available",
273 if (!is_gc
&& lun
->vlun
.nr_free_blocks
< lun
->reserved_blocks
)
276 blk
= list_first_entry(&lun
->free_list
, struct nvm_block
, list
);
277 list_move_tail(&blk
->list
, &lun
->used_list
);
280 lun
->vlun
.nr_free_blocks
--;
281 lun
->vlun
.nr_inuse_blocks
++;
284 spin_unlock(&vlun
->lock
);
288 static void gennvm_put_blk(struct nvm_dev
*dev
, struct nvm_block
*blk
)
290 struct nvm_lun
*vlun
= blk
->lun
;
291 struct gen_lun
*lun
= container_of(vlun
, struct gen_lun
, vlun
);
293 spin_lock(&vlun
->lock
);
297 list_move_tail(&blk
->list
, &lun
->free_list
);
298 lun
->vlun
.nr_free_blocks
++;
299 lun
->vlun
.nr_inuse_blocks
--;
303 list_move_tail(&blk
->list
, &lun
->bb_list
);
304 lun
->vlun
.nr_bad_blocks
++;
305 lun
->vlun
.nr_inuse_blocks
--;
309 pr_err("gennvm: erroneous block type (%lu -> %u)\n",
311 list_move_tail(&blk
->list
, &lun
->bb_list
);
312 lun
->vlun
.nr_bad_blocks
++;
313 lun
->vlun
.nr_inuse_blocks
--;
316 spin_unlock(&vlun
->lock
);
319 static void gennvm_addr_to_generic_mode(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
323 if (rqd
->nr_pages
> 1) {
324 for (i
= 0; i
< rqd
->nr_pages
; i
++)
325 rqd
->ppa_list
[i
] = dev_to_generic_addr(dev
,
328 rqd
->ppa_addr
= dev_to_generic_addr(dev
, rqd
->ppa_addr
);
332 static void gennvm_generic_to_addr_mode(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
336 if (rqd
->nr_pages
> 1) {
337 for (i
= 0; i
< rqd
->nr_pages
; i
++)
338 rqd
->ppa_list
[i
] = generic_to_dev_addr(dev
,
341 rqd
->ppa_addr
= generic_to_dev_addr(dev
, rqd
->ppa_addr
);
345 static int gennvm_submit_io(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
347 if (!dev
->ops
->submit_io
)
350 /* Convert address space */
351 gennvm_generic_to_addr_mode(dev
, rqd
);
354 return dev
->ops
->submit_io(dev
, rqd
);
357 static void gennvm_blk_set_type(struct nvm_dev
*dev
, struct ppa_addr
*ppa
,
360 struct gen_nvm
*gn
= dev
->mp
;
362 struct nvm_block
*blk
;
364 if (unlikely(ppa
->g
.ch
> dev
->nr_chnls
||
365 ppa
->g
.lun
> dev
->luns_per_chnl
||
366 ppa
->g
.blk
> dev
->blks_per_lun
)) {
368 pr_err("gennvm: ppa broken (ch: %u > %u lun: %u > %u blk: %u > %u",
369 ppa
->g
.ch
, dev
->nr_chnls
,
370 ppa
->g
.lun
, dev
->luns_per_chnl
,
371 ppa
->g
.blk
, dev
->blks_per_lun
);
375 lun
= &gn
->luns
[ppa
->g
.lun
* ppa
->g
.ch
];
376 blk
= &lun
->vlun
.blocks
[ppa
->g
.blk
];
378 /* will be moved to bb list on put_blk from target */
382 /* mark block bad. It is expected the target recover from the error. */
383 static void gennvm_mark_blk_bad(struct nvm_dev
*dev
, struct nvm_rq
*rqd
)
387 if (!dev
->ops
->set_bb_tbl
)
390 if (dev
->ops
->set_bb_tbl(dev
, rqd
, 1))
393 gennvm_addr_to_generic_mode(dev
, rqd
);
395 /* look up blocks and mark them as bad */
396 if (rqd
->nr_pages
> 1)
397 for (i
= 0; i
< rqd
->nr_pages
; i
++)
398 gennvm_blk_set_type(dev
, &rqd
->ppa_list
[i
], 2);
400 gennvm_blk_set_type(dev
, &rqd
->ppa_addr
, 2);
403 static int gennvm_end_io(struct nvm_rq
*rqd
, int error
)
405 struct nvm_tgt_instance
*ins
= rqd
->ins
;
409 case NVM_RSP_SUCCESS
:
411 case NVM_RSP_ERR_EMPTYPAGE
:
413 case NVM_RSP_ERR_FAILWRITE
:
414 gennvm_mark_blk_bad(rqd
->dev
, rqd
);
419 ret
+= ins
->tt
->end_io(rqd
, error
);
424 static int gennvm_erase_blk(struct nvm_dev
*dev
, struct nvm_block
*blk
,
427 int plane_cnt
= 0, pl_idx
, ret
;
428 struct ppa_addr addr
;
431 if (!dev
->ops
->erase_block
)
434 addr
= block_to_ppa(dev
, blk
);
436 if (dev
->plane_mode
== NVM_PLANE_SINGLE
) {
440 plane_cnt
= (1 << dev
->plane_mode
);
441 rqd
.nr_pages
= plane_cnt
;
443 rqd
.ppa_list
= nvm_dev_dma_alloc(dev
, GFP_KERNEL
,
446 pr_err("gennvm: failed to allocate dma memory\n");
450 for (pl_idx
= 0; pl_idx
< plane_cnt
; pl_idx
++) {
452 rqd
.ppa_list
[pl_idx
] = addr
;
456 gennvm_generic_to_addr_mode(dev
, &rqd
);
458 ret
= dev
->ops
->erase_block(dev
, &rqd
);
461 nvm_dev_dma_free(dev
, rqd
.ppa_list
, rqd
.dma_ppa_list
);
466 static struct nvm_lun
*gennvm_get_lun(struct nvm_dev
*dev
, int lunid
)
468 struct gen_nvm
*gn
= dev
->mp
;
470 return &gn
->luns
[lunid
].vlun
;
473 static void gennvm_lun_info_print(struct nvm_dev
*dev
)
475 struct gen_nvm
*gn
= dev
->mp
;
480 gennvm_for_each_lun(gn
, lun
, i
) {
481 spin_lock(&lun
->vlun
.lock
);
483 pr_info("%s: lun%8u\t%u\t%u\t%u\n",
485 lun
->vlun
.nr_free_blocks
,
486 lun
->vlun
.nr_inuse_blocks
,
487 lun
->vlun
.nr_bad_blocks
);
489 spin_unlock(&lun
->vlun
.lock
);
493 static struct nvmm_type gennvm
= {
495 .version
= {0, 1, 0},
497 .register_mgr
= gennvm_register
,
498 .unregister_mgr
= gennvm_unregister
,
500 .get_blk
= gennvm_get_blk
,
501 .put_blk
= gennvm_put_blk
,
503 .submit_io
= gennvm_submit_io
,
504 .end_io
= gennvm_end_io
,
505 .erase_blk
= gennvm_erase_blk
,
507 .get_lun
= gennvm_get_lun
,
508 .lun_info_print
= gennvm_lun_info_print
,
511 static int __init
gennvm_module_init(void)
513 return nvm_register_mgr(&gennvm
);
516 static void gennvm_module_exit(void)
518 nvm_unregister_mgr(&gennvm
);
521 module_init(gennvm_module_init
);
522 module_exit(gennvm_module_exit
);
523 MODULE_LICENSE("GPL v2");
524 MODULE_DESCRIPTION("Generic media manager for Open-Channel SSDs");