Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
20905ec95f |
|||
|
0aec18db63 |
7 changed files with 221 additions and 34 deletions
|
|
@ -19,6 +19,7 @@
|
|||
#include <lib/endian.h>
|
||||
|
||||
#define SPIN_MAX 2000
|
||||
#define AHCI_ALIAS "pci:010601"
|
||||
|
||||
static uint8_t sata_device_count = 0;
|
||||
static uint8_t cd_device_count = 0;
|
||||
|
|
@ -392,8 +393,8 @@ static int ahci_init_device(struct pci_device *device)
|
|||
|
||||
void ahci_init()
|
||||
{
|
||||
driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, pci_driver_class(1, 6, 1));
|
||||
driver_register("ahci", DRIVER_PCI, ahci_init_device, NULL, AHCI_ALIAS);
|
||||
krdy("AHCI ready\n");
|
||||
}
|
||||
DRIVER_ENTRY_PRIORITY(ahci_init, 2);
|
||||
DRIVER_ALIAS("pci:010601");
|
||||
DRIVER_ALIAS(AHCI_ALIAS);
|
||||
|
|
|
|||
|
|
@ -79,7 +79,11 @@ 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);
|
||||
struct driver *driver = driver_find(DRIVER_PCI, driverclass);
|
||||
char alias[11];
|
||||
|
||||
snprintf(alias, 11, "pci:%06x", driverclass);
|
||||
|
||||
struct driver *driver = driver_find(alias);
|
||||
|
||||
if (driver && driver->init_device) {
|
||||
pci_init_t f = driver->init_device;
|
||||
|
|
|
|||
|
|
@ -12,8 +12,7 @@
|
|||
#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
|
||||
|
||||
|
|
@ -33,11 +32,13 @@ 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, u32 class);
|
||||
struct driver *driver_find(u32 type, u32 class);
|
||||
struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, const char *alias);
|
||||
struct driver *driver_find(const char *alias);
|
||||
int driver_load(const char *name);
|
||||
int driver_unload(const char *name);
|
||||
|
||||
|
|
|
|||
33
include/lib/hashchain.h
Normal file
33
include/lib/hashchain.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _ht_H_
|
||||
#define _ht_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/mtx.h>
|
||||
|
||||
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_ */
|
||||
|
|
@ -2,17 +2,33 @@
|
|||
#include <kernel/driver.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <lib/string.h>
|
||||
#include <lib/rbtree.h>
|
||||
#include <lib/hashchain.h>
|
||||
#include <fs/vfs.h>
|
||||
|
||||
static rbtree_t driver_registry;
|
||||
#define DRIVER_HT_SIZE 128
|
||||
|
||||
static ht_t *driver_registry;
|
||||
static char *modules_alias_data;
|
||||
static size_t modules_alias_len;
|
||||
|
||||
list_t pending_drivers = {0};
|
||||
|
||||
struct driver *driver_register(const char *name, u32 type, void *init, void *deinit, u32 class)
|
||||
static void driver_registry_init()
|
||||
{
|
||||
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);
|
||||
|
||||
|
|
@ -46,20 +62,16 @@ struct driver *driver_register(const char *name, u32 type, void *init, void *dei
|
|||
driver->init_device = init;
|
||||
driver->deinit_device = deinit;
|
||||
|
||||
u64 key = (u64)type << 32;
|
||||
key |= class;
|
||||
strncpy(driver->alias, alias, sizeof(driver->alias) - 1);
|
||||
|
||||
rbnode_t *node = rbt_insert(&driver_registry, key);
|
||||
if (!node) {
|
||||
kfree(driver);
|
||||
return NULL;
|
||||
}
|
||||
node->ptr = driver;
|
||||
ht_lock(driver_registry);
|
||||
ht_insert(driver_registry, driver->alias, driver);
|
||||
ht_unlock(driver_registry);
|
||||
|
||||
return driver;
|
||||
}
|
||||
|
||||
static const char *alias_lookup(u32 type, u32 class)
|
||||
static const char *alias_lookup(const char *alias)
|
||||
{
|
||||
if (!modules_alias_data) {
|
||||
dirent_t *dent;
|
||||
|
|
@ -76,10 +88,7 @@ static const char *alias_lookup(u32 type, u32 class)
|
|||
}
|
||||
|
||||
char want[64];
|
||||
if (type == DRIVER_PCI)
|
||||
snprintf(want, sizeof(want), "pci:%06x ", class);
|
||||
else
|
||||
return NULL;
|
||||
snprintf(want, sizeof(want), "%s ", alias);
|
||||
|
||||
size_t want_len = strlen(want);
|
||||
char *p = modules_alias_data;
|
||||
|
|
@ -103,21 +112,21 @@ static const char *alias_lookup(u32 type, u32 class)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
struct driver *driver_find(u32 type, u32 class)
|
||||
struct driver *driver_find(const char *alias)
|
||||
{
|
||||
u64 key = (u64)type << 32;
|
||||
key |= class;
|
||||
|
||||
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 (!driver_registry) {
|
||||
driver_registry_init();
|
||||
if (!driver_registry)
|
||||
return NULL;
|
||||
|
||||
return node->ptr;
|
||||
}
|
||||
|
||||
struct driver *ret = ht_lookup(driver_registry, alias);
|
||||
if (!ret) {
|
||||
const char *modname = alias_lookup(alias);
|
||||
if (modname && driver_load(modname) == 0) {
|
||||
ret = ht_lookup(driver_registry, alias);
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,3 +8,4 @@ obj-y += string.o
|
|||
obj-y += list.o
|
||||
obj-y += sem.o
|
||||
obj-y += rwlock.o
|
||||
obj-y += hashchain.o
|
||||
|
|
|
|||
138
lib/hashchain.c
Normal file
138
lib/hashchain.c
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/mtx.h>
|
||||
#include <lib/hashchain.h>
|
||||
#include <lib/math.h>
|
||||
#include <lib/string.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
Loading…
Reference in a new issue