blob: 267f01ae87e447b6fba7e2fe47f39a1a926416f4 [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
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200282 if (bio->bi_status)
283 pr_err("nvm: gc request failed (%u).\n", bio->bi_status);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100284
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);
321
322 while ((slot = find_first_zero_bit(rblk->invalid_pages,
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700323 nr_sec_per_blk)) < nr_sec_per_blk) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100324
325 /* Lock laddr */
Javier González2a02e622016-11-28 22:39:09 +0100326 phys_addr = rrpc_blk_to_ppa(rrpc, rblk) + slot;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100327
328try:
329 spin_lock(&rrpc->rev_lock);
330 /* Get logical address from physical to logical table */
Javier González8e536242016-11-28 22:39:10 +0100331 rev = &rrpc->rev_trans_map[phys_addr];
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100332 /* already updated by previous regular write */
333 if (rev->addr == ADDR_EMPTY) {
334 spin_unlock(&rrpc->rev_lock);
335 continue;
336 }
337
338 rqd = rrpc_inflight_laddr_acquire(rrpc, rev->addr, 1);
339 if (IS_ERR_OR_NULL(rqd)) {
340 spin_unlock(&rrpc->rev_lock);
341 schedule();
342 goto try;
343 }
344
345 spin_unlock(&rrpc->rev_lock);
346
347 /* Perform read to do GC */
348 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
Mike Christie95fe6c12016-06-05 14:31:48 -0500349 bio_set_op_attrs(bio, REQ_OP_READ, 0);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100350 bio->bi_private = &wait;
351 bio->bi_end_io = rrpc_end_sync_bio;
352
353 /* TODO: may fail when EXP_PG_SIZE > PAGE_SIZE */
354 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
355
356 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
357 pr_err("rrpc: gc read failed.\n");
358 rrpc_inflight_laddr_release(rrpc, rqd);
359 goto finished;
360 }
361 wait_for_completion_io(&wait);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200362 if (bio->bi_status) {
Wenwei Tao2b11c1b2016-01-12 07:49:23 +0100363 rrpc_inflight_laddr_release(rrpc, rqd);
364 goto finished;
365 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100366
367 bio_reset(bio);
368 reinit_completion(&wait);
369
370 bio->bi_iter.bi_sector = rrpc_get_sector(rev->addr);
Mike Christie95fe6c12016-06-05 14:31:48 -0500371 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100372 bio->bi_private = &wait;
373 bio->bi_end_io = rrpc_end_sync_bio;
374
375 bio_add_pc_page(q, bio, page, RRPC_EXPOSED_PAGE_SIZE, 0);
376
377 /* turn the command around and write the data back to a new
378 * address
379 */
380 if (rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_GC)) {
381 pr_err("rrpc: gc write failed.\n");
382 rrpc_inflight_laddr_release(rrpc, rqd);
383 goto finished;
384 }
385 wait_for_completion_io(&wait);
386
387 rrpc_inflight_laddr_release(rrpc, rqd);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200388 if (bio->bi_status)
Wenwei Tao2b11c1b2016-01-12 07:49:23 +0100389 goto finished;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100390
391 bio_reset(bio);
392 }
393
394finished:
395 mempool_free(page, rrpc->page_pool);
396 bio_put(bio);
397
Javier Gonzálezafb18e02016-03-03 14:47:53 -0700398 if (!bitmap_full(rblk->invalid_pages, nr_sec_per_blk)) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100399 pr_err("nvm: failed to garbage collect block\n");
400 return -EIO;
401 }
402
403 return 0;
404}
405
406static void rrpc_block_gc(struct work_struct *work)
407{
408 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
409 ws_gc);
410 struct rrpc *rrpc = gcb->rrpc;
411 struct rrpc_block *rblk = gcb->rblk;
Javier Gonzálezcca87bc2016-05-06 20:03:15 +0200412 struct rrpc_lun *rlun = rblk->rlun;
Javier González2a02e622016-11-28 22:39:09 +0100413 struct ppa_addr ppa;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100414
Wenwei Taod0ca7982016-01-12 07:49:24 +0100415 mempool_free(gcb, rrpc->gcb_pool);
Javier González2a02e622016-11-28 22:39:09 +0100416 pr_debug("nvm: block 'ch:%d,lun:%d,blk:%d' being reclaimed\n",
Javier González8e536242016-11-28 22:39:10 +0100417 rlun->bppa.g.ch, rlun->bppa.g.lun,
Javier González2a02e622016-11-28 22:39:09 +0100418 rblk->id);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100419
420 if (rrpc_move_valid_pages(rrpc, rblk))
Wenwei Taod0ca7982016-01-12 07:49:24 +0100421 goto put_back;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100422
Javier González2a02e622016-11-28 22:39:09 +0100423 ppa.ppa = 0;
Javier González8e536242016-11-28 22:39:10 +0100424 ppa.g.ch = rlun->bppa.g.ch;
425 ppa.g.lun = rlun->bppa.g.lun;
Javier González2a02e622016-11-28 22:39:09 +0100426 ppa.g.blk = rblk->id;
427
Javier González17912c42017-04-15 20:55:37 +0200428 if (nvm_erase_sync(rrpc->dev, &ppa, 1))
Wenwei Taod0ca7982016-01-12 07:49:24 +0100429 goto put_back;
430
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100431 rrpc_put_blk(rrpc, rblk);
Wenwei Taod0ca7982016-01-12 07:49:24 +0100432
433 return;
434
435put_back:
436 spin_lock(&rlun->lock);
437 list_add_tail(&rblk->prio, &rlun->prio_list);
438 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100439}
440
441/* the block with highest number of invalid pages, will be in the beginning
442 * of the list
443 */
Javier González2a02e622016-11-28 22:39:09 +0100444static struct rrpc_block *rblk_max_invalid(struct rrpc_block *ra,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100445 struct rrpc_block *rb)
446{
447 if (ra->nr_invalid_pages == rb->nr_invalid_pages)
448 return ra;
449
450 return (ra->nr_invalid_pages < rb->nr_invalid_pages) ? rb : ra;
451}
452
453/* linearly find the block with highest number of invalid pages
454 * requires lun->lock
455 */
456static struct rrpc_block *block_prio_find_max(struct rrpc_lun *rlun)
457{
458 struct list_head *prio_list = &rlun->prio_list;
Javier González2a02e622016-11-28 22:39:09 +0100459 struct rrpc_block *rblk, *max;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100460
461 BUG_ON(list_empty(prio_list));
462
463 max = list_first_entry(prio_list, struct rrpc_block, prio);
Javier González2a02e622016-11-28 22:39:09 +0100464 list_for_each_entry(rblk, prio_list, prio)
465 max = rblk_max_invalid(max, rblk);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100466
467 return max;
468}
469
470static void rrpc_lun_gc(struct work_struct *work)
471{
472 struct rrpc_lun *rlun = container_of(work, struct rrpc_lun, ws_gc);
473 struct rrpc *rrpc = rlun->rrpc;
Javier González8e79b5c2016-11-28 22:39:06 +0100474 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100475 struct rrpc_block_gc *gcb;
476 unsigned int nr_blocks_need;
477
Javier González8e79b5c2016-11-28 22:39:06 +0100478 nr_blocks_need = dev->geo.blks_per_lun / GC_LIMIT_INVERSE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100479
480 if (nr_blocks_need < rrpc->nr_luns)
481 nr_blocks_need = rrpc->nr_luns;
482
Wenwei Taob2629242016-01-12 07:49:25 +0100483 spin_lock(&rlun->lock);
Javier González2a02e622016-11-28 22:39:09 +0100484 while (nr_blocks_need > rlun->nr_free_blocks &&
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100485 !list_empty(&rlun->prio_list)) {
Javier González2a02e622016-11-28 22:39:09 +0100486 struct rrpc_block *rblk = block_prio_find_max(rlun);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100487
Javier González2a02e622016-11-28 22:39:09 +0100488 if (!rblk->nr_invalid_pages)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100489 break;
490
Wenwei Taob2629242016-01-12 07:49:25 +0100491 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
492 if (!gcb)
493 break;
494
Javier González2a02e622016-11-28 22:39:09 +0100495 list_del_init(&rblk->prio);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100496
Javier González2a02e622016-11-28 22:39:09 +0100497 WARN_ON(!block_is_full(rrpc, rblk));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100498
Javier González2a02e622016-11-28 22:39:09 +0100499 pr_debug("rrpc: selected block 'ch:%d,lun:%d,blk:%d' for GC\n",
Javier González8e536242016-11-28 22:39:10 +0100500 rlun->bppa.g.ch, rlun->bppa.g.lun,
Javier González2a02e622016-11-28 22:39:09 +0100501 rblk->id);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100502
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100503 gcb->rrpc = rrpc;
Javier González2a02e622016-11-28 22:39:09 +0100504 gcb->rblk = rblk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100505 INIT_WORK(&gcb->ws_gc, rrpc_block_gc);
506
507 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
508
509 nr_blocks_need--;
510 }
Wenwei Taob2629242016-01-12 07:49:25 +0100511 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100512
513 /* TODO: Hint that request queue can be started again */
514}
515
516static void rrpc_gc_queue(struct work_struct *work)
517{
518 struct rrpc_block_gc *gcb = container_of(work, struct rrpc_block_gc,
519 ws_gc);
520 struct rrpc *rrpc = gcb->rrpc;
521 struct rrpc_block *rblk = gcb->rblk;
Javier Gonzálezcca87bc2016-05-06 20:03:15 +0200522 struct rrpc_lun *rlun = rblk->rlun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100523
524 spin_lock(&rlun->lock);
525 list_add_tail(&rblk->prio, &rlun->prio_list);
526 spin_unlock(&rlun->lock);
527
528 mempool_free(gcb, rrpc->gcb_pool);
Javier González2a02e622016-11-28 22:39:09 +0100529 pr_debug("nvm: block 'ch:%d,lun:%d,blk:%d' full, allow GC (sched)\n",
Javier González8e536242016-11-28 22:39:10 +0100530 rlun->bppa.g.ch, rlun->bppa.g.lun,
Javier González2a02e622016-11-28 22:39:09 +0100531 rblk->id);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100532}
533
534static const struct block_device_operations rrpc_fops = {
535 .owner = THIS_MODULE,
536};
537
538static struct rrpc_lun *rrpc_get_lun_rr(struct rrpc *rrpc, int is_gc)
539{
540 unsigned int i;
541 struct rrpc_lun *rlun, *max_free;
542
543 if (!is_gc)
544 return get_next_lun(rrpc);
545
546 /* during GC, we don't care about RR, instead we want to make
547 * sure that we maintain evenness between the block luns.
548 */
549 max_free = &rrpc->luns[0];
550 /* prevent GC-ing lun from devouring pages of a lun with
551 * little free blocks. We don't take the lock as we only need an
552 * estimate.
553 */
554 rrpc_for_each_lun(rrpc, rlun, i) {
Javier González2a02e622016-11-28 22:39:09 +0100555 if (rlun->nr_free_blocks > max_free->nr_free_blocks)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100556 max_free = rlun;
557 }
558
559 return max_free;
560}
561
562static struct rrpc_addr *rrpc_update_map(struct rrpc *rrpc, sector_t laddr,
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100563 struct rrpc_block *rblk, u64 paddr)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100564{
565 struct rrpc_addr *gp;
566 struct rrpc_rev_addr *rev;
567
Matias Bjørling4ece44a2016-02-20 08:52:41 +0100568 BUG_ON(laddr >= rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100569
570 gp = &rrpc->trans_map[laddr];
571 spin_lock(&rrpc->rev_lock);
572 if (gp->rblk)
573 rrpc_page_invalidate(rrpc, gp);
574
575 gp->addr = paddr;
576 gp->rblk = rblk;
577
Javier González8e536242016-11-28 22:39:10 +0100578 rev = &rrpc->rev_trans_map[gp->addr];
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100579 rev->addr = laddr;
580 spin_unlock(&rrpc->rev_lock);
581
582 return gp;
583}
584
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100585static u64 rrpc_alloc_addr(struct rrpc *rrpc, struct rrpc_block *rblk)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100586{
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100587 u64 addr = ADDR_EMPTY;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100588
589 spin_lock(&rblk->lock);
590 if (block_is_full(rrpc, rblk))
591 goto out;
592
Javier González8e536242016-11-28 22:39:10 +0100593 addr = rblk->next_page;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100594
595 rblk->next_page++;
596out:
597 spin_unlock(&rblk->lock);
598 return addr;
599}
600
Matias Bjørling855cdd22016-07-07 09:54:20 +0200601/* Map logical address to a physical page. The mapping implements a round robin
602 * approach and allocates a page from the next lun available.
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100603 *
Matias Bjørling855cdd22016-07-07 09:54:20 +0200604 * Returns rrpc_addr with the physical address and block. Returns NULL if no
605 * blocks in the next rlun are available.
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100606 */
Javier González8e536242016-11-28 22:39:10 +0100607static struct ppa_addr rrpc_map_page(struct rrpc *rrpc, sector_t laddr,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100608 int is_gc)
609{
Javier González8e536242016-11-28 22:39:10 +0100610 struct nvm_tgt_dev *tgt_dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100611 struct rrpc_lun *rlun;
Matias Bjørling855cdd22016-07-07 09:54:20 +0200612 struct rrpc_block *rblk, **cur_rblk;
Javier González8e536242016-11-28 22:39:10 +0100613 struct rrpc_addr *p;
614 struct ppa_addr ppa;
Matias Bjørlingb7ceb7d2015-11-02 17:12:27 +0100615 u64 paddr;
Matias Bjørling855cdd22016-07-07 09:54:20 +0200616 int gc_force = 0;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100617
Javier González8e536242016-11-28 22:39:10 +0100618 ppa.ppa = ADDR_EMPTY;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100619 rlun = rrpc_get_lun_rr(rrpc, is_gc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100620
Javier González2a02e622016-11-28 22:39:09 +0100621 if (!is_gc && rlun->nr_free_blocks < rrpc->nr_luns * 4)
Javier González8e536242016-11-28 22:39:10 +0100622 return ppa;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100623
Matias Bjørling855cdd22016-07-07 09:54:20 +0200624 /*
625 * page allocation steps:
626 * 1. Try to allocate new page from current rblk
627 * 2a. If succeed, proceed to map it in and return
628 * 2b. If fail, first try to allocate a new block from media manger,
629 * and then retry step 1. Retry until the normal block pool is
630 * exhausted.
631 * 3. If exhausted, and garbage collector is requesting the block,
632 * go to the reserved block and retry step 1.
633 * In the case that this fails as well, or it is not GC
634 * requesting, report not able to retrieve a block and let the
635 * caller handle further processing.
636 */
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100637
Matias Bjørling855cdd22016-07-07 09:54:20 +0200638 spin_lock(&rlun->lock);
639 cur_rblk = &rlun->cur;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100640 rblk = rlun->cur;
641retry:
642 paddr = rrpc_alloc_addr(rrpc, rblk);
643
Matias Bjørling855cdd22016-07-07 09:54:20 +0200644 if (paddr != ADDR_EMPTY)
645 goto done;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100646
Matias Bjørling855cdd22016-07-07 09:54:20 +0200647 if (!list_empty(&rlun->wblk_list)) {
648new_blk:
649 rblk = list_first_entry(&rlun->wblk_list, struct rrpc_block,
650 prio);
651 rrpc_set_lun_cur(rlun, rblk, cur_rblk);
652 list_del(&rblk->prio);
653 goto retry;
654 }
655 spin_unlock(&rlun->lock);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100656
Matias Bjørling855cdd22016-07-07 09:54:20 +0200657 rblk = rrpc_get_blk(rrpc, rlun, gc_force);
658 if (rblk) {
659 spin_lock(&rlun->lock);
660 list_add_tail(&rblk->prio, &rlun->wblk_list);
661 /*
662 * another thread might already have added a new block,
663 * Therefore, make sure that one is used, instead of the
664 * one just added.
665 */
666 goto new_blk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100667 }
668
Matias Bjørling855cdd22016-07-07 09:54:20 +0200669 if (unlikely(is_gc) && !gc_force) {
670 /* retry from emergency gc block */
671 cur_rblk = &rlun->gc_cur;
672 rblk = rlun->gc_cur;
673 gc_force = 1;
674 spin_lock(&rlun->lock);
675 goto retry;
676 }
677
678 pr_err("rrpc: failed to allocate new block\n");
Javier González8e536242016-11-28 22:39:10 +0100679 return ppa;
Matias Bjørling855cdd22016-07-07 09:54:20 +0200680done:
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100681 spin_unlock(&rlun->lock);
Javier González8e536242016-11-28 22:39:10 +0100682 p = rrpc_update_map(rrpc, laddr, rblk, paddr);
683 if (!p)
684 return ppa;
685
686 /* return global address */
687 return rrpc_ppa_to_gaddr(tgt_dev, p);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100688}
689
690static void rrpc_run_gc(struct rrpc *rrpc, struct rrpc_block *rblk)
691{
692 struct rrpc_block_gc *gcb;
693
694 gcb = mempool_alloc(rrpc->gcb_pool, GFP_ATOMIC);
695 if (!gcb) {
696 pr_err("rrpc: unable to queue block for gc.");
697 return;
698 }
699
700 gcb->rrpc = rrpc;
701 gcb->rblk = rblk;
702
703 INIT_WORK(&gcb->ws_gc, rrpc_gc_queue);
704 queue_work(rrpc->kgc_wq, &gcb->ws_gc);
705}
706
Javier González2a02e622016-11-28 22:39:09 +0100707static struct rrpc_lun *rrpc_ppa_to_lun(struct rrpc *rrpc, struct ppa_addr p)
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100708{
Javier González2a02e622016-11-28 22:39:09 +0100709 struct rrpc_lun *rlun = NULL;
710 int i;
711
712 for (i = 0; i < rrpc->nr_luns; i++) {
Javier González8e536242016-11-28 22:39:10 +0100713 if (rrpc->luns[i].bppa.g.ch == p.g.ch &&
714 rrpc->luns[i].bppa.g.lun == p.g.lun) {
Javier González2a02e622016-11-28 22:39:09 +0100715 rlun = &rrpc->luns[i];
716 break;
717 }
718 }
719
720 return rlun;
721}
722
723static void __rrpc_mark_bad_block(struct rrpc *rrpc, struct ppa_addr ppa)
724{
725 struct nvm_tgt_dev *dev = rrpc->dev;
726 struct rrpc_lun *rlun;
727 struct rrpc_block *rblk;
728
729 rlun = rrpc_ppa_to_lun(rrpc, ppa);
730 rblk = &rlun->blocks[ppa.g.blk];
731 rblk->state = NVM_BLK_ST_BAD;
732
Javier González333ba052016-11-28 22:39:14 +0100733 nvm_set_tgt_bb_tbl(dev, &ppa, 1, NVM_BLK_T_GRWN_BAD);
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100734}
735
736static void rrpc_mark_bad_block(struct rrpc *rrpc, struct nvm_rq *rqd)
737{
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100738 void *comp_bits = &rqd->ppa_status;
739 struct ppa_addr ppa, prev_ppa;
740 int nr_ppas = rqd->nr_ppas;
741 int bit;
742
743 if (rqd->nr_ppas == 1)
Javier González2a02e622016-11-28 22:39:09 +0100744 __rrpc_mark_bad_block(rrpc, rqd->ppa_addr);
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100745
746 ppa_set_empty(&prev_ppa);
747 bit = -1;
748 while ((bit = find_next_bit(comp_bits, nr_ppas, bit + 1)) < nr_ppas) {
749 ppa = rqd->ppa_list[bit];
750 if (ppa_cmp_blk(ppa, prev_ppa))
751 continue;
752
Javier González2a02e622016-11-28 22:39:09 +0100753 __rrpc_mark_bad_block(rrpc, ppa);
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100754 }
755}
756
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100757static void rrpc_end_io_write(struct rrpc *rrpc, struct rrpc_rq *rrqd,
758 sector_t laddr, uint8_t npages)
759{
Javier González8e79b5c2016-11-28 22:39:06 +0100760 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100761 struct rrpc_addr *p;
762 struct rrpc_block *rblk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100763 int cmnt_size, i;
764
765 for (i = 0; i < npages; i++) {
766 p = &rrpc->trans_map[laddr + i];
767 rblk = p->rblk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100768
769 cmnt_size = atomic_inc_return(&rblk->data_cmnt_size);
Javier González8e79b5c2016-11-28 22:39:06 +0100770 if (unlikely(cmnt_size == dev->geo.sec_per_blk))
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100771 rrpc_run_gc(rrpc, rblk);
772 }
773}
774
Matias Bjørling72d256e2016-01-12 07:49:29 +0100775static void rrpc_end_io(struct nvm_rq *rqd)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100776{
Matias Bjørling06894ef2017-01-31 13:17:17 +0100777 struct rrpc *rrpc = rqd->private;
Javier González8e79b5c2016-11-28 22:39:06 +0100778 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100779 struct rrpc_rq *rrqd = nvm_rq_to_pdu(rqd);
Javier González6d5be952016-05-06 20:03:20 +0200780 uint8_t npages = rqd->nr_ppas;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100781 sector_t laddr = rrpc_get_laddr(rqd->bio) - npages;
782
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100783 if (bio_data_dir(rqd->bio) == WRITE) {
784 if (rqd->error == NVM_RSP_ERR_FAILWRITE)
785 rrpc_mark_bad_block(rrpc, rqd);
786
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100787 rrpc_end_io_write(rrpc, rrqd, laddr, npages);
Javier Gonzáleza24ba462016-11-28 22:38:56 +0100788 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100789
Wenwei Tao3cd485b12016-01-12 07:49:15 +0100790 bio_put(rqd->bio);
791
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100792 if (rrqd->flags & NVM_IOTYPE_GC)
Matias Bjørling912761622016-01-12 07:49:21 +0100793 return;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100794
795 rrpc_unlock_rq(rrpc, rqd);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100796
797 if (npages > 1)
Javier González8e79b5c2016-11-28 22:39:06 +0100798 nvm_dev_dma_free(dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100799
800 mempool_free(rqd, rrpc->rq_pool);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100801}
802
803static int rrpc_read_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
804 struct nvm_rq *rqd, unsigned long flags, int npages)
805{
Javier González8e79b5c2016-11-28 22:39:06 +0100806 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100807 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
808 struct rrpc_addr *gp;
809 sector_t laddr = rrpc_get_laddr(bio);
810 int is_gc = flags & NVM_IOTYPE_GC;
811 int i;
812
813 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
Javier González8e79b5c2016-11-28 22:39:06 +0100814 nvm_dev_dma_free(dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100815 return NVM_IO_REQUEUE;
816 }
817
818 for (i = 0; i < npages; i++) {
819 /* We assume that mapping occurs at 4KB granularity */
Javier Gonzálezd788c592017-04-15 20:55:48 +0200820 BUG_ON(!(laddr + i < rrpc->nr_sects));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100821 gp = &rrpc->trans_map[laddr + i];
822
823 if (gp->rblk) {
Javier González8e536242016-11-28 22:39:10 +0100824 rqd->ppa_list[i] = rrpc_ppa_to_gaddr(dev, gp);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100825 } else {
826 BUG_ON(is_gc);
827 rrpc_unlock_laddr(rrpc, r);
Javier González8e79b5c2016-11-28 22:39:06 +0100828 nvm_dev_dma_free(dev->parent, rqd->ppa_list,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100829 rqd->dma_ppa_list);
830 return NVM_IO_DONE;
831 }
832 }
833
834 rqd->opcode = NVM_OP_HBREAD;
835
836 return NVM_IO_OK;
837}
838
839static int rrpc_read_rq(struct rrpc *rrpc, struct bio *bio, struct nvm_rq *rqd,
840 unsigned long flags)
841{
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100842 int is_gc = flags & NVM_IOTYPE_GC;
843 sector_t laddr = rrpc_get_laddr(bio);
844 struct rrpc_addr *gp;
845
846 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
847 return NVM_IO_REQUEUE;
848
Javier Gonzálezd788c592017-04-15 20:55:48 +0200849 BUG_ON(!(laddr < rrpc->nr_sects));
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100850 gp = &rrpc->trans_map[laddr];
851
852 if (gp->rblk) {
Javier González8e536242016-11-28 22:39:10 +0100853 rqd->ppa_addr = rrpc_ppa_to_gaddr(rrpc->dev, gp);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100854 } else {
855 BUG_ON(is_gc);
856 rrpc_unlock_rq(rrpc, rqd);
857 return NVM_IO_DONE;
858 }
859
860 rqd->opcode = NVM_OP_HBREAD;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100861
862 return NVM_IO_OK;
863}
864
865static int rrpc_write_ppalist_rq(struct rrpc *rrpc, struct bio *bio,
866 struct nvm_rq *rqd, unsigned long flags, int npages)
867{
Javier González8e79b5c2016-11-28 22:39:06 +0100868 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100869 struct rrpc_inflight_rq *r = rrpc_get_inflight_rq(rqd);
Javier González8e536242016-11-28 22:39:10 +0100870 struct ppa_addr p;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100871 sector_t laddr = rrpc_get_laddr(bio);
872 int is_gc = flags & NVM_IOTYPE_GC;
873 int i;
874
875 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd)) {
Javier González8e79b5c2016-11-28 22:39:06 +0100876 nvm_dev_dma_free(dev->parent, rqd->ppa_list, rqd->dma_ppa_list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100877 return NVM_IO_REQUEUE;
878 }
879
880 for (i = 0; i < npages; i++) {
881 /* We assume that mapping occurs at 4KB granularity */
882 p = rrpc_map_page(rrpc, laddr + i, is_gc);
Javier González8e536242016-11-28 22:39:10 +0100883 if (p.ppa == ADDR_EMPTY) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100884 BUG_ON(is_gc);
885 rrpc_unlock_laddr(rrpc, r);
Javier González8e79b5c2016-11-28 22:39:06 +0100886 nvm_dev_dma_free(dev->parent, rqd->ppa_list,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100887 rqd->dma_ppa_list);
888 rrpc_gc_kick(rrpc);
889 return NVM_IO_REQUEUE;
890 }
891
Javier González8e536242016-11-28 22:39:10 +0100892 rqd->ppa_list[i] = p;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100893 }
894
895 rqd->opcode = NVM_OP_HBWRITE;
896
897 return NVM_IO_OK;
898}
899
900static int rrpc_write_rq(struct rrpc *rrpc, struct bio *bio,
901 struct nvm_rq *rqd, unsigned long flags)
902{
Javier González8e536242016-11-28 22:39:10 +0100903 struct ppa_addr p;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100904 int is_gc = flags & NVM_IOTYPE_GC;
905 sector_t laddr = rrpc_get_laddr(bio);
906
907 if (!is_gc && rrpc_lock_rq(rrpc, bio, rqd))
908 return NVM_IO_REQUEUE;
909
910 p = rrpc_map_page(rrpc, laddr, is_gc);
Javier González8e536242016-11-28 22:39:10 +0100911 if (p.ppa == ADDR_EMPTY) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100912 BUG_ON(is_gc);
913 rrpc_unlock_rq(rrpc, rqd);
914 rrpc_gc_kick(rrpc);
915 return NVM_IO_REQUEUE;
916 }
917
Javier González8e536242016-11-28 22:39:10 +0100918 rqd->ppa_addr = p;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100919 rqd->opcode = NVM_OP_HBWRITE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100920
921 return NVM_IO_OK;
922}
923
924static int rrpc_setup_rq(struct rrpc *rrpc, struct bio *bio,
925 struct nvm_rq *rqd, unsigned long flags, uint8_t npages)
926{
Javier González8e79b5c2016-11-28 22:39:06 +0100927 struct nvm_tgt_dev *dev = rrpc->dev;
928
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100929 if (npages > 1) {
Javier González8e79b5c2016-11-28 22:39:06 +0100930 rqd->ppa_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100931 &rqd->dma_ppa_list);
932 if (!rqd->ppa_list) {
933 pr_err("rrpc: not able to allocate ppa list\n");
934 return NVM_IO_ERR;
935 }
936
Christoph Hellwig70246282016-07-19 11:28:41 +0200937 if (bio_op(bio) == REQ_OP_WRITE)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100938 return rrpc_write_ppalist_rq(rrpc, bio, rqd, flags,
939 npages);
940
941 return rrpc_read_ppalist_rq(rrpc, bio, rqd, flags, npages);
942 }
943
Christoph Hellwig70246282016-07-19 11:28:41 +0200944 if (bio_op(bio) == REQ_OP_WRITE)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100945 return rrpc_write_rq(rrpc, bio, rqd, flags);
946
947 return rrpc_read_rq(rrpc, bio, rqd, flags);
948}
949
950static int rrpc_submit_io(struct rrpc *rrpc, struct bio *bio,
951 struct nvm_rq *rqd, unsigned long flags)
952{
Javier González8e79b5c2016-11-28 22:39:06 +0100953 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100954 struct rrpc_rq *rrq = nvm_rq_to_pdu(rqd);
955 uint8_t nr_pages = rrpc_get_pages(bio);
956 int bio_size = bio_sectors(bio) << 9;
Javier González8e79b5c2016-11-28 22:39:06 +0100957 int err;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100958
Javier González8e79b5c2016-11-28 22:39:06 +0100959 if (bio_size < dev->geo.sec_size)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100960 return NVM_IO_ERR;
Javier González8e79b5c2016-11-28 22:39:06 +0100961 else if (bio_size > dev->geo.max_rq_size)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100962 return NVM_IO_ERR;
963
964 err = rrpc_setup_rq(rrpc, bio, rqd, flags, nr_pages);
965 if (err)
966 return err;
967
968 bio_get(bio);
969 rqd->bio = bio;
Matias Bjørling06894ef2017-01-31 13:17:17 +0100970 rqd->private = rrpc;
Javier González6d5be952016-05-06 20:03:20 +0200971 rqd->nr_ppas = nr_pages;
Matias Bjørling06894ef2017-01-31 13:17:17 +0100972 rqd->end_io = rrpc_end_io;
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100973 rrq->flags = flags;
974
Javier González8e536242016-11-28 22:39:10 +0100975 err = nvm_submit_io(dev, rqd);
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100976 if (err) {
977 pr_err("rrpc: I/O submission failed: %d\n", err);
Wenwei Tao3cd485b12016-01-12 07:49:15 +0100978 bio_put(bio);
Wenwei Taoc27278b2016-01-12 07:49:18 +0100979 if (!(flags & NVM_IOTYPE_GC)) {
980 rrpc_unlock_rq(rrpc, rqd);
Javier González6d5be952016-05-06 20:03:20 +0200981 if (rqd->nr_ppas > 1)
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +0100982 nvm_dev_dma_free(dev->parent, rqd->ppa_list,
983 rqd->dma_ppa_list);
Wenwei Taoc27278b2016-01-12 07:49:18 +0100984 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100985 return NVM_IO_ERR;
986 }
987
988 return NVM_IO_OK;
989}
990
Jens Axboedece1632015-11-05 10:41:16 -0700991static blk_qc_t rrpc_make_rq(struct request_queue *q, struct bio *bio)
Matias Bjørlingae1519e2015-10-28 19:54:57 +0100992{
993 struct rrpc *rrpc = q->queuedata;
994 struct nvm_rq *rqd;
995 int err;
996
NeilBrownaf67c312017-06-18 14:38:57 +1000997 blk_queue_split(q, &bio);
Javier Gonzálezf0b01b62016-11-28 22:38:58 +0100998
Mike Christie95fe6c12016-06-05 14:31:48 -0500999 if (bio_op(bio) == REQ_OP_DISCARD) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001000 rrpc_discard(rrpc, bio);
Jens Axboedece1632015-11-05 10:41:16 -07001001 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001002 }
1003
1004 rqd = mempool_alloc(rrpc->rq_pool, GFP_KERNEL);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001005 memset(rqd, 0, sizeof(struct nvm_rq));
1006
1007 err = rrpc_submit_io(rrpc, bio, rqd, NVM_IOTYPE_NONE);
1008 switch (err) {
1009 case NVM_IO_OK:
Jens Axboedece1632015-11-05 10:41:16 -07001010 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001011 case NVM_IO_ERR:
1012 bio_io_error(bio);
1013 break;
1014 case NVM_IO_DONE:
1015 bio_endio(bio);
1016 break;
1017 case NVM_IO_REQUEUE:
1018 spin_lock(&rrpc->bio_lock);
1019 bio_list_add(&rrpc->requeue_bios, bio);
1020 spin_unlock(&rrpc->bio_lock);
1021 queue_work(rrpc->kgc_wq, &rrpc->ws_requeue);
1022 break;
1023 }
1024
1025 mempool_free(rqd, rrpc->rq_pool);
Jens Axboedece1632015-11-05 10:41:16 -07001026 return BLK_QC_T_NONE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001027}
1028
1029static void rrpc_requeue(struct work_struct *work)
1030{
1031 struct rrpc *rrpc = container_of(work, struct rrpc, ws_requeue);
1032 struct bio_list bios;
1033 struct bio *bio;
1034
1035 bio_list_init(&bios);
1036
1037 spin_lock(&rrpc->bio_lock);
1038 bio_list_merge(&bios, &rrpc->requeue_bios);
1039 bio_list_init(&rrpc->requeue_bios);
1040 spin_unlock(&rrpc->bio_lock);
1041
1042 while ((bio = bio_list_pop(&bios)))
1043 rrpc_make_rq(rrpc->disk->queue, bio);
1044}
1045
1046static void rrpc_gc_free(struct rrpc *rrpc)
1047{
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001048 if (rrpc->krqd_wq)
1049 destroy_workqueue(rrpc->krqd_wq);
1050
1051 if (rrpc->kgc_wq)
1052 destroy_workqueue(rrpc->kgc_wq);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001053}
1054
1055static int rrpc_gc_init(struct rrpc *rrpc)
1056{
1057 rrpc->krqd_wq = alloc_workqueue("rrpc-lun", WQ_MEM_RECLAIM|WQ_UNBOUND,
1058 rrpc->nr_luns);
1059 if (!rrpc->krqd_wq)
1060 return -ENOMEM;
1061
1062 rrpc->kgc_wq = alloc_workqueue("rrpc-bg", WQ_MEM_RECLAIM, 1);
1063 if (!rrpc->kgc_wq)
1064 return -ENOMEM;
1065
1066 setup_timer(&rrpc->gc_timer, rrpc_gc_timer, (unsigned long)rrpc);
1067
1068 return 0;
1069}
1070
1071static void rrpc_map_free(struct rrpc *rrpc)
1072{
1073 vfree(rrpc->rev_trans_map);
1074 vfree(rrpc->trans_map);
1075}
1076
1077static int rrpc_l2p_update(u64 slba, u32 nlb, __le64 *entries, void *private)
1078{
1079 struct rrpc *rrpc = (struct rrpc *)private;
Javier González8e79b5c2016-11-28 22:39:06 +01001080 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001081 struct rrpc_addr *addr = rrpc->trans_map + slba;
1082 struct rrpc_rev_addr *raddr = rrpc->rev_trans_map;
Javier González2a02e622016-11-28 22:39:09 +01001083 struct rrpc_lun *rlun;
1084 struct rrpc_block *rblk;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001085 u64 i;
1086
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001087 for (i = 0; i < nlb; i++) {
Javier González2a02e622016-11-28 22:39:09 +01001088 struct ppa_addr gaddr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001089 u64 pba = le64_to_cpu(entries[i]);
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001090 unsigned int mod;
Javier González2a02e622016-11-28 22:39:09 +01001091
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001092 /* LNVM treats address-spaces as silos, LBA and PBA are
1093 * equally large and zero-indexed.
1094 */
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001095 if (unlikely(pba >= dev->total_secs && pba != U64_MAX)) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001096 pr_err("nvm: L2P data entry is out of bounds!\n");
Javier González2a02e622016-11-28 22:39:09 +01001097 pr_err("nvm: Maybe loaded an old target L2P\n");
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001098 return -EINVAL;
1099 }
1100
1101 /* Address zero is a special one. The first page on a disk is
1102 * protected. As it often holds internal device boot
1103 * information.
1104 */
1105 if (!pba)
1106 continue;
1107
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001108 div_u64_rem(pba, rrpc->nr_sects, &mod);
1109
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +01001110 gaddr = rrpc_recov_addr(dev, pba);
Javier González2a02e622016-11-28 22:39:09 +01001111 rlun = rrpc_ppa_to_lun(rrpc, gaddr);
1112 if (!rlun) {
1113 pr_err("rrpc: l2p corruption on lba %llu\n",
1114 slba + i);
1115 return -EINVAL;
1116 }
1117
1118 rblk = &rlun->blocks[gaddr.g.blk];
1119 if (!rblk->state) {
1120 /* at this point, we don't know anything about the
1121 * block. It's up to the FTL on top to re-etablish the
1122 * block state. The block is assumed to be open.
1123 */
1124 list_move_tail(&rblk->list, &rlun->used_list);
1125 rblk->state = NVM_BLK_ST_TGT;
1126 rlun->nr_free_blocks--;
1127 }
Javier González8e536242016-11-28 22:39:10 +01001128
1129 addr[i].addr = pba;
1130 addr[i].rblk = rblk;
1131 raddr[mod].addr = slba + i;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001132 }
1133
1134 return 0;
1135}
1136
1137static int rrpc_map_init(struct rrpc *rrpc)
1138{
Javier González8e79b5c2016-11-28 22:39:06 +01001139 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001140 sector_t i;
1141 int ret;
1142
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001143 rrpc->trans_map = vzalloc(sizeof(struct rrpc_addr) * rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001144 if (!rrpc->trans_map)
1145 return -ENOMEM;
1146
1147 rrpc->rev_trans_map = vmalloc(sizeof(struct rrpc_rev_addr)
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001148 * rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001149 if (!rrpc->rev_trans_map)
1150 return -ENOMEM;
1151
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001152 for (i = 0; i < rrpc->nr_sects; i++) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001153 struct rrpc_addr *p = &rrpc->trans_map[i];
1154 struct rrpc_rev_addr *r = &rrpc->rev_trans_map[i];
1155
1156 p->addr = ADDR_EMPTY;
1157 r->addr = ADDR_EMPTY;
1158 }
1159
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001160 /* Bring up the mapping table from device */
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +01001161 ret = nvm_get_l2p_tbl(dev, rrpc->soffset, rrpc->nr_sects,
1162 rrpc_l2p_update, rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001163 if (ret) {
1164 pr_err("nvm: rrpc: could not read L2P table.\n");
1165 return -EINVAL;
1166 }
1167
1168 return 0;
1169}
1170
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001171/* Minimum pages needed within a lun */
1172#define PAGE_POOL_SIZE 16
1173#define ADDR_POOL_SIZE 64
1174
1175static int rrpc_core_init(struct rrpc *rrpc)
1176{
1177 down_write(&rrpc_lock);
1178 if (!rrpc_gcb_cache) {
1179 rrpc_gcb_cache = kmem_cache_create("rrpc_gcb",
1180 sizeof(struct rrpc_block_gc), 0, 0, NULL);
1181 if (!rrpc_gcb_cache) {
1182 up_write(&rrpc_lock);
1183 return -ENOMEM;
1184 }
1185
1186 rrpc_rq_cache = kmem_cache_create("rrpc_rq",
1187 sizeof(struct nvm_rq) + sizeof(struct rrpc_rq),
1188 0, 0, NULL);
1189 if (!rrpc_rq_cache) {
1190 kmem_cache_destroy(rrpc_gcb_cache);
1191 up_write(&rrpc_lock);
1192 return -ENOMEM;
1193 }
1194 }
1195 up_write(&rrpc_lock);
1196
1197 rrpc->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0);
1198 if (!rrpc->page_pool)
1199 return -ENOMEM;
1200
Javier González8e79b5c2016-11-28 22:39:06 +01001201 rrpc->gcb_pool = mempool_create_slab_pool(rrpc->dev->geo.nr_luns,
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001202 rrpc_gcb_cache);
1203 if (!rrpc->gcb_pool)
1204 return -ENOMEM;
1205
1206 rrpc->rq_pool = mempool_create_slab_pool(64, rrpc_rq_cache);
1207 if (!rrpc->rq_pool)
1208 return -ENOMEM;
1209
1210 spin_lock_init(&rrpc->inflights.lock);
1211 INIT_LIST_HEAD(&rrpc->inflights.reqs);
1212
1213 return 0;
1214}
1215
1216static void rrpc_core_free(struct rrpc *rrpc)
1217{
1218 mempool_destroy(rrpc->page_pool);
1219 mempool_destroy(rrpc->gcb_pool);
1220 mempool_destroy(rrpc->rq_pool);
1221}
1222
1223static void rrpc_luns_free(struct rrpc *rrpc)
1224{
Wenwei Taoda1e2842016-03-03 15:06:38 +01001225 struct rrpc_lun *rlun;
1226 int i;
1227
1228 if (!rrpc->luns)
1229 return;
1230
1231 for (i = 0; i < rrpc->nr_luns; i++) {
1232 rlun = &rrpc->luns[i];
Wenwei Taoda1e2842016-03-03 15:06:38 +01001233 vfree(rlun->blocks);
1234 }
1235
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001236 kfree(rrpc->luns);
1237}
1238
Javier González2a02e622016-11-28 22:39:09 +01001239static int rrpc_bb_discovery(struct nvm_tgt_dev *dev, struct rrpc_lun *rlun)
1240{
1241 struct nvm_geo *geo = &dev->geo;
1242 struct rrpc_block *rblk;
1243 struct ppa_addr ppa;
1244 u8 *blks;
1245 int nr_blks;
1246 int i;
1247 int ret;
1248
Javier González959e9112016-11-28 22:39:11 +01001249 if (!dev->parent->ops->get_bb_tbl)
1250 return 0;
1251
Javier González2a02e622016-11-28 22:39:09 +01001252 nr_blks = geo->blks_per_lun * geo->plane_mode;
1253 blks = kmalloc(nr_blks, GFP_KERNEL);
1254 if (!blks)
1255 return -ENOMEM;
1256
1257 ppa.ppa = 0;
Javier González8e536242016-11-28 22:39:10 +01001258 ppa.g.ch = rlun->bppa.g.ch;
1259 ppa.g.lun = rlun->bppa.g.lun;
Javier González2a02e622016-11-28 22:39:09 +01001260
Javier González333ba052016-11-28 22:39:14 +01001261 ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
Javier González2a02e622016-11-28 22:39:09 +01001262 if (ret) {
1263 pr_err("rrpc: could not get BB table\n");
1264 goto out;
1265 }
1266
1267 nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
Christophe JAILLET654a01b2017-04-15 20:55:35 +02001268 if (nr_blks < 0) {
1269 ret = nr_blks;
1270 goto out;
1271 }
Javier González2a02e622016-11-28 22:39:09 +01001272
Javier González2a02e622016-11-28 22:39:09 +01001273 for (i = 0; i < nr_blks; i++) {
1274 if (blks[i] == NVM_BLK_T_FREE)
1275 continue;
1276
1277 rblk = &rlun->blocks[i];
1278 list_move_tail(&rblk->list, &rlun->bb_list);
1279 rblk->state = NVM_BLK_ST_BAD;
1280 rlun->nr_free_blocks--;
1281 }
1282
1283out:
1284 kfree(blks);
1285 return ret;
1286}
1287
Javier González8e536242016-11-28 22:39:10 +01001288static void rrpc_set_lun_ppa(struct rrpc_lun *rlun, struct ppa_addr ppa)
1289{
1290 rlun->bppa.ppa = 0;
1291 rlun->bppa.g.ch = ppa.g.ch;
1292 rlun->bppa.g.lun = ppa.g.lun;
1293}
1294
1295static int rrpc_luns_init(struct rrpc *rrpc, struct ppa_addr *luns)
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001296{
Javier González8e79b5c2016-11-28 22:39:06 +01001297 struct nvm_tgt_dev *dev = rrpc->dev;
1298 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001299 struct rrpc_lun *rlun;
Wenwei Taoda1e2842016-03-03 15:06:38 +01001300 int i, j, ret = -EINVAL;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001301
Javier González8e79b5c2016-11-28 22:39:06 +01001302 if (geo->sec_per_blk > MAX_INVALID_PAGES_STORAGE * BITS_PER_LONG) {
Wenwei Tao4b79beb2016-01-12 07:49:27 +01001303 pr_err("rrpc: number of pages per block too high.");
1304 return -EINVAL;
1305 }
1306
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001307 spin_lock_init(&rrpc->rev_lock);
1308
1309 rrpc->luns = kcalloc(rrpc->nr_luns, sizeof(struct rrpc_lun),
1310 GFP_KERNEL);
1311 if (!rrpc->luns)
1312 return -ENOMEM;
1313
1314 /* 1:1 mapping */
Javier González8e536242016-11-28 22:39:10 +01001315 for (i = 0; i < rrpc->nr_luns; i++) {
1316 rlun = &rrpc->luns[i];
1317 rlun->id = i;
1318 rrpc_set_lun_ppa(rlun, luns[i]);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001319 rlun->blocks = vzalloc(sizeof(struct rrpc_block) *
Javier González8e79b5c2016-11-28 22:39:06 +01001320 geo->blks_per_lun);
Wenwei Taoda1e2842016-03-03 15:06:38 +01001321 if (!rlun->blocks) {
1322 ret = -ENOMEM;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001323 goto err;
Wenwei Taoda1e2842016-03-03 15:06:38 +01001324 }
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001325
Javier González2a02e622016-11-28 22:39:09 +01001326 INIT_LIST_HEAD(&rlun->free_list);
1327 INIT_LIST_HEAD(&rlun->used_list);
1328 INIT_LIST_HEAD(&rlun->bb_list);
1329
Javier González8e79b5c2016-11-28 22:39:06 +01001330 for (j = 0; j < geo->blks_per_lun; j++) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001331 struct rrpc_block *rblk = &rlun->blocks[j];
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001332
Javier González2a02e622016-11-28 22:39:09 +01001333 rblk->id = j;
Javier Gonzálezd7a64d22016-01-12 07:49:31 +01001334 rblk->rlun = rlun;
Javier González2a02e622016-11-28 22:39:09 +01001335 rblk->state = NVM_BLK_T_FREE;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001336 INIT_LIST_HEAD(&rblk->prio);
Javier González2a02e622016-11-28 22:39:09 +01001337 INIT_LIST_HEAD(&rblk->list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001338 spin_lock_init(&rblk->lock);
Javier González2a02e622016-11-28 22:39:09 +01001339
1340 list_add_tail(&rblk->list, &rlun->free_list);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001341 }
Wenwei Taoda1e2842016-03-03 15:06:38 +01001342
Javier González959e9112016-11-28 22:39:11 +01001343 rlun->rrpc = rrpc;
1344 rlun->nr_free_blocks = geo->blks_per_lun;
Javier González8e79b5c2016-11-28 22:39:06 +01001345 rlun->reserved_blocks = 2; /* for GC only */
1346
Wenwei Taoda1e2842016-03-03 15:06:38 +01001347 INIT_LIST_HEAD(&rlun->prio_list);
Matias Bjørling855cdd22016-07-07 09:54:20 +02001348 INIT_LIST_HEAD(&rlun->wblk_list);
Wenwei Taoda1e2842016-03-03 15:06:38 +01001349
1350 INIT_WORK(&rlun->ws_gc, rrpc_lun_gc);
1351 spin_lock_init(&rlun->lock);
Javier González959e9112016-11-28 22:39:11 +01001352
1353 if (rrpc_bb_discovery(dev, rlun))
1354 goto err;
1355
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001356 }
1357
1358 return 0;
1359err:
Wenwei Taoda1e2842016-03-03 15:06:38 +01001360 return ret;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001361}
1362
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001363/* returns 0 on success and stores the beginning address in *begin */
1364static int rrpc_area_init(struct rrpc *rrpc, sector_t *begin)
1365{
Javier González8e79b5c2016-11-28 22:39:06 +01001366 struct nvm_tgt_dev *dev = rrpc->dev;
Javier González8e79b5c2016-11-28 22:39:06 +01001367 sector_t size = rrpc->nr_sects * dev->geo.sec_size;
Wenwei Tao909049a72016-05-06 20:03:01 +02001368 int ret;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001369
1370 size >>= 9;
1371
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +01001372 ret = nvm_get_area(dev, begin, size);
Wenwei Tao909049a72016-05-06 20:03:01 +02001373 if (!ret)
Javier González8e79b5c2016-11-28 22:39:06 +01001374 *begin >>= (ilog2(dev->geo.sec_size) - 9);
Wenwei Tao909049a72016-05-06 20:03:01 +02001375
1376 return ret;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001377}
1378
1379static void rrpc_area_free(struct rrpc *rrpc)
1380{
Javier González8e79b5c2016-11-28 22:39:06 +01001381 struct nvm_tgt_dev *dev = rrpc->dev;
Javier González8e79b5c2016-11-28 22:39:06 +01001382 sector_t begin = rrpc->soffset << (ilog2(dev->geo.sec_size) - 9);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001383
Javier Gonzálezda2d7cb2016-11-28 22:39:13 +01001384 nvm_put_area(dev, begin);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001385}
1386
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001387static void rrpc_free(struct rrpc *rrpc)
1388{
1389 rrpc_gc_free(rrpc);
1390 rrpc_map_free(rrpc);
1391 rrpc_core_free(rrpc);
1392 rrpc_luns_free(rrpc);
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001393 rrpc_area_free(rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001394
1395 kfree(rrpc);
1396}
1397
1398static void rrpc_exit(void *private)
1399{
1400 struct rrpc *rrpc = private;
1401
1402 del_timer(&rrpc->gc_timer);
1403
1404 flush_workqueue(rrpc->krqd_wq);
1405 flush_workqueue(rrpc->kgc_wq);
1406
1407 rrpc_free(rrpc);
1408}
1409
1410static sector_t rrpc_capacity(void *private)
1411{
1412 struct rrpc *rrpc = private;
Javier González8e79b5c2016-11-28 22:39:06 +01001413 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001414 sector_t reserved, provisioned;
1415
1416 /* cur, gc, and two emergency blocks for each lun */
Javier González8e79b5c2016-11-28 22:39:06 +01001417 reserved = rrpc->nr_luns * dev->geo.sec_per_blk * 4;
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001418 provisioned = rrpc->nr_sects - reserved;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001419
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001420 if (reserved > rrpc->nr_sects) {
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001421 pr_err("rrpc: not enough space available to expose storage.\n");
1422 return 0;
1423 }
1424
1425 sector_div(provisioned, 10);
1426 return provisioned * 9 * NR_PHY_IN_LOG;
1427}
1428
1429/*
1430 * Looks up the logical address from reverse trans map and check if its valid by
1431 * comparing the logical to physical address with the physical address.
1432 * Returns 0 on free, otherwise 1 if in use
1433 */
1434static void rrpc_block_map_update(struct rrpc *rrpc, struct rrpc_block *rblk)
1435{
Javier González8e79b5c2016-11-28 22:39:06 +01001436 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001437 int offset;
1438 struct rrpc_addr *laddr;
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001439 u64 bpaddr, paddr, pladdr;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001440
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001441 bpaddr = block_to_rel_addr(rrpc, rblk);
Javier González8e79b5c2016-11-28 22:39:06 +01001442 for (offset = 0; offset < dev->geo.sec_per_blk; offset++) {
Javier Gonzálezafb18e02016-03-03 14:47:53 -07001443 paddr = bpaddr + offset;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001444
1445 pladdr = rrpc->rev_trans_map[paddr].addr;
1446 if (pladdr == ADDR_EMPTY)
1447 continue;
1448
1449 laddr = &rrpc->trans_map[pladdr];
1450
1451 if (paddr == laddr->addr) {
1452 laddr->rblk = rblk;
1453 } else {
1454 set_bit(offset, rblk->invalid_pages);
1455 rblk->nr_invalid_pages++;
1456 }
1457 }
1458}
1459
1460static int rrpc_blocks_init(struct rrpc *rrpc)
1461{
Javier González8e79b5c2016-11-28 22:39:06 +01001462 struct nvm_tgt_dev *dev = rrpc->dev;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001463 struct rrpc_lun *rlun;
1464 struct rrpc_block *rblk;
1465 int lun_iter, blk_iter;
1466
1467 for (lun_iter = 0; lun_iter < rrpc->nr_luns; lun_iter++) {
1468 rlun = &rrpc->luns[lun_iter];
1469
Javier González8e79b5c2016-11-28 22:39:06 +01001470 for (blk_iter = 0; blk_iter < dev->geo.blks_per_lun;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001471 blk_iter++) {
1472 rblk = &rlun->blocks[blk_iter];
1473 rrpc_block_map_update(rrpc, rblk);
1474 }
1475 }
1476
1477 return 0;
1478}
1479
1480static int rrpc_luns_configure(struct rrpc *rrpc)
1481{
1482 struct rrpc_lun *rlun;
1483 struct rrpc_block *rblk;
1484 int i;
1485
1486 for (i = 0; i < rrpc->nr_luns; i++) {
1487 rlun = &rrpc->luns[i];
1488
1489 rblk = rrpc_get_blk(rrpc, rlun, 0);
1490 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001491 goto err;
Matias Bjørling855cdd22016-07-07 09:54:20 +02001492 rrpc_set_lun_cur(rlun, rblk, &rlun->cur);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001493
1494 /* Emergency gc block */
1495 rblk = rrpc_get_blk(rrpc, rlun, 1);
1496 if (!rblk)
Wenwei Taod3d1a432015-12-06 11:25:44 +01001497 goto err;
Matias Bjørling855cdd22016-07-07 09:54:20 +02001498 rrpc_set_lun_cur(rlun, rblk, &rlun->gc_cur);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001499 }
1500
1501 return 0;
Wenwei Taod3d1a432015-12-06 11:25:44 +01001502err:
1503 rrpc_put_blks(rrpc);
1504 return -EINVAL;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001505}
1506
1507static struct nvm_tgt_type tt_rrpc;
1508
Javier González4af3f752017-04-15 20:55:45 +02001509static void *rrpc_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1510 int flags)
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001511{
1512 struct request_queue *bqueue = dev->q;
1513 struct request_queue *tqueue = tdisk->queue;
Javier González8e79b5c2016-11-28 22:39:06 +01001514 struct nvm_geo *geo = &dev->geo;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001515 struct rrpc *rrpc;
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001516 sector_t soffset;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001517 int ret;
1518
1519 if (!(dev->identity.dom & NVM_RSP_L2P)) {
1520 pr_err("nvm: rrpc: device does not support l2p (%x)\n",
1521 dev->identity.dom);
1522 return ERR_PTR(-EINVAL);
1523 }
1524
1525 rrpc = kzalloc(sizeof(struct rrpc), GFP_KERNEL);
1526 if (!rrpc)
1527 return ERR_PTR(-ENOMEM);
1528
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001529 rrpc->dev = dev;
1530 rrpc->disk = tdisk;
1531
1532 bio_list_init(&rrpc->requeue_bios);
1533 spin_lock_init(&rrpc->bio_lock);
1534 INIT_WORK(&rrpc->ws_requeue, rrpc_requeue);
1535
Javier González8e79b5c2016-11-28 22:39:06 +01001536 rrpc->nr_luns = geo->nr_luns;
1537 rrpc->nr_sects = (unsigned long long)geo->sec_per_lun * rrpc->nr_luns;
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001538
1539 /* simple round-robin strategy */
1540 atomic_set(&rrpc->next_lun, -1);
1541
Wenwei Tao4c9dacb2016-03-03 15:06:37 +01001542 ret = rrpc_area_init(rrpc, &soffset);
1543 if (ret < 0) {
1544 pr_err("nvm: rrpc: could not initialize area\n");
1545 return ERR_PTR(ret);
1546 }
1547 rrpc->soffset = soffset;
1548
Javier González8e536242016-11-28 22:39:10 +01001549 ret = rrpc_luns_init(rrpc, dev->luns);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001550 if (ret) {
1551 pr_err("nvm: rrpc: could not initialize luns\n");
1552 goto err;
1553 }
1554
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001555 ret = rrpc_core_init(rrpc);
1556 if (ret) {
1557 pr_err("nvm: rrpc: could not initialize core\n");
1558 goto err;
1559 }
1560
1561 ret = rrpc_map_init(rrpc);
1562 if (ret) {
1563 pr_err("nvm: rrpc: could not initialize maps\n");
1564 goto err;
1565 }
1566
1567 ret = rrpc_blocks_init(rrpc);
1568 if (ret) {
1569 pr_err("nvm: rrpc: could not initialize state for blocks\n");
1570 goto err;
1571 }
1572
1573 ret = rrpc_luns_configure(rrpc);
1574 if (ret) {
1575 pr_err("nvm: rrpc: not enough blocks available in LUNs.\n");
1576 goto err;
1577 }
1578
1579 ret = rrpc_gc_init(rrpc);
1580 if (ret) {
1581 pr_err("nvm: rrpc: could not initialize gc\n");
1582 goto err;
1583 }
1584
1585 /* inherit the size from the underlying device */
1586 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1587 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1588
1589 pr_info("nvm: rrpc initialized with %u luns and %llu pages.\n",
Matias Bjørling4ece44a2016-02-20 08:52:41 +01001590 rrpc->nr_luns, (unsigned long long)rrpc->nr_sects);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001591
1592 mod_timer(&rrpc->gc_timer, jiffies + msecs_to_jiffies(10));
1593
1594 return rrpc;
1595err:
1596 rrpc_free(rrpc);
1597 return ERR_PTR(ret);
1598}
1599
1600/* round robin, page-based FTL, and cost-based GC */
1601static struct nvm_tgt_type tt_rrpc = {
1602 .name = "rrpc",
1603 .version = {1, 0, 0},
1604
1605 .make_rq = rrpc_make_rq,
1606 .capacity = rrpc_capacity,
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001607
1608 .init = rrpc_init,
1609 .exit = rrpc_exit,
1610};
1611
1612static int __init rrpc_module_init(void)
1613{
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001614 return nvm_register_tgt_type(&tt_rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001615}
1616
1617static void rrpc_module_exit(void)
1618{
Simon A. F. Lund6063fe32016-05-06 20:03:02 +02001619 nvm_unregister_tgt_type(&tt_rrpc);
Matias Bjørlingae1519e2015-10-28 19:54:57 +01001620}
1621
1622module_init(rrpc_module_init);
1623module_exit(rrpc_module_exit);
1624MODULE_LICENSE("GPL v2");
1625MODULE_DESCRIPTION("Block-Device Target for Open-Channel SSDs");