nftlcore.c 23 KB

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