ftl.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /* This version ported to the Linux-MTD system by dwmw2@infradead.org
  2. *
  3. * Fixes: Arnaldo Carvalho de Melo <acme@conectiva.com.br>
  4. * - fixes some leaks on failure in build_maps and ftl_notify_add, cleanups
  5. *
  6. * Based on:
  7. */
  8. /*======================================================================
  9. A Flash Translation Layer memory card driver
  10. This driver implements a disk-like block device driver with an
  11. apparent block size of 512 bytes for flash memory cards.
  12. ftl_cs.c 1.62 2000/02/01 00:59:04
  13. The contents of this file are subject to the Mozilla Public
  14. License Version 1.1 (the "License"); you may not use this file
  15. except in compliance with the License. You may obtain a copy of
  16. the License at http://www.mozilla.org/MPL/
  17. Software distributed under the License is distributed on an "AS
  18. IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  19. implied. See the License for the specific language governing
  20. rights and limitations under the License.
  21. The initial developer of the original code is David A. Hinds
  22. <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
  23. are Copyright © 1999 David A. Hinds. All Rights Reserved.
  24. Alternatively, the contents of this file may be used under the
  25. terms of the GNU General Public License version 2 (the "GPL"), in
  26. which case the provisions of the GPL are applicable instead of the
  27. above. If you wish to allow the use of your version of this file
  28. only under the terms of the GPL and not to allow others to use
  29. your version of this file under the MPL, indicate your decision
  30. by deleting the provisions above and replace them with the notice
  31. and other provisions required by the GPL. If you do not delete
  32. the provisions above, a recipient may use your version of this
  33. file under either the MPL or the GPL.
  34. LEGAL NOTE: The FTL format is patented by M-Systems. They have
  35. granted a license for its use with PCMCIA devices:
  36. "M-Systems grants a royalty-free, non-exclusive license under
  37. any presently existing M-Systems intellectual property rights
  38. necessary for the design and development of FTL-compatible
  39. drivers, file systems and utilities using the data formats with
  40. PCMCIA PC Cards as described in the PCMCIA Flash Translation
  41. Layer (FTL) Specification."
  42. Use of the FTL format for non-PCMCIA applications may be an
  43. infringement of these patents. For additional information,
  44. contact M-Systems directly. M-Systems since acquired by Sandisk.
  45. ======================================================================*/
  46. #include <linux/mtd/blktrans.h>
  47. #include <linux/module.h>
  48. #include <linux/mtd/mtd.h>
  49. /*#define PSYCHO_DEBUG */
  50. #include <linux/kernel.h>
  51. #include <linux/ptrace.h>
  52. #include <linux/slab.h>
  53. #include <linux/string.h>
  54. #include <linux/timer.h>
  55. #include <linux/major.h>
  56. #include <linux/fs.h>
  57. #include <linux/init.h>
  58. #include <linux/hdreg.h>
  59. #include <linux/vmalloc.h>
  60. #include <linux/blkpg.h>
  61. #include <asm/uaccess.h>
  62. #include <linux/mtd/ftl.h>
  63. /*====================================================================*/
  64. /* Parameters that can be set with 'insmod' */
  65. static int shuffle_freq = 50;
  66. module_param(shuffle_freq, int, 0);
  67. /*====================================================================*/
  68. /* Major device # for FTL device */
  69. #ifndef FTL_MAJOR
  70. #define FTL_MAJOR 44
  71. #endif
  72. /*====================================================================*/
  73. /* Maximum number of separate memory devices we'll allow */
  74. #define MAX_DEV 4
  75. /* Maximum number of regions per device */
  76. #define MAX_REGION 4
  77. /* Maximum number of partitions in an FTL region */
  78. #define PART_BITS 4
  79. /* Maximum number of outstanding erase requests per socket */
  80. #define MAX_ERASE 8
  81. /* Sector size -- shouldn't need to change */
  82. #define SECTOR_SIZE 512
  83. /* Each memory region corresponds to a minor device */
  84. typedef struct partition_t {
  85. struct mtd_blktrans_dev mbd;
  86. uint32_t state;
  87. uint32_t *VirtualBlockMap;
  88. uint32_t FreeTotal;
  89. struct eun_info_t {
  90. uint32_t Offset;
  91. uint32_t EraseCount;
  92. uint32_t Free;
  93. uint32_t Deleted;
  94. } *EUNInfo;
  95. struct xfer_info_t {
  96. uint32_t Offset;
  97. uint32_t EraseCount;
  98. uint16_t state;
  99. } *XferInfo;
  100. uint16_t bam_index;
  101. uint32_t *bam_cache;
  102. uint16_t DataUnits;
  103. uint32_t BlocksPerUnit;
  104. erase_unit_header_t header;
  105. } partition_t;
  106. /* Partition state flags */
  107. #define FTL_FORMATTED 0x01
  108. /* Transfer unit states */
  109. #define XFER_UNKNOWN 0x00
  110. #define XFER_ERASING 0x01
  111. #define XFER_ERASED 0x02
  112. #define XFER_PREPARED 0x03
  113. #define XFER_FAILED 0x04
  114. /*====================================================================*/
  115. static void ftl_erase_callback(struct erase_info *done);
  116. /*======================================================================
  117. Scan_header() checks to see if a memory region contains an FTL
  118. partition. build_maps() reads all the erase unit headers, builds
  119. the erase unit map, and then builds the virtual page map.
  120. ======================================================================*/
  121. static int scan_header(partition_t *part)
  122. {
  123. erase_unit_header_t header;
  124. loff_t offset, max_offset;
  125. size_t ret;
  126. int err;
  127. part->header.FormattedSize = 0;
  128. max_offset = (0x100000<part->mbd.mtd->size)?0x100000:part->mbd.mtd->size;
  129. /* Search first megabyte for a valid FTL header */
  130. for (offset = 0;
  131. (offset + sizeof(header)) < max_offset;
  132. offset += part->mbd.mtd->erasesize ? : 0x2000) {
  133. err = mtd_read(part->mbd.mtd, offset, sizeof(header), &ret,
  134. (unsigned char *)&header);
  135. if (err)
  136. return err;
  137. if (strcmp(header.DataOrgTuple+3, "FTL100") == 0) break;
  138. }
  139. if (offset == max_offset) {
  140. printk(KERN_NOTICE "ftl_cs: FTL header not found.\n");
  141. return -ENOENT;
  142. }
  143. if (header.BlockSize != 9 ||
  144. (header.EraseUnitSize < 10) || (header.EraseUnitSize > 31) ||
  145. (header.NumTransferUnits >= le16_to_cpu(header.NumEraseUnits))) {
  146. printk(KERN_NOTICE "ftl_cs: FTL header corrupt!\n");
  147. return -1;
  148. }
  149. if ((1 << header.EraseUnitSize) != part->mbd.mtd->erasesize) {
  150. printk(KERN_NOTICE "ftl: FTL EraseUnitSize %x != MTD erasesize %x\n",
  151. 1 << header.EraseUnitSize,part->mbd.mtd->erasesize);
  152. return -1;
  153. }
  154. part->header = header;
  155. return 0;
  156. }
  157. static int build_maps(partition_t *part)
  158. {
  159. erase_unit_header_t header;
  160. uint16_t xvalid, xtrans, i;
  161. unsigned blocks, j;
  162. int hdr_ok, ret = -1;
  163. ssize_t retval;
  164. loff_t offset;
  165. /* Set up erase unit maps */
  166. part->DataUnits = le16_to_cpu(part->header.NumEraseUnits) -
  167. part->header.NumTransferUnits;
  168. part->EUNInfo = kmalloc(part->DataUnits * sizeof(struct eun_info_t),
  169. GFP_KERNEL);
  170. if (!part->EUNInfo)
  171. goto out;
  172. for (i = 0; i < part->DataUnits; i++)
  173. part->EUNInfo[i].Offset = 0xffffffff;
  174. part->XferInfo =
  175. kmalloc(part->header.NumTransferUnits * sizeof(struct xfer_info_t),
  176. GFP_KERNEL);
  177. if (!part->XferInfo)
  178. goto out_EUNInfo;
  179. xvalid = xtrans = 0;
  180. for (i = 0; i < le16_to_cpu(part->header.NumEraseUnits); i++) {
  181. offset = ((i + le16_to_cpu(part->header.FirstPhysicalEUN))
  182. << part->header.EraseUnitSize);
  183. ret = mtd_read(part->mbd.mtd, offset, sizeof(header), &retval,
  184. (unsigned char *)&header);
  185. if (ret)
  186. goto out_XferInfo;
  187. ret = -1;
  188. /* Is this a transfer partition? */
  189. hdr_ok = (strcmp(header.DataOrgTuple+3, "FTL100") == 0);
  190. if (hdr_ok && (le16_to_cpu(header.LogicalEUN) < part->DataUnits) &&
  191. (part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset == 0xffffffff)) {
  192. part->EUNInfo[le16_to_cpu(header.LogicalEUN)].Offset = offset;
  193. part->EUNInfo[le16_to_cpu(header.LogicalEUN)].EraseCount =
  194. le32_to_cpu(header.EraseCount);
  195. xvalid++;
  196. } else {
  197. if (xtrans == part->header.NumTransferUnits) {
  198. printk(KERN_NOTICE "ftl_cs: format error: too many "
  199. "transfer units!\n");
  200. goto out_XferInfo;
  201. }
  202. if (hdr_ok && (le16_to_cpu(header.LogicalEUN) == 0xffff)) {
  203. part->XferInfo[xtrans].state = XFER_PREPARED;
  204. part->XferInfo[xtrans].EraseCount = le32_to_cpu(header.EraseCount);
  205. } else {
  206. part->XferInfo[xtrans].state = XFER_UNKNOWN;
  207. /* Pick anything reasonable for the erase count */
  208. part->XferInfo[xtrans].EraseCount =
  209. le32_to_cpu(part->header.EraseCount);
  210. }
  211. part->XferInfo[xtrans].Offset = offset;
  212. xtrans++;
  213. }
  214. }
  215. /* Check for format trouble */
  216. header = part->header;
  217. if ((xtrans != header.NumTransferUnits) ||
  218. (xvalid+xtrans != le16_to_cpu(header.NumEraseUnits))) {
  219. printk(KERN_NOTICE "ftl_cs: format error: erase units "
  220. "don't add up!\n");
  221. goto out_XferInfo;
  222. }
  223. /* Set up virtual page map */
  224. blocks = le32_to_cpu(header.FormattedSize) >> header.BlockSize;
  225. part->VirtualBlockMap = vmalloc(blocks * sizeof(uint32_t));
  226. if (!part->VirtualBlockMap)
  227. goto out_XferInfo;
  228. memset(part->VirtualBlockMap, 0xff, blocks * sizeof(uint32_t));
  229. part->BlocksPerUnit = (1 << header.EraseUnitSize) >> header.BlockSize;
  230. part->bam_cache = kmalloc(part->BlocksPerUnit * sizeof(uint32_t),
  231. GFP_KERNEL);
  232. if (!part->bam_cache)
  233. goto out_VirtualBlockMap;
  234. part->bam_index = 0xffff;
  235. part->FreeTotal = 0;
  236. for (i = 0; i < part->DataUnits; i++) {
  237. part->EUNInfo[i].Free = 0;
  238. part->EUNInfo[i].Deleted = 0;
  239. offset = part->EUNInfo[i].Offset + le32_to_cpu(header.BAMOffset);
  240. ret = mtd_read(part->mbd.mtd, offset,
  241. part->BlocksPerUnit * sizeof(uint32_t), &retval,
  242. (unsigned char *)part->bam_cache);
  243. if (ret)
  244. goto out_bam_cache;
  245. for (j = 0; j < part->BlocksPerUnit; j++) {
  246. if (BLOCK_FREE(le32_to_cpu(part->bam_cache[j]))) {
  247. part->EUNInfo[i].Free++;
  248. part->FreeTotal++;
  249. } else if ((BLOCK_TYPE(le32_to_cpu(part->bam_cache[j])) == BLOCK_DATA) &&
  250. (BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j])) < blocks))
  251. part->VirtualBlockMap[BLOCK_NUMBER(le32_to_cpu(part->bam_cache[j]))] =
  252. (i << header.EraseUnitSize) + (j << header.BlockSize);
  253. else if (BLOCK_DELETED(le32_to_cpu(part->bam_cache[j])))
  254. part->EUNInfo[i].Deleted++;
  255. }
  256. }
  257. ret = 0;
  258. goto out;
  259. out_bam_cache:
  260. kfree(part->bam_cache);
  261. out_VirtualBlockMap:
  262. vfree(part->VirtualBlockMap);
  263. out_XferInfo:
  264. kfree(part->XferInfo);
  265. out_EUNInfo:
  266. kfree(part->EUNInfo);
  267. out:
  268. return ret;
  269. } /* build_maps */
  270. /*======================================================================
  271. Erase_xfer() schedules an asynchronous erase operation for a
  272. transfer unit.
  273. ======================================================================*/
  274. static int erase_xfer(partition_t *part,
  275. uint16_t xfernum)
  276. {
  277. int ret;
  278. struct xfer_info_t *xfer;
  279. struct erase_info *erase;
  280. xfer = &part->XferInfo[xfernum];
  281. pr_debug("ftl_cs: erasing xfer unit at 0x%x\n", xfer->Offset);
  282. xfer->state = XFER_ERASING;
  283. /* Is there a free erase slot? Always in MTD. */
  284. erase=kmalloc(sizeof(struct erase_info), GFP_KERNEL);
  285. if (!erase)
  286. return -ENOMEM;
  287. erase->mtd = part->mbd.mtd;
  288. erase->callback = ftl_erase_callback;
  289. erase->addr = xfer->Offset;
  290. erase->len = 1 << part->header.EraseUnitSize;
  291. erase->priv = (u_long)part;
  292. ret = mtd_erase(part->mbd.mtd, erase);
  293. if (!ret)
  294. xfer->EraseCount++;
  295. else
  296. kfree(erase);
  297. return ret;
  298. } /* erase_xfer */
  299. /*======================================================================
  300. Prepare_xfer() takes a freshly erased transfer unit and gives
  301. it an appropriate header.
  302. ======================================================================*/
  303. static void ftl_erase_callback(struct erase_info *erase)
  304. {
  305. partition_t *part;
  306. struct xfer_info_t *xfer;
  307. int i;
  308. /* Look up the transfer unit */
  309. part = (partition_t *)(erase->priv);
  310. for (i = 0; i < part->header.NumTransferUnits; i++)
  311. if (part->XferInfo[i].Offset == erase->addr) break;
  312. if (i == part->header.NumTransferUnits) {
  313. printk(KERN_NOTICE "ftl_cs: internal error: "
  314. "erase lookup failed!\n");
  315. return;
  316. }
  317. xfer = &part->XferInfo[i];
  318. if (erase->state == MTD_ERASE_DONE)
  319. xfer->state = XFER_ERASED;
  320. else {
  321. xfer->state = XFER_FAILED;
  322. printk(KERN_NOTICE "ftl_cs: erase failed: state = %d\n",
  323. erase->state);
  324. }
  325. kfree(erase);
  326. } /* ftl_erase_callback */
  327. static int prepare_xfer(partition_t *part, int i)
  328. {
  329. erase_unit_header_t header;
  330. struct xfer_info_t *xfer;
  331. int nbam, ret;
  332. uint32_t ctl;
  333. ssize_t retlen;
  334. loff_t offset;
  335. xfer = &part->XferInfo[i];
  336. xfer->state = XFER_FAILED;
  337. pr_debug("ftl_cs: preparing xfer unit at 0x%x\n", xfer->Offset);
  338. /* Write the transfer unit header */
  339. header = part->header;
  340. header.LogicalEUN = cpu_to_le16(0xffff);
  341. header.EraseCount = cpu_to_le32(xfer->EraseCount);
  342. ret = mtd_write(part->mbd.mtd, xfer->Offset, sizeof(header), &retlen,
  343. (u_char *)&header);
  344. if (ret) {
  345. return ret;
  346. }
  347. /* Write the BAM stub */
  348. nbam = (part->BlocksPerUnit * sizeof(uint32_t) +
  349. le32_to_cpu(part->header.BAMOffset) + SECTOR_SIZE - 1) / SECTOR_SIZE;
  350. offset = xfer->Offset + le32_to_cpu(part->header.BAMOffset);
  351. ctl = cpu_to_le32(BLOCK_CONTROL);
  352. for (i = 0; i < nbam; i++, offset += sizeof(uint32_t)) {
  353. ret = mtd_write(part->mbd.mtd, offset, sizeof(uint32_t), &retlen,
  354. (u_char *)&ctl);
  355. if (ret)
  356. return ret;
  357. }
  358. xfer->state = XFER_PREPARED;
  359. return 0;
  360. } /* prepare_xfer */
  361. /*======================================================================
  362. Copy_erase_unit() takes a full erase block and a transfer unit,
  363. copies everything to the transfer unit, then swaps the block
  364. pointers.
  365. All data blocks are copied to the corresponding blocks in the
  366. target unit, so the virtual block map does not need to be
  367. updated.
  368. ======================================================================*/
  369. static int copy_erase_unit(partition_t *part, uint16_t srcunit,
  370. uint16_t xferunit)
  371. {
  372. u_char buf[SECTOR_SIZE];
  373. struct eun_info_t *eun;
  374. struct xfer_info_t *xfer;
  375. uint32_t src, dest, free, i;
  376. uint16_t unit;
  377. int ret;
  378. ssize_t retlen;
  379. loff_t offset;
  380. uint16_t srcunitswap = cpu_to_le16(srcunit);
  381. eun = &part->EUNInfo[srcunit];
  382. xfer = &part->XferInfo[xferunit];
  383. pr_debug("ftl_cs: copying block 0x%x to 0x%x\n",
  384. eun->Offset, xfer->Offset);
  385. /* Read current BAM */
  386. if (part->bam_index != srcunit) {
  387. offset = eun->Offset + le32_to_cpu(part->header.BAMOffset);
  388. ret = mtd_read(part->mbd.mtd, offset,
  389. part->BlocksPerUnit * sizeof(uint32_t), &retlen,
  390. (u_char *)(part->bam_cache));
  391. /* mark the cache bad, in case we get an error later */
  392. part->bam_index = 0xffff;
  393. if (ret) {
  394. printk( KERN_WARNING "ftl: Failed to read BAM cache in copy_erase_unit()!\n");
  395. return ret;
  396. }
  397. }
  398. /* Write the LogicalEUN for the transfer unit */
  399. xfer->state = XFER_UNKNOWN;
  400. offset = xfer->Offset + 20; /* Bad! */
  401. unit = cpu_to_le16(0x7fff);
  402. ret = mtd_write(part->mbd.mtd, offset, sizeof(uint16_t), &retlen,
  403. (u_char *)&unit);
  404. if (ret) {
  405. printk( KERN_WARNING "ftl: Failed to write back to BAM cache in copy_erase_unit()!\n");
  406. return ret;
  407. }
  408. /* Copy all data blocks from source unit to transfer unit */
  409. src = eun->Offset; dest = xfer->Offset;
  410. free = 0;
  411. ret = 0;
  412. for (i = 0; i < part->BlocksPerUnit; i++) {
  413. switch (BLOCK_TYPE(le32_to_cpu(part->bam_cache[i]))) {
  414. case BLOCK_CONTROL:
  415. /* This gets updated later */
  416. break;
  417. case BLOCK_DATA:
  418. case BLOCK_REPLACEMENT:
  419. ret = mtd_read(part->mbd.mtd, src, SECTOR_SIZE, &retlen,
  420. (u_char *)buf);
  421. if (ret) {
  422. printk(KERN_WARNING "ftl: Error reading old xfer unit in copy_erase_unit\n");
  423. return ret;
  424. }
  425. ret = mtd_write(part->mbd.mtd, dest, SECTOR_SIZE, &retlen,
  426. (u_char *)buf);
  427. if (ret) {
  428. printk(KERN_WARNING "ftl: Error writing new xfer unit in copy_erase_unit\n");
  429. return ret;
  430. }
  431. break;
  432. default:
  433. /* All other blocks must be free */
  434. part->bam_cache[i] = cpu_to_le32(0xffffffff);
  435. free++;
  436. break;
  437. }
  438. src += SECTOR_SIZE;
  439. dest += SECTOR_SIZE;
  440. }
  441. /* Write the BAM to the transfer unit */
  442. ret = mtd_write(part->mbd.mtd,
  443. xfer->Offset + le32_to_cpu(part->header.BAMOffset),
  444. part->BlocksPerUnit * sizeof(int32_t),
  445. &retlen,
  446. (u_char *)part->bam_cache);
  447. if (ret) {
  448. printk( KERN_WARNING "ftl: Error writing BAM in copy_erase_unit\n");
  449. return ret;
  450. }
  451. /* All clear? Then update the LogicalEUN again */
  452. ret = mtd_write(part->mbd.mtd, xfer->Offset + 20, sizeof(uint16_t),
  453. &retlen, (u_char *)&srcunitswap);
  454. if (ret) {
  455. printk(KERN_WARNING "ftl: Error writing new LogicalEUN in copy_erase_unit\n");
  456. return ret;
  457. }
  458. /* Update the maps and usage stats*/
  459. swap(xfer->EraseCount, eun->EraseCount);
  460. swap(xfer->Offset, eun->Offset);
  461. part->FreeTotal -= eun->Free;
  462. part->FreeTotal += free;
  463. eun->Free = free;
  464. eun->Deleted = 0;
  465. /* Now, the cache should be valid for the new block */
  466. part->bam_index = srcunit;
  467. return 0;
  468. } /* copy_erase_unit */
  469. /*======================================================================
  470. reclaim_block() picks a full erase unit and a transfer unit and
  471. then calls copy_erase_unit() to copy one to the other. Then, it
  472. schedules an erase on the expired block.
  473. What's a good way to decide which transfer unit and which erase
  474. unit to use? Beats me. My way is to always pick the transfer
  475. unit with the fewest erases, and usually pick the data unit with
  476. the most deleted blocks. But with a small probability, pick the
  477. oldest data unit instead. This means that we generally postpone
  478. the next reclamation as long as possible, but shuffle static
  479. stuff around a bit for wear leveling.
  480. ======================================================================*/
  481. static int reclaim_block(partition_t *part)
  482. {
  483. uint16_t i, eun, xfer;
  484. uint32_t best;
  485. int queued, ret;
  486. pr_debug("ftl_cs: reclaiming space...\n");
  487. pr_debug("NumTransferUnits == %x\n", part->header.NumTransferUnits);
  488. /* Pick the least erased transfer unit */
  489. best = 0xffffffff; xfer = 0xffff;
  490. do {
  491. queued = 0;
  492. for (i = 0; i < part->header.NumTransferUnits; i++) {
  493. int n=0;
  494. if (part->XferInfo[i].state == XFER_UNKNOWN) {
  495. pr_debug("XferInfo[%d].state == XFER_UNKNOWN\n",i);
  496. n=1;
  497. erase_xfer(part, i);
  498. }
  499. if (part->XferInfo[i].state == XFER_ERASING) {
  500. pr_debug("XferInfo[%d].state == XFER_ERASING\n",i);
  501. n=1;
  502. queued = 1;
  503. }
  504. else if (part->XferInfo[i].state == XFER_ERASED) {
  505. pr_debug("XferInfo[%d].state == XFER_ERASED\n",i);
  506. n=1;
  507. prepare_xfer(part, i);
  508. }
  509. if (part->XferInfo[i].state == XFER_PREPARED) {
  510. pr_debug("XferInfo[%d].state == XFER_PREPARED\n",i);
  511. n=1;
  512. if (part->XferInfo[i].EraseCount <= best) {
  513. best = part->XferInfo[i].EraseCount;
  514. xfer = i;
  515. }
  516. }
  517. if (!n)
  518. pr_debug("XferInfo[%d].state == %x\n",i, part->XferInfo[i].state);
  519. }
  520. if (xfer == 0xffff) {
  521. if (queued) {
  522. pr_debug("ftl_cs: waiting for transfer "
  523. "unit to be prepared...\n");
  524. mtd_sync(part->mbd.mtd);
  525. } else {
  526. static int ne = 0;
  527. if (++ne < 5)
  528. printk(KERN_NOTICE "ftl_cs: reclaim failed: no "
  529. "suitable transfer units!\n");
  530. else
  531. pr_debug("ftl_cs: reclaim failed: no "
  532. "suitable transfer units!\n");
  533. return -EIO;
  534. }
  535. }
  536. } while (xfer == 0xffff);
  537. eun = 0;
  538. if ((jiffies % shuffle_freq) == 0) {
  539. pr_debug("ftl_cs: recycling freshest block...\n");
  540. best = 0xffffffff;
  541. for (i = 0; i < part->DataUnits; i++)
  542. if (part->EUNInfo[i].EraseCount <= best) {
  543. best = part->EUNInfo[i].EraseCount;
  544. eun = i;
  545. }
  546. } else {
  547. best = 0;
  548. for (i = 0; i < part->DataUnits; i++)
  549. if (part->EUNInfo[i].Deleted >= best) {
  550. best = part->EUNInfo[i].Deleted;
  551. eun = i;
  552. }
  553. if (best == 0) {
  554. static int ne = 0;
  555. if (++ne < 5)
  556. printk(KERN_NOTICE "ftl_cs: reclaim failed: "
  557. "no free blocks!\n");
  558. else
  559. pr_debug("ftl_cs: reclaim failed: "
  560. "no free blocks!\n");
  561. return -EIO;
  562. }
  563. }
  564. ret = copy_erase_unit(part, eun, xfer);
  565. if (!ret)
  566. erase_xfer(part, xfer);
  567. else
  568. printk(KERN_NOTICE "ftl_cs: copy_erase_unit failed!\n");
  569. return ret;
  570. } /* reclaim_block */
  571. /*======================================================================
  572. Find_free() searches for a free block. If necessary, it updates
  573. the BAM cache for the erase unit containing the free block. It
  574. returns the block index -- the erase unit is just the currently
  575. cached unit. If there are no free blocks, it returns 0 -- this
  576. is never a valid data block because it contains the header.
  577. ======================================================================*/
  578. #ifdef PSYCHO_DEBUG
  579. static void dump_lists(partition_t *part)
  580. {
  581. int i;
  582. printk(KERN_DEBUG "ftl_cs: Free total = %d\n", part->FreeTotal);
  583. for (i = 0; i < part->DataUnits; i++)
  584. printk(KERN_DEBUG "ftl_cs: unit %d: %d phys, %d free, "
  585. "%d deleted\n", i,
  586. part->EUNInfo[i].Offset >> part->header.EraseUnitSize,
  587. part->EUNInfo[i].Free, part->EUNInfo[i].Deleted);
  588. }
  589. #endif
  590. static uint32_t find_free(partition_t *part)
  591. {
  592. uint16_t stop, eun;
  593. uint32_t blk;
  594. size_t retlen;
  595. int ret;
  596. /* Find an erase unit with some free space */
  597. stop = (part->bam_index == 0xffff) ? 0 : part->bam_index;
  598. eun = stop;
  599. do {
  600. if (part->EUNInfo[eun].Free != 0) break;
  601. /* Wrap around at end of table */
  602. if (++eun == part->DataUnits) eun = 0;
  603. } while (eun != stop);
  604. if (part->EUNInfo[eun].Free == 0)
  605. return 0;
  606. /* Is this unit's BAM cached? */
  607. if (eun != part->bam_index) {
  608. /* Invalidate cache */
  609. part->bam_index = 0xffff;
  610. ret = mtd_read(part->mbd.mtd,
  611. part->EUNInfo[eun].Offset + le32_to_cpu(part->header.BAMOffset),
  612. part->BlocksPerUnit * sizeof(uint32_t),
  613. &retlen,
  614. (u_char *)(part->bam_cache));
  615. if (ret) {
  616. printk(KERN_WARNING"ftl: Error reading BAM in find_free\n");
  617. return 0;
  618. }
  619. part->bam_index = eun;
  620. }
  621. /* Find a free block */
  622. for (blk = 0; blk < part->BlocksPerUnit; blk++)
  623. if (BLOCK_FREE(le32_to_cpu(part->bam_cache[blk]))) break;
  624. if (blk == part->BlocksPerUnit) {
  625. #ifdef PSYCHO_DEBUG
  626. static int ne = 0;
  627. if (++ne == 1)
  628. dump_lists(part);
  629. #endif
  630. printk(KERN_NOTICE "ftl_cs: bad free list!\n");
  631. return 0;
  632. }
  633. pr_debug("ftl_cs: found free block at %d in %d\n", blk, eun);
  634. return blk;
  635. } /* find_free */
  636. /*======================================================================
  637. Read a series of sectors from an FTL partition.
  638. ======================================================================*/
  639. static int ftl_read(partition_t *part, caddr_t buffer,
  640. u_long sector, u_long nblocks)
  641. {
  642. uint32_t log_addr, bsize;
  643. u_long i;
  644. int ret;
  645. size_t offset, retlen;
  646. pr_debug("ftl_cs: ftl_read(0x%p, 0x%lx, %ld)\n",
  647. part, sector, nblocks);
  648. if (!(part->state & FTL_FORMATTED)) {
  649. printk(KERN_NOTICE "ftl_cs: bad partition\n");
  650. return -EIO;
  651. }
  652. bsize = 1 << part->header.EraseUnitSize;
  653. for (i = 0; i < nblocks; i++) {
  654. if (((sector+i) * SECTOR_SIZE) >= le32_to_cpu(part->header.FormattedSize)) {
  655. printk(KERN_NOTICE "ftl_cs: bad read offset\n");
  656. return -EIO;
  657. }
  658. log_addr = part->VirtualBlockMap[sector+i];
  659. if (log_addr == 0xffffffff)
  660. memset(buffer, 0, SECTOR_SIZE);
  661. else {
  662. offset = (part->EUNInfo[log_addr / bsize].Offset
  663. + (log_addr % bsize));
  664. ret = mtd_read(part->mbd.mtd, offset, SECTOR_SIZE, &retlen,
  665. (u_char *)buffer);
  666. if (ret) {
  667. printk(KERN_WARNING "Error reading MTD device in ftl_read()\n");
  668. return ret;
  669. }
  670. }
  671. buffer += SECTOR_SIZE;
  672. }
  673. return 0;
  674. } /* ftl_read */
  675. /*======================================================================
  676. Write a series of sectors to an FTL partition
  677. ======================================================================*/
  678. static int set_bam_entry(partition_t *part, uint32_t log_addr,
  679. uint32_t virt_addr)
  680. {
  681. uint32_t bsize, blk, le_virt_addr;
  682. #ifdef PSYCHO_DEBUG
  683. uint32_t old_addr;
  684. #endif
  685. uint16_t eun;
  686. int ret;
  687. size_t retlen, offset;
  688. pr_debug("ftl_cs: set_bam_entry(0x%p, 0x%x, 0x%x)\n",
  689. part, log_addr, virt_addr);
  690. bsize = 1 << part->header.EraseUnitSize;
  691. eun = log_addr / bsize;
  692. blk = (log_addr % bsize) / SECTOR_SIZE;
  693. offset = (part->EUNInfo[eun].Offset + blk * sizeof(uint32_t) +
  694. le32_to_cpu(part->header.BAMOffset));
  695. #ifdef PSYCHO_DEBUG
  696. ret = mtd_read(part->mbd.mtd, offset, sizeof(uint32_t), &retlen,
  697. (u_char *)&old_addr);
  698. if (ret) {
  699. printk(KERN_WARNING"ftl: Error reading old_addr in set_bam_entry: %d\n",ret);
  700. return ret;
  701. }
  702. old_addr = le32_to_cpu(old_addr);
  703. if (((virt_addr == 0xfffffffe) && !BLOCK_FREE(old_addr)) ||
  704. ((virt_addr == 0) && (BLOCK_TYPE(old_addr) != BLOCK_DATA)) ||
  705. (!BLOCK_DELETED(virt_addr) && (old_addr != 0xfffffffe))) {
  706. static int ne = 0;
  707. if (++ne < 5) {
  708. printk(KERN_NOTICE "ftl_cs: set_bam_entry() inconsistency!\n");
  709. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, old = 0x%x"
  710. ", new = 0x%x\n", log_addr, old_addr, virt_addr);
  711. }
  712. return -EIO;
  713. }
  714. #endif
  715. le_virt_addr = cpu_to_le32(virt_addr);
  716. if (part->bam_index == eun) {
  717. #ifdef PSYCHO_DEBUG
  718. if (le32_to_cpu(part->bam_cache[blk]) != old_addr) {
  719. static int ne = 0;
  720. if (++ne < 5) {
  721. printk(KERN_NOTICE "ftl_cs: set_bam_entry() "
  722. "inconsistency!\n");
  723. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, cache"
  724. " = 0x%x\n",
  725. le32_to_cpu(part->bam_cache[blk]), old_addr);
  726. }
  727. return -EIO;
  728. }
  729. #endif
  730. part->bam_cache[blk] = le_virt_addr;
  731. }
  732. ret = mtd_write(part->mbd.mtd, offset, sizeof(uint32_t), &retlen,
  733. (u_char *)&le_virt_addr);
  734. if (ret) {
  735. printk(KERN_NOTICE "ftl_cs: set_bam_entry() failed!\n");
  736. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, new = 0x%x\n",
  737. log_addr, virt_addr);
  738. }
  739. return ret;
  740. } /* set_bam_entry */
  741. static int ftl_write(partition_t *part, caddr_t buffer,
  742. u_long sector, u_long nblocks)
  743. {
  744. uint32_t bsize, log_addr, virt_addr, old_addr, blk;
  745. u_long i;
  746. int ret;
  747. size_t retlen, offset;
  748. pr_debug("ftl_cs: ftl_write(0x%p, %ld, %ld)\n",
  749. part, sector, nblocks);
  750. if (!(part->state & FTL_FORMATTED)) {
  751. printk(KERN_NOTICE "ftl_cs: bad partition\n");
  752. return -EIO;
  753. }
  754. /* See if we need to reclaim space, before we start */
  755. while (part->FreeTotal < nblocks) {
  756. ret = reclaim_block(part);
  757. if (ret)
  758. return ret;
  759. }
  760. bsize = 1 << part->header.EraseUnitSize;
  761. virt_addr = sector * SECTOR_SIZE | BLOCK_DATA;
  762. for (i = 0; i < nblocks; i++) {
  763. if (virt_addr >= le32_to_cpu(part->header.FormattedSize)) {
  764. printk(KERN_NOTICE "ftl_cs: bad write offset\n");
  765. return -EIO;
  766. }
  767. /* Grab a free block */
  768. blk = find_free(part);
  769. if (blk == 0) {
  770. static int ne = 0;
  771. if (++ne < 5)
  772. printk(KERN_NOTICE "ftl_cs: internal error: "
  773. "no free blocks!\n");
  774. return -ENOSPC;
  775. }
  776. /* Tag the BAM entry, and write the new block */
  777. log_addr = part->bam_index * bsize + blk * SECTOR_SIZE;
  778. part->EUNInfo[part->bam_index].Free--;
  779. part->FreeTotal--;
  780. if (set_bam_entry(part, log_addr, 0xfffffffe))
  781. return -EIO;
  782. part->EUNInfo[part->bam_index].Deleted++;
  783. offset = (part->EUNInfo[part->bam_index].Offset +
  784. blk * SECTOR_SIZE);
  785. ret = mtd_write(part->mbd.mtd, offset, SECTOR_SIZE, &retlen, buffer);
  786. if (ret) {
  787. printk(KERN_NOTICE "ftl_cs: block write failed!\n");
  788. printk(KERN_NOTICE "ftl_cs: log_addr = 0x%x, virt_addr"
  789. " = 0x%x, Offset = 0x%zx\n", log_addr, virt_addr,
  790. offset);
  791. return -EIO;
  792. }
  793. /* Only delete the old entry when the new entry is ready */
  794. old_addr = part->VirtualBlockMap[sector+i];
  795. if (old_addr != 0xffffffff) {
  796. part->VirtualBlockMap[sector+i] = 0xffffffff;
  797. part->EUNInfo[old_addr/bsize].Deleted++;
  798. if (set_bam_entry(part, old_addr, 0))
  799. return -EIO;
  800. }
  801. /* Finally, set up the new pointers */
  802. if (set_bam_entry(part, log_addr, virt_addr))
  803. return -EIO;
  804. part->VirtualBlockMap[sector+i] = log_addr;
  805. part->EUNInfo[part->bam_index].Deleted--;
  806. buffer += SECTOR_SIZE;
  807. virt_addr += SECTOR_SIZE;
  808. }
  809. return 0;
  810. } /* ftl_write */
  811. static int ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  812. {
  813. partition_t *part = (void *)dev;
  814. u_long sect;
  815. /* Sort of arbitrary: round size down to 4KiB boundary */
  816. sect = le32_to_cpu(part->header.FormattedSize)/SECTOR_SIZE;
  817. geo->heads = 1;
  818. geo->sectors = 8;
  819. geo->cylinders = sect >> 3;
  820. return 0;
  821. }
  822. static int ftl_readsect(struct mtd_blktrans_dev *dev,
  823. unsigned long block, char *buf)
  824. {
  825. return ftl_read((void *)dev, buf, block, 1);
  826. }
  827. static int ftl_writesect(struct mtd_blktrans_dev *dev,
  828. unsigned long block, char *buf)
  829. {
  830. return ftl_write((void *)dev, buf, block, 1);
  831. }
  832. static int ftl_discardsect(struct mtd_blktrans_dev *dev,
  833. unsigned long sector, unsigned nr_sects)
  834. {
  835. partition_t *part = (void *)dev;
  836. uint32_t bsize = 1 << part->header.EraseUnitSize;
  837. pr_debug("FTL erase sector %ld for %d sectors\n",
  838. sector, nr_sects);
  839. while (nr_sects) {
  840. uint32_t old_addr = part->VirtualBlockMap[sector];
  841. if (old_addr != 0xffffffff) {
  842. part->VirtualBlockMap[sector] = 0xffffffff;
  843. part->EUNInfo[old_addr/bsize].Deleted++;
  844. if (set_bam_entry(part, old_addr, 0))
  845. return -EIO;
  846. }
  847. nr_sects--;
  848. sector++;
  849. }
  850. return 0;
  851. }
  852. /*====================================================================*/
  853. static void ftl_freepart(partition_t *part)
  854. {
  855. vfree(part->VirtualBlockMap);
  856. part->VirtualBlockMap = NULL;
  857. kfree(part->EUNInfo);
  858. part->EUNInfo = NULL;
  859. kfree(part->XferInfo);
  860. part->XferInfo = NULL;
  861. kfree(part->bam_cache);
  862. part->bam_cache = NULL;
  863. } /* ftl_freepart */
  864. static void ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  865. {
  866. partition_t *partition;
  867. partition = kzalloc(sizeof(partition_t), GFP_KERNEL);
  868. if (!partition) {
  869. printk(KERN_WARNING "No memory to scan for FTL on %s\n",
  870. mtd->name);
  871. return;
  872. }
  873. partition->mbd.mtd = mtd;
  874. if ((scan_header(partition) == 0) &&
  875. (build_maps(partition) == 0)) {
  876. partition->state = FTL_FORMATTED;
  877. #ifdef PCMCIA_DEBUG
  878. printk(KERN_INFO "ftl_cs: opening %d KiB FTL partition\n",
  879. le32_to_cpu(partition->header.FormattedSize) >> 10);
  880. #endif
  881. partition->mbd.size = le32_to_cpu(partition->header.FormattedSize) >> 9;
  882. partition->mbd.tr = tr;
  883. partition->mbd.devnum = -1;
  884. if (!add_mtd_blktrans_dev((void *)partition))
  885. return;
  886. }
  887. kfree(partition);
  888. }
  889. static void ftl_remove_dev(struct mtd_blktrans_dev *dev)
  890. {
  891. del_mtd_blktrans_dev(dev);
  892. ftl_freepart((partition_t *)dev);
  893. }
  894. static struct mtd_blktrans_ops ftl_tr = {
  895. .name = "ftl",
  896. .major = FTL_MAJOR,
  897. .part_bits = PART_BITS,
  898. .blksize = SECTOR_SIZE,
  899. .readsect = ftl_readsect,
  900. .writesect = ftl_writesect,
  901. .discard = ftl_discardsect,
  902. .getgeo = ftl_getgeo,
  903. .add_mtd = ftl_add_mtd,
  904. .remove_dev = ftl_remove_dev,
  905. .owner = THIS_MODULE,
  906. };
  907. static int __init init_ftl(void)
  908. {
  909. return register_mtd_blktrans(&ftl_tr);
  910. }
  911. static void __exit cleanup_ftl(void)
  912. {
  913. deregister_mtd_blktrans(&ftl_tr);
  914. }
  915. module_init(init_ftl);
  916. module_exit(cleanup_ftl);
  917. MODULE_LICENSE("Dual MPL/GPL");
  918. MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
  919. MODULE_DESCRIPTION("Support code for Flash Translation Layer, used on PCMCIA devices");