ahci: add block operations

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-13 00:02:22 -04:00
commit fc0720b118
5 changed files with 60 additions and 9 deletions

View file

@ -1,4 +1,4 @@
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
obj-y += acpi/ obj-y += acpi/
obj-y += pci/ obj-y += pci/
#obj-y += ahci.o obj-y += ahci.o

View file

@ -5,10 +5,13 @@
#include <drivers/pci.h> #include <drivers/pci.h>
#include <drivers/ahci.h> #include <drivers/ahci.h>
#include <kernel/block.h>
#include <lib/string.h> #include <lib/string.h>
#include <lib/stdio.h> #include <lib/stdio.h>
#include <fs/devfs.h>
static uint8_t sata_device_count = 0; static uint8_t sata_device_count = 0;
static struct sata_device sata_devices[32] = {0}; static struct sata_device sata_devices[32] = {0};
@ -212,8 +215,18 @@ int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, pa
return true; return true;
} }
void ahci_init_device(struct pci_device *device) static ssize_t ahci_block_read(struct block_device *dev, char *buf, size_t off, size_t n_blocks)
{ {
}
static int ahci_init_device(struct pci_device *device, size_t ahci_dev_no)
{
struct block_device *bdev = kzalloc(sizeof(struct block_device), ALLOC_KERN);
if (!bdev)
return -ENOMEM;
return 0;
} }
void ahci_init() void ahci_init()
@ -224,8 +237,13 @@ void ahci_init()
size_t n = pci_find_device(0x0001, 0x0006, &dev_list); size_t n = pci_find_device(0x0001, 0x0006, &dev_list);
list_item_t *item = dev_list.head; list_item_t *item = dev_list.head;
size_t i = 0;
while (item) { while (item) {
ahci_init_device(item->ptr); int res = ahci_init_device(item->ptr, i++);
if (res < 0)
kerr("ahci_init_device: %s\n", strerror(-res));
item = item->next; item = item->next;
} }

View file

@ -4,7 +4,6 @@
#include <kernel/spinlock.h> #include <kernel/spinlock.h>
#include <kernel/slab.h> #include <kernel/slab.h>
#include <kernel/block.h>
#include <fs/inode.h> #include <fs/inode.h>
#include <lib/rwlock.h> #include <lib/rwlock.h>
@ -54,6 +53,8 @@
#define FS_RDONLY 0x00000001 #define FS_RDONLY 0x00000001
struct block_device;
struct fs_driver { struct fs_driver {
char name[FILE_NAME_MAX_LEN + 1]; char name[FILE_NAME_MAX_LEN + 1];
int (*init)(int flags, struct block_device *to_mount, fs_t **out); int (*init)(int flags, struct block_device *to_mount, fs_t **out);

View file

@ -2,6 +2,7 @@
#ifndef _BLOCK_H_ #ifndef _BLOCK_H_
#define _BLOCK_H_ #define _BLOCK_H_
#include <fs/vfs.h>
#include <kernel/device.h> #include <kernel/device.h>
struct block_device; struct block_device;
@ -11,11 +12,19 @@ struct block_ops {
}; };
struct block_device { struct block_device {
char *name; char name[256];
dev_t dev; dev_t dev;
size_t size; size_t size;
size_t block_size; size_t block_size;
struct block_ops ops; struct block_ops ops;
void *priv;
size_t lba_start;
};
struct disk {
struct block_device *dev;
char model[FILE_NAME_MAX_LEN + 1];
list_t part_table;
}; };
ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks); ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks);

View file

@ -2,6 +2,29 @@
#include <kernel/common.h> #include <kernel/common.h>
#include <kernel/block.h> #include <kernel/block.h>
#include <lib/string.h>
#include <fs/devfs.h>
int block_device_register(char *name, size_t lba_start, struct block_ops *ops, struct block_device **out)
{
struct block_device *ret = kzalloc(sizeof(struct block_device), ALLOC_KERN);
if (!ret)
return -ENOMEM;
ret->lba_start = lba_start;
strncpy(ret->name, name, sizeof(ret->name) - 1);
*out = ret;
memcpy(ops, &ret->ops, sizeof(struct block_ops));
dirent_t *dirent;
int res = devfs_insert_device_blk(ret->name, ret, &dirent);
return 0;
}
ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks) ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks)
{ {
return dev->ops.read(dev, buf, offset, n_blocks); return dev->ops.read(dev, buf, offset, n_blocks);