Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   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"
  23#include "pblk-trace.h"
  24
  25static unsigned int write_buffer_size;
 
 
 
  26
  27module_param(write_buffer_size, uint, 0644);
  28MODULE_PARM_DESC(write_buffer_size, "number of entries in a write buffer");
  29
  30struct 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
  48struct bio_set pblk_bio_set;
 
  49
  50static blk_qc_t pblk_submit_bio(struct bio *bio)
  51{
  52	struct pblk *pblk = bio->bi_disk->queue->queuedata;
  53
  54	if (bio_op(bio) == REQ_OP_DISCARD) {
  55		pblk_discard(pblk, bio);
  56		if (!(bio->bi_opf & REQ_PREFLUSH)) {
  57			bio_endio(bio);
  58			return BLK_QC_T_NONE;
  59		}
  60	}
  61
  62	/* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
  63	 * constraint. Writes can be of arbitrary size.
  64	 */
  65	if (bio_data_dir(bio) == READ) {
  66		blk_queue_split(&bio);
  67		pblk_submit_read(pblk, bio);
  68	} else {
  69		/* Prevent deadlock in the case of a modest LUN configuration
  70		 * and large user I/Os. Unless stalled, the rate limiter
  71		 * leaves at least 256KB available for user I/O.
  72		 */
  73		if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
  74			blk_queue_split(&bio);
  75
  76		pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
  77	}
  78
  79	return BLK_QC_T_NONE;
  80}
  81
  82static const struct block_device_operations pblk_bops = {
  83	.owner		= THIS_MODULE,
  84	.submit_bio	= pblk_submit_bio,
  85};
  86
  87
  88static size_t pblk_trans_map_size(struct pblk *pblk)
  89{
  90	int entry_size = 8;
  91
  92	if (pblk->addrf_len < 32)
  93		entry_size = 4;
  94
  95	return entry_size * pblk->capacity;
  96}
  97
  98#ifdef CONFIG_NVM_PBLK_DEBUG
  99static u32 pblk_l2p_crc(struct pblk *pblk)
 100{
 101	size_t map_size;
 102	u32 crc = ~(u32)0;
 103
 104	map_size = pblk_trans_map_size(pblk);
 105	crc = crc32_le(crc, pblk->trans_map, map_size);
 106	return crc;
 107}
 108#endif
 109
 110static void pblk_l2p_free(struct pblk *pblk)
 111{
 112	vfree(pblk->trans_map);
 113}
 114
 115static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
 116{
 117	struct pblk_line *line = NULL;
 118
 119	if (factory_init) {
 120		guid_gen(&pblk->instance_uuid);
 121	} else {
 122		line = pblk_recov_l2p(pblk);
 123		if (IS_ERR(line)) {
 124			pblk_err(pblk, "could not recover l2p table\n");
 125			return -EFAULT;
 126		}
 127	}
 128
 129#ifdef CONFIG_NVM_PBLK_DEBUG
 130	pblk_info(pblk, "init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
 131#endif
 132
 133	/* Free full lines directly as GC has not been started yet */
 134	pblk_gc_free_full_lines(pblk);
 135
 136	if (!line) {
 137		/* Configure next line for user data */
 138		line = pblk_line_get_first_data(pblk);
 139		if (!line)
 
 140			return -EFAULT;
 
 141	}
 142
 143	return 0;
 144}
 145
 146static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
 147{
 148	sector_t i;
 149	struct ppa_addr ppa;
 150	size_t map_size;
 151	int ret = 0;
 152
 153	map_size = pblk_trans_map_size(pblk);
 154	pblk->trans_map = __vmalloc(map_size, GFP_KERNEL | __GFP_NOWARN |
 155				    __GFP_RETRY_MAYFAIL | __GFP_HIGHMEM);
 156	if (!pblk->trans_map) {
 157		pblk_err(pblk, "failed to allocate L2P (need %zu of memory)\n",
 158				map_size);
 159		return -ENOMEM;
 160	}
 161
 162	pblk_ppa_set_empty(&ppa);
 163
 164	for (i = 0; i < pblk->capacity; i++)
 165		pblk_trans_map_set(pblk, i, ppa);
 166
 167	ret = pblk_l2p_recover(pblk, factory_init);
 168	if (ret)
 169		vfree(pblk->trans_map);
 170
 171	return ret;
 172}
 173
 174static void pblk_rwb_free(struct pblk *pblk)
 175{
 176	if (pblk_rb_tear_down_check(&pblk->rwb))
 177		pblk_err(pblk, "write buffer error on tear down\n");
 178
 179	pblk_rb_free(&pblk->rwb);
 
 180}
 181
 182static int pblk_rwb_init(struct pblk *pblk)
 183{
 184	struct nvm_tgt_dev *dev = pblk->dev;
 185	struct nvm_geo *geo = &dev->geo;
 186	unsigned long buffer_size;
 187	int pgs_in_buffer, threshold;
 
 188
 189	threshold = geo->mw_cunits * geo->all_luns;
 190	pgs_in_buffer = (max(geo->mw_cunits, geo->ws_opt) + geo->ws_opt)
 191								* geo->all_luns;
 192
 193	if (write_buffer_size && (write_buffer_size > pgs_in_buffer))
 194		buffer_size = write_buffer_size;
 195	else
 196		buffer_size = pgs_in_buffer;
 197
 198	return pblk_rb_init(&pblk->rwb, buffer_size, threshold, geo->csecs);
 
 
 
 199}
 200
 201static int pblk_set_addrf_12(struct pblk *pblk, struct nvm_geo *geo,
 202			     struct nvm_addrf_12 *dst)
 
 
 203{
 204	struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
 205	int power_len;
 206
 207	/* Re-calculate channel and lun format to adapt to configuration */
 208	power_len = get_count_order(geo->num_ch);
 209	if (1 << power_len != geo->num_ch) {
 210		pblk_err(pblk, "supports only power-of-two channel config.\n");
 211		return -EINVAL;
 212	}
 213	dst->ch_len = power_len;
 214
 215	power_len = get_count_order(geo->num_lun);
 216	if (1 << power_len != geo->num_lun) {
 217		pblk_err(pblk, "supports only power-of-two LUN config.\n");
 218		return -EINVAL;
 219	}
 220	dst->lun_len = power_len;
 221
 222	dst->blk_len = src->blk_len;
 223	dst->pg_len = src->pg_len;
 224	dst->pln_len = src->pln_len;
 225	dst->sec_len = src->sec_len;
 226
 227	dst->sec_offset = 0;
 228	dst->pln_offset = dst->sec_len;
 229	dst->ch_offset = dst->pln_offset + dst->pln_len;
 230	dst->lun_offset = dst->ch_offset + dst->ch_len;
 231	dst->pg_offset = dst->lun_offset + dst->lun_len;
 232	dst->blk_offset = dst->pg_offset + dst->pg_len;
 233
 234	dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
 235	dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
 236	dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
 237	dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
 238	dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
 239	dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
 240
 241	return dst->blk_offset + src->blk_len;
 242}
 243
 244static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
 245			     struct pblk_addrf *udst)
 246{
 247	struct nvm_addrf *src = &geo->addrf;
 248
 249	adst->ch_len = get_count_order(geo->num_ch);
 250	adst->lun_len = get_count_order(geo->num_lun);
 251	adst->chk_len = src->chk_len;
 252	adst->sec_len = src->sec_len;
 253
 254	adst->sec_offset = 0;
 255	adst->ch_offset = adst->sec_len;
 256	adst->lun_offset = adst->ch_offset + adst->ch_len;
 257	adst->chk_offset = adst->lun_offset + adst->lun_len;
 258
 259	adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
 260	adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
 261	adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
 262	adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
 263
 264	udst->sec_stripe = geo->ws_opt;
 265	udst->ch_stripe = geo->num_ch;
 266	udst->lun_stripe = geo->num_lun;
 267
 268	udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
 269	udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
 270
 271	return adst->chk_offset + adst->chk_len;
 272}
 273
 274static int pblk_set_addrf(struct pblk *pblk)
 275{
 276	struct nvm_tgt_dev *dev = pblk->dev;
 277	struct nvm_geo *geo = &dev->geo;
 278	int mod;
 279
 280	switch (geo->version) {
 281	case NVM_OCSSD_SPEC_12:
 282		div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
 283		if (mod) {
 284			pblk_err(pblk, "bad configuration of sectors/pages\n");
 285			return -EINVAL;
 286		}
 287
 288		pblk->addrf_len = pblk_set_addrf_12(pblk, geo,
 289							(void *)&pblk->addrf);
 290		break;
 291	case NVM_OCSSD_SPEC_20:
 292		pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
 293							&pblk->uaddrf);
 294		break;
 295	default:
 296		pblk_err(pblk, "OCSSD revision not supported (%d)\n",
 297								geo->version);
 298		return -EINVAL;
 299	}
 300
 301	return 0;
 302}
 303
 304static int pblk_create_global_caches(void)
 305{
 306
 307	pblk_caches.ws = kmem_cache_create("pblk_blk_ws",
 308				sizeof(struct pblk_line_ws), 0, 0, NULL);
 309	if (!pblk_caches.ws)
 
 310		return -ENOMEM;
 
 311
 312	pblk_caches.rec = kmem_cache_create("pblk_rec",
 313				sizeof(struct pblk_rec_ctx), 0, 0, NULL);
 314	if (!pblk_caches.rec)
 315		goto fail_destroy_ws;
 
 
 
 316
 317	pblk_caches.g_rq = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
 318				0, 0, NULL);
 319	if (!pblk_caches.g_rq)
 320		goto fail_destroy_rec;
 
 
 
 
 321
 322	pblk_caches.w_rq = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
 323				0, 0, NULL);
 324	if (!pblk_caches.w_rq)
 325		goto fail_destroy_g_rq;
 
 
 
 
 
 
 326
 327	return 0;
 328
 329fail_destroy_g_rq:
 330	kmem_cache_destroy(pblk_caches.g_rq);
 331fail_destroy_rec:
 332	kmem_cache_destroy(pblk_caches.rec);
 333fail_destroy_ws:
 334	kmem_cache_destroy(pblk_caches.ws);
 335
 336	return -ENOMEM;
 337}
 338
 339static int pblk_get_global_caches(void)
 340{
 341	int ret = 0;
 342
 343	mutex_lock(&pblk_caches.mutex);
 344
 345	if (kref_get_unless_zero(&pblk_caches.kref))
 346		goto out;
 347
 348	ret = pblk_create_global_caches();
 349	if (!ret)
 350		kref_init(&pblk_caches.kref);
 351
 352out:
 353	mutex_unlock(&pblk_caches.mutex);
 354	return ret;
 355}
 356
 357static void pblk_destroy_global_caches(struct kref *ref)
 358{
 359	struct pblk_global_caches *c;
 360
 361	c = container_of(ref, struct pblk_global_caches, kref);
 362
 363	kmem_cache_destroy(c->ws);
 364	kmem_cache_destroy(c->rec);
 365	kmem_cache_destroy(c->g_rq);
 366	kmem_cache_destroy(c->w_rq);
 367}
 368
 369static void pblk_put_global_caches(void)
 370{
 371	mutex_lock(&pblk_caches.mutex);
 372	kref_put(&pblk_caches.kref, pblk_destroy_global_caches);
 373	mutex_unlock(&pblk_caches.mutex);
 374}
 375
 376static int pblk_core_init(struct pblk *pblk)
 377{
 378	struct nvm_tgt_dev *dev = pblk->dev;
 379	struct nvm_geo *geo = &dev->geo;
 380	int ret, max_write_ppas;
 381
 382	atomic64_set(&pblk->user_wa, 0);
 383	atomic64_set(&pblk->pad_wa, 0);
 384	atomic64_set(&pblk->gc_wa, 0);
 385	pblk->user_rst_wa = 0;
 386	pblk->pad_rst_wa = 0;
 387	pblk->gc_rst_wa = 0;
 388
 389	atomic64_set(&pblk->nr_flush, 0);
 390	pblk->nr_flush_rst = 0;
 391
 392	pblk->min_write_pgs = geo->ws_opt;
 393	pblk->min_write_pgs_data = pblk->min_write_pgs;
 
 394	max_write_ppas = pblk->min_write_pgs * geo->all_luns;
 395	pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
 396	pblk->max_write_pgs = min_t(int, pblk->max_write_pgs,
 397		queue_max_hw_sectors(dev->q) / (geo->csecs >> SECTOR_SHIFT));
 398	pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
 399
 400	pblk->oob_meta_size = geo->sos;
 401	if (!pblk_is_oob_meta_supported(pblk)) {
 402		/* For drives which does not have OOB metadata feature
 403		 * in order to support recovery feature we need to use
 404		 * so called packed metadata. Packed metada will store
 405		 * the same information as OOB metadata (l2p table mapping,
 406		 * but in the form of the single page at the end of
 407		 * every write request.
 408		 */
 409		if (pblk->min_write_pgs
 410			* sizeof(struct pblk_sec_meta) > PAGE_SIZE) {
 411			/* We want to keep all the packed metadata on single
 412			 * page per write requests. So we need to ensure that
 413			 * it will fit.
 414			 *
 415			 * This is more like sanity check, since there is
 416			 * no device with such a big minimal write size
 417			 * (above 1 metabytes).
 418			 */
 419			pblk_err(pblk, "Not supported min write size\n");
 420			return -EINVAL;
 421		}
 422		/* For packed meta approach we do some simplification.
 423		 * On read path we always issue requests which size
 424		 * equal to max_write_pgs, with all pages filled with
 425		 * user payload except of last one page which will be
 426		 * filled with packed metadata.
 427		 */
 428		pblk->max_write_pgs = pblk->min_write_pgs;
 429		pblk->min_write_pgs_data = pblk->min_write_pgs - 1;
 430	}
 431
 432	pblk->pad_dist = kcalloc(pblk->min_write_pgs - 1, sizeof(atomic64_t),
 433								GFP_KERNEL);
 434	if (!pblk->pad_dist)
 435		return -ENOMEM;
 436
 437	if (pblk_get_global_caches())
 438		goto fail_free_pad_dist;
 439
 440	/* Internal bios can be at most the sectors signaled by the device. */
 441	ret = mempool_init_page_pool(&pblk->page_bio_pool, NVM_MAX_VLBA, 0);
 442	if (ret)
 443		goto free_global_caches;
 444
 445	ret = mempool_init_slab_pool(&pblk->gen_ws_pool, PBLK_GEN_WS_POOL_SIZE,
 446				     pblk_caches.ws);
 447	if (ret)
 448		goto free_page_bio_pool;
 449
 450	ret = mempool_init_slab_pool(&pblk->rec_pool, geo->all_luns,
 451				     pblk_caches.rec);
 452	if (ret)
 453		goto free_gen_ws_pool;
 454
 455	ret = mempool_init_slab_pool(&pblk->r_rq_pool, geo->all_luns,
 456				     pblk_caches.g_rq);
 457	if (ret)
 458		goto free_rec_pool;
 459
 460	ret = mempool_init_slab_pool(&pblk->e_rq_pool, geo->all_luns,
 461				     pblk_caches.g_rq);
 462	if (ret)
 463		goto free_r_rq_pool;
 464
 465	ret = mempool_init_slab_pool(&pblk->w_rq_pool, geo->all_luns,
 466				     pblk_caches.w_rq);
 467	if (ret)
 468		goto free_e_rq_pool;
 469
 470	pblk->close_wq = alloc_workqueue("pblk-close-wq",
 471			WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
 472	if (!pblk->close_wq)
 473		goto free_w_rq_pool;
 474
 475	pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
 476			WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
 477	if (!pblk->bb_wq)
 478		goto free_close_wq;
 479
 480	pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
 481			WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
 482	if (!pblk->r_end_wq)
 483		goto free_bb_wq;
 484
 485	if (pblk_set_addrf(pblk))
 486		goto free_r_end_wq;
 487
 488	INIT_LIST_HEAD(&pblk->compl_list);
 489	INIT_LIST_HEAD(&pblk->resubmit_list);
 490
 491	return 0;
 492
 493free_r_end_wq:
 494	destroy_workqueue(pblk->r_end_wq);
 495free_bb_wq:
 496	destroy_workqueue(pblk->bb_wq);
 497free_close_wq:
 498	destroy_workqueue(pblk->close_wq);
 499free_w_rq_pool:
 500	mempool_exit(&pblk->w_rq_pool);
 501free_e_rq_pool:
 502	mempool_exit(&pblk->e_rq_pool);
 503free_r_rq_pool:
 504	mempool_exit(&pblk->r_rq_pool);
 505free_rec_pool:
 506	mempool_exit(&pblk->rec_pool);
 507free_gen_ws_pool:
 508	mempool_exit(&pblk->gen_ws_pool);
 509free_page_bio_pool:
 510	mempool_exit(&pblk->page_bio_pool);
 511free_global_caches:
 512	pblk_put_global_caches();
 513fail_free_pad_dist:
 514	kfree(pblk->pad_dist);
 515	return -ENOMEM;
 516}
 517
 518static void pblk_core_free(struct pblk *pblk)
 519{
 520	if (pblk->close_wq)
 521		destroy_workqueue(pblk->close_wq);
 522
 523	if (pblk->r_end_wq)
 524		destroy_workqueue(pblk->r_end_wq);
 525
 526	if (pblk->bb_wq)
 527		destroy_workqueue(pblk->bb_wq);
 528
 529	mempool_exit(&pblk->page_bio_pool);
 530	mempool_exit(&pblk->gen_ws_pool);
 531	mempool_exit(&pblk->rec_pool);
 532	mempool_exit(&pblk->r_rq_pool);
 533	mempool_exit(&pblk->e_rq_pool);
 534	mempool_exit(&pblk->w_rq_pool);
 535
 536	pblk_put_global_caches();
 537	kfree(pblk->pad_dist);
 538}
 539
 540static void pblk_line_mg_free(struct pblk *pblk)
 541{
 542	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 543	int i;
 544
 545	kfree(l_mg->bb_template);
 546	kfree(l_mg->bb_aux);
 547	kfree(l_mg->vsc_list);
 548
 549	for (i = 0; i < PBLK_DATA_LINES; i++) {
 550		kfree(l_mg->sline_meta[i]);
 551		kvfree(l_mg->eline_meta[i]->buf);
 552		kfree(l_mg->eline_meta[i]);
 553	}
 554
 555	mempool_destroy(l_mg->bitmap_pool);
 556	kmem_cache_destroy(l_mg->bitmap_cache);
 557}
 558
 559static void pblk_line_meta_free(struct pblk_line_mgmt *l_mg,
 560				struct pblk_line *line)
 561{
 562	struct pblk_w_err_gc *w_err_gc = line->w_err_gc;
 563
 564	kfree(line->blk_bitmap);
 565	kfree(line->erase_bitmap);
 566	kfree(line->chks);
 567
 568	kvfree(w_err_gc->lba_list);
 569	kfree(w_err_gc);
 570}
 571
 572static void pblk_lines_free(struct pblk *pblk)
 573{
 574	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 575	struct pblk_line *line;
 576	int i;
 577
 
 578	for (i = 0; i < l_mg->nr_lines; i++) {
 579		line = &pblk->lines[i];
 580
 581		pblk_line_free(line);
 582		pblk_line_meta_free(l_mg, line);
 583	}
 
 584
 585	pblk_line_mg_free(pblk);
 586
 587	kfree(pblk->luns);
 588	kfree(pblk->lines);
 589}
 590
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 591static int pblk_luns_init(struct pblk *pblk)
 592{
 593	struct nvm_tgt_dev *dev = pblk->dev;
 594	struct nvm_geo *geo = &dev->geo;
 595	struct pblk_lun *rlun;
 596	int i;
 597
 598	/* TODO: Implement unbalanced LUN support */
 599	if (geo->num_lun < 0) {
 600		pblk_err(pblk, "unbalanced LUN config.\n");
 601		return -EINVAL;
 602	}
 603
 604	pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
 605								GFP_KERNEL);
 606	if (!pblk->luns)
 607		return -ENOMEM;
 608
 609	for (i = 0; i < geo->all_luns; i++) {
 610		/* Stripe across channels */
 611		int ch = i % geo->num_ch;
 612		int lun_raw = i / geo->num_ch;
 613		int lunid = lun_raw + ch * geo->num_lun;
 614
 615		rlun = &pblk->luns[i];
 616		rlun->bppa = dev->luns[lunid];
 617
 618		sema_init(&rlun->wr_sem, 1);
 619	}
 620
 621	return 0;
 622}
 623
 624/* See comment over struct line_emeta definition */
 625static unsigned int calc_emeta_len(struct pblk *pblk)
 626{
 627	struct pblk_line_meta *lm = &pblk->lm;
 628	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 629	struct nvm_tgt_dev *dev = pblk->dev;
 630	struct nvm_geo *geo = &dev->geo;
 631
 632	/* Round to sector size so that lba_list starts on its own sector */
 633	lm->emeta_sec[1] = DIV_ROUND_UP(
 634			sizeof(struct line_emeta) + lm->blk_bitmap_len +
 635			sizeof(struct wa_counters), geo->csecs);
 636	lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
 637
 638	/* Round to sector size so that vsc_list starts on its own sector */
 639	lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
 640	lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
 641			geo->csecs);
 642	lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
 643
 644	lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
 645			geo->csecs);
 646	lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
 647
 648	lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
 649
 650	return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
 651}
 652
 653static int pblk_set_provision(struct pblk *pblk, int nr_free_chks)
 654{
 655	struct nvm_tgt_dev *dev = pblk->dev;
 656	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 657	struct pblk_line_meta *lm = &pblk->lm;
 658	struct nvm_geo *geo = &dev->geo;
 659	sector_t provisioned;
 660	int sec_meta, blk_meta, clba;
 661	int minimum;
 662
 663	if (geo->op == NVM_TARGET_DEFAULT_OP)
 664		pblk->op = PBLK_DEFAULT_OP;
 665	else
 666		pblk->op = geo->op;
 667
 668	minimum = pblk_get_min_chks(pblk);
 669	provisioned = nr_free_chks;
 670	provisioned *= (100 - pblk->op);
 671	sector_div(provisioned, 100);
 672
 673	if ((nr_free_chks - provisioned) < minimum) {
 674		if (geo->op != NVM_TARGET_DEFAULT_OP) {
 675			pblk_err(pblk, "OP too small to create a sane instance\n");
 676			return -EINTR;
 677		}
 678
 679		/* If the user did not specify an OP value, and PBLK_DEFAULT_OP
 680		 * is not enough, calculate and set sane value
 681		 */
 682
 683		provisioned = nr_free_chks - minimum;
 684		pblk->op =  (100 * minimum) / nr_free_chks;
 685		pblk_info(pblk, "Default OP insufficient, adjusting OP to %d\n",
 686				pblk->op);
 687	}
 688
 689	pblk->op_blks = nr_free_chks - provisioned;
 690
 691	/* Internally pblk manages all free blocks, but all calculations based
 692	 * on user capacity consider only provisioned blocks
 693	 */
 694	pblk->rl.total_blocks = nr_free_chks;
 
 695
 696	/* Consider sectors used for metadata */
 697	sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
 698	blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
 699
 700	clba = (geo->clba / pblk->min_write_pgs) * pblk->min_write_pgs_data;
 701	pblk->capacity = (provisioned - blk_meta) * clba;
 702
 703	atomic_set(&pblk->rl.free_blocks, nr_free_chks);
 704	atomic_set(&pblk->rl.free_user_blocks, nr_free_chks);
 
 705
 706	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 707}
 708
 709static int pblk_setup_line_meta_chk(struct pblk *pblk, struct pblk_line *line,
 710				   struct nvm_chk_meta *meta)
 711{
 712	struct nvm_tgt_dev *dev = pblk->dev;
 713	struct nvm_geo *geo = &dev->geo;
 714	struct pblk_line_meta *lm = &pblk->lm;
 715	int i, nr_bad_chks = 0;
 716
 717	for (i = 0; i < lm->blk_per_line; i++) {
 718		struct pblk_lun *rlun = &pblk->luns[i];
 719		struct nvm_chk_meta *chunk;
 720		struct nvm_chk_meta *chunk_meta;
 721		struct ppa_addr ppa;
 722		int pos;
 723
 724		ppa = rlun->bppa;
 725		pos = pblk_ppa_to_pos(geo, ppa);
 726		chunk = &line->chks[pos];
 727
 728		ppa.m.chk = line->id;
 729		chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
 730
 731		chunk->state = chunk_meta->state;
 732		chunk->type = chunk_meta->type;
 733		chunk->wi = chunk_meta->wi;
 734		chunk->slba = chunk_meta->slba;
 735		chunk->cnlb = chunk_meta->cnlb;
 736		chunk->wp = chunk_meta->wp;
 737
 738		trace_pblk_chunk_state(pblk_disk_name(pblk), &ppa,
 739					chunk->state);
 740
 741		if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
 742			WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
 743			continue;
 744		}
 745
 746		if (!(chunk->state & NVM_CHK_ST_OFFLINE))
 747			continue;
 748
 749		set_bit(pos, line->blk_bitmap);
 750		nr_bad_chks++;
 751	}
 752
 753	return nr_bad_chks;
 754}
 755
 756static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
 757				 void *chunk_meta, int line_id)
 758{
 
 
 759	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 760	struct pblk_line_meta *lm = &pblk->lm;
 761	long nr_bad_chks, chk_in_line;
 762
 763	line->pblk = pblk;
 764	line->id = line_id;
 765	line->type = PBLK_LINETYPE_FREE;
 766	line->state = PBLK_LINESTATE_NEW;
 767	line->gc_group = PBLK_LINEGC_NONE;
 768	line->vsc = &l_mg->vsc_list[line_id];
 769	spin_lock_init(&line->lock);
 770
 771	nr_bad_chks = pblk_setup_line_meta_chk(pblk, line, chunk_meta);
 
 
 
 772
 773	chk_in_line = lm->blk_per_line - nr_bad_chks;
 774	if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
 775					chk_in_line < lm->min_blk_line) {
 776		line->state = PBLK_LINESTATE_BAD;
 777		list_add_tail(&line->list, &l_mg->bad_list);
 778		return 0;
 779	}
 780
 781	atomic_set(&line->blk_in_line, chk_in_line);
 782	list_add_tail(&line->list, &l_mg->free_list);
 783	l_mg->nr_free_lines++;
 784
 785	return chk_in_line;
 786}
 787
 788static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
 789{
 790	struct pblk_line_meta *lm = &pblk->lm;
 791
 792	line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
 793	if (!line->blk_bitmap)
 794		return -ENOMEM;
 795
 796	line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
 797	if (!line->erase_bitmap)
 798		goto free_blk_bitmap;
 799
 
 800
 801	line->chks = kmalloc_array(lm->blk_per_line,
 802				   sizeof(struct nvm_chk_meta), GFP_KERNEL);
 803	if (!line->chks)
 804		goto free_erase_bitmap;
 805
 806	line->w_err_gc = kzalloc(sizeof(struct pblk_w_err_gc), GFP_KERNEL);
 807	if (!line->w_err_gc)
 808		goto free_chks;
 809
 810	return 0;
 811
 812free_chks:
 813	kfree(line->chks);
 814free_erase_bitmap:
 815	kfree(line->erase_bitmap);
 816free_blk_bitmap:
 817	kfree(line->blk_bitmap);
 818	return -ENOMEM;
 819}
 820
 821static int pblk_line_mg_init(struct pblk *pblk)
 822{
 823	struct nvm_tgt_dev *dev = pblk->dev;
 824	struct nvm_geo *geo = &dev->geo;
 825	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 826	struct pblk_line_meta *lm = &pblk->lm;
 827	int i, bb_distance;
 828
 829	l_mg->nr_lines = geo->num_chk;
 830	l_mg->log_line = l_mg->data_line = NULL;
 831	l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
 832	l_mg->nr_free_lines = 0;
 833	bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
 834
 835	INIT_LIST_HEAD(&l_mg->free_list);
 836	INIT_LIST_HEAD(&l_mg->corrupt_list);
 837	INIT_LIST_HEAD(&l_mg->bad_list);
 838	INIT_LIST_HEAD(&l_mg->gc_full_list);
 839	INIT_LIST_HEAD(&l_mg->gc_high_list);
 840	INIT_LIST_HEAD(&l_mg->gc_mid_list);
 841	INIT_LIST_HEAD(&l_mg->gc_low_list);
 842	INIT_LIST_HEAD(&l_mg->gc_empty_list);
 843	INIT_LIST_HEAD(&l_mg->gc_werr_list);
 844
 845	INIT_LIST_HEAD(&l_mg->emeta_list);
 846
 847	l_mg->gc_lists[0] = &l_mg->gc_werr_list;
 848	l_mg->gc_lists[1] = &l_mg->gc_high_list;
 849	l_mg->gc_lists[2] = &l_mg->gc_mid_list;
 850	l_mg->gc_lists[3] = &l_mg->gc_low_list;
 851
 852	spin_lock_init(&l_mg->free_lock);
 853	spin_lock_init(&l_mg->close_lock);
 854	spin_lock_init(&l_mg->gc_lock);
 855
 856	l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
 857	if (!l_mg->vsc_list)
 858		goto fail;
 859
 860	l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
 861	if (!l_mg->bb_template)
 862		goto fail_free_vsc_list;
 863
 864	l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
 865	if (!l_mg->bb_aux)
 866		goto fail_free_bb_template;
 867
 868	/* smeta is always small enough to fit on a kmalloc memory allocation,
 869	 * emeta depends on the number of LUNs allocated to the pblk instance
 870	 */
 871	for (i = 0; i < PBLK_DATA_LINES; i++) {
 872		l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
 873		if (!l_mg->sline_meta[i])
 874			goto fail_free_smeta;
 875	}
 876
 877	l_mg->bitmap_cache = kmem_cache_create("pblk_lm_bitmap",
 878			lm->sec_bitmap_len, 0, 0, NULL);
 879	if (!l_mg->bitmap_cache)
 880		goto fail_free_smeta;
 881
 882	/* the bitmap pool is used for both valid and map bitmaps */
 883	l_mg->bitmap_pool = mempool_create_slab_pool(PBLK_DATA_LINES * 2,
 884				l_mg->bitmap_cache);
 885	if (!l_mg->bitmap_pool)
 886		goto fail_destroy_bitmap_cache;
 887
 888	/* emeta allocates three different buffers for managing metadata with
 889	 * in-memory and in-media layouts
 890	 */
 891	for (i = 0; i < PBLK_DATA_LINES; i++) {
 892		struct pblk_emeta *emeta;
 893
 894		emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
 895		if (!emeta)
 896			goto fail_free_emeta;
 897
 898		emeta->buf = kvmalloc(lm->emeta_len[0], GFP_KERNEL);
 899		if (!emeta->buf) {
 900			kfree(emeta);
 901			goto fail_free_emeta;
 902		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 903
 904		emeta->nr_entries = lm->emeta_sec[0];
 905		l_mg->eline_meta[i] = emeta;
 
 906	}
 907
 908	for (i = 0; i < l_mg->nr_lines; i++)
 909		l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
 910
 911	bb_distance = (geo->all_luns) * geo->ws_opt;
 912	for (i = 0; i < lm->sec_per_line; i += bb_distance)
 913		bitmap_set(l_mg->bb_template, i, geo->ws_opt);
 914
 915	return 0;
 916
 917fail_free_emeta:
 918	while (--i >= 0) {
 919		kvfree(l_mg->eline_meta[i]->buf);
 
 
 
 920		kfree(l_mg->eline_meta[i]);
 921	}
 922
 923	mempool_destroy(l_mg->bitmap_pool);
 924fail_destroy_bitmap_cache:
 925	kmem_cache_destroy(l_mg->bitmap_cache);
 926fail_free_smeta:
 927	for (i = 0; i < PBLK_DATA_LINES; i++)
 928		kfree(l_mg->sline_meta[i]);
 929	kfree(l_mg->bb_aux);
 930fail_free_bb_template:
 931	kfree(l_mg->bb_template);
 932fail_free_vsc_list:
 933	kfree(l_mg->vsc_list);
 934fail:
 935	return -ENOMEM;
 936}
 937
 938static int pblk_line_meta_init(struct pblk *pblk)
 939{
 940	struct nvm_tgt_dev *dev = pblk->dev;
 941	struct nvm_geo *geo = &dev->geo;
 942	struct pblk_line_meta *lm = &pblk->lm;
 943	unsigned int smeta_len, emeta_len;
 944	int i;
 945
 946	lm->sec_per_line = geo->clba * geo->all_luns;
 947	lm->blk_per_line = geo->all_luns;
 948	lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
 949	lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
 950	lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
 951	lm->mid_thrs = lm->sec_per_line / 2;
 952	lm->high_thrs = lm->sec_per_line / 4;
 953	lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
 954
 955	/* Calculate necessary pages for smeta. See comment over struct
 956	 * line_smeta definition
 957	 */
 958	i = 1;
 959add_smeta_page:
 960	lm->smeta_sec = i * geo->ws_opt;
 961	lm->smeta_len = lm->smeta_sec * geo->csecs;
 962
 963	smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
 964	if (smeta_len > lm->smeta_len) {
 965		i++;
 966		goto add_smeta_page;
 967	}
 968
 969	/* Calculate necessary pages for emeta. See comment over struct
 970	 * line_emeta definition
 971	 */
 972	i = 1;
 973add_emeta_page:
 974	lm->emeta_sec[0] = i * geo->ws_opt;
 975	lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
 976
 977	emeta_len = calc_emeta_len(pblk);
 978	if (emeta_len > lm->emeta_len[0]) {
 979		i++;
 980		goto add_emeta_page;
 981	}
 982
 983	lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
 984
 985	lm->min_blk_line = 1;
 986	if (geo->all_luns > 1)
 987		lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
 988					lm->emeta_sec[0], geo->clba);
 989
 990	if (lm->min_blk_line > lm->blk_per_line) {
 991		pblk_err(pblk, "config. not supported. Min. LUN in line:%d\n",
 992							lm->blk_per_line);
 993		return -EINVAL;
 994	}
 995
 996	return 0;
 997}
 998
 999static int pblk_lines_init(struct pblk *pblk)
1000{
1001	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1002	struct pblk_line *line;
1003	void *chunk_meta;
1004	int nr_free_chks = 0;
1005	int i, ret;
1006
1007	ret = pblk_line_meta_init(pblk);
1008	if (ret)
1009		return ret;
1010
1011	ret = pblk_line_mg_init(pblk);
1012	if (ret)
1013		return ret;
1014
1015	ret = pblk_luns_init(pblk);
1016	if (ret)
1017		goto fail_free_meta;
1018
1019	chunk_meta = pblk_get_chunk_meta(pblk);
1020	if (IS_ERR(chunk_meta)) {
1021		ret = PTR_ERR(chunk_meta);
1022		goto fail_free_luns;
1023	}
1024
1025	pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
1026								GFP_KERNEL);
1027	if (!pblk->lines) {
1028		ret = -ENOMEM;
1029		goto fail_free_chunk_meta;
1030	}
1031
1032	for (i = 0; i < l_mg->nr_lines; i++) {
1033		line = &pblk->lines[i];
1034
1035		ret = pblk_alloc_line_meta(pblk, line);
1036		if (ret)
1037			goto fail_free_lines;
1038
1039		nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
1040
1041		trace_pblk_line_state(pblk_disk_name(pblk), line->id,
1042								line->state);
1043	}
1044
1045	if (!nr_free_chks) {
1046		pblk_err(pblk, "too many bad blocks prevent for sane instance\n");
1047		ret = -EINTR;
1048		goto fail_free_lines;
1049	}
1050
1051	ret = pblk_set_provision(pblk, nr_free_chks);
1052	if (ret)
1053		goto fail_free_lines;
1054
1055	vfree(chunk_meta);
1056	return 0;
1057
1058fail_free_lines:
1059	while (--i >= 0)
1060		pblk_line_meta_free(l_mg, &pblk->lines[i]);
1061	kfree(pblk->lines);
1062fail_free_chunk_meta:
1063	vfree(chunk_meta);
1064fail_free_luns:
1065	kfree(pblk->luns);
1066fail_free_meta:
1067	pblk_line_mg_free(pblk);
1068
1069	return ret;
1070}
1071
1072static int pblk_writer_init(struct pblk *pblk)
1073{
1074	pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
1075	if (IS_ERR(pblk->writer_ts)) {
1076		int err = PTR_ERR(pblk->writer_ts);
1077
1078		if (err != -EINTR)
1079			pblk_err(pblk, "could not allocate writer kthread (%d)\n",
1080					err);
1081		return err;
1082	}
1083
1084	timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1085	mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1086
1087	return 0;
1088}
1089
1090static void pblk_writer_stop(struct pblk *pblk)
1091{
1092	/* The pipeline must be stopped and the write buffer emptied before the
1093	 * write thread is stopped
1094	 */
1095	WARN(pblk_rb_read_count(&pblk->rwb),
1096			"Stopping not fully persisted write buffer\n");
1097
1098	WARN(pblk_rb_sync_count(&pblk->rwb),
1099			"Stopping not fully synced write buffer\n");
1100
1101	del_timer_sync(&pblk->wtimer);
1102	if (pblk->writer_ts)
1103		kthread_stop(pblk->writer_ts);
1104}
1105
1106static void pblk_free(struct pblk *pblk)
1107{
1108	pblk_lines_free(pblk);
1109	pblk_l2p_free(pblk);
1110	pblk_rwb_free(pblk);
1111	pblk_core_free(pblk);
1112
1113	kfree(pblk);
1114}
1115
1116static void pblk_tear_down(struct pblk *pblk, bool graceful)
1117{
1118	if (graceful)
1119		__pblk_pipeline_flush(pblk);
1120	__pblk_pipeline_stop(pblk);
1121	pblk_writer_stop(pblk);
1122	pblk_rb_sync_l2p(&pblk->rwb);
1123	pblk_rl_free(&pblk->rl);
1124
1125	pblk_debug(pblk, "consistent tear down (graceful:%d)\n", graceful);
1126}
1127
1128static void pblk_exit(void *private, bool graceful)
1129{
1130	struct pblk *pblk = private;
1131
1132	pblk_gc_exit(pblk, graceful);
1133	pblk_tear_down(pblk, graceful);
 
1134
1135#ifdef CONFIG_NVM_PBLK_DEBUG
1136	pblk_info(pblk, "exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
1137#endif
1138
1139	pblk_free(pblk);
 
1140}
1141
1142static sector_t pblk_capacity(void *private)
1143{
1144	struct pblk *pblk = private;
1145
1146	return pblk->capacity * NR_PHY_IN_LOG;
1147}
1148
1149static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1150		       int flags)
1151{
1152	struct nvm_geo *geo = &dev->geo;
1153	struct request_queue *bqueue = dev->q;
1154	struct request_queue *tqueue = tdisk->queue;
1155	struct pblk *pblk;
1156	int ret;
1157
1158	pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1159	if (!pblk)
1160		return ERR_PTR(-ENOMEM);
1161
1162	pblk->dev = dev;
1163	pblk->disk = tdisk;
1164	pblk->state = PBLK_STATE_RUNNING;
1165	trace_pblk_state(pblk_disk_name(pblk), pblk->state);
1166	pblk->gc.gc_enabled = 0;
1167
1168	if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1169					geo->version == NVM_OCSSD_SPEC_20)) {
1170		pblk_err(pblk, "OCSSD version not supported (%u)\n",
1171							geo->version);
1172		kfree(pblk);
1173		return ERR_PTR(-EINVAL);
1174	}
1175
1176	if (geo->ext) {
1177		pblk_err(pblk, "extended metadata not supported\n");
1178		kfree(pblk);
1179		return ERR_PTR(-EINVAL);
1180	}
1181
1182	spin_lock_init(&pblk->resubmit_lock);
 
 
 
 
 
 
 
 
1183	spin_lock_init(&pblk->trans_lock);
1184	spin_lock_init(&pblk->lock);
1185
1186#ifdef CONFIG_NVM_PBLK_DEBUG
1187	atomic_long_set(&pblk->inflight_writes, 0);
1188	atomic_long_set(&pblk->padded_writes, 0);
1189	atomic_long_set(&pblk->padded_wb, 0);
1190	atomic_long_set(&pblk->req_writes, 0);
1191	atomic_long_set(&pblk->sub_writes, 0);
1192	atomic_long_set(&pblk->sync_writes, 0);
1193	atomic_long_set(&pblk->inflight_reads, 0);
1194	atomic_long_set(&pblk->cache_reads, 0);
1195	atomic_long_set(&pblk->sync_reads, 0);
1196	atomic_long_set(&pblk->recov_writes, 0);
1197	atomic_long_set(&pblk->recov_writes, 0);
1198	atomic_long_set(&pblk->recov_gc_writes, 0);
1199	atomic_long_set(&pblk->recov_gc_reads, 0);
1200#endif
1201
1202	atomic_long_set(&pblk->read_failed, 0);
1203	atomic_long_set(&pblk->read_empty, 0);
1204	atomic_long_set(&pblk->read_high_ecc, 0);
1205	atomic_long_set(&pblk->read_failed_gc, 0);
1206	atomic_long_set(&pblk->write_failed, 0);
1207	atomic_long_set(&pblk->erase_failed, 0);
1208
1209	ret = pblk_core_init(pblk);
1210	if (ret) {
1211		pblk_err(pblk, "could not initialize core\n");
1212		goto fail;
1213	}
1214
1215	ret = pblk_lines_init(pblk);
1216	if (ret) {
1217		pblk_err(pblk, "could not initialize lines\n");
1218		goto fail_free_core;
1219	}
1220
1221	ret = pblk_rwb_init(pblk);
1222	if (ret) {
1223		pblk_err(pblk, "could not initialize write buffer\n");
1224		goto fail_free_lines;
1225	}
1226
1227	ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1228	if (ret) {
1229		pblk_err(pblk, "could not initialize maps\n");
1230		goto fail_free_rwb;
1231	}
1232
1233	ret = pblk_writer_init(pblk);
1234	if (ret) {
1235		if (ret != -EINTR)
1236			pblk_err(pblk, "could not initialize write thread\n");
1237		goto fail_free_l2p;
1238	}
1239
1240	ret = pblk_gc_init(pblk);
1241	if (ret) {
1242		pblk_err(pblk, "could not initialize gc\n");
1243		goto fail_stop_writer;
1244	}
1245
1246	/* inherit the size from the underlying device */
1247	blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1248	blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1249
1250	blk_queue_write_cache(tqueue, true, false);
1251
1252	tqueue->limits.discard_granularity = geo->clba * geo->csecs;
1253	tqueue->limits.discard_alignment = 0;
1254	blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
1255	blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
1256
1257	pblk_info(pblk, "luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
 
1258			geo->all_luns, pblk->l_mg.nr_lines,
1259			(unsigned long long)pblk->capacity,
1260			pblk->rwb.nr_entries);
1261
1262	wake_up_process(pblk->writer_ts);
1263
1264	/* Check if we need to start GC */
1265	pblk_gc_should_kick(pblk);
1266
1267	return pblk;
1268
1269fail_stop_writer:
1270	pblk_writer_stop(pblk);
1271fail_free_l2p:
1272	pblk_l2p_free(pblk);
1273fail_free_rwb:
1274	pblk_rwb_free(pblk);
1275fail_free_lines:
1276	pblk_lines_free(pblk);
1277fail_free_core:
1278	pblk_core_free(pblk);
1279fail:
1280	kfree(pblk);
1281	return ERR_PTR(ret);
1282}
1283
1284/* physical block device target */
1285static struct nvm_tgt_type tt_pblk = {
1286	.name		= "pblk",
1287	.version	= {1, 0, 0},
1288
1289	.bops		= &pblk_bops,
1290	.capacity	= pblk_capacity,
1291
1292	.init		= pblk_init,
1293	.exit		= pblk_exit,
1294
1295	.sysfs_init	= pblk_sysfs_init,
1296	.sysfs_exit	= pblk_sysfs_exit,
1297	.owner		= THIS_MODULE,
1298};
1299
1300static int __init pblk_module_init(void)
1301{
1302	int ret;
1303
1304	ret = bioset_init(&pblk_bio_set, BIO_POOL_SIZE, 0, 0);
1305	if (ret)
1306		return ret;
1307	ret = nvm_register_tgt_type(&tt_pblk);
1308	if (ret)
1309		bioset_exit(&pblk_bio_set);
1310	return ret;
1311}
1312
1313static void pblk_module_exit(void)
1314{
1315	bioset_exit(&pblk_bio_set);
1316	nvm_unregister_tgt_type(&tt_pblk);
1317}
1318
1319module_init(pblk_module_init);
1320module_exit(pblk_module_exit);
1321MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1322MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1323MODULE_LICENSE("GPL v2");
1324MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");
v4.17
 
   1/*
   2 * Copyright (C) 2015 IT University of Copenhagen (rrpc.c)
   3 * Copyright (C) 2016 CNEX Labs
   4 * Initial release: Javier Gonzalez <javier@cnexlabs.com>
   5 *                  Matias Bjorling <matias@cnexlabs.com>
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License version
   9 * 2 as published by the Free Software Foundation.
  10 *
  11 * This program is distributed in the hope that it will be useful, but
  12 * WITHOUT ANY WARRANTY; without even the implied warranty of
  13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14 * General Public License for more details.
  15 *
  16 * Implementation of a physical block-device target for Open-channel SSDs.
  17 *
  18 * pblk-init.c - pblk's initialization.
  19 */
  20
  21#include "pblk.h"
 
  22
  23static struct kmem_cache *pblk_ws_cache, *pblk_rec_cache, *pblk_g_rq_cache,
  24				*pblk_w_rq_cache;
  25static DECLARE_RWSEM(pblk_lock);
  26struct bio_set *pblk_bio_set;
  27
  28static int pblk_rw_io(struct request_queue *q, struct pblk *pblk,
  29			  struct bio *bio)
  30{
  31	int ret;
 
 
 
 
  32
  33	/* Read requests must be <= 256kb due to NVMe's 64 bit completion bitmap
  34	 * constraint. Writes can be of arbitrary size.
  35	 */
  36	if (bio_data_dir(bio) == READ) {
  37		blk_queue_split(q, &bio);
  38		ret = pblk_submit_read(pblk, bio);
  39		if (ret == NVM_IO_DONE && bio_flagged(bio, BIO_CLONED))
  40			bio_put(bio);
  41
  42		return ret;
  43	}
 
 
  44
  45	/* Prevent deadlock in the case of a modest LUN configuration and large
  46	 * user I/Os. Unless stalled, the rate limiter leaves at least 256KB
  47	 * available for user I/O.
  48	 */
  49	if (pblk_get_secs(bio) > pblk_rl_max_io(&pblk->rl))
  50		blk_queue_split(q, &bio);
  51
  52	return pblk_write_to_cache(pblk, bio, PBLK_IOTYPE_USER);
  53}
  54
  55static blk_qc_t pblk_make_rq(struct request_queue *q, struct bio *bio)
  56{
  57	struct pblk *pblk = q->queuedata;
  58
  59	if (bio_op(bio) == REQ_OP_DISCARD) {
  60		pblk_discard(pblk, bio);
  61		if (!(bio->bi_opf & REQ_PREFLUSH)) {
  62			bio_endio(bio);
  63			return BLK_QC_T_NONE;
  64		}
  65	}
  66
  67	switch (pblk_rw_io(q, pblk, bio)) {
  68	case NVM_IO_ERR:
  69		bio_io_error(bio);
  70		break;
  71	case NVM_IO_DONE:
  72		bio_endio(bio);
  73		break;
 
 
 
 
 
 
 
 
  74	}
  75
  76	return BLK_QC_T_NONE;
  77}
  78
 
 
 
 
 
 
  79static size_t pblk_trans_map_size(struct pblk *pblk)
  80{
  81	int entry_size = 8;
  82
  83	if (pblk->addrf_len < 32)
  84		entry_size = 4;
  85
  86	return entry_size * pblk->rl.nr_secs;
  87}
  88
  89#ifdef CONFIG_NVM_DEBUG
  90static u32 pblk_l2p_crc(struct pblk *pblk)
  91{
  92	size_t map_size;
  93	u32 crc = ~(u32)0;
  94
  95	map_size = pblk_trans_map_size(pblk);
  96	crc = crc32_le(crc, pblk->trans_map, map_size);
  97	return crc;
  98}
  99#endif
 100
 101static void pblk_l2p_free(struct pblk *pblk)
 102{
 103	vfree(pblk->trans_map);
 104}
 105
 106static int pblk_l2p_recover(struct pblk *pblk, bool factory_init)
 107{
 108	struct pblk_line *line = NULL;
 109
 110	if (factory_init) {
 111		pblk_setup_uuid(pblk);
 112	} else {
 113		line = pblk_recov_l2p(pblk);
 114		if (IS_ERR(line)) {
 115			pr_err("pblk: could not recover l2p table\n");
 116			return -EFAULT;
 117		}
 118	}
 119
 120#ifdef CONFIG_NVM_DEBUG
 121	pr_info("pblk init: L2P CRC: %x\n", pblk_l2p_crc(pblk));
 122#endif
 123
 124	/* Free full lines directly as GC has not been started yet */
 125	pblk_gc_free_full_lines(pblk);
 126
 127	if (!line) {
 128		/* Configure next line for user data */
 129		line = pblk_line_get_first_data(pblk);
 130		if (!line) {
 131			pr_err("pblk: line list corrupted\n");
 132			return -EFAULT;
 133		}
 134	}
 135
 136	return 0;
 137}
 138
 139static int pblk_l2p_init(struct pblk *pblk, bool factory_init)
 140{
 141	sector_t i;
 142	struct ppa_addr ppa;
 143	size_t map_size;
 
 144
 145	map_size = pblk_trans_map_size(pblk);
 146	pblk->trans_map = vmalloc(map_size);
 147	if (!pblk->trans_map)
 
 
 
 148		return -ENOMEM;
 
 149
 150	pblk_ppa_set_empty(&ppa);
 151
 152	for (i = 0; i < pblk->rl.nr_secs; i++)
 153		pblk_trans_map_set(pblk, i, ppa);
 154
 155	return pblk_l2p_recover(pblk, factory_init);
 
 
 
 
 156}
 157
 158static void pblk_rwb_free(struct pblk *pblk)
 159{
 160	if (pblk_rb_tear_down_check(&pblk->rwb))
 161		pr_err("pblk: write buffer error on tear down\n");
 162
 163	pblk_rb_data_free(&pblk->rwb);
 164	vfree(pblk_rb_entries_ref(&pblk->rwb));
 165}
 166
 167static int pblk_rwb_init(struct pblk *pblk)
 168{
 169	struct nvm_tgt_dev *dev = pblk->dev;
 170	struct nvm_geo *geo = &dev->geo;
 171	struct pblk_rb_entry *entries;
 172	unsigned long nr_entries;
 173	unsigned int power_size, power_seg_sz;
 174
 175	nr_entries = pblk_rb_calculate_size(pblk->pgs_in_buffer);
 
 
 176
 177	entries = vzalloc(nr_entries * sizeof(struct pblk_rb_entry));
 178	if (!entries)
 179		return -ENOMEM;
 
 180
 181	power_size = get_count_order(nr_entries);
 182	power_seg_sz = get_count_order(geo->csecs);
 183
 184	return pblk_rb_init(&pblk->rwb, entries, power_size, power_seg_sz);
 185}
 186
 187/* Minimum pages needed within a lun */
 188#define ADDR_POOL_SIZE 64
 189
 190static int pblk_set_addrf_12(struct nvm_geo *geo, struct nvm_addrf_12 *dst)
 191{
 192	struct nvm_addrf_12 *src = (struct nvm_addrf_12 *)&geo->addrf;
 193	int power_len;
 194
 195	/* Re-calculate channel and lun format to adapt to configuration */
 196	power_len = get_count_order(geo->num_ch);
 197	if (1 << power_len != geo->num_ch) {
 198		pr_err("pblk: supports only power-of-two channel config.\n");
 199		return -EINVAL;
 200	}
 201	dst->ch_len = power_len;
 202
 203	power_len = get_count_order(geo->num_lun);
 204	if (1 << power_len != geo->num_lun) {
 205		pr_err("pblk: supports only power-of-two LUN config.\n");
 206		return -EINVAL;
 207	}
 208	dst->lun_len = power_len;
 209
 210	dst->blk_len = src->blk_len;
 211	dst->pg_len = src->pg_len;
 212	dst->pln_len = src->pln_len;
 213	dst->sec_len = src->sec_len;
 214
 215	dst->sec_offset = 0;
 216	dst->pln_offset = dst->sec_len;
 217	dst->ch_offset = dst->pln_offset + dst->pln_len;
 218	dst->lun_offset = dst->ch_offset + dst->ch_len;
 219	dst->pg_offset = dst->lun_offset + dst->lun_len;
 220	dst->blk_offset = dst->pg_offset + dst->pg_len;
 221
 222	dst->sec_mask = ((1ULL << dst->sec_len) - 1) << dst->sec_offset;
 223	dst->pln_mask = ((1ULL << dst->pln_len) - 1) << dst->pln_offset;
 224	dst->ch_mask = ((1ULL << dst->ch_len) - 1) << dst->ch_offset;
 225	dst->lun_mask = ((1ULL << dst->lun_len) - 1) << dst->lun_offset;
 226	dst->pg_mask = ((1ULL << dst->pg_len) - 1) << dst->pg_offset;
 227	dst->blk_mask = ((1ULL << dst->blk_len) - 1) << dst->blk_offset;
 228
 229	return dst->blk_offset + src->blk_len;
 230}
 231
 232static int pblk_set_addrf_20(struct nvm_geo *geo, struct nvm_addrf *adst,
 233			     struct pblk_addrf *udst)
 234{
 235	struct nvm_addrf *src = &geo->addrf;
 236
 237	adst->ch_len = get_count_order(geo->num_ch);
 238	adst->lun_len = get_count_order(geo->num_lun);
 239	adst->chk_len = src->chk_len;
 240	adst->sec_len = src->sec_len;
 241
 242	adst->sec_offset = 0;
 243	adst->ch_offset = adst->sec_len;
 244	adst->lun_offset = adst->ch_offset + adst->ch_len;
 245	adst->chk_offset = adst->lun_offset + adst->lun_len;
 246
 247	adst->sec_mask = ((1ULL << adst->sec_len) - 1) << adst->sec_offset;
 248	adst->chk_mask = ((1ULL << adst->chk_len) - 1) << adst->chk_offset;
 249	adst->lun_mask = ((1ULL << adst->lun_len) - 1) << adst->lun_offset;
 250	adst->ch_mask = ((1ULL << adst->ch_len) - 1) << adst->ch_offset;
 251
 252	udst->sec_stripe = geo->ws_opt;
 253	udst->ch_stripe = geo->num_ch;
 254	udst->lun_stripe = geo->num_lun;
 255
 256	udst->sec_lun_stripe = udst->sec_stripe * udst->ch_stripe;
 257	udst->sec_ws_stripe = udst->sec_lun_stripe * udst->lun_stripe;
 258
 259	return adst->chk_offset + adst->chk_len;
 260}
 261
 262static int pblk_set_addrf(struct pblk *pblk)
 263{
 264	struct nvm_tgt_dev *dev = pblk->dev;
 265	struct nvm_geo *geo = &dev->geo;
 266	int mod;
 267
 268	switch (geo->version) {
 269	case NVM_OCSSD_SPEC_12:
 270		div_u64_rem(geo->clba, pblk->min_write_pgs, &mod);
 271		if (mod) {
 272			pr_err("pblk: bad configuration of sectors/pages\n");
 273			return -EINVAL;
 274		}
 275
 276		pblk->addrf_len = pblk_set_addrf_12(geo, (void *)&pblk->addrf);
 
 277		break;
 278	case NVM_OCSSD_SPEC_20:
 279		pblk->addrf_len = pblk_set_addrf_20(geo, (void *)&pblk->addrf,
 280								&pblk->uaddrf);
 281		break;
 282	default:
 283		pr_err("pblk: OCSSD revision not supported (%d)\n",
 284								geo->version);
 285		return -EINVAL;
 286	}
 287
 288	return 0;
 289}
 290
 291static int pblk_init_global_caches(struct pblk *pblk)
 292{
 293	down_write(&pblk_lock);
 294	pblk_ws_cache = kmem_cache_create("pblk_blk_ws",
 295				sizeof(struct pblk_line_ws), 0, 0, NULL);
 296	if (!pblk_ws_cache) {
 297		up_write(&pblk_lock);
 298		return -ENOMEM;
 299	}
 300
 301	pblk_rec_cache = kmem_cache_create("pblk_rec",
 302				sizeof(struct pblk_rec_ctx), 0, 0, NULL);
 303	if (!pblk_rec_cache) {
 304		kmem_cache_destroy(pblk_ws_cache);
 305		up_write(&pblk_lock);
 306		return -ENOMEM;
 307	}
 308
 309	pblk_g_rq_cache = kmem_cache_create("pblk_g_rq", pblk_g_rq_size,
 310				0, 0, NULL);
 311	if (!pblk_g_rq_cache) {
 312		kmem_cache_destroy(pblk_ws_cache);
 313		kmem_cache_destroy(pblk_rec_cache);
 314		up_write(&pblk_lock);
 315		return -ENOMEM;
 316	}
 317
 318	pblk_w_rq_cache = kmem_cache_create("pblk_w_rq", pblk_w_rq_size,
 319				0, 0, NULL);
 320	if (!pblk_w_rq_cache) {
 321		kmem_cache_destroy(pblk_ws_cache);
 322		kmem_cache_destroy(pblk_rec_cache);
 323		kmem_cache_destroy(pblk_g_rq_cache);
 324		up_write(&pblk_lock);
 325		return -ENOMEM;
 326	}
 327	up_write(&pblk_lock);
 328
 329	return 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 330}
 331
 332static void pblk_free_global_caches(struct pblk *pblk)
 333{
 334	kmem_cache_destroy(pblk_ws_cache);
 335	kmem_cache_destroy(pblk_rec_cache);
 336	kmem_cache_destroy(pblk_g_rq_cache);
 337	kmem_cache_destroy(pblk_w_rq_cache);
 
 
 
 
 
 
 
 
 
 
 
 338}
 339
 340static int pblk_core_init(struct pblk *pblk)
 341{
 342	struct nvm_tgt_dev *dev = pblk->dev;
 343	struct nvm_geo *geo = &dev->geo;
 344	int max_write_ppas;
 345
 346	atomic64_set(&pblk->user_wa, 0);
 347	atomic64_set(&pblk->pad_wa, 0);
 348	atomic64_set(&pblk->gc_wa, 0);
 349	pblk->user_rst_wa = 0;
 350	pblk->pad_rst_wa = 0;
 351	pblk->gc_rst_wa = 0;
 352
 353	atomic64_set(&pblk->nr_flush, 0);
 354	pblk->nr_flush_rst = 0;
 355
 356	pblk->pgs_in_buffer = geo->mw_cunits * geo->all_luns;
 357
 358	pblk->min_write_pgs = geo->ws_opt * (geo->csecs / PAGE_SIZE);
 359	max_write_ppas = pblk->min_write_pgs * geo->all_luns;
 360	pblk->max_write_pgs = min_t(int, max_write_ppas, NVM_MAX_VLBA);
 
 
 361	pblk_set_sec_per_write(pblk, pblk->min_write_pgs);
 362
 363	if (pblk->max_write_pgs > PBLK_MAX_REQ_ADDRS) {
 364		pr_err("pblk: vector list too big(%u > %u)\n",
 365				pblk->max_write_pgs, PBLK_MAX_REQ_ADDRS);
 366		return -EINVAL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 367	}
 368
 369	pblk->pad_dist = kzalloc((pblk->min_write_pgs - 1) * sizeof(atomic64_t),
 370								GFP_KERNEL);
 371	if (!pblk->pad_dist)
 372		return -ENOMEM;
 373
 374	if (pblk_init_global_caches(pblk))
 375		goto fail_free_pad_dist;
 376
 377	/* Internal bios can be at most the sectors signaled by the device. */
 378	pblk->page_bio_pool = mempool_create_page_pool(NVM_MAX_VLBA, 0);
 379	if (!pblk->page_bio_pool)
 380		goto free_global_caches;
 381
 382	pblk->gen_ws_pool = mempool_create_slab_pool(PBLK_GEN_WS_POOL_SIZE,
 383							pblk_ws_cache);
 384	if (!pblk->gen_ws_pool)
 385		goto free_page_bio_pool;
 386
 387	pblk->rec_pool = mempool_create_slab_pool(geo->all_luns,
 388							pblk_rec_cache);
 389	if (!pblk->rec_pool)
 390		goto free_gen_ws_pool;
 391
 392	pblk->r_rq_pool = mempool_create_slab_pool(geo->all_luns,
 393							pblk_g_rq_cache);
 394	if (!pblk->r_rq_pool)
 395		goto free_rec_pool;
 396
 397	pblk->e_rq_pool = mempool_create_slab_pool(geo->all_luns,
 398							pblk_g_rq_cache);
 399	if (!pblk->e_rq_pool)
 400		goto free_r_rq_pool;
 401
 402	pblk->w_rq_pool = mempool_create_slab_pool(geo->all_luns,
 403							pblk_w_rq_cache);
 404	if (!pblk->w_rq_pool)
 405		goto free_e_rq_pool;
 406
 407	pblk->close_wq = alloc_workqueue("pblk-close-wq",
 408			WQ_MEM_RECLAIM | WQ_UNBOUND, PBLK_NR_CLOSE_JOBS);
 409	if (!pblk->close_wq)
 410		goto free_w_rq_pool;
 411
 412	pblk->bb_wq = alloc_workqueue("pblk-bb-wq",
 413			WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
 414	if (!pblk->bb_wq)
 415		goto free_close_wq;
 416
 417	pblk->r_end_wq = alloc_workqueue("pblk-read-end-wq",
 418			WQ_MEM_RECLAIM | WQ_UNBOUND, 0);
 419	if (!pblk->r_end_wq)
 420		goto free_bb_wq;
 421
 422	if (pblk_set_addrf(pblk))
 423		goto free_r_end_wq;
 424
 425	INIT_LIST_HEAD(&pblk->compl_list);
 
 426
 427	return 0;
 428
 429free_r_end_wq:
 430	destroy_workqueue(pblk->r_end_wq);
 431free_bb_wq:
 432	destroy_workqueue(pblk->bb_wq);
 433free_close_wq:
 434	destroy_workqueue(pblk->close_wq);
 435free_w_rq_pool:
 436	mempool_destroy(pblk->w_rq_pool);
 437free_e_rq_pool:
 438	mempool_destroy(pblk->e_rq_pool);
 439free_r_rq_pool:
 440	mempool_destroy(pblk->r_rq_pool);
 441free_rec_pool:
 442	mempool_destroy(pblk->rec_pool);
 443free_gen_ws_pool:
 444	mempool_destroy(pblk->gen_ws_pool);
 445free_page_bio_pool:
 446	mempool_destroy(pblk->page_bio_pool);
 447free_global_caches:
 448	pblk_free_global_caches(pblk);
 449fail_free_pad_dist:
 450	kfree(pblk->pad_dist);
 451	return -ENOMEM;
 452}
 453
 454static void pblk_core_free(struct pblk *pblk)
 455{
 456	if (pblk->close_wq)
 457		destroy_workqueue(pblk->close_wq);
 458
 459	if (pblk->r_end_wq)
 460		destroy_workqueue(pblk->r_end_wq);
 461
 462	if (pblk->bb_wq)
 463		destroy_workqueue(pblk->bb_wq);
 464
 465	mempool_destroy(pblk->page_bio_pool);
 466	mempool_destroy(pblk->gen_ws_pool);
 467	mempool_destroy(pblk->rec_pool);
 468	mempool_destroy(pblk->r_rq_pool);
 469	mempool_destroy(pblk->e_rq_pool);
 470	mempool_destroy(pblk->w_rq_pool);
 471
 472	pblk_free_global_caches(pblk);
 473	kfree(pblk->pad_dist);
 474}
 475
 476static void pblk_line_mg_free(struct pblk *pblk)
 477{
 478	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 479	int i;
 480
 481	kfree(l_mg->bb_template);
 482	kfree(l_mg->bb_aux);
 483	kfree(l_mg->vsc_list);
 484
 485	for (i = 0; i < PBLK_DATA_LINES; i++) {
 486		kfree(l_mg->sline_meta[i]);
 487		pblk_mfree(l_mg->eline_meta[i]->buf, l_mg->emeta_alloc_type);
 488		kfree(l_mg->eline_meta[i]);
 489	}
 
 
 
 490}
 491
 492static void pblk_line_meta_free(struct pblk_line *line)
 
 493{
 
 
 494	kfree(line->blk_bitmap);
 495	kfree(line->erase_bitmap);
 496	kfree(line->chks);
 
 
 
 497}
 498
 499static void pblk_lines_free(struct pblk *pblk)
 500{
 501	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 502	struct pblk_line *line;
 503	int i;
 504
 505	spin_lock(&l_mg->free_lock);
 506	for (i = 0; i < l_mg->nr_lines; i++) {
 507		line = &pblk->lines[i];
 508
 509		pblk_line_free(pblk, line);
 510		pblk_line_meta_free(line);
 511	}
 512	spin_unlock(&l_mg->free_lock);
 513
 514	pblk_line_mg_free(pblk);
 515
 516	kfree(pblk->luns);
 517	kfree(pblk->lines);
 518}
 519
 520static int pblk_bb_get_tbl(struct nvm_tgt_dev *dev, struct pblk_lun *rlun,
 521			   u8 *blks, int nr_blks)
 522{
 523	struct ppa_addr ppa;
 524	int ret;
 525
 526	ppa.ppa = 0;
 527	ppa.g.ch = rlun->bppa.g.ch;
 528	ppa.g.lun = rlun->bppa.g.lun;
 529
 530	ret = nvm_get_tgt_bb_tbl(dev, ppa, blks);
 531	if (ret)
 532		return ret;
 533
 534	nr_blks = nvm_bb_tbl_fold(dev->parent, blks, nr_blks);
 535	if (nr_blks < 0)
 536		return -EIO;
 537
 538	return 0;
 539}
 540
 541static void *pblk_bb_get_meta(struct pblk *pblk)
 542{
 543	struct nvm_tgt_dev *dev = pblk->dev;
 544	struct nvm_geo *geo = &dev->geo;
 545	u8 *meta;
 546	int i, nr_blks, blk_per_lun;
 547	int ret;
 548
 549	blk_per_lun = geo->num_chk * geo->pln_mode;
 550	nr_blks = blk_per_lun * geo->all_luns;
 551
 552	meta = kmalloc(nr_blks, GFP_KERNEL);
 553	if (!meta)
 554		return ERR_PTR(-ENOMEM);
 555
 556	for (i = 0; i < geo->all_luns; i++) {
 557		struct pblk_lun *rlun = &pblk->luns[i];
 558		u8 *meta_pos = meta + i * blk_per_lun;
 559
 560		ret = pblk_bb_get_tbl(dev, rlun, meta_pos, blk_per_lun);
 561		if (ret) {
 562			kfree(meta);
 563			return ERR_PTR(-EIO);
 564		}
 565	}
 566
 567	return meta;
 568}
 569
 570static void *pblk_chunk_get_meta(struct pblk *pblk)
 571{
 572	struct nvm_tgt_dev *dev = pblk->dev;
 573	struct nvm_geo *geo = &dev->geo;
 574
 575	if (geo->version == NVM_OCSSD_SPEC_12)
 576		return pblk_bb_get_meta(pblk);
 577	else
 578		return pblk_chunk_get_info(pblk);
 579}
 580
 581static int pblk_luns_init(struct pblk *pblk)
 582{
 583	struct nvm_tgt_dev *dev = pblk->dev;
 584	struct nvm_geo *geo = &dev->geo;
 585	struct pblk_lun *rlun;
 586	int i;
 587
 588	/* TODO: Implement unbalanced LUN support */
 589	if (geo->num_lun < 0) {
 590		pr_err("pblk: unbalanced LUN config.\n");
 591		return -EINVAL;
 592	}
 593
 594	pblk->luns = kcalloc(geo->all_luns, sizeof(struct pblk_lun),
 595								GFP_KERNEL);
 596	if (!pblk->luns)
 597		return -ENOMEM;
 598
 599	for (i = 0; i < geo->all_luns; i++) {
 600		/* Stripe across channels */
 601		int ch = i % geo->num_ch;
 602		int lun_raw = i / geo->num_ch;
 603		int lunid = lun_raw + ch * geo->num_lun;
 604
 605		rlun = &pblk->luns[i];
 606		rlun->bppa = dev->luns[lunid];
 607
 608		sema_init(&rlun->wr_sem, 1);
 609	}
 610
 611	return 0;
 612}
 613
 614/* See comment over struct line_emeta definition */
 615static unsigned int calc_emeta_len(struct pblk *pblk)
 616{
 617	struct pblk_line_meta *lm = &pblk->lm;
 618	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 619	struct nvm_tgt_dev *dev = pblk->dev;
 620	struct nvm_geo *geo = &dev->geo;
 621
 622	/* Round to sector size so that lba_list starts on its own sector */
 623	lm->emeta_sec[1] = DIV_ROUND_UP(
 624			sizeof(struct line_emeta) + lm->blk_bitmap_len +
 625			sizeof(struct wa_counters), geo->csecs);
 626	lm->emeta_len[1] = lm->emeta_sec[1] * geo->csecs;
 627
 628	/* Round to sector size so that vsc_list starts on its own sector */
 629	lm->dsec_per_line = lm->sec_per_line - lm->emeta_sec[0];
 630	lm->emeta_sec[2] = DIV_ROUND_UP(lm->dsec_per_line * sizeof(u64),
 631			geo->csecs);
 632	lm->emeta_len[2] = lm->emeta_sec[2] * geo->csecs;
 633
 634	lm->emeta_sec[3] = DIV_ROUND_UP(l_mg->nr_lines * sizeof(u32),
 635			geo->csecs);
 636	lm->emeta_len[3] = lm->emeta_sec[3] * geo->csecs;
 637
 638	lm->vsc_list_len = l_mg->nr_lines * sizeof(u32);
 639
 640	return (lm->emeta_len[1] + lm->emeta_len[2] + lm->emeta_len[3]);
 641}
 642
 643static void pblk_set_provision(struct pblk *pblk, long nr_free_blks)
 644{
 645	struct nvm_tgt_dev *dev = pblk->dev;
 646	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 647	struct pblk_line_meta *lm = &pblk->lm;
 648	struct nvm_geo *geo = &dev->geo;
 649	sector_t provisioned;
 650	int sec_meta, blk_meta;
 
 651
 652	if (geo->op == NVM_TARGET_DEFAULT_OP)
 653		pblk->op = PBLK_DEFAULT_OP;
 654	else
 655		pblk->op = geo->op;
 656
 657	provisioned = nr_free_blks;
 
 658	provisioned *= (100 - pblk->op);
 659	sector_div(provisioned, 100);
 660
 661	pblk->op_blks = nr_free_blks - provisioned;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 662
 663	/* Internally pblk manages all free blocks, but all calculations based
 664	 * on user capacity consider only provisioned blocks
 665	 */
 666	pblk->rl.total_blocks = nr_free_blks;
 667	pblk->rl.nr_secs = nr_free_blks * geo->clba;
 668
 669	/* Consider sectors used for metadata */
 670	sec_meta = (lm->smeta_sec + lm->emeta_sec[0]) * l_mg->nr_free_lines;
 671	blk_meta = DIV_ROUND_UP(sec_meta, geo->clba);
 672
 673	pblk->capacity = (provisioned - blk_meta) * geo->clba;
 
 674
 675	atomic_set(&pblk->rl.free_blocks, nr_free_blks);
 676	atomic_set(&pblk->rl.free_user_blocks, nr_free_blks);
 677}
 678
 679static int pblk_setup_line_meta_12(struct pblk *pblk, struct pblk_line *line,
 680				   void *chunk_meta)
 681{
 682	struct nvm_tgt_dev *dev = pblk->dev;
 683	struct nvm_geo *geo = &dev->geo;
 684	struct pblk_line_meta *lm = &pblk->lm;
 685	int i, chk_per_lun, nr_bad_chks = 0;
 686
 687	chk_per_lun = geo->num_chk * geo->pln_mode;
 688
 689	for (i = 0; i < lm->blk_per_line; i++) {
 690		struct pblk_lun *rlun = &pblk->luns[i];
 691		struct nvm_chk_meta *chunk;
 692		int pos = pblk_ppa_to_pos(geo, rlun->bppa);
 693		u8 *lun_bb_meta = chunk_meta + pos * chk_per_lun;
 694
 695		chunk = &line->chks[pos];
 696
 697		/*
 698		 * In 1.2 spec. chunk state is not persisted by the device. Thus
 699		 * some of the values are reset each time pblk is instantiated.
 700		 */
 701		if (lun_bb_meta[line->id] == NVM_BLK_T_FREE)
 702			chunk->state =  NVM_CHK_ST_FREE;
 703		else
 704			chunk->state = NVM_CHK_ST_OFFLINE;
 705
 706		chunk->type = NVM_CHK_TP_W_SEQ;
 707		chunk->wi = 0;
 708		chunk->slba = -1;
 709		chunk->cnlb = geo->clba;
 710		chunk->wp = 0;
 711
 712		if (!(chunk->state & NVM_CHK_ST_OFFLINE))
 713			continue;
 714
 715		set_bit(pos, line->blk_bitmap);
 716		nr_bad_chks++;
 717	}
 718
 719	return nr_bad_chks;
 720}
 721
 722static int pblk_setup_line_meta_20(struct pblk *pblk, struct pblk_line *line,
 723				   struct nvm_chk_meta *meta)
 724{
 725	struct nvm_tgt_dev *dev = pblk->dev;
 726	struct nvm_geo *geo = &dev->geo;
 727	struct pblk_line_meta *lm = &pblk->lm;
 728	int i, nr_bad_chks = 0;
 729
 730	for (i = 0; i < lm->blk_per_line; i++) {
 731		struct pblk_lun *rlun = &pblk->luns[i];
 732		struct nvm_chk_meta *chunk;
 733		struct nvm_chk_meta *chunk_meta;
 734		struct ppa_addr ppa;
 735		int pos;
 736
 737		ppa = rlun->bppa;
 738		pos = pblk_ppa_to_pos(geo, ppa);
 739		chunk = &line->chks[pos];
 740
 741		ppa.m.chk = line->id;
 742		chunk_meta = pblk_chunk_get_off(pblk, meta, ppa);
 743
 744		chunk->state = chunk_meta->state;
 745		chunk->type = chunk_meta->type;
 746		chunk->wi = chunk_meta->wi;
 747		chunk->slba = chunk_meta->slba;
 748		chunk->cnlb = chunk_meta->cnlb;
 749		chunk->wp = chunk_meta->wp;
 750
 751		if (!(chunk->state & NVM_CHK_ST_OFFLINE))
 752			continue;
 753
 754		if (chunk->type & NVM_CHK_TP_SZ_SPEC) {
 755			WARN_ONCE(1, "pblk: custom-sized chunks unsupported\n");
 756			continue;
 757		}
 758
 
 
 
 759		set_bit(pos, line->blk_bitmap);
 760		nr_bad_chks++;
 761	}
 762
 763	return nr_bad_chks;
 764}
 765
 766static long pblk_setup_line_meta(struct pblk *pblk, struct pblk_line *line,
 767				 void *chunk_meta, int line_id)
 768{
 769	struct nvm_tgt_dev *dev = pblk->dev;
 770	struct nvm_geo *geo = &dev->geo;
 771	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 772	struct pblk_line_meta *lm = &pblk->lm;
 773	long nr_bad_chks, chk_in_line;
 774
 775	line->pblk = pblk;
 776	line->id = line_id;
 777	line->type = PBLK_LINETYPE_FREE;
 778	line->state = PBLK_LINESTATE_NEW;
 779	line->gc_group = PBLK_LINEGC_NONE;
 780	line->vsc = &l_mg->vsc_list[line_id];
 781	spin_lock_init(&line->lock);
 782
 783	if (geo->version == NVM_OCSSD_SPEC_12)
 784		nr_bad_chks = pblk_setup_line_meta_12(pblk, line, chunk_meta);
 785	else
 786		nr_bad_chks = pblk_setup_line_meta_20(pblk, line, chunk_meta);
 787
 788	chk_in_line = lm->blk_per_line - nr_bad_chks;
 789	if (nr_bad_chks < 0 || nr_bad_chks > lm->blk_per_line ||
 790					chk_in_line < lm->min_blk_line) {
 791		line->state = PBLK_LINESTATE_BAD;
 792		list_add_tail(&line->list, &l_mg->bad_list);
 793		return 0;
 794	}
 795
 796	atomic_set(&line->blk_in_line, chk_in_line);
 797	list_add_tail(&line->list, &l_mg->free_list);
 798	l_mg->nr_free_lines++;
 799
 800	return chk_in_line;
 801}
 802
 803static int pblk_alloc_line_meta(struct pblk *pblk, struct pblk_line *line)
 804{
 805	struct pblk_line_meta *lm = &pblk->lm;
 806
 807	line->blk_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
 808	if (!line->blk_bitmap)
 809		return -ENOMEM;
 810
 811	line->erase_bitmap = kzalloc(lm->blk_bitmap_len, GFP_KERNEL);
 812	if (!line->erase_bitmap) {
 813		kfree(line->blk_bitmap);
 814		return -ENOMEM;
 815	}
 816
 817	line->chks = kmalloc(lm->blk_per_line * sizeof(struct nvm_chk_meta),
 818								GFP_KERNEL);
 819	if (!line->chks) {
 820		kfree(line->erase_bitmap);
 821		kfree(line->blk_bitmap);
 822		return -ENOMEM;
 823	}
 
 824
 825	return 0;
 
 
 
 
 
 
 
 
 826}
 827
 828static int pblk_line_mg_init(struct pblk *pblk)
 829{
 830	struct nvm_tgt_dev *dev = pblk->dev;
 831	struct nvm_geo *geo = &dev->geo;
 832	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
 833	struct pblk_line_meta *lm = &pblk->lm;
 834	int i, bb_distance;
 835
 836	l_mg->nr_lines = geo->num_chk;
 837	l_mg->log_line = l_mg->data_line = NULL;
 838	l_mg->l_seq_nr = l_mg->d_seq_nr = 0;
 839	l_mg->nr_free_lines = 0;
 840	bitmap_zero(&l_mg->meta_bitmap, PBLK_DATA_LINES);
 841
 842	INIT_LIST_HEAD(&l_mg->free_list);
 843	INIT_LIST_HEAD(&l_mg->corrupt_list);
 844	INIT_LIST_HEAD(&l_mg->bad_list);
 845	INIT_LIST_HEAD(&l_mg->gc_full_list);
 846	INIT_LIST_HEAD(&l_mg->gc_high_list);
 847	INIT_LIST_HEAD(&l_mg->gc_mid_list);
 848	INIT_LIST_HEAD(&l_mg->gc_low_list);
 849	INIT_LIST_HEAD(&l_mg->gc_empty_list);
 
 850
 851	INIT_LIST_HEAD(&l_mg->emeta_list);
 852
 853	l_mg->gc_lists[0] = &l_mg->gc_high_list;
 854	l_mg->gc_lists[1] = &l_mg->gc_mid_list;
 855	l_mg->gc_lists[2] = &l_mg->gc_low_list;
 
 856
 857	spin_lock_init(&l_mg->free_lock);
 858	spin_lock_init(&l_mg->close_lock);
 859	spin_lock_init(&l_mg->gc_lock);
 860
 861	l_mg->vsc_list = kcalloc(l_mg->nr_lines, sizeof(__le32), GFP_KERNEL);
 862	if (!l_mg->vsc_list)
 863		goto fail;
 864
 865	l_mg->bb_template = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
 866	if (!l_mg->bb_template)
 867		goto fail_free_vsc_list;
 868
 869	l_mg->bb_aux = kzalloc(lm->sec_bitmap_len, GFP_KERNEL);
 870	if (!l_mg->bb_aux)
 871		goto fail_free_bb_template;
 872
 873	/* smeta is always small enough to fit on a kmalloc memory allocation,
 874	 * emeta depends on the number of LUNs allocated to the pblk instance
 875	 */
 876	for (i = 0; i < PBLK_DATA_LINES; i++) {
 877		l_mg->sline_meta[i] = kmalloc(lm->smeta_len, GFP_KERNEL);
 878		if (!l_mg->sline_meta[i])
 879			goto fail_free_smeta;
 880	}
 881
 
 
 
 
 
 
 
 
 
 
 
 882	/* emeta allocates three different buffers for managing metadata with
 883	 * in-memory and in-media layouts
 884	 */
 885	for (i = 0; i < PBLK_DATA_LINES; i++) {
 886		struct pblk_emeta *emeta;
 887
 888		emeta = kmalloc(sizeof(struct pblk_emeta), GFP_KERNEL);
 889		if (!emeta)
 890			goto fail_free_emeta;
 891
 892		if (lm->emeta_len[0] > KMALLOC_MAX_CACHE_SIZE) {
 893			l_mg->emeta_alloc_type = PBLK_VMALLOC_META;
 894
 895			emeta->buf = vmalloc(lm->emeta_len[0]);
 896			if (!emeta->buf) {
 897				kfree(emeta);
 898				goto fail_free_emeta;
 899			}
 900
 901			emeta->nr_entries = lm->emeta_sec[0];
 902			l_mg->eline_meta[i] = emeta;
 903		} else {
 904			l_mg->emeta_alloc_type = PBLK_KMALLOC_META;
 905
 906			emeta->buf = kmalloc(lm->emeta_len[0], GFP_KERNEL);
 907			if (!emeta->buf) {
 908				kfree(emeta);
 909				goto fail_free_emeta;
 910			}
 911
 912			emeta->nr_entries = lm->emeta_sec[0];
 913			l_mg->eline_meta[i] = emeta;
 914		}
 915	}
 916
 917	for (i = 0; i < l_mg->nr_lines; i++)
 918		l_mg->vsc_list[i] = cpu_to_le32(EMPTY_ENTRY);
 919
 920	bb_distance = (geo->all_luns) * geo->ws_opt;
 921	for (i = 0; i < lm->sec_per_line; i += bb_distance)
 922		bitmap_set(l_mg->bb_template, i, geo->ws_opt);
 923
 924	return 0;
 925
 926fail_free_emeta:
 927	while (--i >= 0) {
 928		if (l_mg->emeta_alloc_type == PBLK_VMALLOC_META)
 929			vfree(l_mg->eline_meta[i]->buf);
 930		else
 931			kfree(l_mg->eline_meta[i]->buf);
 932		kfree(l_mg->eline_meta[i]);
 933	}
 
 
 
 
 934fail_free_smeta:
 935	for (i = 0; i < PBLK_DATA_LINES; i++)
 936		kfree(l_mg->sline_meta[i]);
 937	kfree(l_mg->bb_aux);
 938fail_free_bb_template:
 939	kfree(l_mg->bb_template);
 940fail_free_vsc_list:
 941	kfree(l_mg->vsc_list);
 942fail:
 943	return -ENOMEM;
 944}
 945
 946static int pblk_line_meta_init(struct pblk *pblk)
 947{
 948	struct nvm_tgt_dev *dev = pblk->dev;
 949	struct nvm_geo *geo = &dev->geo;
 950	struct pblk_line_meta *lm = &pblk->lm;
 951	unsigned int smeta_len, emeta_len;
 952	int i;
 953
 954	lm->sec_per_line = geo->clba * geo->all_luns;
 955	lm->blk_per_line = geo->all_luns;
 956	lm->blk_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
 957	lm->sec_bitmap_len = BITS_TO_LONGS(lm->sec_per_line) * sizeof(long);
 958	lm->lun_bitmap_len = BITS_TO_LONGS(geo->all_luns) * sizeof(long);
 959	lm->mid_thrs = lm->sec_per_line / 2;
 960	lm->high_thrs = lm->sec_per_line / 4;
 961	lm->meta_distance = (geo->all_luns / 2) * pblk->min_write_pgs;
 962
 963	/* Calculate necessary pages for smeta. See comment over struct
 964	 * line_smeta definition
 965	 */
 966	i = 1;
 967add_smeta_page:
 968	lm->smeta_sec = i * geo->ws_opt;
 969	lm->smeta_len = lm->smeta_sec * geo->csecs;
 970
 971	smeta_len = sizeof(struct line_smeta) + lm->lun_bitmap_len;
 972	if (smeta_len > lm->smeta_len) {
 973		i++;
 974		goto add_smeta_page;
 975	}
 976
 977	/* Calculate necessary pages for emeta. See comment over struct
 978	 * line_emeta definition
 979	 */
 980	i = 1;
 981add_emeta_page:
 982	lm->emeta_sec[0] = i * geo->ws_opt;
 983	lm->emeta_len[0] = lm->emeta_sec[0] * geo->csecs;
 984
 985	emeta_len = calc_emeta_len(pblk);
 986	if (emeta_len > lm->emeta_len[0]) {
 987		i++;
 988		goto add_emeta_page;
 989	}
 990
 991	lm->emeta_bb = geo->all_luns > i ? geo->all_luns - i : 0;
 992
 993	lm->min_blk_line = 1;
 994	if (geo->all_luns > 1)
 995		lm->min_blk_line += DIV_ROUND_UP(lm->smeta_sec +
 996					lm->emeta_sec[0], geo->clba);
 997
 998	if (lm->min_blk_line > lm->blk_per_line) {
 999		pr_err("pblk: config. not supported. Min. LUN in line:%d\n",
1000							lm->blk_per_line);
1001		return -EINVAL;
1002	}
1003
1004	return 0;
1005}
1006
1007static int pblk_lines_init(struct pblk *pblk)
1008{
1009	struct pblk_line_mgmt *l_mg = &pblk->l_mg;
1010	struct pblk_line *line;
1011	void *chunk_meta;
1012	long nr_free_chks = 0;
1013	int i, ret;
1014
1015	ret = pblk_line_meta_init(pblk);
1016	if (ret)
1017		return ret;
1018
1019	ret = pblk_line_mg_init(pblk);
1020	if (ret)
1021		return ret;
1022
1023	ret = pblk_luns_init(pblk);
1024	if (ret)
1025		goto fail_free_meta;
1026
1027	chunk_meta = pblk_chunk_get_meta(pblk);
1028	if (IS_ERR(chunk_meta)) {
1029		ret = PTR_ERR(chunk_meta);
1030		goto fail_free_luns;
1031	}
1032
1033	pblk->lines = kcalloc(l_mg->nr_lines, sizeof(struct pblk_line),
1034								GFP_KERNEL);
1035	if (!pblk->lines) {
1036		ret = -ENOMEM;
1037		goto fail_free_chunk_meta;
1038	}
1039
1040	for (i = 0; i < l_mg->nr_lines; i++) {
1041		line = &pblk->lines[i];
1042
1043		ret = pblk_alloc_line_meta(pblk, line);
1044		if (ret)
1045			goto fail_free_lines;
1046
1047		nr_free_chks += pblk_setup_line_meta(pblk, line, chunk_meta, i);
 
 
 
1048	}
1049
1050	pblk_set_provision(pblk, nr_free_chks);
 
 
 
 
1051
1052	kfree(chunk_meta);
 
 
 
 
1053	return 0;
1054
1055fail_free_lines:
1056	while (--i >= 0)
1057		pblk_line_meta_free(&pblk->lines[i]);
1058	kfree(pblk->lines);
1059fail_free_chunk_meta:
1060	kfree(chunk_meta);
1061fail_free_luns:
1062	kfree(pblk->luns);
1063fail_free_meta:
1064	pblk_line_mg_free(pblk);
1065
1066	return ret;
1067}
1068
1069static int pblk_writer_init(struct pblk *pblk)
1070{
1071	pblk->writer_ts = kthread_create(pblk_write_ts, pblk, "pblk-writer-t");
1072	if (IS_ERR(pblk->writer_ts)) {
1073		int err = PTR_ERR(pblk->writer_ts);
1074
1075		if (err != -EINTR)
1076			pr_err("pblk: could not allocate writer kthread (%d)\n",
1077					err);
1078		return err;
1079	}
1080
1081	timer_setup(&pblk->wtimer, pblk_write_timer_fn, 0);
1082	mod_timer(&pblk->wtimer, jiffies + msecs_to_jiffies(100));
1083
1084	return 0;
1085}
1086
1087static void pblk_writer_stop(struct pblk *pblk)
1088{
1089	/* The pipeline must be stopped and the write buffer emptied before the
1090	 * write thread is stopped
1091	 */
1092	WARN(pblk_rb_read_count(&pblk->rwb),
1093			"Stopping not fully persisted write buffer\n");
1094
1095	WARN(pblk_rb_sync_count(&pblk->rwb),
1096			"Stopping not fully synced write buffer\n");
1097
1098	del_timer_sync(&pblk->wtimer);
1099	if (pblk->writer_ts)
1100		kthread_stop(pblk->writer_ts);
1101}
1102
1103static void pblk_free(struct pblk *pblk)
1104{
1105	pblk_lines_free(pblk);
1106	pblk_l2p_free(pblk);
1107	pblk_rwb_free(pblk);
1108	pblk_core_free(pblk);
1109
1110	kfree(pblk);
1111}
1112
1113static void pblk_tear_down(struct pblk *pblk)
1114{
1115	pblk_pipeline_stop(pblk);
 
 
1116	pblk_writer_stop(pblk);
1117	pblk_rb_sync_l2p(&pblk->rwb);
1118	pblk_rl_free(&pblk->rl);
1119
1120	pr_debug("pblk: consistent tear down\n");
1121}
1122
1123static void pblk_exit(void *private)
1124{
1125	struct pblk *pblk = private;
1126
1127	down_write(&pblk_lock);
1128	pblk_gc_exit(pblk);
1129	pblk_tear_down(pblk);
1130
1131#ifdef CONFIG_NVM_DEBUG
1132	pr_info("pblk exit: L2P CRC: %x\n", pblk_l2p_crc(pblk));
1133#endif
1134
1135	pblk_free(pblk);
1136	up_write(&pblk_lock);
1137}
1138
1139static sector_t pblk_capacity(void *private)
1140{
1141	struct pblk *pblk = private;
1142
1143	return pblk->capacity * NR_PHY_IN_LOG;
1144}
1145
1146static void *pblk_init(struct nvm_tgt_dev *dev, struct gendisk *tdisk,
1147		       int flags)
1148{
1149	struct nvm_geo *geo = &dev->geo;
1150	struct request_queue *bqueue = dev->q;
1151	struct request_queue *tqueue = tdisk->queue;
1152	struct pblk *pblk;
1153	int ret;
1154
1155	/* pblk supports 1.2 and 2.0 versions */
 
 
 
 
 
 
 
 
 
1156	if (!(geo->version == NVM_OCSSD_SPEC_12 ||
1157					geo->version == NVM_OCSSD_SPEC_20)) {
1158		pr_err("pblk: OCSSD version not supported (%u)\n",
1159							geo->version);
 
1160		return ERR_PTR(-EINVAL);
1161	}
1162
1163	if (geo->version == NVM_OCSSD_SPEC_12 && geo->dom & NVM_RSP_L2P) {
1164		pr_err("pblk: host-side L2P table not supported. (%x)\n",
1165							geo->dom);
1166		return ERR_PTR(-EINVAL);
1167	}
1168
1169	pblk = kzalloc(sizeof(struct pblk), GFP_KERNEL);
1170	if (!pblk)
1171		return ERR_PTR(-ENOMEM);
1172
1173	pblk->dev = dev;
1174	pblk->disk = tdisk;
1175	pblk->state = PBLK_STATE_RUNNING;
1176	pblk->gc.gc_enabled = 0;
1177
1178	spin_lock_init(&pblk->trans_lock);
1179	spin_lock_init(&pblk->lock);
1180
1181#ifdef CONFIG_NVM_DEBUG
1182	atomic_long_set(&pblk->inflight_writes, 0);
1183	atomic_long_set(&pblk->padded_writes, 0);
1184	atomic_long_set(&pblk->padded_wb, 0);
1185	atomic_long_set(&pblk->req_writes, 0);
1186	atomic_long_set(&pblk->sub_writes, 0);
1187	atomic_long_set(&pblk->sync_writes, 0);
1188	atomic_long_set(&pblk->inflight_reads, 0);
1189	atomic_long_set(&pblk->cache_reads, 0);
1190	atomic_long_set(&pblk->sync_reads, 0);
1191	atomic_long_set(&pblk->recov_writes, 0);
1192	atomic_long_set(&pblk->recov_writes, 0);
1193	atomic_long_set(&pblk->recov_gc_writes, 0);
1194	atomic_long_set(&pblk->recov_gc_reads, 0);
1195#endif
1196
1197	atomic_long_set(&pblk->read_failed, 0);
1198	atomic_long_set(&pblk->read_empty, 0);
1199	atomic_long_set(&pblk->read_high_ecc, 0);
1200	atomic_long_set(&pblk->read_failed_gc, 0);
1201	atomic_long_set(&pblk->write_failed, 0);
1202	atomic_long_set(&pblk->erase_failed, 0);
1203
1204	ret = pblk_core_init(pblk);
1205	if (ret) {
1206		pr_err("pblk: could not initialize core\n");
1207		goto fail;
1208	}
1209
1210	ret = pblk_lines_init(pblk);
1211	if (ret) {
1212		pr_err("pblk: could not initialize lines\n");
1213		goto fail_free_core;
1214	}
1215
1216	ret = pblk_rwb_init(pblk);
1217	if (ret) {
1218		pr_err("pblk: could not initialize write buffer\n");
1219		goto fail_free_lines;
1220	}
1221
1222	ret = pblk_l2p_init(pblk, flags & NVM_TARGET_FACTORY);
1223	if (ret) {
1224		pr_err("pblk: could not initialize maps\n");
1225		goto fail_free_rwb;
1226	}
1227
1228	ret = pblk_writer_init(pblk);
1229	if (ret) {
1230		if (ret != -EINTR)
1231			pr_err("pblk: could not initialize write thread\n");
1232		goto fail_free_l2p;
1233	}
1234
1235	ret = pblk_gc_init(pblk);
1236	if (ret) {
1237		pr_err("pblk: could not initialize gc\n");
1238		goto fail_stop_writer;
1239	}
1240
1241	/* inherit the size from the underlying device */
1242	blk_queue_logical_block_size(tqueue, queue_physical_block_size(bqueue));
1243	blk_queue_max_hw_sectors(tqueue, queue_max_hw_sectors(bqueue));
1244
1245	blk_queue_write_cache(tqueue, true, false);
1246
1247	tqueue->limits.discard_granularity = geo->clba * geo->csecs;
1248	tqueue->limits.discard_alignment = 0;
1249	blk_queue_max_discard_sectors(tqueue, UINT_MAX >> 9);
1250	blk_queue_flag_set(QUEUE_FLAG_DISCARD, tqueue);
1251
1252	pr_info("pblk(%s): luns:%u, lines:%d, secs:%llu, buf entries:%u\n",
1253			tdisk->disk_name,
1254			geo->all_luns, pblk->l_mg.nr_lines,
1255			(unsigned long long)pblk->rl.nr_secs,
1256			pblk->rwb.nr_entries);
1257
1258	wake_up_process(pblk->writer_ts);
1259
1260	/* Check if we need to start GC */
1261	pblk_gc_should_kick(pblk);
1262
1263	return pblk;
1264
1265fail_stop_writer:
1266	pblk_writer_stop(pblk);
1267fail_free_l2p:
1268	pblk_l2p_free(pblk);
1269fail_free_rwb:
1270	pblk_rwb_free(pblk);
1271fail_free_lines:
1272	pblk_lines_free(pblk);
1273fail_free_core:
1274	pblk_core_free(pblk);
1275fail:
1276	kfree(pblk);
1277	return ERR_PTR(ret);
1278}
1279
1280/* physical block device target */
1281static struct nvm_tgt_type tt_pblk = {
1282	.name		= "pblk",
1283	.version	= {1, 0, 0},
1284
1285	.make_rq	= pblk_make_rq,
1286	.capacity	= pblk_capacity,
1287
1288	.init		= pblk_init,
1289	.exit		= pblk_exit,
1290
1291	.sysfs_init	= pblk_sysfs_init,
1292	.sysfs_exit	= pblk_sysfs_exit,
1293	.owner		= THIS_MODULE,
1294};
1295
1296static int __init pblk_module_init(void)
1297{
1298	int ret;
1299
1300	pblk_bio_set = bioset_create(BIO_POOL_SIZE, 0, 0);
1301	if (!pblk_bio_set)
1302		return -ENOMEM;
1303	ret = nvm_register_tgt_type(&tt_pblk);
1304	if (ret)
1305		bioset_free(pblk_bio_set);
1306	return ret;
1307}
1308
1309static void pblk_module_exit(void)
1310{
1311	bioset_free(pblk_bio_set);
1312	nvm_unregister_tgt_type(&tt_pblk);
1313}
1314
1315module_init(pblk_module_init);
1316module_exit(pblk_module_exit);
1317MODULE_AUTHOR("Javier Gonzalez <javier@cnexlabs.com>");
1318MODULE_AUTHOR("Matias Bjorling <matias@cnexlabs.com>");
1319MODULE_LICENSE("GPL v2");
1320MODULE_DESCRIPTION("Physical Block-Device for Open-Channel SSDs");