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