2 * Copyright (C) 2015 IT University of Copenhagen
3 * Initial release: Matias Bjorling <m@bjorling.me>
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.
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.
14 * Implementation of a Round-robin page-based Hybrid FTL for Open-channel SSDs.
19 static struct kmem_cache *rrpc_gcb_cache, *rrpc_rq_cache;
20 static DECLARE_RWSEM(rrpc_lock);
22 static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio,
23 struct nvm_rq *rqd, unsigned long flags);
25 #define rrpc_for_each_lun(rrpc, rlun, i) \
26 for ((i) = 0, rlun = &(rrpc)->luns[0]; \
27 (i) < (rrpc)->nr_luns; (i)++, rlun = &(rrpc)->luns[(i)])
29 static void rrpc_page_invalidate(struct rrpc *rrpc, struct rrpc_addr *a)
31 struct rrpc_block *rblk = a->rblk;
32 unsigned int pg_offset;
34 lockdep_assert_held(&rrpc->rev_lock);
36 if (a->addr == ADDR_EMPTY || !rblk)
39 spin_lock(&rblk->lock);
41 div_u64_rem(a->addr, rrpc->dev->pgs_per_blk, &pg_offset);
42 WARN_ON(test_and_set_bit(pg_offset, rblk->invalid_pages));
43 rblk->nr_invalid_pages++;
45 spin_unlock(&rblk->lock);
47 rrpc->rev_trans_map[a->addr - rrpc->poffset].addr = ADDR_EMPTY;
50 static void rrpc_invalidate_range(struct rrpc *rrpc, sector_t slba,
55 spin_lock(&rrpc->rev_lock);
56 for (i = slba; i < slba + len; i++) {
57 struct rrpc_addr *gp = &rrpc->trans_map[i];
59 rrpc_page_invalidate(rrpc, gp);
62 spin_unlock(&rrpc->rev_lock);
65 static struct nvm_rq *rrpc_inflight_laddr_acquire(struct rrpc *rrpc,
66 sector_t laddr, unsigned int pages)
69 struct rrpc_inflight_rq *inf;
71 rqd = mempool_alloc(rrpc->rq_pool, GFP_ATOMIC);
73 return ERR_PTR(-ENOMEM);
75 inf = rrpc_get_inflight_rq(rqd);
76 if (rrpc_lock_laddr(rrpc, laddr, pages, inf)) {
77 mempool_free(rqd, rrpc->rq_pool);
84 static void rrpc_inflight_laddr_release(struct rrpc *rrpc, struct nvm_rq *rqd)
86 struct rrpc_inflight_rq *inf = rrpc_get_inflight_rq(rqd);
88 rrpc_unlock_laddr(rrpc, inf);
90 mempool_free(rqd, rrpc->rq_pool);
93 static void rrpc_discard(struct rrpc *rrpc, struct bio *bio)
95 sector_t slba = bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
96 sector_t len = bio->bi_iter.bi_size / RRPC_EXPOSED_PAGE_SIZE;
100 rqd = rrpc_inflight_laddr_acquire(rrpc, slba, len);
105 pr_err("rrpc: unable to acquire inflight IO\n");
110 rrpc_invalidate_range(rrpc, slba, len);
111 rrpc_inflight_laddr_release(rrpc, rqd);
114 static int block_is_full(struct rrpc *rrpc, struct rrpc_block *rblk)
116 return (rblk->next_page == rrpc->dev->pgs_per_blk);
119 static u64 block_to_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
121 struct nvm_block *blk = rblk->parent;
123 return blk->id * rrpc->dev->pgs_per_blk;
126 static struct ppa_addr linear_to_generic_addr(struct nvm_dev *dev,
130 int secs, pgs, blks, luns;
131 sector_t ppa = r.ppa;
135 div_u64_rem(ppa, dev->sec_per_pg, &secs);
138 sector_div(ppa, dev->sec_per_pg);
139 div_u64_rem(ppa, dev->sec_per_blk, &pgs);
142 sector_div(ppa, dev->pgs_per_blk);
143 div_u64_rem(ppa, dev->blks_per_lun, &blks);
146 sector_div(ppa, dev->blks_per_lun);
147 div_u64_rem(ppa, dev->luns_per_chnl, &luns);
150 sector_div(ppa, dev->luns_per_chnl);
156 static struct ppa_addr rrpc_ppa_to_gaddr(struct nvm_dev *dev, u64 addr)
158 struct ppa_addr paddr;
161 return linear_to_generic_addr(dev, paddr);
164 /* requires lun->lock taken */
165 static void rrpc_set_lun_cur(struct rrpc_lun *rlun, struct rrpc_block *rblk)
167 struct rrpc *rrpc = rlun->rrpc;
172 spin_lock(&rlun->cur->lock);
173 WARN_ON(!block_is_full(rrpc, rlun->cur));
174 spin_unlock(&rlun->cur->lock);
179 static struct rrpc_block *rrpc_get_blk(struct rrpc *rrpc, struct rrpc_lun *rlun,
182 struct nvm_block *blk;
183 struct rrpc_block *rblk;
185 blk = nvm_get_blk(rrpc->dev, rlun->parent, 0);
189 rblk = &rlun->blocks[blk->id];
192 bitmap_zero(rblk->invalid_pages, rrpc->dev->pgs_per_blk);
194 rblk->nr_invalid_pages = 0;
195 atomic_set(&rblk->data_cmnt_size, 0);
200 static void rrpc_put_blk(struct rrpc *rrpc, struct rrpc_block *rblk)
202 nvm_put_blk(rrpc->dev, rblk->parent);
205 static struct rrpc_lun *get_next_lun(struct rrpc *rrpc)
207 int next = atomic_inc_return(&rrpc->next_lun);
209 return &rrpc->luns[next % rrpc->nr_luns];
212 static void rrpc_gc_kick(struct rrpc *rrpc)
214 struct rrpc_lun *rlun;
217 for (i = 0; i < rrpc->nr_luns; i++) {
218 rlun = &rrpc->luns[i];
219 queue_work(rrpc->krqd_wq, &rlun->ws_gc);
224 * timed GC every interval.
226 static void rrpc_gc_timer(unsigned long data)
228 struct rrpc *rrpc = (struct rrpc *)data;
231 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
234 static void rrpc_end_sync_bio(struct bio *bio)
236 struct completion *waiting = bio->bi_private;
239 pr_err("nvm: gc request failed (%u).\n", bio->bi_error);
245 * rrpc_move_valid_pages -- migrate live data off the block
246 * @rrpc: the 'rrpc' structure
247 * @block: the block from which to migrate live pages
250 * GC algorithms may call this function to migrate remaining live
251 * pages off the block prior to erasing it. This function blocks
252 * further execution until the operation is complete.
254 static int rrpc_move_valid_pages(struct rrpc *rrpc, struct rrpc_block *rblk)
256 struct request_queue *q = rrpc->dev->q;
257 struct rrpc_rev_addr *rev;
262 int nr_pgs_per_blk = rrpc->dev->pgs_per_blk;
264 DECLARE_COMPLETION_ONSTACK(wait);
266 if (bitmap_full(rblk->invalid_pages, nr_pgs_per_blk))
269 bio = bio_alloc(GFP_NOIO, 1);
271 pr_err("nvm: could not alloc bio to gc\n");
275 page = mempool_alloc(rrpc->page_pool, GFP_NOIO);
277 while ((slot = find_first_zero_bit(rblk->invalid_pages,
278 nr_pgs_per_blk)) < nr_pgs_per_blk) {
281 phys_addr = (rblk->parent->id * nr_pgs_per_blk) + slot;
284 spin_lock(&rrpc->rev_lock);
285 /* Get logical address from physical to logical table */
286 rev = &rrpc->rev_trans_map[phys_addr - rrpc->poffset];
287 /* already updated by previous regular write */
288 if (rev->addr == ADDR_EMPTY) {
289 spin_unlock(&rrpc->rev_lock);
293 rqd = rrpc_inflight_laddr_acquire(rrpc, rev->addr, 1);
294 if (IS_ERR_OR_NULL(rqd)) {
295 spin_unlock(&rrpc->rev_lock);
300 spin_unlock(&rrpc->rev_lock);
302 /* Perform read to do GC */
303 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
305 bio->bi_private = &wait;
306 bio->bi_end_io = rrpc_end_sync_bio;
308 /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
309 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
311 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
312 pr_err("rrpc: gc read failed.\n");
313 rrpc_inflight_laddr_release(rrpc, rqd);
316 wait_for_completion_io(&wait);
319 reinit_completion(&wait);
321 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
323 bio->bi_private = &wait;
324 bio->bi_end_io = rrpc_end_sync_bio;
326 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
328 /* turn the command around and write the data back to a new
331 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
332 pr_err("rrpc: gc write failed.\n");
333 rrpc_inflight_laddr_release(rrpc, rqd);
336 wait_for_completion_io(&wait);
338 rrpc_inflight_laddr_release(rrpc, rqd);
344 mempool_free(page, rrpc->page_pool);
347 if (!bitmap_full(rblk->invalid_pages, nr_pgs_per_blk)) {
348 pr_err("nvm: failed to garbage collect block\n");
355 static void rrpc_block_gc(struct work_struct *work)
357 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
359 struct rrpc *rrpc = gcb->rrpc;
360 struct rrpc_block *rblk = gcb->rblk;
361 struct nvm_dev *dev = rrpc->dev;
363 pr_debug("nvm: block '%lu' being reclaimed\n", rblk->parent->id);
365 if (rrpc_move_valid_pages(rrpc, rblk))
368 nvm_erase_blk(dev, rblk->parent);
369 rrpc_put_blk(rrpc, rblk);
371 mempool_free(gcb, rrpc->gcb_pool);
374 /* the block with highest number of invalid pages, will be in the beginning
377 static struct rrpc_block *rblock_max_invalid(struct rrpc_block *ra,
378 struct rrpc_block *rb)
380 if (ra->nr_invalid_pages == rb->nr_invalid_pages)
383 return (ra->nr_invalid_pages < rb->nr_invalid_pages) ? rb : ra;
386 /* linearly find the block with highest number of invalid pages
389 static struct rrpc_block *block_prio_find_max(struct rrpc_lun *rlun)
391 struct list_head *prio_list = &rlun->prio_list;
392 struct rrpc_block *rblock, *max;
394 BUG_ON(list_empty(prio_list));
396 max = list_first_entry(prio_list, struct rrpc_block, prio);
397 list_for_each_entry(rblock, prio_list, prio)
398 max = rblock_max_invalid(max, rblock);
403 static void rrpc_lun_gc(struct work_struct *work)
405 struct rrpc_lun *rlun = container_of(work, struct rrpc_lun, ws_gc);
406 struct rrpc *rrpc = rlun->rrpc;
407 struct nvm_lun *lun = rlun->parent;
408 struct rrpc_block_gc *gcb;
409 unsigned int nr_blocks_need;
411 nr_blocks_need = rrpc->dev->blks_per_lun / GC_LIMIT_INVERSE;
413 if (nr_blocks_need < rrpc->nr_luns)
414 nr_blocks_need = rrpc->nr_luns;
416 spin_lock(&lun->lock);
417 while (nr_blocks_need > lun->nr_free_blocks &&
418 !list_empty(&rlun->prio_list)) {
419 struct rrpc_block *rblock = block_prio_find_max(rlun);
420 struct nvm_block *block = rblock->parent;
422 if (!rblock->nr_invalid_pages)
425 list_del_init(&rblock->prio);
427 BUG_ON(!block_is_full(rrpc, rblock));
429 pr_debug("rrpc: selected block '%lu' for GC\n", block->id);
431 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
437 INIT_WORK(&gcb->ws_gc, rrpc_block_gc);
439 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
443 spin_unlock(&lun->lock);
445 /* TODO: Hint that request queue can be started again */
448 static void rrpc_gc_queue(struct work_struct *work)
450 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
452 struct rrpc *rrpc = gcb->rrpc;
453 struct rrpc_block *rblk = gcb->rblk;
454 struct nvm_lun *lun = rblk->parent->lun;
455 struct rrpc_lun *rlun = &rrpc->luns[lun->id - rrpc->lun_offset];
457 spin_lock(&rlun->lock);
458 list_add_tail(&rblk->prio, &rlun->prio_list);
459 spin_unlock(&rlun->lock);
461 mempool_free(gcb, rrpc->gcb_pool);
462 pr_debug("nvm: block '%lu' is full, allow GC (sched)\n",
466 static const struct block_device_operations rrpc_fops = {
467 .owner = THIS_MODULE,
470 static struct rrpc_lun *rrpc_get_lun_rr(struct rrpc *rrpc, int is_gc)
473 struct rrpc_lun *rlun, *max_free;
476 return get_next_lun(rrpc);
478 /* during GC, we don't care about RR, instead we want to make
479 * sure that we maintain evenness between the block luns.
481 max_free = &rrpc->luns[0];
482 /* prevent GC-ing lun from devouring pages of a lun with
483 * little free blocks. We don't take the lock as we only need an
486 rrpc_for_each_lun(rrpc, rlun, i) {
487 if (rlun->parent->nr_free_blocks >
488 max_free->parent->nr_free_blocks)
495 static struct rrpc_addr *rrpc_update_map(struct rrpc *rrpc, sector_t laddr,
496 struct rrpc_block *rblk, u64 paddr)
498 struct rrpc_addr *gp;
499 struct rrpc_rev_addr *rev;
501 BUG_ON(laddr >= rrpc->nr_pages);
503 gp = &rrpc->trans_map[laddr];
504 spin_lock(&rrpc->rev_lock);
506 rrpc_page_invalidate(rrpc, gp);
511 rev = &rrpc->rev_trans_map[gp->addr - rrpc->poffset];
513 spin_unlock(&rrpc->rev_lock);
518 static u64 rrpc_alloc_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
520 u64 addr = ADDR_EMPTY;
522 spin_lock(&rblk->lock);
523 if (block_is_full(rrpc, rblk))
526 addr = block_to_addr(rrpc, rblk) + rblk->next_page;
530 spin_unlock(&rblk->lock);
534 /* Simple round-robin Logical to physical address translation.
536 * Retrieve the mapping using the active append point. Then update the ap for
537 * the next write to the disk.
539 * Returns rrpc_addr with the physical address and block. Remember to return to
540 * rrpc->addr_cache when request is finished.
542 static struct rrpc_addr *rrpc_map_page(struct rrpc *rrpc, sector_t laddr,
545 struct rrpc_lun *rlun;
546 struct rrpc_block *rblk;
550 rlun = rrpc_get_lun_rr(rrpc, is_gc);
553 if (!is_gc && lun->nr_free_blocks < rrpc->nr_luns * 4)
556 spin_lock(&rlun->lock);
560 paddr = rrpc_alloc_addr(rrpc, rblk);
562 if (paddr == ADDR_EMPTY) {
563 rblk = rrpc_get_blk(rrpc, rlun, 0);
565 rrpc_set_lun_cur(rlun, rblk);
570 /* retry from emergency gc block */
571 paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur);
572 if (paddr == ADDR_EMPTY) {
573 rblk = rrpc_get_blk(rrpc, rlun, 1);
575 pr_err("rrpc: no more blocks");
580 paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur);
586 spin_unlock(&rlun->lock);
587 return rrpc_update_map(rrpc, laddr, rblk, paddr);
589 spin_unlock(&rlun->lock);
593 static void rrpc_run_gc(struct rrpc *rrpc, struct rrpc_block *rblk)
595 struct rrpc_block_gc *gcb;
597 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
599 pr_err("rrpc: unable to queue block for gc.");
606 INIT_WORK(&gcb->ws_gc, rrpc_gc_queue);
607 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
610 static void rrpc_end_io_write(struct rrpc *rrpc, struct rrpc_rq *rrqd,
611 sector_t laddr, uint8_t npages)
614 struct rrpc_block *rblk;
618 for (i = 0; i < npages; i++) {
619 p = &rrpc->trans_map[laddr + i];
621 lun = rblk->parent->lun;
623 cmnt_size = atomic_inc_return(&rblk->data_cmnt_size);
624 if (unlikely(cmnt_size == rrpc->dev->pgs_per_blk))
625 rrpc_run_gc(rrpc, rblk);
629 static int rrpc_end_io(struct nvm_rq *rqd, int error)
631 struct rrpc *rrpc = container_of(rqd->ins, struct rrpc, instance);
632 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
633 uint8_t npages = rqd->nr_pages;
634 sector_t laddr = rrpc_get_laddr(rqd->bio) - npages;
636 if (bio_data_dir(rqd->bio) == WRITE)
637 rrpc_end_io_write(rrpc, rrqd, laddr, npages);
639 if (rrqd->flags & NVM_IOTYPE_GC)
642 rrpc_unlock_rq(rrpc, rqd);
646 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
648 nvm_dev_dma_free(rrpc->dev, rqd->metadata, rqd->dma_metadata);
650 mempool_free(rqd, rrpc->rq_pool);
655 static int rrpc_read_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
656 struct nvm_rq *rqd, unsigned long flags, int npages)
658 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
659 struct rrpc_addr *gp;
660 sector_t laddr = rrpc_get_laddr(bio);
661 int is_gc = flags & NVM_IOTYPE_GC;
664 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
665 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
666 return NVM_IO_REQUEUE;
669 for (i = 0; i < npages; i++) {
670 /* We assume that mapping occurs at 4KB granularity */
671 BUG_ON(!(laddr + i >= 0 && laddr + i < rrpc->nr_pages));
672 gp = &rrpc->trans_map[laddr + i];
675 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
679 rrpc_unlock_laddr(rrpc, r);
680 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
686 rqd->opcode = NVM_OP_HBREAD;
691 static int rrpc_read_rq(struct rrpc *rrpc, struct bio *bio, struct nvm_rq *rqd,
694 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
695 int is_gc = flags & NVM_IOTYPE_GC;
696 sector_t laddr = rrpc_get_laddr(bio);
697 struct rrpc_addr *gp;
699 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
700 return NVM_IO_REQUEUE;
702 BUG_ON(!(laddr >= 0 && laddr < rrpc->nr_pages));
703 gp = &rrpc->trans_map[laddr];
706 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, gp->addr);
709 rrpc_unlock_rq(rrpc, rqd);
713 rqd->opcode = NVM_OP_HBREAD;
719 static int rrpc_write_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
720 struct nvm_rq *rqd, unsigned long flags, int npages)
722 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
724 sector_t laddr = rrpc_get_laddr(bio);
725 int is_gc = flags & NVM_IOTYPE_GC;
728 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
729 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
730 return NVM_IO_REQUEUE;
733 for (i = 0; i < npages; i++) {
734 /* We assume that mapping occurs at 4KB granularity */
735 p = rrpc_map_page(rrpc, laddr + i, is_gc);
738 rrpc_unlock_laddr(rrpc, r);
739 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
742 return NVM_IO_REQUEUE;
745 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
749 rqd->opcode = NVM_OP_HBWRITE;
754 static int rrpc_write_rq(struct rrpc *rrpc, struct bio *bio,
755 struct nvm_rq *rqd, unsigned long flags)
757 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
759 int is_gc = flags & NVM_IOTYPE_GC;
760 sector_t laddr = rrpc_get_laddr(bio);
762 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
763 return NVM_IO_REQUEUE;
765 p = rrpc_map_page(rrpc, laddr, is_gc);
768 rrpc_unlock_rq(rrpc, rqd);
770 return NVM_IO_REQUEUE;
773 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, p->addr);
774 rqd->opcode = NVM_OP_HBWRITE;
780 static int rrpc_setup_rq(struct rrpc *rrpc, struct bio *bio,
781 struct nvm_rq *rqd, unsigned long flags, uint8_t npages)
784 rqd->ppa_list = nvm_dev_dma_alloc(rrpc->dev, GFP_KERNEL,
786 if (!rqd->ppa_list) {
787 pr_err("rrpc: not able to allocate ppa list\n");
791 if (bio_rw(bio) == WRITE)
792 return rrpc_write_ppalist_rq(rrpc, bio, rqd, flags,
795 return rrpc_read_ppalist_rq(rrpc, bio, rqd, flags, npages);
798 if (bio_rw(bio) == WRITE)
799 return rrpc_write_rq(rrpc, bio, rqd, flags);
801 return rrpc_read_rq(rrpc, bio, rqd, flags);
804 static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio,
805 struct nvm_rq *rqd, unsigned long flags)
808 struct rrpc_rq *rrq = nvm_rq_to_pdu(rqd);
809 uint8_t nr_pages = rrpc_get_pages(bio);
810 int bio_size = bio_sectors(bio) << 9;
812 if (bio_size < rrpc->dev->sec_size)
814 else if (bio_size > rrpc->dev->max_rq_size)
817 err = rrpc_setup_rq(rrpc, bio, rqd, flags, nr_pages);
823 rqd->ins = &rrpc->instance;
824 rqd->nr_pages = nr_pages;
827 err = nvm_submit_io(rrpc->dev, rqd);
829 pr_err("rrpc: I/O submission failed: %d\n", err);
836 static blk_qc_t rrpc_make_rq(struct request_queue *q, struct bio *bio)
838 struct rrpc *rrpc = q->queuedata;
842 if (bio->bi_rw & REQ_DISCARD) {
843 rrpc_discard(rrpc, bio);
844 return BLK_QC_T_NONE;
847 rqd = mempool_alloc(rrpc->rq_pool, GFP_KERNEL);
849 pr_err_ratelimited("rrpc: not able to queue bio.");
851 return BLK_QC_T_NONE;
853 memset(rqd, 0, sizeof(struct nvm_rq));
855 err = rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_NONE);
858 return BLK_QC_T_NONE;
866 spin_lock(&rrpc->bio_lock);
867 bio_list_add(&rrpc->requeue_bios, bio);
868 spin_unlock(&rrpc->bio_lock);
869 queue_work(rrpc->kgc_wq, &rrpc->ws_requeue);
873 mempool_free(rqd, rrpc->rq_pool);
874 return BLK_QC_T_NONE;
877 static void rrpc_requeue(struct work_struct *work)
879 struct rrpc *rrpc = container_of(work, struct rrpc, ws_requeue);
880 struct bio_list bios;
883 bio_list_init(&bios);
885 spin_lock(&rrpc->bio_lock);
886 bio_list_merge(&bios, &rrpc->requeue_bios);
887 bio_list_init(&rrpc->requeue_bios);
888 spin_unlock(&rrpc->bio_lock);
890 while ((bio = bio_list_pop(&bios)))
891 rrpc_make_rq(rrpc->disk->queue, bio);
894 static void rrpc_gc_free(struct rrpc *rrpc)
896 struct rrpc_lun *rlun;
900 destroy_workqueue(rrpc->krqd_wq);
903 destroy_workqueue(rrpc->kgc_wq);
908 for (i = 0; i < rrpc->nr_luns; i++) {
909 rlun = &rrpc->luns[i];
917 static int rrpc_gc_init(struct rrpc *rrpc)
919 rrpc->krqd_wq = alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM|WQ_UNBOUND,
924 rrpc->kgc_wq = alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM, 1);
928 setup_timer(&rrpc->gc_timer, rrpc_gc_timer, (unsigned long)rrpc);
933 static void rrpc_map_free(struct rrpc *rrpc)
935 vfree(rrpc->rev_trans_map);
936 vfree(rrpc->trans_map);
939 static int rrpc_l2p_update(u64 slba, u32 nlb, __le64 *entries, void *private)
941 struct rrpc *rrpc = (struct rrpc *)private;
942 struct nvm_dev *dev = rrpc->dev;
943 struct rrpc_addr *addr = rrpc->trans_map + slba;
944 struct rrpc_rev_addr *raddr = rrpc->rev_trans_map;
945 sector_t max_pages = dev->total_pages * (dev->sec_size >> 9);
946 u64 elba = slba + nlb;
949 if (unlikely(elba > dev->total_pages)) {
950 pr_err("nvm: L2P data from device is out of bounds!\n");
954 for (i = 0; i < nlb; i++) {
955 u64 pba = le64_to_cpu(entries[i]);
956 /* LNVM treats address-spaces as silos, LBA and PBA are
957 * equally large and zero-indexed.
959 if (unlikely(pba >= max_pages && pba != U64_MAX)) {
960 pr_err("nvm: L2P data entry is out of bounds!\n");
964 /* Address zero is a special one. The first page on a disk is
965 * protected. As it often holds internal device boot
972 raddr[pba].addr = slba + i;
978 static int rrpc_map_init(struct rrpc *rrpc)
980 struct nvm_dev *dev = rrpc->dev;
984 rrpc->trans_map = vzalloc(sizeof(struct rrpc_addr) * rrpc->nr_pages);
985 if (!rrpc->trans_map)
988 rrpc->rev_trans_map = vmalloc(sizeof(struct rrpc_rev_addr)
990 if (!rrpc->rev_trans_map)
993 for (i = 0; i < rrpc->nr_pages; i++) {
994 struct rrpc_addr *p = &rrpc->trans_map[i];
995 struct rrpc_rev_addr *r = &rrpc->rev_trans_map[i];
997 p->addr = ADDR_EMPTY;
998 r->addr = ADDR_EMPTY;
1001 if (!dev->ops->get_l2p_tbl)
1004 /* Bring up the mapping table from device */
1005 ret = dev->ops->get_l2p_tbl(dev->q, 0, dev->total_pages,
1006 rrpc_l2p_update, rrpc);
1008 pr_err("nvm: rrpc: could not read L2P table.\n");
1016 /* Minimum pages needed within a lun */
1017 #define PAGE_POOL_SIZE 16
1018 #define ADDR_POOL_SIZE 64
1020 static int rrpc_core_init(struct rrpc *rrpc)
1022 down_write(&rrpc_lock);
1023 if (!rrpc_gcb_cache) {
1024 rrpc_gcb_cache = kmem_cache_create("rrpc_gcb",
1025 sizeof(struct rrpc_block_gc), 0, 0, NULL);
1026 if (!rrpc_gcb_cache) {
1027 up_write(&rrpc_lock);
1031 rrpc_rq_cache = kmem_cache_create("rrpc_rq",
1032 sizeof(struct nvm_rq) + sizeof(struct rrpc_rq),
1034 if (!rrpc_rq_cache) {
1035 kmem_cache_destroy(rrpc_gcb_cache);
1036 up_write(&rrpc_lock);
1040 up_write(&rrpc_lock);
1042 rrpc->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0);
1043 if (!rrpc->page_pool)
1046 rrpc->gcb_pool = mempool_create_slab_pool(rrpc->dev->nr_luns,
1048 if (!rrpc->gcb_pool)
1051 rrpc->rq_pool = mempool_create_slab_pool(64, rrpc_rq_cache);
1055 spin_lock_init(&rrpc->inflights.lock);
1056 INIT_LIST_HEAD(&rrpc->inflights.reqs);
1061 static void rrpc_core_free(struct rrpc *rrpc)
1063 mempool_destroy(rrpc->page_pool);
1064 mempool_destroy(rrpc->gcb_pool);
1065 mempool_destroy(rrpc->rq_pool);
1068 static void rrpc_luns_free(struct rrpc *rrpc)
1073 static int rrpc_luns_init(struct rrpc *rrpc, int lun_begin, int lun_end)
1075 struct nvm_dev *dev = rrpc->dev;
1076 struct rrpc_lun *rlun;
1079 spin_lock_init(&rrpc->rev_lock);
1081 rrpc->luns = kcalloc(rrpc->nr_luns, sizeof(struct rrpc_lun),
1087 for (i = 0; i < rrpc->nr_luns; i++) {
1088 struct nvm_lun *lun = dev->mt->get_lun(dev, lun_begin + i);
1090 if (dev->pgs_per_blk >
1091 MAX_INVALID_PAGES_STORAGE * BITS_PER_LONG) {
1092 pr_err("rrpc: number of pages per block too high.");
1096 rlun = &rrpc->luns[i];
1099 INIT_LIST_HEAD(&rlun->prio_list);
1100 INIT_WORK(&rlun->ws_gc, rrpc_lun_gc);
1101 spin_lock_init(&rlun->lock);
1103 rrpc->total_blocks += dev->blks_per_lun;
1104 rrpc->nr_pages += dev->sec_per_lun;
1106 rlun->blocks = vzalloc(sizeof(struct rrpc_block) *
1107 rrpc->dev->blks_per_lun);
1111 for (j = 0; j < rrpc->dev->blks_per_lun; j++) {
1112 struct rrpc_block *rblk = &rlun->blocks[j];
1113 struct nvm_block *blk = &lun->blocks[j];
1116 INIT_LIST_HEAD(&rblk->prio);
1117 spin_lock_init(&rblk->lock);
1126 static void rrpc_free(struct rrpc *rrpc)
1129 rrpc_map_free(rrpc);
1130 rrpc_core_free(rrpc);
1131 rrpc_luns_free(rrpc);
1136 static void rrpc_exit(void *private)
1138 struct rrpc *rrpc = private;
1140 del_timer(&rrpc->gc_timer);
1142 flush_workqueue(rrpc->krqd_wq);
1143 flush_workqueue(rrpc->kgc_wq);
1148 static sector_t rrpc_capacity(void *private)
1150 struct rrpc *rrpc = private;
1151 struct nvm_dev *dev = rrpc->dev;
1152 sector_t reserved, provisioned;
1154 /* cur, gc, and two emergency blocks for each lun */
1155 reserved = rrpc->nr_luns * dev->max_pages_per_blk * 4;
1156 provisioned = rrpc->nr_pages - reserved;
1158 if (reserved > rrpc->nr_pages) {
1159 pr_err("rrpc: not enough space available to expose storage.\n");
1163 sector_div(provisioned, 10);
1164 return provisioned * 9 * NR_PHY_IN_LOG;
1168 * Looks up the logical address from reverse trans map and check if its valid by
1169 * comparing the logical to physical address with the physical address.
1170 * Returns 0 on free, otherwise 1 if in use
1172 static void rrpc_block_map_update(struct rrpc *rrpc, struct rrpc_block *rblk)
1174 struct nvm_dev *dev = rrpc->dev;
1176 struct rrpc_addr *laddr;
1179 for (offset = 0; offset < dev->pgs_per_blk; offset++) {
1180 paddr = block_to_addr(rrpc, rblk) + offset;
1182 pladdr = rrpc->rev_trans_map[paddr].addr;
1183 if (pladdr == ADDR_EMPTY)
1186 laddr = &rrpc->trans_map[pladdr];
1188 if (paddr == laddr->addr) {
1191 set_bit(offset, rblk->invalid_pages);
1192 rblk->nr_invalid_pages++;
1197 static int rrpc_blocks_init(struct rrpc *rrpc)
1199 struct rrpc_lun *rlun;
1200 struct rrpc_block *rblk;
1201 int lun_iter, blk_iter;
1203 for (lun_iter = 0; lun_iter < rrpc->nr_luns; lun_iter++) {
1204 rlun = &rrpc->luns[lun_iter];
1206 for (blk_iter = 0; blk_iter < rrpc->dev->blks_per_lun;
1208 rblk = &rlun->blocks[blk_iter];
1209 rrpc_block_map_update(rrpc, rblk);
1216 static int rrpc_luns_configure(struct rrpc *rrpc)
1218 struct rrpc_lun *rlun;
1219 struct rrpc_block *rblk;
1222 for (i = 0; i < rrpc->nr_luns; i++) {
1223 rlun = &rrpc->luns[i];
1225 rblk = rrpc_get_blk(rrpc, rlun, 0);
1229 rrpc_set_lun_cur(rlun, rblk);
1231 /* Emergency gc block */
1232 rblk = rrpc_get_blk(rrpc, rlun, 1);
1235 rlun->gc_cur = rblk;
1241 static struct nvm_tgt_type tt_rrpc;
1243 static void *rrpc_init(struct nvm_dev *dev, struct gendisk *tdisk,
1244 int lun_begin, int lun_end)
1246 struct request_queue *bqueue = dev->q;
1247 struct request_queue *tqueue = tdisk->queue;
1251 if (!(dev->identity.dom & NVM_RSP_L2P)) {
1252 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1254 return ERR_PTR(-EINVAL);
1257 rrpc = kzalloc(sizeof(struct rrpc), GFP_KERNEL);
1259 return ERR_PTR(-ENOMEM);
1261 rrpc->instance.tt = &tt_rrpc;
1265 bio_list_init(&rrpc->requeue_bios);
1266 spin_lock_init(&rrpc->bio_lock);
1267 INIT_WORK(&rrpc->ws_requeue, rrpc_requeue);
1269 rrpc->nr_luns = lun_end - lun_begin + 1;
1271 /* simple round-robin strategy */
1272 atomic_set(&rrpc->next_lun, -1);
1274 ret = rrpc_luns_init(rrpc, lun_begin, lun_end);
1276 pr_err("nvm: rrpc: could not initialize luns\n");
1280 rrpc->poffset = dev->sec_per_lun * lun_begin;
1281 rrpc->lun_offset = lun_begin;
1283 ret = rrpc_core_init(rrpc);
1285 pr_err("nvm: rrpc: could not initialize core\n");
1289 ret = rrpc_map_init(rrpc);
1291 pr_err("nvm: rrpc: could not initialize maps\n");
1295 ret = rrpc_blocks_init(rrpc);
1297 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1301 ret = rrpc_luns_configure(rrpc);
1303 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1307 ret = rrpc_gc_init(rrpc);
1309 pr_err("nvm: rrpc: could not initialize gc\n");
1313 /* inherit the size from the underlying device */
1314 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1315 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1317 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
1318 rrpc->nr_luns, (unsigned long long)rrpc->nr_pages);
1320 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
1325 return ERR_PTR(ret);
1328 /* round robin, page-based FTL, and cost-based GC */
1329 static struct nvm_tgt_type tt_rrpc = {
1331 .version = {1, 0, 0},
1333 .make_rq = rrpc_make_rq,
1334 .capacity = rrpc_capacity,
1335 .end_io = rrpc_end_io,
1341 static int __init rrpc_module_init(void)
1343 return nvm_register_target(&tt_rrpc);
1346 static void rrpc_module_exit(void)
1348 nvm_unregister_target(&tt_rrpc);
1351 module_init(rrpc_module_init);
1352 module_exit(rrpc_module_exit);
1353 MODULE_LICENSE("GPL v2");
1354 MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");