blob: 7ef8249108f0508ed45dd7695593089f5c340864 [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"
Hans Holmberg4c44abf2018-10-09 13:11:52 +020022#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020023
Wei Yongjun21ff1392018-06-05 09:15:12 +000024static unsigned int write_buffer_size;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +020025
26module_param(write_buffer_size, uint, 0644);
27MODULE_PARM_DESC(write_buffer_size, "number of entries in a write buffer");
28
Hans Holmberg1864de92018-10-09 13:11:58 +020029struct pblk_global_caches {
30 struct kmem_cache *ws;
31 struct kmem_cache *rec;
32 struct kmem_cache *g_rq;
33 struct kmem_cache *w_rq;
34
35 struct kref kref;
36
37 struct mutex mutex; /* Ensures consistency between
38 * caches and kref
39 */
40};
41
42static struct pblk_global_caches pblk_caches = {
43 .mutex = __MUTEX_INITIALIZER(pblk_caches.mutex),
44 .kref = KREF_INIT(0),
45};
46
Kent Overstreetb906bbb2018-05-20 18:25:50 -040047struct bio_set pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020048
49static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
50 struct bio *bio)
51{
52 int ret;
53
54 /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
55 * constraint. Writes can be of arbitrary size.
56 */
57 if (bio_data_dir(bio) == READ) {
NeilBrownaf67c312017-06-18 14:38:57 +100058 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020059 ret = pblk_submit_read(pblk, bio);
60 if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
61 bio_put(bio);
62
63 return ret;
64 }
65
66 /* Prevent deadlock in the case of a modest LUN configuration and large
67 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
68 * available for user I/O.
69 */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +020070 if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
NeilBrownaf67c312017-06-18 14:38:57 +100071 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020072
73 return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
74}
75
76static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
77{
78 struct pblk *pblk = q->queuedata;
79
80 if (bio_op(bio) == REQ_OP_DISCARD) {
81 pblk_discard(pblk, bio);
82 if (!(bio->bi_opf & REQ_PREFLUSH)) {
83 bio_endio(bio);
84 return BLK_QC_T_NONE;
85 }
86 }
87
88 switch (pblk_rw_io(q, pblk, bio)) {
89 case NVM_IO_ERR:
90 bio_io_error(bio);
91 break;
92 case NVM_IO_DONE:
93 bio_endio(bio);
94 break;
95 }
96
97 return BLK_QC_T_NONE;
98}
99
Hans Holmbergc5586192017-10-13 14:46:40 +0200100static size_t pblk_trans_map_size(struct pblk *pblk)
101{
102 int entry_size = 8;
103
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200104 if (pblk->addrf_len < 32)
Hans Holmbergc5586192017-10-13 14:46:40 +0200105 entry_size = 4;
106
107 return entry_size * pblk->rl.nr_secs;
108}
109
Matias Bjørling880eda52018-07-13 10:48:37 +0200110#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergc5586192017-10-13 14:46:40 +0200111static u32 pblk_l2p_crc(struct pblk *pblk)
112{
113 size_t map_size;
114 u32 crc = ~(u32)0;
115
116 map_size = pblk_trans_map_size(pblk);
117 crc = crc32_le(crc, pblk->trans_map, map_size);
118 return crc;
119}
120#endif
121
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200122static void pblk_l2p_free(struct pblk *pblk)
123{
124 vfree(pblk->trans_map);
125}
126
Javier González43d47122018-03-30 00:05:09 +0200127static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
128{
129 struct pblk_line *line = NULL;
130
131 if (factory_init) {
132 pblk_setup_uuid(pblk);
133 } else {
134 line = pblk_recov_l2p(pblk);
135 if (IS_ERR(line)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200136 pblk_err(pblk, "could not recover l2p table\n");
Javier González43d47122018-03-30 00:05:09 +0200137 return -EFAULT;
138 }
139 }
140
Matias Bjørling880eda52018-07-13 10:48:37 +0200141#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +0200142 pblk_info(pblk, "init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Javier González43d47122018-03-30 00:05:09 +0200143#endif
144
145 /* Free full lines directly as GC has not been started yet */
146 pblk_gc_free_full_lines(pblk);
147
148 if (!line) {
149 /* Configure next line for user data */
150 line = pblk_line_get_first_data(pblk);
Javier González1d8b33e2018-06-01 15:04:16 +0200151 if (!line)
Javier González43d47122018-03-30 00:05:09 +0200152 return -EFAULT;
Javier González43d47122018-03-30 00:05:09 +0200153 }
154
155 return 0;
156}
157
158static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200159{
160 sector_t i;
161 struct ppa_addr ppa;
Hans Holmbergc5586192017-10-13 14:46:40 +0200162 size_t map_size;
Javier González1d8b33e2018-06-01 15:04:16 +0200163 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200164
Hans Holmbergc5586192017-10-13 14:46:40 +0200165 map_size = pblk_trans_map_size(pblk);
166 pblk->trans_map = vmalloc(map_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200167 if (!pblk->trans_map)
168 return -ENOMEM;
169
170 pblk_ppa_set_empty(&ppa);
171
172 for (i = 0; i < pblk->rl.nr_secs; i++)
173 pblk_trans_map_set(pblk, i, ppa);
174
Javier González1d8b33e2018-06-01 15:04:16 +0200175 ret = pblk_l2p_recover(pblk, factory_init);
176 if (ret)
177 vfree(pblk->trans_map);
178
179 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200180}
181
182static void pblk_rwb_free(struct pblk *pblk)
183{
184 if (pblk_rb_tear_down_check(&pblk->rwb))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200185 pblk_err(pblk, "write buffer error on tear down\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200186
187 pblk_rb_data_free(&pblk->rwb);
188 vfree(pblk_rb_entries_ref(&pblk->rwb));
189}
190
191static int pblk_rwb_init(struct pblk *pblk)
192{
193 struct nvm_tgt_dev *dev = pblk->dev;
194 struct nvm_geo *geo = &dev->geo;
195 struct pblk_rb_entry *entries;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200196 unsigned long nr_entries, buffer_size;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200197 unsigned int power_size, power_seg_sz;
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200198 int pgs_in_buffer;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200199
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200200 pgs_in_buffer = max(geo->mw_cunits, geo->ws_opt) * geo->all_luns;
201
202 if (write_buffer_size && (write_buffer_size > pgs_in_buffer))
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200203 buffer_size = write_buffer_size;
204 else
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200205 buffer_size = pgs_in_buffer;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200206
207 nr_entries = pblk_rb_calculate_size(buffer_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200208
Kees Cookfad953c2018-06-12 14:27:37 -0700209 entries = vzalloc(array_size(nr_entries, sizeof(struct pblk_rb_entry)));
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200210 if (!entries)
211 return -ENOMEM;
212
213 power_size = get_count_order(nr_entries);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200214 power_seg_sz = get_count_order(geo->csecs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200215
216 return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
217}
218
219/* Minimum pages needed within a lun */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200220#define ADDR_POOL_SIZE 64
221
Matias Bjørling4e495a42018-07-13 10:48:42 +0200222static int pblk_set_addrf_12(struct pblk *pblk, struct nvm_geo *geo,
223 struct nvm_addrf_12 *dst)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200224{
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200225 struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
226 int power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200227
228 /* Re-calculate channel and lun format to adapt to configuration */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200229 power_len = get_count_order(geo->num_ch);
230 if (1 << power_len != geo->num_ch) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200231 pblk_err(pblk, "supports only power-of-two channel config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200232 return -EINVAL;
233 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200234 dst->ch_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200235
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200236 power_len = get_count_order(geo->num_lun);
237 if (1 << power_len != geo->num_lun) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200238 pblk_err(pblk, "supports only power-of-two LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200239 return -EINVAL;
240 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200241 dst->lun_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200242
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200243 dst->blk_len = src->blk_len;
244 dst->pg_len = src->pg_len;
245 dst->pln_len = src->pln_len;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200246 dst->sec_len = src->sec_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200247
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200248 dst->sec_offset = 0;
249 dst->pln_offset = dst->sec_len;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200250 dst->ch_offset = dst->pln_offset + dst->pln_len;
251 dst->lun_offset = dst->ch_offset + dst->ch_len;
252 dst->pg_offset = dst->lun_offset + dst->lun_len;
253 dst->blk_offset = dst->pg_offset + dst->pg_len;
254
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200255 dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200256 dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
257 dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
258 dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
259 dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
260 dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
261
262 return dst->blk_offset + src->blk_len;
263}
264
Javier González3b2a3ad2018-03-30 00:05:21 +0200265static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
266 struct pblk_addrf *udst)
267{
268 struct nvm_addrf *src = &geo->addrf;
269
270 adst->ch_len = get_count_order(geo->num_ch);
271 adst->lun_len = get_count_order(geo->num_lun);
272 adst->chk_len = src->chk_len;
273 adst->sec_len = src->sec_len;
274
275 adst->sec_offset = 0;
276 adst->ch_offset = adst->sec_len;
277 adst->lun_offset = adst->ch_offset + adst->ch_len;
278 adst->chk_offset = adst->lun_offset + adst->lun_len;
279
280 adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
281 adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
282 adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
283 adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
284
285 udst->sec_stripe = geo->ws_opt;
286 udst->ch_stripe = geo->num_ch;
287 udst->lun_stripe = geo->num_lun;
288
289 udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
290 udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
291
292 return adst->chk_offset + adst->chk_len;
293}
294
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200295static int pblk_set_addrf(struct pblk *pblk)
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200296{
297 struct nvm_tgt_dev *dev = pblk->dev;
298 struct nvm_geo *geo = &dev->geo;
299 int mod;
300
Javier González3b2a3ad2018-03-30 00:05:21 +0200301 switch (geo->version) {
302 case NVM_OCSSD_SPEC_12:
303 div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
304 if (mod) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200305 pblk_err(pblk, "bad configuration of sectors/pages\n");
Javier González3b2a3ad2018-03-30 00:05:21 +0200306 return -EINVAL;
307 }
308
Matias Bjørling4e495a42018-07-13 10:48:42 +0200309 pblk->addrf_len = pblk_set_addrf_12(pblk, geo,
310 (void *)&pblk->addrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200311 break;
312 case NVM_OCSSD_SPEC_20:
313 pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
Matias Bjørling4e495a42018-07-13 10:48:42 +0200314 &pblk->uaddrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200315 break;
316 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200317 pblk_err(pblk, "OCSSD revision not supported (%d)\n",
Javier González3b2a3ad2018-03-30 00:05:21 +0200318 geo->version);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200319 return -EINVAL;
320 }
321
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200322 return 0;
323}
324
Hans Holmberg1864de92018-10-09 13:11:58 +0200325static int pblk_create_global_caches(void)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200326{
Hans Holmberg1864de92018-10-09 13:11:58 +0200327
328 pblk_caches.ws = kmem_cache_create("pblk_blk_ws",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200329 sizeof(struct pblk_line_ws), 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200330 if (!pblk_caches.ws)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200331 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200332
Hans Holmberg1864de92018-10-09 13:11:58 +0200333 pblk_caches.rec = kmem_cache_create("pblk_rec",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200334 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200335 if (!pblk_caches.rec)
336 goto fail_destroy_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200337
Hans Holmberg1864de92018-10-09 13:11:58 +0200338 pblk_caches.g_rq = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200339 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200340 if (!pblk_caches.g_rq)
341 goto fail_destroy_rec;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200342
Hans Holmberg1864de92018-10-09 13:11:58 +0200343 pblk_caches.w_rq = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200344 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200345 if (!pblk_caches.w_rq)
346 goto fail_destroy_g_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200347
348 return 0;
Hans Holmberg1864de92018-10-09 13:11:58 +0200349
350fail_destroy_g_rq:
351 kmem_cache_destroy(pblk_caches.g_rq);
352fail_destroy_rec:
353 kmem_cache_destroy(pblk_caches.rec);
354fail_destroy_ws:
355 kmem_cache_destroy(pblk_caches.ws);
356
357 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200358}
359
Hans Holmberg1864de92018-10-09 13:11:58 +0200360static int pblk_get_global_caches(void)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200361{
Hans Holmberg1864de92018-10-09 13:11:58 +0200362 int ret;
363
364 mutex_lock(&pblk_caches.mutex);
365
366 if (kref_read(&pblk_caches.kref) > 0) {
367 kref_get(&pblk_caches.kref);
368 mutex_unlock(&pblk_caches.mutex);
369 return 0;
370 }
371
372 ret = pblk_create_global_caches();
373
374 if (!ret)
375 kref_get(&pblk_caches.kref);
376
377 mutex_unlock(&pblk_caches.mutex);
378
379 return ret;
380}
381
382static void pblk_destroy_global_caches(struct kref *ref)
383{
384 struct pblk_global_caches *c;
385
386 c = container_of(ref, struct pblk_global_caches, kref);
387
388 kmem_cache_destroy(c->ws);
389 kmem_cache_destroy(c->rec);
390 kmem_cache_destroy(c->g_rq);
391 kmem_cache_destroy(c->w_rq);
392}
393
394static void pblk_put_global_caches(void)
395{
396 mutex_lock(&pblk_caches.mutex);
397 kref_put(&pblk_caches.kref, pblk_destroy_global_caches);
398 mutex_unlock(&pblk_caches.mutex);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200399}
400
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200401static int pblk_core_init(struct pblk *pblk)
402{
403 struct nvm_tgt_dev *dev = pblk->dev;
404 struct nvm_geo *geo = &dev->geo;
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400405 int ret, max_write_ppas;
Javier González43d47122018-03-30 00:05:09 +0200406
407 atomic64_set(&pblk->user_wa, 0);
408 atomic64_set(&pblk->pad_wa, 0);
409 atomic64_set(&pblk->gc_wa, 0);
410 pblk->user_rst_wa = 0;
411 pblk->pad_rst_wa = 0;
412 pblk->gc_rst_wa = 0;
413
414 atomic64_set(&pblk->nr_flush, 0);
415 pblk->nr_flush_rst = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200416
Matias Bjørling8bbd45d2018-10-09 13:11:38 +0200417 pblk->min_write_pgs = geo->ws_opt;
Javier González43d47122018-03-30 00:05:09 +0200418 max_write_ppas = pblk->min_write_pgs * geo->all_luns;
419 pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
420 pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
421
Kees Cook6396bb22018-06-12 14:03:40 -0700422 pblk->pad_dist = kcalloc(pblk->min_write_pgs - 1, sizeof(atomic64_t),
Javier González43d47122018-03-30 00:05:09 +0200423 GFP_KERNEL);
424 if (!pblk->pad_dist)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200425 return -ENOMEM;
426
Hans Holmberg1864de92018-10-09 13:11:58 +0200427 if (pblk_get_global_caches())
Javier González43d47122018-03-30 00:05:09 +0200428 goto fail_free_pad_dist;
429
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200430 /* Internal bios can be at most the sectors signaled by the device. */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400431 ret = mempool_init_page_pool(&pblk->page_bio_pool, NVM_MAX_VLBA, 0);
432 if (ret)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200433 goto free_global_caches;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200434
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400435 ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
Hans Holmberg1864de92018-10-09 13:11:58 +0200436 pblk_caches.ws);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400437 if (ret)
Javier Gonzálezbd432412017-10-13 14:46:06 +0200438 goto free_page_bio_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200439
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400440 ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200441 pblk_caches.rec);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400442 if (ret)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200443 goto free_gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200444
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400445 ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200446 pblk_caches.g_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400447 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200448 goto free_rec_pool;
449
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400450 ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200451 pblk_caches.g_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400452 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200453 goto free_r_rq_pool;
454
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400455 ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200456 pblk_caches.w_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400457 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200458 goto free_e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200459
Javier Gonzálezef576492017-06-26 11:57:28 +0200460 pblk->close_wq = alloc_workqueue("pblk-close-wq",
461 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
462 if (!pblk->close_wq)
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200463 goto free_w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200464
Javier Gonzálezef576492017-06-26 11:57:28 +0200465 pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
466 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
467 if (!pblk->bb_wq)
468 goto free_close_wq;
469
Javier González7bd4d372017-10-13 14:46:23 +0200470 pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
471 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
472 if (!pblk->r_end_wq)
Javier Gonzálezef576492017-06-26 11:57:28 +0200473 goto free_bb_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200474
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200475 if (pblk_set_addrf(pblk))
Javier González7bd4d372017-10-13 14:46:23 +0200476 goto free_r_end_wq;
477
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200478 INIT_LIST_HEAD(&pblk->compl_list);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200479 INIT_LIST_HEAD(&pblk->resubmit_list);
Javier González43d47122018-03-30 00:05:09 +0200480
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200481 return 0;
482
Javier González7bd4d372017-10-13 14:46:23 +0200483free_r_end_wq:
484 destroy_workqueue(pblk->r_end_wq);
Javier Gonzálezef576492017-06-26 11:57:28 +0200485free_bb_wq:
486 destroy_workqueue(pblk->bb_wq);
487free_close_wq:
488 destroy_workqueue(pblk->close_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200489free_w_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400490 mempool_exit(&pblk->w_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200491free_e_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400492 mempool_exit(&pblk->e_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200493free_r_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400494 mempool_exit(&pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200495free_rec_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400496 mempool_exit(&pblk->rec_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200497free_gen_ws_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400498 mempool_exit(&pblk->gen_ws_pool);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200499free_page_bio_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400500 mempool_exit(&pblk->page_bio_pool);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200501free_global_caches:
Hans Holmberg1864de92018-10-09 13:11:58 +0200502 pblk_put_global_caches();
Javier González43d47122018-03-30 00:05:09 +0200503fail_free_pad_dist:
504 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200505 return -ENOMEM;
506}
507
508static void pblk_core_free(struct pblk *pblk)
509{
Javier Gonzálezef576492017-06-26 11:57:28 +0200510 if (pblk->close_wq)
511 destroy_workqueue(pblk->close_wq);
512
Javier González7bd4d372017-10-13 14:46:23 +0200513 if (pblk->r_end_wq)
514 destroy_workqueue(pblk->r_end_wq);
515
Javier Gonzálezef576492017-06-26 11:57:28 +0200516 if (pblk->bb_wq)
517 destroy_workqueue(pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200518
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400519 mempool_exit(&pblk->page_bio_pool);
520 mempool_exit(&pblk->gen_ws_pool);
521 mempool_exit(&pblk->rec_pool);
522 mempool_exit(&pblk->r_rq_pool);
523 mempool_exit(&pblk->e_rq_pool);
524 mempool_exit(&pblk->w_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200525
Hans Holmberg1864de92018-10-09 13:11:58 +0200526 pblk_put_global_caches();
Javier González43d47122018-03-30 00:05:09 +0200527 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200528}
529
Javier Gonzáleze411b332018-03-30 00:04:59 +0200530static void pblk_line_mg_free(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200531{
532 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
533 int i;
534
535 kfree(l_mg->bb_template);
536 kfree(l_mg->bb_aux);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200537 kfree(l_mg->vsc_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200538
539 for (i = 0; i < PBLK_DATA_LINES; i++) {
Javier Gonzálezf680f192017-06-26 11:57:21 +0200540 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200541 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200542 kfree(l_mg->eline_meta[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200543 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200544
545 mempool_destroy(l_mg->bitmap_pool);
546 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200547}
548
Hans Holmberg48b8d202018-06-01 16:41:06 +0200549static void pblk_line_meta_free(struct pblk_line_mgmt *l_mg,
550 struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200551{
Hans Holmberg48b8d202018-06-01 16:41:06 +0200552 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
553
Javier Gonzáleze411b332018-03-30 00:04:59 +0200554 kfree(line->blk_bitmap);
555 kfree(line->erase_bitmap);
Javier González32ef9412018-03-30 00:05:20 +0200556 kfree(line->chks);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200557
558 pblk_mfree(w_err_gc->lba_list, l_mg->emeta_alloc_type);
559 kfree(w_err_gc);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200560}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200561
Javier Gonzáleze411b332018-03-30 00:04:59 +0200562static void pblk_lines_free(struct pblk *pblk)
563{
564 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
565 struct pblk_line *line;
566 int i;
567
568 spin_lock(&l_mg->free_lock);
569 for (i = 0; i < l_mg->nr_lines; i++) {
570 line = &pblk->lines[i];
571
Javier González8e55c072018-06-01 15:04:22 +0200572 pblk_line_free(line);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200573 pblk_line_meta_free(l_mg, line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200574 }
575 spin_unlock(&l_mg->free_lock);
Javier González43d47122018-03-30 00:05:09 +0200576
577 pblk_line_mg_free(pblk);
578
579 kfree(pblk->luns);
580 kfree(pblk->lines);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200581}
582
Javier González43d47122018-03-30 00:05:09 +0200583static int pblk_luns_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200584{
585 struct nvm_tgt_dev *dev = pblk->dev;
586 struct nvm_geo *geo = &dev->geo;
587 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200588 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200589
590 /* TODO: Implement unbalanced LUN support */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200591 if (geo->num_lun < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200592 pblk_err(pblk, "unbalanced LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200593 return -EINVAL;
594 }
595
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100596 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
597 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200598 if (!pblk->luns)
599 return -ENOMEM;
600
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100601 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200602 /* Stripe across channels */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200603 int ch = i % geo->num_ch;
604 int lun_raw = i / geo->num_ch;
605 int lunid = lun_raw + ch * geo->num_lun;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200606
607 rlun = &pblk->luns[i];
Javier González43d47122018-03-30 00:05:09 +0200608 rlun->bppa = dev->luns[lunid];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200609
610 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200611 }
612
613 return 0;
614}
615
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200616/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200617static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200618{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200619 struct pblk_line_meta *lm = &pblk->lm;
620 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
621 struct nvm_tgt_dev *dev = pblk->dev;
622 struct nvm_geo *geo = &dev->geo;
623
624 /* Round to sector size so that lba_list starts on its own sector */
625 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200626 sizeof(struct line_emeta) + lm->blk_bitmap_len +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200627 sizeof(struct wa_counters), geo->csecs);
628 lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200629
630 /* Round to sector size so that vsc_list starts on its own sector */
631 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
632 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200633 geo->csecs);
634 lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200635
636 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200637 geo->csecs);
638 lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200639
640 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
641
642 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200643}
644
645static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
646{
647 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100648 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
649 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200650 struct nvm_geo *geo = &dev->geo;
651 sector_t provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100652 int sec_meta, blk_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200653
Javier Gonzáleze5392732018-01-05 14:16:14 +0100654 if (geo->op == NVM_TARGET_DEFAULT_OP)
655 pblk->op = PBLK_DEFAULT_OP;
656 else
657 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200658
659 provisioned = nr_free_blks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100660 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200661 sector_div(provisioned, 100);
662
Javier Gonzáleza7689932018-01-05 14:16:13 +0100663 pblk->op_blks = nr_free_blks - provisioned;
664
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200665 /* Internally pblk manages all free blocks, but all calculations based
666 * on user capacity consider only provisioned blocks
667 */
668 pblk->rl.total_blocks = nr_free_blks;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200669 pblk->rl.nr_secs = nr_free_blks * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100670
671 /* Consider sectors used for metadata */
672 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200673 blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100674
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200675 pblk->capacity = (provisioned - blk_meta) * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100676
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200677 atomic_set(&pblk->rl.free_blocks, nr_free_blks);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100678 atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200679}
680
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200681static int pblk_setup_line_meta_chk(struct pblk *pblk, struct pblk_line *line,
Javier González32ef9412018-03-30 00:05:20 +0200682 struct nvm_chk_meta *meta)
683{
684 struct nvm_tgt_dev *dev = pblk->dev;
685 struct nvm_geo *geo = &dev->geo;
686 struct pblk_line_meta *lm = &pblk->lm;
687 int i, nr_bad_chks = 0;
688
689 for (i = 0; i < lm->blk_per_line; i++) {
690 struct pblk_lun *rlun = &pblk->luns[i];
691 struct nvm_chk_meta *chunk;
692 struct nvm_chk_meta *chunk_meta;
693 struct ppa_addr ppa;
694 int pos;
695
696 ppa = rlun->bppa;
697 pos = pblk_ppa_to_pos(geo, ppa);
698 chunk = &line->chks[pos];
699
700 ppa.m.chk = line->id;
701 chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
702
703 chunk->state = chunk_meta->state;
704 chunk->type = chunk_meta->type;
705 chunk->wi = chunk_meta->wi;
706 chunk->slba = chunk_meta->slba;
707 chunk->cnlb = chunk_meta->cnlb;
708 chunk->wp = chunk_meta->wp;
709
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200710 trace_pblk_chunk_state(pblk_disk_name(pblk), &ppa,
711 chunk->state);
712
Javier González32ef9412018-03-30 00:05:20 +0200713 if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
714 WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
715 continue;
716 }
717
Javier González6f9c9602018-06-01 15:04:23 +0200718 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
719 continue;
720
Javier González32ef9412018-03-30 00:05:20 +0200721 set_bit(pos, line->blk_bitmap);
722 nr_bad_chks++;
723 }
724
725 return nr_bad_chks;
726}
727
728static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
729 void *chunk_meta, int line_id)
730{
Javier González32ef9412018-03-30 00:05:20 +0200731 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
732 struct pblk_line_meta *lm = &pblk->lm;
733 long nr_bad_chks, chk_in_line;
734
735 line->pblk = pblk;
736 line->id = line_id;
737 line->type = PBLK_LINETYPE_FREE;
738 line->state = PBLK_LINESTATE_NEW;
739 line->gc_group = PBLK_LINEGC_NONE;
740 line->vsc = &l_mg->vsc_list[line_id];
741 spin_lock_init(&line->lock);
742
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200743 nr_bad_chks = pblk_setup_line_meta_chk(pblk, line, chunk_meta);
Javier González32ef9412018-03-30 00:05:20 +0200744
745 chk_in_line = lm->blk_per_line - nr_bad_chks;
746 if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
747 chk_in_line < lm->min_blk_line) {
748 line->state = PBLK_LINESTATE_BAD;
749 list_add_tail(&line->list, &l_mg->bad_list);
750 return 0;
751 }
752
753 atomic_set(&line->blk_in_line, chk_in_line);
754 list_add_tail(&line->list, &l_mg->free_list);
755 l_mg->nr_free_lines++;
756
757 return chk_in_line;
758}
759
760static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200761{
Javier González43d47122018-03-30 00:05:09 +0200762 struct pblk_line_meta *lm = &pblk->lm;
763
764 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
765 if (!line->blk_bitmap)
766 return -ENOMEM;
767
768 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200769 if (!line->erase_bitmap)
770 goto free_blk_bitmap;
771
Javier González43d47122018-03-30 00:05:09 +0200772
Kees Cook6da2ec52018-06-12 13:55:00 -0700773 line->chks = kmalloc_array(lm->blk_per_line,
774 sizeof(struct nvm_chk_meta), GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200775 if (!line->chks)
776 goto free_erase_bitmap;
777
778 line->w_err_gc = kzalloc(sizeof(struct pblk_w_err_gc), GFP_KERNEL);
779 if (!line->w_err_gc)
780 goto free_chks;
Javier González43d47122018-03-30 00:05:09 +0200781
782 return 0;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200783
784free_chks:
785 kfree(line->chks);
786free_erase_bitmap:
787 kfree(line->erase_bitmap);
788free_blk_bitmap:
789 kfree(line->blk_bitmap);
790 return -ENOMEM;
Javier González43d47122018-03-30 00:05:09 +0200791}
792
793static int pblk_line_mg_init(struct pblk *pblk)
794{
795 struct nvm_tgt_dev *dev = pblk->dev;
796 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200797 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
798 struct pblk_line_meta *lm = &pblk->lm;
Javier González43d47122018-03-30 00:05:09 +0200799 int i, bb_distance;
800
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200801 l_mg->nr_lines = geo->num_chk;
Javier González43d47122018-03-30 00:05:09 +0200802 l_mg->log_line = l_mg->data_line = NULL;
803 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
804 l_mg->nr_free_lines = 0;
805 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
806
807 INIT_LIST_HEAD(&l_mg->free_list);
808 INIT_LIST_HEAD(&l_mg->corrupt_list);
809 INIT_LIST_HEAD(&l_mg->bad_list);
810 INIT_LIST_HEAD(&l_mg->gc_full_list);
811 INIT_LIST_HEAD(&l_mg->gc_high_list);
812 INIT_LIST_HEAD(&l_mg->gc_mid_list);
813 INIT_LIST_HEAD(&l_mg->gc_low_list);
814 INIT_LIST_HEAD(&l_mg->gc_empty_list);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200815 INIT_LIST_HEAD(&l_mg->gc_werr_list);
Javier González43d47122018-03-30 00:05:09 +0200816
817 INIT_LIST_HEAD(&l_mg->emeta_list);
818
Hans Holmberg48b8d202018-06-01 16:41:06 +0200819 l_mg->gc_lists[0] = &l_mg->gc_werr_list;
820 l_mg->gc_lists[1] = &l_mg->gc_high_list;
821 l_mg->gc_lists[2] = &l_mg->gc_mid_list;
822 l_mg->gc_lists[3] = &l_mg->gc_low_list;
Javier González43d47122018-03-30 00:05:09 +0200823
824 spin_lock_init(&l_mg->free_lock);
825 spin_lock_init(&l_mg->close_lock);
826 spin_lock_init(&l_mg->gc_lock);
827
828 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
829 if (!l_mg->vsc_list)
830 goto fail;
831
832 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
833 if (!l_mg->bb_template)
834 goto fail_free_vsc_list;
835
836 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
837 if (!l_mg->bb_aux)
838 goto fail_free_bb_template;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200839
840 /* smeta is always small enough to fit on a kmalloc memory allocation,
841 * emeta depends on the number of LUNs allocated to the pblk instance
842 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200843 for (i = 0; i < PBLK_DATA_LINES; i++) {
844 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
845 if (!l_mg->sline_meta[i])
846 goto fail_free_smeta;
847 }
848
Hans Holmberg53d82db2018-10-09 13:11:47 +0200849 l_mg->bitmap_cache = kmem_cache_create("pblk_lm_bitmap",
850 lm->sec_bitmap_len, 0, 0, NULL);
851 if (!l_mg->bitmap_cache)
852 goto fail_free_smeta;
853
854 /* the bitmap pool is used for both valid and map bitmaps */
855 l_mg->bitmap_pool = mempool_create_slab_pool(PBLK_DATA_LINES * 2,
856 l_mg->bitmap_cache);
857 if (!l_mg->bitmap_pool)
858 goto fail_destroy_bitmap_cache;
859
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200860 /* emeta allocates three different buffers for managing metadata with
861 * in-memory and in-media layouts
862 */
863 for (i = 0; i < PBLK_DATA_LINES; i++) {
864 struct pblk_emeta *emeta;
865
866 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
867 if (!emeta)
868 goto fail_free_emeta;
869
870 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
871 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
872
873 emeta->buf = vmalloc(lm->emeta_len[0]);
874 if (!emeta->buf) {
875 kfree(emeta);
876 goto fail_free_emeta;
877 }
878
879 emeta->nr_entries = lm->emeta_sec[0];
880 l_mg->eline_meta[i] = emeta;
881 } else {
882 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
883
884 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
885 if (!emeta->buf) {
886 kfree(emeta);
887 goto fail_free_emeta;
888 }
889
890 emeta->nr_entries = lm->emeta_sec[0];
891 l_mg->eline_meta[i] = emeta;
892 }
893 }
894
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200895 for (i = 0; i < l_mg->nr_lines; i++)
896 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
897
Javier González43d47122018-03-30 00:05:09 +0200898 bb_distance = (geo->all_luns) * geo->ws_opt;
899 for (i = 0; i < lm->sec_per_line; i += bb_distance)
900 bitmap_set(l_mg->bb_template, i, geo->ws_opt);
901
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200902 return 0;
903
904fail_free_emeta:
905 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200906 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
907 vfree(l_mg->eline_meta[i]->buf);
908 else
909 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200910 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200911 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200912
913 mempool_destroy(l_mg->bitmap_pool);
914fail_destroy_bitmap_cache:
915 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200916fail_free_smeta:
917 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200918 kfree(l_mg->sline_meta[i]);
Javier González43d47122018-03-30 00:05:09 +0200919 kfree(l_mg->bb_aux);
920fail_free_bb_template:
921 kfree(l_mg->bb_template);
922fail_free_vsc_list:
923 kfree(l_mg->vsc_list);
924fail:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200925 return -ENOMEM;
926}
927
Javier González43d47122018-03-30 00:05:09 +0200928static int pblk_line_meta_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200929{
930 struct nvm_tgt_dev *dev = pblk->dev;
931 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200932 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200933 unsigned int smeta_len, emeta_len;
Javier González43d47122018-03-30 00:05:09 +0200934 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200935
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200936 lm->sec_per_line = geo->clba * geo->all_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100937 lm->blk_per_line = geo->all_luns;
938 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200939 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100940 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200941 lm->mid_thrs = lm->sec_per_line / 2;
942 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100943 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200944
945 /* Calculate necessary pages for smeta. See comment over struct
946 * line_smeta definition
947 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200948 i = 1;
949add_smeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200950 lm->smeta_sec = i * geo->ws_opt;
951 lm->smeta_len = lm->smeta_sec * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200952
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200953 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200954 if (smeta_len > lm->smeta_len) {
955 i++;
956 goto add_smeta_page;
957 }
958
959 /* Calculate necessary pages for emeta. See comment over struct
960 * line_emeta definition
961 */
962 i = 1;
963add_emeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200964 lm->emeta_sec[0] = i * geo->ws_opt;
965 lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200966
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200967 emeta_len = calc_emeta_len(pblk);
968 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200969 i++;
970 goto add_emeta_page;
971 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200972
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100973 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +0200974
975 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100976 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +0200977 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200978 lm->emeta_sec[0], geo->clba);
Javier González21d22872017-10-13 14:46:26 +0200979
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200980 if (lm->min_blk_line > lm->blk_per_line) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200981 pblk_err(pblk, "config. not supported. Min. LUN in line:%d\n",
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200982 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200983 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200984 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200985
Javier González43d47122018-03-30 00:05:09 +0200986 return 0;
987}
988
989static int pblk_lines_init(struct pblk *pblk)
990{
991 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González43d47122018-03-30 00:05:09 +0200992 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +0200993 void *chunk_meta;
994 long nr_free_chks = 0;
Javier González43d47122018-03-30 00:05:09 +0200995 int i, ret;
996
997 ret = pblk_line_meta_init(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200998 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200999 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001000
Javier González43d47122018-03-30 00:05:09 +02001001 ret = pblk_line_mg_init(pblk);
1002 if (ret)
1003 return ret;
1004
1005 ret = pblk_luns_init(pblk);
1006 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001007 goto fail_free_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001008
Matias Bjørlingaff3fb12018-10-09 13:11:36 +02001009 chunk_meta = pblk_get_chunk_meta(pblk);
Javier González32ef9412018-03-30 00:05:20 +02001010 if (IS_ERR(chunk_meta)) {
1011 ret = PTR_ERR(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001012 goto fail_free_luns;
1013 }
1014
1015 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
1016 GFP_KERNEL);
1017 if (!pblk->lines) {
1018 ret = -ENOMEM;
Javier González32ef9412018-03-30 00:05:20 +02001019 goto fail_free_chunk_meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001020 }
1021
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001022 for (i = 0; i < l_mg->nr_lines; i++) {
1023 line = &pblk->lines[i];
1024
Javier González32ef9412018-03-30 00:05:20 +02001025 ret = pblk_alloc_line_meta(pblk, line);
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001026 if (ret)
Javier González43d47122018-03-30 00:05:09 +02001027 goto fail_free_lines;
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001028
Javier González32ef9412018-03-30 00:05:20 +02001029 nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
Hans Holmbergf2937232018-10-09 13:11:53 +02001030
1031 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1032 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001033 }
1034
Javier González2deeefc2018-06-01 15:04:15 +02001035 if (!nr_free_chks) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001036 pblk_err(pblk, "too many bad blocks prevent for sane instance\n");
Javier González2deeefc2018-06-01 15:04:15 +02001037 return -EINTR;
1038 }
1039
Javier González32ef9412018-03-30 00:05:20 +02001040 pblk_set_provision(pblk, nr_free_chks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001041
Javier González090ee262018-10-09 13:12:01 +02001042 vfree(chunk_meta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001043 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001044
Javier González43d47122018-03-30 00:05:09 +02001045fail_free_lines:
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001046 while (--i >= 0)
Hans Holmberg48b8d202018-06-01 16:41:06 +02001047 pblk_line_meta_free(l_mg, &pblk->lines[i]);
Javier González43d47122018-03-30 00:05:09 +02001048 kfree(pblk->lines);
Javier González32ef9412018-03-30 00:05:20 +02001049fail_free_chunk_meta:
1050 kfree(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001051fail_free_luns:
1052 kfree(pblk->luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001053fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +02001054 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001055
1056 return ret;
1057}
1058
1059static int pblk_writer_init(struct pblk *pblk)
1060{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001061 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
1062 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001063 int err = PTR_ERR(pblk->writer_ts);
1064
1065 if (err != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001066 pblk_err(pblk, "could not allocate writer kthread (%d)\n",
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001067 err);
1068 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001069 }
1070
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001071 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1072 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1073
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001074 return 0;
1075}
1076
1077static void pblk_writer_stop(struct pblk *pblk)
1078{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001079 /* The pipeline must be stopped and the write buffer emptied before the
1080 * write thread is stopped
1081 */
1082 WARN(pblk_rb_read_count(&pblk->rwb),
1083 "Stopping not fully persisted write buffer\n");
1084
1085 WARN(pblk_rb_sync_count(&pblk->rwb),
1086 "Stopping not fully synced write buffer\n");
1087
Hans Holmberg7be970b2018-03-30 00:04:56 +02001088 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001089 if (pblk->writer_ts)
1090 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001091}
1092
1093static void pblk_free(struct pblk *pblk)
1094{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001095 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001096 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001097 pblk_rwb_free(pblk);
1098 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001099
1100 kfree(pblk);
1101}
1102
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001103static void pblk_tear_down(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001104{
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001105 if (graceful)
1106 __pblk_pipeline_flush(pblk);
1107 __pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001108 pblk_writer_stop(pblk);
1109 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001110 pblk_rl_free(&pblk->rl);
1111
Matias Bjørling4e495a42018-07-13 10:48:42 +02001112 pblk_debug(pblk, "consistent tear down (graceful:%d)\n", graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001113}
1114
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001115static void pblk_exit(void *private, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001116{
1117 struct pblk *pblk = private;
1118
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001119 pblk_gc_exit(pblk, graceful);
1120 pblk_tear_down(pblk, graceful);
Hans Holmbergc5586192017-10-13 14:46:40 +02001121
Matias Bjørling880eda52018-07-13 10:48:37 +02001122#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001123 pblk_info(pblk, "exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Hans Holmbergc5586192017-10-13 14:46:40 +02001124#endif
1125
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001126 pblk_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001127}
1128
1129static sector_t pblk_capacity(void *private)
1130{
1131 struct pblk *pblk = private;
1132
1133 return pblk->capacity * NR_PHY_IN_LOG;
1134}
1135
1136static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1137 int flags)
1138{
1139 struct nvm_geo *geo = &dev->geo;
1140 struct request_queue *bqueue = dev->q;
1141 struct request_queue *tqueue = tdisk->queue;
1142 struct pblk *pblk;
1143 int ret;
1144
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001145 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1146 if (!pblk)
1147 return ERR_PTR(-ENOMEM);
1148
1149 pblk->dev = dev;
1150 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +02001151 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001152 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +02001153 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001154
Matias Bjørling4e495a42018-07-13 10:48:42 +02001155 if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1156 geo->version == NVM_OCSSD_SPEC_20)) {
1157 pblk_err(pblk, "OCSSD version not supported (%u)\n",
1158 geo->version);
1159 kfree(pblk);
1160 return ERR_PTR(-EINVAL);
1161 }
1162
Hans Holmberg6a3abf52018-06-01 16:41:05 +02001163 spin_lock_init(&pblk->resubmit_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001164 spin_lock_init(&pblk->trans_lock);
1165 spin_lock_init(&pblk->lock);
1166
Matias Bjørling880eda52018-07-13 10:48:37 +02001167#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001168 atomic_long_set(&pblk->inflight_writes, 0);
1169 atomic_long_set(&pblk->padded_writes, 0);
1170 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001171 atomic_long_set(&pblk->req_writes, 0);
1172 atomic_long_set(&pblk->sub_writes, 0);
1173 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001174 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001175 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001176 atomic_long_set(&pblk->sync_reads, 0);
1177 atomic_long_set(&pblk->recov_writes, 0);
1178 atomic_long_set(&pblk->recov_writes, 0);
1179 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001180 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001181#endif
1182
1183 atomic_long_set(&pblk->read_failed, 0);
1184 atomic_long_set(&pblk->read_empty, 0);
1185 atomic_long_set(&pblk->read_high_ecc, 0);
1186 atomic_long_set(&pblk->read_failed_gc, 0);
1187 atomic_long_set(&pblk->write_failed, 0);
1188 atomic_long_set(&pblk->erase_failed, 0);
1189
Javier González43d47122018-03-30 00:05:09 +02001190 ret = pblk_core_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001191 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001192 pblk_err(pblk, "could not initialize core\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001193 goto fail;
1194 }
1195
1196 ret = pblk_lines_init(pblk);
1197 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001198 pblk_err(pblk, "could not initialize lines\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001199 goto fail_free_core;
1200 }
1201
Javier González43d47122018-03-30 00:05:09 +02001202 ret = pblk_rwb_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001203 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001204 pblk_err(pblk, "could not initialize write buffer\n");
Javier González43d47122018-03-30 00:05:09 +02001205 goto fail_free_lines;
1206 }
1207
1208 ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1209 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001210 pblk_err(pblk, "could not initialize maps\n");
Javier González43d47122018-03-30 00:05:09 +02001211 goto fail_free_rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001212 }
1213
1214 ret = pblk_writer_init(pblk);
1215 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001216 if (ret != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001217 pblk_err(pblk, "could not initialize write thread\n");
Javier González43d47122018-03-30 00:05:09 +02001218 goto fail_free_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001219 }
1220
1221 ret = pblk_gc_init(pblk);
1222 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001223 pblk_err(pblk, "could not initialize gc\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001224 goto fail_stop_writer;
1225 }
1226
1227 /* inherit the size from the underlying device */
1228 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1229 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1230
1231 blk_queue_write_cache(tqueue, true, false);
1232
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001233 tqueue->limits.discard_granularity = geo->clba * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001234 tqueue->limits.discard_alignment = 0;
1235 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001236 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001237
Matias Bjørling4e495a42018-07-13 10:48:42 +02001238 pblk_info(pblk, "luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001239 geo->all_luns, pblk->l_mg.nr_lines,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001240 (unsigned long long)pblk->rl.nr_secs,
1241 pblk->rwb.nr_entries);
1242
1243 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001244
1245 /* Check if we need to start GC */
1246 pblk_gc_should_kick(pblk);
1247
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001248 return pblk;
1249
1250fail_stop_writer:
1251 pblk_writer_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001252fail_free_l2p:
1253 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001254fail_free_rwb:
1255 pblk_rwb_free(pblk);
1256fail_free_lines:
1257 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001258fail_free_core:
1259 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001260fail:
1261 kfree(pblk);
1262 return ERR_PTR(ret);
1263}
1264
1265/* physical block device target */
1266static struct nvm_tgt_type tt_pblk = {
1267 .name = "pblk",
1268 .version = {1, 0, 0},
1269
1270 .make_rq = pblk_make_rq,
1271 .capacity = pblk_capacity,
1272
1273 .init = pblk_init,
1274 .exit = pblk_exit,
1275
1276 .sysfs_init = pblk_sysfs_init,
1277 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001278 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001279};
1280
1281static int __init pblk_module_init(void)
1282{
NeilBrownb25d5232017-06-18 14:38:58 +10001283 int ret;
1284
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001285 ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
1286 if (ret)
1287 return ret;
NeilBrownb25d5232017-06-18 14:38:58 +10001288 ret = nvm_register_tgt_type(&tt_pblk);
1289 if (ret)
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001290 bioset_exit(&pblk_bio_set);
NeilBrownb25d5232017-06-18 14:38:58 +10001291 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001292}
1293
1294static void pblk_module_exit(void)
1295{
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001296 bioset_exit(&pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001297 nvm_unregister_tgt_type(&tt_pblk);
1298}
1299
1300module_init(pblk_module_init);
1301module_exit(pblk_module_exit);
1302MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1303MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1304MODULE_LICENSE("GPL v2");
1305MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");