Commit | Line | Data |
---|---|---|
02a1520d | 1 | // SPDX-License-Identifier: GPL-2.0 |
a4bd217b JG |
2 | /* |
3 | * Copyright (C) 2016 CNEX Labs | |
4 | * Initial release: Javier Gonzalez <javier@cnexlabs.com> | |
5 | * Matias Bjorling <matias@cnexlabs.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU General Public License version | |
9 | * 2 as published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * pblk-map.c - pblk's lba-ppa mapping strategy | |
17 | * | |
18 | */ | |
19 | ||
20 | #include "pblk.h" | |
21 | ||
2deeefc0 JG |
22 | static int pblk_map_page_data(struct pblk *pblk, unsigned int sentry, |
23 | struct ppa_addr *ppa_list, | |
24 | unsigned long *lun_bitmap, | |
25 | struct pblk_sec_meta *meta_list, | |
26 | unsigned int valid_secs) | |
a4bd217b JG |
27 | { |
28 | struct pblk_line *line = pblk_line_get_data(pblk); | |
21d22871 | 29 | struct pblk_emeta *emeta; |
a4bd217b | 30 | struct pblk_w_ctx *w_ctx; |
21d22871 | 31 | __le64 *lba_list; |
a4bd217b JG |
32 | u64 paddr; |
33 | int nr_secs = pblk->min_write_pgs; | |
34 | int i; | |
35 | ||
21d22871 JG |
36 | if (pblk_line_is_full(line)) { |
37 | struct pblk_line *prev_line = line; | |
38 | ||
2deeefc0 JG |
39 | /* If we cannot allocate a new line, make sure to store metadata |
40 | * on current line and then fail | |
41 | */ | |
21d22871 JG |
42 | line = pblk_line_replace_data(pblk); |
43 | pblk_line_close_meta(pblk, prev_line); | |
2deeefc0 JG |
44 | |
45 | if (!line) | |
46 | return -EINTR; | |
21d22871 JG |
47 | } |
48 | ||
49 | emeta = line->emeta; | |
50 | lba_list = emeta_to_lbas(pblk, emeta->buf); | |
51 | ||
a4bd217b JG |
52 | paddr = pblk_alloc_page(pblk, line, nr_secs); |
53 | ||
54 | for (i = 0; i < nr_secs; i++, paddr++) { | |
03e868eb HH |
55 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
56 | ||
a4bd217b JG |
57 | /* ppa to be sent to the device */ |
58 | ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id); | |
59 | ||
60 | /* Write context for target bio completion on write buffer. Note | |
61 | * that the write buffer is protected by the sync backpointer, | |
62 | * and a single writer thread have access to each specific entry | |
63 | * at a time. Thus, it is safe to modify the context for the | |
64 | * entry we are setting up for submission without taking any | |
65 | * lock or memory barrier. | |
66 | */ | |
67 | if (i < valid_secs) { | |
68 | kref_get(&line->ref); | |
69 | w_ctx = pblk_rb_w_ctx(&pblk->rwb, sentry + i); | |
70 | w_ctx->ppa = ppa_list[i]; | |
71 | meta_list[i].lba = cpu_to_le64(w_ctx->lba); | |
72 | lba_list[paddr] = cpu_to_le64(w_ctx->lba); | |
03e868eb HH |
73 | if (lba_list[paddr] != addr_empty) |
74 | line->nr_valid_lbas++; | |
76758390 HH |
75 | else |
76 | atomic64_inc(&pblk->pad_wa); | |
a4bd217b | 77 | } else { |
caa69fa5 | 78 | lba_list[paddr] = meta_list[i].lba = addr_empty; |
0880a9aa | 79 | __pblk_map_invalidate(pblk, line, paddr); |
a4bd217b JG |
80 | } |
81 | } | |
82 | ||
43241cfe | 83 | pblk_down_rq(pblk, ppa_list[0], lun_bitmap); |
2deeefc0 | 84 | return 0; |
a4bd217b JG |
85 | } |
86 | ||
87 | void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry, | |
88 | unsigned long *lun_bitmap, unsigned int valid_secs, | |
89 | unsigned int off) | |
90 | { | |
91 | struct pblk_sec_meta *meta_list = rqd->meta_list; | |
d68a9344 | 92 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
a4bd217b JG |
93 | unsigned int map_secs; |
94 | int min = pblk->min_write_pgs; | |
95 | int i; | |
96 | ||
97 | for (i = off; i < rqd->nr_ppas; i += min) { | |
98 | map_secs = (i + min > valid_secs) ? (valid_secs % min) : min; | |
d68a9344 | 99 | if (pblk_map_page_data(pblk, sentry + i, &ppa_list[i], |
2deeefc0 JG |
100 | lun_bitmap, &meta_list[i], map_secs)) { |
101 | bio_put(rqd->bio); | |
102 | pblk_free_rqd(pblk, rqd, PBLK_WRITE); | |
103 | pblk_pipeline_stop(pblk); | |
104 | } | |
a4bd217b JG |
105 | } |
106 | } | |
107 | ||
108 | /* only if erase_ppa is set, acquire erase semaphore */ | |
109 | void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd, | |
110 | unsigned int sentry, unsigned long *lun_bitmap, | |
111 | unsigned int valid_secs, struct ppa_addr *erase_ppa) | |
112 | { | |
113 | struct nvm_tgt_dev *dev = pblk->dev; | |
114 | struct nvm_geo *geo = &dev->geo; | |
d624f371 | 115 | struct pblk_line_meta *lm = &pblk->lm; |
a4bd217b | 116 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
d68a9344 | 117 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
d624f371 | 118 | struct pblk_line *e_line, *d_line; |
a4bd217b JG |
119 | unsigned int map_secs; |
120 | int min = pblk->min_write_pgs; | |
121 | int i, erase_lun; | |
122 | ||
123 | for (i = 0; i < rqd->nr_ppas; i += min) { | |
124 | map_secs = (i + min > valid_secs) ? (valid_secs % min) : min; | |
d68a9344 | 125 | if (pblk_map_page_data(pblk, sentry + i, &ppa_list[i], |
2deeefc0 JG |
126 | lun_bitmap, &meta_list[i], map_secs)) { |
127 | bio_put(rqd->bio); | |
128 | pblk_free_rqd(pblk, rqd, PBLK_WRITE); | |
129 | pblk_pipeline_stop(pblk); | |
130 | } | |
a4bd217b | 131 | |
d68a9344 | 132 | erase_lun = pblk_ppa_to_pos(geo, ppa_list[i]); |
dd2a4343 | 133 | |
588726d3 JG |
134 | /* line can change after page map. We might also be writing the |
135 | * last line. | |
136 | */ | |
137 | e_line = pblk_line_get_erase(pblk); | |
138 | if (!e_line) | |
139 | return pblk_map_rq(pblk, rqd, sentry, lun_bitmap, | |
140 | valid_secs, i + min); | |
141 | ||
d624f371 | 142 | spin_lock(&e_line->lock); |
a4bd217b | 143 | if (!test_bit(erase_lun, e_line->erase_bitmap)) { |
a4bd217b | 144 | set_bit(erase_lun, e_line->erase_bitmap); |
a44f53fa | 145 | atomic_dec(&e_line->left_eblks); |
d624f371 | 146 | |
d68a9344 | 147 | *erase_ppa = ppa_list[i]; |
69471513 | 148 | erase_ppa->a.blk = e_line->id; |
a4bd217b | 149 | |
d624f371 JG |
150 | spin_unlock(&e_line->lock); |
151 | ||
a4bd217b JG |
152 | /* Avoid evaluating e_line->left_eblks */ |
153 | return pblk_map_rq(pblk, rqd, sentry, lun_bitmap, | |
154 | valid_secs, i + min); | |
155 | } | |
d624f371 | 156 | spin_unlock(&e_line->lock); |
a4bd217b JG |
157 | } |
158 | ||
d624f371 | 159 | d_line = pblk_line_get_data(pblk); |
a4bd217b | 160 | |
588726d3 JG |
161 | /* line can change after page map. We might also be writing the |
162 | * last line. | |
163 | */ | |
164 | e_line = pblk_line_get_erase(pblk); | |
165 | if (!e_line) | |
166 | return; | |
167 | ||
d624f371 | 168 | /* Erase blocks that are bad in this line but might not be in next */ |
26f76dce | 169 | if (unlikely(pblk_ppa_empty(*erase_ppa)) && |
d624f371 JG |
170 | bitmap_weight(d_line->blk_bitmap, lm->blk_per_line)) { |
171 | int bit = -1; | |
172 | ||
173 | retry: | |
174 | bit = find_next_bit(d_line->blk_bitmap, | |
175 | lm->blk_per_line, bit + 1); | |
176 | if (bit >= lm->blk_per_line) | |
a4bd217b JG |
177 | return; |
178 | ||
d624f371 JG |
179 | spin_lock(&e_line->lock); |
180 | if (test_bit(bit, e_line->erase_bitmap)) { | |
181 | spin_unlock(&e_line->lock); | |
182 | goto retry; | |
183 | } | |
184 | spin_unlock(&e_line->lock); | |
185 | ||
186 | set_bit(bit, e_line->erase_bitmap); | |
a44f53fa | 187 | atomic_dec(&e_line->left_eblks); |
d624f371 | 188 | *erase_ppa = pblk->luns[bit].bppa; /* set ch and lun */ |
69471513 | 189 | erase_ppa->a.blk = e_line->id; |
a4bd217b JG |
190 | } |
191 | } |