blkdev.c 4.5 KB

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