gennvm.c 11.1 KB
Newer Older
M
Matias Bjørling 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
/*
 * Copyright (C) 2015 Matias Bjorling <m@bjorling.me>
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
 * USA.
 *
 * Implementation of a generic nvm manager for Open-Channel SSDs.
 */

#include "gennvm.h"

static void gennvm_blocks_free(struct nvm_dev *dev)
{
	struct gen_nvm *gn = dev->mp;
	struct gen_lun *lun;
	int i;

	gennvm_for_each_lun(gn, lun, i) {
		if (!lun->vlun.blocks)
			break;
		vfree(lun->vlun.blocks);
	}
}

static void gennvm_luns_free(struct nvm_dev *dev)
{
	struct gen_nvm *gn = dev->mp;

	kfree(gn->luns);
}

static int gennvm_luns_init(struct nvm_dev *dev, struct gen_nvm *gn)
{
	struct gen_lun *lun;
	int i;

	gn->luns = kcalloc(dev->nr_luns, sizeof(struct gen_lun), GFP_KERNEL);
	if (!gn->luns)
		return -ENOMEM;

	gennvm_for_each_lun(gn, lun, i) {
		spin_lock_init(&lun->vlun.lock);
		INIT_LIST_HEAD(&lun->free_list);
		INIT_LIST_HEAD(&lun->used_list);
		INIT_LIST_HEAD(&lun->bb_list);

		lun->reserved_blocks = 2; /* for GC only */
		lun->vlun.id = i;
		lun->vlun.lun_id = i % dev->luns_per_chnl;
		lun->vlun.chnl_id = i / dev->luns_per_chnl;
		lun->vlun.nr_free_blocks = dev->blks_per_lun;
63 64
		lun->vlun.nr_open_blocks = 0;
		lun->vlun.nr_closed_blocks = 0;
65
		lun->vlun.nr_bad_blocks = 0;
M
Matias Bjørling 已提交
66 67 68 69
	}
	return 0;
}

70
static int gennvm_block_bb(struct ppa_addr ppa, int nr_blocks, u8 *blks,
M
Matias Bjørling 已提交
71 72 73
								void *private)
{
	struct gen_nvm *gn = private;
74 75
	struct nvm_dev *dev = gn->dev;
	struct gen_lun *lun;
M
Matias Bjørling 已提交
76 77 78
	struct nvm_block *blk;
	int i;

79
	lun = &gn->luns[(dev->luns_per_chnl * ppa.g.ch) + ppa.g.lun];
80 81 82 83

	for (i = 0; i < nr_blocks; i++) {
		if (blks[i] == 0)
			continue;
M
Matias Bjørling 已提交
84 85 86 87 88 89 90 91

		blk = &lun->vlun.blocks[i];
		if (!blk) {
			pr_err("gennvm: BB data is out of bounds.\n");
			return -EINVAL;
		}

		list_move_tail(&blk->list, &lun->bb_list);
92
		lun->vlun.nr_bad_blocks++;
93
		lun->vlun.nr_free_blocks--;
M
Matias Bjørling 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	}

	return 0;
}

static int gennvm_block_map(u64 slba, u32 nlb, __le64 *entries, void *private)
{
	struct nvm_dev *dev = private;
	struct gen_nvm *gn = dev->mp;
	sector_t max_pages = dev->total_pages * (dev->sec_size >> 9);
	u64 elba = slba + nlb;
	struct gen_lun *lun;
	struct nvm_block *blk;
	u64 i;
	int lun_id;

	if (unlikely(elba > dev->total_pages)) {
		pr_err("gennvm: L2P data from device is out of bounds!\n");
		return -EINVAL;
	}

	for (i = 0; i < nlb; i++) {
		u64 pba = le64_to_cpu(entries[i]);

		if (unlikely(pba >= max_pages && pba != U64_MAX)) {
			pr_err("gennvm: L2P data entry is out of bounds!\n");
			return -EINVAL;
		}

		/* Address zero is a special one. The first page on a disk is
		 * protected. It often holds internal device boot
		 * information.
		 */
		if (!pba)
			continue;

		/* resolve block from physical address */
		lun_id = div_u64(pba, dev->sec_per_lun);
		lun = &gn->luns[lun_id];

		/* Calculate block offset into lun */
		pba = pba - (dev->sec_per_lun * lun_id);
		blk = &lun->vlun.blocks[div_u64(pba, dev->sec_per_blk)];

138
		if (!blk->state) {
M
Matias Bjørling 已提交
139 140
			/* at this point, we don't know anything about the
			 * block. It's up to the FTL on top to re-etablish the
141
			 * block state. The block is assumed to be open.
M
Matias Bjørling 已提交
142 143
			 */
			list_move_tail(&blk->list, &lun->used_list);
144
			blk->state = NVM_BLK_ST_OPEN;
M
Matias Bjørling 已提交
145
			lun->vlun.nr_free_blocks--;
146
			lun->vlun.nr_open_blocks++;
M
Matias Bjørling 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		}
	}

	return 0;
}

static int gennvm_blocks_init(struct nvm_dev *dev, struct gen_nvm *gn)
{
	struct gen_lun *lun;
	struct nvm_block *block;
	sector_t lun_iter, blk_iter, cur_block_id = 0;
	int ret;

	gennvm_for_each_lun(gn, lun, lun_iter) {
		lun->vlun.blocks = vzalloc(sizeof(struct nvm_block) *
							dev->blks_per_lun);
		if (!lun->vlun.blocks)
			return -ENOMEM;

		for (blk_iter = 0; blk_iter < dev->blks_per_lun; blk_iter++) {
			block = &lun->vlun.blocks[blk_iter];

			INIT_LIST_HEAD(&block->list);

			block->lun = &lun->vlun;
			block->id = cur_block_id++;

			/* First block is reserved for device */
175 176
			if (unlikely(lun_iter == 0 && blk_iter == 0)) {
				lun->vlun.nr_free_blocks--;
M
Matias Bjørling 已提交
177
				continue;
178
			}
M
Matias Bjørling 已提交
179 180 181 182 183

			list_add_tail(&block->list, &lun->free_list);
		}

		if (dev->ops->get_bb_tbl) {
184 185 186 187 188
			struct ppa_addr ppa;

			ppa.ppa = 0;
			ppa.g.ch = lun->vlun.chnl_id;
			ppa.g.lun = lun->vlun.id;
189
			ppa = generic_to_dev_addr(dev, ppa);
190

191
			ret = dev->ops->get_bb_tbl(dev, ppa,
192 193
						dev->blks_per_lun,
						gennvm_block_bb, gn);
M
Matias Bjørling 已提交
194 195 196 197 198 199
			if (ret)
				pr_err("gennvm: could not read BB table\n");
		}
	}

	if (dev->ops->get_l2p_tbl) {
200
		ret = dev->ops->get_l2p_tbl(dev, 0, dev->total_pages,
M
Matias Bjørling 已提交
201 202 203 204 205 206 207 208 209 210
							gennvm_block_map, dev);
		if (ret) {
			pr_err("gennvm: could not read L2P table.\n");
			pr_warn("gennvm: default block initialization");
		}
	}

	return 0;
}

211 212 213 214 215 216 217 218
static void gennvm_free(struct nvm_dev *dev)
{
	gennvm_blocks_free(dev);
	gennvm_luns_free(dev);
	kfree(dev->mp);
	dev->mp = NULL;
}

M
Matias Bjørling 已提交
219 220 221 222 223
static int gennvm_register(struct nvm_dev *dev)
{
	struct gen_nvm *gn;
	int ret;

224 225 226
	if (!try_module_get(THIS_MODULE))
		return -ENODEV;

M
Matias Bjørling 已提交
227 228 229 230
	gn = kzalloc(sizeof(struct gen_nvm), GFP_KERNEL);
	if (!gn)
		return -ENOMEM;

231
	gn->dev = dev;
M
Matias Bjørling 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	gn->nr_luns = dev->nr_luns;
	dev->mp = gn;

	ret = gennvm_luns_init(dev, gn);
	if (ret) {
		pr_err("gennvm: could not initialize luns\n");
		goto err;
	}

	ret = gennvm_blocks_init(dev, gn);
	if (ret) {
		pr_err("gennvm: could not initialize blocks\n");
		goto err;
	}

	return 1;
err:
249
	gennvm_free(dev);
250
	module_put(THIS_MODULE);
M
Matias Bjørling 已提交
251 252 253 254 255
	return ret;
}

static void gennvm_unregister(struct nvm_dev *dev)
{
256
	gennvm_free(dev);
257
	module_put(THIS_MODULE);
M
Matias Bjørling 已提交
258 259
}

260
static struct nvm_block *gennvm_get_blk_unlocked(struct nvm_dev *dev,
M
Matias Bjørling 已提交
261 262 263 264 265 266
				struct nvm_lun *vlun, unsigned long flags)
{
	struct gen_lun *lun = container_of(vlun, struct gen_lun, vlun);
	struct nvm_block *blk = NULL;
	int is_gc = flags & NVM_IOTYPE_GC;

267
	assert_spin_locked(&vlun->lock);
M
Matias Bjørling 已提交
268 269 270 271 272 273 274

	if (list_empty(&lun->free_list)) {
		pr_err_ratelimited("gennvm: lun %u have no free pages available",
								lun->vlun.id);
		goto out;
	}

275
	if (!is_gc && lun->vlun.nr_free_blocks < lun->reserved_blocks)
M
Matias Bjørling 已提交
276 277 278 279
		goto out;

	blk = list_first_entry(&lun->free_list, struct nvm_block, list);
	list_move_tail(&blk->list, &lun->used_list);
280
	blk->state = NVM_BLK_ST_OPEN;
M
Matias Bjørling 已提交
281 282

	lun->vlun.nr_free_blocks--;
283
	lun->vlun.nr_open_blocks++;
M
Matias Bjørling 已提交
284 285

out:
286 287 288 289 290 291 292 293 294 295
	return blk;
}

static struct nvm_block *gennvm_get_blk(struct nvm_dev *dev,
				struct nvm_lun *vlun, unsigned long flags)
{
	struct nvm_block *blk;

	spin_lock(&vlun->lock);
	blk = gennvm_get_blk_unlocked(dev, vlun, flags);
296
	spin_unlock(&vlun->lock);
M
Matias Bjørling 已提交
297 298 299
	return blk;
}

300
static void gennvm_put_blk_unlocked(struct nvm_dev *dev, struct nvm_block *blk)
M
Matias Bjørling 已提交
301 302 303 304
{
	struct nvm_lun *vlun = blk->lun;
	struct gen_lun *lun = container_of(vlun, struct gen_lun, vlun);

305
	assert_spin_locked(&vlun->lock);
M
Matias Bjørling 已提交
306

307
	if (blk->state & NVM_BLK_ST_OPEN) {
M
Matias Bjørling 已提交
308
		list_move_tail(&blk->list, &lun->free_list);
309
		lun->vlun.nr_open_blocks--;
M
Matias Bjørling 已提交
310
		lun->vlun.nr_free_blocks++;
311 312 313 314 315 316 317
		blk->state = NVM_BLK_ST_FREE;
	} else if (blk->state & NVM_BLK_ST_CLOSED) {
		list_move_tail(&blk->list, &lun->free_list);
		lun->vlun.nr_closed_blocks--;
		lun->vlun.nr_free_blocks++;
		blk->state = NVM_BLK_ST_FREE;
	} else if (blk->state & NVM_BLK_ST_BAD) {
M
Matias Bjørling 已提交
318
		list_move_tail(&blk->list, &lun->bb_list);
319
		lun->vlun.nr_bad_blocks++;
320 321
		blk->state = NVM_BLK_ST_BAD;
	} else {
M
Matias Bjørling 已提交
322 323
		WARN_ON_ONCE(1);
		pr_err("gennvm: erroneous block type (%lu -> %u)\n",
324
							blk->id, blk->state);
M
Matias Bjørling 已提交
325
		list_move_tail(&blk->list, &lun->bb_list);
326
		lun->vlun.nr_bad_blocks++;
327
		blk->state = NVM_BLK_ST_BAD;
M
Matias Bjørling 已提交
328
	}
329
}
M
Matias Bjørling 已提交
330

331 332 333 334 335 336
static void gennvm_put_blk(struct nvm_dev *dev, struct nvm_block *blk)
{
	struct nvm_lun *vlun = blk->lun;

	spin_lock(&vlun->lock);
	gennvm_put_blk_unlocked(dev, blk);
M
Matias Bjørling 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
	spin_unlock(&vlun->lock);
}

static void gennvm_blk_set_type(struct nvm_dev *dev, struct ppa_addr *ppa,
								int type)
{
	struct gen_nvm *gn = dev->mp;
	struct gen_lun *lun;
	struct nvm_block *blk;

	if (unlikely(ppa->g.ch > dev->nr_chnls ||
					ppa->g.lun > dev->luns_per_chnl ||
					ppa->g.blk > dev->blks_per_lun)) {
		WARN_ON_ONCE(1);
		pr_err("gennvm: ppa broken (ch: %u > %u lun: %u > %u blk: %u > %u",
				ppa->g.ch, dev->nr_chnls,
				ppa->g.lun, dev->luns_per_chnl,
				ppa->g.blk, dev->blks_per_lun);
		return;
	}

	lun = &gn->luns[ppa->g.lun * ppa->g.ch];
	blk = &lun->vlun.blocks[ppa->g.blk];

	/* will be moved to bb list on put_blk from target */
362
	blk->state = type;
M
Matias Bjørling 已提交
363 364 365 366 367 368 369
}

/* mark block bad. It is expected the target recover from the error. */
static void gennvm_mark_blk_bad(struct nvm_dev *dev, struct nvm_rq *rqd)
{
	int i;

370
	if (!dev->ops->set_bb_tbl)
M
Matias Bjørling 已提交
371 372
		return;

373
	if (dev->ops->set_bb_tbl(dev, rqd, 1))
M
Matias Bjørling 已提交
374 375
		return;

376
	nvm_addr_to_generic_mode(dev, rqd);
M
Matias Bjørling 已提交
377 378 379 380

	/* look up blocks and mark them as bad */
	if (rqd->nr_pages > 1)
		for (i = 0; i < rqd->nr_pages; i++)
381 382
			gennvm_blk_set_type(dev, &rqd->ppa_list[i],
						NVM_BLK_ST_BAD);
M
Matias Bjørling 已提交
383
	else
384
		gennvm_blk_set_type(dev, &rqd->ppa_addr, NVM_BLK_ST_BAD);
M
Matias Bjørling 已提交
385 386
}

387
static void gennvm_end_io(struct nvm_rq *rqd)
M
Matias Bjørling 已提交
388 389 390
{
	struct nvm_tgt_instance *ins = rqd->ins;

391
	switch (rqd->error) {
M
Matias Bjørling 已提交
392 393 394 395 396 397 398
	case NVM_RSP_SUCCESS:
	case NVM_RSP_ERR_EMPTYPAGE:
		break;
	case NVM_RSP_ERR_FAILWRITE:
		gennvm_mark_blk_bad(rqd->dev, rqd);
	}

399
	ins->tt->end_io(rqd);
400
}
M
Matias Bjørling 已提交
401

402 403 404 405 406 407 408 409 410 411 412
static int gennvm_submit_io(struct nvm_dev *dev, struct nvm_rq *rqd)
{
	if (!dev->ops->submit_io)
		return -ENODEV;

	/* Convert address space */
	nvm_generic_to_addr_mode(dev, rqd);

	rqd->dev = dev;
	rqd->end_io = gennvm_end_io;
	return dev->ops->submit_io(dev, rqd);
M
Matias Bjørling 已提交
413 414 415 416 417
}

static int gennvm_erase_blk(struct nvm_dev *dev, struct nvm_block *blk,
							unsigned long flags)
{
418
	struct ppa_addr addr = block_to_ppa(dev, blk);
M
Matias Bjørling 已提交
419

420
	return nvm_erase_ppa(dev, &addr, 1);
M
Matias Bjørling 已提交
421 422 423 424 425 426 427 428 429
}

static struct nvm_lun *gennvm_get_lun(struct nvm_dev *dev, int lunid)
{
	struct gen_nvm *gn = dev->mp;

	return &gn->luns[lunid].vlun;
}

430
static void gennvm_lun_info_print(struct nvm_dev *dev)
M
Matias Bjørling 已提交
431 432 433 434 435
{
	struct gen_nvm *gn = dev->mp;
	struct gen_lun *lun;
	unsigned int i;

436 437 438 439

	gennvm_for_each_lun(gn, lun, i) {
		spin_lock(&lun->vlun.lock);

440
		pr_info("%s: lun%8u\t%u\t%u\t%u\t%u\n",
441 442
				dev->name, i,
				lun->vlun.nr_free_blocks,
443 444
				lun->vlun.nr_open_blocks,
				lun->vlun.nr_closed_blocks,
445 446 447 448
				lun->vlun.nr_bad_blocks);

		spin_unlock(&lun->vlun.lock);
	}
M
Matias Bjørling 已提交
449 450 451
}

static struct nvmm_type gennvm = {
452 453 454 455 456
	.name			= "gennvm",
	.version		= {0, 1, 0},

	.register_mgr		= gennvm_register,
	.unregister_mgr		= gennvm_unregister,
M
Matias Bjørling 已提交
457

458 459
	.get_blk_unlocked	= gennvm_get_blk_unlocked,
	.put_blk_unlocked	= gennvm_put_blk_unlocked,
M
Matias Bjørling 已提交
460

461 462
	.get_blk		= gennvm_get_blk,
	.put_blk		= gennvm_put_blk,
M
Matias Bjørling 已提交
463

464 465
	.submit_io		= gennvm_submit_io,
	.erase_blk		= gennvm_erase_blk,
M
Matias Bjørling 已提交
466

467 468
	.get_lun		= gennvm_get_lun,
	.lun_info_print		= gennvm_lun_info_print,
M
Matias Bjørling 已提交
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
};

static int __init gennvm_module_init(void)
{
	return nvm_register_mgr(&gennvm);
}

static void gennvm_module_exit(void)
{
	nvm_unregister_mgr(&gennvm);
}

module_init(gennvm_module_init);
module_exit(gennvm_module_exit);
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Generic media manager for Open-Channel SSDs");