Merge tag 'microblaze-4.17-rc1' of git://git.monstr.eu/linux-2.6-microblaze
[linux-block.git] / drivers / lightnvm / pblk-cache.c
CommitLineData
a4bd217b
JG
1/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * pblk-cache.c - pblk's write cache
16 */
17
18#include "pblk.h"
19
20int pblk_write_to_cache(struct pblk *pblk, struct bio *bio, unsigned long flags)
21{
998ba629 22 struct request_queue *q = pblk->dev->q;
a4bd217b
JG
23 struct pblk_w_ctx w_ctx;
24 sector_t lba = pblk_get_lba(bio);
998ba629 25 unsigned long start_time = jiffies;
a4bd217b
JG
26 unsigned int bpos, pos;
27 int nr_entries = pblk_get_secs(bio);
28 int i, ret;
29
998ba629
JG
30 generic_start_io_acct(q, WRITE, bio_sectors(bio), &pblk->disk->part0);
31
a4bd217b
JG
32 /* Update the write buffer head (mem) with the entries that we can
33 * write. The write in itself cannot fail, so there is no need to
34 * rollback from here on.
35 */
36retry:
37 ret = pblk_rb_may_write_user(&pblk->rwb, bio, nr_entries, &bpos);
588726d3
JG
38 switch (ret) {
39 case NVM_IO_REQUEUE:
a4bd217b
JG
40 io_schedule();
41 goto retry;
588726d3
JG
42 case NVM_IO_ERR:
43 pblk_pipeline_stop(pblk);
44 goto out;
a4bd217b
JG
45 }
46
47 if (unlikely(!bio_has_data(bio)))
48 goto out;
49
a4bd217b 50 pblk_ppa_set_empty(&w_ctx.ppa);
6ca2f71f
JG
51 w_ctx.flags = flags;
52 if (bio->bi_opf & REQ_PREFLUSH)
53 w_ctx.flags |= PBLK_FLUSH_ENTRY;
a4bd217b
JG
54
55 for (i = 0; i < nr_entries; i++) {
56 void *data = bio_data(bio);
57
58 w_ctx.lba = lba + i;
59
60 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + i);
61 pblk_rb_write_entry_user(&pblk->rwb, data, w_ctx, pos);
62
63 bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
64 }
65
76758390
HH
66 atomic64_add(nr_entries, &pblk->user_wa);
67
a4bd217b
JG
68#ifdef CONFIG_NVM_DEBUG
69 atomic_long_add(nr_entries, &pblk->inflight_writes);
70 atomic_long_add(nr_entries, &pblk->req_writes);
71#endif
72
588726d3
JG
73 pblk_rl_inserted(&pblk->rl, nr_entries);
74
a4bd217b 75out:
998ba629 76 generic_end_io_acct(q, WRITE, &pblk->disk->part0, start_time);
a4bd217b
JG
77 pblk_write_should_kick(pblk);
78 return ret;
79}
80
81/*
82 * On GC the incoming lbas are not necessarily sequential. Also, some of the
83 * lbas might not be valid entries, which are marked as empty by the GC thread
84 */
d340121e 85int pblk_write_gc_to_cache(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
a4bd217b
JG
86{
87 struct pblk_w_ctx w_ctx;
88 unsigned int bpos, pos;
d340121e 89 void *data = gc_rq->data;
a4bd217b
JG
90 int i, valid_entries;
91
92 /* Update the write buffer head (mem) with the entries that we can
93 * write. The write in itself cannot fail, so there is no need to
94 * rollback from here on.
95 */
96retry:
d340121e 97 if (!pblk_rb_may_write_gc(&pblk->rwb, gc_rq->secs_to_gc, &bpos)) {
a4bd217b
JG
98 io_schedule();
99 goto retry;
100 }
101
d340121e 102 w_ctx.flags = PBLK_IOTYPE_GC;
a4bd217b
JG
103 pblk_ppa_set_empty(&w_ctx.ppa);
104
d340121e
JG
105 for (i = 0, valid_entries = 0; i < gc_rq->nr_secs; i++) {
106 if (gc_rq->lba_list[i] == ADDR_EMPTY)
a4bd217b
JG
107 continue;
108
d340121e 109 w_ctx.lba = gc_rq->lba_list[i];
a4bd217b
JG
110
111 pos = pblk_rb_wrap_pos(&pblk->rwb, bpos + valid_entries);
d340121e
JG
112 pblk_rb_write_entry_gc(&pblk->rwb, data, w_ctx, gc_rq->line,
113 gc_rq->paddr_list[i], pos);
a4bd217b
JG
114
115 data += PBLK_EXPOSED_PAGE_SIZE;
116 valid_entries++;
117 }
118
d340121e 119 WARN_ONCE(gc_rq->secs_to_gc != valid_entries,
a4bd217b
JG
120 "pblk: inconsistent GC write\n");
121
76758390
HH
122 atomic64_add(valid_entries, &pblk->gc_wa);
123
a4bd217b
JG
124#ifdef CONFIG_NVM_DEBUG
125 atomic_long_add(valid_entries, &pblk->inflight_writes);
126 atomic_long_add(valid_entries, &pblk->recov_gc_writes);
127#endif
128
129 pblk_write_should_kick(pblk);
130 return NVM_IO_OK;
131}