inftlcore.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*
  2. * inftlcore.c -- Linux driver for Inverse Flash Translation Layer (INFTL)
  3. *
  4. * (C) Copyright 2002, Greg Ungerer (gerg@snapgear.com)
  5. *
  6. * Based heavily on the nftlcore.c code which is:
  7. * (c) 1999 Machine Vision Holdings, Inc.
  8. * Author: David Woodhouse <dwmw2@infradead.org>
  9. *
  10. * $Id: inftlcore.c,v 1.19 2005/11/07 11:14:20 gleixner Exp $
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/sched.h>
  31. #include <linux/init.h>
  32. #include <linux/kmod.h>
  33. #include <linux/hdreg.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/nftl.h>
  36. #include <linux/mtd/inftl.h>
  37. #include <linux/mtd/nand.h>
  38. #include <asm/uaccess.h>
  39. #include <asm/errno.h>
  40. #include <asm/io.h>
  41. /*
  42. * Maximum number of loops while examining next block, to have a
  43. * chance to detect consistency problems (they should never happen
  44. * because of the checks done in the mounting.
  45. */
  46. #define MAX_LOOPS 10000
  47. static void inftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  48. {
  49. struct INFTLrecord *inftl;
  50. unsigned long temp;
  51. if (mtd->type != MTD_NANDFLASH)
  52. return;
  53. /* OK, this is moderately ugly. But probably safe. Alternatives? */
  54. if (memcmp(mtd->name, "DiskOnChip", 10))
  55. return;
  56. if (!mtd->block_isbad) {
  57. printk(KERN_ERR
  58. "INFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
  59. "Please use the new diskonchip driver under the NAND subsystem.\n");
  60. return;
  61. }
  62. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: add_mtd for %s\n", mtd->name);
  63. inftl = kmalloc(sizeof(*inftl), GFP_KERNEL);
  64. if (!inftl) {
  65. printk(KERN_WARNING "INFTL: Out of memory for data structures\n");
  66. return;
  67. }
  68. memset(inftl, 0, sizeof(*inftl));
  69. inftl->mbd.mtd = mtd;
  70. inftl->mbd.devnum = -1;
  71. inftl->mbd.blksize = 512;
  72. inftl->mbd.tr = tr;
  73. if (INFTL_mount(inftl) < 0) {
  74. printk(KERN_WARNING "INFTL: could not mount device\n");
  75. kfree(inftl);
  76. return;
  77. }
  78. /* OK, it's a new one. Set up all the data structures. */
  79. /* Calculate geometry */
  80. inftl->cylinders = 1024;
  81. inftl->heads = 16;
  82. temp = inftl->cylinders * inftl->heads;
  83. inftl->sectors = inftl->mbd.size / temp;
  84. if (inftl->mbd.size % temp) {
  85. inftl->sectors++;
  86. temp = inftl->cylinders * inftl->sectors;
  87. inftl->heads = inftl->mbd.size / temp;
  88. if (inftl->mbd.size % temp) {
  89. inftl->heads++;
  90. temp = inftl->heads * inftl->sectors;
  91. inftl->cylinders = inftl->mbd.size / temp;
  92. }
  93. }
  94. if (inftl->mbd.size != inftl->heads * inftl->cylinders * inftl->sectors) {
  95. /*
  96. Oh no we don't have
  97. mbd.size == heads * cylinders * sectors
  98. */
  99. printk(KERN_WARNING "INFTL: cannot calculate a geometry to "
  100. "match size of 0x%lx.\n", inftl->mbd.size);
  101. printk(KERN_WARNING "INFTL: using C:%d H:%d S:%d "
  102. "(== 0x%lx sects)\n",
  103. inftl->cylinders, inftl->heads , inftl->sectors,
  104. (long)inftl->cylinders * (long)inftl->heads *
  105. (long)inftl->sectors );
  106. }
  107. if (add_mtd_blktrans_dev(&inftl->mbd)) {
  108. kfree(inftl->PUtable);
  109. kfree(inftl->VUtable);
  110. kfree(inftl);
  111. return;
  112. }
  113. #ifdef PSYCHO_DEBUG
  114. printk(KERN_INFO "INFTL: Found new inftl%c\n", inftl->mbd.devnum + 'a');
  115. #endif
  116. return;
  117. }
  118. static void inftl_remove_dev(struct mtd_blktrans_dev *dev)
  119. {
  120. struct INFTLrecord *inftl = (void *)dev;
  121. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: remove_dev (i=%d)\n", dev->devnum);
  122. del_mtd_blktrans_dev(dev);
  123. kfree(inftl->PUtable);
  124. kfree(inftl->VUtable);
  125. kfree(inftl);
  126. }
  127. /*
  128. * Actual INFTL access routines.
  129. */
  130. /*
  131. * Read oob data from flash
  132. */
  133. int inftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
  134. size_t *retlen, uint8_t *buf)
  135. {
  136. struct mtd_oob_ops ops;
  137. int res;
  138. ops.mode = MTD_OOB_PLACE;
  139. ops.ooboffs = offs & (mtd->writesize - 1);
  140. ops.ooblen = len;
  141. ops.oobbuf = buf;
  142. ops.datbuf = NULL;
  143. ops.len = len;
  144. res = mtd->read_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
  145. *retlen = ops.retlen;
  146. return res;
  147. }
  148. /*
  149. * Write oob data to flash
  150. */
  151. int inftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
  152. size_t *retlen, uint8_t *buf)
  153. {
  154. struct mtd_oob_ops ops;
  155. int res;
  156. ops.mode = MTD_OOB_PLACE;
  157. ops.ooboffs = offs & (mtd->writesize - 1);
  158. ops.ooblen = len;
  159. ops.oobbuf = buf;
  160. ops.datbuf = NULL;
  161. ops.len = len;
  162. res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
  163. *retlen = ops.retlen;
  164. return res;
  165. }
  166. /*
  167. * Write data and oob to flash
  168. */
  169. static int inftl_write(struct mtd_info *mtd, loff_t offs, size_t len,
  170. size_t *retlen, uint8_t *buf, uint8_t *oob)
  171. {
  172. struct mtd_oob_ops ops;
  173. int res;
  174. ops.mode = MTD_OOB_PLACE;
  175. ops.ooboffs = offs;
  176. ops.ooblen = mtd->oobsize;
  177. ops.oobbuf = oob;
  178. ops.datbuf = buf;
  179. ops.len = len;
  180. res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
  181. *retlen = ops.retlen;
  182. return res;
  183. }
  184. /*
  185. * INFTL_findfreeblock: Find a free Erase Unit on the INFTL partition.
  186. * This function is used when the give Virtual Unit Chain.
  187. */
  188. static u16 INFTL_findfreeblock(struct INFTLrecord *inftl, int desperate)
  189. {
  190. u16 pot = inftl->LastFreeEUN;
  191. int silly = inftl->nb_blocks;
  192. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_findfreeblock(inftl=%p,"
  193. "desperate=%d)\n", inftl, desperate);
  194. /*
  195. * Normally, we force a fold to happen before we run out of free
  196. * blocks completely.
  197. */
  198. if (!desperate && inftl->numfreeEUNs < 2) {
  199. DEBUG(MTD_DEBUG_LEVEL1, "INFTL: there are too few free "
  200. "EUNs (%d)\n", inftl->numfreeEUNs);
  201. return 0xffff;
  202. }
  203. /* Scan for a free block */
  204. do {
  205. if (inftl->PUtable[pot] == BLOCK_FREE) {
  206. inftl->LastFreeEUN = pot;
  207. return pot;
  208. }
  209. if (++pot > inftl->lastEUN)
  210. pot = 0;
  211. if (!silly--) {
  212. printk(KERN_WARNING "INFTL: no free blocks found! "
  213. "EUN range = %d - %d\n", 0, inftl->LastFreeEUN);
  214. return BLOCK_NIL;
  215. }
  216. } while (pot != inftl->LastFreeEUN);
  217. return BLOCK_NIL;
  218. }
  219. static u16 INFTL_foldchain(struct INFTLrecord *inftl, unsigned thisVUC, unsigned pendingblock)
  220. {
  221. u16 BlockMap[MAX_SECTORS_PER_UNIT];
  222. unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT];
  223. unsigned int thisEUN, prevEUN, status;
  224. struct mtd_info *mtd = inftl->mbd.mtd;
  225. int block, silly;
  226. unsigned int targetEUN;
  227. struct inftl_oob oob;
  228. size_t retlen;
  229. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_foldchain(inftl=%p,thisVUC=%d,"
  230. "pending=%d)\n", inftl, thisVUC, pendingblock);
  231. memset(BlockMap, 0xff, sizeof(BlockMap));
  232. memset(BlockDeleted, 0, sizeof(BlockDeleted));
  233. thisEUN = targetEUN = inftl->VUtable[thisVUC];
  234. if (thisEUN == BLOCK_NIL) {
  235. printk(KERN_WARNING "INFTL: trying to fold non-existent "
  236. "Virtual Unit Chain %d!\n", thisVUC);
  237. return BLOCK_NIL;
  238. }
  239. /*
  240. * Scan to find the Erase Unit which holds the actual data for each
  241. * 512-byte block within the Chain.
  242. */
  243. silly = MAX_LOOPS;
  244. while (thisEUN < inftl->nb_blocks) {
  245. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block ++) {
  246. if ((BlockMap[block] != 0xffff) || BlockDeleted[block])
  247. continue;
  248. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
  249. + (block * SECTORSIZE), 16, &retlen,
  250. (char *)&oob) < 0)
  251. status = SECTOR_IGNORE;
  252. else
  253. status = oob.b.Status | oob.b.Status1;
  254. switch(status) {
  255. case SECTOR_FREE:
  256. case SECTOR_IGNORE:
  257. break;
  258. case SECTOR_USED:
  259. BlockMap[block] = thisEUN;
  260. continue;
  261. case SECTOR_DELETED:
  262. BlockDeleted[block] = 1;
  263. continue;
  264. default:
  265. printk(KERN_WARNING "INFTL: unknown status "
  266. "for block %d in EUN %d: %x\n",
  267. block, thisEUN, status);
  268. break;
  269. }
  270. }
  271. if (!silly--) {
  272. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  273. "Unit Chain 0x%x\n", thisVUC);
  274. return BLOCK_NIL;
  275. }
  276. thisEUN = inftl->PUtable[thisEUN];
  277. }
  278. /*
  279. * OK. We now know the location of every block in the Virtual Unit
  280. * Chain, and the Erase Unit into which we are supposed to be copying.
  281. * Go for it.
  282. */
  283. DEBUG(MTD_DEBUG_LEVEL1, "INFTL: folding chain %d into unit %d\n",
  284. thisVUC, targetEUN);
  285. for (block = 0; block < inftl->EraseSize/SECTORSIZE ; block++) {
  286. unsigned char movebuf[SECTORSIZE];
  287. int ret;
  288. /*
  289. * If it's in the target EUN already, or if it's pending write,
  290. * do nothing.
  291. */
  292. if (BlockMap[block] == targetEUN || (pendingblock ==
  293. (thisVUC * (inftl->EraseSize / SECTORSIZE) + block))) {
  294. continue;
  295. }
  296. /*
  297. * Copy only in non free block (free blocks can only
  298. * happen in case of media errors or deleted blocks).
  299. */
  300. if (BlockMap[block] == BLOCK_NIL)
  301. continue;
  302. ret = mtd->read(mtd, (inftl->EraseSize * BlockMap[block]) +
  303. (block * SECTORSIZE), SECTORSIZE, &retlen,
  304. movebuf);
  305. if (ret < 0 && ret != -EUCLEAN) {
  306. ret = mtd->read(mtd,
  307. (inftl->EraseSize * BlockMap[block]) +
  308. (block * SECTORSIZE), SECTORSIZE,
  309. &retlen, movebuf);
  310. if (ret != -EIO)
  311. DEBUG(MTD_DEBUG_LEVEL1, "INFTL: error went "
  312. "away on retry?\n");
  313. }
  314. memset(&oob, 0xff, sizeof(struct inftl_oob));
  315. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  316. inftl_write(inftl->mbd.mtd, (inftl->EraseSize * targetEUN) +
  317. (block * SECTORSIZE), SECTORSIZE, &retlen,
  318. movebuf, (char *)&oob);
  319. }
  320. /*
  321. * Newest unit in chain now contains data from _all_ older units.
  322. * So go through and erase each unit in chain, oldest first. (This
  323. * is important, by doing oldest first if we crash/reboot then it
  324. * it is relatively simple to clean up the mess).
  325. */
  326. DEBUG(MTD_DEBUG_LEVEL1, "INFTL: want to erase virtual chain %d\n",
  327. thisVUC);
  328. for (;;) {
  329. /* Find oldest unit in chain. */
  330. thisEUN = inftl->VUtable[thisVUC];
  331. prevEUN = BLOCK_NIL;
  332. while (inftl->PUtable[thisEUN] != BLOCK_NIL) {
  333. prevEUN = thisEUN;
  334. thisEUN = inftl->PUtable[thisEUN];
  335. }
  336. /* Check if we are all done */
  337. if (thisEUN == targetEUN)
  338. break;
  339. if (INFTL_formatblock(inftl, thisEUN) < 0) {
  340. /*
  341. * Could not erase : mark block as reserved.
  342. */
  343. inftl->PUtable[thisEUN] = BLOCK_RESERVED;
  344. } else {
  345. /* Correctly erased : mark it as free */
  346. inftl->PUtable[thisEUN] = BLOCK_FREE;
  347. inftl->PUtable[prevEUN] = BLOCK_NIL;
  348. inftl->numfreeEUNs++;
  349. }
  350. }
  351. return targetEUN;
  352. }
  353. static u16 INFTL_makefreeblock(struct INFTLrecord *inftl, unsigned pendingblock)
  354. {
  355. /*
  356. * This is the part that needs some cleverness applied.
  357. * For now, I'm doing the minimum applicable to actually
  358. * get the thing to work.
  359. * Wear-levelling and other clever stuff needs to be implemented
  360. * and we also need to do some assessment of the results when
  361. * the system loses power half-way through the routine.
  362. */
  363. u16 LongestChain = 0;
  364. u16 ChainLength = 0, thislen;
  365. u16 chain, EUN;
  366. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_makefreeblock(inftl=%p,"
  367. "pending=%d)\n", inftl, pendingblock);
  368. for (chain = 0; chain < inftl->nb_blocks; chain++) {
  369. EUN = inftl->VUtable[chain];
  370. thislen = 0;
  371. while (EUN <= inftl->lastEUN) {
  372. thislen++;
  373. EUN = inftl->PUtable[EUN];
  374. if (thislen > 0xff00) {
  375. printk(KERN_WARNING "INFTL: endless loop in "
  376. "Virtual Chain %d: Unit %x\n",
  377. chain, EUN);
  378. /*
  379. * Actually, don't return failure.
  380. * Just ignore this chain and get on with it.
  381. */
  382. thislen = 0;
  383. break;
  384. }
  385. }
  386. if (thislen > ChainLength) {
  387. ChainLength = thislen;
  388. LongestChain = chain;
  389. }
  390. }
  391. if (ChainLength < 2) {
  392. printk(KERN_WARNING "INFTL: no Virtual Unit Chains available "
  393. "for folding. Failing request\n");
  394. return BLOCK_NIL;
  395. }
  396. return INFTL_foldchain(inftl, LongestChain, pendingblock);
  397. }
  398. static int nrbits(unsigned int val, int bitcount)
  399. {
  400. int i, total = 0;
  401. for (i = 0; (i < bitcount); i++)
  402. total += (((0x1 << i) & val) ? 1 : 0);
  403. return total;
  404. }
  405. /*
  406. * INFTL_findwriteunit: Return the unit number into which we can write
  407. * for this block. Make it available if it isn't already.
  408. */
  409. static inline u16 INFTL_findwriteunit(struct INFTLrecord *inftl, unsigned block)
  410. {
  411. unsigned int thisVUC = block / (inftl->EraseSize / SECTORSIZE);
  412. unsigned int thisEUN, writeEUN, prev_block, status;
  413. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize -1);
  414. struct mtd_info *mtd = inftl->mbd.mtd;
  415. struct inftl_oob oob;
  416. struct inftl_bci bci;
  417. unsigned char anac, nacs, parity;
  418. size_t retlen;
  419. int silly, silly2 = 3;
  420. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_findwriteunit(inftl=%p,"
  421. "block=%d)\n", inftl, block);
  422. do {
  423. /*
  424. * Scan the media to find a unit in the VUC which has
  425. * a free space for the block in question.
  426. */
  427. writeEUN = BLOCK_NIL;
  428. thisEUN = inftl->VUtable[thisVUC];
  429. silly = MAX_LOOPS;
  430. while (thisEUN <= inftl->lastEUN) {
  431. inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  432. blockofs, 8, &retlen, (char *)&bci);
  433. status = bci.Status | bci.Status1;
  434. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: status of block %d in "
  435. "EUN %d is %x\n", block , writeEUN, status);
  436. switch(status) {
  437. case SECTOR_FREE:
  438. writeEUN = thisEUN;
  439. break;
  440. case SECTOR_DELETED:
  441. case SECTOR_USED:
  442. /* Can't go any further */
  443. goto hitused;
  444. case SECTOR_IGNORE:
  445. break;
  446. default:
  447. /*
  448. * Invalid block. Don't use it any more.
  449. * Must implement.
  450. */
  451. break;
  452. }
  453. if (!silly--) {
  454. printk(KERN_WARNING "INFTL: infinite loop in "
  455. "Virtual Unit Chain 0x%x\n", thisVUC);
  456. return 0xffff;
  457. }
  458. /* Skip to next block in chain */
  459. thisEUN = inftl->PUtable[thisEUN];
  460. }
  461. hitused:
  462. if (writeEUN != BLOCK_NIL)
  463. return writeEUN;
  464. /*
  465. * OK. We didn't find one in the existing chain, or there
  466. * is no existing chain. Allocate a new one.
  467. */
  468. writeEUN = INFTL_findfreeblock(inftl, 0);
  469. if (writeEUN == BLOCK_NIL) {
  470. /*
  471. * That didn't work - there were no free blocks just
  472. * waiting to be picked up. We're going to have to fold
  473. * a chain to make room.
  474. */
  475. thisEUN = INFTL_makefreeblock(inftl, 0xffff);
  476. /*
  477. * Hopefully we free something, lets try again.
  478. * This time we are desperate...
  479. */
  480. DEBUG(MTD_DEBUG_LEVEL1, "INFTL: using desperate==1 "
  481. "to find free EUN to accommodate write to "
  482. "VUC %d\n", thisVUC);
  483. writeEUN = INFTL_findfreeblock(inftl, 1);
  484. if (writeEUN == BLOCK_NIL) {
  485. /*
  486. * Ouch. This should never happen - we should
  487. * always be able to make some room somehow.
  488. * If we get here, we've allocated more storage
  489. * space than actual media, or our makefreeblock
  490. * routine is missing something.
  491. */
  492. printk(KERN_WARNING "INFTL: cannot make free "
  493. "space.\n");
  494. #ifdef DEBUG
  495. INFTL_dumptables(inftl);
  496. INFTL_dumpVUchains(inftl);
  497. #endif
  498. return BLOCK_NIL;
  499. }
  500. }
  501. /*
  502. * Insert new block into virtual chain. Firstly update the
  503. * block headers in flash...
  504. */
  505. anac = 0;
  506. nacs = 0;
  507. thisEUN = inftl->VUtable[thisVUC];
  508. if (thisEUN != BLOCK_NIL) {
  509. inftl_read_oob(mtd, thisEUN * inftl->EraseSize
  510. + 8, 8, &retlen, (char *)&oob.u);
  511. anac = oob.u.a.ANAC + 1;
  512. nacs = oob.u.a.NACs + 1;
  513. }
  514. prev_block = inftl->VUtable[thisVUC];
  515. if (prev_block < inftl->nb_blocks)
  516. prev_block -= inftl->firstEUN;
  517. parity = (nrbits(thisVUC, 16) & 0x1) ? 0x1 : 0;
  518. parity |= (nrbits(prev_block, 16) & 0x1) ? 0x2 : 0;
  519. parity |= (nrbits(anac, 8) & 0x1) ? 0x4 : 0;
  520. parity |= (nrbits(nacs, 8) & 0x1) ? 0x8 : 0;
  521. oob.u.a.virtualUnitNo = cpu_to_le16(thisVUC);
  522. oob.u.a.prevUnitNo = cpu_to_le16(prev_block);
  523. oob.u.a.ANAC = anac;
  524. oob.u.a.NACs = nacs;
  525. oob.u.a.parityPerField = parity;
  526. oob.u.a.discarded = 0xaa;
  527. inftl_write_oob(mtd, writeEUN * inftl->EraseSize + 8, 8,
  528. &retlen, (char *)&oob.u);
  529. /* Also back up header... */
  530. oob.u.b.virtualUnitNo = cpu_to_le16(thisVUC);
  531. oob.u.b.prevUnitNo = cpu_to_le16(prev_block);
  532. oob.u.b.ANAC = anac;
  533. oob.u.b.NACs = nacs;
  534. oob.u.b.parityPerField = parity;
  535. oob.u.b.discarded = 0xaa;
  536. inftl_write_oob(mtd, writeEUN * inftl->EraseSize +
  537. SECTORSIZE * 4 + 8, 8, &retlen, (char *)&oob.u);
  538. inftl->PUtable[writeEUN] = inftl->VUtable[thisVUC];
  539. inftl->VUtable[thisVUC] = writeEUN;
  540. inftl->numfreeEUNs--;
  541. return writeEUN;
  542. } while (silly2--);
  543. printk(KERN_WARNING "INFTL: error folding to make room for Virtual "
  544. "Unit Chain 0x%x\n", thisVUC);
  545. return 0xffff;
  546. }
  547. /*
  548. * Given a Virtual Unit Chain, see if it can be deleted, and if so do it.
  549. */
  550. static void INFTL_trydeletechain(struct INFTLrecord *inftl, unsigned thisVUC)
  551. {
  552. struct mtd_info *mtd = inftl->mbd.mtd;
  553. unsigned char BlockUsed[MAX_SECTORS_PER_UNIT];
  554. unsigned char BlockDeleted[MAX_SECTORS_PER_UNIT];
  555. unsigned int thisEUN, status;
  556. int block, silly;
  557. struct inftl_bci bci;
  558. size_t retlen;
  559. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_trydeletechain(inftl=%p,"
  560. "thisVUC=%d)\n", inftl, thisVUC);
  561. memset(BlockUsed, 0, sizeof(BlockUsed));
  562. memset(BlockDeleted, 0, sizeof(BlockDeleted));
  563. thisEUN = inftl->VUtable[thisVUC];
  564. if (thisEUN == BLOCK_NIL) {
  565. printk(KERN_WARNING "INFTL: trying to delete non-existent "
  566. "Virtual Unit Chain %d!\n", thisVUC);
  567. return;
  568. }
  569. /*
  570. * Scan through the Erase Units to determine whether any data is in
  571. * each of the 512-byte blocks within the Chain.
  572. */
  573. silly = MAX_LOOPS;
  574. while (thisEUN < inftl->nb_blocks) {
  575. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++) {
  576. if (BlockUsed[block] || BlockDeleted[block])
  577. continue;
  578. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize)
  579. + (block * SECTORSIZE), 8 , &retlen,
  580. (char *)&bci) < 0)
  581. status = SECTOR_IGNORE;
  582. else
  583. status = bci.Status | bci.Status1;
  584. switch(status) {
  585. case SECTOR_FREE:
  586. case SECTOR_IGNORE:
  587. break;
  588. case SECTOR_USED:
  589. BlockUsed[block] = 1;
  590. continue;
  591. case SECTOR_DELETED:
  592. BlockDeleted[block] = 1;
  593. continue;
  594. default:
  595. printk(KERN_WARNING "INFTL: unknown status "
  596. "for block %d in EUN %d: 0x%x\n",
  597. block, thisEUN, status);
  598. }
  599. }
  600. if (!silly--) {
  601. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  602. "Unit Chain 0x%x\n", thisVUC);
  603. return;
  604. }
  605. thisEUN = inftl->PUtable[thisEUN];
  606. }
  607. for (block = 0; block < inftl->EraseSize/SECTORSIZE; block++)
  608. if (BlockUsed[block])
  609. return;
  610. /*
  611. * For each block in the chain free it and make it available
  612. * for future use. Erase from the oldest unit first.
  613. */
  614. DEBUG(MTD_DEBUG_LEVEL1, "INFTL: deleting empty VUC %d\n", thisVUC);
  615. for (;;) {
  616. u16 *prevEUN = &inftl->VUtable[thisVUC];
  617. thisEUN = *prevEUN;
  618. /* If the chain is all gone already, we're done */
  619. if (thisEUN == BLOCK_NIL) {
  620. DEBUG(MTD_DEBUG_LEVEL2, "INFTL: Empty VUC %d for deletion was already absent\n", thisEUN);
  621. return;
  622. }
  623. /* Find oldest unit in chain. */
  624. while (inftl->PUtable[thisEUN] != BLOCK_NIL) {
  625. BUG_ON(thisEUN >= inftl->nb_blocks);
  626. prevEUN = &inftl->PUtable[thisEUN];
  627. thisEUN = *prevEUN;
  628. }
  629. DEBUG(MTD_DEBUG_LEVEL3, "Deleting EUN %d from VUC %d\n",
  630. thisEUN, thisVUC);
  631. if (INFTL_formatblock(inftl, thisEUN) < 0) {
  632. /*
  633. * Could not erase : mark block as reserved.
  634. */
  635. inftl->PUtable[thisEUN] = BLOCK_RESERVED;
  636. } else {
  637. /* Correctly erased : mark it as free */
  638. inftl->PUtable[thisEUN] = BLOCK_FREE;
  639. inftl->numfreeEUNs++;
  640. }
  641. /* Now sort out whatever was pointing to it... */
  642. *prevEUN = BLOCK_NIL;
  643. /* Ideally we'd actually be responsive to new
  644. requests while we're doing this -- if there's
  645. free space why should others be made to wait? */
  646. cond_resched();
  647. }
  648. inftl->VUtable[thisVUC] = BLOCK_NIL;
  649. }
  650. static int INFTL_deleteblock(struct INFTLrecord *inftl, unsigned block)
  651. {
  652. unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
  653. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  654. struct mtd_info *mtd = inftl->mbd.mtd;
  655. unsigned int status;
  656. int silly = MAX_LOOPS;
  657. size_t retlen;
  658. struct inftl_bci bci;
  659. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: INFTL_deleteblock(inftl=%p,"
  660. "block=%d)\n", inftl, block);
  661. while (thisEUN < inftl->nb_blocks) {
  662. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  663. blockofs, 8, &retlen, (char *)&bci) < 0)
  664. status = SECTOR_IGNORE;
  665. else
  666. status = bci.Status | bci.Status1;
  667. switch (status) {
  668. case SECTOR_FREE:
  669. case SECTOR_IGNORE:
  670. break;
  671. case SECTOR_DELETED:
  672. thisEUN = BLOCK_NIL;
  673. goto foundit;
  674. case SECTOR_USED:
  675. goto foundit;
  676. default:
  677. printk(KERN_WARNING "INFTL: unknown status for "
  678. "block %d in EUN %d: 0x%x\n",
  679. block, thisEUN, status);
  680. break;
  681. }
  682. if (!silly--) {
  683. printk(KERN_WARNING "INFTL: infinite loop in Virtual "
  684. "Unit Chain 0x%x\n",
  685. block / (inftl->EraseSize / SECTORSIZE));
  686. return 1;
  687. }
  688. thisEUN = inftl->PUtable[thisEUN];
  689. }
  690. foundit:
  691. if (thisEUN != BLOCK_NIL) {
  692. loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
  693. if (inftl_read_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
  694. return -EIO;
  695. bci.Status = bci.Status1 = SECTOR_DELETED;
  696. if (inftl_write_oob(mtd, ptr, 8, &retlen, (char *)&bci) < 0)
  697. return -EIO;
  698. INFTL_trydeletechain(inftl, block / (inftl->EraseSize / SECTORSIZE));
  699. }
  700. return 0;
  701. }
  702. static int inftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  703. char *buffer)
  704. {
  705. struct INFTLrecord *inftl = (void *)mbd;
  706. unsigned int writeEUN;
  707. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  708. size_t retlen;
  709. struct inftl_oob oob;
  710. char *p, *pend;
  711. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: inftl_writeblock(inftl=%p,block=%ld,"
  712. "buffer=%p)\n", inftl, block, buffer);
  713. /* Is block all zero? */
  714. pend = buffer + SECTORSIZE;
  715. for (p = buffer; p < pend && !*p; p++)
  716. ;
  717. if (p < pend) {
  718. writeEUN = INFTL_findwriteunit(inftl, block);
  719. if (writeEUN == BLOCK_NIL) {
  720. printk(KERN_WARNING "inftl_writeblock(): cannot find "
  721. "block to write to\n");
  722. /*
  723. * If we _still_ haven't got a block to use,
  724. * we're screwed.
  725. */
  726. return 1;
  727. }
  728. memset(&oob, 0xff, sizeof(struct inftl_oob));
  729. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  730. inftl_write(inftl->mbd.mtd, (writeEUN * inftl->EraseSize) +
  731. blockofs, SECTORSIZE, &retlen, (char *)buffer,
  732. (char *)&oob);
  733. /*
  734. * need to write SECTOR_USED flags since they are not written
  735. * in mtd_writeecc
  736. */
  737. } else {
  738. INFTL_deleteblock(inftl, block);
  739. }
  740. return 0;
  741. }
  742. static int inftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  743. char *buffer)
  744. {
  745. struct INFTLrecord *inftl = (void *)mbd;
  746. unsigned int thisEUN = inftl->VUtable[block / (inftl->EraseSize / SECTORSIZE)];
  747. unsigned long blockofs = (block * SECTORSIZE) & (inftl->EraseSize - 1);
  748. struct mtd_info *mtd = inftl->mbd.mtd;
  749. unsigned int status;
  750. int silly = MAX_LOOPS;
  751. struct inftl_bci bci;
  752. size_t retlen;
  753. DEBUG(MTD_DEBUG_LEVEL3, "INFTL: inftl_readblock(inftl=%p,block=%ld,"
  754. "buffer=%p)\n", inftl, block, buffer);
  755. while (thisEUN < inftl->nb_blocks) {
  756. if (inftl_read_oob(mtd, (thisEUN * inftl->EraseSize) +
  757. blockofs, 8, &retlen, (char *)&bci) < 0)
  758. status = SECTOR_IGNORE;
  759. else
  760. status = bci.Status | bci.Status1;
  761. switch (status) {
  762. case SECTOR_DELETED:
  763. thisEUN = BLOCK_NIL;
  764. goto foundit;
  765. case SECTOR_USED:
  766. goto foundit;
  767. case SECTOR_FREE:
  768. case SECTOR_IGNORE:
  769. break;
  770. default:
  771. printk(KERN_WARNING "INFTL: unknown status for "
  772. "block %ld in EUN %d: 0x%04x\n",
  773. block, thisEUN, status);
  774. break;
  775. }
  776. if (!silly--) {
  777. printk(KERN_WARNING "INFTL: infinite loop in "
  778. "Virtual Unit Chain 0x%lx\n",
  779. block / (inftl->EraseSize / SECTORSIZE));
  780. return 1;
  781. }
  782. thisEUN = inftl->PUtable[thisEUN];
  783. }
  784. foundit:
  785. if (thisEUN == BLOCK_NIL) {
  786. /* The requested block is not on the media, return all 0x00 */
  787. memset(buffer, 0, SECTORSIZE);
  788. } else {
  789. size_t retlen;
  790. loff_t ptr = (thisEUN * inftl->EraseSize) + blockofs;
  791. int ret = mtd->read(mtd, ptr, SECTORSIZE, &retlen, buffer);
  792. /* Handle corrected bit flips gracefully */
  793. if (ret < 0 && ret != -EUCLEAN)
  794. return -EIO;
  795. }
  796. return 0;
  797. }
  798. static int inftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  799. {
  800. struct INFTLrecord *inftl = (void *)dev;
  801. geo->heads = inftl->heads;
  802. geo->sectors = inftl->sectors;
  803. geo->cylinders = inftl->cylinders;
  804. return 0;
  805. }
  806. static struct mtd_blktrans_ops inftl_tr = {
  807. .name = "inftl",
  808. .major = INFTL_MAJOR,
  809. .part_bits = INFTL_PARTN_BITS,
  810. .getgeo = inftl_getgeo,
  811. .readsect = inftl_readblock,
  812. .writesect = inftl_writeblock,
  813. .add_mtd = inftl_add_mtd,
  814. .remove_dev = inftl_remove_dev,
  815. .owner = THIS_MODULE,
  816. };
  817. static int __init init_inftl(void)
  818. {
  819. printk(KERN_INFO "INFTL: inftlcore.c $Revision: 1.19 $, "
  820. "inftlmount.c %s\n", inftlmountrev);
  821. return register_mtd_blktrans(&inftl_tr);
  822. }
  823. static void __exit cleanup_inftl(void)
  824. {
  825. deregister_mtd_blktrans(&inftl_tr);
  826. }
  827. module_init(init_inftl);
  828. module_exit(cleanup_inftl);
  829. MODULE_LICENSE("GPL");
  830. MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>, David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
  831. MODULE_DESCRIPTION("Support code for Inverse Flash Translation Layer, used on M-Systems DiskOnChip 2000, Millennium and Millennium Plus");