blob: a465d9980df4cd2c12c51fa427a56b219fffe3c7 [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{
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
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ález84454e62017-10-13 14:46:13 +020042 sector_t blba, unsigned long *read_bitmap)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020043{
Javier Gonzáleza4809fe2017-10-13 14:46:22 +020044 struct pblk_sec_meta *meta_list = rqd->meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020045 struct bio *bio = rqd->bio;
46 struct ppa_addr ppas[PBLK_MAX_REQ_ADDRS];
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;
Javier Gonzálezdb7ada32017-06-26 11:57:13 +020082#ifdef CONFIG_NVM_DEBUG
83 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))
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
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200100#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
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200109 err = pblk_submit_io(pblk, rqd);
110 if (err)
111 return NVM_IO_ERR;
112
113 return NVM_IO_OK;
114}
115
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200116static void pblk_read_check(struct pblk *pblk, struct nvm_rq *rqd,
117 sector_t blba)
118{
119 struct pblk_sec_meta *meta_list = rqd->meta_list;
120 int nr_lbas = rqd->nr_ppas;
121 int i;
122
123 for (i = 0; i < nr_lbas; i++) {
124 u64 lba = le64_to_cpu(meta_list[i].lba);
125
126 if (lba == ADDR_EMPTY)
127 continue;
128
129 WARN(lba != blba + i, "pblk: corrupted read LBA\n");
130 }
131}
132
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200133static void pblk_end_io_read(struct nvm_rq *rqd)
134{
135 struct pblk *pblk = rqd->private;
Javier González084ec9b2017-06-26 16:27:13 -0600136 struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200137 struct bio *bio = rqd->bio;
138
139 if (rqd->error)
140 pblk_log_read_err(pblk, rqd);
141#ifdef CONFIG_NVM_DEBUG
142 else
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200143 WARN_ONCE(bio->bi_status, "pblk: corrupted read error\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200144#endif
145
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200146 pblk_read_check(pblk, rqd, r_ctx->lba);
147
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200148 bio_put(bio);
Javier González084ec9b2017-06-26 16:27:13 -0600149 if (r_ctx->private) {
150 struct bio *orig_bio = r_ctx->private;
151
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200152#ifdef CONFIG_NVM_DEBUG
Javier González084ec9b2017-06-26 16:27:13 -0600153 WARN_ONCE(orig_bio->bi_status, "pblk: corrupted read bio\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200154#endif
Javier González084ec9b2017-06-26 16:27:13 -0600155 bio_endio(orig_bio);
156 bio_put(orig_bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200157 }
158
159#ifdef CONFIG_NVM_DEBUG
160 atomic_long_add(rqd->nr_ppas, &pblk->sync_reads);
161 atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads);
162#endif
163
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200164 pblk_free_rqd(pblk, rqd, PBLK_READ);
Javier González588726d32017-06-26 11:57:29 +0200165 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200166}
167
168static int pblk_fill_partial_read_bio(struct pblk *pblk, struct nvm_rq *rqd,
169 unsigned int bio_init_idx,
170 unsigned long *read_bitmap)
171{
172 struct bio *new_bio, *bio = rqd->bio;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200173 struct pblk_sec_meta *meta_list = rqd->meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200174 struct bio_vec src_bv, dst_bv;
175 void *ppa_ptr = NULL;
176 void *src_p, *dst_p;
177 dma_addr_t dma_ppa_list = 0;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200178 __le64 *lba_list_mem, *lba_list_media;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200179 int nr_secs = rqd->nr_ppas;
180 int nr_holes = nr_secs - bitmap_weight(read_bitmap, nr_secs);
181 int i, ret, hole;
182 DECLARE_COMPLETION_ONSTACK(wait);
183
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200184 /* Re-use allocated memory for intermediate lbas */
185 lba_list_mem = (((void *)rqd->ppa_list) + pblk_dma_ppa_size);
186 lba_list_media = (((void *)rqd->ppa_list) + 2 * pblk_dma_ppa_size);
187
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200188 new_bio = bio_alloc(GFP_KERNEL, nr_holes);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200189
190 if (pblk_bio_add_pages(pblk, new_bio, GFP_KERNEL, nr_holes))
191 goto err;
192
193 if (nr_holes != new_bio->bi_vcnt) {
194 pr_err("pblk: malformed bio\n");
195 goto err;
196 }
197
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200198 for (i = 0; i < nr_secs; i++)
199 lba_list_mem[i] = meta_list[i].lba;
200
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200201 new_bio->bi_iter.bi_sector = 0; /* internal bio */
202 bio_set_op_attrs(new_bio, REQ_OP_READ, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200203
204 rqd->bio = new_bio;
205 rqd->nr_ppas = nr_holes;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200206 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
Javier González26532ee2017-10-13 14:46:21 +0200207 rqd->end_io = pblk_end_io_sync;
208 rqd->private = &wait;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200209
210 if (unlikely(nr_secs > 1 && nr_holes == 1)) {
211 ppa_ptr = rqd->ppa_list;
212 dma_ppa_list = rqd->dma_ppa_list;
213 rqd->ppa_addr = rqd->ppa_list[0];
214 }
215
216 ret = pblk_submit_read_io(pblk, rqd);
217 if (ret) {
218 bio_put(rqd->bio);
219 pr_err("pblk: read IO submission failed\n");
220 goto err;
221 }
222
223 if (!wait_for_completion_io_timeout(&wait,
224 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
225 pr_err("pblk: partial read I/O timed out\n");
226 }
227
228 if (rqd->error) {
229 atomic_long_inc(&pblk->read_failed);
230#ifdef CONFIG_NVM_DEBUG
231 pblk_print_failed_rqd(pblk, rqd, rqd->error);
232#endif
233 }
234
235 if (unlikely(nr_secs > 1 && nr_holes == 1)) {
236 rqd->ppa_list = ppa_ptr;
237 rqd->dma_ppa_list = dma_ppa_list;
238 }
239
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200240 for (i = 0; i < nr_secs; i++) {
241 lba_list_media[i] = meta_list[i].lba;
242 meta_list[i].lba = lba_list_mem[i];
243 }
244
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200245 /* Fill the holes in the original bio */
246 i = 0;
247 hole = find_first_zero_bit(read_bitmap, nr_secs);
248 do {
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200249 meta_list[hole].lba = lba_list_media[i];
250
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200251 src_bv = new_bio->bi_io_vec[i++];
252 dst_bv = bio->bi_io_vec[bio_init_idx + hole];
253
254 src_p = kmap_atomic(src_bv.bv_page);
255 dst_p = kmap_atomic(dst_bv.bv_page);
256
257 memcpy(dst_p + dst_bv.bv_offset,
258 src_p + src_bv.bv_offset,
259 PBLK_EXPOSED_PAGE_SIZE);
260
261 kunmap_atomic(src_p);
262 kunmap_atomic(dst_p);
263
Javier Gonzálezbd432412017-10-13 14:46:06 +0200264 mempool_free(src_bv.bv_page, pblk->page_bio_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200265
266 hole = find_next_zero_bit(read_bitmap, nr_secs, hole + 1);
267 } while (hole < nr_secs);
268
269 bio_put(new_bio);
270
271 /* Complete the original bio and associated request */
272 rqd->bio = bio;
273 rqd->nr_ppas = nr_secs;
274 rqd->private = pblk;
275
276 bio_endio(bio);
277 pblk_end_io_read(rqd);
278 return NVM_IO_OK;
279
280err:
281 /* Free allocated pages in new bio */
282 pblk_bio_free_pages(pblk, bio, 0, new_bio->bi_vcnt);
283 rqd->private = pblk;
284 pblk_end_io_read(rqd);
285 return NVM_IO_ERR;
286}
287
288static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd,
Javier González84454e62017-10-13 14:46:13 +0200289 sector_t lba, unsigned long *read_bitmap)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200290{
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200291 struct pblk_sec_meta *meta_list = rqd->meta_list;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200292 struct bio *bio = rqd->bio;
293 struct ppa_addr ppa;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200294
295 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
296
297#ifdef CONFIG_NVM_DEBUG
298 atomic_long_inc(&pblk->inflight_reads);
299#endif
300
301retry:
302 if (pblk_ppa_empty(ppa)) {
303 WARN_ON(test_and_set_bit(0, read_bitmap));
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200304 meta_list[0].lba = cpu_to_le64(ADDR_EMPTY);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200305 return;
306 }
307
308 /* Try to read from write buffer. The address is later checked on the
309 * write buffer to prevent retrieving overwritten data.
310 */
311 if (pblk_addr_in_cache(ppa)) {
Javier González75cb8e92017-07-28 15:13:16 +0200312 if (!pblk_read_from_cache(pblk, bio, lba, ppa, 0, 1)) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200313 pblk_lookup_l2p_seq(pblk, &ppa, lba, 1);
314 goto retry;
315 }
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200316
317 meta_list[0].lba = cpu_to_le64(lba);
318
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200319 WARN_ON(test_and_set_bit(0, read_bitmap));
Javier Gonzálezdb7ada32017-06-26 11:57:13 +0200320#ifdef CONFIG_NVM_DEBUG
321 atomic_long_inc(&pblk->cache_reads);
322#endif
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200323 } else {
324 rqd->ppa_addr = ppa;
325 }
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200326
327 rqd->flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200328}
329
330int pblk_submit_read(struct pblk *pblk, struct bio *bio)
331{
332 struct nvm_tgt_dev *dev = pblk->dev;
Javier González84454e62017-10-13 14:46:13 +0200333 sector_t blba = pblk_get_lba(bio);
Dan Carpenter5bf1e1ee2017-04-21 16:48:40 -0600334 unsigned int nr_secs = pblk_get_secs(bio);
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200335 struct pblk_g_ctx *r_ctx;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200336 struct nvm_rq *rqd;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200337 unsigned int bio_init_idx;
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200338 unsigned long read_bitmap; /* Max 64 ppas per request */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200339 int ret = NVM_IO_ERR;
340
Javier González84454e62017-10-13 14:46:13 +0200341 /* logic error: lba out-of-bounds. Ignore read request */
342 if (blba >= pblk->rl.nr_secs || nr_secs > PBLK_MAX_REQ_ADDRS) {
343 WARN(1, "pblk: read lba out of bounds (lba:%llu, nr:%d)\n",
344 (unsigned long long)blba, nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200345 return NVM_IO_ERR;
Javier González84454e62017-10-13 14:46:13 +0200346 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200347
348 bitmap_zero(&read_bitmap, nr_secs);
349
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200350 rqd = pblk_alloc_rqd(pblk, PBLK_READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200351
352 rqd->opcode = NVM_OP_PREAD;
353 rqd->bio = bio;
354 rqd->nr_ppas = nr_secs;
355 rqd->private = pblk;
356 rqd->end_io = pblk_end_io_read;
357
Javier Gonzáleza4809fe2017-10-13 14:46:22 +0200358 r_ctx = nvm_rq_to_pdu(rqd);
359 r_ctx->lba = blba;
360
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200361 /* Save the index for this bio's start. This is needed in case
362 * we need to fill a partial read.
363 */
364 bio_init_idx = pblk_get_bi_idx(bio);
365
Javier González63e38092017-06-26 11:57:24 +0200366 rqd->meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
367 &rqd->dma_meta_list);
368 if (!rqd->meta_list) {
369 pr_err("pblk: not able to allocate ppa list\n");
370 goto fail_rqd_free;
371 }
372
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200373 if (nr_secs > 1) {
Javier González63e38092017-06-26 11:57:24 +0200374 rqd->ppa_list = rqd->meta_list + pblk_dma_meta_size;
375 rqd->dma_ppa_list = rqd->dma_meta_list + pblk_dma_meta_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200376
Javier González84454e62017-10-13 14:46:13 +0200377 pblk_read_ppalist_rq(pblk, rqd, blba, &read_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200378 } else {
Javier González84454e62017-10-13 14:46:13 +0200379 pblk_read_rq(pblk, rqd, blba, &read_bitmap);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200380 }
381
382 bio_get(bio);
383 if (bitmap_full(&read_bitmap, nr_secs)) {
384 bio_endio(bio);
Javier González588726d32017-06-26 11:57:29 +0200385 atomic_inc(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200386 pblk_end_io_read(rqd);
387 return NVM_IO_OK;
388 }
389
390 /* All sectors are to be read from the device */
391 if (bitmap_empty(&read_bitmap, rqd->nr_ppas)) {
392 struct bio *int_bio = NULL;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200393
394 /* Clone read bio to deal with read errors internally */
NeilBrownb25d5232017-06-18 14:38:58 +1000395 int_bio = bio_clone_fast(bio, GFP_KERNEL, pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200396 if (!int_bio) {
397 pr_err("pblk: could not clone read bio\n");
398 return NVM_IO_ERR;
399 }
400
401 rqd->bio = int_bio;
Javier González084ec9b2017-06-26 16:27:13 -0600402 r_ctx->private = bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200403
404 ret = pblk_submit_read_io(pblk, rqd);
405 if (ret) {
406 pr_err("pblk: read IO submission failed\n");
407 if (int_bio)
408 bio_put(int_bio);
409 return ret;
410 }
411
412 return NVM_IO_OK;
413 }
414
415 /* The read bio request could be partially filled by the write buffer,
416 * but there are some holes that need to be read from the drive.
417 */
418 ret = pblk_fill_partial_read_bio(pblk, rqd, bio_init_idx, &read_bitmap);
419 if (ret) {
420 pr_err("pblk: failed to perform partial read\n");
421 return ret;
422 }
423
424 return NVM_IO_OK;
425
426fail_rqd_free:
Javier Gonzáleze2cddf22017-10-13 14:46:19 +0200427 pblk_free_rqd(pblk, rqd, PBLK_READ);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200428 return ret;
429}
430
431static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
432 struct pblk_line *line, u64 *lba_list,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200433 u64 *paddr_list_gc, unsigned int nr_secs)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200434{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200435 struct ppa_addr ppa_list_l2p[PBLK_MAX_REQ_ADDRS];
436 struct ppa_addr ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200437 int valid_secs = 0;
438 int i;
439
Javier Gonzálezd3401212017-10-13 14:46:14 +0200440 pblk_lookup_l2p_rand(pblk, ppa_list_l2p, lba_list, nr_secs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200441
442 for (i = 0; i < nr_secs; i++) {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200443 if (lba_list[i] == ADDR_EMPTY)
444 continue;
445
446 ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id);
447 if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) {
448 paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200449 continue;
450 }
451
Javier Gonzálezd3401212017-10-13 14:46:14 +0200452 rqd->ppa_list[valid_secs++] = ppa_list_l2p[i];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200453 }
454
455#ifdef CONFIG_NVM_DEBUG
456 atomic_long_add(valid_secs, &pblk->inflight_reads);
457#endif
Javier Gonzálezd3401212017-10-13 14:46:14 +0200458
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200459 return valid_secs;
460}
461
462static int read_rq_gc(struct pblk *pblk, struct nvm_rq *rqd,
Javier Gonzálezd3401212017-10-13 14:46:14 +0200463 struct pblk_line *line, sector_t lba,
464 u64 paddr_gc)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200465{
Javier Gonzálezd3401212017-10-13 14:46:14 +0200466 struct ppa_addr ppa_l2p, ppa_gc;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200467 int valid_secs = 0;
468
Dan Carpenter659226e2017-04-21 16:49:56 -0600469 if (lba == ADDR_EMPTY)
470 goto out;
471
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200472 /* logic error: lba out-of-bounds */
Dan Carpenter2a79efd2017-04-15 20:55:52 +0200473 if (lba >= pblk->rl.nr_secs) {
474 WARN(1, "pblk: read lba out of bounds\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200475 goto out;
476 }
477
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200478 spin_lock(&pblk->trans_lock);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200479 ppa_l2p = pblk_trans_map_get(pblk, lba);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200480 spin_unlock(&pblk->trans_lock);
481
Javier Gonzálezd3401212017-10-13 14:46:14 +0200482 ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id);
483 if (!pblk_ppa_comp(ppa_l2p, ppa_gc))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484 goto out;
485
Javier Gonzálezd3401212017-10-13 14:46:14 +0200486 rqd->ppa_addr = ppa_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200487 valid_secs = 1;
488
489#ifdef CONFIG_NVM_DEBUG
490 atomic_long_inc(&pblk->inflight_reads);
491#endif
492
493out:
494 return valid_secs;
495}
496
Javier Gonzálezd3401212017-10-13 14:46:14 +0200497int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200498{
499 struct nvm_tgt_dev *dev = pblk->dev;
500 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200501 struct bio *bio;
502 struct nvm_rq rqd;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200503 int data_len;
504 int ret = NVM_IO_OK;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200505 DECLARE_COMPLETION_ONSTACK(wait);
506
507 memset(&rqd, 0, sizeof(struct nvm_rq));
508
Javier González63e38092017-06-26 11:57:24 +0200509 rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL,
510 &rqd.dma_meta_list);
511 if (!rqd.meta_list)
Javier Gonzálezd3401212017-10-13 14:46:14 +0200512 return -ENOMEM;
Javier González63e38092017-06-26 11:57:24 +0200513
Javier Gonzálezd3401212017-10-13 14:46:14 +0200514 if (gc_rq->nr_secs > 1) {
Javier González63e38092017-06-26 11:57:24 +0200515 rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size;
516 rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200517
Javier Gonzálezd3401212017-10-13 14:46:14 +0200518 gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line,
519 gc_rq->lba_list,
520 gc_rq->paddr_list,
521 gc_rq->nr_secs);
522 if (gc_rq->secs_to_gc == 1)
Javier González63e38092017-06-26 11:57:24 +0200523 rqd.ppa_addr = rqd.ppa_list[0];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200524 } else {
Javier Gonzálezd3401212017-10-13 14:46:14 +0200525 gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line,
526 gc_rq->lba_list[0],
527 gc_rq->paddr_list[0]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200528 }
529
Javier Gonzálezd3401212017-10-13 14:46:14 +0200530 if (!(gc_rq->secs_to_gc))
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200531 goto out;
532
Javier Gonzálezd3401212017-10-13 14:46:14 +0200533 data_len = (gc_rq->secs_to_gc) * geo->sec_size;
534 bio = pblk_bio_map_addr(pblk, gc_rq->data, gc_rq->secs_to_gc, data_len,
Javier González7d327a92017-10-13 14:46:02 +0200535 PBLK_VMALLOC_META, GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200536 if (IS_ERR(bio)) {
537 pr_err("pblk: could not allocate GC bio (%lu)\n", PTR_ERR(bio));
538 goto err_free_dma;
539 }
540
541 bio->bi_iter.bi_sector = 0; /* internal bio */
542 bio_set_op_attrs(bio, REQ_OP_READ, 0);
543
544 rqd.opcode = NVM_OP_PREAD;
545 rqd.end_io = pblk_end_io_sync;
546 rqd.private = &wait;
Javier Gonzálezd3401212017-10-13 14:46:14 +0200547 rqd.nr_ppas = gc_rq->secs_to_gc;
Javier Gonzálezf9c10152017-06-26 11:57:20 +0200548 rqd.flags = pblk_set_read_mode(pblk, PBLK_READ_RANDOM);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200549 rqd.bio = bio;
550
Javier Gonzálezd3401212017-10-13 14:46:14 +0200551 if (pblk_submit_read_io(pblk, &rqd)) {
552 ret = -EIO;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200553 pr_err("pblk: GC read request failed\n");
Javier González7d327a92017-10-13 14:46:02 +0200554 goto err_free_bio;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200555 }
556
557 if (!wait_for_completion_io_timeout(&wait,
558 msecs_to_jiffies(PBLK_COMMAND_TIMEOUT_MS))) {
559 pr_err("pblk: GC read I/O timed out\n");
560 }
Javier González588726d32017-06-26 11:57:29 +0200561 atomic_dec(&pblk->inflight_io);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200562
563 if (rqd.error) {
564 atomic_long_inc(&pblk->read_failed_gc);
565#ifdef CONFIG_NVM_DEBUG
566 pblk_print_failed_rqd(pblk, &rqd, rqd.error);
567#endif
568 }
569
570#ifdef CONFIG_NVM_DEBUG
Javier Gonzálezd3401212017-10-13 14:46:14 +0200571 atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads);
572 atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads);
573 atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200574#endif
575
576out:
Javier González63e38092017-06-26 11:57:24 +0200577 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200578 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200579
Javier González7d327a92017-10-13 14:46:02 +0200580err_free_bio:
581 bio_put(bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582err_free_dma:
Javier González63e38092017-06-26 11:57:24 +0200583 nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list);
Javier Gonzálezd3401212017-10-13 14:46:14 +0200584 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200585}