David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 CNEX Labs |
| 4 | * Initial release: Javier Gonzalez <javier@cnexlabs.com> |
| 5 | * Matias Bjorling <matias@cnexlabs.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version |
| 9 | * 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * pblk-read.c - pblk's read path |
| 17 | */ |
| 18 | |
| 19 | #include "pblk.h" |
| 20 | |
| 21 | /* |
| 22 | * There is no guarantee that the value read from cache has not been updated and |
| 23 | * resides at another location in the cache. We guarantee though that if the |
| 24 | * value is read from the cache, it belongs to the mapped lba. In order to |
| 25 | * guarantee and order between writes and reads are ordered, a flush must be |
| 26 | * issued. |
| 27 | */ |
| 28 | static int pblk_read_from_cache(struct pblk *pblk, struct bio *bio, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 29 | sector_t lba, struct ppa_addr ppa) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 30 | { |
| 31 | #ifdef CONFIG_NVM_PBLK_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 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 37 | return pblk_rb_copy_to_bio(&pblk->rwb, bio, lba, ppa); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 38 | } |
| 39 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 40 | static int pblk_read_ppalist_rq(struct pblk *pblk, struct nvm_rq *rqd, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | struct bio *bio, sector_t blba, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 42 | bool *from_cache) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 43 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 44 | void *meta_list = rqd->meta_list; |
| 45 | int nr_secs, i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 46 | |
| 47 | retry: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 48 | nr_secs = pblk_lookup_l2p_seq(pblk, rqd->ppa_list, blba, rqd->nr_ppas, |
| 49 | from_cache); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 50 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 51 | if (!*from_cache) |
| 52 | goto end; |
| 53 | |
| 54 | for (i = 0; i < nr_secs; i++) { |
| 55 | struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i); |
| 56 | sector_t lba = blba + i; |
| 57 | |
| 58 | if (pblk_ppa_empty(rqd->ppa_list[i])) { |
| 59 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
| 60 | |
| 61 | meta->lba = addr_empty; |
| 62 | } else if (pblk_addr_in_cache(rqd->ppa_list[i])) { |
| 63 | /* |
| 64 | * Try to read from write buffer. The address is later |
| 65 | * checked on the write buffer to prevent retrieving |
| 66 | * overwritten data. |
| 67 | */ |
| 68 | if (!pblk_read_from_cache(pblk, bio, lba, |
| 69 | rqd->ppa_list[i])) { |
| 70 | if (i == 0) { |
| 71 | /* |
| 72 | * We didn't call with bio_advance() |
| 73 | * yet, so we can just retry. |
| 74 | */ |
| 75 | goto retry; |
| 76 | } else { |
| 77 | /* |
| 78 | * We already call bio_advance() |
| 79 | * so we cannot retry and we need |
| 80 | * to quit that function in order |
| 81 | * to allow caller to handle the bio |
| 82 | * splitting in the current sector |
| 83 | * position. |
| 84 | */ |
| 85 | nr_secs = i; |
| 86 | goto end; |
| 87 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 88 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 89 | meta->lba = cpu_to_le64(lba); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 90 | #ifdef CONFIG_NVM_PBLK_DEBUG |
| 91 | atomic_long_inc(&pblk->cache_reads); |
| 92 | #endif |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 93 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 94 | bio_advance(bio, PBLK_EXPOSED_PAGE_SIZE); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 95 | } |
| 96 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 97 | end: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 98 | if (pblk_io_aligned(pblk, nr_secs)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 99 | rqd->is_seq = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 100 | |
| 101 | #ifdef CONFIG_NVM_PBLK_DEBUG |
| 102 | atomic_long_add(nr_secs, &pblk->inflight_reads); |
| 103 | #endif |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 104 | |
| 105 | return nr_secs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
| 109 | static void pblk_read_check_seq(struct pblk *pblk, struct nvm_rq *rqd, |
| 110 | sector_t blba) |
| 111 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 112 | void *meta_list = rqd->meta_list; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 113 | int nr_lbas = rqd->nr_ppas; |
| 114 | int i; |
| 115 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 116 | if (!pblk_is_oob_meta_supported(pblk)) |
| 117 | return; |
| 118 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 119 | for (i = 0; i < nr_lbas; i++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 120 | struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i); |
| 121 | u64 lba = le64_to_cpu(meta->lba); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 122 | |
| 123 | if (lba == ADDR_EMPTY) |
| 124 | continue; |
| 125 | |
| 126 | if (lba != blba + i) { |
| 127 | #ifdef CONFIG_NVM_PBLK_DEBUG |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 128 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 129 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 130 | print_ppa(pblk, &ppa_list[i], "seq", i); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 131 | #endif |
| 132 | pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n", |
| 133 | lba, (u64)blba + i); |
| 134 | WARN_ON(1); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * There can be holes in the lba list. |
| 141 | */ |
| 142 | static void pblk_read_check_rand(struct pblk *pblk, struct nvm_rq *rqd, |
| 143 | u64 *lba_list, int nr_lbas) |
| 144 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 145 | void *meta_lba_list = rqd->meta_list; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 146 | int i, j; |
| 147 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 148 | if (!pblk_is_oob_meta_supported(pblk)) |
| 149 | return; |
| 150 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 151 | for (i = 0, j = 0; i < nr_lbas; i++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 152 | struct pblk_sec_meta *meta = pblk_get_meta(pblk, |
| 153 | meta_lba_list, j); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 154 | u64 lba = lba_list[i]; |
| 155 | u64 meta_lba; |
| 156 | |
| 157 | if (lba == ADDR_EMPTY) |
| 158 | continue; |
| 159 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 160 | meta_lba = le64_to_cpu(meta->lba); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 161 | |
| 162 | if (lba != meta_lba) { |
| 163 | #ifdef CONFIG_NVM_PBLK_DEBUG |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 164 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 165 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 166 | print_ppa(pblk, &ppa_list[j], "rnd", j); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 167 | #endif |
| 168 | pblk_err(pblk, "corrupted read LBA (%llu/%llu)\n", |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 169 | meta_lba, lba); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 170 | WARN_ON(1); |
| 171 | } |
| 172 | |
| 173 | j++; |
| 174 | } |
| 175 | |
| 176 | WARN_ONCE(j != rqd->nr_ppas, "pblk: corrupted random request\n"); |
| 177 | } |
| 178 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 179 | static void pblk_end_user_read(struct bio *bio, int error) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 180 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 181 | if (error && error != NVM_RSP_WARN_HIGHECC) |
| 182 | bio_io_error(bio); |
| 183 | else |
| 184 | bio_endio(bio); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static void __pblk_end_io_read(struct pblk *pblk, struct nvm_rq *rqd, |
| 188 | bool put_line) |
| 189 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 190 | struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd); |
| 191 | struct bio *int_bio = rqd->bio; |
| 192 | unsigned long start_time = r_ctx->start_time; |
| 193 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 194 | bio_end_io_acct(int_bio, start_time); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 195 | |
| 196 | if (rqd->error) |
| 197 | pblk_log_read_err(pblk, rqd); |
| 198 | |
| 199 | pblk_read_check_seq(pblk, rqd, r_ctx->lba); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 200 | bio_put(int_bio); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 201 | |
| 202 | if (put_line) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 203 | pblk_rq_to_line_put(pblk, rqd); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 204 | |
| 205 | #ifdef CONFIG_NVM_PBLK_DEBUG |
| 206 | atomic_long_add(rqd->nr_ppas, &pblk->sync_reads); |
| 207 | atomic_long_sub(rqd->nr_ppas, &pblk->inflight_reads); |
| 208 | #endif |
| 209 | |
| 210 | pblk_free_rqd(pblk, rqd, PBLK_READ); |
| 211 | atomic_dec(&pblk->inflight_io); |
| 212 | } |
| 213 | |
| 214 | static void pblk_end_io_read(struct nvm_rq *rqd) |
| 215 | { |
| 216 | struct pblk *pblk = rqd->private; |
| 217 | struct pblk_g_ctx *r_ctx = nvm_rq_to_pdu(rqd); |
| 218 | struct bio *bio = (struct bio *)r_ctx->private; |
| 219 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 220 | pblk_end_user_read(bio, rqd->error); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 221 | __pblk_end_io_read(pblk, rqd, true); |
| 222 | } |
| 223 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 224 | static void pblk_read_rq(struct pblk *pblk, struct nvm_rq *rqd, struct bio *bio, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 225 | sector_t lba, bool *from_cache) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 226 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 227 | struct pblk_sec_meta *meta = pblk_get_meta(pblk, rqd->meta_list, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 228 | struct ppa_addr ppa; |
| 229 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 230 | pblk_lookup_l2p_seq(pblk, &ppa, lba, 1, from_cache); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 231 | |
| 232 | #ifdef CONFIG_NVM_PBLK_DEBUG |
| 233 | atomic_long_inc(&pblk->inflight_reads); |
| 234 | #endif |
| 235 | |
| 236 | retry: |
| 237 | if (pblk_ppa_empty(ppa)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 238 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
| 239 | |
| 240 | meta->lba = addr_empty; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
| 244 | /* Try to read from write buffer. The address is later checked on the |
| 245 | * write buffer to prevent retrieving overwritten data. |
| 246 | */ |
| 247 | if (pblk_addr_in_cache(ppa)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 248 | if (!pblk_read_from_cache(pblk, bio, lba, ppa)) { |
| 249 | pblk_lookup_l2p_seq(pblk, &ppa, lba, 1, from_cache); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 250 | goto retry; |
| 251 | } |
| 252 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 253 | meta->lba = cpu_to_le64(lba); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 254 | |
| 255 | #ifdef CONFIG_NVM_PBLK_DEBUG |
| 256 | atomic_long_inc(&pblk->cache_reads); |
| 257 | #endif |
| 258 | } else { |
| 259 | rqd->ppa_addr = ppa; |
| 260 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 261 | } |
| 262 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 263 | void pblk_submit_read(struct pblk *pblk, struct bio *bio) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 264 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 265 | sector_t blba = pblk_get_lba(bio); |
| 266 | unsigned int nr_secs = pblk_get_secs(bio); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 267 | bool from_cache; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 268 | struct pblk_g_ctx *r_ctx; |
| 269 | struct nvm_rq *rqd; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 270 | struct bio *int_bio, *split_bio; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 271 | unsigned long start_time; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 272 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 273 | start_time = bio_start_io_acct(bio); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 274 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 275 | rqd = pblk_alloc_rqd(pblk, PBLK_READ); |
| 276 | |
| 277 | rqd->opcode = NVM_OP_PREAD; |
| 278 | rqd->nr_ppas = nr_secs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 279 | rqd->private = pblk; |
| 280 | rqd->end_io = pblk_end_io_read; |
| 281 | |
| 282 | r_ctx = nvm_rq_to_pdu(rqd); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 283 | r_ctx->start_time = start_time; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 284 | r_ctx->lba = blba; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 285 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 286 | if (pblk_alloc_rqd_meta(pblk, rqd)) { |
| 287 | bio_io_error(bio); |
| 288 | pblk_free_rqd(pblk, rqd, PBLK_READ); |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | /* Clone read bio to deal internally with: |
| 293 | * -read errors when reading from drive |
| 294 | * -bio_advance() calls during cache reads |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 295 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 296 | int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 297 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 298 | if (nr_secs > 1) |
| 299 | nr_secs = pblk_read_ppalist_rq(pblk, rqd, int_bio, blba, |
| 300 | &from_cache); |
| 301 | else |
| 302 | pblk_read_rq(pblk, rqd, int_bio, blba, &from_cache); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 303 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 304 | split_retry: |
| 305 | r_ctx->private = bio; /* original bio */ |
| 306 | rqd->bio = int_bio; /* internal bio */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 307 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 308 | if (from_cache && nr_secs == rqd->nr_ppas) { |
| 309 | /* All data was read from cache, we can complete the IO. */ |
| 310 | pblk_end_user_read(bio, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 311 | atomic_inc(&pblk->inflight_io); |
| 312 | __pblk_end_io_read(pblk, rqd, false); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 313 | } else if (nr_secs != rqd->nr_ppas) { |
| 314 | /* The read bio request could be partially filled by the write |
| 315 | * buffer, but there are some holes that need to be read from |
| 316 | * the drive. In order to handle this, we will use block layer |
| 317 | * mechanism to split this request in to smaller ones and make |
| 318 | * a chain of it. |
| 319 | */ |
| 320 | split_bio = bio_split(bio, nr_secs * NR_PHY_IN_LOG, GFP_KERNEL, |
| 321 | &pblk_bio_set); |
| 322 | bio_chain(split_bio, bio); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 323 | submit_bio_noacct(bio); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 324 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 325 | /* New bio contains first N sectors of the previous one, so |
| 326 | * we can continue to use existing rqd, but we need to shrink |
| 327 | * the number of PPAs in it. New bio is also guaranteed that |
| 328 | * it contains only either data from cache or from drive, newer |
| 329 | * mix of them. |
| 330 | */ |
| 331 | bio = split_bio; |
| 332 | rqd->nr_ppas = nr_secs; |
| 333 | if (rqd->nr_ppas == 1) |
| 334 | rqd->ppa_addr = rqd->ppa_list[0]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 335 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 336 | /* Recreate int_bio - existing might have some needed internal |
| 337 | * fields modified already. |
| 338 | */ |
| 339 | bio_put(int_bio); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 340 | int_bio = bio_clone_fast(bio, GFP_KERNEL, &pblk_bio_set); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 341 | goto split_retry; |
| 342 | } else if (pblk_submit_io(pblk, rqd, NULL)) { |
| 343 | /* Submitting IO to drive failed, let's report an error */ |
| 344 | rqd->error = -ENODEV; |
| 345 | pblk_end_io_read(rqd); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 346 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static int read_ppalist_rq_gc(struct pblk *pblk, struct nvm_rq *rqd, |
| 350 | struct pblk_line *line, u64 *lba_list, |
| 351 | u64 *paddr_list_gc, unsigned int nr_secs) |
| 352 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 353 | struct ppa_addr ppa_list_l2p[NVM_MAX_VLBA]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 354 | struct ppa_addr ppa_gc; |
| 355 | int valid_secs = 0; |
| 356 | int i; |
| 357 | |
| 358 | pblk_lookup_l2p_rand(pblk, ppa_list_l2p, lba_list, nr_secs); |
| 359 | |
| 360 | for (i = 0; i < nr_secs; i++) { |
| 361 | if (lba_list[i] == ADDR_EMPTY) |
| 362 | continue; |
| 363 | |
| 364 | ppa_gc = addr_to_gen_ppa(pblk, paddr_list_gc[i], line->id); |
| 365 | if (!pblk_ppa_comp(ppa_list_l2p[i], ppa_gc)) { |
| 366 | paddr_list_gc[i] = lba_list[i] = ADDR_EMPTY; |
| 367 | continue; |
| 368 | } |
| 369 | |
| 370 | rqd->ppa_list[valid_secs++] = ppa_list_l2p[i]; |
| 371 | } |
| 372 | |
| 373 | #ifdef CONFIG_NVM_PBLK_DEBUG |
| 374 | atomic_long_add(valid_secs, &pblk->inflight_reads); |
| 375 | #endif |
| 376 | |
| 377 | return valid_secs; |
| 378 | } |
| 379 | |
| 380 | static int read_rq_gc(struct pblk *pblk, struct nvm_rq *rqd, |
| 381 | struct pblk_line *line, sector_t lba, |
| 382 | u64 paddr_gc) |
| 383 | { |
| 384 | struct ppa_addr ppa_l2p, ppa_gc; |
| 385 | int valid_secs = 0; |
| 386 | |
| 387 | if (lba == ADDR_EMPTY) |
| 388 | goto out; |
| 389 | |
| 390 | /* logic error: lba out-of-bounds */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 391 | if (lba >= pblk->capacity) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 392 | WARN(1, "pblk: read lba out of bounds\n"); |
| 393 | goto out; |
| 394 | } |
| 395 | |
| 396 | spin_lock(&pblk->trans_lock); |
| 397 | ppa_l2p = pblk_trans_map_get(pblk, lba); |
| 398 | spin_unlock(&pblk->trans_lock); |
| 399 | |
| 400 | ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, line->id); |
| 401 | if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) |
| 402 | goto out; |
| 403 | |
| 404 | rqd->ppa_addr = ppa_l2p; |
| 405 | valid_secs = 1; |
| 406 | |
| 407 | #ifdef CONFIG_NVM_PBLK_DEBUG |
| 408 | atomic_long_inc(&pblk->inflight_reads); |
| 409 | #endif |
| 410 | |
| 411 | out: |
| 412 | return valid_secs; |
| 413 | } |
| 414 | |
| 415 | int pblk_submit_read_gc(struct pblk *pblk, struct pblk_gc_rq *gc_rq) |
| 416 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 417 | struct nvm_rq rqd; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 418 | int ret = NVM_IO_OK; |
| 419 | |
| 420 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 421 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 422 | ret = pblk_alloc_rqd_meta(pblk, &rqd); |
| 423 | if (ret) |
| 424 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 425 | |
| 426 | if (gc_rq->nr_secs > 1) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 427 | gc_rq->secs_to_gc = read_ppalist_rq_gc(pblk, &rqd, gc_rq->line, |
| 428 | gc_rq->lba_list, |
| 429 | gc_rq->paddr_list, |
| 430 | gc_rq->nr_secs); |
| 431 | if (gc_rq->secs_to_gc == 1) |
| 432 | rqd.ppa_addr = rqd.ppa_list[0]; |
| 433 | } else { |
| 434 | gc_rq->secs_to_gc = read_rq_gc(pblk, &rqd, gc_rq->line, |
| 435 | gc_rq->lba_list[0], |
| 436 | gc_rq->paddr_list[0]); |
| 437 | } |
| 438 | |
| 439 | if (!(gc_rq->secs_to_gc)) |
| 440 | goto out; |
| 441 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 442 | rqd.opcode = NVM_OP_PREAD; |
| 443 | rqd.nr_ppas = gc_rq->secs_to_gc; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 444 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 445 | if (pblk_submit_io_sync(pblk, &rqd, gc_rq->data)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 446 | ret = -EIO; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 447 | goto err_free_dma; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | pblk_read_check_rand(pblk, &rqd, gc_rq->lba_list, gc_rq->nr_secs); |
| 451 | |
| 452 | atomic_dec(&pblk->inflight_io); |
| 453 | |
| 454 | if (rqd.error) { |
| 455 | atomic_long_inc(&pblk->read_failed_gc); |
| 456 | #ifdef CONFIG_NVM_PBLK_DEBUG |
| 457 | pblk_print_failed_rqd(pblk, &rqd, rqd.error); |
| 458 | #endif |
| 459 | } |
| 460 | |
| 461 | #ifdef CONFIG_NVM_PBLK_DEBUG |
| 462 | atomic_long_add(gc_rq->secs_to_gc, &pblk->sync_reads); |
| 463 | atomic_long_add(gc_rq->secs_to_gc, &pblk->recov_gc_reads); |
| 464 | atomic_long_sub(gc_rq->secs_to_gc, &pblk->inflight_reads); |
| 465 | #endif |
| 466 | |
| 467 | out: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 468 | pblk_free_rqd_meta(pblk, &rqd); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 469 | return ret; |
| 470 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 471 | err_free_dma: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 472 | pblk_free_rqd_meta(pblk, &rqd); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 473 | return ret; |
| 474 | } |