nftlcore.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /* Linux driver for NAND Flash Translation Layer */
  2. /* (c) 1999 Machine Vision Holdings, Inc. */
  3. /* Author: David Woodhouse <dwmw2@infradead.org> */
  4. /* $Id: nftlcore.c,v 1.98 2005/11/07 11:14:21 gleixner Exp $ */
  5. /*
  6. The contents of this file are distributed under the GNU General
  7. Public License version 2. The author places no additional
  8. restrictions of any kind on it.
  9. */
  10. #define PRERELEASE
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <asm/errno.h>
  14. #include <asm/io.h>
  15. #include <asm/uaccess.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/sched.h>
  21. #include <linux/init.h>
  22. #include <linux/hdreg.h>
  23. #include <linux/kmod.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/nand.h>
  26. #include <linux/mtd/nftl.h>
  27. #include <linux/mtd/blktrans.h>
  28. /* maximum number of loops while examining next block, to have a
  29. chance to detect consistency problems (they should never happen
  30. because of the checks done in the mounting */
  31. #define MAX_LOOPS 10000
  32. static void nftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  33. {
  34. struct NFTLrecord *nftl;
  35. unsigned long temp;
  36. if (mtd->type != MTD_NANDFLASH)
  37. return;
  38. /* OK, this is moderately ugly. But probably safe. Alternatives? */
  39. if (memcmp(mtd->name, "DiskOnChip", 10))
  40. return;
  41. if (!mtd->block_isbad) {
  42. printk(KERN_ERR
  43. "NFTL no longer supports the old DiskOnChip drivers loaded via docprobe.\n"
  44. "Please use the new diskonchip driver under the NAND subsystem.\n");
  45. return;
  46. }
  47. DEBUG(MTD_DEBUG_LEVEL1, "NFTL: add_mtd for %s\n", mtd->name);
  48. nftl = kmalloc(sizeof(struct NFTLrecord), GFP_KERNEL);
  49. if (!nftl) {
  50. printk(KERN_WARNING "NFTL: out of memory for data structures\n");
  51. return;
  52. }
  53. memset(nftl, 0, sizeof(*nftl));
  54. nftl->mbd.mtd = mtd;
  55. nftl->mbd.devnum = -1;
  56. nftl->mbd.blksize = 512;
  57. nftl->mbd.tr = tr;
  58. if (NFTL_mount(nftl) < 0) {
  59. printk(KERN_WARNING "NFTL: could not mount device\n");
  60. kfree(nftl);
  61. return;
  62. }
  63. /* OK, it's a new one. Set up all the data structures. */
  64. /* Calculate geometry */
  65. nftl->cylinders = 1024;
  66. nftl->heads = 16;
  67. temp = nftl->cylinders * nftl->heads;
  68. nftl->sectors = nftl->mbd.size / temp;
  69. if (nftl->mbd.size % temp) {
  70. nftl->sectors++;
  71. temp = nftl->cylinders * nftl->sectors;
  72. nftl->heads = nftl->mbd.size / temp;
  73. if (nftl->mbd.size % temp) {
  74. nftl->heads++;
  75. temp = nftl->heads * nftl->sectors;
  76. nftl->cylinders = nftl->mbd.size / temp;
  77. }
  78. }
  79. if (nftl->mbd.size != nftl->heads * nftl->cylinders * nftl->sectors) {
  80. /*
  81. Oh no we don't have
  82. mbd.size == heads * cylinders * sectors
  83. */
  84. printk(KERN_WARNING "NFTL: cannot calculate a geometry to "
  85. "match size of 0x%lx.\n", nftl->mbd.size);
  86. printk(KERN_WARNING "NFTL: using C:%d H:%d S:%d "
  87. "(== 0x%lx sects)\n",
  88. nftl->cylinders, nftl->heads , nftl->sectors,
  89. (long)nftl->cylinders * (long)nftl->heads *
  90. (long)nftl->sectors );
  91. }
  92. if (add_mtd_blktrans_dev(&nftl->mbd)) {
  93. kfree(nftl->ReplUnitTable);
  94. kfree(nftl->EUNtable);
  95. kfree(nftl);
  96. return;
  97. }
  98. #ifdef PSYCHO_DEBUG
  99. printk(KERN_INFO "NFTL: Found new nftl%c\n", nftl->mbd.devnum + 'a');
  100. #endif
  101. }
  102. static void nftl_remove_dev(struct mtd_blktrans_dev *dev)
  103. {
  104. struct NFTLrecord *nftl = (void *)dev;
  105. DEBUG(MTD_DEBUG_LEVEL1, "NFTL: remove_dev (i=%d)\n", dev->devnum);
  106. del_mtd_blktrans_dev(dev);
  107. kfree(nftl->ReplUnitTable);
  108. kfree(nftl->EUNtable);
  109. kfree(nftl);
  110. }
  111. /*
  112. * Read oob data from flash
  113. */
  114. int nftl_read_oob(struct mtd_info *mtd, loff_t offs, size_t len,
  115. size_t *retlen, uint8_t *buf)
  116. {
  117. struct mtd_oob_ops ops;
  118. int res;
  119. ops.mode = MTD_OOB_PLACE;
  120. ops.ooboffs = offs & (mtd->writesize - 1);
  121. ops.ooblen = len;
  122. ops.oobbuf = buf;
  123. ops.datbuf = NULL;
  124. ops.len = len;
  125. res = mtd->read_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
  126. *retlen = ops.retlen;
  127. return res;
  128. }
  129. /*
  130. * Write oob data to flash
  131. */
  132. int nftl_write_oob(struct mtd_info *mtd, loff_t offs, size_t len,
  133. size_t *retlen, uint8_t *buf)
  134. {
  135. struct mtd_oob_ops ops;
  136. int res;
  137. ops.mode = MTD_OOB_PLACE;
  138. ops.ooboffs = offs & (mtd->writesize - 1);
  139. ops.ooblen = len;
  140. ops.oobbuf = buf;
  141. ops.datbuf = NULL;
  142. ops.len = len;
  143. res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
  144. *retlen = ops.retlen;
  145. return res;
  146. }
  147. /*
  148. * Write data and oob to flash
  149. */
  150. static int nftl_write(struct mtd_info *mtd, loff_t offs, size_t len,
  151. size_t *retlen, uint8_t *buf, uint8_t *oob)
  152. {
  153. struct mtd_oob_ops ops;
  154. int res;
  155. ops.mode = MTD_OOB_PLACE;
  156. ops.ooboffs = offs;
  157. ops.ooblen = mtd->oobsize;
  158. ops.oobbuf = oob;
  159. ops.datbuf = buf;
  160. ops.len = len;
  161. res = mtd->write_oob(mtd, offs & ~(mtd->writesize - 1), &ops);
  162. *retlen = ops.retlen;
  163. return res;
  164. }
  165. #ifdef CONFIG_NFTL_RW
  166. /* Actual NFTL access routines */
  167. /* NFTL_findfreeblock: Find a free Erase Unit on the NFTL partition. This function is used
  168. * when the give Virtual Unit Chain
  169. */
  170. static u16 NFTL_findfreeblock(struct NFTLrecord *nftl, int desperate )
  171. {
  172. /* For a given Virtual Unit Chain: find or create a free block and
  173. add it to the chain */
  174. /* We're passed the number of the last EUN in the chain, to save us from
  175. having to look it up again */
  176. u16 pot = nftl->LastFreeEUN;
  177. int silly = nftl->nb_blocks;
  178. /* Normally, we force a fold to happen before we run out of free blocks completely */
  179. if (!desperate && nftl->numfreeEUNs < 2) {
  180. DEBUG(MTD_DEBUG_LEVEL1, "NFTL_findfreeblock: there are too few free EUNs\n");
  181. return 0xffff;
  182. }
  183. /* Scan for a free block */
  184. do {
  185. if (nftl->ReplUnitTable[pot] == BLOCK_FREE) {
  186. nftl->LastFreeEUN = pot;
  187. nftl->numfreeEUNs--;
  188. return pot;
  189. }
  190. /* This will probably point to the MediaHdr unit itself,
  191. right at the beginning of the partition. But that unit
  192. (and the backup unit too) should have the UCI set
  193. up so that it's not selected for overwriting */
  194. if (++pot > nftl->lastEUN)
  195. pot = le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN);
  196. if (!silly--) {
  197. printk("Argh! No free blocks found! LastFreeEUN = %d, "
  198. "FirstEUN = %d\n", nftl->LastFreeEUN,
  199. le16_to_cpu(nftl->MediaHdr.FirstPhysicalEUN));
  200. return 0xffff;
  201. }
  202. } while (pot != nftl->LastFreeEUN);
  203. return 0xffff;
  204. }
  205. static u16 NFTL_foldchain (struct NFTLrecord *nftl, unsigned thisVUC, unsigned pendingblock )
  206. {
  207. struct mtd_info *mtd = nftl->mbd.mtd;
  208. u16 BlockMap[MAX_SECTORS_PER_UNIT];
  209. unsigned char BlockLastState[MAX_SECTORS_PER_UNIT];
  210. unsigned char BlockFreeFound[MAX_SECTORS_PER_UNIT];
  211. unsigned int thisEUN;
  212. int block;
  213. int silly;
  214. unsigned int targetEUN;
  215. struct nftl_oob oob;
  216. int inplace = 1;
  217. size_t retlen;
  218. memset(BlockMap, 0xff, sizeof(BlockMap));
  219. memset(BlockFreeFound, 0, sizeof(BlockFreeFound));
  220. thisEUN = nftl->EUNtable[thisVUC];
  221. if (thisEUN == BLOCK_NIL) {
  222. printk(KERN_WARNING "Trying to fold non-existent "
  223. "Virtual Unit Chain %d!\n", thisVUC);
  224. return BLOCK_NIL;
  225. }
  226. /* Scan to find the Erase Unit which holds the actual data for each
  227. 512-byte block within the Chain.
  228. */
  229. silly = MAX_LOOPS;
  230. targetEUN = BLOCK_NIL;
  231. while (thisEUN <= nftl->lastEUN ) {
  232. unsigned int status, foldmark;
  233. targetEUN = thisEUN;
  234. for (block = 0; block < nftl->EraseSize / 512; block ++) {
  235. nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) +
  236. (block * 512), 16 , &retlen,
  237. (char *)&oob);
  238. if (block == 2) {
  239. foldmark = oob.u.c.FoldMark | oob.u.c.FoldMark1;
  240. if (foldmark == FOLD_MARK_IN_PROGRESS) {
  241. DEBUG(MTD_DEBUG_LEVEL1,
  242. "Write Inhibited on EUN %d\n", thisEUN);
  243. inplace = 0;
  244. } else {
  245. /* There's no other reason not to do inplace,
  246. except ones that come later. So we don't need
  247. to preserve inplace */
  248. inplace = 1;
  249. }
  250. }
  251. status = oob.b.Status | oob.b.Status1;
  252. BlockLastState[block] = status;
  253. switch(status) {
  254. case SECTOR_FREE:
  255. BlockFreeFound[block] = 1;
  256. break;
  257. case SECTOR_USED:
  258. if (!BlockFreeFound[block])
  259. BlockMap[block] = thisEUN;
  260. else
  261. printk(KERN_WARNING
  262. "SECTOR_USED found after SECTOR_FREE "
  263. "in Virtual Unit Chain %d for block %d\n",
  264. thisVUC, block);
  265. break;
  266. case SECTOR_DELETED:
  267. if (!BlockFreeFound[block])
  268. BlockMap[block] = BLOCK_NIL;
  269. else
  270. printk(KERN_WARNING
  271. "SECTOR_DELETED found after SECTOR_FREE "
  272. "in Virtual Unit Chain %d for block %d\n",
  273. thisVUC, block);
  274. break;
  275. case SECTOR_IGNORE:
  276. break;
  277. default:
  278. printk("Unknown status for block %d in EUN %d: %x\n",
  279. block, thisEUN, status);
  280. }
  281. }
  282. if (!silly--) {
  283. printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%x\n",
  284. thisVUC);
  285. return BLOCK_NIL;
  286. }
  287. thisEUN = nftl->ReplUnitTable[thisEUN];
  288. }
  289. if (inplace) {
  290. /* We're being asked to be a fold-in-place. Check
  291. that all blocks which actually have data associated
  292. with them (i.e. BlockMap[block] != BLOCK_NIL) are
  293. either already present or SECTOR_FREE in the target
  294. block. If not, we're going to have to fold out-of-place
  295. anyway.
  296. */
  297. for (block = 0; block < nftl->EraseSize / 512 ; block++) {
  298. if (BlockLastState[block] != SECTOR_FREE &&
  299. BlockMap[block] != BLOCK_NIL &&
  300. BlockMap[block] != targetEUN) {
  301. DEBUG(MTD_DEBUG_LEVEL1, "Setting inplace to 0. VUC %d, "
  302. "block %d was %x lastEUN, "
  303. "and is in EUN %d (%s) %d\n",
  304. thisVUC, block, BlockLastState[block],
  305. BlockMap[block],
  306. BlockMap[block]== targetEUN ? "==" : "!=",
  307. targetEUN);
  308. inplace = 0;
  309. break;
  310. }
  311. }
  312. if (pendingblock >= (thisVUC * (nftl->EraseSize / 512)) &&
  313. pendingblock < ((thisVUC + 1)* (nftl->EraseSize / 512)) &&
  314. BlockLastState[pendingblock - (thisVUC * (nftl->EraseSize / 512))] !=
  315. SECTOR_FREE) {
  316. DEBUG(MTD_DEBUG_LEVEL1, "Pending write not free in EUN %d. "
  317. "Folding out of place.\n", targetEUN);
  318. inplace = 0;
  319. }
  320. }
  321. if (!inplace) {
  322. DEBUG(MTD_DEBUG_LEVEL1, "Cannot fold Virtual Unit Chain %d in place. "
  323. "Trying out-of-place\n", thisVUC);
  324. /* We need to find a targetEUN to fold into. */
  325. targetEUN = NFTL_findfreeblock(nftl, 1);
  326. if (targetEUN == BLOCK_NIL) {
  327. /* Ouch. Now we're screwed. We need to do a
  328. fold-in-place of another chain to make room
  329. for this one. We need a better way of selecting
  330. which chain to fold, because makefreeblock will
  331. only ask us to fold the same one again.
  332. */
  333. printk(KERN_WARNING
  334. "NFTL_findfreeblock(desperate) returns 0xffff.\n");
  335. return BLOCK_NIL;
  336. }
  337. } else {
  338. /* We put a fold mark in the chain we are folding only if we
  339. fold in place to help the mount check code. If we do not fold in
  340. place, it is possible to find the valid chain by selecting the
  341. longer one */
  342. oob.u.c.FoldMark = oob.u.c.FoldMark1 = cpu_to_le16(FOLD_MARK_IN_PROGRESS);
  343. oob.u.c.unused = 0xffffffff;
  344. nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 2 * 512 + 8,
  345. 8, &retlen, (char *)&oob.u);
  346. }
  347. /* OK. We now know the location of every block in the Virtual Unit Chain,
  348. and the Erase Unit into which we are supposed to be copying.
  349. Go for it.
  350. */
  351. DEBUG(MTD_DEBUG_LEVEL1,"Folding chain %d into unit %d\n", thisVUC, targetEUN);
  352. for (block = 0; block < nftl->EraseSize / 512 ; block++) {
  353. unsigned char movebuf[512];
  354. int ret;
  355. /* If it's in the target EUN already, or if it's pending write, do nothing */
  356. if (BlockMap[block] == targetEUN ||
  357. (pendingblock == (thisVUC * (nftl->EraseSize / 512) + block))) {
  358. continue;
  359. }
  360. /* copy only in non free block (free blocks can only
  361. happen in case of media errors or deleted blocks) */
  362. if (BlockMap[block] == BLOCK_NIL)
  363. continue;
  364. ret = mtd->read(mtd, (nftl->EraseSize * BlockMap[block]) + (block * 512),
  365. 512, &retlen, movebuf);
  366. if (ret < 0 && ret != -EUCLEAN) {
  367. ret = mtd->read(mtd, (nftl->EraseSize * BlockMap[block])
  368. + (block * 512), 512, &retlen,
  369. movebuf);
  370. if (ret != -EIO)
  371. printk("Error went away on retry.\n");
  372. }
  373. memset(&oob, 0xff, sizeof(struct nftl_oob));
  374. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  375. nftl_write(nftl->mbd.mtd, (nftl->EraseSize * targetEUN) +
  376. (block * 512), 512, &retlen, movebuf, (char *)&oob);
  377. }
  378. /* add the header so that it is now a valid chain */
  379. oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
  380. oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum = 0xffff;
  381. nftl_write_oob(mtd, (nftl->EraseSize * targetEUN) + 8,
  382. 8, &retlen, (char *)&oob.u);
  383. /* OK. We've moved the whole lot into the new block. Now we have to free the original blocks. */
  384. /* At this point, we have two different chains for this Virtual Unit, and no way to tell
  385. them apart. If we crash now, we get confused. However, both contain the same data, so we
  386. shouldn't actually lose data in this case. It's just that when we load up on a medium which
  387. has duplicate chains, we need to free one of the chains because it's not necessary any more.
  388. */
  389. thisEUN = nftl->EUNtable[thisVUC];
  390. DEBUG(MTD_DEBUG_LEVEL1,"Want to erase\n");
  391. /* For each block in the old chain (except the targetEUN of course),
  392. free it and make it available for future use */
  393. while (thisEUN <= nftl->lastEUN && thisEUN != targetEUN) {
  394. unsigned int EUNtmp;
  395. EUNtmp = nftl->ReplUnitTable[thisEUN];
  396. if (NFTL_formatblock(nftl, thisEUN) < 0) {
  397. /* could not erase : mark block as reserved
  398. */
  399. nftl->ReplUnitTable[thisEUN] = BLOCK_RESERVED;
  400. } else {
  401. /* correctly erased : mark it as free */
  402. nftl->ReplUnitTable[thisEUN] = BLOCK_FREE;
  403. nftl->numfreeEUNs++;
  404. }
  405. thisEUN = EUNtmp;
  406. }
  407. /* Make this the new start of chain for thisVUC */
  408. nftl->ReplUnitTable[targetEUN] = BLOCK_NIL;
  409. nftl->EUNtable[thisVUC] = targetEUN;
  410. return targetEUN;
  411. }
  412. static u16 NFTL_makefreeblock( struct NFTLrecord *nftl , unsigned pendingblock)
  413. {
  414. /* This is the part that needs some cleverness applied.
  415. For now, I'm doing the minimum applicable to actually
  416. get the thing to work.
  417. Wear-levelling and other clever stuff needs to be implemented
  418. and we also need to do some assessment of the results when
  419. the system loses power half-way through the routine.
  420. */
  421. u16 LongestChain = 0;
  422. u16 ChainLength = 0, thislen;
  423. u16 chain, EUN;
  424. for (chain = 0; chain < le32_to_cpu(nftl->MediaHdr.FormattedSize) / nftl->EraseSize; chain++) {
  425. EUN = nftl->EUNtable[chain];
  426. thislen = 0;
  427. while (EUN <= nftl->lastEUN) {
  428. thislen++;
  429. //printk("VUC %d reaches len %d with EUN %d\n", chain, thislen, EUN);
  430. EUN = nftl->ReplUnitTable[EUN] & 0x7fff;
  431. if (thislen > 0xff00) {
  432. printk("Endless loop in Virtual Chain %d: Unit %x\n",
  433. chain, EUN);
  434. }
  435. if (thislen > 0xff10) {
  436. /* Actually, don't return failure. Just ignore this chain and
  437. get on with it. */
  438. thislen = 0;
  439. break;
  440. }
  441. }
  442. if (thislen > ChainLength) {
  443. //printk("New longest chain is %d with length %d\n", chain, thislen);
  444. ChainLength = thislen;
  445. LongestChain = chain;
  446. }
  447. }
  448. if (ChainLength < 2) {
  449. printk(KERN_WARNING "No Virtual Unit Chains available for folding. "
  450. "Failing request\n");
  451. return 0xffff;
  452. }
  453. return NFTL_foldchain (nftl, LongestChain, pendingblock);
  454. }
  455. /* NFTL_findwriteunit: Return the unit number into which we can write
  456. for this block. Make it available if it isn't already
  457. */
  458. static inline u16 NFTL_findwriteunit(struct NFTLrecord *nftl, unsigned block)
  459. {
  460. u16 lastEUN;
  461. u16 thisVUC = block / (nftl->EraseSize / 512);
  462. struct mtd_info *mtd = nftl->mbd.mtd;
  463. unsigned int writeEUN;
  464. unsigned long blockofs = (block * 512) & (nftl->EraseSize -1);
  465. size_t retlen;
  466. int silly, silly2 = 3;
  467. struct nftl_oob oob;
  468. do {
  469. /* Scan the media to find a unit in the VUC which has
  470. a free space for the block in question.
  471. */
  472. /* This condition catches the 0x[7f]fff cases, as well as
  473. being a sanity check for past-end-of-media access
  474. */
  475. lastEUN = BLOCK_NIL;
  476. writeEUN = nftl->EUNtable[thisVUC];
  477. silly = MAX_LOOPS;
  478. while (writeEUN <= nftl->lastEUN) {
  479. struct nftl_bci bci;
  480. size_t retlen;
  481. unsigned int status;
  482. lastEUN = writeEUN;
  483. nftl_read_oob(mtd,
  484. (writeEUN * nftl->EraseSize) + blockofs,
  485. 8, &retlen, (char *)&bci);
  486. DEBUG(MTD_DEBUG_LEVEL2, "Status of block %d in EUN %d is %x\n",
  487. block , writeEUN, le16_to_cpu(bci.Status));
  488. status = bci.Status | bci.Status1;
  489. switch(status) {
  490. case SECTOR_FREE:
  491. return writeEUN;
  492. case SECTOR_DELETED:
  493. case SECTOR_USED:
  494. case SECTOR_IGNORE:
  495. break;
  496. default:
  497. // Invalid block. Don't use it any more. Must implement.
  498. break;
  499. }
  500. if (!silly--) {
  501. printk(KERN_WARNING
  502. "Infinite loop in Virtual Unit Chain 0x%x\n",
  503. thisVUC);
  504. return 0xffff;
  505. }
  506. /* Skip to next block in chain */
  507. writeEUN = nftl->ReplUnitTable[writeEUN];
  508. }
  509. /* OK. We didn't find one in the existing chain, or there
  510. is no existing chain. */
  511. /* Try to find an already-free block */
  512. writeEUN = NFTL_findfreeblock(nftl, 0);
  513. if (writeEUN == BLOCK_NIL) {
  514. /* That didn't work - there were no free blocks just
  515. waiting to be picked up. We're going to have to fold
  516. a chain to make room.
  517. */
  518. /* First remember the start of this chain */
  519. //u16 startEUN = nftl->EUNtable[thisVUC];
  520. //printk("Write to VirtualUnitChain %d, calling makefreeblock()\n", thisVUC);
  521. writeEUN = NFTL_makefreeblock(nftl, 0xffff);
  522. if (writeEUN == BLOCK_NIL) {
  523. /* OK, we accept that the above comment is
  524. lying - there may have been free blocks
  525. last time we called NFTL_findfreeblock(),
  526. but they are reserved for when we're
  527. desperate. Well, now we're desperate.
  528. */
  529. DEBUG(MTD_DEBUG_LEVEL1, "Using desperate==1 to find free EUN to accommodate write to VUC %d\n", thisVUC);
  530. writeEUN = NFTL_findfreeblock(nftl, 1);
  531. }
  532. if (writeEUN == BLOCK_NIL) {
  533. /* Ouch. This should never happen - we should
  534. always be able to make some room somehow.
  535. If we get here, we've allocated more storage
  536. space than actual media, or our makefreeblock
  537. routine is missing something.
  538. */
  539. printk(KERN_WARNING "Cannot make free space.\n");
  540. return BLOCK_NIL;
  541. }
  542. //printk("Restarting scan\n");
  543. lastEUN = BLOCK_NIL;
  544. continue;
  545. }
  546. /* We've found a free block. Insert it into the chain. */
  547. if (lastEUN != BLOCK_NIL) {
  548. thisVUC |= 0x8000; /* It's a replacement block */
  549. } else {
  550. /* The first block in a new chain */
  551. nftl->EUNtable[thisVUC] = writeEUN;
  552. }
  553. /* set up the actual EUN we're writing into */
  554. /* Both in our cache... */
  555. nftl->ReplUnitTable[writeEUN] = BLOCK_NIL;
  556. /* ... and on the flash itself */
  557. nftl_read_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
  558. &retlen, (char *)&oob.u);
  559. oob.u.a.VirtUnitNum = oob.u.a.SpareVirtUnitNum = cpu_to_le16(thisVUC);
  560. nftl_write_oob(mtd, writeEUN * nftl->EraseSize + 8, 8,
  561. &retlen, (char *)&oob.u);
  562. /* we link the new block to the chain only after the
  563. block is ready. It avoids the case where the chain
  564. could point to a free block */
  565. if (lastEUN != BLOCK_NIL) {
  566. /* Both in our cache... */
  567. nftl->ReplUnitTable[lastEUN] = writeEUN;
  568. /* ... and on the flash itself */
  569. nftl_read_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
  570. 8, &retlen, (char *)&oob.u);
  571. oob.u.a.ReplUnitNum = oob.u.a.SpareReplUnitNum
  572. = cpu_to_le16(writeEUN);
  573. nftl_write_oob(mtd, (lastEUN * nftl->EraseSize) + 8,
  574. 8, &retlen, (char *)&oob.u);
  575. }
  576. return writeEUN;
  577. } while (silly2--);
  578. printk(KERN_WARNING "Error folding to make room for Virtual Unit Chain 0x%x\n",
  579. thisVUC);
  580. return 0xffff;
  581. }
  582. static int nftl_writeblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  583. char *buffer)
  584. {
  585. struct NFTLrecord *nftl = (void *)mbd;
  586. u16 writeEUN;
  587. unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
  588. size_t retlen;
  589. struct nftl_oob oob;
  590. writeEUN = NFTL_findwriteunit(nftl, block);
  591. if (writeEUN == BLOCK_NIL) {
  592. printk(KERN_WARNING
  593. "NFTL_writeblock(): Cannot find block to write to\n");
  594. /* If we _still_ haven't got a block to use, we're screwed */
  595. return 1;
  596. }
  597. memset(&oob, 0xff, sizeof(struct nftl_oob));
  598. oob.b.Status = oob.b.Status1 = SECTOR_USED;
  599. nftl_write(nftl->mbd.mtd, (writeEUN * nftl->EraseSize) + blockofs,
  600. 512, &retlen, (char *)buffer, (char *)&oob);
  601. return 0;
  602. }
  603. #endif /* CONFIG_NFTL_RW */
  604. static int nftl_readblock(struct mtd_blktrans_dev *mbd, unsigned long block,
  605. char *buffer)
  606. {
  607. struct NFTLrecord *nftl = (void *)mbd;
  608. struct mtd_info *mtd = nftl->mbd.mtd;
  609. u16 lastgoodEUN;
  610. u16 thisEUN = nftl->EUNtable[block / (nftl->EraseSize / 512)];
  611. unsigned long blockofs = (block * 512) & (nftl->EraseSize - 1);
  612. unsigned int status;
  613. int silly = MAX_LOOPS;
  614. size_t retlen;
  615. struct nftl_bci bci;
  616. lastgoodEUN = BLOCK_NIL;
  617. if (thisEUN != BLOCK_NIL) {
  618. while (thisEUN < nftl->nb_blocks) {
  619. if (nftl_read_oob(mtd, (thisEUN * nftl->EraseSize) +
  620. blockofs, 8, &retlen,
  621. (char *)&bci) < 0)
  622. status = SECTOR_IGNORE;
  623. else
  624. status = bci.Status | bci.Status1;
  625. switch (status) {
  626. case SECTOR_FREE:
  627. /* no modification of a sector should follow a free sector */
  628. goto the_end;
  629. case SECTOR_DELETED:
  630. lastgoodEUN = BLOCK_NIL;
  631. break;
  632. case SECTOR_USED:
  633. lastgoodEUN = thisEUN;
  634. break;
  635. case SECTOR_IGNORE:
  636. break;
  637. default:
  638. printk("Unknown status for block %ld in EUN %d: %x\n",
  639. block, thisEUN, status);
  640. break;
  641. }
  642. if (!silly--) {
  643. printk(KERN_WARNING "Infinite loop in Virtual Unit Chain 0x%lx\n",
  644. block / (nftl->EraseSize / 512));
  645. return 1;
  646. }
  647. thisEUN = nftl->ReplUnitTable[thisEUN];
  648. }
  649. }
  650. the_end:
  651. if (lastgoodEUN == BLOCK_NIL) {
  652. /* the requested block is not on the media, return all 0x00 */
  653. memset(buffer, 0, 512);
  654. } else {
  655. loff_t ptr = (lastgoodEUN * nftl->EraseSize) + blockofs;
  656. size_t retlen;
  657. int res = mtd->read(mtd, ptr, 512, &retlen, buffer);
  658. if (res < 0 && res != -EUCLEAN)
  659. return -EIO;
  660. }
  661. return 0;
  662. }
  663. static int nftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  664. {
  665. struct NFTLrecord *nftl = (void *)dev;
  666. geo->heads = nftl->heads;
  667. geo->sectors = nftl->sectors;
  668. geo->cylinders = nftl->cylinders;
  669. return 0;
  670. }
  671. /****************************************************************************
  672. *
  673. * Module stuff
  674. *
  675. ****************************************************************************/
  676. static struct mtd_blktrans_ops nftl_tr = {
  677. .name = "nftl",
  678. .major = NFTL_MAJOR,
  679. .part_bits = NFTL_PARTN_BITS,
  680. .getgeo = nftl_getgeo,
  681. .readsect = nftl_readblock,
  682. #ifdef CONFIG_NFTL_RW
  683. .writesect = nftl_writeblock,
  684. #endif
  685. .add_mtd = nftl_add_mtd,
  686. .remove_dev = nftl_remove_dev,
  687. .owner = THIS_MODULE,
  688. };
  689. extern char nftlmountrev[];
  690. static int __init init_nftl(void)
  691. {
  692. printk(KERN_INFO "NFTL driver: nftlcore.c $Revision: 1.98 $, nftlmount.c %s\n", nftlmountrev);
  693. return register_mtd_blktrans(&nftl_tr);
  694. }
  695. static void __exit cleanup_nftl(void)
  696. {
  697. deregister_mtd_blktrans(&nftl_tr);
  698. }
  699. module_init(init_nftl);
  700. module_exit(cleanup_nftl);
  701. MODULE_LICENSE("GPL");
  702. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>, Fabrice Bellard <fabrice.bellard@netgem.com> et al.");
  703. MODULE_DESCRIPTION("Support code for NAND Flash Translation Layer, used on M-Systems DiskOnChip 2000 and Millennium");