blkdev.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. blk_dev_t blk_devs[BLKDEV_MAX];
  7. unsigned int blk_dev_count;
  8. /* Names for SATA disks, built from the AHCI port number ("sata0".."sata31"). */
  9. static char sata_names[AHCI_MAX_DISKS][8];
  10. static const char *sata_name(unsigned int idx, unsigned int port)
  11. {
  12. char *b = sata_names[idx];
  13. unsigned int i = 4;
  14. b[0] = 's'; b[1] = 'a'; b[2] = 't'; b[3] = 'a';
  15. if (port >= 10)
  16. b[i++] = (char)('0' + (port / 10) % 10);
  17. b[i++] = (char)('0' + port % 10);
  18. b[i] = '\0';
  19. return b;
  20. }
  21. static blk_dev_t *add(const char *name, blk_kind_t kind,
  22. unsigned int bus, unsigned int unit)
  23. {
  24. blk_dev_t *d;
  25. if (blk_dev_count >= BLKDEV_MAX)
  26. return 0;
  27. d = &blk_devs[blk_dev_count++];
  28. d->name = name;
  29. d->kind = kind;
  30. d->bus = bus;
  31. d->unit = unit;
  32. d->present = 0;
  33. d->total_sectors = 0;
  34. d->model[0] = '\0';
  35. return d;
  36. }
  37. void blk_scan(void)
  38. {
  39. blk_dev_t *d;
  40. blk_dev_count = 0;
  41. /* Floppy drives 0 and 1. */
  42. d = add("floppy0", BLK_FLOPPY, 0, 0);
  43. if (d) { d->present = 1; d->total_sectors = 2880; }
  44. d = add("floppy1", BLK_FLOPPY, 1, 0);
  45. if (d) { d->present = 1; d->total_sectors = 2880; }
  46. /* IDE: primary + secondary, master + slave. The channel port bases
  47. * come from the PCI IDE interface (native mode) or the legacy default. */
  48. ata_init_ports();
  49. {
  50. static const char *names[4] = { "ide0", "ide1", "ide2", "ide3" };
  51. unsigned int i;
  52. for (i = 0; i < 4; i++) {
  53. unsigned int sectors = 0;
  54. char model[41];
  55. d = add(names[i], BLK_IDE, i / 2, i % 2);
  56. if (!d)
  57. continue;
  58. if (ata_probe(i / 2, i % 2, &sectors, model) == 0) {
  59. unsigned int j;
  60. d->present = 1;
  61. d->total_sectors = sectors;
  62. for (j = 0; j < 40; j++)
  63. d->model[j] = model[j];
  64. d->model[40] = '\0';
  65. }
  66. }
  67. }
  68. /* SATA/AHCI: only present disks are registered, named by port index. */
  69. {
  70. struct ahci_disk disks[AHCI_MAX_DISKS];
  71. unsigned int n = ahci_scan(disks, AHCI_MAX_DISKS);
  72. unsigned int i;
  73. for (i = 0; i < n; i++) {
  74. unsigned int j;
  75. d = add(sata_name(i, disks[i].port), BLK_SATA, 0, disks[i].port);
  76. if (!d)
  77. break;
  78. d->present = 1;
  79. d->total_sectors = disks[i].sectors;
  80. for (j = 0; j < 40; j++)
  81. d->model[j] = disks[i].model[j];
  82. d->model[40] = '\0';
  83. }
  84. }
  85. }
  86. static int name_eq(const char *a, const char *b)
  87. {
  88. while (*a && *b) {
  89. if (*a != *b)
  90. return 0;
  91. a++;
  92. b++;
  93. }
  94. return (*a == *b);
  95. }
  96. blk_dev_t *blk_find(const char *name)
  97. {
  98. unsigned int i;
  99. for (i = 0; i < blk_dev_count; i++)
  100. if (name_eq(blk_devs[i].name, name))
  101. return &blk_devs[i];
  102. return 0;
  103. }
  104. int blk_read(blk_dev_t *d, unsigned int lba, void *buf, unsigned int count)
  105. {
  106. if (!d || count == 0)
  107. return -1;
  108. if (d->kind == BLK_FLOPPY) {
  109. disk_set_drive(d->bus);
  110. return disk_read_sectors(lba, buf, count);
  111. }
  112. if (d->kind == BLK_SATA)
  113. return ahci_read_sectors(d->unit, lba, buf, count);
  114. return ata_read_sectors(d->bus, d->unit, lba, buf, count);
  115. }
  116. int blk_write(blk_dev_t *d, unsigned int lba, const void *buf, unsigned int count)
  117. {
  118. if (!d || count == 0)
  119. return -1;
  120. if (d->kind == BLK_FLOPPY) {
  121. disk_set_drive(d->bus);
  122. return disk_write_sectors(lba, buf, count);
  123. }
  124. if (d->kind == BLK_SATA)
  125. return ahci_write_sectors(d->unit, lba, buf, count);
  126. return ata_write_sectors(d->bus, d->unit, lba, buf, count);
  127. }