ahci: load AHCI as module

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-08-12 16:06:58 -04:00
commit cb2c3e1053
Signed by: michaelsshaw
GPG key ID: 5A5E0FB20708A9E7
6 changed files with 74 additions and 13 deletions

View file

@ -138,11 +138,12 @@ modules_install: modules
$(Q)echo -n > initramfs/modules/modules.alias $(Q)echo -n > initramfs/modules/modules.alias
$(Q)for ko in initramfs/modules/*.ko; do \ $(Q)for ko in initramfs/modules/*.ko; do \
[ -f "$$ko" ] || continue; \ [ -f "$$ko" ] || continue; \
modname=$$(basename "$$ko" .ko); \
readelf -p .modinfo "$$ko" 2>/dev/null | \ readelf -p .modinfo "$$ko" 2>/dev/null | \
sed -n 's/^\s*\[.*\]\s*//p' | \ sed -n 's/^\s*\[.*\]\s*//p' | \
while IFS= read -r line; do \ while IFS= read -r line; do \
case "$$line" in alias=*) \ case "$$line" in alias=*) \
echo "$${line#alias=} $$ko" >> initramfs/modules/modules.alias ;; \ echo "$${line#alias=} $$modname" >> initramfs/modules/modules.alias ;; \
esac; \ esac; \
done; \ done; \
done done

View file

@ -392,11 +392,8 @@ static int ahci_init_device(struct pci_device *device)
void ahci_init() void ahci_init()
{ {
list_t dev_list;
list_create(&dev_list);
driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, pci_driver_class(1, 6, 1)); driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, pci_driver_class(1, 6, 1));
krdy("AHCI ready\n"); krdy("AHCI ready\n");
} }
DRIVER_ENTRY_PRIORITY(ahci_init, 2); DRIVER_ENTRY_PRIORITY(ahci_init, 2);
DRIVER_ALIAS("pci:010601");

View file

@ -76,6 +76,17 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out)
return n; return n;
} }
static void device_init(struct pci_device *pdev)
{
u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if);
struct driver *driver = driver_find(DRIVER_PCI, driverclass);
if (driver && driver->init_device) {
pci_init_t f = driver->init_device;
f(pdev);
}
}
static void host_probe_devices(struct pci_host *host) static void host_probe_devices(struct pci_host *host)
{ {
for (int bus = 0; bus < 256; bus++) { for (int bus = 0; bus < 256; bus++) {
@ -138,14 +149,7 @@ static void host_probe_devices(struct pci_host *host)
break; break;
} }
/* call the driver */ device_init(pdev);
u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if);
struct driver *driver = driver_find(DRIVER_PCI, driverclass);
if (driver && driver->init_device) {
pci_init_t f = driver->init_device;
f(pdev);
}
kdebug("PCI device: vendor=%04x, id=%x, major=%x, minor=%x\n", pdev->vendor, pdev->device_id, kdebug("PCI device: vendor=%04x, id=%x, major=%x, minor=%x\n", pdev->vendor, pdev->device_id,
pdev->class_major, pdev->class_minor); pdev->class_major, pdev->class_minor);

View file

@ -11,6 +11,10 @@
#define DRIVER_ENTRY_PRIORITY(fn, pr) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall." #pr))) = fn #define DRIVER_ENTRY_PRIORITY(fn, pr) static void (*__initcall_##fn)(void) __attribute((used, section(".initcall." #pr))) = fn
#define DRIVER_EXIT(fn) static void (*__exitcall_##fn)(void) __attribute((used, section(".exitcall"))) = fn #define DRIVER_EXIT(fn) static void (*__exitcall_##fn)(void) __attribute((used, section(".exitcall"))) = fn
#define DRIVER_ALIAS(_alias) \
static const char __driver_alias_##__LINE__[] \
__attribute__((used, section(".modinfo"), aligned(1))) = "alias=" _alias
#define DRIVER_PCI 1 #define DRIVER_PCI 1
typedef int (*pci_init_t)(struct pci_device *dev); typedef int (*pci_init_t)(struct pci_device *dev);

View file

@ -15,6 +15,7 @@ void *memcpy_user(struct proc *proc, void *dest, const void *src, size_t num);
int memcmp(const void *aa, const void *bb, size_t num); int memcmp(const void *aa, const void *bb, size_t num);
int strcmp(const char *a, const char *b); int strcmp(const char *a, const char *b);
int strncmp(const char *a, const char *b, size_t n); int strncmp(const char *a, const char *b, size_t n);
char *strchr(const char *s, int c);
void *memset(void *str, int c, size_t n); void *memset(void *str, int c, size_t n);
char *strcpy(char *dest, const char *src); char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n); char *strncpy(char *dest, const char *src, size_t n);

View file

@ -3,8 +3,11 @@
#include <kernel/slab.h> #include <kernel/slab.h>
#include <lib/string.h> #include <lib/string.h>
#include <lib/rbtree.h> #include <lib/rbtree.h>
#include <fs/vfs.h>
static rbtree_t driver_registry; static rbtree_t driver_registry;
static char *modules_alias_data;
static size_t modules_alias_len;
list_t pending_drivers = {0}; list_t pending_drivers = {0};
@ -56,12 +59,63 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei
return driver; return driver;
} }
static const char *alias_lookup(u32 type, u32 class)
{
if (!modules_alias_data) {
dirent_t *dent;
if (vfs_open("/modules/modules.alias", &dent) < 0)
return NULL;
modules_alias_len = dent->inode->file_size;
modules_alias_data = kalloc(modules_alias_len + 1, ALLOC_KERN);
if (!modules_alias_data)
return NULL;
vfs_read(dent->inode, modules_alias_data, modules_alias_len, 0);
modules_alias_data[modules_alias_len] = '\0';
}
char want[64];
if (type == DRIVER_PCI)
snprintf(want, sizeof(want), "pci:%06x ", class);
else
return NULL;
size_t want_len = strlen(want);
char *p = modules_alias_data;
while (p && p < modules_alias_data + modules_alias_len) {
if (strncmp(p, want, want_len) == 0) {
char *nl = strchr(p + want_len, '\n');
size_t name_len = nl ? (size_t)(nl - p - want_len) : strlen(p + want_len);
char *modname = kalloc(name_len + 1, ALLOC_KERN);
if (modname) {
memcpy(modname, p + want_len, name_len);
modname[name_len] = '\0';
}
return modname;
}
char *nl = strchr(p, '\n');
p = nl ? nl + 1 : NULL;
}
return NULL;
}
struct driver *driver_find(u32 type, u32 class) struct driver *driver_find(u32 type, u32 class)
{ {
u64 key = (u64)type << 32; u64 key = (u64)type << 32;
key |= class; key |= class;
rbnode_t *node = rbt_search(&driver_registry, key); rbnode_t *node = rbt_search(&driver_registry, key);
if (!node) {
const char *modname = alias_lookup(type, class);
if (modname && driver_load(modname) == 0) {
node = rbt_search(&driver_registry, key);
}
}
if (!node) if (!node)
return NULL; return NULL;