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