Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 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-core.c - pblk's core functionality |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "pblk.h" |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 20 | |
Javier González | 8bd4002 | 2017-10-13 14:46:44 +0200 | [diff] [blame] | 21 | static void pblk_line_mark_bb(struct work_struct *work) |
| 22 | { |
| 23 | struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws, |
| 24 | ws); |
| 25 | struct pblk *pblk = line_ws->pblk; |
| 26 | struct nvm_tgt_dev *dev = pblk->dev; |
| 27 | struct ppa_addr *ppa = line_ws->priv; |
| 28 | int ret; |
| 29 | |
| 30 | ret = nvm_set_tgt_bb_tbl(dev, ppa, 1, NVM_BLK_T_GRWN_BAD); |
| 31 | if (ret) { |
| 32 | struct pblk_line *line; |
| 33 | int pos; |
| 34 | |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 35 | line = &pblk->lines[pblk_ppa_to_line(*ppa)]; |
| 36 | pos = pblk_ppa_to_pos(&dev->geo, *ppa); |
Javier González | 8bd4002 | 2017-10-13 14:46:44 +0200 | [diff] [blame] | 37 | |
| 38 | pr_err("pblk: failed to mark bb, line:%d, pos:%d\n", |
| 39 | line->id, pos); |
| 40 | } |
| 41 | |
| 42 | kfree(ppa); |
| 43 | mempool_free(line_ws, pblk->gen_ws_pool); |
| 44 | } |
| 45 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 46 | static void pblk_mark_bb(struct pblk *pblk, struct pblk_line *line, |
| 47 | struct ppa_addr *ppa) |
| 48 | { |
| 49 | struct nvm_tgt_dev *dev = pblk->dev; |
| 50 | struct nvm_geo *geo = &dev->geo; |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 51 | int pos = pblk_ppa_to_pos(geo, *ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 52 | |
| 53 | pr_debug("pblk: erase failed: line:%d, pos:%d\n", line->id, pos); |
| 54 | atomic_long_inc(&pblk->erase_failed); |
| 55 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 56 | atomic_dec(&line->blk_in_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 57 | if (test_and_set_bit(pos, line->blk_bitmap)) |
| 58 | pr_err("pblk: attempted to erase bb: line:%d, pos:%d\n", |
| 59 | line->id, pos); |
| 60 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 61 | pblk_gen_run_ws(pblk, NULL, ppa, pblk_line_mark_bb, |
| 62 | GFP_ATOMIC, pblk->bb_wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void __pblk_end_io_erase(struct pblk *pblk, struct nvm_rq *rqd) |
| 66 | { |
| 67 | struct pblk_line *line; |
| 68 | |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 69 | line = &pblk->lines[pblk_ppa_to_line(rqd->ppa_addr)]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 70 | atomic_dec(&line->left_seblks); |
| 71 | |
| 72 | if (rqd->error) { |
| 73 | struct ppa_addr *ppa; |
| 74 | |
| 75 | ppa = kmalloc(sizeof(struct ppa_addr), GFP_ATOMIC); |
| 76 | if (!ppa) |
| 77 | return; |
| 78 | |
| 79 | *ppa = rqd->ppa_addr; |
| 80 | pblk_mark_bb(pblk, line, ppa); |
| 81 | } |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 82 | |
| 83 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /* Erase completion assumes that only one block is erased at the time */ |
| 87 | static void pblk_end_io_erase(struct nvm_rq *rqd) |
| 88 | { |
| 89 | struct pblk *pblk = rqd->private; |
| 90 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 91 | __pblk_end_io_erase(pblk, rqd); |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame] | 92 | mempool_free(rqd, pblk->e_rq_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Javier González | 0880a9a | 2017-06-26 11:57:19 +0200 | [diff] [blame] | 95 | void __pblk_map_invalidate(struct pblk *pblk, struct pblk_line *line, |
| 96 | u64 paddr) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 97 | { |
| 98 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 99 | struct list_head *move_list = NULL; |
| 100 | |
| 101 | /* Lines being reclaimed (GC'ed) cannot be invalidated. Before the L2P |
| 102 | * table is modified with reclaimed sectors, a check is done to endure |
| 103 | * that newer updates are not overwritten. |
| 104 | */ |
| 105 | spin_lock(&line->lock); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 106 | WARN_ON(line->state == PBLK_LINESTATE_FREE); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 107 | |
| 108 | if (test_and_set_bit(paddr, line->invalid_bitmap)) { |
| 109 | WARN_ONCE(1, "pblk: double invalidate\n"); |
| 110 | spin_unlock(&line->lock); |
| 111 | return; |
| 112 | } |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 113 | le32_add_cpu(line->vsc, -1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 114 | |
| 115 | if (line->state == PBLK_LINESTATE_CLOSED) |
| 116 | move_list = pblk_line_gc_list(pblk, line); |
| 117 | spin_unlock(&line->lock); |
| 118 | |
| 119 | if (move_list) { |
| 120 | spin_lock(&l_mg->gc_lock); |
| 121 | spin_lock(&line->lock); |
| 122 | /* Prevent moving a line that has just been chosen for GC */ |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 123 | if (line->state == PBLK_LINESTATE_GC) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 124 | spin_unlock(&line->lock); |
| 125 | spin_unlock(&l_mg->gc_lock); |
| 126 | return; |
| 127 | } |
| 128 | spin_unlock(&line->lock); |
| 129 | |
| 130 | list_move_tail(&line->list, move_list); |
| 131 | spin_unlock(&l_mg->gc_lock); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void pblk_map_invalidate(struct pblk *pblk, struct ppa_addr ppa) |
| 136 | { |
| 137 | struct pblk_line *line; |
| 138 | u64 paddr; |
| 139 | int line_id; |
| 140 | |
| 141 | #ifdef CONFIG_NVM_DEBUG |
| 142 | /* Callers must ensure that the ppa points to a device address */ |
| 143 | BUG_ON(pblk_addr_in_cache(ppa)); |
| 144 | BUG_ON(pblk_ppa_empty(ppa)); |
| 145 | #endif |
| 146 | |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 147 | line_id = pblk_ppa_to_line(ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 148 | line = &pblk->lines[line_id]; |
| 149 | paddr = pblk_dev_ppa_to_line_addr(pblk, ppa); |
| 150 | |
| 151 | __pblk_map_invalidate(pblk, line, paddr); |
| 152 | } |
| 153 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 154 | static void pblk_invalidate_range(struct pblk *pblk, sector_t slba, |
| 155 | unsigned int nr_secs) |
| 156 | { |
| 157 | sector_t lba; |
| 158 | |
| 159 | spin_lock(&pblk->trans_lock); |
| 160 | for (lba = slba; lba < slba + nr_secs; lba++) { |
| 161 | struct ppa_addr ppa; |
| 162 | |
| 163 | ppa = pblk_trans_map_get(pblk, lba); |
| 164 | |
| 165 | if (!pblk_addr_in_cache(ppa) && !pblk_ppa_empty(ppa)) |
| 166 | pblk_map_invalidate(pblk, ppa); |
| 167 | |
| 168 | pblk_ppa_set_empty(&ppa); |
| 169 | pblk_trans_map_set(pblk, lba, ppa); |
| 170 | } |
| 171 | spin_unlock(&pblk->trans_lock); |
| 172 | } |
| 173 | |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 174 | /* Caller must guarantee that the request is a valid type */ |
| 175 | struct nvm_rq *pblk_alloc_rqd(struct pblk *pblk, int type) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 176 | { |
| 177 | mempool_t *pool; |
| 178 | struct nvm_rq *rqd; |
| 179 | int rq_size; |
| 180 | |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 181 | switch (type) { |
| 182 | case PBLK_WRITE: |
| 183 | case PBLK_WRITE_INT: |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 184 | pool = pblk->w_rq_pool; |
| 185 | rq_size = pblk_w_rq_size; |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 186 | break; |
| 187 | case PBLK_READ: |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame] | 188 | pool = pblk->r_rq_pool; |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 189 | rq_size = pblk_g_rq_size; |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 190 | break; |
| 191 | default: |
| 192 | pool = pblk->e_rq_pool; |
| 193 | rq_size = pblk_g_rq_size; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | rqd = mempool_alloc(pool, GFP_KERNEL); |
| 197 | memset(rqd, 0, rq_size); |
| 198 | |
| 199 | return rqd; |
| 200 | } |
| 201 | |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 202 | /* Typically used on completion path. Cannot guarantee request consistency */ |
| 203 | void pblk_free_rqd(struct pblk *pblk, struct nvm_rq *rqd, int type) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 204 | { |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 205 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 206 | mempool_t *pool; |
| 207 | |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 208 | switch (type) { |
| 209 | case PBLK_WRITE: |
| 210 | kfree(((struct pblk_c_ctx *)nvm_rq_to_pdu(rqd))->lun_bitmap); |
| 211 | case PBLK_WRITE_INT: |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 212 | pool = pblk->w_rq_pool; |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 213 | break; |
| 214 | case PBLK_READ: |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame] | 215 | pool = pblk->r_rq_pool; |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 216 | break; |
| 217 | case PBLK_ERASE: |
| 218 | pool = pblk->e_rq_pool; |
| 219 | break; |
| 220 | default: |
| 221 | pr_err("pblk: trying to free unknown rqd type\n"); |
| 222 | return; |
| 223 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 224 | |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 225 | nvm_dev_dma_free(dev->parent, rqd->meta_list, rqd->dma_meta_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 226 | mempool_free(rqd, pool); |
| 227 | } |
| 228 | |
| 229 | void pblk_bio_free_pages(struct pblk *pblk, struct bio *bio, int off, |
| 230 | int nr_pages) |
| 231 | { |
| 232 | struct bio_vec bv; |
| 233 | int i; |
| 234 | |
| 235 | WARN_ON(off + nr_pages != bio->bi_vcnt); |
| 236 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 237 | for (i = off; i < nr_pages + off; i++) { |
| 238 | bv = bio->bi_io_vec[i]; |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 239 | mempool_free(bv.bv_page, pblk->page_bio_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | |
| 243 | int pblk_bio_add_pages(struct pblk *pblk, struct bio *bio, gfp_t flags, |
| 244 | int nr_pages) |
| 245 | { |
| 246 | struct request_queue *q = pblk->dev->q; |
| 247 | struct page *page; |
| 248 | int i, ret; |
| 249 | |
| 250 | for (i = 0; i < nr_pages; i++) { |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 251 | page = mempool_alloc(pblk->page_bio_pool, flags); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 252 | |
| 253 | ret = bio_add_pc_page(q, bio, page, PBLK_EXPOSED_PAGE_SIZE, 0); |
| 254 | if (ret != PBLK_EXPOSED_PAGE_SIZE) { |
| 255 | pr_err("pblk: could not add page to bio\n"); |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 256 | mempool_free(page, pblk->page_bio_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 257 | goto err; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | err: |
| 263 | pblk_bio_free_pages(pblk, bio, 0, i - 1); |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | static void pblk_write_kick(struct pblk *pblk) |
| 268 | { |
| 269 | wake_up_process(pblk->writer_ts); |
| 270 | mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(1000)); |
| 271 | } |
| 272 | |
Kees Cook | 87c1d2d | 2017-10-17 21:10:19 -0700 | [diff] [blame] | 273 | void pblk_write_timer_fn(struct timer_list *t) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 274 | { |
Kees Cook | 87c1d2d | 2017-10-17 21:10:19 -0700 | [diff] [blame] | 275 | struct pblk *pblk = from_timer(pblk, t, wtimer); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 276 | |
| 277 | /* kick the write thread every tick to flush outstanding data */ |
| 278 | pblk_write_kick(pblk); |
| 279 | } |
| 280 | |
| 281 | void pblk_write_should_kick(struct pblk *pblk) |
| 282 | { |
| 283 | unsigned int secs_avail = pblk_rb_read_count(&pblk->rwb); |
| 284 | |
| 285 | if (secs_avail >= pblk->min_write_pgs) |
| 286 | pblk_write_kick(pblk); |
| 287 | } |
| 288 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 289 | void pblk_end_io_sync(struct nvm_rq *rqd) |
| 290 | { |
| 291 | struct completion *waiting = rqd->private; |
| 292 | |
| 293 | complete(waiting); |
| 294 | } |
| 295 | |
Javier González | 8bd4002 | 2017-10-13 14:46:44 +0200 | [diff] [blame] | 296 | static void pblk_wait_for_meta(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 297 | { |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 298 | do { |
| 299 | if (!atomic_read(&pblk->inflight_io)) |
| 300 | break; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 301 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 302 | schedule(); |
| 303 | } while (1); |
| 304 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 305 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 306 | static void pblk_flush_writer(struct pblk *pblk) |
| 307 | { |
| 308 | pblk_rb_flush(&pblk->rwb); |
| 309 | do { |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 310 | if (!pblk_rb_sync_count(&pblk->rwb)) |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 311 | break; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 312 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 313 | pblk_write_kick(pblk); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 314 | schedule(); |
| 315 | } while (1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | struct list_head *pblk_line_gc_list(struct pblk *pblk, struct pblk_line *line) |
| 319 | { |
| 320 | struct pblk_line_meta *lm = &pblk->lm; |
| 321 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 322 | struct list_head *move_list = NULL; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 323 | int vsc = le32_to_cpu(*line->vsc); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 324 | |
Javier González | 476118c | 2017-06-26 11:57:26 +0200 | [diff] [blame] | 325 | lockdep_assert_held(&line->lock); |
| 326 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 327 | if (!vsc) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 328 | if (line->gc_group != PBLK_LINEGC_FULL) { |
| 329 | line->gc_group = PBLK_LINEGC_FULL; |
| 330 | move_list = &l_mg->gc_full_list; |
| 331 | } |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 332 | } else if (vsc < lm->high_thrs) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 333 | if (line->gc_group != PBLK_LINEGC_HIGH) { |
| 334 | line->gc_group = PBLK_LINEGC_HIGH; |
| 335 | move_list = &l_mg->gc_high_list; |
| 336 | } |
Javier González | b20ba1b | 2017-06-26 11:57:27 +0200 | [diff] [blame] | 337 | } else if (vsc < lm->mid_thrs) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 338 | if (line->gc_group != PBLK_LINEGC_MID) { |
| 339 | line->gc_group = PBLK_LINEGC_MID; |
| 340 | move_list = &l_mg->gc_mid_list; |
| 341 | } |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 342 | } else if (vsc < line->sec_in_line) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 343 | if (line->gc_group != PBLK_LINEGC_LOW) { |
| 344 | line->gc_group = PBLK_LINEGC_LOW; |
| 345 | move_list = &l_mg->gc_low_list; |
| 346 | } |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 347 | } else if (vsc == line->sec_in_line) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 348 | if (line->gc_group != PBLK_LINEGC_EMPTY) { |
| 349 | line->gc_group = PBLK_LINEGC_EMPTY; |
| 350 | move_list = &l_mg->gc_empty_list; |
| 351 | } |
| 352 | } else { |
| 353 | line->state = PBLK_LINESTATE_CORRUPT; |
| 354 | line->gc_group = PBLK_LINEGC_NONE; |
| 355 | move_list = &l_mg->corrupt_list; |
| 356 | pr_err("pblk: corrupted vsc for line %d, vsc:%d (%d/%d/%d)\n", |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 357 | line->id, vsc, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 358 | line->sec_in_line, |
| 359 | lm->high_thrs, lm->mid_thrs); |
| 360 | } |
| 361 | |
| 362 | return move_list; |
| 363 | } |
| 364 | |
| 365 | void pblk_discard(struct pblk *pblk, struct bio *bio) |
| 366 | { |
| 367 | sector_t slba = pblk_get_lba(bio); |
| 368 | sector_t nr_secs = pblk_get_secs(bio); |
| 369 | |
| 370 | pblk_invalidate_range(pblk, slba, nr_secs); |
| 371 | } |
| 372 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 373 | void pblk_log_write_err(struct pblk *pblk, struct nvm_rq *rqd) |
| 374 | { |
| 375 | atomic_long_inc(&pblk->write_failed); |
| 376 | #ifdef CONFIG_NVM_DEBUG |
| 377 | pblk_print_failed_rqd(pblk, rqd, rqd->error); |
| 378 | #endif |
| 379 | } |
| 380 | |
| 381 | void pblk_log_read_err(struct pblk *pblk, struct nvm_rq *rqd) |
| 382 | { |
| 383 | /* Empty page read is not necessarily an error (e.g., L2P recovery) */ |
| 384 | if (rqd->error == NVM_RSP_ERR_EMPTYPAGE) { |
| 385 | atomic_long_inc(&pblk->read_empty); |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | switch (rqd->error) { |
| 390 | case NVM_RSP_WARN_HIGHECC: |
| 391 | atomic_long_inc(&pblk->read_high_ecc); |
| 392 | break; |
| 393 | case NVM_RSP_ERR_FAILECC: |
| 394 | case NVM_RSP_ERR_FAILCRC: |
| 395 | atomic_long_inc(&pblk->read_failed); |
| 396 | break; |
| 397 | default: |
| 398 | pr_err("pblk: unknown read error:%d\n", rqd->error); |
| 399 | } |
| 400 | #ifdef CONFIG_NVM_DEBUG |
| 401 | pblk_print_failed_rqd(pblk, rqd, rqd->error); |
| 402 | #endif |
| 403 | } |
| 404 | |
Javier González | c2e9f5d | 2017-06-26 11:57:14 +0200 | [diff] [blame] | 405 | void pblk_set_sec_per_write(struct pblk *pblk, int sec_per_write) |
| 406 | { |
| 407 | pblk->sec_per_write = sec_per_write; |
| 408 | } |
| 409 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 410 | int pblk_submit_io(struct pblk *pblk, struct nvm_rq *rqd) |
| 411 | { |
| 412 | struct nvm_tgt_dev *dev = pblk->dev; |
| 413 | |
| 414 | #ifdef CONFIG_NVM_DEBUG |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 415 | int ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 416 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 417 | ret = pblk_check_io(pblk, rqd); |
| 418 | if (ret) |
| 419 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 420 | #endif |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 421 | |
| 422 | atomic_inc(&pblk->inflight_io); |
| 423 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 424 | return nvm_submit_io(dev, rqd); |
| 425 | } |
| 426 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 427 | int pblk_submit_io_sync(struct pblk *pblk, struct nvm_rq *rqd) |
| 428 | { |
| 429 | struct nvm_tgt_dev *dev = pblk->dev; |
| 430 | |
| 431 | #ifdef CONFIG_NVM_DEBUG |
| 432 | int ret; |
| 433 | |
| 434 | ret = pblk_check_io(pblk, rqd); |
| 435 | if (ret) |
| 436 | return ret; |
| 437 | #endif |
| 438 | |
| 439 | atomic_inc(&pblk->inflight_io); |
| 440 | |
| 441 | return nvm_submit_io_sync(dev, rqd); |
| 442 | } |
| 443 | |
Javier González | 55e836d | 2017-10-13 14:46:16 +0200 | [diff] [blame] | 444 | static void pblk_bio_map_addr_endio(struct bio *bio) |
| 445 | { |
| 446 | bio_put(bio); |
| 447 | } |
| 448 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 449 | struct bio *pblk_bio_map_addr(struct pblk *pblk, void *data, |
| 450 | unsigned int nr_secs, unsigned int len, |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 451 | int alloc_type, gfp_t gfp_mask) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 452 | { |
| 453 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 454 | void *kaddr = data; |
| 455 | struct page *page; |
| 456 | struct bio *bio; |
| 457 | int i, ret; |
| 458 | |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 459 | if (alloc_type == PBLK_KMALLOC_META) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 460 | return bio_map_kern(dev->q, kaddr, len, gfp_mask); |
| 461 | |
| 462 | bio = bio_kmalloc(gfp_mask, nr_secs); |
| 463 | if (!bio) |
| 464 | return ERR_PTR(-ENOMEM); |
| 465 | |
| 466 | for (i = 0; i < nr_secs; i++) { |
| 467 | page = vmalloc_to_page(kaddr); |
| 468 | if (!page) { |
| 469 | pr_err("pblk: could not map vmalloc bio\n"); |
| 470 | bio_put(bio); |
| 471 | bio = ERR_PTR(-ENOMEM); |
| 472 | goto out; |
| 473 | } |
| 474 | |
| 475 | ret = bio_add_pc_page(dev->q, bio, page, PAGE_SIZE, 0); |
| 476 | if (ret != PAGE_SIZE) { |
| 477 | pr_err("pblk: could not add page to bio\n"); |
| 478 | bio_put(bio); |
| 479 | bio = ERR_PTR(-ENOMEM); |
| 480 | goto out; |
| 481 | } |
| 482 | |
| 483 | kaddr += PAGE_SIZE; |
| 484 | } |
Javier González | 55e836d | 2017-10-13 14:46:16 +0200 | [diff] [blame] | 485 | |
| 486 | bio->bi_end_io = pblk_bio_map_addr_endio; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 487 | out: |
| 488 | return bio; |
| 489 | } |
| 490 | |
| 491 | int pblk_calc_secs(struct pblk *pblk, unsigned long secs_avail, |
| 492 | unsigned long secs_to_flush) |
| 493 | { |
Javier González | c2e9f5d | 2017-06-26 11:57:14 +0200 | [diff] [blame] | 494 | int max = pblk->sec_per_write; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 495 | int min = pblk->min_write_pgs; |
| 496 | int secs_to_sync = 0; |
| 497 | |
| 498 | if (secs_avail >= max) |
| 499 | secs_to_sync = max; |
| 500 | else if (secs_avail >= min) |
| 501 | secs_to_sync = min * (secs_avail / min); |
| 502 | else if (secs_to_flush) |
| 503 | secs_to_sync = min; |
| 504 | |
| 505 | return secs_to_sync; |
| 506 | } |
| 507 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 508 | void pblk_dealloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs) |
| 509 | { |
| 510 | u64 addr; |
| 511 | int i; |
| 512 | |
Rakesh Pandit | e57903f | 2017-10-13 14:45:56 +0200 | [diff] [blame] | 513 | spin_lock(&line->lock); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 514 | addr = find_next_zero_bit(line->map_bitmap, |
| 515 | pblk->lm.sec_per_line, line->cur_sec); |
| 516 | line->cur_sec = addr - nr_secs; |
| 517 | |
| 518 | for (i = 0; i < nr_secs; i++, line->cur_sec--) |
| 519 | WARN_ON(!test_and_clear_bit(line->cur_sec, line->map_bitmap)); |
Rakesh Pandit | e57903f | 2017-10-13 14:45:56 +0200 | [diff] [blame] | 520 | spin_unlock(&line->lock); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | u64 __pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 524 | { |
| 525 | u64 addr; |
| 526 | int i; |
| 527 | |
Javier González | 476118c | 2017-06-26 11:57:26 +0200 | [diff] [blame] | 528 | lockdep_assert_held(&line->lock); |
| 529 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 530 | /* logic error: ppa out-of-bounds. Prevent generating bad address */ |
| 531 | if (line->cur_sec + nr_secs > pblk->lm.sec_per_line) { |
| 532 | WARN(1, "pblk: page allocation out of bounds\n"); |
| 533 | nr_secs = pblk->lm.sec_per_line - line->cur_sec; |
| 534 | } |
| 535 | |
| 536 | line->cur_sec = addr = find_next_zero_bit(line->map_bitmap, |
| 537 | pblk->lm.sec_per_line, line->cur_sec); |
| 538 | for (i = 0; i < nr_secs; i++, line->cur_sec++) |
| 539 | WARN_ON(test_and_set_bit(line->cur_sec, line->map_bitmap)); |
| 540 | |
| 541 | return addr; |
| 542 | } |
| 543 | |
| 544 | u64 pblk_alloc_page(struct pblk *pblk, struct pblk_line *line, int nr_secs) |
| 545 | { |
| 546 | u64 addr; |
| 547 | |
| 548 | /* Lock needed in case a write fails and a recovery needs to remap |
| 549 | * failed write buffer entries |
| 550 | */ |
| 551 | spin_lock(&line->lock); |
| 552 | addr = __pblk_alloc_page(pblk, line, nr_secs); |
| 553 | line->left_msecs -= nr_secs; |
| 554 | WARN(line->left_msecs < 0, "pblk: page allocation out of bounds\n"); |
| 555 | spin_unlock(&line->lock); |
| 556 | |
| 557 | return addr; |
| 558 | } |
| 559 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 560 | u64 pblk_lookup_page(struct pblk *pblk, struct pblk_line *line) |
| 561 | { |
| 562 | u64 paddr; |
| 563 | |
| 564 | spin_lock(&line->lock); |
| 565 | paddr = find_next_zero_bit(line->map_bitmap, |
| 566 | pblk->lm.sec_per_line, line->cur_sec); |
| 567 | spin_unlock(&line->lock); |
| 568 | |
| 569 | return paddr; |
| 570 | } |
| 571 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 572 | /* |
| 573 | * Submit emeta to one LUN in the raid line at the time to avoid a deadlock when |
| 574 | * taking the per LUN semaphore. |
| 575 | */ |
| 576 | static int pblk_line_submit_emeta_io(struct pblk *pblk, struct pblk_line *line, |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 577 | void *emeta_buf, u64 paddr, int dir) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 578 | { |
| 579 | struct nvm_tgt_dev *dev = pblk->dev; |
| 580 | struct nvm_geo *geo = &dev->geo; |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 581 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 582 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 583 | void *ppa_list, *meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 584 | struct bio *bio; |
| 585 | struct nvm_rq rqd; |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 586 | dma_addr_t dma_ppa_list, dma_meta_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 587 | int min = pblk->min_write_pgs; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 588 | int left_ppas = lm->emeta_sec[0]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 589 | int id = line->id; |
| 590 | int rq_ppas, rq_len; |
| 591 | int cmd_op, bio_op; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 592 | int i, j; |
| 593 | int ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 594 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 595 | if (dir == PBLK_WRITE) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 596 | bio_op = REQ_OP_WRITE; |
| 597 | cmd_op = NVM_OP_PWRITE; |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 598 | } else if (dir == PBLK_READ) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 599 | bio_op = REQ_OP_READ; |
| 600 | cmd_op = NVM_OP_PREAD; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 601 | } else |
| 602 | return -EINVAL; |
| 603 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 604 | meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, |
| 605 | &dma_meta_list); |
| 606 | if (!meta_list) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 607 | return -ENOMEM; |
| 608 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 609 | ppa_list = meta_list + pblk_dma_meta_size; |
| 610 | dma_ppa_list = dma_meta_list + pblk_dma_meta_size; |
| 611 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 612 | next_rq: |
| 613 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 614 | |
| 615 | rq_ppas = pblk_calc_secs(pblk, left_ppas, 0); |
| 616 | rq_len = rq_ppas * geo->sec_size; |
| 617 | |
Javier González | de54e70 | 2017-06-30 17:56:39 +0200 | [diff] [blame] | 618 | bio = pblk_bio_map_addr(pblk, emeta_buf, rq_ppas, rq_len, |
| 619 | l_mg->emeta_alloc_type, GFP_KERNEL); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 620 | if (IS_ERR(bio)) { |
| 621 | ret = PTR_ERR(bio); |
| 622 | goto free_rqd_dma; |
| 623 | } |
| 624 | |
| 625 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 626 | bio_set_op_attrs(bio, bio_op, 0); |
| 627 | |
| 628 | rqd.bio = bio; |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 629 | rqd.meta_list = meta_list; |
| 630 | rqd.ppa_list = ppa_list; |
| 631 | rqd.dma_meta_list = dma_meta_list; |
| 632 | rqd.dma_ppa_list = dma_ppa_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 633 | rqd.opcode = cmd_op; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 634 | rqd.nr_ppas = rq_ppas; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 635 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 636 | if (dir == PBLK_WRITE) { |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 637 | struct pblk_sec_meta *meta_list = rqd.meta_list; |
| 638 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 639 | rqd.flags = pblk_set_progr_mode(pblk, PBLK_WRITE); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 640 | for (i = 0; i < rqd.nr_ppas; ) { |
| 641 | spin_lock(&line->lock); |
| 642 | paddr = __pblk_alloc_page(pblk, line, min); |
| 643 | spin_unlock(&line->lock); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 644 | for (j = 0; j < min; j++, i++, paddr++) { |
| 645 | meta_list[i].lba = cpu_to_le64(ADDR_EMPTY); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 646 | rqd.ppa_list[i] = |
| 647 | addr_to_gen_ppa(pblk, paddr, id); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 648 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 649 | } |
| 650 | } else { |
| 651 | for (i = 0; i < rqd.nr_ppas; ) { |
| 652 | struct ppa_addr ppa = addr_to_gen_ppa(pblk, paddr, id); |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 653 | int pos = pblk_ppa_to_pos(geo, ppa); |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 654 | int read_type = PBLK_READ_RANDOM; |
| 655 | |
| 656 | if (pblk_io_aligned(pblk, rq_ppas)) |
| 657 | read_type = PBLK_READ_SEQUENTIAL; |
| 658 | rqd.flags = pblk_set_read_mode(pblk, read_type); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 659 | |
| 660 | while (test_bit(pos, line->blk_bitmap)) { |
| 661 | paddr += min; |
| 662 | if (pblk_boundary_paddr_checks(pblk, paddr)) { |
| 663 | pr_err("pblk: corrupt emeta line:%d\n", |
| 664 | line->id); |
| 665 | bio_put(bio); |
| 666 | ret = -EINTR; |
| 667 | goto free_rqd_dma; |
| 668 | } |
| 669 | |
| 670 | ppa = addr_to_gen_ppa(pblk, paddr, id); |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 671 | pos = pblk_ppa_to_pos(geo, ppa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | if (pblk_boundary_paddr_checks(pblk, paddr + min)) { |
| 675 | pr_err("pblk: corrupt emeta line:%d\n", |
| 676 | line->id); |
| 677 | bio_put(bio); |
| 678 | ret = -EINTR; |
| 679 | goto free_rqd_dma; |
| 680 | } |
| 681 | |
| 682 | for (j = 0; j < min; j++, i++, paddr++) |
| 683 | rqd.ppa_list[i] = |
| 684 | addr_to_gen_ppa(pblk, paddr, line->id); |
| 685 | } |
| 686 | } |
| 687 | |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 688 | ret = pblk_submit_io_sync(pblk, &rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 689 | if (ret) { |
| 690 | pr_err("pblk: emeta I/O submission failed: %d\n", ret); |
| 691 | bio_put(bio); |
| 692 | goto free_rqd_dma; |
| 693 | } |
| 694 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 695 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 696 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 697 | if (rqd.error) { |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 698 | if (dir == PBLK_WRITE) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 699 | pblk_log_write_err(pblk, &rqd); |
| 700 | else |
| 701 | pblk_log_read_err(pblk, &rqd); |
| 702 | } |
| 703 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 704 | emeta_buf += rq_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 705 | left_ppas -= rq_ppas; |
| 706 | if (left_ppas) |
| 707 | goto next_rq; |
| 708 | free_rqd_dma: |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 709 | nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | u64 pblk_line_smeta_start(struct pblk *pblk, struct pblk_line *line) |
| 714 | { |
| 715 | struct nvm_tgt_dev *dev = pblk->dev; |
| 716 | struct nvm_geo *geo = &dev->geo; |
| 717 | struct pblk_line_meta *lm = &pblk->lm; |
| 718 | int bit; |
| 719 | |
| 720 | /* This usually only happens on bad lines */ |
| 721 | bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line); |
| 722 | if (bit >= lm->blk_per_line) |
| 723 | return -1; |
| 724 | |
| 725 | return bit * geo->sec_per_pl; |
| 726 | } |
| 727 | |
| 728 | static int pblk_line_submit_smeta_io(struct pblk *pblk, struct pblk_line *line, |
| 729 | u64 paddr, int dir) |
| 730 | { |
| 731 | struct nvm_tgt_dev *dev = pblk->dev; |
| 732 | struct pblk_line_meta *lm = &pblk->lm; |
| 733 | struct bio *bio; |
| 734 | struct nvm_rq rqd; |
| 735 | __le64 *lba_list = NULL; |
| 736 | int i, ret; |
| 737 | int cmd_op, bio_op; |
| 738 | int flags; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 739 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 740 | if (dir == PBLK_WRITE) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 741 | bio_op = REQ_OP_WRITE; |
| 742 | cmd_op = NVM_OP_PWRITE; |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 743 | flags = pblk_set_progr_mode(pblk, PBLK_WRITE); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 744 | lba_list = emeta_to_lbas(pblk, line->emeta->buf); |
Javier González | 8f55459 | 2018-01-05 14:16:16 +0100 | [diff] [blame] | 745 | } else if (dir == PBLK_READ_RECOV || dir == PBLK_READ) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 746 | bio_op = REQ_OP_READ; |
| 747 | cmd_op = NVM_OP_PREAD; |
Javier González | f9c1015 | 2017-06-26 11:57:20 +0200 | [diff] [blame] | 748 | flags = pblk_set_read_mode(pblk, PBLK_READ_SEQUENTIAL); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 749 | } else |
| 750 | return -EINVAL; |
| 751 | |
| 752 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 753 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 754 | rqd.meta_list = nvm_dev_dma_alloc(dev->parent, GFP_KERNEL, |
| 755 | &rqd.dma_meta_list); |
| 756 | if (!rqd.meta_list) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 757 | return -ENOMEM; |
| 758 | |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 759 | rqd.ppa_list = rqd.meta_list + pblk_dma_meta_size; |
| 760 | rqd.dma_ppa_list = rqd.dma_meta_list + pblk_dma_meta_size; |
| 761 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 762 | bio = bio_map_kern(dev->q, line->smeta, lm->smeta_len, GFP_KERNEL); |
| 763 | if (IS_ERR(bio)) { |
| 764 | ret = PTR_ERR(bio); |
| 765 | goto free_ppa_list; |
| 766 | } |
| 767 | |
| 768 | bio->bi_iter.bi_sector = 0; /* internal bio */ |
| 769 | bio_set_op_attrs(bio, bio_op, 0); |
| 770 | |
| 771 | rqd.bio = bio; |
| 772 | rqd.opcode = cmd_op; |
| 773 | rqd.flags = flags; |
| 774 | rqd.nr_ppas = lm->smeta_sec; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 775 | |
| 776 | for (i = 0; i < lm->smeta_sec; i++, paddr++) { |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 777 | struct pblk_sec_meta *meta_list = rqd.meta_list; |
| 778 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 779 | rqd.ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 780 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 781 | if (dir == PBLK_WRITE) { |
Javier González | f417aa0 | 2017-06-30 17:56:34 +0200 | [diff] [blame] | 782 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 783 | |
| 784 | meta_list[i].lba = lba_list[paddr] = addr_empty; |
| 785 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* |
| 789 | * This I/O is sent by the write thread when a line is replace. Since |
| 790 | * the write thread is the only one sending write and erase commands, |
| 791 | * there is no need to take the LUN semaphore. |
| 792 | */ |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 793 | ret = pblk_submit_io_sync(pblk, &rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 794 | if (ret) { |
| 795 | pr_err("pblk: smeta I/O submission failed: %d\n", ret); |
| 796 | bio_put(bio); |
| 797 | goto free_ppa_list; |
| 798 | } |
| 799 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 800 | atomic_dec(&pblk->inflight_io); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 801 | |
| 802 | if (rqd.error) { |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 803 | if (dir == PBLK_WRITE) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 804 | pblk_log_write_err(pblk, &rqd); |
Javier González | 8f55459 | 2018-01-05 14:16:16 +0100 | [diff] [blame] | 805 | else if (dir == PBLK_READ) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 806 | pblk_log_read_err(pblk, &rqd); |
| 807 | } |
| 808 | |
| 809 | free_ppa_list: |
Javier González | 63e3809 | 2017-06-26 11:57:24 +0200 | [diff] [blame] | 810 | nvm_dev_dma_free(dev->parent, rqd.meta_list, rqd.dma_meta_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 811 | |
| 812 | return ret; |
| 813 | } |
| 814 | |
| 815 | int pblk_line_read_smeta(struct pblk *pblk, struct pblk_line *line) |
| 816 | { |
| 817 | u64 bpaddr = pblk_line_smeta_start(pblk, line); |
| 818 | |
Javier González | 8f55459 | 2018-01-05 14:16:16 +0100 | [diff] [blame] | 819 | return pblk_line_submit_smeta_io(pblk, line, bpaddr, PBLK_READ_RECOV); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 820 | } |
| 821 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 822 | int pblk_line_read_emeta(struct pblk *pblk, struct pblk_line *line, |
| 823 | void *emeta_buf) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 824 | { |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 825 | return pblk_line_submit_emeta_io(pblk, line, emeta_buf, |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 826 | line->emeta_ssec, PBLK_READ); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | static void pblk_setup_e_rq(struct pblk *pblk, struct nvm_rq *rqd, |
| 830 | struct ppa_addr ppa) |
| 831 | { |
| 832 | rqd->opcode = NVM_OP_ERASE; |
| 833 | rqd->ppa_addr = ppa; |
| 834 | rqd->nr_ppas = 1; |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 835 | rqd->flags = pblk_set_progr_mode(pblk, PBLK_ERASE); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 836 | rqd->bio = NULL; |
| 837 | } |
| 838 | |
| 839 | static int pblk_blk_erase_sync(struct pblk *pblk, struct ppa_addr ppa) |
| 840 | { |
| 841 | struct nvm_rq rqd; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 842 | int ret = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 843 | |
| 844 | memset(&rqd, 0, sizeof(struct nvm_rq)); |
| 845 | |
| 846 | pblk_setup_e_rq(pblk, &rqd, ppa); |
| 847 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 848 | /* The write thread schedules erases so that it minimizes disturbances |
| 849 | * with writes. Thus, there is no need to take the LUN semaphore. |
| 850 | */ |
Javier González | 1a94b2d | 2017-10-13 14:46:47 +0200 | [diff] [blame] | 851 | ret = pblk_submit_io_sync(pblk, &rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 852 | if (ret) { |
| 853 | struct nvm_tgt_dev *dev = pblk->dev; |
| 854 | struct nvm_geo *geo = &dev->geo; |
| 855 | |
| 856 | pr_err("pblk: could not sync erase line:%d,blk:%d\n", |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 857 | pblk_ppa_to_line(ppa), |
| 858 | pblk_ppa_to_pos(geo, ppa)); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 859 | |
| 860 | rqd.error = ret; |
| 861 | goto out; |
| 862 | } |
| 863 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 864 | out: |
| 865 | rqd.private = pblk; |
| 866 | __pblk_end_io_erase(pblk, &rqd); |
| 867 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 868 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | int pblk_line_erase(struct pblk *pblk, struct pblk_line *line) |
| 872 | { |
| 873 | struct pblk_line_meta *lm = &pblk->lm; |
| 874 | struct ppa_addr ppa; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 875 | int ret, bit = -1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 876 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 877 | /* Erase only good blocks, one at a time */ |
| 878 | do { |
| 879 | spin_lock(&line->lock); |
| 880 | bit = find_next_zero_bit(line->erase_bitmap, lm->blk_per_line, |
| 881 | bit + 1); |
| 882 | if (bit >= lm->blk_per_line) { |
| 883 | spin_unlock(&line->lock); |
| 884 | break; |
| 885 | } |
| 886 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 887 | ppa = pblk->luns[bit].bppa; /* set ch and lun */ |
| 888 | ppa.g.blk = line->id; |
| 889 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 890 | atomic_dec(&line->left_eblks); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 891 | WARN_ON(test_and_set_bit(bit, line->erase_bitmap)); |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 892 | spin_unlock(&line->lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 893 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 894 | ret = pblk_blk_erase_sync(pblk, ppa); |
| 895 | if (ret) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 896 | pr_err("pblk: failed to erase line %d\n", line->id); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 897 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 898 | } |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 899 | } while (1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 900 | |
| 901 | return 0; |
| 902 | } |
| 903 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 904 | static void pblk_line_setup_metadata(struct pblk_line *line, |
| 905 | struct pblk_line_mgmt *l_mg, |
| 906 | struct pblk_line_meta *lm) |
| 907 | { |
| 908 | int meta_line; |
| 909 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 910 | lockdep_assert_held(&l_mg->free_lock); |
| 911 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 912 | retry_meta: |
| 913 | meta_line = find_first_zero_bit(&l_mg->meta_bitmap, PBLK_DATA_LINES); |
| 914 | if (meta_line == PBLK_DATA_LINES) { |
| 915 | spin_unlock(&l_mg->free_lock); |
| 916 | io_schedule(); |
| 917 | spin_lock(&l_mg->free_lock); |
| 918 | goto retry_meta; |
| 919 | } |
| 920 | |
| 921 | set_bit(meta_line, &l_mg->meta_bitmap); |
| 922 | line->meta_line = meta_line; |
| 923 | |
| 924 | line->smeta = l_mg->sline_meta[meta_line]; |
| 925 | line->emeta = l_mg->eline_meta[meta_line]; |
| 926 | |
| 927 | memset(line->smeta, 0, lm->smeta_len); |
| 928 | memset(line->emeta->buf, 0, lm->emeta_len[0]); |
| 929 | |
| 930 | line->emeta->mem = 0; |
| 931 | atomic_set(&line->emeta->sync, 0); |
| 932 | } |
| 933 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 934 | /* For now lines are always assumed full lines. Thus, smeta former and current |
| 935 | * lun bitmaps are omitted. |
| 936 | */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 937 | static int pblk_line_init_metadata(struct pblk *pblk, struct pblk_line *line, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 938 | struct pblk_line *cur) |
| 939 | { |
| 940 | struct nvm_tgt_dev *dev = pblk->dev; |
| 941 | struct nvm_geo *geo = &dev->geo; |
| 942 | struct pblk_line_meta *lm = &pblk->lm; |
| 943 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 944 | struct pblk_emeta *emeta = line->emeta; |
| 945 | struct line_emeta *emeta_buf = emeta->buf; |
| 946 | struct line_smeta *smeta_buf = (struct line_smeta *)line->smeta; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 947 | int nr_blk_line; |
| 948 | |
| 949 | /* After erasing the line, new bad blocks might appear and we risk |
| 950 | * having an invalid line |
| 951 | */ |
| 952 | nr_blk_line = lm->blk_per_line - |
| 953 | bitmap_weight(line->blk_bitmap, lm->blk_per_line); |
| 954 | if (nr_blk_line < lm->min_blk_line) { |
| 955 | spin_lock(&l_mg->free_lock); |
| 956 | spin_lock(&line->lock); |
| 957 | line->state = PBLK_LINESTATE_BAD; |
| 958 | spin_unlock(&line->lock); |
| 959 | |
| 960 | list_add_tail(&line->list, &l_mg->bad_list); |
| 961 | spin_unlock(&l_mg->free_lock); |
| 962 | |
| 963 | pr_debug("pblk: line %d is bad\n", line->id); |
| 964 | |
| 965 | return 0; |
| 966 | } |
| 967 | |
| 968 | /* Run-time metadata */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 969 | line->lun_bitmap = ((void *)(smeta_buf)) + sizeof(struct line_smeta); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 970 | |
| 971 | /* Mark LUNs allocated in this line (all for now) */ |
| 972 | bitmap_set(line->lun_bitmap, 0, lm->lun_bitmap_len); |
| 973 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 974 | smeta_buf->header.identifier = cpu_to_le32(PBLK_MAGIC); |
| 975 | memcpy(smeta_buf->header.uuid, pblk->instance_uuid, 16); |
| 976 | smeta_buf->header.id = cpu_to_le32(line->id); |
| 977 | smeta_buf->header.type = cpu_to_le16(line->type); |
Hans Holmberg | d0ab0b1 | 2018-03-30 00:04:51 +0200 | [diff] [blame^] | 978 | smeta_buf->header.version_major = SMETA_VERSION_MAJOR; |
| 979 | smeta_buf->header.version_minor = SMETA_VERSION_MINOR; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 980 | |
| 981 | /* Start metadata */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 982 | smeta_buf->seq_nr = cpu_to_le64(line->seq_nr); |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 983 | smeta_buf->window_wr_lun = cpu_to_le32(geo->all_luns); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 984 | |
| 985 | /* Fill metadata among lines */ |
| 986 | if (cur) { |
| 987 | memcpy(line->lun_bitmap, cur->lun_bitmap, lm->lun_bitmap_len); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 988 | smeta_buf->prev_id = cpu_to_le32(cur->id); |
| 989 | cur->emeta->buf->next_id = cpu_to_le32(line->id); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 990 | } else { |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 991 | smeta_buf->prev_id = cpu_to_le32(PBLK_LINE_EMPTY); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | /* All smeta must be set at this point */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 995 | smeta_buf->header.crc = cpu_to_le32( |
| 996 | pblk_calc_meta_header_crc(pblk, &smeta_buf->header)); |
| 997 | smeta_buf->crc = cpu_to_le32(pblk_calc_smeta_crc(pblk, smeta_buf)); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 998 | |
| 999 | /* End metadata */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1000 | memcpy(&emeta_buf->header, &smeta_buf->header, |
| 1001 | sizeof(struct line_header)); |
Hans Holmberg | d0ab0b1 | 2018-03-30 00:04:51 +0200 | [diff] [blame^] | 1002 | |
| 1003 | emeta_buf->header.version_major = EMETA_VERSION_MAJOR; |
| 1004 | emeta_buf->header.version_minor = EMETA_VERSION_MINOR; |
| 1005 | emeta_buf->header.crc = cpu_to_le32( |
| 1006 | pblk_calc_meta_header_crc(pblk, &emeta_buf->header)); |
| 1007 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1008 | emeta_buf->seq_nr = cpu_to_le64(line->seq_nr); |
| 1009 | emeta_buf->nr_lbas = cpu_to_le64(line->sec_in_line); |
| 1010 | emeta_buf->nr_valid_lbas = cpu_to_le64(0); |
| 1011 | emeta_buf->next_id = cpu_to_le32(PBLK_LINE_EMPTY); |
| 1012 | emeta_buf->crc = cpu_to_le32(0); |
| 1013 | emeta_buf->prev_id = smeta_buf->prev_id; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1014 | |
| 1015 | return 1; |
| 1016 | } |
| 1017 | |
| 1018 | /* For now lines are always assumed full lines. Thus, smeta former and current |
| 1019 | * lun bitmaps are omitted. |
| 1020 | */ |
| 1021 | static int pblk_line_init_bb(struct pblk *pblk, struct pblk_line *line, |
| 1022 | int init) |
| 1023 | { |
| 1024 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1025 | struct nvm_geo *geo = &dev->geo; |
| 1026 | struct pblk_line_meta *lm = &pblk->lm; |
| 1027 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1028 | int nr_bb = 0; |
| 1029 | u64 off; |
| 1030 | int bit = -1; |
Hans Holmberg | cfe1c9e | 2018-03-30 00:04:50 +0200 | [diff] [blame] | 1031 | int emeta_secs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1032 | |
| 1033 | line->sec_in_line = lm->sec_per_line; |
| 1034 | |
| 1035 | /* Capture bad block information on line mapping bitmaps */ |
| 1036 | while ((bit = find_next_bit(line->blk_bitmap, lm->blk_per_line, |
| 1037 | bit + 1)) < lm->blk_per_line) { |
| 1038 | off = bit * geo->sec_per_pl; |
| 1039 | bitmap_shift_left(l_mg->bb_aux, l_mg->bb_template, off, |
| 1040 | lm->sec_per_line); |
| 1041 | bitmap_or(line->map_bitmap, line->map_bitmap, l_mg->bb_aux, |
| 1042 | lm->sec_per_line); |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 1043 | line->sec_in_line -= geo->sec_per_chk; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1044 | if (bit >= lm->emeta_bb) |
| 1045 | nr_bb++; |
| 1046 | } |
| 1047 | |
| 1048 | /* Mark smeta metadata sectors as bad sectors */ |
| 1049 | bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line); |
| 1050 | off = bit * geo->sec_per_pl; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1051 | bitmap_set(line->map_bitmap, off, lm->smeta_sec); |
| 1052 | line->sec_in_line -= lm->smeta_sec; |
| 1053 | line->smeta_ssec = off; |
| 1054 | line->cur_sec = off + lm->smeta_sec; |
| 1055 | |
Javier González | e2cddf2 | 2017-10-13 14:46:19 +0200 | [diff] [blame] | 1056 | if (init && pblk_line_submit_smeta_io(pblk, line, off, PBLK_WRITE)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1057 | pr_debug("pblk: line smeta I/O failed. Retry\n"); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1058 | return 1; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | bitmap_copy(line->invalid_bitmap, line->map_bitmap, lm->sec_per_line); |
| 1062 | |
| 1063 | /* Mark emeta metadata sectors as bad sectors. We need to consider bad |
| 1064 | * blocks to make sure that there are enough sectors to store emeta |
| 1065 | */ |
Hans Holmberg | cfe1c9e | 2018-03-30 00:04:50 +0200 | [diff] [blame] | 1066 | emeta_secs = lm->emeta_sec[0]; |
| 1067 | off = lm->sec_per_line; |
| 1068 | while (emeta_secs) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1069 | off -= geo->sec_per_pl; |
| 1070 | if (!test_bit(off, line->invalid_bitmap)) { |
| 1071 | bitmap_set(line->invalid_bitmap, off, geo->sec_per_pl); |
Hans Holmberg | cfe1c9e | 2018-03-30 00:04:50 +0200 | [diff] [blame] | 1072 | emeta_secs -= geo->sec_per_pl; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1076 | line->emeta_ssec = off; |
Hans Holmberg | cfe1c9e | 2018-03-30 00:04:50 +0200 | [diff] [blame] | 1077 | line->sec_in_line -= lm->emeta_sec[0]; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1078 | line->nr_valid_lbas = 0; |
Javier González | 0880a9a | 2017-06-26 11:57:19 +0200 | [diff] [blame] | 1079 | line->left_msecs = line->sec_in_line; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1080 | *line->vsc = cpu_to_le32(line->sec_in_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1081 | |
| 1082 | if (lm->sec_per_line - line->sec_in_line != |
| 1083 | bitmap_weight(line->invalid_bitmap, lm->sec_per_line)) { |
| 1084 | spin_lock(&line->lock); |
| 1085 | line->state = PBLK_LINESTATE_BAD; |
| 1086 | spin_unlock(&line->lock); |
| 1087 | |
| 1088 | list_add_tail(&line->list, &l_mg->bad_list); |
| 1089 | pr_err("pblk: unexpected line %d is bad\n", line->id); |
| 1090 | |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | return 1; |
| 1095 | } |
| 1096 | |
| 1097 | static int pblk_line_prepare(struct pblk *pblk, struct pblk_line *line) |
| 1098 | { |
| 1099 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 1100 | int blk_in_line = atomic_read(&line->blk_in_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1101 | |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 1102 | line->map_bitmap = kzalloc(lm->sec_bitmap_len, GFP_ATOMIC); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1103 | if (!line->map_bitmap) |
| 1104 | return -ENOMEM; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1105 | |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 1106 | /* will be initialized using bb info from map_bitmap */ |
| 1107 | line->invalid_bitmap = kmalloc(lm->sec_bitmap_len, GFP_ATOMIC); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1108 | if (!line->invalid_bitmap) { |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 1109 | kfree(line->map_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1110 | return -ENOMEM; |
| 1111 | } |
| 1112 | |
| 1113 | spin_lock(&line->lock); |
| 1114 | if (line->state != PBLK_LINESTATE_FREE) { |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 1115 | kfree(line->map_bitmap); |
| 1116 | kfree(line->invalid_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1117 | spin_unlock(&line->lock); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1118 | WARN(1, "pblk: corrupted line %d, state %d\n", |
| 1119 | line->id, line->state); |
| 1120 | return -EAGAIN; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1121 | } |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1122 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1123 | line->state = PBLK_LINESTATE_OPEN; |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 1124 | |
| 1125 | atomic_set(&line->left_eblks, blk_in_line); |
| 1126 | atomic_set(&line->left_seblks, blk_in_line); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1127 | |
| 1128 | line->meta_distance = lm->meta_distance; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1129 | spin_unlock(&line->lock); |
| 1130 | |
| 1131 | /* Bad blocks do not need to be erased */ |
| 1132 | bitmap_copy(line->erase_bitmap, line->blk_bitmap, lm->blk_per_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1133 | |
| 1134 | kref_init(&line->ref); |
| 1135 | |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | int pblk_line_recov_alloc(struct pblk *pblk, struct pblk_line *line) |
| 1140 | { |
| 1141 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1142 | int ret; |
| 1143 | |
| 1144 | spin_lock(&l_mg->free_lock); |
| 1145 | l_mg->data_line = line; |
| 1146 | list_del(&line->list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1147 | |
| 1148 | ret = pblk_line_prepare(pblk, line); |
| 1149 | if (ret) { |
| 1150 | list_add(&line->list, &l_mg->free_list); |
Javier González | 3dc001f | 2017-04-22 01:32:45 +0200 | [diff] [blame] | 1151 | spin_unlock(&l_mg->free_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1152 | return ret; |
| 1153 | } |
Javier González | 3dc001f | 2017-04-22 01:32:45 +0200 | [diff] [blame] | 1154 | spin_unlock(&l_mg->free_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1155 | |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 1156 | pblk_rl_free_lines_dec(&pblk->rl, line, true); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1157 | |
| 1158 | if (!pblk_line_init_bb(pblk, line, 0)) { |
| 1159 | list_add(&line->list, &l_mg->free_list); |
| 1160 | return -EINTR; |
| 1161 | } |
| 1162 | |
| 1163 | return 0; |
| 1164 | } |
| 1165 | |
| 1166 | void pblk_line_recov_close(struct pblk *pblk, struct pblk_line *line) |
| 1167 | { |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 1168 | kfree(line->map_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1169 | line->map_bitmap = NULL; |
| 1170 | line->smeta = NULL; |
| 1171 | line->emeta = NULL; |
| 1172 | } |
| 1173 | |
| 1174 | struct pblk_line *pblk_line_get(struct pblk *pblk) |
| 1175 | { |
| 1176 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1177 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1178 | struct pblk_line *line; |
| 1179 | int ret, bit; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1180 | |
| 1181 | lockdep_assert_held(&l_mg->free_lock); |
| 1182 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1183 | retry: |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1184 | if (list_empty(&l_mg->free_list)) { |
| 1185 | pr_err("pblk: no free lines\n"); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1186 | return NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | line = list_first_entry(&l_mg->free_list, struct pblk_line, list); |
| 1190 | list_del(&line->list); |
| 1191 | l_mg->nr_free_lines--; |
| 1192 | |
| 1193 | bit = find_first_zero_bit(line->blk_bitmap, lm->blk_per_line); |
| 1194 | if (unlikely(bit >= lm->blk_per_line)) { |
| 1195 | spin_lock(&line->lock); |
| 1196 | line->state = PBLK_LINESTATE_BAD; |
| 1197 | spin_unlock(&line->lock); |
| 1198 | |
| 1199 | list_add_tail(&line->list, &l_mg->bad_list); |
| 1200 | |
| 1201 | pr_debug("pblk: line %d is bad\n", line->id); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1202 | goto retry; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1203 | } |
| 1204 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1205 | ret = pblk_line_prepare(pblk, line); |
| 1206 | if (ret) { |
| 1207 | if (ret == -EAGAIN) { |
| 1208 | list_add(&line->list, &l_mg->corrupt_list); |
| 1209 | goto retry; |
| 1210 | } else { |
| 1211 | pr_err("pblk: failed to prepare line %d\n", line->id); |
| 1212 | list_add(&line->list, &l_mg->free_list); |
| 1213 | l_mg->nr_free_lines++; |
| 1214 | return NULL; |
| 1215 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1216 | } |
| 1217 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1218 | return line; |
| 1219 | } |
| 1220 | |
| 1221 | static struct pblk_line *pblk_line_retry(struct pblk *pblk, |
| 1222 | struct pblk_line *line) |
| 1223 | { |
| 1224 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1225 | struct pblk_line *retry_line; |
| 1226 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1227 | retry: |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1228 | spin_lock(&l_mg->free_lock); |
| 1229 | retry_line = pblk_line_get(pblk); |
| 1230 | if (!retry_line) { |
Javier González | be388d9 | 2017-04-22 01:32:48 +0200 | [diff] [blame] | 1231 | l_mg->data_line = NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1232 | spin_unlock(&l_mg->free_lock); |
| 1233 | return NULL; |
| 1234 | } |
| 1235 | |
| 1236 | retry_line->smeta = line->smeta; |
| 1237 | retry_line->emeta = line->emeta; |
| 1238 | retry_line->meta_line = line->meta_line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1239 | |
Javier González | be388d9 | 2017-04-22 01:32:48 +0200 | [diff] [blame] | 1240 | pblk_line_free(pblk, line); |
Javier González | 3dc001f | 2017-04-22 01:32:45 +0200 | [diff] [blame] | 1241 | l_mg->data_line = retry_line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1242 | spin_unlock(&l_mg->free_lock); |
| 1243 | |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 1244 | pblk_rl_free_lines_dec(&pblk->rl, line, false); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1245 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1246 | if (pblk_line_erase(pblk, retry_line)) |
| 1247 | goto retry; |
| 1248 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1249 | return retry_line; |
| 1250 | } |
| 1251 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1252 | static void pblk_set_space_limit(struct pblk *pblk) |
| 1253 | { |
| 1254 | struct pblk_rl *rl = &pblk->rl; |
| 1255 | |
| 1256 | atomic_set(&rl->rb_space, 0); |
| 1257 | } |
| 1258 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1259 | struct pblk_line *pblk_line_get_first_data(struct pblk *pblk) |
| 1260 | { |
| 1261 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1262 | struct pblk_line *line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1263 | |
| 1264 | spin_lock(&l_mg->free_lock); |
| 1265 | line = pblk_line_get(pblk); |
| 1266 | if (!line) { |
| 1267 | spin_unlock(&l_mg->free_lock); |
| 1268 | return NULL; |
| 1269 | } |
| 1270 | |
| 1271 | line->seq_nr = l_mg->d_seq_nr++; |
| 1272 | line->type = PBLK_LINETYPE_DATA; |
| 1273 | l_mg->data_line = line; |
| 1274 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1275 | pblk_line_setup_metadata(line, l_mg, &pblk->lm); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1276 | |
| 1277 | /* Allocate next line for preparation */ |
| 1278 | l_mg->data_next = pblk_line_get(pblk); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1279 | if (!l_mg->data_next) { |
| 1280 | /* If we cannot get a new line, we need to stop the pipeline. |
| 1281 | * Only allow as many writes in as we can store safely and then |
| 1282 | * fail gracefully |
| 1283 | */ |
| 1284 | pblk_set_space_limit(pblk); |
| 1285 | |
| 1286 | l_mg->data_next = NULL; |
| 1287 | } else { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1288 | l_mg->data_next->seq_nr = l_mg->d_seq_nr++; |
| 1289 | l_mg->data_next->type = PBLK_LINETYPE_DATA; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1290 | } |
| 1291 | spin_unlock(&l_mg->free_lock); |
| 1292 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1293 | if (pblk_line_erase(pblk, line)) { |
| 1294 | line = pblk_line_retry(pblk, line); |
| 1295 | if (!line) |
| 1296 | return NULL; |
| 1297 | } |
| 1298 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1299 | retry_setup: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1300 | if (!pblk_line_init_metadata(pblk, line, NULL)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1301 | line = pblk_line_retry(pblk, line); |
| 1302 | if (!line) |
| 1303 | return NULL; |
| 1304 | |
| 1305 | goto retry_setup; |
| 1306 | } |
| 1307 | |
| 1308 | if (!pblk_line_init_bb(pblk, line, 1)) { |
| 1309 | line = pblk_line_retry(pblk, line); |
| 1310 | if (!line) |
| 1311 | return NULL; |
| 1312 | |
| 1313 | goto retry_setup; |
| 1314 | } |
| 1315 | |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 1316 | pblk_rl_free_lines_dec(&pblk->rl, line, true); |
| 1317 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1318 | return line; |
| 1319 | } |
| 1320 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1321 | static void pblk_stop_writes(struct pblk *pblk, struct pblk_line *line) |
| 1322 | { |
| 1323 | lockdep_assert_held(&pblk->l_mg.free_lock); |
| 1324 | |
| 1325 | pblk_set_space_limit(pblk); |
| 1326 | pblk->state = PBLK_STATE_STOPPING; |
| 1327 | } |
| 1328 | |
Javier González | 8bd4002 | 2017-10-13 14:46:44 +0200 | [diff] [blame] | 1329 | static void pblk_line_close_meta_sync(struct pblk *pblk) |
| 1330 | { |
| 1331 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1332 | struct pblk_line_meta *lm = &pblk->lm; |
| 1333 | struct pblk_line *line, *tline; |
| 1334 | LIST_HEAD(list); |
| 1335 | |
| 1336 | spin_lock(&l_mg->close_lock); |
| 1337 | if (list_empty(&l_mg->emeta_list)) { |
| 1338 | spin_unlock(&l_mg->close_lock); |
| 1339 | return; |
| 1340 | } |
| 1341 | |
| 1342 | list_cut_position(&list, &l_mg->emeta_list, l_mg->emeta_list.prev); |
| 1343 | spin_unlock(&l_mg->close_lock); |
| 1344 | |
| 1345 | list_for_each_entry_safe(line, tline, &list, list) { |
| 1346 | struct pblk_emeta *emeta = line->emeta; |
| 1347 | |
| 1348 | while (emeta->mem < lm->emeta_len[0]) { |
| 1349 | int ret; |
| 1350 | |
| 1351 | ret = pblk_submit_meta_io(pblk, line); |
| 1352 | if (ret) { |
| 1353 | pr_err("pblk: sync meta line %d failed (%d)\n", |
| 1354 | line->id, ret); |
| 1355 | return; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | pblk_wait_for_meta(pblk); |
| 1361 | flush_workqueue(pblk->close_wq); |
| 1362 | } |
| 1363 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1364 | void pblk_pipeline_stop(struct pblk *pblk) |
| 1365 | { |
| 1366 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1367 | int ret; |
| 1368 | |
| 1369 | spin_lock(&l_mg->free_lock); |
| 1370 | if (pblk->state == PBLK_STATE_RECOVERING || |
| 1371 | pblk->state == PBLK_STATE_STOPPED) { |
| 1372 | spin_unlock(&l_mg->free_lock); |
| 1373 | return; |
| 1374 | } |
| 1375 | pblk->state = PBLK_STATE_RECOVERING; |
| 1376 | spin_unlock(&l_mg->free_lock); |
| 1377 | |
| 1378 | pblk_flush_writer(pblk); |
| 1379 | pblk_wait_for_meta(pblk); |
| 1380 | |
| 1381 | ret = pblk_recov_pad(pblk); |
| 1382 | if (ret) { |
| 1383 | pr_err("pblk: could not close data on teardown(%d)\n", ret); |
| 1384 | return; |
| 1385 | } |
| 1386 | |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 1387 | flush_workqueue(pblk->bb_wq); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1388 | pblk_line_close_meta_sync(pblk); |
| 1389 | |
| 1390 | spin_lock(&l_mg->free_lock); |
| 1391 | pblk->state = PBLK_STATE_STOPPED; |
| 1392 | l_mg->data_line = NULL; |
| 1393 | l_mg->data_next = NULL; |
| 1394 | spin_unlock(&l_mg->free_lock); |
| 1395 | } |
| 1396 | |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 1397 | struct pblk_line *pblk_line_replace_data(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1398 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1399 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 1400 | struct pblk_line *cur, *new = NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1401 | unsigned int left_seblks; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1402 | |
| 1403 | cur = l_mg->data_line; |
| 1404 | new = l_mg->data_next; |
| 1405 | if (!new) |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 1406 | goto out; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1407 | l_mg->data_line = new; |
| 1408 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1409 | spin_lock(&l_mg->free_lock); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1410 | if (pblk->state != PBLK_STATE_RUNNING) { |
| 1411 | l_mg->data_line = NULL; |
| 1412 | l_mg->data_next = NULL; |
| 1413 | spin_unlock(&l_mg->free_lock); |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 1414 | goto out; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1415 | } |
| 1416 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1417 | pblk_line_setup_metadata(new, l_mg, &pblk->lm); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1418 | spin_unlock(&l_mg->free_lock); |
| 1419 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1420 | retry_erase: |
| 1421 | left_seblks = atomic_read(&new->left_seblks); |
| 1422 | if (left_seblks) { |
| 1423 | /* If line is not fully erased, erase it */ |
| 1424 | if (atomic_read(&new->left_eblks)) { |
| 1425 | if (pblk_line_erase(pblk, new)) |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 1426 | goto out; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1427 | } else { |
| 1428 | io_schedule(); |
| 1429 | } |
| 1430 | goto retry_erase; |
| 1431 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1432 | |
| 1433 | retry_setup: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1434 | if (!pblk_line_init_metadata(pblk, new, cur)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1435 | new = pblk_line_retry(pblk, new); |
Javier González | f3236cef | 2017-04-22 01:32:46 +0200 | [diff] [blame] | 1436 | if (!new) |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 1437 | goto out; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1438 | |
| 1439 | goto retry_setup; |
| 1440 | } |
| 1441 | |
| 1442 | if (!pblk_line_init_bb(pblk, new, 1)) { |
| 1443 | new = pblk_line_retry(pblk, new); |
| 1444 | if (!new) |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 1445 | goto out; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1446 | |
| 1447 | goto retry_setup; |
| 1448 | } |
| 1449 | |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 1450 | pblk_rl_free_lines_dec(&pblk->rl, new, true); |
| 1451 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1452 | /* Allocate next line for preparation */ |
| 1453 | spin_lock(&l_mg->free_lock); |
| 1454 | l_mg->data_next = pblk_line_get(pblk); |
| 1455 | if (!l_mg->data_next) { |
| 1456 | /* If we cannot get a new line, we need to stop the pipeline. |
| 1457 | * Only allow as many writes in as we can store safely and then |
| 1458 | * fail gracefully |
| 1459 | */ |
| 1460 | pblk_stop_writes(pblk, new); |
| 1461 | l_mg->data_next = NULL; |
| 1462 | } else { |
| 1463 | l_mg->data_next->seq_nr = l_mg->d_seq_nr++; |
| 1464 | l_mg->data_next->type = PBLK_LINETYPE_DATA; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1465 | } |
| 1466 | spin_unlock(&l_mg->free_lock); |
| 1467 | |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 1468 | out: |
| 1469 | return new; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | void pblk_line_free(struct pblk *pblk, struct pblk_line *line) |
| 1473 | { |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 1474 | kfree(line->map_bitmap); |
| 1475 | kfree(line->invalid_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1476 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1477 | *line->vsc = cpu_to_le32(EMPTY_ENTRY); |
| 1478 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1479 | line->map_bitmap = NULL; |
| 1480 | line->invalid_bitmap = NULL; |
Javier González | be388d9 | 2017-04-22 01:32:48 +0200 | [diff] [blame] | 1481 | line->smeta = NULL; |
| 1482 | line->emeta = NULL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1483 | } |
| 1484 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 1485 | static void __pblk_line_put(struct pblk *pblk, struct pblk_line *line) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1486 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1487 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 1488 | struct pblk_gc *gc = &pblk->gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1489 | |
| 1490 | spin_lock(&line->lock); |
| 1491 | WARN_ON(line->state != PBLK_LINESTATE_GC); |
| 1492 | line->state = PBLK_LINESTATE_FREE; |
| 1493 | line->gc_group = PBLK_LINEGC_NONE; |
| 1494 | pblk_line_free(pblk, line); |
| 1495 | spin_unlock(&line->lock); |
| 1496 | |
Hans Holmberg | d6b992f | 2017-10-13 14:46:41 +0200 | [diff] [blame] | 1497 | atomic_dec(&gc->pipeline_gc); |
| 1498 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1499 | spin_lock(&l_mg->free_lock); |
| 1500 | list_add_tail(&line->list, &l_mg->free_list); |
| 1501 | l_mg->nr_free_lines++; |
| 1502 | spin_unlock(&l_mg->free_lock); |
| 1503 | |
| 1504 | pblk_rl_free_lines_inc(&pblk->rl, line); |
| 1505 | } |
| 1506 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 1507 | static void pblk_line_put_ws(struct work_struct *work) |
| 1508 | { |
| 1509 | struct pblk_line_ws *line_put_ws = container_of(work, |
| 1510 | struct pblk_line_ws, ws); |
| 1511 | struct pblk *pblk = line_put_ws->pblk; |
| 1512 | struct pblk_line *line = line_put_ws->line; |
| 1513 | |
| 1514 | __pblk_line_put(pblk, line); |
| 1515 | mempool_free(line_put_ws, pblk->gen_ws_pool); |
| 1516 | } |
| 1517 | |
| 1518 | void pblk_line_put(struct kref *ref) |
| 1519 | { |
| 1520 | struct pblk_line *line = container_of(ref, struct pblk_line, ref); |
| 1521 | struct pblk *pblk = line->pblk; |
| 1522 | |
| 1523 | __pblk_line_put(pblk, line); |
| 1524 | } |
| 1525 | |
| 1526 | void pblk_line_put_wq(struct kref *ref) |
| 1527 | { |
| 1528 | struct pblk_line *line = container_of(ref, struct pblk_line, ref); |
| 1529 | struct pblk *pblk = line->pblk; |
| 1530 | struct pblk_line_ws *line_put_ws; |
| 1531 | |
| 1532 | line_put_ws = mempool_alloc(pblk->gen_ws_pool, GFP_ATOMIC); |
| 1533 | if (!line_put_ws) |
| 1534 | return; |
| 1535 | |
| 1536 | line_put_ws->pblk = pblk; |
| 1537 | line_put_ws->line = line; |
| 1538 | line_put_ws->priv = NULL; |
| 1539 | |
| 1540 | INIT_WORK(&line_put_ws->ws, pblk_line_put_ws); |
| 1541 | queue_work(pblk->r_end_wq, &line_put_ws->ws); |
| 1542 | } |
| 1543 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1544 | int pblk_blk_erase_async(struct pblk *pblk, struct ppa_addr ppa) |
| 1545 | { |
| 1546 | struct nvm_rq *rqd; |
| 1547 | int err; |
| 1548 | |
Javier González | 67bf26a | 2017-10-13 14:46:20 +0200 | [diff] [blame] | 1549 | rqd = pblk_alloc_rqd(pblk, PBLK_ERASE); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1550 | |
| 1551 | pblk_setup_e_rq(pblk, rqd, ppa); |
| 1552 | |
| 1553 | rqd->end_io = pblk_end_io_erase; |
| 1554 | rqd->private = pblk; |
| 1555 | |
| 1556 | /* The write thread schedules erases so that it minimizes disturbances |
| 1557 | * with writes. Thus, there is no need to take the LUN semaphore. |
| 1558 | */ |
| 1559 | err = pblk_submit_io(pblk, rqd); |
| 1560 | if (err) { |
| 1561 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1562 | struct nvm_geo *geo = &dev->geo; |
| 1563 | |
| 1564 | pr_err("pblk: could not async erase line:%d,blk:%d\n", |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1565 | pblk_ppa_to_line(ppa), |
| 1566 | pblk_ppa_to_pos(geo, ppa)); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | return err; |
| 1570 | } |
| 1571 | |
| 1572 | struct pblk_line *pblk_line_get_data(struct pblk *pblk) |
| 1573 | { |
| 1574 | return pblk->l_mg.data_line; |
| 1575 | } |
| 1576 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 1577 | /* For now, always erase next line */ |
| 1578 | struct pblk_line *pblk_line_get_erase(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1579 | { |
| 1580 | return pblk->l_mg.data_next; |
| 1581 | } |
| 1582 | |
| 1583 | int pblk_line_is_full(struct pblk_line *line) |
| 1584 | { |
| 1585 | return (line->left_msecs == 0); |
| 1586 | } |
| 1587 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1588 | static void pblk_line_should_sync_meta(struct pblk *pblk) |
| 1589 | { |
| 1590 | if (pblk_rl_is_limit(&pblk->rl)) |
| 1591 | pblk_line_close_meta_sync(pblk); |
| 1592 | } |
| 1593 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1594 | void pblk_line_close(struct pblk *pblk, struct pblk_line *line) |
| 1595 | { |
| 1596 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1597 | struct list_head *move_list; |
| 1598 | |
Javier González | a84ebb8 | 2017-06-30 17:56:43 +0200 | [diff] [blame] | 1599 | #ifdef CONFIG_NVM_DEBUG |
| 1600 | struct pblk_line_meta *lm = &pblk->lm; |
| 1601 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1602 | WARN(!bitmap_full(line->map_bitmap, lm->sec_per_line), |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1603 | "pblk: corrupt closed line %d\n", line->id); |
Javier González | a84ebb8 | 2017-06-30 17:56:43 +0200 | [diff] [blame] | 1604 | #endif |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1605 | |
| 1606 | spin_lock(&l_mg->free_lock); |
| 1607 | WARN_ON(!test_and_clear_bit(line->meta_line, &l_mg->meta_bitmap)); |
| 1608 | spin_unlock(&l_mg->free_lock); |
| 1609 | |
| 1610 | spin_lock(&l_mg->gc_lock); |
| 1611 | spin_lock(&line->lock); |
| 1612 | WARN_ON(line->state != PBLK_LINESTATE_OPEN); |
| 1613 | line->state = PBLK_LINESTATE_CLOSED; |
| 1614 | move_list = pblk_line_gc_list(pblk, line); |
| 1615 | |
| 1616 | list_add_tail(&line->list, move_list); |
| 1617 | |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 1618 | kfree(line->map_bitmap); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1619 | line->map_bitmap = NULL; |
| 1620 | line->smeta = NULL; |
| 1621 | line->emeta = NULL; |
| 1622 | |
| 1623 | spin_unlock(&line->lock); |
| 1624 | spin_unlock(&l_mg->gc_lock); |
| 1625 | } |
| 1626 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1627 | void pblk_line_close_meta(struct pblk *pblk, struct pblk_line *line) |
| 1628 | { |
| 1629 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 1630 | struct pblk_line_meta *lm = &pblk->lm; |
| 1631 | struct pblk_emeta *emeta = line->emeta; |
| 1632 | struct line_emeta *emeta_buf = emeta->buf; |
| 1633 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1634 | /* No need for exact vsc value; avoid a big line lock and take aprox. */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1635 | memcpy(emeta_to_vsc(pblk, emeta_buf), l_mg->vsc_list, lm->vsc_list_len); |
| 1636 | memcpy(emeta_to_bb(emeta_buf), line->blk_bitmap, lm->blk_bitmap_len); |
| 1637 | |
| 1638 | emeta_buf->nr_valid_lbas = cpu_to_le64(line->nr_valid_lbas); |
| 1639 | emeta_buf->crc = cpu_to_le32(pblk_calc_emeta_crc(pblk, emeta_buf)); |
| 1640 | |
| 1641 | spin_lock(&l_mg->close_lock); |
| 1642 | spin_lock(&line->lock); |
| 1643 | list_add_tail(&line->list, &l_mg->emeta_list); |
| 1644 | spin_unlock(&line->lock); |
| 1645 | spin_unlock(&l_mg->close_lock); |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1646 | |
| 1647 | pblk_line_should_sync_meta(pblk); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 1648 | } |
| 1649 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1650 | void pblk_line_close_ws(struct work_struct *work) |
| 1651 | { |
| 1652 | struct pblk_line_ws *line_ws = container_of(work, struct pblk_line_ws, |
| 1653 | ws); |
| 1654 | struct pblk *pblk = line_ws->pblk; |
| 1655 | struct pblk_line *line = line_ws->line; |
| 1656 | |
| 1657 | pblk_line_close(pblk, line); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 1658 | mempool_free(line_ws, pblk->gen_ws_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1659 | } |
| 1660 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 1661 | void pblk_gen_run_ws(struct pblk *pblk, struct pblk_line *line, void *priv, |
| 1662 | void (*work)(struct work_struct *), gfp_t gfp_mask, |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 1663 | struct workqueue_struct *wq) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1664 | { |
| 1665 | struct pblk_line_ws *line_ws; |
| 1666 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 1667 | line_ws = mempool_alloc(pblk->gen_ws_pool, gfp_mask); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1668 | |
| 1669 | line_ws->pblk = pblk; |
| 1670 | line_ws->line = line; |
| 1671 | line_ws->priv = priv; |
| 1672 | |
| 1673 | INIT_WORK(&line_ws->ws, work); |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 1674 | queue_work(wq, &line_ws->ws); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1675 | } |
| 1676 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 1677 | static void __pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, |
| 1678 | int nr_ppas, int pos) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1679 | { |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 1680 | struct pblk_lun *rlun = &pblk->luns[pos]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1681 | int ret; |
| 1682 | |
| 1683 | /* |
| 1684 | * Only send one inflight I/O per LUN. Since we map at a page |
| 1685 | * granurality, all ppas in the I/O will map to the same LUN |
| 1686 | */ |
| 1687 | #ifdef CONFIG_NVM_DEBUG |
| 1688 | int i; |
| 1689 | |
| 1690 | for (i = 1; i < nr_ppas; i++) |
| 1691 | WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun || |
| 1692 | ppa_list[0].g.ch != ppa_list[i].g.ch); |
| 1693 | #endif |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1694 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 1695 | ret = down_timeout(&rlun->wr_sem, msecs_to_jiffies(30000)); |
Rakesh Pandit | c549384 | 2017-10-13 14:45:58 +0200 | [diff] [blame] | 1696 | if (ret == -ETIME || ret == -EINTR) |
| 1697 | pr_err("pblk: taking lun semaphore timed out: err %d\n", -ret); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1698 | } |
| 1699 | |
Javier González | 3eaa11e | 2017-07-07 21:08:52 +0200 | [diff] [blame] | 1700 | void pblk_down_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas) |
| 1701 | { |
| 1702 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1703 | struct nvm_geo *geo = &dev->geo; |
| 1704 | int pos = pblk_ppa_to_pos(geo, ppa_list[0]); |
| 1705 | |
| 1706 | __pblk_down_page(pblk, ppa_list, nr_ppas, pos); |
| 1707 | } |
| 1708 | |
| 1709 | void pblk_down_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas, |
| 1710 | unsigned long *lun_bitmap) |
| 1711 | { |
| 1712 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1713 | struct nvm_geo *geo = &dev->geo; |
| 1714 | int pos = pblk_ppa_to_pos(geo, ppa_list[0]); |
| 1715 | |
| 1716 | /* If the LUN has been locked for this same request, do no attempt to |
| 1717 | * lock it again |
| 1718 | */ |
| 1719 | if (test_and_set_bit(pos, lun_bitmap)) |
| 1720 | return; |
| 1721 | |
| 1722 | __pblk_down_page(pblk, ppa_list, nr_ppas, pos); |
| 1723 | } |
| 1724 | |
| 1725 | void pblk_up_page(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas) |
| 1726 | { |
| 1727 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1728 | struct nvm_geo *geo = &dev->geo; |
| 1729 | struct pblk_lun *rlun; |
| 1730 | int pos = pblk_ppa_to_pos(geo, ppa_list[0]); |
| 1731 | |
| 1732 | #ifdef CONFIG_NVM_DEBUG |
| 1733 | int i; |
| 1734 | |
| 1735 | for (i = 1; i < nr_ppas; i++) |
| 1736 | WARN_ON(ppa_list[0].g.lun != ppa_list[i].g.lun || |
| 1737 | ppa_list[0].g.ch != ppa_list[i].g.ch); |
| 1738 | #endif |
| 1739 | |
| 1740 | rlun = &pblk->luns[pos]; |
| 1741 | up(&rlun->wr_sem); |
| 1742 | } |
| 1743 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1744 | void pblk_up_rq(struct pblk *pblk, struct ppa_addr *ppa_list, int nr_ppas, |
| 1745 | unsigned long *lun_bitmap) |
| 1746 | { |
| 1747 | struct nvm_tgt_dev *dev = pblk->dev; |
| 1748 | struct nvm_geo *geo = &dev->geo; |
| 1749 | struct pblk_lun *rlun; |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 1750 | int nr_luns = geo->all_luns; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1751 | int bit = -1; |
| 1752 | |
| 1753 | while ((bit = find_next_bit(lun_bitmap, nr_luns, bit + 1)) < nr_luns) { |
| 1754 | rlun = &pblk->luns[bit]; |
| 1755 | up(&rlun->wr_sem); |
| 1756 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1757 | } |
| 1758 | |
| 1759 | void pblk_update_map(struct pblk *pblk, sector_t lba, struct ppa_addr ppa) |
| 1760 | { |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1761 | struct ppa_addr ppa_l2p; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1762 | |
| 1763 | /* logic error: lba out-of-bounds. Ignore update */ |
| 1764 | if (!(lba < pblk->rl.nr_secs)) { |
| 1765 | WARN(1, "pblk: corrupted L2P map request\n"); |
| 1766 | return; |
| 1767 | } |
| 1768 | |
| 1769 | spin_lock(&pblk->trans_lock); |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1770 | ppa_l2p = pblk_trans_map_get(pblk, lba); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1771 | |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1772 | if (!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p)) |
| 1773 | pblk_map_invalidate(pblk, ppa_l2p); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1774 | |
| 1775 | pblk_trans_map_set(pblk, lba, ppa); |
| 1776 | spin_unlock(&pblk->trans_lock); |
| 1777 | } |
| 1778 | |
| 1779 | void pblk_update_map_cache(struct pblk *pblk, sector_t lba, struct ppa_addr ppa) |
| 1780 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 1781 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1782 | #ifdef CONFIG_NVM_DEBUG |
| 1783 | /* Callers must ensure that the ppa points to a cache address */ |
| 1784 | BUG_ON(!pblk_addr_in_cache(ppa)); |
| 1785 | BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa))); |
| 1786 | #endif |
| 1787 | |
| 1788 | pblk_update_map(pblk, lba, ppa); |
| 1789 | } |
| 1790 | |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1791 | int pblk_update_map_gc(struct pblk *pblk, sector_t lba, struct ppa_addr ppa_new, |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 1792 | struct pblk_line *gc_line, u64 paddr_gc) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1793 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 1794 | struct ppa_addr ppa_l2p, ppa_gc; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1795 | int ret = 1; |
| 1796 | |
| 1797 | #ifdef CONFIG_NVM_DEBUG |
| 1798 | /* Callers must ensure that the ppa points to a cache address */ |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1799 | BUG_ON(!pblk_addr_in_cache(ppa_new)); |
| 1800 | BUG_ON(pblk_rb_pos_oob(&pblk->rwb, pblk_addr_to_cacheline(ppa_new))); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1801 | #endif |
| 1802 | |
| 1803 | /* logic error: lba out-of-bounds. Ignore update */ |
| 1804 | if (!(lba < pblk->rl.nr_secs)) { |
| 1805 | WARN(1, "pblk: corrupted L2P map request\n"); |
| 1806 | return 0; |
| 1807 | } |
| 1808 | |
| 1809 | spin_lock(&pblk->trans_lock); |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1810 | ppa_l2p = pblk_trans_map_get(pblk, lba); |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 1811 | ppa_gc = addr_to_gen_ppa(pblk, paddr_gc, gc_line->id); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1812 | |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 1813 | if (!pblk_ppa_comp(ppa_l2p, ppa_gc)) { |
| 1814 | spin_lock(&gc_line->lock); |
| 1815 | WARN(!test_bit(paddr_gc, gc_line->invalid_bitmap), |
| 1816 | "pblk: corrupted GC update"); |
| 1817 | spin_unlock(&gc_line->lock); |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1818 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1819 | ret = 0; |
| 1820 | goto out; |
| 1821 | } |
| 1822 | |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1823 | pblk_trans_map_set(pblk, lba, ppa_new); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1824 | out: |
| 1825 | spin_unlock(&pblk->trans_lock); |
| 1826 | return ret; |
| 1827 | } |
| 1828 | |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1829 | void pblk_update_map_dev(struct pblk *pblk, sector_t lba, |
| 1830 | struct ppa_addr ppa_mapped, struct ppa_addr ppa_cache) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1831 | { |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1832 | struct ppa_addr ppa_l2p; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1833 | |
| 1834 | #ifdef CONFIG_NVM_DEBUG |
| 1835 | /* Callers must ensure that the ppa points to a device address */ |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1836 | BUG_ON(pblk_addr_in_cache(ppa_mapped)); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1837 | #endif |
| 1838 | /* Invalidate and discard padded entries */ |
| 1839 | if (lba == ADDR_EMPTY) { |
| 1840 | #ifdef CONFIG_NVM_DEBUG |
| 1841 | atomic_long_inc(&pblk->padded_wb); |
| 1842 | #endif |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1843 | if (!pblk_ppa_empty(ppa_mapped)) |
| 1844 | pblk_map_invalidate(pblk, ppa_mapped); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1845 | return; |
| 1846 | } |
| 1847 | |
| 1848 | /* logic error: lba out-of-bounds. Ignore update */ |
| 1849 | if (!(lba < pblk->rl.nr_secs)) { |
| 1850 | WARN(1, "pblk: corrupted L2P map request\n"); |
| 1851 | return; |
| 1852 | } |
| 1853 | |
| 1854 | spin_lock(&pblk->trans_lock); |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1855 | ppa_l2p = pblk_trans_map_get(pblk, lba); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1856 | |
| 1857 | /* Do not update L2P if the cacheline has been updated. In this case, |
| 1858 | * the mapped ppa must be invalidated |
| 1859 | */ |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1860 | if (!pblk_ppa_comp(ppa_l2p, ppa_cache)) { |
| 1861 | if (!pblk_ppa_empty(ppa_mapped)) |
| 1862 | pblk_map_invalidate(pblk, ppa_mapped); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1863 | goto out; |
| 1864 | } |
| 1865 | |
| 1866 | #ifdef CONFIG_NVM_DEBUG |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1867 | WARN_ON(!pblk_addr_in_cache(ppa_l2p) && !pblk_ppa_empty(ppa_l2p)); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1868 | #endif |
| 1869 | |
Javier González | 9f6cb13 | 2017-10-13 14:46:12 +0200 | [diff] [blame] | 1870 | pblk_trans_map_set(pblk, lba, ppa_mapped); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1871 | out: |
| 1872 | spin_unlock(&pblk->trans_lock); |
| 1873 | } |
| 1874 | |
| 1875 | void pblk_lookup_l2p_seq(struct pblk *pblk, struct ppa_addr *ppas, |
| 1876 | sector_t blba, int nr_secs) |
| 1877 | { |
| 1878 | int i; |
| 1879 | |
| 1880 | spin_lock(&pblk->trans_lock); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 1881 | for (i = 0; i < nr_secs; i++) { |
| 1882 | struct ppa_addr ppa; |
| 1883 | |
| 1884 | ppa = ppas[i] = pblk_trans_map_get(pblk, blba + i); |
| 1885 | |
| 1886 | /* If the L2P entry maps to a line, the reference is valid */ |
| 1887 | if (!pblk_ppa_empty(ppa) && !pblk_addr_in_cache(ppa)) { |
Javier González | b1bcfda | 2018-01-05 14:16:06 +0100 | [diff] [blame] | 1888 | int line_id = pblk_ppa_to_line(ppa); |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 1889 | struct pblk_line *line = &pblk->lines[line_id]; |
| 1890 | |
| 1891 | kref_get(&line->ref); |
| 1892 | } |
| 1893 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1894 | spin_unlock(&pblk->trans_lock); |
| 1895 | } |
| 1896 | |
| 1897 | void pblk_lookup_l2p_rand(struct pblk *pblk, struct ppa_addr *ppas, |
| 1898 | u64 *lba_list, int nr_secs) |
| 1899 | { |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 1900 | u64 lba; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1901 | int i; |
| 1902 | |
| 1903 | spin_lock(&pblk->trans_lock); |
| 1904 | for (i = 0; i < nr_secs; i++) { |
| 1905 | lba = lba_list[i]; |
Javier González | d340121 | 2017-10-13 14:46:14 +0200 | [diff] [blame] | 1906 | if (lba != ADDR_EMPTY) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1907 | /* logic error: lba out-of-bounds. Ignore update */ |
| 1908 | if (!(lba < pblk->rl.nr_secs)) { |
| 1909 | WARN(1, "pblk: corrupted L2P map request\n"); |
| 1910 | continue; |
| 1911 | } |
| 1912 | ppas[i] = pblk_trans_map_get(pblk, lba); |
| 1913 | } |
| 1914 | } |
| 1915 | spin_unlock(&pblk->trans_lock); |
| 1916 | } |