diff --git a/drivers/ahci.c b/drivers/ahci.c index 3185427..da92459 100644 --- a/drivers/ahci.c +++ b/drivers/ahci.c @@ -19,7 +19,6 @@ #include #define SPIN_MAX 2000 -#define AHCI_ALIAS "pci:010601" static uint8_t sata_device_count = 0; static uint8_t cd_device_count = 0; @@ -393,8 +392,8 @@ static int ahci_init_device(struct pci_device *device) void ahci_init() { - driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, AHCI_ALIAS); + driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, pci_driver_class(1, 6, 1)); krdy("AHCI ready\n"); } DRIVER_ENTRY_PRIORITY(ahci_init, 2); -DRIVER_ALIAS(AHCI_ALIAS); +DRIVER_ALIAS("pci:010601"); diff --git a/drivers/pci/pci.c b/drivers/pci/pci.c index 7bb7712..1e4ab2e 100644 --- a/drivers/pci/pci.c +++ b/drivers/pci/pci.c @@ -79,11 +79,7 @@ size_t pci_find_device(u16 major, u16 minor, list_t *out) static void device_init(struct pci_device *pdev) { u32 driverclass = pci_driver_class(pdev->class_major, pdev->class_minor, pdev->prog_if); - char alias[11]; - - snprintf(alias, 11, "pci:%06x", driverclass); - - struct driver *driver = driver_find(alias); + struct driver *driver = driver_find(DRIVER_PCI, driverclass); if (driver && driver->init_device) { pci_init_t f = driver->init_device; diff --git a/include/kernel/driver.h b/include/kernel/driver.h index 65d4656..bafa244 100644 --- a/include/kernel/driver.h +++ b/include/kernel/driver.h @@ -12,7 +12,8 @@ #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 + static const char __driver_alias_##__LINE__[] \ + __attribute__((used, section(".modinfo"), aligned(1))) = "alias=" _alias #define DRIVER_PCI 1 @@ -32,13 +33,11 @@ struct driver { void *base; size_t size; - char alias[64]; - u32 n_users; }; -struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, const char *alias); -struct driver *driver_find(const char *alias); +struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class); +struct driver *driver_find(u32 type, u32 class); int driver_load(const char *name); int driver_unload(const char *name); diff --git a/include/lib/hashchain.h b/include/lib/hashchain.h deleted file mode 100644 index 77c0ad4..0000000 --- a/include/lib/hashchain.h +++ /dev/null @@ -1,33 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#ifndef _ht_H_ -#define _ht_H_ - -#include -#include - -typedef struct ht_entry { - char *key; - union { - void *ptr; - u64 val; - }; - struct ht_entry *next; -} ht_entry_t; - -typedef struct { - ht_entry_t **buckets; - size_t capacity; - size_t size; - - mtx_t lock; -} ht_t; - -ht_t *ht_create(size_t capacity); -void ht_destroy(ht_t *ht); -int ht_insert(ht_t *ht, const char *key, void *value); -void *ht_lookup(ht_t *ht, const char *key); -int ht_delete(ht_t *ht, const char *key); -void ht_lock(ht_t *ht); -void ht_unlock(ht_t *ht); - -#endif /* _ht_H_ */ diff --git a/kernel/driver.c b/kernel/driver.c index a7c31fd..62b42c2 100644 --- a/kernel/driver.c +++ b/kernel/driver.c @@ -2,33 +2,17 @@ #include #include #include -#include +#include #include -#define DRIVER_HT_SIZE 128 - -static ht_t *driver_registry; +static rbtree_t driver_registry; static char *modules_alias_data; static size_t modules_alias_len; list_t pending_drivers = {0}; -static void driver_registry_init() +struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class) { - driver_registry = ht_create(DRIVER_HT_SIZE); -} - -struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, const char *alias) -{ - if (!alias) - return NULL; - - if (!driver_registry) { - driver_registry_init(); - if (!driver_registry) - return NULL; - } - if (!pending_drivers.head && !pending_drivers.tail) list_create(&pending_drivers); @@ -62,16 +46,20 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei driver->init_device = init; driver->deinit_device = deinit; - strncpy(driver->alias, alias, sizeof(driver->alias) - 1); + u64 key = (u64)type << 32; + key |= class; - ht_lock(driver_registry); - ht_insert(driver_registry, driver->alias, driver); - ht_unlock(driver_registry); + rbnode_t *node = rbt_insert(&driver_registry, key); + if (!node) { + kfree(driver); + return NULL; + } + node->ptr = driver; return driver; } -static const char *alias_lookup(const char *alias) +static const char *alias_lookup(u32 type, u32 class) { if (!modules_alias_data) { dirent_t *dent; @@ -88,7 +76,10 @@ static const char *alias_lookup(const char *alias) } char want[64]; - snprintf(want, sizeof(want), "%s ", alias); + 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; @@ -112,21 +103,21 @@ static const char *alias_lookup(const char *alias) return NULL; } -struct driver *driver_find(const char *alias) +struct driver *driver_find(u32 type, u32 class) { - if (!driver_registry) { - driver_registry_init(); - if (!driver_registry) - return NULL; - } + u64 key = (u64)type << 32; + key |= class; - struct driver *ret = ht_lookup(driver_registry, alias); - if (!ret) { - const char *modname = alias_lookup(alias); + rbnode_t *node = rbt_search(&driver_registry, key); + if (!node) { + const char *modname = alias_lookup(type, class); if (modname && driver_load(modname) == 0) { - ret = ht_lookup(driver_registry, alias); + node = rbt_search(&driver_registry, key); } } - return ret; + if (!node) + return NULL; + + return node->ptr; } diff --git a/lib/Makefile b/lib/Makefile index c19dae5..a238d4e 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -8,4 +8,3 @@ obj-y += string.o obj-y += list.o obj-y += sem.o obj-y += rwlock.o -obj-y += hashchain.o diff --git a/lib/hashchain.c b/lib/hashchain.c deleted file mode 100644 index 841bd2c..0000000 --- a/lib/hashchain.c +++ /dev/null @@ -1,138 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ -#include -#include -#include -#include -#include -#include - -#define ht_MIN 32 - -static u64 hash_str(const char *str) -{ - u64 hash = 5381; - for (const char *p = str; *p; p++) - hash = ((hash << 5) + hash) + (u64)*p; - return hash; -} - -ht_t *ht_create(size_t capacity) -{ - size_t cap = npow2(capacity); - if (cap < ht_MIN) - cap = ht_MIN; - ht_t *ht = kzalloc(sizeof(ht_t), ALLOC_KERN); - if (!ht) - return NULL; - - ht->buckets = kzalloc(sizeof(ht_entry_t *) * cap, ALLOC_KERN); - if (!ht->buckets) { - kfree(ht); - return NULL; - } - - ht->capacity = cap; - ht->size = 0; - return ht; -} - -void ht_destroy(ht_t *ht) -{ - if (!ht) - return; - - for (size_t i = 0; i < ht->capacity; i++) { - ht_entry_t *entry = ht->buckets[i]; - while (entry) { - ht_entry_t *next = entry->next; - kfree(entry->key); - kfree(entry); - entry = next; - } - } - - kfree(ht->buckets); - kfree(ht); -} - -int ht_insert(ht_t *ht, const char *key, void *value) -{ - u64 hash = hash_str(key); - size_t idx = hash & (ht->capacity - 1); - - ht_entry_t *entry = ht->buckets[idx]; - while (entry) { - if (!strcmp(entry->key, key)) { - return -EEXIST; - } - entry = entry->next; - } - - entry = kzalloc(sizeof(ht_entry_t), ALLOC_KERN); - if (!entry) - return -ENOMEM; - - entry->key = strdup(key); - if (!entry->key) { - kfree(entry); - return -ENOMEM; - } - - entry->ptr = value; - entry->next = ht->buckets[idx]; - ht->buckets[idx] = entry; - ht->size++; - return 0; -} - -void *ht_lookup(ht_t *ht, const char *key) -{ - u64 hash = hash_str(key); - size_t idx = hash & (ht->capacity - 1); - - ht_entry_t *entry = ht->buckets[idx]; - while (entry) { - if (strcmp(entry->key, key) == 0) - return entry->ptr; - entry = entry->next; - } - - return NULL; -} - -int ht_delete(ht_t *ht, const char *key) -{ - u64 hash = hash_str(key); - size_t idx = hash & (ht->capacity - 1); - - ht_entry_t *entry = ht->buckets[idx]; - ht_entry_t *prev = NULL; - - while (entry) { - if (strcmp(entry->key, key) == 0) { - if (prev) - prev->next = entry->next; - else - ht->buckets[idx] = entry->next; - - kfree(entry->key); - kfree(entry); - ht->size--; - return 0; - } - prev = entry; - entry = entry->next; - } - - return -1; -} - -void ht_lock(ht_t *ht) -{ - mtx_lock(&ht->lock); -} - -void ht_unlock(ht_t *ht) -{ - mtx_unlock(&ht->lock); -}