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
);
291 while ((slot
= find_first_zero_bit(rblk
->invalid_pages
,
292 nr_pgs_per_blk
)) < nr_pgs_per_blk
) {
295 phys_addr
= (rblk
->parent
->id
* nr_pgs_per_blk
) + slot
;
298 spin_lock(&rrpc
->rev_lock
);
299 /* Get logical address from physical to logical table */
300 rev
= &rrpc
->rev_trans_map
[phys_addr
- rrpc
->poffset
];
301 /* already updated by previous regular write */
302 if (rev
->addr
== ADDR_EMPTY
) {
303 spin_unlock(&rrpc
->rev_lock
);
307 rqd
= rrpc_inflight_laddr_acquire(rrpc
, rev
->addr
, 1);
308 if (IS_ERR_OR_NULL(rqd
)) {
309 spin_unlock(&rrpc
->rev_lock
);
314 spin_unlock(&rrpc
->rev_lock
);
316 /* Perform read to do GC */
317 bio
->bi_iter
.bi_sector
= rrpc_get_sector(rev
->addr
);
319 bio
->bi_private
= &wait
;
320 bio
->bi_end_io
= rrpc_end_sync_bio
;
322 /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
323 bio_add_pc_page(q
, bio
, page
, RRPC_EXPOSED_PAGE_SIZE
, 0);
325 if (rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_GC
)) {
326 pr_err("rrpc: gc read failed.\n");
327 rrpc_inflight_laddr_release(rrpc
, rqd
);
330 wait_for_completion_io(&wait
);
333 reinit_completion(&wait
);
335 bio
->bi_iter
.bi_sector
= rrpc_get_sector(rev
->addr
);
337 bio
->bi_private
= &wait
;
338 bio
->bi_end_io
= rrpc_end_sync_bio
;
340 bio_add_pc_page(q
, bio
, page
, RRPC_EXPOSED_PAGE_SIZE
, 0);
342 /* turn the command around and write the data back to a new
345 if (rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_GC
)) {
346 pr_err("rrpc: gc write failed.\n");
347 rrpc_inflight_laddr_release(rrpc
, rqd
);
350 wait_for_completion_io(&wait
);
352 rrpc_inflight_laddr_release(rrpc
, rqd
);
358 mempool_free(page
, rrpc
->page_pool
);
361 if (!bitmap_full(rblk
->invalid_pages
, nr_pgs_per_blk
)) {
362 pr_err("nvm: failed to garbage collect block\n");
369 static void rrpc_block_gc(struct work_struct
*work
)
371 struct rrpc_block_gc
*gcb
= container_of(work
, struct rrpc_block_gc
,
373 struct rrpc
*rrpc
= gcb
->rrpc
;
374 struct rrpc_block
*rblk
= gcb
->rblk
;
375 struct nvm_dev
*dev
= rrpc
->dev
;
377 pr_debug("nvm: block '%lu' being reclaimed\n", rblk
->parent
->id
);
379 if (rrpc_move_valid_pages(rrpc
, rblk
))
382 nvm_erase_blk(dev
, rblk
->parent
);
383 rrpc_put_blk(rrpc
, rblk
);
385 mempool_free(gcb
, rrpc
->gcb_pool
);
388 /* the block with highest number of invalid pages, will be in the beginning
391 static struct rrpc_block
*rblock_max_invalid(struct rrpc_block
*ra
,
392 struct rrpc_block
*rb
)
394 if (ra
->nr_invalid_pages
== rb
->nr_invalid_pages
)
397 return (ra
->nr_invalid_pages
< rb
->nr_invalid_pages
) ? rb
: ra
;
400 /* linearly find the block with highest number of invalid pages
403 static struct rrpc_block
*block_prio_find_max(struct rrpc_lun
*rlun
)
405 struct list_head
*prio_list
= &rlun
->prio_list
;
406 struct rrpc_block
*rblock
, *max
;
408 BUG_ON(list_empty(prio_list
));
410 max
= list_first_entry(prio_list
, struct rrpc_block
, prio
);
411 list_for_each_entry(rblock
, prio_list
, prio
)
412 max
= rblock_max_invalid(max
, rblock
);
417 static void rrpc_lun_gc(struct work_struct
*work
)
419 struct rrpc_lun
*rlun
= container_of(work
, struct rrpc_lun
, ws_gc
);
420 struct rrpc
*rrpc
= rlun
->rrpc
;
421 struct nvm_lun
*lun
= rlun
->parent
;
422 struct rrpc_block_gc
*gcb
;
423 unsigned int nr_blocks_need
;
425 nr_blocks_need
= rrpc
->dev
->blks_per_lun
/ GC_LIMIT_INVERSE
;
427 if (nr_blocks_need
< rrpc
->nr_luns
)
428 nr_blocks_need
= rrpc
->nr_luns
;
430 spin_lock(&lun
->lock
);
431 while (nr_blocks_need
> lun
->nr_free_blocks
&&
432 !list_empty(&rlun
->prio_list
)) {
433 struct rrpc_block
*rblock
= block_prio_find_max(rlun
);
434 struct nvm_block
*block
= rblock
->parent
;
436 if (!rblock
->nr_invalid_pages
)
439 list_del_init(&rblock
->prio
);
441 BUG_ON(!block_is_full(rrpc
, rblock
));
443 pr_debug("rrpc: selected block '%lu' for GC\n", block
->id
);
445 gcb
= mempool_alloc(rrpc
->gcb_pool
, GFP_ATOMIC
);
451 INIT_WORK(&gcb
->ws_gc
, rrpc_block_gc
);
453 queue_work(rrpc
->kgc_wq
, &gcb
->ws_gc
);
457 spin_unlock(&lun
->lock
);
459 /* TODO: Hint that request queue can be started again */
462 static void rrpc_gc_queue(struct work_struct
*work
)
464 struct rrpc_block_gc
*gcb
= container_of(work
, struct rrpc_block_gc
,
466 struct rrpc
*rrpc
= gcb
->rrpc
;
467 struct rrpc_block
*rblk
= gcb
->rblk
;
468 struct nvm_lun
*lun
= rblk
->parent
->lun
;
469 struct rrpc_lun
*rlun
= &rrpc
->luns
[lun
->id
- rrpc
->lun_offset
];
471 spin_lock(&rlun
->lock
);
472 list_add_tail(&rblk
->prio
, &rlun
->prio_list
);
473 spin_unlock(&rlun
->lock
);
475 mempool_free(gcb
, rrpc
->gcb_pool
);
476 pr_debug("nvm: block '%lu' is full, allow GC (sched)\n",
480 static const struct block_device_operations rrpc_fops
= {
481 .owner
= THIS_MODULE
,
484 static struct rrpc_lun
*rrpc_get_lun_rr(struct rrpc
*rrpc
, int is_gc
)
487 struct rrpc_lun
*rlun
, *max_free
;
490 return get_next_lun(rrpc
);
492 /* during GC, we don't care about RR, instead we want to make
493 * sure that we maintain evenness between the block luns.
495 max_free
= &rrpc
->luns
[0];
496 /* prevent GC-ing lun from devouring pages of a lun with
497 * little free blocks. We don't take the lock as we only need an
500 rrpc_for_each_lun(rrpc
, rlun
, i
) {
501 if (rlun
->parent
->nr_free_blocks
>
502 max_free
->parent
->nr_free_blocks
)
509 static struct rrpc_addr
*rrpc_update_map(struct rrpc
*rrpc
, sector_t laddr
,
510 struct rrpc_block
*rblk
, u64 paddr
)
512 struct rrpc_addr
*gp
;
513 struct rrpc_rev_addr
*rev
;
515 BUG_ON(laddr
>= rrpc
->nr_pages
);
517 gp
= &rrpc
->trans_map
[laddr
];
518 spin_lock(&rrpc
->rev_lock
);
520 rrpc_page_invalidate(rrpc
, gp
);
525 rev
= &rrpc
->rev_trans_map
[gp
->addr
- rrpc
->poffset
];
527 spin_unlock(&rrpc
->rev_lock
);
532 static u64
rrpc_alloc_addr(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
534 u64 addr
= ADDR_EMPTY
;
536 spin_lock(&rblk
->lock
);
537 if (block_is_full(rrpc
, rblk
))
540 addr
= block_to_addr(rrpc
, rblk
) + rblk
->next_page
;
544 spin_unlock(&rblk
->lock
);
548 /* Simple round-robin Logical to physical address translation.
550 * Retrieve the mapping using the active append point. Then update the ap for
551 * the next write to the disk.
553 * Returns rrpc_addr with the physical address and block. Remember to return to
554 * rrpc->addr_cache when request is finished.
556 static struct rrpc_addr
*rrpc_map_page(struct rrpc
*rrpc
, sector_t laddr
,
559 struct rrpc_lun
*rlun
;
560 struct rrpc_block
*rblk
;
564 rlun
= rrpc_get_lun_rr(rrpc
, is_gc
);
567 if (!is_gc
&& lun
->nr_free_blocks
< rrpc
->nr_luns
* 4)
570 spin_lock(&rlun
->lock
);
574 paddr
= rrpc_alloc_addr(rrpc
, rblk
);
576 if (paddr
== ADDR_EMPTY
) {
577 rblk
= rrpc_get_blk(rrpc
, rlun
, 0);
579 rrpc_set_lun_cur(rlun
, rblk
);
584 /* retry from emergency gc block */
585 paddr
= rrpc_alloc_addr(rrpc
, rlun
->gc_cur
);
586 if (paddr
== ADDR_EMPTY
) {
587 rblk
= rrpc_get_blk(rrpc
, rlun
, 1);
589 pr_err("rrpc: no more blocks");
594 paddr
= rrpc_alloc_addr(rrpc
, rlun
->gc_cur
);
600 spin_unlock(&rlun
->lock
);
601 return rrpc_update_map(rrpc
, laddr
, rblk
, paddr
);
603 spin_unlock(&rlun
->lock
);
607 static void rrpc_run_gc(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
609 struct rrpc_block_gc
*gcb
;
611 gcb
= mempool_alloc(rrpc
->gcb_pool
, GFP_ATOMIC
);
613 pr_err("rrpc: unable to queue block for gc.");
620 INIT_WORK(&gcb
->ws_gc
, rrpc_gc_queue
);
621 queue_work(rrpc
->kgc_wq
, &gcb
->ws_gc
);
624 static void rrpc_end_io_write(struct rrpc
*rrpc
, struct rrpc_rq
*rrqd
,
625 sector_t laddr
, uint8_t npages
)
628 struct rrpc_block
*rblk
;
632 for (i
= 0; i
< npages
; i
++) {
633 p
= &rrpc
->trans_map
[laddr
+ i
];
635 lun
= rblk
->parent
->lun
;
637 cmnt_size
= atomic_inc_return(&rblk
->data_cmnt_size
);
638 if (unlikely(cmnt_size
== rrpc
->dev
->pgs_per_blk
))
639 rrpc_run_gc(rrpc
, rblk
);
643 static int rrpc_end_io(struct nvm_rq
*rqd
, int error
)
645 struct rrpc
*rrpc
= container_of(rqd
->ins
, struct rrpc
, instance
);
646 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
647 uint8_t npages
= rqd
->nr_pages
;
648 sector_t laddr
= rrpc_get_laddr(rqd
->bio
) - npages
;
650 if (bio_data_dir(rqd
->bio
) == WRITE
)
651 rrpc_end_io_write(rrpc
, rrqd
, laddr
, npages
);
653 if (rrqd
->flags
& NVM_IOTYPE_GC
)
656 rrpc_unlock_rq(rrpc
, rqd
);
660 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
662 nvm_dev_dma_free(rrpc
->dev
, rqd
->metadata
, rqd
->dma_metadata
);
664 mempool_free(rqd
, rrpc
->rq_pool
);
669 static int rrpc_read_ppalist_rq(struct rrpc
*rrpc
, struct bio
*bio
,
670 struct nvm_rq
*rqd
, unsigned long flags
, int npages
)
672 struct rrpc_inflight_rq
*r
= rrpc_get_inflight_rq(rqd
);
673 struct rrpc_addr
*gp
;
674 sector_t laddr
= rrpc_get_laddr(bio
);
675 int is_gc
= flags
& NVM_IOTYPE_GC
;
678 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
)) {
679 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
680 return NVM_IO_REQUEUE
;
683 for (i
= 0; i
< npages
; i
++) {
684 /* We assume that mapping occurs at 4KB granularity */
685 BUG_ON(!(laddr
+ i
>= 0 && laddr
+ i
< rrpc
->nr_pages
));
686 gp
= &rrpc
->trans_map
[laddr
+ i
];
689 rqd
->ppa_list
[i
] = rrpc_ppa_to_gaddr(rrpc
->dev
,
693 rrpc_unlock_laddr(rrpc
, r
);
694 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
,
700 rqd
->opcode
= NVM_OP_HBREAD
;
705 static int rrpc_read_rq(struct rrpc
*rrpc
, struct bio
*bio
, struct nvm_rq
*rqd
,
708 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
709 int is_gc
= flags
& NVM_IOTYPE_GC
;
710 sector_t laddr
= rrpc_get_laddr(bio
);
711 struct rrpc_addr
*gp
;
713 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
))
714 return NVM_IO_REQUEUE
;
716 BUG_ON(!(laddr
>= 0 && laddr
< rrpc
->nr_pages
));
717 gp
= &rrpc
->trans_map
[laddr
];
720 rqd
->ppa_addr
= rrpc_ppa_to_gaddr(rrpc
->dev
, gp
->addr
);
723 rrpc_unlock_rq(rrpc
, rqd
);
727 rqd
->opcode
= NVM_OP_HBREAD
;
733 static int rrpc_write_ppalist_rq(struct rrpc
*rrpc
, struct bio
*bio
,
734 struct nvm_rq
*rqd
, unsigned long flags
, int npages
)
736 struct rrpc_inflight_rq
*r
= rrpc_get_inflight_rq(rqd
);
738 sector_t laddr
= rrpc_get_laddr(bio
);
739 int is_gc
= flags
& NVM_IOTYPE_GC
;
742 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
)) {
743 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
744 return NVM_IO_REQUEUE
;
747 for (i
= 0; i
< npages
; i
++) {
748 /* We assume that mapping occurs at 4KB granularity */
749 p
= rrpc_map_page(rrpc
, laddr
+ i
, is_gc
);
752 rrpc_unlock_laddr(rrpc
, r
);
753 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
,
756 return NVM_IO_REQUEUE
;
759 rqd
->ppa_list
[i
] = rrpc_ppa_to_gaddr(rrpc
->dev
,
763 rqd
->opcode
= NVM_OP_HBWRITE
;
768 static int rrpc_write_rq(struct rrpc
*rrpc
, struct bio
*bio
,
769 struct nvm_rq
*rqd
, unsigned long flags
)
771 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
773 int is_gc
= flags
& NVM_IOTYPE_GC
;
774 sector_t laddr
= rrpc_get_laddr(bio
);
776 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
))
777 return NVM_IO_REQUEUE
;
779 p
= rrpc_map_page(rrpc
, laddr
, is_gc
);
782 rrpc_unlock_rq(rrpc
, rqd
);
784 return NVM_IO_REQUEUE
;
787 rqd
->ppa_addr
= rrpc_ppa_to_gaddr(rrpc
->dev
, p
->addr
);
788 rqd
->opcode
= NVM_OP_HBWRITE
;
794 static int rrpc_setup_rq(struct rrpc
*rrpc
, struct bio
*bio
,
795 struct nvm_rq
*rqd
, unsigned long flags
, uint8_t npages
)
798 rqd
->ppa_list
= nvm_dev_dma_alloc(rrpc
->dev
, GFP_KERNEL
,
800 if (!rqd
->ppa_list
) {
801 pr_err("rrpc: not able to allocate ppa list\n");
805 if (bio_rw(bio
) == WRITE
)
806 return rrpc_write_ppalist_rq(rrpc
, bio
, rqd
, flags
,
809 return rrpc_read_ppalist_rq(rrpc
, bio
, rqd
, flags
, npages
);
812 if (bio_rw(bio
) == WRITE
)
813 return rrpc_write_rq(rrpc
, bio
, rqd
, flags
);
815 return rrpc_read_rq(rrpc
, bio
, rqd
, flags
);
818 static int rrpc_submit_io(struct rrpc
*rrpc
, struct bio
*bio
,
819 struct nvm_rq
*rqd
, unsigned long flags
)
822 struct rrpc_rq
*rrq
= nvm_rq_to_pdu(rqd
);
823 uint8_t nr_pages
= rrpc_get_pages(bio
);
824 int bio_size
= bio_sectors(bio
) << 9;
826 if (bio_size
< rrpc
->dev
->sec_size
)
828 else if (bio_size
> rrpc
->dev
->max_rq_size
)
831 err
= rrpc_setup_rq(rrpc
, bio
, rqd
, flags
, nr_pages
);
837 rqd
->ins
= &rrpc
->instance
;
838 rqd
->nr_pages
= nr_pages
;
841 err
= nvm_submit_io(rrpc
->dev
, rqd
);
843 pr_err("rrpc: I/O submission failed: %d\n", err
);
850 static blk_qc_t
rrpc_make_rq(struct request_queue
*q
, struct bio
*bio
)
852 struct rrpc
*rrpc
= q
->queuedata
;
856 if (bio
->bi_rw
& REQ_DISCARD
) {
857 rrpc_discard(rrpc
, bio
);
858 return BLK_QC_T_NONE
;
861 rqd
= mempool_alloc(rrpc
->rq_pool
, GFP_KERNEL
);
863 pr_err_ratelimited("rrpc: not able to queue bio.");
865 return BLK_QC_T_NONE
;
867 memset(rqd
, 0, sizeof(struct nvm_rq
));
869 err
= rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_NONE
);
872 return BLK_QC_T_NONE
;
880 spin_lock(&rrpc
->bio_lock
);
881 bio_list_add(&rrpc
->requeue_bios
, bio
);
882 spin_unlock(&rrpc
->bio_lock
);
883 queue_work(rrpc
->kgc_wq
, &rrpc
->ws_requeue
);
887 mempool_free(rqd
, rrpc
->rq_pool
);
888 return BLK_QC_T_NONE
;
891 static void rrpc_requeue(struct work_struct
*work
)
893 struct rrpc
*rrpc
= container_of(work
, struct rrpc
, ws_requeue
);
894 struct bio_list bios
;
897 bio_list_init(&bios
);
899 spin_lock(&rrpc
->bio_lock
);
900 bio_list_merge(&bios
, &rrpc
->requeue_bios
);
901 bio_list_init(&rrpc
->requeue_bios
);
902 spin_unlock(&rrpc
->bio_lock
);
904 while ((bio
= bio_list_pop(&bios
)))
905 rrpc_make_rq(rrpc
->disk
->queue
, bio
);
908 static void rrpc_gc_free(struct rrpc
*rrpc
)
910 struct rrpc_lun
*rlun
;
914 destroy_workqueue(rrpc
->krqd_wq
);
917 destroy_workqueue(rrpc
->kgc_wq
);
922 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
923 rlun
= &rrpc
->luns
[i
];
931 static int rrpc_gc_init(struct rrpc
*rrpc
)
933 rrpc
->krqd_wq
= alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM
|WQ_UNBOUND
,
938 rrpc
->kgc_wq
= alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM
, 1);
942 setup_timer(&rrpc
->gc_timer
, rrpc_gc_timer
, (unsigned long)rrpc
);
947 static void rrpc_map_free(struct rrpc
*rrpc
)
949 vfree(rrpc
->rev_trans_map
);
950 vfree(rrpc
->trans_map
);
953 static int rrpc_l2p_update(u64 slba
, u32 nlb
, __le64
*entries
, void *private)
955 struct rrpc
*rrpc
= (struct rrpc
*)private;
956 struct nvm_dev
*dev
= rrpc
->dev
;
957 struct rrpc_addr
*addr
= rrpc
->trans_map
+ slba
;
958 struct rrpc_rev_addr
*raddr
= rrpc
->rev_trans_map
;
959 sector_t max_pages
= dev
->total_pages
* (dev
->sec_size
>> 9);
960 u64 elba
= slba
+ nlb
;
963 if (unlikely(elba
> dev
->total_pages
)) {
964 pr_err("nvm: L2P data from device is out of bounds!\n");
968 for (i
= 0; i
< nlb
; i
++) {
969 u64 pba
= le64_to_cpu(entries
[i
]);
970 /* LNVM treats address-spaces as silos, LBA and PBA are
971 * equally large and zero-indexed.
973 if (unlikely(pba
>= max_pages
&& pba
!= U64_MAX
)) {
974 pr_err("nvm: L2P data entry is out of bounds!\n");
978 /* Address zero is a special one. The first page on a disk is
979 * protected. As it often holds internal device boot
986 raddr
[pba
].addr
= slba
+ i
;
992 static int rrpc_map_init(struct rrpc
*rrpc
)
994 struct nvm_dev
*dev
= rrpc
->dev
;
998 rrpc
->trans_map
= vzalloc(sizeof(struct rrpc_addr
) * rrpc
->nr_pages
);
999 if (!rrpc
->trans_map
)
1002 rrpc
->rev_trans_map
= vmalloc(sizeof(struct rrpc_rev_addr
)
1004 if (!rrpc
->rev_trans_map
)
1007 for (i
= 0; i
< rrpc
->nr_pages
; i
++) {
1008 struct rrpc_addr
*p
= &rrpc
->trans_map
[i
];
1009 struct rrpc_rev_addr
*r
= &rrpc
->rev_trans_map
[i
];
1011 p
->addr
= ADDR_EMPTY
;
1012 r
->addr
= ADDR_EMPTY
;
1015 if (!dev
->ops
->get_l2p_tbl
)
1018 /* Bring up the mapping table from device */
1019 ret
= dev
->ops
->get_l2p_tbl(dev
, 0, dev
->total_pages
,
1020 rrpc_l2p_update
, rrpc
);
1022 pr_err("nvm: rrpc: could not read L2P table.\n");
1030 /* Minimum pages needed within a lun */
1031 #define PAGE_POOL_SIZE 16
1032 #define ADDR_POOL_SIZE 64
1034 static int rrpc_core_init(struct rrpc
*rrpc
)
1036 down_write(&rrpc_lock
);
1037 if (!rrpc_gcb_cache
) {
1038 rrpc_gcb_cache
= kmem_cache_create("rrpc_gcb",
1039 sizeof(struct rrpc_block_gc
), 0, 0, NULL
);
1040 if (!rrpc_gcb_cache
) {
1041 up_write(&rrpc_lock
);
1045 rrpc_rq_cache
= kmem_cache_create("rrpc_rq",
1046 sizeof(struct nvm_rq
) + sizeof(struct rrpc_rq
),
1048 if (!rrpc_rq_cache
) {
1049 kmem_cache_destroy(rrpc_gcb_cache
);
1050 up_write(&rrpc_lock
);
1054 up_write(&rrpc_lock
);
1056 rrpc
->page_pool
= mempool_create_page_pool(PAGE_POOL_SIZE
, 0);
1057 if (!rrpc
->page_pool
)
1060 rrpc
->gcb_pool
= mempool_create_slab_pool(rrpc
->dev
->nr_luns
,
1062 if (!rrpc
->gcb_pool
)
1065 rrpc
->rq_pool
= mempool_create_slab_pool(64, rrpc_rq_cache
);
1069 spin_lock_init(&rrpc
->inflights
.lock
);
1070 INIT_LIST_HEAD(&rrpc
->inflights
.reqs
);
1075 static void rrpc_core_free(struct rrpc
*rrpc
)
1077 mempool_destroy(rrpc
->page_pool
);
1078 mempool_destroy(rrpc
->gcb_pool
);
1079 mempool_destroy(rrpc
->rq_pool
);
1082 static void rrpc_luns_free(struct rrpc
*rrpc
)
1087 static int rrpc_luns_init(struct rrpc
*rrpc
, int lun_begin
, int lun_end
)
1089 struct nvm_dev
*dev
= rrpc
->dev
;
1090 struct rrpc_lun
*rlun
;
1093 spin_lock_init(&rrpc
->rev_lock
);
1095 rrpc
->luns
= kcalloc(rrpc
->nr_luns
, sizeof(struct rrpc_lun
),
1101 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
1102 struct nvm_lun
*lun
= dev
->mt
->get_lun(dev
, lun_begin
+ i
);
1104 if (dev
->pgs_per_blk
>
1105 MAX_INVALID_PAGES_STORAGE
* BITS_PER_LONG
) {
1106 pr_err("rrpc: number of pages per block too high.");
1110 rlun
= &rrpc
->luns
[i
];
1113 INIT_LIST_HEAD(&rlun
->prio_list
);
1114 INIT_WORK(&rlun
->ws_gc
, rrpc_lun_gc
);
1115 spin_lock_init(&rlun
->lock
);
1117 rrpc
->total_blocks
+= dev
->blks_per_lun
;
1118 rrpc
->nr_pages
+= dev
->sec_per_lun
;
1120 rlun
->blocks
= vzalloc(sizeof(struct rrpc_block
) *
1121 rrpc
->dev
->blks_per_lun
);
1125 for (j
= 0; j
< rrpc
->dev
->blks_per_lun
; j
++) {
1126 struct rrpc_block
*rblk
= &rlun
->blocks
[j
];
1127 struct nvm_block
*blk
= &lun
->blocks
[j
];
1130 INIT_LIST_HEAD(&rblk
->prio
);
1131 spin_lock_init(&rblk
->lock
);
1140 static void rrpc_free(struct rrpc
*rrpc
)
1143 rrpc_map_free(rrpc
);
1144 rrpc_core_free(rrpc
);
1145 rrpc_luns_free(rrpc
);
1150 static void rrpc_exit(void *private)
1152 struct rrpc
*rrpc
= private;
1154 del_timer(&rrpc
->gc_timer
);
1156 flush_workqueue(rrpc
->krqd_wq
);
1157 flush_workqueue(rrpc
->kgc_wq
);
1162 static sector_t
rrpc_capacity(void *private)
1164 struct rrpc
*rrpc
= private;
1165 struct nvm_dev
*dev
= rrpc
->dev
;
1166 sector_t reserved
, provisioned
;
1168 /* cur, gc, and two emergency blocks for each lun */
1169 reserved
= rrpc
->nr_luns
* dev
->max_pages_per_blk
* 4;
1170 provisioned
= rrpc
->nr_pages
- reserved
;
1172 if (reserved
> rrpc
->nr_pages
) {
1173 pr_err("rrpc: not enough space available to expose storage.\n");
1177 sector_div(provisioned
, 10);
1178 return provisioned
* 9 * NR_PHY_IN_LOG
;
1182 * Looks up the logical address from reverse trans map and check if its valid by
1183 * comparing the logical to physical address with the physical address.
1184 * Returns 0 on free, otherwise 1 if in use
1186 static void rrpc_block_map_update(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
1188 struct nvm_dev
*dev
= rrpc
->dev
;
1190 struct rrpc_addr
*laddr
;
1193 for (offset
= 0; offset
< dev
->pgs_per_blk
; offset
++) {
1194 paddr
= block_to_addr(rrpc
, rblk
) + offset
;
1196 pladdr
= rrpc
->rev_trans_map
[paddr
].addr
;
1197 if (pladdr
== ADDR_EMPTY
)
1200 laddr
= &rrpc
->trans_map
[pladdr
];
1202 if (paddr
== laddr
->addr
) {
1205 set_bit(offset
, rblk
->invalid_pages
);
1206 rblk
->nr_invalid_pages
++;
1211 static int rrpc_blocks_init(struct rrpc
*rrpc
)
1213 struct rrpc_lun
*rlun
;
1214 struct rrpc_block
*rblk
;
1215 int lun_iter
, blk_iter
;
1217 for (lun_iter
= 0; lun_iter
< rrpc
->nr_luns
; lun_iter
++) {
1218 rlun
= &rrpc
->luns
[lun_iter
];
1220 for (blk_iter
= 0; blk_iter
< rrpc
->dev
->blks_per_lun
;
1222 rblk
= &rlun
->blocks
[blk_iter
];
1223 rrpc_block_map_update(rrpc
, rblk
);
1230 static int rrpc_luns_configure(struct rrpc
*rrpc
)
1232 struct rrpc_lun
*rlun
;
1233 struct rrpc_block
*rblk
;
1236 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
1237 rlun
= &rrpc
->luns
[i
];
1239 rblk
= rrpc_get_blk(rrpc
, rlun
, 0);
1243 rrpc_set_lun_cur(rlun
, rblk
);
1245 /* Emergency gc block */
1246 rblk
= rrpc_get_blk(rrpc
, rlun
, 1);
1249 rlun
->gc_cur
= rblk
;
1254 rrpc_put_blks(rrpc
);
1258 static struct nvm_tgt_type tt_rrpc
;
1260 static void *rrpc_init(struct nvm_dev
*dev
, struct gendisk
*tdisk
,
1261 int lun_begin
, int lun_end
)
1263 struct request_queue
*bqueue
= dev
->q
;
1264 struct request_queue
*tqueue
= tdisk
->queue
;
1268 if (!(dev
->identity
.dom
& NVM_RSP_L2P
)) {
1269 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1271 return ERR_PTR(-EINVAL
);
1274 rrpc
= kzalloc(sizeof(struct rrpc
), GFP_KERNEL
);
1276 return ERR_PTR(-ENOMEM
);
1278 rrpc
->instance
.tt
= &tt_rrpc
;
1282 bio_list_init(&rrpc
->requeue_bios
);
1283 spin_lock_init(&rrpc
->bio_lock
);
1284 INIT_WORK(&rrpc
->ws_requeue
, rrpc_requeue
);
1286 rrpc
->nr_luns
= lun_end
- lun_begin
+ 1;
1288 /* simple round-robin strategy */
1289 atomic_set(&rrpc
->next_lun
, -1);
1291 ret
= rrpc_luns_init(rrpc
, lun_begin
, lun_end
);
1293 pr_err("nvm: rrpc: could not initialize luns\n");
1297 rrpc
->poffset
= dev
->sec_per_lun
* lun_begin
;
1298 rrpc
->lun_offset
= lun_begin
;
1300 ret
= rrpc_core_init(rrpc
);
1302 pr_err("nvm: rrpc: could not initialize core\n");
1306 ret
= rrpc_map_init(rrpc
);
1308 pr_err("nvm: rrpc: could not initialize maps\n");
1312 ret
= rrpc_blocks_init(rrpc
);
1314 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1318 ret
= rrpc_luns_configure(rrpc
);
1320 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1324 ret
= rrpc_gc_init(rrpc
);
1326 pr_err("nvm: rrpc: could not initialize gc\n");
1330 /* inherit the size from the underlying device */
1331 blk_queue_logical_block_size(tqueue
, queue_physical_block_size(bqueue
));
1332 blk_queue_max_hw_sectors(tqueue
, queue_max_hw_sectors(bqueue
));
1334 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
1335 rrpc
->nr_luns
, (unsigned long long)rrpc
->nr_pages
);
1337 mod_timer(&rrpc
->gc_timer
, jiffies
+ msecs_to_jiffies(10));
1342 return ERR_PTR(ret
);
1345 /* round robin, page-based FTL, and cost-based GC */
1346 static struct nvm_tgt_type tt_rrpc
= {
1348 .version
= {1, 0, 0},
1350 .make_rq
= rrpc_make_rq
,
1351 .capacity
= rrpc_capacity
,
1352 .end_io
= rrpc_end_io
,
1358 static int __init
rrpc_module_init(void)
1360 return nvm_register_target(&tt_rrpc
);
1363 static void rrpc_module_exit(void)
1365 nvm_unregister_target(&tt_rrpc
);
1368 module_init(rrpc_module_init
);
1369 module_exit(rrpc_module_exit
);
1370 MODULE_LICENSE("GPL v2");
1371 MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");