blob: fa8d5be2987c16fa4002ce9a3e8d96cfbf8a1eae [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
Javier Gonzálezafb18e02016-03-03 14:47:53 -070041 div_u64_rem(a->addr, rrpc->dev->sec_per_blk, &pg_offset);
Matias Bjørlingae1519e2015-10-28 19:54:57 +010042 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,
Matias Bjørling5114e272016-07-07 09:54:12 +020051 unsigned int len)
Matias Bjørlingae1519e2015-10-28 19:54:57 +010052{
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
Wenwei Tao0de24152016-07-07 09:54:07 +020099 while (1) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100100 rqd = rrpc_inflight_laddr_acquire(rrpc, slba, len);
Wenwei Tao0de24152016-07-07 09:54:07 +0200101 if (rqd)
102 break;
103
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100104 schedule();
Wenwei Tao0de24152016-07-07 09:54:07 +0200105 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100106
107 if (IS_ERR(rqd)) {
108 pr_err("rrpc: unable to acquire inflight IO\n");
109 bio_io_error(bio);
110 return;
111 }
112
113 rrpc_invalidate_range(rrpc, slba, len);
114 rrpc_inflight_laddr_release(rrpc, rqd);
115}
116
117static int block_is_full(struct rrpc *rrpc, struct rrpc_block *rblk)
118{
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700119 return (rblk->next_page == rrpc->dev->sec_per_blk);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100120}
121
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700122/* Calculate relative addr for the given block, considering instantiated LUNs */
123static u64 block_to_rel_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
124{
125 struct nvm_block *blk = rblk->parent;
126 int lun_blk = blk->id % (rrpc->dev->blks_per_lun * rrpc->nr_luns);
127
128 return lun_blk * rrpc->dev->sec_per_blk;
129}
130
131/* Calculate global addr for the given block */
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100132static u64 block_to_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100133{
134 struct nvm_block *blk = rblk->parent;
135
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700136 return blk->id * rrpc->dev->sec_per_blk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100137}
138
Matias Bjørling7386af22015-11-16 15:34:44 +0100139static struct ppa_addr linear_to_generic_addr(struct nvm_dev *dev,
140 struct ppa_addr r)
141{
142 struct ppa_addr l;
143 int secs, pgs, blks, luns;
144 sector_t ppa = r.ppa;
145
146 l.ppa = 0;
147
148 div_u64_rem(ppa, dev->sec_per_pg, &secs);
149 l.g.sec = secs;
150
151 sector_div(ppa, dev->sec_per_pg);
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700152 div_u64_rem(ppa, dev->pgs_per_blk, &pgs);
Matias Bjørling7386af22015-11-16 15:34:44 +0100153 l.g.pg = pgs;
154
155 sector_div(ppa, dev->pgs_per_blk);
156 div_u64_rem(ppa, dev->blks_per_lun, &blks);
157 l.g.blk = blks;
158
159 sector_div(ppa, dev->blks_per_lun);
160 div_u64_rem(ppa, dev->luns_per_chnl, &luns);
161 l.g.lun = luns;
162
163 sector_div(ppa, dev->luns_per_chnl);
164 l.g.ch = ppa;
165
166 return l;
167}
168
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100169static struct ppa_addr rrpc_ppa_to_gaddr(struct nvm_dev *dev, u64 addr)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100170{
171 struct ppa_addr paddr;
172
173 paddr.ppa = addr;
Matias Bjørling7386af22015-11-16 15:34:44 +0100174 return linear_to_generic_addr(dev, paddr);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100175}
176
177/* requires lun->lock taken */
178static void rrpc_set_lun_cur(struct rrpc_lun *rlun, struct rrpc_block *rblk)
179{
180 struct rrpc *rrpc = rlun->rrpc;
181
182 BUG_ON(!rblk);
183
184 if (rlun->cur) {
185 spin_lock(&rlun->cur->lock);
186 WARN_ON(!block_is_full(rrpc, rlun->cur));
187 spin_unlock(&rlun->cur->lock);
188 }
189 rlun->cur = rblk;
190}
191
192static struct rrpc_block *rrpc_get_blk(struct rrpc *rrpc, struct rrpc_lun *rlun,
193 unsigned long flags)
194{
195 struct nvm_block *blk;
196 struct rrpc_block *rblk;
197
Matias Bjørling41285fa2016-07-07 09:54:19 +0200198 blk = nvm_get_blk(rrpc->dev, rlun->parent, flags);
Javier Gonzálezff0e4982016-01-12 07:49:33 +0100199 if (!blk) {
200 pr_err("nvm: rrpc: cannot get new block from media manager\n");
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100201 return NULL;
Javier Gonzálezff0e4982016-01-12 07:49:33 +0100202 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100203
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700204 rblk = rrpc_get_rblk(rlun, blk->id);
Javier Gonzálezff0e4982016-01-12 07:49:33 +0100205 blk->priv = rblk;
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700206 bitmap_zero(rblk->invalid_pages, rrpc->dev->sec_per_blk);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100207 rblk->next_page = 0;
208 rblk->nr_invalid_pages = 0;
209 atomic_set(&rblk->data_cmnt_size, 0);
210
211 return rblk;
212}
213
214static void rrpc_put_blk(struct rrpc *rrpc, struct rrpc_block *rblk)
215{
Matias Bjørling41285fa2016-07-07 09:54:19 +0200216 nvm_put_blk(rrpc->dev, rblk->parent);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100217}
218
Wenwei Taod3d1a432015-12-06 11:25:44 +0100219static void rrpc_put_blks(struct rrpc *rrpc)
220{
221 struct rrpc_lun *rlun;
222 int i;
223
224 for (i = 0; i < rrpc->nr_luns; i++) {
225 rlun = &rrpc->luns[i];
226 if (rlun->cur)
227 rrpc_put_blk(rrpc, rlun->cur);
228 if (rlun->gc_cur)
229 rrpc_put_blk(rrpc, rlun->gc_cur);
230 }
231}
232
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100233static struct rrpc_lun *get_next_lun(struct rrpc *rrpc)
234{
235 int next = atomic_inc_return(&rrpc->next_lun);
236
237 return &rrpc->luns[next % rrpc->nr_luns];
238}
239
240static void rrpc_gc_kick(struct rrpc *rrpc)
241{
242 struct rrpc_lun *rlun;
243 unsigned int i;
244
245 for (i = 0; i < rrpc->nr_luns; i++) {
246 rlun = &rrpc->luns[i];
247 queue_work(rrpc->krqd_wq, &rlun->ws_gc);
248 }
249}
250
251/*
252 * timed GC every interval.
253 */
254static void rrpc_gc_timer(unsigned long data)
255{
256 struct rrpc *rrpc = (struct rrpc *)data;
257
258 rrpc_gc_kick(rrpc);
259 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
260}
261
262static void rrpc_end_sync_bio(struct bio *bio)
263{
264 struct completion *waiting = bio->bi_private;
265
266 if (bio->bi_error)
267 pr_err("nvm: gc request failed (%u).\n", bio->bi_error);
268
269 complete(waiting);
270}
271
272/*
273 * rrpc_move_valid_pages -- migrate live data off the block
274 * @rrpc: the 'rrpc' structure
275 * @block: the block from which to migrate live pages
276 *
277 * Description:
278 * GC algorithms may call this function to migrate remaining live
279 * pages off the block prior to erasing it. This function blocks
280 * further execution until the operation is complete.
281 */
282static int rrpc_move_valid_pages(struct rrpc *rrpc, struct rrpc_block *rblk)
283{
284 struct request_queue *q = rrpc->dev->q;
285 struct rrpc_rev_addr *rev;
286 struct nvm_rq *rqd;
287 struct bio *bio;
288 struct page *page;
289 int slot;
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700290 int nr_sec_per_blk = rrpc->dev->sec_per_blk;
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100291 u64 phys_addr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100292 DECLARE_COMPLETION_ONSTACK(wait);
293
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700294 if (bitmap_full(rblk->invalid_pages, nr_sec_per_blk))
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100295 return 0;
296
297 bio = bio_alloc(GFP_NOIO, 1);
298 if (!bio) {
299 pr_err("nvm: could not alloc bio to gc\n");
300 return -ENOMEM;
301 }
302
303 page = mempool_alloc(rrpc->page_pool, GFP_NOIO);
Wenwei Tao16c6d042016-02-04 15:13:23 +0100304 if (!page) {
305 bio_put(bio);
Javier Gonzalez3bfbc6a2016-01-12 07:49:17 +0100306 return -ENOMEM;
Wenwei Tao16c6d042016-02-04 15:13:23 +0100307 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100308
309 while ((slot = find_first_zero_bit(rblk->invalid_pages,
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700310 nr_sec_per_blk)) < nr_sec_per_blk) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100311
312 /* Lock laddr */
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700313 phys_addr = rblk->parent->id * nr_sec_per_blk + slot;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100314
315try:
316 spin_lock(&rrpc->rev_lock);
317 /* Get logical address from physical to logical table */
318 rev = &rrpc->rev_trans_map[phys_addr - rrpc->poffset];
319 /* already updated by previous regular write */
320 if (rev->addr == ADDR_EMPTY) {
321 spin_unlock(&rrpc->rev_lock);
322 continue;
323 }
324
325 rqd = rrpc_inflight_laddr_acquire(rrpc, rev->addr, 1);
326 if (IS_ERR_OR_NULL(rqd)) {
327 spin_unlock(&rrpc->rev_lock);
328 schedule();
329 goto try;
330 }
331
332 spin_unlock(&rrpc->rev_lock);
333
334 /* Perform read to do GC */
335 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
Mike Christie95fe6c12016-06-05 14:31:48 -0500336 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100337 bio->bi_private = &wait;
338 bio->bi_end_io = rrpc_end_sync_bio;
339
340 /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
341 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
342
343 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
344 pr_err("rrpc: gc read failed.\n");
345 rrpc_inflight_laddr_release(rrpc, rqd);
346 goto finished;
347 }
348 wait_for_completion_io(&wait);
Wenwei Tao2b11c1b2016-01-12 07:49:23 +0100349 if (bio->bi_error) {
350 rrpc_inflight_laddr_release(rrpc, rqd);
351 goto finished;
352 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100353
354 bio_reset(bio);
355 reinit_completion(&wait);
356
357 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
Mike Christie95fe6c12016-06-05 14:31:48 -0500358 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100359 bio->bi_private = &wait;
360 bio->bi_end_io = rrpc_end_sync_bio;
361
362 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
363
364 /* turn the command around and write the data back to a new
365 * address
366 */
367 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
368 pr_err("rrpc: gc write failed.\n");
369 rrpc_inflight_laddr_release(rrpc, rqd);
370 goto finished;
371 }
372 wait_for_completion_io(&wait);
373
374 rrpc_inflight_laddr_release(rrpc, rqd);
Wenwei Tao2b11c1b2016-01-12 07:49:23 +0100375 if (bio->bi_error)
376 goto finished;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100377
378 bio_reset(bio);
379 }
380
381finished:
382 mempool_free(page, rrpc->page_pool);
383 bio_put(bio);
384
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700385 if (!bitmap_full(rblk->invalid_pages, nr_sec_per_blk)) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100386 pr_err("nvm: failed to garbage collect block\n");
387 return -EIO;
388 }
389
390 return 0;
391}
392
393static void rrpc_block_gc(struct work_struct *work)
394{
395 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
396 ws_gc);
397 struct rrpc *rrpc = gcb->rrpc;
398 struct rrpc_block *rblk = gcb->rblk;
Javier Gonzálezcca87bc2016-05-06 20:03:15 +0200399 struct rrpc_lun *rlun = rblk->rlun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100400 struct nvm_dev *dev = rrpc->dev;
401
Wenwei Taod0ca7982016-01-12 07:49:24 +0100402 mempool_free(gcb, rrpc->gcb_pool);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100403 pr_debug("nvm: block '%lu' being reclaimed\n", rblk->parent->id);
404
405 if (rrpc_move_valid_pages(rrpc, rblk))
Wenwei Taod0ca7982016-01-12 07:49:24 +0100406 goto put_back;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100407
Wenwei Taod0ca7982016-01-12 07:49:24 +0100408 if (nvm_erase_blk(dev, rblk->parent))
409 goto put_back;
410
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100411 rrpc_put_blk(rrpc, rblk);
Wenwei Taod0ca7982016-01-12 07:49:24 +0100412
413 return;
414
415put_back:
416 spin_lock(&rlun->lock);
417 list_add_tail(&rblk->prio, &rlun->prio_list);
418 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100419}
420
421/* the block with highest number of invalid pages, will be in the beginning
422 * of the list
423 */
424static struct rrpc_block *rblock_max_invalid(struct rrpc_block *ra,
425 struct rrpc_block *rb)
426{
427 if (ra->nr_invalid_pages == rb->nr_invalid_pages)
428 return ra;
429
430 return (ra->nr_invalid_pages < rb->nr_invalid_pages) ? rb : ra;
431}
432
433/* linearly find the block with highest number of invalid pages
434 * requires lun->lock
435 */
436static struct rrpc_block *block_prio_find_max(struct rrpc_lun *rlun)
437{
438 struct list_head *prio_list = &rlun->prio_list;
439 struct rrpc_block *rblock, *max;
440
441 BUG_ON(list_empty(prio_list));
442
443 max = list_first_entry(prio_list, struct rrpc_block, prio);
444 list_for_each_entry(rblock, prio_list, prio)
445 max = rblock_max_invalid(max, rblock);
446
447 return max;
448}
449
450static void rrpc_lun_gc(struct work_struct *work)
451{
452 struct rrpc_lun *rlun = container_of(work, struct rrpc_lun, ws_gc);
453 struct rrpc *rrpc = rlun->rrpc;
454 struct nvm_lun *lun = rlun->parent;
455 struct rrpc_block_gc *gcb;
456 unsigned int nr_blocks_need;
457
458 nr_blocks_need = rrpc->dev->blks_per_lun / GC_LIMIT_INVERSE;
459
460 if (nr_blocks_need < rrpc->nr_luns)
461 nr_blocks_need = rrpc->nr_luns;
462
Wenwei Taob2629242016-01-12 07:49:25 +0100463 spin_lock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100464 while (nr_blocks_need > lun->nr_free_blocks &&
465 !list_empty(&rlun->prio_list)) {
466 struct rrpc_block *rblock = block_prio_find_max(rlun);
467 struct nvm_block *block = rblock->parent;
468
469 if (!rblock->nr_invalid_pages)
470 break;
471
Wenwei Taob2629242016-01-12 07:49:25 +0100472 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
473 if (!gcb)
474 break;
475
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100476 list_del_init(&rblock->prio);
477
478 BUG_ON(!block_is_full(rrpc, rblock));
479
480 pr_debug("rrpc: selected block '%lu' for GC\n", block->id);
481
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100482 gcb->rrpc = rrpc;
483 gcb->rblk = rblock;
484 INIT_WORK(&gcb->ws_gc, rrpc_block_gc);
485
486 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
487
488 nr_blocks_need--;
489 }
Wenwei Taob2629242016-01-12 07:49:25 +0100490 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100491
492 /* TODO: Hint that request queue can be started again */
493}
494
495static void rrpc_gc_queue(struct work_struct *work)
496{
497 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
498 ws_gc);
499 struct rrpc *rrpc = gcb->rrpc;
500 struct rrpc_block *rblk = gcb->rblk;
Javier Gonzálezcca87bc2016-05-06 20:03:15 +0200501 struct rrpc_lun *rlun = rblk->rlun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100502
503 spin_lock(&rlun->lock);
504 list_add_tail(&rblk->prio, &rlun->prio_list);
505 spin_unlock(&rlun->lock);
506
507 mempool_free(gcb, rrpc->gcb_pool);
508 pr_debug("nvm: block '%lu' is full, allow GC (sched)\n",
509 rblk->parent->id);
510}
511
512static const struct block_device_operations rrpc_fops = {
513 .owner = THIS_MODULE,
514};
515
516static struct rrpc_lun *rrpc_get_lun_rr(struct rrpc *rrpc, int is_gc)
517{
518 unsigned int i;
519 struct rrpc_lun *rlun, *max_free;
520
521 if (!is_gc)
522 return get_next_lun(rrpc);
523
524 /* during GC, we don't care about RR, instead we want to make
525 * sure that we maintain evenness between the block luns.
526 */
527 max_free = &rrpc->luns[0];
528 /* prevent GC-ing lun from devouring pages of a lun with
529 * little free blocks. We don't take the lock as we only need an
530 * estimate.
531 */
532 rrpc_for_each_lun(rrpc, rlun, i) {
533 if (rlun->parent->nr_free_blocks >
534 max_free->parent->nr_free_blocks)
535 max_free = rlun;
536 }
537
538 return max_free;
539}
540
541static struct rrpc_addr *rrpc_update_map(struct rrpc *rrpc, sector_t laddr,
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100542 struct rrpc_block *rblk, u64 paddr)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100543{
544 struct rrpc_addr *gp;
545 struct rrpc_rev_addr *rev;
546
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100547 BUG_ON(laddr >= rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100548
549 gp = &rrpc->trans_map[laddr];
550 spin_lock(&rrpc->rev_lock);
551 if (gp->rblk)
552 rrpc_page_invalidate(rrpc, gp);
553
554 gp->addr = paddr;
555 gp->rblk = rblk;
556
557 rev = &rrpc->rev_trans_map[gp->addr - rrpc->poffset];
558 rev->addr = laddr;
559 spin_unlock(&rrpc->rev_lock);
560
561 return gp;
562}
563
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100564static u64 rrpc_alloc_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100565{
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100566 u64 addr = ADDR_EMPTY;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100567
568 spin_lock(&rblk->lock);
569 if (block_is_full(rrpc, rblk))
570 goto out;
571
572 addr = block_to_addr(rrpc, rblk) + rblk->next_page;
573
574 rblk->next_page++;
575out:
576 spin_unlock(&rblk->lock);
577 return addr;
578}
579
580/* Simple round-robin Logical to physical address translation.
581 *
582 * Retrieve the mapping using the active append point. Then update the ap for
583 * the next write to the disk.
584 *
585 * Returns rrpc_addr with the physical address and block. Remember to return to
586 * rrpc->addr_cache when request is finished.
587 */
588static struct rrpc_addr *rrpc_map_page(struct rrpc *rrpc, sector_t laddr,
589 int is_gc)
590{
591 struct rrpc_lun *rlun;
592 struct rrpc_block *rblk;
593 struct nvm_lun *lun;
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100594 u64 paddr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100595
596 rlun = rrpc_get_lun_rr(rrpc, is_gc);
597 lun = rlun->parent;
598
599 if (!is_gc && lun->nr_free_blocks < rrpc->nr_luns * 4)
600 return NULL;
601
602 spin_lock(&rlun->lock);
603
604 rblk = rlun->cur;
605retry:
606 paddr = rrpc_alloc_addr(rrpc, rblk);
607
608 if (paddr == ADDR_EMPTY) {
609 rblk = rrpc_get_blk(rrpc, rlun, 0);
610 if (rblk) {
611 rrpc_set_lun_cur(rlun, rblk);
612 goto retry;
613 }
614
615 if (is_gc) {
616 /* retry from emergency gc block */
617 paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur);
618 if (paddr == ADDR_EMPTY) {
619 rblk = rrpc_get_blk(rrpc, rlun, 1);
620 if (!rblk) {
621 pr_err("rrpc: no more blocks");
622 goto err;
623 }
624
625 rlun->gc_cur = rblk;
626 paddr = rrpc_alloc_addr(rrpc, rlun->gc_cur);
627 }
628 rblk = rlun->gc_cur;
629 }
630 }
631
632 spin_unlock(&rlun->lock);
633 return rrpc_update_map(rrpc, laddr, rblk, paddr);
634err:
635 spin_unlock(&rlun->lock);
636 return NULL;
637}
638
639static void rrpc_run_gc(struct rrpc *rrpc, struct rrpc_block *rblk)
640{
641 struct rrpc_block_gc *gcb;
642
643 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
644 if (!gcb) {
645 pr_err("rrpc: unable to queue block for gc.");
646 return;
647 }
648
649 gcb->rrpc = rrpc;
650 gcb->rblk = rblk;
651
652 INIT_WORK(&gcb->ws_gc, rrpc_gc_queue);
653 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
654}
655
656static void rrpc_end_io_write(struct rrpc *rrpc, struct rrpc_rq *rrqd,
657 sector_t laddr, uint8_t npages)
658{
659 struct rrpc_addr *p;
660 struct rrpc_block *rblk;
661 struct nvm_lun *lun;
662 int cmnt_size, i;
663
664 for (i = 0; i < npages; i++) {
665 p = &rrpc->trans_map[laddr + i];
666 rblk = p->rblk;
667 lun = rblk->parent->lun;
668
669 cmnt_size = atomic_inc_return(&rblk->data_cmnt_size);
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700670 if (unlikely(cmnt_size == rrpc->dev->sec_per_blk))
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100671 rrpc_run_gc(rrpc, rblk);
672 }
673}
674
Matias Bjørling72d256e2016-01-12 07:49:29 +0100675static void rrpc_end_io(struct nvm_rq *rqd)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100676{
677 struct rrpc *rrpc = container_of(rqd->ins, struct rrpc, instance);
678 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
Javier González6d5be952016-05-06 20:03:20 +0200679 uint8_t npages = rqd->nr_ppas;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100680 sector_t laddr = rrpc_get_laddr(rqd->bio) - npages;
681
682 if (bio_data_dir(rqd->bio) == WRITE)
683 rrpc_end_io_write(rrpc, rrqd, laddr, npages);
684
Wenwei Tao3cd485b12016-01-12 07:49:15 +0100685 bio_put(rqd->bio);
686
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100687 if (rrqd->flags & NVM_IOTYPE_GC)
Matias Bjørling912761622016-01-12 07:49:21 +0100688 return;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100689
690 rrpc_unlock_rq(rrpc, rqd);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100691
692 if (npages > 1)
693 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100694
695 mempool_free(rqd, rrpc->rq_pool);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100696}
697
698static int rrpc_read_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
699 struct nvm_rq *rqd, unsigned long flags, int npages)
700{
701 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
702 struct rrpc_addr *gp;
703 sector_t laddr = rrpc_get_laddr(bio);
704 int is_gc = flags & NVM_IOTYPE_GC;
705 int i;
706
707 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
708 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
709 return NVM_IO_REQUEUE;
710 }
711
712 for (i = 0; i < npages; i++) {
713 /* We assume that mapping occurs at 4KB granularity */
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100714 BUG_ON(!(laddr + i >= 0 && laddr + i < rrpc->nr_sects));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100715 gp = &rrpc->trans_map[laddr + i];
716
717 if (gp->rblk) {
718 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
719 gp->addr);
720 } else {
721 BUG_ON(is_gc);
722 rrpc_unlock_laddr(rrpc, r);
723 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
724 rqd->dma_ppa_list);
725 return NVM_IO_DONE;
726 }
727 }
728
729 rqd->opcode = NVM_OP_HBREAD;
730
731 return NVM_IO_OK;
732}
733
734static int rrpc_read_rq(struct rrpc *rrpc, struct bio *bio, struct nvm_rq *rqd,
735 unsigned long flags)
736{
737 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
738 int is_gc = flags & NVM_IOTYPE_GC;
739 sector_t laddr = rrpc_get_laddr(bio);
740 struct rrpc_addr *gp;
741
742 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
743 return NVM_IO_REQUEUE;
744
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100745 BUG_ON(!(laddr >= 0 && laddr < rrpc->nr_sects));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100746 gp = &rrpc->trans_map[laddr];
747
748 if (gp->rblk) {
749 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, gp->addr);
750 } else {
751 BUG_ON(is_gc);
752 rrpc_unlock_rq(rrpc, rqd);
753 return NVM_IO_DONE;
754 }
755
756 rqd->opcode = NVM_OP_HBREAD;
757 rrqd->addr = gp;
758
759 return NVM_IO_OK;
760}
761
762static int rrpc_write_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
763 struct nvm_rq *rqd, unsigned long flags, int npages)
764{
765 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
766 struct rrpc_addr *p;
767 sector_t laddr = rrpc_get_laddr(bio);
768 int is_gc = flags & NVM_IOTYPE_GC;
769 int i;
770
771 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
772 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list, rqd->dma_ppa_list);
773 return NVM_IO_REQUEUE;
774 }
775
776 for (i = 0; i < npages; i++) {
777 /* We assume that mapping occurs at 4KB granularity */
778 p = rrpc_map_page(rrpc, laddr + i, is_gc);
779 if (!p) {
780 BUG_ON(is_gc);
781 rrpc_unlock_laddr(rrpc, r);
782 nvm_dev_dma_free(rrpc->dev, rqd->ppa_list,
783 rqd->dma_ppa_list);
784 rrpc_gc_kick(rrpc);
785 return NVM_IO_REQUEUE;
786 }
787
788 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(rrpc->dev,
789 p->addr);
790 }
791
792 rqd->opcode = NVM_OP_HBWRITE;
793
794 return NVM_IO_OK;
795}
796
797static int rrpc_write_rq(struct rrpc *rrpc, struct bio *bio,
798 struct nvm_rq *rqd, unsigned long flags)
799{
800 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
801 struct rrpc_addr *p;
802 int is_gc = flags & NVM_IOTYPE_GC;
803 sector_t laddr = rrpc_get_laddr(bio);
804
805 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
806 return NVM_IO_REQUEUE;
807
808 p = rrpc_map_page(rrpc, laddr, is_gc);
809 if (!p) {
810 BUG_ON(is_gc);
811 rrpc_unlock_rq(rrpc, rqd);
812 rrpc_gc_kick(rrpc);
813 return NVM_IO_REQUEUE;
814 }
815
816 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, p->addr);
817 rqd->opcode = NVM_OP_HBWRITE;
818 rrqd->addr = p;
819
820 return NVM_IO_OK;
821}
822
823static int rrpc_setup_rq(struct rrpc *rrpc, struct bio *bio,
824 struct nvm_rq *rqd, unsigned long flags, uint8_t npages)
825{
826 if (npages > 1) {
827 rqd->ppa_list = nvm_dev_dma_alloc(rrpc->dev, GFP_KERNEL,
828 &rqd->dma_ppa_list);
829 if (!rqd->ppa_list) {
830 pr_err("rrpc: not able to allocate ppa list\n");
831 return NVM_IO_ERR;
832 }
833
834 if (bio_rw(bio) == WRITE)
835 return rrpc_write_ppalist_rq(rrpc, bio, rqd, flags,
836 npages);
837
838 return rrpc_read_ppalist_rq(rrpc, bio, rqd, flags, npages);
839 }
840
841 if (bio_rw(bio) == WRITE)
842 return rrpc_write_rq(rrpc, bio, rqd, flags);
843
844 return rrpc_read_rq(rrpc, bio, rqd, flags);
845}
846
847static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio,
848 struct nvm_rq *rqd, unsigned long flags)
849{
850 int err;
851 struct rrpc_rq *rrq = nvm_rq_to_pdu(rqd);
852 uint8_t nr_pages = rrpc_get_pages(bio);
853 int bio_size = bio_sectors(bio) << 9;
854
855 if (bio_size < rrpc->dev->sec_size)
856 return NVM_IO_ERR;
857 else if (bio_size > rrpc->dev->max_rq_size)
858 return NVM_IO_ERR;
859
860 err = rrpc_setup_rq(rrpc, bio, rqd, flags, nr_pages);
861 if (err)
862 return err;
863
864 bio_get(bio);
865 rqd->bio = bio;
866 rqd->ins = &rrpc->instance;
Javier González6d5be952016-05-06 20:03:20 +0200867 rqd->nr_ppas = nr_pages;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100868 rrq->flags = flags;
869
870 err = nvm_submit_io(rrpc->dev, rqd);
871 if (err) {
872 pr_err("rrpc: I/O submission failed: %d\n", err);
Wenwei Tao3cd485b12016-01-12 07:49:15 +0100873 bio_put(bio);
Wenwei Taoc27278b2016-01-12 07:49:18 +0100874 if (!(flags & NVM_IOTYPE_GC)) {
875 rrpc_unlock_rq(rrpc, rqd);
Javier González6d5be952016-05-06 20:03:20 +0200876 if (rqd->nr_ppas > 1)
Wenwei Taoc27278b2016-01-12 07:49:18 +0100877 nvm_dev_dma_free(rrpc->dev,
878 rqd->ppa_list, rqd->dma_ppa_list);
879 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100880 return NVM_IO_ERR;
881 }
882
883 return NVM_IO_OK;
884}
885
Jens Axboedece1632015-11-05 10:41:16 -0700886static blk_qc_t rrpc_make_rq(struct request_queue *q, struct bio *bio)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100887{
888 struct rrpc *rrpc = q->queuedata;
889 struct nvm_rq *rqd;
890 int err;
891
Mike Christie95fe6c12016-06-05 14:31:48 -0500892 if (bio_op(bio) == REQ_OP_DISCARD) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100893 rrpc_discard(rrpc, bio);
Jens Axboedece1632015-11-05 10:41:16 -0700894 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100895 }
896
897 rqd = mempool_alloc(rrpc->rq_pool, GFP_KERNEL);
898 if (!rqd) {
899 pr_err_ratelimited("rrpc: not able to queue bio.");
900 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -0700901 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100902 }
903 memset(rqd, 0, sizeof(struct nvm_rq));
904
905 err = rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_NONE);
906 switch (err) {
907 case NVM_IO_OK:
Jens Axboedece1632015-11-05 10:41:16 -0700908 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100909 case NVM_IO_ERR:
910 bio_io_error(bio);
911 break;
912 case NVM_IO_DONE:
913 bio_endio(bio);
914 break;
915 case NVM_IO_REQUEUE:
916 spin_lock(&rrpc->bio_lock);
917 bio_list_add(&rrpc->requeue_bios, bio);
918 spin_unlock(&rrpc->bio_lock);
919 queue_work(rrpc->kgc_wq, &rrpc->ws_requeue);
920 break;
921 }
922
923 mempool_free(rqd, rrpc->rq_pool);
Jens Axboedece1632015-11-05 10:41:16 -0700924 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100925}
926
927static void rrpc_requeue(struct work_struct *work)
928{
929 struct rrpc *rrpc = container_of(work, struct rrpc, ws_requeue);
930 struct bio_list bios;
931 struct bio *bio;
932
933 bio_list_init(&bios);
934
935 spin_lock(&rrpc->bio_lock);
936 bio_list_merge(&bios, &rrpc->requeue_bios);
937 bio_list_init(&rrpc->requeue_bios);
938 spin_unlock(&rrpc->bio_lock);
939
940 while ((bio = bio_list_pop(&bios)))
941 rrpc_make_rq(rrpc->disk->queue, bio);
942}
943
944static void rrpc_gc_free(struct rrpc *rrpc)
945{
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100946 if (rrpc->krqd_wq)
947 destroy_workqueue(rrpc->krqd_wq);
948
949 if (rrpc->kgc_wq)
950 destroy_workqueue(rrpc->kgc_wq);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100951}
952
953static int rrpc_gc_init(struct rrpc *rrpc)
954{
955 rrpc->krqd_wq = alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM|WQ_UNBOUND,
956 rrpc->nr_luns);
957 if (!rrpc->krqd_wq)
958 return -ENOMEM;
959
960 rrpc->kgc_wq = alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM, 1);
961 if (!rrpc->kgc_wq)
962 return -ENOMEM;
963
964 setup_timer(&rrpc->gc_timer, rrpc_gc_timer, (unsigned long)rrpc);
965
966 return 0;
967}
968
969static void rrpc_map_free(struct rrpc *rrpc)
970{
971 vfree(rrpc->rev_trans_map);
972 vfree(rrpc->trans_map);
973}
974
975static int rrpc_l2p_update(u64 slba, u32 nlb, __le64 *entries, void *private)
976{
977 struct rrpc *rrpc = (struct rrpc *)private;
978 struct nvm_dev *dev = rrpc->dev;
979 struct rrpc_addr *addr = rrpc->trans_map + slba;
980 struct rrpc_rev_addr *raddr = rrpc->rev_trans_map;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100981 u64 elba = slba + nlb;
982 u64 i;
983
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100984 if (unlikely(elba > dev->total_secs)) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100985 pr_err("nvm: L2P data from device is out of bounds!\n");
986 return -EINVAL;
987 }
988
989 for (i = 0; i < nlb; i++) {
990 u64 pba = le64_to_cpu(entries[i]);
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700991 unsigned int mod;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100992 /* LNVM treats address-spaces as silos, LBA and PBA are
993 * equally large and zero-indexed.
994 */
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100995 if (unlikely(pba >= dev->total_secs && pba != U64_MAX)) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100996 pr_err("nvm: L2P data entry is out of bounds!\n");
997 return -EINVAL;
998 }
999
1000 /* Address zero is a special one. The first page on a disk is
1001 * protected. As it often holds internal device boot
1002 * information.
1003 */
1004 if (!pba)
1005 continue;
1006
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001007 div_u64_rem(pba, rrpc->nr_sects, &mod);
1008
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001009 addr[i].addr = pba;
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001010 raddr[mod].addr = slba + i;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001011 }
1012
1013 return 0;
1014}
1015
1016static int rrpc_map_init(struct rrpc *rrpc)
1017{
1018 struct nvm_dev *dev = rrpc->dev;
1019 sector_t i;
1020 int ret;
1021
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001022 rrpc->trans_map = vzalloc(sizeof(struct rrpc_addr) * rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001023 if (!rrpc->trans_map)
1024 return -ENOMEM;
1025
1026 rrpc->rev_trans_map = vmalloc(sizeof(struct rrpc_rev_addr)
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001027 * rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001028 if (!rrpc->rev_trans_map)
1029 return -ENOMEM;
1030
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001031 for (i = 0; i < rrpc->nr_sects; i++) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001032 struct rrpc_addr *p = &rrpc->trans_map[i];
1033 struct rrpc_rev_addr *r = &rrpc->rev_trans_map[i];
1034
1035 p->addr = ADDR_EMPTY;
1036 r->addr = ADDR_EMPTY;
1037 }
1038
1039 if (!dev->ops->get_l2p_tbl)
1040 return 0;
1041
1042 /* Bring up the mapping table from device */
Wenwei Tao909049a72016-05-06 20:03:01 +02001043 ret = dev->ops->get_l2p_tbl(dev, rrpc->soffset, rrpc->nr_sects,
1044 rrpc_l2p_update, rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001045 if (ret) {
1046 pr_err("nvm: rrpc: could not read L2P table.\n");
1047 return -EINVAL;
1048 }
1049
1050 return 0;
1051}
1052
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001053/* Minimum pages needed within a lun */
1054#define PAGE_POOL_SIZE 16
1055#define ADDR_POOL_SIZE 64
1056
1057static int rrpc_core_init(struct rrpc *rrpc)
1058{
1059 down_write(&rrpc_lock);
1060 if (!rrpc_gcb_cache) {
1061 rrpc_gcb_cache = kmem_cache_create("rrpc_gcb",
1062 sizeof(struct rrpc_block_gc), 0, 0, NULL);
1063 if (!rrpc_gcb_cache) {
1064 up_write(&rrpc_lock);
1065 return -ENOMEM;
1066 }
1067
1068 rrpc_rq_cache = kmem_cache_create("rrpc_rq",
1069 sizeof(struct nvm_rq) + sizeof(struct rrpc_rq),
1070 0, 0, NULL);
1071 if (!rrpc_rq_cache) {
1072 kmem_cache_destroy(rrpc_gcb_cache);
1073 up_write(&rrpc_lock);
1074 return -ENOMEM;
1075 }
1076 }
1077 up_write(&rrpc_lock);
1078
1079 rrpc->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0);
1080 if (!rrpc->page_pool)
1081 return -ENOMEM;
1082
1083 rrpc->gcb_pool = mempool_create_slab_pool(rrpc->dev->nr_luns,
1084 rrpc_gcb_cache);
1085 if (!rrpc->gcb_pool)
1086 return -ENOMEM;
1087
1088 rrpc->rq_pool = mempool_create_slab_pool(64, rrpc_rq_cache);
1089 if (!rrpc->rq_pool)
1090 return -ENOMEM;
1091
1092 spin_lock_init(&rrpc->inflights.lock);
1093 INIT_LIST_HEAD(&rrpc->inflights.reqs);
1094
1095 return 0;
1096}
1097
1098static void rrpc_core_free(struct rrpc *rrpc)
1099{
1100 mempool_destroy(rrpc->page_pool);
1101 mempool_destroy(rrpc->gcb_pool);
1102 mempool_destroy(rrpc->rq_pool);
1103}
1104
1105static void rrpc_luns_free(struct rrpc *rrpc)
1106{
Wenwei Taoda1e2842016-03-03 15:06:38 +01001107 struct nvm_dev *dev = rrpc->dev;
1108 struct nvm_lun *lun;
1109 struct rrpc_lun *rlun;
1110 int i;
1111
1112 if (!rrpc->luns)
1113 return;
1114
1115 for (i = 0; i < rrpc->nr_luns; i++) {
1116 rlun = &rrpc->luns[i];
1117 lun = rlun->parent;
1118 if (!lun)
1119 break;
1120 dev->mt->release_lun(dev, lun->id);
1121 vfree(rlun->blocks);
1122 }
1123
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001124 kfree(rrpc->luns);
1125}
1126
1127static int rrpc_luns_init(struct rrpc *rrpc, int lun_begin, int lun_end)
1128{
1129 struct nvm_dev *dev = rrpc->dev;
1130 struct rrpc_lun *rlun;
Wenwei Taoda1e2842016-03-03 15:06:38 +01001131 int i, j, ret = -EINVAL;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001132
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001133 if (dev->sec_per_blk > MAX_INVALID_PAGES_STORAGE * BITS_PER_LONG) {
Wenwei Tao4b79beb2016-01-12 07:49:27 +01001134 pr_err("rrpc: number of pages per block too high.");
1135 return -EINVAL;
1136 }
1137
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001138 spin_lock_init(&rrpc->rev_lock);
1139
1140 rrpc->luns = kcalloc(rrpc->nr_luns, sizeof(struct rrpc_lun),
1141 GFP_KERNEL);
1142 if (!rrpc->luns)
1143 return -ENOMEM;
1144
1145 /* 1:1 mapping */
1146 for (i = 0; i < rrpc->nr_luns; i++) {
Wenwei Taoda1e2842016-03-03 15:06:38 +01001147 int lunid = lun_begin + i;
1148 struct nvm_lun *lun;
1149
1150 if (dev->mt->reserve_lun(dev, lunid)) {
1151 pr_err("rrpc: lun %u is already allocated\n", lunid);
1152 goto err;
1153 }
1154
1155 lun = dev->mt->get_lun(dev, lunid);
1156 if (!lun)
1157 goto err;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001158
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001159 rlun = &rrpc->luns[i];
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001160 rlun->parent = lun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001161 rlun->blocks = vzalloc(sizeof(struct rrpc_block) *
1162 rrpc->dev->blks_per_lun);
Wenwei Taoda1e2842016-03-03 15:06:38 +01001163 if (!rlun->blocks) {
1164 ret = -ENOMEM;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001165 goto err;
Wenwei Taoda1e2842016-03-03 15:06:38 +01001166 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001167
1168 for (j = 0; j < rrpc->dev->blks_per_lun; j++) {
1169 struct rrpc_block *rblk = &rlun->blocks[j];
1170 struct nvm_block *blk = &lun->blocks[j];
1171
1172 rblk->parent = blk;
Javier Gonzálezd7a64d22016-01-12 07:49:31 +01001173 rblk->rlun = rlun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001174 INIT_LIST_HEAD(&rblk->prio);
1175 spin_lock_init(&rblk->lock);
1176 }
Wenwei Taoda1e2842016-03-03 15:06:38 +01001177
1178 rlun->rrpc = rrpc;
1179 INIT_LIST_HEAD(&rlun->prio_list);
Wenwei Taoda1e2842016-03-03 15:06:38 +01001180
1181 INIT_WORK(&rlun->ws_gc, rrpc_lun_gc);
1182 spin_lock_init(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001183 }
1184
1185 return 0;
1186err:
Wenwei Taoda1e2842016-03-03 15:06:38 +01001187 return ret;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001188}
1189
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001190/* returns 0 on success and stores the beginning address in *begin */
1191static int rrpc_area_init(struct rrpc *rrpc, sector_t *begin)
1192{
1193 struct nvm_dev *dev = rrpc->dev;
1194 struct nvmm_type *mt = dev->mt;
1195 sector_t size = rrpc->nr_sects * dev->sec_size;
Wenwei Tao909049a72016-05-06 20:03:01 +02001196 int ret;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001197
1198 size >>= 9;
1199
Wenwei Tao909049a72016-05-06 20:03:01 +02001200 ret = mt->get_area(dev, begin, size);
1201 if (!ret)
1202 *begin >>= (ilog2(dev->sec_size) - 9);
1203
1204 return ret;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001205}
1206
1207static void rrpc_area_free(struct rrpc *rrpc)
1208{
1209 struct nvm_dev *dev = rrpc->dev;
1210 struct nvmm_type *mt = dev->mt;
Wenwei Tao909049a72016-05-06 20:03:01 +02001211 sector_t begin = rrpc->soffset << (ilog2(dev->sec_size) - 9);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001212
Wenwei Tao909049a72016-05-06 20:03:01 +02001213 mt->put_area(dev, begin);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001214}
1215
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001216static void rrpc_free(struct rrpc *rrpc)
1217{
1218 rrpc_gc_free(rrpc);
1219 rrpc_map_free(rrpc);
1220 rrpc_core_free(rrpc);
1221 rrpc_luns_free(rrpc);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001222 rrpc_area_free(rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001223
1224 kfree(rrpc);
1225}
1226
1227static void rrpc_exit(void *private)
1228{
1229 struct rrpc *rrpc = private;
1230
1231 del_timer(&rrpc->gc_timer);
1232
1233 flush_workqueue(rrpc->krqd_wq);
1234 flush_workqueue(rrpc->kgc_wq);
1235
1236 rrpc_free(rrpc);
1237}
1238
1239static sector_t rrpc_capacity(void *private)
1240{
1241 struct rrpc *rrpc = private;
1242 struct nvm_dev *dev = rrpc->dev;
1243 sector_t reserved, provisioned;
1244
1245 /* cur, gc, and two emergency blocks for each lun */
Javier González116f7d42016-05-06 20:03:21 +02001246 reserved = rrpc->nr_luns * dev->sec_per_blk * 4;
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001247 provisioned = rrpc->nr_sects - reserved;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001248
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001249 if (reserved > rrpc->nr_sects) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001250 pr_err("rrpc: not enough space available to expose storage.\n");
1251 return 0;
1252 }
1253
1254 sector_div(provisioned, 10);
1255 return provisioned * 9 * NR_PHY_IN_LOG;
1256}
1257
1258/*
1259 * Looks up the logical address from reverse trans map and check if its valid by
1260 * comparing the logical to physical address with the physical address.
1261 * Returns 0 on free, otherwise 1 if in use
1262 */
1263static void rrpc_block_map_update(struct rrpc *rrpc, struct rrpc_block *rblk)
1264{
1265 struct nvm_dev *dev = rrpc->dev;
1266 int offset;
1267 struct rrpc_addr *laddr;
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001268 u64 bpaddr, paddr, pladdr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001269
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001270 bpaddr = block_to_rel_addr(rrpc, rblk);
1271 for (offset = 0; offset < dev->sec_per_blk; offset++) {
1272 paddr = bpaddr + offset;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001273
1274 pladdr = rrpc->rev_trans_map[paddr].addr;
1275 if (pladdr == ADDR_EMPTY)
1276 continue;
1277
1278 laddr = &rrpc->trans_map[pladdr];
1279
1280 if (paddr == laddr->addr) {
1281 laddr->rblk = rblk;
1282 } else {
1283 set_bit(offset, rblk->invalid_pages);
1284 rblk->nr_invalid_pages++;
1285 }
1286 }
1287}
1288
1289static int rrpc_blocks_init(struct rrpc *rrpc)
1290{
1291 struct rrpc_lun *rlun;
1292 struct rrpc_block *rblk;
1293 int lun_iter, blk_iter;
1294
1295 for (lun_iter = 0; lun_iter < rrpc->nr_luns; lun_iter++) {
1296 rlun = &rrpc->luns[lun_iter];
1297
1298 for (blk_iter = 0; blk_iter < rrpc->dev->blks_per_lun;
1299 blk_iter++) {
1300 rblk = &rlun->blocks[blk_iter];
1301 rrpc_block_map_update(rrpc, rblk);
1302 }
1303 }
1304
1305 return 0;
1306}
1307
1308static int rrpc_luns_configure(struct rrpc *rrpc)
1309{
1310 struct rrpc_lun *rlun;
1311 struct rrpc_block *rblk;
1312 int i;
1313
1314 for (i = 0; i < rrpc->nr_luns; i++) {
1315 rlun = &rrpc->luns[i];
1316
1317 rblk = rrpc_get_blk(rrpc, rlun, 0);
1318 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001319 goto err;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001320
1321 rrpc_set_lun_cur(rlun, rblk);
1322
1323 /* Emergency gc block */
1324 rblk = rrpc_get_blk(rrpc, rlun, 1);
1325 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001326 goto err;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001327 rlun->gc_cur = rblk;
1328 }
1329
1330 return 0;
Wenwei Taod3d1a432015-12-06 11:25:44 +01001331err:
1332 rrpc_put_blks(rrpc);
1333 return -EINVAL;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001334}
1335
1336static struct nvm_tgt_type tt_rrpc;
1337
1338static void *rrpc_init(struct nvm_dev *dev, struct gendisk *tdisk,
1339 int lun_begin, int lun_end)
1340{
1341 struct request_queue *bqueue = dev->q;
1342 struct request_queue *tqueue = tdisk->queue;
1343 struct rrpc *rrpc;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001344 sector_t soffset;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001345 int ret;
1346
1347 if (!(dev->identity.dom & NVM_RSP_L2P)) {
1348 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1349 dev->identity.dom);
1350 return ERR_PTR(-EINVAL);
1351 }
1352
1353 rrpc = kzalloc(sizeof(struct rrpc), GFP_KERNEL);
1354 if (!rrpc)
1355 return ERR_PTR(-ENOMEM);
1356
1357 rrpc->instance.tt = &tt_rrpc;
1358 rrpc->dev = dev;
1359 rrpc->disk = tdisk;
1360
1361 bio_list_init(&rrpc->requeue_bios);
1362 spin_lock_init(&rrpc->bio_lock);
1363 INIT_WORK(&rrpc->ws_requeue, rrpc_requeue);
1364
1365 rrpc->nr_luns = lun_end - lun_begin + 1;
Wenwei Tao66e3d072016-05-06 20:03:00 +02001366 rrpc->total_blocks = (unsigned long)dev->blks_per_lun * rrpc->nr_luns;
1367 rrpc->nr_sects = (unsigned long long)dev->sec_per_lun * rrpc->nr_luns;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001368
1369 /* simple round-robin strategy */
1370 atomic_set(&rrpc->next_lun, -1);
1371
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001372 ret = rrpc_area_init(rrpc, &soffset);
1373 if (ret < 0) {
1374 pr_err("nvm: rrpc: could not initialize area\n");
1375 return ERR_PTR(ret);
1376 }
1377 rrpc->soffset = soffset;
1378
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001379 ret = rrpc_luns_init(rrpc, lun_begin, lun_end);
1380 if (ret) {
1381 pr_err("nvm: rrpc: could not initialize luns\n");
1382 goto err;
1383 }
1384
1385 rrpc->poffset = dev->sec_per_lun * lun_begin;
1386 rrpc->lun_offset = lun_begin;
1387
1388 ret = rrpc_core_init(rrpc);
1389 if (ret) {
1390 pr_err("nvm: rrpc: could not initialize core\n");
1391 goto err;
1392 }
1393
1394 ret = rrpc_map_init(rrpc);
1395 if (ret) {
1396 pr_err("nvm: rrpc: could not initialize maps\n");
1397 goto err;
1398 }
1399
1400 ret = rrpc_blocks_init(rrpc);
1401 if (ret) {
1402 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1403 goto err;
1404 }
1405
1406 ret = rrpc_luns_configure(rrpc);
1407 if (ret) {
1408 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1409 goto err;
1410 }
1411
1412 ret = rrpc_gc_init(rrpc);
1413 if (ret) {
1414 pr_err("nvm: rrpc: could not initialize gc\n");
1415 goto err;
1416 }
1417
1418 /* inherit the size from the underlying device */
1419 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1420 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1421
1422 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001423 rrpc->nr_luns, (unsigned long long)rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001424
1425 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
1426
1427 return rrpc;
1428err:
1429 rrpc_free(rrpc);
1430 return ERR_PTR(ret);
1431}
1432
1433/* round robin, page-based FTL, and cost-based GC */
1434static struct nvm_tgt_type tt_rrpc = {
1435 .name = "rrpc",
1436 .version = {1, 0, 0},
1437
1438 .make_rq = rrpc_make_rq,
1439 .capacity = rrpc_capacity,
1440 .end_io = rrpc_end_io,
1441
1442 .init = rrpc_init,
1443 .exit = rrpc_exit,
1444};
1445
1446static int __init rrpc_module_init(void)
1447{
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001448 return nvm_register_tgt_type(&tt_rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001449}
1450
1451static void rrpc_module_exit(void)
1452{
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001453 nvm_unregister_tgt_type(&tt_rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001454}
1455
1456module_init(rrpc_module_init);
1457module_exit(rrpc_module_exit);
1458MODULE_LICENSE("GPL v2");
1459MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");