inftlcore.c 25 KB

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