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