PCI IDE driver, new create_volume () syscall, test scripts
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m37s
All checks were successful
Build documentation / build-and-deploy (push) Successful in 2m37s
This commit is contained in:
307
kernel/device/idedrv.c
Normal file
307
kernel/device/idedrv.c
Normal file
@@ -0,0 +1,307 @@
|
||||
#include <amd64/io.h>
|
||||
#include <device/device.h>
|
||||
#include <device/idedrv.h>
|
||||
#include <devices.h>
|
||||
#include <libk/std.h>
|
||||
#include <mm/liballoc.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
#include <status.h>
|
||||
|
||||
#define IDE_REG_DATA 0x00
|
||||
#define IDE_REG_ERROR 0x01
|
||||
#define IDE_REG_SECCOUNT 0x02
|
||||
#define IDE_REG_LBA0 0x03
|
||||
#define IDE_REG_LBA1 0x04
|
||||
#define IDE_REG_LBA2 0x05
|
||||
#define IDE_REG_DRIVE 0x06
|
||||
#define IDE_REG_STATUS 0x07
|
||||
#define IDE_REG_CMD 0x07
|
||||
|
||||
#define IDE_BSY 0x80
|
||||
#define IDE_DRDY 0x40
|
||||
#define IDE_DF 0x20
|
||||
#define IDE_ERR 0x01
|
||||
#define IDE_DRQ 0x08
|
||||
|
||||
#define IDE_CMD_READ28 0x20
|
||||
#define IDE_CMD_WRITE28 0x30
|
||||
#define IDE_CMD_READ48 0x24
|
||||
#define IDE_CMD_WRITE48 0x34
|
||||
#define IDE_CMD_FLUSH48 0xEA
|
||||
#define IDE_CMD_FLUSH28 0xE7
|
||||
#define IDE_CMD_IDENTIFY 0xEC
|
||||
|
||||
static bool ide_wait (uint16_t io, uint32_t timeout, bool drq, bool errcheck) {
|
||||
uint32_t i = 0;
|
||||
uint8_t status;
|
||||
|
||||
for (;;) {
|
||||
status = inb (io + IDE_REG_STATUS);
|
||||
|
||||
if (!(status & IDE_BSY))
|
||||
break;
|
||||
|
||||
if (++i >= timeout)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (drq) {
|
||||
i = 0;
|
||||
while (!(status & IDE_DRQ)) {
|
||||
if (status & IDE_ERR)
|
||||
return false;
|
||||
|
||||
status = inb (io + IDE_REG_STATUS);
|
||||
|
||||
if (status == 0xFF)
|
||||
return true;
|
||||
|
||||
if (++i >= timeout)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (errcheck && (status & (IDE_DF | IDE_ERR)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#pragma clang optimize off
|
||||
static void ide_delay (uint16_t ctrl) {
|
||||
inb (ctrl);
|
||||
inb (ctrl);
|
||||
inb (ctrl);
|
||||
inb (ctrl);
|
||||
}
|
||||
#pragma clang optimize on
|
||||
|
||||
void ide_probe (uint16_t io, uint16_t ctrl, uint8_t devno, struct ide_probe* probe) {
|
||||
probe->flags = 0;
|
||||
probe->sector_count = 0;
|
||||
probe->sector_size = 0;
|
||||
|
||||
uint16_t identify_buffer[256];
|
||||
|
||||
uint8_t status = inb (io + IDE_REG_STATUS);
|
||||
|
||||
if (status == 0xFF)
|
||||
return;
|
||||
|
||||
outb (io + IDE_REG_DRIVE, 0xA0 | (devno << 4));
|
||||
ide_delay (ctrl);
|
||||
|
||||
outb (io + IDE_REG_SECCOUNT, 0);
|
||||
outb (io + IDE_REG_LBA0, 0);
|
||||
outb (io + IDE_REG_LBA1, 0);
|
||||
outb (io + IDE_REG_LBA2, 0);
|
||||
|
||||
outb (io + IDE_REG_CMD, IDE_CMD_IDENTIFY);
|
||||
|
||||
status = inb (io + IDE_REG_STATUS);
|
||||
|
||||
if (status == 0)
|
||||
return;
|
||||
|
||||
if (!ide_wait (io, 100000, true, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
insw (io + IDE_REG_DATA, identify_buffer, 256);
|
||||
|
||||
probe->flags |= IDE_PROBE_AVAIL;
|
||||
|
||||
if ((identify_buffer[106] & 0xC000) == 0x4000) {
|
||||
if (identify_buffer[106] & (1 << 12)) {
|
||||
uint32_t words_per_sector =
|
||||
(uint32_t)identify_buffer[117] | ((uint32_t)identify_buffer[118] << 16);
|
||||
probe->sector_size = (size_t)words_per_sector * 2;
|
||||
}
|
||||
}
|
||||
|
||||
if ((identify_buffer[83] & (1 << 10)) != 0)
|
||||
probe->flags |= IDE_PROBE_LBA48;
|
||||
|
||||
if ((probe->flags & IDE_PROBE_LBA48)) {
|
||||
probe->sector_count =
|
||||
(size_t)((uint64_t)identify_buffer[100] | ((uint64_t)identify_buffer[101] << 16) |
|
||||
((uint64_t)identify_buffer[102] << 32) | ((uint64_t)identify_buffer[103] << 48));
|
||||
} else {
|
||||
probe->sector_count =
|
||||
(size_t)((uint64_t)identify_buffer[60] | ((uint64_t)identify_buffer[61] << 16));
|
||||
}
|
||||
|
||||
probe->io = io;
|
||||
probe->ctrl = ctrl;
|
||||
probe->devno = devno;
|
||||
|
||||
if (probe->sector_size == 0)
|
||||
probe->sector_size = 512;
|
||||
}
|
||||
|
||||
static void ide_prepare (struct idedrv* idedrv, size_t sector, uint16_t sector_count) {
|
||||
if (idedrv->lba48) {
|
||||
outb (idedrv->io + IDE_REG_DRIVE, 0x40 | (idedrv->devno << 4));
|
||||
ide_delay (idedrv->ctrl);
|
||||
|
||||
outb (idedrv->io + IDE_REG_SECCOUNT, (sector_count >> 8) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA0, (sector >> 24) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA1, (sector >> 32) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA2, (sector >> 40) & 0xFF);
|
||||
|
||||
outb (idedrv->io + IDE_REG_SECCOUNT, sector_count & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA0, sector & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA1, (sector >> 8) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA2, (sector >> 16) & 0xFF);
|
||||
} else {
|
||||
outb (idedrv->io + IDE_REG_DRIVE, 0xE0 | (idedrv->devno << 4) | ((sector >> 24) & 0xFF));
|
||||
ide_delay (idedrv->ctrl);
|
||||
|
||||
uint8_t count = (sector_count == 256) ? 0 : (uint8_t)sector_count;
|
||||
outb (idedrv->io + IDE_REG_SECCOUNT, count);
|
||||
outb (idedrv->io + IDE_REG_LBA0, sector & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA1, (sector >> 8) & 0xFF);
|
||||
outb (idedrv->io + IDE_REG_LBA2, (sector >> 16) & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
bool idedrv_init (struct device* device, void* arg) {
|
||||
struct idedrv_init* init = arg;
|
||||
|
||||
struct idedrv* idedrv = malloc (sizeof (*idedrv));
|
||||
|
||||
if (idedrv == NULL)
|
||||
return false;
|
||||
|
||||
idedrv->lba48 = init->lba48;
|
||||
idedrv->sector_count = init->sector_count;
|
||||
idedrv->sector_size = init->sector_size;
|
||||
idedrv->io = init->io;
|
||||
idedrv->ctrl = init->ctrl;
|
||||
idedrv->devno = init->devno;
|
||||
|
||||
device->udata = idedrv;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void idedrv_fini (struct device* device) {
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
free (idedrv);
|
||||
}
|
||||
|
||||
int idedrv_read (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, void* a1,
|
||||
void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)a4;
|
||||
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t sector = *(size_t*)a1;
|
||||
size_t sector_count = *(size_t*)a2;
|
||||
uint16_t* buffer = a3;
|
||||
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
if (sector + sector_count > idedrv->sector_count)
|
||||
return -ST_OOB_ERROR;
|
||||
|
||||
ide_prepare (idedrv, sector, sector_count);
|
||||
|
||||
uint8_t cmd = idedrv->lba48 ? IDE_CMD_READ48 : IDE_CMD_READ28;
|
||||
outb (idedrv->io + IDE_REG_CMD, cmd);
|
||||
|
||||
for (uint16_t s = 0; s < sector_count; s++) {
|
||||
if (!ide_wait (idedrv->io, 100000, true, true))
|
||||
return -ST_XDRV_READ_ERROR;
|
||||
|
||||
insw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)),
|
||||
idedrv->sector_size / 2);
|
||||
}
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int idedrv_write (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, void* a1,
|
||||
void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)a4;
|
||||
|
||||
if (a1 == NULL || a2 == NULL || a3 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t sector = *(size_t*)a1;
|
||||
size_t sector_count = *(size_t*)a2;
|
||||
uint16_t* buffer = a3;
|
||||
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
if (sector + sector_count > idedrv->sector_count)
|
||||
return -ST_OOB_ERROR;
|
||||
|
||||
ide_prepare (idedrv, sector, sector_count);
|
||||
|
||||
uint8_t cmd = idedrv->lba48 ? IDE_CMD_WRITE48 : IDE_CMD_WRITE28;
|
||||
outb (idedrv->io + IDE_REG_CMD, cmd);
|
||||
|
||||
for (uint16_t s = 0; s < sector_count; s++) {
|
||||
if (!ide_wait (idedrv->io, 100000, true, true))
|
||||
return -ST_XDRV_WRITE_ERROR;
|
||||
|
||||
outsw (idedrv->io + IDE_REG_DATA, buffer + (s * (idedrv->sector_size / 2)),
|
||||
idedrv->sector_size / 2);
|
||||
}
|
||||
|
||||
cmd = idedrv->lba48 ? IDE_CMD_FLUSH48 : IDE_CMD_FLUSH28;
|
||||
outb (idedrv->io + IDE_REG_CMD, cmd);
|
||||
ide_wait (idedrv->io, 100000, false, true);
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int idedrv_get_device_type (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
void* a1, void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)device, (void)a2, (void)a3, (void)a4;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
int* device_type = (int*)a1;
|
||||
|
||||
*device_type = XDRV_TYPE_IDEDRV;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int idedrv_get_sector_size (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
void* a1, void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)a2, (void)a3, (void)a4;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* secsize = (size_t*)a1;
|
||||
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
*secsize = idedrv->sector_size;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
|
||||
int idedrv_get_size (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
void* a1, void* a2, void* a3, void* a4) {
|
||||
(void)proc, (void)rctx, (void)a2, (void)a3, (void)a4;
|
||||
|
||||
if (a1 == NULL)
|
||||
return -ST_BAD_ADDRESS_SPACE;
|
||||
|
||||
size_t* size = (size_t*)a1;
|
||||
|
||||
struct idedrv* idedrv = device->udata;
|
||||
|
||||
*size = idedrv->sector_size * idedrv->sector_count;
|
||||
|
||||
return ST_OK;
|
||||
}
|
||||
58
kernel/device/idedrv.h
Normal file
58
kernel/device/idedrv.h
Normal file
@@ -0,0 +1,58 @@
|
||||
#ifndef _KERNEL_DEVICE_IDEDRV_H
|
||||
#define _KERNEL_DEVICE_IDEDRV_H
|
||||
|
||||
#include <libk/std.h>
|
||||
#include <proc/proc.h>
|
||||
#include <proc/reschedule.h>
|
||||
|
||||
#define IDE_PROBE_AVAIL (1 << 0)
|
||||
#define IDE_PROBE_LBA48 (1 << 1)
|
||||
|
||||
struct device;
|
||||
|
||||
struct idedrv_init {
|
||||
bool lba48;
|
||||
size_t sector_count;
|
||||
size_t sector_size;
|
||||
uint16_t io, ctrl;
|
||||
uint8_t devno;
|
||||
};
|
||||
|
||||
struct idedrv {
|
||||
bool lba48;
|
||||
size_t sector_count;
|
||||
size_t sector_size;
|
||||
uint16_t io, ctrl;
|
||||
uint8_t devno;
|
||||
};
|
||||
|
||||
struct ide_probe {
|
||||
uint16_t flags;
|
||||
size_t sector_count;
|
||||
size_t sector_size;
|
||||
uint16_t io, ctrl;
|
||||
uint8_t devno;
|
||||
};
|
||||
|
||||
bool idedrv_init (struct device* device, void* arg);
|
||||
|
||||
void idedrv_fini (struct device* device);
|
||||
|
||||
int idedrv_read (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, void* a1,
|
||||
void* a2, void* a3, void* a4);
|
||||
|
||||
int idedrv_write (struct device* device, struct proc* proc, struct reschedule_ctx* rctx, void* a1,
|
||||
void* a2, void* a3, void* a4);
|
||||
|
||||
int idedrv_get_device_type (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
void* a1, void* a2, void* a3, void* a4);
|
||||
|
||||
int idedrv_get_sector_size (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
void* a1, void* a2, void* a3, void* a4);
|
||||
|
||||
int idedrv_get_size (struct device* device, struct proc* proc, struct reschedule_ctx* rctx,
|
||||
void* a1, void* a2, void* a3, void* a4);
|
||||
|
||||
void ide_probe (uint16_t io, uint16_t ctrl, uint8_t devno, struct ide_probe* probe);
|
||||
|
||||
#endif // _KERNEL_DEVICE_IDEDRV_H
|
||||
@@ -1,20 +1,94 @@
|
||||
#include <amd64/io.h>
|
||||
#include <device/pci.h>
|
||||
#include <device/pci_ide.h>
|
||||
#include <device/pci_info.h>
|
||||
#include <libk/lengthof.h>
|
||||
#include <libk/std.h>
|
||||
#include <sync/spin_lock.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
static uint32_t pci_read32 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
|
||||
static const struct pci_driver_info pci_driver_infos[] = {
|
||||
{.class = 0x01, .subclass = 0x01, .init = &pci_ide_init},
|
||||
};
|
||||
|
||||
static spin_lock_t pci_lock = SPIN_LOCK_INIT;
|
||||
|
||||
uint32_t pci_read32 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
|
||||
uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) |
|
||||
(offset & 0xFc) | ((uint32_t)0x80000000);
|
||||
(offset & 0xFC) | ((uint32_t)0x80000000);
|
||||
|
||||
spin_lock (&pci_lock);
|
||||
|
||||
outl (PCI_CONFIG_ADDR, addr);
|
||||
return inl (PCI_CONFIG_DATA);
|
||||
uint32_t r = inl (PCI_CONFIG_DATA);
|
||||
|
||||
spin_unlock (&pci_lock);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static void pci_write32 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t value) {
|
||||
void pci_write32 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t value) {
|
||||
uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) |
|
||||
(offset & 0xFc) | ((uint32_t)0x80000000);
|
||||
(offset & 0xFC) | ((uint32_t)0x80000000);
|
||||
|
||||
spin_lock (&pci_lock);
|
||||
|
||||
outl (PCI_CONFIG_ADDR, addr);
|
||||
outl (PCI_CONFIG_DATA, value);
|
||||
|
||||
spin_unlock (&pci_lock);
|
||||
}
|
||||
|
||||
uint16_t pci_read16 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
|
||||
uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) |
|
||||
(offset & 0xFC) | ((uint32_t)0x80000000);
|
||||
|
||||
spin_lock (&pci_lock);
|
||||
|
||||
outl (PCI_CONFIG_ADDR, addr);
|
||||
uint16_t r = inw (PCI_CONFIG_DATA + (offset & 2));
|
||||
|
||||
spin_unlock (&pci_lock);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void pci_write16 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t value) {
|
||||
uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) |
|
||||
(offset & 0xFC) | ((uint32_t)0x80000000);
|
||||
|
||||
spin_lock (&pci_lock);
|
||||
|
||||
outl (PCI_CONFIG_ADDR, addr);
|
||||
outw (PCI_CONFIG_DATA + (offset & 2), value);
|
||||
|
||||
spin_unlock (&pci_lock);
|
||||
}
|
||||
|
||||
uint8_t pci_read8 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset) {
|
||||
uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) |
|
||||
(offset & 0xFC) | ((uint32_t)0x80000000);
|
||||
|
||||
spin_lock (&pci_lock);
|
||||
|
||||
outl (PCI_CONFIG_ADDR, addr);
|
||||
uint8_t r = inb (PCI_CONFIG_DATA + (offset & 3));
|
||||
|
||||
spin_unlock (&pci_lock);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
void pci_write8 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint8_t value) {
|
||||
uint32_t addr = (uint32_t)((uint32_t)bus << 16) | ((uint32_t)slot << 11) | ((uint32_t)func << 8) |
|
||||
(offset & 0xFC) | ((uint32_t)0x80000000);
|
||||
|
||||
spin_lock (&pci_lock);
|
||||
|
||||
outl (PCI_CONFIG_ADDR, addr);
|
||||
outb (PCI_CONFIG_DATA + (offset & 3), value);
|
||||
|
||||
spin_unlock (&pci_lock);
|
||||
}
|
||||
|
||||
static void pci_check_bus (uint8_t bus, pci_cb_func_t cb);
|
||||
@@ -28,20 +102,20 @@ static void pci_check_func (uint8_t bus, uint8_t slot, uint8_t func, pci_cb_func
|
||||
|
||||
uint32_t reg8 = pci_read32 (bus, slot, func, PCI_REVISION_ID);
|
||||
|
||||
struct pci_dev pci_dev = {
|
||||
struct pci_info pci_info = {
|
||||
.bus = bus,
|
||||
.slot = slot,
|
||||
.func = func,
|
||||
.vendor = vendor,
|
||||
.device = ((uint16_t)(reg0 >> 16)),
|
||||
.classcode = ((uint8_t)(reg8 >> 24)),
|
||||
.class = ((uint8_t)(reg8 >> 24)),
|
||||
.subclass = ((uint8_t)(reg8 >> 16)),
|
||||
};
|
||||
|
||||
cb (pci_dev);
|
||||
cb (pci_info);
|
||||
|
||||
/* PCI 2 PCI bridge */
|
||||
if (pci_dev.classcode == 0x06 && pci_dev.subclass == 0x04) {
|
||||
if (pci_info.class == 0x06 && pci_info.subclass == 0x04) {
|
||||
uint32_t reg18 = pci_read32 (bus, slot, func, 0x18);
|
||||
uint8_t secondary = (uint8_t)(reg18 >> 8);
|
||||
pci_check_bus (secondary, cb);
|
||||
@@ -112,13 +186,21 @@ static void pci_string_identifiers (uint16_t vendor_id, uint16_t device_id, uint
|
||||
}
|
||||
}
|
||||
|
||||
static void pci_discovery_cb (struct pci_dev pci_dev) {
|
||||
static void pci_discovery_cb (struct pci_info pci_info) {
|
||||
const char *vname, *dname, *cname;
|
||||
pci_string_identifiers (pci_dev.vendor, pci_dev.device, pci_dev.classcode, pci_dev.subclass,
|
||||
pci_string_identifiers (pci_info.vendor, pci_info.device, pci_info.class, pci_info.subclass,
|
||||
&vname, &dname, &cname);
|
||||
|
||||
DEBUG ("PCI DEVICE: %04x:%04x at %02d:%02d:%02d / %s; %s; %s\n", pci_dev.vendor, pci_dev.device,
|
||||
pci_dev.bus, pci_dev.slot, pci_dev.func, vname, dname, cname);
|
||||
DEBUG ("PCI DEVICE: %04x:%04x %02x:%02x at %03d:%03d:%03d / %s; %s; %s\n", pci_info.vendor,
|
||||
pci_info.device, pci_info.class, pci_info.subclass, pci_info.bus, pci_info.slot,
|
||||
pci_info.func, vname, dname, cname);
|
||||
|
||||
for (size_t driver = 0; driver < lengthof (pci_driver_infos); driver++) {
|
||||
if (pci_driver_infos[driver].class == pci_info.class &&
|
||||
pci_driver_infos[driver].subclass == pci_info.subclass) {
|
||||
pci_driver_infos[driver].init (pci_info);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void pci_init (void) { pci_enumerate (&pci_discovery_cb); }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef _KERNEL_DEVICE_PCI_H
|
||||
#define _KERNEL_DEVICE_PCI_H
|
||||
|
||||
#include <device/pci_info.h>
|
||||
#include <libk/std.h>
|
||||
|
||||
#define PCI_CONFIG_ADDR 0xCF8
|
||||
@@ -20,16 +21,6 @@
|
||||
#define PCI_BIST 0x0F
|
||||
#define PCI_BAR0 0x10
|
||||
|
||||
struct pci_dev {
|
||||
uint8_t bus;
|
||||
uint8_t slot;
|
||||
uint8_t func;
|
||||
uint16_t vendor;
|
||||
uint16_t device;
|
||||
uint8_t classcode;
|
||||
uint8_t subclass;
|
||||
};
|
||||
|
||||
struct pci_vendor {
|
||||
uint16_t id;
|
||||
const char* name;
|
||||
@@ -47,10 +38,28 @@ struct pci_class {
|
||||
const char* name;
|
||||
};
|
||||
|
||||
typedef void (*pci_cb_func_t) (struct pci_dev pci_dev);
|
||||
struct pci_driver_info {
|
||||
uint8_t class;
|
||||
uint8_t subclass;
|
||||
bool (*init) (struct pci_info pci_info);
|
||||
};
|
||||
|
||||
typedef void (*pci_cb_func_t) (struct pci_info pci_info);
|
||||
|
||||
void pci_init (void);
|
||||
|
||||
uint32_t pci_read32 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
|
||||
|
||||
void pci_write32 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint32_t value);
|
||||
|
||||
uint16_t pci_read16 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
|
||||
|
||||
void pci_write16 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint16_t value);
|
||||
|
||||
uint8_t pci_read8 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset);
|
||||
|
||||
void pci_write8 (uint8_t bus, uint8_t slot, uint8_t func, uint8_t offset, uint8_t value);
|
||||
|
||||
extern const struct pci_vendor pci_vendors[];
|
||||
|
||||
extern const struct pci_device_id pci_device_names[];
|
||||
|
||||
92
kernel/device/pci_ide.c
Normal file
92
kernel/device/pci_ide.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include <amd64/io.h>
|
||||
#include <device/device.h>
|
||||
#include <device/idedrv.h>
|
||||
#include <device/pci.h>
|
||||
#include <device/pci_info.h>
|
||||
#include <devices.h>
|
||||
#include <libk/fieldsizeof.h>
|
||||
#include <libk/lengthof.h>
|
||||
#include <libk/printf.h>
|
||||
#include <libk/std.h>
|
||||
#include <sys/debug.h>
|
||||
|
||||
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;
|
||||
}
|
||||
9
kernel/device/pci_ide.h
Normal file
9
kernel/device/pci_ide.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef _KERNEL_DEVICE_PCI_IDE_H
|
||||
#define _KERNEL_DEVICE_PCI_IDE_H
|
||||
|
||||
#include <device/pci_info.h>
|
||||
#include <libk/std.h>
|
||||
|
||||
bool pci_ide_init (struct pci_info pci_info);
|
||||
|
||||
#endif // _KERNEL_DEVICE_PCI_IDE_H
|
||||
16
kernel/device/pci_info.h
Normal file
16
kernel/device/pci_info.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef _KERNEL_DEVICE_PCI_INFO_H
|
||||
#define _KERNEL_DEVICE_PCI_INFO_H
|
||||
|
||||
#include <libk/std.h>
|
||||
|
||||
struct pci_info {
|
||||
uint8_t bus;
|
||||
uint8_t slot;
|
||||
uint8_t func;
|
||||
uint16_t vendor;
|
||||
uint16_t device;
|
||||
uint8_t class;
|
||||
uint8_t subclass;
|
||||
};
|
||||
|
||||
#endif // _KERNEL_DEVICE_PCI_INFO_H
|
||||
@@ -12,10 +12,14 @@ o += device/device.o \
|
||||
|
||||
ifeq ($(platform),amd64)
|
||||
c += device/ps2_kb.c \
|
||||
device/idedrv.c \
|
||||
device/pci.c \
|
||||
device/pci_defs.c
|
||||
device/pci_defs.c \
|
||||
device/pci_ide.c
|
||||
|
||||
o += device/ps2_kb.o \
|
||||
device/idedrv.o \
|
||||
device/pci.o \
|
||||
device/pci_defs.o
|
||||
device/pci_defs.o \
|
||||
device/pci_ide.o
|
||||
endif
|
||||
|
||||
Reference in New Issue
Block a user