Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / lightnvm / rrpc.c
blob3ab6495c3fd87b1144640250874144742c0df1bb
1 /*
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.
17 #include "rrpc.h"
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)
37 return;
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,
51 unsigned len)
53 sector_t i;
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);
60 gp->rblk = NULL;
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)
68 struct nvm_rq *rqd;
69 struct rrpc_inflight_rq *inf;
71 rqd = mempool_alloc(rrpc->rq_pool, GFP_ATOMIC);
72 if (!rqd)
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);
78 return NULL;
81 return rqd;
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;
97 struct nvm_rq *rqd;
99 do {
100 rqd = rrpc_inflight_laddr_acquire(rrpc, slba, len);
101 schedule();
102 } while (!rqd);
104 if (IS_ERR(rqd)) {
105 pr_err("rrpc: unable to acquire inflight IO\n");
106 bio_io_error(bio);
107 return;
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->sec_per_blk);
119 /* Calculate relative addr for the given block, considering instantiated LUNs */
120 static u64 block_to_rel_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
122 struct nvm_block *blk = rblk->parent;
123 int lun_blk = blk->id % (rrpc->dev->blks_per_lun * rrpc->nr_luns);
125 return lun_blk * rrpc->dev->sec_per_blk;
128 /* Calculate global addr for the given block */
129 static u64 block_to_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
131 struct nvm_block *blk = rblk->parent;
133 return blk->id * rrpc->dev->sec_per_blk;
136 static struct ppa_addr linear_to_generic_addr(struct nvm_dev *dev,
137 struct ppa_addr r)
139 struct ppa_addr l;
140 int secs, pgs, blks, luns;
141 sector_t ppa = r.ppa;
143 l.ppa = 0;
145 div_u64_rem(ppa, dev->sec_per_pg, &secs);
146 l.g.sec = secs;
148 sector_div(ppa, dev->sec_per_pg);
149 div_u64_rem(ppa, dev->pgs_per_blk, &pgs);
150 l.g.pg = pgs;
152 sector_div(ppa, dev->pgs_per_blk);
153 div_u64_rem(ppa, dev->blks_per_lun, &blks);
154 l.g.blk = blks;
156 sector_div(ppa, dev->blks_per_lun);
157 div_u64_rem(ppa, dev->luns_per_chnl, &luns);
158 l.g.lun = luns;
160 sector_div(ppa, dev->luns_per_chnl);
161 l.g.ch = ppa;
163 return l;
166 static struct ppa_addr rrpc_ppa_to_gaddr(struct nvm_dev *dev, u64 addr)
168 struct ppa_addr paddr;
170 paddr.ppa = addr;
171 return linear_to_generic_addr(dev, paddr);
174 /* requires lun->lock taken */
175 static void rrpc_set_lun_cur(struct rrpc_lun *rlun, struct rrpc_block *rblk)
177 struct rrpc *rrpc = rlun->rrpc;
179 BUG_ON(!rblk);
181 if (rlun->cur) {
182 spin_lock(&rlun->cur->lock);
183 WARN_ON(!block_is_full(rrpc, rlun->cur));
184 spin_unlock(&rlun->cur->lock);
186 rlun->cur = rblk;
189 static struct rrpc_block *rrpc_get_blk(struct rrpc *rrpc, struct rrpc_lun *rlun,
190 unsigned long flags)
192 struct nvm_lun *lun = rlun->parent;
193 struct nvm_block *blk;
194 struct rrpc_block *rblk;
196 spin_lock(&lun->lock);
197 blk = nvm_get_blk_unlocked(rrpc->dev, rlun->parent, flags);
198 if (!blk) {
199 pr_err("nvm: rrpc: cannot get new block from media manager\n");
200 spin_unlock(&lun->lock);
201 return NULL;
204 rblk = rrpc_get_rblk(rlun, blk->id);
205 list_add_tail(&rblk->list, &rlun->open_list);
206 spin_unlock(&lun->lock);
208 blk->priv = rblk;
209 bitmap_zero(rblk->invalid_pages, rrpc->dev->sec_per_blk);
210 rblk->next_page = 0;
211 rblk->nr_invalid_pages = 0;
212 atomic_set(&rblk->data_cmnt_size, 0);
214 return rblk;
217 static void rrpc_put_blk(struct rrpc *rrpc, struct rrpc_block *rblk)
219 struct rrpc_lun *rlun = rblk->rlun;
220 struct nvm_lun *lun = rlun->parent;
222 spin_lock(&lun->lock);
223 nvm_put_blk_unlocked(rrpc->dev, rblk->parent);
224 list_del(&rblk->list);
225 spin_unlock(&lun->lock);
228 static void rrpc_put_blks(struct rrpc *rrpc)
230 struct rrpc_lun *rlun;
231 int i;
233 for (i = 0; i < rrpc->nr_luns; i++) {
234 rlun = &rrpc->luns[i];
235 if (rlun->cur)
236 rrpc_put_blk(rrpc, rlun->cur);
237 if (rlun->gc_cur)
238 rrpc_put_blk(rrpc, rlun->gc_cur);
242 static struct rrpc_lun *get_next_lun(struct rrpc *rrpc)
244 int next = atomic_inc_return(&rrpc->next_lun);
246 return &rrpc->luns[next % rrpc->nr_luns];
249 static void rrpc_gc_kick(struct rrpc *rrpc)
251 struct rrpc_lun *rlun;
252 unsigned int i;
254 for (i = 0; i < rrpc->nr_luns; i++) {
255 rlun = &rrpc->luns[i];
256 queue_work(rrpc->krqd_wq, &rlun->ws_gc);
261 * timed GC every interval.
263 static void rrpc_gc_timer(unsigned long data)
265 struct rrpc *rrpc = (struct rrpc *)data;
267 rrpc_gc_kick(rrpc);
268 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
271 static void rrpc_end_sync_bio(struct bio *bio)
273 struct completion *waiting = bio->bi_private;
275 if (bio->bi_error)
276 pr_err("nvm: gc request failed (%u).\n", bio->bi_error);
278 complete(waiting);
282 * rrpc_move_valid_pages -- migrate live data off the block
283 * @rrpc: the 'rrpc' structure
284 * @block: the block from which to migrate live pages
286 * Description:
287 * GC algorithms may call this function to migrate remaining live
288 * pages off the block prior to erasing it. This function blocks
289 * further execution until the operation is complete.
291 static int rrpc_move_valid_pages(struct rrpc *rrpc, struct rrpc_block *rblk)
293 struct request_queue *q = rrpc->dev->q;
294 struct rrpc_rev_addr *rev;
295 struct nvm_rq *rqd;
296 struct bio *bio;
297 struct page *page;
298 int slot;
299 int nr_sec_per_blk = rrpc->dev->sec_per_blk;
300 u64 phys_addr;
301 DECLARE_COMPLETION_ONSTACK(wait);
303 if (bitmap_full(rblk->invalid_pages, nr_sec_per_blk))
304 return 0;
306 bio = bio_alloc(GFP_NOIO, 1);
307 if (!bio) {
308 pr_err("nvm: could not alloc bio to gc\n");
309 return -ENOMEM;
312 page = mempool_alloc(rrpc->page_pool, GFP_NOIO);
313 if (!page) {
314 bio_put(bio);
315 return -ENOMEM;
318 while ((slot = find_first_zero_bit(rblk->invalid_pages,
319 nr_sec_per_blk)) < nr_sec_per_blk) {
321 /* Lock laddr */
322 phys_addr = rblk->parent->id * nr_sec_per_blk + slot;
324 try:
325 spin_lock(&rrpc->rev_lock);
326 /* Get logical address from physical to logical table */
327 rev = &rrpc->rev_trans_map[phys_addr - rrpc->poffset];
328 /* already updated by previous regular write */
329 if (rev->addr == ADDR_EMPTY) {
330 spin_unlock(&rrpc->rev_lock);
331 continue;
334 rqd = rrpc_inflight_laddr_acquire(rrpc, rev->addr, 1);
335 if (IS_ERR_OR_NULL(rqd)) {
336 spin_unlock(&rrpc->rev_lock);
337 schedule();
338 goto try;
341 spin_unlock(&rrpc->rev_lock);
343 /* Perform read to do GC */
344 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
345 bio->bi_rw = READ;
346 bio->bi_private = &wait;
347 bio->bi_end_io = rrpc_end_sync_bio;
349 /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
350 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
352 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
353 pr_err("rrpc: gc read failed.\n");
354 rrpc_inflight_laddr_release(rrpc, rqd);
355 goto finished;
357 wait_for_completion_io(&wait);
358 if (bio->bi_error) {
359 rrpc_inflight_laddr_release(rrpc, rqd);
360 goto finished;
363 bio_reset(bio);
364 reinit_completion(&wait);
366 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
367 bio->bi_rw = WRITE;
368 bio->bi_private = &wait;
369 bio->bi_end_io = rrpc_end_sync_bio;
371 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
373 /* turn the command around and write the data back to a new
374 * address
376 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
377 pr_err("rrpc: gc write failed.\n");
378 rrpc_inflight_laddr_release(rrpc, rqd);
379 goto finished;
381 wait_for_completion_io(&wait);
383 rrpc_inflight_laddr_release(rrpc, rqd);
384 if (bio->bi_error)
385 goto finished;
387 bio_reset(bio);
390 finished:
391 mempool_free(page, rrpc->page_pool);
392 bio_put(bio);
394 if (!bitmap_full(rblk->invalid_pages, nr_sec_per_blk)) {
395 pr_err("nvm: failed to garbage collect block\n");
396 return -EIO;
399 return 0;
402 static void rrpc_block_gc(struct work_struct *work)
404 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
405 ws_gc);
406 struct rrpc *rrpc = gcb->rrpc;
407 struct rrpc_block *rblk = gcb->rblk;
408 struct nvm_dev *dev = rrpc->dev;
409 struct nvm_lun *lun = rblk->parent->lun;
410 struct rrpc_lun *rlun = &rrpc->luns[lun->id - rrpc->lun_offset];
412 mempool_free(gcb, rrpc->gcb_pool);
413 pr_debug("nvm: block '%lu' being reclaimed\n", rblk->parent->id);
415 if (rrpc_move_valid_pages(rrpc, rblk))
416 goto put_back;
418 if (nvm_erase_blk(dev, rblk->parent))
419 goto put_back;
421 rrpc_put_blk(rrpc, rblk);
423 return;
425 put_back:
426 spin_lock(&rlun->lock);
427 list_add_tail(&rblk->prio, &rlun->prio_list);
428 spin_unlock(&rlun->lock);
431 /* the block with highest number of invalid pages, will be in the beginning
432 * of the list
434 static struct rrpc_block *rblock_max_invalid(struct rrpc_block *ra,
435 struct rrpc_block *rb)
437 if (ra->nr_invalid_pages == rb->nr_invalid_pages)
438 return ra;
440 return (ra->nr_invalid_pages < rb->nr_invalid_pages) ? rb : ra;
443 /* linearly find the block with highest number of invalid pages
444 * requires lun->lock
446 static struct rrpc_block *block_prio_find_max(struct rrpc_lun *rlun)
448 struct list_head *prio_list = &rlun->prio_list;
449 struct rrpc_block *rblock, *max;
451 BUG_ON(list_empty(prio_list));
453 max = list_first_entry(prio_list, struct rrpc_block, prio);
454 list_for_each_entry(rblock, prio_list, prio)
455 max = rblock_max_invalid(max, rblock);
457 return max;
460 static void rrpc_lun_gc(struct work_struct *work)
462 struct rrpc_lun *rlun = container_of(work, struct rrpc_lun, ws_gc);
463 struct rrpc *rrpc = rlun->rrpc;
464 struct nvm_lun *lun = rlun->parent;
465 struct rrpc_block_gc *gcb;
466 unsigned int nr_blocks_need;
468 nr_blocks_need = rrpc->dev->blks_per_lun / GC_LIMIT_INVERSE;
470 if (nr_blocks_need < rrpc->nr_luns)
471 nr_blocks_need = rrpc->nr_luns;
473 spin_lock(&rlun->lock);
474 while (nr_blocks_need > lun->nr_free_blocks &&
475 !list_empty(&rlun->prio_list)) {
476 struct rrpc_block *rblock = block_prio_find_max(rlun);
477 struct nvm_block *block = rblock->parent;
479 if (!rblock->nr_invalid_pages)
480 break;
482 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
483 if (!gcb)
484 break;
486 list_del_init(&rblock->prio);
488 BUG_ON(!block_is_full(rrpc, rblock));
490 pr_debug("rrpc: selected block '%lu' for GC\n", block->id);
492 gcb->rrpc = rrpc;
493 gcb->rblk = rblock;
494 INIT_WORK(&gcb->ws_gc, rrpc_block_gc);
496 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
498 nr_blocks_need--;
500 spin_unlock(&rlun->lock);
502 /* TODO: Hint that request queue can be started again */
505 static void rrpc_gc_queue(struct work_struct *work)
507 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
508 ws_gc);
509 struct rrpc *rrpc = gcb->rrpc;
510 struct rrpc_block *rblk = gcb->rblk;
511 struct nvm_lun *lun = rblk->parent->lun;
512 struct nvm_block *blk = rblk->parent;
513 struct rrpc_lun *rlun = &rrpc->luns[lun->id - rrpc->lun_offset];
515 spin_lock(&rlun->lock);
516 list_add_tail(&rblk->prio, &rlun->prio_list);
517 spin_unlock(&rlun->lock);
519 spin_lock(&lun->lock);
520 lun->nr_open_blocks--;
521 lun->nr_closed_blocks++;
522 blk->state &= ~NVM_BLK_ST_OPEN;
523 blk->state |= NVM_BLK_ST_CLOSED;
524 list_move_tail(&rblk->list, &rlun->closed_list);
525 spin_unlock(&lun->lock);
527 mempool_free(gcb, rrpc->gcb_pool);
528 pr_debug("nvm: block '%lu' is full, allow GC (sched)\n",
529 rblk->parent->id);
532 static const struct block_device_operations rrpc_fops = {
533 .owner = THIS_MODULE,
536 static struct rrpc_lun *rrpc_get_lun_rr(struct rrpc *rrpc, int is_gc)
538 unsigned int i;
539 struct rrpc_lun *rlun, *max_free;
541 if (!is_gc)
542 return get_next_lun(rrpc);
544 /* during GC, we don't care about RR, instead we want to make
545 * sure that we maintain evenness between the block luns.
547 max_free = &rrpc->luns[0];
548 /* prevent GC-ing lun from devouring pages of a lun with
549 * little free blocks. We don't take the lock as we only need an
550 * estimate.
552 rrpc_for_each_lun(rrpc, rlun, i) {
553 if (rlun->parent->nr_free_blocks >
554 max_free->parent->nr_free_blocks)
555 max_free = rlun;
558 return max_free;
561 static struct rrpc_addr *rrpc_update_map(struct rrpc *rrpc, sector_t laddr,
562 struct rrpc_block *rblk, u64 paddr)
564 struct rrpc_addr *gp;
565 struct rrpc_rev_addr *rev;
567 BUG_ON(laddr >= rrpc->nr_sects);
569 gp = &rrpc->trans_map[laddr];
570 spin_lock(&rrpc->rev_lock);
571 if (gp->rblk)
572 rrpc_page_invalidate(rrpc, gp);
574 gp->addr = paddr;
575 gp->rblk = rblk;
577 rev = &rrpc->rev_trans_map[gp->addr - rrpc->poffset];
578 rev->addr = laddr;
579 spin_unlock(&rrpc->rev_lock);
581 return gp;
584 static u64 rrpc_alloc_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
586 u64 addr = ADDR_EMPTY;
588 spin_lock(&rblk->lock);
589 if (block_is_full(rrpc, rblk))
590 goto out;
592 addr = block_to_addr(rrpc, rblk) + rblk->next_page;
594 rblk->next_page++;
595 out:
596 spin_unlock(&rblk->lock);
597 return addr;
600 /* Simple round-robin Logical to physical address translation.
602 * Retrieve the mapping using the active append point. Then update the ap for
603 * the next write to the disk.
605 * Returns rrpc_addr with the physical address and block. Remember to return to
606 * rrpc->addr_cache when request is finished.
608 static struct rrpc_addr *rrpc_map_page(struct rrpc *rrpc, sector_t laddr,
609 int is_gc)
611 struct rrpc_lun *rlun;
612 struct rrpc_block *rblk;
613 struct nvm_lun *lun;
614 u64 paddr;
616 rlun = rrpc_get_lun_rr(rrpc, is_gc);
617 lun = rlun->parent;
619 if (!is_gc && lun->nr_free_blocks < rrpc->nr_luns * 4)
620 return NULL;
622 spin_lock(&rlun->lock);
624 rblk = rlun->cur;
625 retry:
626 paddr = rrpc_alloc_addr(rrpc, rblk);
628 if (paddr == ADDR_EMPTY) {
629 rblk = rrpc_get_blk(rrpc, rlun, 0);
630 if (rblk) {
631 rrpc_set_lun_cur(rlun, rblk);
632 goto retry;
635 if (is_gc) {
636 /* retry from emergency gc block */
637 paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur);
638 if (paddr == ADDR_EMPTY) {
639 rblk = rrpc_get_blk(rrpc, rlun, 1);
640 if (!rblk) {
641 pr_err("rrpc: no more blocks");
642 goto err;
645 rlun->gc_cur = rblk;
646 paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur);
648 rblk = rlun->gc_cur;
652 spin_unlock(&rlun->lock);
653 return rrpc_update_map(rrpc, laddr, rblk, paddr);
654 err:
655 spin_unlock(&rlun->lock);
656 return NULL;
659 static void rrpc_run_gc(struct rrpc *rrpc, struct rrpc_block *rblk)
661 struct rrpc_block_gc *gcb;
663 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
664 if (!gcb) {
665 pr_err("rrpc: unable to queue block for gc.");
666 return;
669 gcb->rrpc = rrpc;
670 gcb->rblk = rblk;
672 INIT_WORK(&gcb->ws_gc, rrpc_gc_queue);
673 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
676 static void rrpc_end_io_write(struct rrpc *rrpc, struct rrpc_rq *rrqd,
677 sector_t laddr, uint8_t npages)
679 struct rrpc_addr *p;
680 struct rrpc_block *rblk;
681 struct nvm_lun *lun;
682 int cmnt_size, i;
684 for (i = 0; i < npages; i++) {
685 p = &rrpc->trans_map[laddr + i];
686 rblk = p->rblk;
687 lun = rblk->parent->lun;
689 cmnt_size = atomic_inc_return(&rblk->data_cmnt_size);
690 if (unlikely(cmnt_size == rrpc->dev->sec_per_blk))
691 rrpc_run_gc(rrpc, rblk);
695 static void rrpc_end_io(struct nvm_rq *rqd)
697 struct rrpc *rrpc = container_of(rqd->ins, struct rrpc, instance);
698 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
699 uint8_t npages = rqd->nr_pages;
700 sector_t laddr = rrpc_get_laddr(rqd->bio) - npages;
702 if (bio_data_dir(rqd->bio) == WRITE)
703 rrpc_end_io_write(rrpc, rrqd, laddr, npages);
705 bio_put(rqd->bio);
707 if (rrqd->flags & NVM_IOTYPE_GC)
708 return;
710 rrpc_unlock_rq(rrpc, rqd);
712 if (npages > 1)
713 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
714 if (rqd->metadata)
715 nvm_dev_dma_free(rrpc->dev, rqd->metadata, rqd->dma_metadata);
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;
727 int i;
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];
739 if (gp->rblk) {
740 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
741 gp->addr);
742 } else {
743 BUG_ON(is_gc);
744 rrpc_unlock_laddr(rrpc, r);
745 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
746 rqd->dma_ppa_list);
747 return NVM_IO_DONE;
751 rqd->opcode = NVM_OP_HBREAD;
753 return NVM_IO_OK;
756 static int rrpc_read_rq(struct rrpc *rrpc, struct bio *bio, struct nvm_rq *rqd,
757 unsigned long flags)
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];
770 if (gp->rblk) {
771 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, gp->addr);
772 } else {
773 BUG_ON(is_gc);
774 rrpc_unlock_rq(rrpc, rqd);
775 return NVM_IO_DONE;
778 rqd->opcode = NVM_OP_HBREAD;
779 rrqd->addr = gp;
781 return NVM_IO_OK;
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);
788 struct rrpc_addr *p;
789 sector_t laddr = rrpc_get_laddr(bio);
790 int is_gc = flags & NVM_IOTYPE_GC;
791 int i;
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);
801 if (!p) {
802 BUG_ON(is_gc);
803 rrpc_unlock_laddr(rrpc, r);
804 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
805 rqd->dma_ppa_list);
806 rrpc_gc_kick(rrpc);
807 return NVM_IO_REQUEUE;
810 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
811 p->addr);
814 rqd->opcode = NVM_OP_HBWRITE;
816 return NVM_IO_OK;
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);
823 struct rrpc_addr *p;
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);
831 if (!p) {
832 BUG_ON(is_gc);
833 rrpc_unlock_rq(rrpc, rqd);
834 rrpc_gc_kick(rrpc);
835 return NVM_IO_REQUEUE;
838 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, p->addr);
839 rqd->opcode = NVM_OP_HBWRITE;
840 rrqd->addr = p;
842 return NVM_IO_OK;
845 static int rrpc_setup_rq(struct rrpc *rrpc, struct bio *bio,
846 struct nvm_rq *rqd, unsigned long flags, uint8_t npages)
848 if (npages > 1) {
849 rqd->ppa_list = nvm_dev_dma_alloc(rrpc->dev, GFP_KERNEL,
850 &rqd->dma_ppa_list);
851 if (!rqd->ppa_list) {
852 pr_err("rrpc: not able to allocate ppa list\n");
853 return NVM_IO_ERR;
856 if (bio_rw(bio) == WRITE)
857 return rrpc_write_ppalist_rq(rrpc, bio, rqd, flags,
858 npages);
860 return rrpc_read_ppalist_rq(rrpc, bio, rqd, flags, npages);
863 if (bio_rw(bio) == 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)
872 int err;
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)
878 return NVM_IO_ERR;
879 else if (bio_size > rrpc->dev->max_rq_size)
880 return NVM_IO_ERR;
882 err = rrpc_setup_rq(rrpc, bio, rqd, flags, nr_pages);
883 if (err)
884 return err;
886 bio_get(bio);
887 rqd->bio = bio;
888 rqd->ins = &rrpc->instance;
889 rqd->nr_pages = nr_pages;
890 rrq->flags = flags;
892 err = nvm_submit_io(rrpc->dev, rqd);
893 if (err) {
894 pr_err("rrpc: I/O submission failed: %d\n", err);
895 bio_put(bio);
896 if (!(flags & NVM_IOTYPE_GC)) {
897 rrpc_unlock_rq(rrpc, rqd);
898 if (rqd->nr_pages > 1)
899 nvm_dev_dma_free(rrpc->dev,
900 rqd->ppa_list, rqd->dma_ppa_list);
902 return NVM_IO_ERR;
905 return NVM_IO_OK;
908 static blk_qc_t rrpc_make_rq(struct request_queue *q, struct bio *bio)
910 struct rrpc *rrpc = q->queuedata;
911 struct nvm_rq *rqd;
912 int err;
914 if (bio->bi_rw & REQ_DISCARD) {
915 rrpc_discard(rrpc, bio);
916 return BLK_QC_T_NONE;
919 rqd = mempool_alloc(rrpc->rq_pool, GFP_KERNEL);
920 if (!rqd) {
921 pr_err_ratelimited("rrpc: not able to queue bio.");
922 bio_io_error(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);
928 switch (err) {
929 case NVM_IO_OK:
930 return BLK_QC_T_NONE;
931 case NVM_IO_ERR:
932 bio_io_error(bio);
933 break;
934 case NVM_IO_DONE:
935 bio_endio(bio);
936 break;
937 case NVM_IO_REQUEUE:
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);
942 break;
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;
953 struct bio *bio;
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)
968 if (rrpc->krqd_wq)
969 destroy_workqueue(rrpc->krqd_wq);
971 if (rrpc->kgc_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,
978 rrpc->nr_luns);
979 if (!rrpc->krqd_wq)
980 return -ENOMEM;
982 rrpc->kgc_wq = alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM, 1);
983 if (!rrpc->kgc_wq)
984 return -ENOMEM;
986 setup_timer(&rrpc->gc_timer, rrpc_gc_timer, (unsigned long)rrpc);
988 return 0;
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;
1004 u64 i;
1006 if (unlikely(elba > dev->total_secs)) {
1007 pr_err("nvm: L2P data from device is out of bounds!\n");
1008 return -EINVAL;
1011 for (i = 0; i < nlb; i++) {
1012 u64 pba = le64_to_cpu(entries[i]);
1013 unsigned int mod;
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");
1019 return -EINVAL;
1022 /* Address zero is a special one. The first page on a disk is
1023 * protected. As it often holds internal device boot
1024 * information.
1026 if (!pba)
1027 continue;
1029 div_u64_rem(pba, rrpc->nr_sects, &mod);
1031 addr[i].addr = pba;
1032 raddr[mod].addr = slba + i;
1035 return 0;
1038 static int rrpc_map_init(struct rrpc *rrpc)
1040 struct nvm_dev *dev = rrpc->dev;
1041 sector_t i;
1042 u64 slba;
1043 int ret;
1045 slba = rrpc->soffset >> (ilog2(dev->sec_size) - 9);
1047 rrpc->trans_map = vzalloc(sizeof(struct rrpc_addr) * rrpc->nr_sects);
1048 if (!rrpc->trans_map)
1049 return -ENOMEM;
1051 rrpc->rev_trans_map = vmalloc(sizeof(struct rrpc_rev_addr)
1052 * rrpc->nr_sects);
1053 if (!rrpc->rev_trans_map)
1054 return -ENOMEM;
1056 for (i = 0; i < rrpc->nr_sects; i++) {
1057 struct rrpc_addr *p = &rrpc->trans_map[i];
1058 struct rrpc_rev_addr *r = &rrpc->rev_trans_map[i];
1060 p->addr = ADDR_EMPTY;
1061 r->addr = ADDR_EMPTY;
1064 if (!dev->ops->get_l2p_tbl)
1065 return 0;
1067 /* Bring up the mapping table from device */
1068 ret = dev->ops->get_l2p_tbl(dev, slba, rrpc->nr_sects, rrpc_l2p_update,
1069 rrpc);
1070 if (ret) {
1071 pr_err("nvm: rrpc: could not read L2P table.\n");
1072 return -EINVAL;
1075 return 0;
1078 /* Minimum pages needed within a lun */
1079 #define PAGE_POOL_SIZE 16
1080 #define ADDR_POOL_SIZE 64
1082 static int rrpc_core_init(struct rrpc *rrpc)
1084 down_write(&rrpc_lock);
1085 if (!rrpc_gcb_cache) {
1086 rrpc_gcb_cache = kmem_cache_create("rrpc_gcb",
1087 sizeof(struct rrpc_block_gc), 0, 0, NULL);
1088 if (!rrpc_gcb_cache) {
1089 up_write(&rrpc_lock);
1090 return -ENOMEM;
1093 rrpc_rq_cache = kmem_cache_create("rrpc_rq",
1094 sizeof(struct nvm_rq) + sizeof(struct rrpc_rq),
1095 0, 0, NULL);
1096 if (!rrpc_rq_cache) {
1097 kmem_cache_destroy(rrpc_gcb_cache);
1098 up_write(&rrpc_lock);
1099 return -ENOMEM;
1102 up_write(&rrpc_lock);
1104 rrpc->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0);
1105 if (!rrpc->page_pool)
1106 return -ENOMEM;
1108 rrpc->gcb_pool = mempool_create_slab_pool(rrpc->dev->nr_luns,
1109 rrpc_gcb_cache);
1110 if (!rrpc->gcb_pool)
1111 return -ENOMEM;
1113 rrpc->rq_pool = mempool_create_slab_pool(64, rrpc_rq_cache);
1114 if (!rrpc->rq_pool)
1115 return -ENOMEM;
1117 spin_lock_init(&rrpc->inflights.lock);
1118 INIT_LIST_HEAD(&rrpc->inflights.reqs);
1120 return 0;
1123 static void rrpc_core_free(struct rrpc *rrpc)
1125 mempool_destroy(rrpc->page_pool);
1126 mempool_destroy(rrpc->gcb_pool);
1127 mempool_destroy(rrpc->rq_pool);
1130 static void rrpc_luns_free(struct rrpc *rrpc)
1132 struct nvm_dev *dev = rrpc->dev;
1133 struct nvm_lun *lun;
1134 struct rrpc_lun *rlun;
1135 int i;
1137 if (!rrpc->luns)
1138 return;
1140 for (i = 0; i < rrpc->nr_luns; i++) {
1141 rlun = &rrpc->luns[i];
1142 lun = rlun->parent;
1143 if (!lun)
1144 break;
1145 dev->mt->release_lun(dev, lun->id);
1146 vfree(rlun->blocks);
1149 kfree(rrpc->luns);
1152 static int rrpc_luns_init(struct rrpc *rrpc, int lun_begin, int lun_end)
1154 struct nvm_dev *dev = rrpc->dev;
1155 struct rrpc_lun *rlun;
1156 int i, j, ret = -EINVAL;
1158 if (dev->sec_per_blk > MAX_INVALID_PAGES_STORAGE * BITS_PER_LONG) {
1159 pr_err("rrpc: number of pages per block too high.");
1160 return -EINVAL;
1163 spin_lock_init(&rrpc->rev_lock);
1165 rrpc->luns = kcalloc(rrpc->nr_luns, sizeof(struct rrpc_lun),
1166 GFP_KERNEL);
1167 if (!rrpc->luns)
1168 return -ENOMEM;
1170 /* 1:1 mapping */
1171 for (i = 0; i < rrpc->nr_luns; i++) {
1172 int lunid = lun_begin + i;
1173 struct nvm_lun *lun;
1175 if (dev->mt->reserve_lun(dev, lunid)) {
1176 pr_err("rrpc: lun %u is already allocated\n", lunid);
1177 goto err;
1180 lun = dev->mt->get_lun(dev, lunid);
1181 if (!lun)
1182 goto err;
1184 rlun = &rrpc->luns[i];
1185 rlun->parent = lun;
1186 rlun->blocks = vzalloc(sizeof(struct rrpc_block) *
1187 rrpc->dev->blks_per_lun);
1188 if (!rlun->blocks) {
1189 ret = -ENOMEM;
1190 goto err;
1193 for (j = 0; j < rrpc->dev->blks_per_lun; j++) {
1194 struct rrpc_block *rblk = &rlun->blocks[j];
1195 struct nvm_block *blk = &lun->blocks[j];
1197 rblk->parent = blk;
1198 rblk->rlun = rlun;
1199 INIT_LIST_HEAD(&rblk->prio);
1200 spin_lock_init(&rblk->lock);
1203 rlun->rrpc = rrpc;
1204 INIT_LIST_HEAD(&rlun->prio_list);
1205 INIT_LIST_HEAD(&rlun->open_list);
1206 INIT_LIST_HEAD(&rlun->closed_list);
1208 INIT_WORK(&rlun->ws_gc, rrpc_lun_gc);
1209 spin_lock_init(&rlun->lock);
1211 rrpc->total_blocks += dev->blks_per_lun;
1212 rrpc->nr_sects += dev->sec_per_lun;
1216 return 0;
1217 err:
1218 return ret;
1221 /* returns 0 on success and stores the beginning address in *begin */
1222 static int rrpc_area_init(struct rrpc *rrpc, sector_t *begin)
1224 struct nvm_dev *dev = rrpc->dev;
1225 struct nvmm_type *mt = dev->mt;
1226 sector_t size = rrpc->nr_sects * dev->sec_size;
1228 size >>= 9;
1230 return mt->get_area(dev, begin, size);
1233 static void rrpc_area_free(struct rrpc *rrpc)
1235 struct nvm_dev *dev = rrpc->dev;
1236 struct nvmm_type *mt = dev->mt;
1238 mt->put_area(dev, rrpc->soffset);
1241 static void rrpc_free(struct rrpc *rrpc)
1243 rrpc_gc_free(rrpc);
1244 rrpc_map_free(rrpc);
1245 rrpc_core_free(rrpc);
1246 rrpc_luns_free(rrpc);
1247 rrpc_area_free(rrpc);
1249 kfree(rrpc);
1252 static void rrpc_exit(void *private)
1254 struct rrpc *rrpc = private;
1256 del_timer(&rrpc->gc_timer);
1258 flush_workqueue(rrpc->krqd_wq);
1259 flush_workqueue(rrpc->kgc_wq);
1261 rrpc_free(rrpc);
1264 static sector_t rrpc_capacity(void *private)
1266 struct rrpc *rrpc = private;
1267 struct nvm_dev *dev = rrpc->dev;
1268 sector_t reserved, provisioned;
1270 /* cur, gc, and two emergency blocks for each lun */
1271 reserved = rrpc->nr_luns * dev->max_pages_per_blk * 4;
1272 provisioned = rrpc->nr_sects - reserved;
1274 if (reserved > rrpc->nr_sects) {
1275 pr_err("rrpc: not enough space available to expose storage.\n");
1276 return 0;
1279 sector_div(provisioned, 10);
1280 return provisioned * 9 * NR_PHY_IN_LOG;
1284 * Looks up the logical address from reverse trans map and check if its valid by
1285 * comparing the logical to physical address with the physical address.
1286 * Returns 0 on free, otherwise 1 if in use
1288 static void rrpc_block_map_update(struct rrpc *rrpc, struct rrpc_block *rblk)
1290 struct nvm_dev *dev = rrpc->dev;
1291 int offset;
1292 struct rrpc_addr *laddr;
1293 u64 bpaddr, paddr, pladdr;
1295 bpaddr = block_to_rel_addr(rrpc, rblk);
1296 for (offset = 0; offset < dev->sec_per_blk; offset++) {
1297 paddr = bpaddr + offset;
1299 pladdr = rrpc->rev_trans_map[paddr].addr;
1300 if (pladdr == ADDR_EMPTY)
1301 continue;
1303 laddr = &rrpc->trans_map[pladdr];
1305 if (paddr == laddr->addr) {
1306 laddr->rblk = rblk;
1307 } else {
1308 set_bit(offset, rblk->invalid_pages);
1309 rblk->nr_invalid_pages++;
1314 static int rrpc_blocks_init(struct rrpc *rrpc)
1316 struct rrpc_lun *rlun;
1317 struct rrpc_block *rblk;
1318 int lun_iter, blk_iter;
1320 for (lun_iter = 0; lun_iter < rrpc->nr_luns; lun_iter++) {
1321 rlun = &rrpc->luns[lun_iter];
1323 for (blk_iter = 0; blk_iter < rrpc->dev->blks_per_lun;
1324 blk_iter++) {
1325 rblk = &rlun->blocks[blk_iter];
1326 rrpc_block_map_update(rrpc, rblk);
1330 return 0;
1333 static int rrpc_luns_configure(struct rrpc *rrpc)
1335 struct rrpc_lun *rlun;
1336 struct rrpc_block *rblk;
1337 int i;
1339 for (i = 0; i < rrpc->nr_luns; i++) {
1340 rlun = &rrpc->luns[i];
1342 rblk = rrpc_get_blk(rrpc, rlun, 0);
1343 if (!rblk)
1344 goto err;
1346 rrpc_set_lun_cur(rlun, rblk);
1348 /* Emergency gc block */
1349 rblk = rrpc_get_blk(rrpc, rlun, 1);
1350 if (!rblk)
1351 goto err;
1352 rlun->gc_cur = rblk;
1355 return 0;
1356 err:
1357 rrpc_put_blks(rrpc);
1358 return -EINVAL;
1361 static struct nvm_tgt_type tt_rrpc;
1363 static void *rrpc_init(struct nvm_dev *dev, struct gendisk *tdisk,
1364 int lun_begin, int lun_end)
1366 struct request_queue *bqueue = dev->q;
1367 struct request_queue *tqueue = tdisk->queue;
1368 struct rrpc *rrpc;
1369 sector_t soffset;
1370 int ret;
1372 if (!(dev->identity.dom & NVM_RSP_L2P)) {
1373 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1374 dev->identity.dom);
1375 return ERR_PTR(-EINVAL);
1378 rrpc = kzalloc(sizeof(struct rrpc), GFP_KERNEL);
1379 if (!rrpc)
1380 return ERR_PTR(-ENOMEM);
1382 rrpc->instance.tt = &tt_rrpc;
1383 rrpc->dev = dev;
1384 rrpc->disk = tdisk;
1386 bio_list_init(&rrpc->requeue_bios);
1387 spin_lock_init(&rrpc->bio_lock);
1388 INIT_WORK(&rrpc->ws_requeue, rrpc_requeue);
1390 rrpc->nr_luns = lun_end - lun_begin + 1;
1392 /* simple round-robin strategy */
1393 atomic_set(&rrpc->next_lun, -1);
1395 ret = rrpc_area_init(rrpc, &soffset);
1396 if (ret < 0) {
1397 pr_err("nvm: rrpc: could not initialize area\n");
1398 return ERR_PTR(ret);
1400 rrpc->soffset = soffset;
1402 ret = rrpc_luns_init(rrpc, lun_begin, lun_end);
1403 if (ret) {
1404 pr_err("nvm: rrpc: could not initialize luns\n");
1405 goto err;
1408 rrpc->poffset = dev->sec_per_lun * lun_begin;
1409 rrpc->lun_offset = lun_begin;
1411 ret = rrpc_core_init(rrpc);
1412 if (ret) {
1413 pr_err("nvm: rrpc: could not initialize core\n");
1414 goto err;
1417 ret = rrpc_map_init(rrpc);
1418 if (ret) {
1419 pr_err("nvm: rrpc: could not initialize maps\n");
1420 goto err;
1423 ret = rrpc_blocks_init(rrpc);
1424 if (ret) {
1425 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1426 goto err;
1429 ret = rrpc_luns_configure(rrpc);
1430 if (ret) {
1431 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1432 goto err;
1435 ret = rrpc_gc_init(rrpc);
1436 if (ret) {
1437 pr_err("nvm: rrpc: could not initialize gc\n");
1438 goto err;
1441 /* inherit the size from the underlying device */
1442 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1443 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1445 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
1446 rrpc->nr_luns, (unsigned long long)rrpc->nr_sects);
1448 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
1450 return rrpc;
1451 err:
1452 rrpc_free(rrpc);
1453 return ERR_PTR(ret);
1456 /* round robin, page-based FTL, and cost-based GC */
1457 static struct nvm_tgt_type tt_rrpc = {
1458 .name = "rrpc",
1459 .version = {1, 0, 0},
1461 .make_rq = rrpc_make_rq,
1462 .capacity = rrpc_capacity,
1463 .end_io = rrpc_end_io,
1465 .init = rrpc_init,
1466 .exit = rrpc_exit,
1469 static int __init rrpc_module_init(void)
1471 return nvm_register_target(&tt_rrpc);
1474 static void rrpc_module_exit(void)
1476 nvm_unregister_target(&tt_rrpc);
1479 module_init(rrpc_module_init);
1480 module_exit(rrpc_module_exit);
1481 MODULE_LICENSE("GPL v2");
1482 MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");