blob: 76a4a271b9cfcc86dd69d12b44c87e534279b9c8 [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 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200501
502 mempool_destroy(l_mg->bitmap_pool);
503 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200504}
505
Hans Holmberg48b8d202018-06-01 16:41:06 +0200506static void pblk_line_meta_free(struct pblk_line_mgmt *l_mg,
507 struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200508{
Hans Holmberg48b8d202018-06-01 16:41:06 +0200509 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
510
Javier Gonzáleze411b332018-03-30 00:04:59 +0200511 kfree(line->blk_bitmap);
512 kfree(line->erase_bitmap);
Javier González32ef9412018-03-30 00:05:20 +0200513 kfree(line->chks);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200514
515 pblk_mfree(w_err_gc->lba_list, l_mg->emeta_alloc_type);
516 kfree(w_err_gc);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200517}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200518
Javier Gonzáleze411b332018-03-30 00:04:59 +0200519static void pblk_lines_free(struct pblk *pblk)
520{
521 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
522 struct pblk_line *line;
523 int i;
524
525 spin_lock(&l_mg->free_lock);
526 for (i = 0; i < l_mg->nr_lines; i++) {
527 line = &pblk->lines[i];
528
Javier González8e55c072018-06-01 15:04:22 +0200529 pblk_line_free(line);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200530 pblk_line_meta_free(l_mg, line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200531 }
532 spin_unlock(&l_mg->free_lock);
Javier González43d47122018-03-30 00:05:09 +0200533
534 pblk_line_mg_free(pblk);
535
536 kfree(pblk->luns);
537 kfree(pblk->lines);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200538}
539
Javier González43d47122018-03-30 00:05:09 +0200540static int pblk_luns_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200541{
542 struct nvm_tgt_dev *dev = pblk->dev;
543 struct nvm_geo *geo = &dev->geo;
544 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200545 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200546
547 /* TODO: Implement unbalanced LUN support */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200548 if (geo->num_lun < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200549 pblk_err(pblk, "unbalanced LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200550 return -EINVAL;
551 }
552
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100553 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
554 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200555 if (!pblk->luns)
556 return -ENOMEM;
557
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100558 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200559 /* Stripe across channels */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200560 int ch = i % geo->num_ch;
561 int lun_raw = i / geo->num_ch;
562 int lunid = lun_raw + ch * geo->num_lun;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200563
564 rlun = &pblk->luns[i];
Javier González43d47122018-03-30 00:05:09 +0200565 rlun->bppa = dev->luns[lunid];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200566
567 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200568 }
569
570 return 0;
571}
572
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200573/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200574static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200575{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200576 struct pblk_line_meta *lm = &pblk->lm;
577 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
578 struct nvm_tgt_dev *dev = pblk->dev;
579 struct nvm_geo *geo = &dev->geo;
580
581 /* Round to sector size so that lba_list starts on its own sector */
582 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200583 sizeof(struct line_emeta) + lm->blk_bitmap_len +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200584 sizeof(struct wa_counters), geo->csecs);
585 lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200586
587 /* Round to sector size so that vsc_list starts on its own sector */
588 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
589 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200590 geo->csecs);
591 lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200592
593 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200594 geo->csecs);
595 lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200596
597 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
598
599 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200600}
601
602static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
603{
604 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100605 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
606 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607 struct nvm_geo *geo = &dev->geo;
608 sector_t provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100609 int sec_meta, blk_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200610
Javier Gonzáleze5392732018-01-05 14:16:14 +0100611 if (geo->op == NVM_TARGET_DEFAULT_OP)
612 pblk->op = PBLK_DEFAULT_OP;
613 else
614 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200615
616 provisioned = nr_free_blks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100617 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200618 sector_div(provisioned, 100);
619
Javier Gonzáleza7689932018-01-05 14:16:13 +0100620 pblk->op_blks = nr_free_blks - provisioned;
621
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200622 /* Internally pblk manages all free blocks, but all calculations based
623 * on user capacity consider only provisioned blocks
624 */
625 pblk->rl.total_blocks = nr_free_blks;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200626 pblk->rl.nr_secs = nr_free_blks * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100627
628 /* Consider sectors used for metadata */
629 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200630 blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100631
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200632 pblk->capacity = (provisioned - blk_meta) * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100633
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200634 atomic_set(&pblk->rl.free_blocks, nr_free_blks);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100635 atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200636}
637
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200638static int pblk_setup_line_meta_chk(struct pblk *pblk, struct pblk_line *line,
Javier González32ef9412018-03-30 00:05:20 +0200639 struct nvm_chk_meta *meta)
640{
641 struct nvm_tgt_dev *dev = pblk->dev;
642 struct nvm_geo *geo = &dev->geo;
643 struct pblk_line_meta *lm = &pblk->lm;
644 int i, nr_bad_chks = 0;
645
646 for (i = 0; i < lm->blk_per_line; i++) {
647 struct pblk_lun *rlun = &pblk->luns[i];
648 struct nvm_chk_meta *chunk;
649 struct nvm_chk_meta *chunk_meta;
650 struct ppa_addr ppa;
651 int pos;
652
653 ppa = rlun->bppa;
654 pos = pblk_ppa_to_pos(geo, ppa);
655 chunk = &line->chks[pos];
656
657 ppa.m.chk = line->id;
658 chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
659
660 chunk->state = chunk_meta->state;
661 chunk->type = chunk_meta->type;
662 chunk->wi = chunk_meta->wi;
663 chunk->slba = chunk_meta->slba;
664 chunk->cnlb = chunk_meta->cnlb;
665 chunk->wp = chunk_meta->wp;
666
Javier González32ef9412018-03-30 00:05:20 +0200667 if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
668 WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
669 continue;
670 }
671
Javier González6f9c9602018-06-01 15:04:23 +0200672 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
673 continue;
674
Javier González32ef9412018-03-30 00:05:20 +0200675 set_bit(pos, line->blk_bitmap);
676 nr_bad_chks++;
677 }
678
679 return nr_bad_chks;
680}
681
682static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
683 void *chunk_meta, int line_id)
684{
Javier González32ef9412018-03-30 00:05:20 +0200685 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
686 struct pblk_line_meta *lm = &pblk->lm;
687 long nr_bad_chks, chk_in_line;
688
689 line->pblk = pblk;
690 line->id = line_id;
691 line->type = PBLK_LINETYPE_FREE;
692 line->state = PBLK_LINESTATE_NEW;
693 line->gc_group = PBLK_LINEGC_NONE;
694 line->vsc = &l_mg->vsc_list[line_id];
695 spin_lock_init(&line->lock);
696
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200697 nr_bad_chks = pblk_setup_line_meta_chk(pblk, line, chunk_meta);
Javier González32ef9412018-03-30 00:05:20 +0200698
699 chk_in_line = lm->blk_per_line - nr_bad_chks;
700 if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
701 chk_in_line < lm->min_blk_line) {
702 line->state = PBLK_LINESTATE_BAD;
703 list_add_tail(&line->list, &l_mg->bad_list);
704 return 0;
705 }
706
707 atomic_set(&line->blk_in_line, chk_in_line);
708 list_add_tail(&line->list, &l_mg->free_list);
709 l_mg->nr_free_lines++;
710
711 return chk_in_line;
712}
713
714static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200715{
Javier González43d47122018-03-30 00:05:09 +0200716 struct pblk_line_meta *lm = &pblk->lm;
717
718 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
719 if (!line->blk_bitmap)
720 return -ENOMEM;
721
722 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200723 if (!line->erase_bitmap)
724 goto free_blk_bitmap;
725
Javier González43d47122018-03-30 00:05:09 +0200726
Kees Cook6da2ec52018-06-12 13:55:00 -0700727 line->chks = kmalloc_array(lm->blk_per_line,
728 sizeof(struct nvm_chk_meta), GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200729 if (!line->chks)
730 goto free_erase_bitmap;
731
732 line->w_err_gc = kzalloc(sizeof(struct pblk_w_err_gc), GFP_KERNEL);
733 if (!line->w_err_gc)
734 goto free_chks;
Javier González43d47122018-03-30 00:05:09 +0200735
736 return 0;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200737
738free_chks:
739 kfree(line->chks);
740free_erase_bitmap:
741 kfree(line->erase_bitmap);
742free_blk_bitmap:
743 kfree(line->blk_bitmap);
744 return -ENOMEM;
Javier González43d47122018-03-30 00:05:09 +0200745}
746
747static int pblk_line_mg_init(struct pblk *pblk)
748{
749 struct nvm_tgt_dev *dev = pblk->dev;
750 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200751 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
752 struct pblk_line_meta *lm = &pblk->lm;
Javier González43d47122018-03-30 00:05:09 +0200753 int i, bb_distance;
754
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200755 l_mg->nr_lines = geo->num_chk;
Javier González43d47122018-03-30 00:05:09 +0200756 l_mg->log_line = l_mg->data_line = NULL;
757 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
758 l_mg->nr_free_lines = 0;
759 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
760
761 INIT_LIST_HEAD(&l_mg->free_list);
762 INIT_LIST_HEAD(&l_mg->corrupt_list);
763 INIT_LIST_HEAD(&l_mg->bad_list);
764 INIT_LIST_HEAD(&l_mg->gc_full_list);
765 INIT_LIST_HEAD(&l_mg->gc_high_list);
766 INIT_LIST_HEAD(&l_mg->gc_mid_list);
767 INIT_LIST_HEAD(&l_mg->gc_low_list);
768 INIT_LIST_HEAD(&l_mg->gc_empty_list);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200769 INIT_LIST_HEAD(&l_mg->gc_werr_list);
Javier González43d47122018-03-30 00:05:09 +0200770
771 INIT_LIST_HEAD(&l_mg->emeta_list);
772
Hans Holmberg48b8d202018-06-01 16:41:06 +0200773 l_mg->gc_lists[0] = &l_mg->gc_werr_list;
774 l_mg->gc_lists[1] = &l_mg->gc_high_list;
775 l_mg->gc_lists[2] = &l_mg->gc_mid_list;
776 l_mg->gc_lists[3] = &l_mg->gc_low_list;
Javier González43d47122018-03-30 00:05:09 +0200777
778 spin_lock_init(&l_mg->free_lock);
779 spin_lock_init(&l_mg->close_lock);
780 spin_lock_init(&l_mg->gc_lock);
781
782 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
783 if (!l_mg->vsc_list)
784 goto fail;
785
786 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
787 if (!l_mg->bb_template)
788 goto fail_free_vsc_list;
789
790 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
791 if (!l_mg->bb_aux)
792 goto fail_free_bb_template;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200793
794 /* smeta is always small enough to fit on a kmalloc memory allocation,
795 * emeta depends on the number of LUNs allocated to the pblk instance
796 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200797 for (i = 0; i < PBLK_DATA_LINES; i++) {
798 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
799 if (!l_mg->sline_meta[i])
800 goto fail_free_smeta;
801 }
802
Hans Holmberg53d82db2018-10-09 13:11:47 +0200803 l_mg->bitmap_cache = kmem_cache_create("pblk_lm_bitmap",
804 lm->sec_bitmap_len, 0, 0, NULL);
805 if (!l_mg->bitmap_cache)
806 goto fail_free_smeta;
807
808 /* the bitmap pool is used for both valid and map bitmaps */
809 l_mg->bitmap_pool = mempool_create_slab_pool(PBLK_DATA_LINES * 2,
810 l_mg->bitmap_cache);
811 if (!l_mg->bitmap_pool)
812 goto fail_destroy_bitmap_cache;
813
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200814 /* emeta allocates three different buffers for managing metadata with
815 * in-memory and in-media layouts
816 */
817 for (i = 0; i < PBLK_DATA_LINES; i++) {
818 struct pblk_emeta *emeta;
819
820 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
821 if (!emeta)
822 goto fail_free_emeta;
823
824 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
825 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
826
827 emeta->buf = vmalloc(lm->emeta_len[0]);
828 if (!emeta->buf) {
829 kfree(emeta);
830 goto fail_free_emeta;
831 }
832
833 emeta->nr_entries = lm->emeta_sec[0];
834 l_mg->eline_meta[i] = emeta;
835 } else {
836 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
837
838 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
839 if (!emeta->buf) {
840 kfree(emeta);
841 goto fail_free_emeta;
842 }
843
844 emeta->nr_entries = lm->emeta_sec[0];
845 l_mg->eline_meta[i] = emeta;
846 }
847 }
848
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200849 for (i = 0; i < l_mg->nr_lines; i++)
850 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
851
Javier González43d47122018-03-30 00:05:09 +0200852 bb_distance = (geo->all_luns) * geo->ws_opt;
853 for (i = 0; i < lm->sec_per_line; i += bb_distance)
854 bitmap_set(l_mg->bb_template, i, geo->ws_opt);
855
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200856 return 0;
857
858fail_free_emeta:
859 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200860 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
861 vfree(l_mg->eline_meta[i]->buf);
862 else
863 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200864 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200865 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200866
867 mempool_destroy(l_mg->bitmap_pool);
868fail_destroy_bitmap_cache:
869 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200870fail_free_smeta:
871 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200872 kfree(l_mg->sline_meta[i]);
Javier González43d47122018-03-30 00:05:09 +0200873 kfree(l_mg->bb_aux);
874fail_free_bb_template:
875 kfree(l_mg->bb_template);
876fail_free_vsc_list:
877 kfree(l_mg->vsc_list);
878fail:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200879 return -ENOMEM;
880}
881
Javier González43d47122018-03-30 00:05:09 +0200882static int pblk_line_meta_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200883{
884 struct nvm_tgt_dev *dev = pblk->dev;
885 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200886 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200887 unsigned int smeta_len, emeta_len;
Javier González43d47122018-03-30 00:05:09 +0200888 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200889
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200890 lm->sec_per_line = geo->clba * geo->all_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100891 lm->blk_per_line = geo->all_luns;
892 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200893 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100894 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200895 lm->mid_thrs = lm->sec_per_line / 2;
896 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100897 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200898
899 /* Calculate necessary pages for smeta. See comment over struct
900 * line_smeta definition
901 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200902 i = 1;
903add_smeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200904 lm->smeta_sec = i * geo->ws_opt;
905 lm->smeta_len = lm->smeta_sec * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200906
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200907 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200908 if (smeta_len > lm->smeta_len) {
909 i++;
910 goto add_smeta_page;
911 }
912
913 /* Calculate necessary pages for emeta. See comment over struct
914 * line_emeta definition
915 */
916 i = 1;
917add_emeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200918 lm->emeta_sec[0] = i * geo->ws_opt;
919 lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200920
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200921 emeta_len = calc_emeta_len(pblk);
922 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200923 i++;
924 goto add_emeta_page;
925 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200926
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100927 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +0200928
929 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100930 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +0200931 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200932 lm->emeta_sec[0], geo->clba);
Javier González21d22872017-10-13 14:46:26 +0200933
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200934 if (lm->min_blk_line > lm->blk_per_line) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200935 pblk_err(pblk, "config. not supported. Min. LUN in line:%d\n",
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200936 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200937 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200938 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200939
Javier González43d47122018-03-30 00:05:09 +0200940 return 0;
941}
942
943static int pblk_lines_init(struct pblk *pblk)
944{
945 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González43d47122018-03-30 00:05:09 +0200946 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +0200947 void *chunk_meta;
948 long nr_free_chks = 0;
Javier González43d47122018-03-30 00:05:09 +0200949 int i, ret;
950
951 ret = pblk_line_meta_init(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200952 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200953 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200954
Javier González43d47122018-03-30 00:05:09 +0200955 ret = pblk_line_mg_init(pblk);
956 if (ret)
957 return ret;
958
959 ret = pblk_luns_init(pblk);
960 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200961 goto fail_free_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200962
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200963 chunk_meta = pblk_get_chunk_meta(pblk);
Javier González32ef9412018-03-30 00:05:20 +0200964 if (IS_ERR(chunk_meta)) {
965 ret = PTR_ERR(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +0200966 goto fail_free_luns;
967 }
968
969 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
970 GFP_KERNEL);
971 if (!pblk->lines) {
972 ret = -ENOMEM;
Javier González32ef9412018-03-30 00:05:20 +0200973 goto fail_free_chunk_meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200974 }
975
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200976 for (i = 0; i < l_mg->nr_lines; i++) {
977 line = &pblk->lines[i];
978
Javier González32ef9412018-03-30 00:05:20 +0200979 ret = pblk_alloc_line_meta(pblk, line);
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200980 if (ret)
Javier González43d47122018-03-30 00:05:09 +0200981 goto fail_free_lines;
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200982
Javier González32ef9412018-03-30 00:05:20 +0200983 nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200984 }
985
Javier González2deeefc2018-06-01 15:04:15 +0200986 if (!nr_free_chks) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200987 pblk_err(pblk, "too many bad blocks prevent for sane instance\n");
Javier González2deeefc2018-06-01 15:04:15 +0200988 return -EINTR;
989 }
990
Javier González32ef9412018-03-30 00:05:20 +0200991 pblk_set_provision(pblk, nr_free_chks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200992
Javier González32ef9412018-03-30 00:05:20 +0200993 kfree(chunk_meta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200994 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200995
Javier González43d47122018-03-30 00:05:09 +0200996fail_free_lines:
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200997 while (--i >= 0)
Hans Holmberg48b8d202018-06-01 16:41:06 +0200998 pblk_line_meta_free(l_mg, &pblk->lines[i]);
Javier González43d47122018-03-30 00:05:09 +0200999 kfree(pblk->lines);
Javier González32ef9412018-03-30 00:05:20 +02001000fail_free_chunk_meta:
1001 kfree(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001002fail_free_luns:
1003 kfree(pblk->luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001004fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +02001005 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001006
1007 return ret;
1008}
1009
1010static int pblk_writer_init(struct pblk *pblk)
1011{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001012 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
1013 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001014 int err = PTR_ERR(pblk->writer_ts);
1015
1016 if (err != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001017 pblk_err(pblk, "could not allocate writer kthread (%d)\n",
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001018 err);
1019 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001020 }
1021
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001022 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1023 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1024
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001025 return 0;
1026}
1027
1028static void pblk_writer_stop(struct pblk *pblk)
1029{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001030 /* The pipeline must be stopped and the write buffer emptied before the
1031 * write thread is stopped
1032 */
1033 WARN(pblk_rb_read_count(&pblk->rwb),
1034 "Stopping not fully persisted write buffer\n");
1035
1036 WARN(pblk_rb_sync_count(&pblk->rwb),
1037 "Stopping not fully synced write buffer\n");
1038
Hans Holmberg7be970b2018-03-30 00:04:56 +02001039 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001040 if (pblk->writer_ts)
1041 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001042}
1043
1044static void pblk_free(struct pblk *pblk)
1045{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001046 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001047 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001048 pblk_rwb_free(pblk);
1049 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001050
1051 kfree(pblk);
1052}
1053
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001054static void pblk_tear_down(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001055{
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001056 if (graceful)
1057 __pblk_pipeline_flush(pblk);
1058 __pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001059 pblk_writer_stop(pblk);
1060 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001061 pblk_rl_free(&pblk->rl);
1062
Matias Bjørling4e495a42018-07-13 10:48:42 +02001063 pblk_debug(pblk, "consistent tear down (graceful:%d)\n", graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001064}
1065
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001066static void pblk_exit(void *private, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001067{
1068 struct pblk *pblk = private;
1069
1070 down_write(&pblk_lock);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001071 pblk_gc_exit(pblk, graceful);
1072 pblk_tear_down(pblk, graceful);
Hans Holmbergc5586192017-10-13 14:46:40 +02001073
Matias Bjørling880eda52018-07-13 10:48:37 +02001074#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001075 pblk_info(pblk, "exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Hans Holmbergc5586192017-10-13 14:46:40 +02001076#endif
1077
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001078 pblk_free(pblk);
1079 up_write(&pblk_lock);
1080}
1081
1082static sector_t pblk_capacity(void *private)
1083{
1084 struct pblk *pblk = private;
1085
1086 return pblk->capacity * NR_PHY_IN_LOG;
1087}
1088
1089static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1090 int flags)
1091{
1092 struct nvm_geo *geo = &dev->geo;
1093 struct request_queue *bqueue = dev->q;
1094 struct request_queue *tqueue = tdisk->queue;
1095 struct pblk *pblk;
1096 int ret;
1097
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001098 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1099 if (!pblk)
1100 return ERR_PTR(-ENOMEM);
1101
1102 pblk->dev = dev;
1103 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +02001104 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg3e3a5b82017-10-13 14:46:34 +02001105 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001106
Matias Bjørling4e495a42018-07-13 10:48:42 +02001107 if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1108 geo->version == NVM_OCSSD_SPEC_20)) {
1109 pblk_err(pblk, "OCSSD version not supported (%u)\n",
1110 geo->version);
1111 kfree(pblk);
1112 return ERR_PTR(-EINVAL);
1113 }
1114
Hans Holmberg6a3abf52018-06-01 16:41:05 +02001115 spin_lock_init(&pblk->resubmit_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001116 spin_lock_init(&pblk->trans_lock);
1117 spin_lock_init(&pblk->lock);
1118
Matias Bjørling880eda52018-07-13 10:48:37 +02001119#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001120 atomic_long_set(&pblk->inflight_writes, 0);
1121 atomic_long_set(&pblk->padded_writes, 0);
1122 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001123 atomic_long_set(&pblk->req_writes, 0);
1124 atomic_long_set(&pblk->sub_writes, 0);
1125 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001126 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001127 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001128 atomic_long_set(&pblk->sync_reads, 0);
1129 atomic_long_set(&pblk->recov_writes, 0);
1130 atomic_long_set(&pblk->recov_writes, 0);
1131 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001132 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001133#endif
1134
1135 atomic_long_set(&pblk->read_failed, 0);
1136 atomic_long_set(&pblk->read_empty, 0);
1137 atomic_long_set(&pblk->read_high_ecc, 0);
1138 atomic_long_set(&pblk->read_failed_gc, 0);
1139 atomic_long_set(&pblk->write_failed, 0);
1140 atomic_long_set(&pblk->erase_failed, 0);
1141
Javier González43d47122018-03-30 00:05:09 +02001142 ret = pblk_core_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001143 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001144 pblk_err(pblk, "could not initialize core\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001145 goto fail;
1146 }
1147
1148 ret = pblk_lines_init(pblk);
1149 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001150 pblk_err(pblk, "could not initialize lines\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001151 goto fail_free_core;
1152 }
1153
Javier González43d47122018-03-30 00:05:09 +02001154 ret = pblk_rwb_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001155 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001156 pblk_err(pblk, "could not initialize write buffer\n");
Javier González43d47122018-03-30 00:05:09 +02001157 goto fail_free_lines;
1158 }
1159
1160 ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1161 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001162 pblk_err(pblk, "could not initialize maps\n");
Javier González43d47122018-03-30 00:05:09 +02001163 goto fail_free_rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001164 }
1165
1166 ret = pblk_writer_init(pblk);
1167 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001168 if (ret != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001169 pblk_err(pblk, "could not initialize write thread\n");
Javier González43d47122018-03-30 00:05:09 +02001170 goto fail_free_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001171 }
1172
1173 ret = pblk_gc_init(pblk);
1174 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001175 pblk_err(pblk, "could not initialize gc\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001176 goto fail_stop_writer;
1177 }
1178
1179 /* inherit the size from the underlying device */
1180 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1181 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1182
1183 blk_queue_write_cache(tqueue, true, false);
1184
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001185 tqueue->limits.discard_granularity = geo->clba * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001186 tqueue->limits.discard_alignment = 0;
1187 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001188 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001189
Matias Bjørling4e495a42018-07-13 10:48:42 +02001190 pblk_info(pblk, "luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001191 geo->all_luns, pblk->l_mg.nr_lines,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001192 (unsigned long long)pblk->rl.nr_secs,
1193 pblk->rwb.nr_entries);
1194
1195 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001196
1197 /* Check if we need to start GC */
1198 pblk_gc_should_kick(pblk);
1199
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001200 return pblk;
1201
1202fail_stop_writer:
1203 pblk_writer_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001204fail_free_l2p:
1205 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001206fail_free_rwb:
1207 pblk_rwb_free(pblk);
1208fail_free_lines:
1209 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001210fail_free_core:
1211 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001212fail:
1213 kfree(pblk);
1214 return ERR_PTR(ret);
1215}
1216
1217/* physical block device target */
1218static struct nvm_tgt_type tt_pblk = {
1219 .name = "pblk",
1220 .version = {1, 0, 0},
1221
1222 .make_rq = pblk_make_rq,
1223 .capacity = pblk_capacity,
1224
1225 .init = pblk_init,
1226 .exit = pblk_exit,
1227
1228 .sysfs_init = pblk_sysfs_init,
1229 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001230 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001231};
1232
1233static int __init pblk_module_init(void)
1234{
NeilBrownb25d5232017-06-18 14:38:58 +10001235 int ret;
1236
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001237 ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
1238 if (ret)
1239 return ret;
NeilBrownb25d5232017-06-18 14:38:58 +10001240 ret = nvm_register_tgt_type(&tt_pblk);
1241 if (ret)
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001242 bioset_exit(&pblk_bio_set);
NeilBrownb25d5232017-06-18 14:38:58 +10001243 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001244}
1245
1246static void pblk_module_exit(void)
1247{
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001248 bioset_exit(&pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001249 nvm_unregister_tgt_type(&tt_pblk);
1250}
1251
1252module_init(pblk_module_init);
1253module_exit(pblk_module_exit);
1254MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1255MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1256MODULE_LICENSE("GPL v2");
1257MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");