blkdev.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* Block device registry: floppy (via FDC) + IDE (via ATA PIO) + SATA (AHCI)
  2. * + eMMC (SDHCI). */
  3. #include "blkdev.h"
  4. #include "ahci.h"
  5. #include "ata.h"
  6. #include "disk.h"
  7. #include "sdhci.h"
  8. #include "serial.h"
  9. #include "timer.h"
  10. blk_dev_t blk_devs[BLKDEV_MAX];
  11. unsigned int blk_dev_count;
  12. /* Names for SATA disks, built from the AHCI port number ("sata0".."sata31"). */
  13. static char sata_names[AHCI_MAX_DISKS][8];
  14. static const char *sata_name(unsigned int idx, unsigned int port)
  15. {
  16. char *b = sata_names[idx];
  17. unsigned int i = 4;
  18. b[0] = 's'; b[1] = 'a'; b[2] = 't'; b[3] = 'a';
  19. if (port >= 10)
  20. b[i++] = (char)('0' + (port / 10) % 10);
  21. b[i++] = (char)('0' + port % 10);
  22. b[i] = '\0';
  23. return b;
  24. }
  25. static blk_dev_t *add(const char *name, blk_kind_t kind,
  26. unsigned int bus, unsigned int unit)
  27. {
  28. blk_dev_t *d;
  29. if (blk_dev_count >= BLKDEV_MAX)
  30. return 0;
  31. d = &blk_devs[blk_dev_count++];
  32. d->name = name;
  33. d->kind = kind;
  34. d->bus = bus;
  35. d->unit = unit;
  36. d->present = 0;
  37. d->total_sectors = 0;
  38. d->model[0] = '\0';
  39. return d;
  40. }
  41. static void blk_trace(const char *name, int present, unsigned int ms)
  42. {
  43. serial_puts(SERIAL_COM1, "blk: ");
  44. serial_puts(SERIAL_COM1, name);
  45. serial_puts(SERIAL_COM1, present ? " present " : " absent ");
  46. serial_putdec(SERIAL_COM1, ms);
  47. serial_puts(SERIAL_COM1, " ms\n");
  48. }
  49. void blk_scan(void)
  50. {
  51. blk_dev_t *d;
  52. unsigned int t0;
  53. blk_dev_count = 0;
  54. /* Floppy drives 0 and 1. Probe for media instead of assuming both
  55. * drives exist: an empty drive would otherwise go through the full
  56. * ten-retry recovery loop at every mount. */
  57. t0 = get_ticks_ms();
  58. d = add("floppy0", BLK_FLOPPY, 0, 0);
  59. if (d) {
  60. d->present = disk_probe(0);
  61. d->total_sectors = 2880;
  62. }
  63. blk_trace("floppy0", d ? d->present : 0, get_ticks_ms() - t0);
  64. t0 = get_ticks_ms();
  65. d = add("floppy1", BLK_FLOPPY, 1, 0);
  66. if (d) {
  67. d->present = disk_probe(1);
  68. d->total_sectors = 2880;
  69. }
  70. blk_trace("floppy1", d ? d->present : 0, get_ticks_ms() - t0);
  71. /* IDE: primary + secondary, master + slave. The channel port bases
  72. * come from the PCI IDE interface (native mode) or the legacy default. */
  73. ata_init_ports();
  74. {
  75. static const char *names[4] = { "ide0", "ide1", "ide2", "ide3" };
  76. unsigned int i;
  77. for (i = 0; i < 4; i++) {
  78. unsigned int sectors = 0;
  79. char model[41];
  80. int ok;
  81. d = add(names[i], BLK_IDE, i / 2, i % 2);
  82. if (!d)
  83. continue;
  84. t0 = get_ticks_ms();
  85. ok = ata_probe(i / 2, i % 2, &sectors, model) == 0;
  86. blk_trace(names[i], ok, get_ticks_ms() - t0);
  87. if (ok) {
  88. unsigned int j;
  89. d->present = 1;
  90. d->total_sectors = sectors;
  91. for (j = 0; j < 40; j++)
  92. d->model[j] = model[j];
  93. d->model[40] = '\0';
  94. }
  95. }
  96. }
  97. /* SATA/AHCI: only present disks are registered, named by port index. */
  98. {
  99. struct ahci_disk disks[AHCI_MAX_DISKS];
  100. unsigned int n, i;
  101. t0 = get_ticks_ms();
  102. n = ahci_scan(disks, AHCI_MAX_DISKS);
  103. serial_puts(SERIAL_COM1, "blk: ahci ");
  104. serial_putdec(SERIAL_COM1, n);
  105. serial_puts(SERIAL_COM1, " disk(s) ");
  106. serial_putdec(SERIAL_COM1, get_ticks_ms() - t0);
  107. serial_puts(SERIAL_COM1, " ms\n");
  108. for (i = 0; i < n; i++) {
  109. unsigned int j;
  110. d = add(sata_name(i, disks[i].port), BLK_SATA, 0, disks[i].port);
  111. if (!d)
  112. break;
  113. d->present = 1;
  114. d->total_sectors = disks[i].sectors;
  115. for (j = 0; j < 40; j++)
  116. d->model[j] = disks[i].model[j];
  117. d->model[40] = '\0';
  118. }
  119. }
  120. /* eMMC: the Intel Braswell SDHCI controller, one soldered card. */
  121. {
  122. struct sdhci_disk disk;
  123. unsigned int n;
  124. t0 = get_ticks_ms();
  125. n = sdhci_scan(&disk, 1);
  126. blk_trace("mmc0", n ? 1 : 0, get_ticks_ms() - t0);
  127. if (n) {
  128. unsigned int j;
  129. d = add("mmc0", BLK_MMC, 0, 0);
  130. if (d) {
  131. d->present = 1;
  132. d->total_sectors = disk.sectors;
  133. for (j = 0; j < 40; j++)
  134. d->model[j] = disk.model[j];
  135. d->model[40] = '\0';
  136. }
  137. }
  138. }
  139. }
  140. static int name_eq(const char *a, const char *b)
  141. {
  142. while (*a && *b) {
  143. if (*a != *b)
  144. return 0;
  145. a++;
  146. b++;
  147. }
  148. return (*a == *b);
  149. }
  150. blk_dev_t *blk_find(const char *name)
  151. {
  152. unsigned int i;
  153. for (i = 0; i < blk_dev_count; i++)
  154. if (name_eq(blk_devs[i].name, name))
  155. return &blk_devs[i];
  156. return 0;
  157. }
  158. int blk_read(blk_dev_t *d, unsigned int lba, void *buf, unsigned int count)
  159. {
  160. if (!d || count == 0)
  161. return -1;
  162. if (d->kind == BLK_FLOPPY) {
  163. disk_set_drive(d->bus);
  164. return disk_read_sectors(lba, buf, count);
  165. }
  166. if (d->kind == BLK_SATA)
  167. return ahci_read_sectors(d->unit, lba, buf, count);
  168. if (d->kind == BLK_MMC)
  169. return sdhci_read_sectors(lba, buf, count);
  170. return ata_read_sectors(d->bus, d->unit, lba, buf, count);
  171. }
  172. int blk_write(blk_dev_t *d, unsigned int lba, const void *buf, unsigned int count)
  173. {
  174. if (!d || count == 0)
  175. return -1;
  176. if (d->kind == BLK_FLOPPY) {
  177. disk_set_drive(d->bus);
  178. return disk_write_sectors(lba, buf, count);
  179. }
  180. if (d->kind == BLK_SATA)
  181. return ahci_write_sectors(d->unit, lba, buf, count);
  182. if (d->kind == BLK_MMC)
  183. return sdhci_write_sectors(lba, buf, count);
  184. return ata_write_sectors(d->bus, d->unit, lba, buf, count);
  185. }
  186. int blk_flush(blk_dev_t *d)
  187. {
  188. if (!d)
  189. return -1;
  190. if (d->kind == BLK_FLOPPY)
  191. return 0;
  192. if (d->kind == BLK_SATA)
  193. return ahci_flush(d->unit);
  194. if (d->kind == BLK_MMC)
  195. return sdhci_flush();
  196. return ata_flush(d->bus, d->unit);
  197. }