blob: 5b381700ef30ff863795924cec47fd88ad33b1c1 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2015 IT University of Copenhagen (rrpc.c)
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Matias Bjorling <matias@cnexlabs.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * Implementation of a physical block-device target for Open-channel SSDs.
17 *
18 * pblk-init.c - pblk's initialization.
19 */
20
21#include "pblk.h"
22
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020023static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache,
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +020024 *pblk_w_rq_cache;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020025static DECLARE_RWSEM(pblk_lock);
NeilBrownb25d5232017-06-18 14:38:58 +100026struct bio_set *pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020027
28static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
29 struct bio *bio)
30{
31 int ret;
32
33 /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
34 * constraint. Writes can be of arbitrary size.
35 */
36 if (bio_data_dir(bio) == READ) {
NeilBrownaf67c312017-06-18 14:38:57 +100037 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020038 ret = pblk_submit_read(pblk, bio);
39 if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
40 bio_put(bio);
41
42 return ret;
43 }
44
45 /* Prevent deadlock in the case of a modest LUN configuration and large
46 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
47 * available for user I/O.
48 */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +020049 if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
NeilBrownaf67c312017-06-18 14:38:57 +100050 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020051
52 return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
53}
54
55static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
56{
57 struct pblk *pblk = q->queuedata;
58
59 if (bio_op(bio) == REQ_OP_DISCARD) {
60 pblk_discard(pblk, bio);
61 if (!(bio->bi_opf & REQ_PREFLUSH)) {
62 bio_endio(bio);
63 return BLK_QC_T_NONE;
64 }
65 }
66
67 switch (pblk_rw_io(q, pblk, bio)) {
68 case NVM_IO_ERR:
69 bio_io_error(bio);
70 break;
71 case NVM_IO_DONE:
72 bio_endio(bio);
73 break;
74 }
75
76 return BLK_QC_T_NONE;
77}
78
Hans Holmbergc5586192017-10-13 14:46:40 +020079static size_t pblk_trans_map_size(struct pblk *pblk)
80{
81 int entry_size = 8;
82
Javier Gonzálezbb845ae42018-03-30 00:05:19 +020083 if (pblk->addrf_len < 32)
Hans Holmbergc5586192017-10-13 14:46:40 +020084 entry_size = 4;
85
86 return entry_size * pblk->rl.nr_secs;
87}
88
89#ifdef CONFIG_NVM_DEBUG
90static u32 pblk_l2p_crc(struct pblk *pblk)
91{
92 size_t map_size;
93 u32 crc = ~(u32)0;
94
95 map_size = pblk_trans_map_size(pblk);
96 crc = crc32_le(crc, pblk->trans_map, map_size);
97 return crc;
98}
99#endif
100
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200101static void pblk_l2p_free(struct pblk *pblk)
102{
103 vfree(pblk->trans_map);
104}
105
Javier González43d47122018-03-30 00:05:09 +0200106static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
107{
108 struct pblk_line *line = NULL;
109
110 if (factory_init) {
111 pblk_setup_uuid(pblk);
112 } else {
113 line = pblk_recov_l2p(pblk);
114 if (IS_ERR(line)) {
115 pr_err("pblk: could not recover l2p table\n");
116 return -EFAULT;
117 }
118 }
119
120#ifdef CONFIG_NVM_DEBUG
121 pr_info("pblk init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
122#endif
123
124 /* Free full lines directly as GC has not been started yet */
125 pblk_gc_free_full_lines(pblk);
126
127 if (!line) {
128 /* Configure next line for user data */
129 line = pblk_line_get_first_data(pblk);
130 if (!line) {
131 pr_err("pblk: line list corrupted\n");
132 return -EFAULT;
133 }
134 }
135
136 return 0;
137}
138
139static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200140{
141 sector_t i;
142 struct ppa_addr ppa;
Hans Holmbergc5586192017-10-13 14:46:40 +0200143 size_t map_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200144
Hans Holmbergc5586192017-10-13 14:46:40 +0200145 map_size = pblk_trans_map_size(pblk);
146 pblk->trans_map = vmalloc(map_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200147 if (!pblk->trans_map)
148 return -ENOMEM;
149
150 pblk_ppa_set_empty(&ppa);
151
152 for (i = 0; i < pblk->rl.nr_secs; i++)
153 pblk_trans_map_set(pblk, i, ppa);
154
Javier González43d47122018-03-30 00:05:09 +0200155 return pblk_l2p_recover(pblk, factory_init);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200156}
157
158static void pblk_rwb_free(struct pblk *pblk)
159{
160 if (pblk_rb_tear_down_check(&pblk->rwb))
161 pr_err("pblk: write buffer error on tear down\n");
162
163 pblk_rb_data_free(&pblk->rwb);
164 vfree(pblk_rb_entries_ref(&pblk->rwb));
165}
166
167static int pblk_rwb_init(struct pblk *pblk)
168{
169 struct nvm_tgt_dev *dev = pblk->dev;
170 struct nvm_geo *geo = &dev->geo;
171 struct pblk_rb_entry *entries;
172 unsigned long nr_entries;
173 unsigned int power_size, power_seg_sz;
174
175 nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer);
176
177 entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
178 if (!entries)
179 return -ENOMEM;
180
181 power_size = get_count_order(nr_entries);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200182 power_seg_sz = get_count_order(geo->csecs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200183
184 return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
185}
186
187/* Minimum pages needed within a lun */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200188#define ADDR_POOL_SIZE 64
189
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200190static int pblk_set_addrf_12(struct nvm_geo *geo, struct nvm_addrf_12 *dst)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200191{
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200192 struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
193 int power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200194
195 /* Re-calculate channel and lun format to adapt to configuration */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200196 power_len = get_count_order(geo->num_ch);
197 if (1 << power_len != geo->num_ch) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200198 pr_err("pblk: supports only power-of-two channel config.\n");
199 return -EINVAL;
200 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200201 dst->ch_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200202
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200203 power_len = get_count_order(geo->num_lun);
204 if (1 << power_len != geo->num_lun) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200205 pr_err("pblk: supports only power-of-two LUN config.\n");
206 return -EINVAL;
207 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200208 dst->lun_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200209
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200210 dst->blk_len = src->blk_len;
211 dst->pg_len = src->pg_len;
212 dst->pln_len = src->pln_len;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200213 dst->sec_len = src->sec_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200214
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200215 dst->sec_offset = 0;
216 dst->pln_offset = dst->sec_len;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200217 dst->ch_offset = dst->pln_offset + dst->pln_len;
218 dst->lun_offset = dst->ch_offset + dst->ch_len;
219 dst->pg_offset = dst->lun_offset + dst->lun_len;
220 dst->blk_offset = dst->pg_offset + dst->pg_len;
221
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200222 dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200223 dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
224 dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
225 dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
226 dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
227 dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
228
229 return dst->blk_offset + src->blk_len;
230}
231
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200232static int pblk_set_addrf(struct pblk *pblk)
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200233{
234 struct nvm_tgt_dev *dev = pblk->dev;
235 struct nvm_geo *geo = &dev->geo;
236 int mod;
237
238 div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
239 if (mod) {
240 pr_err("pblk: bad configuration of sectors/pages\n");
241 return -EINVAL;
242 }
243
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200244 pblk->addrf_len = pblk_set_addrf_12(geo, (void *)&pblk->addrf);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200245
246 return 0;
247}
248
249static int pblk_init_global_caches(struct pblk *pblk)
250{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200251 down_write(&pblk_lock);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200252 pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200253 sizeof(struct pblk_line_ws), 0, 0, NULL);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200254 if (!pblk_ws_cache) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200255 up_write(&pblk_lock);
256 return -ENOMEM;
257 }
258
259 pblk_rec_cache = kmem_cache_create("pblk_rec",
260 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
261 if (!pblk_rec_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200262 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200263 up_write(&pblk_lock);
264 return -ENOMEM;
265 }
266
Javier González084ec9b2017-06-26 16:27:13 -0600267 pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200268 0, 0, NULL);
Javier González084ec9b2017-06-26 16:27:13 -0600269 if (!pblk_g_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200270 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200271 kmem_cache_destroy(pblk_rec_cache);
272 up_write(&pblk_lock);
273 return -ENOMEM;
274 }
275
276 pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
277 0, 0, NULL);
278 if (!pblk_w_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200279 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200280 kmem_cache_destroy(pblk_rec_cache);
Javier González084ec9b2017-06-26 16:27:13 -0600281 kmem_cache_destroy(pblk_g_rq_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200282 up_write(&pblk_lock);
283 return -ENOMEM;
284 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200285 up_write(&pblk_lock);
286
287 return 0;
288}
289
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200290static void pblk_free_global_caches(struct pblk *pblk)
291{
292 kmem_cache_destroy(pblk_ws_cache);
293 kmem_cache_destroy(pblk_rec_cache);
294 kmem_cache_destroy(pblk_g_rq_cache);
295 kmem_cache_destroy(pblk_w_rq_cache);
296}
297
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298static int pblk_core_init(struct pblk *pblk)
299{
300 struct nvm_tgt_dev *dev = pblk->dev;
301 struct nvm_geo *geo = &dev->geo;
Javier González43d47122018-03-30 00:05:09 +0200302 int max_write_ppas;
303
304 atomic64_set(&pblk->user_wa, 0);
305 atomic64_set(&pblk->pad_wa, 0);
306 atomic64_set(&pblk->gc_wa, 0);
307 pblk->user_rst_wa = 0;
308 pblk->pad_rst_wa = 0;
309 pblk->gc_rst_wa = 0;
310
311 atomic64_set(&pblk->nr_flush, 0);
312 pblk->nr_flush_rst = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200313
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200314 pblk->pgs_in_buffer = geo->mw_cunits * geo->all_luns;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200315
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200316 pblk->min_write_pgs = geo->ws_opt * (geo->csecs / PAGE_SIZE);
Javier González43d47122018-03-30 00:05:09 +0200317 max_write_ppas = pblk->min_write_pgs * geo->all_luns;
318 pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
319 pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
320
321 if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
322 pr_err("pblk: vector list too big(%u > %u)\n",
323 pblk->max_write_pgs, PBLK_MAX_REQ_ADDRS);
324 return -EINVAL;
325 }
326
327 pblk->pad_dist = kzalloc((pblk->min_write_pgs - 1) * sizeof(atomic64_t),
328 GFP_KERNEL);
329 if (!pblk->pad_dist)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200330 return -ENOMEM;
331
Javier González43d47122018-03-30 00:05:09 +0200332 if (pblk_init_global_caches(pblk))
333 goto fail_free_pad_dist;
334
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200335 /* Internal bios can be at most the sectors signaled by the device. */
Matias Bjørling89a09c52018-03-30 00:05:04 +0200336 pblk->page_bio_pool = mempool_create_page_pool(NVM_MAX_VLBA, 0);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200337 if (!pblk->page_bio_pool)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200338 goto free_global_caches;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200339
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200340 pblk->gen_ws_pool = mempool_create_slab_pool(PBLK_GEN_WS_POOL_SIZE,
341 pblk_ws_cache);
342 if (!pblk->gen_ws_pool)
Javier Gonzálezbd432412017-10-13 14:46:06 +0200343 goto free_page_bio_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200344
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100345 pblk->rec_pool = mempool_create_slab_pool(geo->all_luns,
346 pblk_rec_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200347 if (!pblk->rec_pool)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200348 goto free_gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200349
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100350 pblk->r_rq_pool = mempool_create_slab_pool(geo->all_luns,
Javier Gonzálezef576492017-06-26 11:57:28 +0200351 pblk_g_rq_cache);
Javier González0d880392017-10-13 14:46:08 +0200352 if (!pblk->r_rq_pool)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200353 goto free_rec_pool;
354
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100355 pblk->e_rq_pool = mempool_create_slab_pool(geo->all_luns,
Javier González0d880392017-10-13 14:46:08 +0200356 pblk_g_rq_cache);
357 if (!pblk->e_rq_pool)
358 goto free_r_rq_pool;
359
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100360 pblk->w_rq_pool = mempool_create_slab_pool(geo->all_luns,
Javier Gonzálezef576492017-06-26 11:57:28 +0200361 pblk_w_rq_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200362 if (!pblk->w_rq_pool)
Javier González0d880392017-10-13 14:46:08 +0200363 goto free_e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200364
Javier Gonzálezef576492017-06-26 11:57:28 +0200365 pblk->close_wq = alloc_workqueue("pblk-close-wq",
366 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
367 if (!pblk->close_wq)
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200368 goto free_w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200369
Javier Gonzálezef576492017-06-26 11:57:28 +0200370 pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
371 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
372 if (!pblk->bb_wq)
373 goto free_close_wq;
374
Javier González7bd4d372017-10-13 14:46:23 +0200375 pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
376 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
377 if (!pblk->r_end_wq)
Javier Gonzálezef576492017-06-26 11:57:28 +0200378 goto free_bb_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200379
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200380 if (pblk_set_addrf(pblk))
Javier González7bd4d372017-10-13 14:46:23 +0200381 goto free_r_end_wq;
382
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200383 INIT_LIST_HEAD(&pblk->compl_list);
Javier González43d47122018-03-30 00:05:09 +0200384
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200385 return 0;
386
Javier González7bd4d372017-10-13 14:46:23 +0200387free_r_end_wq:
388 destroy_workqueue(pblk->r_end_wq);
Javier Gonzálezef576492017-06-26 11:57:28 +0200389free_bb_wq:
390 destroy_workqueue(pblk->bb_wq);
391free_close_wq:
392 destroy_workqueue(pblk->close_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200393free_w_rq_pool:
394 mempool_destroy(pblk->w_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200395free_e_rq_pool:
396 mempool_destroy(pblk->e_rq_pool);
397free_r_rq_pool:
398 mempool_destroy(pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200399free_rec_pool:
400 mempool_destroy(pblk->rec_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200401free_gen_ws_pool:
402 mempool_destroy(pblk->gen_ws_pool);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200403free_page_bio_pool:
404 mempool_destroy(pblk->page_bio_pool);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200405free_global_caches:
406 pblk_free_global_caches(pblk);
Javier González43d47122018-03-30 00:05:09 +0200407fail_free_pad_dist:
408 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200409 return -ENOMEM;
410}
411
412static void pblk_core_free(struct pblk *pblk)
413{
Javier Gonzálezef576492017-06-26 11:57:28 +0200414 if (pblk->close_wq)
415 destroy_workqueue(pblk->close_wq);
416
Javier González7bd4d372017-10-13 14:46:23 +0200417 if (pblk->r_end_wq)
418 destroy_workqueue(pblk->r_end_wq);
419
Javier Gonzálezef576492017-06-26 11:57:28 +0200420 if (pblk->bb_wq)
421 destroy_workqueue(pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200422
Javier Gonzálezbd432412017-10-13 14:46:06 +0200423 mempool_destroy(pblk->page_bio_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200424 mempool_destroy(pblk->gen_ws_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200425 mempool_destroy(pblk->rec_pool);
Javier González0d880392017-10-13 14:46:08 +0200426 mempool_destroy(pblk->r_rq_pool);
427 mempool_destroy(pblk->e_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200428 mempool_destroy(pblk->w_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200429
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200430 pblk_free_global_caches(pblk);
Javier González43d47122018-03-30 00:05:09 +0200431 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200432}
433
Javier Gonzáleze411b332018-03-30 00:04:59 +0200434static void pblk_line_mg_free(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200435{
436 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
437 int i;
438
439 kfree(l_mg->bb_template);
440 kfree(l_mg->bb_aux);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200441 kfree(l_mg->vsc_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200442
443 for (i = 0; i < PBLK_DATA_LINES; i++) {
Javier Gonzálezf680f192017-06-26 11:57:21 +0200444 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200445 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200446 kfree(l_mg->eline_meta[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200447 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200448}
449
Javier Gonzáleze411b332018-03-30 00:04:59 +0200450static void pblk_line_meta_free(struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200451{
Javier Gonzáleze411b332018-03-30 00:04:59 +0200452 kfree(line->blk_bitmap);
453 kfree(line->erase_bitmap);
454}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200455
Javier Gonzáleze411b332018-03-30 00:04:59 +0200456static void pblk_lines_free(struct pblk *pblk)
457{
458 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
459 struct pblk_line *line;
460 int i;
461
462 spin_lock(&l_mg->free_lock);
463 for (i = 0; i < l_mg->nr_lines; i++) {
464 line = &pblk->lines[i];
465
466 pblk_line_free(pblk, line);
467 pblk_line_meta_free(line);
468 }
469 spin_unlock(&l_mg->free_lock);
Javier González43d47122018-03-30 00:05:09 +0200470
471 pblk_line_mg_free(pblk);
472
473 kfree(pblk->luns);
474 kfree(pblk->lines);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200475}
476
477static int pblk_bb_get_tbl(struct nvm_tgt_dev *dev, struct pblk_lun *rlun,
478 u8 *blks, int nr_blks)
479{
480 struct ppa_addr ppa;
481 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200482
483 ppa.ppa = 0;
484 ppa.g.ch = rlun->bppa.g.ch;
485 ppa.g.lun = rlun->bppa.g.lun;
486
487 ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
488 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200489 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200490
491 nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200492 if (nr_blks < 0)
493 return -EIO;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494
Wei Yongjun5136a4f2017-04-25 16:15:30 +0000495 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200496}
497
Javier Gonzáleze411b332018-03-30 00:04:59 +0200498static void *pblk_bb_get_log(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200499{
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200500 struct nvm_tgt_dev *dev = pblk->dev;
501 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200502 u8 *log;
503 int i, nr_blks, blk_per_lun;
504 int ret;
505
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200506 blk_per_lun = geo->num_chk * geo->pln_mode;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200507 nr_blks = blk_per_lun * geo->all_luns;
508
509 log = kmalloc(nr_blks, GFP_KERNEL);
510 if (!log)
511 return ERR_PTR(-ENOMEM);
512
513 for (i = 0; i < geo->all_luns; i++) {
514 struct pblk_lun *rlun = &pblk->luns[i];
515 u8 *log_pos = log + i * blk_per_lun;
516
517 ret = pblk_bb_get_tbl(dev, rlun, log_pos, blk_per_lun);
518 if (ret) {
519 kfree(log);
520 return ERR_PTR(-EIO);
521 }
522 }
523
524 return log;
525}
526
527static int pblk_bb_line(struct pblk *pblk, struct pblk_line *line,
528 u8 *bb_log, int blk_per_line)
529{
530 struct nvm_tgt_dev *dev = pblk->dev;
531 struct nvm_geo *geo = &dev->geo;
532 int i, bb_cnt = 0;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200533 int blk_per_lun = geo->num_chk * geo->pln_mode;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200534
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200535 for (i = 0; i < blk_per_line; i++) {
Javier Gonzáleze411b332018-03-30 00:04:59 +0200536 struct pblk_lun *rlun = &pblk->luns[i];
Heiner Litza38c78d2018-03-30 00:05:06 +0200537 u8 *lun_bb_log = bb_log + i * blk_per_lun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200538
539 if (lun_bb_log[line->id] == NVM_BLK_T_FREE)
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200540 continue;
541
542 set_bit(pblk_ppa_to_pos(geo, rlun->bppa), line->blk_bitmap);
543 bb_cnt++;
544 }
545
546 return bb_cnt;
547}
548
Javier González43d47122018-03-30 00:05:09 +0200549static int pblk_luns_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200550{
551 struct nvm_tgt_dev *dev = pblk->dev;
552 struct nvm_geo *geo = &dev->geo;
553 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200554 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200555
556 /* TODO: Implement unbalanced LUN support */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200557 if (geo->num_lun < 0) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200558 pr_err("pblk: unbalanced LUN config.\n");
559 return -EINVAL;
560 }
561
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100562 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
563 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200564 if (!pblk->luns)
565 return -ENOMEM;
566
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100567 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200568 /* Stripe across channels */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200569 int ch = i % geo->num_ch;
570 int lun_raw = i / geo->num_ch;
571 int lunid = lun_raw + ch * geo->num_lun;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200572
573 rlun = &pblk->luns[i];
Javier González43d47122018-03-30 00:05:09 +0200574 rlun->bppa = dev->luns[lunid];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200575
576 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200577 }
578
579 return 0;
580}
581
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200583static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200584{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200585 struct pblk_line_meta *lm = &pblk->lm;
586 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
587 struct nvm_tgt_dev *dev = pblk->dev;
588 struct nvm_geo *geo = &dev->geo;
589
590 /* Round to sector size so that lba_list starts on its own sector */
591 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200592 sizeof(struct line_emeta) + lm->blk_bitmap_len +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200593 sizeof(struct wa_counters), geo->csecs);
594 lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200595
596 /* Round to sector size so that vsc_list starts on its own sector */
597 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
598 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200599 geo->csecs);
600 lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200601
602 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200603 geo->csecs);
604 lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200605
606 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
607
608 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200609}
610
611static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
612{
613 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100614 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
615 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200616 struct nvm_geo *geo = &dev->geo;
617 sector_t provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100618 int sec_meta, blk_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200619
Javier Gonzáleze5392732018-01-05 14:16:14 +0100620 if (geo->op == NVM_TARGET_DEFAULT_OP)
621 pblk->op = PBLK_DEFAULT_OP;
622 else
623 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200624
625 provisioned = nr_free_blks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100626 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200627 sector_div(provisioned, 100);
628
Javier Gonzáleza7689932018-01-05 14:16:13 +0100629 pblk->op_blks = nr_free_blks - provisioned;
630
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200631 /* Internally pblk manages all free blocks, but all calculations based
632 * on user capacity consider only provisioned blocks
633 */
634 pblk->rl.total_blocks = nr_free_blks;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200635 pblk->rl.nr_secs = nr_free_blks * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100636
637 /* Consider sectors used for metadata */
638 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200639 blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100640
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200641 pblk->capacity = (provisioned - blk_meta) * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100642
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200643 atomic_set(&pblk->rl.free_blocks, nr_free_blks);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100644 atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200645}
646
Javier González43d47122018-03-30 00:05:09 +0200647static int pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
648 void *chunk_log, long *nr_bad_blks)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200649{
Javier González43d47122018-03-30 00:05:09 +0200650 struct pblk_line_meta *lm = &pblk->lm;
651
652 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
653 if (!line->blk_bitmap)
654 return -ENOMEM;
655
656 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
657 if (!line->erase_bitmap) {
658 kfree(line->blk_bitmap);
659 return -ENOMEM;
660 }
661
662 *nr_bad_blks = pblk_bb_line(pblk, line, chunk_log, lm->blk_per_line);
663
664 return 0;
665}
666
667static int pblk_line_mg_init(struct pblk *pblk)
668{
669 struct nvm_tgt_dev *dev = pblk->dev;
670 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200671 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
672 struct pblk_line_meta *lm = &pblk->lm;
Javier González43d47122018-03-30 00:05:09 +0200673 int i, bb_distance;
674
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200675 l_mg->nr_lines = geo->num_chk;
Javier González43d47122018-03-30 00:05:09 +0200676 l_mg->log_line = l_mg->data_line = NULL;
677 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
678 l_mg->nr_free_lines = 0;
679 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
680
681 INIT_LIST_HEAD(&l_mg->free_list);
682 INIT_LIST_HEAD(&l_mg->corrupt_list);
683 INIT_LIST_HEAD(&l_mg->bad_list);
684 INIT_LIST_HEAD(&l_mg->gc_full_list);
685 INIT_LIST_HEAD(&l_mg->gc_high_list);
686 INIT_LIST_HEAD(&l_mg->gc_mid_list);
687 INIT_LIST_HEAD(&l_mg->gc_low_list);
688 INIT_LIST_HEAD(&l_mg->gc_empty_list);
689
690 INIT_LIST_HEAD(&l_mg->emeta_list);
691
692 l_mg->gc_lists[0] = &l_mg->gc_high_list;
693 l_mg->gc_lists[1] = &l_mg->gc_mid_list;
694 l_mg->gc_lists[2] = &l_mg->gc_low_list;
695
696 spin_lock_init(&l_mg->free_lock);
697 spin_lock_init(&l_mg->close_lock);
698 spin_lock_init(&l_mg->gc_lock);
699
700 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
701 if (!l_mg->vsc_list)
702 goto fail;
703
704 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
705 if (!l_mg->bb_template)
706 goto fail_free_vsc_list;
707
708 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
709 if (!l_mg->bb_aux)
710 goto fail_free_bb_template;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200711
712 /* smeta is always small enough to fit on a kmalloc memory allocation,
713 * emeta depends on the number of LUNs allocated to the pblk instance
714 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200715 for (i = 0; i < PBLK_DATA_LINES; i++) {
716 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
717 if (!l_mg->sline_meta[i])
718 goto fail_free_smeta;
719 }
720
721 /* emeta allocates three different buffers for managing metadata with
722 * in-memory and in-media layouts
723 */
724 for (i = 0; i < PBLK_DATA_LINES; i++) {
725 struct pblk_emeta *emeta;
726
727 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
728 if (!emeta)
729 goto fail_free_emeta;
730
731 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
732 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
733
734 emeta->buf = vmalloc(lm->emeta_len[0]);
735 if (!emeta->buf) {
736 kfree(emeta);
737 goto fail_free_emeta;
738 }
739
740 emeta->nr_entries = lm->emeta_sec[0];
741 l_mg->eline_meta[i] = emeta;
742 } else {
743 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
744
745 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
746 if (!emeta->buf) {
747 kfree(emeta);
748 goto fail_free_emeta;
749 }
750
751 emeta->nr_entries = lm->emeta_sec[0];
752 l_mg->eline_meta[i] = emeta;
753 }
754 }
755
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200756 for (i = 0; i < l_mg->nr_lines; i++)
757 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
758
Javier González43d47122018-03-30 00:05:09 +0200759 bb_distance = (geo->all_luns) * geo->ws_opt;
760 for (i = 0; i < lm->sec_per_line; i += bb_distance)
761 bitmap_set(l_mg->bb_template, i, geo->ws_opt);
762
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200763 return 0;
764
765fail_free_emeta:
766 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200767 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
768 vfree(l_mg->eline_meta[i]->buf);
769 else
770 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200771 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200772 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200773fail_free_smeta:
774 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200775 kfree(l_mg->sline_meta[i]);
Javier González43d47122018-03-30 00:05:09 +0200776 kfree(l_mg->bb_aux);
777fail_free_bb_template:
778 kfree(l_mg->bb_template);
779fail_free_vsc_list:
780 kfree(l_mg->vsc_list);
781fail:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200782 return -ENOMEM;
783}
784
Javier González43d47122018-03-30 00:05:09 +0200785static int pblk_line_meta_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200786{
787 struct nvm_tgt_dev *dev = pblk->dev;
788 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200789 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200790 unsigned int smeta_len, emeta_len;
Javier González43d47122018-03-30 00:05:09 +0200791 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200792
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200793 lm->sec_per_line = geo->clba * geo->all_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100794 lm->blk_per_line = geo->all_luns;
795 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200796 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100797 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200798 lm->mid_thrs = lm->sec_per_line / 2;
799 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100800 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200801
802 /* Calculate necessary pages for smeta. See comment over struct
803 * line_smeta definition
804 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200805 i = 1;
806add_smeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200807 lm->smeta_sec = i * geo->ws_opt;
808 lm->smeta_len = lm->smeta_sec * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200809
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200810 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200811 if (smeta_len > lm->smeta_len) {
812 i++;
813 goto add_smeta_page;
814 }
815
816 /* Calculate necessary pages for emeta. See comment over struct
817 * line_emeta definition
818 */
819 i = 1;
820add_emeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200821 lm->emeta_sec[0] = i * geo->ws_opt;
822 lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200823
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200824 emeta_len = calc_emeta_len(pblk);
825 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200826 i++;
827 goto add_emeta_page;
828 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200829
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100830 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +0200831
832 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100833 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +0200834 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200835 lm->emeta_sec[0], geo->clba);
Javier González21d22872017-10-13 14:46:26 +0200836
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200837 if (lm->min_blk_line > lm->blk_per_line) {
838 pr_err("pblk: config. not supported. Min. LUN in line:%d\n",
839 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200840 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200841 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200842
Javier González43d47122018-03-30 00:05:09 +0200843 return 0;
844}
845
846static int pblk_lines_init(struct pblk *pblk)
847{
848 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
849 struct pblk_line_meta *lm = &pblk->lm;
850 struct pblk_line *line;
851 void *chunk_log;
852 long nr_bad_blks = 0, nr_free_blks = 0;
853 int i, ret;
854
855 ret = pblk_line_meta_init(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200856 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200857 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200858
Javier González43d47122018-03-30 00:05:09 +0200859 ret = pblk_line_mg_init(pblk);
860 if (ret)
861 return ret;
862
863 ret = pblk_luns_init(pblk);
864 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200865 goto fail_free_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200866
Javier Gonzáleze411b332018-03-30 00:04:59 +0200867 chunk_log = pblk_bb_get_log(pblk);
868 if (IS_ERR(chunk_log)) {
869 pr_err("pblk: could not get bad block log (%lu)\n",
870 PTR_ERR(chunk_log));
871 ret = PTR_ERR(chunk_log);
Javier González43d47122018-03-30 00:05:09 +0200872 goto fail_free_luns;
873 }
874
875 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
876 GFP_KERNEL);
877 if (!pblk->lines) {
878 ret = -ENOMEM;
879 goto fail_free_chunk_log;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200880 }
881
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200882 for (i = 0; i < l_mg->nr_lines; i++) {
Javier Gonzáleze411b332018-03-30 00:04:59 +0200883 int chk_in_line;
Javier Gonzáleza44f53f2017-04-22 01:32:49 +0200884
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200885 line = &pblk->lines[i];
886
887 line->pblk = pblk;
888 line->id = i;
889 line->type = PBLK_LINETYPE_FREE;
890 line->state = PBLK_LINESTATE_FREE;
891 line->gc_group = PBLK_LINEGC_NONE;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200892 line->vsc = &l_mg->vsc_list[i];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200893 spin_lock_init(&line->lock);
894
Javier Gonzáleze411b332018-03-30 00:04:59 +0200895 ret = pblk_setup_line_meta(pblk, line, chunk_log, &nr_bad_blks);
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200896 if (ret)
Javier González43d47122018-03-30 00:05:09 +0200897 goto fail_free_lines;
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200898
Javier Gonzáleze411b332018-03-30 00:04:59 +0200899 chk_in_line = lm->blk_per_line - nr_bad_blks;
900 if (nr_bad_blks < 0 || nr_bad_blks > lm->blk_per_line ||
901 chk_in_line < lm->min_blk_line) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200902 line->state = PBLK_LINESTATE_BAD;
903 list_add_tail(&line->list, &l_mg->bad_list);
904 continue;
905 }
906
Javier Gonzáleze411b332018-03-30 00:04:59 +0200907 nr_free_blks += chk_in_line;
908 atomic_set(&line->blk_in_line, chk_in_line);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200909
910 l_mg->nr_free_lines++;
911 list_add_tail(&line->list, &l_mg->free_list);
912 }
913
914 pblk_set_provision(pblk, nr_free_blks);
915
Javier Gonzáleze411b332018-03-30 00:04:59 +0200916 kfree(chunk_log);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200917 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200918
Javier González43d47122018-03-30 00:05:09 +0200919fail_free_lines:
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200920 while (--i >= 0)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200921 pblk_line_meta_free(&pblk->lines[i]);
Javier González43d47122018-03-30 00:05:09 +0200922 kfree(pblk->lines);
923fail_free_chunk_log:
924 kfree(chunk_log);
925fail_free_luns:
926 kfree(pblk->luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200927fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +0200928 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200929
930 return ret;
931}
932
933static int pblk_writer_init(struct pblk *pblk)
934{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200935 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
936 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +0100937 int err = PTR_ERR(pblk->writer_ts);
938
939 if (err != -EINTR)
940 pr_err("pblk: could not allocate writer kthread (%d)\n",
941 err);
942 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200943 }
944
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +0100945 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
946 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
947
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200948 return 0;
949}
950
951static void pblk_writer_stop(struct pblk *pblk)
952{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +0200953 /* The pipeline must be stopped and the write buffer emptied before the
954 * write thread is stopped
955 */
956 WARN(pblk_rb_read_count(&pblk->rwb),
957 "Stopping not fully persisted write buffer\n");
958
959 WARN(pblk_rb_sync_count(&pblk->rwb),
960 "Stopping not fully synced write buffer\n");
961
Hans Holmberg7be970b2018-03-30 00:04:56 +0200962 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200963 if (pblk->writer_ts)
964 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200965}
966
967static void pblk_free(struct pblk *pblk)
968{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200969 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200970 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +0200971 pblk_rwb_free(pblk);
972 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200973
974 kfree(pblk);
975}
976
977static void pblk_tear_down(struct pblk *pblk)
978{
Javier González588726d32017-06-26 11:57:29 +0200979 pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200980 pblk_writer_stop(pblk);
981 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200982 pblk_rl_free(&pblk->rl);
983
984 pr_debug("pblk: consistent tear down\n");
985}
986
987static void pblk_exit(void *private)
988{
989 struct pblk *pblk = private;
990
991 down_write(&pblk_lock);
992 pblk_gc_exit(pblk);
993 pblk_tear_down(pblk);
Hans Holmbergc5586192017-10-13 14:46:40 +0200994
995#ifdef CONFIG_NVM_DEBUG
996 pr_info("pblk exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
997#endif
998
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200999 pblk_free(pblk);
1000 up_write(&pblk_lock);
1001}
1002
1003static sector_t pblk_capacity(void *private)
1004{
1005 struct pblk *pblk = private;
1006
1007 return pblk->capacity * NR_PHY_IN_LOG;
1008}
1009
1010static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1011 int flags)
1012{
1013 struct nvm_geo *geo = &dev->geo;
1014 struct request_queue *bqueue = dev->q;
1015 struct request_queue *tqueue = tdisk->queue;
1016 struct pblk *pblk;
1017 int ret;
1018
Javier González7ad50392018-03-30 00:05:18 +02001019 if (geo->version != NVM_OCSSD_SPEC_12) {
1020 pr_err("pblk: OCSSD version not supported (%u)\n",
1021 geo->version);
1022 return ERR_PTR(-EINVAL);
1023 }
1024
1025 if (geo->version == NVM_OCSSD_SPEC_12 && geo->dom & NVM_RSP_L2P) {
Rakesh Pandit4e76af52017-10-13 14:45:57 +02001026 pr_err("pblk: host-side L2P table not supported. (%x)\n",
Javier González7ad50392018-03-30 00:05:18 +02001027 geo->dom);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001028 return ERR_PTR(-EINVAL);
1029 }
1030
1031 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1032 if (!pblk)
1033 return ERR_PTR(-ENOMEM);
1034
1035 pblk->dev = dev;
1036 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +02001037 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg3e3a5b82017-10-13 14:46:34 +02001038 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001039
1040 spin_lock_init(&pblk->trans_lock);
1041 spin_lock_init(&pblk->lock);
1042
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001043#ifdef CONFIG_NVM_DEBUG
1044 atomic_long_set(&pblk->inflight_writes, 0);
1045 atomic_long_set(&pblk->padded_writes, 0);
1046 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001047 atomic_long_set(&pblk->req_writes, 0);
1048 atomic_long_set(&pblk->sub_writes, 0);
1049 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001050 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001051 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001052 atomic_long_set(&pblk->sync_reads, 0);
1053 atomic_long_set(&pblk->recov_writes, 0);
1054 atomic_long_set(&pblk->recov_writes, 0);
1055 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001056 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001057#endif
1058
1059 atomic_long_set(&pblk->read_failed, 0);
1060 atomic_long_set(&pblk->read_empty, 0);
1061 atomic_long_set(&pblk->read_high_ecc, 0);
1062 atomic_long_set(&pblk->read_failed_gc, 0);
1063 atomic_long_set(&pblk->write_failed, 0);
1064 atomic_long_set(&pblk->erase_failed, 0);
1065
Javier González43d47122018-03-30 00:05:09 +02001066 ret = pblk_core_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001067 if (ret) {
Javier González43d47122018-03-30 00:05:09 +02001068 pr_err("pblk: could not initialize core\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001069 goto fail;
1070 }
1071
1072 ret = pblk_lines_init(pblk);
1073 if (ret) {
1074 pr_err("pblk: could not initialize lines\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001075 goto fail_free_core;
1076 }
1077
Javier González43d47122018-03-30 00:05:09 +02001078 ret = pblk_rwb_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001079 if (ret) {
Javier González43d47122018-03-30 00:05:09 +02001080 pr_err("pblk: could not initialize write buffer\n");
1081 goto fail_free_lines;
1082 }
1083
1084 ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1085 if (ret) {
1086 pr_err("pblk: could not initialize maps\n");
1087 goto fail_free_rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001088 }
1089
1090 ret = pblk_writer_init(pblk);
1091 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001092 if (ret != -EINTR)
1093 pr_err("pblk: could not initialize write thread\n");
Javier González43d47122018-03-30 00:05:09 +02001094 goto fail_free_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001095 }
1096
1097 ret = pblk_gc_init(pblk);
1098 if (ret) {
1099 pr_err("pblk: could not initialize gc\n");
1100 goto fail_stop_writer;
1101 }
1102
1103 /* inherit the size from the underlying device */
1104 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1105 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1106
1107 blk_queue_write_cache(tqueue, true, false);
1108
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001109 tqueue->limits.discard_granularity = geo->clba * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001110 tqueue->limits.discard_alignment = 0;
1111 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001112 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001113
Javier González30d82a862018-01-05 14:16:19 +01001114 pr_info("pblk(%s): luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
1115 tdisk->disk_name,
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001116 geo->all_luns, pblk->l_mg.nr_lines,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001117 (unsigned long long)pblk->rl.nr_secs,
1118 pblk->rwb.nr_entries);
1119
1120 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001121
1122 /* Check if we need to start GC */
1123 pblk_gc_should_kick(pblk);
1124
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001125 return pblk;
1126
1127fail_stop_writer:
1128 pblk_writer_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001129fail_free_l2p:
1130 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001131fail_free_rwb:
1132 pblk_rwb_free(pblk);
1133fail_free_lines:
1134 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001135fail_free_core:
1136 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001137fail:
1138 kfree(pblk);
1139 return ERR_PTR(ret);
1140}
1141
1142/* physical block device target */
1143static struct nvm_tgt_type tt_pblk = {
1144 .name = "pblk",
1145 .version = {1, 0, 0},
1146
1147 .make_rq = pblk_make_rq,
1148 .capacity = pblk_capacity,
1149
1150 .init = pblk_init,
1151 .exit = pblk_exit,
1152
1153 .sysfs_init = pblk_sysfs_init,
1154 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001155 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001156};
1157
1158static int __init pblk_module_init(void)
1159{
NeilBrownb25d5232017-06-18 14:38:58 +10001160 int ret;
1161
1162 pblk_bio_set = bioset_create(BIO_POOL_SIZE, 0, 0);
1163 if (!pblk_bio_set)
1164 return -ENOMEM;
1165 ret = nvm_register_tgt_type(&tt_pblk);
1166 if (ret)
1167 bioset_free(pblk_bio_set);
1168 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001169}
1170
1171static void pblk_module_exit(void)
1172{
NeilBrownb25d5232017-06-18 14:38:58 +10001173 bioset_free(pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001174 nvm_unregister_tgt_type(&tt_pblk);
1175}
1176
1177module_init(pblk_module_init);
1178module_exit(pblk_module_exit);
1179MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1180MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1181MODULE_LICENSE("GPL v2");
1182MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");