blob: 9c61c19be5dc1a7c3a033f6021d83355dc18c729 [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-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,
Javier González75cb8e92017-07-28 15:13:16 +020029 int bio_iter, bool advanced_bio)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020030{
Matias Bjørling880eda52018-07-13 10:48:37 +020031#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020032 /* 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
Javier González75cb8e92017-07-28 15:13:16 +020037 return pblk_rb_copy_to_bio(&pblk->rwb, bio, lba, ppa,
38 bio_iter, advanced_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020039}
40
41static void pblk_read_ppalist_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier González87cc40b2018-06-01 16:41:11 +020042 struct bio *bio, sector_t blba,
43 unsigned long *read_bitmap)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020044{
Javier Gonzáleza4809fe2017-10-13 14:46:22 +020045 struct pblk_sec_meta *meta_list = rqd->meta_list;
Matias Bjørlingafdc23c2018-10-09 13:11:37 +020046 struct ppa_addr ppas[NVM_MAX_VLBA];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020047 int nr_secs = rqd->nr_ppas;
Javier González75cb8e92017-07-28 15:13:16 +020048 bool advanced_bio = false;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020049 int i, j = 0;
50
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020051 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));
Javier Gonzáleza4809fe2017-10-13 14:46:22 +020060 meta_list[i].lba = cpu_to_le64(ADDR_EMPTY);
Javier González75cb8e92017-07-28 15:13:16 +020061
62 if (unlikely(!advanced_bio)) {
63 bio_advance(bio, (i) * PBLK_EXPOSED_PAGE_SIZE);
64 advanced_bio = true;
65 }
66
67 goto next;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020068 }
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)) {
Javier González75cb8e92017-07-28 15:13:16 +020074 if (!pblk_read_from_cache(pblk, bio, lba, p, i,
75 advanced_bio)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020076 pblk_lookup_l2p_seq(pblk, &p, lba, 1);
77 goto retry;
78 }
79 WARN_ON(test_and_set_bit(i, read_bitmap));
Javier Gonzáleza4809fe2017-10-13 14:46:22 +020080 meta_list[i].lba = cpu_to_le64(lba);
Javier González75cb8e92017-07-28 15:13:16 +020081 advanced_bio = true;
Matias Bjørling880eda52018-07-13 10:48:37 +020082#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezdb7ada32017-06-26 11:57:13 +020083 atomic_long_inc(&pblk->cache_reads);
84#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020085 } else {
86 /* Read from media non-cached sectors */
87 rqd->ppa_list[j++] = p;
88 }
89
Javier González75cb8e92017-07-28 15:13:16 +020090next:
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020091 if (advanced_bio)
92 bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE);
93 }
94
Javier Gonzálezf9c10152017-06-26 11:57:20 +020095 if (pblk_io_aligned(pblk, nr_secs))
Matias Bjørlingd7b68012018-10-09 13:11:32 +020096 rqd->is_seq = 1;
Javier Gonzálezf9c10152017-06-26 11:57:20 +020097
Matias Bjørling880eda52018-07-13 10:48:37 +020098#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020099 atomic_long_add(nr_secs, &pblk->inflight_reads);
100#endif
101}
102
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200103
Javier González03a34b22018-06-01 15:04:18 +0200104static void pblk_read_check_seq(struct pblk *pblk, struct nvm_rq *rqd,
105 sector_t blba)
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200106{
Javier González03a34b22018-06-01 15:04:18 +0200107 struct pblk_sec_meta *meta_lba_list = rqd->meta_list;
108 int nr_lbas = rqd->nr_ppas;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200109 int i;
110
111 for (i = 0; i < nr_lbas; i++) {
Javier González310df582018-06-01 15:04:17 +0200112 u64 lba = le64_to_cpu(meta_lba_list[i].lba);
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200113
114 if (lba == ADDR_EMPTY)
115 continue;
116
Javier González03a34b22018-06-01 15:04:18 +0200117 if (lba != blba + i) {
Matias Bjørling880eda52018-07-13 10:48:37 +0200118#ifdef CONFIG_NVM_PBLK_DEBUG
Javier González03a34b22018-06-01 15:04:18 +0200119 struct ppa_addr *p;
120
121 p = (nr_lbas == 1) ? &rqd->ppa_list[i] : &rqd->ppa_addr;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200122 print_ppa(pblk, p, "seq", i);
Javier González03a34b22018-06-01 15:04:18 +0200123#endif
Matias Bjørling4e495a42018-07-13 10:48:42 +0200124 pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n",
Javier González03a34b22018-06-01 15:04:18 +0200125 lba, (u64)blba + i);
126 WARN_ON(1);
127 }
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200128 }
129}
130
Javier González310df582018-06-01 15:04:17 +0200131/*
132 * There can be holes in the lba list.
133 */
Javier González03a34b22018-06-01 15:04:18 +0200134static void pblk_read_check_rand(struct pblk *pblk, struct nvm_rq *rqd,
135 u64 *lba_list, int nr_lbas)
Javier González310df582018-06-01 15:04:17 +0200136{
Javier González03a34b22018-06-01 15:04:18 +0200137 struct pblk_sec_meta *meta_lba_list = rqd->meta_list;
Javier González310df582018-06-01 15:04:17 +0200138 int i, j;
139
140 for (i = 0, j = 0; i < nr_lbas; i++) {
141 u64 lba = lba_list[i];
142 u64 meta_lba;
143
144 if (lba == ADDR_EMPTY)
145 continue;
146
Javier González03a34b22018-06-01 15:04:18 +0200147 meta_lba = le64_to_cpu(meta_lba_list[j].lba);
Javier González310df582018-06-01 15:04:17 +0200148
149 if (lba != meta_lba) {
Matias Bjørling880eda52018-07-13 10:48:37 +0200150#ifdef CONFIG_NVM_PBLK_DEBUG
Javier González03a34b22018-06-01 15:04:18 +0200151 struct ppa_addr *p;
152 int nr_ppas = rqd->nr_ppas;
153
154 p = (nr_ppas == 1) ? &rqd->ppa_list[j] : &rqd->ppa_addr;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200155 print_ppa(pblk, p, "seq", j);
Javier González03a34b22018-06-01 15:04:18 +0200156#endif
Matias Bjørling4e495a42018-07-13 10:48:42 +0200157 pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n",
Javier González310df582018-06-01 15:04:17 +0200158 lba, meta_lba);
159 WARN_ON(1);
160 }
Javier González03a34b22018-06-01 15:04:18 +0200161
162 j++;
Javier González310df582018-06-01 15:04:17 +0200163 }
Javier González03a34b22018-06-01 15:04:18 +0200164
165 WARN_ONCE(j != rqd->nr_ppas, "pblk: corrupted random request\n");
Javier González310df582018-06-01 15:04:17 +0200166}
167
Javier González7bd4d372017-10-13 14:46:23 +0200168static void pblk_read_put_rqd_kref(struct pblk *pblk, struct nvm_rq *rqd)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200169{
Javier González7bd4d372017-10-13 14:46:23 +0200170 struct ppa_addr *ppa_list;
171 int i;
172
173 ppa_list = (rqd->nr_ppas > 1) ? rqd->ppa_list : &rqd->ppa_addr;
174
175 for (i = 0; i < rqd->nr_ppas; i++) {
176 struct ppa_addr ppa = ppa_list[i];
177 struct pblk_line *line;
178
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100179 line = &pblk->lines[pblk_ppa_to_line(ppa)];
Javier González7bd4d372017-10-13 14:46:23 +0200180 kref_put(&line->ref, pblk_line_put_wq);
181 }
182}
183
184static void pblk_end_user_read(struct bio *bio)
185{
Matias Bjørling880eda52018-07-13 10:48:37 +0200186#ifdef CONFIG_NVM_PBLK_DEBUG
Javier González7bd4d372017-10-13 14:46:23 +0200187 WARN_ONCE(bio->bi_status, "pblk: corrupted read bio\n");
188#endif
189 bio_endio(bio);
Javier González7bd4d372017-10-13 14:46:23 +0200190}
191
192static void __pblk_end_io_read(struct pblk *pblk, struct nvm_rq *rqd,
193 bool put_line)
194{
Javier González998ba622018-01-05 14:16:20 +0100195 struct nvm_tgt_dev *dev = pblk->dev;
Javier González084ec9b2017-06-26 16:27:13 -0600196 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
Javier González87cc40b2018-06-01 16:41:11 +0200197 struct bio *int_bio = rqd->bio;
Javier González998ba622018-01-05 14:16:20 +0100198 unsigned long start_time = r_ctx->start_time;
199
Michael Callahanddcf35d2018-07-18 04:47:39 -0700200 generic_end_io_acct(dev->q, REQ_OP_READ, &pblk->disk->part0, start_time);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200201
202 if (rqd->error)
203 pblk_log_read_err(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200204
Javier González03a34b22018-06-01 15:04:18 +0200205 pblk_read_check_seq(pblk, rqd, r_ctx->lba);
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200206
Javier González87cc40b2018-06-01 16:41:11 +0200207 if (int_bio)
208 bio_put(int_bio);
Javier González084ec9b2017-06-26 16:27:13 -0600209
Javier González7bd4d372017-10-13 14:46:23 +0200210 if (put_line)
211 pblk_read_put_rqd_kref(pblk, rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212
Matias Bjørling880eda52018-07-13 10:48:37 +0200213#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200214 atomic_long_add(rqd->nr_ppas, &pblk->sync_reads);
215 atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads);
216#endif
217
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200218 pblk_free_rqd(pblk, rqd, PBLK_READ);
Javier González588726d32017-06-26 11:57:29 +0200219 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200220}
221
Javier González7bd4d372017-10-13 14:46:23 +0200222static void pblk_end_io_read(struct nvm_rq *rqd)
223{
224 struct pblk *pblk = rqd->private;
Javier González87cc40b2018-06-01 16:41:11 +0200225 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
226 struct bio *bio = (struct bio *)r_ctx->private;
Javier González7bd4d372017-10-13 14:46:23 +0200227
Javier González87cc40b2018-06-01 16:41:11 +0200228 pblk_end_user_read(bio);
Javier González7bd4d372017-10-13 14:46:23 +0200229 __pblk_end_io_read(pblk, rqd, true);
230}
231
Heiner Litz11f6ad62018-07-13 10:48:44 +0200232static void pblk_end_partial_read(struct nvm_rq *rqd)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200233{
Heiner Litz11f6ad62018-07-13 10:48:44 +0200234 struct pblk *pblk = rqd->private;
235 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
236 struct pblk_pr_ctx *pr_ctx = r_ctx->private;
237 struct bio *new_bio = rqd->bio;
238 struct bio *bio = pr_ctx->orig_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200239 struct bio_vec src_bv, dst_bv;
Heiner Litz11f6ad62018-07-13 10:48:44 +0200240 struct pblk_sec_meta *meta_list = rqd->meta_list;
241 int bio_init_idx = pr_ctx->bio_init_idx;
242 unsigned long *read_bitmap = pr_ctx->bitmap;
243 int nr_secs = pr_ctx->orig_nr_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200244 int nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
Heiner Litz11f6ad62018-07-13 10:48:44 +0200245 __le64 *lba_list_mem, *lba_list_media;
246 void *src_p, *dst_p;
247 int hole, i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200248
Javier González0f9248c2017-10-13 14:46:24 +0200249 if (unlikely(nr_holes == 1)) {
Javier González7bd4d372017-10-13 14:46:23 +0200250 struct ppa_addr ppa;
251
252 ppa = rqd->ppa_addr;
Heiner Litz11f6ad62018-07-13 10:48:44 +0200253 rqd->ppa_list = pr_ctx->ppa_ptr;
254 rqd->dma_ppa_list = pr_ctx->dma_ppa_list;
Javier González7bd4d372017-10-13 14:46:23 +0200255 rqd->ppa_list[0] = ppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200256 }
257
Heiner Litz11f6ad62018-07-13 10:48:44 +0200258 /* Re-use allocated memory for intermediate lbas */
259 lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size);
260 lba_list_media = (((void *)rqd->ppa_list) + 2 * pblk_dma_ppa_size);
261
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200262 for (i = 0; i < nr_secs; i++) {
263 lba_list_media[i] = meta_list[i].lba;
264 meta_list[i].lba = lba_list_mem[i];
265 }
266
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200267 /* Fill the holes in the original bio */
268 i = 0;
269 hole = find_first_zero_bit(read_bitmap, nr_secs);
270 do {
Javier Gonzálezb1bcfda2018-01-05 14:16:06 +0100271 int line_id = pblk_ppa_to_line(rqd->ppa_list[i]);
Javier González7bd4d372017-10-13 14:46:23 +0200272 struct pblk_line *line = &pblk->lines[line_id];
273
274 kref_put(&line->ref, pblk_line_put);
275
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200276 meta_list[hole].lba = lba_list_media[i];
277
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200278 src_bv = new_bio->bi_io_vec[i++];
Heiner Litz11f6ad62018-07-13 10:48:44 +0200279 dst_bv = bio->bi_io_vec[bio_init_idx + hole];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200280
281 src_p = kmap_atomic(src_bv.bv_page);
282 dst_p = kmap_atomic(dst_bv.bv_page);
283
284 memcpy(dst_p + dst_bv.bv_offset,
285 src_p + src_bv.bv_offset,
286 PBLK_EXPOSED_PAGE_SIZE);
287
288 kunmap_atomic(src_p);
289 kunmap_atomic(dst_p);
290
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400291 mempool_free(src_bv.bv_page, &pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200292
293 hole = find_next_zero_bit(read_bitmap, nr_secs, hole + 1);
294 } while (hole < nr_secs);
295
296 bio_put(new_bio);
Heiner Litz11f6ad62018-07-13 10:48:44 +0200297 kfree(pr_ctx);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298
Javier González87cc40b2018-06-01 16:41:11 +0200299 /* restore original request */
300 rqd->bio = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200301 rqd->nr_ppas = nr_secs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200302
Heiner Litz11f6ad62018-07-13 10:48:44 +0200303 bio_endio(bio);
Javier González7bd4d372017-10-13 14:46:23 +0200304 __pblk_end_io_read(pblk, rqd, false);
Heiner Litz11f6ad62018-07-13 10:48:44 +0200305}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200306
Heiner Litz11f6ad62018-07-13 10:48:44 +0200307static int pblk_setup_partial_read(struct pblk *pblk, struct nvm_rq *rqd,
308 unsigned int bio_init_idx,
309 unsigned long *read_bitmap,
310 int nr_holes)
311{
312 struct pblk_sec_meta *meta_list = rqd->meta_list;
313 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
314 struct pblk_pr_ctx *pr_ctx;
315 struct bio *new_bio, *bio = r_ctx->private;
316 __le64 *lba_list_mem;
317 int nr_secs = rqd->nr_ppas;
318 int i;
319
320 /* Re-use allocated memory for intermediate lbas */
321 lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size);
322
323 new_bio = bio_alloc(GFP_KERNEL, nr_holes);
324
325 if (pblk_bio_add_pages(pblk, new_bio, GFP_KERNEL, nr_holes))
326 goto fail_bio_put;
327
328 if (nr_holes != new_bio->bi_vcnt) {
329 WARN_ONCE(1, "pblk: malformed bio\n");
330 goto fail_free_pages;
331 }
332
333 pr_ctx = kmalloc(sizeof(struct pblk_pr_ctx), GFP_KERNEL);
334 if (!pr_ctx)
335 goto fail_free_pages;
336
337 for (i = 0; i < nr_secs; i++)
338 lba_list_mem[i] = meta_list[i].lba;
339
340 new_bio->bi_iter.bi_sector = 0; /* internal bio */
341 bio_set_op_attrs(new_bio, REQ_OP_READ, 0);
342
343 rqd->bio = new_bio;
344 rqd->nr_ppas = nr_holes;
Heiner Litz11f6ad62018-07-13 10:48:44 +0200345
346 pr_ctx->ppa_ptr = NULL;
347 pr_ctx->orig_bio = bio;
348 bitmap_copy(pr_ctx->bitmap, read_bitmap, NVM_MAX_VLBA);
349 pr_ctx->bio_init_idx = bio_init_idx;
350 pr_ctx->orig_nr_secs = nr_secs;
351 r_ctx->private = pr_ctx;
352
353 if (unlikely(nr_holes == 1)) {
354 pr_ctx->ppa_ptr = rqd->ppa_list;
355 pr_ctx->dma_ppa_list = rqd->dma_ppa_list;
356 rqd->ppa_addr = rqd->ppa_list[0];
357 }
358 return 0;
359
360fail_free_pages:
Igor Konopkofbadca72018-06-01 16:41:09 +0200361 pblk_bio_free_pages(pblk, new_bio, 0, new_bio->bi_vcnt);
Heiner Litz11f6ad62018-07-13 10:48:44 +0200362fail_bio_put:
363 bio_put(new_bio);
364
365 return -ENOMEM;
366}
367
368static int pblk_partial_read_bio(struct pblk *pblk, struct nvm_rq *rqd,
369 unsigned int bio_init_idx,
370 unsigned long *read_bitmap, int nr_secs)
371{
372 int nr_holes;
373 int ret;
374
375 nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
376
377 if (pblk_setup_partial_read(pblk, rqd, bio_init_idx, read_bitmap,
378 nr_holes))
379 return NVM_IO_ERR;
380
381 rqd->end_io = pblk_end_partial_read;
382
383 ret = pblk_submit_io(pblk, rqd);
384 if (ret) {
385 bio_put(rqd->bio);
386 pblk_err(pblk, "partial read IO submission failed\n");
387 goto err;
388 }
389
390 return NVM_IO_OK;
391
392err:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200393 pblk_err(pblk, "failed to perform partial read\n");
Heiner Litz11f6ad62018-07-13 10:48:44 +0200394
395 /* Free allocated pages in new bio */
396 pblk_bio_free_pages(pblk, rqd->bio, 0, rqd->bio->bi_vcnt);
Javier González7bd4d372017-10-13 14:46:23 +0200397 __pblk_end_io_read(pblk, rqd, false);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200398 return NVM_IO_ERR;
399}
400
Javier González87cc40b2018-06-01 16:41:11 +0200401static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd, struct bio *bio,
Javier González84454e62017-10-13 14:46:13 +0200402 sector_t lba, unsigned long *read_bitmap)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200403{
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200404 struct pblk_sec_meta *meta_list = rqd->meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200405 struct ppa_addr ppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200406
407 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
408
Matias Bjørling880eda52018-07-13 10:48:37 +0200409#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200410 atomic_long_inc(&pblk->inflight_reads);
411#endif
412
413retry:
414 if (pblk_ppa_empty(ppa)) {
415 WARN_ON(test_and_set_bit(0, read_bitmap));
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200416 meta_list[0].lba = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200417 return;
418 }
419
420 /* Try to read from write buffer. The address is later checked on the
421 * write buffer to prevent retrieving overwritten data.
422 */
423 if (pblk_addr_in_cache(ppa)) {
Javier González75cb8e92017-07-28 15:13:16 +0200424 if (!pblk_read_from_cache(pblk, bio, lba, ppa, 0, 1)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200425 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
426 goto retry;
427 }
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200428
Javier González7bd4d372017-10-13 14:46:23 +0200429 WARN_ON(test_and_set_bit(0, read_bitmap));
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200430 meta_list[0].lba = cpu_to_le64(lba);
431
Matias Bjørling880eda52018-07-13 10:48:37 +0200432#ifdef CONFIG_NVM_PBLK_DEBUG
Javier González7bd4d372017-10-13 14:46:23 +0200433 atomic_long_inc(&pblk->cache_reads);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200434#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200435 } else {
436 rqd->ppa_addr = ppa;
437 }
438}
439
440int pblk_submit_read(struct pblk *pblk, struct bio *bio)
441{
442 struct nvm_tgt_dev *dev = pblk->dev;
Javier González998ba622018-01-05 14:16:20 +0100443 struct request_queue *q = dev->q;
Javier González84454e62017-10-13 14:46:13 +0200444 sector_t blba = pblk_get_lba(bio);
Dan Carpenter5bf1e1ee2017-04-21 16:48:40 -0600445 unsigned int nr_secs = pblk_get_secs(bio);
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200446 struct pblk_g_ctx *r_ctx;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200447 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200448 unsigned int bio_init_idx;
Matias Bjørling921aebf2018-07-13 10:48:40 +0200449 DECLARE_BITMAP(read_bitmap, NVM_MAX_VLBA);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200450 int ret = NVM_IO_ERR;
451
Javier González84454e62017-10-13 14:46:13 +0200452 /* logic error: lba out-of-bounds. Ignore read request */
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200453 if (blba >= pblk->rl.nr_secs || nr_secs > NVM_MAX_VLBA) {
Javier González84454e62017-10-13 14:46:13 +0200454 WARN(1, "pblk: read lba out of bounds (lba:%llu, nr:%d)\n",
455 (unsigned long long)blba, nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200456 return NVM_IO_ERR;
Javier González84454e62017-10-13 14:46:13 +0200457 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200458
Michael Callahanddcf35d2018-07-18 04:47:39 -0700459 generic_start_io_acct(q, REQ_OP_READ, bio_sectors(bio),
460 &pblk->disk->part0);
Javier González998ba622018-01-05 14:16:20 +0100461
Matias Bjørling921aebf2018-07-13 10:48:40 +0200462 bitmap_zero(read_bitmap, nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200463
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200464 rqd = pblk_alloc_rqd(pblk, PBLK_READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200465
466 rqd->opcode = NVM_OP_PREAD;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200467 rqd->nr_ppas = nr_secs;
Javier González87cc40b2018-06-01 16:41:11 +0200468 rqd->bio = NULL; /* cloned bio if needed */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200469 rqd->private = pblk;
470 rqd->end_io = pblk_end_io_read;
471
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200472 r_ctx = nvm_rq_to_pdu(rqd);
Javier González998ba622018-01-05 14:16:20 +0100473 r_ctx->start_time = jiffies;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200474 r_ctx->lba = blba;
Javier González87cc40b2018-06-01 16:41:11 +0200475 r_ctx->private = bio; /* original bio */
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200476
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200477 /* Save the index for this bio's start. This is needed in case
478 * we need to fill a partial read.
479 */
480 bio_init_idx = pblk_get_bi_idx(bio);
481
Javier González63e38092017-06-26 11:57:24 +0200482 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
483 &rqd->dma_meta_list);
484 if (!rqd->meta_list) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200485 pblk_err(pblk, "not able to allocate ppa list\n");
Javier González63e38092017-06-26 11:57:24 +0200486 goto fail_rqd_free;
487 }
488
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200489 if (nr_secs > 1) {
Javier González63e38092017-06-26 11:57:24 +0200490 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
491 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200492
Matias Bjørling921aebf2018-07-13 10:48:40 +0200493 pblk_read_ppalist_rq(pblk, rqd, bio, blba, read_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494 } else {
Matias Bjørling921aebf2018-07-13 10:48:40 +0200495 pblk_read_rq(pblk, rqd, bio, blba, read_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200496 }
497
Matias Bjørling921aebf2018-07-13 10:48:40 +0200498 if (bitmap_full(read_bitmap, nr_secs)) {
Javier González588726d32017-06-26 11:57:29 +0200499 atomic_inc(&pblk->inflight_io);
Javier González7bd4d372017-10-13 14:46:23 +0200500 __pblk_end_io_read(pblk, rqd, false);
Javier González87cc40b2018-06-01 16:41:11 +0200501 return NVM_IO_DONE;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200502 }
503
504 /* All sectors are to be read from the device */
Matias Bjørling921aebf2018-07-13 10:48:40 +0200505 if (bitmap_empty(read_bitmap, rqd->nr_ppas)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200506 struct bio *int_bio = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200507
508 /* Clone read bio to deal with read errors internally */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400509 int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200510 if (!int_bio) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200511 pblk_err(pblk, "could not clone read bio\n");
Javier González998ba622018-01-05 14:16:20 +0100512 goto fail_end_io;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200513 }
514
515 rqd->bio = int_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200516
Javier Gonzáleze13f4212018-06-01 15:04:21 +0200517 if (pblk_submit_io(pblk, rqd)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200518 pblk_err(pblk, "read IO submission failed\n");
Javier Gonzáleze13f4212018-06-01 15:04:21 +0200519 ret = NVM_IO_ERR;
Javier González998ba622018-01-05 14:16:20 +0100520 goto fail_end_io;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200521 }
522
523 return NVM_IO_OK;
524 }
525
526 /* The read bio request could be partially filled by the write buffer,
527 * but there are some holes that need to be read from the drive.
528 */
Heiner Litz11f6ad62018-07-13 10:48:44 +0200529 ret = pblk_partial_read_bio(pblk, rqd, bio_init_idx, read_bitmap,
530 nr_secs);
531 if (ret)
532 goto fail_meta_free;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200533
Heiner Litz11f6ad62018-07-13 10:48:44 +0200534 return NVM_IO_OK;
535
536fail_meta_free:
537 nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200538fail_rqd_free:
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200539 pblk_free_rqd(pblk, rqd, PBLK_READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200540 return ret;
Javier González998ba622018-01-05 14:16:20 +0100541fail_end_io:
542 __pblk_end_io_read(pblk, rqd, false);
543 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200544}
545
546static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
547 struct pblk_line *line, u64 *lba_list,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200548 u64 *paddr_list_gc, unsigned int nr_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200549{
Matias Bjørlingafdc23c2018-10-09 13:11:37 +0200550 struct ppa_addr ppa_list_l2p[NVM_MAX_VLBA];
Javier Gonzálezd3401212017-10-13 14:46:14 +0200551 struct ppa_addr ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200552 int valid_secs = 0;
553 int i;
554
Javier Gonzálezd3401212017-10-13 14:46:14 +0200555 pblk_lookup_l2p_rand(pblk, ppa_list_l2p, lba_list, nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200556
557 for (i = 0; i < nr_secs; i++) {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200558 if (lba_list[i] == ADDR_EMPTY)
559 continue;
560
561 ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id);
562 if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) {
563 paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200564 continue;
565 }
566
Javier Gonzálezd3401212017-10-13 14:46:14 +0200567 rqd->ppa_list[valid_secs++] = ppa_list_l2p[i];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200568 }
569
Matias Bjørling880eda52018-07-13 10:48:37 +0200570#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200571 atomic_long_add(valid_secs, &pblk->inflight_reads);
572#endif
Javier Gonzálezd3401212017-10-13 14:46:14 +0200573
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200574 return valid_secs;
575}
576
577static int read_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200578 struct pblk_line *line, sector_t lba,
579 u64 paddr_gc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200580{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200581 struct ppa_addr ppa_l2p, ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582 int valid_secs = 0;
583
Dan Carpenter659226e2017-04-21 16:49:56 -0600584 if (lba == ADDR_EMPTY)
585 goto out;
586
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200587 /* logic error: lba out-of-bounds */
Dan Carpenter2a79efd2017-04-15 20:55:52 +0200588 if (lba >= pblk->rl.nr_secs) {
589 WARN(1, "pblk: read lba out of bounds\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200590 goto out;
591 }
592
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200593 spin_lock(&pblk->trans_lock);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200594 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200595 spin_unlock(&pblk->trans_lock);
596
Javier Gonzálezd3401212017-10-13 14:46:14 +0200597 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id);
598 if (!pblk_ppa_comp(ppa_l2p, ppa_gc))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200599 goto out;
600
Javier Gonzálezd3401212017-10-13 14:46:14 +0200601 rqd->ppa_addr = ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200602 valid_secs = 1;
603
Matias Bjørling880eda52018-07-13 10:48:37 +0200604#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200605 atomic_long_inc(&pblk->inflight_reads);
606#endif
607
608out:
609 return valid_secs;
610}
611
Javier Gonzálezd3401212017-10-13 14:46:14 +0200612int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200613{
614 struct nvm_tgt_dev *dev = pblk->dev;
615 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200616 struct bio *bio;
617 struct nvm_rq rqd;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200618 int data_len;
619 int ret = NVM_IO_OK;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200620
621 memset(&rqd, 0, sizeof(struct nvm_rq));
622
Javier González63e38092017-06-26 11:57:24 +0200623 rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
624 &rqd.dma_meta_list);
625 if (!rqd.meta_list)
Javier Gonzálezd3401212017-10-13 14:46:14 +0200626 return -ENOMEM;
Javier González63e38092017-06-26 11:57:24 +0200627
Javier Gonzálezd3401212017-10-13 14:46:14 +0200628 if (gc_rq->nr_secs > 1) {
Javier González63e38092017-06-26 11:57:24 +0200629 rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size;
630 rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200631
Javier Gonzálezd3401212017-10-13 14:46:14 +0200632 gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line,
633 gc_rq->lba_list,
634 gc_rq->paddr_list,
635 gc_rq->nr_secs);
636 if (gc_rq->secs_to_gc == 1)
Javier González63e38092017-06-26 11:57:24 +0200637 rqd.ppa_addr = rqd.ppa_list[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200638 } else {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200639 gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line,
640 gc_rq->lba_list[0],
641 gc_rq->paddr_list[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200642 }
643
Javier Gonzálezd3401212017-10-13 14:46:14 +0200644 if (!(gc_rq->secs_to_gc))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200645 goto out;
646
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200647 data_len = (gc_rq->secs_to_gc) * geo->csecs;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200648 bio = pblk_bio_map_addr(pblk, gc_rq->data, gc_rq->secs_to_gc, data_len,
Javier González7d327a92017-10-13 14:46:02 +0200649 PBLK_VMALLOC_META, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200650 if (IS_ERR(bio)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200651 pblk_err(pblk, "could not allocate GC bio (%lu)\n",
652 PTR_ERR(bio));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200653 goto err_free_dma;
654 }
655
656 bio->bi_iter.bi_sector = 0; /* internal bio */
657 bio_set_op_attrs(bio, REQ_OP_READ, 0);
658
659 rqd.opcode = NVM_OP_PREAD;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200660 rqd.nr_ppas = gc_rq->secs_to_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200661 rqd.bio = bio;
662
Javier González1a94b2d2017-10-13 14:46:47 +0200663 if (pblk_submit_io_sync(pblk, &rqd)) {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200664 ret = -EIO;
Matias Bjørling4e495a42018-07-13 10:48:42 +0200665 pblk_err(pblk, "GC read request failed\n");
Javier González7d327a92017-10-13 14:46:02 +0200666 goto err_free_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200667 }
668
Javier González03a34b22018-06-01 15:04:18 +0200669 pblk_read_check_rand(pblk, &rqd, gc_rq->lba_list, gc_rq->nr_secs);
Javier González310df582018-06-01 15:04:17 +0200670
Javier González588726d32017-06-26 11:57:29 +0200671 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200672
673 if (rqd.error) {
674 atomic_long_inc(&pblk->read_failed_gc);
Matias Bjørling880eda52018-07-13 10:48:37 +0200675#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200676 pblk_print_failed_rqd(pblk, &rqd, rqd.error);
677#endif
678 }
679
Matias Bjørling880eda52018-07-13 10:48:37 +0200680#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzálezd3401212017-10-13 14:46:14 +0200681 atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads);
682 atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads);
683 atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200684#endif
685
686out:
Javier González63e38092017-06-26 11:57:24 +0200687 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200688 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200689
Javier González7d327a92017-10-13 14:46:02 +0200690err_free_bio:
691 bio_put(bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200692err_free_dma:
Javier González63e38092017-06-26 11:57:24 +0200693 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200694 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200695}