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-map.c - pblk's lba-ppa mapping strategy |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "pblk.h" |
| 20 | |
| 21 | static void pblk_map_page_data(struct pblk *pblk, unsigned int sentry, |
| 22 | struct ppa_addr *ppa_list, |
| 23 | unsigned long *lun_bitmap, |
| 24 | struct pblk_sec_meta *meta_list, |
| 25 | unsigned int valid_secs) |
| 26 | { |
| 27 | struct pblk_line *line = pblk_line_get_data(pblk); |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 28 | struct pblk_emeta *emeta; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 29 | struct pblk_w_ctx *w_ctx; |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 30 | __le64 *lba_list; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 31 | u64 paddr; |
| 32 | int nr_secs = pblk->min_write_pgs; |
| 33 | int i; |
| 34 | |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 35 | if (pblk_line_is_full(line)) { |
| 36 | struct pblk_line *prev_line = line; |
| 37 | |
| 38 | line = pblk_line_replace_data(pblk); |
| 39 | pblk_line_close_meta(pblk, prev_line); |
| 40 | } |
| 41 | |
| 42 | emeta = line->emeta; |
| 43 | lba_list = emeta_to_lbas(pblk, emeta->buf); |
| 44 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 45 | paddr = pblk_alloc_page(pblk, line, nr_secs); |
| 46 | |
| 47 | for (i = 0; i < nr_secs; i++, paddr++) { |
Hans Holmberg | 03e868e | 2017-10-13 14:46:42 +0200 | [diff] [blame^] | 48 | __le64 addr_empty = cpu_to_le64(ADDR_EMPTY); |
| 49 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 50 | /* ppa to be sent to the device */ |
| 51 | ppa_list[i] = addr_to_gen_ppa(pblk, paddr, line->id); |
| 52 | |
| 53 | /* Write context for target bio completion on write buffer. Note |
| 54 | * that the write buffer is protected by the sync backpointer, |
| 55 | * and a single writer thread have access to each specific entry |
| 56 | * at a time. Thus, it is safe to modify the context for the |
| 57 | * entry we are setting up for submission without taking any |
| 58 | * lock or memory barrier. |
| 59 | */ |
| 60 | if (i < valid_secs) { |
| 61 | kref_get(&line->ref); |
| 62 | w_ctx = pblk_rb_w_ctx(&pblk->rwb, sentry + i); |
| 63 | w_ctx->ppa = ppa_list[i]; |
| 64 | meta_list[i].lba = cpu_to_le64(w_ctx->lba); |
| 65 | lba_list[paddr] = cpu_to_le64(w_ctx->lba); |
Hans Holmberg | 03e868e | 2017-10-13 14:46:42 +0200 | [diff] [blame^] | 66 | if (lba_list[paddr] != addr_empty) |
| 67 | line->nr_valid_lbas++; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 68 | } else { |
Javier González | caa69fa | 2017-06-26 11:57:12 +0200 | [diff] [blame] | 69 | lba_list[paddr] = meta_list[i].lba = addr_empty; |
Javier González | 0880a9a | 2017-06-26 11:57:19 +0200 | [diff] [blame] | 70 | __pblk_map_invalidate(pblk, line, paddr); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 74 | pblk_down_rq(pblk, ppa_list, nr_secs, lun_bitmap); |
| 75 | } |
| 76 | |
| 77 | void pblk_map_rq(struct pblk *pblk, struct nvm_rq *rqd, unsigned int sentry, |
| 78 | unsigned long *lun_bitmap, unsigned int valid_secs, |
| 79 | unsigned int off) |
| 80 | { |
| 81 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
| 82 | unsigned int map_secs; |
| 83 | int min = pblk->min_write_pgs; |
| 84 | int i; |
| 85 | |
| 86 | for (i = off; i < rqd->nr_ppas; i += min) { |
| 87 | map_secs = (i + min > valid_secs) ? (valid_secs % min) : min; |
| 88 | pblk_map_page_data(pblk, sentry + i, &rqd->ppa_list[i], |
| 89 | lun_bitmap, &meta_list[i], map_secs); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | /* only if erase_ppa is set, acquire erase semaphore */ |
| 94 | void pblk_map_erase_rq(struct pblk *pblk, struct nvm_rq *rqd, |
| 95 | unsigned int sentry, unsigned long *lun_bitmap, |
| 96 | unsigned int valid_secs, struct ppa_addr *erase_ppa) |
| 97 | { |
| 98 | struct nvm_tgt_dev *dev = pblk->dev; |
| 99 | struct nvm_geo *geo = &dev->geo; |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 100 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 101 | struct pblk_sec_meta *meta_list = rqd->meta_list; |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 102 | struct pblk_line *e_line, *d_line; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 103 | unsigned int map_secs; |
| 104 | int min = pblk->min_write_pgs; |
| 105 | int i, erase_lun; |
| 106 | |
| 107 | for (i = 0; i < rqd->nr_ppas; i += min) { |
| 108 | map_secs = (i + min > valid_secs) ? (valid_secs % min) : min; |
| 109 | pblk_map_page_data(pblk, sentry + i, &rqd->ppa_list[i], |
| 110 | lun_bitmap, &meta_list[i], map_secs); |
| 111 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 112 | erase_lun = pblk_ppa_to_pos(geo, rqd->ppa_list[i]); |
| 113 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 114 | /* line can change after page map. We might also be writing the |
| 115 | * last line. |
| 116 | */ |
| 117 | e_line = pblk_line_get_erase(pblk); |
| 118 | if (!e_line) |
| 119 | return pblk_map_rq(pblk, rqd, sentry, lun_bitmap, |
| 120 | valid_secs, i + min); |
| 121 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 122 | spin_lock(&e_line->lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 123 | if (!test_bit(erase_lun, e_line->erase_bitmap)) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 124 | set_bit(erase_lun, e_line->erase_bitmap); |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 125 | atomic_dec(&e_line->left_eblks); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 126 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 127 | *erase_ppa = rqd->ppa_list[i]; |
| 128 | erase_ppa->g.blk = e_line->id; |
| 129 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 130 | spin_unlock(&e_line->lock); |
| 131 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 132 | /* Avoid evaluating e_line->left_eblks */ |
| 133 | return pblk_map_rq(pblk, rqd, sentry, lun_bitmap, |
| 134 | valid_secs, i + min); |
| 135 | } |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 136 | spin_unlock(&e_line->lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 139 | d_line = pblk_line_get_data(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 140 | |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 141 | /* line can change after page map. We might also be writing the |
| 142 | * last line. |
| 143 | */ |
| 144 | e_line = pblk_line_get_erase(pblk); |
| 145 | if (!e_line) |
| 146 | return; |
| 147 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 148 | /* Erase blocks that are bad in this line but might not be in next */ |
| 149 | if (unlikely(ppa_empty(*erase_ppa)) && |
| 150 | bitmap_weight(d_line->blk_bitmap, lm->blk_per_line)) { |
| 151 | int bit = -1; |
| 152 | |
| 153 | retry: |
| 154 | bit = find_next_bit(d_line->blk_bitmap, |
| 155 | lm->blk_per_line, bit + 1); |
| 156 | if (bit >= lm->blk_per_line) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 157 | return; |
| 158 | |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 159 | spin_lock(&e_line->lock); |
| 160 | if (test_bit(bit, e_line->erase_bitmap)) { |
| 161 | spin_unlock(&e_line->lock); |
| 162 | goto retry; |
| 163 | } |
| 164 | spin_unlock(&e_line->lock); |
| 165 | |
| 166 | set_bit(bit, e_line->erase_bitmap); |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 167 | atomic_dec(&e_line->left_eblks); |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 168 | *erase_ppa = pblk->luns[bit].bppa; /* set ch and lun */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 169 | erase_ppa->g.blk = e_line->id; |
| 170 | } |
| 171 | } |