blob: e00b1d7b976f0e892c7ff27ce697e3eaeadd6481 [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{
Javier González8e79b5c2016-11-28 22:39:06 +010031 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +010032 struct rrpc_block *rblk = a->rblk;
33 unsigned int pg_offset;
34
35 lockdep_assert_held(&rrpc->rev_lock);
36
37 if (a->addr == ADDR_EMPTY || !rblk)
38 return;
39
40 spin_lock(&rblk->lock);
41
Javier González8e79b5c2016-11-28 22:39:06 +010042 div_u64_rem(a->addr, dev->geo.sec_per_blk, &pg_offset);
Matias Bjørlingae1519e2015-10-28 19:54:57 +010043 WARN_ON(test_and_set_bit(pg_offset, rblk->invalid_pages));
44 rblk->nr_invalid_pages++;
45
46 spin_unlock(&rblk->lock);
47
Javier González8e536242016-11-28 22:39:10 +010048 rrpc->rev_trans_map[a->addr].addr = ADDR_EMPTY;
Matias Bjørlingae1519e2015-10-28 19:54:57 +010049}
50
51static void rrpc_invalidate_range(struct rrpc *rrpc, sector_t slba,
Matias Bjørling5114e272016-07-07 09:54:12 +020052 unsigned int len)
Matias Bjørlingae1519e2015-10-28 19:54:57 +010053{
54 sector_t i;
55
56 spin_lock(&rrpc->rev_lock);
57 for (i = slba; i < slba + len; i++) {
58 struct rrpc_addr *gp = &rrpc->trans_map[i];
59
60 rrpc_page_invalidate(rrpc, gp);
61 gp->rblk = NULL;
62 }
63 spin_unlock(&rrpc->rev_lock);
64}
65
66static struct nvm_rq *rrpc_inflight_laddr_acquire(struct rrpc *rrpc,
67 sector_t laddr, unsigned int pages)
68{
69 struct nvm_rq *rqd;
70 struct rrpc_inflight_rq *inf;
71
72 rqd = mempool_alloc(rrpc->rq_pool, GFP_ATOMIC);
73 if (!rqd)
74 return ERR_PTR(-ENOMEM);
75
76 inf = rrpc_get_inflight_rq(rqd);
77 if (rrpc_lock_laddr(rrpc, laddr, pages, inf)) {
78 mempool_free(rqd, rrpc->rq_pool);
79 return NULL;
80 }
81
82 return rqd;
83}
84
85static void rrpc_inflight_laddr_release(struct rrpc *rrpc, struct nvm_rq *rqd)
86{
87 struct rrpc_inflight_rq *inf = rrpc_get_inflight_rq(rqd);
88
89 rrpc_unlock_laddr(rrpc, inf);
90
91 mempool_free(rqd, rrpc->rq_pool);
92}
93
94static void rrpc_discard(struct rrpc *rrpc, struct bio *bio)
95{
96 sector_t slba = bio->bi_iter.bi_sector / NR_PHY_IN_LOG;
97 sector_t len = bio->bi_iter.bi_size / RRPC_EXPOSED_PAGE_SIZE;
98 struct nvm_rq *rqd;
99
Wenwei Tao0de24152016-07-07 09:54:07 +0200100 while (1) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100101 rqd = rrpc_inflight_laddr_acquire(rrpc, slba, len);
Wenwei Tao0de24152016-07-07 09:54:07 +0200102 if (rqd)
103 break;
104
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100105 schedule();
Wenwei Tao0de24152016-07-07 09:54:07 +0200106 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100107
108 if (IS_ERR(rqd)) {
109 pr_err("rrpc: unable to acquire inflight IO\n");
110 bio_io_error(bio);
111 return;
112 }
113
114 rrpc_invalidate_range(rrpc, slba, len);
115 rrpc_inflight_laddr_release(rrpc, rqd);
116}
117
118static int block_is_full(struct rrpc *rrpc, struct rrpc_block *rblk)
119{
Javier González8e79b5c2016-11-28 22:39:06 +0100120 struct nvm_tgt_dev *dev = rrpc->dev;
121
122 return (rblk->next_page == dev->geo.sec_per_blk);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100123}
124
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700125/* Calculate relative addr for the given block, considering instantiated LUNs */
126static u64 block_to_rel_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
127{
Javier González8e79b5c2016-11-28 22:39:06 +0100128 struct nvm_tgt_dev *dev = rrpc->dev;
Javier González2a02e622016-11-28 22:39:09 +0100129 struct rrpc_lun *rlun = rblk->rlun;
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700130
Javier González8e536242016-11-28 22:39:10 +0100131 return rlun->id * dev->geo.sec_per_blk;
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700132}
133
Javier González8e536242016-11-28 22:39:10 +0100134static struct ppa_addr rrpc_ppa_to_gaddr(struct nvm_tgt_dev *dev,
135 struct rrpc_addr *gp)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100136{
Javier González8e536242016-11-28 22:39:10 +0100137 struct rrpc_block *rblk = gp->rblk;
Javier González2a02e622016-11-28 22:39:09 +0100138 struct rrpc_lun *rlun = rblk->rlun;
Javier González8e536242016-11-28 22:39:10 +0100139 u64 addr = gp->addr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100140 struct ppa_addr paddr;
141
142 paddr.ppa = addr;
Javier González8e536242016-11-28 22:39:10 +0100143 paddr = rrpc_linear_to_generic_addr(&dev->geo, paddr);
144 paddr.g.ch = rlun->bppa.g.ch;
145 paddr.g.lun = rlun->bppa.g.lun;
146 paddr.g.blk = rblk->id;
147
148 return paddr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100149}
150
151/* requires lun->lock taken */
Matias Bjørling855cdd22016-07-07 09:54:20 +0200152static void rrpc_set_lun_cur(struct rrpc_lun *rlun, struct rrpc_block *new_rblk,
153 struct rrpc_block **cur_rblk)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100154{
155 struct rrpc *rrpc = rlun->rrpc;
156
Matias Bjørling855cdd22016-07-07 09:54:20 +0200157 if (*cur_rblk) {
158 spin_lock(&(*cur_rblk)->lock);
159 WARN_ON(!block_is_full(rrpc, *cur_rblk));
160 spin_unlock(&(*cur_rblk)->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100161 }
Matias Bjørling855cdd22016-07-07 09:54:20 +0200162 *cur_rblk = new_rblk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100163}
164
Javier González2a02e622016-11-28 22:39:09 +0100165static struct rrpc_block *__rrpc_get_blk(struct rrpc *rrpc,
Javier González8e79b5c2016-11-28 22:39:06 +0100166 struct rrpc_lun *rlun)
167{
Javier González2a02e622016-11-28 22:39:09 +0100168 struct rrpc_block *rblk = NULL;
Javier González8e79b5c2016-11-28 22:39:06 +0100169
Javier González2a02e622016-11-28 22:39:09 +0100170 if (list_empty(&rlun->free_list))
Javier González8e79b5c2016-11-28 22:39:06 +0100171 goto out;
172
Javier González2a02e622016-11-28 22:39:09 +0100173 rblk = list_first_entry(&rlun->free_list, struct rrpc_block, list);
Javier González8e79b5c2016-11-28 22:39:06 +0100174
Javier González2a02e622016-11-28 22:39:09 +0100175 list_move_tail(&rblk->list, &rlun->used_list);
176 rblk->state = NVM_BLK_ST_TGT;
177 rlun->nr_free_blocks--;
Javier González8e79b5c2016-11-28 22:39:06 +0100178
179out:
Javier González2a02e622016-11-28 22:39:09 +0100180 return rblk;
Javier González8e79b5c2016-11-28 22:39:06 +0100181}
182
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100183static struct rrpc_block *rrpc_get_blk(struct rrpc *rrpc, struct rrpc_lun *rlun,
184 unsigned long flags)
185{
Javier González8e79b5c2016-11-28 22:39:06 +0100186 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100187 struct rrpc_block *rblk;
Javier González8e79b5c2016-11-28 22:39:06 +0100188 int is_gc = flags & NVM_IOTYPE_GC;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100189
Javier González8e79b5c2016-11-28 22:39:06 +0100190 spin_lock(&rlun->lock);
Javier González2a02e622016-11-28 22:39:09 +0100191 if (!is_gc && rlun->nr_free_blocks < rlun->reserved_blocks) {
Javier González8e79b5c2016-11-28 22:39:06 +0100192 pr_err("nvm: rrpc: cannot give block to non GC request\n");
193 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100194 return NULL;
Javier Gonzálezff0e4982016-01-12 07:49:33 +0100195 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100196
Javier González2a02e622016-11-28 22:39:09 +0100197 rblk = __rrpc_get_blk(rrpc, rlun);
198 if (!rblk) {
Javier González8e79b5c2016-11-28 22:39:06 +0100199 pr_err("nvm: rrpc: cannot get new block\n");
200 spin_unlock(&rlun->lock);
201 return NULL;
202 }
203 spin_unlock(&rlun->lock);
204
Javier González8e79b5c2016-11-28 22:39:06 +0100205 bitmap_zero(rblk->invalid_pages, dev->geo.sec_per_blk);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100206 rblk->next_page = 0;
207 rblk->nr_invalid_pages = 0;
208 atomic_set(&rblk->data_cmnt_size, 0);
209
210 return rblk;
211}
212
213static void rrpc_put_blk(struct rrpc *rrpc, struct rrpc_block *rblk)
214{
Javier González8e79b5c2016-11-28 22:39:06 +0100215 struct rrpc_lun *rlun = rblk->rlun;
Javier González8e79b5c2016-11-28 22:39:06 +0100216
217 spin_lock(&rlun->lock);
Javier González2a02e622016-11-28 22:39:09 +0100218 if (rblk->state & NVM_BLK_ST_TGT) {
219 list_move_tail(&rblk->list, &rlun->free_list);
220 rlun->nr_free_blocks++;
221 rblk->state = NVM_BLK_ST_FREE;
222 } else if (rblk->state & NVM_BLK_ST_BAD) {
223 list_move_tail(&rblk->list, &rlun->bb_list);
224 rblk->state = NVM_BLK_ST_BAD;
Javier González8e79b5c2016-11-28 22:39:06 +0100225 } else {
226 WARN_ON_ONCE(1);
Javier González2a02e622016-11-28 22:39:09 +0100227 pr_err("rrpc: erroneous type (ch:%d,lun:%d,blk%d-> %u)\n",
Javier González8e536242016-11-28 22:39:10 +0100228 rlun->bppa.g.ch, rlun->bppa.g.lun,
229 rblk->id, rblk->state);
Javier González2a02e622016-11-28 22:39:09 +0100230 list_move_tail(&rblk->list, &rlun->bb_list);
Javier González8e79b5c2016-11-28 22:39:06 +0100231 }
232 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100233}
234
Wenwei Taod3d1a432015-12-06 11:25:44 +0100235static void rrpc_put_blks(struct rrpc *rrpc)
236{
237 struct rrpc_lun *rlun;
238 int i;
239
240 for (i = 0; i < rrpc->nr_luns; i++) {
241 rlun = &rrpc->luns[i];
242 if (rlun->cur)
243 rrpc_put_blk(rrpc, rlun->cur);
244 if (rlun->gc_cur)
245 rrpc_put_blk(rrpc, rlun->gc_cur);
246 }
247}
248
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100249static struct rrpc_lun *get_next_lun(struct rrpc *rrpc)
250{
251 int next = atomic_inc_return(&rrpc->next_lun);
252
253 return &rrpc->luns[next % rrpc->nr_luns];
254}
255
256static void rrpc_gc_kick(struct rrpc *rrpc)
257{
258 struct rrpc_lun *rlun;
259 unsigned int i;
260
261 for (i = 0; i < rrpc->nr_luns; i++) {
262 rlun = &rrpc->luns[i];
263 queue_work(rrpc->krqd_wq, &rlun->ws_gc);
264 }
265}
266
267/*
268 * timed GC every interval.
269 */
270static void rrpc_gc_timer(unsigned long data)
271{
272 struct rrpc *rrpc = (struct rrpc *)data;
273
274 rrpc_gc_kick(rrpc);
275 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
276}
277
278static void rrpc_end_sync_bio(struct bio *bio)
279{
280 struct completion *waiting = bio->bi_private;
281
282 if (bio->bi_error)
283 pr_err("nvm: gc request failed (%u).\n", bio->bi_error);
284
285 complete(waiting);
286}
287
288/*
289 * rrpc_move_valid_pages -- migrate live data off the block
290 * @rrpc: the 'rrpc' structure
291 * @block: the block from which to migrate live pages
292 *
293 * Description:
294 * GC algorithms may call this function to migrate remaining live
295 * pages off the block prior to erasing it. This function blocks
296 * further execution until the operation is complete.
297 */
298static int rrpc_move_valid_pages(struct rrpc *rrpc, struct rrpc_block *rblk)
299{
Javier González8e79b5c2016-11-28 22:39:06 +0100300 struct nvm_tgt_dev *dev = rrpc->dev;
301 struct request_queue *q = dev->q;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100302 struct rrpc_rev_addr *rev;
303 struct nvm_rq *rqd;
304 struct bio *bio;
305 struct page *page;
306 int slot;
Javier González8e79b5c2016-11-28 22:39:06 +0100307 int nr_sec_per_blk = dev->geo.sec_per_blk;
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100308 u64 phys_addr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100309 DECLARE_COMPLETION_ONSTACK(wait);
310
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700311 if (bitmap_full(rblk->invalid_pages, nr_sec_per_blk))
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100312 return 0;
313
314 bio = bio_alloc(GFP_NOIO, 1);
315 if (!bio) {
316 pr_err("nvm: could not alloc bio to gc\n");
317 return -ENOMEM;
318 }
319
320 page = mempool_alloc(rrpc->page_pool, GFP_NOIO);
Wenwei Tao16c6d042016-02-04 15:13:23 +0100321 if (!page) {
322 bio_put(bio);
Javier Gonzalez3bfbc6a2016-01-12 07:49:17 +0100323 return -ENOMEM;
Wenwei Tao16c6d042016-02-04 15:13:23 +0100324 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100325
326 while ((slot = find_first_zero_bit(rblk->invalid_pages,
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700327 nr_sec_per_blk)) < nr_sec_per_blk) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100328
329 /* Lock laddr */
Javier González2a02e622016-11-28 22:39:09 +0100330 phys_addr = rrpc_blk_to_ppa(rrpc, rblk) + slot;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100331
332try:
333 spin_lock(&rrpc->rev_lock);
334 /* Get logical address from physical to logical table */
Javier González8e536242016-11-28 22:39:10 +0100335 rev = &rrpc->rev_trans_map[phys_addr];
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100336 /* already updated by previous regular write */
337 if (rev->addr == ADDR_EMPTY) {
338 spin_unlock(&rrpc->rev_lock);
339 continue;
340 }
341
342 rqd = rrpc_inflight_laddr_acquire(rrpc, rev->addr, 1);
343 if (IS_ERR_OR_NULL(rqd)) {
344 spin_unlock(&rrpc->rev_lock);
345 schedule();
346 goto try;
347 }
348
349 spin_unlock(&rrpc->rev_lock);
350
351 /* Perform read to do GC */
352 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
Mike Christie95fe6c12016-06-05 14:31:48 -0500353 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100354 bio->bi_private = &wait;
355 bio->bi_end_io = rrpc_end_sync_bio;
356
357 /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
358 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
359
360 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
361 pr_err("rrpc: gc read failed.\n");
362 rrpc_inflight_laddr_release(rrpc, rqd);
363 goto finished;
364 }
365 wait_for_completion_io(&wait);
Wenwei Tao2b11c1b2016-01-12 07:49:23 +0100366 if (bio->bi_error) {
367 rrpc_inflight_laddr_release(rrpc, rqd);
368 goto finished;
369 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100370
371 bio_reset(bio);
372 reinit_completion(&wait);
373
374 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
Mike Christie95fe6c12016-06-05 14:31:48 -0500375 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100376 bio->bi_private = &wait;
377 bio->bi_end_io = rrpc_end_sync_bio;
378
379 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
380
381 /* turn the command around and write the data back to a new
382 * address
383 */
384 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
385 pr_err("rrpc: gc write failed.\n");
386 rrpc_inflight_laddr_release(rrpc, rqd);
387 goto finished;
388 }
389 wait_for_completion_io(&wait);
390
391 rrpc_inflight_laddr_release(rrpc, rqd);
Wenwei Tao2b11c1b2016-01-12 07:49:23 +0100392 if (bio->bi_error)
393 goto finished;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100394
395 bio_reset(bio);
396 }
397
398finished:
399 mempool_free(page, rrpc->page_pool);
400 bio_put(bio);
401
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700402 if (!bitmap_full(rblk->invalid_pages, nr_sec_per_blk)) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100403 pr_err("nvm: failed to garbage collect block\n");
404 return -EIO;
405 }
406
407 return 0;
408}
409
410static void rrpc_block_gc(struct work_struct *work)
411{
412 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
413 ws_gc);
414 struct rrpc *rrpc = gcb->rrpc;
415 struct rrpc_block *rblk = gcb->rblk;
Javier Gonzálezcca87bc2016-05-06 20:03:15 +0200416 struct rrpc_lun *rlun = rblk->rlun;
Javier González8e79b5c2016-11-28 22:39:06 +0100417 struct nvm_tgt_dev *dev = rrpc->dev;
Javier González2a02e622016-11-28 22:39:09 +0100418 struct ppa_addr ppa;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100419
Wenwei Taod0ca7982016-01-12 07:49:24 +0100420 mempool_free(gcb, rrpc->gcb_pool);
Javier González2a02e622016-11-28 22:39:09 +0100421 pr_debug("nvm: block 'ch:%d,lun:%d,blk:%d' being reclaimed\n",
Javier González8e536242016-11-28 22:39:10 +0100422 rlun->bppa.g.ch, rlun->bppa.g.lun,
Javier González2a02e622016-11-28 22:39:09 +0100423 rblk->id);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100424
425 if (rrpc_move_valid_pages(rrpc, rblk))
Wenwei Taod0ca7982016-01-12 07:49:24 +0100426 goto put_back;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100427
Javier González2a02e622016-11-28 22:39:09 +0100428 ppa.ppa = 0;
Javier González8e536242016-11-28 22:39:10 +0100429 ppa.g.ch = rlun->bppa.g.ch;
430 ppa.g.lun = rlun->bppa.g.lun;
Javier González2a02e622016-11-28 22:39:09 +0100431 ppa.g.blk = rblk->id;
432
Javier González8e536242016-11-28 22:39:10 +0100433 if (nvm_erase_blk(dev, &ppa, 0))
Wenwei Taod0ca7982016-01-12 07:49:24 +0100434 goto put_back;
435
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100436 rrpc_put_blk(rrpc, rblk);
Wenwei Taod0ca7982016-01-12 07:49:24 +0100437
438 return;
439
440put_back:
441 spin_lock(&rlun->lock);
442 list_add_tail(&rblk->prio, &rlun->prio_list);
443 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100444}
445
446/* the block with highest number of invalid pages, will be in the beginning
447 * of the list
448 */
Javier González2a02e622016-11-28 22:39:09 +0100449static struct rrpc_block *rblk_max_invalid(struct rrpc_block *ra,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100450 struct rrpc_block *rb)
451{
452 if (ra->nr_invalid_pages == rb->nr_invalid_pages)
453 return ra;
454
455 return (ra->nr_invalid_pages < rb->nr_invalid_pages) ? rb : ra;
456}
457
458/* linearly find the block with highest number of invalid pages
459 * requires lun->lock
460 */
461static struct rrpc_block *block_prio_find_max(struct rrpc_lun *rlun)
462{
463 struct list_head *prio_list = &rlun->prio_list;
Javier González2a02e622016-11-28 22:39:09 +0100464 struct rrpc_block *rblk, *max;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100465
466 BUG_ON(list_empty(prio_list));
467
468 max = list_first_entry(prio_list, struct rrpc_block, prio);
Javier González2a02e622016-11-28 22:39:09 +0100469 list_for_each_entry(rblk, prio_list, prio)
470 max = rblk_max_invalid(max, rblk);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100471
472 return max;
473}
474
475static void rrpc_lun_gc(struct work_struct *work)
476{
477 struct rrpc_lun *rlun = container_of(work, struct rrpc_lun, ws_gc);
478 struct rrpc *rrpc = rlun->rrpc;
Javier González8e79b5c2016-11-28 22:39:06 +0100479 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100480 struct rrpc_block_gc *gcb;
481 unsigned int nr_blocks_need;
482
Javier González8e79b5c2016-11-28 22:39:06 +0100483 nr_blocks_need = dev->geo.blks_per_lun / GC_LIMIT_INVERSE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100484
485 if (nr_blocks_need < rrpc->nr_luns)
486 nr_blocks_need = rrpc->nr_luns;
487
Wenwei Taob2629242016-01-12 07:49:25 +0100488 spin_lock(&rlun->lock);
Javier González2a02e622016-11-28 22:39:09 +0100489 while (nr_blocks_need > rlun->nr_free_blocks &&
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100490 !list_empty(&rlun->prio_list)) {
Javier González2a02e622016-11-28 22:39:09 +0100491 struct rrpc_block *rblk = block_prio_find_max(rlun);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100492
Javier González2a02e622016-11-28 22:39:09 +0100493 if (!rblk->nr_invalid_pages)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100494 break;
495
Wenwei Taob2629242016-01-12 07:49:25 +0100496 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
497 if (!gcb)
498 break;
499
Javier González2a02e622016-11-28 22:39:09 +0100500 list_del_init(&rblk->prio);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100501
Javier González2a02e622016-11-28 22:39:09 +0100502 WARN_ON(!block_is_full(rrpc, rblk));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100503
Javier González2a02e622016-11-28 22:39:09 +0100504 pr_debug("rrpc: selected block 'ch:%d,lun:%d,blk:%d' for GC\n",
Javier González8e536242016-11-28 22:39:10 +0100505 rlun->bppa.g.ch, rlun->bppa.g.lun,
Javier González2a02e622016-11-28 22:39:09 +0100506 rblk->id);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100507
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100508 gcb->rrpc = rrpc;
Javier González2a02e622016-11-28 22:39:09 +0100509 gcb->rblk = rblk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100510 INIT_WORK(&gcb->ws_gc, rrpc_block_gc);
511
512 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
513
514 nr_blocks_need--;
515 }
Wenwei Taob2629242016-01-12 07:49:25 +0100516 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100517
518 /* TODO: Hint that request queue can be started again */
519}
520
521static void rrpc_gc_queue(struct work_struct *work)
522{
523 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
524 ws_gc);
525 struct rrpc *rrpc = gcb->rrpc;
526 struct rrpc_block *rblk = gcb->rblk;
Javier Gonzálezcca87bc2016-05-06 20:03:15 +0200527 struct rrpc_lun *rlun = rblk->rlun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100528
529 spin_lock(&rlun->lock);
530 list_add_tail(&rblk->prio, &rlun->prio_list);
531 spin_unlock(&rlun->lock);
532
533 mempool_free(gcb, rrpc->gcb_pool);
Javier González2a02e622016-11-28 22:39:09 +0100534 pr_debug("nvm: block 'ch:%d,lun:%d,blk:%d' full, allow GC (sched)\n",
Javier González8e536242016-11-28 22:39:10 +0100535 rlun->bppa.g.ch, rlun->bppa.g.lun,
Javier González2a02e622016-11-28 22:39:09 +0100536 rblk->id);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100537}
538
539static const struct block_device_operations rrpc_fops = {
540 .owner = THIS_MODULE,
541};
542
543static struct rrpc_lun *rrpc_get_lun_rr(struct rrpc *rrpc, int is_gc)
544{
545 unsigned int i;
546 struct rrpc_lun *rlun, *max_free;
547
548 if (!is_gc)
549 return get_next_lun(rrpc);
550
551 /* during GC, we don't care about RR, instead we want to make
552 * sure that we maintain evenness between the block luns.
553 */
554 max_free = &rrpc->luns[0];
555 /* prevent GC-ing lun from devouring pages of a lun with
556 * little free blocks. We don't take the lock as we only need an
557 * estimate.
558 */
559 rrpc_for_each_lun(rrpc, rlun, i) {
Javier González2a02e622016-11-28 22:39:09 +0100560 if (rlun->nr_free_blocks > max_free->nr_free_blocks)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100561 max_free = rlun;
562 }
563
564 return max_free;
565}
566
567static struct rrpc_addr *rrpc_update_map(struct rrpc *rrpc, sector_t laddr,
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100568 struct rrpc_block *rblk, u64 paddr)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100569{
570 struct rrpc_addr *gp;
571 struct rrpc_rev_addr *rev;
572
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100573 BUG_ON(laddr >= rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100574
575 gp = &rrpc->trans_map[laddr];
576 spin_lock(&rrpc->rev_lock);
577 if (gp->rblk)
578 rrpc_page_invalidate(rrpc, gp);
579
580 gp->addr = paddr;
581 gp->rblk = rblk;
582
Javier González8e536242016-11-28 22:39:10 +0100583 rev = &rrpc->rev_trans_map[gp->addr];
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100584 rev->addr = laddr;
585 spin_unlock(&rrpc->rev_lock);
586
587 return gp;
588}
589
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100590static u64 rrpc_alloc_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100591{
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100592 u64 addr = ADDR_EMPTY;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100593
594 spin_lock(&rblk->lock);
595 if (block_is_full(rrpc, rblk))
596 goto out;
597
Javier González8e536242016-11-28 22:39:10 +0100598 addr = rblk->next_page;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100599
600 rblk->next_page++;
601out:
602 spin_unlock(&rblk->lock);
603 return addr;
604}
605
Matias Bjørling855cdd22016-07-07 09:54:20 +0200606/* Map logical address to a physical page. The mapping implements a round robin
607 * approach and allocates a page from the next lun available.
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100608 *
Matias Bjørling855cdd22016-07-07 09:54:20 +0200609 * Returns rrpc_addr with the physical address and block. Returns NULL if no
610 * blocks in the next rlun are available.
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100611 */
Javier González8e536242016-11-28 22:39:10 +0100612static struct ppa_addr rrpc_map_page(struct rrpc *rrpc, sector_t laddr,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100613 int is_gc)
614{
Javier González8e536242016-11-28 22:39:10 +0100615 struct nvm_tgt_dev *tgt_dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100616 struct rrpc_lun *rlun;
Matias Bjørling855cdd22016-07-07 09:54:20 +0200617 struct rrpc_block *rblk, **cur_rblk;
Javier González8e536242016-11-28 22:39:10 +0100618 struct rrpc_addr *p;
619 struct ppa_addr ppa;
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100620 u64 paddr;
Matias Bjørling855cdd22016-07-07 09:54:20 +0200621 int gc_force = 0;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100622
Javier González8e536242016-11-28 22:39:10 +0100623 ppa.ppa = ADDR_EMPTY;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100624 rlun = rrpc_get_lun_rr(rrpc, is_gc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100625
Javier González2a02e622016-11-28 22:39:09 +0100626 if (!is_gc && rlun->nr_free_blocks < rrpc->nr_luns * 4)
Javier González8e536242016-11-28 22:39:10 +0100627 return ppa;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100628
Matias Bjørling855cdd22016-07-07 09:54:20 +0200629 /*
630 * page allocation steps:
631 * 1. Try to allocate new page from current rblk
632 * 2a. If succeed, proceed to map it in and return
633 * 2b. If fail, first try to allocate a new block from media manger,
634 * and then retry step 1. Retry until the normal block pool is
635 * exhausted.
636 * 3. If exhausted, and garbage collector is requesting the block,
637 * go to the reserved block and retry step 1.
638 * In the case that this fails as well, or it is not GC
639 * requesting, report not able to retrieve a block and let the
640 * caller handle further processing.
641 */
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100642
Matias Bjørling855cdd22016-07-07 09:54:20 +0200643 spin_lock(&rlun->lock);
644 cur_rblk = &rlun->cur;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100645 rblk = rlun->cur;
646retry:
647 paddr = rrpc_alloc_addr(rrpc, rblk);
648
Matias Bjørling855cdd22016-07-07 09:54:20 +0200649 if (paddr != ADDR_EMPTY)
650 goto done;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100651
Matias Bjørling855cdd22016-07-07 09:54:20 +0200652 if (!list_empty(&rlun->wblk_list)) {
653new_blk:
654 rblk = list_first_entry(&rlun->wblk_list, struct rrpc_block,
655 prio);
656 rrpc_set_lun_cur(rlun, rblk, cur_rblk);
657 list_del(&rblk->prio);
658 goto retry;
659 }
660 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100661
Matias Bjørling855cdd22016-07-07 09:54:20 +0200662 rblk = rrpc_get_blk(rrpc, rlun, gc_force);
663 if (rblk) {
664 spin_lock(&rlun->lock);
665 list_add_tail(&rblk->prio, &rlun->wblk_list);
666 /*
667 * another thread might already have added a new block,
668 * Therefore, make sure that one is used, instead of the
669 * one just added.
670 */
671 goto new_blk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100672 }
673
Matias Bjørling855cdd22016-07-07 09:54:20 +0200674 if (unlikely(is_gc) && !gc_force) {
675 /* retry from emergency gc block */
676 cur_rblk = &rlun->gc_cur;
677 rblk = rlun->gc_cur;
678 gc_force = 1;
679 spin_lock(&rlun->lock);
680 goto retry;
681 }
682
683 pr_err("rrpc: failed to allocate new block\n");
Javier González8e536242016-11-28 22:39:10 +0100684 return ppa;
Matias Bjørling855cdd22016-07-07 09:54:20 +0200685done:
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100686 spin_unlock(&rlun->lock);
Javier González8e536242016-11-28 22:39:10 +0100687 p = rrpc_update_map(rrpc, laddr, rblk, paddr);
688 if (!p)
689 return ppa;
690
691 /* return global address */
692 return rrpc_ppa_to_gaddr(tgt_dev, p);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100693}
694
695static void rrpc_run_gc(struct rrpc *rrpc, struct rrpc_block *rblk)
696{
697 struct rrpc_block_gc *gcb;
698
699 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
700 if (!gcb) {
701 pr_err("rrpc: unable to queue block for gc.");
702 return;
703 }
704
705 gcb->rrpc = rrpc;
706 gcb->rblk = rblk;
707
708 INIT_WORK(&gcb->ws_gc, rrpc_gc_queue);
709 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
710}
711
Javier González2a02e622016-11-28 22:39:09 +0100712static struct rrpc_lun *rrpc_ppa_to_lun(struct rrpc *rrpc, struct ppa_addr p)
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100713{
Javier González2a02e622016-11-28 22:39:09 +0100714 struct rrpc_lun *rlun = NULL;
715 int i;
716
717 for (i = 0; i < rrpc->nr_luns; i++) {
Javier González8e536242016-11-28 22:39:10 +0100718 if (rrpc->luns[i].bppa.g.ch == p.g.ch &&
719 rrpc->luns[i].bppa.g.lun == p.g.lun) {
Javier González2a02e622016-11-28 22:39:09 +0100720 rlun = &rrpc->luns[i];
721 break;
722 }
723 }
724
725 return rlun;
726}
727
728static void __rrpc_mark_bad_block(struct rrpc *rrpc, struct ppa_addr ppa)
729{
730 struct nvm_tgt_dev *dev = rrpc->dev;
731 struct rrpc_lun *rlun;
732 struct rrpc_block *rblk;
733
734 rlun = rrpc_ppa_to_lun(rrpc, ppa);
735 rblk = &rlun->blocks[ppa.g.blk];
736 rblk->state = NVM_BLK_ST_BAD;
737
Javier González333ba052016-11-28 22:39:14 +0100738 nvm_set_tgt_bb_tbl(dev, &ppa, 1, NVM_BLK_T_GRWN_BAD);
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100739}
740
741static void rrpc_mark_bad_block(struct rrpc *rrpc, struct nvm_rq *rqd)
742{
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100743 void *comp_bits = &rqd->ppa_status;
744 struct ppa_addr ppa, prev_ppa;
745 int nr_ppas = rqd->nr_ppas;
746 int bit;
747
748 if (rqd->nr_ppas == 1)
Javier González2a02e622016-11-28 22:39:09 +0100749 __rrpc_mark_bad_block(rrpc, rqd->ppa_addr);
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100750
751 ppa_set_empty(&prev_ppa);
752 bit = -1;
753 while ((bit = find_next_bit(comp_bits, nr_ppas, bit + 1)) < nr_ppas) {
754 ppa = rqd->ppa_list[bit];
755 if (ppa_cmp_blk(ppa, prev_ppa))
756 continue;
757
Javier González2a02e622016-11-28 22:39:09 +0100758 __rrpc_mark_bad_block(rrpc, ppa);
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100759 }
760}
761
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100762static void rrpc_end_io_write(struct rrpc *rrpc, struct rrpc_rq *rrqd,
763 sector_t laddr, uint8_t npages)
764{
Javier González8e79b5c2016-11-28 22:39:06 +0100765 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100766 struct rrpc_addr *p;
767 struct rrpc_block *rblk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100768 int cmnt_size, i;
769
770 for (i = 0; i < npages; i++) {
771 p = &rrpc->trans_map[laddr + i];
772 rblk = p->rblk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100773
774 cmnt_size = atomic_inc_return(&rblk->data_cmnt_size);
Javier González8e79b5c2016-11-28 22:39:06 +0100775 if (unlikely(cmnt_size == dev->geo.sec_per_blk))
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100776 rrpc_run_gc(rrpc, rblk);
777 }
778}
779
Matias Bjørling72d256e2016-01-12 07:49:29 +0100780static void rrpc_end_io(struct nvm_rq *rqd)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100781{
Matias Bjørling06894ef2017-01-31 13:17:17 +0100782 struct rrpc *rrpc = rqd->private;
Javier González8e79b5c2016-11-28 22:39:06 +0100783 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100784 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
Javier González6d5be952016-05-06 20:03:20 +0200785 uint8_t npages = rqd->nr_ppas;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100786 sector_t laddr = rrpc_get_laddr(rqd->bio) - npages;
787
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100788 if (bio_data_dir(rqd->bio) == WRITE) {
789 if (rqd->error == NVM_RSP_ERR_FAILWRITE)
790 rrpc_mark_bad_block(rrpc, rqd);
791
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100792 rrpc_end_io_write(rrpc, rrqd, laddr, npages);
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100793 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100794
Wenwei Tao3cd485b12016-01-12 07:49:15 +0100795 bio_put(rqd->bio);
796
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100797 if (rrqd->flags & NVM_IOTYPE_GC)
Matias Bjørling912761622016-01-12 07:49:21 +0100798 return;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100799
800 rrpc_unlock_rq(rrpc, rqd);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100801
802 if (npages > 1)
Javier González8e79b5c2016-11-28 22:39:06 +0100803 nvm_dev_dma_free(dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100804
805 mempool_free(rqd, rrpc->rq_pool);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100806}
807
808static int rrpc_read_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
809 struct nvm_rq *rqd, unsigned long flags, int npages)
810{
Javier González8e79b5c2016-11-28 22:39:06 +0100811 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100812 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
813 struct rrpc_addr *gp;
814 sector_t laddr = rrpc_get_laddr(bio);
815 int is_gc = flags & NVM_IOTYPE_GC;
816 int i;
817
818 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
Javier González8e79b5c2016-11-28 22:39:06 +0100819 nvm_dev_dma_free(dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100820 return NVM_IO_REQUEUE;
821 }
822
823 for (i = 0; i < npages; i++) {
824 /* We assume that mapping occurs at 4KB granularity */
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100825 BUG_ON(!(laddr + i >= 0 && laddr + i < rrpc->nr_sects));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100826 gp = &rrpc->trans_map[laddr + i];
827
828 if (gp->rblk) {
Javier González8e536242016-11-28 22:39:10 +0100829 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(dev, gp);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100830 } else {
831 BUG_ON(is_gc);
832 rrpc_unlock_laddr(rrpc, r);
Javier González8e79b5c2016-11-28 22:39:06 +0100833 nvm_dev_dma_free(dev->parent, rqd->ppa_list,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100834 rqd->dma_ppa_list);
835 return NVM_IO_DONE;
836 }
837 }
838
839 rqd->opcode = NVM_OP_HBREAD;
840
841 return NVM_IO_OK;
842}
843
844static int rrpc_read_rq(struct rrpc *rrpc, struct bio *bio, struct nvm_rq *rqd,
845 unsigned long flags)
846{
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100847 int is_gc = flags & NVM_IOTYPE_GC;
848 sector_t laddr = rrpc_get_laddr(bio);
849 struct rrpc_addr *gp;
850
851 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
852 return NVM_IO_REQUEUE;
853
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100854 BUG_ON(!(laddr >= 0 && laddr < rrpc->nr_sects));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100855 gp = &rrpc->trans_map[laddr];
856
857 if (gp->rblk) {
Javier González8e536242016-11-28 22:39:10 +0100858 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, gp);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100859 } else {
860 BUG_ON(is_gc);
861 rrpc_unlock_rq(rrpc, rqd);
862 return NVM_IO_DONE;
863 }
864
865 rqd->opcode = NVM_OP_HBREAD;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100866
867 return NVM_IO_OK;
868}
869
870static int rrpc_write_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
871 struct nvm_rq *rqd, unsigned long flags, int npages)
872{
Javier González8e79b5c2016-11-28 22:39:06 +0100873 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100874 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
Javier González8e536242016-11-28 22:39:10 +0100875 struct ppa_addr p;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100876 sector_t laddr = rrpc_get_laddr(bio);
877 int is_gc = flags & NVM_IOTYPE_GC;
878 int i;
879
880 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
Javier González8e79b5c2016-11-28 22:39:06 +0100881 nvm_dev_dma_free(dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100882 return NVM_IO_REQUEUE;
883 }
884
885 for (i = 0; i < npages; i++) {
886 /* We assume that mapping occurs at 4KB granularity */
887 p = rrpc_map_page(rrpc, laddr + i, is_gc);
Javier González8e536242016-11-28 22:39:10 +0100888 if (p.ppa == ADDR_EMPTY) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100889 BUG_ON(is_gc);
890 rrpc_unlock_laddr(rrpc, r);
Javier González8e79b5c2016-11-28 22:39:06 +0100891 nvm_dev_dma_free(dev->parent, rqd->ppa_list,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100892 rqd->dma_ppa_list);
893 rrpc_gc_kick(rrpc);
894 return NVM_IO_REQUEUE;
895 }
896
Javier González8e536242016-11-28 22:39:10 +0100897 rqd->ppa_list[i] = p;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100898 }
899
900 rqd->opcode = NVM_OP_HBWRITE;
901
902 return NVM_IO_OK;
903}
904
905static int rrpc_write_rq(struct rrpc *rrpc, struct bio *bio,
906 struct nvm_rq *rqd, unsigned long flags)
907{
Javier González8e536242016-11-28 22:39:10 +0100908 struct ppa_addr p;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100909 int is_gc = flags & NVM_IOTYPE_GC;
910 sector_t laddr = rrpc_get_laddr(bio);
911
912 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
913 return NVM_IO_REQUEUE;
914
915 p = rrpc_map_page(rrpc, laddr, is_gc);
Javier González8e536242016-11-28 22:39:10 +0100916 if (p.ppa == ADDR_EMPTY) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100917 BUG_ON(is_gc);
918 rrpc_unlock_rq(rrpc, rqd);
919 rrpc_gc_kick(rrpc);
920 return NVM_IO_REQUEUE;
921 }
922
Javier González8e536242016-11-28 22:39:10 +0100923 rqd->ppa_addr = p;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100924 rqd->opcode = NVM_OP_HBWRITE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100925
926 return NVM_IO_OK;
927}
928
929static int rrpc_setup_rq(struct rrpc *rrpc, struct bio *bio,
930 struct nvm_rq *rqd, unsigned long flags, uint8_t npages)
931{
Javier González8e79b5c2016-11-28 22:39:06 +0100932 struct nvm_tgt_dev *dev = rrpc->dev;
933
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100934 if (npages > 1) {
Javier González8e79b5c2016-11-28 22:39:06 +0100935 rqd->ppa_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100936 &rqd->dma_ppa_list);
937 if (!rqd->ppa_list) {
938 pr_err("rrpc: not able to allocate ppa list\n");
939 return NVM_IO_ERR;
940 }
941
Christoph Hellwig70246282016-07-19 11:28:41 +0200942 if (bio_op(bio) == REQ_OP_WRITE)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100943 return rrpc_write_ppalist_rq(rrpc, bio, rqd, flags,
944 npages);
945
946 return rrpc_read_ppalist_rq(rrpc, bio, rqd, flags, npages);
947 }
948
Christoph Hellwig70246282016-07-19 11:28:41 +0200949 if (bio_op(bio) == REQ_OP_WRITE)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100950 return rrpc_write_rq(rrpc, bio, rqd, flags);
951
952 return rrpc_read_rq(rrpc, bio, rqd, flags);
953}
954
955static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio,
956 struct nvm_rq *rqd, unsigned long flags)
957{
Javier González8e79b5c2016-11-28 22:39:06 +0100958 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100959 struct rrpc_rq *rrq = nvm_rq_to_pdu(rqd);
960 uint8_t nr_pages = rrpc_get_pages(bio);
961 int bio_size = bio_sectors(bio) << 9;
Javier González8e79b5c2016-11-28 22:39:06 +0100962 int err;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100963
Javier González8e79b5c2016-11-28 22:39:06 +0100964 if (bio_size < dev->geo.sec_size)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100965 return NVM_IO_ERR;
Javier González8e79b5c2016-11-28 22:39:06 +0100966 else if (bio_size > dev->geo.max_rq_size)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100967 return NVM_IO_ERR;
968
969 err = rrpc_setup_rq(rrpc, bio, rqd, flags, nr_pages);
970 if (err)
971 return err;
972
973 bio_get(bio);
974 rqd->bio = bio;
Matias Bjørling06894ef2017-01-31 13:17:17 +0100975 rqd->private = rrpc;
Javier González6d5be952016-05-06 20:03:20 +0200976 rqd->nr_ppas = nr_pages;
Matias Bjørling06894ef2017-01-31 13:17:17 +0100977 rqd->end_io = rrpc_end_io;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100978 rrq->flags = flags;
979
Javier González8e536242016-11-28 22:39:10 +0100980 err = nvm_submit_io(dev, rqd);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100981 if (err) {
982 pr_err("rrpc: I/O submission failed: %d\n", err);
Wenwei Tao3cd485b12016-01-12 07:49:15 +0100983 bio_put(bio);
Wenwei Taoc27278b2016-01-12 07:49:18 +0100984 if (!(flags & NVM_IOTYPE_GC)) {
985 rrpc_unlock_rq(rrpc, rqd);
Javier González6d5be952016-05-06 20:03:20 +0200986 if (rqd->nr_ppas > 1)
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100987 nvm_dev_dma_free(dev->parent, rqd->ppa_list,
988 rqd->dma_ppa_list);
Wenwei Taoc27278b2016-01-12 07:49:18 +0100989 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100990 return NVM_IO_ERR;
991 }
992
993 return NVM_IO_OK;
994}
995
Jens Axboedece1632015-11-05 10:41:16 -0700996static blk_qc_t rrpc_make_rq(struct request_queue *q, struct bio *bio)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100997{
998 struct rrpc *rrpc = q->queuedata;
999 struct nvm_rq *rqd;
1000 int err;
1001
Javier Gonzálezf0b01b62016-11-28 22:38:58 +01001002 blk_queue_split(q, &bio, q->bio_split);
1003
Mike Christie95fe6c12016-06-05 14:31:48 -05001004 if (bio_op(bio) == REQ_OP_DISCARD) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001005 rrpc_discard(rrpc, bio);
Jens Axboedece1632015-11-05 10:41:16 -07001006 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001007 }
1008
1009 rqd = mempool_alloc(rrpc->rq_pool, GFP_KERNEL);
1010 if (!rqd) {
1011 pr_err_ratelimited("rrpc: not able to queue bio.");
1012 bio_io_error(bio);
Jens Axboedece1632015-11-05 10:41:16 -07001013 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001014 }
1015 memset(rqd, 0, sizeof(struct nvm_rq));
1016
1017 err = rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_NONE);
1018 switch (err) {
1019 case NVM_IO_OK:
Jens Axboedece1632015-11-05 10:41:16 -07001020 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001021 case NVM_IO_ERR:
1022 bio_io_error(bio);
1023 break;
1024 case NVM_IO_DONE:
1025 bio_endio(bio);
1026 break;
1027 case NVM_IO_REQUEUE:
1028 spin_lock(&rrpc->bio_lock);
1029 bio_list_add(&rrpc->requeue_bios, bio);
1030 spin_unlock(&rrpc->bio_lock);
1031 queue_work(rrpc->kgc_wq, &rrpc->ws_requeue);
1032 break;
1033 }
1034
1035 mempool_free(rqd, rrpc->rq_pool);
Jens Axboedece1632015-11-05 10:41:16 -07001036 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001037}
1038
1039static void rrpc_requeue(struct work_struct *work)
1040{
1041 struct rrpc *rrpc = container_of(work, struct rrpc, ws_requeue);
1042 struct bio_list bios;
1043 struct bio *bio;
1044
1045 bio_list_init(&bios);
1046
1047 spin_lock(&rrpc->bio_lock);
1048 bio_list_merge(&bios, &rrpc->requeue_bios);
1049 bio_list_init(&rrpc->requeue_bios);
1050 spin_unlock(&rrpc->bio_lock);
1051
1052 while ((bio = bio_list_pop(&bios)))
1053 rrpc_make_rq(rrpc->disk->queue, bio);
1054}
1055
1056static void rrpc_gc_free(struct rrpc *rrpc)
1057{
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001058 if (rrpc->krqd_wq)
1059 destroy_workqueue(rrpc->krqd_wq);
1060
1061 if (rrpc->kgc_wq)
1062 destroy_workqueue(rrpc->kgc_wq);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001063}
1064
1065static int rrpc_gc_init(struct rrpc *rrpc)
1066{
1067 rrpc->krqd_wq = alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM|WQ_UNBOUND,
1068 rrpc->nr_luns);
1069 if (!rrpc->krqd_wq)
1070 return -ENOMEM;
1071
1072 rrpc->kgc_wq = alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM, 1);
1073 if (!rrpc->kgc_wq)
1074 return -ENOMEM;
1075
1076 setup_timer(&rrpc->gc_timer, rrpc_gc_timer, (unsigned long)rrpc);
1077
1078 return 0;
1079}
1080
1081static void rrpc_map_free(struct rrpc *rrpc)
1082{
1083 vfree(rrpc->rev_trans_map);
1084 vfree(rrpc->trans_map);
1085}
1086
1087static int rrpc_l2p_update(u64 slba, u32 nlb, __le64 *entries, void *private)
1088{
1089 struct rrpc *rrpc = (struct rrpc *)private;
Javier González8e79b5c2016-11-28 22:39:06 +01001090 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001091 struct rrpc_addr *addr = rrpc->trans_map + slba;
1092 struct rrpc_rev_addr *raddr = rrpc->rev_trans_map;
Javier González2a02e622016-11-28 22:39:09 +01001093 struct rrpc_lun *rlun;
1094 struct rrpc_block *rblk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001095 u64 i;
1096
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001097 for (i = 0; i < nlb; i++) {
Javier González2a02e622016-11-28 22:39:09 +01001098 struct ppa_addr gaddr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001099 u64 pba = le64_to_cpu(entries[i]);
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001100 unsigned int mod;
Javier González2a02e622016-11-28 22:39:09 +01001101
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001102 /* LNVM treats address-spaces as silos, LBA and PBA are
1103 * equally large and zero-indexed.
1104 */
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001105 if (unlikely(pba >= dev->total_secs && pba != U64_MAX)) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001106 pr_err("nvm: L2P data entry is out of bounds!\n");
Javier González2a02e622016-11-28 22:39:09 +01001107 pr_err("nvm: Maybe loaded an old target L2P\n");
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001108 return -EINVAL;
1109 }
1110
1111 /* Address zero is a special one. The first page on a disk is
1112 * protected. As it often holds internal device boot
1113 * information.
1114 */
1115 if (!pba)
1116 continue;
1117
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001118 div_u64_rem(pba, rrpc->nr_sects, &mod);
1119
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +01001120 gaddr = rrpc_recov_addr(dev, pba);
Javier González2a02e622016-11-28 22:39:09 +01001121 rlun = rrpc_ppa_to_lun(rrpc, gaddr);
1122 if (!rlun) {
1123 pr_err("rrpc: l2p corruption on lba %llu\n",
1124 slba + i);
1125 return -EINVAL;
1126 }
1127
1128 rblk = &rlun->blocks[gaddr.g.blk];
1129 if (!rblk->state) {
1130 /* at this point, we don't know anything about the
1131 * block. It's up to the FTL on top to re-etablish the
1132 * block state. The block is assumed to be open.
1133 */
1134 list_move_tail(&rblk->list, &rlun->used_list);
1135 rblk->state = NVM_BLK_ST_TGT;
1136 rlun->nr_free_blocks--;
1137 }
Javier González8e536242016-11-28 22:39:10 +01001138
1139 addr[i].addr = pba;
1140 addr[i].rblk = rblk;
1141 raddr[mod].addr = slba + i;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001142 }
1143
1144 return 0;
1145}
1146
1147static int rrpc_map_init(struct rrpc *rrpc)
1148{
Javier González8e79b5c2016-11-28 22:39:06 +01001149 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001150 sector_t i;
1151 int ret;
1152
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001153 rrpc->trans_map = vzalloc(sizeof(struct rrpc_addr) * rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001154 if (!rrpc->trans_map)
1155 return -ENOMEM;
1156
1157 rrpc->rev_trans_map = vmalloc(sizeof(struct rrpc_rev_addr)
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001158 * rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001159 if (!rrpc->rev_trans_map)
1160 return -ENOMEM;
1161
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001162 for (i = 0; i < rrpc->nr_sects; i++) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001163 struct rrpc_addr *p = &rrpc->trans_map[i];
1164 struct rrpc_rev_addr *r = &rrpc->rev_trans_map[i];
1165
1166 p->addr = ADDR_EMPTY;
1167 r->addr = ADDR_EMPTY;
1168 }
1169
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001170 /* Bring up the mapping table from device */
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +01001171 ret = nvm_get_l2p_tbl(dev, rrpc->soffset, rrpc->nr_sects,
1172 rrpc_l2p_update, rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001173 if (ret) {
1174 pr_err("nvm: rrpc: could not read L2P table.\n");
1175 return -EINVAL;
1176 }
1177
1178 return 0;
1179}
1180
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001181/* Minimum pages needed within a lun */
1182#define PAGE_POOL_SIZE 16
1183#define ADDR_POOL_SIZE 64
1184
1185static int rrpc_core_init(struct rrpc *rrpc)
1186{
1187 down_write(&rrpc_lock);
1188 if (!rrpc_gcb_cache) {
1189 rrpc_gcb_cache = kmem_cache_create("rrpc_gcb",
1190 sizeof(struct rrpc_block_gc), 0, 0, NULL);
1191 if (!rrpc_gcb_cache) {
1192 up_write(&rrpc_lock);
1193 return -ENOMEM;
1194 }
1195
1196 rrpc_rq_cache = kmem_cache_create("rrpc_rq",
1197 sizeof(struct nvm_rq) + sizeof(struct rrpc_rq),
1198 0, 0, NULL);
1199 if (!rrpc_rq_cache) {
1200 kmem_cache_destroy(rrpc_gcb_cache);
1201 up_write(&rrpc_lock);
1202 return -ENOMEM;
1203 }
1204 }
1205 up_write(&rrpc_lock);
1206
1207 rrpc->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0);
1208 if (!rrpc->page_pool)
1209 return -ENOMEM;
1210
Javier González8e79b5c2016-11-28 22:39:06 +01001211 rrpc->gcb_pool = mempool_create_slab_pool(rrpc->dev->geo.nr_luns,
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001212 rrpc_gcb_cache);
1213 if (!rrpc->gcb_pool)
1214 return -ENOMEM;
1215
1216 rrpc->rq_pool = mempool_create_slab_pool(64, rrpc_rq_cache);
1217 if (!rrpc->rq_pool)
1218 return -ENOMEM;
1219
1220 spin_lock_init(&rrpc->inflights.lock);
1221 INIT_LIST_HEAD(&rrpc->inflights.reqs);
1222
1223 return 0;
1224}
1225
1226static void rrpc_core_free(struct rrpc *rrpc)
1227{
1228 mempool_destroy(rrpc->page_pool);
1229 mempool_destroy(rrpc->gcb_pool);
1230 mempool_destroy(rrpc->rq_pool);
1231}
1232
1233static void rrpc_luns_free(struct rrpc *rrpc)
1234{
Wenwei Taoda1e2842016-03-03 15:06:38 +01001235 struct rrpc_lun *rlun;
1236 int i;
1237
1238 if (!rrpc->luns)
1239 return;
1240
1241 for (i = 0; i < rrpc->nr_luns; i++) {
1242 rlun = &rrpc->luns[i];
Wenwei Taoda1e2842016-03-03 15:06:38 +01001243 vfree(rlun->blocks);
1244 }
1245
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001246 kfree(rrpc->luns);
1247}
1248
Javier González2a02e622016-11-28 22:39:09 +01001249static int rrpc_bb_discovery(struct nvm_tgt_dev *dev, struct rrpc_lun *rlun)
1250{
1251 struct nvm_geo *geo = &dev->geo;
1252 struct rrpc_block *rblk;
1253 struct ppa_addr ppa;
1254 u8 *blks;
1255 int nr_blks;
1256 int i;
1257 int ret;
1258
Javier González959e9112016-11-28 22:39:11 +01001259 if (!dev->parent->ops->get_bb_tbl)
1260 return 0;
1261
Javier González2a02e622016-11-28 22:39:09 +01001262 nr_blks = geo->blks_per_lun * geo->plane_mode;
1263 blks = kmalloc(nr_blks, GFP_KERNEL);
1264 if (!blks)
1265 return -ENOMEM;
1266
1267 ppa.ppa = 0;
Javier González8e536242016-11-28 22:39:10 +01001268 ppa.g.ch = rlun->bppa.g.ch;
1269 ppa.g.lun = rlun->bppa.g.lun;
Javier González2a02e622016-11-28 22:39:09 +01001270
Javier González333ba052016-11-28 22:39:14 +01001271 ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
Javier González2a02e622016-11-28 22:39:09 +01001272 if (ret) {
1273 pr_err("rrpc: could not get BB table\n");
1274 goto out;
1275 }
1276
1277 nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
1278 if (nr_blks < 0)
1279 return nr_blks;
1280
Javier González2a02e622016-11-28 22:39:09 +01001281 for (i = 0; i < nr_blks; i++) {
1282 if (blks[i] == NVM_BLK_T_FREE)
1283 continue;
1284
1285 rblk = &rlun->blocks[i];
1286 list_move_tail(&rblk->list, &rlun->bb_list);
1287 rblk->state = NVM_BLK_ST_BAD;
1288 rlun->nr_free_blocks--;
1289 }
1290
1291out:
1292 kfree(blks);
1293 return ret;
1294}
1295
Javier González8e536242016-11-28 22:39:10 +01001296static void rrpc_set_lun_ppa(struct rrpc_lun *rlun, struct ppa_addr ppa)
1297{
1298 rlun->bppa.ppa = 0;
1299 rlun->bppa.g.ch = ppa.g.ch;
1300 rlun->bppa.g.lun = ppa.g.lun;
1301}
1302
1303static int rrpc_luns_init(struct rrpc *rrpc, struct ppa_addr *luns)
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001304{
Javier González8e79b5c2016-11-28 22:39:06 +01001305 struct nvm_tgt_dev *dev = rrpc->dev;
1306 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001307 struct rrpc_lun *rlun;
Wenwei Taoda1e2842016-03-03 15:06:38 +01001308 int i, j, ret = -EINVAL;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001309
Javier González8e79b5c2016-11-28 22:39:06 +01001310 if (geo->sec_per_blk > MAX_INVALID_PAGES_STORAGE * BITS_PER_LONG) {
Wenwei Tao4b79beb2016-01-12 07:49:27 +01001311 pr_err("rrpc: number of pages per block too high.");
1312 return -EINVAL;
1313 }
1314
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001315 spin_lock_init(&rrpc->rev_lock);
1316
1317 rrpc->luns = kcalloc(rrpc->nr_luns, sizeof(struct rrpc_lun),
1318 GFP_KERNEL);
1319 if (!rrpc->luns)
1320 return -ENOMEM;
1321
1322 /* 1:1 mapping */
Javier González8e536242016-11-28 22:39:10 +01001323 for (i = 0; i < rrpc->nr_luns; i++) {
1324 rlun = &rrpc->luns[i];
1325 rlun->id = i;
1326 rrpc_set_lun_ppa(rlun, luns[i]);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001327 rlun->blocks = vzalloc(sizeof(struct rrpc_block) *
Javier González8e79b5c2016-11-28 22:39:06 +01001328 geo->blks_per_lun);
Wenwei Taoda1e2842016-03-03 15:06:38 +01001329 if (!rlun->blocks) {
1330 ret = -ENOMEM;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001331 goto err;
Wenwei Taoda1e2842016-03-03 15:06:38 +01001332 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001333
Javier González2a02e622016-11-28 22:39:09 +01001334 INIT_LIST_HEAD(&rlun->free_list);
1335 INIT_LIST_HEAD(&rlun->used_list);
1336 INIT_LIST_HEAD(&rlun->bb_list);
1337
Javier González8e79b5c2016-11-28 22:39:06 +01001338 for (j = 0; j < geo->blks_per_lun; j++) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001339 struct rrpc_block *rblk = &rlun->blocks[j];
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001340
Javier González2a02e622016-11-28 22:39:09 +01001341 rblk->id = j;
Javier Gonzálezd7a64d22016-01-12 07:49:31 +01001342 rblk->rlun = rlun;
Javier González2a02e622016-11-28 22:39:09 +01001343 rblk->state = NVM_BLK_T_FREE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001344 INIT_LIST_HEAD(&rblk->prio);
Javier González2a02e622016-11-28 22:39:09 +01001345 INIT_LIST_HEAD(&rblk->list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001346 spin_lock_init(&rblk->lock);
Javier González2a02e622016-11-28 22:39:09 +01001347
1348 list_add_tail(&rblk->list, &rlun->free_list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001349 }
Wenwei Taoda1e2842016-03-03 15:06:38 +01001350
Javier González959e9112016-11-28 22:39:11 +01001351 rlun->rrpc = rrpc;
1352 rlun->nr_free_blocks = geo->blks_per_lun;
Javier González8e79b5c2016-11-28 22:39:06 +01001353 rlun->reserved_blocks = 2; /* for GC only */
1354
Wenwei Taoda1e2842016-03-03 15:06:38 +01001355 INIT_LIST_HEAD(&rlun->prio_list);
Matias Bjørling855cdd22016-07-07 09:54:20 +02001356 INIT_LIST_HEAD(&rlun->wblk_list);
Wenwei Taoda1e2842016-03-03 15:06:38 +01001357
1358 INIT_WORK(&rlun->ws_gc, rrpc_lun_gc);
1359 spin_lock_init(&rlun->lock);
Javier González959e9112016-11-28 22:39:11 +01001360
1361 if (rrpc_bb_discovery(dev, rlun))
1362 goto err;
1363
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001364 }
1365
1366 return 0;
1367err:
Wenwei Taoda1e2842016-03-03 15:06:38 +01001368 return ret;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001369}
1370
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001371/* returns 0 on success and stores the beginning address in *begin */
1372static int rrpc_area_init(struct rrpc *rrpc, sector_t *begin)
1373{
Javier González8e79b5c2016-11-28 22:39:06 +01001374 struct nvm_tgt_dev *dev = rrpc->dev;
Javier González8e79b5c2016-11-28 22:39:06 +01001375 sector_t size = rrpc->nr_sects * dev->geo.sec_size;
Wenwei Tao909049a72016-05-06 20:03:01 +02001376 int ret;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001377
1378 size >>= 9;
1379
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +01001380 ret = nvm_get_area(dev, begin, size);
Wenwei Tao909049a72016-05-06 20:03:01 +02001381 if (!ret)
Javier González8e79b5c2016-11-28 22:39:06 +01001382 *begin >>= (ilog2(dev->geo.sec_size) - 9);
Wenwei Tao909049a72016-05-06 20:03:01 +02001383
1384 return ret;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001385}
1386
1387static void rrpc_area_free(struct rrpc *rrpc)
1388{
Javier González8e79b5c2016-11-28 22:39:06 +01001389 struct nvm_tgt_dev *dev = rrpc->dev;
Javier González8e79b5c2016-11-28 22:39:06 +01001390 sector_t begin = rrpc->soffset << (ilog2(dev->geo.sec_size) - 9);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001391
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +01001392 nvm_put_area(dev, begin);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001393}
1394
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001395static void rrpc_free(struct rrpc *rrpc)
1396{
1397 rrpc_gc_free(rrpc);
1398 rrpc_map_free(rrpc);
1399 rrpc_core_free(rrpc);
1400 rrpc_luns_free(rrpc);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001401 rrpc_area_free(rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001402
1403 kfree(rrpc);
1404}
1405
1406static void rrpc_exit(void *private)
1407{
1408 struct rrpc *rrpc = private;
1409
1410 del_timer(&rrpc->gc_timer);
1411
1412 flush_workqueue(rrpc->krqd_wq);
1413 flush_workqueue(rrpc->kgc_wq);
1414
1415 rrpc_free(rrpc);
1416}
1417
1418static sector_t rrpc_capacity(void *private)
1419{
1420 struct rrpc *rrpc = private;
Javier González8e79b5c2016-11-28 22:39:06 +01001421 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001422 sector_t reserved, provisioned;
1423
1424 /* cur, gc, and two emergency blocks for each lun */
Javier González8e79b5c2016-11-28 22:39:06 +01001425 reserved = rrpc->nr_luns * dev->geo.sec_per_blk * 4;
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001426 provisioned = rrpc->nr_sects - reserved;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001427
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001428 if (reserved > rrpc->nr_sects) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001429 pr_err("rrpc: not enough space available to expose storage.\n");
1430 return 0;
1431 }
1432
1433 sector_div(provisioned, 10);
1434 return provisioned * 9 * NR_PHY_IN_LOG;
1435}
1436
1437/*
1438 * Looks up the logical address from reverse trans map and check if its valid by
1439 * comparing the logical to physical address with the physical address.
1440 * Returns 0 on free, otherwise 1 if in use
1441 */
1442static void rrpc_block_map_update(struct rrpc *rrpc, struct rrpc_block *rblk)
1443{
Javier González8e79b5c2016-11-28 22:39:06 +01001444 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001445 int offset;
1446 struct rrpc_addr *laddr;
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001447 u64 bpaddr, paddr, pladdr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001448
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001449 bpaddr = block_to_rel_addr(rrpc, rblk);
Javier González8e79b5c2016-11-28 22:39:06 +01001450 for (offset = 0; offset < dev->geo.sec_per_blk; offset++) {
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001451 paddr = bpaddr + offset;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001452
1453 pladdr = rrpc->rev_trans_map[paddr].addr;
1454 if (pladdr == ADDR_EMPTY)
1455 continue;
1456
1457 laddr = &rrpc->trans_map[pladdr];
1458
1459 if (paddr == laddr->addr) {
1460 laddr->rblk = rblk;
1461 } else {
1462 set_bit(offset, rblk->invalid_pages);
1463 rblk->nr_invalid_pages++;
1464 }
1465 }
1466}
1467
1468static int rrpc_blocks_init(struct rrpc *rrpc)
1469{
Javier González8e79b5c2016-11-28 22:39:06 +01001470 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001471 struct rrpc_lun *rlun;
1472 struct rrpc_block *rblk;
1473 int lun_iter, blk_iter;
1474
1475 for (lun_iter = 0; lun_iter < rrpc->nr_luns; lun_iter++) {
1476 rlun = &rrpc->luns[lun_iter];
1477
Javier González8e79b5c2016-11-28 22:39:06 +01001478 for (blk_iter = 0; blk_iter < dev->geo.blks_per_lun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001479 blk_iter++) {
1480 rblk = &rlun->blocks[blk_iter];
1481 rrpc_block_map_update(rrpc, rblk);
1482 }
1483 }
1484
1485 return 0;
1486}
1487
1488static int rrpc_luns_configure(struct rrpc *rrpc)
1489{
1490 struct rrpc_lun *rlun;
1491 struct rrpc_block *rblk;
1492 int i;
1493
1494 for (i = 0; i < rrpc->nr_luns; i++) {
1495 rlun = &rrpc->luns[i];
1496
1497 rblk = rrpc_get_blk(rrpc, rlun, 0);
1498 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001499 goto err;
Matias Bjørling855cdd22016-07-07 09:54:20 +02001500 rrpc_set_lun_cur(rlun, rblk, &rlun->cur);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001501
1502 /* Emergency gc block */
1503 rblk = rrpc_get_blk(rrpc, rlun, 1);
1504 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001505 goto err;
Matias Bjørling855cdd22016-07-07 09:54:20 +02001506 rrpc_set_lun_cur(rlun, rblk, &rlun->gc_cur);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001507 }
1508
1509 return 0;
Wenwei Taod3d1a432015-12-06 11:25:44 +01001510err:
1511 rrpc_put_blks(rrpc);
1512 return -EINVAL;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001513}
1514
1515static struct nvm_tgt_type tt_rrpc;
1516
Javier González8e536242016-11-28 22:39:10 +01001517static void *rrpc_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk)
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001518{
1519 struct request_queue *bqueue = dev->q;
1520 struct request_queue *tqueue = tdisk->queue;
Javier González8e79b5c2016-11-28 22:39:06 +01001521 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001522 struct rrpc *rrpc;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001523 sector_t soffset;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001524 int ret;
1525
1526 if (!(dev->identity.dom & NVM_RSP_L2P)) {
1527 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1528 dev->identity.dom);
1529 return ERR_PTR(-EINVAL);
1530 }
1531
1532 rrpc = kzalloc(sizeof(struct rrpc), GFP_KERNEL);
1533 if (!rrpc)
1534 return ERR_PTR(-ENOMEM);
1535
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001536 rrpc->dev = dev;
1537 rrpc->disk = tdisk;
1538
1539 bio_list_init(&rrpc->requeue_bios);
1540 spin_lock_init(&rrpc->bio_lock);
1541 INIT_WORK(&rrpc->ws_requeue, rrpc_requeue);
1542
Javier González8e79b5c2016-11-28 22:39:06 +01001543 rrpc->nr_luns = geo->nr_luns;
1544 rrpc->nr_sects = (unsigned long long)geo->sec_per_lun * rrpc->nr_luns;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001545
1546 /* simple round-robin strategy */
1547 atomic_set(&rrpc->next_lun, -1);
1548
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001549 ret = rrpc_area_init(rrpc, &soffset);
1550 if (ret < 0) {
1551 pr_err("nvm: rrpc: could not initialize area\n");
1552 return ERR_PTR(ret);
1553 }
1554 rrpc->soffset = soffset;
1555
Javier González8e536242016-11-28 22:39:10 +01001556 ret = rrpc_luns_init(rrpc, dev->luns);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001557 if (ret) {
1558 pr_err("nvm: rrpc: could not initialize luns\n");
1559 goto err;
1560 }
1561
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001562 ret = rrpc_core_init(rrpc);
1563 if (ret) {
1564 pr_err("nvm: rrpc: could not initialize core\n");
1565 goto err;
1566 }
1567
1568 ret = rrpc_map_init(rrpc);
1569 if (ret) {
1570 pr_err("nvm: rrpc: could not initialize maps\n");
1571 goto err;
1572 }
1573
1574 ret = rrpc_blocks_init(rrpc);
1575 if (ret) {
1576 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1577 goto err;
1578 }
1579
1580 ret = rrpc_luns_configure(rrpc);
1581 if (ret) {
1582 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1583 goto err;
1584 }
1585
1586 ret = rrpc_gc_init(rrpc);
1587 if (ret) {
1588 pr_err("nvm: rrpc: could not initialize gc\n");
1589 goto err;
1590 }
1591
1592 /* inherit the size from the underlying device */
1593 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1594 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1595
1596 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001597 rrpc->nr_luns, (unsigned long long)rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001598
1599 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
1600
1601 return rrpc;
1602err:
1603 rrpc_free(rrpc);
1604 return ERR_PTR(ret);
1605}
1606
1607/* round robin, page-based FTL, and cost-based GC */
1608static struct nvm_tgt_type tt_rrpc = {
1609 .name = "rrpc",
1610 .version = {1, 0, 0},
1611
1612 .make_rq = rrpc_make_rq,
1613 .capacity = rrpc_capacity,
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001614
1615 .init = rrpc_init,
1616 .exit = rrpc_exit,
1617};
1618
1619static int __init rrpc_module_init(void)
1620{
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001621 return nvm_register_tgt_type(&tt_rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001622}
1623
1624static void rrpc_module_exit(void)
1625{
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001626 nvm_unregister_tgt_type(&tt_rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001627}
1628
1629module_init(rrpc_module_init);
1630module_exit(rrpc_module_exit);
1631MODULE_LICENSE("GPL v2");
1632MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");