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
rrpc_ppa_to_gaddr(struct nvm_dev
*dev
, u64 addr
)
128 struct ppa_addr paddr
;
131 return __linear_to_generic_addr(dev
, paddr
);
134 /* requires lun->lock taken */
135 static void rrpc_set_lun_cur(struct rrpc_lun
*rlun
, struct rrpc_block
*rblk
)
137 struct rrpc
*rrpc
= rlun
->rrpc
;
142 spin_lock(&rlun
->cur
->lock
);
143 WARN_ON(!block_is_full(rrpc
, rlun
->cur
));
144 spin_unlock(&rlun
->cur
->lock
);
149 static struct rrpc_block
*rrpc_get_blk(struct rrpc
*rrpc
, struct rrpc_lun
*rlun
,
152 struct nvm_block
*blk
;
153 struct rrpc_block
*rblk
;
155 blk
= nvm_get_blk(rrpc
->dev
, rlun
->parent
, 0);
159 rblk
= &rlun
->blocks
[blk
->id
];
162 bitmap_zero(rblk
->invalid_pages
, rrpc
->dev
->pgs_per_blk
);
164 rblk
->nr_invalid_pages
= 0;
165 atomic_set(&rblk
->data_cmnt_size
, 0);
170 static void rrpc_put_blk(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
172 nvm_put_blk(rrpc
->dev
, rblk
->parent
);
175 static struct rrpc_lun
*get_next_lun(struct rrpc
*rrpc
)
177 int next
= atomic_inc_return(&rrpc
->next_lun
);
179 return &rrpc
->luns
[next
% rrpc
->nr_luns
];
182 static void rrpc_gc_kick(struct rrpc
*rrpc
)
184 struct rrpc_lun
*rlun
;
187 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
188 rlun
= &rrpc
->luns
[i
];
189 queue_work(rrpc
->krqd_wq
, &rlun
->ws_gc
);
194 * timed GC every interval.
196 static void rrpc_gc_timer(unsigned long data
)
198 struct rrpc
*rrpc
= (struct rrpc
*)data
;
201 mod_timer(&rrpc
->gc_timer
, jiffies
+ msecs_to_jiffies(10));
204 static void rrpc_end_sync_bio(struct bio
*bio
)
206 struct completion
*waiting
= bio
->bi_private
;
209 pr_err("nvm: gc request failed (%u).\n", bio
->bi_error
);
215 * rrpc_move_valid_pages -- migrate live data off the block
216 * @rrpc: the 'rrpc' structure
217 * @block: the block from which to migrate live pages
220 * GC algorithms may call this function to migrate remaining live
221 * pages off the block prior to erasing it. This function blocks
222 * further execution until the operation is complete.
224 static int rrpc_move_valid_pages(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
226 struct request_queue
*q
= rrpc
->dev
->q
;
227 struct rrpc_rev_addr
*rev
;
232 int nr_pgs_per_blk
= rrpc
->dev
->pgs_per_blk
;
234 DECLARE_COMPLETION_ONSTACK(wait
);
236 if (bitmap_full(rblk
->invalid_pages
, nr_pgs_per_blk
))
239 bio
= bio_alloc(GFP_NOIO
, 1);
241 pr_err("nvm: could not alloc bio to gc\n");
245 page
= mempool_alloc(rrpc
->page_pool
, GFP_NOIO
);
247 while ((slot
= find_first_zero_bit(rblk
->invalid_pages
,
248 nr_pgs_per_blk
)) < nr_pgs_per_blk
) {
251 phys_addr
= (rblk
->parent
->id
* nr_pgs_per_blk
) + slot
;
254 spin_lock(&rrpc
->rev_lock
);
255 /* Get logical address from physical to logical table */
256 rev
= &rrpc
->rev_trans_map
[phys_addr
- rrpc
->poffset
];
257 /* already updated by previous regular write */
258 if (rev
->addr
== ADDR_EMPTY
) {
259 spin_unlock(&rrpc
->rev_lock
);
263 rqd
= rrpc_inflight_laddr_acquire(rrpc
, rev
->addr
, 1);
264 if (IS_ERR_OR_NULL(rqd
)) {
265 spin_unlock(&rrpc
->rev_lock
);
270 spin_unlock(&rrpc
->rev_lock
);
272 /* Perform read to do GC */
273 bio
->bi_iter
.bi_sector
= rrpc_get_sector(rev
->addr
);
275 bio
->bi_private
= &wait
;
276 bio
->bi_end_io
= rrpc_end_sync_bio
;
278 /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
279 bio_add_pc_page(q
, bio
, page
, RRPC_EXPOSED_PAGE_SIZE
, 0);
281 if (rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_GC
)) {
282 pr_err("rrpc: gc read failed.\n");
283 rrpc_inflight_laddr_release(rrpc
, rqd
);
286 wait_for_completion_io(&wait
);
289 reinit_completion(&wait
);
291 bio
->bi_iter
.bi_sector
= rrpc_get_sector(rev
->addr
);
293 bio
->bi_private
= &wait
;
294 bio
->bi_end_io
= rrpc_end_sync_bio
;
296 bio_add_pc_page(q
, bio
, page
, RRPC_EXPOSED_PAGE_SIZE
, 0);
298 /* turn the command around and write the data back to a new
301 if (rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_GC
)) {
302 pr_err("rrpc: gc write failed.\n");
303 rrpc_inflight_laddr_release(rrpc
, rqd
);
306 wait_for_completion_io(&wait
);
308 rrpc_inflight_laddr_release(rrpc
, rqd
);
314 mempool_free(page
, rrpc
->page_pool
);
317 if (!bitmap_full(rblk
->invalid_pages
, nr_pgs_per_blk
)) {
318 pr_err("nvm: failed to garbage collect block\n");
325 static void rrpc_block_gc(struct work_struct
*work
)
327 struct rrpc_block_gc
*gcb
= container_of(work
, struct rrpc_block_gc
,
329 struct rrpc
*rrpc
= gcb
->rrpc
;
330 struct rrpc_block
*rblk
= gcb
->rblk
;
331 struct nvm_dev
*dev
= rrpc
->dev
;
333 pr_debug("nvm: block '%lu' being reclaimed\n", rblk
->parent
->id
);
335 if (rrpc_move_valid_pages(rrpc
, rblk
))
338 nvm_erase_blk(dev
, rblk
->parent
);
339 rrpc_put_blk(rrpc
, rblk
);
341 mempool_free(gcb
, rrpc
->gcb_pool
);
344 /* the block with highest number of invalid pages, will be in the beginning
347 static struct rrpc_block
*rblock_max_invalid(struct rrpc_block
*ra
,
348 struct rrpc_block
*rb
)
350 if (ra
->nr_invalid_pages
== rb
->nr_invalid_pages
)
353 return (ra
->nr_invalid_pages
< rb
->nr_invalid_pages
) ? rb
: ra
;
356 /* linearly find the block with highest number of invalid pages
359 static struct rrpc_block
*block_prio_find_max(struct rrpc_lun
*rlun
)
361 struct list_head
*prio_list
= &rlun
->prio_list
;
362 struct rrpc_block
*rblock
, *max
;
364 BUG_ON(list_empty(prio_list
));
366 max
= list_first_entry(prio_list
, struct rrpc_block
, prio
);
367 list_for_each_entry(rblock
, prio_list
, prio
)
368 max
= rblock_max_invalid(max
, rblock
);
373 static void rrpc_lun_gc(struct work_struct
*work
)
375 struct rrpc_lun
*rlun
= container_of(work
, struct rrpc_lun
, ws_gc
);
376 struct rrpc
*rrpc
= rlun
->rrpc
;
377 struct nvm_lun
*lun
= rlun
->parent
;
378 struct rrpc_block_gc
*gcb
;
379 unsigned int nr_blocks_need
;
381 nr_blocks_need
= rrpc
->dev
->blks_per_lun
/ GC_LIMIT_INVERSE
;
383 if (nr_blocks_need
< rrpc
->nr_luns
)
384 nr_blocks_need
= rrpc
->nr_luns
;
386 spin_lock(&lun
->lock
);
387 while (nr_blocks_need
> lun
->nr_free_blocks
&&
388 !list_empty(&rlun
->prio_list
)) {
389 struct rrpc_block
*rblock
= block_prio_find_max(rlun
);
390 struct nvm_block
*block
= rblock
->parent
;
392 if (!rblock
->nr_invalid_pages
)
395 list_del_init(&rblock
->prio
);
397 BUG_ON(!block_is_full(rrpc
, rblock
));
399 pr_debug("rrpc: selected block '%lu' for GC\n", block
->id
);
401 gcb
= mempool_alloc(rrpc
->gcb_pool
, GFP_ATOMIC
);
407 INIT_WORK(&gcb
->ws_gc
, rrpc_block_gc
);
409 queue_work(rrpc
->kgc_wq
, &gcb
->ws_gc
);
413 spin_unlock(&lun
->lock
);
415 /* TODO: Hint that request queue can be started again */
418 static void rrpc_gc_queue(struct work_struct
*work
)
420 struct rrpc_block_gc
*gcb
= container_of(work
, struct rrpc_block_gc
,
422 struct rrpc
*rrpc
= gcb
->rrpc
;
423 struct rrpc_block
*rblk
= gcb
->rblk
;
424 struct nvm_lun
*lun
= rblk
->parent
->lun
;
425 struct rrpc_lun
*rlun
= &rrpc
->luns
[lun
->id
- rrpc
->lun_offset
];
427 spin_lock(&rlun
->lock
);
428 list_add_tail(&rblk
->prio
, &rlun
->prio_list
);
429 spin_unlock(&rlun
->lock
);
431 mempool_free(gcb
, rrpc
->gcb_pool
);
432 pr_debug("nvm: block '%lu' is full, allow GC (sched)\n",
436 static const struct block_device_operations rrpc_fops
= {
437 .owner
= THIS_MODULE
,
440 static struct rrpc_lun
*rrpc_get_lun_rr(struct rrpc
*rrpc
, int is_gc
)
443 struct rrpc_lun
*rlun
, *max_free
;
446 return get_next_lun(rrpc
);
448 /* during GC, we don't care about RR, instead we want to make
449 * sure that we maintain evenness between the block luns.
451 max_free
= &rrpc
->luns
[0];
452 /* prevent GC-ing lun from devouring pages of a lun with
453 * little free blocks. We don't take the lock as we only need an
456 rrpc_for_each_lun(rrpc
, rlun
, i
) {
457 if (rlun
->parent
->nr_free_blocks
>
458 max_free
->parent
->nr_free_blocks
)
465 static struct rrpc_addr
*rrpc_update_map(struct rrpc
*rrpc
, sector_t laddr
,
466 struct rrpc_block
*rblk
, u64 paddr
)
468 struct rrpc_addr
*gp
;
469 struct rrpc_rev_addr
*rev
;
471 BUG_ON(laddr
>= rrpc
->nr_pages
);
473 gp
= &rrpc
->trans_map
[laddr
];
474 spin_lock(&rrpc
->rev_lock
);
476 rrpc_page_invalidate(rrpc
, gp
);
481 rev
= &rrpc
->rev_trans_map
[gp
->addr
- rrpc
->poffset
];
483 spin_unlock(&rrpc
->rev_lock
);
488 static u64
rrpc_alloc_addr(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
490 u64 addr
= ADDR_EMPTY
;
492 spin_lock(&rblk
->lock
);
493 if (block_is_full(rrpc
, rblk
))
496 addr
= block_to_addr(rrpc
, rblk
) + rblk
->next_page
;
500 spin_unlock(&rblk
->lock
);
504 /* Simple round-robin Logical to physical address translation.
506 * Retrieve the mapping using the active append point. Then update the ap for
507 * the next write to the disk.
509 * Returns rrpc_addr with the physical address and block. Remember to return to
510 * rrpc->addr_cache when request is finished.
512 static struct rrpc_addr
*rrpc_map_page(struct rrpc
*rrpc
, sector_t laddr
,
515 struct rrpc_lun
*rlun
;
516 struct rrpc_block
*rblk
;
520 rlun
= rrpc_get_lun_rr(rrpc
, is_gc
);
523 if (!is_gc
&& lun
->nr_free_blocks
< rrpc
->nr_luns
* 4)
526 spin_lock(&rlun
->lock
);
530 paddr
= rrpc_alloc_addr(rrpc
, rblk
);
532 if (paddr
== ADDR_EMPTY
) {
533 rblk
= rrpc_get_blk(rrpc
, rlun
, 0);
535 rrpc_set_lun_cur(rlun
, rblk
);
540 /* retry from emergency gc block */
541 paddr
= rrpc_alloc_addr(rrpc
, rlun
->gc_cur
);
542 if (paddr
== ADDR_EMPTY
) {
543 rblk
= rrpc_get_blk(rrpc
, rlun
, 1);
545 pr_err("rrpc: no more blocks");
550 paddr
= rrpc_alloc_addr(rrpc
, rlun
->gc_cur
);
556 spin_unlock(&rlun
->lock
);
557 return rrpc_update_map(rrpc
, laddr
, rblk
, paddr
);
559 spin_unlock(&rlun
->lock
);
563 static void rrpc_run_gc(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
565 struct rrpc_block_gc
*gcb
;
567 gcb
= mempool_alloc(rrpc
->gcb_pool
, GFP_ATOMIC
);
569 pr_err("rrpc: unable to queue block for gc.");
576 INIT_WORK(&gcb
->ws_gc
, rrpc_gc_queue
);
577 queue_work(rrpc
->kgc_wq
, &gcb
->ws_gc
);
580 static void rrpc_end_io_write(struct rrpc
*rrpc
, struct rrpc_rq
*rrqd
,
581 sector_t laddr
, uint8_t npages
)
584 struct rrpc_block
*rblk
;
588 for (i
= 0; i
< npages
; i
++) {
589 p
= &rrpc
->trans_map
[laddr
+ i
];
591 lun
= rblk
->parent
->lun
;
593 cmnt_size
= atomic_inc_return(&rblk
->data_cmnt_size
);
594 if (unlikely(cmnt_size
== rrpc
->dev
->pgs_per_blk
))
595 rrpc_run_gc(rrpc
, rblk
);
599 static int rrpc_end_io(struct nvm_rq
*rqd
, int error
)
601 struct rrpc
*rrpc
= container_of(rqd
->ins
, struct rrpc
, instance
);
602 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
603 uint8_t npages
= rqd
->nr_pages
;
604 sector_t laddr
= rrpc_get_laddr(rqd
->bio
) - npages
;
606 if (bio_data_dir(rqd
->bio
) == WRITE
)
607 rrpc_end_io_write(rrpc
, rrqd
, laddr
, npages
);
609 if (rrqd
->flags
& NVM_IOTYPE_GC
)
612 rrpc_unlock_rq(rrpc
, rqd
);
616 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
618 nvm_dev_dma_free(rrpc
->dev
, rqd
->metadata
, rqd
->dma_metadata
);
620 mempool_free(rqd
, rrpc
->rq_pool
);
625 static int rrpc_read_ppalist_rq(struct rrpc
*rrpc
, struct bio
*bio
,
626 struct nvm_rq
*rqd
, unsigned long flags
, int npages
)
628 struct rrpc_inflight_rq
*r
= rrpc_get_inflight_rq(rqd
);
629 struct rrpc_addr
*gp
;
630 sector_t laddr
= rrpc_get_laddr(bio
);
631 int is_gc
= flags
& NVM_IOTYPE_GC
;
634 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
)) {
635 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
636 return NVM_IO_REQUEUE
;
639 for (i
= 0; i
< npages
; i
++) {
640 /* We assume that mapping occurs at 4KB granularity */
641 BUG_ON(!(laddr
+ i
>= 0 && laddr
+ i
< rrpc
->nr_pages
));
642 gp
= &rrpc
->trans_map
[laddr
+ i
];
645 rqd
->ppa_list
[i
] = rrpc_ppa_to_gaddr(rrpc
->dev
,
649 rrpc_unlock_laddr(rrpc
, r
);
650 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
,
656 rqd
->opcode
= NVM_OP_HBREAD
;
661 static int rrpc_read_rq(struct rrpc
*rrpc
, struct bio
*bio
, struct nvm_rq
*rqd
,
664 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
665 int is_gc
= flags
& NVM_IOTYPE_GC
;
666 sector_t laddr
= rrpc_get_laddr(bio
);
667 struct rrpc_addr
*gp
;
669 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
))
670 return NVM_IO_REQUEUE
;
672 BUG_ON(!(laddr
>= 0 && laddr
< rrpc
->nr_pages
));
673 gp
= &rrpc
->trans_map
[laddr
];
676 rqd
->ppa_addr
= rrpc_ppa_to_gaddr(rrpc
->dev
, gp
->addr
);
679 rrpc_unlock_rq(rrpc
, rqd
);
683 rqd
->opcode
= NVM_OP_HBREAD
;
689 static int rrpc_write_ppalist_rq(struct rrpc
*rrpc
, struct bio
*bio
,
690 struct nvm_rq
*rqd
, unsigned long flags
, int npages
)
692 struct rrpc_inflight_rq
*r
= rrpc_get_inflight_rq(rqd
);
694 sector_t laddr
= rrpc_get_laddr(bio
);
695 int is_gc
= flags
& NVM_IOTYPE_GC
;
698 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
)) {
699 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
, rqd
->dma_ppa_list
);
700 return NVM_IO_REQUEUE
;
703 for (i
= 0; i
< npages
; i
++) {
704 /* We assume that mapping occurs at 4KB granularity */
705 p
= rrpc_map_page(rrpc
, laddr
+ i
, is_gc
);
708 rrpc_unlock_laddr(rrpc
, r
);
709 nvm_dev_dma_free(rrpc
->dev
, rqd
->ppa_list
,
712 return NVM_IO_REQUEUE
;
715 rqd
->ppa_list
[i
] = rrpc_ppa_to_gaddr(rrpc
->dev
,
719 rqd
->opcode
= NVM_OP_HBWRITE
;
724 static int rrpc_write_rq(struct rrpc
*rrpc
, struct bio
*bio
,
725 struct nvm_rq
*rqd
, unsigned long flags
)
727 struct rrpc_rq
*rrqd
= nvm_rq_to_pdu(rqd
);
729 int is_gc
= flags
& NVM_IOTYPE_GC
;
730 sector_t laddr
= rrpc_get_laddr(bio
);
732 if (!is_gc
&& rrpc_lock_rq(rrpc
, bio
, rqd
))
733 return NVM_IO_REQUEUE
;
735 p
= rrpc_map_page(rrpc
, laddr
, is_gc
);
738 rrpc_unlock_rq(rrpc
, rqd
);
740 return NVM_IO_REQUEUE
;
743 rqd
->ppa_addr
= rrpc_ppa_to_gaddr(rrpc
->dev
, p
->addr
);
744 rqd
->opcode
= NVM_OP_HBWRITE
;
750 static int rrpc_setup_rq(struct rrpc
*rrpc
, struct bio
*bio
,
751 struct nvm_rq
*rqd
, unsigned long flags
, uint8_t npages
)
754 rqd
->ppa_list
= nvm_dev_dma_alloc(rrpc
->dev
, GFP_KERNEL
,
756 if (!rqd
->ppa_list
) {
757 pr_err("rrpc: not able to allocate ppa list\n");
761 if (bio_rw(bio
) == WRITE
)
762 return rrpc_write_ppalist_rq(rrpc
, bio
, rqd
, flags
,
765 return rrpc_read_ppalist_rq(rrpc
, bio
, rqd
, flags
, npages
);
768 if (bio_rw(bio
) == WRITE
)
769 return rrpc_write_rq(rrpc
, bio
, rqd
, flags
);
771 return rrpc_read_rq(rrpc
, bio
, rqd
, flags
);
774 static int rrpc_submit_io(struct rrpc
*rrpc
, struct bio
*bio
,
775 struct nvm_rq
*rqd
, unsigned long flags
)
778 struct rrpc_rq
*rrq
= nvm_rq_to_pdu(rqd
);
779 uint8_t nr_pages
= rrpc_get_pages(bio
);
780 int bio_size
= bio_sectors(bio
) << 9;
782 if (bio_size
< rrpc
->dev
->sec_size
)
784 else if (bio_size
> rrpc
->dev
->max_rq_size
)
787 err
= rrpc_setup_rq(rrpc
, bio
, rqd
, flags
, nr_pages
);
793 rqd
->ins
= &rrpc
->instance
;
794 rqd
->nr_pages
= nr_pages
;
797 err
= nvm_submit_io(rrpc
->dev
, rqd
);
799 pr_err("rrpc: I/O submission failed: %d\n", err
);
806 static blk_qc_t
rrpc_make_rq(struct request_queue
*q
, struct bio
*bio
)
808 struct rrpc
*rrpc
= q
->queuedata
;
812 if (bio
->bi_rw
& REQ_DISCARD
) {
813 rrpc_discard(rrpc
, bio
);
814 return BLK_QC_T_NONE
;
817 rqd
= mempool_alloc(rrpc
->rq_pool
, GFP_KERNEL
);
819 pr_err_ratelimited("rrpc: not able to queue bio.");
821 return BLK_QC_T_NONE
;
823 memset(rqd
, 0, sizeof(struct nvm_rq
));
825 err
= rrpc_submit_io(rrpc
, bio
, rqd
, NVM_IOTYPE_NONE
);
828 return BLK_QC_T_NONE
;
836 spin_lock(&rrpc
->bio_lock
);
837 bio_list_add(&rrpc
->requeue_bios
, bio
);
838 spin_unlock(&rrpc
->bio_lock
);
839 queue_work(rrpc
->kgc_wq
, &rrpc
->ws_requeue
);
843 mempool_free(rqd
, rrpc
->rq_pool
);
844 return BLK_QC_T_NONE
;
847 static void rrpc_requeue(struct work_struct
*work
)
849 struct rrpc
*rrpc
= container_of(work
, struct rrpc
, ws_requeue
);
850 struct bio_list bios
;
853 bio_list_init(&bios
);
855 spin_lock(&rrpc
->bio_lock
);
856 bio_list_merge(&bios
, &rrpc
->requeue_bios
);
857 bio_list_init(&rrpc
->requeue_bios
);
858 spin_unlock(&rrpc
->bio_lock
);
860 while ((bio
= bio_list_pop(&bios
)))
861 rrpc_make_rq(rrpc
->disk
->queue
, bio
);
864 static void rrpc_gc_free(struct rrpc
*rrpc
)
866 struct rrpc_lun
*rlun
;
870 destroy_workqueue(rrpc
->krqd_wq
);
873 destroy_workqueue(rrpc
->kgc_wq
);
878 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
879 rlun
= &rrpc
->luns
[i
];
887 static int rrpc_gc_init(struct rrpc
*rrpc
)
889 rrpc
->krqd_wq
= alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM
|WQ_UNBOUND
,
894 rrpc
->kgc_wq
= alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM
, 1);
898 setup_timer(&rrpc
->gc_timer
, rrpc_gc_timer
, (unsigned long)rrpc
);
903 static void rrpc_map_free(struct rrpc
*rrpc
)
905 vfree(rrpc
->rev_trans_map
);
906 vfree(rrpc
->trans_map
);
909 static int rrpc_l2p_update(u64 slba
, u32 nlb
, __le64
*entries
, void *private)
911 struct rrpc
*rrpc
= (struct rrpc
*)private;
912 struct nvm_dev
*dev
= rrpc
->dev
;
913 struct rrpc_addr
*addr
= rrpc
->trans_map
+ slba
;
914 struct rrpc_rev_addr
*raddr
= rrpc
->rev_trans_map
;
915 sector_t max_pages
= dev
->total_pages
* (dev
->sec_size
>> 9);
916 u64 elba
= slba
+ nlb
;
919 if (unlikely(elba
> dev
->total_pages
)) {
920 pr_err("nvm: L2P data from device is out of bounds!\n");
924 for (i
= 0; i
< nlb
; i
++) {
925 u64 pba
= le64_to_cpu(entries
[i
]);
926 /* LNVM treats address-spaces as silos, LBA and PBA are
927 * equally large and zero-indexed.
929 if (unlikely(pba
>= max_pages
&& pba
!= U64_MAX
)) {
930 pr_err("nvm: L2P data entry is out of bounds!\n");
934 /* Address zero is a special one. The first page on a disk is
935 * protected. As it often holds internal device boot
942 raddr
[pba
].addr
= slba
+ i
;
948 static int rrpc_map_init(struct rrpc
*rrpc
)
950 struct nvm_dev
*dev
= rrpc
->dev
;
954 rrpc
->trans_map
= vzalloc(sizeof(struct rrpc_addr
) * rrpc
->nr_pages
);
955 if (!rrpc
->trans_map
)
958 rrpc
->rev_trans_map
= vmalloc(sizeof(struct rrpc_rev_addr
)
960 if (!rrpc
->rev_trans_map
)
963 for (i
= 0; i
< rrpc
->nr_pages
; i
++) {
964 struct rrpc_addr
*p
= &rrpc
->trans_map
[i
];
965 struct rrpc_rev_addr
*r
= &rrpc
->rev_trans_map
[i
];
967 p
->addr
= ADDR_EMPTY
;
968 r
->addr
= ADDR_EMPTY
;
971 if (!dev
->ops
->get_l2p_tbl
)
974 /* Bring up the mapping table from device */
975 ret
= dev
->ops
->get_l2p_tbl(dev
->q
, 0, dev
->total_pages
,
976 rrpc_l2p_update
, rrpc
);
978 pr_err("nvm: rrpc: could not read L2P table.\n");
986 /* Minimum pages needed within a lun */
987 #define PAGE_POOL_SIZE 16
988 #define ADDR_POOL_SIZE 64
990 static int rrpc_core_init(struct rrpc
*rrpc
)
992 down_write(&rrpc_lock
);
993 if (!rrpc_gcb_cache
) {
994 rrpc_gcb_cache
= kmem_cache_create("rrpc_gcb",
995 sizeof(struct rrpc_block_gc
), 0, 0, NULL
);
996 if (!rrpc_gcb_cache
) {
997 up_write(&rrpc_lock
);
1001 rrpc_rq_cache
= kmem_cache_create("rrpc_rq",
1002 sizeof(struct nvm_rq
) + sizeof(struct rrpc_rq
),
1004 if (!rrpc_rq_cache
) {
1005 kmem_cache_destroy(rrpc_gcb_cache
);
1006 up_write(&rrpc_lock
);
1010 up_write(&rrpc_lock
);
1012 rrpc
->page_pool
= mempool_create_page_pool(PAGE_POOL_SIZE
, 0);
1013 if (!rrpc
->page_pool
)
1016 rrpc
->gcb_pool
= mempool_create_slab_pool(rrpc
->dev
->nr_luns
,
1018 if (!rrpc
->gcb_pool
)
1021 rrpc
->rq_pool
= mempool_create_slab_pool(64, rrpc_rq_cache
);
1025 spin_lock_init(&rrpc
->inflights
.lock
);
1026 INIT_LIST_HEAD(&rrpc
->inflights
.reqs
);
1031 static void rrpc_core_free(struct rrpc
*rrpc
)
1033 mempool_destroy(rrpc
->page_pool
);
1034 mempool_destroy(rrpc
->gcb_pool
);
1035 mempool_destroy(rrpc
->rq_pool
);
1038 static void rrpc_luns_free(struct rrpc
*rrpc
)
1043 static int rrpc_luns_init(struct rrpc
*rrpc
, int lun_begin
, int lun_end
)
1045 struct nvm_dev
*dev
= rrpc
->dev
;
1046 struct rrpc_lun
*rlun
;
1049 spin_lock_init(&rrpc
->rev_lock
);
1051 rrpc
->luns
= kcalloc(rrpc
->nr_luns
, sizeof(struct rrpc_lun
),
1057 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
1058 struct nvm_lun
*lun
= dev
->mt
->get_lun(dev
, lun_begin
+ i
);
1060 if (dev
->pgs_per_blk
>
1061 MAX_INVALID_PAGES_STORAGE
* BITS_PER_LONG
) {
1062 pr_err("rrpc: number of pages per block too high.");
1066 rlun
= &rrpc
->luns
[i
];
1069 INIT_LIST_HEAD(&rlun
->prio_list
);
1070 INIT_WORK(&rlun
->ws_gc
, rrpc_lun_gc
);
1071 spin_lock_init(&rlun
->lock
);
1073 rrpc
->total_blocks
+= dev
->blks_per_lun
;
1074 rrpc
->nr_pages
+= dev
->sec_per_lun
;
1076 rlun
->blocks
= vzalloc(sizeof(struct rrpc_block
) *
1077 rrpc
->dev
->blks_per_lun
);
1081 for (j
= 0; j
< rrpc
->dev
->blks_per_lun
; j
++) {
1082 struct rrpc_block
*rblk
= &rlun
->blocks
[j
];
1083 struct nvm_block
*blk
= &lun
->blocks
[j
];
1086 INIT_LIST_HEAD(&rblk
->prio
);
1087 spin_lock_init(&rblk
->lock
);
1096 static void rrpc_free(struct rrpc
*rrpc
)
1099 rrpc_map_free(rrpc
);
1100 rrpc_core_free(rrpc
);
1101 rrpc_luns_free(rrpc
);
1106 static void rrpc_exit(void *private)
1108 struct rrpc
*rrpc
= private;
1110 del_timer(&rrpc
->gc_timer
);
1112 flush_workqueue(rrpc
->krqd_wq
);
1113 flush_workqueue(rrpc
->kgc_wq
);
1118 static sector_t
rrpc_capacity(void *private)
1120 struct rrpc
*rrpc
= private;
1121 struct nvm_dev
*dev
= rrpc
->dev
;
1122 sector_t reserved
, provisioned
;
1124 /* cur, gc, and two emergency blocks for each lun */
1125 reserved
= rrpc
->nr_luns
* dev
->max_pages_per_blk
* 4;
1126 provisioned
= rrpc
->nr_pages
- reserved
;
1128 if (reserved
> rrpc
->nr_pages
) {
1129 pr_err("rrpc: not enough space available to expose storage.\n");
1133 sector_div(provisioned
, 10);
1134 return provisioned
* 9 * NR_PHY_IN_LOG
;
1138 * Looks up the logical address from reverse trans map and check if its valid by
1139 * comparing the logical to physical address with the physical address.
1140 * Returns 0 on free, otherwise 1 if in use
1142 static void rrpc_block_map_update(struct rrpc
*rrpc
, struct rrpc_block
*rblk
)
1144 struct nvm_dev
*dev
= rrpc
->dev
;
1146 struct rrpc_addr
*laddr
;
1149 for (offset
= 0; offset
< dev
->pgs_per_blk
; offset
++) {
1150 paddr
= block_to_addr(rrpc
, rblk
) + offset
;
1152 pladdr
= rrpc
->rev_trans_map
[paddr
].addr
;
1153 if (pladdr
== ADDR_EMPTY
)
1156 laddr
= &rrpc
->trans_map
[pladdr
];
1158 if (paddr
== laddr
->addr
) {
1161 set_bit(offset
, rblk
->invalid_pages
);
1162 rblk
->nr_invalid_pages
++;
1167 static int rrpc_blocks_init(struct rrpc
*rrpc
)
1169 struct rrpc_lun
*rlun
;
1170 struct rrpc_block
*rblk
;
1171 int lun_iter
, blk_iter
;
1173 for (lun_iter
= 0; lun_iter
< rrpc
->nr_luns
; lun_iter
++) {
1174 rlun
= &rrpc
->luns
[lun_iter
];
1176 for (blk_iter
= 0; blk_iter
< rrpc
->dev
->blks_per_lun
;
1178 rblk
= &rlun
->blocks
[blk_iter
];
1179 rrpc_block_map_update(rrpc
, rblk
);
1186 static int rrpc_luns_configure(struct rrpc
*rrpc
)
1188 struct rrpc_lun
*rlun
;
1189 struct rrpc_block
*rblk
;
1192 for (i
= 0; i
< rrpc
->nr_luns
; i
++) {
1193 rlun
= &rrpc
->luns
[i
];
1195 rblk
= rrpc_get_blk(rrpc
, rlun
, 0);
1199 rrpc_set_lun_cur(rlun
, rblk
);
1201 /* Emergency gc block */
1202 rblk
= rrpc_get_blk(rrpc
, rlun
, 1);
1205 rlun
->gc_cur
= rblk
;
1211 static struct nvm_tgt_type tt_rrpc
;
1213 static void *rrpc_init(struct nvm_dev
*dev
, struct gendisk
*tdisk
,
1214 int lun_begin
, int lun_end
)
1216 struct request_queue
*bqueue
= dev
->q
;
1217 struct request_queue
*tqueue
= tdisk
->queue
;
1221 if (!(dev
->identity
.dom
& NVM_RSP_L2P
)) {
1222 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1224 return ERR_PTR(-EINVAL
);
1227 rrpc
= kzalloc(sizeof(struct rrpc
), GFP_KERNEL
);
1229 return ERR_PTR(-ENOMEM
);
1231 rrpc
->instance
.tt
= &tt_rrpc
;
1235 bio_list_init(&rrpc
->requeue_bios
);
1236 spin_lock_init(&rrpc
->bio_lock
);
1237 INIT_WORK(&rrpc
->ws_requeue
, rrpc_requeue
);
1239 rrpc
->nr_luns
= lun_end
- lun_begin
+ 1;
1241 /* simple round-robin strategy */
1242 atomic_set(&rrpc
->next_lun
, -1);
1244 ret
= rrpc_luns_init(rrpc
, lun_begin
, lun_end
);
1246 pr_err("nvm: rrpc: could not initialize luns\n");
1250 rrpc
->poffset
= dev
->sec_per_lun
* lun_begin
;
1251 rrpc
->lun_offset
= lun_begin
;
1253 ret
= rrpc_core_init(rrpc
);
1255 pr_err("nvm: rrpc: could not initialize core\n");
1259 ret
= rrpc_map_init(rrpc
);
1261 pr_err("nvm: rrpc: could not initialize maps\n");
1265 ret
= rrpc_blocks_init(rrpc
);
1267 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1271 ret
= rrpc_luns_configure(rrpc
);
1273 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1277 ret
= rrpc_gc_init(rrpc
);
1279 pr_err("nvm: rrpc: could not initialize gc\n");
1283 /* inherit the size from the underlying device */
1284 blk_queue_logical_block_size(tqueue
, queue_physical_block_size(bqueue
));
1285 blk_queue_max_hw_sectors(tqueue
, queue_max_hw_sectors(bqueue
));
1287 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
1288 rrpc
->nr_luns
, (unsigned long long)rrpc
->nr_pages
);
1290 mod_timer(&rrpc
->gc_timer
, jiffies
+ msecs_to_jiffies(10));
1295 return ERR_PTR(ret
);
1298 /* round robin, page-based FTL, and cost-based GC */
1299 static struct nvm_tgt_type tt_rrpc
= {
1301 .version
= {1, 0, 0},
1303 .make_rq
= rrpc_make_rq
,
1304 .capacity
= rrpc_capacity
,
1305 .end_io
= rrpc_end_io
,
1311 static int __init
rrpc_module_init(void)
1313 return nvm_register_target(&tt_rrpc
);
1316 static void rrpc_module_exit(void)
1318 nvm_unregister_target(&tt_rrpc
);
1321 module_init(rrpc_module_init
);
1322 module_exit(rrpc_module_exit
);
1323 MODULE_LICENSE("GPL v2");
1324 MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");