rfd_ftl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /*
  2. * rfd_ftl.c -- resident flash disk (flash translation layer)
  3. *
  4. * Copyright © 2005 Sean Young <sean@mess.org>
  5. *
  6. * This type of flash translation layer (FTL) is used by the Embedded BIOS
  7. * by General Software. It is known as the Resident Flash Disk (RFD), see:
  8. *
  9. * http://www.gensw.com/pages/prod/bios/rfd.htm
  10. *
  11. * based on ftl.c
  12. */
  13. #include <linux/hdreg.h>
  14. #include <linux/init.h>
  15. #include <linux/mtd/blktrans.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/slab.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/module.h>
  21. #include <asm/types.h>
  22. static int block_size = 0;
  23. module_param(block_size, int, 0);
  24. MODULE_PARM_DESC(block_size, "Block size to use by RFD, defaults to erase unit size");
  25. #define PREFIX "rfd_ftl: "
  26. /* This major has been assigned by device@lanana.org */
  27. #ifndef RFD_FTL_MAJOR
  28. #define RFD_FTL_MAJOR 256
  29. #endif
  30. /* Maximum number of partitions in an FTL region */
  31. #define PART_BITS 4
  32. /* An erase unit should start with this value */
  33. #define RFD_MAGIC 0x9193
  34. /* the second value is 0xffff or 0xffc8; function unknown */
  35. /* the third value is always 0xffff, ignored */
  36. /* next is an array of mapping for each corresponding sector */
  37. #define HEADER_MAP_OFFSET 3
  38. #define SECTOR_DELETED 0x0000
  39. #define SECTOR_ZERO 0xfffe
  40. #define SECTOR_FREE 0xffff
  41. #define SECTOR_SIZE 512
  42. #define SECTORS_PER_TRACK 63
  43. struct block {
  44. enum {
  45. BLOCK_OK,
  46. BLOCK_ERASING,
  47. BLOCK_ERASED,
  48. BLOCK_UNUSED,
  49. BLOCK_FAILED
  50. } state;
  51. int free_sectors;
  52. int used_sectors;
  53. int erases;
  54. u_long offset;
  55. };
  56. struct partition {
  57. struct mtd_blktrans_dev mbd;
  58. u_int block_size; /* size of erase unit */
  59. u_int total_blocks; /* number of erase units */
  60. u_int header_sectors_per_block; /* header sectors in erase unit */
  61. u_int data_sectors_per_block; /* data sectors in erase unit */
  62. u_int sector_count; /* sectors in translated disk */
  63. u_int header_size; /* bytes in header sector */
  64. int reserved_block; /* block next up for reclaim */
  65. int current_block; /* block to write to */
  66. u16 *header_cache; /* cached header */
  67. int is_reclaiming;
  68. int cylinders;
  69. int errors;
  70. u_long *sector_map;
  71. struct block *blocks;
  72. };
  73. static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf);
  74. static int build_block_map(struct partition *part, int block_no)
  75. {
  76. struct block *block = &part->blocks[block_no];
  77. int i;
  78. block->offset = part->block_size * block_no;
  79. if (le16_to_cpu(part->header_cache[0]) != RFD_MAGIC) {
  80. block->state = BLOCK_UNUSED;
  81. return -ENOENT;
  82. }
  83. block->state = BLOCK_OK;
  84. for (i=0; i<part->data_sectors_per_block; i++) {
  85. u16 entry;
  86. entry = le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i]);
  87. if (entry == SECTOR_DELETED)
  88. continue;
  89. if (entry == SECTOR_FREE) {
  90. block->free_sectors++;
  91. continue;
  92. }
  93. if (entry == SECTOR_ZERO)
  94. entry = 0;
  95. if (entry >= part->sector_count) {
  96. printk(KERN_WARNING PREFIX
  97. "'%s': unit #%d: entry %d corrupt, "
  98. "sector %d out of range\n",
  99. part->mbd.mtd->name, block_no, i, entry);
  100. continue;
  101. }
  102. if (part->sector_map[entry] != -1) {
  103. printk(KERN_WARNING PREFIX
  104. "'%s': more than one entry for sector %d\n",
  105. part->mbd.mtd->name, entry);
  106. part->errors = 1;
  107. continue;
  108. }
  109. part->sector_map[entry] = block->offset +
  110. (i + part->header_sectors_per_block) * SECTOR_SIZE;
  111. block->used_sectors++;
  112. }
  113. if (block->free_sectors == part->data_sectors_per_block)
  114. part->reserved_block = block_no;
  115. return 0;
  116. }
  117. static int scan_header(struct partition *part)
  118. {
  119. int sectors_per_block;
  120. int i, rc = -ENOMEM;
  121. int blocks_found;
  122. size_t retlen;
  123. sectors_per_block = part->block_size / SECTOR_SIZE;
  124. part->total_blocks = (u32)part->mbd.mtd->size / part->block_size;
  125. if (part->total_blocks < 2)
  126. return -ENOENT;
  127. /* each erase block has three bytes header, followed by the map */
  128. part->header_sectors_per_block =
  129. ((HEADER_MAP_OFFSET + sectors_per_block) *
  130. sizeof(u16) + SECTOR_SIZE - 1) / SECTOR_SIZE;
  131. part->data_sectors_per_block = sectors_per_block -
  132. part->header_sectors_per_block;
  133. part->header_size = (HEADER_MAP_OFFSET +
  134. part->data_sectors_per_block) * sizeof(u16);
  135. part->cylinders = (part->data_sectors_per_block *
  136. (part->total_blocks - 1) - 1) / SECTORS_PER_TRACK;
  137. part->sector_count = part->cylinders * SECTORS_PER_TRACK;
  138. part->current_block = -1;
  139. part->reserved_block = -1;
  140. part->is_reclaiming = 0;
  141. part->header_cache = kmalloc(part->header_size, GFP_KERNEL);
  142. if (!part->header_cache)
  143. goto err;
  144. part->blocks = kcalloc(part->total_blocks, sizeof(struct block),
  145. GFP_KERNEL);
  146. if (!part->blocks)
  147. goto err;
  148. part->sector_map = vmalloc(part->sector_count * sizeof(u_long));
  149. if (!part->sector_map) {
  150. printk(KERN_ERR PREFIX "'%s': unable to allocate memory for "
  151. "sector map", part->mbd.mtd->name);
  152. goto err;
  153. }
  154. for (i=0; i<part->sector_count; i++)
  155. part->sector_map[i] = -1;
  156. for (i=0, blocks_found=0; i<part->total_blocks; i++) {
  157. rc = mtd_read(part->mbd.mtd, i * part->block_size,
  158. part->header_size, &retlen,
  159. (u_char *)part->header_cache);
  160. if (!rc && retlen != part->header_size)
  161. rc = -EIO;
  162. if (rc)
  163. goto err;
  164. if (!build_block_map(part, i))
  165. blocks_found++;
  166. }
  167. if (blocks_found == 0) {
  168. printk(KERN_NOTICE PREFIX "no RFD magic found in '%s'\n",
  169. part->mbd.mtd->name);
  170. rc = -ENOENT;
  171. goto err;
  172. }
  173. if (part->reserved_block == -1) {
  174. printk(KERN_WARNING PREFIX "'%s': no empty erase unit found\n",
  175. part->mbd.mtd->name);
  176. part->errors = 1;
  177. }
  178. return 0;
  179. err:
  180. vfree(part->sector_map);
  181. kfree(part->header_cache);
  182. kfree(part->blocks);
  183. return rc;
  184. }
  185. static int rfd_ftl_readsect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
  186. {
  187. struct partition *part = (struct partition*)dev;
  188. u_long addr;
  189. size_t retlen;
  190. int rc;
  191. if (sector >= part->sector_count)
  192. return -EIO;
  193. addr = part->sector_map[sector];
  194. if (addr != -1) {
  195. rc = mtd_read(part->mbd.mtd, addr, SECTOR_SIZE, &retlen,
  196. (u_char *)buf);
  197. if (!rc && retlen != SECTOR_SIZE)
  198. rc = -EIO;
  199. if (rc) {
  200. printk(KERN_WARNING PREFIX "error reading '%s' at "
  201. "0x%lx\n", part->mbd.mtd->name, addr);
  202. return rc;
  203. }
  204. } else
  205. memset(buf, 0, SECTOR_SIZE);
  206. return 0;
  207. }
  208. static void erase_callback(struct erase_info *erase)
  209. {
  210. struct partition *part;
  211. u16 magic;
  212. int i, rc;
  213. size_t retlen;
  214. part = (struct partition*)erase->priv;
  215. i = (u32)erase->addr / part->block_size;
  216. if (i >= part->total_blocks || part->blocks[i].offset != erase->addr ||
  217. erase->addr > UINT_MAX) {
  218. printk(KERN_ERR PREFIX "erase callback for unknown offset %llx "
  219. "on '%s'\n", (unsigned long long)erase->addr, part->mbd.mtd->name);
  220. return;
  221. }
  222. if (erase->state != MTD_ERASE_DONE) {
  223. printk(KERN_WARNING PREFIX "erase failed at 0x%llx on '%s', "
  224. "state %d\n", (unsigned long long)erase->addr,
  225. part->mbd.mtd->name, erase->state);
  226. part->blocks[i].state = BLOCK_FAILED;
  227. part->blocks[i].free_sectors = 0;
  228. part->blocks[i].used_sectors = 0;
  229. kfree(erase);
  230. return;
  231. }
  232. magic = cpu_to_le16(RFD_MAGIC);
  233. part->blocks[i].state = BLOCK_ERASED;
  234. part->blocks[i].free_sectors = part->data_sectors_per_block;
  235. part->blocks[i].used_sectors = 0;
  236. part->blocks[i].erases++;
  237. rc = mtd_write(part->mbd.mtd, part->blocks[i].offset, sizeof(magic),
  238. &retlen, (u_char *)&magic);
  239. if (!rc && retlen != sizeof(magic))
  240. rc = -EIO;
  241. if (rc) {
  242. printk(KERN_ERR PREFIX "'%s': unable to write RFD "
  243. "header at 0x%lx\n",
  244. part->mbd.mtd->name,
  245. part->blocks[i].offset);
  246. part->blocks[i].state = BLOCK_FAILED;
  247. }
  248. else
  249. part->blocks[i].state = BLOCK_OK;
  250. kfree(erase);
  251. }
  252. static int erase_block(struct partition *part, int block)
  253. {
  254. struct erase_info *erase;
  255. int rc = -ENOMEM;
  256. erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
  257. if (!erase)
  258. goto err;
  259. erase->mtd = part->mbd.mtd;
  260. erase->callback = erase_callback;
  261. erase->addr = part->blocks[block].offset;
  262. erase->len = part->block_size;
  263. erase->priv = (u_long)part;
  264. part->blocks[block].state = BLOCK_ERASING;
  265. part->blocks[block].free_sectors = 0;
  266. rc = mtd_erase(part->mbd.mtd, erase);
  267. if (rc) {
  268. printk(KERN_ERR PREFIX "erase of region %llx,%llx on '%s' "
  269. "failed\n", (unsigned long long)erase->addr,
  270. (unsigned long long)erase->len, part->mbd.mtd->name);
  271. kfree(erase);
  272. }
  273. err:
  274. return rc;
  275. }
  276. static int move_block_contents(struct partition *part, int block_no, u_long *old_sector)
  277. {
  278. void *sector_data;
  279. u16 *map;
  280. size_t retlen;
  281. int i, rc = -ENOMEM;
  282. part->is_reclaiming = 1;
  283. sector_data = kmalloc(SECTOR_SIZE, GFP_KERNEL);
  284. if (!sector_data)
  285. goto err3;
  286. map = kmalloc(part->header_size, GFP_KERNEL);
  287. if (!map)
  288. goto err2;
  289. rc = mtd_read(part->mbd.mtd, part->blocks[block_no].offset,
  290. part->header_size, &retlen, (u_char *)map);
  291. if (!rc && retlen != part->header_size)
  292. rc = -EIO;
  293. if (rc) {
  294. printk(KERN_ERR PREFIX "error reading '%s' at "
  295. "0x%lx\n", part->mbd.mtd->name,
  296. part->blocks[block_no].offset);
  297. goto err;
  298. }
  299. for (i=0; i<part->data_sectors_per_block; i++) {
  300. u16 entry = le16_to_cpu(map[HEADER_MAP_OFFSET + i]);
  301. u_long addr;
  302. if (entry == SECTOR_FREE || entry == SECTOR_DELETED)
  303. continue;
  304. if (entry == SECTOR_ZERO)
  305. entry = 0;
  306. /* already warned about and ignored in build_block_map() */
  307. if (entry >= part->sector_count)
  308. continue;
  309. addr = part->blocks[block_no].offset +
  310. (i + part->header_sectors_per_block) * SECTOR_SIZE;
  311. if (*old_sector == addr) {
  312. *old_sector = -1;
  313. if (!part->blocks[block_no].used_sectors--) {
  314. rc = erase_block(part, block_no);
  315. break;
  316. }
  317. continue;
  318. }
  319. rc = mtd_read(part->mbd.mtd, addr, SECTOR_SIZE, &retlen,
  320. sector_data);
  321. if (!rc && retlen != SECTOR_SIZE)
  322. rc = -EIO;
  323. if (rc) {
  324. printk(KERN_ERR PREFIX "'%s': Unable to "
  325. "read sector for relocation\n",
  326. part->mbd.mtd->name);
  327. goto err;
  328. }
  329. rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part,
  330. entry, sector_data);
  331. if (rc)
  332. goto err;
  333. }
  334. err:
  335. kfree(map);
  336. err2:
  337. kfree(sector_data);
  338. err3:
  339. part->is_reclaiming = 0;
  340. return rc;
  341. }
  342. static int reclaim_block(struct partition *part, u_long *old_sector)
  343. {
  344. int block, best_block, score, old_sector_block;
  345. int rc;
  346. /* we have a race if sync doesn't exist */
  347. mtd_sync(part->mbd.mtd);
  348. score = 0x7fffffff; /* MAX_INT */
  349. best_block = -1;
  350. if (*old_sector != -1)
  351. old_sector_block = *old_sector / part->block_size;
  352. else
  353. old_sector_block = -1;
  354. for (block=0; block<part->total_blocks; block++) {
  355. int this_score;
  356. if (block == part->reserved_block)
  357. continue;
  358. /*
  359. * Postpone reclaiming if there is a free sector as
  360. * more removed sectors is more efficient (have to move
  361. * less).
  362. */
  363. if (part->blocks[block].free_sectors)
  364. return 0;
  365. this_score = part->blocks[block].used_sectors;
  366. if (block == old_sector_block)
  367. this_score--;
  368. else {
  369. /* no point in moving a full block */
  370. if (part->blocks[block].used_sectors ==
  371. part->data_sectors_per_block)
  372. continue;
  373. }
  374. this_score += part->blocks[block].erases;
  375. if (this_score < score) {
  376. best_block = block;
  377. score = this_score;
  378. }
  379. }
  380. if (best_block == -1)
  381. return -ENOSPC;
  382. part->current_block = -1;
  383. part->reserved_block = best_block;
  384. pr_debug("reclaim_block: reclaiming block #%d with %d used "
  385. "%d free sectors\n", best_block,
  386. part->blocks[best_block].used_sectors,
  387. part->blocks[best_block].free_sectors);
  388. if (part->blocks[best_block].used_sectors)
  389. rc = move_block_contents(part, best_block, old_sector);
  390. else
  391. rc = erase_block(part, best_block);
  392. return rc;
  393. }
  394. /*
  395. * IMPROVE: It would be best to choose the block with the most deleted sectors,
  396. * because if we fill that one up first it'll have the most chance of having
  397. * the least live sectors at reclaim.
  398. */
  399. static int find_free_block(struct partition *part)
  400. {
  401. int block, stop;
  402. block = part->current_block == -1 ?
  403. jiffies % part->total_blocks : part->current_block;
  404. stop = block;
  405. do {
  406. if (part->blocks[block].free_sectors &&
  407. block != part->reserved_block)
  408. return block;
  409. if (part->blocks[block].state == BLOCK_UNUSED)
  410. erase_block(part, block);
  411. if (++block >= part->total_blocks)
  412. block = 0;
  413. } while (block != stop);
  414. return -1;
  415. }
  416. static int find_writable_block(struct partition *part, u_long *old_sector)
  417. {
  418. int rc, block;
  419. size_t retlen;
  420. block = find_free_block(part);
  421. if (block == -1) {
  422. if (!part->is_reclaiming) {
  423. rc = reclaim_block(part, old_sector);
  424. if (rc)
  425. goto err;
  426. block = find_free_block(part);
  427. }
  428. if (block == -1) {
  429. rc = -ENOSPC;
  430. goto err;
  431. }
  432. }
  433. rc = mtd_read(part->mbd.mtd, part->blocks[block].offset,
  434. part->header_size, &retlen,
  435. (u_char *)part->header_cache);
  436. if (!rc && retlen != part->header_size)
  437. rc = -EIO;
  438. if (rc) {
  439. printk(KERN_ERR PREFIX "'%s': unable to read header at "
  440. "0x%lx\n", part->mbd.mtd->name,
  441. part->blocks[block].offset);
  442. goto err;
  443. }
  444. part->current_block = block;
  445. err:
  446. return rc;
  447. }
  448. static int mark_sector_deleted(struct partition *part, u_long old_addr)
  449. {
  450. int block, offset, rc;
  451. u_long addr;
  452. size_t retlen;
  453. u16 del = cpu_to_le16(SECTOR_DELETED);
  454. block = old_addr / part->block_size;
  455. offset = (old_addr % part->block_size) / SECTOR_SIZE -
  456. part->header_sectors_per_block;
  457. addr = part->blocks[block].offset +
  458. (HEADER_MAP_OFFSET + offset) * sizeof(u16);
  459. rc = mtd_write(part->mbd.mtd, addr, sizeof(del), &retlen,
  460. (u_char *)&del);
  461. if (!rc && retlen != sizeof(del))
  462. rc = -EIO;
  463. if (rc) {
  464. printk(KERN_ERR PREFIX "error writing '%s' at "
  465. "0x%lx\n", part->mbd.mtd->name, addr);
  466. goto err;
  467. }
  468. if (block == part->current_block)
  469. part->header_cache[offset + HEADER_MAP_OFFSET] = del;
  470. part->blocks[block].used_sectors--;
  471. if (!part->blocks[block].used_sectors &&
  472. !part->blocks[block].free_sectors)
  473. rc = erase_block(part, block);
  474. err:
  475. return rc;
  476. }
  477. static int find_free_sector(const struct partition *part, const struct block *block)
  478. {
  479. int i, stop;
  480. i = stop = part->data_sectors_per_block - block->free_sectors;
  481. do {
  482. if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i])
  483. == SECTOR_FREE)
  484. return i;
  485. if (++i == part->data_sectors_per_block)
  486. i = 0;
  487. }
  488. while(i != stop);
  489. return -1;
  490. }
  491. static int do_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf, ulong *old_addr)
  492. {
  493. struct partition *part = (struct partition*)dev;
  494. struct block *block;
  495. u_long addr;
  496. int i;
  497. int rc;
  498. size_t retlen;
  499. u16 entry;
  500. if (part->current_block == -1 ||
  501. !part->blocks[part->current_block].free_sectors) {
  502. rc = find_writable_block(part, old_addr);
  503. if (rc)
  504. goto err;
  505. }
  506. block = &part->blocks[part->current_block];
  507. i = find_free_sector(part, block);
  508. if (i < 0) {
  509. rc = -ENOSPC;
  510. goto err;
  511. }
  512. addr = (i + part->header_sectors_per_block) * SECTOR_SIZE +
  513. block->offset;
  514. rc = mtd_write(part->mbd.mtd, addr, SECTOR_SIZE, &retlen,
  515. (u_char *)buf);
  516. if (!rc && retlen != SECTOR_SIZE)
  517. rc = -EIO;
  518. if (rc) {
  519. printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
  520. part->mbd.mtd->name, addr);
  521. goto err;
  522. }
  523. part->sector_map[sector] = addr;
  524. entry = cpu_to_le16(sector == 0 ? SECTOR_ZERO : sector);
  525. part->header_cache[i + HEADER_MAP_OFFSET] = entry;
  526. addr = block->offset + (HEADER_MAP_OFFSET + i) * sizeof(u16);
  527. rc = mtd_write(part->mbd.mtd, addr, sizeof(entry), &retlen,
  528. (u_char *)&entry);
  529. if (!rc && retlen != sizeof(entry))
  530. rc = -EIO;
  531. if (rc) {
  532. printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
  533. part->mbd.mtd->name, addr);
  534. goto err;
  535. }
  536. block->used_sectors++;
  537. block->free_sectors--;
  538. err:
  539. return rc;
  540. }
  541. static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
  542. {
  543. struct partition *part = (struct partition*)dev;
  544. u_long old_addr;
  545. int i;
  546. int rc = 0;
  547. pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector);
  548. if (part->reserved_block == -1) {
  549. rc = -EACCES;
  550. goto err;
  551. }
  552. if (sector >= part->sector_count) {
  553. rc = -EIO;
  554. goto err;
  555. }
  556. old_addr = part->sector_map[sector];
  557. for (i=0; i<SECTOR_SIZE; i++) {
  558. if (!buf[i])
  559. continue;
  560. rc = do_writesect(dev, sector, buf, &old_addr);
  561. if (rc)
  562. goto err;
  563. break;
  564. }
  565. if (i == SECTOR_SIZE)
  566. part->sector_map[sector] = -1;
  567. if (old_addr != -1)
  568. rc = mark_sector_deleted(part, old_addr);
  569. err:
  570. return rc;
  571. }
  572. static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  573. {
  574. struct partition *part = (struct partition*)dev;
  575. geo->heads = 1;
  576. geo->sectors = SECTORS_PER_TRACK;
  577. geo->cylinders = part->cylinders;
  578. return 0;
  579. }
  580. static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  581. {
  582. struct partition *part;
  583. if (mtd->type != MTD_NORFLASH || mtd->size > UINT_MAX)
  584. return;
  585. part = kzalloc(sizeof(struct partition), GFP_KERNEL);
  586. if (!part)
  587. return;
  588. part->mbd.mtd = mtd;
  589. if (block_size)
  590. part->block_size = block_size;
  591. else {
  592. if (!mtd->erasesize) {
  593. printk(KERN_WARNING PREFIX "please provide block_size");
  594. goto out;
  595. } else
  596. part->block_size = mtd->erasesize;
  597. }
  598. if (scan_header(part) == 0) {
  599. part->mbd.size = part->sector_count;
  600. part->mbd.tr = tr;
  601. part->mbd.devnum = -1;
  602. if (!(mtd->flags & MTD_WRITEABLE))
  603. part->mbd.readonly = 1;
  604. else if (part->errors) {
  605. printk(KERN_WARNING PREFIX "'%s': errors found, "
  606. "setting read-only\n", mtd->name);
  607. part->mbd.readonly = 1;
  608. }
  609. printk(KERN_INFO PREFIX "name: '%s' type: %d flags %x\n",
  610. mtd->name, mtd->type, mtd->flags);
  611. if (!add_mtd_blktrans_dev((void*)part))
  612. return;
  613. }
  614. out:
  615. kfree(part);
  616. }
  617. static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
  618. {
  619. struct partition *part = (struct partition*)dev;
  620. int i;
  621. for (i=0; i<part->total_blocks; i++) {
  622. pr_debug("rfd_ftl_remove_dev:'%s': erase unit #%02d: %d erases\n",
  623. part->mbd.mtd->name, i, part->blocks[i].erases);
  624. }
  625. del_mtd_blktrans_dev(dev);
  626. vfree(part->sector_map);
  627. kfree(part->header_cache);
  628. kfree(part->blocks);
  629. }
  630. static struct mtd_blktrans_ops rfd_ftl_tr = {
  631. .name = "rfd",
  632. .major = RFD_FTL_MAJOR,
  633. .part_bits = PART_BITS,
  634. .blksize = SECTOR_SIZE,
  635. .readsect = rfd_ftl_readsect,
  636. .writesect = rfd_ftl_writesect,
  637. .getgeo = rfd_ftl_getgeo,
  638. .add_mtd = rfd_ftl_add_mtd,
  639. .remove_dev = rfd_ftl_remove_dev,
  640. .owner = THIS_MODULE,
  641. };
  642. static int __init init_rfd_ftl(void)
  643. {
  644. return register_mtd_blktrans(&rfd_ftl_tr);
  645. }
  646. static void __exit cleanup_rfd_ftl(void)
  647. {
  648. deregister_mtd_blktrans(&rfd_ftl_tr);
  649. }
  650. module_init(init_rfd_ftl);
  651. module_exit(cleanup_rfd_ftl);
  652. MODULE_LICENSE("GPL");
  653. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  654. MODULE_DESCRIPTION("Support code for RFD Flash Translation Layer, "
  655. "used by General Software's Embedded BIOS");