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 | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 23 | static struct kmem_cache *pblk_blk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache, |
| 24 | *pblk_w_rq_cache, *pblk_line_meta_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 | */ |
| 49 | if (unlikely(pblk_get_secs(bio) >= pblk_rl_sysfs_rate_show(&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 | |
| 79 | static void pblk_l2p_free(struct pblk *pblk) |
| 80 | { |
| 81 | vfree(pblk->trans_map); |
| 82 | } |
| 83 | |
| 84 | static int pblk_l2p_init(struct pblk *pblk) |
| 85 | { |
| 86 | sector_t i; |
| 87 | struct ppa_addr ppa; |
| 88 | int entry_size = 8; |
| 89 | |
| 90 | if (pblk->ppaf_bitsize < 32) |
| 91 | entry_size = 4; |
| 92 | |
| 93 | pblk->trans_map = vmalloc(entry_size * pblk->rl.nr_secs); |
| 94 | if (!pblk->trans_map) |
| 95 | return -ENOMEM; |
| 96 | |
| 97 | pblk_ppa_set_empty(&ppa); |
| 98 | |
| 99 | for (i = 0; i < pblk->rl.nr_secs; i++) |
| 100 | pblk_trans_map_set(pblk, i, ppa); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | static void pblk_rwb_free(struct pblk *pblk) |
| 106 | { |
| 107 | if (pblk_rb_tear_down_check(&pblk->rwb)) |
| 108 | pr_err("pblk: write buffer error on tear down\n"); |
| 109 | |
| 110 | pblk_rb_data_free(&pblk->rwb); |
| 111 | vfree(pblk_rb_entries_ref(&pblk->rwb)); |
| 112 | } |
| 113 | |
| 114 | static int pblk_rwb_init(struct pblk *pblk) |
| 115 | { |
| 116 | struct nvm_tgt_dev *dev = pblk->dev; |
| 117 | struct nvm_geo *geo = &dev->geo; |
| 118 | struct pblk_rb_entry *entries; |
| 119 | unsigned long nr_entries; |
| 120 | unsigned int power_size, power_seg_sz; |
| 121 | |
| 122 | nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer); |
| 123 | |
| 124 | entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry)); |
| 125 | if (!entries) |
| 126 | return -ENOMEM; |
| 127 | |
| 128 | power_size = get_count_order(nr_entries); |
| 129 | power_seg_sz = get_count_order(geo->sec_size); |
| 130 | |
| 131 | return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz); |
| 132 | } |
| 133 | |
| 134 | /* Minimum pages needed within a lun */ |
| 135 | #define PAGE_POOL_SIZE 16 |
| 136 | #define ADDR_POOL_SIZE 64 |
| 137 | |
| 138 | static int pblk_set_ppaf(struct pblk *pblk) |
| 139 | { |
| 140 | struct nvm_tgt_dev *dev = pblk->dev; |
| 141 | struct nvm_geo *geo = &dev->geo; |
| 142 | struct nvm_addr_format ppaf = geo->ppaf; |
| 143 | int power_len; |
| 144 | |
| 145 | /* Re-calculate channel and lun format to adapt to configuration */ |
| 146 | power_len = get_count_order(geo->nr_chnls); |
| 147 | if (1 << power_len != geo->nr_chnls) { |
| 148 | pr_err("pblk: supports only power-of-two channel config.\n"); |
| 149 | return -EINVAL; |
| 150 | } |
| 151 | ppaf.ch_len = power_len; |
| 152 | |
| 153 | power_len = get_count_order(geo->luns_per_chnl); |
| 154 | if (1 << power_len != geo->luns_per_chnl) { |
| 155 | pr_err("pblk: supports only power-of-two LUN config.\n"); |
| 156 | return -EINVAL; |
| 157 | } |
| 158 | ppaf.lun_len = power_len; |
| 159 | |
| 160 | pblk->ppaf.sec_offset = 0; |
| 161 | pblk->ppaf.pln_offset = ppaf.sect_len; |
| 162 | pblk->ppaf.ch_offset = pblk->ppaf.pln_offset + ppaf.pln_len; |
| 163 | pblk->ppaf.lun_offset = pblk->ppaf.ch_offset + ppaf.ch_len; |
| 164 | pblk->ppaf.pg_offset = pblk->ppaf.lun_offset + ppaf.lun_len; |
| 165 | pblk->ppaf.blk_offset = pblk->ppaf.pg_offset + ppaf.pg_len; |
| 166 | pblk->ppaf.sec_mask = (1ULL << ppaf.sect_len) - 1; |
| 167 | pblk->ppaf.pln_mask = ((1ULL << ppaf.pln_len) - 1) << |
| 168 | pblk->ppaf.pln_offset; |
| 169 | pblk->ppaf.ch_mask = ((1ULL << ppaf.ch_len) - 1) << |
| 170 | pblk->ppaf.ch_offset; |
| 171 | pblk->ppaf.lun_mask = ((1ULL << ppaf.lun_len) - 1) << |
| 172 | pblk->ppaf.lun_offset; |
| 173 | pblk->ppaf.pg_mask = ((1ULL << ppaf.pg_len) - 1) << |
| 174 | pblk->ppaf.pg_offset; |
| 175 | pblk->ppaf.blk_mask = ((1ULL << ppaf.blk_len) - 1) << |
| 176 | pblk->ppaf.blk_offset; |
| 177 | |
| 178 | pblk->ppaf_bitsize = pblk->ppaf.blk_offset + ppaf.blk_len; |
| 179 | |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | static int pblk_init_global_caches(struct pblk *pblk) |
| 184 | { |
| 185 | char cache_name[PBLK_CACHE_NAME_LEN]; |
| 186 | |
| 187 | down_write(&pblk_lock); |
| 188 | pblk_blk_ws_cache = kmem_cache_create("pblk_blk_ws", |
| 189 | sizeof(struct pblk_line_ws), 0, 0, NULL); |
| 190 | if (!pblk_blk_ws_cache) { |
| 191 | up_write(&pblk_lock); |
| 192 | return -ENOMEM; |
| 193 | } |
| 194 | |
| 195 | pblk_rec_cache = kmem_cache_create("pblk_rec", |
| 196 | sizeof(struct pblk_rec_ctx), 0, 0, NULL); |
| 197 | if (!pblk_rec_cache) { |
| 198 | kmem_cache_destroy(pblk_blk_ws_cache); |
| 199 | up_write(&pblk_lock); |
| 200 | return -ENOMEM; |
| 201 | } |
| 202 | |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 203 | 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] | 204 | 0, 0, NULL); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 205 | if (!pblk_g_rq_cache) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 206 | kmem_cache_destroy(pblk_blk_ws_cache); |
| 207 | kmem_cache_destroy(pblk_rec_cache); |
| 208 | up_write(&pblk_lock); |
| 209 | return -ENOMEM; |
| 210 | } |
| 211 | |
| 212 | pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size, |
| 213 | 0, 0, NULL); |
| 214 | if (!pblk_w_rq_cache) { |
| 215 | kmem_cache_destroy(pblk_blk_ws_cache); |
| 216 | kmem_cache_destroy(pblk_rec_cache); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 217 | kmem_cache_destroy(pblk_g_rq_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 218 | up_write(&pblk_lock); |
| 219 | return -ENOMEM; |
| 220 | } |
| 221 | |
| 222 | snprintf(cache_name, sizeof(cache_name), "pblk_line_m_%s", |
| 223 | pblk->disk->disk_name); |
| 224 | pblk_line_meta_cache = kmem_cache_create(cache_name, |
| 225 | pblk->lm.sec_bitmap_len, 0, 0, NULL); |
| 226 | if (!pblk_line_meta_cache) { |
| 227 | kmem_cache_destroy(pblk_blk_ws_cache); |
| 228 | kmem_cache_destroy(pblk_rec_cache); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 229 | kmem_cache_destroy(pblk_g_rq_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 230 | kmem_cache_destroy(pblk_w_rq_cache); |
| 231 | up_write(&pblk_lock); |
| 232 | return -ENOMEM; |
| 233 | } |
| 234 | up_write(&pblk_lock); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int pblk_core_init(struct pblk *pblk) |
| 240 | { |
| 241 | struct nvm_tgt_dev *dev = pblk->dev; |
| 242 | struct nvm_geo *geo = &dev->geo; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 243 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 244 | pblk->pgs_in_buffer = NVM_MEM_PAGE_WRITE * geo->sec_per_pg * |
| 245 | geo->nr_planes * geo->nr_luns; |
| 246 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 247 | if (pblk_init_global_caches(pblk)) |
| 248 | return -ENOMEM; |
| 249 | |
| 250 | pblk->page_pool = mempool_create_page_pool(PAGE_POOL_SIZE, 0); |
| 251 | if (!pblk->page_pool) |
| 252 | return -ENOMEM; |
| 253 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 254 | pblk->line_ws_pool = mempool_create_slab_pool(PBLK_WS_POOL_SIZE, |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 255 | pblk_blk_ws_cache); |
| 256 | if (!pblk->line_ws_pool) |
| 257 | goto free_page_pool; |
| 258 | |
| 259 | pblk->rec_pool = mempool_create_slab_pool(geo->nr_luns, pblk_rec_cache); |
| 260 | if (!pblk->rec_pool) |
| 261 | goto free_blk_ws_pool; |
| 262 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 263 | pblk->g_rq_pool = mempool_create_slab_pool(PBLK_READ_REQ_POOL_SIZE, |
| 264 | pblk_g_rq_cache); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 265 | if (!pblk->g_rq_pool) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 266 | goto free_rec_pool; |
| 267 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 268 | pblk->w_rq_pool = mempool_create_slab_pool(geo->nr_luns * 2, |
| 269 | pblk_w_rq_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 270 | if (!pblk->w_rq_pool) |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 271 | goto free_g_rq_pool; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 272 | |
| 273 | pblk->line_meta_pool = |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 274 | mempool_create_slab_pool(PBLK_META_POOL_SIZE, |
| 275 | pblk_line_meta_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 276 | if (!pblk->line_meta_pool) |
| 277 | goto free_w_rq_pool; |
| 278 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 279 | pblk->close_wq = alloc_workqueue("pblk-close-wq", |
| 280 | WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS); |
| 281 | if (!pblk->close_wq) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 282 | goto free_line_meta_pool; |
| 283 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 284 | pblk->bb_wq = alloc_workqueue("pblk-bb-wq", |
| 285 | WQ_MEM_RECLAIM | WQ_UNBOUND, 0); |
| 286 | if (!pblk->bb_wq) |
| 287 | goto free_close_wq; |
| 288 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 289 | if (pblk_set_ppaf(pblk)) |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 290 | goto free_bb_wq; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 291 | |
| 292 | if (pblk_rwb_init(pblk)) |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 293 | goto free_bb_wq; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 294 | |
| 295 | INIT_LIST_HEAD(&pblk->compl_list); |
| 296 | return 0; |
| 297 | |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 298 | free_bb_wq: |
| 299 | destroy_workqueue(pblk->bb_wq); |
| 300 | free_close_wq: |
| 301 | destroy_workqueue(pblk->close_wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 302 | free_line_meta_pool: |
| 303 | mempool_destroy(pblk->line_meta_pool); |
| 304 | free_w_rq_pool: |
| 305 | mempool_destroy(pblk->w_rq_pool); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 306 | free_g_rq_pool: |
| 307 | mempool_destroy(pblk->g_rq_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 308 | free_rec_pool: |
| 309 | mempool_destroy(pblk->rec_pool); |
| 310 | free_blk_ws_pool: |
| 311 | mempool_destroy(pblk->line_ws_pool); |
| 312 | free_page_pool: |
| 313 | mempool_destroy(pblk->page_pool); |
| 314 | return -ENOMEM; |
| 315 | } |
| 316 | |
| 317 | static void pblk_core_free(struct pblk *pblk) |
| 318 | { |
Javier González | ef57649 | 2017-06-26 11:57:28 +0200 | [diff] [blame^] | 319 | if (pblk->close_wq) |
| 320 | destroy_workqueue(pblk->close_wq); |
| 321 | |
| 322 | if (pblk->bb_wq) |
| 323 | destroy_workqueue(pblk->bb_wq); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 324 | |
| 325 | mempool_destroy(pblk->page_pool); |
| 326 | mempool_destroy(pblk->line_ws_pool); |
| 327 | mempool_destroy(pblk->rec_pool); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 328 | mempool_destroy(pblk->g_rq_pool); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 329 | mempool_destroy(pblk->w_rq_pool); |
| 330 | mempool_destroy(pblk->line_meta_pool); |
| 331 | |
| 332 | kmem_cache_destroy(pblk_blk_ws_cache); |
| 333 | kmem_cache_destroy(pblk_rec_cache); |
Javier González | 084ec9b | 2017-06-26 16:27:13 -0600 | [diff] [blame] | 334 | kmem_cache_destroy(pblk_g_rq_cache); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 335 | kmem_cache_destroy(pblk_w_rq_cache); |
| 336 | kmem_cache_destroy(pblk_line_meta_cache); |
| 337 | } |
| 338 | |
| 339 | static void pblk_luns_free(struct pblk *pblk) |
| 340 | { |
| 341 | kfree(pblk->luns); |
| 342 | } |
| 343 | |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 344 | static void pblk_free_line_bitmaps(struct pblk_line *line) |
| 345 | { |
| 346 | kfree(line->blk_bitmap); |
| 347 | kfree(line->erase_bitmap); |
| 348 | } |
| 349 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 350 | static void pblk_lines_free(struct pblk *pblk) |
| 351 | { |
| 352 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 353 | struct pblk_line *line; |
| 354 | int i; |
| 355 | |
| 356 | spin_lock(&l_mg->free_lock); |
| 357 | for (i = 0; i < l_mg->nr_lines; i++) { |
| 358 | line = &pblk->lines[i]; |
| 359 | |
| 360 | pblk_line_free(pblk, line); |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 361 | pblk_free_line_bitmaps(line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 362 | } |
| 363 | spin_unlock(&l_mg->free_lock); |
| 364 | } |
| 365 | |
| 366 | static void pblk_line_meta_free(struct pblk *pblk) |
| 367 | { |
| 368 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 369 | int i; |
| 370 | |
| 371 | kfree(l_mg->bb_template); |
| 372 | kfree(l_mg->bb_aux); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 373 | kfree(l_mg->vsc_list); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 374 | |
| 375 | for (i = 0; i < PBLK_DATA_LINES; i++) { |
Javier González | f680f19 | 2017-06-26 11:57:21 +0200 | [diff] [blame] | 376 | kfree(l_mg->sline_meta[i]); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 377 | 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] | 378 | kfree(l_mg->eline_meta[i]); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | kfree(pblk->lines); |
| 382 | } |
| 383 | |
| 384 | static int pblk_bb_discovery(struct nvm_tgt_dev *dev, struct pblk_lun *rlun) |
| 385 | { |
| 386 | struct nvm_geo *geo = &dev->geo; |
| 387 | struct ppa_addr ppa; |
| 388 | u8 *blks; |
| 389 | int nr_blks, ret; |
| 390 | |
| 391 | nr_blks = geo->blks_per_lun * geo->plane_mode; |
| 392 | blks = kmalloc(nr_blks, GFP_KERNEL); |
| 393 | if (!blks) |
| 394 | return -ENOMEM; |
| 395 | |
| 396 | ppa.ppa = 0; |
| 397 | ppa.g.ch = rlun->bppa.g.ch; |
| 398 | ppa.g.lun = rlun->bppa.g.lun; |
| 399 | |
| 400 | ret = nvm_get_tgt_bb_tbl(dev, ppa, blks); |
| 401 | if (ret) |
| 402 | goto out; |
| 403 | |
| 404 | nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks); |
| 405 | if (nr_blks < 0) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 406 | ret = nr_blks; |
Wei Yongjun | 5136a4f | 2017-04-25 16:15:30 +0000 | [diff] [blame] | 407 | goto out; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | rlun->bb_list = blks; |
| 411 | |
Wei Yongjun | 5136a4f | 2017-04-25 16:15:30 +0000 | [diff] [blame] | 412 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 413 | out: |
Wei Yongjun | 5136a4f | 2017-04-25 16:15:30 +0000 | [diff] [blame] | 414 | kfree(blks); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 415 | return ret; |
| 416 | } |
| 417 | |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 418 | static int pblk_bb_line(struct pblk *pblk, struct pblk_line *line, |
| 419 | int blk_per_line) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 420 | { |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 421 | struct nvm_tgt_dev *dev = pblk->dev; |
| 422 | struct nvm_geo *geo = &dev->geo; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 423 | struct pblk_lun *rlun; |
| 424 | int bb_cnt = 0; |
| 425 | int i; |
| 426 | |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 427 | for (i = 0; i < blk_per_line; i++) { |
| 428 | rlun = &pblk->luns[i]; |
| 429 | if (rlun->bb_list[line->id] == NVM_BLK_T_FREE) |
| 430 | continue; |
| 431 | |
| 432 | set_bit(pblk_ppa_to_pos(geo, rlun->bppa), line->blk_bitmap); |
| 433 | bb_cnt++; |
| 434 | } |
| 435 | |
| 436 | return bb_cnt; |
| 437 | } |
| 438 | |
| 439 | static int pblk_alloc_line_bitmaps(struct pblk *pblk, struct pblk_line *line) |
| 440 | { |
| 441 | struct pblk_line_meta *lm = &pblk->lm; |
| 442 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 443 | line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL); |
| 444 | if (!line->blk_bitmap) |
| 445 | return -ENOMEM; |
| 446 | |
| 447 | line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL); |
| 448 | if (!line->erase_bitmap) { |
| 449 | kfree(line->blk_bitmap); |
| 450 | return -ENOMEM; |
| 451 | } |
| 452 | |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 453 | return 0; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static int pblk_luns_init(struct pblk *pblk, struct ppa_addr *luns) |
| 457 | { |
| 458 | struct nvm_tgt_dev *dev = pblk->dev; |
| 459 | struct nvm_geo *geo = &dev->geo; |
| 460 | struct pblk_lun *rlun; |
| 461 | int i, ret; |
| 462 | |
| 463 | /* TODO: Implement unbalanced LUN support */ |
| 464 | if (geo->luns_per_chnl < 0) { |
| 465 | pr_err("pblk: unbalanced LUN config.\n"); |
| 466 | return -EINVAL; |
| 467 | } |
| 468 | |
| 469 | pblk->luns = kcalloc(geo->nr_luns, sizeof(struct pblk_lun), GFP_KERNEL); |
| 470 | if (!pblk->luns) |
| 471 | return -ENOMEM; |
| 472 | |
| 473 | for (i = 0; i < geo->nr_luns; i++) { |
| 474 | /* Stripe across channels */ |
| 475 | int ch = i % geo->nr_chnls; |
| 476 | int lun_raw = i / geo->nr_chnls; |
| 477 | int lunid = lun_raw + ch * geo->luns_per_chnl; |
| 478 | |
| 479 | rlun = &pblk->luns[i]; |
| 480 | rlun->bppa = luns[lunid]; |
| 481 | |
| 482 | sema_init(&rlun->wr_sem, 1); |
| 483 | |
| 484 | ret = pblk_bb_discovery(dev, rlun); |
| 485 | if (ret) { |
| 486 | while (--i >= 0) |
| 487 | kfree(pblk->luns[i].bb_list); |
| 488 | return ret; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int pblk_lines_configure(struct pblk *pblk, int flags) |
| 496 | { |
| 497 | struct pblk_line *line = NULL; |
| 498 | int ret = 0; |
| 499 | |
| 500 | if (!(flags & NVM_TARGET_FACTORY)) { |
| 501 | line = pblk_recov_l2p(pblk); |
| 502 | if (IS_ERR(line)) { |
| 503 | pr_err("pblk: could not recover l2p table\n"); |
| 504 | ret = -EFAULT; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (!line) { |
| 509 | /* Configure next line for user data */ |
| 510 | line = pblk_line_get_first_data(pblk); |
| 511 | if (!line) { |
| 512 | pr_err("pblk: line list corrupted\n"); |
| 513 | ret = -EFAULT; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | return ret; |
| 518 | } |
| 519 | |
| 520 | /* See comment over struct line_emeta definition */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 521 | static unsigned int calc_emeta_len(struct pblk *pblk) |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 522 | { |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 523 | struct pblk_line_meta *lm = &pblk->lm; |
| 524 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 525 | struct nvm_tgt_dev *dev = pblk->dev; |
| 526 | struct nvm_geo *geo = &dev->geo; |
| 527 | |
| 528 | /* Round to sector size so that lba_list starts on its own sector */ |
| 529 | lm->emeta_sec[1] = DIV_ROUND_UP( |
| 530 | sizeof(struct line_emeta) + lm->blk_bitmap_len, |
| 531 | geo->sec_size); |
| 532 | lm->emeta_len[1] = lm->emeta_sec[1] * geo->sec_size; |
| 533 | |
| 534 | /* Round to sector size so that vsc_list starts on its own sector */ |
| 535 | lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0]; |
| 536 | lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64), |
| 537 | geo->sec_size); |
| 538 | lm->emeta_len[2] = lm->emeta_sec[2] * geo->sec_size; |
| 539 | |
| 540 | lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32), |
| 541 | geo->sec_size); |
| 542 | lm->emeta_len[3] = lm->emeta_sec[3] * geo->sec_size; |
| 543 | |
| 544 | lm->vsc_list_len = l_mg->nr_lines * sizeof(u32); |
| 545 | |
| 546 | 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] | 547 | } |
| 548 | |
| 549 | static void pblk_set_provision(struct pblk *pblk, long nr_free_blks) |
| 550 | { |
| 551 | struct nvm_tgt_dev *dev = pblk->dev; |
| 552 | struct nvm_geo *geo = &dev->geo; |
| 553 | sector_t provisioned; |
| 554 | |
| 555 | pblk->over_pct = 20; |
| 556 | |
| 557 | provisioned = nr_free_blks; |
| 558 | provisioned *= (100 - pblk->over_pct); |
| 559 | sector_div(provisioned, 100); |
| 560 | |
| 561 | /* Internally pblk manages all free blocks, but all calculations based |
| 562 | * on user capacity consider only provisioned blocks |
| 563 | */ |
| 564 | pblk->rl.total_blocks = nr_free_blks; |
| 565 | pblk->rl.nr_secs = nr_free_blks * geo->sec_per_blk; |
| 566 | pblk->capacity = provisioned * geo->sec_per_blk; |
| 567 | atomic_set(&pblk->rl.free_blocks, nr_free_blks); |
| 568 | } |
| 569 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 570 | static int pblk_lines_alloc_metadata(struct pblk *pblk) |
| 571 | { |
| 572 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 573 | struct pblk_line_meta *lm = &pblk->lm; |
| 574 | int i; |
| 575 | |
| 576 | /* smeta is always small enough to fit on a kmalloc memory allocation, |
| 577 | * emeta depends on the number of LUNs allocated to the pblk instance |
| 578 | */ |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 579 | for (i = 0; i < PBLK_DATA_LINES; i++) { |
| 580 | l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL); |
| 581 | if (!l_mg->sline_meta[i]) |
| 582 | goto fail_free_smeta; |
| 583 | } |
| 584 | |
| 585 | /* emeta allocates three different buffers for managing metadata with |
| 586 | * in-memory and in-media layouts |
| 587 | */ |
| 588 | for (i = 0; i < PBLK_DATA_LINES; i++) { |
| 589 | struct pblk_emeta *emeta; |
| 590 | |
| 591 | emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL); |
| 592 | if (!emeta) |
| 593 | goto fail_free_emeta; |
| 594 | |
| 595 | if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) { |
| 596 | l_mg->emeta_alloc_type = PBLK_VMALLOC_META; |
| 597 | |
| 598 | emeta->buf = vmalloc(lm->emeta_len[0]); |
| 599 | if (!emeta->buf) { |
| 600 | kfree(emeta); |
| 601 | goto fail_free_emeta; |
| 602 | } |
| 603 | |
| 604 | emeta->nr_entries = lm->emeta_sec[0]; |
| 605 | l_mg->eline_meta[i] = emeta; |
| 606 | } else { |
| 607 | l_mg->emeta_alloc_type = PBLK_KMALLOC_META; |
| 608 | |
| 609 | emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL); |
| 610 | if (!emeta->buf) { |
| 611 | kfree(emeta); |
| 612 | goto fail_free_emeta; |
| 613 | } |
| 614 | |
| 615 | emeta->nr_entries = lm->emeta_sec[0]; |
| 616 | l_mg->eline_meta[i] = emeta; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL); |
| 621 | if (!l_mg->vsc_list) |
| 622 | goto fail_free_emeta; |
| 623 | |
| 624 | for (i = 0; i < l_mg->nr_lines; i++) |
| 625 | l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY); |
| 626 | |
| 627 | return 0; |
| 628 | |
| 629 | fail_free_emeta: |
| 630 | while (--i >= 0) { |
| 631 | vfree(l_mg->eline_meta[i]->buf); |
Javier González | f680f19 | 2017-06-26 11:57:21 +0200 | [diff] [blame] | 632 | kfree(l_mg->eline_meta[i]); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | fail_free_smeta: |
| 636 | for (i = 0; i < PBLK_DATA_LINES; i++) |
Javier González | f680f19 | 2017-06-26 11:57:21 +0200 | [diff] [blame] | 637 | kfree(l_mg->sline_meta[i]); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 638 | |
| 639 | return -ENOMEM; |
| 640 | } |
| 641 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 642 | static int pblk_lines_init(struct pblk *pblk) |
| 643 | { |
| 644 | struct nvm_tgt_dev *dev = pblk->dev; |
| 645 | struct nvm_geo *geo = &dev->geo; |
| 646 | struct pblk_line_mgmt *l_mg = &pblk->l_mg; |
| 647 | struct pblk_line_meta *lm = &pblk->lm; |
| 648 | struct pblk_line *line; |
| 649 | unsigned int smeta_len, emeta_len; |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 650 | long nr_bad_blks, nr_free_blks; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 651 | int bb_distance, max_write_ppas, mod; |
| 652 | int i, ret; |
| 653 | |
| 654 | pblk->min_write_pgs = geo->sec_per_pl * (geo->sec_size / PAGE_SIZE); |
| 655 | max_write_ppas = pblk->min_write_pgs * geo->nr_luns; |
| 656 | pblk->max_write_pgs = (max_write_ppas < nvm_max_phys_sects(dev)) ? |
| 657 | max_write_ppas : nvm_max_phys_sects(dev); |
| 658 | pblk_set_sec_per_write(pblk, pblk->min_write_pgs); |
| 659 | |
| 660 | if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) { |
| 661 | pr_err("pblk: cannot support device max_phys_sect\n"); |
| 662 | return -EINVAL; |
| 663 | } |
| 664 | |
| 665 | div_u64_rem(geo->sec_per_blk, pblk->min_write_pgs, &mod); |
| 666 | if (mod) { |
| 667 | pr_err("pblk: bad configuration of sectors/pages\n"); |
| 668 | return -EINVAL; |
| 669 | } |
| 670 | |
| 671 | l_mg->nr_lines = geo->blks_per_lun; |
| 672 | l_mg->log_line = l_mg->data_line = NULL; |
| 673 | l_mg->l_seq_nr = l_mg->d_seq_nr = 0; |
| 674 | l_mg->nr_free_lines = 0; |
| 675 | bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 676 | |
| 677 | lm->sec_per_line = geo->sec_per_blk * geo->nr_luns; |
| 678 | lm->blk_per_line = geo->nr_luns; |
| 679 | lm->blk_bitmap_len = BITS_TO_LONGS(geo->nr_luns) * sizeof(long); |
| 680 | lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long); |
| 681 | lm->lun_bitmap_len = BITS_TO_LONGS(geo->nr_luns) * sizeof(long); |
| 682 | lm->high_thrs = lm->sec_per_line / 2; |
| 683 | lm->mid_thrs = lm->sec_per_line / 4; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 684 | lm->meta_distance = (geo->nr_luns / 2) * pblk->min_write_pgs; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 685 | |
| 686 | /* Calculate necessary pages for smeta. See comment over struct |
| 687 | * line_smeta definition |
| 688 | */ |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 689 | i = 1; |
| 690 | add_smeta_page: |
| 691 | lm->smeta_sec = i * geo->sec_per_pl; |
| 692 | lm->smeta_len = lm->smeta_sec * geo->sec_size; |
| 693 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 694 | smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 695 | if (smeta_len > lm->smeta_len) { |
| 696 | i++; |
| 697 | goto add_smeta_page; |
| 698 | } |
| 699 | |
| 700 | /* Calculate necessary pages for emeta. See comment over struct |
| 701 | * line_emeta definition |
| 702 | */ |
| 703 | i = 1; |
| 704 | add_emeta_page: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 705 | lm->emeta_sec[0] = i * geo->sec_per_pl; |
| 706 | lm->emeta_len[0] = lm->emeta_sec[0] * geo->sec_size; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 707 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 708 | emeta_len = calc_emeta_len(pblk); |
| 709 | if (emeta_len > lm->emeta_len[0]) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 710 | i++; |
| 711 | goto add_emeta_page; |
| 712 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 713 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 714 | lm->emeta_bb = geo->nr_luns - i; |
| 715 | lm->min_blk_line = 1 + DIV_ROUND_UP(lm->smeta_sec + lm->emeta_sec[0], |
Javier González | d624f37 | 2017-06-26 11:57:15 +0200 | [diff] [blame] | 716 | geo->sec_per_blk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 717 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 718 | ret = pblk_lines_alloc_metadata(pblk); |
| 719 | if (ret) |
| 720 | goto fail; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 721 | |
| 722 | l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL); |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 723 | if (!l_mg->bb_template) { |
| 724 | ret = -ENOMEM; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 725 | goto fail_free_meta; |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 726 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 727 | |
| 728 | l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL); |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 729 | if (!l_mg->bb_aux) { |
| 730 | ret = -ENOMEM; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 731 | goto fail_free_bb_template; |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 732 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 733 | |
| 734 | bb_distance = (geo->nr_luns) * geo->sec_per_pl; |
| 735 | for (i = 0; i < lm->sec_per_line; i += bb_distance) |
| 736 | bitmap_set(l_mg->bb_template, i, geo->sec_per_pl); |
| 737 | |
| 738 | INIT_LIST_HEAD(&l_mg->free_list); |
| 739 | INIT_LIST_HEAD(&l_mg->corrupt_list); |
| 740 | INIT_LIST_HEAD(&l_mg->bad_list); |
| 741 | INIT_LIST_HEAD(&l_mg->gc_full_list); |
| 742 | INIT_LIST_HEAD(&l_mg->gc_high_list); |
| 743 | INIT_LIST_HEAD(&l_mg->gc_mid_list); |
| 744 | INIT_LIST_HEAD(&l_mg->gc_low_list); |
| 745 | INIT_LIST_HEAD(&l_mg->gc_empty_list); |
| 746 | |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 747 | INIT_LIST_HEAD(&l_mg->emeta_list); |
| 748 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 749 | l_mg->gc_lists[0] = &l_mg->gc_high_list; |
| 750 | l_mg->gc_lists[1] = &l_mg->gc_mid_list; |
| 751 | l_mg->gc_lists[2] = &l_mg->gc_low_list; |
| 752 | |
| 753 | spin_lock_init(&l_mg->free_lock); |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 754 | spin_lock_init(&l_mg->close_lock); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 755 | spin_lock_init(&l_mg->gc_lock); |
| 756 | |
| 757 | pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line), |
| 758 | GFP_KERNEL); |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 759 | if (!pblk->lines) { |
| 760 | ret = -ENOMEM; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 761 | goto fail_free_bb_aux; |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 762 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 763 | |
| 764 | nr_free_blks = 0; |
| 765 | for (i = 0; i < l_mg->nr_lines; i++) { |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 766 | int blk_in_line; |
| 767 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 768 | line = &pblk->lines[i]; |
| 769 | |
| 770 | line->pblk = pblk; |
| 771 | line->id = i; |
| 772 | line->type = PBLK_LINETYPE_FREE; |
| 773 | line->state = PBLK_LINESTATE_FREE; |
| 774 | line->gc_group = PBLK_LINEGC_NONE; |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 775 | line->vsc = &l_mg->vsc_list[i]; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 776 | spin_lock_init(&line->lock); |
| 777 | |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 778 | ret = pblk_alloc_line_bitmaps(pblk, line); |
| 779 | if (ret) |
| 780 | goto fail_free_lines; |
| 781 | |
| 782 | nr_bad_blks = pblk_bb_line(pblk, line, lm->blk_per_line); |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 783 | if (nr_bad_blks < 0 || nr_bad_blks > lm->blk_per_line) { |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 784 | pblk_free_line_bitmaps(line); |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 785 | ret = -EINVAL; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 786 | goto fail_free_lines; |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 787 | } |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 788 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 789 | blk_in_line = lm->blk_per_line - nr_bad_blks; |
| 790 | if (blk_in_line < lm->min_blk_line) { |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 791 | line->state = PBLK_LINESTATE_BAD; |
| 792 | list_add_tail(&line->list, &l_mg->bad_list); |
| 793 | continue; |
| 794 | } |
| 795 | |
Javier González | a44f53f | 2017-04-22 01:32:49 +0200 | [diff] [blame] | 796 | nr_free_blks += blk_in_line; |
| 797 | atomic_set(&line->blk_in_line, blk_in_line); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 798 | |
| 799 | l_mg->nr_free_lines++; |
| 800 | list_add_tail(&line->list, &l_mg->free_list); |
| 801 | } |
| 802 | |
| 803 | pblk_set_provision(pblk, nr_free_blks); |
| 804 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 805 | /* Cleanup per-LUN bad block lists - managed within lines on run-time */ |
| 806 | for (i = 0; i < geo->nr_luns; i++) |
| 807 | kfree(pblk->luns[i].bb_list); |
| 808 | |
| 809 | return 0; |
| 810 | fail_free_lines: |
Javier González | dffdd96 | 2017-06-26 11:57:22 +0200 | [diff] [blame] | 811 | while (--i >= 0) |
| 812 | pblk_free_line_bitmaps(&pblk->lines[i]); |
| 813 | |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 814 | kfree(pblk->lines); |
| 815 | fail_free_bb_aux: |
| 816 | kfree(l_mg->bb_aux); |
| 817 | fail_free_bb_template: |
| 818 | kfree(l_mg->bb_template); |
| 819 | fail_free_meta: |
Javier González | dd2a434 | 2017-06-26 11:57:17 +0200 | [diff] [blame] | 820 | pblk_line_meta_free(pblk); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 821 | fail: |
| 822 | for (i = 0; i < geo->nr_luns; i++) |
| 823 | kfree(pblk->luns[i].bb_list); |
| 824 | |
| 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | static int pblk_writer_init(struct pblk *pblk) |
| 829 | { |
| 830 | setup_timer(&pblk->wtimer, pblk_write_timer_fn, (unsigned long)pblk); |
| 831 | mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100)); |
| 832 | |
| 833 | pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t"); |
| 834 | if (IS_ERR(pblk->writer_ts)) { |
| 835 | pr_err("pblk: could not allocate writer kthread\n"); |
Dan Carpenter | 1c6286f | 2017-04-15 20:55:53 +0200 | [diff] [blame] | 836 | return PTR_ERR(pblk->writer_ts); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static void pblk_writer_stop(struct pblk *pblk) |
| 843 | { |
| 844 | if (pblk->writer_ts) |
| 845 | kthread_stop(pblk->writer_ts); |
| 846 | del_timer(&pblk->wtimer); |
| 847 | } |
| 848 | |
| 849 | static void pblk_free(struct pblk *pblk) |
| 850 | { |
| 851 | pblk_luns_free(pblk); |
| 852 | pblk_lines_free(pblk); |
| 853 | pblk_line_meta_free(pblk); |
| 854 | pblk_core_free(pblk); |
| 855 | pblk_l2p_free(pblk); |
| 856 | |
| 857 | kfree(pblk); |
| 858 | } |
| 859 | |
| 860 | static void pblk_tear_down(struct pblk *pblk) |
| 861 | { |
| 862 | pblk_flush_writer(pblk); |
| 863 | pblk_writer_stop(pblk); |
| 864 | pblk_rb_sync_l2p(&pblk->rwb); |
| 865 | pblk_recov_pad(pblk); |
| 866 | pblk_rwb_free(pblk); |
| 867 | pblk_rl_free(&pblk->rl); |
| 868 | |
| 869 | pr_debug("pblk: consistent tear down\n"); |
| 870 | } |
| 871 | |
| 872 | static void pblk_exit(void *private) |
| 873 | { |
| 874 | struct pblk *pblk = private; |
| 875 | |
| 876 | down_write(&pblk_lock); |
| 877 | pblk_gc_exit(pblk); |
| 878 | pblk_tear_down(pblk); |
| 879 | pblk_free(pblk); |
| 880 | up_write(&pblk_lock); |
| 881 | } |
| 882 | |
| 883 | static sector_t pblk_capacity(void *private) |
| 884 | { |
| 885 | struct pblk *pblk = private; |
| 886 | |
| 887 | return pblk->capacity * NR_PHY_IN_LOG; |
| 888 | } |
| 889 | |
| 890 | static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk, |
| 891 | int flags) |
| 892 | { |
| 893 | struct nvm_geo *geo = &dev->geo; |
| 894 | struct request_queue *bqueue = dev->q; |
| 895 | struct request_queue *tqueue = tdisk->queue; |
| 896 | struct pblk *pblk; |
| 897 | int ret; |
| 898 | |
| 899 | if (dev->identity.dom & NVM_RSP_L2P) { |
| 900 | pr_err("pblk: device-side L2P table not supported. (%x)\n", |
| 901 | dev->identity.dom); |
| 902 | return ERR_PTR(-EINVAL); |
| 903 | } |
| 904 | |
| 905 | pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL); |
| 906 | if (!pblk) |
| 907 | return ERR_PTR(-ENOMEM); |
| 908 | |
| 909 | pblk->dev = dev; |
| 910 | pblk->disk = tdisk; |
| 911 | |
| 912 | spin_lock_init(&pblk->trans_lock); |
| 913 | spin_lock_init(&pblk->lock); |
| 914 | |
| 915 | if (flags & NVM_TARGET_FACTORY) |
| 916 | pblk_setup_uuid(pblk); |
| 917 | |
| 918 | #ifdef CONFIG_NVM_DEBUG |
| 919 | atomic_long_set(&pblk->inflight_writes, 0); |
| 920 | atomic_long_set(&pblk->padded_writes, 0); |
| 921 | atomic_long_set(&pblk->padded_wb, 0); |
| 922 | atomic_long_set(&pblk->nr_flush, 0); |
| 923 | atomic_long_set(&pblk->req_writes, 0); |
| 924 | atomic_long_set(&pblk->sub_writes, 0); |
| 925 | atomic_long_set(&pblk->sync_writes, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 926 | atomic_long_set(&pblk->inflight_reads, 0); |
Javier González | db7ada3 | 2017-06-26 11:57:13 +0200 | [diff] [blame] | 927 | atomic_long_set(&pblk->cache_reads, 0); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 928 | atomic_long_set(&pblk->sync_reads, 0); |
| 929 | atomic_long_set(&pblk->recov_writes, 0); |
| 930 | atomic_long_set(&pblk->recov_writes, 0); |
| 931 | atomic_long_set(&pblk->recov_gc_writes, 0); |
| 932 | #endif |
| 933 | |
| 934 | atomic_long_set(&pblk->read_failed, 0); |
| 935 | atomic_long_set(&pblk->read_empty, 0); |
| 936 | atomic_long_set(&pblk->read_high_ecc, 0); |
| 937 | atomic_long_set(&pblk->read_failed_gc, 0); |
| 938 | atomic_long_set(&pblk->write_failed, 0); |
| 939 | atomic_long_set(&pblk->erase_failed, 0); |
| 940 | |
| 941 | ret = pblk_luns_init(pblk, dev->luns); |
| 942 | if (ret) { |
| 943 | pr_err("pblk: could not initialize luns\n"); |
| 944 | goto fail; |
| 945 | } |
| 946 | |
| 947 | ret = pblk_lines_init(pblk); |
| 948 | if (ret) { |
| 949 | pr_err("pblk: could not initialize lines\n"); |
| 950 | goto fail_free_luns; |
| 951 | } |
| 952 | |
| 953 | ret = pblk_core_init(pblk); |
| 954 | if (ret) { |
| 955 | pr_err("pblk: could not initialize core\n"); |
| 956 | goto fail_free_line_meta; |
| 957 | } |
| 958 | |
| 959 | ret = pblk_l2p_init(pblk); |
| 960 | if (ret) { |
| 961 | pr_err("pblk: could not initialize maps\n"); |
| 962 | goto fail_free_core; |
| 963 | } |
| 964 | |
| 965 | ret = pblk_lines_configure(pblk, flags); |
| 966 | if (ret) { |
| 967 | pr_err("pblk: could not configure lines\n"); |
| 968 | goto fail_free_l2p; |
| 969 | } |
| 970 | |
| 971 | ret = pblk_writer_init(pblk); |
| 972 | if (ret) { |
| 973 | pr_err("pblk: could not initialize write thread\n"); |
| 974 | goto fail_free_lines; |
| 975 | } |
| 976 | |
| 977 | ret = pblk_gc_init(pblk); |
| 978 | if (ret) { |
| 979 | pr_err("pblk: could not initialize gc\n"); |
| 980 | goto fail_stop_writer; |
| 981 | } |
| 982 | |
| 983 | /* inherit the size from the underlying device */ |
| 984 | blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue)); |
| 985 | blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue)); |
| 986 | |
| 987 | blk_queue_write_cache(tqueue, true, false); |
| 988 | |
| 989 | tqueue->limits.discard_granularity = geo->pgs_per_blk * geo->pfpg_size; |
| 990 | tqueue->limits.discard_alignment = 0; |
| 991 | blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9); |
| 992 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, tqueue); |
| 993 | |
| 994 | pr_info("pblk init: luns:%u, lines:%d, secs:%llu, buf entries:%u\n", |
| 995 | geo->nr_luns, pblk->l_mg.nr_lines, |
| 996 | (unsigned long long)pblk->rl.nr_secs, |
| 997 | pblk->rwb.nr_entries); |
| 998 | |
| 999 | wake_up_process(pblk->writer_ts); |
| 1000 | return pblk; |
| 1001 | |
| 1002 | fail_stop_writer: |
| 1003 | pblk_writer_stop(pblk); |
| 1004 | fail_free_lines: |
| 1005 | pblk_lines_free(pblk); |
| 1006 | fail_free_l2p: |
| 1007 | pblk_l2p_free(pblk); |
| 1008 | fail_free_core: |
| 1009 | pblk_core_free(pblk); |
| 1010 | fail_free_line_meta: |
| 1011 | pblk_line_meta_free(pblk); |
| 1012 | fail_free_luns: |
| 1013 | pblk_luns_free(pblk); |
| 1014 | fail: |
| 1015 | kfree(pblk); |
| 1016 | return ERR_PTR(ret); |
| 1017 | } |
| 1018 | |
| 1019 | /* physical block device target */ |
| 1020 | static struct nvm_tgt_type tt_pblk = { |
| 1021 | .name = "pblk", |
| 1022 | .version = {1, 0, 0}, |
| 1023 | |
| 1024 | .make_rq = pblk_make_rq, |
| 1025 | .capacity = pblk_capacity, |
| 1026 | |
| 1027 | .init = pblk_init, |
| 1028 | .exit = pblk_exit, |
| 1029 | |
| 1030 | .sysfs_init = pblk_sysfs_init, |
| 1031 | .sysfs_exit = pblk_sysfs_exit, |
| 1032 | }; |
| 1033 | |
| 1034 | static int __init pblk_module_init(void) |
| 1035 | { |
NeilBrown | b25d523 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 1036 | int ret; |
| 1037 | |
| 1038 | pblk_bio_set = bioset_create(BIO_POOL_SIZE, 0, 0); |
| 1039 | if (!pblk_bio_set) |
| 1040 | return -ENOMEM; |
| 1041 | ret = nvm_register_tgt_type(&tt_pblk); |
| 1042 | if (ret) |
| 1043 | bioset_free(pblk_bio_set); |
| 1044 | return ret; |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | static void pblk_module_exit(void) |
| 1048 | { |
NeilBrown | b25d523 | 2017-06-18 14:38:58 +1000 | [diff] [blame] | 1049 | bioset_free(pblk_bio_set); |
Javier González | a4bd217 | 2017-04-15 20:55:50 +0200 | [diff] [blame] | 1050 | nvm_unregister_tgt_type(&tt_pblk); |
| 1051 | } |
| 1052 | |
| 1053 | module_init(pblk_module_init); |
| 1054 | module_exit(pblk_module_exit); |
| 1055 | MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>"); |
| 1056 | MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>"); |
| 1057 | MODULE_LICENSE("GPL v2"); |
| 1058 | MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs"); |