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
->sec_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
);
108 pr_err("rrpc: unable to acquire inflight IO\n");
113 rrpc_invalidate_range(rrpc
, slba
, len
);
114 rrpc_inflight_laddr_release(rrpc
, rqd
);
117 static int block_is_full(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
119 return (rblk
->next_page
== rrpc
->dev
->sec_per_blk
);
122 /* Calculate relative addr for the given block, considering instantiated LUNs */
123 static u64
block_to_rel_addr(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
125 struct nvm_block
*blk
= rblk
->parent
;
126 int lun_blk
= blk
->id
% (rrpc
->dev
->blks_per_lun
* rrpc
->nr_luns
);
128 return lun_blk
* rrpc
->dev
->sec_per_blk
;
131 /* Calculate global addr for the given block */
132 static u64
block_to_addr(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
134 struct nvm_block
*blk
= rblk
->parent
;
136 return blk
->id
* rrpc
->dev
->sec_per_blk
;
139 static struct ppa_addr
linear_to_generic_addr(struct nvm_dev
*dev
,
143 int secs
, pgs
, blks
, luns
;
144 sector_t ppa
= r
.ppa
;
148 div_u64_rem(ppa
, dev
->sec_per_pg
, &secs
);
151 sector_div(ppa
, dev
->sec_per_pg
);
152 div_u64_rem(ppa
, dev
->pgs_per_blk
, &pgs
);
155 sector_div(ppa
, dev
->pgs_per_blk
);
156 div_u64_rem(ppa
, dev
->blks_per_lun
, &blks
);
159 sector_div(ppa
, dev
->blks_per_lun
);
160 div_u64_rem(ppa
, dev
->luns_per_chnl
, &luns
);
163 sector_div(ppa
, dev
->luns_per_chnl
);
169 static struct ppa_addr
rrpc_ppa_to_gaddr(struct nvm_dev
*dev
, u64 addr
)
171 struct ppa_addr paddr
;
174 return linear_to_generic_addr(dev
, paddr
);
177 /* requires lun->lock taken */
178 static void rrpc_set_lun_cur(struct rrpc_lun
*rlun
, struct rrpc_block
*new_rblk
,
179 struct rrpc_block
**cur_rblk
)
181 struct rrpc
*rrpc
= rlun
->rrpc
;
184 spin_lock(&(*cur_rblk
)->lock
);
185 WARN_ON(!block_is_full(rrpc
, *cur_rblk
));
186 spin_unlock(&(*cur_rblk
)->lock
);
188 *cur_rblk
= new_rblk
;
191 static struct rrpc_block
*rrpc_get_blk(struct rrpc
*rrpc
, struct rrpc_lun
*rlun
,
194 struct nvm_block
*blk
;
195 struct rrpc_block
*rblk
;
197 blk
= nvm_get_blk(rrpc
->dev
, rlun
->parent
, flags
);
199 pr_err("nvm: rrpc: cannot get new block from media manager\n");
203 rblk
= rrpc_get_rblk(rlun
, blk
->id
);
205 bitmap_zero(rblk
->invalid_pages
, rrpc
->dev
->sec_per_blk
);
207 rblk
->nr_invalid_pages
= 0;
208 atomic_set(&rblk
->data_cmnt_size
, 0);
213 static void rrpc_put_blk(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
215 nvm_put_blk(rrpc
->dev
, rblk
->parent
);
218 static void rrpc_put_blks(struct rrpc
*rrpc
)
220 struct rrpc_lun
*rlun
;
223 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
224 rlun
= &rrpc
->luns
[i
];
226 rrpc_put_blk(rrpc
, rlun
->cur
);
228 rrpc_put_blk(rrpc
, rlun
->gc_cur
);
232 static struct rrpc_lun
*get_next_lun(struct rrpc
*rrpc
)
234 int next
= atomic_inc_return(&rrpc
->next_lun
);
236 return &rrpc
->luns
[next
% rrpc
->nr_luns
];
239 static void rrpc_gc_kick(struct rrpc
*rrpc
)
241 struct rrpc_lun
*rlun
;
244 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
245 rlun
= &rrpc
->luns
[i
];
246 queue_work(rrpc
->krqd_wq
, &rlun
->ws_gc
);
251 * timed GC every interval.
253 static void rrpc_gc_timer(unsigned long data
)
255 struct rrpc
*rrpc
= (struct rrpc
*)data
;
258 mod_timer(&rrpc
->gc_timer
, jiffies
+ msecs_to_jiffies(10));
261 static void rrpc_end_sync_bio(struct bio
*bio
)
263 struct completion
*waiting
= bio
->bi_private
;
266 pr_err("nvm: gc request failed (%u).\n", bio
->bi_error
);
272 * rrpc_move_valid_pages -- migrate live data off the block
273 * @rrpc: the 'rrpc' structure
274 * @block: the block from which to migrate live pages
277 * GC algorithms may call this function to migrate remaining live
278 * pages off the block prior to erasing it. This function blocks
279 * further execution until the operation is complete.
281 static int rrpc_move_valid_pages(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
283 struct request_queue
*q
= rrpc
->dev
->q
;
284 struct rrpc_rev_addr
*rev
;
289 int nr_sec_per_blk
= rrpc
->dev
->sec_per_blk
;
291 DECLARE_COMPLETION_ONSTACK(wait
);
293 if (bitmap_full(rblk
->invalid_pages
, nr_sec_per_blk
))
296 bio
= bio_alloc(GFP_NOIO
, 1);
298 pr_err("nvm: could not alloc bio to gc\n");
302 page
= mempool_alloc(rrpc
->page_pool
, GFP_NOIO
);
308 while ((slot
= find_first_zero_bit(rblk
->invalid_pages
,
309 nr_sec_per_blk
)) < nr_sec_per_blk
) {
312 phys_addr
= rblk
->parent
->id
* nr_sec_per_blk
+ slot
;
315 spin_lock(&rrpc
->rev_lock
);
316 /* Get logical address from physical to logical table */
317 rev
= &rrpc
->rev_trans_map
[phys_addr
- rrpc
->poffset
];
318 /* already updated by previous regular write */
319 if (rev
->addr
== ADDR_EMPTY
) {
320 spin_unlock(&rrpc
->rev_lock
);
324 rqd
= rrpc_inflight_laddr_acquire(rrpc
, rev
->addr
, 1);
325 if (IS_ERR_OR_NULL(rqd
)) {
326 spin_unlock(&rrpc
->rev_lock
);
331 spin_unlock(&rrpc
->rev_lock
);
333 /* Perform read to do GC */
334 bio
->bi_iter
.bi_sector
= rrpc_get_sector(rev
->addr
);
335 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
336 bio
->bi_private
= &wait
;
337 bio
->bi_end_io
= rrpc_end_sync_bio
;
339 /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
340 bio_add_pc_page(q
, bio
, page
, RRPC_EXPOSED_PAGE_SIZE
, 0);
342 if (rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_GC
)) {
343 pr_err("rrpc: gc read failed.\n");
344 rrpc_inflight_laddr_release(rrpc
, rqd
);
347 wait_for_completion_io(&wait
);
349 rrpc_inflight_laddr_release(rrpc
, rqd
);
354 reinit_completion(&wait
);
356 bio
->bi_iter
.bi_sector
= rrpc_get_sector(rev
->addr
);
357 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
358 bio
->bi_private
= &wait
;
359 bio
->bi_end_io
= rrpc_end_sync_bio
;
361 bio_add_pc_page(q
, bio
, page
, RRPC_EXPOSED_PAGE_SIZE
, 0);
363 /* turn the command around and write the data back to a new
366 if (rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_GC
)) {
367 pr_err("rrpc: gc write failed.\n");
368 rrpc_inflight_laddr_release(rrpc
, rqd
);
371 wait_for_completion_io(&wait
);
373 rrpc_inflight_laddr_release(rrpc
, rqd
);
381 mempool_free(page
, rrpc
->page_pool
);
384 if (!bitmap_full(rblk
->invalid_pages
, nr_sec_per_blk
)) {
385 pr_err("nvm: failed to garbage collect block\n");
392 static void rrpc_block_gc(struct work_struct
*work
)
394 struct rrpc_block_gc
*gcb
= container_of(work
, struct rrpc_block_gc
,
396 struct rrpc
*rrpc
= gcb
->rrpc
;
397 struct rrpc_block
*rblk
= gcb
->rblk
;
398 struct rrpc_lun
*rlun
= rblk
->rlun
;
399 struct nvm_dev
*dev
= rrpc
->dev
;
401 mempool_free(gcb
, rrpc
->gcb_pool
);
402 pr_debug("nvm: block '%lu' being reclaimed\n", rblk
->parent
->id
);
404 if (rrpc_move_valid_pages(rrpc
, rblk
))
407 if (nvm_erase_blk(dev
, rblk
->parent
))
410 rrpc_put_blk(rrpc
, rblk
);
415 spin_lock(&rlun
->lock
);
416 list_add_tail(&rblk
->prio
, &rlun
->prio_list
);
417 spin_unlock(&rlun
->lock
);
420 /* the block with highest number of invalid pages, will be in the beginning
423 static struct rrpc_block
*rblock_max_invalid(struct rrpc_block
*ra
,
424 struct rrpc_block
*rb
)
426 if (ra
->nr_invalid_pages
== rb
->nr_invalid_pages
)
429 return (ra
->nr_invalid_pages
< rb
->nr_invalid_pages
) ? rb
: ra
;
432 /* linearly find the block with highest number of invalid pages
435 static struct rrpc_block
*block_prio_find_max(struct rrpc_lun
*rlun
)
437 struct list_head
*prio_list
= &rlun
->prio_list
;
438 struct rrpc_block
*rblock
, *max
;
440 BUG_ON(list_empty(prio_list
));
442 max
= list_first_entry(prio_list
, struct rrpc_block
, prio
);
443 list_for_each_entry(rblock
, prio_list
, prio
)
444 max
= rblock_max_invalid(max
, rblock
);
449 static void rrpc_lun_gc(struct work_struct
*work
)
451 struct rrpc_lun
*rlun
= container_of(work
, struct rrpc_lun
, ws_gc
);
452 struct rrpc
*rrpc
= rlun
->rrpc
;
453 struct nvm_lun
*lun
= rlun
->parent
;
454 struct rrpc_block_gc
*gcb
;
455 unsigned int nr_blocks_need
;
457 nr_blocks_need
= rrpc
->dev
->blks_per_lun
/ GC_LIMIT_INVERSE
;
459 if (nr_blocks_need
< rrpc
->nr_luns
)
460 nr_blocks_need
= rrpc
->nr_luns
;
462 spin_lock(&rlun
->lock
);
463 while (nr_blocks_need
> lun
->nr_free_blocks
&&
464 !list_empty(&rlun
->prio_list
)) {
465 struct rrpc_block
*rblock
= block_prio_find_max(rlun
);
466 struct nvm_block
*block
= rblock
->parent
;
468 if (!rblock
->nr_invalid_pages
)
471 gcb
= mempool_alloc(rrpc
->gcb_pool
, GFP_ATOMIC
);
475 list_del_init(&rblock
->prio
);
477 BUG_ON(!block_is_full(rrpc
, rblock
));
479 pr_debug("rrpc: selected block '%lu' for GC\n", block
->id
);
483 INIT_WORK(&gcb
->ws_gc
, rrpc_block_gc
);
485 queue_work(rrpc
->kgc_wq
, &gcb
->ws_gc
);
489 spin_unlock(&rlun
->lock
);
491 /* TODO: Hint that request queue can be started again */
494 static void rrpc_gc_queue(struct work_struct
*work
)
496 struct rrpc_block_gc
*gcb
= container_of(work
, struct rrpc_block_gc
,
498 struct rrpc
*rrpc
= gcb
->rrpc
;
499 struct rrpc_block
*rblk
= gcb
->rblk
;
500 struct rrpc_lun
*rlun
= rblk
->rlun
;
502 spin_lock(&rlun
->lock
);
503 list_add_tail(&rblk
->prio
, &rlun
->prio_list
);
504 spin_unlock(&rlun
->lock
);
506 mempool_free(gcb
, rrpc
->gcb_pool
);
507 pr_debug("nvm: block '%lu' is full, allow GC (sched)\n",
511 static const struct block_device_operations rrpc_fops
= {
512 .owner
= THIS_MODULE
,
515 static struct rrpc_lun
*rrpc_get_lun_rr(struct rrpc
*rrpc
, int is_gc
)
518 struct rrpc_lun
*rlun
, *max_free
;
521 return get_next_lun(rrpc
);
523 /* during GC, we don't care about RR, instead we want to make
524 * sure that we maintain evenness between the block luns.
526 max_free
= &rrpc
->luns
[0];
527 /* prevent GC-ing lun from devouring pages of a lun with
528 * little free blocks. We don't take the lock as we only need an
531 rrpc_for_each_lun(rrpc
, rlun
, i
) {
532 if (rlun
->parent
->nr_free_blocks
>
533 max_free
->parent
->nr_free_blocks
)
540 static struct rrpc_addr
*rrpc_update_map(struct rrpc
*rrpc
, sector_t laddr
,
541 struct rrpc_block
*rblk
, u64 paddr
)
543 struct rrpc_addr
*gp
;
544 struct rrpc_rev_addr
*rev
;
546 BUG_ON(laddr
>= rrpc
->nr_sects
);
548 gp
= &rrpc
->trans_map
[laddr
];
549 spin_lock(&rrpc
->rev_lock
);
551 rrpc_page_invalidate(rrpc
, gp
);
556 rev
= &rrpc
->rev_trans_map
[gp
->addr
- rrpc
->poffset
];
558 spin_unlock(&rrpc
->rev_lock
);
563 static u64
rrpc_alloc_addr(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
565 u64 addr
= ADDR_EMPTY
;
567 spin_lock(&rblk
->lock
);
568 if (block_is_full(rrpc
, rblk
))
571 addr
= block_to_addr(rrpc
, rblk
) + rblk
->next_page
;
575 spin_unlock(&rblk
->lock
);
579 /* Map logical address to a physical page. The mapping implements a round robin
580 * approach and allocates a page from the next lun available.
582 * Returns rrpc_addr with the physical address and block. Returns NULL if no
583 * blocks in the next rlun are available.
585 static struct rrpc_addr
*rrpc_map_page(struct rrpc
*rrpc
, sector_t laddr
,
588 struct rrpc_lun
*rlun
;
589 struct rrpc_block
*rblk
, **cur_rblk
;
594 rlun
= rrpc_get_lun_rr(rrpc
, is_gc
);
597 if (!is_gc
&& lun
->nr_free_blocks
< rrpc
->nr_luns
* 4)
601 * page allocation steps:
602 * 1. Try to allocate new page from current rblk
603 * 2a. If succeed, proceed to map it in and return
604 * 2b. If fail, first try to allocate a new block from media manger,
605 * and then retry step 1. Retry until the normal block pool is
607 * 3. If exhausted, and garbage collector is requesting the block,
608 * go to the reserved block and retry step 1.
609 * In the case that this fails as well, or it is not GC
610 * requesting, report not able to retrieve a block and let the
611 * caller handle further processing.
614 spin_lock(&rlun
->lock
);
615 cur_rblk
= &rlun
->cur
;
618 paddr
= rrpc_alloc_addr(rrpc
, rblk
);
620 if (paddr
!= ADDR_EMPTY
)
623 if (!list_empty(&rlun
->wblk_list
)) {
625 rblk
= list_first_entry(&rlun
->wblk_list
, struct rrpc_block
,
627 rrpc_set_lun_cur(rlun
, rblk
, cur_rblk
);
628 list_del(&rblk
->prio
);
631 spin_unlock(&rlun
->lock
);
633 rblk
= rrpc_get_blk(rrpc
, rlun
, gc_force
);
635 spin_lock(&rlun
->lock
);
636 list_add_tail(&rblk
->prio
, &rlun
->wblk_list
);
638 * another thread might already have added a new block,
639 * Therefore, make sure that one is used, instead of the
645 if (unlikely(is_gc
) && !gc_force
) {
646 /* retry from emergency gc block */
647 cur_rblk
= &rlun
->gc_cur
;
650 spin_lock(&rlun
->lock
);
654 pr_err("rrpc: failed to allocate new block\n");
657 spin_unlock(&rlun
->lock
);
658 return rrpc_update_map(rrpc
, laddr
, rblk
, paddr
);
661 static void rrpc_run_gc(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
663 struct rrpc_block_gc
*gcb
;
665 gcb
= mempool_alloc(rrpc
->gcb_pool
, GFP_ATOMIC
);
667 pr_err("rrpc: unable to queue block for gc.");
674 INIT_WORK(&gcb
->ws_gc
, rrpc_gc_queue
);
675 queue_work(rrpc
->kgc_wq
, &gcb
->ws_gc
);
678 static void rrpc_end_io_write(struct rrpc
*rrpc
, struct rrpc_rq
*rrqd
,
679 sector_t laddr
, uint8_t npages
)
682 struct rrpc_block
*rblk
;
686 for (i
= 0; i
< npages
; i
++) {
687 p
= &rrpc
->trans_map
[laddr
+ i
];
689 lun
= rblk
->parent
->lun
;
691 cmnt_size
= atomic_inc_return(&rblk
->data_cmnt_size
);
692 if (unlikely(cmnt_size
== rrpc
->dev
->sec_per_blk
))
693 rrpc_run_gc(rrpc
, rblk
);
697 static void rrpc_end_io(struct nvm_rq
*rqd
)
699 struct rrpc
*rrpc
= container_of(rqd
->ins
, struct rrpc
, instance
);
700 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
701 uint8_t npages
= rqd
->nr_ppas
;
702 sector_t laddr
= rrpc_get_laddr(rqd
->bio
) - npages
;
704 if (bio_data_dir(rqd
->bio
) == WRITE
)
705 rrpc_end_io_write(rrpc
, rrqd
, laddr
, npages
);
709 if (rrqd
->flags
& NVM_IOTYPE_GC
)
712 rrpc_unlock_rq(rrpc
, rqd
);
715 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
717 mempool_free(rqd
, rrpc
->rq_pool
);
720 static int rrpc_read_ppalist_rq(struct rrpc
*rrpc
, struct bio
*bio
,
721 struct nvm_rq
*rqd
, unsigned long flags
, int npages
)
723 struct rrpc_inflight_rq
*r
= rrpc_get_inflight_rq(rqd
);
724 struct rrpc_addr
*gp
;
725 sector_t laddr
= rrpc_get_laddr(bio
);
726 int is_gc
= flags
& NVM_IOTYPE_GC
;
729 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
)) {
730 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
731 return NVM_IO_REQUEUE
;
734 for (i
= 0; i
< npages
; i
++) {
735 /* We assume that mapping occurs at 4KB granularity */
736 BUG_ON(!(laddr
+ i
>= 0 && laddr
+ i
< rrpc
->nr_sects
));
737 gp
= &rrpc
->trans_map
[laddr
+ i
];
740 rqd
->ppa_list
[i
] = rrpc_ppa_to_gaddr(rrpc
->dev
,
744 rrpc_unlock_laddr(rrpc
, r
);
745 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
,
751 rqd
->opcode
= NVM_OP_HBREAD
;
756 static int rrpc_read_rq(struct rrpc
*rrpc
, struct bio
*bio
, struct nvm_rq
*rqd
,
759 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
760 int is_gc
= flags
& NVM_IOTYPE_GC
;
761 sector_t laddr
= rrpc_get_laddr(bio
);
762 struct rrpc_addr
*gp
;
764 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
))
765 return NVM_IO_REQUEUE
;
767 BUG_ON(!(laddr
>= 0 && laddr
< rrpc
->nr_sects
));
768 gp
= &rrpc
->trans_map
[laddr
];
771 rqd
->ppa_addr
= rrpc_ppa_to_gaddr(rrpc
->dev
, gp
->addr
);
774 rrpc_unlock_rq(rrpc
, rqd
);
778 rqd
->opcode
= NVM_OP_HBREAD
;
784 static int rrpc_write_ppalist_rq(struct rrpc
*rrpc
, struct bio
*bio
,
785 struct nvm_rq
*rqd
, unsigned long flags
, int npages
)
787 struct rrpc_inflight_rq
*r
= rrpc_get_inflight_rq(rqd
);
789 sector_t laddr
= rrpc_get_laddr(bio
);
790 int is_gc
= flags
& NVM_IOTYPE_GC
;
793 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
)) {
794 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
795 return NVM_IO_REQUEUE
;
798 for (i
= 0; i
< npages
; i
++) {
799 /* We assume that mapping occurs at 4KB granularity */
800 p
= rrpc_map_page(rrpc
, laddr
+ i
, is_gc
);
803 rrpc_unlock_laddr(rrpc
, r
);
804 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
,
807 return NVM_IO_REQUEUE
;
810 rqd
->ppa_list
[i
] = rrpc_ppa_to_gaddr(rrpc
->dev
,
814 rqd
->opcode
= NVM_OP_HBWRITE
;
819 static int rrpc_write_rq(struct rrpc
*rrpc
, struct bio
*bio
,
820 struct nvm_rq
*rqd
, unsigned long flags
)
822 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
824 int is_gc
= flags
& NVM_IOTYPE_GC
;
825 sector_t laddr
= rrpc_get_laddr(bio
);
827 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
))
828 return NVM_IO_REQUEUE
;
830 p
= rrpc_map_page(rrpc
, laddr
, is_gc
);
833 rrpc_unlock_rq(rrpc
, rqd
);
835 return NVM_IO_REQUEUE
;
838 rqd
->ppa_addr
= rrpc_ppa_to_gaddr(rrpc
->dev
, p
->addr
);
839 rqd
->opcode
= NVM_OP_HBWRITE
;
845 static int rrpc_setup_rq(struct rrpc
*rrpc
, struct bio
*bio
,
846 struct nvm_rq
*rqd
, unsigned long flags
, uint8_t npages
)
849 rqd
->ppa_list
= nvm_dev_dma_alloc(rrpc
->dev
, GFP_KERNEL
,
851 if (!rqd
->ppa_list
) {
852 pr_err("rrpc: not able to allocate ppa list\n");
856 if (bio_op(bio
) == REQ_OP_WRITE
)
857 return rrpc_write_ppalist_rq(rrpc
, bio
, rqd
, flags
,
860 return rrpc_read_ppalist_rq(rrpc
, bio
, rqd
, flags
, npages
);
863 if (bio_op(bio
) == REQ_OP_WRITE
)
864 return rrpc_write_rq(rrpc
, bio
, rqd
, flags
);
866 return rrpc_read_rq(rrpc
, bio
, rqd
, flags
);
869 static int rrpc_submit_io(struct rrpc
*rrpc
, struct bio
*bio
,
870 struct nvm_rq
*rqd
, unsigned long flags
)
873 struct rrpc_rq
*rrq
= nvm_rq_to_pdu(rqd
);
874 uint8_t nr_pages
= rrpc_get_pages(bio
);
875 int bio_size
= bio_sectors(bio
) << 9;
877 if (bio_size
< rrpc
->dev
->sec_size
)
879 else if (bio_size
> rrpc
->dev
->max_rq_size
)
882 err
= rrpc_setup_rq(rrpc
, bio
, rqd
, flags
, nr_pages
);
888 rqd
->ins
= &rrpc
->instance
;
889 rqd
->nr_ppas
= nr_pages
;
892 err
= nvm_submit_io(rrpc
->dev
, rqd
);
894 pr_err("rrpc: I/O submission failed: %d\n", err
);
896 if (!(flags
& NVM_IOTYPE_GC
)) {
897 rrpc_unlock_rq(rrpc
, rqd
);
898 if (rqd
->nr_ppas
> 1)
899 nvm_dev_dma_free(rrpc
->dev
,
900 rqd
->ppa_list
, rqd
->dma_ppa_list
);
908 static blk_qc_t
rrpc_make_rq(struct request_queue
*q
, struct bio
*bio
)
910 struct rrpc
*rrpc
= q
->queuedata
;
914 if (bio_op(bio
) == REQ_OP_DISCARD
) {
915 rrpc_discard(rrpc
, bio
);
916 return BLK_QC_T_NONE
;
919 rqd
= mempool_alloc(rrpc
->rq_pool
, GFP_KERNEL
);
921 pr_err_ratelimited("rrpc: not able to queue bio.");
923 return BLK_QC_T_NONE
;
925 memset(rqd
, 0, sizeof(struct nvm_rq
));
927 err
= rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_NONE
);
930 return BLK_QC_T_NONE
;
938 spin_lock(&rrpc
->bio_lock
);
939 bio_list_add(&rrpc
->requeue_bios
, bio
);
940 spin_unlock(&rrpc
->bio_lock
);
941 queue_work(rrpc
->kgc_wq
, &rrpc
->ws_requeue
);
945 mempool_free(rqd
, rrpc
->rq_pool
);
946 return BLK_QC_T_NONE
;
949 static void rrpc_requeue(struct work_struct
*work
)
951 struct rrpc
*rrpc
= container_of(work
, struct rrpc
, ws_requeue
);
952 struct bio_list bios
;
955 bio_list_init(&bios
);
957 spin_lock(&rrpc
->bio_lock
);
958 bio_list_merge(&bios
, &rrpc
->requeue_bios
);
959 bio_list_init(&rrpc
->requeue_bios
);
960 spin_unlock(&rrpc
->bio_lock
);
962 while ((bio
= bio_list_pop(&bios
)))
963 rrpc_make_rq(rrpc
->disk
->queue
, bio
);
966 static void rrpc_gc_free(struct rrpc
*rrpc
)
969 destroy_workqueue(rrpc
->krqd_wq
);
972 destroy_workqueue(rrpc
->kgc_wq
);
975 static int rrpc_gc_init(struct rrpc
*rrpc
)
977 rrpc
->krqd_wq
= alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM
|WQ_UNBOUND
,
982 rrpc
->kgc_wq
= alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM
, 1);
986 setup_timer(&rrpc
->gc_timer
, rrpc_gc_timer
, (unsigned long)rrpc
);
991 static void rrpc_map_free(struct rrpc
*rrpc
)
993 vfree(rrpc
->rev_trans_map
);
994 vfree(rrpc
->trans_map
);
997 static int rrpc_l2p_update(u64 slba
, u32 nlb
, __le64
*entries
, void *private)
999 struct rrpc
*rrpc
= (struct rrpc
*)private;
1000 struct nvm_dev
*dev
= rrpc
->dev
;
1001 struct rrpc_addr
*addr
= rrpc
->trans_map
+ slba
;
1002 struct rrpc_rev_addr
*raddr
= rrpc
->rev_trans_map
;
1003 u64 elba
= slba
+ nlb
;
1006 if (unlikely(elba
> dev
->total_secs
)) {
1007 pr_err("nvm: L2P data from device is out of bounds!\n");
1011 for (i
= 0; i
< nlb
; i
++) {
1012 u64 pba
= le64_to_cpu(entries
[i
]);
1014 /* LNVM treats address-spaces as silos, LBA and PBA are
1015 * equally large and zero-indexed.
1017 if (unlikely(pba
>= dev
->total_secs
&& pba
!= U64_MAX
)) {
1018 pr_err("nvm: L2P data entry is out of bounds!\n");
1022 /* Address zero is a special one. The first page on a disk is
1023 * protected. As it often holds internal device boot
1029 div_u64_rem(pba
, rrpc
->nr_sects
, &mod
);
1032 raddr
[mod
].addr
= slba
+ i
;
1038 static int rrpc_map_init(struct rrpc
*rrpc
)
1040 struct nvm_dev
*dev
= rrpc
->dev
;
1044 rrpc
->trans_map
= vzalloc(sizeof(struct rrpc_addr
) * rrpc
->nr_sects
);
1045 if (!rrpc
->trans_map
)
1048 rrpc
->rev_trans_map
= vmalloc(sizeof(struct rrpc_rev_addr
)
1050 if (!rrpc
->rev_trans_map
)
1053 for (i
= 0; i
< rrpc
->nr_sects
; i
++) {
1054 struct rrpc_addr
*p
= &rrpc
->trans_map
[i
];
1055 struct rrpc_rev_addr
*r
= &rrpc
->rev_trans_map
[i
];
1057 p
->addr
= ADDR_EMPTY
;
1058 r
->addr
= ADDR_EMPTY
;
1061 if (!dev
->ops
->get_l2p_tbl
)
1064 /* Bring up the mapping table from device */
1065 ret
= dev
->ops
->get_l2p_tbl(dev
, rrpc
->soffset
, rrpc
->nr_sects
,
1066 rrpc_l2p_update
, rrpc
);
1068 pr_err("nvm: rrpc: could not read L2P table.\n");
1075 /* Minimum pages needed within a lun */
1076 #define PAGE_POOL_SIZE 16
1077 #define ADDR_POOL_SIZE 64
1079 static int rrpc_core_init(struct rrpc
*rrpc
)
1081 down_write(&rrpc_lock
);
1082 if (!rrpc_gcb_cache
) {
1083 rrpc_gcb_cache
= kmem_cache_create("rrpc_gcb",
1084 sizeof(struct rrpc_block_gc
), 0, 0, NULL
);
1085 if (!rrpc_gcb_cache
) {
1086 up_write(&rrpc_lock
);
1090 rrpc_rq_cache
= kmem_cache_create("rrpc_rq",
1091 sizeof(struct nvm_rq
) + sizeof(struct rrpc_rq
),
1093 if (!rrpc_rq_cache
) {
1094 kmem_cache_destroy(rrpc_gcb_cache
);
1095 up_write(&rrpc_lock
);
1099 up_write(&rrpc_lock
);
1101 rrpc
->page_pool
= mempool_create_page_pool(PAGE_POOL_SIZE
, 0);
1102 if (!rrpc
->page_pool
)
1105 rrpc
->gcb_pool
= mempool_create_slab_pool(rrpc
->dev
->nr_luns
,
1107 if (!rrpc
->gcb_pool
)
1110 rrpc
->rq_pool
= mempool_create_slab_pool(64, rrpc_rq_cache
);
1114 spin_lock_init(&rrpc
->inflights
.lock
);
1115 INIT_LIST_HEAD(&rrpc
->inflights
.reqs
);
1120 static void rrpc_core_free(struct rrpc
*rrpc
)
1122 mempool_destroy(rrpc
->page_pool
);
1123 mempool_destroy(rrpc
->gcb_pool
);
1124 mempool_destroy(rrpc
->rq_pool
);
1127 static void rrpc_luns_free(struct rrpc
*rrpc
)
1129 struct nvm_dev
*dev
= rrpc
->dev
;
1130 struct nvm_lun
*lun
;
1131 struct rrpc_lun
*rlun
;
1137 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
1138 rlun
= &rrpc
->luns
[i
];
1142 dev
->mt
->release_lun(dev
, lun
->id
);
1143 vfree(rlun
->blocks
);
1149 static int rrpc_luns_init(struct rrpc
*rrpc
, int lun_begin
, int lun_end
)
1151 struct nvm_dev
*dev
= rrpc
->dev
;
1152 struct rrpc_lun
*rlun
;
1153 int i
, j
, ret
= -EINVAL
;
1155 if (dev
->sec_per_blk
> MAX_INVALID_PAGES_STORAGE
* BITS_PER_LONG
) {
1156 pr_err("rrpc: number of pages per block too high.");
1160 spin_lock_init(&rrpc
->rev_lock
);
1162 rrpc
->luns
= kcalloc(rrpc
->nr_luns
, sizeof(struct rrpc_lun
),
1168 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
1169 int lunid
= lun_begin
+ i
;
1170 struct nvm_lun
*lun
;
1172 if (dev
->mt
->reserve_lun(dev
, lunid
)) {
1173 pr_err("rrpc: lun %u is already allocated\n", lunid
);
1177 lun
= dev
->mt
->get_lun(dev
, lunid
);
1181 rlun
= &rrpc
->luns
[i
];
1183 rlun
->blocks
= vzalloc(sizeof(struct rrpc_block
) *
1184 rrpc
->dev
->blks_per_lun
);
1185 if (!rlun
->blocks
) {
1190 for (j
= 0; j
< rrpc
->dev
->blks_per_lun
; j
++) {
1191 struct rrpc_block
*rblk
= &rlun
->blocks
[j
];
1192 struct nvm_block
*blk
= &lun
->blocks
[j
];
1196 INIT_LIST_HEAD(&rblk
->prio
);
1197 spin_lock_init(&rblk
->lock
);
1201 INIT_LIST_HEAD(&rlun
->prio_list
);
1202 INIT_LIST_HEAD(&rlun
->wblk_list
);
1204 INIT_WORK(&rlun
->ws_gc
, rrpc_lun_gc
);
1205 spin_lock_init(&rlun
->lock
);
1213 /* returns 0 on success and stores the beginning address in *begin */
1214 static int rrpc_area_init(struct rrpc
*rrpc
, sector_t
*begin
)
1216 struct nvm_dev
*dev
= rrpc
->dev
;
1217 struct nvmm_type
*mt
= dev
->mt
;
1218 sector_t size
= rrpc
->nr_sects
* dev
->sec_size
;
1223 ret
= mt
->get_area(dev
, begin
, size
);
1225 *begin
>>= (ilog2(dev
->sec_size
) - 9);
1230 static void rrpc_area_free(struct rrpc
*rrpc
)
1232 struct nvm_dev
*dev
= rrpc
->dev
;
1233 struct nvmm_type
*mt
= dev
->mt
;
1234 sector_t begin
= rrpc
->soffset
<< (ilog2(dev
->sec_size
) - 9);
1236 mt
->put_area(dev
, begin
);
1239 static void rrpc_free(struct rrpc
*rrpc
)
1242 rrpc_map_free(rrpc
);
1243 rrpc_core_free(rrpc
);
1244 rrpc_luns_free(rrpc
);
1245 rrpc_area_free(rrpc
);
1250 static void rrpc_exit(void *private)
1252 struct rrpc
*rrpc
= private;
1254 del_timer(&rrpc
->gc_timer
);
1256 flush_workqueue(rrpc
->krqd_wq
);
1257 flush_workqueue(rrpc
->kgc_wq
);
1262 static sector_t
rrpc_capacity(void *private)
1264 struct rrpc
*rrpc
= private;
1265 struct nvm_dev
*dev
= rrpc
->dev
;
1266 sector_t reserved
, provisioned
;
1268 /* cur, gc, and two emergency blocks for each lun */
1269 reserved
= rrpc
->nr_luns
* dev
->sec_per_blk
* 4;
1270 provisioned
= rrpc
->nr_sects
- reserved
;
1272 if (reserved
> rrpc
->nr_sects
) {
1273 pr_err("rrpc: not enough space available to expose storage.\n");
1277 sector_div(provisioned
, 10);
1278 return provisioned
* 9 * NR_PHY_IN_LOG
;
1282 * Looks up the logical address from reverse trans map and check if its valid by
1283 * comparing the logical to physical address with the physical address.
1284 * Returns 0 on free, otherwise 1 if in use
1286 static void rrpc_block_map_update(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
1288 struct nvm_dev
*dev
= rrpc
->dev
;
1290 struct rrpc_addr
*laddr
;
1291 u64 bpaddr
, paddr
, pladdr
;
1293 bpaddr
= block_to_rel_addr(rrpc
, rblk
);
1294 for (offset
= 0; offset
< dev
->sec_per_blk
; offset
++) {
1295 paddr
= bpaddr
+ offset
;
1297 pladdr
= rrpc
->rev_trans_map
[paddr
].addr
;
1298 if (pladdr
== ADDR_EMPTY
)
1301 laddr
= &rrpc
->trans_map
[pladdr
];
1303 if (paddr
== laddr
->addr
) {
1306 set_bit(offset
, rblk
->invalid_pages
);
1307 rblk
->nr_invalid_pages
++;
1312 static int rrpc_blocks_init(struct rrpc
*rrpc
)
1314 struct rrpc_lun
*rlun
;
1315 struct rrpc_block
*rblk
;
1316 int lun_iter
, blk_iter
;
1318 for (lun_iter
= 0; lun_iter
< rrpc
->nr_luns
; lun_iter
++) {
1319 rlun
= &rrpc
->luns
[lun_iter
];
1321 for (blk_iter
= 0; blk_iter
< rrpc
->dev
->blks_per_lun
;
1323 rblk
= &rlun
->blocks
[blk_iter
];
1324 rrpc_block_map_update(rrpc
, rblk
);
1331 static int rrpc_luns_configure(struct rrpc
*rrpc
)
1333 struct rrpc_lun
*rlun
;
1334 struct rrpc_block
*rblk
;
1337 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
1338 rlun
= &rrpc
->luns
[i
];
1340 rblk
= rrpc_get_blk(rrpc
, rlun
, 0);
1343 rrpc_set_lun_cur(rlun
, rblk
, &rlun
->cur
);
1345 /* Emergency gc block */
1346 rblk
= rrpc_get_blk(rrpc
, rlun
, 1);
1349 rrpc_set_lun_cur(rlun
, rblk
, &rlun
->gc_cur
);
1354 rrpc_put_blks(rrpc
);
1358 static struct nvm_tgt_type tt_rrpc
;
1360 static void *rrpc_init(struct nvm_dev
*dev
, struct gendisk
*tdisk
,
1361 int lun_begin
, int lun_end
)
1363 struct request_queue
*bqueue
= dev
->q
;
1364 struct request_queue
*tqueue
= tdisk
->queue
;
1369 if (!(dev
->identity
.dom
& NVM_RSP_L2P
)) {
1370 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1372 return ERR_PTR(-EINVAL
);
1375 rrpc
= kzalloc(sizeof(struct rrpc
), GFP_KERNEL
);
1377 return ERR_PTR(-ENOMEM
);
1379 rrpc
->instance
.tt
= &tt_rrpc
;
1383 bio_list_init(&rrpc
->requeue_bios
);
1384 spin_lock_init(&rrpc
->bio_lock
);
1385 INIT_WORK(&rrpc
->ws_requeue
, rrpc_requeue
);
1387 rrpc
->nr_luns
= lun_end
- lun_begin
+ 1;
1388 rrpc
->total_blocks
= (unsigned long)dev
->blks_per_lun
* rrpc
->nr_luns
;
1389 rrpc
->nr_sects
= (unsigned long long)dev
->sec_per_lun
* rrpc
->nr_luns
;
1391 /* simple round-robin strategy */
1392 atomic_set(&rrpc
->next_lun
, -1);
1394 ret
= rrpc_area_init(rrpc
, &soffset
);
1396 pr_err("nvm: rrpc: could not initialize area\n");
1397 return ERR_PTR(ret
);
1399 rrpc
->soffset
= soffset
;
1401 ret
= rrpc_luns_init(rrpc
, lun_begin
, lun_end
);
1403 pr_err("nvm: rrpc: could not initialize luns\n");
1407 rrpc
->poffset
= dev
->sec_per_lun
* lun_begin
;
1408 rrpc
->lun_offset
= lun_begin
;
1410 ret
= rrpc_core_init(rrpc
);
1412 pr_err("nvm: rrpc: could not initialize core\n");
1416 ret
= rrpc_map_init(rrpc
);
1418 pr_err("nvm: rrpc: could not initialize maps\n");
1422 ret
= rrpc_blocks_init(rrpc
);
1424 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1428 ret
= rrpc_luns_configure(rrpc
);
1430 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1434 ret
= rrpc_gc_init(rrpc
);
1436 pr_err("nvm: rrpc: could not initialize gc\n");
1440 /* inherit the size from the underlying device */
1441 blk_queue_logical_block_size(tqueue
, queue_physical_block_size(bqueue
));
1442 blk_queue_max_hw_sectors(tqueue
, queue_max_hw_sectors(bqueue
));
1444 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
1445 rrpc
->nr_luns
, (unsigned long long)rrpc
->nr_sects
);
1447 mod_timer(&rrpc
->gc_timer
, jiffies
+ msecs_to_jiffies(10));
1452 return ERR_PTR(ret
);
1455 /* round robin, page-based FTL, and cost-based GC */
1456 static struct nvm_tgt_type tt_rrpc
= {
1458 .version
= {1, 0, 0},
1460 .make_rq
= rrpc_make_rq
,
1461 .capacity
= rrpc_capacity
,
1462 .end_io
= rrpc_end_io
,
1468 static int __init
rrpc_module_init(void)
1470 return nvm_register_tgt_type(&tt_rrpc
);
1473 static void rrpc_module_exit(void)
1475 nvm_unregister_tgt_type(&tt_rrpc
);
1478 module_init(rrpc_module_init
);
1479 module_exit(rrpc_module_exit
);
1480 MODULE_LICENSE("GPL v2");
1481 MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");