blob: 5df4a696184add2c85273981248ab5c86210c649 [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;
384
385 pr_debug("nvm: block '%lu' being reclaimed\n", rblk->parent->id);
386
387 if (rrpc_move_valid_pages(rrpc, rblk))
388 goto done;
389
390 nvm_erase_blk(dev, rblk->parent);
391 rrpc_put_blk(rrpc, rblk);
392done:
393 mempool_free(gcb, rrpc->gcb_pool);
394}
395
396/* the block with highest number of invalid pages, will be in the beginning
397 * of the list
398 */
399static struct rrpc_block *rblock_max_invalid(struct rrpc_block *ra,
400 struct rrpc_block *rb)
401{
402 if (ra->nr_invalid_pages == rb->nr_invalid_pages)
403 return ra;
404
405 return (ra->nr_invalid_pages < rb->nr_invalid_pages) ? rb : ra;
406}
407
408/* linearly find the block with highest number of invalid pages
409 * requires lun->lock
410 */
411static struct rrpc_block *block_prio_find_max(struct rrpc_lun *rlun)
412{
413 struct list_head *prio_list = &rlun->prio_list;
414 struct rrpc_block *rblock, *max;
415
416 BUG_ON(list_empty(prio_list));
417
418 max = list_first_entry(prio_list, struct rrpc_block, prio);
419 list_for_each_entry(rblock, prio_list, prio)
420 max = rblock_max_invalid(max, rblock);
421
422 return max;
423}
424
425static void rrpc_lun_gc(struct work_struct *work)
426{
427 struct rrpc_lun *rlun = container_of(work, struct rrpc_lun, ws_gc);
428 struct rrpc *rrpc = rlun->rrpc;
429 struct nvm_lun *lun = rlun->parent;
430 struct rrpc_block_gc *gcb;
431 unsigned int nr_blocks_need;
432
433 nr_blocks_need = rrpc->dev->blks_per_lun / GC_LIMIT_INVERSE;
434
435 if (nr_blocks_need < rrpc->nr_luns)
436 nr_blocks_need = rrpc->nr_luns;
437
438 spin_lock(&lun->lock);
439 while (nr_blocks_need > lun->nr_free_blocks &&
440 !list_empty(&rlun->prio_list)) {
441 struct rrpc_block *rblock = block_prio_find_max(rlun);
442 struct nvm_block *block = rblock->parent;
443
444 if (!rblock->nr_invalid_pages)
445 break;
446
447 list_del_init(&rblock->prio);
448
449 BUG_ON(!block_is_full(rrpc, rblock));
450
451 pr_debug("rrpc: selected block '%lu' for GC\n", block->id);
452
453 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
454 if (!gcb)
455 break;
456
457 gcb->rrpc = rrpc;
458 gcb->rblk = rblock;
459 INIT_WORK(&gcb->ws_gc, rrpc_block_gc);
460
461 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
462
463 nr_blocks_need--;
464 }
465 spin_unlock(&lun->lock);
466
467 /* TODO: Hint that request queue can be started again */
468}
469
470static void rrpc_gc_queue(struct work_struct *work)
471{
472 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
473 ws_gc);
474 struct rrpc *rrpc = gcb->rrpc;
475 struct rrpc_block *rblk = gcb->rblk;
476 struct nvm_lun *lun = rblk->parent->lun;
477 struct rrpc_lun *rlun = &rrpc->luns[lun->id - rrpc->lun_offset];
478
479 spin_lock(&rlun->lock);
480 list_add_tail(&rblk->prio, &rlun->prio_list);
481 spin_unlock(&rlun->lock);
482
483 mempool_free(gcb, rrpc->gcb_pool);
484 pr_debug("nvm: block '%lu' is full, allow GC (sched)\n",
485 rblk->parent->id);
486}
487
488static const struct block_device_operations rrpc_fops = {
489 .owner = THIS_MODULE,
490};
491
492static struct rrpc_lun *rrpc_get_lun_rr(struct rrpc *rrpc, int is_gc)
493{
494 unsigned int i;
495 struct rrpc_lun *rlun, *max_free;
496
497 if (!is_gc)
498 return get_next_lun(rrpc);
499
500 /* during GC, we don't care about RR, instead we want to make
501 * sure that we maintain evenness between the block luns.
502 */
503 max_free = &rrpc->luns[0];
504 /* prevent GC-ing lun from devouring pages of a lun with
505 * little free blocks. We don't take the lock as we only need an
506 * estimate.
507 */
508 rrpc_for_each_lun(rrpc, rlun, i) {
509 if (rlun->parent->nr_free_blocks >
510 max_free->parent->nr_free_blocks)
511 max_free = rlun;
512 }
513
514 return max_free;
515}
516
517static struct rrpc_addr *rrpc_update_map(struct rrpc *rrpc, sector_t laddr,
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100518 struct rrpc_block *rblk, u64 paddr)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100519{
520 struct rrpc_addr *gp;
521 struct rrpc_rev_addr *rev;
522
523 BUG_ON(laddr >= rrpc->nr_pages);
524
525 gp = &rrpc->trans_map[laddr];
526 spin_lock(&rrpc->rev_lock);
527 if (gp->rblk)
528 rrpc_page_invalidate(rrpc, gp);
529
530 gp->addr = paddr;
531 gp->rblk = rblk;
532
533 rev = &rrpc->rev_trans_map[gp->addr - rrpc->poffset];
534 rev->addr = laddr;
535 spin_unlock(&rrpc->rev_lock);
536
537 return gp;
538}
539
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100540static u64 rrpc_alloc_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100541{
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100542 u64 addr = ADDR_EMPTY;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100543
544 spin_lock(&rblk->lock);
545 if (block_is_full(rrpc, rblk))
546 goto out;
547
548 addr = block_to_addr(rrpc, rblk) + rblk->next_page;
549
550 rblk->next_page++;
551out:
552 spin_unlock(&rblk->lock);
553 return addr;
554}
555
556/* Simple round-robin Logical to physical address translation.
557 *
558 * Retrieve the mapping using the active append point. Then update the ap for
559 * the next write to the disk.
560 *
561 * Returns rrpc_addr with the physical address and block. Remember to return to
562 * rrpc->addr_cache when request is finished.
563 */
564static struct rrpc_addr *rrpc_map_page(struct rrpc *rrpc, sector_t laddr,
565 int is_gc)
566{
567 struct rrpc_lun *rlun;
568 struct rrpc_block *rblk;
569 struct nvm_lun *lun;
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100570 u64 paddr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100571
572 rlun = rrpc_get_lun_rr(rrpc, is_gc);
573 lun = rlun->parent;
574
575 if (!is_gc && lun->nr_free_blocks < rrpc->nr_luns * 4)
576 return NULL;
577
578 spin_lock(&rlun->lock);
579
580 rblk = rlun->cur;
581retry:
582 paddr = rrpc_alloc_addr(rrpc, rblk);
583
584 if (paddr == ADDR_EMPTY) {
585 rblk = rrpc_get_blk(rrpc, rlun, 0);
586 if (rblk) {
587 rrpc_set_lun_cur(rlun, rblk);
588 goto retry;
589 }
590
591 if (is_gc) {
592 /* retry from emergency gc block */
593 paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur);
594 if (paddr == ADDR_EMPTY) {
595 rblk = rrpc_get_blk(rrpc, rlun, 1);
596 if (!rblk) {
597 pr_err("rrpc: no more blocks");
598 goto err;
599 }
600
601 rlun->gc_cur = rblk;
602 paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur);
603 }
604 rblk = rlun->gc_cur;
605 }
606 }
607
608 spin_unlock(&rlun->lock);
609 return rrpc_update_map(rrpc, laddr, rblk, paddr);
610err:
611 spin_unlock(&rlun->lock);
612 return NULL;
613}
614
615static void rrpc_run_gc(struct rrpc *rrpc, struct rrpc_block *rblk)
616{
617 struct rrpc_block_gc *gcb;
618
619 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
620 if (!gcb) {
621 pr_err("rrpc: unable to queue block for gc.");
622 return;
623 }
624
625 gcb->rrpc = rrpc;
626 gcb->rblk = rblk;
627
628 INIT_WORK(&gcb->ws_gc, rrpc_gc_queue);
629 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
630}
631
632static void rrpc_end_io_write(struct rrpc *rrpc, struct rrpc_rq *rrqd,
633 sector_t laddr, uint8_t npages)
634{
635 struct rrpc_addr *p;
636 struct rrpc_block *rblk;
637 struct nvm_lun *lun;
638 int cmnt_size, i;
639
640 for (i = 0; i < npages; i++) {
641 p = &rrpc->trans_map[laddr + i];
642 rblk = p->rblk;
643 lun = rblk->parent->lun;
644
645 cmnt_size = atomic_inc_return(&rblk->data_cmnt_size);
646 if (unlikely(cmnt_size == rrpc->dev->pgs_per_blk))
647 rrpc_run_gc(rrpc, rblk);
648 }
649}
650
Matias Bjørling912761622016-01-12 07:49:21 +0100651static void rrpc_end_io(struct nvm_rq *rqd, int error)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100652{
653 struct rrpc *rrpc = container_of(rqd->ins, struct rrpc, instance);
654 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
655 uint8_t npages = rqd->nr_pages;
656 sector_t laddr = rrpc_get_laddr(rqd->bio) - npages;
657
658 if (bio_data_dir(rqd->bio) == WRITE)
659 rrpc_end_io_write(rrpc, rrqd, laddr, npages);
660
Wenwei Tao3cd485b12016-01-12 07:49:15 +0100661 bio_put(rqd->bio);
662
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100663 if (rrqd->flags & NVM_IOTYPE_GC)
Matias Bjørling912761622016-01-12 07:49:21 +0100664 return;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100665
666 rrpc_unlock_rq(rrpc, rqd);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100667
668 if (npages > 1)
669 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
670 if (rqd->metadata)
671 nvm_dev_dma_free(rrpc->dev, rqd->metadata, rqd->dma_metadata);
672
673 mempool_free(rqd, rrpc->rq_pool);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100674}
675
676static int rrpc_read_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
677 struct nvm_rq *rqd, unsigned long flags, int npages)
678{
679 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
680 struct rrpc_addr *gp;
681 sector_t laddr = rrpc_get_laddr(bio);
682 int is_gc = flags & NVM_IOTYPE_GC;
683 int i;
684
685 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
686 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
687 return NVM_IO_REQUEUE;
688 }
689
690 for (i = 0; i < npages; i++) {
691 /* We assume that mapping occurs at 4KB granularity */
692 BUG_ON(!(laddr + i >= 0 && laddr + i < rrpc->nr_pages));
693 gp = &rrpc->trans_map[laddr + i];
694
695 if (gp->rblk) {
696 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
697 gp->addr);
698 } else {
699 BUG_ON(is_gc);
700 rrpc_unlock_laddr(rrpc, r);
701 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
702 rqd->dma_ppa_list);
703 return NVM_IO_DONE;
704 }
705 }
706
707 rqd->opcode = NVM_OP_HBREAD;
708
709 return NVM_IO_OK;
710}
711
712static int rrpc_read_rq(struct rrpc *rrpc, struct bio *bio, struct nvm_rq *rqd,
713 unsigned long flags)
714{
715 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
716 int is_gc = flags & NVM_IOTYPE_GC;
717 sector_t laddr = rrpc_get_laddr(bio);
718 struct rrpc_addr *gp;
719
720 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
721 return NVM_IO_REQUEUE;
722
723 BUG_ON(!(laddr >= 0 && laddr < rrpc->nr_pages));
724 gp = &rrpc->trans_map[laddr];
725
726 if (gp->rblk) {
727 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, gp->addr);
728 } else {
729 BUG_ON(is_gc);
730 rrpc_unlock_rq(rrpc, rqd);
731 return NVM_IO_DONE;
732 }
733
734 rqd->opcode = NVM_OP_HBREAD;
735 rrqd->addr = gp;
736
737 return NVM_IO_OK;
738}
739
740static int rrpc_write_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
741 struct nvm_rq *rqd, unsigned long flags, int npages)
742{
743 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
744 struct rrpc_addr *p;
745 sector_t laddr = rrpc_get_laddr(bio);
746 int is_gc = flags & NVM_IOTYPE_GC;
747 int i;
748
749 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
750 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
751 return NVM_IO_REQUEUE;
752 }
753
754 for (i = 0; i < npages; i++) {
755 /* We assume that mapping occurs at 4KB granularity */
756 p = rrpc_map_page(rrpc, laddr + i, is_gc);
757 if (!p) {
758 BUG_ON(is_gc);
759 rrpc_unlock_laddr(rrpc, r);
760 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
761 rqd->dma_ppa_list);
762 rrpc_gc_kick(rrpc);
763 return NVM_IO_REQUEUE;
764 }
765
766 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
767 p->addr);
768 }
769
770 rqd->opcode = NVM_OP_HBWRITE;
771
772 return NVM_IO_OK;
773}
774
775static int rrpc_write_rq(struct rrpc *rrpc, struct bio *bio,
776 struct nvm_rq *rqd, unsigned long flags)
777{
778 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
779 struct rrpc_addr *p;
780 int is_gc = flags & NVM_IOTYPE_GC;
781 sector_t laddr = rrpc_get_laddr(bio);
782
783 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
784 return NVM_IO_REQUEUE;
785
786 p = rrpc_map_page(rrpc, laddr, is_gc);
787 if (!p) {
788 BUG_ON(is_gc);
789 rrpc_unlock_rq(rrpc, rqd);
790 rrpc_gc_kick(rrpc);
791 return NVM_IO_REQUEUE;
792 }
793
794 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, p->addr);
795 rqd->opcode = NVM_OP_HBWRITE;
796 rrqd->addr = p;
797
798 return NVM_IO_OK;
799}
800
801static int rrpc_setup_rq(struct rrpc *rrpc, struct bio *bio,
802 struct nvm_rq *rqd, unsigned long flags, uint8_t npages)
803{
804 if (npages > 1) {
805 rqd->ppa_list = nvm_dev_dma_alloc(rrpc->dev, GFP_KERNEL,
806 &rqd->dma_ppa_list);
807 if (!rqd->ppa_list) {
808 pr_err("rrpc: not able to allocate ppa list\n");
809 return NVM_IO_ERR;
810 }
811
812 if (bio_rw(bio) == WRITE)
813 return rrpc_write_ppalist_rq(rrpc, bio, rqd, flags,
814 npages);
815
816 return rrpc_read_ppalist_rq(rrpc, bio, rqd, flags, npages);
817 }
818
819 if (bio_rw(bio) == WRITE)
820 return rrpc_write_rq(rrpc, bio, rqd, flags);
821
822 return rrpc_read_rq(rrpc, bio, rqd, flags);
823}
824
825static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio,
826 struct nvm_rq *rqd, unsigned long flags)
827{
828 int err;
829 struct rrpc_rq *rrq = nvm_rq_to_pdu(rqd);
830 uint8_t nr_pages = rrpc_get_pages(bio);
831 int bio_size = bio_sectors(bio) << 9;
832
833 if (bio_size < rrpc->dev->sec_size)
834 return NVM_IO_ERR;
835 else if (bio_size > rrpc->dev->max_rq_size)
836 return NVM_IO_ERR;
837
838 err = rrpc_setup_rq(rrpc, bio, rqd, flags, nr_pages);
839 if (err)
840 return err;
841
842 bio_get(bio);
843 rqd->bio = bio;
844 rqd->ins = &rrpc->instance;
845 rqd->nr_pages = nr_pages;
846 rrq->flags = flags;
847
848 err = nvm_submit_io(rrpc->dev, rqd);
849 if (err) {
850 pr_err("rrpc: I/O submission failed: %d\n", err);
Wenwei Tao3cd485b12016-01-12 07:49:15 +0100851 bio_put(bio);
Wenwei Taoc27278b2016-01-12 07:49:18 +0100852 if (!(flags & NVM_IOTYPE_GC)) {
853 rrpc_unlock_rq(rrpc, rqd);
854 if (rqd->nr_pages > 1)
855 nvm_dev_dma_free(rrpc->dev,
856 rqd->ppa_list, rqd->dma_ppa_list);
857 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100858 return NVM_IO_ERR;
859 }
860
861 return NVM_IO_OK;
862}
863
Jens Axboedece1632015-11-05 10:41:16 -0700864static blk_qc_t rrpc_make_rq(struct request_queue *q, struct bio *bio)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100865{
866 struct rrpc *rrpc = q->queuedata;
867 struct nvm_rq *rqd;
868 int err;
869
870 if (bio->bi_rw & REQ_DISCARD) {
871 rrpc_discard(rrpc, bio);
Jens Axboedece1632015-11-05 10:41:16 -0700872 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100873 }
874
875 rqd = mempool_alloc(rrpc->rq_pool, GFP_KERNEL);
876 if (!rqd) {
877 pr_err_ratelimited("rrpc: not able to queue bio.");
878 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700879 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100880 }
881 memset(rqd, 0, sizeof(struct nvm_rq));
882
883 err = rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_NONE);
884 switch (err) {
885 case NVM_IO_OK:
Jens Axboedece1632015-11-05 10:41:16 -0700886 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100887 case NVM_IO_ERR:
888 bio_io_error(bio);
889 break;
890 case NVM_IO_DONE:
891 bio_endio(bio);
892 break;
893 case NVM_IO_REQUEUE:
894 spin_lock(&rrpc->bio_lock);
895 bio_list_add(&rrpc->requeue_bios, bio);
896 spin_unlock(&rrpc->bio_lock);
897 queue_work(rrpc->kgc_wq, &rrpc->ws_requeue);
898 break;
899 }
900
901 mempool_free(rqd, rrpc->rq_pool);
Jens Axboedece1632015-11-05 10:41:16 -0700902 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100903}
904
905static void rrpc_requeue(struct work_struct *work)
906{
907 struct rrpc *rrpc = container_of(work, struct rrpc, ws_requeue);
908 struct bio_list bios;
909 struct bio *bio;
910
911 bio_list_init(&bios);
912
913 spin_lock(&rrpc->bio_lock);
914 bio_list_merge(&bios, &rrpc->requeue_bios);
915 bio_list_init(&rrpc->requeue_bios);
916 spin_unlock(&rrpc->bio_lock);
917
918 while ((bio = bio_list_pop(&bios)))
919 rrpc_make_rq(rrpc->disk->queue, bio);
920}
921
922static void rrpc_gc_free(struct rrpc *rrpc)
923{
924 struct rrpc_lun *rlun;
925 int i;
926
927 if (rrpc->krqd_wq)
928 destroy_workqueue(rrpc->krqd_wq);
929
930 if (rrpc->kgc_wq)
931 destroy_workqueue(rrpc->kgc_wq);
932
933 if (!rrpc->luns)
934 return;
935
936 for (i = 0; i < rrpc->nr_luns; i++) {
937 rlun = &rrpc->luns[i];
938
939 if (!rlun->blocks)
940 break;
941 vfree(rlun->blocks);
942 }
943}
944
945static int rrpc_gc_init(struct rrpc *rrpc)
946{
947 rrpc->krqd_wq = alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM|WQ_UNBOUND,
948 rrpc->nr_luns);
949 if (!rrpc->krqd_wq)
950 return -ENOMEM;
951
952 rrpc->kgc_wq = alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM, 1);
953 if (!rrpc->kgc_wq)
954 return -ENOMEM;
955
956 setup_timer(&rrpc->gc_timer, rrpc_gc_timer, (unsigned long)rrpc);
957
958 return 0;
959}
960
961static void rrpc_map_free(struct rrpc *rrpc)
962{
963 vfree(rrpc->rev_trans_map);
964 vfree(rrpc->trans_map);
965}
966
967static int rrpc_l2p_update(u64 slba, u32 nlb, __le64 *entries, void *private)
968{
969 struct rrpc *rrpc = (struct rrpc *)private;
970 struct nvm_dev *dev = rrpc->dev;
971 struct rrpc_addr *addr = rrpc->trans_map + slba;
972 struct rrpc_rev_addr *raddr = rrpc->rev_trans_map;
973 sector_t max_pages = dev->total_pages * (dev->sec_size >> 9);
974 u64 elba = slba + nlb;
975 u64 i;
976
977 if (unlikely(elba > dev->total_pages)) {
978 pr_err("nvm: L2P data from device is out of bounds!\n");
979 return -EINVAL;
980 }
981
982 for (i = 0; i < nlb; i++) {
983 u64 pba = le64_to_cpu(entries[i]);
984 /* LNVM treats address-spaces as silos, LBA and PBA are
985 * equally large and zero-indexed.
986 */
987 if (unlikely(pba >= max_pages && pba != U64_MAX)) {
988 pr_err("nvm: L2P data entry is out of bounds!\n");
989 return -EINVAL;
990 }
991
992 /* Address zero is a special one. The first page on a disk is
993 * protected. As it often holds internal device boot
994 * information.
995 */
996 if (!pba)
997 continue;
998
999 addr[i].addr = pba;
1000 raddr[pba].addr = slba + i;
1001 }
1002
1003 return 0;
1004}
1005
1006static int rrpc_map_init(struct rrpc *rrpc)
1007{
1008 struct nvm_dev *dev = rrpc->dev;
1009 sector_t i;
1010 int ret;
1011
1012 rrpc->trans_map = vzalloc(sizeof(struct rrpc_addr) * rrpc->nr_pages);
1013 if (!rrpc->trans_map)
1014 return -ENOMEM;
1015
1016 rrpc->rev_trans_map = vmalloc(sizeof(struct rrpc_rev_addr)
1017 * rrpc->nr_pages);
1018 if (!rrpc->rev_trans_map)
1019 return -ENOMEM;
1020
1021 for (i = 0; i < rrpc->nr_pages; i++) {
1022 struct rrpc_addr *p = &rrpc->trans_map[i];
1023 struct rrpc_rev_addr *r = &rrpc->rev_trans_map[i];
1024
1025 p->addr = ADDR_EMPTY;
1026 r->addr = ADDR_EMPTY;
1027 }
1028
1029 if (!dev->ops->get_l2p_tbl)
1030 return 0;
1031
1032 /* Bring up the mapping table from device */
Matias Bjørling16f26c32015-12-06 11:25:48 +01001033 ret = dev->ops->get_l2p_tbl(dev, 0, dev->total_pages,
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001034 rrpc_l2p_update, rrpc);
1035 if (ret) {
1036 pr_err("nvm: rrpc: could not read L2P table.\n");
1037 return -EINVAL;
1038 }
1039
1040 return 0;
1041}
1042
1043
1044/* Minimum pages needed within a lun */
1045#define PAGE_POOL_SIZE 16
1046#define ADDR_POOL_SIZE 64
1047
1048static int rrpc_core_init(struct rrpc *rrpc)
1049{
1050 down_write(&rrpc_lock);
1051 if (!rrpc_gcb_cache) {
1052 rrpc_gcb_cache = kmem_cache_create("rrpc_gcb",
1053 sizeof(struct rrpc_block_gc), 0, 0, NULL);
1054 if (!rrpc_gcb_cache) {
1055 up_write(&rrpc_lock);
1056 return -ENOMEM;
1057 }
1058
1059 rrpc_rq_cache = kmem_cache_create("rrpc_rq",
1060 sizeof(struct nvm_rq) + sizeof(struct rrpc_rq),
1061 0, 0, NULL);
1062 if (!rrpc_rq_cache) {
1063 kmem_cache_destroy(rrpc_gcb_cache);
1064 up_write(&rrpc_lock);
1065 return -ENOMEM;
1066 }
1067 }
1068 up_write(&rrpc_lock);
1069
1070 rrpc->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0);
1071 if (!rrpc->page_pool)
1072 return -ENOMEM;
1073
1074 rrpc->gcb_pool = mempool_create_slab_pool(rrpc->dev->nr_luns,
1075 rrpc_gcb_cache);
1076 if (!rrpc->gcb_pool)
1077 return -ENOMEM;
1078
1079 rrpc->rq_pool = mempool_create_slab_pool(64, rrpc_rq_cache);
1080 if (!rrpc->rq_pool)
1081 return -ENOMEM;
1082
1083 spin_lock_init(&rrpc->inflights.lock);
1084 INIT_LIST_HEAD(&rrpc->inflights.reqs);
1085
1086 return 0;
1087}
1088
1089static void rrpc_core_free(struct rrpc *rrpc)
1090{
1091 mempool_destroy(rrpc->page_pool);
1092 mempool_destroy(rrpc->gcb_pool);
1093 mempool_destroy(rrpc->rq_pool);
1094}
1095
1096static void rrpc_luns_free(struct rrpc *rrpc)
1097{
1098 kfree(rrpc->luns);
1099}
1100
1101static int rrpc_luns_init(struct rrpc *rrpc, int lun_begin, int lun_end)
1102{
1103 struct nvm_dev *dev = rrpc->dev;
1104 struct rrpc_lun *rlun;
1105 int i, j;
1106
1107 spin_lock_init(&rrpc->rev_lock);
1108
1109 rrpc->luns = kcalloc(rrpc->nr_luns, sizeof(struct rrpc_lun),
1110 GFP_KERNEL);
1111 if (!rrpc->luns)
1112 return -ENOMEM;
1113
1114 /* 1:1 mapping */
1115 for (i = 0; i < rrpc->nr_luns; i++) {
1116 struct nvm_lun *lun = dev->mt->get_lun(dev, lun_begin + i);
1117
1118 if (dev->pgs_per_blk >
1119 MAX_INVALID_PAGES_STORAGE * BITS_PER_LONG) {
1120 pr_err("rrpc: number of pages per block too high.");
1121 goto err;
1122 }
1123
1124 rlun = &rrpc->luns[i];
1125 rlun->rrpc = rrpc;
1126 rlun->parent = lun;
1127 INIT_LIST_HEAD(&rlun->prio_list);
1128 INIT_WORK(&rlun->ws_gc, rrpc_lun_gc);
1129 spin_lock_init(&rlun->lock);
1130
1131 rrpc->total_blocks += dev->blks_per_lun;
1132 rrpc->nr_pages += dev->sec_per_lun;
1133
1134 rlun->blocks = vzalloc(sizeof(struct rrpc_block) *
1135 rrpc->dev->blks_per_lun);
1136 if (!rlun->blocks)
1137 goto err;
1138
1139 for (j = 0; j < rrpc->dev->blks_per_lun; j++) {
1140 struct rrpc_block *rblk = &rlun->blocks[j];
1141 struct nvm_block *blk = &lun->blocks[j];
1142
1143 rblk->parent = blk;
1144 INIT_LIST_HEAD(&rblk->prio);
1145 spin_lock_init(&rblk->lock);
1146 }
1147 }
1148
1149 return 0;
1150err:
1151 return -ENOMEM;
1152}
1153
1154static void rrpc_free(struct rrpc *rrpc)
1155{
1156 rrpc_gc_free(rrpc);
1157 rrpc_map_free(rrpc);
1158 rrpc_core_free(rrpc);
1159 rrpc_luns_free(rrpc);
1160
1161 kfree(rrpc);
1162}
1163
1164static void rrpc_exit(void *private)
1165{
1166 struct rrpc *rrpc = private;
1167
1168 del_timer(&rrpc->gc_timer);
1169
1170 flush_workqueue(rrpc->krqd_wq);
1171 flush_workqueue(rrpc->kgc_wq);
1172
1173 rrpc_free(rrpc);
1174}
1175
1176static sector_t rrpc_capacity(void *private)
1177{
1178 struct rrpc *rrpc = private;
1179 struct nvm_dev *dev = rrpc->dev;
1180 sector_t reserved, provisioned;
1181
1182 /* cur, gc, and two emergency blocks for each lun */
1183 reserved = rrpc->nr_luns * dev->max_pages_per_blk * 4;
1184 provisioned = rrpc->nr_pages - reserved;
1185
1186 if (reserved > rrpc->nr_pages) {
1187 pr_err("rrpc: not enough space available to expose storage.\n");
1188 return 0;
1189 }
1190
1191 sector_div(provisioned, 10);
1192 return provisioned * 9 * NR_PHY_IN_LOG;
1193}
1194
1195/*
1196 * Looks up the logical address from reverse trans map and check if its valid by
1197 * comparing the logical to physical address with the physical address.
1198 * Returns 0 on free, otherwise 1 if in use
1199 */
1200static void rrpc_block_map_update(struct rrpc *rrpc, struct rrpc_block *rblk)
1201{
1202 struct nvm_dev *dev = rrpc->dev;
1203 int offset;
1204 struct rrpc_addr *laddr;
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +01001205 u64 paddr, pladdr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001206
1207 for (offset = 0; offset < dev->pgs_per_blk; offset++) {
1208 paddr = block_to_addr(rrpc, rblk) + offset;
1209
1210 pladdr = rrpc->rev_trans_map[paddr].addr;
1211 if (pladdr == ADDR_EMPTY)
1212 continue;
1213
1214 laddr = &rrpc->trans_map[pladdr];
1215
1216 if (paddr == laddr->addr) {
1217 laddr->rblk = rblk;
1218 } else {
1219 set_bit(offset, rblk->invalid_pages);
1220 rblk->nr_invalid_pages++;
1221 }
1222 }
1223}
1224
1225static int rrpc_blocks_init(struct rrpc *rrpc)
1226{
1227 struct rrpc_lun *rlun;
1228 struct rrpc_block *rblk;
1229 int lun_iter, blk_iter;
1230
1231 for (lun_iter = 0; lun_iter < rrpc->nr_luns; lun_iter++) {
1232 rlun = &rrpc->luns[lun_iter];
1233
1234 for (blk_iter = 0; blk_iter < rrpc->dev->blks_per_lun;
1235 blk_iter++) {
1236 rblk = &rlun->blocks[blk_iter];
1237 rrpc_block_map_update(rrpc, rblk);
1238 }
1239 }
1240
1241 return 0;
1242}
1243
1244static int rrpc_luns_configure(struct rrpc *rrpc)
1245{
1246 struct rrpc_lun *rlun;
1247 struct rrpc_block *rblk;
1248 int i;
1249
1250 for (i = 0; i < rrpc->nr_luns; i++) {
1251 rlun = &rrpc->luns[i];
1252
1253 rblk = rrpc_get_blk(rrpc, rlun, 0);
1254 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001255 goto err;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001256
1257 rrpc_set_lun_cur(rlun, rblk);
1258
1259 /* Emergency gc block */
1260 rblk = rrpc_get_blk(rrpc, rlun, 1);
1261 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001262 goto err;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001263 rlun->gc_cur = rblk;
1264 }
1265
1266 return 0;
Wenwei Taod3d1a432015-12-06 11:25:44 +01001267err:
1268 rrpc_put_blks(rrpc);
1269 return -EINVAL;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001270}
1271
1272static struct nvm_tgt_type tt_rrpc;
1273
1274static void *rrpc_init(struct nvm_dev *dev, struct gendisk *tdisk,
1275 int lun_begin, int lun_end)
1276{
1277 struct request_queue *bqueue = dev->q;
1278 struct request_queue *tqueue = tdisk->queue;
1279 struct rrpc *rrpc;
1280 int ret;
1281
1282 if (!(dev->identity.dom & NVM_RSP_L2P)) {
1283 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1284 dev->identity.dom);
1285 return ERR_PTR(-EINVAL);
1286 }
1287
1288 rrpc = kzalloc(sizeof(struct rrpc), GFP_KERNEL);
1289 if (!rrpc)
1290 return ERR_PTR(-ENOMEM);
1291
1292 rrpc->instance.tt = &tt_rrpc;
1293 rrpc->dev = dev;
1294 rrpc->disk = tdisk;
1295
1296 bio_list_init(&rrpc->requeue_bios);
1297 spin_lock_init(&rrpc->bio_lock);
1298 INIT_WORK(&rrpc->ws_requeue, rrpc_requeue);
1299
1300 rrpc->nr_luns = lun_end - lun_begin + 1;
1301
1302 /* simple round-robin strategy */
1303 atomic_set(&rrpc->next_lun, -1);
1304
1305 ret = rrpc_luns_init(rrpc, lun_begin, lun_end);
1306 if (ret) {
1307 pr_err("nvm: rrpc: could not initialize luns\n");
1308 goto err;
1309 }
1310
1311 rrpc->poffset = dev->sec_per_lun * lun_begin;
1312 rrpc->lun_offset = lun_begin;
1313
1314 ret = rrpc_core_init(rrpc);
1315 if (ret) {
1316 pr_err("nvm: rrpc: could not initialize core\n");
1317 goto err;
1318 }
1319
1320 ret = rrpc_map_init(rrpc);
1321 if (ret) {
1322 pr_err("nvm: rrpc: could not initialize maps\n");
1323 goto err;
1324 }
1325
1326 ret = rrpc_blocks_init(rrpc);
1327 if (ret) {
1328 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1329 goto err;
1330 }
1331
1332 ret = rrpc_luns_configure(rrpc);
1333 if (ret) {
1334 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1335 goto err;
1336 }
1337
1338 ret = rrpc_gc_init(rrpc);
1339 if (ret) {
1340 pr_err("nvm: rrpc: could not initialize gc\n");
1341 goto err;
1342 }
1343
1344 /* inherit the size from the underlying device */
1345 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1346 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1347
1348 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
1349 rrpc->nr_luns, (unsigned long long)rrpc->nr_pages);
1350
1351 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
1352
1353 return rrpc;
1354err:
1355 rrpc_free(rrpc);
1356 return ERR_PTR(ret);
1357}
1358
1359/* round robin, page-based FTL, and cost-based GC */
1360static struct nvm_tgt_type tt_rrpc = {
1361 .name = "rrpc",
1362 .version = {1, 0, 0},
1363
1364 .make_rq = rrpc_make_rq,
1365 .capacity = rrpc_capacity,
1366 .end_io = rrpc_end_io,
1367
1368 .init = rrpc_init,
1369 .exit = rrpc_exit,
1370};
1371
1372static int __init rrpc_module_init(void)
1373{
1374 return nvm_register_target(&tt_rrpc);
1375}
1376
1377static void rrpc_module_exit(void)
1378{
1379 nvm_unregister_target(&tt_rrpc);
1380}
1381
1382module_init(rrpc_module_init);
1383module_exit(rrpc_module_exit);
1384MODULE_LICENSE("GPL v2");
1385MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");