inftlcore.c 24 KB

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