inftlcore.c 23 KB

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