inftlcore.c 24 KB

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