pblk-init.c 33.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2015 IT University of Copenhagen (rrpc.c)
 * Copyright (C) 2016 CNEX Labs
 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
 *                  Matias Bjorling <matias@cnexlabs.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * Implementation of a physical block-device target for Open-channel SSDs.
 *
 * pblk-init.c - pblk's initialization.
 */

#include "pblk.h"

23
static unsigned int write_buffer_size;
24 25 26 27

module_param(write_buffer_size, uint, 0644);
MODULE_PARM_DESC(write_buffer_size, "number of entries in a write buffer");

28
static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache,
29
				*pblk_w_rq_cache;
30
static DECLARE_RWSEM(pblk_lock);
31
struct bio_set pblk_bio_set;
32 33 34 35 36 37 38 39 40 41

static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
			  struct bio *bio)
{
	int ret;

	/* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
	 * constraint. Writes can be of arbitrary size.
	 */
	if (bio_data_dir(bio) == READ) {
42
		blk_queue_split(q, &bio);
43 44 45 46 47 48 49 50 51 52 53
		ret = pblk_submit_read(pblk, bio);
		if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
			bio_put(bio);

		return ret;
	}

	/* Prevent deadlock in the case of a modest LUN configuration and large
	 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
	 * available for user I/O.
	 */
54
	if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
55
		blk_queue_split(q, &bio);
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

	return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
}

static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
{
	struct pblk *pblk = q->queuedata;

	if (bio_op(bio) == REQ_OP_DISCARD) {
		pblk_discard(pblk, bio);
		if (!(bio->bi_opf & REQ_PREFLUSH)) {
			bio_endio(bio);
			return BLK_QC_T_NONE;
		}
	}

	switch (pblk_rw_io(q, pblk, bio)) {
	case NVM_IO_ERR:
		bio_io_error(bio);
		break;
	case NVM_IO_DONE:
		bio_endio(bio);
		break;
	}

	return BLK_QC_T_NONE;
}

84 85 86 87
static size_t pblk_trans_map_size(struct pblk *pblk)
{
	int entry_size = 8;

88
	if (pblk->addrf_len < 32)
89 90 91 92 93
		entry_size = 4;

	return entry_size * pblk->rl.nr_secs;
}

94
#ifdef CONFIG_NVM_PBLK_DEBUG
95 96 97 98 99 100 101 102 103 104 105
static u32 pblk_l2p_crc(struct pblk *pblk)
{
	size_t map_size;
	u32 crc = ~(u32)0;

	map_size = pblk_trans_map_size(pblk);
	crc = crc32_le(crc, pblk->trans_map, map_size);
	return crc;
}
#endif

106 107 108 109 110
static void pblk_l2p_free(struct pblk *pblk)
{
	vfree(pblk->trans_map);
}

111 112 113 114 115 116 117 118 119
static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
{
	struct pblk_line *line = NULL;

	if (factory_init) {
		pblk_setup_uuid(pblk);
	} else {
		line = pblk_recov_l2p(pblk);
		if (IS_ERR(line)) {
120
			pblk_err(pblk, "could not recover l2p table\n");
121 122 123 124
			return -EFAULT;
		}
	}

125
#ifdef CONFIG_NVM_PBLK_DEBUG
126
	pblk_info(pblk, "init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
127 128 129 130 131 132 133 134
#endif

	/* Free full lines directly as GC has not been started yet */
	pblk_gc_free_full_lines(pblk);

	if (!line) {
		/* Configure next line for user data */
		line = pblk_line_get_first_data(pblk);
135
		if (!line)
136 137 138 139 140 141 142
			return -EFAULT;
	}

	return 0;
}

static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
143 144 145
{
	sector_t i;
	struct ppa_addr ppa;
146
	size_t map_size;
147
	int ret = 0;
148

149 150
	map_size = pblk_trans_map_size(pblk);
	pblk->trans_map = vmalloc(map_size);
151 152 153 154 155 156 157 158
	if (!pblk->trans_map)
		return -ENOMEM;

	pblk_ppa_set_empty(&ppa);

	for (i = 0; i < pblk->rl.nr_secs; i++)
		pblk_trans_map_set(pblk, i, ppa);

159 160 161 162 163
	ret = pblk_l2p_recover(pblk, factory_init);
	if (ret)
		vfree(pblk->trans_map);

	return ret;
164 165 166 167 168
}

static void pblk_rwb_free(struct pblk *pblk)
{
	if (pblk_rb_tear_down_check(&pblk->rwb))
169
		pblk_err(pblk, "write buffer error on tear down\n");
170 171 172 173 174 175 176 177 178 179

	pblk_rb_data_free(&pblk->rwb);
	vfree(pblk_rb_entries_ref(&pblk->rwb));
}

static int pblk_rwb_init(struct pblk *pblk)
{
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
	struct pblk_rb_entry *entries;
180
	unsigned long nr_entries, buffer_size;
181
	unsigned int power_size, power_seg_sz;
182
	int pgs_in_buffer;
183

184 185 186
	pgs_in_buffer = max(geo->mw_cunits, geo->ws_opt) * geo->all_luns;

	if (write_buffer_size && (write_buffer_size > pgs_in_buffer))
187 188
		buffer_size = write_buffer_size;
	else
189
		buffer_size = pgs_in_buffer;
190 191

	nr_entries = pblk_rb_calculate_size(buffer_size);
192

193
	entries = vzalloc(array_size(nr_entries, sizeof(struct pblk_rb_entry)));
194 195 196 197
	if (!entries)
		return -ENOMEM;

	power_size = get_count_order(nr_entries);
198
	power_seg_sz = get_count_order(geo->csecs);
199 200 201 202 203 204 205

	return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
}

/* Minimum pages needed within a lun */
#define ADDR_POOL_SIZE 64

206 207
static int pblk_set_addrf_12(struct pblk *pblk, struct nvm_geo *geo,
			     struct nvm_addrf_12 *dst)
208
{
209 210
	struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
	int power_len;
211 212

	/* Re-calculate channel and lun format to adapt to configuration */
213 214
	power_len = get_count_order(geo->num_ch);
	if (1 << power_len != geo->num_ch) {
215
		pblk_err(pblk, "supports only power-of-two channel config.\n");
216 217
		return -EINVAL;
	}
218
	dst->ch_len = power_len;
219

220 221
	power_len = get_count_order(geo->num_lun);
	if (1 << power_len != geo->num_lun) {
222
		pblk_err(pblk, "supports only power-of-two LUN config.\n");
223 224
		return -EINVAL;
	}
225 226 227 228 229
	dst->lun_len = power_len;

	dst->blk_len = src->blk_len;
	dst->pg_len = src->pg_len;
	dst->pln_len = src->pln_len;
230
	dst->sec_len = src->sec_len;
231

232 233
	dst->sec_offset = 0;
	dst->pln_offset = dst->sec_len;
234 235 236 237 238
	dst->ch_offset = dst->pln_offset + dst->pln_len;
	dst->lun_offset = dst->ch_offset + dst->ch_len;
	dst->pg_offset = dst->lun_offset + dst->lun_len;
	dst->blk_offset = dst->pg_offset + dst->pg_len;

239
	dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
240 241 242 243 244 245 246 247 248
	dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
	dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
	dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
	dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
	dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;

	return dst->blk_offset + src->blk_len;
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
			     struct pblk_addrf *udst)
{
	struct nvm_addrf *src = &geo->addrf;

	adst->ch_len = get_count_order(geo->num_ch);
	adst->lun_len = get_count_order(geo->num_lun);
	adst->chk_len = src->chk_len;
	adst->sec_len = src->sec_len;

	adst->sec_offset = 0;
	adst->ch_offset = adst->sec_len;
	adst->lun_offset = adst->ch_offset + adst->ch_len;
	adst->chk_offset = adst->lun_offset + adst->lun_len;

	adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
	adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
	adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
	adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;

	udst->sec_stripe = geo->ws_opt;
	udst->ch_stripe = geo->num_ch;
	udst->lun_stripe = geo->num_lun;

	udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
	udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;

	return adst->chk_offset + adst->chk_len;
}

279
static int pblk_set_addrf(struct pblk *pblk)
280 281 282 283 284
{
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
	int mod;

285 286 287 288
	switch (geo->version) {
	case NVM_OCSSD_SPEC_12:
		div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
		if (mod) {
289
			pblk_err(pblk, "bad configuration of sectors/pages\n");
290 291 292
			return -EINVAL;
		}

293 294
		pblk->addrf_len = pblk_set_addrf_12(pblk, geo,
							(void *)&pblk->addrf);
295 296 297
		break;
	case NVM_OCSSD_SPEC_20:
		pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
298
							&pblk->uaddrf);
299 300
		break;
	default:
301
		pblk_err(pblk, "OCSSD revision not supported (%d)\n",
302
								geo->version);
303 304 305
		return -EINVAL;
	}

306 307 308 309 310 311
	return 0;
}

static int pblk_init_global_caches(struct pblk *pblk)
{
	down_write(&pblk_lock);
312
	pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
313
				sizeof(struct pblk_line_ws), 0, 0, NULL);
314
	if (!pblk_ws_cache) {
315 316 317 318 319 320 321
		up_write(&pblk_lock);
		return -ENOMEM;
	}

	pblk_rec_cache = kmem_cache_create("pblk_rec",
				sizeof(struct pblk_rec_ctx), 0, 0, NULL);
	if (!pblk_rec_cache) {
322
		kmem_cache_destroy(pblk_ws_cache);
323 324 325 326
		up_write(&pblk_lock);
		return -ENOMEM;
	}

327
	pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
328
				0, 0, NULL);
329
	if (!pblk_g_rq_cache) {
330
		kmem_cache_destroy(pblk_ws_cache);
331 332 333 334 335 336 337 338
		kmem_cache_destroy(pblk_rec_cache);
		up_write(&pblk_lock);
		return -ENOMEM;
	}

	pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
				0, 0, NULL);
	if (!pblk_w_rq_cache) {
339
		kmem_cache_destroy(pblk_ws_cache);
340
		kmem_cache_destroy(pblk_rec_cache);
341
		kmem_cache_destroy(pblk_g_rq_cache);
342 343 344 345 346 347 348 349
		up_write(&pblk_lock);
		return -ENOMEM;
	}
	up_write(&pblk_lock);

	return 0;
}

350 351 352 353 354 355 356 357
static void pblk_free_global_caches(struct pblk *pblk)
{
	kmem_cache_destroy(pblk_ws_cache);
	kmem_cache_destroy(pblk_rec_cache);
	kmem_cache_destroy(pblk_g_rq_cache);
	kmem_cache_destroy(pblk_w_rq_cache);
}

358 359 360 361
static int pblk_core_init(struct pblk *pblk)
{
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
362
	int ret, max_write_ppas;
363 364 365 366 367 368 369 370 371 372

	atomic64_set(&pblk->user_wa, 0);
	atomic64_set(&pblk->pad_wa, 0);
	atomic64_set(&pblk->gc_wa, 0);
	pblk->user_rst_wa = 0;
	pblk->pad_rst_wa = 0;
	pblk->gc_rst_wa = 0;

	atomic64_set(&pblk->nr_flush, 0);
	pblk->nr_flush_rst = 0;
373

374
	pblk->min_write_pgs = geo->ws_opt;
375 376 377 378 379
	max_write_ppas = pblk->min_write_pgs * geo->all_luns;
	pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
	pblk_set_sec_per_write(pblk, pblk->min_write_pgs);

	if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
380
		pblk_err(pblk, "vector list too big(%u > %u)\n",
381 382 383 384
				pblk->max_write_pgs, PBLK_MAX_REQ_ADDRS);
		return -EINVAL;
	}

K
Kees Cook 已提交
385
	pblk->pad_dist = kcalloc(pblk->min_write_pgs - 1, sizeof(atomic64_t),
386 387
								GFP_KERNEL);
	if (!pblk->pad_dist)
388 389
		return -ENOMEM;

390 391 392
	if (pblk_init_global_caches(pblk))
		goto fail_free_pad_dist;

393
	/* Internal bios can be at most the sectors signaled by the device. */
394 395
	ret = mempool_init_page_pool(&pblk->page_bio_pool, NVM_MAX_VLBA, 0);
	if (ret)
396
		goto free_global_caches;
397

398 399 400
	ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
				     pblk_ws_cache);
	if (ret)
401
		goto free_page_bio_pool;
402

403 404 405
	ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
				     pblk_rec_cache);
	if (ret)
406
		goto free_gen_ws_pool;
407

408 409 410
	ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
				     pblk_g_rq_cache);
	if (ret)
411 412
		goto free_rec_pool;

413 414 415
	ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
				     pblk_g_rq_cache);
	if (ret)
416 417
		goto free_r_rq_pool;

418 419 420
	ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
				     pblk_w_rq_cache);
	if (ret)
421
		goto free_e_rq_pool;
422

423 424 425
	pblk->close_wq = alloc_workqueue("pblk-close-wq",
			WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
	if (!pblk->close_wq)
426
		goto free_w_rq_pool;
427

428 429 430 431 432
	pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
			WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
	if (!pblk->bb_wq)
		goto free_close_wq;

433 434 435
	pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
			WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
	if (!pblk->r_end_wq)
436
		goto free_bb_wq;
437

438
	if (pblk_set_addrf(pblk))
439 440
		goto free_r_end_wq;

441
	INIT_LIST_HEAD(&pblk->compl_list);
442
	INIT_LIST_HEAD(&pblk->resubmit_list);
443

444 445
	return 0;

446 447
free_r_end_wq:
	destroy_workqueue(pblk->r_end_wq);
448 449 450 451
free_bb_wq:
	destroy_workqueue(pblk->bb_wq);
free_close_wq:
	destroy_workqueue(pblk->close_wq);
452
free_w_rq_pool:
453
	mempool_exit(&pblk->w_rq_pool);
454
free_e_rq_pool:
455
	mempool_exit(&pblk->e_rq_pool);
456
free_r_rq_pool:
457
	mempool_exit(&pblk->r_rq_pool);
458
free_rec_pool:
459
	mempool_exit(&pblk->rec_pool);
460
free_gen_ws_pool:
461
	mempool_exit(&pblk->gen_ws_pool);
462
free_page_bio_pool:
463
	mempool_exit(&pblk->page_bio_pool);
464 465
free_global_caches:
	pblk_free_global_caches(pblk);
466 467
fail_free_pad_dist:
	kfree(pblk->pad_dist);
468 469 470 471 472
	return -ENOMEM;
}

static void pblk_core_free(struct pblk *pblk)
{
473 474 475
	if (pblk->close_wq)
		destroy_workqueue(pblk->close_wq);

476 477 478
	if (pblk->r_end_wq)
		destroy_workqueue(pblk->r_end_wq);

479 480
	if (pblk->bb_wq)
		destroy_workqueue(pblk->bb_wq);
481

482 483 484 485 486 487
	mempool_exit(&pblk->page_bio_pool);
	mempool_exit(&pblk->gen_ws_pool);
	mempool_exit(&pblk->rec_pool);
	mempool_exit(&pblk->r_rq_pool);
	mempool_exit(&pblk->e_rq_pool);
	mempool_exit(&pblk->w_rq_pool);
488

489
	pblk_free_global_caches(pblk);
490
	kfree(pblk->pad_dist);
491 492
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
static void pblk_line_mg_free(struct pblk *pblk)
{
	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
	int i;

	kfree(l_mg->bb_template);
	kfree(l_mg->bb_aux);
	kfree(l_mg->vsc_list);

	for (i = 0; i < PBLK_DATA_LINES; i++) {
		kfree(l_mg->sline_meta[i]);
		pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
		kfree(l_mg->eline_meta[i]);
	}
}

509 510
static void pblk_line_meta_free(struct pblk_line_mgmt *l_mg,
				struct pblk_line *line)
511
{
512 513
	struct pblk_w_err_gc *w_err_gc = line->w_err_gc;

514 515
	kfree(line->blk_bitmap);
	kfree(line->erase_bitmap);
516
	kfree(line->chks);
517 518 519

	pblk_mfree(w_err_gc->lba_list, l_mg->emeta_alloc_type);
	kfree(w_err_gc);
520 521
}

522 523 524 525 526 527 528 529 530 531
static void pblk_lines_free(struct pblk *pblk)
{
	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
	struct pblk_line *line;
	int i;

	spin_lock(&l_mg->free_lock);
	for (i = 0; i < l_mg->nr_lines; i++) {
		line = &pblk->lines[i];

532
		pblk_line_free(line);
533
		pblk_line_meta_free(l_mg, line);
534 535
	}
	spin_unlock(&l_mg->free_lock);
536 537 538 539 540

	pblk_line_mg_free(pblk);

	kfree(pblk->luns);
	kfree(pblk->lines);
541 542
}

543 544
static int pblk_bb_get_tbl(struct nvm_tgt_dev *dev, struct pblk_lun *rlun,
			   u8 *blks, int nr_blks)
545 546
{
	struct ppa_addr ppa;
547
	int ret;
548 549 550 551 552 553 554

	ppa.ppa = 0;
	ppa.g.ch = rlun->bppa.g.ch;
	ppa.g.lun = rlun->bppa.g.lun;

	ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
	if (ret)
555
		return ret;
556 557

	nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
558 559
	if (nr_blks < 0)
		return -EIO;
560

561
	return 0;
562 563
}

564
static void *pblk_bb_get_meta(struct pblk *pblk)
565
{
566 567
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
568
	u8 *meta;
569 570
	int i, nr_blks, blk_per_lun;
	int ret;
571

572
	blk_per_lun = geo->num_chk * geo->pln_mode;
573
	nr_blks = blk_per_lun * geo->all_luns;
574

575 576
	meta = kmalloc(nr_blks, GFP_KERNEL);
	if (!meta)
577 578 579 580
		return ERR_PTR(-ENOMEM);

	for (i = 0; i < geo->all_luns; i++) {
		struct pblk_lun *rlun = &pblk->luns[i];
581
		u8 *meta_pos = meta + i * blk_per_lun;
582

583
		ret = pblk_bb_get_tbl(dev, rlun, meta_pos, blk_per_lun);
584
		if (ret) {
585
			kfree(meta);
586 587
			return ERR_PTR(-EIO);
		}
588 589
	}

590
	return meta;
591 592
}

593
static void *pblk_chunk_get_meta(struct pblk *pblk)
594
{
595 596
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
597

598 599 600 601
	if (geo->version == NVM_OCSSD_SPEC_12)
		return pblk_bb_get_meta(pblk);
	else
		return pblk_chunk_get_info(pblk);
602 603
}

604
static int pblk_luns_init(struct pblk *pblk)
605 606 607 608
{
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
	struct pblk_lun *rlun;
609
	int i;
610 611

	/* TODO: Implement unbalanced LUN support */
612
	if (geo->num_lun < 0) {
613
		pblk_err(pblk, "unbalanced LUN config.\n");
614 615 616
		return -EINVAL;
	}

617 618
	pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
								GFP_KERNEL);
619 620 621
	if (!pblk->luns)
		return -ENOMEM;

622
	for (i = 0; i < geo->all_luns; i++) {
623
		/* Stripe across channels */
624 625 626
		int ch = i % geo->num_ch;
		int lun_raw = i / geo->num_ch;
		int lunid = lun_raw + ch * geo->num_lun;
627 628

		rlun = &pblk->luns[i];
629
		rlun->bppa = dev->luns[lunid];
630 631 632 633 634 635 636 637

		sema_init(&rlun->wr_sem, 1);
	}

	return 0;
}

/* See comment over struct line_emeta definition */
638
static unsigned int calc_emeta_len(struct pblk *pblk)
639
{
640 641 642 643 644 645 646
	struct pblk_line_meta *lm = &pblk->lm;
	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;

	/* Round to sector size so that lba_list starts on its own sector */
	lm->emeta_sec[1] = DIV_ROUND_UP(
647
			sizeof(struct line_emeta) + lm->blk_bitmap_len +
648 649
			sizeof(struct wa_counters), geo->csecs);
	lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
650 651 652 653

	/* Round to sector size so that vsc_list starts on its own sector */
	lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
	lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
654 655
			geo->csecs);
	lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
656 657

	lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
658 659
			geo->csecs);
	lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
660 661 662 663

	lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);

	return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
664 665 666 667 668
}

static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
{
	struct nvm_tgt_dev *dev = pblk->dev;
669 670
	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
	struct pblk_line_meta *lm = &pblk->lm;
671 672
	struct nvm_geo *geo = &dev->geo;
	sector_t provisioned;
673
	int sec_meta, blk_meta;
674

675 676 677 678
	if (geo->op == NVM_TARGET_DEFAULT_OP)
		pblk->op = PBLK_DEFAULT_OP;
	else
		pblk->op = geo->op;
679 680

	provisioned = nr_free_blks;
681
	provisioned *= (100 - pblk->op);
682 683
	sector_div(provisioned, 100);

684 685
	pblk->op_blks = nr_free_blks - provisioned;

686 687 688 689
	/* Internally pblk manages all free blocks, but all calculations based
	 * on user capacity consider only provisioned blocks
	 */
	pblk->rl.total_blocks = nr_free_blks;
690
	pblk->rl.nr_secs = nr_free_blks * geo->clba;
691 692 693

	/* Consider sectors used for metadata */
	sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
694
	blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
695

696
	pblk->capacity = (provisioned - blk_meta) * geo->clba;
697

698
	atomic_set(&pblk->rl.free_blocks, nr_free_blks);
699
	atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
700 701
}

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
static int pblk_setup_line_meta_12(struct pblk *pblk, struct pblk_line *line,
				   void *chunk_meta)
{
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
	struct pblk_line_meta *lm = &pblk->lm;
	int i, chk_per_lun, nr_bad_chks = 0;

	chk_per_lun = geo->num_chk * geo->pln_mode;

	for (i = 0; i < lm->blk_per_line; i++) {
		struct pblk_lun *rlun = &pblk->luns[i];
		struct nvm_chk_meta *chunk;
		int pos = pblk_ppa_to_pos(geo, rlun->bppa);
		u8 *lun_bb_meta = chunk_meta + pos * chk_per_lun;

		chunk = &line->chks[pos];

		/*
		 * In 1.2 spec. chunk state is not persisted by the device. Thus
722 723
		 * some of the values are reset each time pblk is instantiated,
		 * so we have to assume that the block is closed.
724 725
		 */
		if (lun_bb_meta[line->id] == NVM_BLK_T_FREE)
726
			chunk->state =  NVM_CHK_ST_CLOSED;
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
		else
			chunk->state = NVM_CHK_ST_OFFLINE;

		chunk->type = NVM_CHK_TP_W_SEQ;
		chunk->wi = 0;
		chunk->slba = -1;
		chunk->cnlb = geo->clba;
		chunk->wp = 0;

		if (!(chunk->state & NVM_CHK_ST_OFFLINE))
			continue;

		set_bit(pos, line->blk_bitmap);
		nr_bad_chks++;
	}

	return nr_bad_chks;
}

static int pblk_setup_line_meta_20(struct pblk *pblk, struct pblk_line *line,
				   struct nvm_chk_meta *meta)
{
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
	struct pblk_line_meta *lm = &pblk->lm;
	int i, nr_bad_chks = 0;

	for (i = 0; i < lm->blk_per_line; i++) {
		struct pblk_lun *rlun = &pblk->luns[i];
		struct nvm_chk_meta *chunk;
		struct nvm_chk_meta *chunk_meta;
		struct ppa_addr ppa;
		int pos;

		ppa = rlun->bppa;
		pos = pblk_ppa_to_pos(geo, ppa);
		chunk = &line->chks[pos];

		ppa.m.chk = line->id;
		chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);

		chunk->state = chunk_meta->state;
		chunk->type = chunk_meta->type;
		chunk->wi = chunk_meta->wi;
		chunk->slba = chunk_meta->slba;
		chunk->cnlb = chunk_meta->cnlb;
		chunk->wp = chunk_meta->wp;

		if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
			WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
			continue;
		}

780 781 782
		if (!(chunk->state & NVM_CHK_ST_OFFLINE))
			continue;

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827
		set_bit(pos, line->blk_bitmap);
		nr_bad_chks++;
	}

	return nr_bad_chks;
}

static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
				 void *chunk_meta, int line_id)
{
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
	struct pblk_line_meta *lm = &pblk->lm;
	long nr_bad_chks, chk_in_line;

	line->pblk = pblk;
	line->id = line_id;
	line->type = PBLK_LINETYPE_FREE;
	line->state = PBLK_LINESTATE_NEW;
	line->gc_group = PBLK_LINEGC_NONE;
	line->vsc = &l_mg->vsc_list[line_id];
	spin_lock_init(&line->lock);

	if (geo->version == NVM_OCSSD_SPEC_12)
		nr_bad_chks = pblk_setup_line_meta_12(pblk, line, chunk_meta);
	else
		nr_bad_chks = pblk_setup_line_meta_20(pblk, line, chunk_meta);

	chk_in_line = lm->blk_per_line - nr_bad_chks;
	if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
					chk_in_line < lm->min_blk_line) {
		line->state = PBLK_LINESTATE_BAD;
		list_add_tail(&line->list, &l_mg->bad_list);
		return 0;
	}

	atomic_set(&line->blk_in_line, chk_in_line);
	list_add_tail(&line->list, &l_mg->free_list);
	l_mg->nr_free_lines++;

	return chk_in_line;
}

static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
828 829 830 831 832 833 834 835
{
	struct pblk_line_meta *lm = &pblk->lm;

	line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
	if (!line->blk_bitmap)
		return -ENOMEM;

	line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
836 837 838
	if (!line->erase_bitmap)
		goto free_blk_bitmap;

839

840 841
	line->chks = kmalloc_array(lm->blk_per_line,
				   sizeof(struct nvm_chk_meta), GFP_KERNEL);
842 843 844 845 846 847
	if (!line->chks)
		goto free_erase_bitmap;

	line->w_err_gc = kzalloc(sizeof(struct pblk_w_err_gc), GFP_KERNEL);
	if (!line->w_err_gc)
		goto free_chks;
848 849

	return 0;
850 851 852 853 854 855 856 857

free_chks:
	kfree(line->chks);
free_erase_bitmap:
	kfree(line->erase_bitmap);
free_blk_bitmap:
	kfree(line->blk_bitmap);
	return -ENOMEM;
858 859 860
}

static int pblk_line_mg_init(struct pblk *pblk)
861
{
862 863
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
864 865
	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
	struct pblk_line_meta *lm = &pblk->lm;
866 867
	int i, bb_distance;

868
	l_mg->nr_lines = geo->num_chk;
869 870 871 872 873 874 875 876 877 878 879 880 881
	l_mg->log_line = l_mg->data_line = NULL;
	l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
	l_mg->nr_free_lines = 0;
	bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);

	INIT_LIST_HEAD(&l_mg->free_list);
	INIT_LIST_HEAD(&l_mg->corrupt_list);
	INIT_LIST_HEAD(&l_mg->bad_list);
	INIT_LIST_HEAD(&l_mg->gc_full_list);
	INIT_LIST_HEAD(&l_mg->gc_high_list);
	INIT_LIST_HEAD(&l_mg->gc_mid_list);
	INIT_LIST_HEAD(&l_mg->gc_low_list);
	INIT_LIST_HEAD(&l_mg->gc_empty_list);
882
	INIT_LIST_HEAD(&l_mg->gc_werr_list);
883 884 885

	INIT_LIST_HEAD(&l_mg->emeta_list);

886 887 888 889
	l_mg->gc_lists[0] = &l_mg->gc_werr_list;
	l_mg->gc_lists[1] = &l_mg->gc_high_list;
	l_mg->gc_lists[2] = &l_mg->gc_mid_list;
	l_mg->gc_lists[3] = &l_mg->gc_low_list;
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

	spin_lock_init(&l_mg->free_lock);
	spin_lock_init(&l_mg->close_lock);
	spin_lock_init(&l_mg->gc_lock);

	l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
	if (!l_mg->vsc_list)
		goto fail;

	l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
	if (!l_mg->bb_template)
		goto fail_free_vsc_list;

	l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
	if (!l_mg->bb_aux)
		goto fail_free_bb_template;
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953

	/* smeta is always small enough to fit on a kmalloc memory allocation,
	 * emeta depends on the number of LUNs allocated to the pblk instance
	 */
	for (i = 0; i < PBLK_DATA_LINES; i++) {
		l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
		if (!l_mg->sline_meta[i])
			goto fail_free_smeta;
	}

	/* emeta allocates three different buffers for managing metadata with
	 * in-memory and in-media layouts
	 */
	for (i = 0; i < PBLK_DATA_LINES; i++) {
		struct pblk_emeta *emeta;

		emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
		if (!emeta)
			goto fail_free_emeta;

		if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
			l_mg->emeta_alloc_type = PBLK_VMALLOC_META;

			emeta->buf = vmalloc(lm->emeta_len[0]);
			if (!emeta->buf) {
				kfree(emeta);
				goto fail_free_emeta;
			}

			emeta->nr_entries = lm->emeta_sec[0];
			l_mg->eline_meta[i] = emeta;
		} else {
			l_mg->emeta_alloc_type = PBLK_KMALLOC_META;

			emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
			if (!emeta->buf) {
				kfree(emeta);
				goto fail_free_emeta;
			}

			emeta->nr_entries = lm->emeta_sec[0];
			l_mg->eline_meta[i] = emeta;
		}
	}

	for (i = 0; i < l_mg->nr_lines; i++)
		l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);

954 955 956 957
	bb_distance = (geo->all_luns) * geo->ws_opt;
	for (i = 0; i < lm->sec_per_line; i += bb_distance)
		bitmap_set(l_mg->bb_template, i, geo->ws_opt);

958 959 960 961
	return 0;

fail_free_emeta:
	while (--i >= 0) {
962 963 964 965
		if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
			vfree(l_mg->eline_meta[i]->buf);
		else
			kfree(l_mg->eline_meta[i]->buf);
966
		kfree(l_mg->eline_meta[i]);
967 968 969
	}
fail_free_smeta:
	for (i = 0; i < PBLK_DATA_LINES; i++)
970
		kfree(l_mg->sline_meta[i]);
971 972 973 974 975 976
	kfree(l_mg->bb_aux);
fail_free_bb_template:
	kfree(l_mg->bb_template);
fail_free_vsc_list:
	kfree(l_mg->vsc_list);
fail:
977 978 979
	return -ENOMEM;
}

980
static int pblk_line_meta_init(struct pblk *pblk)
981 982 983 984 985
{
	struct nvm_tgt_dev *dev = pblk->dev;
	struct nvm_geo *geo = &dev->geo;
	struct pblk_line_meta *lm = &pblk->lm;
	unsigned int smeta_len, emeta_len;
986
	int i;
987

988
	lm->sec_per_line = geo->clba * geo->all_luns;
989 990
	lm->blk_per_line = geo->all_luns;
	lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
991
	lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
992
	lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
993 994
	lm->mid_thrs = lm->sec_per_line / 2;
	lm->high_thrs = lm->sec_per_line / 4;
995
	lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
996 997 998 999 1000 1001

	/* Calculate necessary pages for smeta. See comment over struct
	 * line_smeta definition
	 */
	i = 1;
add_smeta_page:
1002 1003
	lm->smeta_sec = i * geo->ws_opt;
	lm->smeta_len = lm->smeta_sec * geo->csecs;
1004

1005
	smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	if (smeta_len > lm->smeta_len) {
		i++;
		goto add_smeta_page;
	}

	/* Calculate necessary pages for emeta. See comment over struct
	 * line_emeta definition
	 */
	i = 1;
add_emeta_page:
1016 1017
	lm->emeta_sec[0] = i * geo->ws_opt;
	lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
1018

1019 1020
	emeta_len = calc_emeta_len(pblk);
	if (emeta_len > lm->emeta_len[0]) {
1021 1022 1023 1024
		i++;
		goto add_emeta_page;
	}

1025
	lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
1026 1027

	lm->min_blk_line = 1;
1028
	if (geo->all_luns > 1)
1029
		lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
1030
					lm->emeta_sec[0], geo->clba);
1031

1032
	if (lm->min_blk_line > lm->blk_per_line) {
1033
		pblk_err(pblk, "config. not supported. Min. LUN in line:%d\n",
1034
							lm->blk_per_line);
1035
		return -EINVAL;
1036
	}
1037

1038 1039 1040 1041 1042 1043 1044
	return 0;
}

static int pblk_lines_init(struct pblk *pblk)
{
	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
	struct pblk_line *line;
1045 1046
	void *chunk_meta;
	long nr_free_chks = 0;
1047 1048 1049
	int i, ret;

	ret = pblk_line_meta_init(pblk);
1050
	if (ret)
1051
		return ret;
1052

1053 1054 1055 1056 1057 1058
	ret = pblk_line_mg_init(pblk);
	if (ret)
		return ret;

	ret = pblk_luns_init(pblk);
	if (ret)
1059 1060
		goto fail_free_meta;

1061 1062 1063
	chunk_meta = pblk_chunk_get_meta(pblk);
	if (IS_ERR(chunk_meta)) {
		ret = PTR_ERR(chunk_meta);
1064
		goto fail_free_luns;
1065
	}
1066 1067 1068

	pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
								GFP_KERNEL);
1069 1070
	if (!pblk->lines) {
		ret = -ENOMEM;
1071
		goto fail_free_chunk_meta;
1072 1073
	}

1074 1075 1076
	for (i = 0; i < l_mg->nr_lines; i++) {
		line = &pblk->lines[i];

1077
		ret = pblk_alloc_line_meta(pblk, line);
1078
		if (ret)
1079
			goto fail_free_lines;
1080

1081
		nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
1082 1083
	}

1084
	if (!nr_free_chks) {
1085
		pblk_err(pblk, "too many bad blocks prevent for sane instance\n");
1086 1087 1088
		return -EINTR;
	}

1089
	pblk_set_provision(pblk, nr_free_chks);
1090

1091
	kfree(chunk_meta);
1092
	return 0;
1093

1094
fail_free_lines:
1095
	while (--i >= 0)
1096
		pblk_line_meta_free(l_mg, &pblk->lines[i]);
1097
	kfree(pblk->lines);
1098 1099
fail_free_chunk_meta:
	kfree(chunk_meta);
1100 1101
fail_free_luns:
	kfree(pblk->luns);
1102
fail_free_meta:
1103
	pblk_line_mg_free(pblk);
1104 1105 1106 1107 1108 1109 1110 1111

	return ret;
}

static int pblk_writer_init(struct pblk *pblk)
{
	pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
	if (IS_ERR(pblk->writer_ts)) {
1112 1113 1114
		int err = PTR_ERR(pblk->writer_ts);

		if (err != -EINTR)
1115
			pblk_err(pblk, "could not allocate writer kthread (%d)\n",
1116 1117
					err);
		return err;
1118 1119
	}

1120 1121 1122
	timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
	mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));

1123 1124 1125 1126 1127
	return 0;
}

static void pblk_writer_stop(struct pblk *pblk)
{
1128 1129 1130 1131 1132 1133 1134 1135 1136
	/* The pipeline must be stopped and the write buffer emptied before the
	 * write thread is stopped
	 */
	WARN(pblk_rb_read_count(&pblk->rwb),
			"Stopping not fully persisted write buffer\n");

	WARN(pblk_rb_sync_count(&pblk->rwb),
			"Stopping not fully synced write buffer\n");

1137
	del_timer_sync(&pblk->wtimer);
1138 1139 1140 1141 1142 1143 1144 1145
	if (pblk->writer_ts)
		kthread_stop(pblk->writer_ts);
}

static void pblk_free(struct pblk *pblk)
{
	pblk_lines_free(pblk);
	pblk_l2p_free(pblk);
1146 1147
	pblk_rwb_free(pblk);
	pblk_core_free(pblk);
1148 1149 1150 1151

	kfree(pblk);
}

1152
static void pblk_tear_down(struct pblk *pblk, bool graceful)
1153
{
1154 1155 1156
	if (graceful)
		__pblk_pipeline_flush(pblk);
	__pblk_pipeline_stop(pblk);
1157 1158 1159 1160
	pblk_writer_stop(pblk);
	pblk_rb_sync_l2p(&pblk->rwb);
	pblk_rl_free(&pblk->rl);

1161
	pblk_debug(pblk, "consistent tear down (graceful:%d)\n", graceful);
1162 1163
}

1164
static void pblk_exit(void *private, bool graceful)
1165 1166 1167 1168
{
	struct pblk *pblk = private;

	down_write(&pblk_lock);
1169 1170
	pblk_gc_exit(pblk, graceful);
	pblk_tear_down(pblk, graceful);
1171

1172
#ifdef CONFIG_NVM_PBLK_DEBUG
1173
	pblk_info(pblk, "exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
1174 1175
#endif

1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
	pblk_free(pblk);
	up_write(&pblk_lock);
}

static sector_t pblk_capacity(void *private)
{
	struct pblk *pblk = private;

	return pblk->capacity * NR_PHY_IN_LOG;
}

static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
		       int flags)
{
	struct nvm_geo *geo = &dev->geo;
	struct request_queue *bqueue = dev->q;
	struct request_queue *tqueue = tdisk->queue;
	struct pblk *pblk;
	int ret;

1196 1197 1198 1199 1200 1201 1202 1203 1204
	pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
	if (!pblk)
		return ERR_PTR(-ENOMEM);

	pblk->dev = dev;
	pblk->disk = tdisk;
	pblk->state = PBLK_STATE_RUNNING;
	pblk->gc.gc_enabled = 0;

1205 1206
	if (!(geo->version == NVM_OCSSD_SPEC_12 ||
					geo->version == NVM_OCSSD_SPEC_20)) {
1207
		pblk_err(pblk, "OCSSD version not supported (%u)\n",
1208
							geo->version);
1209
		kfree(pblk);
1210 1211 1212 1213
		return ERR_PTR(-EINVAL);
	}

	if (geo->version == NVM_OCSSD_SPEC_12 && geo->dom & NVM_RSP_L2P) {
1214
		pblk_err(pblk, "host-side L2P table not supported. (%x)\n",
1215
							geo->dom);
1216
		kfree(pblk);
1217 1218 1219
		return ERR_PTR(-EINVAL);
	}

1220
	spin_lock_init(&pblk->resubmit_lock);
1221 1222 1223
	spin_lock_init(&pblk->trans_lock);
	spin_lock_init(&pblk->lock);

1224
#ifdef CONFIG_NVM_PBLK_DEBUG
1225 1226 1227 1228 1229 1230 1231
	atomic_long_set(&pblk->inflight_writes, 0);
	atomic_long_set(&pblk->padded_writes, 0);
	atomic_long_set(&pblk->padded_wb, 0);
	atomic_long_set(&pblk->req_writes, 0);
	atomic_long_set(&pblk->sub_writes, 0);
	atomic_long_set(&pblk->sync_writes, 0);
	atomic_long_set(&pblk->inflight_reads, 0);
1232
	atomic_long_set(&pblk->cache_reads, 0);
1233 1234 1235 1236
	atomic_long_set(&pblk->sync_reads, 0);
	atomic_long_set(&pblk->recov_writes, 0);
	atomic_long_set(&pblk->recov_writes, 0);
	atomic_long_set(&pblk->recov_gc_writes, 0);
1237
	atomic_long_set(&pblk->recov_gc_reads, 0);
1238 1239 1240 1241 1242 1243 1244 1245 1246
#endif

	atomic_long_set(&pblk->read_failed, 0);
	atomic_long_set(&pblk->read_empty, 0);
	atomic_long_set(&pblk->read_high_ecc, 0);
	atomic_long_set(&pblk->read_failed_gc, 0);
	atomic_long_set(&pblk->write_failed, 0);
	atomic_long_set(&pblk->erase_failed, 0);

1247
	ret = pblk_core_init(pblk);
1248
	if (ret) {
1249
		pblk_err(pblk, "could not initialize core\n");
1250 1251 1252 1253 1254
		goto fail;
	}

	ret = pblk_lines_init(pblk);
	if (ret) {
1255
		pblk_err(pblk, "could not initialize lines\n");
1256
		goto fail_free_core;
1257 1258
	}

1259
	ret = pblk_rwb_init(pblk);
1260
	if (ret) {
1261
		pblk_err(pblk, "could not initialize write buffer\n");
1262
		goto fail_free_lines;
1263 1264
	}

1265
	ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1266
	if (ret) {
1267
		pblk_err(pblk, "could not initialize maps\n");
1268
		goto fail_free_rwb;
1269 1270 1271 1272
	}

	ret = pblk_writer_init(pblk);
	if (ret) {
1273
		if (ret != -EINTR)
1274
			pblk_err(pblk, "could not initialize write thread\n");
1275
		goto fail_free_l2p;
1276 1277 1278 1279
	}

	ret = pblk_gc_init(pblk);
	if (ret) {
1280
		pblk_err(pblk, "could not initialize gc\n");
1281 1282 1283 1284 1285 1286 1287 1288 1289
		goto fail_stop_writer;
	}

	/* inherit the size from the underlying device */
	blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
	blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));

	blk_queue_write_cache(tqueue, true, false);

1290
	tqueue->limits.discard_granularity = geo->clba * geo->csecs;
1291 1292
	tqueue->limits.discard_alignment = 0;
	blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
1293
	blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
1294

1295
	pblk_info(pblk, "luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
1296
			geo->all_luns, pblk->l_mg.nr_lines,
1297 1298 1299 1300
			(unsigned long long)pblk->rl.nr_secs,
			pblk->rwb.nr_entries);

	wake_up_process(pblk->writer_ts);
1301 1302 1303 1304

	/* Check if we need to start GC */
	pblk_gc_should_kick(pblk);

1305 1306 1307 1308 1309 1310
	return pblk;

fail_stop_writer:
	pblk_writer_stop(pblk);
fail_free_l2p:
	pblk_l2p_free(pblk);
1311 1312 1313 1314
fail_free_rwb:
	pblk_rwb_free(pblk);
fail_free_lines:
	pblk_lines_free(pblk);
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
fail_free_core:
	pblk_core_free(pblk);
fail:
	kfree(pblk);
	return ERR_PTR(ret);
}

/* physical block device target */
static struct nvm_tgt_type tt_pblk = {
	.name		= "pblk",
	.version	= {1, 0, 0},

	.make_rq	= pblk_make_rq,
	.capacity	= pblk_capacity,

	.init		= pblk_init,
	.exit		= pblk_exit,

	.sysfs_init	= pblk_sysfs_init,
	.sysfs_exit	= pblk_sysfs_exit,
1335
	.owner		= THIS_MODULE,
1336 1337 1338 1339
};

static int __init pblk_module_init(void)
{
1340 1341
	int ret;

1342 1343 1344
	ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
	if (ret)
		return ret;
1345 1346
	ret = nvm_register_tgt_type(&tt_pblk);
	if (ret)
1347
		bioset_exit(&pblk_bio_set);
1348
	return ret;
1349 1350 1351 1352
}

static void pblk_module_exit(void)
{
1353
	bioset_exit(&pblk_bio_set);
1354 1355 1356 1357 1358 1359 1360 1361 1362
	nvm_unregister_tgt_type(&tt_pblk);
}

module_init(pblk_module_init);
module_exit(pblk_module_exit);
MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");