diff --git a/dev/ahci.c b/dev/ahci.c index 752ec52..7ed87fa 100644 --- a/dev/ahci.c +++ b/dev/ahci.c @@ -1,12 +1,21 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #include #include +#include #include #include #include #include +static int ahci_block_read(struct block_device *dev, void *buf, size_t offset, size_t sector_count); +static int ahci_block_write(struct block_device *dev, void *buf, size_t offset, size_t sector_count); + +static struct block_device_ops ahci_block_ops = { + .read = ahci_block_read, + .write = ahci_block_write, +}; + static hbamem_t *abar = NULL; static uint8_t sata_device_count = 0; @@ -89,13 +98,18 @@ static void ahci_probe_ports(struct pci_device *dev, hbamem_t *abar) if (pi & 1) { uint32_t type = ahci_port_type(&abar->ports[i]); if (type == AHCI_DEV_SATA) { - printf(LOG_INFO "SATA drive found at port %d\n", i); - sata_devices[sata_device_count].port = i; - sata_devices[sata_device_count].controller = dev; - sata_devices[sata_device_count].abar = abar; + struct sata_device *sdev = &sata_devices[sata_device_count]; + sdev->port = i; + sdev->controller = dev; + sdev->abar = abar; + + char *name = kzalloc(32, ALLOC_KERN); + snprintf(name, "ahci%d", 32, sata_device_count); + block_register(name, &ahci_block_ops, sdev); ahci_rebase(&abar->ports[i], i); sata_device_count++; + printf(LOG_INFO "SATA drive found at port %d\n", i); } else if (type == AHCI_DEV_SATAPI) { printf(LOG_INFO "SATAPI drive found at port %d\n", i); } else if (type == AHCI_DEV_SEMB) { @@ -203,6 +217,38 @@ bool ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, p return true; } +static int ahci_block_read(struct block_device *dev, void *buf, size_t offset, size_t sector_count) +{ + struct sata_device *sdev = (struct sata_device *)dev->data; + + /* repeat every 64K, due to the current limitations of this driver */ + while (sector_count > 0) { + if (!ahci_access_sectors(sdev, offset, MIN(sector_count, 128u), (paddr_t)buf, false)) + return -1; + sector_count -= MIN(sector_count, 128u); + offset += 128; + buf += 128 << 9; + } + + return 0; +} + +static int ahci_block_write(struct block_device *dev, void *buf, size_t offset, size_t sector_count) +{ + struct sata_device *sdev = (struct sata_device *)dev->data; + + /* repeat every 64K, same as above */ + while (sector_count > 0) { + if (!ahci_access_sectors(sdev, offset, MIN(sector_count, 128u), (paddr_t)buf, true)) + return -1; + sector_count -= MIN(sector_count, 128u); + offset += 128; + buf += 128 << 9; + } + + return 0; +} + void ahci_init() { struct pci_device *dev = pci_find_device(0x01, 0x06); diff --git a/fs/block.c b/fs/block.c new file mode 100644 index 0000000..2558e8b --- /dev/null +++ b/fs/block.c @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#include +#include +#include + +#define MAX_BLOCK_DEVICES 24 + +struct block_device *block_devices[MAX_BLOCK_DEVICES]; +size_t block_devices_count = 0; + +struct block_device *block_register(char *name, struct block_device_ops *ops, void *data) +{ + struct block_device *bdev; + + bdev = kmalloc(sizeof(struct block_device), ALLOC_KERN); + if (!bdev) + return NULL; + + bdev->name = name; + bdev->data = data; + memcpy(&bdev->ops, ops, sizeof(struct block_device_ops)); + + block_devices[block_devices_count] = bdev; + block_devices_count++; + +#ifdef KDEBUG + printf(LOG_DEBUG "Registered block device %s\n", name); +#endif + + return bdev; +} + +inline int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size) +{ + return bdev->ops.read(bdev, buf, offset, size); +} + +inline int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size) +{ + return bdev->ops.write(bdev, buf, offset, size); +} diff --git a/include/kernel/block.h b/include/kernel/block.h new file mode 100644 index 0000000..afeaa85 --- /dev/null +++ b/include/kernel/block.h @@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _BLOCK_H_ +#define _BLOCK_H_ + +#include + +struct block_device; + +struct block_device_ops { + int (*read)(struct block_device *bdev, void *buf, size_t offset, size_t size); + int (*write)(struct block_device *bdev, void *buf, size_t offset, size_t size); +}; + +struct block_device { + char *name; + struct block_device_ops ops; + void *data; + size_t size; +}; + +struct block_device *block_register(char *name, struct block_device_ops *ops, void *data); +int block_read(struct block_device *bdev, void *buf, size_t offset, size_t size); +int block_write(struct block_device *bdev, void *buf, size_t offset, size_t size); + +#endif /* _BLOCK_H_ */