blob: b2c49fc006c9cb33764aa6c3da02760709a408d4 [file] [log] [blame]
Javier González02a15202018-10-09 13:12:06 +02001// SPDX-License-Identifier: GPL-2.0
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02002/*
3 * Copyright (C) 2015 IT University of Copenhagen (rrpc.c)
4 * Copyright (C) 2016 CNEX Labs
5 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
6 * Matias Bjorling <matias@cnexlabs.com>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version
10 * 2 as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * Implementation of a physical block-device target for Open-channel SSDs.
18 *
19 * pblk-init.c - pblk's initialization.
20 */
21
22#include "pblk.h"
Hans Holmberg4c44abf2018-10-09 13:11:52 +020023#include "pblk-trace.h"
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020024
Wei Yongjun21ff1392018-06-05 09:15:12 +000025static unsigned int write_buffer_size;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +020026
27module_param(write_buffer_size, uint, 0644);
28MODULE_PARM_DESC(write_buffer_size, "number of entries in a write buffer");
29
Hans Holmberg1864de92018-10-09 13:11:58 +020030struct pblk_global_caches {
31 struct kmem_cache *ws;
32 struct kmem_cache *rec;
33 struct kmem_cache *g_rq;
34 struct kmem_cache *w_rq;
35
36 struct kref kref;
37
38 struct mutex mutex; /* Ensures consistency between
39 * caches and kref
40 */
41};
42
43static struct pblk_global_caches pblk_caches = {
44 .mutex = __MUTEX_INITIALIZER(pblk_caches.mutex),
45 .kref = KREF_INIT(0),
46};
47
Kent Overstreetb906bbb2018-05-20 18:25:50 -040048struct bio_set pblk_bio_set;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020049
50static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
51 struct bio *bio)
52{
53 int ret;
54
55 /* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
56 * constraint. Writes can be of arbitrary size.
57 */
58 if (bio_data_dir(bio) == READ) {
NeilBrownaf67c312017-06-18 14:38:57 +100059 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020060 ret = pblk_submit_read(pblk, bio);
61 if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
62 bio_put(bio);
63
64 return ret;
65 }
66
67 /* Prevent deadlock in the case of a modest LUN configuration and large
68 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
69 * available for user I/O.
70 */
Javier Gonzálezda67e68f2017-10-13 14:46:05 +020071 if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
NeilBrownaf67c312017-06-18 14:38:57 +100072 blk_queue_split(q, &bio);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +020073
74 return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
75}
76
77static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
78{
79 struct pblk *pblk = q->queuedata;
80
81 if (bio_op(bio) == REQ_OP_DISCARD) {
82 pblk_discard(pblk, bio);
83 if (!(bio->bi_opf & REQ_PREFLUSH)) {
84 bio_endio(bio);
85 return BLK_QC_T_NONE;
86 }
87 }
88
89 switch (pblk_rw_io(q, pblk, bio)) {
90 case NVM_IO_ERR:
91 bio_io_error(bio);
92 break;
93 case NVM_IO_DONE:
94 bio_endio(bio);
95 break;
96 }
97
98 return BLK_QC_T_NONE;
99}
100
Hans Holmbergc5586192017-10-13 14:46:40 +0200101static size_t pblk_trans_map_size(struct pblk *pblk)
102{
103 int entry_size = 8;
104
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200105 if (pblk->addrf_len < 32)
Hans Holmbergc5586192017-10-13 14:46:40 +0200106 entry_size = 4;
107
108 return entry_size * pblk->rl.nr_secs;
109}
110
Matias Bjørling880eda52018-07-13 10:48:37 +0200111#ifdef CONFIG_NVM_PBLK_DEBUG
Hans Holmbergc5586192017-10-13 14:46:40 +0200112static u32 pblk_l2p_crc(struct pblk *pblk)
113{
114 size_t map_size;
115 u32 crc = ~(u32)0;
116
117 map_size = pblk_trans_map_size(pblk);
118 crc = crc32_le(crc, pblk->trans_map, map_size);
119 return crc;
120}
121#endif
122
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200123static void pblk_l2p_free(struct pblk *pblk)
124{
125 vfree(pblk->trans_map);
126}
127
Javier González43d47122018-03-30 00:05:09 +0200128static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
129{
130 struct pblk_line *line = NULL;
131
132 if (factory_init) {
133 pblk_setup_uuid(pblk);
134 } else {
135 line = pblk_recov_l2p(pblk);
136 if (IS_ERR(line)) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200137 pblk_err(pblk, "could not recover l2p table\n");
Javier González43d47122018-03-30 00:05:09 +0200138 return -EFAULT;
139 }
140 }
141
Matias Bjørling880eda52018-07-13 10:48:37 +0200142#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +0200143 pblk_info(pblk, "init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Javier González43d47122018-03-30 00:05:09 +0200144#endif
145
146 /* Free full lines directly as GC has not been started yet */
147 pblk_gc_free_full_lines(pblk);
148
149 if (!line) {
150 /* Configure next line for user data */
151 line = pblk_line_get_first_data(pblk);
Javier González1d8b33e2018-06-01 15:04:16 +0200152 if (!line)
Javier González43d47122018-03-30 00:05:09 +0200153 return -EFAULT;
Javier González43d47122018-03-30 00:05:09 +0200154 }
155
156 return 0;
157}
158
159static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200160{
161 sector_t i;
162 struct ppa_addr ppa;
Hans Holmbergc5586192017-10-13 14:46:40 +0200163 size_t map_size;
Javier González1d8b33e2018-06-01 15:04:16 +0200164 int ret = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200165
Hans Holmbergc5586192017-10-13 14:46:40 +0200166 map_size = pblk_trans_map_size(pblk);
167 pblk->trans_map = vmalloc(map_size);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200168 if (!pblk->trans_map)
169 return -ENOMEM;
170
171 pblk_ppa_set_empty(&ppa);
172
173 for (i = 0; i < pblk->rl.nr_secs; i++)
174 pblk_trans_map_set(pblk, i, ppa);
175
Javier González1d8b33e2018-06-01 15:04:16 +0200176 ret = pblk_l2p_recover(pblk, factory_init);
177 if (ret)
178 vfree(pblk->trans_map);
179
180 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200181}
182
183static void pblk_rwb_free(struct pblk *pblk)
184{
185 if (pblk_rb_tear_down_check(&pblk->rwb))
Matias Bjørling4e495a42018-07-13 10:48:42 +0200186 pblk_err(pblk, "write buffer error on tear down\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200187
Javier González9bd1f872018-10-09 13:12:10 +0200188 pblk_rb_free(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200189}
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;
Javier González9bd1f872018-10-09 13:12:10 +0200195 unsigned long buffer_size;
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200196 int pgs_in_buffer;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200197
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200198 pgs_in_buffer = max(geo->mw_cunits, geo->ws_opt) * geo->all_luns;
199
200 if (write_buffer_size && (write_buffer_size > pgs_in_buffer))
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200201 buffer_size = write_buffer_size;
202 else
Marcin Dziegielewskiffc03fb2018-07-13 10:48:36 +0200203 buffer_size = pgs_in_buffer;
Marcin Dziegielewski4a828882018-06-01 16:41:10 +0200204
Javier González9bd1f872018-10-09 13:12:10 +0200205 return pblk_rb_init(&pblk->rwb, buffer_size, geo->csecs);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200206}
207
208/* Minimum pages needed within a lun */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200209#define ADDR_POOL_SIZE 64
210
Matias Bjørling4e495a42018-07-13 10:48:42 +0200211static int pblk_set_addrf_12(struct pblk *pblk, struct nvm_geo *geo,
212 struct nvm_addrf_12 *dst)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200213{
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200214 struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
215 int power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200216
217 /* Re-calculate channel and lun format to adapt to configuration */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200218 power_len = get_count_order(geo->num_ch);
219 if (1 << power_len != geo->num_ch) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200220 pblk_err(pblk, "supports only power-of-two channel config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200221 return -EINVAL;
222 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200223 dst->ch_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200224
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200225 power_len = get_count_order(geo->num_lun);
226 if (1 << power_len != geo->num_lun) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200227 pblk_err(pblk, "supports only power-of-two LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200228 return -EINVAL;
229 }
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200230 dst->lun_len = power_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200231
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200232 dst->blk_len = src->blk_len;
233 dst->pg_len = src->pg_len;
234 dst->pln_len = src->pln_len;
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200235 dst->sec_len = src->sec_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200236
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200237 dst->sec_offset = 0;
238 dst->pln_offset = dst->sec_len;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200239 dst->ch_offset = dst->pln_offset + dst->pln_len;
240 dst->lun_offset = dst->ch_offset + dst->ch_len;
241 dst->pg_offset = dst->lun_offset + dst->lun_len;
242 dst->blk_offset = dst->pg_offset + dst->pg_len;
243
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200244 dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200245 dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
246 dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
247 dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
248 dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
249 dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
250
251 return dst->blk_offset + src->blk_len;
252}
253
Javier González3b2a3ad2018-03-30 00:05:21 +0200254static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
255 struct pblk_addrf *udst)
256{
257 struct nvm_addrf *src = &geo->addrf;
258
259 adst->ch_len = get_count_order(geo->num_ch);
260 adst->lun_len = get_count_order(geo->num_lun);
261 adst->chk_len = src->chk_len;
262 adst->sec_len = src->sec_len;
263
264 adst->sec_offset = 0;
265 adst->ch_offset = adst->sec_len;
266 adst->lun_offset = adst->ch_offset + adst->ch_len;
267 adst->chk_offset = adst->lun_offset + adst->lun_len;
268
269 adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
270 adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
271 adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
272 adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
273
274 udst->sec_stripe = geo->ws_opt;
275 udst->ch_stripe = geo->num_ch;
276 udst->lun_stripe = geo->num_lun;
277
278 udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
279 udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
280
281 return adst->chk_offset + adst->chk_len;
282}
283
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200284static int pblk_set_addrf(struct pblk *pblk)
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200285{
286 struct nvm_tgt_dev *dev = pblk->dev;
287 struct nvm_geo *geo = &dev->geo;
288 int mod;
289
Javier González3b2a3ad2018-03-30 00:05:21 +0200290 switch (geo->version) {
291 case NVM_OCSSD_SPEC_12:
292 div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
293 if (mod) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200294 pblk_err(pblk, "bad configuration of sectors/pages\n");
Javier González3b2a3ad2018-03-30 00:05:21 +0200295 return -EINVAL;
296 }
297
Matias Bjørling4e495a42018-07-13 10:48:42 +0200298 pblk->addrf_len = pblk_set_addrf_12(pblk, geo,
299 (void *)&pblk->addrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200300 break;
301 case NVM_OCSSD_SPEC_20:
302 pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
Matias Bjørling4e495a42018-07-13 10:48:42 +0200303 &pblk->uaddrf);
Javier González3b2a3ad2018-03-30 00:05:21 +0200304 break;
305 default:
Matias Bjørling4e495a42018-07-13 10:48:42 +0200306 pblk_err(pblk, "OCSSD revision not supported (%d)\n",
Javier González3b2a3ad2018-03-30 00:05:21 +0200307 geo->version);
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200308 return -EINVAL;
309 }
310
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200311 return 0;
312}
313
Hans Holmberg1864de92018-10-09 13:11:58 +0200314static int pblk_create_global_caches(void)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200315{
Hans Holmberg1864de92018-10-09 13:11:58 +0200316
317 pblk_caches.ws = kmem_cache_create("pblk_blk_ws",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200318 sizeof(struct pblk_line_ws), 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200319 if (!pblk_caches.ws)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200320 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200321
Hans Holmberg1864de92018-10-09 13:11:58 +0200322 pblk_caches.rec = kmem_cache_create("pblk_rec",
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200323 sizeof(struct pblk_rec_ctx), 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200324 if (!pblk_caches.rec)
325 goto fail_destroy_ws;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200326
Hans Holmberg1864de92018-10-09 13:11:58 +0200327 pblk_caches.g_rq = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200328 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200329 if (!pblk_caches.g_rq)
330 goto fail_destroy_rec;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200331
Hans Holmberg1864de92018-10-09 13:11:58 +0200332 pblk_caches.w_rq = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200333 0, 0, NULL);
Hans Holmberg1864de92018-10-09 13:11:58 +0200334 if (!pblk_caches.w_rq)
335 goto fail_destroy_g_rq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200336
337 return 0;
Hans Holmberg1864de92018-10-09 13:11:58 +0200338
339fail_destroy_g_rq:
340 kmem_cache_destroy(pblk_caches.g_rq);
341fail_destroy_rec:
342 kmem_cache_destroy(pblk_caches.rec);
343fail_destroy_ws:
344 kmem_cache_destroy(pblk_caches.ws);
345
346 return -ENOMEM;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200347}
348
Hans Holmberg1864de92018-10-09 13:11:58 +0200349static int pblk_get_global_caches(void)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200350{
Hans Holmberg1864de92018-10-09 13:11:58 +0200351 int ret;
352
353 mutex_lock(&pblk_caches.mutex);
354
355 if (kref_read(&pblk_caches.kref) > 0) {
356 kref_get(&pblk_caches.kref);
357 mutex_unlock(&pblk_caches.mutex);
358 return 0;
359 }
360
361 ret = pblk_create_global_caches();
362
363 if (!ret)
364 kref_get(&pblk_caches.kref);
365
366 mutex_unlock(&pblk_caches.mutex);
367
368 return ret;
369}
370
371static void pblk_destroy_global_caches(struct kref *ref)
372{
373 struct pblk_global_caches *c;
374
375 c = container_of(ref, struct pblk_global_caches, kref);
376
377 kmem_cache_destroy(c->ws);
378 kmem_cache_destroy(c->rec);
379 kmem_cache_destroy(c->g_rq);
380 kmem_cache_destroy(c->w_rq);
381}
382
383static void pblk_put_global_caches(void)
384{
385 mutex_lock(&pblk_caches.mutex);
386 kref_put(&pblk_caches.kref, pblk_destroy_global_caches);
387 mutex_unlock(&pblk_caches.mutex);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200388}
389
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200390static int pblk_core_init(struct pblk *pblk)
391{
392 struct nvm_tgt_dev *dev = pblk->dev;
393 struct nvm_geo *geo = &dev->geo;
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400394 int ret, max_write_ppas;
Javier González43d47122018-03-30 00:05:09 +0200395
396 atomic64_set(&pblk->user_wa, 0);
397 atomic64_set(&pblk->pad_wa, 0);
398 atomic64_set(&pblk->gc_wa, 0);
399 pblk->user_rst_wa = 0;
400 pblk->pad_rst_wa = 0;
401 pblk->gc_rst_wa = 0;
402
403 atomic64_set(&pblk->nr_flush, 0);
404 pblk->nr_flush_rst = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200405
Matias Bjørling8bbd45d2018-10-09 13:11:38 +0200406 pblk->min_write_pgs = geo->ws_opt;
Javier González43d47122018-03-30 00:05:09 +0200407 max_write_ppas = pblk->min_write_pgs * geo->all_luns;
408 pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
409 pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
410
Kees Cook6396bb22018-06-12 14:03:40 -0700411 pblk->pad_dist = kcalloc(pblk->min_write_pgs - 1, sizeof(atomic64_t),
Javier González43d47122018-03-30 00:05:09 +0200412 GFP_KERNEL);
413 if (!pblk->pad_dist)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200414 return -ENOMEM;
415
Hans Holmberg1864de92018-10-09 13:11:58 +0200416 if (pblk_get_global_caches())
Javier González43d47122018-03-30 00:05:09 +0200417 goto fail_free_pad_dist;
418
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200419 /* Internal bios can be at most the sectors signaled by the device. */
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400420 ret = mempool_init_page_pool(&pblk->page_bio_pool, NVM_MAX_VLBA, 0);
421 if (ret)
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200422 goto free_global_caches;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200423
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400424 ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
Hans Holmberg1864de92018-10-09 13:11:58 +0200425 pblk_caches.ws);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400426 if (ret)
Javier Gonzálezbd432412017-10-13 14:46:06 +0200427 goto free_page_bio_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200428
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400429 ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200430 pblk_caches.rec);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400431 if (ret)
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200432 goto free_gen_ws_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200433
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400434 ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200435 pblk_caches.g_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400436 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200437 goto free_rec_pool;
438
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400439 ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200440 pblk_caches.g_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400441 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200442 goto free_r_rq_pool;
443
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400444 ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
Hans Holmberg1864de92018-10-09 13:11:58 +0200445 pblk_caches.w_rq);
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400446 if (ret)
Javier González0d880392017-10-13 14:46:08 +0200447 goto free_e_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200448
Javier Gonzálezef576492017-06-26 11:57:28 +0200449 pblk->close_wq = alloc_workqueue("pblk-close-wq",
450 WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
451 if (!pblk->close_wq)
Javier Gonzáleze72ec1d2017-10-13 14:46:09 +0200452 goto free_w_rq_pool;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200453
Javier Gonzálezef576492017-06-26 11:57:28 +0200454 pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
455 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
456 if (!pblk->bb_wq)
457 goto free_close_wq;
458
Javier González7bd4d372017-10-13 14:46:23 +0200459 pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
460 WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
461 if (!pblk->r_end_wq)
Javier Gonzálezef576492017-06-26 11:57:28 +0200462 goto free_bb_wq;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200463
Javier Gonzálezbb845ae42018-03-30 00:05:19 +0200464 if (pblk_set_addrf(pblk))
Javier González7bd4d372017-10-13 14:46:23 +0200465 goto free_r_end_wq;
466
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200467 INIT_LIST_HEAD(&pblk->compl_list);
Hans Holmberg6a3abf52018-06-01 16:41:05 +0200468 INIT_LIST_HEAD(&pblk->resubmit_list);
Javier González43d47122018-03-30 00:05:09 +0200469
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200470 return 0;
471
Javier González7bd4d372017-10-13 14:46:23 +0200472free_r_end_wq:
473 destroy_workqueue(pblk->r_end_wq);
Javier Gonzálezef576492017-06-26 11:57:28 +0200474free_bb_wq:
475 destroy_workqueue(pblk->bb_wq);
476free_close_wq:
477 destroy_workqueue(pblk->close_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200478free_w_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400479 mempool_exit(&pblk->w_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200480free_e_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400481 mempool_exit(&pblk->e_rq_pool);
Javier González0d880392017-10-13 14:46:08 +0200482free_r_rq_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400483 mempool_exit(&pblk->r_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200484free_rec_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400485 mempool_exit(&pblk->rec_pool);
Javier Gonzálezb84ae4a82017-10-13 14:46:07 +0200486free_gen_ws_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400487 mempool_exit(&pblk->gen_ws_pool);
Javier Gonzálezbd432412017-10-13 14:46:06 +0200488free_page_bio_pool:
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400489 mempool_exit(&pblk->page_bio_pool);
Rakesh Pandit22a4e062017-10-13 14:46:33 +0200490free_global_caches:
Hans Holmberg1864de92018-10-09 13:11:58 +0200491 pblk_put_global_caches();
Javier González43d47122018-03-30 00:05:09 +0200492fail_free_pad_dist:
493 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200494 return -ENOMEM;
495}
496
497static void pblk_core_free(struct pblk *pblk)
498{
Javier Gonzálezef576492017-06-26 11:57:28 +0200499 if (pblk->close_wq)
500 destroy_workqueue(pblk->close_wq);
501
Javier González7bd4d372017-10-13 14:46:23 +0200502 if (pblk->r_end_wq)
503 destroy_workqueue(pblk->r_end_wq);
504
Javier Gonzálezef576492017-06-26 11:57:28 +0200505 if (pblk->bb_wq)
506 destroy_workqueue(pblk->bb_wq);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200507
Kent Overstreetb906bbb2018-05-20 18:25:50 -0400508 mempool_exit(&pblk->page_bio_pool);
509 mempool_exit(&pblk->gen_ws_pool);
510 mempool_exit(&pblk->rec_pool);
511 mempool_exit(&pblk->r_rq_pool);
512 mempool_exit(&pblk->e_rq_pool);
513 mempool_exit(&pblk->w_rq_pool);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200514
Hans Holmberg1864de92018-10-09 13:11:58 +0200515 pblk_put_global_caches();
Javier González43d47122018-03-30 00:05:09 +0200516 kfree(pblk->pad_dist);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200517}
518
Javier Gonzáleze411b332018-03-30 00:04:59 +0200519static void pblk_line_mg_free(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200520{
521 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
522 int i;
523
524 kfree(l_mg->bb_template);
525 kfree(l_mg->bb_aux);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200526 kfree(l_mg->vsc_list);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200527
528 for (i = 0; i < PBLK_DATA_LINES; i++) {
Javier Gonzálezf680f192017-06-26 11:57:21 +0200529 kfree(l_mg->sline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200530 pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200531 kfree(l_mg->eline_meta[i]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200532 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200533
534 mempool_destroy(l_mg->bitmap_pool);
535 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200536}
537
Hans Holmberg48b8d202018-06-01 16:41:06 +0200538static void pblk_line_meta_free(struct pblk_line_mgmt *l_mg,
539 struct pblk_line *line)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200540{
Hans Holmberg48b8d202018-06-01 16:41:06 +0200541 struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
542
Javier Gonzáleze411b332018-03-30 00:04:59 +0200543 kfree(line->blk_bitmap);
544 kfree(line->erase_bitmap);
Javier González32ef9412018-03-30 00:05:20 +0200545 kfree(line->chks);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200546
547 pblk_mfree(w_err_gc->lba_list, l_mg->emeta_alloc_type);
548 kfree(w_err_gc);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200549}
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200550
Javier Gonzáleze411b332018-03-30 00:04:59 +0200551static void pblk_lines_free(struct pblk *pblk)
552{
553 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
554 struct pblk_line *line;
555 int i;
556
557 spin_lock(&l_mg->free_lock);
558 for (i = 0; i < l_mg->nr_lines; i++) {
559 line = &pblk->lines[i];
560
Javier González8e55c072018-06-01 15:04:22 +0200561 pblk_line_free(line);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200562 pblk_line_meta_free(l_mg, line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200563 }
564 spin_unlock(&l_mg->free_lock);
Javier González43d47122018-03-30 00:05:09 +0200565
566 pblk_line_mg_free(pblk);
567
568 kfree(pblk->luns);
569 kfree(pblk->lines);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200570}
571
Javier González43d47122018-03-30 00:05:09 +0200572static int pblk_luns_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200573{
574 struct nvm_tgt_dev *dev = pblk->dev;
575 struct nvm_geo *geo = &dev->geo;
576 struct pblk_lun *rlun;
Javier Gonzáleze411b332018-03-30 00:04:59 +0200577 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200578
579 /* TODO: Implement unbalanced LUN support */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200580 if (geo->num_lun < 0) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200581 pblk_err(pblk, "unbalanced LUN config.\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200582 return -EINVAL;
583 }
584
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100585 pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
586 GFP_KERNEL);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200587 if (!pblk->luns)
588 return -ENOMEM;
589
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100590 for (i = 0; i < geo->all_luns; i++) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200591 /* Stripe across channels */
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200592 int ch = i % geo->num_ch;
593 int lun_raw = i / geo->num_ch;
594 int lunid = lun_raw + ch * geo->num_lun;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200595
596 rlun = &pblk->luns[i];
Javier González43d47122018-03-30 00:05:09 +0200597 rlun->bppa = dev->luns[lunid];
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200598
599 sema_init(&rlun->wr_sem, 1);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200600 }
601
602 return 0;
603}
604
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200605/* See comment over struct line_emeta definition */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200606static unsigned int calc_emeta_len(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200607{
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200608 struct pblk_line_meta *lm = &pblk->lm;
609 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
610 struct nvm_tgt_dev *dev = pblk->dev;
611 struct nvm_geo *geo = &dev->geo;
612
613 /* Round to sector size so that lba_list starts on its own sector */
614 lm->emeta_sec[1] = DIV_ROUND_UP(
Hans Holmberg76758392018-03-30 00:04:52 +0200615 sizeof(struct line_emeta) + lm->blk_bitmap_len +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200616 sizeof(struct wa_counters), geo->csecs);
617 lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200618
619 /* Round to sector size so that vsc_list starts on its own sector */
620 lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
621 lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200622 geo->csecs);
623 lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200624
625 lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200626 geo->csecs);
627 lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200628
629 lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
630
631 return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200632}
633
634static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
635{
636 struct nvm_tgt_dev *dev = pblk->dev;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100637 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
638 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200639 struct nvm_geo *geo = &dev->geo;
640 sector_t provisioned;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100641 int sec_meta, blk_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200642
Javier Gonzáleze5392732018-01-05 14:16:14 +0100643 if (geo->op == NVM_TARGET_DEFAULT_OP)
644 pblk->op = PBLK_DEFAULT_OP;
645 else
646 pblk->op = geo->op;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200647
648 provisioned = nr_free_blks;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100649 provisioned *= (100 - pblk->op);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200650 sector_div(provisioned, 100);
651
Javier Gonzáleza7689932018-01-05 14:16:13 +0100652 pblk->op_blks = nr_free_blks - provisioned;
653
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200654 /* Internally pblk manages all free blocks, but all calculations based
655 * on user capacity consider only provisioned blocks
656 */
657 pblk->rl.total_blocks = nr_free_blks;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200658 pblk->rl.nr_secs = nr_free_blks * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100659
660 /* Consider sectors used for metadata */
661 sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200662 blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100663
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200664 pblk->capacity = (provisioned - blk_meta) * geo->clba;
Javier Gonzáleza7689932018-01-05 14:16:13 +0100665
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200666 atomic_set(&pblk->rl.free_blocks, nr_free_blks);
Javier Gonzáleza7689932018-01-05 14:16:13 +0100667 atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200668}
669
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200670static int pblk_setup_line_meta_chk(struct pblk *pblk, struct pblk_line *line,
Javier González32ef9412018-03-30 00:05:20 +0200671 struct nvm_chk_meta *meta)
672{
673 struct nvm_tgt_dev *dev = pblk->dev;
674 struct nvm_geo *geo = &dev->geo;
675 struct pblk_line_meta *lm = &pblk->lm;
676 int i, nr_bad_chks = 0;
677
678 for (i = 0; i < lm->blk_per_line; i++) {
679 struct pblk_lun *rlun = &pblk->luns[i];
680 struct nvm_chk_meta *chunk;
681 struct nvm_chk_meta *chunk_meta;
682 struct ppa_addr ppa;
683 int pos;
684
685 ppa = rlun->bppa;
686 pos = pblk_ppa_to_pos(geo, ppa);
687 chunk = &line->chks[pos];
688
689 ppa.m.chk = line->id;
690 chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
691
692 chunk->state = chunk_meta->state;
693 chunk->type = chunk_meta->type;
694 chunk->wi = chunk_meta->wi;
695 chunk->slba = chunk_meta->slba;
696 chunk->cnlb = chunk_meta->cnlb;
697 chunk->wp = chunk_meta->wp;
698
Hans Holmberg4c44abf2018-10-09 13:11:52 +0200699 trace_pblk_chunk_state(pblk_disk_name(pblk), &ppa,
700 chunk->state);
701
Javier González32ef9412018-03-30 00:05:20 +0200702 if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
703 WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
704 continue;
705 }
706
Javier González6f9c9602018-06-01 15:04:23 +0200707 if (!(chunk->state & NVM_CHK_ST_OFFLINE))
708 continue;
709
Javier González32ef9412018-03-30 00:05:20 +0200710 set_bit(pos, line->blk_bitmap);
711 nr_bad_chks++;
712 }
713
714 return nr_bad_chks;
715}
716
717static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
718 void *chunk_meta, int line_id)
719{
Javier González32ef9412018-03-30 00:05:20 +0200720 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
721 struct pblk_line_meta *lm = &pblk->lm;
722 long nr_bad_chks, chk_in_line;
723
724 line->pblk = pblk;
725 line->id = line_id;
726 line->type = PBLK_LINETYPE_FREE;
727 line->state = PBLK_LINESTATE_NEW;
728 line->gc_group = PBLK_LINEGC_NONE;
729 line->vsc = &l_mg->vsc_list[line_id];
730 spin_lock_init(&line->lock);
731
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200732 nr_bad_chks = pblk_setup_line_meta_chk(pblk, line, chunk_meta);
Javier González32ef9412018-03-30 00:05:20 +0200733
734 chk_in_line = lm->blk_per_line - nr_bad_chks;
735 if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
736 chk_in_line < lm->min_blk_line) {
737 line->state = PBLK_LINESTATE_BAD;
738 list_add_tail(&line->list, &l_mg->bad_list);
739 return 0;
740 }
741
742 atomic_set(&line->blk_in_line, chk_in_line);
743 list_add_tail(&line->list, &l_mg->free_list);
744 l_mg->nr_free_lines++;
745
746 return chk_in_line;
747}
748
749static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200750{
Javier González43d47122018-03-30 00:05:09 +0200751 struct pblk_line_meta *lm = &pblk->lm;
752
753 line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
754 if (!line->blk_bitmap)
755 return -ENOMEM;
756
757 line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200758 if (!line->erase_bitmap)
759 goto free_blk_bitmap;
760
Javier González43d47122018-03-30 00:05:09 +0200761
Kees Cook6da2ec52018-06-12 13:55:00 -0700762 line->chks = kmalloc_array(lm->blk_per_line,
763 sizeof(struct nvm_chk_meta), GFP_KERNEL);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200764 if (!line->chks)
765 goto free_erase_bitmap;
766
767 line->w_err_gc = kzalloc(sizeof(struct pblk_w_err_gc), GFP_KERNEL);
768 if (!line->w_err_gc)
769 goto free_chks;
Javier González43d47122018-03-30 00:05:09 +0200770
771 return 0;
Hans Holmberg48b8d202018-06-01 16:41:06 +0200772
773free_chks:
774 kfree(line->chks);
775free_erase_bitmap:
776 kfree(line->erase_bitmap);
777free_blk_bitmap:
778 kfree(line->blk_bitmap);
779 return -ENOMEM;
Javier González43d47122018-03-30 00:05:09 +0200780}
781
782static int pblk_line_mg_init(struct pblk *pblk)
783{
784 struct nvm_tgt_dev *dev = pblk->dev;
785 struct nvm_geo *geo = &dev->geo;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200786 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
787 struct pblk_line_meta *lm = &pblk->lm;
Javier González43d47122018-03-30 00:05:09 +0200788 int i, bb_distance;
789
Javier Gonzáleza40afad2018-03-30 00:05:14 +0200790 l_mg->nr_lines = geo->num_chk;
Javier González43d47122018-03-30 00:05:09 +0200791 l_mg->log_line = l_mg->data_line = NULL;
792 l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
793 l_mg->nr_free_lines = 0;
794 bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
795
796 INIT_LIST_HEAD(&l_mg->free_list);
797 INIT_LIST_HEAD(&l_mg->corrupt_list);
798 INIT_LIST_HEAD(&l_mg->bad_list);
799 INIT_LIST_HEAD(&l_mg->gc_full_list);
800 INIT_LIST_HEAD(&l_mg->gc_high_list);
801 INIT_LIST_HEAD(&l_mg->gc_mid_list);
802 INIT_LIST_HEAD(&l_mg->gc_low_list);
803 INIT_LIST_HEAD(&l_mg->gc_empty_list);
Hans Holmberg48b8d202018-06-01 16:41:06 +0200804 INIT_LIST_HEAD(&l_mg->gc_werr_list);
Javier González43d47122018-03-30 00:05:09 +0200805
806 INIT_LIST_HEAD(&l_mg->emeta_list);
807
Hans Holmberg48b8d202018-06-01 16:41:06 +0200808 l_mg->gc_lists[0] = &l_mg->gc_werr_list;
809 l_mg->gc_lists[1] = &l_mg->gc_high_list;
810 l_mg->gc_lists[2] = &l_mg->gc_mid_list;
811 l_mg->gc_lists[3] = &l_mg->gc_low_list;
Javier González43d47122018-03-30 00:05:09 +0200812
813 spin_lock_init(&l_mg->free_lock);
814 spin_lock_init(&l_mg->close_lock);
815 spin_lock_init(&l_mg->gc_lock);
816
817 l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
818 if (!l_mg->vsc_list)
819 goto fail;
820
821 l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
822 if (!l_mg->bb_template)
823 goto fail_free_vsc_list;
824
825 l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
826 if (!l_mg->bb_aux)
827 goto fail_free_bb_template;
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200828
829 /* smeta is always small enough to fit on a kmalloc memory allocation,
830 * emeta depends on the number of LUNs allocated to the pblk instance
831 */
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200832 for (i = 0; i < PBLK_DATA_LINES; i++) {
833 l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
834 if (!l_mg->sline_meta[i])
835 goto fail_free_smeta;
836 }
837
Hans Holmberg53d82db2018-10-09 13:11:47 +0200838 l_mg->bitmap_cache = kmem_cache_create("pblk_lm_bitmap",
839 lm->sec_bitmap_len, 0, 0, NULL);
840 if (!l_mg->bitmap_cache)
841 goto fail_free_smeta;
842
843 /* the bitmap pool is used for both valid and map bitmaps */
844 l_mg->bitmap_pool = mempool_create_slab_pool(PBLK_DATA_LINES * 2,
845 l_mg->bitmap_cache);
846 if (!l_mg->bitmap_pool)
847 goto fail_destroy_bitmap_cache;
848
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200849 /* emeta allocates three different buffers for managing metadata with
850 * in-memory and in-media layouts
851 */
852 for (i = 0; i < PBLK_DATA_LINES; i++) {
853 struct pblk_emeta *emeta;
854
855 emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
856 if (!emeta)
857 goto fail_free_emeta;
858
859 if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
860 l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
861
862 emeta->buf = vmalloc(lm->emeta_len[0]);
863 if (!emeta->buf) {
864 kfree(emeta);
865 goto fail_free_emeta;
866 }
867
868 emeta->nr_entries = lm->emeta_sec[0];
869 l_mg->eline_meta[i] = emeta;
870 } else {
871 l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
872
873 emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
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 }
882 }
883
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200884 for (i = 0; i < l_mg->nr_lines; i++)
885 l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
886
Javier González43d47122018-03-30 00:05:09 +0200887 bb_distance = (geo->all_luns) * geo->ws_opt;
888 for (i = 0; i < lm->sec_per_line; i += bb_distance)
889 bitmap_set(l_mg->bb_template, i, geo->ws_opt);
890
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200891 return 0;
892
893fail_free_emeta:
894 while (--i >= 0) {
Rakesh Panditc9d84b32017-10-13 14:45:54 +0200895 if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
896 vfree(l_mg->eline_meta[i]->buf);
897 else
898 kfree(l_mg->eline_meta[i]->buf);
Javier Gonzálezf680f192017-06-26 11:57:21 +0200899 kfree(l_mg->eline_meta[i]);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200900 }
Hans Holmberg53d82db2018-10-09 13:11:47 +0200901
902 mempool_destroy(l_mg->bitmap_pool);
903fail_destroy_bitmap_cache:
904 kmem_cache_destroy(l_mg->bitmap_cache);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200905fail_free_smeta:
906 for (i = 0; i < PBLK_DATA_LINES; i++)
Javier Gonzálezf680f192017-06-26 11:57:21 +0200907 kfree(l_mg->sline_meta[i]);
Javier González43d47122018-03-30 00:05:09 +0200908 kfree(l_mg->bb_aux);
909fail_free_bb_template:
910 kfree(l_mg->bb_template);
911fail_free_vsc_list:
912 kfree(l_mg->vsc_list);
913fail:
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200914 return -ENOMEM;
915}
916
Javier González43d47122018-03-30 00:05:09 +0200917static int pblk_line_meta_init(struct pblk *pblk)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200918{
919 struct nvm_tgt_dev *dev = pblk->dev;
920 struct nvm_geo *geo = &dev->geo;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200921 struct pblk_line_meta *lm = &pblk->lm;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200922 unsigned int smeta_len, emeta_len;
Javier González43d47122018-03-30 00:05:09 +0200923 int i;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200924
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200925 lm->sec_per_line = geo->clba * geo->all_luns;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100926 lm->blk_per_line = geo->all_luns;
927 lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200928 lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100929 lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
Rakesh Pandit27b97872017-10-13 14:46:28 +0200930 lm->mid_thrs = lm->sec_per_line / 2;
931 lm->high_thrs = lm->sec_per_line / 4;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100932 lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200933
934 /* Calculate necessary pages for smeta. See comment over struct
935 * line_smeta definition
936 */
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200937 i = 1;
938add_smeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200939 lm->smeta_sec = i * geo->ws_opt;
940 lm->smeta_len = lm->smeta_sec * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200941
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200942 smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200943 if (smeta_len > lm->smeta_len) {
944 i++;
945 goto add_smeta_page;
946 }
947
948 /* Calculate necessary pages for emeta. See comment over struct
949 * line_emeta definition
950 */
951 i = 1;
952add_emeta_page:
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200953 lm->emeta_sec[0] = i * geo->ws_opt;
954 lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200955
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200956 emeta_len = calc_emeta_len(pblk);
957 if (emeta_len > lm->emeta_len[0]) {
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200958 i++;
959 goto add_emeta_page;
960 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200961
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100962 lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
Javier González21d22872017-10-13 14:46:26 +0200963
964 lm->min_blk_line = 1;
Matias Bjørlingfae7fae2018-01-05 14:16:03 +0100965 if (geo->all_luns > 1)
Javier González21d22872017-10-13 14:46:26 +0200966 lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
Javier Gonzáleze46f4e42018-03-30 00:05:10 +0200967 lm->emeta_sec[0], geo->clba);
Javier González21d22872017-10-13 14:46:26 +0200968
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200969 if (lm->min_blk_line > lm->blk_per_line) {
Matias Bjørling4e495a42018-07-13 10:48:42 +0200970 pblk_err(pblk, "config. not supported. Min. LUN in line:%d\n",
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200971 lm->blk_per_line);
Javier Gonzáleze411b332018-03-30 00:04:59 +0200972 return -EINVAL;
Javier Gonzálezb5e063a2017-06-30 17:56:41 +0200973 }
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200974
Javier González43d47122018-03-30 00:05:09 +0200975 return 0;
976}
977
978static int pblk_lines_init(struct pblk *pblk)
979{
980 struct pblk_line_mgmt *l_mg = &pblk->l_mg;
Javier González43d47122018-03-30 00:05:09 +0200981 struct pblk_line *line;
Javier González32ef9412018-03-30 00:05:20 +0200982 void *chunk_meta;
983 long nr_free_chks = 0;
Javier González43d47122018-03-30 00:05:09 +0200984 int i, ret;
985
986 ret = pblk_line_meta_init(pblk);
Javier Gonzálezdd2a4342017-06-26 11:57:17 +0200987 if (ret)
Javier Gonzáleze411b332018-03-30 00:04:59 +0200988 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200989
Javier González43d47122018-03-30 00:05:09 +0200990 ret = pblk_line_mg_init(pblk);
991 if (ret)
992 return ret;
993
994 ret = pblk_luns_init(pblk);
995 if (ret)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200996 goto fail_free_meta;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +0200997
Matias Bjørlingaff3fb12018-10-09 13:11:36 +0200998 chunk_meta = pblk_get_chunk_meta(pblk);
Javier González32ef9412018-03-30 00:05:20 +0200999 if (IS_ERR(chunk_meta)) {
1000 ret = PTR_ERR(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001001 goto fail_free_luns;
1002 }
1003
1004 pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
1005 GFP_KERNEL);
1006 if (!pblk->lines) {
1007 ret = -ENOMEM;
Javier González32ef9412018-03-30 00:05:20 +02001008 goto fail_free_chunk_meta;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001009 }
1010
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001011 for (i = 0; i < l_mg->nr_lines; i++) {
1012 line = &pblk->lines[i];
1013
Javier González32ef9412018-03-30 00:05:20 +02001014 ret = pblk_alloc_line_meta(pblk, line);
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001015 if (ret)
Javier González43d47122018-03-30 00:05:09 +02001016 goto fail_free_lines;
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001017
Javier González32ef9412018-03-30 00:05:20 +02001018 nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
Hans Holmbergf2937232018-10-09 13:11:53 +02001019
1020 trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1021 line->state);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001022 }
1023
Javier González2deeefc2018-06-01 15:04:15 +02001024 if (!nr_free_chks) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001025 pblk_err(pblk, "too many bad blocks prevent for sane instance\n");
Javier González2deeefc2018-06-01 15:04:15 +02001026 return -EINTR;
1027 }
1028
Javier González32ef9412018-03-30 00:05:20 +02001029 pblk_set_provision(pblk, nr_free_chks);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001030
Javier González090ee262018-10-09 13:12:01 +02001031 vfree(chunk_meta);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001032 return 0;
Javier Gonzáleze411b332018-03-30 00:04:59 +02001033
Javier González43d47122018-03-30 00:05:09 +02001034fail_free_lines:
Javier Gonzálezdffdd962017-06-26 11:57:22 +02001035 while (--i >= 0)
Hans Holmberg48b8d202018-06-01 16:41:06 +02001036 pblk_line_meta_free(l_mg, &pblk->lines[i]);
Javier González43d47122018-03-30 00:05:09 +02001037 kfree(pblk->lines);
Javier González32ef9412018-03-30 00:05:20 +02001038fail_free_chunk_meta:
1039 kfree(chunk_meta);
Javier González43d47122018-03-30 00:05:09 +02001040fail_free_luns:
1041 kfree(pblk->luns);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001042fail_free_meta:
Javier Gonzáleze411b332018-03-30 00:04:59 +02001043 pblk_line_mg_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001044
1045 return ret;
1046}
1047
1048static int pblk_writer_init(struct pblk *pblk)
1049{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001050 pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
1051 if (IS_ERR(pblk->writer_ts)) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001052 int err = PTR_ERR(pblk->writer_ts);
1053
1054 if (err != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001055 pblk_err(pblk, "could not allocate writer kthread (%d)\n",
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001056 err);
1057 return err;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001058 }
1059
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001060 timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1061 mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1062
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001063 return 0;
1064}
1065
1066static void pblk_writer_stop(struct pblk *pblk)
1067{
Javier Gonzálezee8d5c12017-06-30 17:56:40 +02001068 /* The pipeline must be stopped and the write buffer emptied before the
1069 * write thread is stopped
1070 */
1071 WARN(pblk_rb_read_count(&pblk->rwb),
1072 "Stopping not fully persisted write buffer\n");
1073
1074 WARN(pblk_rb_sync_count(&pblk->rwb),
1075 "Stopping not fully synced write buffer\n");
1076
Hans Holmberg7be970b2018-03-30 00:04:56 +02001077 del_timer_sync(&pblk->wtimer);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001078 if (pblk->writer_ts)
1079 kthread_stop(pblk->writer_ts);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001080}
1081
1082static void pblk_free(struct pblk *pblk)
1083{
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001084 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001085 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001086 pblk_rwb_free(pblk);
1087 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001088
1089 kfree(pblk);
1090}
1091
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001092static void pblk_tear_down(struct pblk *pblk, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001093{
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001094 if (graceful)
1095 __pblk_pipeline_flush(pblk);
1096 __pblk_pipeline_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001097 pblk_writer_stop(pblk);
1098 pblk_rb_sync_l2p(&pblk->rwb);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001099 pblk_rl_free(&pblk->rl);
1100
Matias Bjørling4e495a42018-07-13 10:48:42 +02001101 pblk_debug(pblk, "consistent tear down (graceful:%d)\n", graceful);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001102}
1103
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001104static void pblk_exit(void *private, bool graceful)
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001105{
1106 struct pblk *pblk = private;
1107
Javier Gonzáleza7c9e912018-06-01 15:04:24 +02001108 pblk_gc_exit(pblk, graceful);
1109 pblk_tear_down(pblk, graceful);
Hans Holmbergc5586192017-10-13 14:46:40 +02001110
Matias Bjørling880eda52018-07-13 10:48:37 +02001111#ifdef CONFIG_NVM_PBLK_DEBUG
Matias Bjørling4e495a42018-07-13 10:48:42 +02001112 pblk_info(pblk, "exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
Hans Holmbergc5586192017-10-13 14:46:40 +02001113#endif
1114
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001115 pblk_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001116}
1117
1118static sector_t pblk_capacity(void *private)
1119{
1120 struct pblk *pblk = private;
1121
1122 return pblk->capacity * NR_PHY_IN_LOG;
1123}
1124
1125static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1126 int flags)
1127{
1128 struct nvm_geo *geo = &dev->geo;
1129 struct request_queue *bqueue = dev->q;
1130 struct request_queue *tqueue = tdisk->queue;
1131 struct pblk *pblk;
1132 int ret;
1133
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001134 pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1135 if (!pblk)
1136 return ERR_PTR(-ENOMEM);
1137
1138 pblk->dev = dev;
1139 pblk->disk = tdisk;
Javier González588726d32017-06-26 11:57:29 +02001140 pblk->state = PBLK_STATE_RUNNING;
Hans Holmberg1b0dd0b2018-10-09 13:11:54 +02001141 trace_pblk_state(pblk_disk_name(pblk), pblk->state);
Hans Holmberg3e3a5b82017-10-13 14:46:34 +02001142 pblk->gc.gc_enabled = 0;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001143
Matias Bjørling4e495a42018-07-13 10:48:42 +02001144 if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1145 geo->version == NVM_OCSSD_SPEC_20)) {
1146 pblk_err(pblk, "OCSSD version not supported (%u)\n",
1147 geo->version);
1148 kfree(pblk);
1149 return ERR_PTR(-EINVAL);
1150 }
1151
Hans Holmberg6a3abf52018-06-01 16:41:05 +02001152 spin_lock_init(&pblk->resubmit_lock);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001153 spin_lock_init(&pblk->trans_lock);
1154 spin_lock_init(&pblk->lock);
1155
Matias Bjørling880eda52018-07-13 10:48:37 +02001156#ifdef CONFIG_NVM_PBLK_DEBUG
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001157 atomic_long_set(&pblk->inflight_writes, 0);
1158 atomic_long_set(&pblk->padded_writes, 0);
1159 atomic_long_set(&pblk->padded_wb, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001160 atomic_long_set(&pblk->req_writes, 0);
1161 atomic_long_set(&pblk->sub_writes, 0);
1162 atomic_long_set(&pblk->sync_writes, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001163 atomic_long_set(&pblk->inflight_reads, 0);
Javier Gonzálezdb7ada32017-06-26 11:57:13 +02001164 atomic_long_set(&pblk->cache_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001165 atomic_long_set(&pblk->sync_reads, 0);
1166 atomic_long_set(&pblk->recov_writes, 0);
1167 atomic_long_set(&pblk->recov_writes, 0);
1168 atomic_long_set(&pblk->recov_gc_writes, 0);
Javier Gonzáleza1121172017-10-13 14:46:01 +02001169 atomic_long_set(&pblk->recov_gc_reads, 0);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001170#endif
1171
1172 atomic_long_set(&pblk->read_failed, 0);
1173 atomic_long_set(&pblk->read_empty, 0);
1174 atomic_long_set(&pblk->read_high_ecc, 0);
1175 atomic_long_set(&pblk->read_failed_gc, 0);
1176 atomic_long_set(&pblk->write_failed, 0);
1177 atomic_long_set(&pblk->erase_failed, 0);
1178
Javier González43d47122018-03-30 00:05:09 +02001179 ret = pblk_core_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001180 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001181 pblk_err(pblk, "could not initialize core\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001182 goto fail;
1183 }
1184
1185 ret = pblk_lines_init(pblk);
1186 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001187 pblk_err(pblk, "could not initialize lines\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001188 goto fail_free_core;
1189 }
1190
Javier González43d47122018-03-30 00:05:09 +02001191 ret = pblk_rwb_init(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001192 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001193 pblk_err(pblk, "could not initialize write buffer\n");
Javier González43d47122018-03-30 00:05:09 +02001194 goto fail_free_lines;
1195 }
1196
1197 ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1198 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001199 pblk_err(pblk, "could not initialize maps\n");
Javier González43d47122018-03-30 00:05:09 +02001200 goto fail_free_rwb;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001201 }
1202
1203 ret = pblk_writer_init(pblk);
1204 if (ret) {
Javier Gonzálezcc4f5ba2018-01-05 14:16:17 +01001205 if (ret != -EINTR)
Matias Bjørling4e495a42018-07-13 10:48:42 +02001206 pblk_err(pblk, "could not initialize write thread\n");
Javier González43d47122018-03-30 00:05:09 +02001207 goto fail_free_l2p;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001208 }
1209
1210 ret = pblk_gc_init(pblk);
1211 if (ret) {
Matias Bjørling4e495a42018-07-13 10:48:42 +02001212 pblk_err(pblk, "could not initialize gc\n");
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001213 goto fail_stop_writer;
1214 }
1215
1216 /* inherit the size from the underlying device */
1217 blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1218 blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1219
1220 blk_queue_write_cache(tqueue, true, false);
1221
Javier Gonzáleze46f4e42018-03-30 00:05:10 +02001222 tqueue->limits.discard_granularity = geo->clba * geo->csecs;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001223 tqueue->limits.discard_alignment = 0;
1224 blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
Bart Van Assche8b904b52018-03-07 17:10:10 -08001225 blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001226
Matias Bjørling4e495a42018-07-13 10:48:42 +02001227 pblk_info(pblk, "luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
Matias Bjørlingfae7fae2018-01-05 14:16:03 +01001228 geo->all_luns, pblk->l_mg.nr_lines,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001229 (unsigned long long)pblk->rl.nr_secs,
1230 pblk->rwb.nr_entries);
1231
1232 wake_up_process(pblk->writer_ts);
Hans Holmberg03661b52017-10-13 14:46:37 +02001233
1234 /* Check if we need to start GC */
1235 pblk_gc_should_kick(pblk);
1236
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001237 return pblk;
1238
1239fail_stop_writer:
1240 pblk_writer_stop(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001241fail_free_l2p:
1242 pblk_l2p_free(pblk);
Javier González43d47122018-03-30 00:05:09 +02001243fail_free_rwb:
1244 pblk_rwb_free(pblk);
1245fail_free_lines:
1246 pblk_lines_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001247fail_free_core:
1248 pblk_core_free(pblk);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001249fail:
1250 kfree(pblk);
1251 return ERR_PTR(ret);
1252}
1253
1254/* physical block device target */
1255static struct nvm_tgt_type tt_pblk = {
1256 .name = "pblk",
1257 .version = {1, 0, 0},
1258
1259 .make_rq = pblk_make_rq,
1260 .capacity = pblk_capacity,
1261
1262 .init = pblk_init,
1263 .exit = pblk_exit,
1264
1265 .sysfs_init = pblk_sysfs_init,
1266 .sysfs_exit = pblk_sysfs_exit,
Rakesh Pandit90014822017-10-13 14:45:50 +02001267 .owner = THIS_MODULE,
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001268};
1269
1270static int __init pblk_module_init(void)
1271{
NeilBrownb25d5232017-06-18 14:38:58 +10001272 int ret;
1273
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001274 ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
1275 if (ret)
1276 return ret;
NeilBrownb25d5232017-06-18 14:38:58 +10001277 ret = nvm_register_tgt_type(&tt_pblk);
1278 if (ret)
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001279 bioset_exit(&pblk_bio_set);
NeilBrownb25d5232017-06-18 14:38:58 +10001280 return ret;
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001281}
1282
1283static void pblk_module_exit(void)
1284{
Kent Overstreetb906bbb2018-05-20 18:25:50 -04001285 bioset_exit(&pblk_bio_set);
Javier Gonzáleza4bd2172017-04-15 20:55:50 +02001286 nvm_unregister_tgt_type(&tt_pblk);
1287}
1288
1289module_init(pblk_module_init);
1290module_exit(pblk_module_exit);
1291MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1292MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1293MODULE_LICENSE("GPL v2");
1294MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");