Linux Audio

Check our new training course

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