Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 IT University of Copenhagen (rrpc.c) |
| 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 | * Implementation of a physical block-device target for Open-channel SSDs. |
| 17 | * |
| 18 | * pblk-init.c - pblk's initialization. |
| 19 | */ |
| 20 | |
| 21 | #include "pblk.h" |
| 22 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 23 | static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache, |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 24 | *pblk_w_rq_cache; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 25 | static DECLARE_RWSEM(pblk_lock); |
NeilBrown | b25d523 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 26 | struct bio_set *pblk_bio_set; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 27 | |
| 28 | static int pblk_rw_io(struct request_queue *q, struct pblk *pblk, |
| 29 | struct bio *bio) |
| 30 | { |
| 31 | int ret; |
| 32 | |
| 33 | /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap |
| 34 | * constraint. Writes can be of arbitrary size. |
| 35 | */ |
| 36 | if (bio_data_dir(bio) == READ) { |
NeilBrown | af67c31 | 2017-06-18 14:38:57 +1000 | [diff] [blame] | 37 | blk_queue_split(q, &bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 38 | ret = pblk_submit_read(pblk, bio); |
| 39 | if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED)) |
| 40 | bio_put(bio); |
| 41 | |
| 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | /* Prevent deadlock in the case of a modest LUN configuration and large |
| 46 | * user I/Os. Unless stalled, the rate limiter leaves at least 256KB |
| 47 | * available for user I/O. |
| 48 | */ |
Javier González | da67e68f | 2017-10-13 14:46:05 +0200 | [diff] [blame] | 49 | if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl)) |
NeilBrown | af67c31 | 2017-06-18 14:38:57 +1000 | [diff] [blame] | 50 | blk_queue_split(q, &bio); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 51 | |
| 52 | return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER); |
| 53 | } |
| 54 | |
| 55 | static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio) |
| 56 | { |
| 57 | struct pblk *pblk = q->queuedata; |
| 58 | |
| 59 | if (bio_op(bio) == REQ_OP_DISCARD) { |
| 60 | pblk_discard(pblk, bio); |
| 61 | if (!(bio->bi_opf & REQ_PREFLUSH)) { |
| 62 | bio_endio(bio); |
| 63 | return BLK_QC_T_NONE; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | switch (pblk_rw_io(q, pblk, bio)) { |
| 68 | case NVM_IO_ERR: |
| 69 | bio_io_error(bio); |
| 70 | break; |
| 71 | case NVM_IO_DONE: |
| 72 | bio_endio(bio); |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | return BLK_QC_T_NONE; |
| 77 | } |
| 78 | |
Hans Holmberg | c558619 | 2017-10-13 14:46:40 +0200 | [diff] [blame] | 79 | static size_t pblk_trans_map_size(struct pblk *pblk) |
| 80 | { |
| 81 | int entry_size = 8; |
| 82 | |
Javier González | bb845ae4 | 2018-03-30 00:05:19 +0200 | [diff] [blame^] | 83 | if (pblk->addrf_len < 32) |
Hans Holmberg | c558619 | 2017-10-13 14:46:40 +0200 | [diff] [blame] | 84 | entry_size = 4; |
| 85 | |
| 86 | return entry_size * pblk->rl.nr_secs; |
| 87 | } |
| 88 | |
| 89 | #ifdef CONFIG_NVM_DEBUG |
| 90 | static u32 pblk_l2p_crc(struct pblk *pblk) |
| 91 | { |
| 92 | size_t map_size; |
| 93 | u32 crc = ~(u32)0; |
| 94 | |
| 95 | map_size = pblk_trans_map_size(pblk); |
| 96 | crc = crc32_le(crc, pblk->trans_map, map_size); |
| 97 | return crc; |
| 98 | } |
| 99 | #endif |
| 100 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 101 | static void pblk_l2p_free(struct pblk *pblk) |
| 102 | { |
| 103 | vfree(pblk->trans_map); |
| 104 | } |
| 105 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 106 | static int pblk_l2p_recover(struct pblk *pblk, bool factory_init) |
| 107 | { |
| 108 | struct pblk_line *line = NULL; |
| 109 | |
| 110 | if (factory_init) { |
| 111 | pblk_setup_uuid(pblk); |
| 112 | } else { |
| 113 | line = pblk_recov_l2p(pblk); |
| 114 | if (IS_ERR(line)) { |
| 115 | pr_err("pblk: could not recover l2p table\n"); |
| 116 | return -EFAULT; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #ifdef CONFIG_NVM_DEBUG |
| 121 | pr_info("pblk init: L2P CRC: %x\n", pblk_l2p_crc(pblk)); |
| 122 | #endif |
| 123 | |
| 124 | /* Free full lines directly as GC has not been started yet */ |
| 125 | pblk_gc_free_full_lines(pblk); |
| 126 | |
| 127 | if (!line) { |
| 128 | /* Configure next line for user data */ |
| 129 | line = pblk_line_get_first_data(pblk); |
| 130 | if (!line) { |
| 131 | pr_err("pblk: line list corrupted\n"); |
| 132 | return -EFAULT; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static int pblk_l2p_init(struct pblk *pblk, bool factory_init) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 140 | { |
| 141 | sector_t i; |
| 142 | struct ppa_addr ppa; |
Hans Holmberg | c558619 | 2017-10-13 14:46:40 +0200 | [diff] [blame] | 143 | size_t map_size; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 144 | |
Hans Holmberg | c558619 | 2017-10-13 14:46:40 +0200 | [diff] [blame] | 145 | map_size = pblk_trans_map_size(pblk); |
| 146 | pblk->trans_map = vmalloc(map_size); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 147 | if (!pblk->trans_map) |
| 148 | return -ENOMEM; |
| 149 | |
| 150 | pblk_ppa_set_empty(&ppa); |
| 151 | |
| 152 | for (i = 0; i < pblk->rl.nr_secs; i++) |
| 153 | pblk_trans_map_set(pblk, i, ppa); |
| 154 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 155 | return pblk_l2p_recover(pblk, factory_init); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void pblk_rwb_free(struct pblk *pblk) |
| 159 | { |
| 160 | if (pblk_rb_tear_down_check(&pblk->rwb)) |
| 161 | pr_err("pblk: write buffer error on tear down\n"); |
| 162 | |
| 163 | pblk_rb_data_free(&pblk->rwb); |
| 164 | vfree(pblk_rb_entries_ref(&pblk->rwb)); |
| 165 | } |
| 166 | |
| 167 | static int pblk_rwb_init(struct pblk *pblk) |
| 168 | { |
| 169 | struct nvm_tgt_dev *dev = pblk->dev; |
| 170 | struct nvm_geo *geo = &dev->geo; |
| 171 | struct pblk_rb_entry *entries; |
| 172 | unsigned long nr_entries; |
| 173 | unsigned int power_size, power_seg_sz; |
| 174 | |
| 175 | nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer); |
| 176 | |
| 177 | entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry)); |
| 178 | if (!entries) |
| 179 | return -ENOMEM; |
| 180 | |
| 181 | power_size = get_count_order(nr_entries); |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 182 | power_seg_sz = get_count_order(geo->csecs); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 183 | |
| 184 | return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz); |
| 185 | } |
| 186 | |
| 187 | /* Minimum pages needed within a lun */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 188 | #define ADDR_POOL_SIZE 64 |
| 189 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 190 | static int pblk_set_addrf_12(struct nvm_geo *geo, struct nvm_addrf_12 *dst) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 191 | { |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 192 | struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf; |
| 193 | int power_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 194 | |
| 195 | /* Re-calculate channel and lun format to adapt to configuration */ |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 196 | power_len = get_count_order(geo->num_ch); |
| 197 | if (1 << power_len != geo->num_ch) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 198 | pr_err("pblk: supports only power-of-two channel config.\n"); |
| 199 | return -EINVAL; |
| 200 | } |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 201 | dst->ch_len = power_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 202 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 203 | power_len = get_count_order(geo->num_lun); |
| 204 | if (1 << power_len != geo->num_lun) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 205 | pr_err("pblk: supports only power-of-two LUN config.\n"); |
| 206 | return -EINVAL; |
| 207 | } |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 208 | dst->lun_len = power_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 209 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 210 | dst->blk_len = src->blk_len; |
| 211 | dst->pg_len = src->pg_len; |
| 212 | dst->pln_len = src->pln_len; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 213 | dst->sec_len = src->sec_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 214 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 215 | dst->sec_offset = 0; |
| 216 | dst->pln_offset = dst->sec_len; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 217 | dst->ch_offset = dst->pln_offset + dst->pln_len; |
| 218 | dst->lun_offset = dst->ch_offset + dst->ch_len; |
| 219 | dst->pg_offset = dst->lun_offset + dst->lun_len; |
| 220 | dst->blk_offset = dst->pg_offset + dst->pg_len; |
| 221 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 222 | dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 223 | dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset; |
| 224 | dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset; |
| 225 | dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset; |
| 226 | dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset; |
| 227 | dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset; |
| 228 | |
| 229 | return dst->blk_offset + src->blk_len; |
| 230 | } |
| 231 | |
Javier González | bb845ae4 | 2018-03-30 00:05:19 +0200 | [diff] [blame^] | 232 | static int pblk_set_addrf(struct pblk *pblk) |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 233 | { |
| 234 | struct nvm_tgt_dev *dev = pblk->dev; |
| 235 | struct nvm_geo *geo = &dev->geo; |
| 236 | int mod; |
| 237 | |
| 238 | div_u64_rem(geo->clba, pblk->min_write_pgs, &mod); |
| 239 | if (mod) { |
| 240 | pr_err("pblk: bad configuration of sectors/pages\n"); |
| 241 | return -EINVAL; |
| 242 | } |
| 243 | |
Javier González | bb845ae4 | 2018-03-30 00:05:19 +0200 | [diff] [blame^] | 244 | pblk->addrf_len = pblk_set_addrf_12(geo, (void *)&pblk->addrf); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int pblk_init_global_caches(struct pblk *pblk) |
| 250 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 251 | down_write(&pblk_lock); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 252 | pblk_ws_cache = kmem_cache_create("pblk_blk_ws", |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 253 | sizeof(struct pblk_line_ws), 0, 0, NULL); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 254 | if (!pblk_ws_cache) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 255 | up_write(&pblk_lock); |
| 256 | return -ENOMEM; |
| 257 | } |
| 258 | |
| 259 | pblk_rec_cache = kmem_cache_create("pblk_rec", |
| 260 | sizeof(struct pblk_rec_ctx), 0, 0, NULL); |
| 261 | if (!pblk_rec_cache) { |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 262 | kmem_cache_destroy(pblk_ws_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 263 | up_write(&pblk_lock); |
| 264 | return -ENOMEM; |
| 265 | } |
| 266 | |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 267 | pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 268 | 0, 0, NULL); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 269 | if (!pblk_g_rq_cache) { |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 270 | kmem_cache_destroy(pblk_ws_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 271 | kmem_cache_destroy(pblk_rec_cache); |
| 272 | up_write(&pblk_lock); |
| 273 | return -ENOMEM; |
| 274 | } |
| 275 | |
| 276 | pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size, |
| 277 | 0, 0, NULL); |
| 278 | if (!pblk_w_rq_cache) { |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 279 | kmem_cache_destroy(pblk_ws_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 280 | kmem_cache_destroy(pblk_rec_cache); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 281 | kmem_cache_destroy(pblk_g_rq_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 282 | up_write(&pblk_lock); |
| 283 | return -ENOMEM; |
| 284 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 285 | up_write(&pblk_lock); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | |
Rakesh Pandit | 22a4e06 | 2017-10-13 14:46:33 +0200 | [diff] [blame] | 290 | static void pblk_free_global_caches(struct pblk *pblk) |
| 291 | { |
| 292 | kmem_cache_destroy(pblk_ws_cache); |
| 293 | kmem_cache_destroy(pblk_rec_cache); |
| 294 | kmem_cache_destroy(pblk_g_rq_cache); |
| 295 | kmem_cache_destroy(pblk_w_rq_cache); |
| 296 | } |
| 297 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 298 | static int pblk_core_init(struct pblk *pblk) |
| 299 | { |
| 300 | struct nvm_tgt_dev *dev = pblk->dev; |
| 301 | struct nvm_geo *geo = &dev->geo; |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 302 | int max_write_ppas; |
| 303 | |
| 304 | atomic64_set(&pblk->user_wa, 0); |
| 305 | atomic64_set(&pblk->pad_wa, 0); |
| 306 | atomic64_set(&pblk->gc_wa, 0); |
| 307 | pblk->user_rst_wa = 0; |
| 308 | pblk->pad_rst_wa = 0; |
| 309 | pblk->gc_rst_wa = 0; |
| 310 | |
| 311 | atomic64_set(&pblk->nr_flush, 0); |
| 312 | pblk->nr_flush_rst = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 313 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 314 | pblk->pgs_in_buffer = geo->mw_cunits * geo->all_luns; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 315 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 316 | pblk->min_write_pgs = geo->ws_opt * (geo->csecs / PAGE_SIZE); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 317 | max_write_ppas = pblk->min_write_pgs * geo->all_luns; |
| 318 | pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA); |
| 319 | pblk_set_sec_per_write(pblk, pblk->min_write_pgs); |
| 320 | |
| 321 | if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) { |
| 322 | pr_err("pblk: vector list too big(%u > %u)\n", |
| 323 | pblk->max_write_pgs, PBLK_MAX_REQ_ADDRS); |
| 324 | return -EINVAL; |
| 325 | } |
| 326 | |
| 327 | pblk->pad_dist = kzalloc((pblk->min_write_pgs - 1) * sizeof(atomic64_t), |
| 328 | GFP_KERNEL); |
| 329 | if (!pblk->pad_dist) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 330 | return -ENOMEM; |
| 331 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 332 | if (pblk_init_global_caches(pblk)) |
| 333 | goto fail_free_pad_dist; |
| 334 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 335 | /* Internal bios can be at most the sectors signaled by the device. */ |
Matias Bjørling | 89a09c5 | 2018-03-30 00:05:04 +0200 | [diff] [blame] | 336 | pblk->page_bio_pool = mempool_create_page_pool(NVM_MAX_VLBA, 0); |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 337 | if (!pblk->page_bio_pool) |
Rakesh Pandit | 22a4e06 | 2017-10-13 14:46:33 +0200 | [diff] [blame] | 338 | goto free_global_caches; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 339 | |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 340 | pblk->gen_ws_pool = mempool_create_slab_pool(PBLK_GEN_WS_POOL_SIZE, |
| 341 | pblk_ws_cache); |
| 342 | if (!pblk->gen_ws_pool) |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 343 | goto free_page_bio_pool; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 344 | |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 345 | pblk->rec_pool = mempool_create_slab_pool(geo->all_luns, |
| 346 | pblk_rec_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 347 | if (!pblk->rec_pool) |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 348 | goto free_gen_ws_pool; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 349 | |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 350 | pblk->r_rq_pool = mempool_create_slab_pool(geo->all_luns, |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 351 | pblk_g_rq_cache); |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame] | 352 | if (!pblk->r_rq_pool) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 353 | goto free_rec_pool; |
| 354 | |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 355 | pblk->e_rq_pool = mempool_create_slab_pool(geo->all_luns, |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame] | 356 | pblk_g_rq_cache); |
| 357 | if (!pblk->e_rq_pool) |
| 358 | goto free_r_rq_pool; |
| 359 | |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 360 | pblk->w_rq_pool = mempool_create_slab_pool(geo->all_luns, |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 361 | pblk_w_rq_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 362 | if (!pblk->w_rq_pool) |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame] | 363 | goto free_e_rq_pool; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 364 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 365 | pblk->close_wq = alloc_workqueue("pblk-close-wq", |
| 366 | WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS); |
| 367 | if (!pblk->close_wq) |
Javier González | e72ec1d | 2017-10-13 14:46:09 +0200 | [diff] [blame] | 368 | goto free_w_rq_pool; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 369 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 370 | pblk->bb_wq = alloc_workqueue("pblk-bb-wq", |
| 371 | WQ_MEM_RECLAIM | WQ_UNBOUND, 0); |
| 372 | if (!pblk->bb_wq) |
| 373 | goto free_close_wq; |
| 374 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 375 | pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq", |
| 376 | WQ_MEM_RECLAIM | WQ_UNBOUND, 0); |
| 377 | if (!pblk->r_end_wq) |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 378 | goto free_bb_wq; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 379 | |
Javier González | bb845ae4 | 2018-03-30 00:05:19 +0200 | [diff] [blame^] | 380 | if (pblk_set_addrf(pblk)) |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 381 | goto free_r_end_wq; |
| 382 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 383 | INIT_LIST_HEAD(&pblk->compl_list); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 384 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 385 | return 0; |
| 386 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 387 | free_r_end_wq: |
| 388 | destroy_workqueue(pblk->r_end_wq); |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 389 | free_bb_wq: |
| 390 | destroy_workqueue(pblk->bb_wq); |
| 391 | free_close_wq: |
| 392 | destroy_workqueue(pblk->close_wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 393 | free_w_rq_pool: |
| 394 | mempool_destroy(pblk->w_rq_pool); |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame] | 395 | free_e_rq_pool: |
| 396 | mempool_destroy(pblk->e_rq_pool); |
| 397 | free_r_rq_pool: |
| 398 | mempool_destroy(pblk->r_rq_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 399 | free_rec_pool: |
| 400 | mempool_destroy(pblk->rec_pool); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 401 | free_gen_ws_pool: |
| 402 | mempool_destroy(pblk->gen_ws_pool); |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 403 | free_page_bio_pool: |
| 404 | mempool_destroy(pblk->page_bio_pool); |
Rakesh Pandit | 22a4e06 | 2017-10-13 14:46:33 +0200 | [diff] [blame] | 405 | free_global_caches: |
| 406 | pblk_free_global_caches(pblk); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 407 | fail_free_pad_dist: |
| 408 | kfree(pblk->pad_dist); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 409 | return -ENOMEM; |
| 410 | } |
| 411 | |
| 412 | static void pblk_core_free(struct pblk *pblk) |
| 413 | { |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 414 | if (pblk->close_wq) |
| 415 | destroy_workqueue(pblk->close_wq); |
| 416 | |
Javier González | 7bd4d37 | 2017-10-13 14:46:23 +0200 | [diff] [blame] | 417 | if (pblk->r_end_wq) |
| 418 | destroy_workqueue(pblk->r_end_wq); |
| 419 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame] | 420 | if (pblk->bb_wq) |
| 421 | destroy_workqueue(pblk->bb_wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 422 | |
Javier González | bd43241 | 2017-10-13 14:46:06 +0200 | [diff] [blame] | 423 | mempool_destroy(pblk->page_bio_pool); |
Javier González | b84ae4a8 | 2017-10-13 14:46:07 +0200 | [diff] [blame] | 424 | mempool_destroy(pblk->gen_ws_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 425 | mempool_destroy(pblk->rec_pool); |
Javier González | 0d88039 | 2017-10-13 14:46:08 +0200 | [diff] [blame] | 426 | mempool_destroy(pblk->r_rq_pool); |
| 427 | mempool_destroy(pblk->e_rq_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 428 | mempool_destroy(pblk->w_rq_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 429 | |
Rakesh Pandit | 22a4e06 | 2017-10-13 14:46:33 +0200 | [diff] [blame] | 430 | pblk_free_global_caches(pblk); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 431 | kfree(pblk->pad_dist); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 434 | static void pblk_line_mg_free(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 435 | { |
| 436 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 437 | int i; |
| 438 | |
| 439 | kfree(l_mg->bb_template); |
| 440 | kfree(l_mg->bb_aux); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 441 | kfree(l_mg->vsc_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 442 | |
| 443 | for (i = 0; i < PBLK_DATA_LINES; i++) { |
Javier González | f680f19 | 2017-06-26 11:57:21 +0200 | [diff] [blame] | 444 | kfree(l_mg->sline_meta[i]); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 445 | pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type); |
Javier González | f680f19 | 2017-06-26 11:57:21 +0200 | [diff] [blame] | 446 | kfree(l_mg->eline_meta[i]); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 447 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 448 | } |
| 449 | |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 450 | static void pblk_line_meta_free(struct pblk_line *line) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 451 | { |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 452 | kfree(line->blk_bitmap); |
| 453 | kfree(line->erase_bitmap); |
| 454 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 455 | |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 456 | static void pblk_lines_free(struct pblk *pblk) |
| 457 | { |
| 458 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 459 | struct pblk_line *line; |
| 460 | int i; |
| 461 | |
| 462 | spin_lock(&l_mg->free_lock); |
| 463 | for (i = 0; i < l_mg->nr_lines; i++) { |
| 464 | line = &pblk->lines[i]; |
| 465 | |
| 466 | pblk_line_free(pblk, line); |
| 467 | pblk_line_meta_free(line); |
| 468 | } |
| 469 | spin_unlock(&l_mg->free_lock); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 470 | |
| 471 | pblk_line_mg_free(pblk); |
| 472 | |
| 473 | kfree(pblk->luns); |
| 474 | kfree(pblk->lines); |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | static int pblk_bb_get_tbl(struct nvm_tgt_dev *dev, struct pblk_lun *rlun, |
| 478 | u8 *blks, int nr_blks) |
| 479 | { |
| 480 | struct ppa_addr ppa; |
| 481 | int ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 482 | |
| 483 | ppa.ppa = 0; |
| 484 | ppa.g.ch = rlun->bppa.g.ch; |
| 485 | ppa.g.lun = rlun->bppa.g.lun; |
| 486 | |
| 487 | ret = nvm_get_tgt_bb_tbl(dev, ppa, blks); |
| 488 | if (ret) |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 489 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 490 | |
| 491 | nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks); |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 492 | if (nr_blks < 0) |
| 493 | return -EIO; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 494 | |
Wei Yongjun | 5136a4f | 2017-04-25 16:15:30 +0000 | [diff] [blame] | 495 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 496 | } |
| 497 | |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 498 | static void *pblk_bb_get_log(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 499 | { |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 500 | struct nvm_tgt_dev *dev = pblk->dev; |
| 501 | struct nvm_geo *geo = &dev->geo; |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 502 | u8 *log; |
| 503 | int i, nr_blks, blk_per_lun; |
| 504 | int ret; |
| 505 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 506 | blk_per_lun = geo->num_chk * geo->pln_mode; |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 507 | nr_blks = blk_per_lun * geo->all_luns; |
| 508 | |
| 509 | log = kmalloc(nr_blks, GFP_KERNEL); |
| 510 | if (!log) |
| 511 | return ERR_PTR(-ENOMEM); |
| 512 | |
| 513 | for (i = 0; i < geo->all_luns; i++) { |
| 514 | struct pblk_lun *rlun = &pblk->luns[i]; |
| 515 | u8 *log_pos = log + i * blk_per_lun; |
| 516 | |
| 517 | ret = pblk_bb_get_tbl(dev, rlun, log_pos, blk_per_lun); |
| 518 | if (ret) { |
| 519 | kfree(log); |
| 520 | return ERR_PTR(-EIO); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return log; |
| 525 | } |
| 526 | |
| 527 | static int pblk_bb_line(struct pblk *pblk, struct pblk_line *line, |
| 528 | u8 *bb_log, int blk_per_line) |
| 529 | { |
| 530 | struct nvm_tgt_dev *dev = pblk->dev; |
| 531 | struct nvm_geo *geo = &dev->geo; |
| 532 | int i, bb_cnt = 0; |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 533 | int blk_per_lun = geo->num_chk * geo->pln_mode; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 534 | |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 535 | for (i = 0; i < blk_per_line; i++) { |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 536 | struct pblk_lun *rlun = &pblk->luns[i]; |
Heiner Litz | a38c78d | 2018-03-30 00:05:06 +0200 | [diff] [blame] | 537 | u8 *lun_bb_log = bb_log + i * blk_per_lun; |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 538 | |
| 539 | if (lun_bb_log[line->id] == NVM_BLK_T_FREE) |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 540 | continue; |
| 541 | |
| 542 | set_bit(pblk_ppa_to_pos(geo, rlun->bppa), line->blk_bitmap); |
| 543 | bb_cnt++; |
| 544 | } |
| 545 | |
| 546 | return bb_cnt; |
| 547 | } |
| 548 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 549 | static int pblk_luns_init(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 550 | { |
| 551 | struct nvm_tgt_dev *dev = pblk->dev; |
| 552 | struct nvm_geo *geo = &dev->geo; |
| 553 | struct pblk_lun *rlun; |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 554 | int i; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 555 | |
| 556 | /* TODO: Implement unbalanced LUN support */ |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 557 | if (geo->num_lun < 0) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 558 | pr_err("pblk: unbalanced LUN config.\n"); |
| 559 | return -EINVAL; |
| 560 | } |
| 561 | |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 562 | pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun), |
| 563 | GFP_KERNEL); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 564 | if (!pblk->luns) |
| 565 | return -ENOMEM; |
| 566 | |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 567 | for (i = 0; i < geo->all_luns; i++) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 568 | /* Stripe across channels */ |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 569 | int ch = i % geo->num_ch; |
| 570 | int lun_raw = i / geo->num_ch; |
| 571 | int lunid = lun_raw + ch * geo->num_lun; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 572 | |
| 573 | rlun = &pblk->luns[i]; |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 574 | rlun->bppa = dev->luns[lunid]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 575 | |
| 576 | sema_init(&rlun->wr_sem, 1); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | return 0; |
| 580 | } |
| 581 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 582 | /* See comment over struct line_emeta definition */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 583 | static unsigned int calc_emeta_len(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 584 | { |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 585 | struct pblk_line_meta *lm = &pblk->lm; |
| 586 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 587 | struct nvm_tgt_dev *dev = pblk->dev; |
| 588 | struct nvm_geo *geo = &dev->geo; |
| 589 | |
| 590 | /* Round to sector size so that lba_list starts on its own sector */ |
| 591 | lm->emeta_sec[1] = DIV_ROUND_UP( |
Hans Holmberg | 7675839 | 2018-03-30 00:04:52 +0200 | [diff] [blame] | 592 | sizeof(struct line_emeta) + lm->blk_bitmap_len + |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 593 | sizeof(struct wa_counters), geo->csecs); |
| 594 | lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 595 | |
| 596 | /* Round to sector size so that vsc_list starts on its own sector */ |
| 597 | lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0]; |
| 598 | lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64), |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 599 | geo->csecs); |
| 600 | lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 601 | |
| 602 | lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32), |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 603 | geo->csecs); |
| 604 | lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 605 | |
| 606 | lm->vsc_list_len = l_mg->nr_lines * sizeof(u32); |
| 607 | |
| 608 | return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | static void pblk_set_provision(struct pblk *pblk, long nr_free_blks) |
| 612 | { |
| 613 | struct nvm_tgt_dev *dev = pblk->dev; |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 614 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 615 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 616 | struct nvm_geo *geo = &dev->geo; |
| 617 | sector_t provisioned; |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 618 | int sec_meta, blk_meta; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 619 | |
Javier González | e539273 | 2018-01-05 14:16:14 +0100 | [diff] [blame] | 620 | if (geo->op == NVM_TARGET_DEFAULT_OP) |
| 621 | pblk->op = PBLK_DEFAULT_OP; |
| 622 | else |
| 623 | pblk->op = geo->op; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 624 | |
| 625 | provisioned = nr_free_blks; |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 626 | provisioned *= (100 - pblk->op); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 627 | sector_div(provisioned, 100); |
| 628 | |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 629 | pblk->op_blks = nr_free_blks - provisioned; |
| 630 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 631 | /* Internally pblk manages all free blocks, but all calculations based |
| 632 | * on user capacity consider only provisioned blocks |
| 633 | */ |
| 634 | pblk->rl.total_blocks = nr_free_blks; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 635 | pblk->rl.nr_secs = nr_free_blks * geo->clba; |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 636 | |
| 637 | /* Consider sectors used for metadata */ |
| 638 | sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines; |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 639 | blk_meta = DIV_ROUND_UP(sec_meta, geo->clba); |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 640 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 641 | pblk->capacity = (provisioned - blk_meta) * geo->clba; |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 642 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 643 | atomic_set(&pblk->rl.free_blocks, nr_free_blks); |
Javier González | a768993 | 2018-01-05 14:16:13 +0100 | [diff] [blame] | 644 | atomic_set(&pblk->rl.free_user_blocks, nr_free_blks); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 645 | } |
| 646 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 647 | static int pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line, |
| 648 | void *chunk_log, long *nr_bad_blks) |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 649 | { |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 650 | struct pblk_line_meta *lm = &pblk->lm; |
| 651 | |
| 652 | line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL); |
| 653 | if (!line->blk_bitmap) |
| 654 | return -ENOMEM; |
| 655 | |
| 656 | line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL); |
| 657 | if (!line->erase_bitmap) { |
| 658 | kfree(line->blk_bitmap); |
| 659 | return -ENOMEM; |
| 660 | } |
| 661 | |
| 662 | *nr_bad_blks = pblk_bb_line(pblk, line, chunk_log, lm->blk_per_line); |
| 663 | |
| 664 | return 0; |
| 665 | } |
| 666 | |
| 667 | static int pblk_line_mg_init(struct pblk *pblk) |
| 668 | { |
| 669 | struct nvm_tgt_dev *dev = pblk->dev; |
| 670 | struct nvm_geo *geo = &dev->geo; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 671 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 672 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 673 | int i, bb_distance; |
| 674 | |
Javier González | a40afad | 2018-03-30 00:05:14 +0200 | [diff] [blame] | 675 | l_mg->nr_lines = geo->num_chk; |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 676 | l_mg->log_line = l_mg->data_line = NULL; |
| 677 | l_mg->l_seq_nr = l_mg->d_seq_nr = 0; |
| 678 | l_mg->nr_free_lines = 0; |
| 679 | bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES); |
| 680 | |
| 681 | INIT_LIST_HEAD(&l_mg->free_list); |
| 682 | INIT_LIST_HEAD(&l_mg->corrupt_list); |
| 683 | INIT_LIST_HEAD(&l_mg->bad_list); |
| 684 | INIT_LIST_HEAD(&l_mg->gc_full_list); |
| 685 | INIT_LIST_HEAD(&l_mg->gc_high_list); |
| 686 | INIT_LIST_HEAD(&l_mg->gc_mid_list); |
| 687 | INIT_LIST_HEAD(&l_mg->gc_low_list); |
| 688 | INIT_LIST_HEAD(&l_mg->gc_empty_list); |
| 689 | |
| 690 | INIT_LIST_HEAD(&l_mg->emeta_list); |
| 691 | |
| 692 | l_mg->gc_lists[0] = &l_mg->gc_high_list; |
| 693 | l_mg->gc_lists[1] = &l_mg->gc_mid_list; |
| 694 | l_mg->gc_lists[2] = &l_mg->gc_low_list; |
| 695 | |
| 696 | spin_lock_init(&l_mg->free_lock); |
| 697 | spin_lock_init(&l_mg->close_lock); |
| 698 | spin_lock_init(&l_mg->gc_lock); |
| 699 | |
| 700 | l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL); |
| 701 | if (!l_mg->vsc_list) |
| 702 | goto fail; |
| 703 | |
| 704 | l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL); |
| 705 | if (!l_mg->bb_template) |
| 706 | goto fail_free_vsc_list; |
| 707 | |
| 708 | l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL); |
| 709 | if (!l_mg->bb_aux) |
| 710 | goto fail_free_bb_template; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 711 | |
| 712 | /* smeta is always small enough to fit on a kmalloc memory allocation, |
| 713 | * emeta depends on the number of LUNs allocated to the pblk instance |
| 714 | */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 715 | for (i = 0; i < PBLK_DATA_LINES; i++) { |
| 716 | l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL); |
| 717 | if (!l_mg->sline_meta[i]) |
| 718 | goto fail_free_smeta; |
| 719 | } |
| 720 | |
| 721 | /* emeta allocates three different buffers for managing metadata with |
| 722 | * in-memory and in-media layouts |
| 723 | */ |
| 724 | for (i = 0; i < PBLK_DATA_LINES; i++) { |
| 725 | struct pblk_emeta *emeta; |
| 726 | |
| 727 | emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL); |
| 728 | if (!emeta) |
| 729 | goto fail_free_emeta; |
| 730 | |
| 731 | if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) { |
| 732 | l_mg->emeta_alloc_type = PBLK_VMALLOC_META; |
| 733 | |
| 734 | emeta->buf = vmalloc(lm->emeta_len[0]); |
| 735 | if (!emeta->buf) { |
| 736 | kfree(emeta); |
| 737 | goto fail_free_emeta; |
| 738 | } |
| 739 | |
| 740 | emeta->nr_entries = lm->emeta_sec[0]; |
| 741 | l_mg->eline_meta[i] = emeta; |
| 742 | } else { |
| 743 | l_mg->emeta_alloc_type = PBLK_KMALLOC_META; |
| 744 | |
| 745 | emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL); |
| 746 | if (!emeta->buf) { |
| 747 | kfree(emeta); |
| 748 | goto fail_free_emeta; |
| 749 | } |
| 750 | |
| 751 | emeta->nr_entries = lm->emeta_sec[0]; |
| 752 | l_mg->eline_meta[i] = emeta; |
| 753 | } |
| 754 | } |
| 755 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 756 | for (i = 0; i < l_mg->nr_lines; i++) |
| 757 | l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY); |
| 758 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 759 | bb_distance = (geo->all_luns) * geo->ws_opt; |
| 760 | for (i = 0; i < lm->sec_per_line; i += bb_distance) |
| 761 | bitmap_set(l_mg->bb_template, i, geo->ws_opt); |
| 762 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 763 | return 0; |
| 764 | |
| 765 | fail_free_emeta: |
| 766 | while (--i >= 0) { |
Rakesh Pandit | c9d84b3 | 2017-10-13 14:45:54 +0200 | [diff] [blame] | 767 | if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META) |
| 768 | vfree(l_mg->eline_meta[i]->buf); |
| 769 | else |
| 770 | kfree(l_mg->eline_meta[i]->buf); |
Javier González | f680f19 | 2017-06-26 11:57:21 +0200 | [diff] [blame] | 771 | kfree(l_mg->eline_meta[i]); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 772 | } |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 773 | fail_free_smeta: |
| 774 | for (i = 0; i < PBLK_DATA_LINES; i++) |
Javier González | f680f19 | 2017-06-26 11:57:21 +0200 | [diff] [blame] | 775 | kfree(l_mg->sline_meta[i]); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 776 | kfree(l_mg->bb_aux); |
| 777 | fail_free_bb_template: |
| 778 | kfree(l_mg->bb_template); |
| 779 | fail_free_vsc_list: |
| 780 | kfree(l_mg->vsc_list); |
| 781 | fail: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 782 | return -ENOMEM; |
| 783 | } |
| 784 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 785 | static int pblk_line_meta_init(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 786 | { |
| 787 | struct nvm_tgt_dev *dev = pblk->dev; |
| 788 | struct nvm_geo *geo = &dev->geo; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 789 | struct pblk_line_meta *lm = &pblk->lm; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 790 | unsigned int smeta_len, emeta_len; |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 791 | int i; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 792 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 793 | lm->sec_per_line = geo->clba * geo->all_luns; |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 794 | lm->blk_per_line = geo->all_luns; |
| 795 | lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 796 | lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long); |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 797 | lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long); |
Rakesh Pandit | 27b9787 | 2017-10-13 14:46:28 +0200 | [diff] [blame] | 798 | lm->mid_thrs = lm->sec_per_line / 2; |
| 799 | lm->high_thrs = lm->sec_per_line / 4; |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 800 | lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 801 | |
| 802 | /* Calculate necessary pages for smeta. See comment over struct |
| 803 | * line_smeta definition |
| 804 | */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 805 | i = 1; |
| 806 | add_smeta_page: |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 807 | lm->smeta_sec = i * geo->ws_opt; |
| 808 | lm->smeta_len = lm->smeta_sec * geo->csecs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 809 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 810 | smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 811 | if (smeta_len > lm->smeta_len) { |
| 812 | i++; |
| 813 | goto add_smeta_page; |
| 814 | } |
| 815 | |
| 816 | /* Calculate necessary pages for emeta. See comment over struct |
| 817 | * line_emeta definition |
| 818 | */ |
| 819 | i = 1; |
| 820 | add_emeta_page: |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 821 | lm->emeta_sec[0] = i * geo->ws_opt; |
| 822 | lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 823 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 824 | emeta_len = calc_emeta_len(pblk); |
| 825 | if (emeta_len > lm->emeta_len[0]) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 826 | i++; |
| 827 | goto add_emeta_page; |
| 828 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 829 | |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 830 | lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0; |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 831 | |
| 832 | lm->min_blk_line = 1; |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 833 | if (geo->all_luns > 1) |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 834 | lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec + |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 835 | lm->emeta_sec[0], geo->clba); |
Javier González | 21d2287 | 2017-10-13 14:46:26 +0200 | [diff] [blame] | 836 | |
Javier González | b5e063a | 2017-06-30 17:56:41 +0200 | [diff] [blame] | 837 | if (lm->min_blk_line > lm->blk_per_line) { |
| 838 | pr_err("pblk: config. not supported. Min. LUN in line:%d\n", |
| 839 | lm->blk_per_line); |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 840 | return -EINVAL; |
Javier González | b5e063a | 2017-06-30 17:56:41 +0200 | [diff] [blame] | 841 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 842 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static int pblk_lines_init(struct pblk *pblk) |
| 847 | { |
| 848 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 849 | struct pblk_line_meta *lm = &pblk->lm; |
| 850 | struct pblk_line *line; |
| 851 | void *chunk_log; |
| 852 | long nr_bad_blks = 0, nr_free_blks = 0; |
| 853 | int i, ret; |
| 854 | |
| 855 | ret = pblk_line_meta_init(pblk); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 856 | if (ret) |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 857 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 858 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 859 | ret = pblk_line_mg_init(pblk); |
| 860 | if (ret) |
| 861 | return ret; |
| 862 | |
| 863 | ret = pblk_luns_init(pblk); |
| 864 | if (ret) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 865 | goto fail_free_meta; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 866 | |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 867 | chunk_log = pblk_bb_get_log(pblk); |
| 868 | if (IS_ERR(chunk_log)) { |
| 869 | pr_err("pblk: could not get bad block log (%lu)\n", |
| 870 | PTR_ERR(chunk_log)); |
| 871 | ret = PTR_ERR(chunk_log); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 872 | goto fail_free_luns; |
| 873 | } |
| 874 | |
| 875 | pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line), |
| 876 | GFP_KERNEL); |
| 877 | if (!pblk->lines) { |
| 878 | ret = -ENOMEM; |
| 879 | goto fail_free_chunk_log; |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 880 | } |
| 881 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 882 | for (i = 0; i < l_mg->nr_lines; i++) { |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 883 | int chk_in_line; |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 884 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 885 | line = &pblk->lines[i]; |
| 886 | |
| 887 | line->pblk = pblk; |
| 888 | line->id = i; |
| 889 | line->type = PBLK_LINETYPE_FREE; |
| 890 | line->state = PBLK_LINESTATE_FREE; |
| 891 | line->gc_group = PBLK_LINEGC_NONE; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 892 | line->vsc = &l_mg->vsc_list[i]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 893 | spin_lock_init(&line->lock); |
| 894 | |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 895 | ret = pblk_setup_line_meta(pblk, line, chunk_log, &nr_bad_blks); |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 896 | if (ret) |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 897 | goto fail_free_lines; |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 898 | |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 899 | chk_in_line = lm->blk_per_line - nr_bad_blks; |
| 900 | if (nr_bad_blks < 0 || nr_bad_blks > lm->blk_per_line || |
| 901 | chk_in_line < lm->min_blk_line) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 902 | line->state = PBLK_LINESTATE_BAD; |
| 903 | list_add_tail(&line->list, &l_mg->bad_list); |
| 904 | continue; |
| 905 | } |
| 906 | |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 907 | nr_free_blks += chk_in_line; |
| 908 | atomic_set(&line->blk_in_line, chk_in_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 909 | |
| 910 | l_mg->nr_free_lines++; |
| 911 | list_add_tail(&line->list, &l_mg->free_list); |
| 912 | } |
| 913 | |
| 914 | pblk_set_provision(pblk, nr_free_blks); |
| 915 | |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 916 | kfree(chunk_log); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 917 | return 0; |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 918 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 919 | fail_free_lines: |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 920 | while (--i >= 0) |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 921 | pblk_line_meta_free(&pblk->lines[i]); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 922 | kfree(pblk->lines); |
| 923 | fail_free_chunk_log: |
| 924 | kfree(chunk_log); |
| 925 | fail_free_luns: |
| 926 | kfree(pblk->luns); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 927 | fail_free_meta: |
Javier González | e411b33 | 2018-03-30 00:04:59 +0200 | [diff] [blame] | 928 | pblk_line_mg_free(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 929 | |
| 930 | return ret; |
| 931 | } |
| 932 | |
| 933 | static int pblk_writer_init(struct pblk *pblk) |
| 934 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 935 | pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t"); |
| 936 | if (IS_ERR(pblk->writer_ts)) { |
Javier González | cc4f5ba | 2018-01-05 14:16:17 +0100 | [diff] [blame] | 937 | int err = PTR_ERR(pblk->writer_ts); |
| 938 | |
| 939 | if (err != -EINTR) |
| 940 | pr_err("pblk: could not allocate writer kthread (%d)\n", |
| 941 | err); |
| 942 | return err; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 943 | } |
| 944 | |
Javier González | cc4f5ba | 2018-01-05 14:16:17 +0100 | [diff] [blame] | 945 | timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0); |
| 946 | mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100)); |
| 947 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 948 | return 0; |
| 949 | } |
| 950 | |
| 951 | static void pblk_writer_stop(struct pblk *pblk) |
| 952 | { |
Javier González | ee8d5c1 | 2017-06-30 17:56:40 +0200 | [diff] [blame] | 953 | /* The pipeline must be stopped and the write buffer emptied before the |
| 954 | * write thread is stopped |
| 955 | */ |
| 956 | WARN(pblk_rb_read_count(&pblk->rwb), |
| 957 | "Stopping not fully persisted write buffer\n"); |
| 958 | |
| 959 | WARN(pblk_rb_sync_count(&pblk->rwb), |
| 960 | "Stopping not fully synced write buffer\n"); |
| 961 | |
Hans Holmberg | 7be970b | 2018-03-30 00:04:56 +0200 | [diff] [blame] | 962 | del_timer_sync(&pblk->wtimer); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 963 | if (pblk->writer_ts) |
| 964 | kthread_stop(pblk->writer_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | static void pblk_free(struct pblk *pblk) |
| 968 | { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 969 | pblk_lines_free(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 970 | pblk_l2p_free(pblk); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 971 | pblk_rwb_free(pblk); |
| 972 | pblk_core_free(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 973 | |
| 974 | kfree(pblk); |
| 975 | } |
| 976 | |
| 977 | static void pblk_tear_down(struct pblk *pblk) |
| 978 | { |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 979 | pblk_pipeline_stop(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 980 | pblk_writer_stop(pblk); |
| 981 | pblk_rb_sync_l2p(&pblk->rwb); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 982 | pblk_rl_free(&pblk->rl); |
| 983 | |
| 984 | pr_debug("pblk: consistent tear down\n"); |
| 985 | } |
| 986 | |
| 987 | static void pblk_exit(void *private) |
| 988 | { |
| 989 | struct pblk *pblk = private; |
| 990 | |
| 991 | down_write(&pblk_lock); |
| 992 | pblk_gc_exit(pblk); |
| 993 | pblk_tear_down(pblk); |
Hans Holmberg | c558619 | 2017-10-13 14:46:40 +0200 | [diff] [blame] | 994 | |
| 995 | #ifdef CONFIG_NVM_DEBUG |
| 996 | pr_info("pblk exit: L2P CRC: %x\n", pblk_l2p_crc(pblk)); |
| 997 | #endif |
| 998 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 999 | pblk_free(pblk); |
| 1000 | up_write(&pblk_lock); |
| 1001 | } |
| 1002 | |
| 1003 | static sector_t pblk_capacity(void *private) |
| 1004 | { |
| 1005 | struct pblk *pblk = private; |
| 1006 | |
| 1007 | return pblk->capacity * NR_PHY_IN_LOG; |
| 1008 | } |
| 1009 | |
| 1010 | static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk, |
| 1011 | int flags) |
| 1012 | { |
| 1013 | struct nvm_geo *geo = &dev->geo; |
| 1014 | struct request_queue *bqueue = dev->q; |
| 1015 | struct request_queue *tqueue = tdisk->queue; |
| 1016 | struct pblk *pblk; |
| 1017 | int ret; |
| 1018 | |
Javier González | 7ad5039 | 2018-03-30 00:05:18 +0200 | [diff] [blame] | 1019 | if (geo->version != NVM_OCSSD_SPEC_12) { |
| 1020 | pr_err("pblk: OCSSD version not supported (%u)\n", |
| 1021 | geo->version); |
| 1022 | return ERR_PTR(-EINVAL); |
| 1023 | } |
| 1024 | |
| 1025 | if (geo->version == NVM_OCSSD_SPEC_12 && geo->dom & NVM_RSP_L2P) { |
Rakesh Pandit | 4e76af5 | 2017-10-13 14:45:57 +0200 | [diff] [blame] | 1026 | pr_err("pblk: host-side L2P table not supported. (%x)\n", |
Javier González | 7ad5039 | 2018-03-30 00:05:18 +0200 | [diff] [blame] | 1027 | geo->dom); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1028 | return ERR_PTR(-EINVAL); |
| 1029 | } |
| 1030 | |
| 1031 | pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL); |
| 1032 | if (!pblk) |
| 1033 | return ERR_PTR(-ENOMEM); |
| 1034 | |
| 1035 | pblk->dev = dev; |
| 1036 | pblk->disk = tdisk; |
Javier González | 588726d3 | 2017-06-26 11:57:29 +0200 | [diff] [blame] | 1037 | pblk->state = PBLK_STATE_RUNNING; |
Hans Holmberg | 3e3a5b8 | 2017-10-13 14:46:34 +0200 | [diff] [blame] | 1038 | pblk->gc.gc_enabled = 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1039 | |
| 1040 | spin_lock_init(&pblk->trans_lock); |
| 1041 | spin_lock_init(&pblk->lock); |
| 1042 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1043 | #ifdef CONFIG_NVM_DEBUG |
| 1044 | atomic_long_set(&pblk->inflight_writes, 0); |
| 1045 | atomic_long_set(&pblk->padded_writes, 0); |
| 1046 | atomic_long_set(&pblk->padded_wb, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1047 | atomic_long_set(&pblk->req_writes, 0); |
| 1048 | atomic_long_set(&pblk->sub_writes, 0); |
| 1049 | atomic_long_set(&pblk->sync_writes, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1050 | atomic_long_set(&pblk->inflight_reads, 0); |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 1051 | atomic_long_set(&pblk->cache_reads, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1052 | atomic_long_set(&pblk->sync_reads, 0); |
| 1053 | atomic_long_set(&pblk->recov_writes, 0); |
| 1054 | atomic_long_set(&pblk->recov_writes, 0); |
| 1055 | atomic_long_set(&pblk->recov_gc_writes, 0); |
Javier González | a112117 | 2017-10-13 14:46:01 +0200 | [diff] [blame] | 1056 | atomic_long_set(&pblk->recov_gc_reads, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1057 | #endif |
| 1058 | |
| 1059 | atomic_long_set(&pblk->read_failed, 0); |
| 1060 | atomic_long_set(&pblk->read_empty, 0); |
| 1061 | atomic_long_set(&pblk->read_high_ecc, 0); |
| 1062 | atomic_long_set(&pblk->read_failed_gc, 0); |
| 1063 | atomic_long_set(&pblk->write_failed, 0); |
| 1064 | atomic_long_set(&pblk->erase_failed, 0); |
| 1065 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 1066 | ret = pblk_core_init(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1067 | if (ret) { |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 1068 | pr_err("pblk: could not initialize core\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1069 | goto fail; |
| 1070 | } |
| 1071 | |
| 1072 | ret = pblk_lines_init(pblk); |
| 1073 | if (ret) { |
| 1074 | pr_err("pblk: could not initialize lines\n"); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1075 | goto fail_free_core; |
| 1076 | } |
| 1077 | |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 1078 | ret = pblk_rwb_init(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1079 | if (ret) { |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 1080 | pr_err("pblk: could not initialize write buffer\n"); |
| 1081 | goto fail_free_lines; |
| 1082 | } |
| 1083 | |
| 1084 | ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY); |
| 1085 | if (ret) { |
| 1086 | pr_err("pblk: could not initialize maps\n"); |
| 1087 | goto fail_free_rwb; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | ret = pblk_writer_init(pblk); |
| 1091 | if (ret) { |
Javier González | cc4f5ba | 2018-01-05 14:16:17 +0100 | [diff] [blame] | 1092 | if (ret != -EINTR) |
| 1093 | pr_err("pblk: could not initialize write thread\n"); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 1094 | goto fail_free_l2p; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | ret = pblk_gc_init(pblk); |
| 1098 | if (ret) { |
| 1099 | pr_err("pblk: could not initialize gc\n"); |
| 1100 | goto fail_stop_writer; |
| 1101 | } |
| 1102 | |
| 1103 | /* inherit the size from the underlying device */ |
| 1104 | blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue)); |
| 1105 | blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue)); |
| 1106 | |
| 1107 | blk_queue_write_cache(tqueue, true, false); |
| 1108 | |
Javier González | e46f4e4 | 2018-03-30 00:05:10 +0200 | [diff] [blame] | 1109 | tqueue->limits.discard_granularity = geo->clba * geo->csecs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1110 | tqueue->limits.discard_alignment = 0; |
| 1111 | blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9); |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 1112 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1113 | |
Javier González | 30d82a86 | 2018-01-05 14:16:19 +0100 | [diff] [blame] | 1114 | pr_info("pblk(%s): luns:%u, lines:%d, secs:%llu, buf entries:%u\n", |
| 1115 | tdisk->disk_name, |
Matias Bjørling | fae7fae | 2018-01-05 14:16:03 +0100 | [diff] [blame] | 1116 | geo->all_luns, pblk->l_mg.nr_lines, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1117 | (unsigned long long)pblk->rl.nr_secs, |
| 1118 | pblk->rwb.nr_entries); |
| 1119 | |
| 1120 | wake_up_process(pblk->writer_ts); |
Hans Holmberg | 03661b5 | 2017-10-13 14:46:37 +0200 | [diff] [blame] | 1121 | |
| 1122 | /* Check if we need to start GC */ |
| 1123 | pblk_gc_should_kick(pblk); |
| 1124 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1125 | return pblk; |
| 1126 | |
| 1127 | fail_stop_writer: |
| 1128 | pblk_writer_stop(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1129 | fail_free_l2p: |
| 1130 | pblk_l2p_free(pblk); |
Javier González | 43d4712 | 2018-03-30 00:05:09 +0200 | [diff] [blame] | 1131 | fail_free_rwb: |
| 1132 | pblk_rwb_free(pblk); |
| 1133 | fail_free_lines: |
| 1134 | pblk_lines_free(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1135 | fail_free_core: |
| 1136 | pblk_core_free(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1137 | fail: |
| 1138 | kfree(pblk); |
| 1139 | return ERR_PTR(ret); |
| 1140 | } |
| 1141 | |
| 1142 | /* physical block device target */ |
| 1143 | static struct nvm_tgt_type tt_pblk = { |
| 1144 | .name = "pblk", |
| 1145 | .version = {1, 0, 0}, |
| 1146 | |
| 1147 | .make_rq = pblk_make_rq, |
| 1148 | .capacity = pblk_capacity, |
| 1149 | |
| 1150 | .init = pblk_init, |
| 1151 | .exit = pblk_exit, |
| 1152 | |
| 1153 | .sysfs_init = pblk_sysfs_init, |
| 1154 | .sysfs_exit = pblk_sysfs_exit, |
Rakesh Pandit | 9001482 | 2017-10-13 14:45:50 +0200 | [diff] [blame] | 1155 | .owner = THIS_MODULE, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1156 | }; |
| 1157 | |
| 1158 | static int __init pblk_module_init(void) |
| 1159 | { |
NeilBrown | b25d523 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 1160 | int ret; |
| 1161 | |
| 1162 | pblk_bio_set = bioset_create(BIO_POOL_SIZE, 0, 0); |
| 1163 | if (!pblk_bio_set) |
| 1164 | return -ENOMEM; |
| 1165 | ret = nvm_register_tgt_type(&tt_pblk); |
| 1166 | if (ret) |
| 1167 | bioset_free(pblk_bio_set); |
| 1168 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | static void pblk_module_exit(void) |
| 1172 | { |
NeilBrown | b25d523 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 1173 | bioset_free(pblk_bio_set); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1174 | nvm_unregister_tgt_type(&tt_pblk); |
| 1175 | } |
| 1176 | |
| 1177 | module_init(pblk_module_init); |
| 1178 | module_exit(pblk_module_exit); |
| 1179 | MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>"); |
| 1180 | MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>"); |
| 1181 | MODULE_LICENSE("GPL v2"); |
| 1182 | MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs"); |