blob: 7e1c314f2766b19b25da8c1f9a61d9fbb02fe1d5 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial: Javier Gonzalez <javier@cnexlabs.com>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version
7 * 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * pblk-recovery.c - pblk's recovery path
15 */
16
17#include "pblk.h"
18
19void pblk_submit_rec(struct work_struct *work)
20{
21 struct pblk_rec_ctx *recovery =
22 container_of(work, struct pblk_rec_ctx, ws_rec);
23 struct pblk *pblk = recovery->pblk;
24 struct nvm_tgt_dev *dev = pblk->dev;
25 struct nvm_rq *rqd = recovery->rqd;
26 struct pblk_c_ctx *c_ctx = nvm_rq_to_pdu(rqd);
27 int max_secs = nvm_max_phys_sects(dev);
28 struct bio *bio;
29 unsigned int nr_rec_secs;
30 unsigned int pgs_read;
31 int ret;
32
33 nr_rec_secs = bitmap_weight((unsigned long int *)&rqd->ppa_status,
34 max_secs);
35
36 bio = bio_alloc(GFP_KERNEL, nr_rec_secs);
37 if (!bio) {
38 pr_err("pblk: not able to create recovery bio\n");
39 return;
40 }
41
42 bio->bi_iter.bi_sector = 0;
43 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
44 rqd->bio = bio;
45 rqd->nr_ppas = nr_rec_secs;
46
47 pgs_read = pblk_rb_read_to_bio_list(&pblk->rwb, bio, &recovery->failed,
48 nr_rec_secs);
49 if (pgs_read != nr_rec_secs) {
50 pr_err("pblk: could not read recovery entries\n");
51 goto err;
52 }
53
54 if (pblk_setup_w_rec_rq(pblk, rqd, c_ctx)) {
55 pr_err("pblk: could not setup recovery request\n");
56 goto err;
57 }
58
59#ifdef CONFIG_NVM_DEBUG
60 atomic_long_add(nr_rec_secs, &pblk->recov_writes);
61#endif
62
63 ret = pblk_submit_io(pblk, rqd);
64 if (ret) {
65 pr_err("pblk: I/O submission failed: %d\n", ret);
66 goto err;
67 }
68
69 mempool_free(recovery, pblk->rec_pool);
70 return;
71
72err:
73 bio_put(bio);
74 pblk_free_rqd(pblk, rqd, WRITE);
75}
76
77int pblk_recov_setup_rq(struct pblk *pblk, struct pblk_c_ctx *c_ctx,
78 struct pblk_rec_ctx *recovery, u64 *comp_bits,
79 unsigned int comp)
80{
81 struct nvm_tgt_dev *dev = pblk->dev;
82 int max_secs = nvm_max_phys_sects(dev);
83 struct nvm_rq *rec_rqd;
84 struct pblk_c_ctx *rec_ctx;
85 int nr_entries = c_ctx->nr_valid + c_ctx->nr_padded;
86
87 rec_rqd = pblk_alloc_rqd(pblk, WRITE);
88 if (IS_ERR(rec_rqd)) {
89 pr_err("pblk: could not create recovery req.\n");
90 return -ENOMEM;
91 }
92
93 rec_ctx = nvm_rq_to_pdu(rec_rqd);
94
95 /* Copy completion bitmap, but exclude the first X completed entries */
96 bitmap_shift_right((unsigned long int *)&rec_rqd->ppa_status,
97 (unsigned long int *)comp_bits,
98 comp, max_secs);
99
100 /* Save the context for the entries that need to be re-written and
101 * update current context with the completed entries.
102 */
103 rec_ctx->sentry = pblk_rb_wrap_pos(&pblk->rwb, c_ctx->sentry + comp);
104 if (comp >= c_ctx->nr_valid) {
105 rec_ctx->nr_valid = 0;
106 rec_ctx->nr_padded = nr_entries - comp;
107
108 c_ctx->nr_padded = comp - c_ctx->nr_valid;
109 } else {
110 rec_ctx->nr_valid = c_ctx->nr_valid - comp;
111 rec_ctx->nr_padded = c_ctx->nr_padded;
112
113 c_ctx->nr_valid = comp;
114 c_ctx->nr_padded = 0;
115 }
116
117 recovery->rqd = rec_rqd;
118 recovery->pblk = pblk;
119
120 return 0;
121}
122
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200123__le64 *pblk_recov_get_lba_list(struct pblk *pblk, struct line_emeta *emeta_buf)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200124{
125 u32 crc;
126
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200127 crc = pblk_calc_emeta_crc(pblk, emeta_buf);
128 if (le32_to_cpu(emeta_buf->crc) != crc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200129 return NULL;
130
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200131 if (le32_to_cpu(emeta_buf->header.identifier) != PBLK_MAGIC)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200132 return NULL;
133
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200134 return emeta_to_lbas(pblk, emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200135}
136
137static int pblk_recov_l2p_from_emeta(struct pblk *pblk, struct pblk_line *line)
138{
139 struct nvm_tgt_dev *dev = pblk->dev;
140 struct nvm_geo *geo = &dev->geo;
141 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200142 struct pblk_emeta *emeta = line->emeta;
143 struct line_emeta *emeta_buf = emeta->buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200144 __le64 *lba_list;
145 int data_start;
146 int nr_data_lbas, nr_valid_lbas, nr_lbas = 0;
147 int i;
148
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200149 lba_list = pblk_recov_get_lba_list(pblk, emeta_buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200150 if (!lba_list)
151 return 1;
152
153 data_start = pblk_line_smeta_start(pblk, line) + lm->smeta_sec;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200154 nr_data_lbas = lm->sec_per_line - lm->emeta_sec[0];
155 nr_valid_lbas = le64_to_cpu(emeta_buf->nr_valid_lbas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200156
157 for (i = data_start; i < nr_data_lbas && nr_lbas < nr_valid_lbas; i++) {
158 struct ppa_addr ppa;
159 int pos;
160
161 ppa = addr_to_pblk_ppa(pblk, i, line->id);
162 pos = pblk_ppa_to_pos(geo, ppa);
163
164 /* Do not update bad blocks */
165 if (test_bit(pos, line->blk_bitmap))
166 continue;
167
168 if (le64_to_cpu(lba_list[i]) == ADDR_EMPTY) {
169 spin_lock(&line->lock);
170 if (test_and_set_bit(i, line->invalid_bitmap))
Dan Carpenter2a79efd2017-04-15 20:55:52 +0200171 WARN_ONCE(1, "pblk: rec. double invalidate:\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200172 else
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200173 le32_add_cpu(line->vsc, -1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200174 spin_unlock(&line->lock);
175
176 continue;
177 }
178
179 pblk_update_map(pblk, le64_to_cpu(lba_list[i]), ppa);
180 nr_lbas++;
181 }
182
183 if (nr_valid_lbas != nr_lbas)
184 pr_err("pblk: line %d - inconsistent lba list(%llu/%d)\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200185 line->id, emeta_buf->nr_valid_lbas, nr_lbas);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200186
187 line->left_msecs = 0;
188
189 return 0;
190}
191
192static int pblk_calc_sec_in_line(struct pblk *pblk, struct pblk_line *line)
193{
194 struct nvm_tgt_dev *dev = pblk->dev;
195 struct nvm_geo *geo = &dev->geo;
196 struct pblk_line_meta *lm = &pblk->lm;
197 int nr_bb = bitmap_weight(line->blk_bitmap, lm->blk_per_line);
198
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200199 return lm->sec_per_line - lm->smeta_sec - lm->emeta_sec[0] -
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200200 nr_bb * geo->sec_per_blk;
201}
202
203struct pblk_recov_alloc {
204 struct ppa_addr *ppa_list;
205 struct pblk_sec_meta *meta_list;
206 struct nvm_rq *rqd;
207 void *data;
208 dma_addr_t dma_ppa_list;
209 dma_addr_t dma_meta_list;
210};
211
212static int pblk_recov_read_oob(struct pblk *pblk, struct pblk_line *line,
213 struct pblk_recov_alloc p, u64 r_ptr)
214{
215 struct nvm_tgt_dev *dev = pblk->dev;
216 struct nvm_geo *geo = &dev->geo;
217 struct ppa_addr *ppa_list;
218 struct pblk_sec_meta *meta_list;
219 struct nvm_rq *rqd;
220 struct bio *bio;
221 void *data;
222 dma_addr_t dma_ppa_list, dma_meta_list;
223 u64 r_ptr_int;
224 int left_ppas;
225 int rq_ppas, rq_len;
226 int i, j;
227 int ret = 0;
228 DECLARE_COMPLETION_ONSTACK(wait);
229
230 ppa_list = p.ppa_list;
231 meta_list = p.meta_list;
232 rqd = p.rqd;
233 data = p.data;
234 dma_ppa_list = p.dma_ppa_list;
235 dma_meta_list = p.dma_meta_list;
236
237 left_ppas = line->cur_sec - r_ptr;
238 if (!left_ppas)
239 return 0;
240
241 r_ptr_int = r_ptr;
242
243next_read_rq:
Javier González084ec9b2017-06-26 16:27:13 -0600244 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200245
246 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
247 if (!rq_ppas)
248 rq_ppas = pblk->min_write_pgs;
249 rq_len = rq_ppas * geo->sec_size;
250
251 bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL);
252 if (IS_ERR(bio))
253 return PTR_ERR(bio);
254
255 bio->bi_iter.bi_sector = 0; /* internal bio */
256 bio_set_op_attrs(bio, REQ_OP_READ, 0);
257
258 rqd->bio = bio;
259 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200260 rqd->meta_list = meta_list;
261 rqd->nr_ppas = rq_ppas;
262 rqd->ppa_list = ppa_list;
263 rqd->dma_ppa_list = dma_ppa_list;
264 rqd->dma_meta_list = dma_meta_list;
265 rqd->end_io = pblk_end_io_sync;
266 rqd->private = &wait;
267
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200268 if (pblk_io_aligned(pblk, rq_ppas))
269 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
270 else
271 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
272
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200273 for (i = 0; i < rqd->nr_ppas; ) {
274 struct ppa_addr ppa;
275 int pos;
276
277 ppa = addr_to_gen_ppa(pblk, r_ptr_int, line->id);
278 pos = pblk_dev_ppa_to_pos(geo, ppa);
279
280 while (test_bit(pos, line->blk_bitmap)) {
281 r_ptr_int += pblk->min_write_pgs;
282 ppa = addr_to_gen_ppa(pblk, r_ptr_int, line->id);
283 pos = pblk_dev_ppa_to_pos(geo, ppa);
284 }
285
286 for (j = 0; j < pblk->min_write_pgs; j++, i++, r_ptr_int++)
287 rqd->ppa_list[i] =
288 addr_to_gen_ppa(pblk, r_ptr_int, line->id);
289 }
290
291 /* If read fails, more padding is needed */
292 ret = pblk_submit_io(pblk, rqd);
293 if (ret) {
294 pr_err("pblk: I/O submission failed: %d\n", ret);
295 return ret;
296 }
297
298 if (!wait_for_completion_io_timeout(&wait,
299 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
300 pr_err("pblk: L2P recovery read timed out\n");
301 return -EINTR;
302 }
Javier González588726d32017-06-26 11:57:29 +0200303 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200304 reinit_completion(&wait);
305
306 /* At this point, the read should not fail. If it does, it is a problem
307 * we cannot recover from here. Need FTL log.
308 */
309 if (rqd->error) {
310 pr_err("pblk: L2P recovery failed (%d)\n", rqd->error);
311 return -EINTR;
312 }
313
314 for (i = 0; i < rqd->nr_ppas; i++) {
315 u64 lba = le64_to_cpu(meta_list[i].lba);
316
317 if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs)
318 continue;
319
320 pblk_update_map(pblk, lba, rqd->ppa_list[i]);
321 }
322
323 left_ppas -= rq_ppas;
324 if (left_ppas > 0)
325 goto next_read_rq;
326
327 return 0;
328}
329
330static int pblk_recov_pad_oob(struct pblk *pblk, struct pblk_line *line,
331 struct pblk_recov_alloc p, int left_ppas)
332{
333 struct nvm_tgt_dev *dev = pblk->dev;
334 struct nvm_geo *geo = &dev->geo;
335 struct ppa_addr *ppa_list;
336 struct pblk_sec_meta *meta_list;
337 struct nvm_rq *rqd;
338 struct bio *bio;
339 void *data;
340 dma_addr_t dma_ppa_list, dma_meta_list;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200341 __le64 *lba_list = emeta_to_lbas(pblk, line->emeta->buf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200342 u64 w_ptr = line->cur_sec;
343 int left_line_ppas = line->left_msecs;
344 int rq_ppas, rq_len;
345 int i, j;
346 int ret = 0;
347 DECLARE_COMPLETION_ONSTACK(wait);
348
349 ppa_list = p.ppa_list;
350 meta_list = p.meta_list;
351 rqd = p.rqd;
352 data = p.data;
353 dma_ppa_list = p.dma_ppa_list;
354 dma_meta_list = p.dma_meta_list;
355
356next_pad_rq:
357 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
358 if (!rq_ppas)
359 rq_ppas = pblk->min_write_pgs;
360 rq_len = rq_ppas * geo->sec_size;
361
362 bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL);
363 if (IS_ERR(bio))
364 return PTR_ERR(bio);
365
366 bio->bi_iter.bi_sector = 0; /* internal bio */
367 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
368
Javier González084ec9b2017-06-26 16:27:13 -0600369 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200370
371 rqd->bio = bio;
372 rqd->opcode = NVM_OP_PWRITE;
373 rqd->flags = pblk_set_progr_mode(pblk, WRITE);
374 rqd->meta_list = meta_list;
375 rqd->nr_ppas = rq_ppas;
376 rqd->ppa_list = ppa_list;
377 rqd->dma_ppa_list = dma_ppa_list;
378 rqd->dma_meta_list = dma_meta_list;
379 rqd->end_io = pblk_end_io_sync;
380 rqd->private = &wait;
381
382 for (i = 0; i < rqd->nr_ppas; ) {
383 struct ppa_addr ppa;
384 int pos;
385
386 w_ptr = pblk_alloc_page(pblk, line, pblk->min_write_pgs);
387 ppa = addr_to_pblk_ppa(pblk, w_ptr, line->id);
388 pos = pblk_ppa_to_pos(geo, ppa);
389
390 while (test_bit(pos, line->blk_bitmap)) {
391 w_ptr += pblk->min_write_pgs;
392 ppa = addr_to_pblk_ppa(pblk, w_ptr, line->id);
393 pos = pblk_ppa_to_pos(geo, ppa);
394 }
395
396 for (j = 0; j < pblk->min_write_pgs; j++, i++, w_ptr++) {
397 struct ppa_addr dev_ppa;
Javier Gonzálezf417aa02017-06-30 17:56:34 +0200398 __le64 addr_empty = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200399
400 dev_ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
401
402 pblk_map_invalidate(pblk, dev_ppa);
Javier Gonzálezcaa69fa2017-06-26 11:57:12 +0200403 lba_list[w_ptr] = meta_list[i].lba = addr_empty;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200404 rqd->ppa_list[i] = dev_ppa;
405 }
406 }
407
408 ret = pblk_submit_io(pblk, rqd);
409 if (ret) {
410 pr_err("pblk: I/O submission failed: %d\n", ret);
411 return ret;
412 }
413
414 if (!wait_for_completion_io_timeout(&wait,
415 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
416 pr_err("pblk: L2P recovery write timed out\n");
417 }
Javier González588726d32017-06-26 11:57:29 +0200418 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200419 reinit_completion(&wait);
420
421 left_line_ppas -= rq_ppas;
422 left_ppas -= rq_ppas;
423 if (left_ppas > 0 && left_line_ppas)
424 goto next_pad_rq;
425
426 return 0;
427}
428
429/* When this function is called, it means that not all upper pages have been
430 * written in a page that contains valid data. In order to recover this data, we
431 * first find the write pointer on the device, then we pad all necessary
432 * sectors, and finally attempt to read the valid data
433 */
434static int pblk_recov_scan_all_oob(struct pblk *pblk, struct pblk_line *line,
435 struct pblk_recov_alloc p)
436{
437 struct nvm_tgt_dev *dev = pblk->dev;
438 struct nvm_geo *geo = &dev->geo;
439 struct ppa_addr *ppa_list;
440 struct pblk_sec_meta *meta_list;
441 struct nvm_rq *rqd;
442 struct bio *bio;
443 void *data;
444 dma_addr_t dma_ppa_list, dma_meta_list;
445 u64 w_ptr = 0, r_ptr;
446 int rq_ppas, rq_len;
447 int i, j;
448 int ret = 0;
449 int rec_round;
450 int left_ppas = pblk_calc_sec_in_line(pblk, line) - line->cur_sec;
451 DECLARE_COMPLETION_ONSTACK(wait);
452
453 ppa_list = p.ppa_list;
454 meta_list = p.meta_list;
455 rqd = p.rqd;
456 data = p.data;
457 dma_ppa_list = p.dma_ppa_list;
458 dma_meta_list = p.dma_meta_list;
459
460 /* we could recover up until the line write pointer */
461 r_ptr = line->cur_sec;
462 rec_round = 0;
463
464next_rq:
Javier González084ec9b2017-06-26 16:27:13 -0600465 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200466
467 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
468 if (!rq_ppas)
469 rq_ppas = pblk->min_write_pgs;
470 rq_len = rq_ppas * geo->sec_size;
471
472 bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL);
473 if (IS_ERR(bio))
474 return PTR_ERR(bio);
475
476 bio->bi_iter.bi_sector = 0; /* internal bio */
477 bio_set_op_attrs(bio, REQ_OP_READ, 0);
478
479 rqd->bio = bio;
480 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200481 rqd->meta_list = meta_list;
482 rqd->nr_ppas = rq_ppas;
483 rqd->ppa_list = ppa_list;
484 rqd->dma_ppa_list = dma_ppa_list;
485 rqd->dma_meta_list = dma_meta_list;
486 rqd->end_io = pblk_end_io_sync;
487 rqd->private = &wait;
488
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200489 if (pblk_io_aligned(pblk, rq_ppas))
490 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
491 else
492 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
493
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494 for (i = 0; i < rqd->nr_ppas; ) {
495 struct ppa_addr ppa;
496 int pos;
497
498 w_ptr = pblk_alloc_page(pblk, line, pblk->min_write_pgs);
499 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
500 pos = pblk_dev_ppa_to_pos(geo, ppa);
501
502 while (test_bit(pos, line->blk_bitmap)) {
503 w_ptr += pblk->min_write_pgs;
504 ppa = addr_to_gen_ppa(pblk, w_ptr, line->id);
505 pos = pblk_dev_ppa_to_pos(geo, ppa);
506 }
507
508 for (j = 0; j < pblk->min_write_pgs; j++, i++, w_ptr++)
509 rqd->ppa_list[i] =
510 addr_to_gen_ppa(pblk, w_ptr, line->id);
511 }
512
513 ret = pblk_submit_io(pblk, rqd);
514 if (ret) {
515 pr_err("pblk: I/O submission failed: %d\n", ret);
516 return ret;
517 }
518
519 if (!wait_for_completion_io_timeout(&wait,
520 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
521 pr_err("pblk: L2P recovery read timed out\n");
522 }
Javier González588726d32017-06-26 11:57:29 +0200523 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200524 reinit_completion(&wait);
525
526 /* This should not happen since the read failed during normal recovery,
527 * but the media works funny sometimes...
528 */
529 if (!rec_round++ && !rqd->error) {
530 rec_round = 0;
531 for (i = 0; i < rqd->nr_ppas; i++, r_ptr++) {
532 u64 lba = le64_to_cpu(meta_list[i].lba);
533
534 if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs)
535 continue;
536
537 pblk_update_map(pblk, lba, rqd->ppa_list[i]);
538 }
539 }
540
541 /* Reached the end of the written line */
542 if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) {
543 int pad_secs, nr_error_bits, bit;
544 int ret;
545
546 bit = find_first_bit((void *)&rqd->ppa_status, rqd->nr_ppas);
547 nr_error_bits = rqd->nr_ppas - bit;
548
549 /* Roll back failed sectors */
550 line->cur_sec -= nr_error_bits;
551 line->left_msecs += nr_error_bits;
552 bitmap_clear(line->map_bitmap, line->cur_sec, nr_error_bits);
553
554 pad_secs = pblk_pad_distance(pblk);
555 if (pad_secs > line->left_msecs)
556 pad_secs = line->left_msecs;
557
558 ret = pblk_recov_pad_oob(pblk, line, p, pad_secs);
559 if (ret)
560 pr_err("pblk: OOB padding failed (err:%d)\n", ret);
561
562 ret = pblk_recov_read_oob(pblk, line, p, r_ptr);
563 if (ret)
564 pr_err("pblk: OOB read failed (err:%d)\n", ret);
565
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200566 left_ppas = 0;
567 }
568
569 left_ppas -= rq_ppas;
570 if (left_ppas > 0)
571 goto next_rq;
572
573 return ret;
574}
575
576static int pblk_recov_scan_oob(struct pblk *pblk, struct pblk_line *line,
577 struct pblk_recov_alloc p, int *done)
578{
579 struct nvm_tgt_dev *dev = pblk->dev;
580 struct nvm_geo *geo = &dev->geo;
581 struct ppa_addr *ppa_list;
582 struct pblk_sec_meta *meta_list;
583 struct nvm_rq *rqd;
584 struct bio *bio;
585 void *data;
586 dma_addr_t dma_ppa_list, dma_meta_list;
587 u64 paddr;
588 int rq_ppas, rq_len;
589 int i, j;
590 int ret = 0;
591 int left_ppas = pblk_calc_sec_in_line(pblk, line);
592 DECLARE_COMPLETION_ONSTACK(wait);
593
594 ppa_list = p.ppa_list;
595 meta_list = p.meta_list;
596 rqd = p.rqd;
597 data = p.data;
598 dma_ppa_list = p.dma_ppa_list;
599 dma_meta_list = p.dma_meta_list;
600
601 *done = 1;
602
603next_rq:
Javier González084ec9b2017-06-26 16:27:13 -0600604 memset(rqd, 0, pblk_g_rq_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200605
606 rq_ppas = pblk_calc_secs(pblk, left_ppas, 0);
607 if (!rq_ppas)
608 rq_ppas = pblk->min_write_pgs;
609 rq_len = rq_ppas * geo->sec_size;
610
611 bio = bio_map_kern(dev->q, data, rq_len, GFP_KERNEL);
612 if (IS_ERR(bio))
613 return PTR_ERR(bio);
614
615 bio->bi_iter.bi_sector = 0; /* internal bio */
616 bio_set_op_attrs(bio, REQ_OP_READ, 0);
617
618 rqd->bio = bio;
619 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200620 rqd->meta_list = meta_list;
621 rqd->nr_ppas = rq_ppas;
622 rqd->ppa_list = ppa_list;
623 rqd->dma_ppa_list = dma_ppa_list;
624 rqd->dma_meta_list = dma_meta_list;
625 rqd->end_io = pblk_end_io_sync;
626 rqd->private = &wait;
627
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200628 if (pblk_io_aligned(pblk, rq_ppas))
629 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
630 else
631 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
632
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200633 for (i = 0; i < rqd->nr_ppas; ) {
634 struct ppa_addr ppa;
635 int pos;
636
637 paddr = pblk_alloc_page(pblk, line, pblk->min_write_pgs);
638 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
639 pos = pblk_dev_ppa_to_pos(geo, ppa);
640
641 while (test_bit(pos, line->blk_bitmap)) {
642 paddr += pblk->min_write_pgs;
643 ppa = addr_to_gen_ppa(pblk, paddr, line->id);
644 pos = pblk_dev_ppa_to_pos(geo, ppa);
645 }
646
647 for (j = 0; j < pblk->min_write_pgs; j++, i++, paddr++)
648 rqd->ppa_list[i] =
649 addr_to_gen_ppa(pblk, paddr, line->id);
650 }
651
652 ret = pblk_submit_io(pblk, rqd);
653 if (ret) {
654 pr_err("pblk: I/O submission failed: %d\n", ret);
655 bio_put(bio);
656 return ret;
657 }
658
659 if (!wait_for_completion_io_timeout(&wait,
660 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
661 pr_err("pblk: L2P recovery read timed out\n");
662 }
Javier González588726d32017-06-26 11:57:29 +0200663 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200664 reinit_completion(&wait);
665
666 /* Reached the end of the written line */
667 if (rqd->error) {
668 int nr_error_bits, bit;
669
670 bit = find_first_bit((void *)&rqd->ppa_status, rqd->nr_ppas);
671 nr_error_bits = rqd->nr_ppas - bit;
672
673 /* Roll back failed sectors */
674 line->cur_sec -= nr_error_bits;
675 line->left_msecs += nr_error_bits;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200676 bitmap_clear(line->map_bitmap, line->cur_sec, nr_error_bits);
677
678 left_ppas = 0;
679 rqd->nr_ppas = bit;
680
681 if (rqd->error != NVM_RSP_ERR_EMPTYPAGE)
682 *done = 0;
683 }
684
685 for (i = 0; i < rqd->nr_ppas; i++) {
686 u64 lba = le64_to_cpu(meta_list[i].lba);
687
688 if (lba == ADDR_EMPTY || lba > pblk->rl.nr_secs)
689 continue;
690
691 pblk_update_map(pblk, lba, rqd->ppa_list[i]);
692 }
693
694 left_ppas -= rq_ppas;
695 if (left_ppas > 0)
696 goto next_rq;
697
698 return ret;
699}
700
701/* Scan line for lbas on out of bound area */
702static int pblk_recov_l2p_from_oob(struct pblk *pblk, struct pblk_line *line)
703{
704 struct nvm_tgt_dev *dev = pblk->dev;
705 struct nvm_geo *geo = &dev->geo;
706 struct nvm_rq *rqd;
707 struct ppa_addr *ppa_list;
708 struct pblk_sec_meta *meta_list;
709 struct pblk_recov_alloc p;
710 void *data;
711 dma_addr_t dma_ppa_list, dma_meta_list;
712 int done, ret = 0;
713
714 rqd = pblk_alloc_rqd(pblk, READ);
715 if (IS_ERR(rqd))
716 return PTR_ERR(rqd);
717
718 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_meta_list);
719 if (!meta_list) {
720 ret = -ENOMEM;
721 goto free_rqd;
722 }
723
724 ppa_list = (void *)(meta_list) + pblk_dma_meta_size;
725 dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
726
727 data = kcalloc(pblk->max_write_pgs, geo->sec_size, GFP_KERNEL);
728 if (!data) {
729 ret = -ENOMEM;
730 goto free_meta_list;
731 }
732
733 p.ppa_list = ppa_list;
734 p.meta_list = meta_list;
735 p.rqd = rqd;
736 p.data = data;
737 p.dma_ppa_list = dma_ppa_list;
738 p.dma_meta_list = dma_meta_list;
739
740 ret = pblk_recov_scan_oob(pblk, line, p, &done);
741 if (ret) {
742 pr_err("pblk: could not recover L2P from OOB\n");
743 goto out;
744 }
745
746 if (!done) {
747 ret = pblk_recov_scan_all_oob(pblk, line, p);
748 if (ret) {
749 pr_err("pblk: could not recover L2P from OOB\n");
750 goto out;
751 }
752 }
753
754 if (pblk_line_is_full(line))
755 pblk_line_recov_close(pblk, line);
756
757out:
758 kfree(data);
759free_meta_list:
760 nvm_dev_dma_free(dev->parent, meta_list, dma_meta_list);
761free_rqd:
762 pblk_free_rqd(pblk, rqd, READ);
763
764 return ret;
765}
766
767/* Insert lines ordered by sequence number (seq_num) on list */
768static void pblk_recov_line_add_ordered(struct list_head *head,
769 struct pblk_line *line)
770{
771 struct pblk_line *t = NULL;
772
773 list_for_each_entry(t, head, list)
774 if (t->seq_nr > line->seq_nr)
775 break;
776
777 __list_add(&line->list, t->list.prev, &t->list);
778}
779
780struct pblk_line *pblk_recov_l2p(struct pblk *pblk)
781{
782 struct nvm_tgt_dev *dev = pblk->dev;
783 struct nvm_geo *geo = &dev->geo;
784 struct pblk_line_meta *lm = &pblk->lm;
785 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
786 struct pblk_line *line, *tline, *data_line = NULL;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200787 struct pblk_smeta *smeta;
788 struct pblk_emeta *emeta;
789 struct line_smeta *smeta_buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200790 int found_lines = 0, recovered_lines = 0, open_lines = 0;
791 int is_next = 0;
792 int meta_line;
793 int i, valid_uuid = 0;
794 LIST_HEAD(recov_list);
795
796 /* TODO: Implement FTL snapshot */
797
798 /* Scan recovery - takes place when FTL snapshot fails */
799 spin_lock(&l_mg->free_lock);
800 meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES);
801 set_bit(meta_line, &l_mg->meta_bitmap);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200802 smeta = l_mg->sline_meta[meta_line];
803 emeta = l_mg->eline_meta[meta_line];
804 smeta_buf = smeta->buf;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200805 spin_unlock(&l_mg->free_lock);
806
807 /* Order data lines using their sequence number */
808 for (i = 0; i < l_mg->nr_lines; i++) {
809 u32 crc;
810
811 line = &pblk->lines[i];
812
813 memset(smeta, 0, lm->smeta_len);
814 line->smeta = smeta;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200815 line->lun_bitmap = ((void *)(smeta_buf)) +
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200816 sizeof(struct line_smeta);
817
818 /* Lines that cannot be read are assumed as not written here */
819 if (pblk_line_read_smeta(pblk, line))
820 continue;
821
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200822 crc = pblk_calc_smeta_crc(pblk, smeta_buf);
823 if (le32_to_cpu(smeta_buf->crc) != crc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200824 continue;
825
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200826 if (le32_to_cpu(smeta_buf->header.identifier) != PBLK_MAGIC)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200827 continue;
828
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200829 if (le16_to_cpu(smeta_buf->header.version) != 1) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200830 pr_err("pblk: found incompatible line version %u\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200831 smeta_buf->header.version);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200832 return ERR_PTR(-EINVAL);
833 }
834
835 /* The first valid instance uuid is used for initialization */
836 if (!valid_uuid) {
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200837 memcpy(pblk->instance_uuid, smeta_buf->header.uuid, 16);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200838 valid_uuid = 1;
839 }
840
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200841 if (memcmp(pblk->instance_uuid, smeta_buf->header.uuid, 16)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842 pr_debug("pblk: ignore line %u due to uuid mismatch\n",
843 i);
844 continue;
845 }
846
847 /* Update line metadata */
848 spin_lock(&line->lock);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200849 line->id = le32_to_cpu(smeta_buf->header.id);
850 line->type = le16_to_cpu(smeta_buf->header.type);
851 line->seq_nr = le64_to_cpu(smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200852 spin_unlock(&line->lock);
853
854 /* Update general metadata */
855 spin_lock(&l_mg->free_lock);
856 if (line->seq_nr >= l_mg->d_seq_nr)
857 l_mg->d_seq_nr = line->seq_nr + 1;
858 l_mg->nr_free_lines--;
859 spin_unlock(&l_mg->free_lock);
860
861 if (pblk_line_recov_alloc(pblk, line))
862 goto out;
863
864 pblk_recov_line_add_ordered(&recov_list, line);
865 found_lines++;
866 pr_debug("pblk: recovering data line %d, seq:%llu\n",
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200867 line->id, smeta_buf->seq_nr);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200868 }
869
870 if (!found_lines) {
871 pblk_setup_uuid(pblk);
872
873 spin_lock(&l_mg->free_lock);
874 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
875 &l_mg->meta_bitmap));
876 spin_unlock(&l_mg->free_lock);
877
878 goto out;
879 }
880
881 /* Verify closed blocks and recover this portion of L2P table*/
882 list_for_each_entry_safe(line, tline, &recov_list, list) {
883 int off, nr_bb;
884
885 recovered_lines++;
886 /* Calculate where emeta starts based on the line bb */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200887 off = lm->sec_per_line - lm->emeta_sec[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200888 nr_bb = bitmap_weight(line->blk_bitmap, lm->blk_per_line);
889 off -= nr_bb * geo->sec_per_pl;
890
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200891 memset(&emeta->buf, 0, lm->emeta_len[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200892 line->emeta = emeta;
893 line->emeta_ssec = off;
894
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200895 if (pblk_line_read_emeta(pblk, line, line->emeta->buf)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200896 pblk_recov_l2p_from_oob(pblk, line);
897 goto next;
898 }
899
900 if (pblk_recov_l2p_from_emeta(pblk, line))
901 pblk_recov_l2p_from_oob(pblk, line);
902
903next:
904 if (pblk_line_is_full(line)) {
905 struct list_head *move_list;
906
907 spin_lock(&line->lock);
908 line->state = PBLK_LINESTATE_CLOSED;
909 move_list = pblk_line_gc_list(pblk, line);
910 spin_unlock(&line->lock);
911
912 spin_lock(&l_mg->gc_lock);
913 list_move_tail(&line->list, move_list);
914 spin_unlock(&l_mg->gc_lock);
915
916 mempool_free(line->map_bitmap, pblk->line_meta_pool);
917 line->map_bitmap = NULL;
918 line->smeta = NULL;
919 line->emeta = NULL;
920 } else {
921 if (open_lines > 1)
922 pr_err("pblk: failed to recover L2P\n");
923
924 open_lines++;
925 line->meta_line = meta_line;
926 data_line = line;
927 }
928 }
929
930 spin_lock(&l_mg->free_lock);
931 if (!open_lines) {
932 WARN_ON_ONCE(!test_and_clear_bit(meta_line,
933 &l_mg->meta_bitmap));
934 pblk_line_replace_data(pblk);
935 } else {
936 /* Allocate next line for preparation */
937 l_mg->data_next = pblk_line_get(pblk);
938 if (l_mg->data_next) {
939 l_mg->data_next->seq_nr = l_mg->d_seq_nr++;
940 l_mg->data_next->type = PBLK_LINETYPE_DATA;
941 is_next = 1;
942 }
943 }
944 spin_unlock(&l_mg->free_lock);
945
946 if (is_next) {
947 pblk_line_erase(pblk, l_mg->data_next);
948 pblk_rl_free_lines_dec(&pblk->rl, l_mg->data_next);
949 }
950
951out:
952 if (found_lines != recovered_lines)
953 pr_err("pblk: failed to recover all found lines %d/%d\n",
954 found_lines, recovered_lines);
955
956 return data_line;
957}
958
959/*
Javier González588726d32017-06-26 11:57:29 +0200960 * Pad current line
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200961 */
Javier González588726d32017-06-26 11:57:29 +0200962int pblk_recov_pad(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200963{
964 struct nvm_tgt_dev *dev = pblk->dev;
965 struct nvm_geo *geo = &dev->geo;
966 struct pblk_line *line;
967 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
968 struct nvm_rq *rqd;
969 struct pblk_recov_alloc p;
970 struct ppa_addr *ppa_list;
971 struct pblk_sec_meta *meta_list;
972 void *data;
Javier González588726d32017-06-26 11:57:29 +0200973 int left_msecs;
974 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200975 dma_addr_t dma_ppa_list, dma_meta_list;
976
977 spin_lock(&l_mg->free_lock);
978 line = l_mg->data_line;
Javier González588726d32017-06-26 11:57:29 +0200979 left_msecs = line->left_msecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200980 spin_unlock(&l_mg->free_lock);
981
982 rqd = pblk_alloc_rqd(pblk, READ);
983 if (IS_ERR(rqd))
Javier González588726d32017-06-26 11:57:29 +0200984 return PTR_ERR(rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200985
986 meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, &dma_meta_list);
Javier González588726d32017-06-26 11:57:29 +0200987 if (!meta_list) {
988 ret = -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200989 goto free_rqd;
Javier González588726d32017-06-26 11:57:29 +0200990 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200991
992 ppa_list = (void *)(meta_list) + pblk_dma_meta_size;
993 dma_ppa_list = dma_meta_list + pblk_dma_meta_size;
994
995 data = kcalloc(pblk->max_write_pgs, geo->sec_size, GFP_KERNEL);
Javier González588726d32017-06-26 11:57:29 +0200996 if (!data) {
997 ret = -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200998 goto free_meta_list;
Javier González588726d32017-06-26 11:57:29 +0200999 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001000
1001 p.ppa_list = ppa_list;
1002 p.meta_list = meta_list;
1003 p.rqd = rqd;
1004 p.data = data;
1005 p.dma_ppa_list = dma_ppa_list;
1006 p.dma_meta_list = dma_meta_list;
1007
Javier González588726d32017-06-26 11:57:29 +02001008 ret = pblk_recov_pad_oob(pblk, line, p, left_msecs);
1009 if (ret) {
1010 pr_err("pblk: Tear down padding failed (%d)\n", ret);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001011 goto free_data;
1012 }
1013
Javier González588726d32017-06-26 11:57:29 +02001014 pblk_line_close_meta(pblk, line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001015
1016free_data:
1017 kfree(data);
1018free_meta_list:
1019 nvm_dev_dma_free(dev->parent, meta_list, dma_meta_list);
1020free_rqd:
1021 pblk_free_rqd(pblk, rqd, READ);
Javier González588726d32017-06-26 11:57:29 +02001022
1023 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001024}