lightnvm: pblk: return NVM_ error on failed submission
[linux-2.6-block.git] / drivers / lightnvm / pblk-read.c
CommitLineData
a4bd217b
JG
1/*
2 * Copyright (C) 2016 CNEX Labs
3 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
4 * Matias Bjorling <matias@cnexlabs.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * pblk-read.c - pblk's read path
16 */
17
18#include "pblk.h"
19
20/*
21 * There is no guarantee that the value read from cache has not been updated and
22 * resides at another location in the cache. We guarantee though that if the
23 * value is read from the cache, it belongs to the mapped lba. In order to
24 * guarantee and order between writes and reads are ordered, a flush must be
25 * issued.
26 */
27static int pblk_read_from_cache(struct pblk *pblk, struct bio *bio,
28 sector_t lba, struct ppa_addr ppa,
75cb8e93 29 int bio_iter, bool advanced_bio)
a4bd217b
JG
30{
31#ifdef CONFIG_NVM_DEBUG
32 /* Callers must ensure that the ppa points to a cache address */
33 BUG_ON(pblk_ppa_empty(ppa));
34 BUG_ON(!pblk_addr_in_cache(ppa));
35#endif
36
75cb8e93
JG
37 return pblk_rb_copy_to_bio(&pblk->rwb, bio, lba, ppa,
38 bio_iter, advanced_bio);
a4bd217b
JG
39}
40
41static void pblk_read_ppalist_rq(struct pblk *pblk, struct nvm_rq *rqd,
84454e6d 42 sector_t blba, unsigned long *read_bitmap)
a4bd217b 43{
a4809fee 44 struct pblk_sec_meta *meta_list = rqd->meta_list;
a4bd217b
JG
45 struct bio *bio = rqd->bio;
46 struct ppa_addr ppas[PBLK_MAX_REQ_ADDRS];
a4bd217b 47 int nr_secs = rqd->nr_ppas;
75cb8e93 48 bool advanced_bio = false;
a4bd217b
JG
49 int i, j = 0;
50
a4bd217b
JG
51 pblk_lookup_l2p_seq(pblk, ppas, blba, nr_secs);
52
53 for (i = 0; i < nr_secs; i++) {
54 struct ppa_addr p = ppas[i];
55 sector_t lba = blba + i;
56
57retry:
58 if (pblk_ppa_empty(p)) {
59 WARN_ON(test_and_set_bit(i, read_bitmap));
a4809fee 60 meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
75cb8e93
JG
61
62 if (unlikely(!advanced_bio)) {
63 bio_advance(bio, (i) * PBLK_EXPOSED_PAGE_SIZE);
64 advanced_bio = true;
65 }
66
67 goto next;
a4bd217b
JG
68 }
69
70 /* Try to read from write buffer. The address is later checked
71 * on the write buffer to prevent retrieving overwritten data.
72 */
73 if (pblk_addr_in_cache(p)) {
75cb8e93
JG
74 if (!pblk_read_from_cache(pblk, bio, lba, p, i,
75 advanced_bio)) {
a4bd217b
JG
76 pblk_lookup_l2p_seq(pblk, &p, lba, 1);
77 goto retry;
78 }
79 WARN_ON(test_and_set_bit(i, read_bitmap));
a4809fee 80 meta_list[i].lba = cpu_to_le64(lba);
75cb8e93 81 advanced_bio = true;
db7ada33
JG
82#ifdef CONFIG_NVM_DEBUG
83 atomic_long_inc(&pblk->cache_reads);
84#endif
a4bd217b
JG
85 } else {
86 /* Read from media non-cached sectors */
87 rqd->ppa_list[j++] = p;
88 }
89
75cb8e93 90next:
a4bd217b
JG
91 if (advanced_bio)
92 bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
93 }
94
f9c10152
JG
95 if (pblk_io_aligned(pblk, nr_secs))
96 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL);
97 else
98 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
99
a4bd217b
JG
100#ifdef CONFIG_NVM_DEBUG
101 atomic_long_add(nr_secs, &pblk->inflight_reads);
102#endif
103}
104
105static int pblk_submit_read_io(struct pblk *pblk, struct nvm_rq *rqd)
106{
107 int err;
108
a4bd217b
JG
109 err = pblk_submit_io(pblk, rqd);
110 if (err)
111 return NVM_IO_ERR;
112
113 return NVM_IO_OK;
114}
115
03a34b2d
JG
116static void pblk_read_check_seq(struct pblk *pblk, struct nvm_rq *rqd,
117 sector_t blba)
a4809fee 118{
03a34b2d
JG
119 struct pblk_sec_meta *meta_lba_list = rqd->meta_list;
120 int nr_lbas = rqd->nr_ppas;
a4809fee
JG
121 int i;
122
123 for (i = 0; i < nr_lbas; i++) {
310df582 124 u64 lba = le64_to_cpu(meta_lba_list[i].lba);
a4809fee
JG
125
126 if (lba == ADDR_EMPTY)
127 continue;
128
03a34b2d
JG
129 if (lba != blba + i) {
130#ifdef CONFIG_NVM_DEBUG
131 struct ppa_addr *p;
132
133 p = (nr_lbas == 1) ? &rqd->ppa_list[i] : &rqd->ppa_addr;
134 print_ppa(&pblk->dev->geo, p, "seq", i);
135#endif
136 pr_err("pblk: corrupted read LBA (%llu/%llu)\n",
137 lba, (u64)blba + i);
138 WARN_ON(1);
139 }
a4809fee
JG
140 }
141}
142
310df582
JG
143/*
144 * There can be holes in the lba list.
145 */
03a34b2d
JG
146static void pblk_read_check_rand(struct pblk *pblk, struct nvm_rq *rqd,
147 u64 *lba_list, int nr_lbas)
310df582 148{
03a34b2d 149 struct pblk_sec_meta *meta_lba_list = rqd->meta_list;
310df582
JG
150 int i, j;
151
152 for (i = 0, j = 0; i < nr_lbas; i++) {
153 u64 lba = lba_list[i];
154 u64 meta_lba;
155
156 if (lba == ADDR_EMPTY)
157 continue;
158
03a34b2d 159 meta_lba = le64_to_cpu(meta_lba_list[j].lba);
310df582
JG
160
161 if (lba != meta_lba) {
03a34b2d
JG
162#ifdef CONFIG_NVM_DEBUG
163 struct ppa_addr *p;
164 int nr_ppas = rqd->nr_ppas;
165
166 p = (nr_ppas == 1) ? &rqd->ppa_list[j] : &rqd->ppa_addr;
167 print_ppa(&pblk->dev->geo, p, "seq", j);
168#endif
310df582
JG
169 pr_err("pblk: corrupted read LBA (%llu/%llu)\n",
170 lba, meta_lba);
171 WARN_ON(1);
172 }
03a34b2d
JG
173
174 j++;
310df582 175 }
03a34b2d
JG
176
177 WARN_ONCE(j != rqd->nr_ppas, "pblk: corrupted random request\n");
310df582
JG
178}
179
7bd4d370
JG
180static void pblk_read_put_rqd_kref(struct pblk *pblk, struct nvm_rq *rqd)
181{
182 struct ppa_addr *ppa_list;
183 int i;
184
185 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
186
187 for (i = 0; i < rqd->nr_ppas; i++) {
188 struct ppa_addr ppa = ppa_list[i];
189 struct pblk_line *line;
190
b1bcfda1 191 line = &pblk->lines[pblk_ppa_to_line(ppa)];
7bd4d370
JG
192 kref_put(&line->ref, pblk_line_put_wq);
193 }
194}
195
196static void pblk_end_user_read(struct bio *bio)
197{
198#ifdef CONFIG_NVM_DEBUG
199 WARN_ONCE(bio->bi_status, "pblk: corrupted read bio\n");
200#endif
201 bio_endio(bio);
202 bio_put(bio);
203}
204
205static void __pblk_end_io_read(struct pblk *pblk, struct nvm_rq *rqd,
206 bool put_line)
a4bd217b 207{
998ba629 208 struct nvm_tgt_dev *dev = pblk->dev;
084ec9ba 209 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
a4bd217b 210 struct bio *bio = rqd->bio;
998ba629
JG
211 unsigned long start_time = r_ctx->start_time;
212
213 generic_end_io_acct(dev->q, READ, &pblk->disk->part0, start_time);
a4bd217b
JG
214
215 if (rqd->error)
216 pblk_log_read_err(pblk, rqd);
217#ifdef CONFIG_NVM_DEBUG
218 else
4e4cbee9 219 WARN_ONCE(bio->bi_status, "pblk: corrupted read error\n");
a4bd217b
JG
220#endif
221
03a34b2d 222 pblk_read_check_seq(pblk, rqd, r_ctx->lba);
a4809fee 223
a4bd217b 224 bio_put(bio);
7bd4d370
JG
225 if (r_ctx->private)
226 pblk_end_user_read((struct bio *)r_ctx->private);
084ec9ba 227
7bd4d370
JG
228 if (put_line)
229 pblk_read_put_rqd_kref(pblk, rqd);
a4bd217b
JG
230
231#ifdef CONFIG_NVM_DEBUG
232 atomic_long_add(rqd->nr_ppas, &pblk->sync_reads);
233 atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads);
234#endif
235
e2cddf20 236 pblk_free_rqd(pblk, rqd, PBLK_READ);
588726d3 237 atomic_dec(&pblk->inflight_io);
a4bd217b
JG
238}
239
7bd4d370
JG
240static void pblk_end_io_read(struct nvm_rq *rqd)
241{
242 struct pblk *pblk = rqd->private;
243
244 __pblk_end_io_read(pblk, rqd, true);
245}
246
998ba629
JG
247static int pblk_partial_read_bio(struct pblk *pblk, struct nvm_rq *rqd,
248 unsigned int bio_init_idx,
249 unsigned long *read_bitmap)
a4bd217b
JG
250{
251 struct bio *new_bio, *bio = rqd->bio;
a4809fee 252 struct pblk_sec_meta *meta_list = rqd->meta_list;
a4bd217b
JG
253 struct bio_vec src_bv, dst_bv;
254 void *ppa_ptr = NULL;
255 void *src_p, *dst_p;
256 dma_addr_t dma_ppa_list = 0;
a4809fee 257 __le64 *lba_list_mem, *lba_list_media;
a4bd217b
JG
258 int nr_secs = rqd->nr_ppas;
259 int nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
260 int i, ret, hole;
a4bd217b 261
a4809fee
JG
262 /* Re-use allocated memory for intermediate lbas */
263 lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size);
264 lba_list_media = (((void *)rqd->ppa_list) + 2 * pblk_dma_ppa_size);
265
a4bd217b 266 new_bio = bio_alloc(GFP_KERNEL, nr_holes);
a4bd217b
JG
267
268 if (pblk_bio_add_pages(pblk, new_bio, GFP_KERNEL, nr_holes))
269 goto err;
270
271 if (nr_holes != new_bio->bi_vcnt) {
272 pr_err("pblk: malformed bio\n");
273 goto err;
274 }
275
a4809fee
JG
276 for (i = 0; i < nr_secs; i++)
277 lba_list_mem[i] = meta_list[i].lba;
278
a4bd217b
JG
279 new_bio->bi_iter.bi_sector = 0; /* internal bio */
280 bio_set_op_attrs(new_bio, REQ_OP_READ, 0);
a4bd217b
JG
281
282 rqd->bio = new_bio;
283 rqd->nr_ppas = nr_holes;
f9c10152 284 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
a4bd217b 285
0f9248cf 286 if (unlikely(nr_holes == 1)) {
a4bd217b
JG
287 ppa_ptr = rqd->ppa_list;
288 dma_ppa_list = rqd->dma_ppa_list;
289 rqd->ppa_addr = rqd->ppa_list[0];
290 }
291
1a94b2d4 292 ret = pblk_submit_io_sync(pblk, rqd);
a4bd217b
JG
293 if (ret) {
294 bio_put(rqd->bio);
1a94b2d4 295 pr_err("pblk: sync read IO submission failed\n");
a4bd217b
JG
296 goto err;
297 }
298
a4bd217b
JG
299 if (rqd->error) {
300 atomic_long_inc(&pblk->read_failed);
301#ifdef CONFIG_NVM_DEBUG
302 pblk_print_failed_rqd(pblk, rqd, rqd->error);
303#endif
304 }
305
0f9248cf 306 if (unlikely(nr_holes == 1)) {
7bd4d370
JG
307 struct ppa_addr ppa;
308
309 ppa = rqd->ppa_addr;
a4bd217b
JG
310 rqd->ppa_list = ppa_ptr;
311 rqd->dma_ppa_list = dma_ppa_list;
7bd4d370 312 rqd->ppa_list[0] = ppa;
a4bd217b
JG
313 }
314
a4809fee
JG
315 for (i = 0; i < nr_secs; i++) {
316 lba_list_media[i] = meta_list[i].lba;
317 meta_list[i].lba = lba_list_mem[i];
318 }
319
a4bd217b
JG
320 /* Fill the holes in the original bio */
321 i = 0;
322 hole = find_first_zero_bit(read_bitmap, nr_secs);
323 do {
b1bcfda1 324 int line_id = pblk_ppa_to_line(rqd->ppa_list[i]);
7bd4d370
JG
325 struct pblk_line *line = &pblk->lines[line_id];
326
327 kref_put(&line->ref, pblk_line_put);
328
a4809fee
JG
329 meta_list[hole].lba = lba_list_media[i];
330
a4bd217b
JG
331 src_bv = new_bio->bi_io_vec[i++];
332 dst_bv = bio->bi_io_vec[bio_init_idx + hole];
333
334 src_p = kmap_atomic(src_bv.bv_page);
335 dst_p = kmap_atomic(dst_bv.bv_page);
336
337 memcpy(dst_p + dst_bv.bv_offset,
338 src_p + src_bv.bv_offset,
339 PBLK_EXPOSED_PAGE_SIZE);
340
341 kunmap_atomic(src_p);
342 kunmap_atomic(dst_p);
343
b906bbb6 344 mempool_free(src_bv.bv_page, &pblk->page_bio_pool);
a4bd217b
JG
345
346 hole = find_next_zero_bit(read_bitmap, nr_secs, hole + 1);
347 } while (hole < nr_secs);
348
349 bio_put(new_bio);
350
351 /* Complete the original bio and associated request */
7bd4d370 352 bio_endio(bio);
a4bd217b
JG
353 rqd->bio = bio;
354 rqd->nr_ppas = nr_secs;
a4bd217b 355
7bd4d370 356 __pblk_end_io_read(pblk, rqd, false);
a4bd217b
JG
357 return NVM_IO_OK;
358
359err:
998ba629
JG
360 pr_err("pblk: failed to perform partial read\n");
361
a4bd217b
JG
362 /* Free allocated pages in new bio */
363 pblk_bio_free_pages(pblk, bio, 0, new_bio->bi_vcnt);
7bd4d370 364 __pblk_end_io_read(pblk, rqd, false);
a4bd217b
JG
365 return NVM_IO_ERR;
366}
367
368static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd,
84454e6d 369 sector_t lba, unsigned long *read_bitmap)
a4bd217b 370{
a4809fee 371 struct pblk_sec_meta *meta_list = rqd->meta_list;
a4bd217b
JG
372 struct bio *bio = rqd->bio;
373 struct ppa_addr ppa;
a4bd217b
JG
374
375 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
376
377#ifdef CONFIG_NVM_DEBUG
378 atomic_long_inc(&pblk->inflight_reads);
379#endif
380
381retry:
382 if (pblk_ppa_empty(ppa)) {
383 WARN_ON(test_and_set_bit(0, read_bitmap));
a4809fee 384 meta_list[0].lba = cpu_to_le64(ADDR_EMPTY);
a4bd217b
JG
385 return;
386 }
387
388 /* Try to read from write buffer. The address is later checked on the
389 * write buffer to prevent retrieving overwritten data.
390 */
391 if (pblk_addr_in_cache(ppa)) {
75cb8e93 392 if (!pblk_read_from_cache(pblk, bio, lba, ppa, 0, 1)) {
a4bd217b
JG
393 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
394 goto retry;
395 }
a4809fee 396
7bd4d370 397 WARN_ON(test_and_set_bit(0, read_bitmap));
a4809fee
JG
398 meta_list[0].lba = cpu_to_le64(lba);
399
db7ada33 400#ifdef CONFIG_NVM_DEBUG
7bd4d370 401 atomic_long_inc(&pblk->cache_reads);
db7ada33 402#endif
a4bd217b
JG
403 } else {
404 rqd->ppa_addr = ppa;
405 }
f9c10152
JG
406
407 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
a4bd217b
JG
408}
409
410int pblk_submit_read(struct pblk *pblk, struct bio *bio)
411{
412 struct nvm_tgt_dev *dev = pblk->dev;
998ba629 413 struct request_queue *q = dev->q;
84454e6d 414 sector_t blba = pblk_get_lba(bio);
5bf1e1ee 415 unsigned int nr_secs = pblk_get_secs(bio);
a4809fee 416 struct pblk_g_ctx *r_ctx;
a4bd217b 417 struct nvm_rq *rqd;
a4bd217b 418 unsigned int bio_init_idx;
a4809fee 419 unsigned long read_bitmap; /* Max 64 ppas per request */
a4bd217b
JG
420 int ret = NVM_IO_ERR;
421
84454e6d
JG
422 /* logic error: lba out-of-bounds. Ignore read request */
423 if (blba >= pblk->rl.nr_secs || nr_secs > PBLK_MAX_REQ_ADDRS) {
424 WARN(1, "pblk: read lba out of bounds (lba:%llu, nr:%d)\n",
425 (unsigned long long)blba, nr_secs);
a4bd217b 426 return NVM_IO_ERR;
84454e6d 427 }
a4bd217b 428
998ba629
JG
429 generic_start_io_acct(q, READ, bio_sectors(bio), &pblk->disk->part0);
430
a4bd217b
JG
431 bitmap_zero(&read_bitmap, nr_secs);
432
e2cddf20 433 rqd = pblk_alloc_rqd(pblk, PBLK_READ);
a4bd217b
JG
434
435 rqd->opcode = NVM_OP_PREAD;
436 rqd->bio = bio;
437 rqd->nr_ppas = nr_secs;
438 rqd->private = pblk;
439 rqd->end_io = pblk_end_io_read;
440
a4809fee 441 r_ctx = nvm_rq_to_pdu(rqd);
998ba629 442 r_ctx->start_time = jiffies;
a4809fee
JG
443 r_ctx->lba = blba;
444
a4bd217b
JG
445 /* Save the index for this bio's start. This is needed in case
446 * we need to fill a partial read.
447 */
448 bio_init_idx = pblk_get_bi_idx(bio);
449
63e3809c
JG
450 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
451 &rqd->dma_meta_list);
452 if (!rqd->meta_list) {
453 pr_err("pblk: not able to allocate ppa list\n");
454 goto fail_rqd_free;
455 }
456
a4bd217b 457 if (nr_secs > 1) {
63e3809c
JG
458 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
459 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
a4bd217b 460
84454e6d 461 pblk_read_ppalist_rq(pblk, rqd, blba, &read_bitmap);
a4bd217b 462 } else {
84454e6d 463 pblk_read_rq(pblk, rqd, blba, &read_bitmap);
a4bd217b
JG
464 }
465
466 bio_get(bio);
467 if (bitmap_full(&read_bitmap, nr_secs)) {
468 bio_endio(bio);
588726d3 469 atomic_inc(&pblk->inflight_io);
7bd4d370 470 __pblk_end_io_read(pblk, rqd, false);
a4bd217b
JG
471 return NVM_IO_OK;
472 }
473
474 /* All sectors are to be read from the device */
475 if (bitmap_empty(&read_bitmap, rqd->nr_ppas)) {
476 struct bio *int_bio = NULL;
a4bd217b
JG
477
478 /* Clone read bio to deal with read errors internally */
b906bbb6 479 int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set);
a4bd217b
JG
480 if (!int_bio) {
481 pr_err("pblk: could not clone read bio\n");
998ba629 482 goto fail_end_io;
a4bd217b
JG
483 }
484
485 rqd->bio = int_bio;
084ec9ba 486 r_ctx->private = bio;
a4bd217b
JG
487
488 ret = pblk_submit_read_io(pblk, rqd);
489 if (ret) {
490 pr_err("pblk: read IO submission failed\n");
491 if (int_bio)
492 bio_put(int_bio);
998ba629 493 goto fail_end_io;
a4bd217b
JG
494 }
495
496 return NVM_IO_OK;
497 }
498
499 /* The read bio request could be partially filled by the write buffer,
500 * but there are some holes that need to be read from the drive.
501 */
998ba629 502 return pblk_partial_read_bio(pblk, rqd, bio_init_idx, &read_bitmap);
a4bd217b
JG
503
504fail_rqd_free:
e2cddf20 505 pblk_free_rqd(pblk, rqd, PBLK_READ);
a4bd217b 506 return ret;
998ba629
JG
507fail_end_io:
508 __pblk_end_io_read(pblk, rqd, false);
509 return ret;
a4bd217b
JG
510}
511
512static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
513 struct pblk_line *line, u64 *lba_list,
d340121e 514 u64 *paddr_list_gc, unsigned int nr_secs)
a4bd217b 515{
d340121e
JG
516 struct ppa_addr ppa_list_l2p[PBLK_MAX_REQ_ADDRS];
517 struct ppa_addr ppa_gc;
a4bd217b
JG
518 int valid_secs = 0;
519 int i;
520
d340121e 521 pblk_lookup_l2p_rand(pblk, ppa_list_l2p, lba_list, nr_secs);
a4bd217b
JG
522
523 for (i = 0; i < nr_secs; i++) {
d340121e
JG
524 if (lba_list[i] == ADDR_EMPTY)
525 continue;
526
527 ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id);
528 if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) {
529 paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY;
a4bd217b
JG
530 continue;
531 }
532
d340121e 533 rqd->ppa_list[valid_secs++] = ppa_list_l2p[i];
a4bd217b
JG
534 }
535
536#ifdef CONFIG_NVM_DEBUG
537 atomic_long_add(valid_secs, &pblk->inflight_reads);
538#endif
d340121e 539
a4bd217b
JG
540 return valid_secs;
541}
542
543static int read_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
d340121e
JG
544 struct pblk_line *line, sector_t lba,
545 u64 paddr_gc)
a4bd217b 546{
d340121e 547 struct ppa_addr ppa_l2p, ppa_gc;
a4bd217b
JG
548 int valid_secs = 0;
549
659226eb
DC
550 if (lba == ADDR_EMPTY)
551 goto out;
552
a4bd217b 553 /* logic error: lba out-of-bounds */
2a79efd8
DC
554 if (lba >= pblk->rl.nr_secs) {
555 WARN(1, "pblk: read lba out of bounds\n");
a4bd217b
JG
556 goto out;
557 }
558
a4bd217b 559 spin_lock(&pblk->trans_lock);
d340121e 560 ppa_l2p = pblk_trans_map_get(pblk, lba);
a4bd217b
JG
561 spin_unlock(&pblk->trans_lock);
562
d340121e
JG
563 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id);
564 if (!pblk_ppa_comp(ppa_l2p, ppa_gc))
a4bd217b
JG
565 goto out;
566
d340121e 567 rqd->ppa_addr = ppa_l2p;
a4bd217b
JG
568 valid_secs = 1;
569
570#ifdef CONFIG_NVM_DEBUG
571 atomic_long_inc(&pblk->inflight_reads);
572#endif
573
574out:
575 return valid_secs;
576}
577
d340121e 578int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
a4bd217b
JG
579{
580 struct nvm_tgt_dev *dev = pblk->dev;
581 struct nvm_geo *geo = &dev->geo;
a4bd217b
JG
582 struct bio *bio;
583 struct nvm_rq rqd;
d340121e
JG
584 int data_len;
585 int ret = NVM_IO_OK;
a4bd217b
JG
586
587 memset(&rqd, 0, sizeof(struct nvm_rq));
588
63e3809c
JG
589 rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
590 &rqd.dma_meta_list);
591 if (!rqd.meta_list)
d340121e 592 return -ENOMEM;
63e3809c 593
d340121e 594 if (gc_rq->nr_secs > 1) {
63e3809c
JG
595 rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size;
596 rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size;
a4bd217b 597
d340121e
JG
598 gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line,
599 gc_rq->lba_list,
600 gc_rq->paddr_list,
601 gc_rq->nr_secs);
602 if (gc_rq->secs_to_gc == 1)
63e3809c 603 rqd.ppa_addr = rqd.ppa_list[0];
a4bd217b 604 } else {
d340121e
JG
605 gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line,
606 gc_rq->lba_list[0],
607 gc_rq->paddr_list[0]);
a4bd217b
JG
608 }
609
d340121e 610 if (!(gc_rq->secs_to_gc))
a4bd217b
JG
611 goto out;
612
e46f4e48 613 data_len = (gc_rq->secs_to_gc) * geo->csecs;
d340121e 614 bio = pblk_bio_map_addr(pblk, gc_rq->data, gc_rq->secs_to_gc, data_len,
7d327a9e 615 PBLK_VMALLOC_META, GFP_KERNEL);
a4bd217b
JG
616 if (IS_ERR(bio)) {
617 pr_err("pblk: could not allocate GC bio (%lu)\n", PTR_ERR(bio));
618 goto err_free_dma;
619 }
620
621 bio->bi_iter.bi_sector = 0; /* internal bio */
622 bio_set_op_attrs(bio, REQ_OP_READ, 0);
623
624 rqd.opcode = NVM_OP_PREAD;
d340121e 625 rqd.nr_ppas = gc_rq->secs_to_gc;
f9c10152 626 rqd.flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
a4bd217b
JG
627 rqd.bio = bio;
628
1a94b2d4 629 if (pblk_submit_io_sync(pblk, &rqd)) {
d340121e 630 ret = -EIO;
a4bd217b 631 pr_err("pblk: GC read request failed\n");
7d327a9e 632 goto err_free_bio;
a4bd217b
JG
633 }
634
03a34b2d 635 pblk_read_check_rand(pblk, &rqd, gc_rq->lba_list, gc_rq->nr_secs);
310df582 636
588726d3 637 atomic_dec(&pblk->inflight_io);
a4bd217b
JG
638
639 if (rqd.error) {
640 atomic_long_inc(&pblk->read_failed_gc);
641#ifdef CONFIG_NVM_DEBUG
642 pblk_print_failed_rqd(pblk, &rqd, rqd.error);
643#endif
644 }
645
646#ifdef CONFIG_NVM_DEBUG
d340121e
JG
647 atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads);
648 atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads);
649 atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads);
a4bd217b
JG
650#endif
651
652out:
63e3809c 653 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
d340121e 654 return ret;
a4bd217b 655
7d327a9e
JG
656err_free_bio:
657 bio_put(bio);
a4bd217b 658err_free_dma:
63e3809c 659 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
d340121e 660 return ret;
a4bd217b 661}