rfd_ftl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  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 = part->mbd.mtd->read(part->mbd.mtd,
  158. i * part->block_size, part->header_size,
  159. &retlen, (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 = part->mbd.mtd->read(part->mbd.mtd, addr, SECTOR_SIZE,
  196. &retlen, (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 = part->mbd.mtd->write(part->mbd.mtd,
  238. part->blocks[i].offset, sizeof(magic), &retlen,
  239. (u_char*)&magic);
  240. if (!rc && retlen != sizeof(magic))
  241. rc = -EIO;
  242. if (rc) {
  243. printk(KERN_ERR PREFIX "'%s': unable to write RFD "
  244. "header at 0x%lx\n",
  245. part->mbd.mtd->name,
  246. part->blocks[i].offset);
  247. part->blocks[i].state = BLOCK_FAILED;
  248. }
  249. else
  250. part->blocks[i].state = BLOCK_OK;
  251. kfree(erase);
  252. }
  253. static int erase_block(struct partition *part, int block)
  254. {
  255. struct erase_info *erase;
  256. int rc = -ENOMEM;
  257. erase = kmalloc(sizeof(struct erase_info), GFP_KERNEL);
  258. if (!erase)
  259. goto err;
  260. erase->mtd = part->mbd.mtd;
  261. erase->callback = erase_callback;
  262. erase->addr = part->blocks[block].offset;
  263. erase->len = part->block_size;
  264. erase->priv = (u_long)part;
  265. part->blocks[block].state = BLOCK_ERASING;
  266. part->blocks[block].free_sectors = 0;
  267. rc = part->mbd.mtd->erase(part->mbd.mtd, erase);
  268. if (rc) {
  269. printk(KERN_ERR PREFIX "erase of region %llx,%llx on '%s' "
  270. "failed\n", (unsigned long long)erase->addr,
  271. (unsigned long long)erase->len, part->mbd.mtd->name);
  272. kfree(erase);
  273. }
  274. err:
  275. return rc;
  276. }
  277. static int move_block_contents(struct partition *part, int block_no, u_long *old_sector)
  278. {
  279. void *sector_data;
  280. u16 *map;
  281. size_t retlen;
  282. int i, rc = -ENOMEM;
  283. part->is_reclaiming = 1;
  284. sector_data = kmalloc(SECTOR_SIZE, GFP_KERNEL);
  285. if (!sector_data)
  286. goto err3;
  287. map = kmalloc(part->header_size, GFP_KERNEL);
  288. if (!map)
  289. goto err2;
  290. rc = part->mbd.mtd->read(part->mbd.mtd,
  291. part->blocks[block_no].offset, part->header_size,
  292. &retlen, (u_char*)map);
  293. if (!rc && retlen != part->header_size)
  294. rc = -EIO;
  295. if (rc) {
  296. printk(KERN_ERR PREFIX "error reading '%s' at "
  297. "0x%lx\n", part->mbd.mtd->name,
  298. part->blocks[block_no].offset);
  299. goto err;
  300. }
  301. for (i=0; i<part->data_sectors_per_block; i++) {
  302. u16 entry = le16_to_cpu(map[HEADER_MAP_OFFSET + i]);
  303. u_long addr;
  304. if (entry == SECTOR_FREE || entry == SECTOR_DELETED)
  305. continue;
  306. if (entry == SECTOR_ZERO)
  307. entry = 0;
  308. /* already warned about and ignored in build_block_map() */
  309. if (entry >= part->sector_count)
  310. continue;
  311. addr = part->blocks[block_no].offset +
  312. (i + part->header_sectors_per_block) * SECTOR_SIZE;
  313. if (*old_sector == addr) {
  314. *old_sector = -1;
  315. if (!part->blocks[block_no].used_sectors--) {
  316. rc = erase_block(part, block_no);
  317. break;
  318. }
  319. continue;
  320. }
  321. rc = part->mbd.mtd->read(part->mbd.mtd, addr,
  322. SECTOR_SIZE, &retlen, sector_data);
  323. if (!rc && retlen != SECTOR_SIZE)
  324. rc = -EIO;
  325. if (rc) {
  326. printk(KERN_ERR PREFIX "'%s': Unable to "
  327. "read sector for relocation\n",
  328. part->mbd.mtd->name);
  329. goto err;
  330. }
  331. rc = rfd_ftl_writesect((struct mtd_blktrans_dev*)part,
  332. entry, sector_data);
  333. if (rc)
  334. goto err;
  335. }
  336. err:
  337. kfree(map);
  338. err2:
  339. kfree(sector_data);
  340. err3:
  341. part->is_reclaiming = 0;
  342. return rc;
  343. }
  344. static int reclaim_block(struct partition *part, u_long *old_sector)
  345. {
  346. int block, best_block, score, old_sector_block;
  347. int rc;
  348. /* we have a race if sync doesn't exist */
  349. if (part->mbd.mtd->sync)
  350. part->mbd.mtd->sync(part->mbd.mtd);
  351. score = 0x7fffffff; /* MAX_INT */
  352. best_block = -1;
  353. if (*old_sector != -1)
  354. old_sector_block = *old_sector / part->block_size;
  355. else
  356. old_sector_block = -1;
  357. for (block=0; block<part->total_blocks; block++) {
  358. int this_score;
  359. if (block == part->reserved_block)
  360. continue;
  361. /*
  362. * Postpone reclaiming if there is a free sector as
  363. * more removed sectors is more efficient (have to move
  364. * less).
  365. */
  366. if (part->blocks[block].free_sectors)
  367. return 0;
  368. this_score = part->blocks[block].used_sectors;
  369. if (block == old_sector_block)
  370. this_score--;
  371. else {
  372. /* no point in moving a full block */
  373. if (part->blocks[block].used_sectors ==
  374. part->data_sectors_per_block)
  375. continue;
  376. }
  377. this_score += part->blocks[block].erases;
  378. if (this_score < score) {
  379. best_block = block;
  380. score = this_score;
  381. }
  382. }
  383. if (best_block == -1)
  384. return -ENOSPC;
  385. part->current_block = -1;
  386. part->reserved_block = best_block;
  387. pr_debug("reclaim_block: reclaiming block #%d with %d used "
  388. "%d free sectors\n", best_block,
  389. part->blocks[best_block].used_sectors,
  390. part->blocks[best_block].free_sectors);
  391. if (part->blocks[best_block].used_sectors)
  392. rc = move_block_contents(part, best_block, old_sector);
  393. else
  394. rc = erase_block(part, best_block);
  395. return rc;
  396. }
  397. /*
  398. * IMPROVE: It would be best to choose the block with the most deleted sectors,
  399. * because if we fill that one up first it'll have the most chance of having
  400. * the least live sectors at reclaim.
  401. */
  402. static int find_free_block(struct partition *part)
  403. {
  404. int block, stop;
  405. block = part->current_block == -1 ?
  406. jiffies % part->total_blocks : part->current_block;
  407. stop = block;
  408. do {
  409. if (part->blocks[block].free_sectors &&
  410. block != part->reserved_block)
  411. return block;
  412. if (part->blocks[block].state == BLOCK_UNUSED)
  413. erase_block(part, block);
  414. if (++block >= part->total_blocks)
  415. block = 0;
  416. } while (block != stop);
  417. return -1;
  418. }
  419. static int find_writable_block(struct partition *part, u_long *old_sector)
  420. {
  421. int rc, block;
  422. size_t retlen;
  423. block = find_free_block(part);
  424. if (block == -1) {
  425. if (!part->is_reclaiming) {
  426. rc = reclaim_block(part, old_sector);
  427. if (rc)
  428. goto err;
  429. block = find_free_block(part);
  430. }
  431. if (block == -1) {
  432. rc = -ENOSPC;
  433. goto err;
  434. }
  435. }
  436. rc = part->mbd.mtd->read(part->mbd.mtd, part->blocks[block].offset,
  437. part->header_size, &retlen, (u_char*)part->header_cache);
  438. if (!rc && retlen != part->header_size)
  439. rc = -EIO;
  440. if (rc) {
  441. printk(KERN_ERR PREFIX "'%s': unable to read header at "
  442. "0x%lx\n", part->mbd.mtd->name,
  443. part->blocks[block].offset);
  444. goto err;
  445. }
  446. part->current_block = block;
  447. err:
  448. return rc;
  449. }
  450. static int mark_sector_deleted(struct partition *part, u_long old_addr)
  451. {
  452. int block, offset, rc;
  453. u_long addr;
  454. size_t retlen;
  455. u16 del = cpu_to_le16(SECTOR_DELETED);
  456. block = old_addr / part->block_size;
  457. offset = (old_addr % part->block_size) / SECTOR_SIZE -
  458. part->header_sectors_per_block;
  459. addr = part->blocks[block].offset +
  460. (HEADER_MAP_OFFSET + offset) * sizeof(u16);
  461. rc = part->mbd.mtd->write(part->mbd.mtd, addr,
  462. sizeof(del), &retlen, (u_char*)&del);
  463. if (!rc && retlen != sizeof(del))
  464. rc = -EIO;
  465. if (rc) {
  466. printk(KERN_ERR PREFIX "error writing '%s' at "
  467. "0x%lx\n", part->mbd.mtd->name, addr);
  468. if (rc)
  469. goto err;
  470. }
  471. if (block == part->current_block)
  472. part->header_cache[offset + HEADER_MAP_OFFSET] = del;
  473. part->blocks[block].used_sectors--;
  474. if (!part->blocks[block].used_sectors &&
  475. !part->blocks[block].free_sectors)
  476. rc = erase_block(part, block);
  477. err:
  478. return rc;
  479. }
  480. static int find_free_sector(const struct partition *part, const struct block *block)
  481. {
  482. int i, stop;
  483. i = stop = part->data_sectors_per_block - block->free_sectors;
  484. do {
  485. if (le16_to_cpu(part->header_cache[HEADER_MAP_OFFSET + i])
  486. == SECTOR_FREE)
  487. return i;
  488. if (++i == part->data_sectors_per_block)
  489. i = 0;
  490. }
  491. while(i != stop);
  492. return -1;
  493. }
  494. static int do_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf, ulong *old_addr)
  495. {
  496. struct partition *part = (struct partition*)dev;
  497. struct block *block;
  498. u_long addr;
  499. int i;
  500. int rc;
  501. size_t retlen;
  502. u16 entry;
  503. if (part->current_block == -1 ||
  504. !part->blocks[part->current_block].free_sectors) {
  505. rc = find_writable_block(part, old_addr);
  506. if (rc)
  507. goto err;
  508. }
  509. block = &part->blocks[part->current_block];
  510. i = find_free_sector(part, block);
  511. if (i < 0) {
  512. rc = -ENOSPC;
  513. goto err;
  514. }
  515. addr = (i + part->header_sectors_per_block) * SECTOR_SIZE +
  516. block->offset;
  517. rc = part->mbd.mtd->write(part->mbd.mtd,
  518. addr, SECTOR_SIZE, &retlen, (u_char*)buf);
  519. if (!rc && retlen != SECTOR_SIZE)
  520. rc = -EIO;
  521. if (rc) {
  522. printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
  523. part->mbd.mtd->name, addr);
  524. if (rc)
  525. goto err;
  526. }
  527. part->sector_map[sector] = addr;
  528. entry = cpu_to_le16(sector == 0 ? SECTOR_ZERO : sector);
  529. part->header_cache[i + HEADER_MAP_OFFSET] = entry;
  530. addr = block->offset + (HEADER_MAP_OFFSET + i) * sizeof(u16);
  531. rc = part->mbd.mtd->write(part->mbd.mtd, addr,
  532. sizeof(entry), &retlen, (u_char*)&entry);
  533. if (!rc && retlen != sizeof(entry))
  534. rc = -EIO;
  535. if (rc) {
  536. printk(KERN_ERR PREFIX "error writing '%s' at 0x%lx\n",
  537. part->mbd.mtd->name, addr);
  538. if (rc)
  539. goto err;
  540. }
  541. block->used_sectors++;
  542. block->free_sectors--;
  543. err:
  544. return rc;
  545. }
  546. static int rfd_ftl_writesect(struct mtd_blktrans_dev *dev, u_long sector, char *buf)
  547. {
  548. struct partition *part = (struct partition*)dev;
  549. u_long old_addr;
  550. int i;
  551. int rc = 0;
  552. pr_debug("rfd_ftl_writesect(sector=0x%lx)\n", sector);
  553. if (part->reserved_block == -1) {
  554. rc = -EACCES;
  555. goto err;
  556. }
  557. if (sector >= part->sector_count) {
  558. rc = -EIO;
  559. goto err;
  560. }
  561. old_addr = part->sector_map[sector];
  562. for (i=0; i<SECTOR_SIZE; i++) {
  563. if (!buf[i])
  564. continue;
  565. rc = do_writesect(dev, sector, buf, &old_addr);
  566. if (rc)
  567. goto err;
  568. break;
  569. }
  570. if (i == SECTOR_SIZE)
  571. part->sector_map[sector] = -1;
  572. if (old_addr != -1)
  573. rc = mark_sector_deleted(part, old_addr);
  574. err:
  575. return rc;
  576. }
  577. static int rfd_ftl_getgeo(struct mtd_blktrans_dev *dev, struct hd_geometry *geo)
  578. {
  579. struct partition *part = (struct partition*)dev;
  580. geo->heads = 1;
  581. geo->sectors = SECTORS_PER_TRACK;
  582. geo->cylinders = part->cylinders;
  583. return 0;
  584. }
  585. static void rfd_ftl_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
  586. {
  587. struct partition *part;
  588. if (mtd->type != MTD_NORFLASH || mtd->size > UINT_MAX)
  589. return;
  590. part = kzalloc(sizeof(struct partition), GFP_KERNEL);
  591. if (!part)
  592. return;
  593. part->mbd.mtd = mtd;
  594. if (block_size)
  595. part->block_size = block_size;
  596. else {
  597. if (!mtd->erasesize) {
  598. printk(KERN_WARNING PREFIX "please provide block_size");
  599. goto out;
  600. } else
  601. part->block_size = mtd->erasesize;
  602. }
  603. if (scan_header(part) == 0) {
  604. part->mbd.size = part->sector_count;
  605. part->mbd.tr = tr;
  606. part->mbd.devnum = -1;
  607. if (!(mtd->flags & MTD_WRITEABLE))
  608. part->mbd.readonly = 1;
  609. else if (part->errors) {
  610. printk(KERN_WARNING PREFIX "'%s': errors found, "
  611. "setting read-only\n", mtd->name);
  612. part->mbd.readonly = 1;
  613. }
  614. printk(KERN_INFO PREFIX "name: '%s' type: %d flags %x\n",
  615. mtd->name, mtd->type, mtd->flags);
  616. if (!add_mtd_blktrans_dev((void*)part))
  617. return;
  618. }
  619. out:
  620. kfree(part);
  621. }
  622. static void rfd_ftl_remove_dev(struct mtd_blktrans_dev *dev)
  623. {
  624. struct partition *part = (struct partition*)dev;
  625. int i;
  626. for (i=0; i<part->total_blocks; i++) {
  627. pr_debug("rfd_ftl_remove_dev:'%s': erase unit #%02d: %d erases\n",
  628. part->mbd.mtd->name, i, part->blocks[i].erases);
  629. }
  630. del_mtd_blktrans_dev(dev);
  631. vfree(part->sector_map);
  632. kfree(part->header_cache);
  633. kfree(part->blocks);
  634. }
  635. static struct mtd_blktrans_ops rfd_ftl_tr = {
  636. .name = "rfd",
  637. .major = RFD_FTL_MAJOR,
  638. .part_bits = PART_BITS,
  639. .blksize = SECTOR_SIZE,
  640. .readsect = rfd_ftl_readsect,
  641. .writesect = rfd_ftl_writesect,
  642. .getgeo = rfd_ftl_getgeo,
  643. .add_mtd = rfd_ftl_add_mtd,
  644. .remove_dev = rfd_ftl_remove_dev,
  645. .owner = THIS_MODULE,
  646. };
  647. static int __init init_rfd_ftl(void)
  648. {
  649. return register_mtd_blktrans(&rfd_ftl_tr);
  650. }
  651. static void __exit cleanup_rfd_ftl(void)
  652. {
  653. deregister_mtd_blktrans(&rfd_ftl_tr);
  654. }
  655. module_init(init_rfd_ftl);
  656. module_exit(cleanup_rfd_ftl);
  657. MODULE_LICENSE("GPL");
  658. MODULE_AUTHOR("Sean Young <sean@mess.org>");
  659. MODULE_DESCRIPTION("Support code for RFD Flash Translation Layer, "
  660. "used by General Software's Embedded BIOS");