blob: c4d0b04ac52144fa1226f403ffebe551a2cd72c7 [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
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100555 BUG_ON(laddr >= rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100556
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 */
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100724 BUG_ON(!(laddr + i >= 0 && laddr + i < rrpc->nr_sects));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100725 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
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100755 BUG_ON(!(laddr >= 0 && laddr < rrpc->nr_sects));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100756 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;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001005 u64 elba = slba + nlb;
1006 u64 i;
1007
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001008 if (unlikely(elba > dev->total_secs)) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001009 pr_err("nvm: L2P data from device is out of bounds!\n");
1010 return -EINVAL;
1011 }
1012
1013 for (i = 0; i < nlb; i++) {
1014 u64 pba = le64_to_cpu(entries[i]);
1015 /* LNVM treats address-spaces as silos, LBA and PBA are
1016 * equally large and zero-indexed.
1017 */
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001018 if (unlikely(pba >= dev->total_secs && pba != U64_MAX)) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001019 pr_err("nvm: L2P data entry is out of bounds!\n");
1020 return -EINVAL;
1021 }
1022
1023 /* Address zero is a special one. The first page on a disk is
1024 * protected. As it often holds internal device boot
1025 * information.
1026 */
1027 if (!pba)
1028 continue;
1029
1030 addr[i].addr = pba;
1031 raddr[pba].addr = slba + i;
1032 }
1033
1034 return 0;
1035}
1036
1037static int rrpc_map_init(struct rrpc *rrpc)
1038{
1039 struct nvm_dev *dev = rrpc->dev;
1040 sector_t i;
1041 int ret;
1042
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001043 rrpc->trans_map = vzalloc(sizeof(struct rrpc_addr) * rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001044 if (!rrpc->trans_map)
1045 return -ENOMEM;
1046
1047 rrpc->rev_trans_map = vmalloc(sizeof(struct rrpc_rev_addr)
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001048 * rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001049 if (!rrpc->rev_trans_map)
1050 return -ENOMEM;
1051
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001052 for (i = 0; i < rrpc->nr_sects; i++) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001053 struct rrpc_addr *p = &rrpc->trans_map[i];
1054 struct rrpc_rev_addr *r = &rrpc->rev_trans_map[i];
1055
1056 p->addr = ADDR_EMPTY;
1057 r->addr = ADDR_EMPTY;
1058 }
1059
1060 if (!dev->ops->get_l2p_tbl)
1061 return 0;
1062
1063 /* Bring up the mapping table from device */
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001064 ret = dev->ops->get_l2p_tbl(dev, 0, dev->total_secs, rrpc_l2p_update,
1065 rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001066 if (ret) {
1067 pr_err("nvm: rrpc: could not read L2P table.\n");
1068 return -EINVAL;
1069 }
1070
1071 return 0;
1072}
1073
1074
1075/* Minimum pages needed within a lun */
1076#define PAGE_POOL_SIZE 16
1077#define ADDR_POOL_SIZE 64
1078
1079static int rrpc_core_init(struct rrpc *rrpc)
1080{
1081 down_write(&rrpc_lock);
1082 if (!rrpc_gcb_cache) {
1083 rrpc_gcb_cache = kmem_cache_create("rrpc_gcb",
1084 sizeof(struct rrpc_block_gc), 0, 0, NULL);
1085 if (!rrpc_gcb_cache) {
1086 up_write(&rrpc_lock);
1087 return -ENOMEM;
1088 }
1089
1090 rrpc_rq_cache = kmem_cache_create("rrpc_rq",
1091 sizeof(struct nvm_rq) + sizeof(struct rrpc_rq),
1092 0, 0, NULL);
1093 if (!rrpc_rq_cache) {
1094 kmem_cache_destroy(rrpc_gcb_cache);
1095 up_write(&rrpc_lock);
1096 return -ENOMEM;
1097 }
1098 }
1099 up_write(&rrpc_lock);
1100
1101 rrpc->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0);
1102 if (!rrpc->page_pool)
1103 return -ENOMEM;
1104
1105 rrpc->gcb_pool = mempool_create_slab_pool(rrpc->dev->nr_luns,
1106 rrpc_gcb_cache);
1107 if (!rrpc->gcb_pool)
1108 return -ENOMEM;
1109
1110 rrpc->rq_pool = mempool_create_slab_pool(64, rrpc_rq_cache);
1111 if (!rrpc->rq_pool)
1112 return -ENOMEM;
1113
1114 spin_lock_init(&rrpc->inflights.lock);
1115 INIT_LIST_HEAD(&rrpc->inflights.reqs);
1116
1117 return 0;
1118}
1119
1120static void rrpc_core_free(struct rrpc *rrpc)
1121{
1122 mempool_destroy(rrpc->page_pool);
1123 mempool_destroy(rrpc->gcb_pool);
1124 mempool_destroy(rrpc->rq_pool);
1125}
1126
1127static void rrpc_luns_free(struct rrpc *rrpc)
1128{
1129 kfree(rrpc->luns);
1130}
1131
1132static int rrpc_luns_init(struct rrpc *rrpc, int lun_begin, int lun_end)
1133{
1134 struct nvm_dev *dev = rrpc->dev;
1135 struct rrpc_lun *rlun;
1136 int i, j;
1137
Wenwei Tao4b79beb2016-01-12 07:49:27 +01001138 if (dev->pgs_per_blk > MAX_INVALID_PAGES_STORAGE * BITS_PER_LONG) {
1139 pr_err("rrpc: number of pages per block too high.");
1140 return -EINVAL;
1141 }
1142
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001143 spin_lock_init(&rrpc->rev_lock);
1144
1145 rrpc->luns = kcalloc(rrpc->nr_luns, sizeof(struct rrpc_lun),
1146 GFP_KERNEL);
1147 if (!rrpc->luns)
1148 return -ENOMEM;
1149
1150 /* 1:1 mapping */
1151 for (i = 0; i < rrpc->nr_luns; i++) {
1152 struct nvm_lun *lun = dev->mt->get_lun(dev, lun_begin + i);
1153
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001154 rlun = &rrpc->luns[i];
1155 rlun->rrpc = rrpc;
1156 rlun->parent = lun;
1157 INIT_LIST_HEAD(&rlun->prio_list);
Javier Gonzálezff0e4982016-01-12 07:49:33 +01001158 INIT_LIST_HEAD(&rlun->open_list);
1159 INIT_LIST_HEAD(&rlun->closed_list);
1160
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001161 INIT_WORK(&rlun->ws_gc, rrpc_lun_gc);
1162 spin_lock_init(&rlun->lock);
1163
1164 rrpc->total_blocks += dev->blks_per_lun;
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001165 rrpc->nr_sects += dev->sec_per_lun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001166
1167 rlun->blocks = vzalloc(sizeof(struct rrpc_block) *
1168 rrpc->dev->blks_per_lun);
1169 if (!rlun->blocks)
1170 goto err;
1171
1172 for (j = 0; j < rrpc->dev->blks_per_lun; j++) {
1173 struct rrpc_block *rblk = &rlun->blocks[j];
1174 struct nvm_block *blk = &lun->blocks[j];
1175
1176 rblk->parent = blk;
Javier Gonzálezd7a64d22016-01-12 07:49:31 +01001177 rblk->rlun = rlun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001178 INIT_LIST_HEAD(&rblk->prio);
1179 spin_lock_init(&rblk->lock);
1180 }
1181 }
1182
1183 return 0;
1184err:
1185 return -ENOMEM;
1186}
1187
1188static void rrpc_free(struct rrpc *rrpc)
1189{
1190 rrpc_gc_free(rrpc);
1191 rrpc_map_free(rrpc);
1192 rrpc_core_free(rrpc);
1193 rrpc_luns_free(rrpc);
1194
1195 kfree(rrpc);
1196}
1197
1198static void rrpc_exit(void *private)
1199{
1200 struct rrpc *rrpc = private;
1201
1202 del_timer(&rrpc->gc_timer);
1203
1204 flush_workqueue(rrpc->krqd_wq);
1205 flush_workqueue(rrpc->kgc_wq);
1206
1207 rrpc_free(rrpc);
1208}
1209
1210static sector_t rrpc_capacity(void *private)
1211{
1212 struct rrpc *rrpc = private;
1213 struct nvm_dev *dev = rrpc->dev;
1214 sector_t reserved, provisioned;
1215
1216 /* cur, gc, and two emergency blocks for each lun */
1217 reserved = rrpc->nr_luns * dev->max_pages_per_blk * 4;
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001218 provisioned = rrpc->nr_sects - reserved;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001219
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001220 if (reserved > rrpc->nr_sects) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001221 pr_err("rrpc: not enough space available to expose storage.\n");
1222 return 0;
1223 }
1224
1225 sector_div(provisioned, 10);
1226 return provisioned * 9 * NR_PHY_IN_LOG;
1227}
1228
1229/*
1230 * Looks up the logical address from reverse trans map and check if its valid by
1231 * comparing the logical to physical address with the physical address.
1232 * Returns 0 on free, otherwise 1 if in use
1233 */
1234static void rrpc_block_map_update(struct rrpc *rrpc, struct rrpc_block *rblk)
1235{
1236 struct nvm_dev *dev = rrpc->dev;
1237 int offset;
1238 struct rrpc_addr *laddr;
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +01001239 u64 paddr, pladdr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001240
1241 for (offset = 0; offset < dev->pgs_per_blk; offset++) {
1242 paddr = block_to_addr(rrpc, rblk) + offset;
1243
1244 pladdr = rrpc->rev_trans_map[paddr].addr;
1245 if (pladdr == ADDR_EMPTY)
1246 continue;
1247
1248 laddr = &rrpc->trans_map[pladdr];
1249
1250 if (paddr == laddr->addr) {
1251 laddr->rblk = rblk;
1252 } else {
1253 set_bit(offset, rblk->invalid_pages);
1254 rblk->nr_invalid_pages++;
1255 }
1256 }
1257}
1258
1259static int rrpc_blocks_init(struct rrpc *rrpc)
1260{
1261 struct rrpc_lun *rlun;
1262 struct rrpc_block *rblk;
1263 int lun_iter, blk_iter;
1264
1265 for (lun_iter = 0; lun_iter < rrpc->nr_luns; lun_iter++) {
1266 rlun = &rrpc->luns[lun_iter];
1267
1268 for (blk_iter = 0; blk_iter < rrpc->dev->blks_per_lun;
1269 blk_iter++) {
1270 rblk = &rlun->blocks[blk_iter];
1271 rrpc_block_map_update(rrpc, rblk);
1272 }
1273 }
1274
1275 return 0;
1276}
1277
1278static int rrpc_luns_configure(struct rrpc *rrpc)
1279{
1280 struct rrpc_lun *rlun;
1281 struct rrpc_block *rblk;
1282 int i;
1283
1284 for (i = 0; i < rrpc->nr_luns; i++) {
1285 rlun = &rrpc->luns[i];
1286
1287 rblk = rrpc_get_blk(rrpc, rlun, 0);
1288 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001289 goto err;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001290
1291 rrpc_set_lun_cur(rlun, rblk);
1292
1293 /* Emergency gc block */
1294 rblk = rrpc_get_blk(rrpc, rlun, 1);
1295 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001296 goto err;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001297 rlun->gc_cur = rblk;
1298 }
1299
1300 return 0;
Wenwei Taod3d1a432015-12-06 11:25:44 +01001301err:
1302 rrpc_put_blks(rrpc);
1303 return -EINVAL;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001304}
1305
1306static struct nvm_tgt_type tt_rrpc;
1307
1308static void *rrpc_init(struct nvm_dev *dev, struct gendisk *tdisk,
1309 int lun_begin, int lun_end)
1310{
1311 struct request_queue *bqueue = dev->q;
1312 struct request_queue *tqueue = tdisk->queue;
1313 struct rrpc *rrpc;
1314 int ret;
1315
1316 if (!(dev->identity.dom & NVM_RSP_L2P)) {
1317 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1318 dev->identity.dom);
1319 return ERR_PTR(-EINVAL);
1320 }
1321
1322 rrpc = kzalloc(sizeof(struct rrpc), GFP_KERNEL);
1323 if (!rrpc)
1324 return ERR_PTR(-ENOMEM);
1325
1326 rrpc->instance.tt = &tt_rrpc;
1327 rrpc->dev = dev;
1328 rrpc->disk = tdisk;
1329
1330 bio_list_init(&rrpc->requeue_bios);
1331 spin_lock_init(&rrpc->bio_lock);
1332 INIT_WORK(&rrpc->ws_requeue, rrpc_requeue);
1333
1334 rrpc->nr_luns = lun_end - lun_begin + 1;
1335
1336 /* simple round-robin strategy */
1337 atomic_set(&rrpc->next_lun, -1);
1338
1339 ret = rrpc_luns_init(rrpc, lun_begin, lun_end);
1340 if (ret) {
1341 pr_err("nvm: rrpc: could not initialize luns\n");
1342 goto err;
1343 }
1344
1345 rrpc->poffset = dev->sec_per_lun * lun_begin;
1346 rrpc->lun_offset = lun_begin;
1347
1348 ret = rrpc_core_init(rrpc);
1349 if (ret) {
1350 pr_err("nvm: rrpc: could not initialize core\n");
1351 goto err;
1352 }
1353
1354 ret = rrpc_map_init(rrpc);
1355 if (ret) {
1356 pr_err("nvm: rrpc: could not initialize maps\n");
1357 goto err;
1358 }
1359
1360 ret = rrpc_blocks_init(rrpc);
1361 if (ret) {
1362 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1363 goto err;
1364 }
1365
1366 ret = rrpc_luns_configure(rrpc);
1367 if (ret) {
1368 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1369 goto err;
1370 }
1371
1372 ret = rrpc_gc_init(rrpc);
1373 if (ret) {
1374 pr_err("nvm: rrpc: could not initialize gc\n");
1375 goto err;
1376 }
1377
1378 /* inherit the size from the underlying device */
1379 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1380 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1381
1382 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001383 rrpc->nr_luns, (unsigned long long)rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001384
1385 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
1386
1387 return rrpc;
1388err:
1389 rrpc_free(rrpc);
1390 return ERR_PTR(ret);
1391}
1392
1393/* round robin, page-based FTL, and cost-based GC */
1394static struct nvm_tgt_type tt_rrpc = {
1395 .name = "rrpc",
1396 .version = {1, 0, 0},
1397
1398 .make_rq = rrpc_make_rq,
1399 .capacity = rrpc_capacity,
1400 .end_io = rrpc_end_io,
1401
1402 .init = rrpc_init,
1403 .exit = rrpc_exit,
1404};
1405
1406static int __init rrpc_module_init(void)
1407{
1408 return nvm_register_target(&tt_rrpc);
1409}
1410
1411static void rrpc_module_exit(void)
1412{
1413 nvm_unregister_target(&tt_rrpc);
1414}
1415
1416module_init(rrpc_module_init);
1417module_exit(rrpc_module_exit);
1418MODULE_LICENSE("GPL v2");
1419MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");