#include #include #include #include #include #include #include #include #include #include #include static atomic_int ide_counter = 0; static const char* progif_msg[] = { [0x00] = "ISA Compatibility mode-only controller", [0x05] = "PCI native mode-only controller", [0x0A] = "ISA Compatibility mode controller, supports both channels switched to PCI native mode", [0x0F] = "PCI native mode controller, supports both channels switched to ISA compatibility mode", [0x80] = "ISA Compatibility mode-only controller, supports bus mastering", [0x85] = "PCI native mode-only controller, supports bus mastering", [0x8A] = "ISA Compatibility mode controller, supports both channels switched to PCI native mode, supports bus mastering", [0x8F] = "PCI native mode controller, supports both channels switched to ISA compatibility mode, supports bus mastering", }; static void ide_make_device (struct ide_probe probe) { DEBUG ("Found IDE drive: io=%x ctrl=%x no=%u sector=%zu count=%zu\n", probe.io, probe.ctrl, probe.devno, probe.sector_size, probe.sector_count); char device_key[fieldsizeof (struct device, key)]; snprintf (device_key, sizeof (device_key), "IDE%d", atomic_fetch_add (&ide_counter, 1)); device_op_func_t ops[] = { [XDRV_GET_SIZE] = &idedrv_get_size, [XDRV_GET_SECTOR_SIZE] = &idedrv_get_sector_size, [XDRV_GET_DEVICE_TYPE] = &idedrv_get_device_type, [XDRV_READ] = &idedrv_read, [XDRV_WRITE] = &idedrv_write, }; struct idedrv_init init = { .lba48 = ((probe.flags & IDE_PROBE_LBA48) != 0), .sector_count = probe.sector_count, .sector_size = probe.sector_size, .io = probe.io, .ctrl = probe.ctrl, .devno = probe.devno, }; device_create (device_key, ops, lengthof (ops), &idedrv_init, &idedrv_fini, &init); } bool pci_ide_init (struct pci_info pci_info) { struct ide_probe probe; uint8_t progif = pci_read8 (pci_info.bus, pci_info.slot, pci_info.func, PCI_PROG_IF); DEBUG ("progif: %s\n", progif_msg[progif]); switch (progif) { case 0x80: ide_probe (0x1F0, 0x3F6, 0, &probe); if ((probe.flags & IDE_PROBE_AVAIL)) ide_make_device (probe); ide_probe (0x1F0, 0x3F6, 1, &probe); if ((probe.flags & IDE_PROBE_AVAIL)) ide_make_device (probe); ide_probe (0x170, 0x376, 0, &probe); if ((probe.flags & IDE_PROBE_AVAIL)) ide_make_device (probe); ide_probe (0x170, 0x376, 1, &probe); if ((probe.flags & IDE_PROBE_AVAIL)) ide_make_device (probe); break; default: DEBUG ("PCI unsupported progif=%02x\n", progif); return false; } return true; }