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