blob: 8ae40855d4c9eb80215a26102643063010afd73c [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
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-core.c - pblk's core functionality
16 *
17 */
18
Hans Holmberg4c44abf2018-10-09 13:11:52 +020019#define CREATE_TRACE_POINTS
20
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020021#include "pblk.h"
Hans Holmberg4c44abf2018-10-09 13:11:52 +020022#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020023
Javier González8bd40022017-10-13 14:46:44 +020024static void pblk_line_mark_bb(struct work_struct *work)
25{
26 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
27 ws);
28 struct pblk *pblk = line_ws->pblk;
29 struct nvm_tgt_dev *dev = pblk->dev;
30 struct ppa_addr *ppa = line_ws->priv;
31 int ret;
32
Matias Bjørlingaff3fb12018-10-09 13:11:36 +020033 ret = nvm_set_chunk_meta(dev, ppa, 1, NVM_BLK_T_GRWN_BAD);
Javier González8bd40022017-10-13 14:46:44 +020034 if (ret) {
35 struct pblk_line *line;
36 int pos;
37
Javier Gonzálezcb216652018-10-09 13:11:42 +020038 line = pblk_ppa_to_line(pblk, *ppa);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +010039 pos = pblk_ppa_to_pos(&dev->geo, *ppa);
Javier González8bd40022017-10-13 14:46:44 +020040
Matias Bjørling4e495a42018-07-13 10:48:42 +020041 pblk_err(pblk, "failed to mark bb, line:%d, pos:%d\n",
Javier González8bd40022017-10-13 14:46:44 +020042 line->id, pos);
43 }
44
45 kfree(ppa);
Kent Overstreetb906bbb2018-05-20 18:25:50 -040046 mempool_free(line_ws, &pblk->gen_ws_pool);
Javier González8bd40022017-10-13 14:46:44 +020047}
48
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020049static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line,
Javier González32ef9412018-03-30 00:05:20 +020050 struct ppa_addr ppa_addr)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020051{
52 struct nvm_tgt_dev *dev = pblk->dev;
53 struct nvm_geo *geo = &dev->geo;
Javier González32ef9412018-03-30 00:05:20 +020054 struct ppa_addr *ppa;
55 int pos = pblk_ppa_to_pos(geo, ppa_addr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020056
Matias Bjørling4e495a42018-07-13 10:48:42 +020057 pblk_debug(pblk, "erase failed: line:%d, pos:%d\n", line->id, pos);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020058 atomic_long_inc(&pblk->erase_failed);
59
Javier Gonzáleza44f53f2017-04-22 01:32:49 +020060 atomic_dec(&line->blk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020061 if (test_and_set_bit(pos, line->blk_bitmap))
Matias Bjørling4e495a42018-07-13 10:48:42 +020062 pblk_err(pblk, "attempted to erase bb: line:%d, pos:%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020063 line->id, pos);
64
Javier González32ef9412018-03-30 00:05:20 +020065 /* Not necessary to mark bad blocks on 2.0 spec. */
66 if (geo->version == NVM_OCSSD_SPEC_20)
67 return;
68
69 ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC);
70 if (!ppa)
71 return;
72
73 *ppa = ppa_addr;
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020074 pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb,
75 GFP_ATOMIC, pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020076}
77
78static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd)
79{
Javier González32ef9412018-03-30 00:05:20 +020080 struct nvm_tgt_dev *dev = pblk->dev;
81 struct nvm_geo *geo = &dev->geo;
82 struct nvm_chk_meta *chunk;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020083 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +020084 int pos;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020085
Javier Gonzálezcb216652018-10-09 13:11:42 +020086 line = pblk_ppa_to_line(pblk, rqd->ppa_addr);
Javier González32ef9412018-03-30 00:05:20 +020087 pos = pblk_ppa_to_pos(geo, rqd->ppa_addr);
88 chunk = &line->chks[pos];
89
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020090 atomic_dec(&line->left_seblks);
91
92 if (rqd->error) {
Hans Holmberg4209c312018-10-09 13:11:55 +020093 trace_pblk_chunk_reset(pblk_disk_name(pblk),
94 &rqd->ppa_addr, PBLK_CHUNK_RESET_FAILED);
95
Javier González32ef9412018-03-30 00:05:20 +020096 chunk->state = NVM_CHK_ST_OFFLINE;
97 pblk_mark_bb(pblk, line, rqd->ppa_addr);
98 } else {
Hans Holmberg4209c312018-10-09 13:11:55 +020099 trace_pblk_chunk_reset(pblk_disk_name(pblk),
100 &rqd->ppa_addr, PBLK_CHUNK_RESET_DONE);
101
Javier González32ef9412018-03-30 00:05:20 +0200102 chunk->state = NVM_CHK_ST_FREE;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200103 }
Javier González588726d32017-06-26 11:57:29 +0200104
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200105 trace_pblk_chunk_state(pblk_disk_name(pblk), &rqd->ppa_addr,
106 chunk->state);
107
Javier González588726d32017-06-26 11:57:29 +0200108 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200109}
110
111/* Erase completion assumes that only one block is erased at the time */
112static void pblk_end_io_erase(struct nvm_rq *rqd)
113{
114 struct pblk *pblk = rqd->private;
115
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200116 __pblk_end_io_erase(pblk, rqd);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400117 mempool_free(rqd, &pblk->e_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200118}
119
Javier González32ef9412018-03-30 00:05:20 +0200120/*
121 * Get information for all chunks from the device.
122 *
Javier González090ee262018-10-09 13:12:01 +0200123 * The caller is responsible for freeing (vmalloc) the returned structure
Javier González32ef9412018-03-30 00:05:20 +0200124 */
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200125struct nvm_chk_meta *pblk_get_chunk_meta(struct pblk *pblk)
Javier González32ef9412018-03-30 00:05:20 +0200126{
127 struct nvm_tgt_dev *dev = pblk->dev;
128 struct nvm_geo *geo = &dev->geo;
129 struct nvm_chk_meta *meta;
130 struct ppa_addr ppa;
131 unsigned long len;
132 int ret;
133
134 ppa.ppa = 0;
135
136 len = geo->all_chunks * sizeof(*meta);
Javier González090ee262018-10-09 13:12:01 +0200137 meta = vzalloc(len);
Javier González32ef9412018-03-30 00:05:20 +0200138 if (!meta)
139 return ERR_PTR(-ENOMEM);
140
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200141 ret = nvm_get_chunk_meta(dev, ppa, geo->all_chunks, meta);
Javier González32ef9412018-03-30 00:05:20 +0200142 if (ret) {
143 kfree(meta);
144 return ERR_PTR(-EIO);
145 }
146
147 return meta;
148}
149
150struct nvm_chk_meta *pblk_chunk_get_off(struct pblk *pblk,
151 struct nvm_chk_meta *meta,
152 struct ppa_addr ppa)
153{
154 struct nvm_tgt_dev *dev = pblk->dev;
155 struct nvm_geo *geo = &dev->geo;
156 int ch_off = ppa.m.grp * geo->num_chk * geo->num_lun;
157 int lun_off = ppa.m.pu * geo->num_chk;
158 int chk_off = ppa.m.chk;
159
160 return meta + ch_off + lun_off + chk_off;
161}
162
Javier González0880a9a2017-06-26 11:57:19 +0200163void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line,
164 u64 paddr)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200165{
166 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
167 struct list_head *move_list = NULL;
168
169 /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P
170 * table is modified with reclaimed sectors, a check is done to endure
171 * that newer updates are not overwritten.
172 */
173 spin_lock(&line->lock);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200174 WARN_ON(line->state == PBLK_LINESTATE_FREE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200175
176 if (test_and_set_bit(paddr, line->invalid_bitmap)) {
177 WARN_ONCE(1, "pblk: double invalidate\n");
178 spin_unlock(&line->lock);
179 return;
180 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200181 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200182
183 if (line->state == PBLK_LINESTATE_CLOSED)
184 move_list = pblk_line_gc_list(pblk, line);
185 spin_unlock(&line->lock);
186
187 if (move_list) {
188 spin_lock(&l_mg->gc_lock);
189 spin_lock(&line->lock);
190 /* Prevent moving a line that has just been chosen for GC */
Javier Gonzálezd3401212017-10-13 14:46:14 +0200191 if (line->state == PBLK_LINESTATE_GC) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200192 spin_unlock(&line->lock);
193 spin_unlock(&l_mg->gc_lock);
194 return;
195 }
196 spin_unlock(&line->lock);
197
198 list_move_tail(&line->list, move_list);
199 spin_unlock(&l_mg->gc_lock);
200 }
201}
202
203void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa)
204{
205 struct pblk_line *line;
206 u64 paddr;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200207
Matias Bjørling880eda52018-07-13 10:48:37 +0200208#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200209 /* Callers must ensure that the ppa points to a device address */
210 BUG_ON(pblk_addr_in_cache(ppa));
211 BUG_ON(pblk_ppa_empty(ppa));
212#endif
213
Javier Gonzálezcb216652018-10-09 13:11:42 +0200214 line = pblk_ppa_to_line(pblk, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200215 paddr = pblk_dev_ppa_to_line_addr(pblk, ppa);
216
217 __pblk_map_invalidate(pblk, line, paddr);
218}
219
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200220static void pblk_invalidate_range(struct pblk *pblk, sector_t slba,
221 unsigned int nr_secs)
222{
223 sector_t lba;
224
225 spin_lock(&pblk->trans_lock);
226 for (lba = slba; lba < slba + nr_secs; lba++) {
227 struct ppa_addr ppa;
228
229 ppa = pblk_trans_map_get(pblk, lba);
230
231 if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa))
232 pblk_map_invalidate(pblk, ppa);
233
234 pblk_ppa_set_empty(&ppa);
235 pblk_trans_map_set(pblk, lba, ppa);
236 }
237 spin_unlock(&pblk->trans_lock);
238}
239
Javier González45dcf292018-10-09 13:12:02 +0200240int pblk_alloc_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
241{
242 struct nvm_tgt_dev *dev = pblk->dev;
243
244 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
245 &rqd->dma_meta_list);
246 if (!rqd->meta_list)
247 return -ENOMEM;
248
249 if (rqd->nr_ppas == 1)
250 return 0;
251
252 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
253 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
254
255 return 0;
256}
257
258void pblk_free_rqd_meta(struct pblk *pblk, struct nvm_rq *rqd)
259{
260 struct nvm_tgt_dev *dev = pblk->dev;
261
262 if (rqd->meta_list)
263 nvm_dev_dma_free(dev->parent, rqd->meta_list,
264 rqd->dma_meta_list);
265}
266
Javier González67bf26a2017-10-13 14:46:20 +0200267/* Caller must guarantee that the request is a valid type */
268struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200269{
270 mempool_t *pool;
271 struct nvm_rq *rqd;
272 int rq_size;
273
Javier González67bf26a2017-10-13 14:46:20 +0200274 switch (type) {
275 case PBLK_WRITE:
276 case PBLK_WRITE_INT:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400277 pool = &pblk->w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200278 rq_size = pblk_w_rq_size;
Javier González67bf26a2017-10-13 14:46:20 +0200279 break;
280 case PBLK_READ:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400281 pool = &pblk->r_rq_pool;
Javier González084ec9b2017-06-26 16:27:13 -0600282 rq_size = pblk_g_rq_size;
Javier González67bf26a2017-10-13 14:46:20 +0200283 break;
284 default:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400285 pool = &pblk->e_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200286 rq_size = pblk_g_rq_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200287 }
288
289 rqd = mempool_alloc(pool, GFP_KERNEL);
290 memset(rqd, 0, rq_size);
291
292 return rqd;
293}
294
Javier González67bf26a2017-10-13 14:46:20 +0200295/* Typically used on completion path. Cannot guarantee request consistency */
296void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200297{
298 mempool_t *pool;
299
Javier González67bf26a2017-10-13 14:46:20 +0200300 switch (type) {
301 case PBLK_WRITE:
302 kfree(((struct pblk_c_ctx *)nvm_rq_to_pdu(rqd))->lun_bitmap);
Gustavo A. R. Silva884b0312018-07-13 10:48:43 +0200303 /* fall through */
Javier González67bf26a2017-10-13 14:46:20 +0200304 case PBLK_WRITE_INT:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400305 pool = &pblk->w_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200306 break;
307 case PBLK_READ:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400308 pool = &pblk->r_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200309 break;
310 case PBLK_ERASE:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400311 pool = &pblk->e_rq_pool;
Javier González67bf26a2017-10-13 14:46:20 +0200312 break;
313 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200314 pblk_err(pblk, "trying to free unknown rqd type\n");
Javier González67bf26a2017-10-13 14:46:20 +0200315 return;
316 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200317
Javier González45dcf292018-10-09 13:12:02 +0200318 pblk_free_rqd_meta(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200319 mempool_free(rqd, pool);
320}
321
322void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off,
323 int nr_pages)
324{
325 struct bio_vec bv;
326 int i;
327
328 WARN_ON(off + nr_pages != bio->bi_vcnt);
329
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200330 for (i = off; i < nr_pages + off; i++) {
331 bv = bio->bi_io_vec[i];
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400332 mempool_free(bv.bv_page, &pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200333 }
334}
335
336int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags,
337 int nr_pages)
338{
339 struct request_queue *q = pblk->dev->q;
340 struct page *page;
341 int i, ret;
342
343 for (i = 0; i < nr_pages; i++) {
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400344 page = mempool_alloc(&pblk->page_bio_pool, flags);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200345
346 ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0);
347 if (ret != PBLK_EXPOSED_PAGE_SIZE) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200348 pblk_err(pblk, "could not add page to bio\n");
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400349 mempool_free(page, &pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200350 goto err;
351 }
352 }
353
354 return 0;
355err:
Igor Konopkof142ac02018-06-01 16:41:08 +0200356 pblk_bio_free_pages(pblk, bio, (bio->bi_vcnt - i), i);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200357 return -1;
358}
359
Hans Holmbergcc9c9a02018-06-01 16:41:13 +0200360void pblk_write_kick(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200361{
362 wake_up_process(pblk->writer_ts);
363 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000));
364}
365
Kees Cook87c1d2d2017-10-17 21:10:19 -0700366void pblk_write_timer_fn(struct timer_list *t)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200367{
Kees Cook87c1d2d2017-10-17 21:10:19 -0700368 struct pblk *pblk = from_timer(pblk, t, wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200369
370 /* kick the write thread every tick to flush outstanding data */
371 pblk_write_kick(pblk);
372}
373
374void pblk_write_should_kick(struct pblk *pblk)
375{
376 unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb);
377
378 if (secs_avail >= pblk->min_write_pgs)
379 pblk_write_kick(pblk);
380}
381
Javier González8bd40022017-10-13 14:46:44 +0200382static void pblk_wait_for_meta(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200383{
Javier González588726d32017-06-26 11:57:29 +0200384 do {
385 if (!atomic_read(&pblk->inflight_io))
386 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200387
Javier González588726d32017-06-26 11:57:29 +0200388 schedule();
389 } while (1);
390}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200391
Javier González588726d32017-06-26 11:57:29 +0200392static void pblk_flush_writer(struct pblk *pblk)
393{
394 pblk_rb_flush(&pblk->rwb);
395 do {
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200396 if (!pblk_rb_sync_count(&pblk->rwb))
Javier González588726d32017-06-26 11:57:29 +0200397 break;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200398
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200399 pblk_write_kick(pblk);
Javier González588726d32017-06-26 11:57:29 +0200400 schedule();
401 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200402}
403
404struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line)
405{
406 struct pblk_line_meta *lm = &pblk->lm;
407 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
408 struct list_head *move_list = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200409 int vsc = le32_to_cpu(*line->vsc);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200410
Javier González476118c2017-06-26 11:57:26 +0200411 lockdep_assert_held(&line->lock);
412
Hans Holmberg48b8d202018-06-01 16:41:06 +0200413 if (line->w_err_gc->has_write_err) {
414 if (line->gc_group != PBLK_LINEGC_WERR) {
415 line->gc_group = PBLK_LINEGC_WERR;
416 move_list = &l_mg->gc_werr_list;
417 pblk_rl_werr_line_in(&pblk->rl);
418 }
419 } else if (!vsc) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200420 if (line->gc_group != PBLK_LINEGC_FULL) {
421 line->gc_group = PBLK_LINEGC_FULL;
422 move_list = &l_mg->gc_full_list;
423 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200424 } else if (vsc < lm->high_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200425 if (line->gc_group != PBLK_LINEGC_HIGH) {
426 line->gc_group = PBLK_LINEGC_HIGH;
427 move_list = &l_mg->gc_high_list;
428 }
Javier Gonzálezb20ba1b2017-06-26 11:57:27 +0200429 } else if (vsc < lm->mid_thrs) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200430 if (line->gc_group != PBLK_LINEGC_MID) {
431 line->gc_group = PBLK_LINEGC_MID;
432 move_list = &l_mg->gc_mid_list;
433 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200434 } else if (vsc < line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200435 if (line->gc_group != PBLK_LINEGC_LOW) {
436 line->gc_group = PBLK_LINEGC_LOW;
437 move_list = &l_mg->gc_low_list;
438 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200439 } else if (vsc == line->sec_in_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200440 if (line->gc_group != PBLK_LINEGC_EMPTY) {
441 line->gc_group = PBLK_LINEGC_EMPTY;
442 move_list = &l_mg->gc_empty_list;
443 }
444 } else {
445 line->state = PBLK_LINESTATE_CORRUPT;
Hans Holmbergf2937232018-10-09 13:11:53 +0200446 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
447 line->state);
448
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200449 line->gc_group = PBLK_LINEGC_NONE;
450 move_list = &l_mg->corrupt_list;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200451 pblk_err(pblk, "corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200452 line->id, vsc,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200453 line->sec_in_line,
454 lm->high_thrs, lm->mid_thrs);
455 }
456
457 return move_list;
458}
459
460void pblk_discard(struct pblk *pblk, struct bio *bio)
461{
462 sector_t slba = pblk_get_lba(bio);
463 sector_t nr_secs = pblk_get_secs(bio);
464
465 pblk_invalidate_range(pblk, slba, nr_secs);
466}
467
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200468void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd)
469{
470 atomic_long_inc(&pblk->write_failed);
Matias Bjørling880eda52018-07-13 10:48:37 +0200471#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200472 pblk_print_failed_rqd(pblk, rqd, rqd->error);
473#endif
474}
475
476void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd)
477{
478 /* Empty page read is not necessarily an error (e.g., L2P recovery) */
479 if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
480 atomic_long_inc(&pblk->read_empty);
481 return;
482 }
483
484 switch (rqd->error) {
485 case NVM_RSP_WARN_HIGHECC:
486 atomic_long_inc(&pblk->read_high_ecc);
487 break;
488 case NVM_RSP_ERR_FAILECC:
489 case NVM_RSP_ERR_FAILCRC:
490 atomic_long_inc(&pblk->read_failed);
491 break;
492 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200493 pblk_err(pblk, "unknown read error:%d\n", rqd->error);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494 }
Matias Bjørling880eda52018-07-13 10:48:37 +0200495#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200496 pblk_print_failed_rqd(pblk, rqd, rqd->error);
497#endif
498}
499
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200500void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write)
501{
502 pblk->sec_per_write = sec_per_write;
503}
504
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200505int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd)
506{
507 struct nvm_tgt_dev *dev = pblk->dev;
508
Javier González588726d32017-06-26 11:57:29 +0200509 atomic_inc(&pblk->inflight_io);
510
Matias Bjørling880eda52018-07-13 10:48:37 +0200511#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezb6730dd42018-06-01 15:04:20 +0200512 if (pblk_check_io(pblk, rqd))
513 return NVM_IO_ERR;
514#endif
515
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200516 return nvm_submit_io(dev, rqd);
517}
518
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200519void pblk_check_chunk_state_update(struct pblk *pblk, struct nvm_rq *rqd)
520{
521 struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd);
522
523 int i;
524
525 for (i = 0; i < rqd->nr_ppas; i++) {
526 struct ppa_addr *ppa = &ppa_list[i];
527 struct nvm_chk_meta *chunk = pblk_dev_ppa_to_chunk(pblk, *ppa);
528 u64 caddr = pblk_dev_ppa_to_chunk_addr(pblk, *ppa);
529
530 if (caddr == 0)
531 trace_pblk_chunk_state(pblk_disk_name(pblk),
532 ppa, NVM_CHK_ST_OPEN);
533 else if (caddr == chunk->cnlb)
534 trace_pblk_chunk_state(pblk_disk_name(pblk),
535 ppa, NVM_CHK_ST_CLOSED);
536 }
537}
538
Javier González1a94b2d2017-10-13 14:46:47 +0200539int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd)
540{
541 struct nvm_tgt_dev *dev = pblk->dev;
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200542 int ret;
Javier González1a94b2d2017-10-13 14:46:47 +0200543
Javier González1a94b2d2017-10-13 14:46:47 +0200544 atomic_inc(&pblk->inflight_io);
545
Matias Bjørling880eda52018-07-13 10:48:37 +0200546#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezb6730dd42018-06-01 15:04:20 +0200547 if (pblk_check_io(pblk, rqd))
548 return NVM_IO_ERR;
549#endif
550
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200551 ret = nvm_submit_io_sync(dev, rqd);
552
553 if (trace_pblk_chunk_state_enabled() && !ret &&
554 rqd->opcode == NVM_OP_PWRITE)
555 pblk_check_chunk_state_update(pblk, rqd);
556
557 return ret;
Javier González1a94b2d2017-10-13 14:46:47 +0200558}
559
Javier González55e836d2017-10-13 14:46:16 +0200560static void pblk_bio_map_addr_endio(struct bio *bio)
561{
562 bio_put(bio);
563}
564
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200565struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data,
566 unsigned int nr_secs, unsigned int len,
Javier Gonzálezde54e702017-06-30 17:56:39 +0200567 int alloc_type, gfp_t gfp_mask)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200568{
569 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200570 void *kaddr = data;
571 struct page *page;
572 struct bio *bio;
573 int i, ret;
574
Javier Gonzálezde54e702017-06-30 17:56:39 +0200575 if (alloc_type == PBLK_KMALLOC_META)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200576 return bio_map_kern(dev->q, kaddr, len, gfp_mask);
577
578 bio = bio_kmalloc(gfp_mask, nr_secs);
579 if (!bio)
580 return ERR_PTR(-ENOMEM);
581
582 for (i = 0; i < nr_secs; i++) {
583 page = vmalloc_to_page(kaddr);
584 if (!page) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200585 pblk_err(pblk, "could not map vmalloc bio\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200586 bio_put(bio);
587 bio = ERR_PTR(-ENOMEM);
588 goto out;
589 }
590
591 ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0);
592 if (ret != PAGE_SIZE) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200593 pblk_err(pblk, "could not add page to bio\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200594 bio_put(bio);
595 bio = ERR_PTR(-ENOMEM);
596 goto out;
597 }
598
599 kaddr += PAGE_SIZE;
600 }
Javier González55e836d2017-10-13 14:46:16 +0200601
602 bio->bi_end_io = pblk_bio_map_addr_endio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200603out:
604 return bio;
605}
606
607int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail,
608 unsigned long secs_to_flush)
609{
Javier Gonzálezc2e9f5d2017-06-26 11:57:14 +0200610 int max = pblk->sec_per_write;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200611 int min = pblk->min_write_pgs;
612 int secs_to_sync = 0;
613
614 if (secs_avail >= max)
615 secs_to_sync = max;
616 else if (secs_avail >= min)
617 secs_to_sync = min * (secs_avail / min);
618 else if (secs_to_flush)
619 secs_to_sync = min;
620
621 return secs_to_sync;
622}
623
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200624void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
625{
626 u64 addr;
627 int i;
628
Rakesh Pandite57903f2017-10-13 14:45:56 +0200629 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200630 addr = find_next_zero_bit(line->map_bitmap,
631 pblk->lm.sec_per_line, line->cur_sec);
632 line->cur_sec = addr - nr_secs;
633
634 for (i = 0; i < nr_secs; i++, line->cur_sec--)
635 WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap));
Rakesh Pandite57903f2017-10-13 14:45:56 +0200636 spin_unlock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200637}
638
639u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200640{
641 u64 addr;
642 int i;
643
Javier González476118c2017-06-26 11:57:26 +0200644 lockdep_assert_held(&line->lock);
645
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200646 /* logic error: ppa out-of-bounds. Prevent generating bad address */
647 if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) {
648 WARN(1, "pblk: page allocation out of bounds\n");
649 nr_secs = pblk->lm.sec_per_line - line->cur_sec;
650 }
651
652 line->cur_sec = addr = find_next_zero_bit(line->map_bitmap,
653 pblk->lm.sec_per_line, line->cur_sec);
654 for (i = 0; i < nr_secs; i++, line->cur_sec++)
655 WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap));
656
657 return addr;
658}
659
660u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs)
661{
662 u64 addr;
663
664 /* Lock needed in case a write fails and a recovery needs to remap
665 * failed write buffer entries
666 */
667 spin_lock(&line->lock);
668 addr = __pblk_alloc_page(pblk, line, nr_secs);
669 line->left_msecs -= nr_secs;
670 WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n");
671 spin_unlock(&line->lock);
672
673 return addr;
674}
675
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200676u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line)
677{
678 u64 paddr;
679
680 spin_lock(&line->lock);
681 paddr = find_next_zero_bit(line->map_bitmap,
682 pblk->lm.sec_per_line, line->cur_sec);
683 spin_unlock(&line->lock);
684
685 return paddr;
686}
687
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200688/*
689 * Submit emeta to one LUN in the raid line at the time to avoid a deadlock when
690 * taking the per LUN semaphore.
691 */
692static int pblk_line_submit_emeta_io(struct pblk *pblk, struct pblk_line *line,
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200693 void *emeta_buf, u64 paddr, int dir)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200694{
695 struct nvm_tgt_dev *dev = pblk->dev;
696 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezde54e702017-06-30 17:56:39 +0200697 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200698 struct pblk_line_meta *lm = &pblk->lm;
Javier González63e38092017-06-26 11:57:24 +0200699 void *ppa_list, *meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200700 struct bio *bio;
701 struct nvm_rq rqd;
Javier González63e38092017-06-26 11:57:24 +0200702 dma_addr_t dma_ppa_list, dma_meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200703 int min = pblk->min_write_pgs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200704 int left_ppas = lm->emeta_sec[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200705 int id = line->id;
706 int rq_ppas, rq_len;
707 int cmd_op, bio_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200708 int i, j;
709 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200710
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200711 if (dir == PBLK_WRITE) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200712 bio_op = REQ_OP_WRITE;
713 cmd_op = NVM_OP_PWRITE;
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200714 } else if (dir == PBLK_READ) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200715 bio_op = REQ_OP_READ;
716 cmd_op = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200717 } else
718 return -EINVAL;
719
Javier González63e38092017-06-26 11:57:24 +0200720 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
721 &dma_meta_list);
722 if (!meta_list)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200723 return -ENOMEM;
724
Javier González63e38092017-06-26 11:57:24 +0200725 ppa_list = meta_list + pblk_dma_meta_size;
726 dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
727
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200728next_rq:
729 memset(&rqd, 0, sizeof(struct nvm_rq));
730
731 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200732 rq_len = rq_ppas * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200733
Javier Gonzálezde54e702017-06-30 17:56:39 +0200734 bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len,
735 l_mg->emeta_alloc_type, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200736 if (IS_ERR(bio)) {
737 ret = PTR_ERR(bio);
738 goto free_rqd_dma;
739 }
740
741 bio->bi_iter.bi_sector = 0; /* internal bio */
742 bio_set_op_attrs(bio, bio_op, 0);
743
744 rqd.bio = bio;
Javier González63e38092017-06-26 11:57:24 +0200745 rqd.meta_list = meta_list;
746 rqd.ppa_list = ppa_list;
747 rqd.dma_meta_list = dma_meta_list;
748 rqd.dma_ppa_list = dma_ppa_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200749 rqd.opcode = cmd_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200750 rqd.nr_ppas = rq_ppas;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200751
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200752 if (dir == PBLK_WRITE) {
Javier González63e38092017-06-26 11:57:24 +0200753 struct pblk_sec_meta *meta_list = rqd.meta_list;
754
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200755 rqd.is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200756 for (i = 0; i < rqd.nr_ppas; ) {
757 spin_lock(&line->lock);
758 paddr = __pblk_alloc_page(pblk, line, min);
759 spin_unlock(&line->lock);
Javier González63e38092017-06-26 11:57:24 +0200760 for (j = 0; j < min; j++, i++, paddr++) {
761 meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200762 rqd.ppa_list[i] =
763 addr_to_gen_ppa(pblk, paddr, id);
Javier González63e38092017-06-26 11:57:24 +0200764 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200765 }
766 } else {
767 for (i = 0; i < rqd.nr_ppas; ) {
768 struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100769 int pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200770
771 if (pblk_io_aligned(pblk, rq_ppas))
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200772 rqd.is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200773
774 while (test_bit(pos, line->blk_bitmap)) {
775 paddr += min;
776 if (pblk_boundary_paddr_checks(pblk, paddr)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200777 pblk_err(pblk, "corrupt emeta line:%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200778 line->id);
779 bio_put(bio);
780 ret = -EINTR;
781 goto free_rqd_dma;
782 }
783
784 ppa = addr_to_gen_ppa(pblk, paddr, id);
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100785 pos = pblk_ppa_to_pos(geo, ppa);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200786 }
787
788 if (pblk_boundary_paddr_checks(pblk, paddr + min)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200789 pblk_err(pblk, "corrupt emeta line:%d\n",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200790 line->id);
791 bio_put(bio);
792 ret = -EINTR;
793 goto free_rqd_dma;
794 }
795
796 for (j = 0; j < min; j++, i++, paddr++)
797 rqd.ppa_list[i] =
798 addr_to_gen_ppa(pblk, paddr, line->id);
799 }
800 }
801
Javier González1a94b2d2017-10-13 14:46:47 +0200802 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200803 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200804 pblk_err(pblk, "emeta I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200805 bio_put(bio);
806 goto free_rqd_dma;
807 }
808
Javier González588726d32017-06-26 11:57:29 +0200809 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200810
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200811 if (rqd.error) {
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200812 if (dir == PBLK_WRITE)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200813 pblk_log_write_err(pblk, &rqd);
814 else
815 pblk_log_read_err(pblk, &rqd);
816 }
817
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200818 emeta_buf += rq_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200819 left_ppas -= rq_ppas;
820 if (left_ppas)
821 goto next_rq;
822free_rqd_dma:
Javier González63e38092017-06-26 11:57:24 +0200823 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200824 return ret;
825}
826
827u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line)
828{
829 struct nvm_tgt_dev *dev = pblk->dev;
830 struct nvm_geo *geo = &dev->geo;
831 struct pblk_line_meta *lm = &pblk->lm;
832 int bit;
833
834 /* This usually only happens on bad lines */
835 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
836 if (bit >= lm->blk_per_line)
837 return -1;
838
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200839 return bit * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200840}
841
842static int pblk_line_submit_smeta_io(struct pblk *pblk, struct pblk_line *line,
843 u64 paddr, int dir)
844{
845 struct nvm_tgt_dev *dev = pblk->dev;
846 struct pblk_line_meta *lm = &pblk->lm;
847 struct bio *bio;
848 struct nvm_rq rqd;
849 __le64 *lba_list = NULL;
850 int i, ret;
851 int cmd_op, bio_op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200852
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200853 if (dir == PBLK_WRITE) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200854 bio_op = REQ_OP_WRITE;
855 cmd_op = NVM_OP_PWRITE;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200856 lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier González8f554592018-01-05 14:16:16 +0100857 } else if (dir == PBLK_READ_RECOV || dir == PBLK_READ) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200858 bio_op = REQ_OP_READ;
859 cmd_op = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200860 } else
861 return -EINVAL;
862
863 memset(&rqd, 0, sizeof(struct nvm_rq));
864
Javier González45dcf292018-10-09 13:12:02 +0200865 ret = pblk_alloc_rqd_meta(pblk, &rqd);
866 if (ret)
867 return ret;
Javier González63e38092017-06-26 11:57:24 +0200868
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200869 bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL);
870 if (IS_ERR(bio)) {
871 ret = PTR_ERR(bio);
Javier González45dcf292018-10-09 13:12:02 +0200872 goto clear_rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200873 }
874
875 bio->bi_iter.bi_sector = 0; /* internal bio */
876 bio_set_op_attrs(bio, bio_op, 0);
877
878 rqd.bio = bio;
879 rqd.opcode = cmd_op;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200880 rqd.is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200881 rqd.nr_ppas = lm->smeta_sec;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200882
883 for (i = 0; i < lm->smeta_sec; i++, paddr++) {
Javier González63e38092017-06-26 11:57:24 +0200884 struct pblk_sec_meta *meta_list = rqd.meta_list;
885
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200886 rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id);
Javier González63e38092017-06-26 11:57:24 +0200887
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200888 if (dir == PBLK_WRITE) {
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200889 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
Javier González63e38092017-06-26 11:57:24 +0200890
891 meta_list[i].lba = lba_list[paddr] = addr_empty;
892 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200893 }
894
895 /*
896 * This I/O is sent by the write thread when a line is replace. Since
897 * the write thread is the only one sending write and erase commands,
898 * there is no need to take the LUN semaphore.
899 */
Javier González1a94b2d2017-10-13 14:46:47 +0200900 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200901 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200902 pblk_err(pblk, "smeta I/O submission failed: %d\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200903 bio_put(bio);
Javier González45dcf292018-10-09 13:12:02 +0200904 goto clear_rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200905 }
906
Javier González588726d32017-06-26 11:57:29 +0200907 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200908
909 if (rqd.error) {
Hans Holmberg6cf17a22018-06-01 16:41:07 +0200910 if (dir == PBLK_WRITE) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200911 pblk_log_write_err(pblk, &rqd);
Hans Holmberg6cf17a22018-06-01 16:41:07 +0200912 ret = 1;
913 } else if (dir == PBLK_READ)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200914 pblk_log_read_err(pblk, &rqd);
915 }
916
Javier González45dcf292018-10-09 13:12:02 +0200917clear_rqd:
918 pblk_free_rqd_meta(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200919 return ret;
920}
921
922int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line)
923{
924 u64 bpaddr = pblk_line_smeta_start(pblk, line);
925
Javier González8f554592018-01-05 14:16:16 +0100926 return pblk_line_submit_smeta_io(pblk, line, bpaddr, PBLK_READ_RECOV);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200927}
928
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200929int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line,
930 void *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200931{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200932 return pblk_line_submit_emeta_io(pblk, line, emeta_buf,
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200933 line->emeta_ssec, PBLK_READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200934}
935
936static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd,
937 struct ppa_addr ppa)
938{
939 rqd->opcode = NVM_OP_ERASE;
940 rqd->ppa_addr = ppa;
941 rqd->nr_ppas = 1;
Matias Bjørlingd7b68012018-10-09 13:11:32 +0200942 rqd->is_seq = 1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200943 rqd->bio = NULL;
944}
945
946static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa)
947{
Matias Bjørling4b5d56e2018-10-09 13:11:33 +0200948 struct nvm_rq rqd = {NULL};
949 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200950
Hans Holmberg4209c312018-10-09 13:11:55 +0200951 trace_pblk_chunk_reset(pblk_disk_name(pblk), &ppa,
952 PBLK_CHUNK_RESET_START);
953
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200954 pblk_setup_e_rq(pblk, &rqd, ppa);
955
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200956 /* The write thread schedules erases so that it minimizes disturbances
957 * with writes. Thus, there is no need to take the LUN semaphore.
958 */
Javier González1a94b2d2017-10-13 14:46:47 +0200959 ret = pblk_submit_io_sync(pblk, &rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200960 rqd.private = pblk;
961 __pblk_end_io_erase(pblk, &rqd);
962
Javier González588726d32017-06-26 11:57:29 +0200963 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200964}
965
966int pblk_line_erase(struct pblk *pblk, struct pblk_line *line)
967{
968 struct pblk_line_meta *lm = &pblk->lm;
969 struct ppa_addr ppa;
Javier González588726d32017-06-26 11:57:29 +0200970 int ret, bit = -1;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200971
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200972 /* Erase only good blocks, one at a time */
973 do {
974 spin_lock(&line->lock);
975 bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line,
976 bit + 1);
977 if (bit >= lm->blk_per_line) {
978 spin_unlock(&line->lock);
979 break;
980 }
981
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200982 ppa = pblk->luns[bit].bppa; /* set ch and lun */
Javier González69471512018-03-30 00:05:15 +0200983 ppa.a.blk = line->id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200984
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200985 atomic_dec(&line->left_eblks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200986 WARN_ON(test_and_set_bit(bit, line->erase_bitmap));
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200987 spin_unlock(&line->lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200988
Javier González588726d32017-06-26 11:57:29 +0200989 ret = pblk_blk_erase_sync(pblk, ppa);
990 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200991 pblk_err(pblk, "failed to erase line %d\n", line->id);
Javier González588726d32017-06-26 11:57:29 +0200992 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200993 }
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200994 } while (1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200995
996 return 0;
997}
998
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200999static void pblk_line_setup_metadata(struct pblk_line *line,
1000 struct pblk_line_mgmt *l_mg,
1001 struct pblk_line_meta *lm)
1002{
1003 int meta_line;
1004
Javier González588726d32017-06-26 11:57:29 +02001005 lockdep_assert_held(&l_mg->free_lock);
1006
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001007retry_meta:
1008 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
1009 if (meta_line == PBLK_DATA_LINES) {
1010 spin_unlock(&l_mg->free_lock);
1011 io_schedule();
1012 spin_lock(&l_mg->free_lock);
1013 goto retry_meta;
1014 }
1015
1016 set_bit(meta_line, &l_mg->meta_bitmap);
1017 line->meta_line = meta_line;
1018
1019 line->smeta = l_mg->sline_meta[meta_line];
1020 line->emeta = l_mg->eline_meta[meta_line];
1021
1022 memset(line->smeta, 0, lm->smeta_len);
1023 memset(line->emeta->buf, 0, lm->emeta_len[0]);
1024
1025 line->emeta->mem = 0;
1026 atomic_set(&line->emeta->sync, 0);
1027}
1028
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001029/* For now lines are always assumed full lines. Thus, smeta former and current
1030 * lun bitmaps are omitted.
1031 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001032static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001033 struct pblk_line *cur)
1034{
1035 struct nvm_tgt_dev *dev = pblk->dev;
1036 struct nvm_geo *geo = &dev->geo;
1037 struct pblk_line_meta *lm = &pblk->lm;
1038 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001039 struct pblk_emeta *emeta = line->emeta;
1040 struct line_emeta *emeta_buf = emeta->buf;
1041 struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001042 int nr_blk_line;
1043
1044 /* After erasing the line, new bad blocks might appear and we risk
1045 * having an invalid line
1046 */
1047 nr_blk_line = lm->blk_per_line -
1048 bitmap_weight(line->blk_bitmap, lm->blk_per_line);
1049 if (nr_blk_line < lm->min_blk_line) {
1050 spin_lock(&l_mg->free_lock);
1051 spin_lock(&line->lock);
1052 line->state = PBLK_LINESTATE_BAD;
Hans Holmbergf2937232018-10-09 13:11:53 +02001053 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1054 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001055 spin_unlock(&line->lock);
1056
1057 list_add_tail(&line->list, &l_mg->bad_list);
1058 spin_unlock(&l_mg->free_lock);
1059
Matias Bjørling4e495a42018-07-13 10:48:42 +02001060 pblk_debug(pblk, "line %d is bad\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001061
1062 return 0;
1063 }
1064
1065 /* Run-time metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001066 line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001067
1068 /* Mark LUNs allocated in this line (all for now) */
1069 bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len);
1070
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001071 smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
1072 memcpy(smeta_buf->header.uuid, pblk->instance_uuid, 16);
1073 smeta_buf->header.id = cpu_to_le32(line->id);
1074 smeta_buf->header.type = cpu_to_le16(line->type);
Hans Holmbergd0ab0b12018-03-30 00:04:51 +02001075 smeta_buf->header.version_major = SMETA_VERSION_MAJOR;
1076 smeta_buf->header.version_minor = SMETA_VERSION_MINOR;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001077
1078 /* Start metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001079 smeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001080 smeta_buf->window_wr_lun = cpu_to_le32(geo->all_luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001081
1082 /* Fill metadata among lines */
1083 if (cur) {
1084 memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001085 smeta_buf->prev_id = cpu_to_le32(cur->id);
1086 cur->emeta->buf->next_id = cpu_to_le32(line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001087 } else {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001088 smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001089 }
1090
1091 /* All smeta must be set at this point */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001092 smeta_buf->header.crc = cpu_to_le32(
1093 pblk_calc_meta_header_crc(pblk, &smeta_buf->header));
1094 smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001095
1096 /* End metadata */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001097 memcpy(&emeta_buf->header, &smeta_buf->header,
1098 sizeof(struct line_header));
Hans Holmbergd0ab0b12018-03-30 00:04:51 +02001099
1100 emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
1101 emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
1102 emeta_buf->header.crc = cpu_to_le32(
1103 pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
1104
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001105 emeta_buf->seq_nr = cpu_to_le64(line->seq_nr);
1106 emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line);
1107 emeta_buf->nr_valid_lbas = cpu_to_le64(0);
1108 emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY);
1109 emeta_buf->crc = cpu_to_le32(0);
1110 emeta_buf->prev_id = smeta_buf->prev_id;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001111
1112 return 1;
1113}
1114
Javier González9cfd5a92018-06-01 16:41:14 +02001115static int pblk_line_alloc_bitmaps(struct pblk *pblk, struct pblk_line *line)
1116{
1117 struct pblk_line_meta *lm = &pblk->lm;
Hans Holmberg53d82db2018-10-09 13:11:47 +02001118 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González9cfd5a92018-06-01 16:41:14 +02001119
Hans Holmberg53d82db2018-10-09 13:11:47 +02001120 line->map_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
Javier González9cfd5a92018-06-01 16:41:14 +02001121 if (!line->map_bitmap)
1122 return -ENOMEM;
1123
Hans Holmberg53d82db2018-10-09 13:11:47 +02001124 memset(line->map_bitmap, 0, lm->sec_bitmap_len);
1125
Javier González9cfd5a92018-06-01 16:41:14 +02001126 /* will be initialized using bb info from map_bitmap */
Hans Holmberg53d82db2018-10-09 13:11:47 +02001127 line->invalid_bitmap = mempool_alloc(l_mg->bitmap_pool, GFP_KERNEL);
Javier González9cfd5a92018-06-01 16:41:14 +02001128 if (!line->invalid_bitmap) {
Hans Holmberg53d82db2018-10-09 13:11:47 +02001129 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier González9cfd5a92018-06-01 16:41:14 +02001130 line->map_bitmap = NULL;
1131 return -ENOMEM;
1132 }
1133
1134 return 0;
1135}
1136
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001137/* For now lines are always assumed full lines. Thus, smeta former and current
1138 * lun bitmaps are omitted.
1139 */
1140static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line,
1141 int init)
1142{
1143 struct nvm_tgt_dev *dev = pblk->dev;
1144 struct nvm_geo *geo = &dev->geo;
1145 struct pblk_line_meta *lm = &pblk->lm;
1146 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001147 u64 off;
1148 int bit = -1;
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001149 int emeta_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001150
1151 line->sec_in_line = lm->sec_per_line;
1152
1153 /* Capture bad block information on line mapping bitmaps */
1154 while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line,
1155 bit + 1)) < lm->blk_per_line) {
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001156 off = bit * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001157 bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off,
1158 lm->sec_per_line);
1159 bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux,
1160 lm->sec_per_line);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001161 line->sec_in_line -= geo->clba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001162 }
1163
1164 /* Mark smeta metadata sectors as bad sectors */
1165 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001166 off = bit * geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001167 bitmap_set(line->map_bitmap, off, lm->smeta_sec);
1168 line->sec_in_line -= lm->smeta_sec;
1169 line->smeta_ssec = off;
1170 line->cur_sec = off + lm->smeta_sec;
1171
Javier Gonzáleze2cddf22017-10-13 14:46:19 +02001172 if (init && pblk_line_submit_smeta_io(pblk, line, off, PBLK_WRITE)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001173 pblk_debug(pblk, "line smeta I/O failed. Retry\n");
Hans Holmberg6cf17a22018-06-01 16:41:07 +02001174 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001175 }
1176
1177 bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line);
1178
1179 /* Mark emeta metadata sectors as bad sectors. We need to consider bad
1180 * blocks to make sure that there are enough sectors to store emeta
1181 */
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001182 emeta_secs = lm->emeta_sec[0];
1183 off = lm->sec_per_line;
1184 while (emeta_secs) {
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001185 off -= geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001186 if (!test_bit(off, line->invalid_bitmap)) {
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001187 bitmap_set(line->invalid_bitmap, off, geo->ws_opt);
1188 emeta_secs -= geo->ws_opt;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001189 }
1190 }
1191
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001192 line->emeta_ssec = off;
Hans Holmbergcfe1c9e2018-03-30 00:04:50 +02001193 line->sec_in_line -= lm->emeta_sec[0];
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001194 line->nr_valid_lbas = 0;
Javier González0880a9a2017-06-26 11:57:19 +02001195 line->left_msecs = line->sec_in_line;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001196 *line->vsc = cpu_to_le32(line->sec_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001197
1198 if (lm->sec_per_line - line->sec_in_line !=
1199 bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) {
1200 spin_lock(&line->lock);
1201 line->state = PBLK_LINESTATE_BAD;
Hans Holmbergf2937232018-10-09 13:11:53 +02001202 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1203 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001204 spin_unlock(&line->lock);
1205
1206 list_add_tail(&line->list, &l_mg->bad_list);
Matias Bjørling4e495a42018-07-13 10:48:42 +02001207 pblk_err(pblk, "unexpected line %d is bad\n", line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001208
1209 return 0;
1210 }
1211
1212 return 1;
1213}
1214
Javier González32ef9412018-03-30 00:05:20 +02001215static int pblk_prepare_new_line(struct pblk *pblk, struct pblk_line *line)
1216{
1217 struct pblk_line_meta *lm = &pblk->lm;
1218 struct nvm_tgt_dev *dev = pblk->dev;
1219 struct nvm_geo *geo = &dev->geo;
1220 int blk_to_erase = atomic_read(&line->blk_in_line);
1221 int i;
1222
1223 for (i = 0; i < lm->blk_per_line; i++) {
1224 struct pblk_lun *rlun = &pblk->luns[i];
1225 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
1226 int state = line->chks[pos].state;
1227
1228 /* Free chunks should not be erased */
1229 if (state & NVM_CHK_ST_FREE) {
1230 set_bit(pblk_ppa_to_pos(geo, rlun->bppa),
1231 line->erase_bitmap);
1232 blk_to_erase--;
1233 }
1234 }
1235
1236 return blk_to_erase;
1237}
1238
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001239static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line)
1240{
1241 struct pblk_line_meta *lm = &pblk->lm;
Javier González1d8b33e2018-06-01 15:04:16 +02001242 int blk_in_line = atomic_read(&line->blk_in_line);
Javier González9cfd5a92018-06-01 16:41:14 +02001243 int blk_to_erase;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001244
Javier González32ef9412018-03-30 00:05:20 +02001245 /* Bad blocks do not need to be erased */
1246 bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line);
1247
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001248 spin_lock(&line->lock);
Javier González32ef9412018-03-30 00:05:20 +02001249
1250 /* If we have not written to this line, we need to mark up free chunks
1251 * as already erased
1252 */
1253 if (line->state == PBLK_LINESTATE_NEW) {
1254 blk_to_erase = pblk_prepare_new_line(pblk, line);
1255 line->state = PBLK_LINESTATE_FREE;
Hans Holmbergf2937232018-10-09 13:11:53 +02001256 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1257 line->state);
Javier González32ef9412018-03-30 00:05:20 +02001258 } else {
Javier González1d8b33e2018-06-01 15:04:16 +02001259 blk_to_erase = blk_in_line;
1260 }
1261
1262 if (blk_in_line < lm->min_blk_line) {
Javier González9cfd5a92018-06-01 16:41:14 +02001263 spin_unlock(&line->lock);
1264 return -EAGAIN;
Javier González32ef9412018-03-30 00:05:20 +02001265 }
1266
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001267 if (line->state != PBLK_LINESTATE_FREE) {
Javier González588726d32017-06-26 11:57:29 +02001268 WARN(1, "pblk: corrupted line %d, state %d\n",
1269 line->id, line->state);
Javier González9cfd5a92018-06-01 16:41:14 +02001270 spin_unlock(&line->lock);
1271 return -EINTR;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001272 }
Javier González588726d32017-06-26 11:57:29 +02001273
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001274 line->state = PBLK_LINESTATE_OPEN;
Hans Holmbergf2937232018-10-09 13:11:53 +02001275 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1276 line->state);
Javier Gonzáleza44f53f2017-04-22 01:32:49 +02001277
Javier González32ef9412018-03-30 00:05:20 +02001278 atomic_set(&line->left_eblks, blk_to_erase);
1279 atomic_set(&line->left_seblks, blk_to_erase);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001280
1281 line->meta_distance = lm->meta_distance;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001282 spin_unlock(&line->lock);
1283
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001284 kref_init(&line->ref);
1285
1286 return 0;
1287}
1288
1289int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line)
1290{
1291 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1292 int ret;
1293
1294 spin_lock(&l_mg->free_lock);
1295 l_mg->data_line = line;
1296 list_del(&line->list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001297
1298 ret = pblk_line_prepare(pblk, line);
1299 if (ret) {
1300 list_add(&line->list, &l_mg->free_list);
Javier González3dc001f2017-04-22 01:32:45 +02001301 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001302 return ret;
1303 }
Javier González3dc001f2017-04-22 01:32:45 +02001304 spin_unlock(&l_mg->free_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001305
Javier González9cfd5a92018-06-01 16:41:14 +02001306 ret = pblk_line_alloc_bitmaps(pblk, line);
1307 if (ret)
1308 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001309
1310 if (!pblk_line_init_bb(pblk, line, 0)) {
1311 list_add(&line->list, &l_mg->free_list);
1312 return -EINTR;
1313 }
1314
Javier González9cfd5a92018-06-01 16:41:14 +02001315 pblk_rl_free_lines_dec(&pblk->rl, line, true);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001316 return 0;
1317}
1318
1319void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line)
1320{
Hans Holmberg53d82db2018-10-09 13:11:47 +02001321 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1322
1323 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001324 line->map_bitmap = NULL;
1325 line->smeta = NULL;
1326 line->emeta = NULL;
1327}
1328
Javier González9cfd5a92018-06-01 16:41:14 +02001329static void pblk_line_reinit(struct pblk_line *line)
1330{
1331 *line->vsc = cpu_to_le32(EMPTY_ENTRY);
1332
1333 line->map_bitmap = NULL;
1334 line->invalid_bitmap = NULL;
1335 line->smeta = NULL;
1336 line->emeta = NULL;
1337}
1338
1339void pblk_line_free(struct pblk_line *line)
1340{
Hans Holmberg53d82db2018-10-09 13:11:47 +02001341 struct pblk *pblk = line->pblk;
1342 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1343
1344 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
1345 mempool_free(line->invalid_bitmap, l_mg->bitmap_pool);
Javier González9cfd5a92018-06-01 16:41:14 +02001346
1347 pblk_line_reinit(line);
1348}
1349
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001350struct pblk_line *pblk_line_get(struct pblk *pblk)
1351{
1352 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1353 struct pblk_line_meta *lm = &pblk->lm;
Javier González588726d32017-06-26 11:57:29 +02001354 struct pblk_line *line;
1355 int ret, bit;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001356
1357 lockdep_assert_held(&l_mg->free_lock);
1358
Javier González588726d32017-06-26 11:57:29 +02001359retry:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001360 if (list_empty(&l_mg->free_list)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001361 pblk_err(pblk, "no free lines\n");
Javier González588726d32017-06-26 11:57:29 +02001362 return NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001363 }
1364
1365 line = list_first_entry(&l_mg->free_list, struct pblk_line, list);
1366 list_del(&line->list);
1367 l_mg->nr_free_lines--;
1368
1369 bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line);
1370 if (unlikely(bit >= lm->blk_per_line)) {
1371 spin_lock(&line->lock);
1372 line->state = PBLK_LINESTATE_BAD;
Hans Holmbergf2937232018-10-09 13:11:53 +02001373 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1374 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001375 spin_unlock(&line->lock);
1376
1377 list_add_tail(&line->list, &l_mg->bad_list);
1378
Matias Bjørling4e495a42018-07-13 10:48:42 +02001379 pblk_debug(pblk, "line %d is bad\n", line->id);
Javier González588726d32017-06-26 11:57:29 +02001380 goto retry;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001381 }
1382
Javier González588726d32017-06-26 11:57:29 +02001383 ret = pblk_line_prepare(pblk, line);
1384 if (ret) {
Javier González1d8b33e2018-06-01 15:04:16 +02001385 switch (ret) {
1386 case -EAGAIN:
1387 list_add(&line->list, &l_mg->bad_list);
1388 goto retry;
1389 case -EINTR:
Javier González588726d32017-06-26 11:57:29 +02001390 list_add(&line->list, &l_mg->corrupt_list);
1391 goto retry;
Javier González1d8b33e2018-06-01 15:04:16 +02001392 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +02001393 pblk_err(pblk, "failed to prepare line %d\n", line->id);
Javier González588726d32017-06-26 11:57:29 +02001394 list_add(&line->list, &l_mg->free_list);
1395 l_mg->nr_free_lines++;
1396 return NULL;
1397 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001398 }
1399
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001400 return line;
1401}
1402
1403static struct pblk_line *pblk_line_retry(struct pblk *pblk,
1404 struct pblk_line *line)
1405{
1406 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1407 struct pblk_line *retry_line;
1408
Javier González588726d32017-06-26 11:57:29 +02001409retry:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001410 spin_lock(&l_mg->free_lock);
1411 retry_line = pblk_line_get(pblk);
1412 if (!retry_line) {
Javier Gonzálezbe388d92017-04-22 01:32:48 +02001413 l_mg->data_line = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001414 spin_unlock(&l_mg->free_lock);
1415 return NULL;
1416 }
1417
Javier González9cfd5a92018-06-01 16:41:14 +02001418 retry_line->map_bitmap = line->map_bitmap;
1419 retry_line->invalid_bitmap = line->invalid_bitmap;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001420 retry_line->smeta = line->smeta;
1421 retry_line->emeta = line->emeta;
1422 retry_line->meta_line = line->meta_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001423
Javier González9cfd5a92018-06-01 16:41:14 +02001424 pblk_line_reinit(line);
1425
Javier González3dc001f2017-04-22 01:32:45 +02001426 l_mg->data_line = retry_line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001427 spin_unlock(&l_mg->free_lock);
1428
Javier Gonzáleza7689932018-01-05 14:16:13 +01001429 pblk_rl_free_lines_dec(&pblk->rl, line, false);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001430
Javier González588726d32017-06-26 11:57:29 +02001431 if (pblk_line_erase(pblk, retry_line))
1432 goto retry;
1433
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001434 return retry_line;
1435}
1436
Javier González588726d32017-06-26 11:57:29 +02001437static void pblk_set_space_limit(struct pblk *pblk)
1438{
1439 struct pblk_rl *rl = &pblk->rl;
1440
1441 atomic_set(&rl->rb_space, 0);
1442}
1443
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001444struct pblk_line *pblk_line_get_first_data(struct pblk *pblk)
1445{
1446 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1447 struct pblk_line *line;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001448
1449 spin_lock(&l_mg->free_lock);
1450 line = pblk_line_get(pblk);
1451 if (!line) {
1452 spin_unlock(&l_mg->free_lock);
1453 return NULL;
1454 }
1455
1456 line->seq_nr = l_mg->d_seq_nr++;
1457 line->type = PBLK_LINETYPE_DATA;
1458 l_mg->data_line = line;
1459
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001460 pblk_line_setup_metadata(line, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001461
1462 /* Allocate next line for preparation */
1463 l_mg->data_next = pblk_line_get(pblk);
Javier González588726d32017-06-26 11:57:29 +02001464 if (!l_mg->data_next) {
1465 /* If we cannot get a new line, we need to stop the pipeline.
1466 * Only allow as many writes in as we can store safely and then
1467 * fail gracefully
1468 */
1469 pblk_set_space_limit(pblk);
1470
1471 l_mg->data_next = NULL;
1472 } else {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001473 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1474 l_mg->data_next->type = PBLK_LINETYPE_DATA;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001475 }
1476 spin_unlock(&l_mg->free_lock);
1477
Javier González9cfd5a92018-06-01 16:41:14 +02001478 if (pblk_line_alloc_bitmaps(pblk, line))
1479 return NULL;
1480
Javier González588726d32017-06-26 11:57:29 +02001481 if (pblk_line_erase(pblk, line)) {
1482 line = pblk_line_retry(pblk, line);
1483 if (!line)
1484 return NULL;
1485 }
1486
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001487retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001488 if (!pblk_line_init_metadata(pblk, line, NULL)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001489 line = pblk_line_retry(pblk, line);
1490 if (!line)
1491 return NULL;
1492
1493 goto retry_setup;
1494 }
1495
1496 if (!pblk_line_init_bb(pblk, line, 1)) {
1497 line = pblk_line_retry(pblk, line);
1498 if (!line)
1499 return NULL;
1500
1501 goto retry_setup;
1502 }
1503
Javier Gonzáleza7689932018-01-05 14:16:13 +01001504 pblk_rl_free_lines_dec(&pblk->rl, line, true);
1505
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001506 return line;
1507}
1508
Matias Bjørlingae14cc02018-10-09 13:11:40 +02001509void pblk_ppa_to_line_put(struct pblk *pblk, struct ppa_addr ppa)
1510{
1511 struct pblk_line *line;
1512
Javier Gonzálezcb216652018-10-09 13:11:42 +02001513 line = pblk_ppa_to_line(pblk, ppa);
Matias Bjørlingae14cc02018-10-09 13:11:40 +02001514 kref_put(&line->ref, pblk_line_put_wq);
1515}
1516
1517void pblk_rq_to_line_put(struct pblk *pblk, struct nvm_rq *rqd)
1518{
1519 struct ppa_addr *ppa_list;
1520 int i;
1521
1522 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
1523
1524 for (i = 0; i < rqd->nr_ppas; i++)
1525 pblk_ppa_to_line_put(pblk, ppa_list[i]);
1526}
1527
Javier González588726d32017-06-26 11:57:29 +02001528static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line)
1529{
1530 lockdep_assert_held(&pblk->l_mg.free_lock);
1531
1532 pblk_set_space_limit(pblk);
1533 pblk->state = PBLK_STATE_STOPPING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001534 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Javier González588726d32017-06-26 11:57:29 +02001535}
1536
Javier González8bd40022017-10-13 14:46:44 +02001537static void pblk_line_close_meta_sync(struct pblk *pblk)
1538{
1539 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1540 struct pblk_line_meta *lm = &pblk->lm;
1541 struct pblk_line *line, *tline;
1542 LIST_HEAD(list);
1543
1544 spin_lock(&l_mg->close_lock);
1545 if (list_empty(&l_mg->emeta_list)) {
1546 spin_unlock(&l_mg->close_lock);
1547 return;
1548 }
1549
1550 list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev);
1551 spin_unlock(&l_mg->close_lock);
1552
1553 list_for_each_entry_safe(line, tline, &list, list) {
1554 struct pblk_emeta *emeta = line->emeta;
1555
1556 while (emeta->mem < lm->emeta_len[0]) {
1557 int ret;
1558
1559 ret = pblk_submit_meta_io(pblk, line);
1560 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001561 pblk_err(pblk, "sync meta line %d failed (%d)\n",
Javier González8bd40022017-10-13 14:46:44 +02001562 line->id, ret);
1563 return;
1564 }
1565 }
1566 }
1567
1568 pblk_wait_for_meta(pblk);
1569 flush_workqueue(pblk->close_wq);
1570}
1571
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001572void __pblk_pipeline_flush(struct pblk *pblk)
Javier González588726d32017-06-26 11:57:29 +02001573{
1574 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1575 int ret;
1576
1577 spin_lock(&l_mg->free_lock);
1578 if (pblk->state == PBLK_STATE_RECOVERING ||
1579 pblk->state == PBLK_STATE_STOPPED) {
1580 spin_unlock(&l_mg->free_lock);
1581 return;
1582 }
1583 pblk->state = PBLK_STATE_RECOVERING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001584 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Javier González588726d32017-06-26 11:57:29 +02001585 spin_unlock(&l_mg->free_lock);
1586
1587 pblk_flush_writer(pblk);
1588 pblk_wait_for_meta(pblk);
1589
1590 ret = pblk_recov_pad(pblk);
1591 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001592 pblk_err(pblk, "could not close data on teardown(%d)\n", ret);
Javier González588726d32017-06-26 11:57:29 +02001593 return;
1594 }
1595
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001596 flush_workqueue(pblk->bb_wq);
Javier González588726d32017-06-26 11:57:29 +02001597 pblk_line_close_meta_sync(pblk);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001598}
1599
1600void __pblk_pipeline_stop(struct pblk *pblk)
1601{
1602 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González588726d32017-06-26 11:57:29 +02001603
1604 spin_lock(&l_mg->free_lock);
1605 pblk->state = PBLK_STATE_STOPPED;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001606 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Javier González588726d32017-06-26 11:57:29 +02001607 l_mg->data_line = NULL;
1608 l_mg->data_next = NULL;
1609 spin_unlock(&l_mg->free_lock);
1610}
1611
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001612void pblk_pipeline_stop(struct pblk *pblk)
1613{
1614 __pblk_pipeline_flush(pblk);
1615 __pblk_pipeline_stop(pblk);
1616}
1617
Javier González21d22872017-10-13 14:46:26 +02001618struct pblk_line *pblk_line_replace_data(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001619{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001620 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González21d22872017-10-13 14:46:26 +02001621 struct pblk_line *cur, *new = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001622 unsigned int left_seblks;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001623
1624 cur = l_mg->data_line;
1625 new = l_mg->data_next;
1626 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001627 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001628 l_mg->data_line = new;
1629
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001630 spin_lock(&l_mg->free_lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001631 pblk_line_setup_metadata(new, l_mg, &pblk->lm);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001632 spin_unlock(&l_mg->free_lock);
1633
Javier González588726d32017-06-26 11:57:29 +02001634retry_erase:
1635 left_seblks = atomic_read(&new->left_seblks);
1636 if (left_seblks) {
1637 /* If line is not fully erased, erase it */
1638 if (atomic_read(&new->left_eblks)) {
1639 if (pblk_line_erase(pblk, new))
Javier González21d22872017-10-13 14:46:26 +02001640 goto out;
Javier González588726d32017-06-26 11:57:29 +02001641 } else {
1642 io_schedule();
1643 }
1644 goto retry_erase;
1645 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001646
Javier González9cfd5a92018-06-01 16:41:14 +02001647 if (pblk_line_alloc_bitmaps(pblk, new))
1648 return NULL;
1649
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001650retry_setup:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001651 if (!pblk_line_init_metadata(pblk, new, cur)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001652 new = pblk_line_retry(pblk, new);
Javier Gonzálezf3236cef2017-04-22 01:32:46 +02001653 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001654 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001655
1656 goto retry_setup;
1657 }
1658
1659 if (!pblk_line_init_bb(pblk, new, 1)) {
1660 new = pblk_line_retry(pblk, new);
1661 if (!new)
Javier González21d22872017-10-13 14:46:26 +02001662 goto out;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001663
1664 goto retry_setup;
1665 }
1666
Javier Gonzáleza7689932018-01-05 14:16:13 +01001667 pblk_rl_free_lines_dec(&pblk->rl, new, true);
1668
Javier González588726d32017-06-26 11:57:29 +02001669 /* Allocate next line for preparation */
1670 spin_lock(&l_mg->free_lock);
1671 l_mg->data_next = pblk_line_get(pblk);
1672 if (!l_mg->data_next) {
1673 /* If we cannot get a new line, we need to stop the pipeline.
1674 * Only allow as many writes in as we can store safely and then
1675 * fail gracefully
1676 */
1677 pblk_stop_writes(pblk, new);
1678 l_mg->data_next = NULL;
1679 } else {
1680 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
1681 l_mg->data_next->type = PBLK_LINETYPE_DATA;
Javier González588726d32017-06-26 11:57:29 +02001682 }
1683 spin_unlock(&l_mg->free_lock);
1684
Javier González21d22872017-10-13 14:46:26 +02001685out:
1686 return new;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001687}
1688
Javier González7bd4d372017-10-13 14:46:23 +02001689static void __pblk_line_put(struct pblk *pblk, struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001690{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001691 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Hans Holmbergd6b992f2017-10-13 14:46:41 +02001692 struct pblk_gc *gc = &pblk->gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001693
1694 spin_lock(&line->lock);
1695 WARN_ON(line->state != PBLK_LINESTATE_GC);
1696 line->state = PBLK_LINESTATE_FREE;
Hans Holmbergf2937232018-10-09 13:11:53 +02001697 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1698 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001699 line->gc_group = PBLK_LINEGC_NONE;
Javier González8e55c072018-06-01 15:04:22 +02001700 pblk_line_free(line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001701
Hans Holmberg48b8d202018-06-01 16:41:06 +02001702 if (line->w_err_gc->has_write_err) {
1703 pblk_rl_werr_line_out(&pblk->rl);
1704 line->w_err_gc->has_write_err = 0;
1705 }
1706
1707 spin_unlock(&line->lock);
Hans Holmbergd6b992f2017-10-13 14:46:41 +02001708 atomic_dec(&gc->pipeline_gc);
1709
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001710 spin_lock(&l_mg->free_lock);
1711 list_add_tail(&line->list, &l_mg->free_list);
1712 l_mg->nr_free_lines++;
1713 spin_unlock(&l_mg->free_lock);
1714
1715 pblk_rl_free_lines_inc(&pblk->rl, line);
1716}
1717
Javier González7bd4d372017-10-13 14:46:23 +02001718static void pblk_line_put_ws(struct work_struct *work)
1719{
1720 struct pblk_line_ws *line_put_ws = container_of(work,
1721 struct pblk_line_ws, ws);
1722 struct pblk *pblk = line_put_ws->pblk;
1723 struct pblk_line *line = line_put_ws->line;
1724
1725 __pblk_line_put(pblk, line);
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001726 mempool_free(line_put_ws, &pblk->gen_ws_pool);
Javier González7bd4d372017-10-13 14:46:23 +02001727}
1728
1729void pblk_line_put(struct kref *ref)
1730{
1731 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1732 struct pblk *pblk = line->pblk;
1733
1734 __pblk_line_put(pblk, line);
1735}
1736
1737void pblk_line_put_wq(struct kref *ref)
1738{
1739 struct pblk_line *line = container_of(ref, struct pblk_line, ref);
1740 struct pblk *pblk = line->pblk;
1741 struct pblk_line_ws *line_put_ws;
1742
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001743 line_put_ws = mempool_alloc(&pblk->gen_ws_pool, GFP_ATOMIC);
Javier González7bd4d372017-10-13 14:46:23 +02001744 if (!line_put_ws)
1745 return;
1746
1747 line_put_ws->pblk = pblk;
1748 line_put_ws->line = line;
1749 line_put_ws->priv = NULL;
1750
1751 INIT_WORK(&line_put_ws->ws, pblk_line_put_ws);
1752 queue_work(pblk->r_end_wq, &line_put_ws->ws);
1753}
1754
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001755int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa)
1756{
1757 struct nvm_rq *rqd;
1758 int err;
1759
Javier González67bf26a2017-10-13 14:46:20 +02001760 rqd = pblk_alloc_rqd(pblk, PBLK_ERASE);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001761
1762 pblk_setup_e_rq(pblk, rqd, ppa);
1763
1764 rqd->end_io = pblk_end_io_erase;
1765 rqd->private = pblk;
1766
Hans Holmberg4209c312018-10-09 13:11:55 +02001767 trace_pblk_chunk_reset(pblk_disk_name(pblk),
1768 &ppa, PBLK_CHUNK_RESET_START);
1769
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001770 /* The write thread schedules erases so that it minimizes disturbances
1771 * with writes. Thus, there is no need to take the LUN semaphore.
1772 */
1773 err = pblk_submit_io(pblk, rqd);
1774 if (err) {
1775 struct nvm_tgt_dev *dev = pblk->dev;
1776 struct nvm_geo *geo = &dev->geo;
1777
Matias Bjørling4e495a42018-07-13 10:48:42 +02001778 pblk_err(pblk, "could not async erase line:%d,blk:%d\n",
Javier Gonzálezcb216652018-10-09 13:11:42 +02001779 pblk_ppa_to_line_id(ppa),
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +01001780 pblk_ppa_to_pos(geo, ppa));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001781 }
1782
1783 return err;
1784}
1785
1786struct pblk_line *pblk_line_get_data(struct pblk *pblk)
1787{
1788 return pblk->l_mg.data_line;
1789}
1790
Javier Gonzálezd624f372017-06-26 11:57:15 +02001791/* For now, always erase next line */
1792struct pblk_line *pblk_line_get_erase(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001793{
1794 return pblk->l_mg.data_next;
1795}
1796
1797int pblk_line_is_full(struct pblk_line *line)
1798{
1799 return (line->left_msecs == 0);
1800}
1801
Javier González588726d32017-06-26 11:57:29 +02001802static void pblk_line_should_sync_meta(struct pblk *pblk)
1803{
1804 if (pblk_rl_is_limit(&pblk->rl))
1805 pblk_line_close_meta_sync(pblk);
1806}
1807
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001808void pblk_line_close(struct pblk *pblk, struct pblk_line *line)
1809{
Javier González32ef9412018-03-30 00:05:20 +02001810 struct nvm_tgt_dev *dev = pblk->dev;
1811 struct nvm_geo *geo = &dev->geo;
1812 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001813 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1814 struct list_head *move_list;
Javier González32ef9412018-03-30 00:05:20 +02001815 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001816
Matias Bjørling880eda52018-07-13 10:48:37 +02001817#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001818 WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line),
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001819 "pblk: corrupt closed line %d\n", line->id);
Javier Gonzáleza84ebb82017-06-30 17:56:43 +02001820#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001821
1822 spin_lock(&l_mg->free_lock);
1823 WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap));
1824 spin_unlock(&l_mg->free_lock);
1825
1826 spin_lock(&l_mg->gc_lock);
1827 spin_lock(&line->lock);
1828 WARN_ON(line->state != PBLK_LINESTATE_OPEN);
1829 line->state = PBLK_LINESTATE_CLOSED;
1830 move_list = pblk_line_gc_list(pblk, line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001831 list_add_tail(&line->list, move_list);
1832
Hans Holmberg53d82db2018-10-09 13:11:47 +02001833 mempool_free(line->map_bitmap, l_mg->bitmap_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001834 line->map_bitmap = NULL;
1835 line->smeta = NULL;
1836 line->emeta = NULL;
1837
Javier González32ef9412018-03-30 00:05:20 +02001838 for (i = 0; i < lm->blk_per_line; i++) {
1839 struct pblk_lun *rlun = &pblk->luns[i];
1840 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
1841 int state = line->chks[pos].state;
1842
1843 if (!(state & NVM_CHK_ST_OFFLINE))
1844 state = NVM_CHK_ST_CLOSED;
1845 }
1846
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001847 spin_unlock(&line->lock);
1848 spin_unlock(&l_mg->gc_lock);
Hans Holmbergf2937232018-10-09 13:11:53 +02001849
1850 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1851 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001852}
1853
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001854void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line)
1855{
1856 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1857 struct pblk_line_meta *lm = &pblk->lm;
1858 struct pblk_emeta *emeta = line->emeta;
1859 struct line_emeta *emeta_buf = emeta->buf;
Hans Holmberg76758392018-03-30 00:04:52 +02001860 struct wa_counters *wa = emeta_to_wa(lm, emeta_buf);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001861
Javier González588726d32017-06-26 11:57:29 +02001862 /* No need for exact vsc value; avoid a big line lock and take aprox. */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001863 memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len);
1864 memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len);
1865
Hans Holmberg76758392018-03-30 00:04:52 +02001866 wa->user = cpu_to_le64(atomic64_read(&pblk->user_wa));
1867 wa->pad = cpu_to_le64(atomic64_read(&pblk->pad_wa));
1868 wa->gc = cpu_to_le64(atomic64_read(&pblk->gc_wa));
1869
Javier González9cc85bc2018-10-09 13:11:45 +02001870 if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC) {
1871 emeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC);
1872 memcpy(emeta_buf->header.uuid, pblk->instance_uuid, 16);
1873 emeta_buf->header.id = cpu_to_le32(line->id);
1874 emeta_buf->header.type = cpu_to_le16(line->type);
1875 emeta_buf->header.version_major = EMETA_VERSION_MAJOR;
1876 emeta_buf->header.version_minor = EMETA_VERSION_MINOR;
1877 emeta_buf->header.crc = cpu_to_le32(
1878 pblk_calc_meta_header_crc(pblk, &emeta_buf->header));
1879 }
1880
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001881 emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas);
1882 emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf));
1883
1884 spin_lock(&l_mg->close_lock);
1885 spin_lock(&line->lock);
Hans Holmberg48b8d202018-06-01 16:41:06 +02001886
1887 /* Update the in-memory start address for emeta, in case it has
1888 * shifted due to write errors
1889 */
1890 if (line->emeta_ssec != line->cur_sec)
1891 line->emeta_ssec = line->cur_sec;
1892
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001893 list_add_tail(&line->list, &l_mg->emeta_list);
1894 spin_unlock(&line->lock);
1895 spin_unlock(&l_mg->close_lock);
Javier González588726d32017-06-26 11:57:29 +02001896
1897 pblk_line_should_sync_meta(pblk);
Hans Holmberg48b8d202018-06-01 16:41:06 +02001898}
1899
1900static void pblk_save_lba_list(struct pblk *pblk, struct pblk_line *line)
1901{
1902 struct pblk_line_meta *lm = &pblk->lm;
1903 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1904 unsigned int lba_list_size = lm->emeta_len[2];
1905 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
1906 struct pblk_emeta *emeta = line->emeta;
1907
1908 w_err_gc->lba_list = pblk_malloc(lba_list_size,
1909 l_mg->emeta_alloc_type, GFP_KERNEL);
1910 memcpy(w_err_gc->lba_list, emeta_to_lbas(pblk, emeta->buf),
1911 lba_list_size);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001912}
1913
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001914void pblk_line_close_ws(struct work_struct *work)
1915{
1916 struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws,
1917 ws);
1918 struct pblk *pblk = line_ws->pblk;
1919 struct pblk_line *line = line_ws->line;
Hans Holmberg48b8d202018-06-01 16:41:06 +02001920 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
1921
1922 /* Write errors makes the emeta start address stored in smeta invalid,
1923 * so keep a copy of the lba list until we've gc'd the line
1924 */
1925 if (w_err_gc->has_write_err)
1926 pblk_save_lba_list(pblk, line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001927
1928 pblk_line_close(pblk, line);
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001929 mempool_free(line_ws, &pblk->gen_ws_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001930}
1931
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +02001932void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv,
1933 void (*work)(struct work_struct *), gfp_t gfp_mask,
Javier Gonzálezef576492017-06-26 11:57:28 +02001934 struct workqueue_struct *wq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001935{
1936 struct pblk_line_ws *line_ws;
1937
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001938 line_ws = mempool_alloc(&pblk->gen_ws_pool, gfp_mask);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001939
1940 line_ws->pblk = pblk;
1941 line_ws->line = line;
1942 line_ws->priv = priv;
1943
1944 INIT_WORK(&line_ws->ws, work);
Javier Gonzálezef576492017-06-26 11:57:28 +02001945 queue_work(wq, &line_ws->ws);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001946}
1947
Matias Bjørling43241cf2018-10-09 13:11:51 +02001948static void __pblk_down_chunk(struct pblk *pblk, int pos)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001949{
Javier González3eaa11e2017-07-07 21:08:52 +02001950 struct pblk_lun *rlun = &pblk->luns[pos];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001951 int ret;
1952
1953 /*
1954 * Only send one inflight I/O per LUN. Since we map at a page
1955 * granurality, all ppas in the I/O will map to the same LUN
1956 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001957
Javier González3eaa11e2017-07-07 21:08:52 +02001958 ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000));
Rakesh Panditc5493842017-10-13 14:45:58 +02001959 if (ret == -ETIME || ret == -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001960 pblk_err(pblk, "taking lun semaphore timed out: err %d\n",
1961 -ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001962}
1963
Matias Bjørling43241cf2018-10-09 13:11:51 +02001964void pblk_down_chunk(struct pblk *pblk, struct ppa_addr ppa)
Javier González3eaa11e2017-07-07 21:08:52 +02001965{
1966 struct nvm_tgt_dev *dev = pblk->dev;
1967 struct nvm_geo *geo = &dev->geo;
Matias Bjørling43241cf2018-10-09 13:11:51 +02001968 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González3eaa11e2017-07-07 21:08:52 +02001969
Matias Bjørling43241cf2018-10-09 13:11:51 +02001970 __pblk_down_chunk(pblk, pos);
Javier González3eaa11e2017-07-07 21:08:52 +02001971}
1972
Matias Bjørling43241cf2018-10-09 13:11:51 +02001973void pblk_down_rq(struct pblk *pblk, struct ppa_addr ppa,
Javier González3eaa11e2017-07-07 21:08:52 +02001974 unsigned long *lun_bitmap)
1975{
1976 struct nvm_tgt_dev *dev = pblk->dev;
1977 struct nvm_geo *geo = &dev->geo;
Matias Bjørling43241cf2018-10-09 13:11:51 +02001978 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González3eaa11e2017-07-07 21:08:52 +02001979
1980 /* If the LUN has been locked for this same request, do no attempt to
1981 * lock it again
1982 */
1983 if (test_and_set_bit(pos, lun_bitmap))
1984 return;
1985
Matias Bjørling43241cf2018-10-09 13:11:51 +02001986 __pblk_down_chunk(pblk, pos);
Javier González3eaa11e2017-07-07 21:08:52 +02001987}
1988
Matias Bjørling43241cf2018-10-09 13:11:51 +02001989void pblk_up_chunk(struct pblk *pblk, struct ppa_addr ppa)
Javier González3eaa11e2017-07-07 21:08:52 +02001990{
1991 struct nvm_tgt_dev *dev = pblk->dev;
1992 struct nvm_geo *geo = &dev->geo;
1993 struct pblk_lun *rlun;
Matias Bjørling43241cf2018-10-09 13:11:51 +02001994 int pos = pblk_ppa_to_pos(geo, ppa);
Javier González3eaa11e2017-07-07 21:08:52 +02001995
1996 rlun = &pblk->luns[pos];
1997 up(&rlun->wr_sem);
1998}
1999
Hans Holmberge99e8022018-10-09 13:11:48 +02002000void pblk_up_rq(struct pblk *pblk, unsigned long *lun_bitmap)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002001{
2002 struct nvm_tgt_dev *dev = pblk->dev;
2003 struct nvm_geo *geo = &dev->geo;
2004 struct pblk_lun *rlun;
Javier Gonzáleza40afad2018-03-30 00:05:14 +02002005 int num_lun = geo->all_luns;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002006 int bit = -1;
2007
Javier Gonzáleza40afad2018-03-30 00:05:14 +02002008 while ((bit = find_next_bit(lun_bitmap, num_lun, bit + 1)) < num_lun) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002009 rlun = &pblk->luns[bit];
2010 up(&rlun->wr_sem);
2011 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002012}
2013
2014void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
2015{
Javier González9f6cb132017-10-13 14:46:12 +02002016 struct ppa_addr ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002017
2018 /* logic error: lba out-of-bounds. Ignore update */
2019 if (!(lba < pblk->rl.nr_secs)) {
2020 WARN(1, "pblk: corrupted L2P map request\n");
2021 return;
2022 }
2023
2024 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02002025 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002026
Javier González9f6cb132017-10-13 14:46:12 +02002027 if (!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p))
2028 pblk_map_invalidate(pblk, ppa_l2p);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002029
2030 pblk_trans_map_set(pblk, lba, ppa);
2031 spin_unlock(&pblk->trans_lock);
2032}
2033
2034void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa)
2035{
Javier Gonzálezd3401212017-10-13 14:46:14 +02002036
Matias Bjørling880eda52018-07-13 10:48:37 +02002037#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002038 /* Callers must ensure that the ppa points to a cache address */
2039 BUG_ON(!pblk_addr_in_cache(ppa));
2040 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa)));
2041#endif
2042
2043 pblk_update_map(pblk, lba, ppa);
2044}
2045
Javier González9f6cb132017-10-13 14:46:12 +02002046int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa_new,
Javier Gonzálezd3401212017-10-13 14:46:14 +02002047 struct pblk_line *gc_line, u64 paddr_gc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002048{
Javier Gonzálezd3401212017-10-13 14:46:14 +02002049 struct ppa_addr ppa_l2p, ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002050 int ret = 1;
2051
Matias Bjørling880eda52018-07-13 10:48:37 +02002052#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002053 /* Callers must ensure that the ppa points to a cache address */
Javier González9f6cb132017-10-13 14:46:12 +02002054 BUG_ON(!pblk_addr_in_cache(ppa_new));
2055 BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa_new)));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002056#endif
2057
2058 /* logic error: lba out-of-bounds. Ignore update */
2059 if (!(lba < pblk->rl.nr_secs)) {
2060 WARN(1, "pblk: corrupted L2P map request\n");
2061 return 0;
2062 }
2063
2064 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02002065 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzálezd3401212017-10-13 14:46:14 +02002066 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, gc_line->id);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002067
Javier Gonzálezd3401212017-10-13 14:46:14 +02002068 if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) {
2069 spin_lock(&gc_line->lock);
2070 WARN(!test_bit(paddr_gc, gc_line->invalid_bitmap),
2071 "pblk: corrupted GC update");
2072 spin_unlock(&gc_line->lock);
Javier González9f6cb132017-10-13 14:46:12 +02002073
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002074 ret = 0;
2075 goto out;
2076 }
2077
Javier González9f6cb132017-10-13 14:46:12 +02002078 pblk_trans_map_set(pblk, lba, ppa_new);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002079out:
2080 spin_unlock(&pblk->trans_lock);
2081 return ret;
2082}
2083
Javier González9f6cb132017-10-13 14:46:12 +02002084void pblk_update_map_dev(struct pblk *pblk, sector_t lba,
2085 struct ppa_addr ppa_mapped, struct ppa_addr ppa_cache)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002086{
Javier González9f6cb132017-10-13 14:46:12 +02002087 struct ppa_addr ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002088
Matias Bjørling880eda52018-07-13 10:48:37 +02002089#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002090 /* Callers must ensure that the ppa points to a device address */
Javier González9f6cb132017-10-13 14:46:12 +02002091 BUG_ON(pblk_addr_in_cache(ppa_mapped));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002092#endif
2093 /* Invalidate and discard padded entries */
2094 if (lba == ADDR_EMPTY) {
Hans Holmberg76758392018-03-30 00:04:52 +02002095 atomic64_inc(&pblk->pad_wa);
Matias Bjørling880eda52018-07-13 10:48:37 +02002096#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002097 atomic_long_inc(&pblk->padded_wb);
2098#endif
Javier González9f6cb132017-10-13 14:46:12 +02002099 if (!pblk_ppa_empty(ppa_mapped))
2100 pblk_map_invalidate(pblk, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002101 return;
2102 }
2103
2104 /* logic error: lba out-of-bounds. Ignore update */
2105 if (!(lba < pblk->rl.nr_secs)) {
2106 WARN(1, "pblk: corrupted L2P map request\n");
2107 return;
2108 }
2109
2110 spin_lock(&pblk->trans_lock);
Javier González9f6cb132017-10-13 14:46:12 +02002111 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002112
2113 /* Do not update L2P if the cacheline has been updated. In this case,
2114 * the mapped ppa must be invalidated
2115 */
Javier González9f6cb132017-10-13 14:46:12 +02002116 if (!pblk_ppa_comp(ppa_l2p, ppa_cache)) {
2117 if (!pblk_ppa_empty(ppa_mapped))
2118 pblk_map_invalidate(pblk, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002119 goto out;
2120 }
2121
Matias Bjørling880eda52018-07-13 10:48:37 +02002122#ifdef CONFIG_NVM_PBLK_DEBUG
Javier González9f6cb132017-10-13 14:46:12 +02002123 WARN_ON(!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002124#endif
2125
Javier González9f6cb132017-10-13 14:46:12 +02002126 pblk_trans_map_set(pblk, lba, ppa_mapped);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002127out:
2128 spin_unlock(&pblk->trans_lock);
2129}
2130
2131void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas,
2132 sector_t blba, int nr_secs)
2133{
2134 int i;
2135
2136 spin_lock(&pblk->trans_lock);
Javier González7bd4d372017-10-13 14:46:23 +02002137 for (i = 0; i < nr_secs; i++) {
2138 struct ppa_addr ppa;
2139
2140 ppa = ppas[i] = pblk_trans_map_get(pblk, blba + i);
2141
2142 /* If the L2P entry maps to a line, the reference is valid */
2143 if (!pblk_ppa_empty(ppa) && !pblk_addr_in_cache(ppa)) {
Javier Gonzálezcb216652018-10-09 13:11:42 +02002144 struct pblk_line *line = pblk_ppa_to_line(pblk, ppa);
Javier González7bd4d372017-10-13 14:46:23 +02002145
2146 kref_get(&line->ref);
2147 }
2148 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002149 spin_unlock(&pblk->trans_lock);
2150}
2151
2152void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas,
2153 u64 *lba_list, int nr_secs)
2154{
Javier Gonzálezd3401212017-10-13 14:46:14 +02002155 u64 lba;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002156 int i;
2157
2158 spin_lock(&pblk->trans_lock);
2159 for (i = 0; i < nr_secs; i++) {
2160 lba = lba_list[i];
Javier Gonzálezd3401212017-10-13 14:46:14 +02002161 if (lba != ADDR_EMPTY) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002162 /* logic error: lba out-of-bounds. Ignore update */
2163 if (!(lba < pblk->rl.nr_secs)) {
2164 WARN(1, "pblk: corrupted L2P map request\n");
2165 continue;
2166 }
2167 ppas[i] = pblk_trans_map_get(pblk, lba);
2168 }
2169 }
2170 spin_unlock(&pblk->trans_lock);
2171}