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