nftlcore.c 23 KB

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