blkdev.c 5.5 KB

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