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