blob: 2a3302f7e8530db69603c45d22fa384329bf7659 [file] [log] [blame]
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001/*
2 * Copyright (C) 2015 IT University of Copenhagen (rrpc.c)
3 * Copyright (C) 2016 CNEX Labs
4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
5 * Matias Bjorling <matias@cnexlabs.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version
9 * 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * Implementation of a physical block-device target for Open-channel SSDs.
17 *
18 * pblk-init.c - pblk's initialization.
19 */
20
21#include "pblk.h"
22
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +020023static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache,
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +020024 *pblk_w_rq_cache;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020025static DECLARE_RWSEM(pblk_lock);
Kent Overstreetb906bbb2018-05-20 18:25:50 -040026struct bio_set pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020027
28static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
29 struct bio *bio)
30{
31 int ret;
32
33 /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
34 * constraint. Writes can be of arbitrary size.
35 */
36 if (bio_data_dir(bio) == READ) {
NeilBrownaf67c312017-06-18 14:38:57 +100037 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020038 ret = pblk_submit_read(pblk, bio);
39 if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
40 bio_put(bio);
41
42 return ret;
43 }
44
45 /* Prevent deadlock in the case of a modest LUN configuration and large
46 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
47 * available for user I/O.
48 */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +020049 if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
NeilBrownaf67c312017-06-18 14:38:57 +100050 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020051
52 return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
53}
54
55static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
56{
57 struct pblk *pblk = q->queuedata;
58
59 if (bio_op(bio) == REQ_OP_DISCARD) {
60 pblk_discard(pblk, bio);
61 if (!(bio->bi_opf & REQ_PREFLUSH)) {
62 bio_endio(bio);
63 return BLK_QC_T_NONE;
64 }
65 }
66
67 switch (pblk_rw_io(q, pblk, bio)) {
68 case NVM_IO_ERR:
69 bio_io_error(bio);
70 break;
71 case NVM_IO_DONE:
72 bio_endio(bio);
73 break;
74 }
75
76 return BLK_QC_T_NONE;
77}
78
Hans Holmbergc5586192017-10-13 14:46:40 +020079static size_t pblk_trans_map_size(struct pblk *pblk)
80{
81 int entry_size = 8;
82
Javier Gonzálezbb845ae42018-03-30 00:05:19 +020083 if (pblk->addrf_len < 32)
Hans Holmbergc5586192017-10-13 14:46:40 +020084 entry_size = 4;
85
86 return entry_size * pblk->rl.nr_secs;
87}
88
89#ifdef CONFIG_NVM_DEBUG
90static u32 pblk_l2p_crc(struct pblk *pblk)
91{
92 size_t map_size;
93 u32 crc = ~(u32)0;
94
95 map_size = pblk_trans_map_size(pblk);
96 crc = crc32_le(crc, pblk->trans_map, map_size);
97 return crc;
98}
99#endif
100
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200101static void pblk_l2p_free(struct pblk *pblk)
102{
103 vfree(pblk->trans_map);
104}
105
Javier González43d47122018-03-30 00:05:09 +0200106static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
107{
108 struct pblk_line *line = NULL;
109
110 if (factory_init) {
111 pblk_setup_uuid(pblk);
112 } else {
113 line = pblk_recov_l2p(pblk);
114 if (IS_ERR(line)) {
115 pr_err("pblk: could not recover l2p table\n");
116 return -EFAULT;
117 }
118 }
119
120#ifdef CONFIG_NVM_DEBUG
121 pr_info("pblk init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
122#endif
123
124 /* Free full lines directly as GC has not been started yet */
125 pblk_gc_free_full_lines(pblk);
126
127 if (!line) {
128 /* Configure next line for user data */
129 line = pblk_line_get_first_data(pblk);
Javier González1d8b33e2018-06-01 15:04:16 +0200130 if (!line)
Javier González43d47122018-03-30 00:05:09 +0200131 return -EFAULT;
Javier González43d47122018-03-30 00:05:09 +0200132 }
133
134 return 0;
135}
136
137static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200138{
139 sector_t i;
140 struct ppa_addr ppa;
Hans Holmbergc5586192017-10-13 14:46:40 +0200141 size_t map_size;
Javier González1d8b33e2018-06-01 15:04:16 +0200142 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200143
Hans Holmbergc5586192017-10-13 14:46:40 +0200144 map_size = pblk_trans_map_size(pblk);
145 pblk->trans_map = vmalloc(map_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200146 if (!pblk->trans_map)
147 return -ENOMEM;
148
149 pblk_ppa_set_empty(&ppa);
150
151 for (i = 0; i < pblk->rl.nr_secs; i++)
152 pblk_trans_map_set(pblk, i, ppa);
153
Javier González1d8b33e2018-06-01 15:04:16 +0200154 ret = pblk_l2p_recover(pblk, factory_init);
155 if (ret)
156 vfree(pblk->trans_map);
157
158 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200159}
160
161static void pblk_rwb_free(struct pblk *pblk)
162{
163 if (pblk_rb_tear_down_check(&pblk->rwb))
164 pr_err("pblk: write buffer error on tear down\n");
165
166 pblk_rb_data_free(&pblk->rwb);
167 vfree(pblk_rb_entries_ref(&pblk->rwb));
168}
169
170static int pblk_rwb_init(struct pblk *pblk)
171{
172 struct nvm_tgt_dev *dev = pblk->dev;
173 struct nvm_geo *geo = &dev->geo;
174 struct pblk_rb_entry *entries;
175 unsigned long nr_entries;
176 unsigned int power_size, power_seg_sz;
177
178 nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer);
179
180 entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
181 if (!entries)
182 return -ENOMEM;
183
184 power_size = get_count_order(nr_entries);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200185 power_seg_sz = get_count_order(geo->csecs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200186
187 return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
188}
189
190/* Minimum pages needed within a lun */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200191#define ADDR_POOL_SIZE 64
192
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200193static int pblk_set_addrf_12(struct nvm_geo *geo, struct nvm_addrf_12 *dst)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200194{
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200195 struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
196 int power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200197
198 /* Re-calculate channel and lun format to adapt to configuration */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200199 power_len = get_count_order(geo->num_ch);
200 if (1 << power_len != geo->num_ch) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200201 pr_err("pblk: supports only power-of-two channel config.\n");
202 return -EINVAL;
203 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200204 dst->ch_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200205
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200206 power_len = get_count_order(geo->num_lun);
207 if (1 << power_len != geo->num_lun) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208 pr_err("pblk: supports only power-of-two LUN config.\n");
209 return -EINVAL;
210 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200211 dst->lun_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200212
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200213 dst->blk_len = src->blk_len;
214 dst->pg_len = src->pg_len;
215 dst->pln_len = src->pln_len;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200216 dst->sec_len = src->sec_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200217
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200218 dst->sec_offset = 0;
219 dst->pln_offset = dst->sec_len;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200220 dst->ch_offset = dst->pln_offset + dst->pln_len;
221 dst->lun_offset = dst->ch_offset + dst->ch_len;
222 dst->pg_offset = dst->lun_offset + dst->lun_len;
223 dst->blk_offset = dst->pg_offset + dst->pg_len;
224
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200225 dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200226 dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
227 dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
228 dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
229 dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
230 dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
231
232 return dst->blk_offset + src->blk_len;
233}
234
Javier González3b2a3ad2018-03-30 00:05:21 +0200235static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
236 struct pblk_addrf *udst)
237{
238 struct nvm_addrf *src = &geo->addrf;
239
240 adst->ch_len = get_count_order(geo->num_ch);
241 adst->lun_len = get_count_order(geo->num_lun);
242 adst->chk_len = src->chk_len;
243 adst->sec_len = src->sec_len;
244
245 adst->sec_offset = 0;
246 adst->ch_offset = adst->sec_len;
247 adst->lun_offset = adst->ch_offset + adst->ch_len;
248 adst->chk_offset = adst->lun_offset + adst->lun_len;
249
250 adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
251 adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
252 adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
253 adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
254
255 udst->sec_stripe = geo->ws_opt;
256 udst->ch_stripe = geo->num_ch;
257 udst->lun_stripe = geo->num_lun;
258
259 udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
260 udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
261
262 return adst->chk_offset + adst->chk_len;
263}
264
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200265static int pblk_set_addrf(struct pblk *pblk)
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200266{
267 struct nvm_tgt_dev *dev = pblk->dev;
268 struct nvm_geo *geo = &dev->geo;
269 int mod;
270
Javier González3b2a3ad2018-03-30 00:05:21 +0200271 switch (geo->version) {
272 case NVM_OCSSD_SPEC_12:
273 div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
274 if (mod) {
275 pr_err("pblk: bad configuration of sectors/pages\n");
276 return -EINVAL;
277 }
278
279 pblk->addrf_len = pblk_set_addrf_12(geo, (void *)&pblk->addrf);
280 break;
281 case NVM_OCSSD_SPEC_20:
282 pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
283 &pblk->uaddrf);
284 break;
285 default:
286 pr_err("pblk: OCSSD revision not supported (%d)\n",
287 geo->version);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200288 return -EINVAL;
289 }
290
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200291 return 0;
292}
293
294static int pblk_init_global_caches(struct pblk *pblk)
295{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200296 down_write(&pblk_lock);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200297 pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200298 sizeof(struct pblk_line_ws), 0, 0, NULL);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200299 if (!pblk_ws_cache) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200300 up_write(&pblk_lock);
301 return -ENOMEM;
302 }
303
304 pblk_rec_cache = kmem_cache_create("pblk_rec",
305 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
306 if (!pblk_rec_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200307 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200308 up_write(&pblk_lock);
309 return -ENOMEM;
310 }
311
Javier González084ec9b2017-06-26 16:27:13 -0600312 pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200313 0, 0, NULL);
Javier González084ec9b2017-06-26 16:27:13 -0600314 if (!pblk_g_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200315 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200316 kmem_cache_destroy(pblk_rec_cache);
317 up_write(&pblk_lock);
318 return -ENOMEM;
319 }
320
321 pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
322 0, 0, NULL);
323 if (!pblk_w_rq_cache) {
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200324 kmem_cache_destroy(pblk_ws_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200325 kmem_cache_destroy(pblk_rec_cache);
Javier González084ec9b2017-06-26 16:27:13 -0600326 kmem_cache_destroy(pblk_g_rq_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200327 up_write(&pblk_lock);
328 return -ENOMEM;
329 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200330 up_write(&pblk_lock);
331
332 return 0;
333}
334
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200335static void pblk_free_global_caches(struct pblk *pblk)
336{
337 kmem_cache_destroy(pblk_ws_cache);
338 kmem_cache_destroy(pblk_rec_cache);
339 kmem_cache_destroy(pblk_g_rq_cache);
340 kmem_cache_destroy(pblk_w_rq_cache);
341}
342
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200343static int pblk_core_init(struct pblk *pblk)
344{
345 struct nvm_tgt_dev *dev = pblk->dev;
346 struct nvm_geo *geo = &dev->geo;
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400347 int ret, max_write_ppas;
Javier González43d47122018-03-30 00:05:09 +0200348
349 atomic64_set(&pblk->user_wa, 0);
350 atomic64_set(&pblk->pad_wa, 0);
351 atomic64_set(&pblk->gc_wa, 0);
352 pblk->user_rst_wa = 0;
353 pblk->pad_rst_wa = 0;
354 pblk->gc_rst_wa = 0;
355
356 atomic64_set(&pblk->nr_flush, 0);
357 pblk->nr_flush_rst = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200358
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200359 pblk->pgs_in_buffer = geo->mw_cunits * geo->all_luns;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200360
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200361 pblk->min_write_pgs = geo->ws_opt * (geo->csecs / PAGE_SIZE);
Javier González43d47122018-03-30 00:05:09 +0200362 max_write_ppas = pblk->min_write_pgs * geo->all_luns;
363 pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
364 pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
365
366 if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
367 pr_err("pblk: vector list too big(%u > %u)\n",
368 pblk->max_write_pgs, PBLK_MAX_REQ_ADDRS);
369 return -EINVAL;
370 }
371
372 pblk->pad_dist = kzalloc((pblk->min_write_pgs - 1) * sizeof(atomic64_t),
373 GFP_KERNEL);
374 if (!pblk->pad_dist)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200375 return -ENOMEM;
376
Javier González43d47122018-03-30 00:05:09 +0200377 if (pblk_init_global_caches(pblk))
378 goto fail_free_pad_dist;
379
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200380 /* Internal bios can be at most the sectors signaled by the device. */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400381 ret = mempool_init_page_pool(&pblk->page_bio_pool, NVM_MAX_VLBA, 0);
382 if (ret)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200383 goto free_global_caches;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200384
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400385 ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
386 pblk_ws_cache);
387 if (ret)
Javier Gonzálezbd432412017-10-13 14:46:06 +0200388 goto free_page_bio_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200389
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400390 ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
391 pblk_rec_cache);
392 if (ret)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200393 goto free_gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200394
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400395 ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
396 pblk_g_rq_cache);
397 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200398 goto free_rec_pool;
399
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400400 ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
401 pblk_g_rq_cache);
402 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200403 goto free_r_rq_pool;
404
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400405 ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
406 pblk_w_rq_cache);
407 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200408 goto free_e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200409
Javier Gonzálezef576492017-06-26 11:57:28 +0200410 pblk->close_wq = alloc_workqueue("pblk-close-wq",
411 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
412 if (!pblk->close_wq)
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200413 goto free_w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200414
Javier Gonzálezef576492017-06-26 11:57:28 +0200415 pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
416 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
417 if (!pblk->bb_wq)
418 goto free_close_wq;
419
Javier González7bd4d372017-10-13 14:46:23 +0200420 pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
421 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
422 if (!pblk->r_end_wq)
Javier Gonzálezef576492017-06-26 11:57:28 +0200423 goto free_bb_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200424
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200425 if (pblk_set_addrf(pblk))
Javier González7bd4d372017-10-13 14:46:23 +0200426 goto free_r_end_wq;
427
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200428 INIT_LIST_HEAD(&pblk->compl_list);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200429 INIT_LIST_HEAD(&pblk->resubmit_list);
Javier González43d47122018-03-30 00:05:09 +0200430
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200431 return 0;
432
Javier González7bd4d372017-10-13 14:46:23 +0200433free_r_end_wq:
434 destroy_workqueue(pblk->r_end_wq);
Javier Gonzálezef576492017-06-26 11:57:28 +0200435free_bb_wq:
436 destroy_workqueue(pblk->bb_wq);
437free_close_wq:
438 destroy_workqueue(pblk->close_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200439free_w_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400440 mempool_exit(&pblk->w_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200441free_e_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400442 mempool_exit(&pblk->e_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200443free_r_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400444 mempool_exit(&pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200445free_rec_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400446 mempool_exit(&pblk->rec_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200447free_gen_ws_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400448 mempool_exit(&pblk->gen_ws_pool);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200449free_page_bio_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400450 mempool_exit(&pblk->page_bio_pool);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200451free_global_caches:
452 pblk_free_global_caches(pblk);
Javier González43d47122018-03-30 00:05:09 +0200453fail_free_pad_dist:
454 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200455 return -ENOMEM;
456}
457
458static void pblk_core_free(struct pblk *pblk)
459{
Javier Gonzálezef576492017-06-26 11:57:28 +0200460 if (pblk->close_wq)
461 destroy_workqueue(pblk->close_wq);
462
Javier González7bd4d372017-10-13 14:46:23 +0200463 if (pblk->r_end_wq)
464 destroy_workqueue(pblk->r_end_wq);
465
Javier Gonzálezef576492017-06-26 11:57:28 +0200466 if (pblk->bb_wq)
467 destroy_workqueue(pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200468
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400469 mempool_exit(&pblk->page_bio_pool);
470 mempool_exit(&pblk->gen_ws_pool);
471 mempool_exit(&pblk->rec_pool);
472 mempool_exit(&pblk->r_rq_pool);
473 mempool_exit(&pblk->e_rq_pool);
474 mempool_exit(&pblk->w_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200475
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200476 pblk_free_global_caches(pblk);
Javier González43d47122018-03-30 00:05:09 +0200477 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200478}
479
Javier Gonzáleze411b332018-03-30 00:04:59 +0200480static void pblk_line_mg_free(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200481{
482 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
483 int i;
484
485 kfree(l_mg->bb_template);
486 kfree(l_mg->bb_aux);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200487 kfree(l_mg->vsc_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200488
489 for (i = 0; i < PBLK_DATA_LINES; i++) {
Javier Gonzálezf680f192017-06-26 11:57:21 +0200490 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200491 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200492 kfree(l_mg->eline_meta[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200493 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494}
495
Javier Gonzáleze411b332018-03-30 00:04:59 +0200496static void pblk_line_meta_free(struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200497{
Javier Gonzáleze411b332018-03-30 00:04:59 +0200498 kfree(line->blk_bitmap);
499 kfree(line->erase_bitmap);
Javier González32ef9412018-03-30 00:05:20 +0200500 kfree(line->chks);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200501}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200502
Javier Gonzáleze411b332018-03-30 00:04:59 +0200503static void pblk_lines_free(struct pblk *pblk)
504{
505 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
506 struct pblk_line *line;
507 int i;
508
509 spin_lock(&l_mg->free_lock);
510 for (i = 0; i < l_mg->nr_lines; i++) {
511 line = &pblk->lines[i];
512
Javier González8e55c072018-06-01 15:04:22 +0200513 pblk_line_free(line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200514 pblk_line_meta_free(line);
515 }
516 spin_unlock(&l_mg->free_lock);
Javier González43d47122018-03-30 00:05:09 +0200517
518 pblk_line_mg_free(pblk);
519
520 kfree(pblk->luns);
521 kfree(pblk->lines);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200522}
523
524static int pblk_bb_get_tbl(struct nvm_tgt_dev *dev, struct pblk_lun *rlun,
525 u8 *blks, int nr_blks)
526{
527 struct ppa_addr ppa;
528 int ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200529
530 ppa.ppa = 0;
531 ppa.g.ch = rlun->bppa.g.ch;
532 ppa.g.lun = rlun->bppa.g.lun;
533
534 ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
535 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200536 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200537
538 nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200539 if (nr_blks < 0)
540 return -EIO;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200541
Wei Yongjun5136a4f2017-04-25 16:15:30 +0000542 return 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200543}
544
Javier González32ef9412018-03-30 00:05:20 +0200545static void *pblk_bb_get_meta(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200546{
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200547 struct nvm_tgt_dev *dev = pblk->dev;
548 struct nvm_geo *geo = &dev->geo;
Javier González32ef9412018-03-30 00:05:20 +0200549 u8 *meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200550 int i, nr_blks, blk_per_lun;
551 int ret;
552
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200553 blk_per_lun = geo->num_chk * geo->pln_mode;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200554 nr_blks = blk_per_lun * geo->all_luns;
555
Javier González32ef9412018-03-30 00:05:20 +0200556 meta = kmalloc(nr_blks, GFP_KERNEL);
557 if (!meta)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200558 return ERR_PTR(-ENOMEM);
559
560 for (i = 0; i < geo->all_luns; i++) {
561 struct pblk_lun *rlun = &pblk->luns[i];
Javier González32ef9412018-03-30 00:05:20 +0200562 u8 *meta_pos = meta + i * blk_per_lun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200563
Javier González32ef9412018-03-30 00:05:20 +0200564 ret = pblk_bb_get_tbl(dev, rlun, meta_pos, blk_per_lun);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200565 if (ret) {
Javier González32ef9412018-03-30 00:05:20 +0200566 kfree(meta);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200567 return ERR_PTR(-EIO);
568 }
569 }
570
Javier González32ef9412018-03-30 00:05:20 +0200571 return meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200572}
573
Javier González32ef9412018-03-30 00:05:20 +0200574static void *pblk_chunk_get_meta(struct pblk *pblk)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200575{
576 struct nvm_tgt_dev *dev = pblk->dev;
577 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200578
Javier González32ef9412018-03-30 00:05:20 +0200579 if (geo->version == NVM_OCSSD_SPEC_12)
580 return pblk_bb_get_meta(pblk);
581 else
582 return pblk_chunk_get_info(pblk);
Javier Gonzálezdffdd962017-06-26 11:57:22 +0200583}
584
Javier González43d47122018-03-30 00:05:09 +0200585static int pblk_luns_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200586{
587 struct nvm_tgt_dev *dev = pblk->dev;
588 struct nvm_geo *geo = &dev->geo;
589 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200590 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200591
592 /* TODO: Implement unbalanced LUN support */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200593 if (geo->num_lun < 0) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200594 pr_err("pblk: unbalanced LUN config.\n");
595 return -EINVAL;
596 }
597
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100598 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
599 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200600 if (!pblk->luns)
601 return -ENOMEM;
602
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100603 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200604 /* Stripe across channels */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200605 int ch = i % geo->num_ch;
606 int lun_raw = i / geo->num_ch;
607 int lunid = lun_raw + ch * geo->num_lun;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200608
609 rlun = &pblk->luns[i];
Javier González43d47122018-03-30 00:05:09 +0200610 rlun->bppa = dev->luns[lunid];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200611
612 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200613 }
614
615 return 0;
616}
617
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200618/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200619static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200620{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200621 struct pblk_line_meta *lm = &pblk->lm;
622 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
623 struct nvm_tgt_dev *dev = pblk->dev;
624 struct nvm_geo *geo = &dev->geo;
625
626 /* Round to sector size so that lba_list starts on its own sector */
627 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200628 sizeof(struct line_emeta) + lm->blk_bitmap_len +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200629 sizeof(struct wa_counters), geo->csecs);
630 lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200631
632 /* Round to sector size so that vsc_list starts on its own sector */
633 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
634 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200635 geo->csecs);
636 lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200637
638 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200639 geo->csecs);
640 lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200641
642 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
643
644 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200645}
646
647static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
648{
649 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100650 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
651 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200652 struct nvm_geo *geo = &dev->geo;
653 sector_t provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100654 int sec_meta, blk_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200655
Javier Gonzáleze5392732018-01-05 14:16:14 +0100656 if (geo->op == NVM_TARGET_DEFAULT_OP)
657 pblk->op = PBLK_DEFAULT_OP;
658 else
659 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200660
661 provisioned = nr_free_blks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100662 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200663 sector_div(provisioned, 100);
664
Javier Gonzáleza7689932018-01-05 14:16:13 +0100665 pblk->op_blks = nr_free_blks - provisioned;
666
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200667 /* Internally pblk manages all free blocks, but all calculations based
668 * on user capacity consider only provisioned blocks
669 */
670 pblk->rl.total_blocks = nr_free_blks;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200671 pblk->rl.nr_secs = nr_free_blks * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100672
673 /* Consider sectors used for metadata */
674 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200675 blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100676
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200677 pblk->capacity = (provisioned - blk_meta) * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100678
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200679 atomic_set(&pblk->rl.free_blocks, nr_free_blks);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100680 atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200681}
682
Javier González32ef9412018-03-30 00:05:20 +0200683static int pblk_setup_line_meta_12(struct pblk *pblk, struct pblk_line *line,
684 void *chunk_meta)
685{
686 struct nvm_tgt_dev *dev = pblk->dev;
687 struct nvm_geo *geo = &dev->geo;
688 struct pblk_line_meta *lm = &pblk->lm;
689 int i, chk_per_lun, nr_bad_chks = 0;
690
691 chk_per_lun = geo->num_chk * geo->pln_mode;
692
693 for (i = 0; i < lm->blk_per_line; i++) {
694 struct pblk_lun *rlun = &pblk->luns[i];
695 struct nvm_chk_meta *chunk;
696 int pos = pblk_ppa_to_pos(geo, rlun->bppa);
697 u8 *lun_bb_meta = chunk_meta + pos * chk_per_lun;
698
699 chunk = &line->chks[pos];
700
701 /*
702 * In 1.2 spec. chunk state is not persisted by the device. Thus
703 * some of the values are reset each time pblk is instantiated.
704 */
705 if (lun_bb_meta[line->id] == NVM_BLK_T_FREE)
706 chunk->state = NVM_CHK_ST_FREE;
707 else
708 chunk->state = NVM_CHK_ST_OFFLINE;
709
710 chunk->type = NVM_CHK_TP_W_SEQ;
711 chunk->wi = 0;
712 chunk->slba = -1;
713 chunk->cnlb = geo->clba;
714 chunk->wp = 0;
715
716 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
717 continue;
718
719 set_bit(pos, line->blk_bitmap);
720 nr_bad_chks++;
721 }
722
723 return nr_bad_chks;
724}
725
726static int pblk_setup_line_meta_20(struct pblk *pblk, struct pblk_line *line,
727 struct nvm_chk_meta *meta)
728{
729 struct nvm_tgt_dev *dev = pblk->dev;
730 struct nvm_geo *geo = &dev->geo;
731 struct pblk_line_meta *lm = &pblk->lm;
732 int i, nr_bad_chks = 0;
733
734 for (i = 0; i < lm->blk_per_line; i++) {
735 struct pblk_lun *rlun = &pblk->luns[i];
736 struct nvm_chk_meta *chunk;
737 struct nvm_chk_meta *chunk_meta;
738 struct ppa_addr ppa;
739 int pos;
740
741 ppa = rlun->bppa;
742 pos = pblk_ppa_to_pos(geo, ppa);
743 chunk = &line->chks[pos];
744
745 ppa.m.chk = line->id;
746 chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
747
748 chunk->state = chunk_meta->state;
749 chunk->type = chunk_meta->type;
750 chunk->wi = chunk_meta->wi;
751 chunk->slba = chunk_meta->slba;
752 chunk->cnlb = chunk_meta->cnlb;
753 chunk->wp = chunk_meta->wp;
754
Javier González32ef9412018-03-30 00:05:20 +0200755 if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
756 WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
757 continue;
758 }
759
Javier González6f9c9602018-06-01 15:04:23 +0200760 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
761 continue;
762
Javier González32ef9412018-03-30 00:05:20 +0200763 set_bit(pos, line->blk_bitmap);
764 nr_bad_chks++;
765 }
766
767 return nr_bad_chks;
768}
769
770static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
771 void *chunk_meta, int line_id)
772{
773 struct nvm_tgt_dev *dev = pblk->dev;
774 struct nvm_geo *geo = &dev->geo;
775 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
776 struct pblk_line_meta *lm = &pblk->lm;
777 long nr_bad_chks, chk_in_line;
778
779 line->pblk = pblk;
780 line->id = line_id;
781 line->type = PBLK_LINETYPE_FREE;
782 line->state = PBLK_LINESTATE_NEW;
783 line->gc_group = PBLK_LINEGC_NONE;
784 line->vsc = &l_mg->vsc_list[line_id];
785 spin_lock_init(&line->lock);
786
787 if (geo->version == NVM_OCSSD_SPEC_12)
788 nr_bad_chks = pblk_setup_line_meta_12(pblk, line, chunk_meta);
789 else
790 nr_bad_chks = pblk_setup_line_meta_20(pblk, line, chunk_meta);
791
792 chk_in_line = lm->blk_per_line - nr_bad_chks;
793 if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
794 chk_in_line < lm->min_blk_line) {
795 line->state = PBLK_LINESTATE_BAD;
796 list_add_tail(&line->list, &l_mg->bad_list);
797 return 0;
798 }
799
800 atomic_set(&line->blk_in_line, chk_in_line);
801 list_add_tail(&line->list, &l_mg->free_list);
802 l_mg->nr_free_lines++;
803
804 return chk_in_line;
805}
806
807static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200808{
Javier González43d47122018-03-30 00:05:09 +0200809 struct pblk_line_meta *lm = &pblk->lm;
810
811 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
812 if (!line->blk_bitmap)
813 return -ENOMEM;
814
815 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
816 if (!line->erase_bitmap) {
817 kfree(line->blk_bitmap);
818 return -ENOMEM;
819 }
820
Javier González32ef9412018-03-30 00:05:20 +0200821 line->chks = kmalloc(lm->blk_per_line * sizeof(struct nvm_chk_meta),
822 GFP_KERNEL);
823 if (!line->chks) {
824 kfree(line->erase_bitmap);
825 kfree(line->blk_bitmap);
826 return -ENOMEM;
827 }
Javier González43d47122018-03-30 00:05:09 +0200828
829 return 0;
830}
831
832static int pblk_line_mg_init(struct pblk *pblk)
833{
834 struct nvm_tgt_dev *dev = pblk->dev;
835 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200836 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
837 struct pblk_line_meta *lm = &pblk->lm;
Javier González43d47122018-03-30 00:05:09 +0200838 int i, bb_distance;
839
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200840 l_mg->nr_lines = geo->num_chk;
Javier González43d47122018-03-30 00:05:09 +0200841 l_mg->log_line = l_mg->data_line = NULL;
842 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
843 l_mg->nr_free_lines = 0;
844 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
845
846 INIT_LIST_HEAD(&l_mg->free_list);
847 INIT_LIST_HEAD(&l_mg->corrupt_list);
848 INIT_LIST_HEAD(&l_mg->bad_list);
849 INIT_LIST_HEAD(&l_mg->gc_full_list);
850 INIT_LIST_HEAD(&l_mg->gc_high_list);
851 INIT_LIST_HEAD(&l_mg->gc_mid_list);
852 INIT_LIST_HEAD(&l_mg->gc_low_list);
853 INIT_LIST_HEAD(&l_mg->gc_empty_list);
854
855 INIT_LIST_HEAD(&l_mg->emeta_list);
856
857 l_mg->gc_lists[0] = &l_mg->gc_high_list;
858 l_mg->gc_lists[1] = &l_mg->gc_mid_list;
859 l_mg->gc_lists[2] = &l_mg->gc_low_list;
860
861 spin_lock_init(&l_mg->free_lock);
862 spin_lock_init(&l_mg->close_lock);
863 spin_lock_init(&l_mg->gc_lock);
864
865 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
866 if (!l_mg->vsc_list)
867 goto fail;
868
869 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
870 if (!l_mg->bb_template)
871 goto fail_free_vsc_list;
872
873 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
874 if (!l_mg->bb_aux)
875 goto fail_free_bb_template;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200876
877 /* smeta is always small enough to fit on a kmalloc memory allocation,
878 * emeta depends on the number of LUNs allocated to the pblk instance
879 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200880 for (i = 0; i < PBLK_DATA_LINES; i++) {
881 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
882 if (!l_mg->sline_meta[i])
883 goto fail_free_smeta;
884 }
885
886 /* emeta allocates three different buffers for managing metadata with
887 * in-memory and in-media layouts
888 */
889 for (i = 0; i < PBLK_DATA_LINES; i++) {
890 struct pblk_emeta *emeta;
891
892 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
893 if (!emeta)
894 goto fail_free_emeta;
895
896 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
897 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
898
899 emeta->buf = vmalloc(lm->emeta_len[0]);
900 if (!emeta->buf) {
901 kfree(emeta);
902 goto fail_free_emeta;
903 }
904
905 emeta->nr_entries = lm->emeta_sec[0];
906 l_mg->eline_meta[i] = emeta;
907 } else {
908 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
909
910 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
911 if (!emeta->buf) {
912 kfree(emeta);
913 goto fail_free_emeta;
914 }
915
916 emeta->nr_entries = lm->emeta_sec[0];
917 l_mg->eline_meta[i] = emeta;
918 }
919 }
920
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200921 for (i = 0; i < l_mg->nr_lines; i++)
922 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
923
Javier González43d47122018-03-30 00:05:09 +0200924 bb_distance = (geo->all_luns) * geo->ws_opt;
925 for (i = 0; i < lm->sec_per_line; i += bb_distance)
926 bitmap_set(l_mg->bb_template, i, geo->ws_opt);
927
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200928 return 0;
929
930fail_free_emeta:
931 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200932 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
933 vfree(l_mg->eline_meta[i]->buf);
934 else
935 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200936 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200937 }
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200938fail_free_smeta:
939 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200940 kfree(l_mg->sline_meta[i]);
Javier González43d47122018-03-30 00:05:09 +0200941 kfree(l_mg->bb_aux);
942fail_free_bb_template:
943 kfree(l_mg->bb_template);
944fail_free_vsc_list:
945 kfree(l_mg->vsc_list);
946fail:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200947 return -ENOMEM;
948}
949
Javier González43d47122018-03-30 00:05:09 +0200950static int pblk_line_meta_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200951{
952 struct nvm_tgt_dev *dev = pblk->dev;
953 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200954 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200955 unsigned int smeta_len, emeta_len;
Javier González43d47122018-03-30 00:05:09 +0200956 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200957
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200958 lm->sec_per_line = geo->clba * geo->all_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100959 lm->blk_per_line = geo->all_luns;
960 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200961 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100962 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200963 lm->mid_thrs = lm->sec_per_line / 2;
964 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100965 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200966
967 /* Calculate necessary pages for smeta. See comment over struct
968 * line_smeta definition
969 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200970 i = 1;
971add_smeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200972 lm->smeta_sec = i * geo->ws_opt;
973 lm->smeta_len = lm->smeta_sec * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200974
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200975 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200976 if (smeta_len > lm->smeta_len) {
977 i++;
978 goto add_smeta_page;
979 }
980
981 /* Calculate necessary pages for emeta. See comment over struct
982 * line_emeta definition
983 */
984 i = 1;
985add_emeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200986 lm->emeta_sec[0] = i * geo->ws_opt;
987 lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200988
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200989 emeta_len = calc_emeta_len(pblk);
990 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200991 i++;
992 goto add_emeta_page;
993 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200994
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100995 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +0200996
997 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100998 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +0200999 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001000 lm->emeta_sec[0], geo->clba);
Javier González21d22872017-10-13 14:46:26 +02001001
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001002 if (lm->min_blk_line > lm->blk_per_line) {
1003 pr_err("pblk: config. not supported. Min. LUN in line:%d\n",
1004 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +02001005 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +02001006 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001007
Javier González43d47122018-03-30 00:05:09 +02001008 return 0;
1009}
1010
1011static int pblk_lines_init(struct pblk *pblk)
1012{
1013 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González43d47122018-03-30 00:05:09 +02001014 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +02001015 void *chunk_meta;
1016 long nr_free_chks = 0;
Javier González43d47122018-03-30 00:05:09 +02001017 int i, ret;
1018
1019 ret = pblk_line_meta_init(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +02001020 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +02001021 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001022
Javier González43d47122018-03-30 00:05:09 +02001023 ret = pblk_line_mg_init(pblk);
1024 if (ret)
1025 return ret;
1026
1027 ret = pblk_luns_init(pblk);
1028 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001029 goto fail_free_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001030
Javier González32ef9412018-03-30 00:05:20 +02001031 chunk_meta = pblk_chunk_get_meta(pblk);
1032 if (IS_ERR(chunk_meta)) {
1033 ret = PTR_ERR(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001034 goto fail_free_luns;
1035 }
1036
1037 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
1038 GFP_KERNEL);
1039 if (!pblk->lines) {
1040 ret = -ENOMEM;
Javier González32ef9412018-03-30 00:05:20 +02001041 goto fail_free_chunk_meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001042 }
1043
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001044 for (i = 0; i < l_mg->nr_lines; i++) {
1045 line = &pblk->lines[i];
1046
Javier González32ef9412018-03-30 00:05:20 +02001047 ret = pblk_alloc_line_meta(pblk, line);
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001048 if (ret)
Javier González43d47122018-03-30 00:05:09 +02001049 goto fail_free_lines;
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001050
Javier González32ef9412018-03-30 00:05:20 +02001051 nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001052 }
1053
Javier González2deeefc2018-06-01 15:04:15 +02001054 if (!nr_free_chks) {
1055 pr_err("pblk: too many bad blocks prevent for sane instance\n");
1056 return -EINTR;
1057 }
1058
Javier González32ef9412018-03-30 00:05:20 +02001059 pblk_set_provision(pblk, nr_free_chks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001060
Javier González32ef9412018-03-30 00:05:20 +02001061 kfree(chunk_meta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001062 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001063
Javier González43d47122018-03-30 00:05:09 +02001064fail_free_lines:
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001065 while (--i >= 0)
Javier Gonzáleze411b332018-03-30 00:04:59 +02001066 pblk_line_meta_free(&pblk->lines[i]);
Javier González43d47122018-03-30 00:05:09 +02001067 kfree(pblk->lines);
Javier González32ef9412018-03-30 00:05:20 +02001068fail_free_chunk_meta:
1069 kfree(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001070fail_free_luns:
1071 kfree(pblk->luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001072fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +02001073 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001074
1075 return ret;
1076}
1077
1078static int pblk_writer_init(struct pblk *pblk)
1079{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001080 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
1081 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001082 int err = PTR_ERR(pblk->writer_ts);
1083
1084 if (err != -EINTR)
1085 pr_err("pblk: could not allocate writer kthread (%d)\n",
1086 err);
1087 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001088 }
1089
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001090 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1091 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1092
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001093 return 0;
1094}
1095
1096static void pblk_writer_stop(struct pblk *pblk)
1097{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001098 /* The pipeline must be stopped and the write buffer emptied before the
1099 * write thread is stopped
1100 */
1101 WARN(pblk_rb_read_count(&pblk->rwb),
1102 "Stopping not fully persisted write buffer\n");
1103
1104 WARN(pblk_rb_sync_count(&pblk->rwb),
1105 "Stopping not fully synced write buffer\n");
1106
Hans Holmberg7be970b2018-03-30 00:04:56 +02001107 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001108 if (pblk->writer_ts)
1109 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001110}
1111
1112static void pblk_free(struct pblk *pblk)
1113{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001114 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001115 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001116 pblk_rwb_free(pblk);
1117 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001118
1119 kfree(pblk);
1120}
1121
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001122static void pblk_tear_down(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001123{
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001124 if (graceful)
1125 __pblk_pipeline_flush(pblk);
1126 __pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001127 pblk_writer_stop(pblk);
1128 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001129 pblk_rl_free(&pblk->rl);
1130
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001131 pr_debug("pblk: consistent tear down (graceful:%d)\n", graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001132}
1133
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001134static void pblk_exit(void *private, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001135{
1136 struct pblk *pblk = private;
1137
1138 down_write(&pblk_lock);
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001139 pblk_gc_exit(pblk, graceful);
1140 pblk_tear_down(pblk, graceful);
Hans Holmbergc5586192017-10-13 14:46:40 +02001141
1142#ifdef CONFIG_NVM_DEBUG
1143 pr_info("pblk exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
1144#endif
1145
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001146 pblk_free(pblk);
1147 up_write(&pblk_lock);
1148}
1149
1150static sector_t pblk_capacity(void *private)
1151{
1152 struct pblk *pblk = private;
1153
1154 return pblk->capacity * NR_PHY_IN_LOG;
1155}
1156
1157static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1158 int flags)
1159{
1160 struct nvm_geo *geo = &dev->geo;
1161 struct request_queue *bqueue = dev->q;
1162 struct request_queue *tqueue = tdisk->queue;
1163 struct pblk *pblk;
1164 int ret;
1165
Javier González3b2a3ad2018-03-30 00:05:21 +02001166 /* pblk supports 1.2 and 2.0 versions */
1167 if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1168 geo->version == NVM_OCSSD_SPEC_20)) {
Javier González7ad50392018-03-30 00:05:18 +02001169 pr_err("pblk: OCSSD version not supported (%u)\n",
1170 geo->version);
1171 return ERR_PTR(-EINVAL);
1172 }
1173
1174 if (geo->version == NVM_OCSSD_SPEC_12 && geo->dom & NVM_RSP_L2P) {
Rakesh Pandit4e76af52017-10-13 14:45:57 +02001175 pr_err("pblk: host-side L2P table not supported. (%x)\n",
Javier González7ad50392018-03-30 00:05:18 +02001176 geo->dom);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001177 return ERR_PTR(-EINVAL);
1178 }
1179
1180 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1181 if (!pblk)
1182 return ERR_PTR(-ENOMEM);
1183
1184 pblk->dev = dev;
1185 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +02001186 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg3e3a5b82017-10-13 14:46:34 +02001187 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001188
Hans Holmberg6a3abf52018-06-01 16:41:05 +02001189 spin_lock_init(&pblk->resubmit_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001190 spin_lock_init(&pblk->trans_lock);
1191 spin_lock_init(&pblk->lock);
1192
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001193#ifdef CONFIG_NVM_DEBUG
1194 atomic_long_set(&pblk->inflight_writes, 0);
1195 atomic_long_set(&pblk->padded_writes, 0);
1196 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001197 atomic_long_set(&pblk->req_writes, 0);
1198 atomic_long_set(&pblk->sub_writes, 0);
1199 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001200 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001201 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001202 atomic_long_set(&pblk->sync_reads, 0);
1203 atomic_long_set(&pblk->recov_writes, 0);
1204 atomic_long_set(&pblk->recov_writes, 0);
1205 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001206 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001207#endif
1208
1209 atomic_long_set(&pblk->read_failed, 0);
1210 atomic_long_set(&pblk->read_empty, 0);
1211 atomic_long_set(&pblk->read_high_ecc, 0);
1212 atomic_long_set(&pblk->read_failed_gc, 0);
1213 atomic_long_set(&pblk->write_failed, 0);
1214 atomic_long_set(&pblk->erase_failed, 0);
1215
Javier González43d47122018-03-30 00:05:09 +02001216 ret = pblk_core_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001217 if (ret) {
Javier González43d47122018-03-30 00:05:09 +02001218 pr_err("pblk: could not initialize core\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001219 goto fail;
1220 }
1221
1222 ret = pblk_lines_init(pblk);
1223 if (ret) {
1224 pr_err("pblk: could not initialize lines\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001225 goto fail_free_core;
1226 }
1227
Javier González43d47122018-03-30 00:05:09 +02001228 ret = pblk_rwb_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001229 if (ret) {
Javier González43d47122018-03-30 00:05:09 +02001230 pr_err("pblk: could not initialize write buffer\n");
1231 goto fail_free_lines;
1232 }
1233
1234 ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1235 if (ret) {
1236 pr_err("pblk: could not initialize maps\n");
1237 goto fail_free_rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001238 }
1239
1240 ret = pblk_writer_init(pblk);
1241 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001242 if (ret != -EINTR)
1243 pr_err("pblk: could not initialize write thread\n");
Javier González43d47122018-03-30 00:05:09 +02001244 goto fail_free_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001245 }
1246
1247 ret = pblk_gc_init(pblk);
1248 if (ret) {
1249 pr_err("pblk: could not initialize gc\n");
1250 goto fail_stop_writer;
1251 }
1252
1253 /* inherit the size from the underlying device */
1254 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1255 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1256
1257 blk_queue_write_cache(tqueue, true, false);
1258
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001259 tqueue->limits.discard_granularity = geo->clba * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001260 tqueue->limits.discard_alignment = 0;
1261 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001262 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001263
Javier González30d82a862018-01-05 14:16:19 +01001264 pr_info("pblk(%s): luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
1265 tdisk->disk_name,
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001266 geo->all_luns, pblk->l_mg.nr_lines,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001267 (unsigned long long)pblk->rl.nr_secs,
1268 pblk->rwb.nr_entries);
1269
1270 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001271
1272 /* Check if we need to start GC */
1273 pblk_gc_should_kick(pblk);
1274
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001275 return pblk;
1276
1277fail_stop_writer:
1278 pblk_writer_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001279fail_free_l2p:
1280 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001281fail_free_rwb:
1282 pblk_rwb_free(pblk);
1283fail_free_lines:
1284 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001285fail_free_core:
1286 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001287fail:
1288 kfree(pblk);
1289 return ERR_PTR(ret);
1290}
1291
1292/* physical block device target */
1293static struct nvm_tgt_type tt_pblk = {
1294 .name = "pblk",
1295 .version = {1, 0, 0},
1296
1297 .make_rq = pblk_make_rq,
1298 .capacity = pblk_capacity,
1299
1300 .init = pblk_init,
1301 .exit = pblk_exit,
1302
1303 .sysfs_init = pblk_sysfs_init,
1304 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001305 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001306};
1307
1308static int __init pblk_module_init(void)
1309{
NeilBrownb25d5232017-06-18 14:38:58 +10001310 int ret;
1311
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001312 ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
1313 if (ret)
1314 return ret;
NeilBrownb25d5232017-06-18 14:38:58 +10001315 ret = nvm_register_tgt_type(&tt_pblk);
1316 if (ret)
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001317 bioset_exit(&pblk_bio_set);
NeilBrownb25d5232017-06-18 14:38:58 +10001318 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001319}
1320
1321static void pblk_module_exit(void)
1322{
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001323 bioset_exit(&pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001324 nvm_unregister_tgt_type(&tt_pblk);
1325}
1326
1327module_init(pblk_module_init);
1328module_exit(pblk_module_exit);
1329MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1330MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1331MODULE_LICENSE("GPL v2");
1332MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");