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