blob: fe501e6d45fc439ba318891aab1b6fd37163638f [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);
Kent Overstreetb906bbb2018-05-20 18:25:50 -040026struct 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);
Javier González1d8b33e2018-06-01 15:04:16 +0200130 if (!line)
Javier González43d47122018-03-30 00:05:09 +0200131 return -EFAULT;
Javier González43d47122018-03-30 00:05:09 +0200132 }
133
134 return 0;
135}
136
137static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200138{
139 sector_t i;
140 struct ppa_addr ppa;
Hans Holmbergc5586192017-10-13 14:46:40 +0200141 size_t map_size;
Javier González1d8b33e2018-06-01 15:04:16 +0200142 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200143
Hans Holmbergc5586192017-10-13 14:46:40 +0200144 map_size = pblk_trans_map_size(pblk);
145 pblk->trans_map = vmalloc(map_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200146 if (!pblk->trans_map)
147 return -ENOMEM;
148
149 pblk_ppa_set_empty(&ppa);
150
151 for (i = 0; i < pblk->rl.nr_secs; i++)
152 pblk_trans_map_set(pblk, i, ppa);
153
Javier González1d8b33e2018-06-01 15:04:16 +0200154 ret = pblk_l2p_recover(pblk, factory_init);
155 if (ret)
156 vfree(pblk->trans_map);
157
158 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200159}
160
161static void pblk_rwb_free(struct pblk *pblk)
162{
163 if (pblk_rb_tear_down_check(&pblk->rwb))
164 pr_err("pblk: write buffer error on tear down\n");
165
166 pblk_rb_data_free(&pblk->rwb);
167 vfree(pblk_rb_entries_ref(&pblk->rwb));
168}
169
170static int pblk_rwb_init(struct pblk *pblk)
171{
172 struct nvm_tgt_dev *dev = pblk->dev;
173 struct nvm_geo *geo = &dev->geo;
174 struct pblk_rb_entry *entries;
175 unsigned long nr_entries;
176 unsigned int power_size, power_seg_sz;
177
178 nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer);
179
180 entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
181 if (!entries)
182 return -ENOMEM;
183
184 power_size = get_count_order(nr_entries);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200185 power_seg_sz = get_count_order(geo->csecs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200186
187 return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
188}
189
190/* Minimum pages needed within a lun */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200191#define ADDR_POOL_SIZE 64
192
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200193static int pblk_set_addrf_12(struct nvm_geo *geo, struct nvm_addrf_12 *dst)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200194{
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200195 struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
196 int power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200197
198 /* Re-calculate channel and lun format to adapt to configuration */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200199 power_len = get_count_order(geo->num_ch);
200 if (1 << power_len != geo->num_ch) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200201 pr_err("pblk: supports only power-of-two channel config.\n");
202 return -EINVAL;
203 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200204 dst->ch_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200205
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200206 power_len = get_count_order(geo->num_lun);
207 if (1 << power_len != geo->num_lun) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208 pr_err("pblk: supports only power-of-two LUN config.\n");
209 return -EINVAL;
210 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200211 dst->lun_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200213 dst->blk_len = src->blk_len;
214 dst->pg_len = src->pg_len;
215 dst->pln_len = src->pln_len;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200216 dst->sec_len = src->sec_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200217
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200218 dst->sec_offset = 0;
219 dst->pln_offset = dst->sec_len;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200220 dst->ch_offset = dst->pln_offset + dst->pln_len;
221 dst->lun_offset = dst->ch_offset + dst->ch_len;
222 dst->pg_offset = dst->lun_offset + dst->lun_len;
223 dst->blk_offset = dst->pg_offset + dst->pg_len;
224
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200225 dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200226 dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
227 dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
228 dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
229 dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
230 dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
231
232 return dst->blk_offset + src->blk_len;
233}
234
Javier González3b2a3ad2018-03-30 00:05:21 +0200235static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
236 struct pblk_addrf *udst)
237{
238 struct nvm_addrf *src = &geo->addrf;
239
240 adst->ch_len = get_count_order(geo->num_ch);
241 adst->lun_len = get_count_order(geo->num_lun);
242 adst->chk_len = src->chk_len;
243 adst->sec_len = src->sec_len;
244
245 adst->sec_offset = 0;
246 adst->ch_offset = adst->sec_len;
247 adst->lun_offset = adst->ch_offset + adst->ch_len;
248 adst->chk_offset = adst->lun_offset + adst->lun_len;
249
250 adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
251 adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
252 adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
253 adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
254
255 udst->sec_stripe = geo->ws_opt;
256 udst->ch_stripe = geo->num_ch;
257 udst->lun_stripe = geo->num_lun;
258
259 udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
260 udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
261
262 return adst->chk_offset + adst->chk_len;
263}
264
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200265static int pblk_set_addrf(struct pblk *pblk)
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200266{
267 struct nvm_tgt_dev *dev = pblk->dev;
268 struct nvm_geo *geo = &dev->geo;
269 int mod;
270
Javier González3b2a3ad2018-03-30 00:05:21 +0200271 switch (geo->version) {
272 case NVM_OCSSD_SPEC_12:
273 div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
274 if (mod) {
275 pr_err("pblk: bad configuration of sectors/pages\n");
276 return -EINVAL;
277 }
278
279 pblk->addrf_len = pblk_set_addrf_12(geo, (void *)&pblk->addrf);
280 break;
281 case NVM_OCSSD_SPEC_20:
282 pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
283 &pblk->uaddrf);
284 break;
285 default:
286 pr_err("pblk: OCSSD revision not supported (%d)\n",
287 geo->version);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200288 return -EINVAL;
289 }
290
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200291 return 0;
292}
293
294static int pblk_init_global_caches(struct pblk *pblk)
295{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200296 down_write(&pblk_lock);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200297 pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298 sizeof(struct pblk_line_ws), 0, 0, NULL);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200299 if (!pblk_ws_cache) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300 up_write(&pblk_lock);
301 return -ENOMEM;
302 }
303
304 pblk_rec_cache = kmem_cache_create("pblk_rec",
305 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
306 if (!pblk_rec_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200307 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200308 up_write(&pblk_lock);
309 return -ENOMEM;
310 }
311
Javier González084ec9b2017-06-26 16:27:13 -0600312 pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200313 0, 0, NULL);
Javier González084ec9b2017-06-26 16:27:13 -0600314 if (!pblk_g_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200315 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200316 kmem_cache_destroy(pblk_rec_cache);
317 up_write(&pblk_lock);
318 return -ENOMEM;
319 }
320
321 pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
322 0, 0, NULL);
323 if (!pblk_w_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200324 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200325 kmem_cache_destroy(pblk_rec_cache);
Javier González084ec9b2017-06-26 16:27:13 -0600326 kmem_cache_destroy(pblk_g_rq_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200327 up_write(&pblk_lock);
328 return -ENOMEM;
329 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200330 up_write(&pblk_lock);
331
332 return 0;
333}
334
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200335static void pblk_free_global_caches(struct pblk *pblk)
336{
337 kmem_cache_destroy(pblk_ws_cache);
338 kmem_cache_destroy(pblk_rec_cache);
339 kmem_cache_destroy(pblk_g_rq_cache);
340 kmem_cache_destroy(pblk_w_rq_cache);
341}
342
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200343static int pblk_core_init(struct pblk *pblk)
344{
345 struct nvm_tgt_dev *dev = pblk->dev;
346 struct nvm_geo *geo = &dev->geo;
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400347 int ret, max_write_ppas;
Javier González43d47122018-03-30 00:05:09 +0200348
349 atomic64_set(&pblk->user_wa, 0);
350 atomic64_set(&pblk->pad_wa, 0);
351 atomic64_set(&pblk->gc_wa, 0);
352 pblk->user_rst_wa = 0;
353 pblk->pad_rst_wa = 0;
354 pblk->gc_rst_wa = 0;
355
356 atomic64_set(&pblk->nr_flush, 0);
357 pblk->nr_flush_rst = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200358
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200359 pblk->pgs_in_buffer = geo->mw_cunits * geo->all_luns;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200360
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200361 pblk->min_write_pgs = geo->ws_opt * (geo->csecs / PAGE_SIZE);
Javier González43d47122018-03-30 00:05:09 +0200362 max_write_ppas = pblk->min_write_pgs * geo->all_luns;
363 pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
364 pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
365
366 if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
367 pr_err("pblk: vector list too big(%u > %u)\n",
368 pblk->max_write_pgs, PBLK_MAX_REQ_ADDRS);
369 return -EINVAL;
370 }
371
372 pblk->pad_dist = kzalloc((pblk->min_write_pgs - 1) * sizeof(atomic64_t),
373 GFP_KERNEL);
374 if (!pblk->pad_dist)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200375 return -ENOMEM;
376
Javier González43d47122018-03-30 00:05:09 +0200377 if (pblk_init_global_caches(pblk))
378 goto fail_free_pad_dist;
379
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200380 /* Internal bios can be at most the sectors signaled by the device. */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400381 ret = mempool_init_page_pool(&pblk->page_bio_pool, NVM_MAX_VLBA, 0);
382 if (ret)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200383 goto free_global_caches;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200384
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400385 ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
386 pblk_ws_cache);
387 if (ret)
Javier Gonzálezbd432412017-10-13 14:46:06 +0200388 goto free_page_bio_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200389
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400390 ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
391 pblk_rec_cache);
392 if (ret)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200393 goto free_gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200394
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400395 ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
396 pblk_g_rq_cache);
397 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200398 goto free_rec_pool;
399
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400400 ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
401 pblk_g_rq_cache);
402 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200403 goto free_r_rq_pool;
404
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400405 ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
406 pblk_w_rq_cache);
407 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200408 goto free_e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200409
Javier Gonzálezef576492017-06-26 11:57:28 +0200410 pblk->close_wq = alloc_workqueue("pblk-close-wq",
411 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
412 if (!pblk->close_wq)
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200413 goto free_w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200414
Javier Gonzálezef576492017-06-26 11:57:28 +0200415 pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
416 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
417 if (!pblk->bb_wq)
418 goto free_close_wq;
419
Javier González7bd4d372017-10-13 14:46:23 +0200420 pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
421 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
422 if (!pblk->r_end_wq)
Javier Gonzálezef576492017-06-26 11:57:28 +0200423 goto free_bb_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200424
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200425 if (pblk_set_addrf(pblk))
Javier González7bd4d372017-10-13 14:46:23 +0200426 goto free_r_end_wq;
427
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200428 INIT_LIST_HEAD(&pblk->compl_list);
Javier González43d47122018-03-30 00:05:09 +0200429
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200430 return 0;
431
Javier González7bd4d372017-10-13 14:46:23 +0200432free_r_end_wq:
433 destroy_workqueue(pblk->r_end_wq);
Javier Gonzálezef576492017-06-26 11:57:28 +0200434free_bb_wq:
435 destroy_workqueue(pblk->bb_wq);
436free_close_wq:
437 destroy_workqueue(pblk->close_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438free_w_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400439 mempool_exit(&pblk->w_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200440free_e_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400441 mempool_exit(&pblk->e_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200442free_r_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400443 mempool_exit(&pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200444free_rec_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400445 mempool_exit(&pblk->rec_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200446free_gen_ws_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400447 mempool_exit(&pblk->gen_ws_pool);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200448free_page_bio_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400449 mempool_exit(&pblk->page_bio_pool);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200450free_global_caches:
451 pblk_free_global_caches(pblk);
Javier González43d47122018-03-30 00:05:09 +0200452fail_free_pad_dist:
453 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200454 return -ENOMEM;
455}
456
457static void pblk_core_free(struct pblk *pblk)
458{
Javier Gonzálezef576492017-06-26 11:57:28 +0200459 if (pblk->close_wq)
460 destroy_workqueue(pblk->close_wq);
461
Javier González7bd4d372017-10-13 14:46:23 +0200462 if (pblk->r_end_wq)
463 destroy_workqueue(pblk->r_end_wq);
464
Javier Gonzálezef576492017-06-26 11:57:28 +0200465 if (pblk->bb_wq)
466 destroy_workqueue(pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200467
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400468 mempool_exit(&pblk->page_bio_pool);
469 mempool_exit(&pblk->gen_ws_pool);
470 mempool_exit(&pblk->rec_pool);
471 mempool_exit(&pblk->r_rq_pool);
472 mempool_exit(&pblk->e_rq_pool);
473 mempool_exit(&pblk->w_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200474
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200475 pblk_free_global_caches(pblk);
Javier González43d47122018-03-30 00:05:09 +0200476 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200477}
478
Javier Gonzáleze411b332018-03-30 00:04:59 +0200479static void pblk_line_mg_free(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200480{
481 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
482 int i;
483
484 kfree(l_mg->bb_template);
485 kfree(l_mg->bb_aux);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200486 kfree(l_mg->vsc_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200487
488 for (i = 0; i < PBLK_DATA_LINES; i++) {
Javier Gonzálezf680f192017-06-26 11:57:21 +0200489 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200490 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200491 kfree(l_mg->eline_meta[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200492 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200493}
494
Javier Gonzáleze411b332018-03-30 00:04:59 +0200495static void pblk_line_meta_free(struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200496{
Javier Gonzáleze411b332018-03-30 00:04:59 +0200497 kfree(line->blk_bitmap);
498 kfree(line->erase_bitmap);
Javier González32ef9412018-03-30 00:05:20 +0200499 kfree(line->chks);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200500}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200501
Javier Gonzáleze411b332018-03-30 00:04:59 +0200502static void pblk_lines_free(struct pblk *pblk)
503{
504 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
505 struct pblk_line *line;
506 int i;
507
508 spin_lock(&l_mg->free_lock);
509 for (i = 0; i < l_mg->nr_lines; i++) {
510 line = &pblk->lines[i];
511
512 pblk_line_free(pblk, line);
513 pblk_line_meta_free(line);
514 }
515 spin_unlock(&l_mg->free_lock);
Javier González43d47122018-03-30 00:05:09 +0200516
517 pblk_line_mg_free(pblk);
518
519 kfree(pblk->luns);
520 kfree(pblk->lines);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200521}
522
523static int pblk_bb_get_tbl(struct nvm_tgt_dev *dev, struct pblk_lun *rlun,
524 u8 *blks, int nr_blks)
525{
526 struct ppa_addr ppa;
527 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200528
529 ppa.ppa = 0;
530 ppa.g.ch = rlun->bppa.g.ch;
531 ppa.g.lun = rlun->bppa.g.lun;
532
533 ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
534 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200535 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200536
537 nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200538 if (nr_blks < 0)
539 return -EIO;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200540
Wei Yongjun5136a4f2017-04-25 16:15:30 +0000541 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200542}
543
Javier González32ef9412018-03-30 00:05:20 +0200544static void *pblk_bb_get_meta(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200545{
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200546 struct nvm_tgt_dev *dev = pblk->dev;
547 struct nvm_geo *geo = &dev->geo;
Javier González32ef9412018-03-30 00:05:20 +0200548 u8 *meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200549 int i, nr_blks, blk_per_lun;
550 int ret;
551
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200552 blk_per_lun = geo->num_chk * geo->pln_mode;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200553 nr_blks = blk_per_lun * geo->all_luns;
554
Javier González32ef9412018-03-30 00:05:20 +0200555 meta = kmalloc(nr_blks, GFP_KERNEL);
556 if (!meta)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200557 return ERR_PTR(-ENOMEM);
558
559 for (i = 0; i < geo->all_luns; i++) {
560 struct pblk_lun *rlun = &pblk->luns[i];
Javier González32ef9412018-03-30 00:05:20 +0200561 u8 *meta_pos = meta + i * blk_per_lun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200562
Javier González32ef9412018-03-30 00:05:20 +0200563 ret = pblk_bb_get_tbl(dev, rlun, meta_pos, blk_per_lun);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200564 if (ret) {
Javier González32ef9412018-03-30 00:05:20 +0200565 kfree(meta);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200566 return ERR_PTR(-EIO);
567 }
568 }
569
Javier González32ef9412018-03-30 00:05:20 +0200570 return meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200571}
572
Javier González32ef9412018-03-30 00:05:20 +0200573static void *pblk_chunk_get_meta(struct pblk *pblk)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200574{
575 struct nvm_tgt_dev *dev = pblk->dev;
576 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200577
Javier González32ef9412018-03-30 00:05:20 +0200578 if (geo->version == NVM_OCSSD_SPEC_12)
579 return pblk_bb_get_meta(pblk);
580 else
581 return pblk_chunk_get_info(pblk);
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200582}
583
Javier González43d47122018-03-30 00:05:09 +0200584static int pblk_luns_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200585{
586 struct nvm_tgt_dev *dev = pblk->dev;
587 struct nvm_geo *geo = &dev->geo;
588 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200589 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200590
591 /* TODO: Implement unbalanced LUN support */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200592 if (geo->num_lun < 0) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200593 pr_err("pblk: unbalanced LUN config.\n");
594 return -EINVAL;
595 }
596
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100597 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
598 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200599 if (!pblk->luns)
600 return -ENOMEM;
601
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100602 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200603 /* Stripe across channels */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200604 int ch = i % geo->num_ch;
605 int lun_raw = i / geo->num_ch;
606 int lunid = lun_raw + ch * geo->num_lun;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607
608 rlun = &pblk->luns[i];
Javier González43d47122018-03-30 00:05:09 +0200609 rlun->bppa = dev->luns[lunid];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200610
611 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200612 }
613
614 return 0;
615}
616
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200617/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200618static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200619{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200620 struct pblk_line_meta *lm = &pblk->lm;
621 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
622 struct nvm_tgt_dev *dev = pblk->dev;
623 struct nvm_geo *geo = &dev->geo;
624
625 /* Round to sector size so that lba_list starts on its own sector */
626 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200627 sizeof(struct line_emeta) + lm->blk_bitmap_len +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200628 sizeof(struct wa_counters), geo->csecs);
629 lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200630
631 /* Round to sector size so that vsc_list starts on its own sector */
632 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
633 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200634 geo->csecs);
635 lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200636
637 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200638 geo->csecs);
639 lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200640
641 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
642
643 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200644}
645
646static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
647{
648 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100649 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
650 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200651 struct nvm_geo *geo = &dev->geo;
652 sector_t provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100653 int sec_meta, blk_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200654
Javier Gonzáleze5392732018-01-05 14:16:14 +0100655 if (geo->op == NVM_TARGET_DEFAULT_OP)
656 pblk->op = PBLK_DEFAULT_OP;
657 else
658 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200659
660 provisioned = nr_free_blks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100661 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200662 sector_div(provisioned, 100);
663
Javier Gonzáleza7689932018-01-05 14:16:13 +0100664 pblk->op_blks = nr_free_blks - provisioned;
665
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200666 /* Internally pblk manages all free blocks, but all calculations based
667 * on user capacity consider only provisioned blocks
668 */
669 pblk->rl.total_blocks = nr_free_blks;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200670 pblk->rl.nr_secs = nr_free_blks * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100671
672 /* Consider sectors used for metadata */
673 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200674 blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100675
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200676 pblk->capacity = (provisioned - blk_meta) * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100677
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200678 atomic_set(&pblk->rl.free_blocks, nr_free_blks);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100679 atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200680}
681
Javier González32ef9412018-03-30 00:05:20 +0200682static int pblk_setup_line_meta_12(struct pblk *pblk, struct pblk_line *line,
683 void *chunk_meta)
684{
685 struct nvm_tgt_dev *dev = pblk->dev;
686 struct nvm_geo *geo = &dev->geo;
687 struct pblk_line_meta *lm = &pblk->lm;
688 int i, chk_per_lun, nr_bad_chks = 0;
689
690 chk_per_lun = geo->num_chk * geo->pln_mode;
691
692 for (i = 0; i < lm->blk_per_line; i++) {
693 struct pblk_lun *rlun = &pblk->luns[i];
694 struct nvm_chk_meta *chunk;
695 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
696 u8 *lun_bb_meta = chunk_meta + pos * chk_per_lun;
697
698 chunk = &line->chks[pos];
699
700 /*
701 * In 1.2 spec. chunk state is not persisted by the device. Thus
702 * some of the values are reset each time pblk is instantiated.
703 */
704 if (lun_bb_meta[line->id] == NVM_BLK_T_FREE)
705 chunk->state = NVM_CHK_ST_FREE;
706 else
707 chunk->state = NVM_CHK_ST_OFFLINE;
708
709 chunk->type = NVM_CHK_TP_W_SEQ;
710 chunk->wi = 0;
711 chunk->slba = -1;
712 chunk->cnlb = geo->clba;
713 chunk->wp = 0;
714
715 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
716 continue;
717
718 set_bit(pos, line->blk_bitmap);
719 nr_bad_chks++;
720 }
721
722 return nr_bad_chks;
723}
724
725static int pblk_setup_line_meta_20(struct pblk *pblk, struct pblk_line *line,
726 struct nvm_chk_meta *meta)
727{
728 struct nvm_tgt_dev *dev = pblk->dev;
729 struct nvm_geo *geo = &dev->geo;
730 struct pblk_line_meta *lm = &pblk->lm;
731 int i, nr_bad_chks = 0;
732
733 for (i = 0; i < lm->blk_per_line; i++) {
734 struct pblk_lun *rlun = &pblk->luns[i];
735 struct nvm_chk_meta *chunk;
736 struct nvm_chk_meta *chunk_meta;
737 struct ppa_addr ppa;
738 int pos;
739
740 ppa = rlun->bppa;
741 pos = pblk_ppa_to_pos(geo, ppa);
742 chunk = &line->chks[pos];
743
744 ppa.m.chk = line->id;
745 chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
746
747 chunk->state = chunk_meta->state;
748 chunk->type = chunk_meta->type;
749 chunk->wi = chunk_meta->wi;
750 chunk->slba = chunk_meta->slba;
751 chunk->cnlb = chunk_meta->cnlb;
752 chunk->wp = chunk_meta->wp;
753
754 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
755 continue;
756
757 if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
758 WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
759 continue;
760 }
761
762 set_bit(pos, line->blk_bitmap);
763 nr_bad_chks++;
764 }
765
766 return nr_bad_chks;
767}
768
769static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
770 void *chunk_meta, int line_id)
771{
772 struct nvm_tgt_dev *dev = pblk->dev;
773 struct nvm_geo *geo = &dev->geo;
774 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
775 struct pblk_line_meta *lm = &pblk->lm;
776 long nr_bad_chks, chk_in_line;
777
778 line->pblk = pblk;
779 line->id = line_id;
780 line->type = PBLK_LINETYPE_FREE;
781 line->state = PBLK_LINESTATE_NEW;
782 line->gc_group = PBLK_LINEGC_NONE;
783 line->vsc = &l_mg->vsc_list[line_id];
784 spin_lock_init(&line->lock);
785
786 if (geo->version == NVM_OCSSD_SPEC_12)
787 nr_bad_chks = pblk_setup_line_meta_12(pblk, line, chunk_meta);
788 else
789 nr_bad_chks = pblk_setup_line_meta_20(pblk, line, chunk_meta);
790
791 chk_in_line = lm->blk_per_line - nr_bad_chks;
792 if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
793 chk_in_line < lm->min_blk_line) {
794 line->state = PBLK_LINESTATE_BAD;
795 list_add_tail(&line->list, &l_mg->bad_list);
796 return 0;
797 }
798
799 atomic_set(&line->blk_in_line, chk_in_line);
800 list_add_tail(&line->list, &l_mg->free_list);
801 l_mg->nr_free_lines++;
802
803 return chk_in_line;
804}
805
806static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200807{
Javier González43d47122018-03-30 00:05:09 +0200808 struct pblk_line_meta *lm = &pblk->lm;
809
810 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
811 if (!line->blk_bitmap)
812 return -ENOMEM;
813
814 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
815 if (!line->erase_bitmap) {
816 kfree(line->blk_bitmap);
817 return -ENOMEM;
818 }
819
Javier González32ef9412018-03-30 00:05:20 +0200820 line->chks = kmalloc(lm->blk_per_line * sizeof(struct nvm_chk_meta),
821 GFP_KERNEL);
822 if (!line->chks) {
823 kfree(line->erase_bitmap);
824 kfree(line->blk_bitmap);
825 return -ENOMEM;
826 }
Javier González43d47122018-03-30 00:05:09 +0200827
828 return 0;
829}
830
831static int pblk_line_mg_init(struct pblk *pblk)
832{
833 struct nvm_tgt_dev *dev = pblk->dev;
834 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200835 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
836 struct pblk_line_meta *lm = &pblk->lm;
Javier González43d47122018-03-30 00:05:09 +0200837 int i, bb_distance;
838
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200839 l_mg->nr_lines = geo->num_chk;
Javier González43d47122018-03-30 00:05:09 +0200840 l_mg->log_line = l_mg->data_line = NULL;
841 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
842 l_mg->nr_free_lines = 0;
843 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
844
845 INIT_LIST_HEAD(&l_mg->free_list);
846 INIT_LIST_HEAD(&l_mg->corrupt_list);
847 INIT_LIST_HEAD(&l_mg->bad_list);
848 INIT_LIST_HEAD(&l_mg->gc_full_list);
849 INIT_LIST_HEAD(&l_mg->gc_high_list);
850 INIT_LIST_HEAD(&l_mg->gc_mid_list);
851 INIT_LIST_HEAD(&l_mg->gc_low_list);
852 INIT_LIST_HEAD(&l_mg->gc_empty_list);
853
854 INIT_LIST_HEAD(&l_mg->emeta_list);
855
856 l_mg->gc_lists[0] = &l_mg->gc_high_list;
857 l_mg->gc_lists[1] = &l_mg->gc_mid_list;
858 l_mg->gc_lists[2] = &l_mg->gc_low_list;
859
860 spin_lock_init(&l_mg->free_lock);
861 spin_lock_init(&l_mg->close_lock);
862 spin_lock_init(&l_mg->gc_lock);
863
864 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
865 if (!l_mg->vsc_list)
866 goto fail;
867
868 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
869 if (!l_mg->bb_template)
870 goto fail_free_vsc_list;
871
872 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
873 if (!l_mg->bb_aux)
874 goto fail_free_bb_template;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200875
876 /* smeta is always small enough to fit on a kmalloc memory allocation,
877 * emeta depends on the number of LUNs allocated to the pblk instance
878 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200879 for (i = 0; i < PBLK_DATA_LINES; i++) {
880 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
881 if (!l_mg->sline_meta[i])
882 goto fail_free_smeta;
883 }
884
885 /* emeta allocates three different buffers for managing metadata with
886 * in-memory and in-media layouts
887 */
888 for (i = 0; i < PBLK_DATA_LINES; i++) {
889 struct pblk_emeta *emeta;
890
891 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
892 if (!emeta)
893 goto fail_free_emeta;
894
895 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
896 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
897
898 emeta->buf = vmalloc(lm->emeta_len[0]);
899 if (!emeta->buf) {
900 kfree(emeta);
901 goto fail_free_emeta;
902 }
903
904 emeta->nr_entries = lm->emeta_sec[0];
905 l_mg->eline_meta[i] = emeta;
906 } else {
907 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
908
909 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
910 if (!emeta->buf) {
911 kfree(emeta);
912 goto fail_free_emeta;
913 }
914
915 emeta->nr_entries = lm->emeta_sec[0];
916 l_mg->eline_meta[i] = emeta;
917 }
918 }
919
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200920 for (i = 0; i < l_mg->nr_lines; i++)
921 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
922
Javier González43d47122018-03-30 00:05:09 +0200923 bb_distance = (geo->all_luns) * geo->ws_opt;
924 for (i = 0; i < lm->sec_per_line; i += bb_distance)
925 bitmap_set(l_mg->bb_template, i, geo->ws_opt);
926
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200927 return 0;
928
929fail_free_emeta:
930 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200931 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
932 vfree(l_mg->eline_meta[i]->buf);
933 else
934 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200935 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200936 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200937fail_free_smeta:
938 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200939 kfree(l_mg->sline_meta[i]);
Javier González43d47122018-03-30 00:05:09 +0200940 kfree(l_mg->bb_aux);
941fail_free_bb_template:
942 kfree(l_mg->bb_template);
943fail_free_vsc_list:
944 kfree(l_mg->vsc_list);
945fail:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200946 return -ENOMEM;
947}
948
Javier González43d47122018-03-30 00:05:09 +0200949static int pblk_line_meta_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200950{
951 struct nvm_tgt_dev *dev = pblk->dev;
952 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200953 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200954 unsigned int smeta_len, emeta_len;
Javier González43d47122018-03-30 00:05:09 +0200955 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200956
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200957 lm->sec_per_line = geo->clba * geo->all_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100958 lm->blk_per_line = geo->all_luns;
959 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200960 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100961 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200962 lm->mid_thrs = lm->sec_per_line / 2;
963 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100964 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200965
966 /* Calculate necessary pages for smeta. See comment over struct
967 * line_smeta definition
968 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200969 i = 1;
970add_smeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200971 lm->smeta_sec = i * geo->ws_opt;
972 lm->smeta_len = lm->smeta_sec * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200973
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200974 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200975 if (smeta_len > lm->smeta_len) {
976 i++;
977 goto add_smeta_page;
978 }
979
980 /* Calculate necessary pages for emeta. See comment over struct
981 * line_emeta definition
982 */
983 i = 1;
984add_emeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200985 lm->emeta_sec[0] = i * geo->ws_opt;
986 lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200987
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200988 emeta_len = calc_emeta_len(pblk);
989 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200990 i++;
991 goto add_emeta_page;
992 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200993
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100994 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +0200995
996 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100997 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +0200998 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200999 lm->emeta_sec[0], geo->clba);
Javier González21d22872017-10-13 14:46:26 +02001000
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001001 if (lm->min_blk_line > lm->blk_per_line) {
1002 pr_err("pblk: config. not supported. Min. LUN in line:%d\n",
1003 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +02001004 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001005 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001006
Javier González43d47122018-03-30 00:05:09 +02001007 return 0;
1008}
1009
1010static int pblk_lines_init(struct pblk *pblk)
1011{
1012 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González43d47122018-03-30 00:05:09 +02001013 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +02001014 void *chunk_meta;
1015 long nr_free_chks = 0;
Javier González43d47122018-03-30 00:05:09 +02001016 int i, ret;
1017
1018 ret = pblk_line_meta_init(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001019 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +02001020 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001021
Javier González43d47122018-03-30 00:05:09 +02001022 ret = pblk_line_mg_init(pblk);
1023 if (ret)
1024 return ret;
1025
1026 ret = pblk_luns_init(pblk);
1027 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001028 goto fail_free_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001029
Javier González32ef9412018-03-30 00:05:20 +02001030 chunk_meta = pblk_chunk_get_meta(pblk);
1031 if (IS_ERR(chunk_meta)) {
1032 ret = PTR_ERR(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001033 goto fail_free_luns;
1034 }
1035
1036 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
1037 GFP_KERNEL);
1038 if (!pblk->lines) {
1039 ret = -ENOMEM;
Javier González32ef9412018-03-30 00:05:20 +02001040 goto fail_free_chunk_meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001041 }
1042
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001043 for (i = 0; i < l_mg->nr_lines; i++) {
1044 line = &pblk->lines[i];
1045
Javier González32ef9412018-03-30 00:05:20 +02001046 ret = pblk_alloc_line_meta(pblk, line);
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001047 if (ret)
Javier González43d47122018-03-30 00:05:09 +02001048 goto fail_free_lines;
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001049
Javier González32ef9412018-03-30 00:05:20 +02001050 nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001051 }
1052
Javier González2deeefc2018-06-01 15:04:15 +02001053 if (!nr_free_chks) {
1054 pr_err("pblk: too many bad blocks prevent for sane instance\n");
1055 return -EINTR;
1056 }
1057
Javier González32ef9412018-03-30 00:05:20 +02001058 pblk_set_provision(pblk, nr_free_chks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001059
Javier González32ef9412018-03-30 00:05:20 +02001060 kfree(chunk_meta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001061 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001062
Javier González43d47122018-03-30 00:05:09 +02001063fail_free_lines:
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001064 while (--i >= 0)
Javier Gonzáleze411b332018-03-30 00:04:59 +02001065 pblk_line_meta_free(&pblk->lines[i]);
Javier González43d47122018-03-30 00:05:09 +02001066 kfree(pblk->lines);
Javier González32ef9412018-03-30 00:05:20 +02001067fail_free_chunk_meta:
1068 kfree(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001069fail_free_luns:
1070 kfree(pblk->luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001071fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +02001072 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001073
1074 return ret;
1075}
1076
1077static int pblk_writer_init(struct pblk *pblk)
1078{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001079 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
1080 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001081 int err = PTR_ERR(pblk->writer_ts);
1082
1083 if (err != -EINTR)
1084 pr_err("pblk: could not allocate writer kthread (%d)\n",
1085 err);
1086 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001087 }
1088
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001089 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1090 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1091
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001092 return 0;
1093}
1094
1095static void pblk_writer_stop(struct pblk *pblk)
1096{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001097 /* The pipeline must be stopped and the write buffer emptied before the
1098 * write thread is stopped
1099 */
1100 WARN(pblk_rb_read_count(&pblk->rwb),
1101 "Stopping not fully persisted write buffer\n");
1102
1103 WARN(pblk_rb_sync_count(&pblk->rwb),
1104 "Stopping not fully synced write buffer\n");
1105
Hans Holmberg7be970b2018-03-30 00:04:56 +02001106 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001107 if (pblk->writer_ts)
1108 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001109}
1110
1111static void pblk_free(struct pblk *pblk)
1112{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001113 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001114 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001115 pblk_rwb_free(pblk);
1116 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001117
1118 kfree(pblk);
1119}
1120
1121static void pblk_tear_down(struct pblk *pblk)
1122{
Javier González588726d32017-06-26 11:57:29 +02001123 pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001124 pblk_writer_stop(pblk);
1125 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001126 pblk_rl_free(&pblk->rl);
1127
1128 pr_debug("pblk: consistent tear down\n");
1129}
1130
1131static void pblk_exit(void *private)
1132{
1133 struct pblk *pblk = private;
1134
1135 down_write(&pblk_lock);
1136 pblk_gc_exit(pblk);
1137 pblk_tear_down(pblk);
Hans Holmbergc5586192017-10-13 14:46:40 +02001138
1139#ifdef CONFIG_NVM_DEBUG
1140 pr_info("pblk exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
1141#endif
1142
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001143 pblk_free(pblk);
1144 up_write(&pblk_lock);
1145}
1146
1147static sector_t pblk_capacity(void *private)
1148{
1149 struct pblk *pblk = private;
1150
1151 return pblk->capacity * NR_PHY_IN_LOG;
1152}
1153
1154static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1155 int flags)
1156{
1157 struct nvm_geo *geo = &dev->geo;
1158 struct request_queue *bqueue = dev->q;
1159 struct request_queue *tqueue = tdisk->queue;
1160 struct pblk *pblk;
1161 int ret;
1162
Javier González3b2a3ad2018-03-30 00:05:21 +02001163 /* pblk supports 1.2 and 2.0 versions */
1164 if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1165 geo->version == NVM_OCSSD_SPEC_20)) {
Javier González7ad50392018-03-30 00:05:18 +02001166 pr_err("pblk: OCSSD version not supported (%u)\n",
1167 geo->version);
1168 return ERR_PTR(-EINVAL);
1169 }
1170
1171 if (geo->version == NVM_OCSSD_SPEC_12 && geo->dom & NVM_RSP_L2P) {
Rakesh Pandit4e76af52017-10-13 14:45:57 +02001172 pr_err("pblk: host-side L2P table not supported. (%x)\n",
Javier González7ad50392018-03-30 00:05:18 +02001173 geo->dom);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001174 return ERR_PTR(-EINVAL);
1175 }
1176
1177 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1178 if (!pblk)
1179 return ERR_PTR(-ENOMEM);
1180
1181 pblk->dev = dev;
1182 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +02001183 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg3e3a5b82017-10-13 14:46:34 +02001184 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001185
1186 spin_lock_init(&pblk->trans_lock);
1187 spin_lock_init(&pblk->lock);
1188
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001189#ifdef CONFIG_NVM_DEBUG
1190 atomic_long_set(&pblk->inflight_writes, 0);
1191 atomic_long_set(&pblk->padded_writes, 0);
1192 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001193 atomic_long_set(&pblk->req_writes, 0);
1194 atomic_long_set(&pblk->sub_writes, 0);
1195 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001196 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001197 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001198 atomic_long_set(&pblk->sync_reads, 0);
1199 atomic_long_set(&pblk->recov_writes, 0);
1200 atomic_long_set(&pblk->recov_writes, 0);
1201 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001202 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001203#endif
1204
1205 atomic_long_set(&pblk->read_failed, 0);
1206 atomic_long_set(&pblk->read_empty, 0);
1207 atomic_long_set(&pblk->read_high_ecc, 0);
1208 atomic_long_set(&pblk->read_failed_gc, 0);
1209 atomic_long_set(&pblk->write_failed, 0);
1210 atomic_long_set(&pblk->erase_failed, 0);
1211
Javier González43d47122018-03-30 00:05:09 +02001212 ret = pblk_core_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001213 if (ret) {
Javier González43d47122018-03-30 00:05:09 +02001214 pr_err("pblk: could not initialize core\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001215 goto fail;
1216 }
1217
1218 ret = pblk_lines_init(pblk);
1219 if (ret) {
1220 pr_err("pblk: could not initialize lines\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001221 goto fail_free_core;
1222 }
1223
Javier González43d47122018-03-30 00:05:09 +02001224 ret = pblk_rwb_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001225 if (ret) {
Javier González43d47122018-03-30 00:05:09 +02001226 pr_err("pblk: could not initialize write buffer\n");
1227 goto fail_free_lines;
1228 }
1229
1230 ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1231 if (ret) {
1232 pr_err("pblk: could not initialize maps\n");
1233 goto fail_free_rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001234 }
1235
1236 ret = pblk_writer_init(pblk);
1237 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001238 if (ret != -EINTR)
1239 pr_err("pblk: could not initialize write thread\n");
Javier González43d47122018-03-30 00:05:09 +02001240 goto fail_free_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001241 }
1242
1243 ret = pblk_gc_init(pblk);
1244 if (ret) {
1245 pr_err("pblk: could not initialize gc\n");
1246 goto fail_stop_writer;
1247 }
1248
1249 /* inherit the size from the underlying device */
1250 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1251 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1252
1253 blk_queue_write_cache(tqueue, true, false);
1254
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001255 tqueue->limits.discard_granularity = geo->clba * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001256 tqueue->limits.discard_alignment = 0;
1257 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001258 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001259
Javier González30d82a862018-01-05 14:16:19 +01001260 pr_info("pblk(%s): luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
1261 tdisk->disk_name,
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001262 geo->all_luns, pblk->l_mg.nr_lines,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001263 (unsigned long long)pblk->rl.nr_secs,
1264 pblk->rwb.nr_entries);
1265
1266 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001267
1268 /* Check if we need to start GC */
1269 pblk_gc_should_kick(pblk);
1270
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001271 return pblk;
1272
1273fail_stop_writer:
1274 pblk_writer_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001275fail_free_l2p:
1276 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001277fail_free_rwb:
1278 pblk_rwb_free(pblk);
1279fail_free_lines:
1280 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001281fail_free_core:
1282 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001283fail:
1284 kfree(pblk);
1285 return ERR_PTR(ret);
1286}
1287
1288/* physical block device target */
1289static struct nvm_tgt_type tt_pblk = {
1290 .name = "pblk",
1291 .version = {1, 0, 0},
1292
1293 .make_rq = pblk_make_rq,
1294 .capacity = pblk_capacity,
1295
1296 .init = pblk_init,
1297 .exit = pblk_exit,
1298
1299 .sysfs_init = pblk_sysfs_init,
1300 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001301 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001302};
1303
1304static int __init pblk_module_init(void)
1305{
NeilBrownb25d5232017-06-18 14:38:58 +10001306 int ret;
1307
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001308 ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
1309 if (ret)
1310 return ret;
NeilBrownb25d5232017-06-18 14:38:58 +10001311 ret = nvm_register_tgt_type(&tt_pblk);
1312 if (ret)
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001313 bioset_exit(&pblk_bio_set);
NeilBrownb25d5232017-06-18 14:38:58 +10001314 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001315}
1316
1317static void pblk_module_exit(void)
1318{
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001319 bioset_exit(&pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001320 nvm_unregister_tgt_type(&tt_pblk);
1321}
1322
1323module_init(pblk_module_init);
1324module_exit(pblk_module_exit);
1325MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1326MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1327MODULE_LICENSE("GPL v2");
1328MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");