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
, flags
);
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 void rrpc_put_blks(struct rrpc
*rrpc
)
207 struct rrpc_lun
*rlun
;
210 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
211 rlun
= &rrpc
->luns
[i
];
213 rrpc_put_blk(rrpc
, rlun
->cur
);
215 rrpc_put_blk(rrpc
, rlun
->gc_cur
);
219 static struct rrpc_lun
*get_next_lun(struct rrpc
*rrpc
)
221 int next
= atomic_inc_return(&rrpc
->next_lun
);
223 return &rrpc
->luns
[next
% rrpc
->nr_luns
];
226 static void rrpc_gc_kick(struct rrpc
*rrpc
)
228 struct rrpc_lun
*rlun
;
231 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
232 rlun
= &rrpc
->luns
[i
];
233 queue_work(rrpc
->krqd_wq
, &rlun
->ws_gc
);
238 * timed GC every interval.
240 static void rrpc_gc_timer(unsigned long data
)
242 struct rrpc
*rrpc
= (struct rrpc
*)data
;
245 mod_timer(&rrpc
->gc_timer
, jiffies
+ msecs_to_jiffies(10));
248 static void rrpc_end_sync_bio(struct bio
*bio
)
250 struct completion
*waiting
= bio
->bi_private
;
253 pr_err("nvm: gc request failed (%u).\n", bio
->bi_error
);
259 * rrpc_move_valid_pages -- migrate live data off the block
260 * @rrpc: the 'rrpc' structure
261 * @block: the block from which to migrate live pages
264 * GC algorithms may call this function to migrate remaining live
265 * pages off the block prior to erasing it. This function blocks
266 * further execution until the operation is complete.
268 static int rrpc_move_valid_pages(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
270 struct request_queue
*q
= rrpc
->dev
->q
;
271 struct rrpc_rev_addr
*rev
;
276 int nr_pgs_per_blk
= rrpc
->dev
->pgs_per_blk
;
278 DECLARE_COMPLETION_ONSTACK(wait
);
280 if (bitmap_full(rblk
->invalid_pages
, nr_pgs_per_blk
))
283 bio
= bio_alloc(GFP_NOIO
, 1);
285 pr_err("nvm: could not alloc bio to gc\n");
289 page
= mempool_alloc(rrpc
->page_pool
, GFP_NOIO
);
295 while ((slot
= find_first_zero_bit(rblk
->invalid_pages
,
296 nr_pgs_per_blk
)) < nr_pgs_per_blk
) {
299 phys_addr
= (rblk
->parent
->id
* nr_pgs_per_blk
) + slot
;
302 spin_lock(&rrpc
->rev_lock
);
303 /* Get logical address from physical to logical table */
304 rev
= &rrpc
->rev_trans_map
[phys_addr
- rrpc
->poffset
];
305 /* already updated by previous regular write */
306 if (rev
->addr
== ADDR_EMPTY
) {
307 spin_unlock(&rrpc
->rev_lock
);
311 rqd
= rrpc_inflight_laddr_acquire(rrpc
, rev
->addr
, 1);
312 if (IS_ERR_OR_NULL(rqd
)) {
313 spin_unlock(&rrpc
->rev_lock
);
318 spin_unlock(&rrpc
->rev_lock
);
320 /* Perform read to do GC */
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 /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
327 bio_add_pc_page(q
, bio
, page
, RRPC_EXPOSED_PAGE_SIZE
, 0);
329 if (rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_GC
)) {
330 pr_err("rrpc: gc read failed.\n");
331 rrpc_inflight_laddr_release(rrpc
, rqd
);
334 wait_for_completion_io(&wait
);
337 reinit_completion(&wait
);
339 bio
->bi_iter
.bi_sector
= rrpc_get_sector(rev
->addr
);
341 bio
->bi_private
= &wait
;
342 bio
->bi_end_io
= rrpc_end_sync_bio
;
344 bio_add_pc_page(q
, bio
, page
, RRPC_EXPOSED_PAGE_SIZE
, 0);
346 /* turn the command around and write the data back to a new
349 if (rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_GC
)) {
350 pr_err("rrpc: gc write failed.\n");
351 rrpc_inflight_laddr_release(rrpc
, rqd
);
354 wait_for_completion_io(&wait
);
356 rrpc_inflight_laddr_release(rrpc
, rqd
);
362 mempool_free(page
, rrpc
->page_pool
);
365 if (!bitmap_full(rblk
->invalid_pages
, nr_pgs_per_blk
)) {
366 pr_err("nvm: failed to garbage collect block\n");
373 static void rrpc_block_gc(struct work_struct
*work
)
375 struct rrpc_block_gc
*gcb
= container_of(work
, struct rrpc_block_gc
,
377 struct rrpc
*rrpc
= gcb
->rrpc
;
378 struct rrpc_block
*rblk
= gcb
->rblk
;
379 struct nvm_dev
*dev
= rrpc
->dev
;
381 pr_debug("nvm: block '%lu' being reclaimed\n", rblk
->parent
->id
);
383 if (rrpc_move_valid_pages(rrpc
, rblk
))
386 nvm_erase_blk(dev
, rblk
->parent
);
387 rrpc_put_blk(rrpc
, rblk
);
389 mempool_free(gcb
, rrpc
->gcb_pool
);
392 /* the block with highest number of invalid pages, will be in the beginning
395 static struct rrpc_block
*rblock_max_invalid(struct rrpc_block
*ra
,
396 struct rrpc_block
*rb
)
398 if (ra
->nr_invalid_pages
== rb
->nr_invalid_pages
)
401 return (ra
->nr_invalid_pages
< rb
->nr_invalid_pages
) ? rb
: ra
;
404 /* linearly find the block with highest number of invalid pages
407 static struct rrpc_block
*block_prio_find_max(struct rrpc_lun
*rlun
)
409 struct list_head
*prio_list
= &rlun
->prio_list
;
410 struct rrpc_block
*rblock
, *max
;
412 BUG_ON(list_empty(prio_list
));
414 max
= list_first_entry(prio_list
, struct rrpc_block
, prio
);
415 list_for_each_entry(rblock
, prio_list
, prio
)
416 max
= rblock_max_invalid(max
, rblock
);
421 static void rrpc_lun_gc(struct work_struct
*work
)
423 struct rrpc_lun
*rlun
= container_of(work
, struct rrpc_lun
, ws_gc
);
424 struct rrpc
*rrpc
= rlun
->rrpc
;
425 struct nvm_lun
*lun
= rlun
->parent
;
426 struct rrpc_block_gc
*gcb
;
427 unsigned int nr_blocks_need
;
429 nr_blocks_need
= rrpc
->dev
->blks_per_lun
/ GC_LIMIT_INVERSE
;
431 if (nr_blocks_need
< rrpc
->nr_luns
)
432 nr_blocks_need
= rrpc
->nr_luns
;
434 spin_lock(&rlun
->lock
);
435 while (nr_blocks_need
> lun
->nr_free_blocks
&&
436 !list_empty(&rlun
->prio_list
)) {
437 struct rrpc_block
*rblock
= block_prio_find_max(rlun
);
438 struct nvm_block
*block
= rblock
->parent
;
440 if (!rblock
->nr_invalid_pages
)
443 gcb
= mempool_alloc(rrpc
->gcb_pool
, GFP_ATOMIC
);
447 list_del_init(&rblock
->prio
);
449 BUG_ON(!block_is_full(rrpc
, rblock
));
451 pr_debug("rrpc: selected block '%lu' for GC\n", block
->id
);
455 INIT_WORK(&gcb
->ws_gc
, rrpc_block_gc
);
457 queue_work(rrpc
->kgc_wq
, &gcb
->ws_gc
);
461 spin_unlock(&rlun
->lock
);
463 /* TODO: Hint that request queue can be started again */
466 static void rrpc_gc_queue(struct work_struct
*work
)
468 struct rrpc_block_gc
*gcb
= container_of(work
, struct rrpc_block_gc
,
470 struct rrpc
*rrpc
= gcb
->rrpc
;
471 struct rrpc_block
*rblk
= gcb
->rblk
;
472 struct nvm_lun
*lun
= rblk
->parent
->lun
;
473 struct rrpc_lun
*rlun
= &rrpc
->luns
[lun
->id
- rrpc
->lun_offset
];
475 spin_lock(&rlun
->lock
);
476 list_add_tail(&rblk
->prio
, &rlun
->prio_list
);
477 spin_unlock(&rlun
->lock
);
479 mempool_free(gcb
, rrpc
->gcb_pool
);
480 pr_debug("nvm: block '%lu' is full, allow GC (sched)\n",
484 static const struct block_device_operations rrpc_fops
= {
485 .owner
= THIS_MODULE
,
488 static struct rrpc_lun
*rrpc_get_lun_rr(struct rrpc
*rrpc
, int is_gc
)
491 struct rrpc_lun
*rlun
, *max_free
;
494 return get_next_lun(rrpc
);
496 /* during GC, we don't care about RR, instead we want to make
497 * sure that we maintain evenness between the block luns.
499 max_free
= &rrpc
->luns
[0];
500 /* prevent GC-ing lun from devouring pages of a lun with
501 * little free blocks. We don't take the lock as we only need an
504 rrpc_for_each_lun(rrpc
, rlun
, i
) {
505 if (rlun
->parent
->nr_free_blocks
>
506 max_free
->parent
->nr_free_blocks
)
513 static struct rrpc_addr
*rrpc_update_map(struct rrpc
*rrpc
, sector_t laddr
,
514 struct rrpc_block
*rblk
, u64 paddr
)
516 struct rrpc_addr
*gp
;
517 struct rrpc_rev_addr
*rev
;
519 BUG_ON(laddr
>= rrpc
->nr_pages
);
521 gp
= &rrpc
->trans_map
[laddr
];
522 spin_lock(&rrpc
->rev_lock
);
524 rrpc_page_invalidate(rrpc
, gp
);
529 rev
= &rrpc
->rev_trans_map
[gp
->addr
- rrpc
->poffset
];
531 spin_unlock(&rrpc
->rev_lock
);
536 static u64
rrpc_alloc_addr(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
538 u64 addr
= ADDR_EMPTY
;
540 spin_lock(&rblk
->lock
);
541 if (block_is_full(rrpc
, rblk
))
544 addr
= block_to_addr(rrpc
, rblk
) + rblk
->next_page
;
548 spin_unlock(&rblk
->lock
);
552 /* Simple round-robin Logical to physical address translation.
554 * Retrieve the mapping using the active append point. Then update the ap for
555 * the next write to the disk.
557 * Returns rrpc_addr with the physical address and block. Remember to return to
558 * rrpc->addr_cache when request is finished.
560 static struct rrpc_addr
*rrpc_map_page(struct rrpc
*rrpc
, sector_t laddr
,
563 struct rrpc_lun
*rlun
;
564 struct rrpc_block
*rblk
;
568 rlun
= rrpc_get_lun_rr(rrpc
, is_gc
);
571 if (!is_gc
&& lun
->nr_free_blocks
< rrpc
->nr_luns
* 4)
574 spin_lock(&rlun
->lock
);
578 paddr
= rrpc_alloc_addr(rrpc
, rblk
);
580 if (paddr
== ADDR_EMPTY
) {
581 rblk
= rrpc_get_blk(rrpc
, rlun
, 0);
583 rrpc_set_lun_cur(rlun
, rblk
);
588 /* retry from emergency gc block */
589 paddr
= rrpc_alloc_addr(rrpc
, rlun
->gc_cur
);
590 if (paddr
== ADDR_EMPTY
) {
591 rblk
= rrpc_get_blk(rrpc
, rlun
, 1);
593 pr_err("rrpc: no more blocks");
598 paddr
= rrpc_alloc_addr(rrpc
, rlun
->gc_cur
);
604 spin_unlock(&rlun
->lock
);
605 return rrpc_update_map(rrpc
, laddr
, rblk
, paddr
);
607 spin_unlock(&rlun
->lock
);
611 static void rrpc_run_gc(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
613 struct rrpc_block_gc
*gcb
;
615 gcb
= mempool_alloc(rrpc
->gcb_pool
, GFP_ATOMIC
);
617 pr_err("rrpc: unable to queue block for gc.");
624 INIT_WORK(&gcb
->ws_gc
, rrpc_gc_queue
);
625 queue_work(rrpc
->kgc_wq
, &gcb
->ws_gc
);
628 static void rrpc_end_io_write(struct rrpc
*rrpc
, struct rrpc_rq
*rrqd
,
629 sector_t laddr
, uint8_t npages
)
632 struct rrpc_block
*rblk
;
636 for (i
= 0; i
< npages
; i
++) {
637 p
= &rrpc
->trans_map
[laddr
+ i
];
639 lun
= rblk
->parent
->lun
;
641 cmnt_size
= atomic_inc_return(&rblk
->data_cmnt_size
);
642 if (unlikely(cmnt_size
== rrpc
->dev
->pgs_per_blk
))
643 rrpc_run_gc(rrpc
, rblk
);
647 static int rrpc_end_io(struct nvm_rq
*rqd
, int error
)
649 struct rrpc
*rrpc
= container_of(rqd
->ins
, struct rrpc
, instance
);
650 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
651 uint8_t npages
= rqd
->nr_pages
;
652 sector_t laddr
= rrpc_get_laddr(rqd
->bio
) - npages
;
654 if (bio_data_dir(rqd
->bio
) == WRITE
)
655 rrpc_end_io_write(rrpc
, rrqd
, laddr
, npages
);
659 if (rrqd
->flags
& NVM_IOTYPE_GC
)
662 rrpc_unlock_rq(rrpc
, rqd
);
665 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
667 nvm_dev_dma_free(rrpc
->dev
, rqd
->metadata
, rqd
->dma_metadata
);
669 mempool_free(rqd
, rrpc
->rq_pool
);
674 static int rrpc_read_ppalist_rq(struct rrpc
*rrpc
, struct bio
*bio
,
675 struct nvm_rq
*rqd
, unsigned long flags
, int npages
)
677 struct rrpc_inflight_rq
*r
= rrpc_get_inflight_rq(rqd
);
678 struct rrpc_addr
*gp
;
679 sector_t laddr
= rrpc_get_laddr(bio
);
680 int is_gc
= flags
& NVM_IOTYPE_GC
;
683 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
)) {
684 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
685 return NVM_IO_REQUEUE
;
688 for (i
= 0; i
< npages
; i
++) {
689 /* We assume that mapping occurs at 4KB granularity */
690 BUG_ON(!(laddr
+ i
>= 0 && laddr
+ i
< rrpc
->nr_pages
));
691 gp
= &rrpc
->trans_map
[laddr
+ i
];
694 rqd
->ppa_list
[i
] = rrpc_ppa_to_gaddr(rrpc
->dev
,
698 rrpc_unlock_laddr(rrpc
, r
);
699 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
,
705 rqd
->opcode
= NVM_OP_HBREAD
;
710 static int rrpc_read_rq(struct rrpc
*rrpc
, struct bio
*bio
, struct nvm_rq
*rqd
,
713 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
714 int is_gc
= flags
& NVM_IOTYPE_GC
;
715 sector_t laddr
= rrpc_get_laddr(bio
);
716 struct rrpc_addr
*gp
;
718 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
))
719 return NVM_IO_REQUEUE
;
721 BUG_ON(!(laddr
>= 0 && laddr
< rrpc
->nr_pages
));
722 gp
= &rrpc
->trans_map
[laddr
];
725 rqd
->ppa_addr
= rrpc_ppa_to_gaddr(rrpc
->dev
, gp
->addr
);
728 rrpc_unlock_rq(rrpc
, rqd
);
732 rqd
->opcode
= NVM_OP_HBREAD
;
738 static int rrpc_write_ppalist_rq(struct rrpc
*rrpc
, struct bio
*bio
,
739 struct nvm_rq
*rqd
, unsigned long flags
, int npages
)
741 struct rrpc_inflight_rq
*r
= rrpc_get_inflight_rq(rqd
);
743 sector_t laddr
= rrpc_get_laddr(bio
);
744 int is_gc
= flags
& NVM_IOTYPE_GC
;
747 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
)) {
748 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
749 return NVM_IO_REQUEUE
;
752 for (i
= 0; i
< npages
; i
++) {
753 /* We assume that mapping occurs at 4KB granularity */
754 p
= rrpc_map_page(rrpc
, laddr
+ i
, is_gc
);
757 rrpc_unlock_laddr(rrpc
, r
);
758 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
,
761 return NVM_IO_REQUEUE
;
764 rqd
->ppa_list
[i
] = rrpc_ppa_to_gaddr(rrpc
->dev
,
768 rqd
->opcode
= NVM_OP_HBWRITE
;
773 static int rrpc_write_rq(struct rrpc
*rrpc
, struct bio
*bio
,
774 struct nvm_rq
*rqd
, unsigned long flags
)
776 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
778 int is_gc
= flags
& NVM_IOTYPE_GC
;
779 sector_t laddr
= rrpc_get_laddr(bio
);
781 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
))
782 return NVM_IO_REQUEUE
;
784 p
= rrpc_map_page(rrpc
, laddr
, is_gc
);
787 rrpc_unlock_rq(rrpc
, rqd
);
789 return NVM_IO_REQUEUE
;
792 rqd
->ppa_addr
= rrpc_ppa_to_gaddr(rrpc
->dev
, p
->addr
);
793 rqd
->opcode
= NVM_OP_HBWRITE
;
799 static int rrpc_setup_rq(struct rrpc
*rrpc
, struct bio
*bio
,
800 struct nvm_rq
*rqd
, unsigned long flags
, uint8_t npages
)
803 rqd
->ppa_list
= nvm_dev_dma_alloc(rrpc
->dev
, GFP_KERNEL
,
805 if (!rqd
->ppa_list
) {
806 pr_err("rrpc: not able to allocate ppa list\n");
810 if (bio_rw(bio
) == WRITE
)
811 return rrpc_write_ppalist_rq(rrpc
, bio
, rqd
, flags
,
814 return rrpc_read_ppalist_rq(rrpc
, bio
, rqd
, flags
, npages
);
817 if (bio_rw(bio
) == WRITE
)
818 return rrpc_write_rq(rrpc
, bio
, rqd
, flags
);
820 return rrpc_read_rq(rrpc
, bio
, rqd
, flags
);
823 static int rrpc_submit_io(struct rrpc
*rrpc
, struct bio
*bio
,
824 struct nvm_rq
*rqd
, unsigned long flags
)
827 struct rrpc_rq
*rrq
= nvm_rq_to_pdu(rqd
);
828 uint8_t nr_pages
= rrpc_get_pages(bio
);
829 int bio_size
= bio_sectors(bio
) << 9;
831 if (bio_size
< rrpc
->dev
->sec_size
)
833 else if (bio_size
> rrpc
->dev
->max_rq_size
)
836 err
= rrpc_setup_rq(rrpc
, bio
, rqd
, flags
, nr_pages
);
842 rqd
->ins
= &rrpc
->instance
;
843 rqd
->nr_pages
= nr_pages
;
846 err
= nvm_submit_io(rrpc
->dev
, rqd
);
848 pr_err("rrpc: I/O submission failed: %d\n", err
);
850 if (!(flags
& NVM_IOTYPE_GC
)) {
851 rrpc_unlock_rq(rrpc
, rqd
);
852 if (rqd
->nr_pages
> 1)
853 nvm_dev_dma_free(rrpc
->dev
,
854 rqd
->ppa_list
, rqd
->dma_ppa_list
);
862 static blk_qc_t
rrpc_make_rq(struct request_queue
*q
, struct bio
*bio
)
864 struct rrpc
*rrpc
= q
->queuedata
;
868 if (bio
->bi_rw
& REQ_DISCARD
) {
869 rrpc_discard(rrpc
, bio
);
870 return BLK_QC_T_NONE
;
873 rqd
= mempool_alloc(rrpc
->rq_pool
, GFP_KERNEL
);
875 pr_err_ratelimited("rrpc: not able to queue bio.");
877 return BLK_QC_T_NONE
;
879 memset(rqd
, 0, sizeof(struct nvm_rq
));
881 err
= rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_NONE
);
884 return BLK_QC_T_NONE
;
892 spin_lock(&rrpc
->bio_lock
);
893 bio_list_add(&rrpc
->requeue_bios
, bio
);
894 spin_unlock(&rrpc
->bio_lock
);
895 queue_work(rrpc
->kgc_wq
, &rrpc
->ws_requeue
);
899 mempool_free(rqd
, rrpc
->rq_pool
);
900 return BLK_QC_T_NONE
;
903 static void rrpc_requeue(struct work_struct
*work
)
905 struct rrpc
*rrpc
= container_of(work
, struct rrpc
, ws_requeue
);
906 struct bio_list bios
;
909 bio_list_init(&bios
);
911 spin_lock(&rrpc
->bio_lock
);
912 bio_list_merge(&bios
, &rrpc
->requeue_bios
);
913 bio_list_init(&rrpc
->requeue_bios
);
914 spin_unlock(&rrpc
->bio_lock
);
916 while ((bio
= bio_list_pop(&bios
)))
917 rrpc_make_rq(rrpc
->disk
->queue
, bio
);
920 static void rrpc_gc_free(struct rrpc
*rrpc
)
922 struct rrpc_lun
*rlun
;
926 destroy_workqueue(rrpc
->krqd_wq
);
929 destroy_workqueue(rrpc
->kgc_wq
);
934 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
935 rlun
= &rrpc
->luns
[i
];
943 static int rrpc_gc_init(struct rrpc
*rrpc
)
945 rrpc
->krqd_wq
= alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM
|WQ_UNBOUND
,
950 rrpc
->kgc_wq
= alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM
, 1);
954 setup_timer(&rrpc
->gc_timer
, rrpc_gc_timer
, (unsigned long)rrpc
);
959 static void rrpc_map_free(struct rrpc
*rrpc
)
961 vfree(rrpc
->rev_trans_map
);
962 vfree(rrpc
->trans_map
);
965 static int rrpc_l2p_update(u64 slba
, u32 nlb
, __le64
*entries
, void *private)
967 struct rrpc
*rrpc
= (struct rrpc
*)private;
968 struct nvm_dev
*dev
= rrpc
->dev
;
969 struct rrpc_addr
*addr
= rrpc
->trans_map
+ slba
;
970 struct rrpc_rev_addr
*raddr
= rrpc
->rev_trans_map
;
971 sector_t max_pages
= dev
->total_pages
* (dev
->sec_size
>> 9);
972 u64 elba
= slba
+ nlb
;
975 if (unlikely(elba
> dev
->total_pages
)) {
976 pr_err("nvm: L2P data from device is out of bounds!\n");
980 for (i
= 0; i
< nlb
; i
++) {
981 u64 pba
= le64_to_cpu(entries
[i
]);
982 /* LNVM treats address-spaces as silos, LBA and PBA are
983 * equally large and zero-indexed.
985 if (unlikely(pba
>= max_pages
&& pba
!= U64_MAX
)) {
986 pr_err("nvm: L2P data entry is out of bounds!\n");
990 /* Address zero is a special one. The first page on a disk is
991 * protected. As it often holds internal device boot
998 raddr
[pba
].addr
= slba
+ i
;
1004 static int rrpc_map_init(struct rrpc
*rrpc
)
1006 struct nvm_dev
*dev
= rrpc
->dev
;
1010 rrpc
->trans_map
= vzalloc(sizeof(struct rrpc_addr
) * rrpc
->nr_pages
);
1011 if (!rrpc
->trans_map
)
1014 rrpc
->rev_trans_map
= vmalloc(sizeof(struct rrpc_rev_addr
)
1016 if (!rrpc
->rev_trans_map
)
1019 for (i
= 0; i
< rrpc
->nr_pages
; i
++) {
1020 struct rrpc_addr
*p
= &rrpc
->trans_map
[i
];
1021 struct rrpc_rev_addr
*r
= &rrpc
->rev_trans_map
[i
];
1023 p
->addr
= ADDR_EMPTY
;
1024 r
->addr
= ADDR_EMPTY
;
1027 if (!dev
->ops
->get_l2p_tbl
)
1030 /* Bring up the mapping table from device */
1031 ret
= dev
->ops
->get_l2p_tbl(dev
, 0, dev
->total_pages
,
1032 rrpc_l2p_update
, rrpc
);
1034 pr_err("nvm: rrpc: could not read L2P table.\n");
1042 /* Minimum pages needed within a lun */
1043 #define PAGE_POOL_SIZE 16
1044 #define ADDR_POOL_SIZE 64
1046 static int rrpc_core_init(struct rrpc
*rrpc
)
1048 down_write(&rrpc_lock
);
1049 if (!rrpc_gcb_cache
) {
1050 rrpc_gcb_cache
= kmem_cache_create("rrpc_gcb",
1051 sizeof(struct rrpc_block_gc
), 0, 0, NULL
);
1052 if (!rrpc_gcb_cache
) {
1053 up_write(&rrpc_lock
);
1057 rrpc_rq_cache
= kmem_cache_create("rrpc_rq",
1058 sizeof(struct nvm_rq
) + sizeof(struct rrpc_rq
),
1060 if (!rrpc_rq_cache
) {
1061 kmem_cache_destroy(rrpc_gcb_cache
);
1062 up_write(&rrpc_lock
);
1066 up_write(&rrpc_lock
);
1068 rrpc
->page_pool
= mempool_create_page_pool(PAGE_POOL_SIZE
, 0);
1069 if (!rrpc
->page_pool
)
1072 rrpc
->gcb_pool
= mempool_create_slab_pool(rrpc
->dev
->nr_luns
,
1074 if (!rrpc
->gcb_pool
)
1077 rrpc
->rq_pool
= mempool_create_slab_pool(64, rrpc_rq_cache
);
1081 spin_lock_init(&rrpc
->inflights
.lock
);
1082 INIT_LIST_HEAD(&rrpc
->inflights
.reqs
);
1087 static void rrpc_core_free(struct rrpc
*rrpc
)
1089 mempool_destroy(rrpc
->page_pool
);
1090 mempool_destroy(rrpc
->gcb_pool
);
1091 mempool_destroy(rrpc
->rq_pool
);
1094 static void rrpc_luns_free(struct rrpc
*rrpc
)
1099 static int rrpc_luns_init(struct rrpc
*rrpc
, int lun_begin
, int lun_end
)
1101 struct nvm_dev
*dev
= rrpc
->dev
;
1102 struct rrpc_lun
*rlun
;
1105 spin_lock_init(&rrpc
->rev_lock
);
1107 rrpc
->luns
= kcalloc(rrpc
->nr_luns
, sizeof(struct rrpc_lun
),
1113 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
1114 struct nvm_lun
*lun
= dev
->mt
->get_lun(dev
, lun_begin
+ i
);
1116 if (dev
->pgs_per_blk
>
1117 MAX_INVALID_PAGES_STORAGE
* BITS_PER_LONG
) {
1118 pr_err("rrpc: number of pages per block too high.");
1122 rlun
= &rrpc
->luns
[i
];
1125 INIT_LIST_HEAD(&rlun
->prio_list
);
1126 INIT_WORK(&rlun
->ws_gc
, rrpc_lun_gc
);
1127 spin_lock_init(&rlun
->lock
);
1129 rrpc
->total_blocks
+= dev
->blks_per_lun
;
1130 rrpc
->nr_pages
+= dev
->sec_per_lun
;
1132 rlun
->blocks
= vzalloc(sizeof(struct rrpc_block
) *
1133 rrpc
->dev
->blks_per_lun
);
1137 for (j
= 0; j
< rrpc
->dev
->blks_per_lun
; j
++) {
1138 struct rrpc_block
*rblk
= &rlun
->blocks
[j
];
1139 struct nvm_block
*blk
= &lun
->blocks
[j
];
1142 INIT_LIST_HEAD(&rblk
->prio
);
1143 spin_lock_init(&rblk
->lock
);
1152 static void rrpc_free(struct rrpc
*rrpc
)
1155 rrpc_map_free(rrpc
);
1156 rrpc_core_free(rrpc
);
1157 rrpc_luns_free(rrpc
);
1162 static void rrpc_exit(void *private)
1164 struct rrpc
*rrpc
= private;
1166 del_timer(&rrpc
->gc_timer
);
1168 flush_workqueue(rrpc
->krqd_wq
);
1169 flush_workqueue(rrpc
->kgc_wq
);
1174 static sector_t
rrpc_capacity(void *private)
1176 struct rrpc
*rrpc
= private;
1177 struct nvm_dev
*dev
= rrpc
->dev
;
1178 sector_t reserved
, provisioned
;
1180 /* cur, gc, and two emergency blocks for each lun */
1181 reserved
= rrpc
->nr_luns
* dev
->max_pages_per_blk
* 4;
1182 provisioned
= rrpc
->nr_pages
- reserved
;
1184 if (reserved
> rrpc
->nr_pages
) {
1185 pr_err("rrpc: not enough space available to expose storage.\n");
1189 sector_div(provisioned
, 10);
1190 return provisioned
* 9 * NR_PHY_IN_LOG
;
1194 * Looks up the logical address from reverse trans map and check if its valid by
1195 * comparing the logical to physical address with the physical address.
1196 * Returns 0 on free, otherwise 1 if in use
1198 static void rrpc_block_map_update(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
1200 struct nvm_dev
*dev
= rrpc
->dev
;
1202 struct rrpc_addr
*laddr
;
1205 for (offset
= 0; offset
< dev
->pgs_per_blk
; offset
++) {
1206 paddr
= block_to_addr(rrpc
, rblk
) + offset
;
1208 pladdr
= rrpc
->rev_trans_map
[paddr
].addr
;
1209 if (pladdr
== ADDR_EMPTY
)
1212 laddr
= &rrpc
->trans_map
[pladdr
];
1214 if (paddr
== laddr
->addr
) {
1217 set_bit(offset
, rblk
->invalid_pages
);
1218 rblk
->nr_invalid_pages
++;
1223 static int rrpc_blocks_init(struct rrpc
*rrpc
)
1225 struct rrpc_lun
*rlun
;
1226 struct rrpc_block
*rblk
;
1227 int lun_iter
, blk_iter
;
1229 for (lun_iter
= 0; lun_iter
< rrpc
->nr_luns
; lun_iter
++) {
1230 rlun
= &rrpc
->luns
[lun_iter
];
1232 for (blk_iter
= 0; blk_iter
< rrpc
->dev
->blks_per_lun
;
1234 rblk
= &rlun
->blocks
[blk_iter
];
1235 rrpc_block_map_update(rrpc
, rblk
);
1242 static int rrpc_luns_configure(struct rrpc
*rrpc
)
1244 struct rrpc_lun
*rlun
;
1245 struct rrpc_block
*rblk
;
1248 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
1249 rlun
= &rrpc
->luns
[i
];
1251 rblk
= rrpc_get_blk(rrpc
, rlun
, 0);
1255 rrpc_set_lun_cur(rlun
, rblk
);
1257 /* Emergency gc block */
1258 rblk
= rrpc_get_blk(rrpc
, rlun
, 1);
1261 rlun
->gc_cur
= rblk
;
1266 rrpc_put_blks(rrpc
);
1270 static struct nvm_tgt_type tt_rrpc
;
1272 static void *rrpc_init(struct nvm_dev
*dev
, struct gendisk
*tdisk
,
1273 int lun_begin
, int lun_end
)
1275 struct request_queue
*bqueue
= dev
->q
;
1276 struct request_queue
*tqueue
= tdisk
->queue
;
1280 if (!(dev
->identity
.dom
& NVM_RSP_L2P
)) {
1281 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1283 return ERR_PTR(-EINVAL
);
1286 rrpc
= kzalloc(sizeof(struct rrpc
), GFP_KERNEL
);
1288 return ERR_PTR(-ENOMEM
);
1290 rrpc
->instance
.tt
= &tt_rrpc
;
1294 bio_list_init(&rrpc
->requeue_bios
);
1295 spin_lock_init(&rrpc
->bio_lock
);
1296 INIT_WORK(&rrpc
->ws_requeue
, rrpc_requeue
);
1298 rrpc
->nr_luns
= lun_end
- lun_begin
+ 1;
1300 /* simple round-robin strategy */
1301 atomic_set(&rrpc
->next_lun
, -1);
1303 ret
= rrpc_luns_init(rrpc
, lun_begin
, lun_end
);
1305 pr_err("nvm: rrpc: could not initialize luns\n");
1309 rrpc
->poffset
= dev
->sec_per_lun
* lun_begin
;
1310 rrpc
->lun_offset
= lun_begin
;
1312 ret
= rrpc_core_init(rrpc
);
1314 pr_err("nvm: rrpc: could not initialize core\n");
1318 ret
= rrpc_map_init(rrpc
);
1320 pr_err("nvm: rrpc: could not initialize maps\n");
1324 ret
= rrpc_blocks_init(rrpc
);
1326 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1330 ret
= rrpc_luns_configure(rrpc
);
1332 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1336 ret
= rrpc_gc_init(rrpc
);
1338 pr_err("nvm: rrpc: could not initialize gc\n");
1342 /* inherit the size from the underlying device */
1343 blk_queue_logical_block_size(tqueue
, queue_physical_block_size(bqueue
));
1344 blk_queue_max_hw_sectors(tqueue
, queue_max_hw_sectors(bqueue
));
1346 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
1347 rrpc
->nr_luns
, (unsigned long long)rrpc
->nr_pages
);
1349 mod_timer(&rrpc
->gc_timer
, jiffies
+ msecs_to_jiffies(10));
1354 return ERR_PTR(ret
);
1357 /* round robin, page-based FTL, and cost-based GC */
1358 static struct nvm_tgt_type tt_rrpc
= {
1360 .version
= {1, 0, 0},
1362 .make_rq
= rrpc_make_rq
,
1363 .capacity
= rrpc_capacity
,
1364 .end_io
= rrpc_end_io
,
1370 static int __init
rrpc_module_init(void)
1372 return nvm_register_target(&tt_rrpc
);
1375 static void rrpc_module_exit(void)
1377 nvm_unregister_target(&tt_rrpc
);
1380 module_init(rrpc_module_init
);
1381 module_exit(rrpc_module_exit
);
1382 MODULE_LICENSE("GPL v2");
1383 MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");