blob: 8adc8ac8b03ca0dc521ea65cd313db1e1df6e93c [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
Wei Yongjun21ff1392018-06-05 09:15:12 +000023static unsigned int write_buffer_size;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +020024
25module_param(write_buffer_size, uint, 0644);
26MODULE_PARM_DESC(write_buffer_size, "number of entries in a write buffer");
27
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020028static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache,
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +020029 *pblk_w_rq_cache;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020030static DECLARE_RWSEM(pblk_lock);
Kent Overstreetb906bbb2018-05-20 18:25:50 -040031struct bio_set pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020032
33static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
34 struct bio *bio)
35{
36 int ret;
37
38 /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
39 * constraint. Writes can be of arbitrary size.
40 */
41 if (bio_data_dir(bio) == READ) {
NeilBrownaf67c312017-06-18 14:38:57 +100042 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020043 ret = pblk_submit_read(pblk, bio);
44 if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
45 bio_put(bio);
46
47 return ret;
48 }
49
50 /* Prevent deadlock in the case of a modest LUN configuration and large
51 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
52 * available for user I/O.
53 */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +020054 if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
NeilBrownaf67c312017-06-18 14:38:57 +100055 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020056
57 return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
58}
59
60static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
61{
62 struct pblk *pblk = q->queuedata;
63
64 if (bio_op(bio) == REQ_OP_DISCARD) {
65 pblk_discard(pblk, bio);
66 if (!(bio->bi_opf & REQ_PREFLUSH)) {
67 bio_endio(bio);
68 return BLK_QC_T_NONE;
69 }
70 }
71
72 switch (pblk_rw_io(q, pblk, bio)) {
73 case NVM_IO_ERR:
74 bio_io_error(bio);
75 break;
76 case NVM_IO_DONE:
77 bio_endio(bio);
78 break;
79 }
80
81 return BLK_QC_T_NONE;
82}
83
Hans Holmbergc5586192017-10-13 14:46:40 +020084static size_t pblk_trans_map_size(struct pblk *pblk)
85{
86 int entry_size = 8;
87
Javier Gonzálezbb845ae42018-03-30 00:05:19 +020088 if (pblk->addrf_len < 32)
Hans Holmbergc5586192017-10-13 14:46:40 +020089 entry_size = 4;
90
91 return entry_size * pblk->rl.nr_secs;
92}
93
Matias Bjørling880eda52018-07-13 10:48:37 +020094#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergc5586192017-10-13 14:46:40 +020095static u32 pblk_l2p_crc(struct pblk *pblk)
96{
97 size_t map_size;
98 u32 crc = ~(u32)0;
99
100 map_size = pblk_trans_map_size(pblk);
101 crc = crc32_le(crc, pblk->trans_map, map_size);
102 return crc;
103}
104#endif
105
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200106static void pblk_l2p_free(struct pblk *pblk)
107{
108 vfree(pblk->trans_map);
109}
110
Javier González43d47122018-03-30 00:05:09 +0200111static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
112{
113 struct pblk_line *line = NULL;
114
115 if (factory_init) {
116 pblk_setup_uuid(pblk);
117 } else {
118 line = pblk_recov_l2p(pblk);
119 if (IS_ERR(line)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200120 pblk_err(pblk, "could not recover l2p table\n");
Javier González43d47122018-03-30 00:05:09 +0200121 return -EFAULT;
122 }
123 }
124
Matias Bjørling880eda52018-07-13 10:48:37 +0200125#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +0200126 pblk_info(pblk, "init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Javier González43d47122018-03-30 00:05:09 +0200127#endif
128
129 /* Free full lines directly as GC has not been started yet */
130 pblk_gc_free_full_lines(pblk);
131
132 if (!line) {
133 /* Configure next line for user data */
134 line = pblk_line_get_first_data(pblk);
Javier González1d8b33e2018-06-01 15:04:16 +0200135 if (!line)
Javier González43d47122018-03-30 00:05:09 +0200136 return -EFAULT;
Javier González43d47122018-03-30 00:05:09 +0200137 }
138
139 return 0;
140}
141
142static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200143{
144 sector_t i;
145 struct ppa_addr ppa;
Hans Holmbergc5586192017-10-13 14:46:40 +0200146 size_t map_size;
Javier González1d8b33e2018-06-01 15:04:16 +0200147 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200148
Hans Holmbergc5586192017-10-13 14:46:40 +0200149 map_size = pblk_trans_map_size(pblk);
150 pblk->trans_map = vmalloc(map_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200151 if (!pblk->trans_map)
152 return -ENOMEM;
153
154 pblk_ppa_set_empty(&ppa);
155
156 for (i = 0; i < pblk->rl.nr_secs; i++)
157 pblk_trans_map_set(pblk, i, ppa);
158
Javier González1d8b33e2018-06-01 15:04:16 +0200159 ret = pblk_l2p_recover(pblk, factory_init);
160 if (ret)
161 vfree(pblk->trans_map);
162
163 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200164}
165
166static void pblk_rwb_free(struct pblk *pblk)
167{
168 if (pblk_rb_tear_down_check(&pblk->rwb))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200169 pblk_err(pblk, "write buffer error on tear down\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200170
171 pblk_rb_data_free(&pblk->rwb);
172 vfree(pblk_rb_entries_ref(&pblk->rwb));
173}
174
175static int pblk_rwb_init(struct pblk *pblk)
176{
177 struct nvm_tgt_dev *dev = pblk->dev;
178 struct nvm_geo *geo = &dev->geo;
179 struct pblk_rb_entry *entries;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200180 unsigned long nr_entries, buffer_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200181 unsigned int power_size, power_seg_sz;
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200182 int pgs_in_buffer;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200183
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200184 pgs_in_buffer = max(geo->mw_cunits, geo->ws_opt) * geo->all_luns;
185
186 if (write_buffer_size && (write_buffer_size > pgs_in_buffer))
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200187 buffer_size = write_buffer_size;
188 else
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200189 buffer_size = pgs_in_buffer;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200190
191 nr_entries = pblk_rb_calculate_size(buffer_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200192
Kees Cookfad953c2018-06-12 14:27:37 -0700193 entries = vzalloc(array_size(nr_entries, sizeof(struct pblk_rb_entry)));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200194 if (!entries)
195 return -ENOMEM;
196
197 power_size = get_count_order(nr_entries);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200198 power_seg_sz = get_count_order(geo->csecs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200199
200 return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
201}
202
203/* Minimum pages needed within a lun */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200204#define ADDR_POOL_SIZE 64
205
Matias Bjørling4e495a42018-07-13 10:48:42 +0200206static int pblk_set_addrf_12(struct pblk *pblk, struct nvm_geo *geo,
207 struct nvm_addrf_12 *dst)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208{
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200209 struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
210 int power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200211
212 /* Re-calculate channel and lun format to adapt to configuration */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200213 power_len = get_count_order(geo->num_ch);
214 if (1 << power_len != geo->num_ch) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200215 pblk_err(pblk, "supports only power-of-two channel config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200216 return -EINVAL;
217 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200218 dst->ch_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200219
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200220 power_len = get_count_order(geo->num_lun);
221 if (1 << power_len != geo->num_lun) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200222 pblk_err(pblk, "supports only power-of-two LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200223 return -EINVAL;
224 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200225 dst->lun_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200226
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200227 dst->blk_len = src->blk_len;
228 dst->pg_len = src->pg_len;
229 dst->pln_len = src->pln_len;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200230 dst->sec_len = src->sec_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200231
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200232 dst->sec_offset = 0;
233 dst->pln_offset = dst->sec_len;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200234 dst->ch_offset = dst->pln_offset + dst->pln_len;
235 dst->lun_offset = dst->ch_offset + dst->ch_len;
236 dst->pg_offset = dst->lun_offset + dst->lun_len;
237 dst->blk_offset = dst->pg_offset + dst->pg_len;
238
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200239 dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200240 dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
241 dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
242 dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
243 dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
244 dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
245
246 return dst->blk_offset + src->blk_len;
247}
248
Javier González3b2a3ad2018-03-30 00:05:21 +0200249static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
250 struct pblk_addrf *udst)
251{
252 struct nvm_addrf *src = &geo->addrf;
253
254 adst->ch_len = get_count_order(geo->num_ch);
255 adst->lun_len = get_count_order(geo->num_lun);
256 adst->chk_len = src->chk_len;
257 adst->sec_len = src->sec_len;
258
259 adst->sec_offset = 0;
260 adst->ch_offset = adst->sec_len;
261 adst->lun_offset = adst->ch_offset + adst->ch_len;
262 adst->chk_offset = adst->lun_offset + adst->lun_len;
263
264 adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
265 adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
266 adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
267 adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
268
269 udst->sec_stripe = geo->ws_opt;
270 udst->ch_stripe = geo->num_ch;
271 udst->lun_stripe = geo->num_lun;
272
273 udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
274 udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
275
276 return adst->chk_offset + adst->chk_len;
277}
278
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200279static int pblk_set_addrf(struct pblk *pblk)
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200280{
281 struct nvm_tgt_dev *dev = pblk->dev;
282 struct nvm_geo *geo = &dev->geo;
283 int mod;
284
Javier González3b2a3ad2018-03-30 00:05:21 +0200285 switch (geo->version) {
286 case NVM_OCSSD_SPEC_12:
287 div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
288 if (mod) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200289 pblk_err(pblk, "bad configuration of sectors/pages\n");
Javier González3b2a3ad2018-03-30 00:05:21 +0200290 return -EINVAL;
291 }
292
Matias Bjørling4e495a42018-07-13 10:48:42 +0200293 pblk->addrf_len = pblk_set_addrf_12(pblk, geo,
294 (void *)&pblk->addrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200295 break;
296 case NVM_OCSSD_SPEC_20:
297 pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
Matias Bjørling4e495a42018-07-13 10:48:42 +0200298 &pblk->uaddrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200299 break;
300 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200301 pblk_err(pblk, "OCSSD revision not supported (%d)\n",
Javier González3b2a3ad2018-03-30 00:05:21 +0200302 geo->version);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200303 return -EINVAL;
304 }
305
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200306 return 0;
307}
308
309static int pblk_init_global_caches(struct pblk *pblk)
310{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200311 down_write(&pblk_lock);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200312 pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200313 sizeof(struct pblk_line_ws), 0, 0, NULL);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200314 if (!pblk_ws_cache) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200315 up_write(&pblk_lock);
316 return -ENOMEM;
317 }
318
319 pblk_rec_cache = kmem_cache_create("pblk_rec",
320 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
321 if (!pblk_rec_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200322 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200323 up_write(&pblk_lock);
324 return -ENOMEM;
325 }
326
Javier González084ec9b2017-06-26 16:27:13 -0600327 pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200328 0, 0, NULL);
Javier González084ec9b2017-06-26 16:27:13 -0600329 if (!pblk_g_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200330 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200331 kmem_cache_destroy(pblk_rec_cache);
332 up_write(&pblk_lock);
333 return -ENOMEM;
334 }
335
336 pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
337 0, 0, NULL);
338 if (!pblk_w_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200339 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200340 kmem_cache_destroy(pblk_rec_cache);
Javier González084ec9b2017-06-26 16:27:13 -0600341 kmem_cache_destroy(pblk_g_rq_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200342 up_write(&pblk_lock);
343 return -ENOMEM;
344 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200345 up_write(&pblk_lock);
346
347 return 0;
348}
349
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200350static void pblk_free_global_caches(struct pblk *pblk)
351{
352 kmem_cache_destroy(pblk_ws_cache);
353 kmem_cache_destroy(pblk_rec_cache);
354 kmem_cache_destroy(pblk_g_rq_cache);
355 kmem_cache_destroy(pblk_w_rq_cache);
356}
357
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200358static int pblk_core_init(struct pblk *pblk)
359{
360 struct nvm_tgt_dev *dev = pblk->dev;
361 struct nvm_geo *geo = &dev->geo;
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400362 int ret, max_write_ppas;
Javier González43d47122018-03-30 00:05:09 +0200363
364 atomic64_set(&pblk->user_wa, 0);
365 atomic64_set(&pblk->pad_wa, 0);
366 atomic64_set(&pblk->gc_wa, 0);
367 pblk->user_rst_wa = 0;
368 pblk->pad_rst_wa = 0;
369 pblk->gc_rst_wa = 0;
370
371 atomic64_set(&pblk->nr_flush, 0);
372 pblk->nr_flush_rst = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200373
Matias Bjørling8bbd45d2018-10-09 13:11:38 +0200374 pblk->min_write_pgs = geo->ws_opt;
Javier González43d47122018-03-30 00:05:09 +0200375 max_write_ppas = pblk->min_write_pgs * geo->all_luns;
376 pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
377 pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
378
Kees Cook6396bb22018-06-12 14:03:40 -0700379 pblk->pad_dist = kcalloc(pblk->min_write_pgs - 1, sizeof(atomic64_t),
Javier González43d47122018-03-30 00:05:09 +0200380 GFP_KERNEL);
381 if (!pblk->pad_dist)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200382 return -ENOMEM;
383
Javier González43d47122018-03-30 00:05:09 +0200384 if (pblk_init_global_caches(pblk))
385 goto fail_free_pad_dist;
386
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200387 /* Internal bios can be at most the sectors signaled by the device. */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400388 ret = mempool_init_page_pool(&pblk->page_bio_pool, NVM_MAX_VLBA, 0);
389 if (ret)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200390 goto free_global_caches;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200391
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400392 ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
393 pblk_ws_cache);
394 if (ret)
Javier Gonzálezbd432412017-10-13 14:46:06 +0200395 goto free_page_bio_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200396
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400397 ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
398 pblk_rec_cache);
399 if (ret)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200400 goto free_gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200401
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400402 ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
403 pblk_g_rq_cache);
404 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200405 goto free_rec_pool;
406
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400407 ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
408 pblk_g_rq_cache);
409 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200410 goto free_r_rq_pool;
411
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400412 ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
413 pblk_w_rq_cache);
414 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200415 goto free_e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200416
Javier Gonzálezef576492017-06-26 11:57:28 +0200417 pblk->close_wq = alloc_workqueue("pblk-close-wq",
418 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
419 if (!pblk->close_wq)
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200420 goto free_w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200421
Javier Gonzálezef576492017-06-26 11:57:28 +0200422 pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
423 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
424 if (!pblk->bb_wq)
425 goto free_close_wq;
426
Javier González7bd4d372017-10-13 14:46:23 +0200427 pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
428 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
429 if (!pblk->r_end_wq)
Javier Gonzálezef576492017-06-26 11:57:28 +0200430 goto free_bb_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200431
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200432 if (pblk_set_addrf(pblk))
Javier González7bd4d372017-10-13 14:46:23 +0200433 goto free_r_end_wq;
434
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200435 INIT_LIST_HEAD(&pblk->compl_list);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200436 INIT_LIST_HEAD(&pblk->resubmit_list);
Javier González43d47122018-03-30 00:05:09 +0200437
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200438 return 0;
439
Javier González7bd4d372017-10-13 14:46:23 +0200440free_r_end_wq:
441 destroy_workqueue(pblk->r_end_wq);
Javier Gonzálezef576492017-06-26 11:57:28 +0200442free_bb_wq:
443 destroy_workqueue(pblk->bb_wq);
444free_close_wq:
445 destroy_workqueue(pblk->close_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200446free_w_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400447 mempool_exit(&pblk->w_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200448free_e_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400449 mempool_exit(&pblk->e_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200450free_r_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400451 mempool_exit(&pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200452free_rec_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400453 mempool_exit(&pblk->rec_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200454free_gen_ws_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400455 mempool_exit(&pblk->gen_ws_pool);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200456free_page_bio_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400457 mempool_exit(&pblk->page_bio_pool);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200458free_global_caches:
459 pblk_free_global_caches(pblk);
Javier González43d47122018-03-30 00:05:09 +0200460fail_free_pad_dist:
461 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200462 return -ENOMEM;
463}
464
465static void pblk_core_free(struct pblk *pblk)
466{
Javier Gonzálezef576492017-06-26 11:57:28 +0200467 if (pblk->close_wq)
468 destroy_workqueue(pblk->close_wq);
469
Javier González7bd4d372017-10-13 14:46:23 +0200470 if (pblk->r_end_wq)
471 destroy_workqueue(pblk->r_end_wq);
472
Javier Gonzálezef576492017-06-26 11:57:28 +0200473 if (pblk->bb_wq)
474 destroy_workqueue(pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200475
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400476 mempool_exit(&pblk->page_bio_pool);
477 mempool_exit(&pblk->gen_ws_pool);
478 mempool_exit(&pblk->rec_pool);
479 mempool_exit(&pblk->r_rq_pool);
480 mempool_exit(&pblk->e_rq_pool);
481 mempool_exit(&pblk->w_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200482
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200483 pblk_free_global_caches(pblk);
Javier González43d47122018-03-30 00:05:09 +0200484 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200485}
486
Javier Gonzáleze411b332018-03-30 00:04:59 +0200487static void pblk_line_mg_free(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200488{
489 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
490 int i;
491
492 kfree(l_mg->bb_template);
493 kfree(l_mg->bb_aux);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200494 kfree(l_mg->vsc_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200495
496 for (i = 0; i < PBLK_DATA_LINES; i++) {
Javier Gonzálezf680f192017-06-26 11:57:21 +0200497 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200498 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200499 kfree(l_mg->eline_meta[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200500 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200501}
502
Hans Holmberg48b8d202018-06-01 16:41:06 +0200503static void pblk_line_meta_free(struct pblk_line_mgmt *l_mg,
504 struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200505{
Hans Holmberg48b8d202018-06-01 16:41:06 +0200506 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
507
Javier Gonzáleze411b332018-03-30 00:04:59 +0200508 kfree(line->blk_bitmap);
509 kfree(line->erase_bitmap);
Javier González32ef9412018-03-30 00:05:20 +0200510 kfree(line->chks);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200511
512 pblk_mfree(w_err_gc->lba_list, l_mg->emeta_alloc_type);
513 kfree(w_err_gc);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200514}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200515
Javier Gonzáleze411b332018-03-30 00:04:59 +0200516static void pblk_lines_free(struct pblk *pblk)
517{
518 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
519 struct pblk_line *line;
520 int i;
521
522 spin_lock(&l_mg->free_lock);
523 for (i = 0; i < l_mg->nr_lines; i++) {
524 line = &pblk->lines[i];
525
Javier González8e55c072018-06-01 15:04:22 +0200526 pblk_line_free(line);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200527 pblk_line_meta_free(l_mg, line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200528 }
529 spin_unlock(&l_mg->free_lock);
Javier González43d47122018-03-30 00:05:09 +0200530
531 pblk_line_mg_free(pblk);
532
533 kfree(pblk->luns);
534 kfree(pblk->lines);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200535}
536
Javier González43d47122018-03-30 00:05:09 +0200537static int pblk_luns_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200538{
539 struct nvm_tgt_dev *dev = pblk->dev;
540 struct nvm_geo *geo = &dev->geo;
541 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200542 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200543
544 /* TODO: Implement unbalanced LUN support */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200545 if (geo->num_lun < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200546 pblk_err(pblk, "unbalanced LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200547 return -EINVAL;
548 }
549
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100550 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
551 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200552 if (!pblk->luns)
553 return -ENOMEM;
554
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100555 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200556 /* Stripe across channels */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200557 int ch = i % geo->num_ch;
558 int lun_raw = i / geo->num_ch;
559 int lunid = lun_raw + ch * geo->num_lun;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200560
561 rlun = &pblk->luns[i];
Javier González43d47122018-03-30 00:05:09 +0200562 rlun->bppa = dev->luns[lunid];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200563
564 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200565 }
566
567 return 0;
568}
569
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200570/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200571static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200572{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200573 struct pblk_line_meta *lm = &pblk->lm;
574 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
575 struct nvm_tgt_dev *dev = pblk->dev;
576 struct nvm_geo *geo = &dev->geo;
577
578 /* Round to sector size so that lba_list starts on its own sector */
579 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200580 sizeof(struct line_emeta) + lm->blk_bitmap_len +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200581 sizeof(struct wa_counters), geo->csecs);
582 lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200583
584 /* Round to sector size so that vsc_list starts on its own sector */
585 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
586 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200587 geo->csecs);
588 lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200589
590 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200591 geo->csecs);
592 lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200593
594 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
595
596 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200597}
598
599static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
600{
601 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100602 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
603 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200604 struct nvm_geo *geo = &dev->geo;
605 sector_t provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100606 int sec_meta, blk_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607
Javier Gonzáleze5392732018-01-05 14:16:14 +0100608 if (geo->op == NVM_TARGET_DEFAULT_OP)
609 pblk->op = PBLK_DEFAULT_OP;
610 else
611 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200612
613 provisioned = nr_free_blks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100614 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200615 sector_div(provisioned, 100);
616
Javier Gonzáleza7689932018-01-05 14:16:13 +0100617 pblk->op_blks = nr_free_blks - provisioned;
618
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200619 /* Internally pblk manages all free blocks, but all calculations based
620 * on user capacity consider only provisioned blocks
621 */
622 pblk->rl.total_blocks = nr_free_blks;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200623 pblk->rl.nr_secs = nr_free_blks * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100624
625 /* Consider sectors used for metadata */
626 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200627 blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100628
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200629 pblk->capacity = (provisioned - blk_meta) * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100630
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200631 atomic_set(&pblk->rl.free_blocks, nr_free_blks);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100632 atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200633}
634
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200635static int pblk_setup_line_meta_chk(struct pblk *pblk, struct pblk_line *line,
Javier González32ef9412018-03-30 00:05:20 +0200636 struct nvm_chk_meta *meta)
637{
638 struct nvm_tgt_dev *dev = pblk->dev;
639 struct nvm_geo *geo = &dev->geo;
640 struct pblk_line_meta *lm = &pblk->lm;
641 int i, nr_bad_chks = 0;
642
643 for (i = 0; i < lm->blk_per_line; i++) {
644 struct pblk_lun *rlun = &pblk->luns[i];
645 struct nvm_chk_meta *chunk;
646 struct nvm_chk_meta *chunk_meta;
647 struct ppa_addr ppa;
648 int pos;
649
650 ppa = rlun->bppa;
651 pos = pblk_ppa_to_pos(geo, ppa);
652 chunk = &line->chks[pos];
653
654 ppa.m.chk = line->id;
655 chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
656
657 chunk->state = chunk_meta->state;
658 chunk->type = chunk_meta->type;
659 chunk->wi = chunk_meta->wi;
660 chunk->slba = chunk_meta->slba;
661 chunk->cnlb = chunk_meta->cnlb;
662 chunk->wp = chunk_meta->wp;
663
Javier González32ef9412018-03-30 00:05:20 +0200664 if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
665 WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
666 continue;
667 }
668
Javier González6f9c9602018-06-01 15:04:23 +0200669 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
670 continue;
671
Javier González32ef9412018-03-30 00:05:20 +0200672 set_bit(pos, line->blk_bitmap);
673 nr_bad_chks++;
674 }
675
676 return nr_bad_chks;
677}
678
679static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
680 void *chunk_meta, int line_id)
681{
Javier González32ef9412018-03-30 00:05:20 +0200682 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
683 struct pblk_line_meta *lm = &pblk->lm;
684 long nr_bad_chks, chk_in_line;
685
686 line->pblk = pblk;
687 line->id = line_id;
688 line->type = PBLK_LINETYPE_FREE;
689 line->state = PBLK_LINESTATE_NEW;
690 line->gc_group = PBLK_LINEGC_NONE;
691 line->vsc = &l_mg->vsc_list[line_id];
692 spin_lock_init(&line->lock);
693
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200694 nr_bad_chks = pblk_setup_line_meta_chk(pblk, line, chunk_meta);
Javier González32ef9412018-03-30 00:05:20 +0200695
696 chk_in_line = lm->blk_per_line - nr_bad_chks;
697 if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
698 chk_in_line < lm->min_blk_line) {
699 line->state = PBLK_LINESTATE_BAD;
700 list_add_tail(&line->list, &l_mg->bad_list);
701 return 0;
702 }
703
704 atomic_set(&line->blk_in_line, chk_in_line);
705 list_add_tail(&line->list, &l_mg->free_list);
706 l_mg->nr_free_lines++;
707
708 return chk_in_line;
709}
710
711static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200712{
Javier González43d47122018-03-30 00:05:09 +0200713 struct pblk_line_meta *lm = &pblk->lm;
714
715 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
716 if (!line->blk_bitmap)
717 return -ENOMEM;
718
719 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200720 if (!line->erase_bitmap)
721 goto free_blk_bitmap;
722
Javier González43d47122018-03-30 00:05:09 +0200723
Kees Cook6da2ec52018-06-12 13:55:00 -0700724 line->chks = kmalloc_array(lm->blk_per_line,
725 sizeof(struct nvm_chk_meta), GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200726 if (!line->chks)
727 goto free_erase_bitmap;
728
729 line->w_err_gc = kzalloc(sizeof(struct pblk_w_err_gc), GFP_KERNEL);
730 if (!line->w_err_gc)
731 goto free_chks;
Javier González43d47122018-03-30 00:05:09 +0200732
733 return 0;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200734
735free_chks:
736 kfree(line->chks);
737free_erase_bitmap:
738 kfree(line->erase_bitmap);
739free_blk_bitmap:
740 kfree(line->blk_bitmap);
741 return -ENOMEM;
Javier González43d47122018-03-30 00:05:09 +0200742}
743
744static int pblk_line_mg_init(struct pblk *pblk)
745{
746 struct nvm_tgt_dev *dev = pblk->dev;
747 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200748 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
749 struct pblk_line_meta *lm = &pblk->lm;
Javier González43d47122018-03-30 00:05:09 +0200750 int i, bb_distance;
751
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200752 l_mg->nr_lines = geo->num_chk;
Javier González43d47122018-03-30 00:05:09 +0200753 l_mg->log_line = l_mg->data_line = NULL;
754 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
755 l_mg->nr_free_lines = 0;
756 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
757
758 INIT_LIST_HEAD(&l_mg->free_list);
759 INIT_LIST_HEAD(&l_mg->corrupt_list);
760 INIT_LIST_HEAD(&l_mg->bad_list);
761 INIT_LIST_HEAD(&l_mg->gc_full_list);
762 INIT_LIST_HEAD(&l_mg->gc_high_list);
763 INIT_LIST_HEAD(&l_mg->gc_mid_list);
764 INIT_LIST_HEAD(&l_mg->gc_low_list);
765 INIT_LIST_HEAD(&l_mg->gc_empty_list);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200766 INIT_LIST_HEAD(&l_mg->gc_werr_list);
Javier González43d47122018-03-30 00:05:09 +0200767
768 INIT_LIST_HEAD(&l_mg->emeta_list);
769
Hans Holmberg48b8d202018-06-01 16:41:06 +0200770 l_mg->gc_lists[0] = &l_mg->gc_werr_list;
771 l_mg->gc_lists[1] = &l_mg->gc_high_list;
772 l_mg->gc_lists[2] = &l_mg->gc_mid_list;
773 l_mg->gc_lists[3] = &l_mg->gc_low_list;
Javier González43d47122018-03-30 00:05:09 +0200774
775 spin_lock_init(&l_mg->free_lock);
776 spin_lock_init(&l_mg->close_lock);
777 spin_lock_init(&l_mg->gc_lock);
778
779 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
780 if (!l_mg->vsc_list)
781 goto fail;
782
783 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
784 if (!l_mg->bb_template)
785 goto fail_free_vsc_list;
786
787 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
788 if (!l_mg->bb_aux)
789 goto fail_free_bb_template;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200790
791 /* smeta is always small enough to fit on a kmalloc memory allocation,
792 * emeta depends on the number of LUNs allocated to the pblk instance
793 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200794 for (i = 0; i < PBLK_DATA_LINES; i++) {
795 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
796 if (!l_mg->sline_meta[i])
797 goto fail_free_smeta;
798 }
799
800 /* emeta allocates three different buffers for managing metadata with
801 * in-memory and in-media layouts
802 */
803 for (i = 0; i < PBLK_DATA_LINES; i++) {
804 struct pblk_emeta *emeta;
805
806 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
807 if (!emeta)
808 goto fail_free_emeta;
809
810 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
811 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
812
813 emeta->buf = vmalloc(lm->emeta_len[0]);
814 if (!emeta->buf) {
815 kfree(emeta);
816 goto fail_free_emeta;
817 }
818
819 emeta->nr_entries = lm->emeta_sec[0];
820 l_mg->eline_meta[i] = emeta;
821 } else {
822 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
823
824 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
825 if (!emeta->buf) {
826 kfree(emeta);
827 goto fail_free_emeta;
828 }
829
830 emeta->nr_entries = lm->emeta_sec[0];
831 l_mg->eline_meta[i] = emeta;
832 }
833 }
834
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200835 for (i = 0; i < l_mg->nr_lines; i++)
836 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
837
Javier González43d47122018-03-30 00:05:09 +0200838 bb_distance = (geo->all_luns) * geo->ws_opt;
839 for (i = 0; i < lm->sec_per_line; i += bb_distance)
840 bitmap_set(l_mg->bb_template, i, geo->ws_opt);
841
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200842 return 0;
843
844fail_free_emeta:
845 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200846 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
847 vfree(l_mg->eline_meta[i]->buf);
848 else
849 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200850 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200851 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200852fail_free_smeta:
853 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200854 kfree(l_mg->sline_meta[i]);
Javier González43d47122018-03-30 00:05:09 +0200855 kfree(l_mg->bb_aux);
856fail_free_bb_template:
857 kfree(l_mg->bb_template);
858fail_free_vsc_list:
859 kfree(l_mg->vsc_list);
860fail:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200861 return -ENOMEM;
862}
863
Javier González43d47122018-03-30 00:05:09 +0200864static int pblk_line_meta_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200865{
866 struct nvm_tgt_dev *dev = pblk->dev;
867 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200868 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200869 unsigned int smeta_len, emeta_len;
Javier González43d47122018-03-30 00:05:09 +0200870 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200871
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200872 lm->sec_per_line = geo->clba * geo->all_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100873 lm->blk_per_line = geo->all_luns;
874 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200875 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100876 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200877 lm->mid_thrs = lm->sec_per_line / 2;
878 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100879 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200880
881 /* Calculate necessary pages for smeta. See comment over struct
882 * line_smeta definition
883 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200884 i = 1;
885add_smeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200886 lm->smeta_sec = i * geo->ws_opt;
887 lm->smeta_len = lm->smeta_sec * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200888
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200889 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200890 if (smeta_len > lm->smeta_len) {
891 i++;
892 goto add_smeta_page;
893 }
894
895 /* Calculate necessary pages for emeta. See comment over struct
896 * line_emeta definition
897 */
898 i = 1;
899add_emeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200900 lm->emeta_sec[0] = i * geo->ws_opt;
901 lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200902
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200903 emeta_len = calc_emeta_len(pblk);
904 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200905 i++;
906 goto add_emeta_page;
907 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200908
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100909 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +0200910
911 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100912 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +0200913 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200914 lm->emeta_sec[0], geo->clba);
Javier González21d22872017-10-13 14:46:26 +0200915
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200916 if (lm->min_blk_line > lm->blk_per_line) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200917 pblk_err(pblk, "config. not supported. Min. LUN in line:%d\n",
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200918 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200919 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200920 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200921
Javier González43d47122018-03-30 00:05:09 +0200922 return 0;
923}
924
925static int pblk_lines_init(struct pblk *pblk)
926{
927 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González43d47122018-03-30 00:05:09 +0200928 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +0200929 void *chunk_meta;
930 long nr_free_chks = 0;
Javier González43d47122018-03-30 00:05:09 +0200931 int i, ret;
932
933 ret = pblk_line_meta_init(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200934 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200935 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200936
Javier González43d47122018-03-30 00:05:09 +0200937 ret = pblk_line_mg_init(pblk);
938 if (ret)
939 return ret;
940
941 ret = pblk_luns_init(pblk);
942 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200943 goto fail_free_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200944
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200945 chunk_meta = pblk_get_chunk_meta(pblk);
Javier González32ef9412018-03-30 00:05:20 +0200946 if (IS_ERR(chunk_meta)) {
947 ret = PTR_ERR(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +0200948 goto fail_free_luns;
949 }
950
951 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
952 GFP_KERNEL);
953 if (!pblk->lines) {
954 ret = -ENOMEM;
Javier González32ef9412018-03-30 00:05:20 +0200955 goto fail_free_chunk_meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200956 }
957
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200958 for (i = 0; i < l_mg->nr_lines; i++) {
959 line = &pblk->lines[i];
960
Javier González32ef9412018-03-30 00:05:20 +0200961 ret = pblk_alloc_line_meta(pblk, line);
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200962 if (ret)
Javier González43d47122018-03-30 00:05:09 +0200963 goto fail_free_lines;
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200964
Javier González32ef9412018-03-30 00:05:20 +0200965 nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200966 }
967
Javier González2deeefc2018-06-01 15:04:15 +0200968 if (!nr_free_chks) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200969 pblk_err(pblk, "too many bad blocks prevent for sane instance\n");
Javier González2deeefc2018-06-01 15:04:15 +0200970 return -EINTR;
971 }
972
Javier González32ef9412018-03-30 00:05:20 +0200973 pblk_set_provision(pblk, nr_free_chks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200974
Javier González32ef9412018-03-30 00:05:20 +0200975 kfree(chunk_meta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200976 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200977
Javier González43d47122018-03-30 00:05:09 +0200978fail_free_lines:
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200979 while (--i >= 0)
Hans Holmberg48b8d202018-06-01 16:41:06 +0200980 pblk_line_meta_free(l_mg, &pblk->lines[i]);
Javier González43d47122018-03-30 00:05:09 +0200981 kfree(pblk->lines);
Javier González32ef9412018-03-30 00:05:20 +0200982fail_free_chunk_meta:
983 kfree(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +0200984fail_free_luns:
985 kfree(pblk->luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200986fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +0200987 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200988
989 return ret;
990}
991
992static int pblk_writer_init(struct pblk *pblk)
993{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200994 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
995 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +0100996 int err = PTR_ERR(pblk->writer_ts);
997
998 if (err != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +0200999 pblk_err(pblk, "could not allocate writer kthread (%d)\n",
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001000 err);
1001 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001002 }
1003
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001004 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1005 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1006
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001007 return 0;
1008}
1009
1010static void pblk_writer_stop(struct pblk *pblk)
1011{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001012 /* The pipeline must be stopped and the write buffer emptied before the
1013 * write thread is stopped
1014 */
1015 WARN(pblk_rb_read_count(&pblk->rwb),
1016 "Stopping not fully persisted write buffer\n");
1017
1018 WARN(pblk_rb_sync_count(&pblk->rwb),
1019 "Stopping not fully synced write buffer\n");
1020
Hans Holmberg7be970b2018-03-30 00:04:56 +02001021 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001022 if (pblk->writer_ts)
1023 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001024}
1025
1026static void pblk_free(struct pblk *pblk)
1027{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001028 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001029 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001030 pblk_rwb_free(pblk);
1031 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001032
1033 kfree(pblk);
1034}
1035
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001036static void pblk_tear_down(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001037{
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001038 if (graceful)
1039 __pblk_pipeline_flush(pblk);
1040 __pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001041 pblk_writer_stop(pblk);
1042 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001043 pblk_rl_free(&pblk->rl);
1044
Matias Bjørling4e495a42018-07-13 10:48:42 +02001045 pblk_debug(pblk, "consistent tear down (graceful:%d)\n", graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001046}
1047
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001048static void pblk_exit(void *private, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001049{
1050 struct pblk *pblk = private;
1051
1052 down_write(&pblk_lock);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001053 pblk_gc_exit(pblk, graceful);
1054 pblk_tear_down(pblk, graceful);
Hans Holmbergc5586192017-10-13 14:46:40 +02001055
Matias Bjørling880eda52018-07-13 10:48:37 +02001056#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001057 pblk_info(pblk, "exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Hans Holmbergc5586192017-10-13 14:46:40 +02001058#endif
1059
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001060 pblk_free(pblk);
1061 up_write(&pblk_lock);
1062}
1063
1064static sector_t pblk_capacity(void *private)
1065{
1066 struct pblk *pblk = private;
1067
1068 return pblk->capacity * NR_PHY_IN_LOG;
1069}
1070
1071static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1072 int flags)
1073{
1074 struct nvm_geo *geo = &dev->geo;
1075 struct request_queue *bqueue = dev->q;
1076 struct request_queue *tqueue = tdisk->queue;
1077 struct pblk *pblk;
1078 int ret;
1079
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001080 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1081 if (!pblk)
1082 return ERR_PTR(-ENOMEM);
1083
1084 pblk->dev = dev;
1085 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +02001086 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg3e3a5b82017-10-13 14:46:34 +02001087 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001088
Matias Bjørling4e495a42018-07-13 10:48:42 +02001089 if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1090 geo->version == NVM_OCSSD_SPEC_20)) {
1091 pblk_err(pblk, "OCSSD version not supported (%u)\n",
1092 geo->version);
1093 kfree(pblk);
1094 return ERR_PTR(-EINVAL);
1095 }
1096
Hans Holmberg6a3abf52018-06-01 16:41:05 +02001097 spin_lock_init(&pblk->resubmit_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001098 spin_lock_init(&pblk->trans_lock);
1099 spin_lock_init(&pblk->lock);
1100
Matias Bjørling880eda52018-07-13 10:48:37 +02001101#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001102 atomic_long_set(&pblk->inflight_writes, 0);
1103 atomic_long_set(&pblk->padded_writes, 0);
1104 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001105 atomic_long_set(&pblk->req_writes, 0);
1106 atomic_long_set(&pblk->sub_writes, 0);
1107 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001108 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001109 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001110 atomic_long_set(&pblk->sync_reads, 0);
1111 atomic_long_set(&pblk->recov_writes, 0);
1112 atomic_long_set(&pblk->recov_writes, 0);
1113 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001114 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001115#endif
1116
1117 atomic_long_set(&pblk->read_failed, 0);
1118 atomic_long_set(&pblk->read_empty, 0);
1119 atomic_long_set(&pblk->read_high_ecc, 0);
1120 atomic_long_set(&pblk->read_failed_gc, 0);
1121 atomic_long_set(&pblk->write_failed, 0);
1122 atomic_long_set(&pblk->erase_failed, 0);
1123
Javier González43d47122018-03-30 00:05:09 +02001124 ret = pblk_core_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001125 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001126 pblk_err(pblk, "could not initialize core\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001127 goto fail;
1128 }
1129
1130 ret = pblk_lines_init(pblk);
1131 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001132 pblk_err(pblk, "could not initialize lines\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001133 goto fail_free_core;
1134 }
1135
Javier González43d47122018-03-30 00:05:09 +02001136 ret = pblk_rwb_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001137 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001138 pblk_err(pblk, "could not initialize write buffer\n");
Javier González43d47122018-03-30 00:05:09 +02001139 goto fail_free_lines;
1140 }
1141
1142 ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1143 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001144 pblk_err(pblk, "could not initialize maps\n");
Javier González43d47122018-03-30 00:05:09 +02001145 goto fail_free_rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001146 }
1147
1148 ret = pblk_writer_init(pblk);
1149 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001150 if (ret != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001151 pblk_err(pblk, "could not initialize write thread\n");
Javier González43d47122018-03-30 00:05:09 +02001152 goto fail_free_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001153 }
1154
1155 ret = pblk_gc_init(pblk);
1156 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001157 pblk_err(pblk, "could not initialize gc\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001158 goto fail_stop_writer;
1159 }
1160
1161 /* inherit the size from the underlying device */
1162 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1163 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1164
1165 blk_queue_write_cache(tqueue, true, false);
1166
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001167 tqueue->limits.discard_granularity = geo->clba * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001168 tqueue->limits.discard_alignment = 0;
1169 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001170 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001171
Matias Bjørling4e495a42018-07-13 10:48:42 +02001172 pblk_info(pblk, "luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001173 geo->all_luns, pblk->l_mg.nr_lines,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001174 (unsigned long long)pblk->rl.nr_secs,
1175 pblk->rwb.nr_entries);
1176
1177 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001178
1179 /* Check if we need to start GC */
1180 pblk_gc_should_kick(pblk);
1181
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001182 return pblk;
1183
1184fail_stop_writer:
1185 pblk_writer_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001186fail_free_l2p:
1187 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001188fail_free_rwb:
1189 pblk_rwb_free(pblk);
1190fail_free_lines:
1191 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001192fail_free_core:
1193 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001194fail:
1195 kfree(pblk);
1196 return ERR_PTR(ret);
1197}
1198
1199/* physical block device target */
1200static struct nvm_tgt_type tt_pblk = {
1201 .name = "pblk",
1202 .version = {1, 0, 0},
1203
1204 .make_rq = pblk_make_rq,
1205 .capacity = pblk_capacity,
1206
1207 .init = pblk_init,
1208 .exit = pblk_exit,
1209
1210 .sysfs_init = pblk_sysfs_init,
1211 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001212 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001213};
1214
1215static int __init pblk_module_init(void)
1216{
NeilBrownb25d5232017-06-18 14:38:58 +10001217 int ret;
1218
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001219 ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
1220 if (ret)
1221 return ret;
NeilBrownb25d5232017-06-18 14:38:58 +10001222 ret = nvm_register_tgt_type(&tt_pblk);
1223 if (ret)
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001224 bioset_exit(&pblk_bio_set);
NeilBrownb25d5232017-06-18 14:38:58 +10001225 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001226}
1227
1228static void pblk_module_exit(void)
1229{
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001230 bioset_exit(&pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001231 nvm_unregister_tgt_type(&tt_pblk);
1232}
1233
1234module_init(pblk_module_init);
1235module_exit(pblk_module_exit);
1236MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1237MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1238MODULE_LICENSE("GPL v2");
1239MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");