Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 IT University of Copenhagen |
| 3 | * Initial release: Matias Bjorling <m@bjorling.me> |
| 4 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 14 | * Implementation of a Round-robin page-based Hybrid FTL for Open-channel SSDs. |
| 15 | */ |
| 16 | |
| 17 | #include "rrpc.h" |
| 18 | |
| 19 | static struct kmem_cache *rrpc_gcb_cache, *rrpc_rq_cache; |
| 20 | static DECLARE_RWSEM(rrpc_lock); |
| 21 | |
| 22 | static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio, |
| 23 | struct nvm_rq *rqd, unsigned long flags); |
| 24 | |
| 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)]) |
| 28 | |
| 29 | static void rrpc_page_invalidate(struct rrpc *rrpc, struct rrpc_addr *a) |
| 30 | { |
| 31 | struct rrpc_block *rblk = a->rblk; |
| 32 | unsigned int pg_offset; |
| 33 | |
| 34 | lockdep_assert_held(&rrpc->rev_lock); |
| 35 | |
| 36 | if (a->addr == ADDR_EMPTY || !rblk) |
| 37 | return; |
| 38 | |
| 39 | spin_lock(&rblk->lock); |
| 40 | |
| 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++; |
| 44 | |
| 45 | spin_unlock(&rblk->lock); |
| 46 | |
| 47 | rrpc->rev_trans_map[a->addr - rrpc->poffset].addr = ADDR_EMPTY; |
| 48 | } |
| 49 | |
| 50 | static void rrpc_invalidate_range(struct rrpc *rrpc, sector_t slba, |
| 51 | unsigned len) |
| 52 | { |
| 53 | sector_t i; |
| 54 | |
| 55 | spin_lock(&rrpc->rev_lock); |
| 56 | for (i = slba; i < slba + len; i++) { |
| 57 | struct rrpc_addr *gp = &rrpc->trans_map[i]; |
| 58 | |
| 59 | rrpc_page_invalidate(rrpc, gp); |
| 60 | gp->rblk = NULL; |
| 61 | } |
| 62 | spin_unlock(&rrpc->rev_lock); |
| 63 | } |
| 64 | |
| 65 | static struct nvm_rq *rrpc_inflight_laddr_acquire(struct rrpc *rrpc, |
| 66 | sector_t laddr, unsigned int pages) |
| 67 | { |
| 68 | struct nvm_rq *rqd; |
| 69 | struct rrpc_inflight_rq *inf; |
| 70 | |
| 71 | rqd = mempool_alloc(rrpc->rq_pool, GFP_ATOMIC); |
| 72 | if (!rqd) |
| 73 | return ERR_PTR(-ENOMEM); |
| 74 | |
| 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; |
| 79 | } |
| 80 | |
| 81 | return rqd; |
| 82 | } |
| 83 | |
| 84 | static void rrpc_inflight_laddr_release(struct rrpc *rrpc, struct nvm_rq *rqd) |
| 85 | { |
| 86 | struct rrpc_inflight_rq *inf = rrpc_get_inflight_rq(rqd); |
| 87 | |
| 88 | rrpc_unlock_laddr(rrpc, inf); |
| 89 | |
| 90 | mempool_free(rqd, rrpc->rq_pool); |
| 91 | } |
| 92 | |
| 93 | static void rrpc_discard(struct rrpc *rrpc, struct bio *bio) |
| 94 | { |
| 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; |
| 98 | |
| 99 | do { |
| 100 | rqd = rrpc_inflight_laddr_acquire(rrpc, slba, len); |
| 101 | schedule(); |
| 102 | } while (!rqd); |
| 103 | |
| 104 | if (IS_ERR(rqd)) { |
| 105 | pr_err("rrpc: unable to acquire inflight IO\n"); |
| 106 | bio_io_error(bio); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | rrpc_invalidate_range(rrpc, slba, len); |
| 111 | rrpc_inflight_laddr_release(rrpc, rqd); |
| 112 | } |
| 113 | |
| 114 | static int block_is_full(struct rrpc *rrpc, struct rrpc_block *rblk) |
| 115 | { |
| 116 | return (rblk->next_page == rrpc->dev->pgs_per_blk); |
| 117 | } |
| 118 | |
Matias Bjørling | b7ceb7d | 2015-11-02 17:12:27 +0100 | [diff] [blame] | 119 | static u64 block_to_addr(struct rrpc *rrpc, struct rrpc_block *rblk) |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 120 | { |
| 121 | struct nvm_block *blk = rblk->parent; |
| 122 | |
| 123 | return blk->id * rrpc->dev->pgs_per_blk; |
| 124 | } |
| 125 | |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 126 | static struct ppa_addr linear_to_generic_addr(struct nvm_dev *dev, |
| 127 | struct ppa_addr r) |
| 128 | { |
| 129 | struct ppa_addr l; |
| 130 | int secs, pgs, blks, luns; |
| 131 | sector_t ppa = r.ppa; |
| 132 | |
| 133 | l.ppa = 0; |
| 134 | |
| 135 | div_u64_rem(ppa, dev->sec_per_pg, &secs); |
| 136 | l.g.sec = secs; |
| 137 | |
| 138 | sector_div(ppa, dev->sec_per_pg); |
| 139 | div_u64_rem(ppa, dev->sec_per_blk, &pgs); |
| 140 | l.g.pg = pgs; |
| 141 | |
| 142 | sector_div(ppa, dev->pgs_per_blk); |
| 143 | div_u64_rem(ppa, dev->blks_per_lun, &blks); |
| 144 | l.g.blk = blks; |
| 145 | |
| 146 | sector_div(ppa, dev->blks_per_lun); |
| 147 | div_u64_rem(ppa, dev->luns_per_chnl, &luns); |
| 148 | l.g.lun = luns; |
| 149 | |
| 150 | sector_div(ppa, dev->luns_per_chnl); |
| 151 | l.g.ch = ppa; |
| 152 | |
| 153 | return l; |
| 154 | } |
| 155 | |
Matias Bjørling | b7ceb7d | 2015-11-02 17:12:27 +0100 | [diff] [blame] | 156 | static struct ppa_addr rrpc_ppa_to_gaddr(struct nvm_dev *dev, u64 addr) |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 157 | { |
| 158 | struct ppa_addr paddr; |
| 159 | |
| 160 | paddr.ppa = addr; |
Matias Bjørling | 7386af2 | 2015-11-16 15:34:44 +0100 | [diff] [blame] | 161 | return linear_to_generic_addr(dev, paddr); |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* requires lun->lock taken */ |
| 165 | static void rrpc_set_lun_cur(struct rrpc_lun *rlun, struct rrpc_block *rblk) |
| 166 | { |
| 167 | struct rrpc *rrpc = rlun->rrpc; |
| 168 | |
| 169 | BUG_ON(!rblk); |
| 170 | |
| 171 | if (rlun->cur) { |
| 172 | spin_lock(&rlun->cur->lock); |
| 173 | WARN_ON(!block_is_full(rrpc, rlun->cur)); |
| 174 | spin_unlock(&rlun->cur->lock); |
| 175 | } |
| 176 | rlun->cur = rblk; |
| 177 | } |
| 178 | |
| 179 | static struct rrpc_block *rrpc_get_blk(struct rrpc *rrpc, struct rrpc_lun *rlun, |
| 180 | unsigned long flags) |
| 181 | { |
| 182 | struct nvm_block *blk; |
| 183 | struct rrpc_block *rblk; |
| 184 | |
Wenwei Tao | f27a629 | 2015-12-06 11:25:43 +0100 | [diff] [blame^] | 185 | blk = nvm_get_blk(rrpc->dev, rlun->parent, flags); |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 186 | if (!blk) |
| 187 | return NULL; |
| 188 | |
| 189 | rblk = &rlun->blocks[blk->id]; |
| 190 | blk->priv = rblk; |
| 191 | |
| 192 | bitmap_zero(rblk->invalid_pages, rrpc->dev->pgs_per_blk); |
| 193 | rblk->next_page = 0; |
| 194 | rblk->nr_invalid_pages = 0; |
| 195 | atomic_set(&rblk->data_cmnt_size, 0); |
| 196 | |
| 197 | return rblk; |
| 198 | } |
| 199 | |
| 200 | static void rrpc_put_blk(struct rrpc *rrpc, struct rrpc_block *rblk) |
| 201 | { |
| 202 | nvm_put_blk(rrpc->dev, rblk->parent); |
| 203 | } |
| 204 | |
| 205 | static struct rrpc_lun *get_next_lun(struct rrpc *rrpc) |
| 206 | { |
| 207 | int next = atomic_inc_return(&rrpc->next_lun); |
| 208 | |
| 209 | return &rrpc->luns[next % rrpc->nr_luns]; |
| 210 | } |
| 211 | |
| 212 | static void rrpc_gc_kick(struct rrpc *rrpc) |
| 213 | { |
| 214 | struct rrpc_lun *rlun; |
| 215 | unsigned int i; |
| 216 | |
| 217 | for (i = 0; i < rrpc->nr_luns; i++) { |
| 218 | rlun = &rrpc->luns[i]; |
| 219 | queue_work(rrpc->krqd_wq, &rlun->ws_gc); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * timed GC every interval. |
| 225 | */ |
| 226 | static void rrpc_gc_timer(unsigned long data) |
| 227 | { |
| 228 | struct rrpc *rrpc = (struct rrpc *)data; |
| 229 | |
| 230 | rrpc_gc_kick(rrpc); |
| 231 | mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10)); |
| 232 | } |
| 233 | |
| 234 | static void rrpc_end_sync_bio(struct bio *bio) |
| 235 | { |
| 236 | struct completion *waiting = bio->bi_private; |
| 237 | |
| 238 | if (bio->bi_error) |
| 239 | pr_err("nvm: gc request failed (%u).\n", bio->bi_error); |
| 240 | |
| 241 | complete(waiting); |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * rrpc_move_valid_pages -- migrate live data off the block |
| 246 | * @rrpc: the 'rrpc' structure |
| 247 | * @block: the block from which to migrate live pages |
| 248 | * |
| 249 | * Description: |
| 250 | * GC algorithms may call this function to migrate remaining live |
| 251 | * pages off the block prior to erasing it. This function blocks |
| 252 | * further execution until the operation is complete. |
| 253 | */ |
| 254 | static int rrpc_move_valid_pages(struct rrpc *rrpc, struct rrpc_block *rblk) |
| 255 | { |
| 256 | struct request_queue *q = rrpc->dev->q; |
| 257 | struct rrpc_rev_addr *rev; |
| 258 | struct nvm_rq *rqd; |
| 259 | struct bio *bio; |
| 260 | struct page *page; |
| 261 | int slot; |
| 262 | int nr_pgs_per_blk = rrpc->dev->pgs_per_blk; |
Matias Bjørling | b7ceb7d | 2015-11-02 17:12:27 +0100 | [diff] [blame] | 263 | u64 phys_addr; |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 264 | DECLARE_COMPLETION_ONSTACK(wait); |
| 265 | |
| 266 | if (bitmap_full(rblk->invalid_pages, nr_pgs_per_blk)) |
| 267 | return 0; |
| 268 | |
| 269 | bio = bio_alloc(GFP_NOIO, 1); |
| 270 | if (!bio) { |
| 271 | pr_err("nvm: could not alloc bio to gc\n"); |
| 272 | return -ENOMEM; |
| 273 | } |
| 274 | |
| 275 | page = mempool_alloc(rrpc->page_pool, GFP_NOIO); |
| 276 | |
| 277 | while ((slot = find_first_zero_bit(rblk->invalid_pages, |
| 278 | nr_pgs_per_blk)) < nr_pgs_per_blk) { |
| 279 | |
| 280 | /* Lock laddr */ |
| 281 | phys_addr = (rblk->parent->id * nr_pgs_per_blk) + slot; |
| 282 | |
| 283 | try: |
| 284 | spin_lock(&rrpc->rev_lock); |
| 285 | /* Get logical address from physical to logical table */ |
| 286 | rev = &rrpc->rev_trans_map[phys_addr - rrpc->poffset]; |
| 287 | /* already updated by previous regular write */ |
| 288 | if (rev->addr == ADDR_EMPTY) { |
| 289 | spin_unlock(&rrpc->rev_lock); |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | rqd = rrpc_inflight_laddr_acquire(rrpc, rev->addr, 1); |
| 294 | if (IS_ERR_OR_NULL(rqd)) { |
| 295 | spin_unlock(&rrpc->rev_lock); |
| 296 | schedule(); |
| 297 | goto try; |
| 298 | } |
| 299 | |
| 300 | spin_unlock(&rrpc->rev_lock); |
| 301 | |
| 302 | /* Perform read to do GC */ |
| 303 | bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr); |
| 304 | bio->bi_rw = READ; |
| 305 | bio->bi_private = &wait; |
| 306 | bio->bi_end_io = rrpc_end_sync_bio; |
| 307 | |
| 308 | /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */ |
| 309 | bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0); |
| 310 | |
| 311 | if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) { |
| 312 | pr_err("rrpc: gc read failed.\n"); |
| 313 | rrpc_inflight_laddr_release(rrpc, rqd); |
| 314 | goto finished; |
| 315 | } |
| 316 | wait_for_completion_io(&wait); |
| 317 | |
| 318 | bio_reset(bio); |
| 319 | reinit_completion(&wait); |
| 320 | |
| 321 | bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr); |
| 322 | bio->bi_rw = WRITE; |
| 323 | bio->bi_private = &wait; |
| 324 | bio->bi_end_io = rrpc_end_sync_bio; |
| 325 | |
| 326 | bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0); |
| 327 | |
| 328 | /* turn the command around and write the data back to a new |
| 329 | * address |
| 330 | */ |
| 331 | if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) { |
| 332 | pr_err("rrpc: gc write failed.\n"); |
| 333 | rrpc_inflight_laddr_release(rrpc, rqd); |
| 334 | goto finished; |
| 335 | } |
| 336 | wait_for_completion_io(&wait); |
| 337 | |
| 338 | rrpc_inflight_laddr_release(rrpc, rqd); |
| 339 | |
| 340 | bio_reset(bio); |
| 341 | } |
| 342 | |
| 343 | finished: |
| 344 | mempool_free(page, rrpc->page_pool); |
| 345 | bio_put(bio); |
| 346 | |
| 347 | if (!bitmap_full(rblk->invalid_pages, nr_pgs_per_blk)) { |
| 348 | pr_err("nvm: failed to garbage collect block\n"); |
| 349 | return -EIO; |
| 350 | } |
| 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | |
| 355 | static void rrpc_block_gc(struct work_struct *work) |
| 356 | { |
| 357 | struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc, |
| 358 | ws_gc); |
| 359 | struct rrpc *rrpc = gcb->rrpc; |
| 360 | struct rrpc_block *rblk = gcb->rblk; |
| 361 | struct nvm_dev *dev = rrpc->dev; |
| 362 | |
| 363 | pr_debug("nvm: block '%lu' being reclaimed\n", rblk->parent->id); |
| 364 | |
| 365 | if (rrpc_move_valid_pages(rrpc, rblk)) |
| 366 | goto done; |
| 367 | |
| 368 | nvm_erase_blk(dev, rblk->parent); |
| 369 | rrpc_put_blk(rrpc, rblk); |
| 370 | done: |
| 371 | mempool_free(gcb, rrpc->gcb_pool); |
| 372 | } |
| 373 | |
| 374 | /* the block with highest number of invalid pages, will be in the beginning |
| 375 | * of the list |
| 376 | */ |
| 377 | static struct rrpc_block *rblock_max_invalid(struct rrpc_block *ra, |
| 378 | struct rrpc_block *rb) |
| 379 | { |
| 380 | if (ra->nr_invalid_pages == rb->nr_invalid_pages) |
| 381 | return ra; |
| 382 | |
| 383 | return (ra->nr_invalid_pages < rb->nr_invalid_pages) ? rb : ra; |
| 384 | } |
| 385 | |
| 386 | /* linearly find the block with highest number of invalid pages |
| 387 | * requires lun->lock |
| 388 | */ |
| 389 | static struct rrpc_block *block_prio_find_max(struct rrpc_lun *rlun) |
| 390 | { |
| 391 | struct list_head *prio_list = &rlun->prio_list; |
| 392 | struct rrpc_block *rblock, *max; |
| 393 | |
| 394 | BUG_ON(list_empty(prio_list)); |
| 395 | |
| 396 | max = list_first_entry(prio_list, struct rrpc_block, prio); |
| 397 | list_for_each_entry(rblock, prio_list, prio) |
| 398 | max = rblock_max_invalid(max, rblock); |
| 399 | |
| 400 | return max; |
| 401 | } |
| 402 | |
| 403 | static void rrpc_lun_gc(struct work_struct *work) |
| 404 | { |
| 405 | struct rrpc_lun *rlun = container_of(work, struct rrpc_lun, ws_gc); |
| 406 | struct rrpc *rrpc = rlun->rrpc; |
| 407 | struct nvm_lun *lun = rlun->parent; |
| 408 | struct rrpc_block_gc *gcb; |
| 409 | unsigned int nr_blocks_need; |
| 410 | |
| 411 | nr_blocks_need = rrpc->dev->blks_per_lun / GC_LIMIT_INVERSE; |
| 412 | |
| 413 | if (nr_blocks_need < rrpc->nr_luns) |
| 414 | nr_blocks_need = rrpc->nr_luns; |
| 415 | |
| 416 | spin_lock(&lun->lock); |
| 417 | while (nr_blocks_need > lun->nr_free_blocks && |
| 418 | !list_empty(&rlun->prio_list)) { |
| 419 | struct rrpc_block *rblock = block_prio_find_max(rlun); |
| 420 | struct nvm_block *block = rblock->parent; |
| 421 | |
| 422 | if (!rblock->nr_invalid_pages) |
| 423 | break; |
| 424 | |
| 425 | list_del_init(&rblock->prio); |
| 426 | |
| 427 | BUG_ON(!block_is_full(rrpc, rblock)); |
| 428 | |
| 429 | pr_debug("rrpc: selected block '%lu' for GC\n", block->id); |
| 430 | |
| 431 | gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC); |
| 432 | if (!gcb) |
| 433 | break; |
| 434 | |
| 435 | gcb->rrpc = rrpc; |
| 436 | gcb->rblk = rblock; |
| 437 | INIT_WORK(&gcb->ws_gc, rrpc_block_gc); |
| 438 | |
| 439 | queue_work(rrpc->kgc_wq, &gcb->ws_gc); |
| 440 | |
| 441 | nr_blocks_need--; |
| 442 | } |
| 443 | spin_unlock(&lun->lock); |
| 444 | |
| 445 | /* TODO: Hint that request queue can be started again */ |
| 446 | } |
| 447 | |
| 448 | static void rrpc_gc_queue(struct work_struct *work) |
| 449 | { |
| 450 | struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc, |
| 451 | ws_gc); |
| 452 | struct rrpc *rrpc = gcb->rrpc; |
| 453 | struct rrpc_block *rblk = gcb->rblk; |
| 454 | struct nvm_lun *lun = rblk->parent->lun; |
| 455 | struct rrpc_lun *rlun = &rrpc->luns[lun->id - rrpc->lun_offset]; |
| 456 | |
| 457 | spin_lock(&rlun->lock); |
| 458 | list_add_tail(&rblk->prio, &rlun->prio_list); |
| 459 | spin_unlock(&rlun->lock); |
| 460 | |
| 461 | mempool_free(gcb, rrpc->gcb_pool); |
| 462 | pr_debug("nvm: block '%lu' is full, allow GC (sched)\n", |
| 463 | rblk->parent->id); |
| 464 | } |
| 465 | |
| 466 | static const struct block_device_operations rrpc_fops = { |
| 467 | .owner = THIS_MODULE, |
| 468 | }; |
| 469 | |
| 470 | static struct rrpc_lun *rrpc_get_lun_rr(struct rrpc *rrpc, int is_gc) |
| 471 | { |
| 472 | unsigned int i; |
| 473 | struct rrpc_lun *rlun, *max_free; |
| 474 | |
| 475 | if (!is_gc) |
| 476 | return get_next_lun(rrpc); |
| 477 | |
| 478 | /* during GC, we don't care about RR, instead we want to make |
| 479 | * sure that we maintain evenness between the block luns. |
| 480 | */ |
| 481 | max_free = &rrpc->luns[0]; |
| 482 | /* prevent GC-ing lun from devouring pages of a lun with |
| 483 | * little free blocks. We don't take the lock as we only need an |
| 484 | * estimate. |
| 485 | */ |
| 486 | rrpc_for_each_lun(rrpc, rlun, i) { |
| 487 | if (rlun->parent->nr_free_blocks > |
| 488 | max_free->parent->nr_free_blocks) |
| 489 | max_free = rlun; |
| 490 | } |
| 491 | |
| 492 | return max_free; |
| 493 | } |
| 494 | |
| 495 | static struct rrpc_addr *rrpc_update_map(struct rrpc *rrpc, sector_t laddr, |
Matias Bjørling | b7ceb7d | 2015-11-02 17:12:27 +0100 | [diff] [blame] | 496 | struct rrpc_block *rblk, u64 paddr) |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 497 | { |
| 498 | struct rrpc_addr *gp; |
| 499 | struct rrpc_rev_addr *rev; |
| 500 | |
| 501 | BUG_ON(laddr >= rrpc->nr_pages); |
| 502 | |
| 503 | gp = &rrpc->trans_map[laddr]; |
| 504 | spin_lock(&rrpc->rev_lock); |
| 505 | if (gp->rblk) |
| 506 | rrpc_page_invalidate(rrpc, gp); |
| 507 | |
| 508 | gp->addr = paddr; |
| 509 | gp->rblk = rblk; |
| 510 | |
| 511 | rev = &rrpc->rev_trans_map[gp->addr - rrpc->poffset]; |
| 512 | rev->addr = laddr; |
| 513 | spin_unlock(&rrpc->rev_lock); |
| 514 | |
| 515 | return gp; |
| 516 | } |
| 517 | |
Matias Bjørling | b7ceb7d | 2015-11-02 17:12:27 +0100 | [diff] [blame] | 518 | static u64 rrpc_alloc_addr(struct rrpc *rrpc, struct rrpc_block *rblk) |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 519 | { |
Matias Bjørling | b7ceb7d | 2015-11-02 17:12:27 +0100 | [diff] [blame] | 520 | u64 addr = ADDR_EMPTY; |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 521 | |
| 522 | spin_lock(&rblk->lock); |
| 523 | if (block_is_full(rrpc, rblk)) |
| 524 | goto out; |
| 525 | |
| 526 | addr = block_to_addr(rrpc, rblk) + rblk->next_page; |
| 527 | |
| 528 | rblk->next_page++; |
| 529 | out: |
| 530 | spin_unlock(&rblk->lock); |
| 531 | return addr; |
| 532 | } |
| 533 | |
| 534 | /* Simple round-robin Logical to physical address translation. |
| 535 | * |
| 536 | * Retrieve the mapping using the active append point. Then update the ap for |
| 537 | * the next write to the disk. |
| 538 | * |
| 539 | * Returns rrpc_addr with the physical address and block. Remember to return to |
| 540 | * rrpc->addr_cache when request is finished. |
| 541 | */ |
| 542 | static struct rrpc_addr *rrpc_map_page(struct rrpc *rrpc, sector_t laddr, |
| 543 | int is_gc) |
| 544 | { |
| 545 | struct rrpc_lun *rlun; |
| 546 | struct rrpc_block *rblk; |
| 547 | struct nvm_lun *lun; |
Matias Bjørling | b7ceb7d | 2015-11-02 17:12:27 +0100 | [diff] [blame] | 548 | u64 paddr; |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 549 | |
| 550 | rlun = rrpc_get_lun_rr(rrpc, is_gc); |
| 551 | lun = rlun->parent; |
| 552 | |
| 553 | if (!is_gc && lun->nr_free_blocks < rrpc->nr_luns * 4) |
| 554 | return NULL; |
| 555 | |
| 556 | spin_lock(&rlun->lock); |
| 557 | |
| 558 | rblk = rlun->cur; |
| 559 | retry: |
| 560 | paddr = rrpc_alloc_addr(rrpc, rblk); |
| 561 | |
| 562 | if (paddr == ADDR_EMPTY) { |
| 563 | rblk = rrpc_get_blk(rrpc, rlun, 0); |
| 564 | if (rblk) { |
| 565 | rrpc_set_lun_cur(rlun, rblk); |
| 566 | goto retry; |
| 567 | } |
| 568 | |
| 569 | if (is_gc) { |
| 570 | /* retry from emergency gc block */ |
| 571 | paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur); |
| 572 | if (paddr == ADDR_EMPTY) { |
| 573 | rblk = rrpc_get_blk(rrpc, rlun, 1); |
| 574 | if (!rblk) { |
| 575 | pr_err("rrpc: no more blocks"); |
| 576 | goto err; |
| 577 | } |
| 578 | |
| 579 | rlun->gc_cur = rblk; |
| 580 | paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur); |
| 581 | } |
| 582 | rblk = rlun->gc_cur; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | spin_unlock(&rlun->lock); |
| 587 | return rrpc_update_map(rrpc, laddr, rblk, paddr); |
| 588 | err: |
| 589 | spin_unlock(&rlun->lock); |
| 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | static void rrpc_run_gc(struct rrpc *rrpc, struct rrpc_block *rblk) |
| 594 | { |
| 595 | struct rrpc_block_gc *gcb; |
| 596 | |
| 597 | gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC); |
| 598 | if (!gcb) { |
| 599 | pr_err("rrpc: unable to queue block for gc."); |
| 600 | return; |
| 601 | } |
| 602 | |
| 603 | gcb->rrpc = rrpc; |
| 604 | gcb->rblk = rblk; |
| 605 | |
| 606 | INIT_WORK(&gcb->ws_gc, rrpc_gc_queue); |
| 607 | queue_work(rrpc->kgc_wq, &gcb->ws_gc); |
| 608 | } |
| 609 | |
| 610 | static void rrpc_end_io_write(struct rrpc *rrpc, struct rrpc_rq *rrqd, |
| 611 | sector_t laddr, uint8_t npages) |
| 612 | { |
| 613 | struct rrpc_addr *p; |
| 614 | struct rrpc_block *rblk; |
| 615 | struct nvm_lun *lun; |
| 616 | int cmnt_size, i; |
| 617 | |
| 618 | for (i = 0; i < npages; i++) { |
| 619 | p = &rrpc->trans_map[laddr + i]; |
| 620 | rblk = p->rblk; |
| 621 | lun = rblk->parent->lun; |
| 622 | |
| 623 | cmnt_size = atomic_inc_return(&rblk->data_cmnt_size); |
| 624 | if (unlikely(cmnt_size == rrpc->dev->pgs_per_blk)) |
| 625 | rrpc_run_gc(rrpc, rblk); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | static int rrpc_end_io(struct nvm_rq *rqd, int error) |
| 630 | { |
| 631 | struct rrpc *rrpc = container_of(rqd->ins, struct rrpc, instance); |
| 632 | struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd); |
| 633 | uint8_t npages = rqd->nr_pages; |
| 634 | sector_t laddr = rrpc_get_laddr(rqd->bio) - npages; |
| 635 | |
| 636 | if (bio_data_dir(rqd->bio) == WRITE) |
| 637 | rrpc_end_io_write(rrpc, rrqd, laddr, npages); |
| 638 | |
| 639 | if (rrqd->flags & NVM_IOTYPE_GC) |
| 640 | return 0; |
| 641 | |
| 642 | rrpc_unlock_rq(rrpc, rqd); |
| 643 | bio_put(rqd->bio); |
| 644 | |
| 645 | if (npages > 1) |
| 646 | nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list); |
| 647 | if (rqd->metadata) |
| 648 | nvm_dev_dma_free(rrpc->dev, rqd->metadata, rqd->dma_metadata); |
| 649 | |
| 650 | mempool_free(rqd, rrpc->rq_pool); |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static int rrpc_read_ppalist_rq(struct rrpc *rrpc, struct bio *bio, |
| 656 | struct nvm_rq *rqd, unsigned long flags, int npages) |
| 657 | { |
| 658 | struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd); |
| 659 | struct rrpc_addr *gp; |
| 660 | sector_t laddr = rrpc_get_laddr(bio); |
| 661 | int is_gc = flags & NVM_IOTYPE_GC; |
| 662 | int i; |
| 663 | |
| 664 | if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) { |
| 665 | nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list); |
| 666 | return NVM_IO_REQUEUE; |
| 667 | } |
| 668 | |
| 669 | for (i = 0; i < npages; i++) { |
| 670 | /* We assume that mapping occurs at 4KB granularity */ |
| 671 | BUG_ON(!(laddr + i >= 0 && laddr + i < rrpc->nr_pages)); |
| 672 | gp = &rrpc->trans_map[laddr + i]; |
| 673 | |
| 674 | if (gp->rblk) { |
| 675 | rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev, |
| 676 | gp->addr); |
| 677 | } else { |
| 678 | BUG_ON(is_gc); |
| 679 | rrpc_unlock_laddr(rrpc, r); |
| 680 | nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, |
| 681 | rqd->dma_ppa_list); |
| 682 | return NVM_IO_DONE; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | rqd->opcode = NVM_OP_HBREAD; |
| 687 | |
| 688 | return NVM_IO_OK; |
| 689 | } |
| 690 | |
| 691 | static int rrpc_read_rq(struct rrpc *rrpc, struct bio *bio, struct nvm_rq *rqd, |
| 692 | unsigned long flags) |
| 693 | { |
| 694 | struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd); |
| 695 | int is_gc = flags & NVM_IOTYPE_GC; |
| 696 | sector_t laddr = rrpc_get_laddr(bio); |
| 697 | struct rrpc_addr *gp; |
| 698 | |
| 699 | if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) |
| 700 | return NVM_IO_REQUEUE; |
| 701 | |
| 702 | BUG_ON(!(laddr >= 0 && laddr < rrpc->nr_pages)); |
| 703 | gp = &rrpc->trans_map[laddr]; |
| 704 | |
| 705 | if (gp->rblk) { |
| 706 | rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, gp->addr); |
| 707 | } else { |
| 708 | BUG_ON(is_gc); |
| 709 | rrpc_unlock_rq(rrpc, rqd); |
| 710 | return NVM_IO_DONE; |
| 711 | } |
| 712 | |
| 713 | rqd->opcode = NVM_OP_HBREAD; |
| 714 | rrqd->addr = gp; |
| 715 | |
| 716 | return NVM_IO_OK; |
| 717 | } |
| 718 | |
| 719 | static int rrpc_write_ppalist_rq(struct rrpc *rrpc, struct bio *bio, |
| 720 | struct nvm_rq *rqd, unsigned long flags, int npages) |
| 721 | { |
| 722 | struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd); |
| 723 | struct rrpc_addr *p; |
| 724 | sector_t laddr = rrpc_get_laddr(bio); |
| 725 | int is_gc = flags & NVM_IOTYPE_GC; |
| 726 | int i; |
| 727 | |
| 728 | if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) { |
| 729 | nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list); |
| 730 | return NVM_IO_REQUEUE; |
| 731 | } |
| 732 | |
| 733 | for (i = 0; i < npages; i++) { |
| 734 | /* We assume that mapping occurs at 4KB granularity */ |
| 735 | p = rrpc_map_page(rrpc, laddr + i, is_gc); |
| 736 | if (!p) { |
| 737 | BUG_ON(is_gc); |
| 738 | rrpc_unlock_laddr(rrpc, r); |
| 739 | nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, |
| 740 | rqd->dma_ppa_list); |
| 741 | rrpc_gc_kick(rrpc); |
| 742 | return NVM_IO_REQUEUE; |
| 743 | } |
| 744 | |
| 745 | rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev, |
| 746 | p->addr); |
| 747 | } |
| 748 | |
| 749 | rqd->opcode = NVM_OP_HBWRITE; |
| 750 | |
| 751 | return NVM_IO_OK; |
| 752 | } |
| 753 | |
| 754 | static int rrpc_write_rq(struct rrpc *rrpc, struct bio *bio, |
| 755 | struct nvm_rq *rqd, unsigned long flags) |
| 756 | { |
| 757 | struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd); |
| 758 | struct rrpc_addr *p; |
| 759 | int is_gc = flags & NVM_IOTYPE_GC; |
| 760 | sector_t laddr = rrpc_get_laddr(bio); |
| 761 | |
| 762 | if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) |
| 763 | return NVM_IO_REQUEUE; |
| 764 | |
| 765 | p = rrpc_map_page(rrpc, laddr, is_gc); |
| 766 | if (!p) { |
| 767 | BUG_ON(is_gc); |
| 768 | rrpc_unlock_rq(rrpc, rqd); |
| 769 | rrpc_gc_kick(rrpc); |
| 770 | return NVM_IO_REQUEUE; |
| 771 | } |
| 772 | |
| 773 | rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, p->addr); |
| 774 | rqd->opcode = NVM_OP_HBWRITE; |
| 775 | rrqd->addr = p; |
| 776 | |
| 777 | return NVM_IO_OK; |
| 778 | } |
| 779 | |
| 780 | static int rrpc_setup_rq(struct rrpc *rrpc, struct bio *bio, |
| 781 | struct nvm_rq *rqd, unsigned long flags, uint8_t npages) |
| 782 | { |
| 783 | if (npages > 1) { |
| 784 | rqd->ppa_list = nvm_dev_dma_alloc(rrpc->dev, GFP_KERNEL, |
| 785 | &rqd->dma_ppa_list); |
| 786 | if (!rqd->ppa_list) { |
| 787 | pr_err("rrpc: not able to allocate ppa list\n"); |
| 788 | return NVM_IO_ERR; |
| 789 | } |
| 790 | |
| 791 | if (bio_rw(bio) == WRITE) |
| 792 | return rrpc_write_ppalist_rq(rrpc, bio, rqd, flags, |
| 793 | npages); |
| 794 | |
| 795 | return rrpc_read_ppalist_rq(rrpc, bio, rqd, flags, npages); |
| 796 | } |
| 797 | |
| 798 | if (bio_rw(bio) == WRITE) |
| 799 | return rrpc_write_rq(rrpc, bio, rqd, flags); |
| 800 | |
| 801 | return rrpc_read_rq(rrpc, bio, rqd, flags); |
| 802 | } |
| 803 | |
| 804 | static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio, |
| 805 | struct nvm_rq *rqd, unsigned long flags) |
| 806 | { |
| 807 | int err; |
| 808 | struct rrpc_rq *rrq = nvm_rq_to_pdu(rqd); |
| 809 | uint8_t nr_pages = rrpc_get_pages(bio); |
| 810 | int bio_size = bio_sectors(bio) << 9; |
| 811 | |
| 812 | if (bio_size < rrpc->dev->sec_size) |
| 813 | return NVM_IO_ERR; |
| 814 | else if (bio_size > rrpc->dev->max_rq_size) |
| 815 | return NVM_IO_ERR; |
| 816 | |
| 817 | err = rrpc_setup_rq(rrpc, bio, rqd, flags, nr_pages); |
| 818 | if (err) |
| 819 | return err; |
| 820 | |
| 821 | bio_get(bio); |
| 822 | rqd->bio = bio; |
| 823 | rqd->ins = &rrpc->instance; |
| 824 | rqd->nr_pages = nr_pages; |
| 825 | rrq->flags = flags; |
| 826 | |
| 827 | err = nvm_submit_io(rrpc->dev, rqd); |
| 828 | if (err) { |
| 829 | pr_err("rrpc: I/O submission failed: %d\n", err); |
| 830 | return NVM_IO_ERR; |
| 831 | } |
| 832 | |
| 833 | return NVM_IO_OK; |
| 834 | } |
| 835 | |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 836 | static blk_qc_t rrpc_make_rq(struct request_queue *q, struct bio *bio) |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 837 | { |
| 838 | struct rrpc *rrpc = q->queuedata; |
| 839 | struct nvm_rq *rqd; |
| 840 | int err; |
| 841 | |
| 842 | if (bio->bi_rw & REQ_DISCARD) { |
| 843 | rrpc_discard(rrpc, bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 844 | return BLK_QC_T_NONE; |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | rqd = mempool_alloc(rrpc->rq_pool, GFP_KERNEL); |
| 848 | if (!rqd) { |
| 849 | pr_err_ratelimited("rrpc: not able to queue bio."); |
| 850 | bio_io_error(bio); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 851 | return BLK_QC_T_NONE; |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 852 | } |
| 853 | memset(rqd, 0, sizeof(struct nvm_rq)); |
| 854 | |
| 855 | err = rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_NONE); |
| 856 | switch (err) { |
| 857 | case NVM_IO_OK: |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 858 | return BLK_QC_T_NONE; |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 859 | case NVM_IO_ERR: |
| 860 | bio_io_error(bio); |
| 861 | break; |
| 862 | case NVM_IO_DONE: |
| 863 | bio_endio(bio); |
| 864 | break; |
| 865 | case NVM_IO_REQUEUE: |
| 866 | spin_lock(&rrpc->bio_lock); |
| 867 | bio_list_add(&rrpc->requeue_bios, bio); |
| 868 | spin_unlock(&rrpc->bio_lock); |
| 869 | queue_work(rrpc->kgc_wq, &rrpc->ws_requeue); |
| 870 | break; |
| 871 | } |
| 872 | |
| 873 | mempool_free(rqd, rrpc->rq_pool); |
Jens Axboe | dece163 | 2015-11-05 10:41:16 -0700 | [diff] [blame] | 874 | return BLK_QC_T_NONE; |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | static void rrpc_requeue(struct work_struct *work) |
| 878 | { |
| 879 | struct rrpc *rrpc = container_of(work, struct rrpc, ws_requeue); |
| 880 | struct bio_list bios; |
| 881 | struct bio *bio; |
| 882 | |
| 883 | bio_list_init(&bios); |
| 884 | |
| 885 | spin_lock(&rrpc->bio_lock); |
| 886 | bio_list_merge(&bios, &rrpc->requeue_bios); |
| 887 | bio_list_init(&rrpc->requeue_bios); |
| 888 | spin_unlock(&rrpc->bio_lock); |
| 889 | |
| 890 | while ((bio = bio_list_pop(&bios))) |
| 891 | rrpc_make_rq(rrpc->disk->queue, bio); |
| 892 | } |
| 893 | |
| 894 | static void rrpc_gc_free(struct rrpc *rrpc) |
| 895 | { |
| 896 | struct rrpc_lun *rlun; |
| 897 | int i; |
| 898 | |
| 899 | if (rrpc->krqd_wq) |
| 900 | destroy_workqueue(rrpc->krqd_wq); |
| 901 | |
| 902 | if (rrpc->kgc_wq) |
| 903 | destroy_workqueue(rrpc->kgc_wq); |
| 904 | |
| 905 | if (!rrpc->luns) |
| 906 | return; |
| 907 | |
| 908 | for (i = 0; i < rrpc->nr_luns; i++) { |
| 909 | rlun = &rrpc->luns[i]; |
| 910 | |
| 911 | if (!rlun->blocks) |
| 912 | break; |
| 913 | vfree(rlun->blocks); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | static int rrpc_gc_init(struct rrpc *rrpc) |
| 918 | { |
| 919 | rrpc->krqd_wq = alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM|WQ_UNBOUND, |
| 920 | rrpc->nr_luns); |
| 921 | if (!rrpc->krqd_wq) |
| 922 | return -ENOMEM; |
| 923 | |
| 924 | rrpc->kgc_wq = alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM, 1); |
| 925 | if (!rrpc->kgc_wq) |
| 926 | return -ENOMEM; |
| 927 | |
| 928 | setup_timer(&rrpc->gc_timer, rrpc_gc_timer, (unsigned long)rrpc); |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | static void rrpc_map_free(struct rrpc *rrpc) |
| 934 | { |
| 935 | vfree(rrpc->rev_trans_map); |
| 936 | vfree(rrpc->trans_map); |
| 937 | } |
| 938 | |
| 939 | static int rrpc_l2p_update(u64 slba, u32 nlb, __le64 *entries, void *private) |
| 940 | { |
| 941 | struct rrpc *rrpc = (struct rrpc *)private; |
| 942 | struct nvm_dev *dev = rrpc->dev; |
| 943 | struct rrpc_addr *addr = rrpc->trans_map + slba; |
| 944 | struct rrpc_rev_addr *raddr = rrpc->rev_trans_map; |
| 945 | sector_t max_pages = dev->total_pages * (dev->sec_size >> 9); |
| 946 | u64 elba = slba + nlb; |
| 947 | u64 i; |
| 948 | |
| 949 | if (unlikely(elba > dev->total_pages)) { |
| 950 | pr_err("nvm: L2P data from device is out of bounds!\n"); |
| 951 | return -EINVAL; |
| 952 | } |
| 953 | |
| 954 | for (i = 0; i < nlb; i++) { |
| 955 | u64 pba = le64_to_cpu(entries[i]); |
| 956 | /* LNVM treats address-spaces as silos, LBA and PBA are |
| 957 | * equally large and zero-indexed. |
| 958 | */ |
| 959 | if (unlikely(pba >= max_pages && pba != U64_MAX)) { |
| 960 | pr_err("nvm: L2P data entry is out of bounds!\n"); |
| 961 | return -EINVAL; |
| 962 | } |
| 963 | |
| 964 | /* Address zero is a special one. The first page on a disk is |
| 965 | * protected. As it often holds internal device boot |
| 966 | * information. |
| 967 | */ |
| 968 | if (!pba) |
| 969 | continue; |
| 970 | |
| 971 | addr[i].addr = pba; |
| 972 | raddr[pba].addr = slba + i; |
| 973 | } |
| 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static int rrpc_map_init(struct rrpc *rrpc) |
| 979 | { |
| 980 | struct nvm_dev *dev = rrpc->dev; |
| 981 | sector_t i; |
| 982 | int ret; |
| 983 | |
| 984 | rrpc->trans_map = vzalloc(sizeof(struct rrpc_addr) * rrpc->nr_pages); |
| 985 | if (!rrpc->trans_map) |
| 986 | return -ENOMEM; |
| 987 | |
| 988 | rrpc->rev_trans_map = vmalloc(sizeof(struct rrpc_rev_addr) |
| 989 | * rrpc->nr_pages); |
| 990 | if (!rrpc->rev_trans_map) |
| 991 | return -ENOMEM; |
| 992 | |
| 993 | for (i = 0; i < rrpc->nr_pages; i++) { |
| 994 | struct rrpc_addr *p = &rrpc->trans_map[i]; |
| 995 | struct rrpc_rev_addr *r = &rrpc->rev_trans_map[i]; |
| 996 | |
| 997 | p->addr = ADDR_EMPTY; |
| 998 | r->addr = ADDR_EMPTY; |
| 999 | } |
| 1000 | |
| 1001 | if (!dev->ops->get_l2p_tbl) |
| 1002 | return 0; |
| 1003 | |
| 1004 | /* Bring up the mapping table from device */ |
| 1005 | ret = dev->ops->get_l2p_tbl(dev->q, 0, dev->total_pages, |
| 1006 | rrpc_l2p_update, rrpc); |
| 1007 | if (ret) { |
| 1008 | pr_err("nvm: rrpc: could not read L2P table.\n"); |
| 1009 | return -EINVAL; |
| 1010 | } |
| 1011 | |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | /* Minimum pages needed within a lun */ |
| 1017 | #define PAGE_POOL_SIZE 16 |
| 1018 | #define ADDR_POOL_SIZE 64 |
| 1019 | |
| 1020 | static int rrpc_core_init(struct rrpc *rrpc) |
| 1021 | { |
| 1022 | down_write(&rrpc_lock); |
| 1023 | if (!rrpc_gcb_cache) { |
| 1024 | rrpc_gcb_cache = kmem_cache_create("rrpc_gcb", |
| 1025 | sizeof(struct rrpc_block_gc), 0, 0, NULL); |
| 1026 | if (!rrpc_gcb_cache) { |
| 1027 | up_write(&rrpc_lock); |
| 1028 | return -ENOMEM; |
| 1029 | } |
| 1030 | |
| 1031 | rrpc_rq_cache = kmem_cache_create("rrpc_rq", |
| 1032 | sizeof(struct nvm_rq) + sizeof(struct rrpc_rq), |
| 1033 | 0, 0, NULL); |
| 1034 | if (!rrpc_rq_cache) { |
| 1035 | kmem_cache_destroy(rrpc_gcb_cache); |
| 1036 | up_write(&rrpc_lock); |
| 1037 | return -ENOMEM; |
| 1038 | } |
| 1039 | } |
| 1040 | up_write(&rrpc_lock); |
| 1041 | |
| 1042 | rrpc->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0); |
| 1043 | if (!rrpc->page_pool) |
| 1044 | return -ENOMEM; |
| 1045 | |
| 1046 | rrpc->gcb_pool = mempool_create_slab_pool(rrpc->dev->nr_luns, |
| 1047 | rrpc_gcb_cache); |
| 1048 | if (!rrpc->gcb_pool) |
| 1049 | return -ENOMEM; |
| 1050 | |
| 1051 | rrpc->rq_pool = mempool_create_slab_pool(64, rrpc_rq_cache); |
| 1052 | if (!rrpc->rq_pool) |
| 1053 | return -ENOMEM; |
| 1054 | |
| 1055 | spin_lock_init(&rrpc->inflights.lock); |
| 1056 | INIT_LIST_HEAD(&rrpc->inflights.reqs); |
| 1057 | |
| 1058 | return 0; |
| 1059 | } |
| 1060 | |
| 1061 | static void rrpc_core_free(struct rrpc *rrpc) |
| 1062 | { |
| 1063 | mempool_destroy(rrpc->page_pool); |
| 1064 | mempool_destroy(rrpc->gcb_pool); |
| 1065 | mempool_destroy(rrpc->rq_pool); |
| 1066 | } |
| 1067 | |
| 1068 | static void rrpc_luns_free(struct rrpc *rrpc) |
| 1069 | { |
| 1070 | kfree(rrpc->luns); |
| 1071 | } |
| 1072 | |
| 1073 | static int rrpc_luns_init(struct rrpc *rrpc, int lun_begin, int lun_end) |
| 1074 | { |
| 1075 | struct nvm_dev *dev = rrpc->dev; |
| 1076 | struct rrpc_lun *rlun; |
| 1077 | int i, j; |
| 1078 | |
| 1079 | spin_lock_init(&rrpc->rev_lock); |
| 1080 | |
| 1081 | rrpc->luns = kcalloc(rrpc->nr_luns, sizeof(struct rrpc_lun), |
| 1082 | GFP_KERNEL); |
| 1083 | if (!rrpc->luns) |
| 1084 | return -ENOMEM; |
| 1085 | |
| 1086 | /* 1:1 mapping */ |
| 1087 | for (i = 0; i < rrpc->nr_luns; i++) { |
| 1088 | struct nvm_lun *lun = dev->mt->get_lun(dev, lun_begin + i); |
| 1089 | |
| 1090 | if (dev->pgs_per_blk > |
| 1091 | MAX_INVALID_PAGES_STORAGE * BITS_PER_LONG) { |
| 1092 | pr_err("rrpc: number of pages per block too high."); |
| 1093 | goto err; |
| 1094 | } |
| 1095 | |
| 1096 | rlun = &rrpc->luns[i]; |
| 1097 | rlun->rrpc = rrpc; |
| 1098 | rlun->parent = lun; |
| 1099 | INIT_LIST_HEAD(&rlun->prio_list); |
| 1100 | INIT_WORK(&rlun->ws_gc, rrpc_lun_gc); |
| 1101 | spin_lock_init(&rlun->lock); |
| 1102 | |
| 1103 | rrpc->total_blocks += dev->blks_per_lun; |
| 1104 | rrpc->nr_pages += dev->sec_per_lun; |
| 1105 | |
| 1106 | rlun->blocks = vzalloc(sizeof(struct rrpc_block) * |
| 1107 | rrpc->dev->blks_per_lun); |
| 1108 | if (!rlun->blocks) |
| 1109 | goto err; |
| 1110 | |
| 1111 | for (j = 0; j < rrpc->dev->blks_per_lun; j++) { |
| 1112 | struct rrpc_block *rblk = &rlun->blocks[j]; |
| 1113 | struct nvm_block *blk = &lun->blocks[j]; |
| 1114 | |
| 1115 | rblk->parent = blk; |
| 1116 | INIT_LIST_HEAD(&rblk->prio); |
| 1117 | spin_lock_init(&rblk->lock); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | return 0; |
| 1122 | err: |
| 1123 | return -ENOMEM; |
| 1124 | } |
| 1125 | |
| 1126 | static void rrpc_free(struct rrpc *rrpc) |
| 1127 | { |
| 1128 | rrpc_gc_free(rrpc); |
| 1129 | rrpc_map_free(rrpc); |
| 1130 | rrpc_core_free(rrpc); |
| 1131 | rrpc_luns_free(rrpc); |
| 1132 | |
| 1133 | kfree(rrpc); |
| 1134 | } |
| 1135 | |
| 1136 | static void rrpc_exit(void *private) |
| 1137 | { |
| 1138 | struct rrpc *rrpc = private; |
| 1139 | |
| 1140 | del_timer(&rrpc->gc_timer); |
| 1141 | |
| 1142 | flush_workqueue(rrpc->krqd_wq); |
| 1143 | flush_workqueue(rrpc->kgc_wq); |
| 1144 | |
| 1145 | rrpc_free(rrpc); |
| 1146 | } |
| 1147 | |
| 1148 | static sector_t rrpc_capacity(void *private) |
| 1149 | { |
| 1150 | struct rrpc *rrpc = private; |
| 1151 | struct nvm_dev *dev = rrpc->dev; |
| 1152 | sector_t reserved, provisioned; |
| 1153 | |
| 1154 | /* cur, gc, and two emergency blocks for each lun */ |
| 1155 | reserved = rrpc->nr_luns * dev->max_pages_per_blk * 4; |
| 1156 | provisioned = rrpc->nr_pages - reserved; |
| 1157 | |
| 1158 | if (reserved > rrpc->nr_pages) { |
| 1159 | pr_err("rrpc: not enough space available to expose storage.\n"); |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | sector_div(provisioned, 10); |
| 1164 | return provisioned * 9 * NR_PHY_IN_LOG; |
| 1165 | } |
| 1166 | |
| 1167 | /* |
| 1168 | * Looks up the logical address from reverse trans map and check if its valid by |
| 1169 | * comparing the logical to physical address with the physical address. |
| 1170 | * Returns 0 on free, otherwise 1 if in use |
| 1171 | */ |
| 1172 | static void rrpc_block_map_update(struct rrpc *rrpc, struct rrpc_block *rblk) |
| 1173 | { |
| 1174 | struct nvm_dev *dev = rrpc->dev; |
| 1175 | int offset; |
| 1176 | struct rrpc_addr *laddr; |
Matias Bjørling | b7ceb7d | 2015-11-02 17:12:27 +0100 | [diff] [blame] | 1177 | u64 paddr, pladdr; |
Matias Bjørling | ae1519e | 2015-10-28 19:54:57 +0100 | [diff] [blame] | 1178 | |
| 1179 | for (offset = 0; offset < dev->pgs_per_blk; offset++) { |
| 1180 | paddr = block_to_addr(rrpc, rblk) + offset; |
| 1181 | |
| 1182 | pladdr = rrpc->rev_trans_map[paddr].addr; |
| 1183 | if (pladdr == ADDR_EMPTY) |
| 1184 | continue; |
| 1185 | |
| 1186 | laddr = &rrpc->trans_map[pladdr]; |
| 1187 | |
| 1188 | if (paddr == laddr->addr) { |
| 1189 | laddr->rblk = rblk; |
| 1190 | } else { |
| 1191 | set_bit(offset, rblk->invalid_pages); |
| 1192 | rblk->nr_invalid_pages++; |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | static int rrpc_blocks_init(struct rrpc *rrpc) |
| 1198 | { |
| 1199 | struct rrpc_lun *rlun; |
| 1200 | struct rrpc_block *rblk; |
| 1201 | int lun_iter, blk_iter; |
| 1202 | |
| 1203 | for (lun_iter = 0; lun_iter < rrpc->nr_luns; lun_iter++) { |
| 1204 | rlun = &rrpc->luns[lun_iter]; |
| 1205 | |
| 1206 | for (blk_iter = 0; blk_iter < rrpc->dev->blks_per_lun; |
| 1207 | blk_iter++) { |
| 1208 | rblk = &rlun->blocks[blk_iter]; |
| 1209 | rrpc_block_map_update(rrpc, rblk); |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | static int rrpc_luns_configure(struct rrpc *rrpc) |
| 1217 | { |
| 1218 | struct rrpc_lun *rlun; |
| 1219 | struct rrpc_block *rblk; |
| 1220 | int i; |
| 1221 | |
| 1222 | for (i = 0; i < rrpc->nr_luns; i++) { |
| 1223 | rlun = &rrpc->luns[i]; |
| 1224 | |
| 1225 | rblk = rrpc_get_blk(rrpc, rlun, 0); |
| 1226 | if (!rblk) |
| 1227 | return -EINVAL; |
| 1228 | |
| 1229 | rrpc_set_lun_cur(rlun, rblk); |
| 1230 | |
| 1231 | /* Emergency gc block */ |
| 1232 | rblk = rrpc_get_blk(rrpc, rlun, 1); |
| 1233 | if (!rblk) |
| 1234 | return -EINVAL; |
| 1235 | rlun->gc_cur = rblk; |
| 1236 | } |
| 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static struct nvm_tgt_type tt_rrpc; |
| 1242 | |
| 1243 | static void *rrpc_init(struct nvm_dev *dev, struct gendisk *tdisk, |
| 1244 | int lun_begin, int lun_end) |
| 1245 | { |
| 1246 | struct request_queue *bqueue = dev->q; |
| 1247 | struct request_queue *tqueue = tdisk->queue; |
| 1248 | struct rrpc *rrpc; |
| 1249 | int ret; |
| 1250 | |
| 1251 | if (!(dev->identity.dom & NVM_RSP_L2P)) { |
| 1252 | pr_err("nvm: rrpc: device does not support l2p (%x)\n", |
| 1253 | dev->identity.dom); |
| 1254 | return ERR_PTR(-EINVAL); |
| 1255 | } |
| 1256 | |
| 1257 | rrpc = kzalloc(sizeof(struct rrpc), GFP_KERNEL); |
| 1258 | if (!rrpc) |
| 1259 | return ERR_PTR(-ENOMEM); |
| 1260 | |
| 1261 | rrpc->instance.tt = &tt_rrpc; |
| 1262 | rrpc->dev = dev; |
| 1263 | rrpc->disk = tdisk; |
| 1264 | |
| 1265 | bio_list_init(&rrpc->requeue_bios); |
| 1266 | spin_lock_init(&rrpc->bio_lock); |
| 1267 | INIT_WORK(&rrpc->ws_requeue, rrpc_requeue); |
| 1268 | |
| 1269 | rrpc->nr_luns = lun_end - lun_begin + 1; |
| 1270 | |
| 1271 | /* simple round-robin strategy */ |
| 1272 | atomic_set(&rrpc->next_lun, -1); |
| 1273 | |
| 1274 | ret = rrpc_luns_init(rrpc, lun_begin, lun_end); |
| 1275 | if (ret) { |
| 1276 | pr_err("nvm: rrpc: could not initialize luns\n"); |
| 1277 | goto err; |
| 1278 | } |
| 1279 | |
| 1280 | rrpc->poffset = dev->sec_per_lun * lun_begin; |
| 1281 | rrpc->lun_offset = lun_begin; |
| 1282 | |
| 1283 | ret = rrpc_core_init(rrpc); |
| 1284 | if (ret) { |
| 1285 | pr_err("nvm: rrpc: could not initialize core\n"); |
| 1286 | goto err; |
| 1287 | } |
| 1288 | |
| 1289 | ret = rrpc_map_init(rrpc); |
| 1290 | if (ret) { |
| 1291 | pr_err("nvm: rrpc: could not initialize maps\n"); |
| 1292 | goto err; |
| 1293 | } |
| 1294 | |
| 1295 | ret = rrpc_blocks_init(rrpc); |
| 1296 | if (ret) { |
| 1297 | pr_err("nvm: rrpc: could not initialize state for blocks\n"); |
| 1298 | goto err; |
| 1299 | } |
| 1300 | |
| 1301 | ret = rrpc_luns_configure(rrpc); |
| 1302 | if (ret) { |
| 1303 | pr_err("nvm: rrpc: not enough blocks available in LUNs.\n"); |
| 1304 | goto err; |
| 1305 | } |
| 1306 | |
| 1307 | ret = rrpc_gc_init(rrpc); |
| 1308 | if (ret) { |
| 1309 | pr_err("nvm: rrpc: could not initialize gc\n"); |
| 1310 | goto err; |
| 1311 | } |
| 1312 | |
| 1313 | /* inherit the size from the underlying device */ |
| 1314 | blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue)); |
| 1315 | blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue)); |
| 1316 | |
| 1317 | pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n", |
| 1318 | rrpc->nr_luns, (unsigned long long)rrpc->nr_pages); |
| 1319 | |
| 1320 | mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10)); |
| 1321 | |
| 1322 | return rrpc; |
| 1323 | err: |
| 1324 | rrpc_free(rrpc); |
| 1325 | return ERR_PTR(ret); |
| 1326 | } |
| 1327 | |
| 1328 | /* round robin, page-based FTL, and cost-based GC */ |
| 1329 | static struct nvm_tgt_type tt_rrpc = { |
| 1330 | .name = "rrpc", |
| 1331 | .version = {1, 0, 0}, |
| 1332 | |
| 1333 | .make_rq = rrpc_make_rq, |
| 1334 | .capacity = rrpc_capacity, |
| 1335 | .end_io = rrpc_end_io, |
| 1336 | |
| 1337 | .init = rrpc_init, |
| 1338 | .exit = rrpc_exit, |
| 1339 | }; |
| 1340 | |
| 1341 | static int __init rrpc_module_init(void) |
| 1342 | { |
| 1343 | return nvm_register_target(&tt_rrpc); |
| 1344 | } |
| 1345 | |
| 1346 | static void rrpc_module_exit(void) |
| 1347 | { |
| 1348 | nvm_unregister_target(&tt_rrpc); |
| 1349 | } |
| 1350 | |
| 1351 | module_init(rrpc_module_init); |
| 1352 | module_exit(rrpc_module_exit); |
| 1353 | MODULE_LICENSE("GPL v2"); |
| 1354 | MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs"); |