Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright © 2009 - Maxim Levitsky
   4 * SmartMedia/xD translation layer
 
 
 
 
   5 */
   6
   7#include <linux/kernel.h>
   8#include <linux/module.h>
   9#include <linux/random.h>
  10#include <linux/hdreg.h>
  11#include <linux/kthread.h>
  12#include <linux/freezer.h>
  13#include <linux/sysfs.h>
  14#include <linux/bitops.h>
  15#include <linux/slab.h>
  16#include <linux/mtd/nand-ecc-sw-hamming.h>
  17#include "nand/raw/sm_common.h"
  18#include "sm_ftl.h"
  19
  20
  21
  22static struct workqueue_struct *cache_flush_workqueue;
  23
  24static int cache_timeout = 1000;
  25module_param(cache_timeout, int, S_IRUGO);
  26MODULE_PARM_DESC(cache_timeout,
  27	"Timeout (in ms) for cache flush (1000 ms default");
  28
  29static int debug;
  30module_param(debug, int, S_IRUGO | S_IWUSR);
  31MODULE_PARM_DESC(debug, "Debug level (0-2)");
  32
  33
  34/* ------------------- sysfs attributes ---------------------------------- */
  35struct sm_sysfs_attribute {
  36	struct device_attribute dev_attr;
  37	char *data;
  38	int len;
  39};
  40
  41static ssize_t sm_attr_show(struct device *dev, struct device_attribute *attr,
  42		     char *buf)
  43{
  44	struct sm_sysfs_attribute *sm_attr =
  45		container_of(attr, struct sm_sysfs_attribute, dev_attr);
  46
  47	strncpy(buf, sm_attr->data, sm_attr->len);
  48	return sm_attr->len;
  49}
  50
  51
  52#define NUM_ATTRIBUTES 1
  53#define SM_CIS_VENDOR_OFFSET 0x59
  54static struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl)
  55{
  56	struct attribute_group *attr_group;
  57	struct attribute **attributes;
  58	struct sm_sysfs_attribute *vendor_attribute;
  59	char *vendor;
  60
  61	vendor = kstrndup(ftl->cis_buffer + SM_CIS_VENDOR_OFFSET,
  62			  SM_SMALL_PAGE - SM_CIS_VENDOR_OFFSET, GFP_KERNEL);
  63	if (!vendor)
  64		goto error1;
  65
  66	/* Initialize sysfs attributes */
  67	vendor_attribute =
  68		kzalloc(sizeof(struct sm_sysfs_attribute), GFP_KERNEL);
  69	if (!vendor_attribute)
  70		goto error2;
  71
  72	sysfs_attr_init(&vendor_attribute->dev_attr.attr);
  73
  74	vendor_attribute->data = vendor;
  75	vendor_attribute->len = strlen(vendor);
  76	vendor_attribute->dev_attr.attr.name = "vendor";
  77	vendor_attribute->dev_attr.attr.mode = S_IRUGO;
  78	vendor_attribute->dev_attr.show = sm_attr_show;
  79
  80
  81	/* Create array of pointers to the attributes */
  82	attributes = kcalloc(NUM_ATTRIBUTES + 1, sizeof(struct attribute *),
  83								GFP_KERNEL);
  84	if (!attributes)
  85		goto error3;
  86	attributes[0] = &vendor_attribute->dev_attr.attr;
  87
  88	/* Finally create the attribute group */
  89	attr_group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
  90	if (!attr_group)
  91		goto error4;
  92	attr_group->attrs = attributes;
  93	return attr_group;
  94error4:
  95	kfree(attributes);
  96error3:
  97	kfree(vendor_attribute);
  98error2:
  99	kfree(vendor);
 100error1:
 101	return NULL;
 102}
 103
 104static void sm_delete_sysfs_attributes(struct sm_ftl *ftl)
 105{
 106	struct attribute **attributes = ftl->disk_attributes->attrs;
 107	int i;
 108
 109	for (i = 0; attributes[i] ; i++) {
 110
 111		struct device_attribute *dev_attr = container_of(attributes[i],
 112			struct device_attribute, attr);
 113
 114		struct sm_sysfs_attribute *sm_attr =
 115			container_of(dev_attr,
 116				struct sm_sysfs_attribute, dev_attr);
 117
 118		kfree(sm_attr->data);
 119		kfree(sm_attr);
 120	}
 121
 122	kfree(ftl->disk_attributes->attrs);
 123	kfree(ftl->disk_attributes);
 124}
 125
 126
 127/* ----------------------- oob helpers -------------------------------------- */
 128
 129static int sm_get_lba(uint8_t *lba)
 130{
 131	/* check fixed bits */
 132	if ((lba[0] & 0xF8) != 0x10)
 133		return -2;
 134
 135	/* check parity - endianness doesn't matter */
 136	if (hweight16(*(uint16_t *)lba) & 1)
 137		return -2;
 138
 139	return (lba[1] >> 1) | ((lba[0] & 0x07) << 7);
 140}
 141
 142
 143/*
 144 * Read LBA associated with block
 145 * returns -1, if block is erased
 146 * returns -2 if error happens
 147 */
 148static int sm_read_lba(struct sm_oob *oob)
 149{
 150	static const uint32_t erased_pattern[4] = {
 151		0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
 152
 153	uint16_t lba_test;
 154	int lba;
 155
 156	/* First test for erased block */
 157	if (!memcmp(oob, erased_pattern, SM_OOB_SIZE))
 158		return -1;
 159
 160	/* Now check is both copies of the LBA differ too much */
 161	lba_test = *(uint16_t *)oob->lba_copy1 ^ *(uint16_t*)oob->lba_copy2;
 162	if (lba_test && !is_power_of_2(lba_test))
 163		return -2;
 164
 165	/* And read it */
 166	lba = sm_get_lba(oob->lba_copy1);
 167
 168	if (lba == -2)
 169		lba = sm_get_lba(oob->lba_copy2);
 170
 171	return lba;
 172}
 173
 174static void sm_write_lba(struct sm_oob *oob, uint16_t lba)
 175{
 176	uint8_t tmp[2];
 177
 178	WARN_ON(lba >= 1000);
 179
 180	tmp[0] = 0x10 | ((lba >> 7) & 0x07);
 181	tmp[1] = (lba << 1) & 0xFF;
 182
 183	if (hweight16(*(uint16_t *)tmp) & 0x01)
 184		tmp[1] |= 1;
 185
 186	oob->lba_copy1[0] = oob->lba_copy2[0] = tmp[0];
 187	oob->lba_copy1[1] = oob->lba_copy2[1] = tmp[1];
 188}
 189
 190
 191/* Make offset from parts */
 192static loff_t sm_mkoffset(struct sm_ftl *ftl, int zone, int block, int boffset)
 193{
 194	WARN_ON(boffset & (SM_SECTOR_SIZE - 1));
 195	WARN_ON(zone < 0 || zone >= ftl->zone_count);
 196	WARN_ON(block >= ftl->zone_size);
 197	WARN_ON(boffset >= ftl->block_size);
 198
 199	if (block == -1)
 200		return -1;
 201
 202	return (zone * SM_MAX_ZONE_SIZE + block) * ftl->block_size + boffset;
 203}
 204
 205/* Breaks offset into parts */
 206static void sm_break_offset(struct sm_ftl *ftl, loff_t loffset,
 207			    int *zone, int *block, int *boffset)
 208{
 209	u64 offset = loffset;
 210	*boffset = do_div(offset, ftl->block_size);
 211	*block = do_div(offset, ftl->max_lba);
 212	*zone = offset >= ftl->zone_count ? -1 : offset;
 213}
 214
 215/* ---------------------- low level IO ------------------------------------- */
 216
 217static int sm_correct_sector(uint8_t *buffer, struct sm_oob *oob)
 218{
 219	bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
 220	uint8_t ecc[3];
 221
 222	ecc_sw_hamming_calculate(buffer, SM_SMALL_PAGE, ecc, sm_order);
 223	if (ecc_sw_hamming_correct(buffer, ecc, oob->ecc1, SM_SMALL_PAGE,
 224				   sm_order) < 0)
 225		return -EIO;
 226
 227	buffer += SM_SMALL_PAGE;
 228
 229	ecc_sw_hamming_calculate(buffer, SM_SMALL_PAGE, ecc, sm_order);
 230	if (ecc_sw_hamming_correct(buffer, ecc, oob->ecc2, SM_SMALL_PAGE,
 231				   sm_order) < 0)
 232		return -EIO;
 233	return 0;
 234}
 235
 236/* Reads a sector + oob*/
 237static int sm_read_sector(struct sm_ftl *ftl,
 238			  int zone, int block, int boffset,
 239			  uint8_t *buffer, struct sm_oob *oob)
 240{
 241	struct mtd_info *mtd = ftl->trans->mtd;
 242	struct mtd_oob_ops ops = { };
 243	struct sm_oob tmp_oob;
 244	int ret = -EIO;
 245	int try = 0;
 246
 247	/* FTL can contain -1 entries that are by default filled with bits */
 248	if (block == -1) {
 249		if (buffer)
 250			memset(buffer, 0xFF, SM_SECTOR_SIZE);
 251		return 0;
 252	}
 253
 254	/* User might not need the oob, but we do for data verification */
 255	if (!oob)
 256		oob = &tmp_oob;
 257
 258	ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
 259	ops.ooboffs = 0;
 260	ops.ooblen = SM_OOB_SIZE;
 261	ops.oobbuf = (void *)oob;
 262	ops.len = SM_SECTOR_SIZE;
 263	ops.datbuf = buffer;
 264
 265again:
 266	if (try++) {
 267		/* Avoid infinite recursion on CIS reads, sm_recheck_media
 268		 * won't help anyway
 269		 */
 270		if (zone == 0 && block == ftl->cis_block && boffset ==
 271			ftl->cis_boffset)
 272			return ret;
 273
 274		/* Test if media is stable */
 275		if (try == 3 || sm_recheck_media(ftl))
 276			return ret;
 277	}
 278
 279	/* Unfortunately, oob read will _always_ succeed,
 280	 * despite card removal.....
 281	 */
 282	ret = mtd_read_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
 283
 284	/* Test for unknown errors */
 285	if (ret != 0 && !mtd_is_bitflip_or_eccerr(ret)) {
 286		dbg("read of block %d at zone %d, failed due to error (%d)",
 287			block, zone, ret);
 288		goto again;
 289	}
 290
 291	/* Do a basic test on the oob, to guard against returned garbage */
 292	if (oob->reserved != 0xFFFFFFFF && !is_power_of_2(~oob->reserved))
 293		goto again;
 294
 295	/* This should never happen, unless there is a bug in the mtd driver */
 296	WARN_ON(ops.oobretlen != SM_OOB_SIZE);
 297	WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
 298
 299	if (!buffer)
 300		return 0;
 301
 302	/* Test if sector marked as bad */
 303	if (!sm_sector_valid(oob)) {
 304		dbg("read of block %d at zone %d, failed because it is marked"
 305			" as bad" , block, zone);
 306		goto again;
 307	}
 308
 309	/* Test ECC*/
 310	if (mtd_is_eccerr(ret) ||
 311		(ftl->smallpagenand && sm_correct_sector(buffer, oob))) {
 312
 313		dbg("read of block %d at zone %d, failed due to ECC error",
 314			block, zone);
 315		goto again;
 316	}
 317
 318	return 0;
 319}
 320
 321/* Writes a sector to media */
 322static int sm_write_sector(struct sm_ftl *ftl,
 323			   int zone, int block, int boffset,
 324			   uint8_t *buffer, struct sm_oob *oob)
 325{
 326	struct mtd_oob_ops ops = { };
 327	struct mtd_info *mtd = ftl->trans->mtd;
 328	int ret;
 329
 330	BUG_ON(ftl->readonly);
 331
 332	if (zone == 0 && (block == ftl->cis_block || block == 0)) {
 333		dbg("attempted to write the CIS!");
 334		return -EIO;
 335	}
 336
 337	if (ftl->unstable)
 338		return -EIO;
 339
 340	ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
 341	ops.len = SM_SECTOR_SIZE;
 342	ops.datbuf = buffer;
 343	ops.ooboffs = 0;
 344	ops.ooblen = SM_OOB_SIZE;
 345	ops.oobbuf = (void *)oob;
 346
 347	ret = mtd_write_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
 348
 349	/* Now we assume that hardware will catch write bitflip errors */
 350
 351	if (ret) {
 352		dbg("write to block %d at zone %d, failed with error %d",
 353			block, zone, ret);
 354
 355		sm_recheck_media(ftl);
 356		return ret;
 357	}
 358
 359	/* This should never happen, unless there is a bug in the driver */
 360	WARN_ON(ops.oobretlen != SM_OOB_SIZE);
 361	WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
 362
 363	return 0;
 364}
 365
 366/* ------------------------ block IO ------------------------------------- */
 367
 368/* Write a block using data and lba, and invalid sector bitmap */
 369static int sm_write_block(struct sm_ftl *ftl, uint8_t *buf,
 370			  int zone, int block, int lba,
 371			  unsigned long invalid_bitmap)
 372{
 373	bool sm_order = IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING_SMC);
 374	struct sm_oob oob;
 375	int boffset;
 376	int retry = 0;
 377
 378	/* Initialize the oob with requested values */
 379	memset(&oob, 0xFF, SM_OOB_SIZE);
 380	sm_write_lba(&oob, lba);
 381restart:
 382	if (ftl->unstable)
 383		return -EIO;
 384
 385	for (boffset = 0; boffset < ftl->block_size;
 386				boffset += SM_SECTOR_SIZE) {
 387
 388		oob.data_status = 0xFF;
 389
 390		if (test_bit(boffset / SM_SECTOR_SIZE, &invalid_bitmap)) {
 391
 392			sm_printk("sector %d of block at LBA %d of zone %d"
 393				" couldn't be read, marking it as invalid",
 394				boffset / SM_SECTOR_SIZE, lba, zone);
 395
 396			oob.data_status = 0;
 397		}
 398
 399		if (ftl->smallpagenand) {
 400			ecc_sw_hamming_calculate(buf + boffset,
 401						 SM_SMALL_PAGE, oob.ecc1,
 402						 sm_order);
 403
 404			ecc_sw_hamming_calculate(buf + boffset + SM_SMALL_PAGE,
 405						 SM_SMALL_PAGE, oob.ecc2,
 406						 sm_order);
 407		}
 408		if (!sm_write_sector(ftl, zone, block, boffset,
 409							buf + boffset, &oob))
 410			continue;
 411
 412		if (!retry) {
 413
 414			/* If write fails. try to erase the block */
 415			/* This is safe, because we never write in blocks
 416			 * that contain valuable data.
 417			 * This is intended to repair block that are marked
 418			 * as erased, but that isn't fully erased
 419			 */
 420
 421			if (sm_erase_block(ftl, zone, block, 0))
 422				return -EIO;
 423
 424			retry = 1;
 425			goto restart;
 426		} else {
 427			sm_mark_block_bad(ftl, zone, block);
 428			return -EIO;
 429		}
 430	}
 431	return 0;
 432}
 433
 434
 435/* Mark whole block at offset 'offs' as bad. */
 436static void sm_mark_block_bad(struct sm_ftl *ftl, int zone, int block)
 437{
 438	struct sm_oob oob;
 439	int boffset;
 440
 441	memset(&oob, 0xFF, SM_OOB_SIZE);
 442	oob.block_status = 0xF0;
 443
 444	if (ftl->unstable)
 445		return;
 446
 447	if (sm_recheck_media(ftl))
 448		return;
 449
 450	sm_printk("marking block %d of zone %d as bad", block, zone);
 451
 452	/* We aren't checking the return value, because we don't care */
 453	/* This also fails on fake xD cards, but I guess these won't expose
 454	 * any bad blocks till fail completely
 455	 */
 456	for (boffset = 0; boffset < ftl->block_size; boffset += SM_SECTOR_SIZE)
 457		sm_write_sector(ftl, zone, block, boffset, NULL, &oob);
 458}
 459
 460/*
 461 * Erase a block within a zone
 462 * If erase succeeds, it updates free block fifo, otherwise marks block as bad
 463 */
 464static int sm_erase_block(struct sm_ftl *ftl, int zone_num, uint16_t block,
 465			  int put_free)
 466{
 467	struct ftl_zone *zone = &ftl->zones[zone_num];
 468	struct mtd_info *mtd = ftl->trans->mtd;
 469	struct erase_info erase;
 470
 471	erase.addr = sm_mkoffset(ftl, zone_num, block, 0);
 472	erase.len = ftl->block_size;
 473
 474	if (ftl->unstable)
 475		return -EIO;
 476
 477	BUG_ON(ftl->readonly);
 478
 479	if (zone_num == 0 && (block == ftl->cis_block || block == 0)) {
 480		sm_printk("attempted to erase the CIS!");
 481		return -EIO;
 482	}
 483
 484	if (mtd_erase(mtd, &erase)) {
 485		sm_printk("erase of block %d in zone %d failed",
 486							block, zone_num);
 487		goto error;
 488	}
 489
 490	if (put_free)
 491		kfifo_in(&zone->free_sectors,
 492			(const unsigned char *)&block, sizeof(block));
 493
 494	return 0;
 495error:
 496	sm_mark_block_bad(ftl, zone_num, block);
 497	return -EIO;
 498}
 499
 500/* Thoroughly test that block is valid. */
 501static int sm_check_block(struct sm_ftl *ftl, int zone, int block)
 502{
 503	int boffset;
 504	struct sm_oob oob;
 505	int lbas[] = { -3, 0, 0, 0 };
 506	int i = 0;
 507	int test_lba;
 508
 509
 510	/* First just check that block doesn't look fishy */
 511	/* Only blocks that are valid or are sliced in two parts, are
 512	 * accepted
 513	 */
 514	for (boffset = 0; boffset < ftl->block_size;
 515					boffset += SM_SECTOR_SIZE) {
 516
 517		/* This shouldn't happen anyway */
 518		if (sm_read_sector(ftl, zone, block, boffset, NULL, &oob))
 519			return -2;
 520
 521		test_lba = sm_read_lba(&oob);
 522
 523		if (lbas[i] != test_lba)
 524			lbas[++i] = test_lba;
 525
 526		/* If we found three different LBAs, something is fishy */
 527		if (i == 3)
 528			return -EIO;
 529	}
 530
 531	/* If the block is sliced (partially erased usually) erase it */
 532	if (i == 2) {
 533		sm_erase_block(ftl, zone, block, 1);
 534		return 1;
 535	}
 536
 537	return 0;
 538}
 539
 540/* ----------------- media scanning --------------------------------- */
 541static const struct chs_entry chs_table[] = {
 542	{ 1,    125,  4,  4  },
 543	{ 2,    125,  4,  8  },
 544	{ 4,    250,  4,  8  },
 545	{ 8,    250,  4,  16 },
 546	{ 16,   500,  4,  16 },
 547	{ 32,   500,  8,  16 },
 548	{ 64,   500,  8,  32 },
 549	{ 128,  500,  16, 32 },
 550	{ 256,  1000, 16, 32 },
 551	{ 512,  1015, 32, 63 },
 552	{ 1024, 985,  33, 63 },
 553	{ 2048, 985,  33, 63 },
 554	{ 0 },
 555};
 556
 557
 558static const uint8_t cis_signature[] = {
 559	0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
 560};
 561/* Find out media parameters.
 562 * This ideally has to be based on nand id, but for now device size is enough
 563 */
 564static int sm_get_media_info(struct sm_ftl *ftl, struct mtd_info *mtd)
 565{
 566	int i;
 567	int size_in_megs = mtd->size / (1024 * 1024);
 568
 569	ftl->readonly = mtd->type == MTD_ROM;
 570
 571	/* Manual settings for very old devices */
 572	ftl->zone_count = 1;
 573	ftl->smallpagenand = 0;
 574
 575	switch (size_in_megs) {
 576	case 1:
 577		/* 1 MiB flash/rom SmartMedia card (256 byte pages)*/
 578		ftl->zone_size = 256;
 579		ftl->max_lba = 250;
 580		ftl->block_size = 8 * SM_SECTOR_SIZE;
 581		ftl->smallpagenand = 1;
 582
 583		break;
 584	case 2:
 585		/* 2 MiB flash SmartMedia (256 byte pages)*/
 586		if (mtd->writesize == SM_SMALL_PAGE) {
 587			ftl->zone_size = 512;
 588			ftl->max_lba = 500;
 589			ftl->block_size = 8 * SM_SECTOR_SIZE;
 590			ftl->smallpagenand = 1;
 591		/* 2 MiB rom SmartMedia */
 592		} else {
 593
 594			if (!ftl->readonly)
 595				return -ENODEV;
 596
 597			ftl->zone_size = 256;
 598			ftl->max_lba = 250;
 599			ftl->block_size = 16 * SM_SECTOR_SIZE;
 600		}
 601		break;
 602	case 4:
 603		/* 4 MiB flash/rom SmartMedia device */
 604		ftl->zone_size = 512;
 605		ftl->max_lba = 500;
 606		ftl->block_size = 16 * SM_SECTOR_SIZE;
 607		break;
 608	case 8:
 609		/* 8 MiB flash/rom SmartMedia device */
 610		ftl->zone_size = 1024;
 611		ftl->max_lba = 1000;
 612		ftl->block_size = 16 * SM_SECTOR_SIZE;
 613	}
 614
 615	/* Minimum xD size is 16MiB. Also, all xD cards have standard zone
 616	 * sizes. SmartMedia cards exist up to 128 MiB and have same layout
 617	 */
 618	if (size_in_megs >= 16) {
 619		ftl->zone_count = size_in_megs / 16;
 620		ftl->zone_size = 1024;
 621		ftl->max_lba = 1000;
 622		ftl->block_size = 32 * SM_SECTOR_SIZE;
 623	}
 624
 625	/* Test for proper write,erase and oob sizes */
 626	if (mtd->erasesize > ftl->block_size)
 627		return -ENODEV;
 628
 629	if (mtd->writesize > SM_SECTOR_SIZE)
 630		return -ENODEV;
 631
 632	if (ftl->smallpagenand && mtd->oobsize < SM_SMALL_OOB_SIZE)
 633		return -ENODEV;
 634
 635	if (!ftl->smallpagenand && mtd->oobsize < SM_OOB_SIZE)
 636		return -ENODEV;
 637
 638	/* We use OOB */
 639	if (!mtd_has_oob(mtd))
 640		return -ENODEV;
 641
 642	/* Find geometry information */
 643	for (i = 0 ; i < ARRAY_SIZE(chs_table) ; i++) {
 644		if (chs_table[i].size == size_in_megs) {
 645			ftl->cylinders = chs_table[i].cyl;
 646			ftl->heads = chs_table[i].head;
 647			ftl->sectors = chs_table[i].sec;
 648			return 0;
 649		}
 650	}
 651
 652	sm_printk("media has unknown size : %dMiB", size_in_megs);
 653	ftl->cylinders = 985;
 654	ftl->heads =  33;
 655	ftl->sectors = 63;
 656	return 0;
 657}
 658
 659/* Validate the CIS */
 660static int sm_read_cis(struct sm_ftl *ftl)
 661{
 662	struct sm_oob oob;
 663
 664	if (sm_read_sector(ftl,
 665		0, ftl->cis_block, ftl->cis_boffset, ftl->cis_buffer, &oob))
 666			return -EIO;
 667
 668	if (!sm_sector_valid(&oob) || !sm_block_valid(&oob))
 669		return -EIO;
 670
 671	if (!memcmp(ftl->cis_buffer + ftl->cis_page_offset,
 672			cis_signature, sizeof(cis_signature))) {
 673		return 0;
 674	}
 675
 676	return -EIO;
 677}
 678
 679/* Scan the media for the CIS */
 680static int sm_find_cis(struct sm_ftl *ftl)
 681{
 682	struct sm_oob oob;
 683	int block, boffset;
 684	int block_found = 0;
 685	int cis_found = 0;
 686
 687	/* Search for first valid block */
 688	for (block = 0 ; block < ftl->zone_size - ftl->max_lba ; block++) {
 689
 690		if (sm_read_sector(ftl, 0, block, 0, NULL, &oob))
 691			continue;
 692
 693		if (!sm_block_valid(&oob))
 694			continue;
 695		block_found = 1;
 696		break;
 697	}
 698
 699	if (!block_found)
 700		return -EIO;
 701
 702	/* Search for first valid sector in this block */
 703	for (boffset = 0 ; boffset < ftl->block_size;
 704						boffset += SM_SECTOR_SIZE) {
 705
 706		if (sm_read_sector(ftl, 0, block, boffset, NULL, &oob))
 707			continue;
 708
 709		if (!sm_sector_valid(&oob))
 710			continue;
 711		break;
 712	}
 713
 714	if (boffset == ftl->block_size)
 715		return -EIO;
 716
 717	ftl->cis_block = block;
 718	ftl->cis_boffset = boffset;
 719	ftl->cis_page_offset = 0;
 720
 721	cis_found = !sm_read_cis(ftl);
 722
 723	if (!cis_found) {
 724		ftl->cis_page_offset = SM_SMALL_PAGE;
 725		cis_found = !sm_read_cis(ftl);
 726	}
 727
 728	if (cis_found) {
 729		dbg("CIS block found at offset %x",
 730			block * ftl->block_size +
 731				boffset + ftl->cis_page_offset);
 732		return 0;
 733	}
 734	return -EIO;
 735}
 736
 737/* Basic test to determine if underlying mtd device if functional */
 738static int sm_recheck_media(struct sm_ftl *ftl)
 739{
 740	if (sm_read_cis(ftl)) {
 741
 742		if (!ftl->unstable) {
 743			sm_printk("media unstable, not allowing writes");
 744			ftl->unstable = 1;
 745		}
 746		return -EIO;
 747	}
 748	return 0;
 749}
 750
 751/* Initialize a FTL zone */
 752static int sm_init_zone(struct sm_ftl *ftl, int zone_num)
 753{
 754	struct ftl_zone *zone = &ftl->zones[zone_num];
 755	struct sm_oob oob;
 756	uint16_t block;
 757	int lba;
 758	int i = 0;
 759	int len;
 760
 761	dbg("initializing zone %d", zone_num);
 762
 763	/* Allocate memory for FTL table */
 764	zone->lba_to_phys_table = kmalloc_array(ftl->max_lba, 2, GFP_KERNEL);
 765
 766	if (!zone->lba_to_phys_table)
 767		return -ENOMEM;
 768	memset(zone->lba_to_phys_table, -1, ftl->max_lba * 2);
 769
 770
 771	/* Allocate memory for free sectors FIFO */
 772	if (kfifo_alloc(&zone->free_sectors, ftl->zone_size * 2, GFP_KERNEL)) {
 773		kfree(zone->lba_to_phys_table);
 774		return -ENOMEM;
 775	}
 776
 777	/* Now scan the zone */
 778	for (block = 0 ; block < ftl->zone_size ; block++) {
 779
 780		/* Skip blocks till the CIS (including) */
 781		if (zone_num == 0 && block <= ftl->cis_block)
 782			continue;
 783
 784		/* Read the oob of first sector */
 785		if (sm_read_sector(ftl, zone_num, block, 0, NULL, &oob)) {
 786			kfifo_free(&zone->free_sectors);
 787			kfree(zone->lba_to_phys_table);
 788			return -EIO;
 789		}
 790
 791		/* Test to see if block is erased. It is enough to test
 792		 * first sector, because erase happens in one shot
 793		 */
 794		if (sm_block_erased(&oob)) {
 795			kfifo_in(&zone->free_sectors,
 796				(unsigned char *)&block, 2);
 797			continue;
 798		}
 799
 800		/* If block is marked as bad, skip it */
 801		/* This assumes we can trust first sector*/
 802		/* However the way the block valid status is defined, ensures
 803		 * very low probability of failure here
 804		 */
 805		if (!sm_block_valid(&oob)) {
 806			dbg("PH %04d <-> <marked bad>", block);
 807			continue;
 808		}
 809
 810
 811		lba = sm_read_lba(&oob);
 812
 813		/* Invalid LBA means that block is damaged. */
 814		/* We can try to erase it, or mark it as bad, but
 815		 * lets leave that to recovery application
 816		 */
 817		if (lba == -2 || lba >= ftl->max_lba) {
 818			dbg("PH %04d <-> LBA %04d(bad)", block, lba);
 819			continue;
 820		}
 821
 822
 823		/* If there is no collision,
 824		 * just put the sector in the FTL table
 825		 */
 826		if (zone->lba_to_phys_table[lba] < 0) {
 827			dbg_verbose("PH %04d <-> LBA %04d", block, lba);
 828			zone->lba_to_phys_table[lba] = block;
 829			continue;
 830		}
 831
 832		sm_printk("collision"
 833			" of LBA %d between blocks %d and %d in zone %d",
 834			lba, zone->lba_to_phys_table[lba], block, zone_num);
 835
 836		/* Test that this block is valid*/
 837		if (sm_check_block(ftl, zone_num, block))
 838			continue;
 839
 840		/* Test now the old block */
 841		if (sm_check_block(ftl, zone_num,
 842					zone->lba_to_phys_table[lba])) {
 843			zone->lba_to_phys_table[lba] = block;
 844			continue;
 845		}
 846
 847		/* If both blocks are valid and share same LBA, it means that
 848		 * they hold different versions of same data. It not
 849		 * known which is more recent, thus just erase one of them
 850		 */
 851		sm_printk("both blocks are valid, erasing the later");
 852		sm_erase_block(ftl, zone_num, block, 1);
 853	}
 854
 855	dbg("zone initialized");
 856	zone->initialized = 1;
 857
 858	/* No free sectors, means that the zone is heavily damaged, write won't
 859	 * work, but it can still can be (partially) read
 860	 */
 861	if (!kfifo_len(&zone->free_sectors)) {
 862		sm_printk("no free blocks in zone %d", zone_num);
 863		return 0;
 864	}
 865
 866	/* Randomize first block we write to */
 867	get_random_bytes(&i, 2);
 868	i %= (kfifo_len(&zone->free_sectors) / 2);
 869
 870	while (i--) {
 871		len = kfifo_out(&zone->free_sectors,
 872					(unsigned char *)&block, 2);
 873		WARN_ON(len != 2);
 874		kfifo_in(&zone->free_sectors, (const unsigned char *)&block, 2);
 875	}
 876	return 0;
 877}
 878
 879/* Get and automatically initialize an FTL mapping for one zone */
 880static struct ftl_zone *sm_get_zone(struct sm_ftl *ftl, int zone_num)
 881{
 882	struct ftl_zone *zone;
 883	int error;
 884
 885	BUG_ON(zone_num >= ftl->zone_count);
 886	zone = &ftl->zones[zone_num];
 887
 888	if (!zone->initialized) {
 889		error = sm_init_zone(ftl, zone_num);
 890
 891		if (error)
 892			return ERR_PTR(error);
 893	}
 894	return zone;
 895}
 896
 897
 898/* ----------------- cache handling ------------------------------------------*/
 899
 900/* Initialize the one block cache */
 901static void sm_cache_init(struct sm_ftl *ftl)
 902{
 903	ftl->cache_data_invalid_bitmap = 0xFFFFFFFF;
 904	ftl->cache_clean = 1;
 905	ftl->cache_zone = -1;
 906	ftl->cache_block = -1;
 907	/*memset(ftl->cache_data, 0xAA, ftl->block_size);*/
 908}
 909
 910/* Put sector in one block cache */
 911static void sm_cache_put(struct sm_ftl *ftl, char *buffer, int boffset)
 912{
 913	memcpy(ftl->cache_data + boffset, buffer, SM_SECTOR_SIZE);
 914	clear_bit(boffset / SM_SECTOR_SIZE, &ftl->cache_data_invalid_bitmap);
 915	ftl->cache_clean = 0;
 916}
 917
 918/* Read a sector from the cache */
 919static int sm_cache_get(struct sm_ftl *ftl, char *buffer, int boffset)
 920{
 921	if (test_bit(boffset / SM_SECTOR_SIZE,
 922		&ftl->cache_data_invalid_bitmap))
 923			return -1;
 924
 925	memcpy(buffer, ftl->cache_data + boffset, SM_SECTOR_SIZE);
 926	return 0;
 927}
 928
 929/* Write the cache to hardware */
 930static int sm_cache_flush(struct sm_ftl *ftl)
 931{
 932	struct ftl_zone *zone;
 933
 934	int sector_num;
 935	uint16_t write_sector;
 936	int zone_num = ftl->cache_zone;
 937	int block_num;
 938
 939	if (ftl->cache_clean)
 940		return 0;
 941
 942	if (ftl->unstable)
 943		return -EIO;
 944
 945	BUG_ON(zone_num < 0);
 946	zone = &ftl->zones[zone_num];
 947	block_num = zone->lba_to_phys_table[ftl->cache_block];
 948
 949
 950	/* Try to read all unread areas of the cache block*/
 951	for_each_set_bit(sector_num, &ftl->cache_data_invalid_bitmap,
 952		ftl->block_size / SM_SECTOR_SIZE) {
 953
 954		if (!sm_read_sector(ftl,
 955			zone_num, block_num, sector_num * SM_SECTOR_SIZE,
 956			ftl->cache_data + sector_num * SM_SECTOR_SIZE, NULL))
 957				clear_bit(sector_num,
 958					&ftl->cache_data_invalid_bitmap);
 959	}
 960restart:
 961
 962	if (ftl->unstable)
 963		return -EIO;
 964
 965	/* If there are no spare blocks, */
 966	/* we could still continue by erasing/writing the current block,
 967	 * but for such worn out media it doesn't worth the trouble,
 968	 * and the dangers
 969	 */
 970	if (kfifo_out(&zone->free_sectors,
 971				(unsigned char *)&write_sector, 2) != 2) {
 972		dbg("no free sectors for write!");
 973		return -EIO;
 974	}
 975
 976
 977	if (sm_write_block(ftl, ftl->cache_data, zone_num, write_sector,
 978		ftl->cache_block, ftl->cache_data_invalid_bitmap))
 979			goto restart;
 980
 981	/* Update the FTL table */
 982	zone->lba_to_phys_table[ftl->cache_block] = write_sector;
 983
 984	/* Write successful, so erase and free the old block */
 985	if (block_num > 0)
 986		sm_erase_block(ftl, zone_num, block_num, 1);
 987
 988	sm_cache_init(ftl);
 989	return 0;
 990}
 991
 992
 993/* flush timer, runs a second after last write */
 994static void sm_cache_flush_timer(struct timer_list *t)
 995{
 996	struct sm_ftl *ftl = from_timer(ftl, t, timer);
 997	queue_work(cache_flush_workqueue, &ftl->flush_work);
 998}
 999
1000/* cache flush work, kicked by timer */
1001static void sm_cache_flush_work(struct work_struct *work)
1002{
1003	struct sm_ftl *ftl = container_of(work, struct sm_ftl, flush_work);
1004	mutex_lock(&ftl->mutex);
1005	sm_cache_flush(ftl);
1006	mutex_unlock(&ftl->mutex);
1007	return;
1008}
1009
1010/* ---------------- outside interface -------------------------------------- */
1011
1012/* outside interface: read a sector */
1013static int sm_read(struct mtd_blktrans_dev *dev,
1014		   unsigned long sect_no, char *buf)
1015{
1016	struct sm_ftl *ftl = dev->priv;
1017	struct ftl_zone *zone;
1018	int error = 0, in_cache = 0;
1019	int zone_num, block, boffset;
1020
1021	sm_break_offset(ftl, sect_no << 9, &zone_num, &block, &boffset);
1022	mutex_lock(&ftl->mutex);
1023
1024
1025	zone = sm_get_zone(ftl, zone_num);
1026	if (IS_ERR(zone)) {
1027		error = PTR_ERR(zone);
1028		goto unlock;
1029	}
1030
1031	/* Have to look at cache first */
1032	if (ftl->cache_zone == zone_num && ftl->cache_block == block) {
1033		in_cache = 1;
1034		if (!sm_cache_get(ftl, buf, boffset))
1035			goto unlock;
1036	}
1037
1038	/* Translate the block and return if doesn't exist in the table */
1039	block = zone->lba_to_phys_table[block];
1040
1041	if (block == -1) {
1042		memset(buf, 0xFF, SM_SECTOR_SIZE);
1043		goto unlock;
1044	}
1045
1046	if (sm_read_sector(ftl, zone_num, block, boffset, buf, NULL)) {
1047		error = -EIO;
1048		goto unlock;
1049	}
1050
1051	if (in_cache)
1052		sm_cache_put(ftl, buf, boffset);
1053unlock:
1054	mutex_unlock(&ftl->mutex);
1055	return error;
1056}
1057
1058/* outside interface: write a sector */
1059static int sm_write(struct mtd_blktrans_dev *dev,
1060				unsigned long sec_no, char *buf)
1061{
1062	struct sm_ftl *ftl = dev->priv;
1063	struct ftl_zone *zone;
1064	int error = 0, zone_num, block, boffset;
1065
1066	BUG_ON(ftl->readonly);
1067	sm_break_offset(ftl, sec_no << 9, &zone_num, &block, &boffset);
1068
1069	/* No need in flush thread running now */
1070	del_timer(&ftl->timer);
1071	mutex_lock(&ftl->mutex);
1072
1073	zone = sm_get_zone(ftl, zone_num);
1074	if (IS_ERR(zone)) {
1075		error = PTR_ERR(zone);
1076		goto unlock;
1077	}
1078
1079	/* If entry is not in cache, flush it */
1080	if (ftl->cache_block != block || ftl->cache_zone != zone_num) {
1081
1082		error = sm_cache_flush(ftl);
1083		if (error)
1084			goto unlock;
1085
1086		ftl->cache_block = block;
1087		ftl->cache_zone = zone_num;
1088	}
1089
1090	sm_cache_put(ftl, buf, boffset);
1091unlock:
1092	mod_timer(&ftl->timer, jiffies + msecs_to_jiffies(cache_timeout));
1093	mutex_unlock(&ftl->mutex);
1094	return error;
1095}
1096
1097/* outside interface: flush everything */
1098static int sm_flush(struct mtd_blktrans_dev *dev)
1099{
1100	struct sm_ftl *ftl = dev->priv;
1101	int retval;
1102
1103	mutex_lock(&ftl->mutex);
1104	retval =  sm_cache_flush(ftl);
1105	mutex_unlock(&ftl->mutex);
1106	return retval;
1107}
1108
1109/* outside interface: device is released */
1110static void sm_release(struct mtd_blktrans_dev *dev)
1111{
1112	struct sm_ftl *ftl = dev->priv;
1113
 
1114	del_timer_sync(&ftl->timer);
1115	cancel_work_sync(&ftl->flush_work);
1116	mutex_lock(&ftl->mutex);
1117	sm_cache_flush(ftl);
1118	mutex_unlock(&ftl->mutex);
1119}
1120
1121/* outside interface: get geometry */
1122static int sm_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
1123{
1124	struct sm_ftl *ftl = dev->priv;
1125	geo->heads = ftl->heads;
1126	geo->sectors = ftl->sectors;
1127	geo->cylinders = ftl->cylinders;
1128	return 0;
1129}
1130
1131/* external interface: main initialization function */
1132static void sm_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
1133{
1134	struct mtd_blktrans_dev *trans;
1135	struct sm_ftl *ftl;
1136
1137	/* Allocate & initialize our private structure */
1138	ftl = kzalloc(sizeof(struct sm_ftl), GFP_KERNEL);
1139	if (!ftl)
1140		goto error1;
1141
1142
1143	mutex_init(&ftl->mutex);
1144	timer_setup(&ftl->timer, sm_cache_flush_timer, 0);
1145	INIT_WORK(&ftl->flush_work, sm_cache_flush_work);
1146
1147	/* Read media information */
1148	if (sm_get_media_info(ftl, mtd)) {
1149		dbg("found unsupported mtd device, aborting");
1150		goto error2;
1151	}
1152
1153
1154	/* Allocate temporary CIS buffer for read retry support */
1155	ftl->cis_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
1156	if (!ftl->cis_buffer)
1157		goto error2;
1158
1159	/* Allocate zone array, it will be initialized on demand */
1160	ftl->zones = kcalloc(ftl->zone_count, sizeof(struct ftl_zone),
1161								GFP_KERNEL);
1162	if (!ftl->zones)
1163		goto error3;
1164
1165	/* Allocate the cache*/
1166	ftl->cache_data = kzalloc(ftl->block_size, GFP_KERNEL);
1167
1168	if (!ftl->cache_data)
1169		goto error4;
1170
1171	sm_cache_init(ftl);
1172
1173
1174	/* Allocate upper layer structure and initialize it */
1175	trans = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL);
1176	if (!trans)
1177		goto error5;
1178
1179	ftl->trans = trans;
1180	trans->priv = ftl;
1181
1182	trans->tr = tr;
1183	trans->mtd = mtd;
1184	trans->devnum = -1;
1185	trans->size = (ftl->block_size * ftl->max_lba * ftl->zone_count) >> 9;
1186	trans->readonly = ftl->readonly;
1187
1188	if (sm_find_cis(ftl)) {
1189		dbg("CIS not found on mtd device, aborting");
1190		goto error6;
1191	}
1192
1193	ftl->disk_attributes = sm_create_sysfs_attributes(ftl);
1194	if (!ftl->disk_attributes)
1195		goto error6;
1196	trans->disk_attributes = ftl->disk_attributes;
1197
1198	sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d",
1199		(int)(mtd->size / (1024 * 1024)), mtd->index);
1200
1201	dbg("FTL layout:");
1202	dbg("%d zone(s), each consists of %d blocks (+%d spares)",
1203		ftl->zone_count, ftl->max_lba,
1204		ftl->zone_size - ftl->max_lba);
1205	dbg("each block consists of %d bytes",
1206		ftl->block_size);
1207
1208
1209	/* Register device*/
1210	if (add_mtd_blktrans_dev(trans)) {
1211		dbg("error in mtdblktrans layer");
1212		goto error6;
1213	}
1214	return;
1215error6:
1216	kfree(trans);
1217error5:
1218	kfree(ftl->cache_data);
1219error4:
1220	kfree(ftl->zones);
1221error3:
1222	kfree(ftl->cis_buffer);
1223error2:
1224	kfree(ftl);
1225error1:
1226	return;
1227}
1228
1229/* main interface: device {surprise,} removal */
1230static void sm_remove_dev(struct mtd_blktrans_dev *dev)
1231{
1232	struct sm_ftl *ftl = dev->priv;
1233	int i;
1234
1235	del_mtd_blktrans_dev(dev);
1236	ftl->trans = NULL;
1237
1238	for (i = 0 ; i < ftl->zone_count; i++) {
1239
1240		if (!ftl->zones[i].initialized)
1241			continue;
1242
1243		kfree(ftl->zones[i].lba_to_phys_table);
1244		kfifo_free(&ftl->zones[i].free_sectors);
1245	}
1246
1247	sm_delete_sysfs_attributes(ftl);
1248	kfree(ftl->cis_buffer);
1249	kfree(ftl->zones);
1250	kfree(ftl->cache_data);
1251	kfree(ftl);
1252}
1253
1254static struct mtd_blktrans_ops sm_ftl_ops = {
1255	.name		= "smblk",
1256	.major		= 0,
1257	.part_bits	= SM_FTL_PARTN_BITS,
1258	.blksize	= SM_SECTOR_SIZE,
1259	.getgeo		= sm_getgeo,
1260
1261	.add_mtd	= sm_add_mtd,
1262	.remove_dev	= sm_remove_dev,
1263
1264	.readsect	= sm_read,
1265	.writesect	= sm_write,
1266
1267	.flush		= sm_flush,
1268	.release	= sm_release,
1269
1270	.owner		= THIS_MODULE,
1271};
1272
1273static __init int sm_module_init(void)
1274{
1275	int error = 0;
1276
1277	cache_flush_workqueue = create_freezable_workqueue("smflush");
1278	if (!cache_flush_workqueue)
1279		return -ENOMEM;
1280
1281	error = register_mtd_blktrans(&sm_ftl_ops);
1282	if (error)
1283		destroy_workqueue(cache_flush_workqueue);
1284	return error;
1285
1286}
1287
1288static void __exit sm_module_exit(void)
1289{
1290	destroy_workqueue(cache_flush_workqueue);
1291	deregister_mtd_blktrans(&sm_ftl_ops);
1292}
1293
1294module_init(sm_module_init);
1295module_exit(sm_module_exit);
1296
1297MODULE_LICENSE("GPL");
1298MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1299MODULE_DESCRIPTION("Smartmedia/xD mtd translation layer");
v4.17
 
   1/*
   2 * Copyright © 2009 - Maxim Levitsky
   3 * SmartMedia/xD translation layer
   4 *
   5 * This program is free software; you can redistribute it and/or modify
   6 * it under the terms of the GNU General Public License version 2 as
   7 * published by the Free Software Foundation.
   8 */
   9
  10#include <linux/kernel.h>
  11#include <linux/module.h>
  12#include <linux/random.h>
  13#include <linux/hdreg.h>
  14#include <linux/kthread.h>
  15#include <linux/freezer.h>
  16#include <linux/sysfs.h>
  17#include <linux/bitops.h>
  18#include <linux/slab.h>
  19#include <linux/mtd/nand_ecc.h>
  20#include "nand/raw/sm_common.h"
  21#include "sm_ftl.h"
  22
  23
  24
  25static struct workqueue_struct *cache_flush_workqueue;
  26
  27static int cache_timeout = 1000;
  28module_param(cache_timeout, int, S_IRUGO);
  29MODULE_PARM_DESC(cache_timeout,
  30	"Timeout (in ms) for cache flush (1000 ms default");
  31
  32static int debug;
  33module_param(debug, int, S_IRUGO | S_IWUSR);
  34MODULE_PARM_DESC(debug, "Debug level (0-2)");
  35
  36
  37/* ------------------- sysfs attributes ---------------------------------- */
  38struct sm_sysfs_attribute {
  39	struct device_attribute dev_attr;
  40	char *data;
  41	int len;
  42};
  43
  44static ssize_t sm_attr_show(struct device *dev, struct device_attribute *attr,
  45		     char *buf)
  46{
  47	struct sm_sysfs_attribute *sm_attr =
  48		container_of(attr, struct sm_sysfs_attribute, dev_attr);
  49
  50	strncpy(buf, sm_attr->data, sm_attr->len);
  51	return sm_attr->len;
  52}
  53
  54
  55#define NUM_ATTRIBUTES 1
  56#define SM_CIS_VENDOR_OFFSET 0x59
  57static struct attribute_group *sm_create_sysfs_attributes(struct sm_ftl *ftl)
  58{
  59	struct attribute_group *attr_group;
  60	struct attribute **attributes;
  61	struct sm_sysfs_attribute *vendor_attribute;
  62	char *vendor;
  63
  64	vendor = kstrndup(ftl->cis_buffer + SM_CIS_VENDOR_OFFSET,
  65			  SM_SMALL_PAGE - SM_CIS_VENDOR_OFFSET, GFP_KERNEL);
  66	if (!vendor)
  67		goto error1;
  68
  69	/* Initialize sysfs attributes */
  70	vendor_attribute =
  71		kzalloc(sizeof(struct sm_sysfs_attribute), GFP_KERNEL);
  72	if (!vendor_attribute)
  73		goto error2;
  74
  75	sysfs_attr_init(&vendor_attribute->dev_attr.attr);
  76
  77	vendor_attribute->data = vendor;
  78	vendor_attribute->len = strlen(vendor);
  79	vendor_attribute->dev_attr.attr.name = "vendor";
  80	vendor_attribute->dev_attr.attr.mode = S_IRUGO;
  81	vendor_attribute->dev_attr.show = sm_attr_show;
  82
  83
  84	/* Create array of pointers to the attributes */
  85	attributes = kzalloc(sizeof(struct attribute *) * (NUM_ATTRIBUTES + 1),
  86								GFP_KERNEL);
  87	if (!attributes)
  88		goto error3;
  89	attributes[0] = &vendor_attribute->dev_attr.attr;
  90
  91	/* Finally create the attribute group */
  92	attr_group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
  93	if (!attr_group)
  94		goto error4;
  95	attr_group->attrs = attributes;
  96	return attr_group;
  97error4:
  98	kfree(attributes);
  99error3:
 100	kfree(vendor_attribute);
 101error2:
 102	kfree(vendor);
 103error1:
 104	return NULL;
 105}
 106
 107static void sm_delete_sysfs_attributes(struct sm_ftl *ftl)
 108{
 109	struct attribute **attributes = ftl->disk_attributes->attrs;
 110	int i;
 111
 112	for (i = 0; attributes[i] ; i++) {
 113
 114		struct device_attribute *dev_attr = container_of(attributes[i],
 115			struct device_attribute, attr);
 116
 117		struct sm_sysfs_attribute *sm_attr =
 118			container_of(dev_attr,
 119				struct sm_sysfs_attribute, dev_attr);
 120
 121		kfree(sm_attr->data);
 122		kfree(sm_attr);
 123	}
 124
 125	kfree(ftl->disk_attributes->attrs);
 126	kfree(ftl->disk_attributes);
 127}
 128
 129
 130/* ----------------------- oob helpers -------------------------------------- */
 131
 132static int sm_get_lba(uint8_t *lba)
 133{
 134	/* check fixed bits */
 135	if ((lba[0] & 0xF8) != 0x10)
 136		return -2;
 137
 138	/* check parity - endianness doesn't matter */
 139	if (hweight16(*(uint16_t *)lba) & 1)
 140		return -2;
 141
 142	return (lba[1] >> 1) | ((lba[0] & 0x07) << 7);
 143}
 144
 145
 146/*
 147 * Read LBA associated with block
 148 * returns -1, if block is erased
 149 * returns -2 if error happens
 150 */
 151static int sm_read_lba(struct sm_oob *oob)
 152{
 153	static const uint32_t erased_pattern[4] = {
 154		0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
 155
 156	uint16_t lba_test;
 157	int lba;
 158
 159	/* First test for erased block */
 160	if (!memcmp(oob, erased_pattern, SM_OOB_SIZE))
 161		return -1;
 162
 163	/* Now check is both copies of the LBA differ too much */
 164	lba_test = *(uint16_t *)oob->lba_copy1 ^ *(uint16_t*)oob->lba_copy2;
 165	if (lba_test && !is_power_of_2(lba_test))
 166		return -2;
 167
 168	/* And read it */
 169	lba = sm_get_lba(oob->lba_copy1);
 170
 171	if (lba == -2)
 172		lba = sm_get_lba(oob->lba_copy2);
 173
 174	return lba;
 175}
 176
 177static void sm_write_lba(struct sm_oob *oob, uint16_t lba)
 178{
 179	uint8_t tmp[2];
 180
 181	WARN_ON(lba >= 1000);
 182
 183	tmp[0] = 0x10 | ((lba >> 7) & 0x07);
 184	tmp[1] = (lba << 1) & 0xFF;
 185
 186	if (hweight16(*(uint16_t *)tmp) & 0x01)
 187		tmp[1] |= 1;
 188
 189	oob->lba_copy1[0] = oob->lba_copy2[0] = tmp[0];
 190	oob->lba_copy1[1] = oob->lba_copy2[1] = tmp[1];
 191}
 192
 193
 194/* Make offset from parts */
 195static loff_t sm_mkoffset(struct sm_ftl *ftl, int zone, int block, int boffset)
 196{
 197	WARN_ON(boffset & (SM_SECTOR_SIZE - 1));
 198	WARN_ON(zone < 0 || zone >= ftl->zone_count);
 199	WARN_ON(block >= ftl->zone_size);
 200	WARN_ON(boffset >= ftl->block_size);
 201
 202	if (block == -1)
 203		return -1;
 204
 205	return (zone * SM_MAX_ZONE_SIZE + block) * ftl->block_size + boffset;
 206}
 207
 208/* Breaks offset into parts */
 209static void sm_break_offset(struct sm_ftl *ftl, loff_t loffset,
 210			    int *zone, int *block, int *boffset)
 211{
 212	u64 offset = loffset;
 213	*boffset = do_div(offset, ftl->block_size);
 214	*block = do_div(offset, ftl->max_lba);
 215	*zone = offset >= ftl->zone_count ? -1 : offset;
 216}
 217
 218/* ---------------------- low level IO ------------------------------------- */
 219
 220static int sm_correct_sector(uint8_t *buffer, struct sm_oob *oob)
 221{
 
 222	uint8_t ecc[3];
 223
 224	__nand_calculate_ecc(buffer, SM_SMALL_PAGE, ecc);
 225	if (__nand_correct_data(buffer, ecc, oob->ecc1, SM_SMALL_PAGE) < 0)
 
 226		return -EIO;
 227
 228	buffer += SM_SMALL_PAGE;
 229
 230	__nand_calculate_ecc(buffer, SM_SMALL_PAGE, ecc);
 231	if (__nand_correct_data(buffer, ecc, oob->ecc2, SM_SMALL_PAGE) < 0)
 
 232		return -EIO;
 233	return 0;
 234}
 235
 236/* Reads a sector + oob*/
 237static int sm_read_sector(struct sm_ftl *ftl,
 238			  int zone, int block, int boffset,
 239			  uint8_t *buffer, struct sm_oob *oob)
 240{
 241	struct mtd_info *mtd = ftl->trans->mtd;
 242	struct mtd_oob_ops ops;
 243	struct sm_oob tmp_oob;
 244	int ret = -EIO;
 245	int try = 0;
 246
 247	/* FTL can contain -1 entries that are by default filled with bits */
 248	if (block == -1) {
 249		memset(buffer, 0xFF, SM_SECTOR_SIZE);
 
 250		return 0;
 251	}
 252
 253	/* User might not need the oob, but we do for data verification */
 254	if (!oob)
 255		oob = &tmp_oob;
 256
 257	ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
 258	ops.ooboffs = 0;
 259	ops.ooblen = SM_OOB_SIZE;
 260	ops.oobbuf = (void *)oob;
 261	ops.len = SM_SECTOR_SIZE;
 262	ops.datbuf = buffer;
 263
 264again:
 265	if (try++) {
 266		/* Avoid infinite recursion on CIS reads, sm_recheck_media
 267			won't help anyway */
 
 268		if (zone == 0 && block == ftl->cis_block && boffset ==
 269			ftl->cis_boffset)
 270			return ret;
 271
 272		/* Test if media is stable */
 273		if (try == 3 || sm_recheck_media(ftl))
 274			return ret;
 275	}
 276
 277	/* Unfortunately, oob read will _always_ succeed,
 278		despite card removal..... */
 
 279	ret = mtd_read_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
 280
 281	/* Test for unknown errors */
 282	if (ret != 0 && !mtd_is_bitflip_or_eccerr(ret)) {
 283		dbg("read of block %d at zone %d, failed due to error (%d)",
 284			block, zone, ret);
 285		goto again;
 286	}
 287
 288	/* Do a basic test on the oob, to guard against returned garbage */
 289	if (oob->reserved != 0xFFFFFFFF && !is_power_of_2(~oob->reserved))
 290		goto again;
 291
 292	/* This should never happen, unless there is a bug in the mtd driver */
 293	WARN_ON(ops.oobretlen != SM_OOB_SIZE);
 294	WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
 295
 296	if (!buffer)
 297		return 0;
 298
 299	/* Test if sector marked as bad */
 300	if (!sm_sector_valid(oob)) {
 301		dbg("read of block %d at zone %d, failed because it is marked"
 302			" as bad" , block, zone);
 303		goto again;
 304	}
 305
 306	/* Test ECC*/
 307	if (mtd_is_eccerr(ret) ||
 308		(ftl->smallpagenand && sm_correct_sector(buffer, oob))) {
 309
 310		dbg("read of block %d at zone %d, failed due to ECC error",
 311			block, zone);
 312		goto again;
 313	}
 314
 315	return 0;
 316}
 317
 318/* Writes a sector to media */
 319static int sm_write_sector(struct sm_ftl *ftl,
 320			   int zone, int block, int boffset,
 321			   uint8_t *buffer, struct sm_oob *oob)
 322{
 323	struct mtd_oob_ops ops;
 324	struct mtd_info *mtd = ftl->trans->mtd;
 325	int ret;
 326
 327	BUG_ON(ftl->readonly);
 328
 329	if (zone == 0 && (block == ftl->cis_block || block == 0)) {
 330		dbg("attempted to write the CIS!");
 331		return -EIO;
 332	}
 333
 334	if (ftl->unstable)
 335		return -EIO;
 336
 337	ops.mode = ftl->smallpagenand ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
 338	ops.len = SM_SECTOR_SIZE;
 339	ops.datbuf = buffer;
 340	ops.ooboffs = 0;
 341	ops.ooblen = SM_OOB_SIZE;
 342	ops.oobbuf = (void *)oob;
 343
 344	ret = mtd_write_oob(mtd, sm_mkoffset(ftl, zone, block, boffset), &ops);
 345
 346	/* Now we assume that hardware will catch write bitflip errors */
 347
 348	if (ret) {
 349		dbg("write to block %d at zone %d, failed with error %d",
 350			block, zone, ret);
 351
 352		sm_recheck_media(ftl);
 353		return ret;
 354	}
 355
 356	/* This should never happen, unless there is a bug in the driver */
 357	WARN_ON(ops.oobretlen != SM_OOB_SIZE);
 358	WARN_ON(buffer && ops.retlen != SM_SECTOR_SIZE);
 359
 360	return 0;
 361}
 362
 363/* ------------------------ block IO ------------------------------------- */
 364
 365/* Write a block using data and lba, and invalid sector bitmap */
 366static int sm_write_block(struct sm_ftl *ftl, uint8_t *buf,
 367			  int zone, int block, int lba,
 368			  unsigned long invalid_bitmap)
 369{
 
 370	struct sm_oob oob;
 371	int boffset;
 372	int retry = 0;
 373
 374	/* Initialize the oob with requested values */
 375	memset(&oob, 0xFF, SM_OOB_SIZE);
 376	sm_write_lba(&oob, lba);
 377restart:
 378	if (ftl->unstable)
 379		return -EIO;
 380
 381	for (boffset = 0; boffset < ftl->block_size;
 382				boffset += SM_SECTOR_SIZE) {
 383
 384		oob.data_status = 0xFF;
 385
 386		if (test_bit(boffset / SM_SECTOR_SIZE, &invalid_bitmap)) {
 387
 388			sm_printk("sector %d of block at LBA %d of zone %d"
 389				" couldn't be read, marking it as invalid",
 390				boffset / SM_SECTOR_SIZE, lba, zone);
 391
 392			oob.data_status = 0;
 393		}
 394
 395		if (ftl->smallpagenand) {
 396			__nand_calculate_ecc(buf + boffset,
 397						SM_SMALL_PAGE, oob.ecc1);
 398
 399			__nand_calculate_ecc(buf + boffset + SM_SMALL_PAGE,
 400						SM_SMALL_PAGE, oob.ecc2);
 
 
 401		}
 402		if (!sm_write_sector(ftl, zone, block, boffset,
 403							buf + boffset, &oob))
 404			continue;
 405
 406		if (!retry) {
 407
 408			/* If write fails. try to erase the block */
 409			/* This is safe, because we never write in blocks
 410				that contain valuable data.
 411			This is intended to repair block that are marked
 412			as erased, but that isn't fully erased*/
 
 413
 414			if (sm_erase_block(ftl, zone, block, 0))
 415				return -EIO;
 416
 417			retry = 1;
 418			goto restart;
 419		} else {
 420			sm_mark_block_bad(ftl, zone, block);
 421			return -EIO;
 422		}
 423	}
 424	return 0;
 425}
 426
 427
 428/* Mark whole block at offset 'offs' as bad. */
 429static void sm_mark_block_bad(struct sm_ftl *ftl, int zone, int block)
 430{
 431	struct sm_oob oob;
 432	int boffset;
 433
 434	memset(&oob, 0xFF, SM_OOB_SIZE);
 435	oob.block_status = 0xF0;
 436
 437	if (ftl->unstable)
 438		return;
 439
 440	if (sm_recheck_media(ftl))
 441		return;
 442
 443	sm_printk("marking block %d of zone %d as bad", block, zone);
 444
 445	/* We aren't checking the return value, because we don't care */
 446	/* This also fails on fake xD cards, but I guess these won't expose
 447		any bad blocks till fail completely */
 
 448	for (boffset = 0; boffset < ftl->block_size; boffset += SM_SECTOR_SIZE)
 449		sm_write_sector(ftl, zone, block, boffset, NULL, &oob);
 450}
 451
 452/*
 453 * Erase a block within a zone
 454 * If erase succeeds, it updates free block fifo, otherwise marks block as bad
 455 */
 456static int sm_erase_block(struct sm_ftl *ftl, int zone_num, uint16_t block,
 457			  int put_free)
 458{
 459	struct ftl_zone *zone = &ftl->zones[zone_num];
 460	struct mtd_info *mtd = ftl->trans->mtd;
 461	struct erase_info erase;
 462
 463	erase.addr = sm_mkoffset(ftl, zone_num, block, 0);
 464	erase.len = ftl->block_size;
 465
 466	if (ftl->unstable)
 467		return -EIO;
 468
 469	BUG_ON(ftl->readonly);
 470
 471	if (zone_num == 0 && (block == ftl->cis_block || block == 0)) {
 472		sm_printk("attempted to erase the CIS!");
 473		return -EIO;
 474	}
 475
 476	if (mtd_erase(mtd, &erase)) {
 477		sm_printk("erase of block %d in zone %d failed",
 478							block, zone_num);
 479		goto error;
 480	}
 481
 482	if (put_free)
 483		kfifo_in(&zone->free_sectors,
 484			(const unsigned char *)&block, sizeof(block));
 485
 486	return 0;
 487error:
 488	sm_mark_block_bad(ftl, zone_num, block);
 489	return -EIO;
 490}
 491
 492/* Thoroughly test that block is valid. */
 493static int sm_check_block(struct sm_ftl *ftl, int zone, int block)
 494{
 495	int boffset;
 496	struct sm_oob oob;
 497	int lbas[] = { -3, 0, 0, 0 };
 498	int i = 0;
 499	int test_lba;
 500
 501
 502	/* First just check that block doesn't look fishy */
 503	/* Only blocks that are valid or are sliced in two parts, are
 504		accepted */
 
 505	for (boffset = 0; boffset < ftl->block_size;
 506					boffset += SM_SECTOR_SIZE) {
 507
 508		/* This shouldn't happen anyway */
 509		if (sm_read_sector(ftl, zone, block, boffset, NULL, &oob))
 510			return -2;
 511
 512		test_lba = sm_read_lba(&oob);
 513
 514		if (lbas[i] != test_lba)
 515			lbas[++i] = test_lba;
 516
 517		/* If we found three different LBAs, something is fishy */
 518		if (i == 3)
 519			return -EIO;
 520	}
 521
 522	/* If the block is sliced (partially erased usually) erase it */
 523	if (i == 2) {
 524		sm_erase_block(ftl, zone, block, 1);
 525		return 1;
 526	}
 527
 528	return 0;
 529}
 530
 531/* ----------------- media scanning --------------------------------- */
 532static const struct chs_entry chs_table[] = {
 533	{ 1,    125,  4,  4  },
 534	{ 2,    125,  4,  8  },
 535	{ 4,    250,  4,  8  },
 536	{ 8,    250,  4,  16 },
 537	{ 16,   500,  4,  16 },
 538	{ 32,   500,  8,  16 },
 539	{ 64,   500,  8,  32 },
 540	{ 128,  500,  16, 32 },
 541	{ 256,  1000, 16, 32 },
 542	{ 512,  1015, 32, 63 },
 543	{ 1024, 985,  33, 63 },
 544	{ 2048, 985,  33, 63 },
 545	{ 0 },
 546};
 547
 548
 549static const uint8_t cis_signature[] = {
 550	0x01, 0x03, 0xD9, 0x01, 0xFF, 0x18, 0x02, 0xDF, 0x01, 0x20
 551};
 552/* Find out media parameters.
 553 * This ideally has to be based on nand id, but for now device size is enough */
 
 554static int sm_get_media_info(struct sm_ftl *ftl, struct mtd_info *mtd)
 555{
 556	int i;
 557	int size_in_megs = mtd->size / (1024 * 1024);
 558
 559	ftl->readonly = mtd->type == MTD_ROM;
 560
 561	/* Manual settings for very old devices */
 562	ftl->zone_count = 1;
 563	ftl->smallpagenand = 0;
 564
 565	switch (size_in_megs) {
 566	case 1:
 567		/* 1 MiB flash/rom SmartMedia card (256 byte pages)*/
 568		ftl->zone_size = 256;
 569		ftl->max_lba = 250;
 570		ftl->block_size = 8 * SM_SECTOR_SIZE;
 571		ftl->smallpagenand = 1;
 572
 573		break;
 574	case 2:
 575		/* 2 MiB flash SmartMedia (256 byte pages)*/
 576		if (mtd->writesize == SM_SMALL_PAGE) {
 577			ftl->zone_size = 512;
 578			ftl->max_lba = 500;
 579			ftl->block_size = 8 * SM_SECTOR_SIZE;
 580			ftl->smallpagenand = 1;
 581		/* 2 MiB rom SmartMedia */
 582		} else {
 583
 584			if (!ftl->readonly)
 585				return -ENODEV;
 586
 587			ftl->zone_size = 256;
 588			ftl->max_lba = 250;
 589			ftl->block_size = 16 * SM_SECTOR_SIZE;
 590		}
 591		break;
 592	case 4:
 593		/* 4 MiB flash/rom SmartMedia device */
 594		ftl->zone_size = 512;
 595		ftl->max_lba = 500;
 596		ftl->block_size = 16 * SM_SECTOR_SIZE;
 597		break;
 598	case 8:
 599		/* 8 MiB flash/rom SmartMedia device */
 600		ftl->zone_size = 1024;
 601		ftl->max_lba = 1000;
 602		ftl->block_size = 16 * SM_SECTOR_SIZE;
 603	}
 604
 605	/* Minimum xD size is 16MiB. Also, all xD cards have standard zone
 606	   sizes. SmartMedia cards exist up to 128 MiB and have same layout*/
 
 607	if (size_in_megs >= 16) {
 608		ftl->zone_count = size_in_megs / 16;
 609		ftl->zone_size = 1024;
 610		ftl->max_lba = 1000;
 611		ftl->block_size = 32 * SM_SECTOR_SIZE;
 612	}
 613
 614	/* Test for proper write,erase and oob sizes */
 615	if (mtd->erasesize > ftl->block_size)
 616		return -ENODEV;
 617
 618	if (mtd->writesize > SM_SECTOR_SIZE)
 619		return -ENODEV;
 620
 621	if (ftl->smallpagenand && mtd->oobsize < SM_SMALL_OOB_SIZE)
 622		return -ENODEV;
 623
 624	if (!ftl->smallpagenand && mtd->oobsize < SM_OOB_SIZE)
 625		return -ENODEV;
 626
 627	/* We use OOB */
 628	if (!mtd_has_oob(mtd))
 629		return -ENODEV;
 630
 631	/* Find geometry information */
 632	for (i = 0 ; i < ARRAY_SIZE(chs_table) ; i++) {
 633		if (chs_table[i].size == size_in_megs) {
 634			ftl->cylinders = chs_table[i].cyl;
 635			ftl->heads = chs_table[i].head;
 636			ftl->sectors = chs_table[i].sec;
 637			return 0;
 638		}
 639	}
 640
 641	sm_printk("media has unknown size : %dMiB", size_in_megs);
 642	ftl->cylinders = 985;
 643	ftl->heads =  33;
 644	ftl->sectors = 63;
 645	return 0;
 646}
 647
 648/* Validate the CIS */
 649static int sm_read_cis(struct sm_ftl *ftl)
 650{
 651	struct sm_oob oob;
 652
 653	if (sm_read_sector(ftl,
 654		0, ftl->cis_block, ftl->cis_boffset, ftl->cis_buffer, &oob))
 655			return -EIO;
 656
 657	if (!sm_sector_valid(&oob) || !sm_block_valid(&oob))
 658		return -EIO;
 659
 660	if (!memcmp(ftl->cis_buffer + ftl->cis_page_offset,
 661			cis_signature, sizeof(cis_signature))) {
 662		return 0;
 663	}
 664
 665	return -EIO;
 666}
 667
 668/* Scan the media for the CIS */
 669static int sm_find_cis(struct sm_ftl *ftl)
 670{
 671	struct sm_oob oob;
 672	int block, boffset;
 673	int block_found = 0;
 674	int cis_found = 0;
 675
 676	/* Search for first valid block */
 677	for (block = 0 ; block < ftl->zone_size - ftl->max_lba ; block++) {
 678
 679		if (sm_read_sector(ftl, 0, block, 0, NULL, &oob))
 680			continue;
 681
 682		if (!sm_block_valid(&oob))
 683			continue;
 684		block_found = 1;
 685		break;
 686	}
 687
 688	if (!block_found)
 689		return -EIO;
 690
 691	/* Search for first valid sector in this block */
 692	for (boffset = 0 ; boffset < ftl->block_size;
 693						boffset += SM_SECTOR_SIZE) {
 694
 695		if (sm_read_sector(ftl, 0, block, boffset, NULL, &oob))
 696			continue;
 697
 698		if (!sm_sector_valid(&oob))
 699			continue;
 700		break;
 701	}
 702
 703	if (boffset == ftl->block_size)
 704		return -EIO;
 705
 706	ftl->cis_block = block;
 707	ftl->cis_boffset = boffset;
 708	ftl->cis_page_offset = 0;
 709
 710	cis_found = !sm_read_cis(ftl);
 711
 712	if (!cis_found) {
 713		ftl->cis_page_offset = SM_SMALL_PAGE;
 714		cis_found = !sm_read_cis(ftl);
 715	}
 716
 717	if (cis_found) {
 718		dbg("CIS block found at offset %x",
 719			block * ftl->block_size +
 720				boffset + ftl->cis_page_offset);
 721		return 0;
 722	}
 723	return -EIO;
 724}
 725
 726/* Basic test to determine if underlying mtd device if functional */
 727static int sm_recheck_media(struct sm_ftl *ftl)
 728{
 729	if (sm_read_cis(ftl)) {
 730
 731		if (!ftl->unstable) {
 732			sm_printk("media unstable, not allowing writes");
 733			ftl->unstable = 1;
 734		}
 735		return -EIO;
 736	}
 737	return 0;
 738}
 739
 740/* Initialize a FTL zone */
 741static int sm_init_zone(struct sm_ftl *ftl, int zone_num)
 742{
 743	struct ftl_zone *zone = &ftl->zones[zone_num];
 744	struct sm_oob oob;
 745	uint16_t block;
 746	int lba;
 747	int i = 0;
 748	int len;
 749
 750	dbg("initializing zone %d", zone_num);
 751
 752	/* Allocate memory for FTL table */
 753	zone->lba_to_phys_table = kmalloc(ftl->max_lba * 2, GFP_KERNEL);
 754
 755	if (!zone->lba_to_phys_table)
 756		return -ENOMEM;
 757	memset(zone->lba_to_phys_table, -1, ftl->max_lba * 2);
 758
 759
 760	/* Allocate memory for free sectors FIFO */
 761	if (kfifo_alloc(&zone->free_sectors, ftl->zone_size * 2, GFP_KERNEL)) {
 762		kfree(zone->lba_to_phys_table);
 763		return -ENOMEM;
 764	}
 765
 766	/* Now scan the zone */
 767	for (block = 0 ; block < ftl->zone_size ; block++) {
 768
 769		/* Skip blocks till the CIS (including) */
 770		if (zone_num == 0 && block <= ftl->cis_block)
 771			continue;
 772
 773		/* Read the oob of first sector */
 774		if (sm_read_sector(ftl, zone_num, block, 0, NULL, &oob))
 
 
 775			return -EIO;
 
 776
 777		/* Test to see if block is erased. It is enough to test
 778			first sector, because erase happens in one shot */
 
 779		if (sm_block_erased(&oob)) {
 780			kfifo_in(&zone->free_sectors,
 781				(unsigned char *)&block, 2);
 782			continue;
 783		}
 784
 785		/* If block is marked as bad, skip it */
 786		/* This assumes we can trust first sector*/
 787		/* However the way the block valid status is defined, ensures
 788			very low probability of failure here */
 
 789		if (!sm_block_valid(&oob)) {
 790			dbg("PH %04d <-> <marked bad>", block);
 791			continue;
 792		}
 793
 794
 795		lba = sm_read_lba(&oob);
 796
 797		/* Invalid LBA means that block is damaged. */
 798		/* We can try to erase it, or mark it as bad, but
 799			lets leave that to recovery application */
 
 800		if (lba == -2 || lba >= ftl->max_lba) {
 801			dbg("PH %04d <-> LBA %04d(bad)", block, lba);
 802			continue;
 803		}
 804
 805
 806		/* If there is no collision,
 807			just put the sector in the FTL table */
 
 808		if (zone->lba_to_phys_table[lba] < 0) {
 809			dbg_verbose("PH %04d <-> LBA %04d", block, lba);
 810			zone->lba_to_phys_table[lba] = block;
 811			continue;
 812		}
 813
 814		sm_printk("collision"
 815			" of LBA %d between blocks %d and %d in zone %d",
 816			lba, zone->lba_to_phys_table[lba], block, zone_num);
 817
 818		/* Test that this block is valid*/
 819		if (sm_check_block(ftl, zone_num, block))
 820			continue;
 821
 822		/* Test now the old block */
 823		if (sm_check_block(ftl, zone_num,
 824					zone->lba_to_phys_table[lba])) {
 825			zone->lba_to_phys_table[lba] = block;
 826			continue;
 827		}
 828
 829		/* If both blocks are valid and share same LBA, it means that
 830			they hold different versions of same data. It not
 831			known which is more recent, thus just erase one of them
 832		*/
 833		sm_printk("both blocks are valid, erasing the later");
 834		sm_erase_block(ftl, zone_num, block, 1);
 835	}
 836
 837	dbg("zone initialized");
 838	zone->initialized = 1;
 839
 840	/* No free sectors, means that the zone is heavily damaged, write won't
 841		work, but it can still can be (partially) read */
 
 842	if (!kfifo_len(&zone->free_sectors)) {
 843		sm_printk("no free blocks in zone %d", zone_num);
 844		return 0;
 845	}
 846
 847	/* Randomize first block we write to */
 848	get_random_bytes(&i, 2);
 849	i %= (kfifo_len(&zone->free_sectors) / 2);
 850
 851	while (i--) {
 852		len = kfifo_out(&zone->free_sectors,
 853					(unsigned char *)&block, 2);
 854		WARN_ON(len != 2);
 855		kfifo_in(&zone->free_sectors, (const unsigned char *)&block, 2);
 856	}
 857	return 0;
 858}
 859
 860/* Get and automatically initialize an FTL mapping for one zone */
 861static struct ftl_zone *sm_get_zone(struct sm_ftl *ftl, int zone_num)
 862{
 863	struct ftl_zone *zone;
 864	int error;
 865
 866	BUG_ON(zone_num >= ftl->zone_count);
 867	zone = &ftl->zones[zone_num];
 868
 869	if (!zone->initialized) {
 870		error = sm_init_zone(ftl, zone_num);
 871
 872		if (error)
 873			return ERR_PTR(error);
 874	}
 875	return zone;
 876}
 877
 878
 879/* ----------------- cache handling ------------------------------------------*/
 880
 881/* Initialize the one block cache */
 882static void sm_cache_init(struct sm_ftl *ftl)
 883{
 884	ftl->cache_data_invalid_bitmap = 0xFFFFFFFF;
 885	ftl->cache_clean = 1;
 886	ftl->cache_zone = -1;
 887	ftl->cache_block = -1;
 888	/*memset(ftl->cache_data, 0xAA, ftl->block_size);*/
 889}
 890
 891/* Put sector in one block cache */
 892static void sm_cache_put(struct sm_ftl *ftl, char *buffer, int boffset)
 893{
 894	memcpy(ftl->cache_data + boffset, buffer, SM_SECTOR_SIZE);
 895	clear_bit(boffset / SM_SECTOR_SIZE, &ftl->cache_data_invalid_bitmap);
 896	ftl->cache_clean = 0;
 897}
 898
 899/* Read a sector from the cache */
 900static int sm_cache_get(struct sm_ftl *ftl, char *buffer, int boffset)
 901{
 902	if (test_bit(boffset / SM_SECTOR_SIZE,
 903		&ftl->cache_data_invalid_bitmap))
 904			return -1;
 905
 906	memcpy(buffer, ftl->cache_data + boffset, SM_SECTOR_SIZE);
 907	return 0;
 908}
 909
 910/* Write the cache to hardware */
 911static int sm_cache_flush(struct sm_ftl *ftl)
 912{
 913	struct ftl_zone *zone;
 914
 915	int sector_num;
 916	uint16_t write_sector;
 917	int zone_num = ftl->cache_zone;
 918	int block_num;
 919
 920	if (ftl->cache_clean)
 921		return 0;
 922
 923	if (ftl->unstable)
 924		return -EIO;
 925
 926	BUG_ON(zone_num < 0);
 927	zone = &ftl->zones[zone_num];
 928	block_num = zone->lba_to_phys_table[ftl->cache_block];
 929
 930
 931	/* Try to read all unread areas of the cache block*/
 932	for_each_set_bit(sector_num, &ftl->cache_data_invalid_bitmap,
 933		ftl->block_size / SM_SECTOR_SIZE) {
 934
 935		if (!sm_read_sector(ftl,
 936			zone_num, block_num, sector_num * SM_SECTOR_SIZE,
 937			ftl->cache_data + sector_num * SM_SECTOR_SIZE, NULL))
 938				clear_bit(sector_num,
 939					&ftl->cache_data_invalid_bitmap);
 940	}
 941restart:
 942
 943	if (ftl->unstable)
 944		return -EIO;
 945
 946	/* If there are no spare blocks, */
 947	/* we could still continue by erasing/writing the current block,
 948		but for such worn out media it doesn't worth the trouble,
 949			and the dangers */
 
 950	if (kfifo_out(&zone->free_sectors,
 951				(unsigned char *)&write_sector, 2) != 2) {
 952		dbg("no free sectors for write!");
 953		return -EIO;
 954	}
 955
 956
 957	if (sm_write_block(ftl, ftl->cache_data, zone_num, write_sector,
 958		ftl->cache_block, ftl->cache_data_invalid_bitmap))
 959			goto restart;
 960
 961	/* Update the FTL table */
 962	zone->lba_to_phys_table[ftl->cache_block] = write_sector;
 963
 964	/* Write succesfull, so erase and free the old block */
 965	if (block_num > 0)
 966		sm_erase_block(ftl, zone_num, block_num, 1);
 967
 968	sm_cache_init(ftl);
 969	return 0;
 970}
 971
 972
 973/* flush timer, runs a second after last write */
 974static void sm_cache_flush_timer(struct timer_list *t)
 975{
 976	struct sm_ftl *ftl = from_timer(ftl, t, timer);
 977	queue_work(cache_flush_workqueue, &ftl->flush_work);
 978}
 979
 980/* cache flush work, kicked by timer */
 981static void sm_cache_flush_work(struct work_struct *work)
 982{
 983	struct sm_ftl *ftl = container_of(work, struct sm_ftl, flush_work);
 984	mutex_lock(&ftl->mutex);
 985	sm_cache_flush(ftl);
 986	mutex_unlock(&ftl->mutex);
 987	return;
 988}
 989
 990/* ---------------- outside interface -------------------------------------- */
 991
 992/* outside interface: read a sector */
 993static int sm_read(struct mtd_blktrans_dev *dev,
 994		   unsigned long sect_no, char *buf)
 995{
 996	struct sm_ftl *ftl = dev->priv;
 997	struct ftl_zone *zone;
 998	int error = 0, in_cache = 0;
 999	int zone_num, block, boffset;
1000
1001	sm_break_offset(ftl, sect_no << 9, &zone_num, &block, &boffset);
1002	mutex_lock(&ftl->mutex);
1003
1004
1005	zone = sm_get_zone(ftl, zone_num);
1006	if (IS_ERR(zone)) {
1007		error = PTR_ERR(zone);
1008		goto unlock;
1009	}
1010
1011	/* Have to look at cache first */
1012	if (ftl->cache_zone == zone_num && ftl->cache_block == block) {
1013		in_cache = 1;
1014		if (!sm_cache_get(ftl, buf, boffset))
1015			goto unlock;
1016	}
1017
1018	/* Translate the block and return if doesn't exist in the table */
1019	block = zone->lba_to_phys_table[block];
1020
1021	if (block == -1) {
1022		memset(buf, 0xFF, SM_SECTOR_SIZE);
1023		goto unlock;
1024	}
1025
1026	if (sm_read_sector(ftl, zone_num, block, boffset, buf, NULL)) {
1027		error = -EIO;
1028		goto unlock;
1029	}
1030
1031	if (in_cache)
1032		sm_cache_put(ftl, buf, boffset);
1033unlock:
1034	mutex_unlock(&ftl->mutex);
1035	return error;
1036}
1037
1038/* outside interface: write a sector */
1039static int sm_write(struct mtd_blktrans_dev *dev,
1040				unsigned long sec_no, char *buf)
1041{
1042	struct sm_ftl *ftl = dev->priv;
1043	struct ftl_zone *zone;
1044	int error = 0, zone_num, block, boffset;
1045
1046	BUG_ON(ftl->readonly);
1047	sm_break_offset(ftl, sec_no << 9, &zone_num, &block, &boffset);
1048
1049	/* No need in flush thread running now */
1050	del_timer(&ftl->timer);
1051	mutex_lock(&ftl->mutex);
1052
1053	zone = sm_get_zone(ftl, zone_num);
1054	if (IS_ERR(zone)) {
1055		error = PTR_ERR(zone);
1056		goto unlock;
1057	}
1058
1059	/* If entry is not in cache, flush it */
1060	if (ftl->cache_block != block || ftl->cache_zone != zone_num) {
1061
1062		error = sm_cache_flush(ftl);
1063		if (error)
1064			goto unlock;
1065
1066		ftl->cache_block = block;
1067		ftl->cache_zone = zone_num;
1068	}
1069
1070	sm_cache_put(ftl, buf, boffset);
1071unlock:
1072	mod_timer(&ftl->timer, jiffies + msecs_to_jiffies(cache_timeout));
1073	mutex_unlock(&ftl->mutex);
1074	return error;
1075}
1076
1077/* outside interface: flush everything */
1078static int sm_flush(struct mtd_blktrans_dev *dev)
1079{
1080	struct sm_ftl *ftl = dev->priv;
1081	int retval;
1082
1083	mutex_lock(&ftl->mutex);
1084	retval =  sm_cache_flush(ftl);
1085	mutex_unlock(&ftl->mutex);
1086	return retval;
1087}
1088
1089/* outside interface: device is released */
1090static void sm_release(struct mtd_blktrans_dev *dev)
1091{
1092	struct sm_ftl *ftl = dev->priv;
1093
1094	mutex_lock(&ftl->mutex);
1095	del_timer_sync(&ftl->timer);
1096	cancel_work_sync(&ftl->flush_work);
 
1097	sm_cache_flush(ftl);
1098	mutex_unlock(&ftl->mutex);
1099}
1100
1101/* outside interface: get geometry */
1102static int sm_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
1103{
1104	struct sm_ftl *ftl = dev->priv;
1105	geo->heads = ftl->heads;
1106	geo->sectors = ftl->sectors;
1107	geo->cylinders = ftl->cylinders;
1108	return 0;
1109}
1110
1111/* external interface: main initialization function */
1112static void sm_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
1113{
1114	struct mtd_blktrans_dev *trans;
1115	struct sm_ftl *ftl;
1116
1117	/* Allocate & initialize our private structure */
1118	ftl = kzalloc(sizeof(struct sm_ftl), GFP_KERNEL);
1119	if (!ftl)
1120		goto error1;
1121
1122
1123	mutex_init(&ftl->mutex);
1124	timer_setup(&ftl->timer, sm_cache_flush_timer, 0);
1125	INIT_WORK(&ftl->flush_work, sm_cache_flush_work);
1126
1127	/* Read media information */
1128	if (sm_get_media_info(ftl, mtd)) {
1129		dbg("found unsupported mtd device, aborting");
1130		goto error2;
1131	}
1132
1133
1134	/* Allocate temporary CIS buffer for read retry support */
1135	ftl->cis_buffer = kzalloc(SM_SECTOR_SIZE, GFP_KERNEL);
1136	if (!ftl->cis_buffer)
1137		goto error2;
1138
1139	/* Allocate zone array, it will be initialized on demand */
1140	ftl->zones = kzalloc(sizeof(struct ftl_zone) * ftl->zone_count,
1141								GFP_KERNEL);
1142	if (!ftl->zones)
1143		goto error3;
1144
1145	/* Allocate the cache*/
1146	ftl->cache_data = kzalloc(ftl->block_size, GFP_KERNEL);
1147
1148	if (!ftl->cache_data)
1149		goto error4;
1150
1151	sm_cache_init(ftl);
1152
1153
1154	/* Allocate upper layer structure and initialize it */
1155	trans = kzalloc(sizeof(struct mtd_blktrans_dev), GFP_KERNEL);
1156	if (!trans)
1157		goto error5;
1158
1159	ftl->trans = trans;
1160	trans->priv = ftl;
1161
1162	trans->tr = tr;
1163	trans->mtd = mtd;
1164	trans->devnum = -1;
1165	trans->size = (ftl->block_size * ftl->max_lba * ftl->zone_count) >> 9;
1166	trans->readonly = ftl->readonly;
1167
1168	if (sm_find_cis(ftl)) {
1169		dbg("CIS not found on mtd device, aborting");
1170		goto error6;
1171	}
1172
1173	ftl->disk_attributes = sm_create_sysfs_attributes(ftl);
1174	if (!ftl->disk_attributes)
1175		goto error6;
1176	trans->disk_attributes = ftl->disk_attributes;
1177
1178	sm_printk("Found %d MiB xD/SmartMedia FTL on mtd%d",
1179		(int)(mtd->size / (1024 * 1024)), mtd->index);
1180
1181	dbg("FTL layout:");
1182	dbg("%d zone(s), each consists of %d blocks (+%d spares)",
1183		ftl->zone_count, ftl->max_lba,
1184		ftl->zone_size - ftl->max_lba);
1185	dbg("each block consists of %d bytes",
1186		ftl->block_size);
1187
1188
1189	/* Register device*/
1190	if (add_mtd_blktrans_dev(trans)) {
1191		dbg("error in mtdblktrans layer");
1192		goto error6;
1193	}
1194	return;
1195error6:
1196	kfree(trans);
1197error5:
1198	kfree(ftl->cache_data);
1199error4:
1200	kfree(ftl->zones);
1201error3:
1202	kfree(ftl->cis_buffer);
1203error2:
1204	kfree(ftl);
1205error1:
1206	return;
1207}
1208
1209/* main interface: device {surprise,} removal */
1210static void sm_remove_dev(struct mtd_blktrans_dev *dev)
1211{
1212	struct sm_ftl *ftl = dev->priv;
1213	int i;
1214
1215	del_mtd_blktrans_dev(dev);
1216	ftl->trans = NULL;
1217
1218	for (i = 0 ; i < ftl->zone_count; i++) {
1219
1220		if (!ftl->zones[i].initialized)
1221			continue;
1222
1223		kfree(ftl->zones[i].lba_to_phys_table);
1224		kfifo_free(&ftl->zones[i].free_sectors);
1225	}
1226
1227	sm_delete_sysfs_attributes(ftl);
1228	kfree(ftl->cis_buffer);
1229	kfree(ftl->zones);
1230	kfree(ftl->cache_data);
1231	kfree(ftl);
1232}
1233
1234static struct mtd_blktrans_ops sm_ftl_ops = {
1235	.name		= "smblk",
1236	.major		= 0,
1237	.part_bits	= SM_FTL_PARTN_BITS,
1238	.blksize	= SM_SECTOR_SIZE,
1239	.getgeo		= sm_getgeo,
1240
1241	.add_mtd	= sm_add_mtd,
1242	.remove_dev	= sm_remove_dev,
1243
1244	.readsect	= sm_read,
1245	.writesect	= sm_write,
1246
1247	.flush		= sm_flush,
1248	.release	= sm_release,
1249
1250	.owner		= THIS_MODULE,
1251};
1252
1253static __init int sm_module_init(void)
1254{
1255	int error = 0;
1256
1257	cache_flush_workqueue = create_freezable_workqueue("smflush");
1258	if (!cache_flush_workqueue)
1259		return -ENOMEM;
1260
1261	error = register_mtd_blktrans(&sm_ftl_ops);
1262	if (error)
1263		destroy_workqueue(cache_flush_workqueue);
1264	return error;
1265
1266}
1267
1268static void __exit sm_module_exit(void)
1269{
1270	destroy_workqueue(cache_flush_workqueue);
1271	deregister_mtd_blktrans(&sm_ftl_ops);
1272}
1273
1274module_init(sm_module_init);
1275module_exit(sm_module_exit);
1276
1277MODULE_LICENSE("GPL");
1278MODULE_AUTHOR("Maxim Levitsky <maximlevitsky@gmail.com>");
1279MODULE_DESCRIPTION("Smartmedia/xD mtd translation layer");