fs: begin basic block driver

This just makes the API for accessing drives generic, instead of
relying solely on the corresponding driver code
This commit is contained in:
Michael Shaw 2023-05-09 21:52:35 -04:00
commit 03eb0b0048
3 changed files with 116 additions and 4 deletions

View file

@ -1,12 +1,21 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/mem.h>
#include <kernel/slab.h>
#include <kernel/block.h>
#include <kernel/irq.h>
#include <kernel/pio.h>
#include <dev/ahci.h>
#include <dev/pci.h>
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);