Javier González | 02a1520 | 2018-10-09 13:12:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 CNEX Labs |
| 4 | * Initial release: Javier Gonzalez <javier@cnexlabs.com> |
| 5 | * Matias Bjorling <matias@cnexlabs.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License version |
| 9 | * 2 as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * pblk-map.c - pblk's lba-ppa mapping strategy |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #include "pblk.h" |
| 21 | |
Javier González | 2deeefc | 2018-06-01 15:04:15 +0200 | [diff] [blame] | 22 | static int pblk_map_page_data(struct pblk *pblk, unsigned int sentry, |
| 23 | struct ppa_addr *ppa_list, |
| 24 | unsigned long *lun_bitmap, |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 25 | void *meta_list, |
Javier González | 2deeefc | 2018-06-01 15:04:15 +0200 | [diff] [blame] | 26 | unsigned int valid_secs) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 27 | { |
| 28 | struct pblk_line *line = pblk_line_get_data(pblk); |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 29 | struct pblk_emeta *emeta; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 30 | struct pblk_w_ctx *w_ctx; |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 31 | __le64 *lba_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 32 | u64 paddr; |
| 33 | int nr_secs = pblk->min_write_pgs; |
| 34 | int i; |
| 35 | |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 36 | if (!line) |
| 37 | return -ENOSPC; |
| 38 | |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 39 | if (pblk_line_is_full(line)) { |
| 40 | struct pblk_line *prev_line = line; |
| 41 | |
Javier González | 2deeefc | 2018-06-01 15:04:15 +0200 | [diff] [blame] | 42 | /* If we cannot allocate a new line, make sure to store metadata |
| 43 | * on current line and then fail |
| 44 | */ |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 45 | line = pblk_line_replace_data(pblk); |
| 46 | pblk_line_close_meta(pblk, prev_line); |
Javier González | 2deeefc | 2018-06-01 15:04:15 +0200 | [diff] [blame] | 47 | |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 48 | if (!line) { |
| 49 | pblk_pipeline_stop(pblk); |
| 50 | return -ENOSPC; |
| 51 | } |
| 52 | |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | emeta = line->emeta; |
| 56 | lba_list = emeta_to_lbas(pblk, emeta->buf); |
| 57 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 58 | paddr = pblk_alloc_page(pblk, line, nr_secs); |
| 59 | |
| 60 | for (i = 0; i < nr_secs; i++, paddr++) { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 61 | struct pblk_sec_meta *meta = pblk_get_meta(pblk, meta_list, i); |
Hans Holmberg | 03e868e | 2017-10-13 14:46:42 +0200 | [diff] [blame] | 62 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
| 63 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 64 | /* ppa to be sent to the device */ |
| 65 | ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id); |
| 66 | |
| 67 | /* Write context for target bio completion on write buffer. Note |
| 68 | * that the write buffer is protected by the sync backpointer, |
| 69 | * and a single writer thread have access to each specific entry |
| 70 | * at a time. Thus, it is safe to modify the context for the |
| 71 | * entry we are setting up for submission without taking any |
| 72 | * lock or memory barrier. |
| 73 | */ |
| 74 | if (i < valid_secs) { |
| 75 | kref_get(&line->ref); |
| 76 | w_ctx = pblk_rb_w_ctx(&pblk->rwb, sentry + i); |
| 77 | w_ctx->ppa = ppa_list[i]; |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 78 | meta->lba = cpu_to_le64(w_ctx->lba); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 79 | lba_list[paddr] = cpu_to_le64(w_ctx->lba); |
Hans Holmberg | 03e868e | 2017-10-13 14:46:42 +0200 | [diff] [blame] | 80 | if (lba_list[paddr] != addr_empty) |
| 81 | line->nr_valid_lbas++; |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 82 | else |
| 83 | atomic64_inc(&pblk->pad_wa); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 84 | } else { |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 85 | lba_list[paddr] = addr_empty; |
| 86 | meta->lba = addr_empty; |
Javier González | 0880a9a | 2017-06-26 11:57:19 +0200 | [diff] [blame] | 87 | __pblk_map_invalidate(pblk, line, paddr); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Matias Bjørling | 43241cf | 2018-10-09 13:11:51 +0200 | [diff] [blame] | 91 | pblk_down_rq(pblk, ppa_list[0], lun_bitmap); |
Javier González | 2deeefc | 2018-06-01 15:04:15 +0200 | [diff] [blame] | 92 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 93 | } |
| 94 | |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 95 | int pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 96 | unsigned long *lun_bitmap, unsigned int valid_secs, |
| 97 | unsigned int off) |
| 98 | { |
Igor Konopko | 55d8ec3 | 2018-12-11 20:16:26 +0100 | [diff] [blame] | 99 | void *meta_list = pblk_get_meta_for_writes(pblk, rqd); |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 100 | void *meta_buffer; |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 101 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 102 | unsigned int map_secs; |
| 103 | int min = pblk->min_write_pgs; |
| 104 | int i; |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 105 | int ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 106 | |
| 107 | for (i = off; i < rqd->nr_ppas; i += min) { |
| 108 | map_secs = (i + min > valid_secs) ? (valid_secs % min) : min; |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 109 | meta_buffer = pblk_get_meta(pblk, meta_list, i); |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 110 | |
| 111 | ret = pblk_map_page_data(pblk, sentry + i, &ppa_list[i], |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 112 | lun_bitmap, meta_buffer, map_secs); |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 113 | if (ret) |
| 114 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 115 | } |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 116 | |
| 117 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /* only if erase_ppa is set, acquire erase semaphore */ |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 121 | int pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 122 | unsigned int sentry, unsigned long *lun_bitmap, |
| 123 | unsigned int valid_secs, struct ppa_addr *erase_ppa) |
| 124 | { |
| 125 | struct nvm_tgt_dev *dev = pblk->dev; |
| 126 | struct nvm_geo *geo = &dev->geo; |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 127 | struct pblk_line_meta *lm = &pblk->lm; |
Igor Konopko | 55d8ec3 | 2018-12-11 20:16:26 +0100 | [diff] [blame] | 128 | void *meta_list = pblk_get_meta_for_writes(pblk, rqd); |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 129 | void *meta_buffer; |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 130 | struct ppa_addr *ppa_list = nvm_rq_to_ppa_list(rqd); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 131 | struct pblk_line *e_line, *d_line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 132 | unsigned int map_secs; |
| 133 | int min = pblk->min_write_pgs; |
| 134 | int i, erase_lun; |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 135 | int ret; |
| 136 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 137 | |
| 138 | for (i = 0; i < rqd->nr_ppas; i += min) { |
| 139 | map_secs = (i + min > valid_secs) ? (valid_secs % min) : min; |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 140 | meta_buffer = pblk_get_meta(pblk, meta_list, i); |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 141 | |
| 142 | ret = pblk_map_page_data(pblk, sentry + i, &ppa_list[i], |
Igor Konopko | faa79f2 | 2018-12-11 20:16:23 +0100 | [diff] [blame] | 143 | lun_bitmap, meta_buffer, map_secs); |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 144 | if (ret) |
| 145 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 146 | |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 147 | erase_lun = pblk_ppa_to_pos(geo, ppa_list[i]); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 148 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 149 | /* line can change after page map. We might also be writing the |
| 150 | * last line. |
| 151 | */ |
| 152 | e_line = pblk_line_get_erase(pblk); |
| 153 | if (!e_line) |
| 154 | return pblk_map_rq(pblk, rqd, sentry, lun_bitmap, |
| 155 | valid_secs, i + min); |
| 156 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 157 | spin_lock(&e_line->lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 158 | if (!test_bit(erase_lun, e_line->erase_bitmap)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 159 | set_bit(erase_lun, e_line->erase_bitmap); |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 160 | atomic_dec(&e_line->left_eblks); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 161 | |
Hans Holmberg | d68a934 | 2018-10-09 13:11:46 +0200 | [diff] [blame] | 162 | *erase_ppa = ppa_list[i]; |
Javier González | 6947151 | 2018-03-30 00:05:15 +0200 | [diff] [blame] | 163 | erase_ppa->a.blk = e_line->id; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 164 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 165 | spin_unlock(&e_line->lock); |
| 166 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 167 | /* Avoid evaluating e_line->left_eblks */ |
| 168 | return pblk_map_rq(pblk, rqd, sentry, lun_bitmap, |
| 169 | valid_secs, i + min); |
| 170 | } |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 171 | spin_unlock(&e_line->lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 172 | } |
| 173 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 174 | d_line = pblk_line_get_data(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 175 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 176 | /* line can change after page map. We might also be writing the |
| 177 | * last line. |
| 178 | */ |
| 179 | e_line = pblk_line_get_erase(pblk); |
| 180 | if (!e_line) |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 181 | return -ENOSPC; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 182 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 183 | /* Erase blocks that are bad in this line but might not be in next */ |
Matias Bjørling | 26f76dc | 2018-01-05 14:15:59 +0100 | [diff] [blame] | 184 | if (unlikely(pblk_ppa_empty(*erase_ppa)) && |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 185 | bitmap_weight(d_line->blk_bitmap, lm->blk_per_line)) { |
| 186 | int bit = -1; |
| 187 | |
| 188 | retry: |
| 189 | bit = find_next_bit(d_line->blk_bitmap, |
| 190 | lm->blk_per_line, bit + 1); |
| 191 | if (bit >= lm->blk_per_line) |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 192 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 193 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 194 | spin_lock(&e_line->lock); |
| 195 | if (test_bit(bit, e_line->erase_bitmap)) { |
| 196 | spin_unlock(&e_line->lock); |
| 197 | goto retry; |
| 198 | } |
| 199 | spin_unlock(&e_line->lock); |
| 200 | |
| 201 | set_bit(bit, e_line->erase_bitmap); |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 202 | atomic_dec(&e_line->left_eblks); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 203 | *erase_ppa = pblk->luns[bit].bppa; /* set ch and lun */ |
Javier González | 6947151 | 2018-03-30 00:05:15 +0200 | [diff] [blame] | 204 | erase_ppa->a.blk = e_line->id; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 205 | } |
Hans Holmberg | 525f7bb | 2018-12-11 20:16:12 +0100 | [diff] [blame] | 206 | |
| 207 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 208 | } |