vbos/drivers/ahci.c
Michael Shaw b6c9e0a2ba
kernel: move drivers to own file
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-08-11 21:09:36 -04:00

400 lines
9.4 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
#include <kernel/mem.h>
#include <kernel/slab.h>
#include <kernel/block.h>
#include <kernel/page.h>
#include <kernel/sg.h>
#include <kernel/driver.h>
#include <arch/page.h>
#include <drivers/pci.h>
#include <drivers/ahci.h>
#include <lib/string.h>
#include <lib/stdio.h>
#include <fs/devfs.h>
#include <lib/endian.h>
#define SPIN_MAX 2000
static uint8_t sata_device_count = 0;
static uint8_t cd_device_count = 0;
static struct sata_device sata_devices[512] = {0};
static int ahci_port_type(hbaport_t *port)
{
uint32_t ssts = port->ssts;
uint8_t det = ssts & 0x0F;
if (det != HBA_PORT_DET_PRESENT)
return AHCI_DEV_NULL;
if (port->clb == 0)
return AHCI_DEV_NULL;
switch (port->sig) {
case SATA_SIG_ATAPI:
return AHCI_DEV_SATAPI;
case SATA_SIG_SEMB:
return AHCI_DEV_SEMB;
case SATA_SIG_PM:
return AHCI_DEV_PM;
default:
return AHCI_DEV_SATA;
}
}
static void halt_cmd(hbaport_t *port)
{
port->cmd &= ~HBA_PORT_CMD_ST;
port->cmd &= ~HBA_PORT_CMD_FRE;
while (port->cmd & (HBA_PORT_CMD_FR | HBA_PORT_CMD_CR))
;
}
static void start_cmd(hbaport_t *port)
{
while (port->cmd & HBA_PORT_CMD_CR)
;
port->cmd |= HBA_PORT_CMD_FRE;
port->cmd |= HBA_PORT_CMD_ST;
}
static void ahci_rebase(paddr_t ahci_pbase, vaddr_t ahci_vbase, hbaport_t *port, int n)
{
halt_cmd(port);
port->clb = ahci_pbase + (n << 10);
memset((void *)(TO_VIRT(port->clb)), 0, 1024);
port->fb = ahci_pbase + (32 << 10) + (n << 8);
memset((void *)(TO_VIRT(port->clb)), 0, 256);
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)TO_VIRT(port->clb);
for (int i = 0; i < 32; i++) {
cmdheader[i].prdt_len = 8;
cmdheader[i].cmd_table_base = ahci_pbase + (n << 13) + (40 << 10) + (i << 8);
void *ctba = (void *)TO_VIRT((paddr_t)cmdheader[i].cmd_table_base);
memset(ctba, 0, 256);
}
start_cmd(port);
}
static int ahci_slot_alloc(hbaport_t *port)
{
uint32_t slots = (port->sact | port->ci);
for (int i = 0; i < 32; i++) {
if ((slots & 1))
slots >>= 1;
else
return i;
}
return -1;
}
static void
ahci_build_fis_h2d(struct hba_cmd_header *ch, struct hba_cmd_tbl *ct, paddr_t lba, uint16_t count, uint8_t cmd, bool write)
{
ch->cmd_fis_len = sizeof(struct fis_reg_h2d) / sizeof(uint32_t);
ch->write = write;
struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&ct->cfis);
cmdfis->fis_type = FIS_REG_H2D;
cmdfis->cmd_mode = 1;
cmdfis->command = cmd;
cmdfis->lba0 = (uint8_t)lba;
cmdfis->lba1 = (uint8_t)(lba >> 8);
cmdfis->lba2 = (uint8_t)(lba >> 16);
cmdfis->device = 1 << 6;
cmdfis->lba3 = (uint8_t)(lba >> 24);
cmdfis->lba4 = (uint8_t)(lba >> 32);
cmdfis->lba5 = (uint8_t)(lba >> 40);
cmdfis->count_low = count & 0xff;
cmdfis->count_high = (count >> 8) & 0xff;
}
static int ahci_setup_prdt(struct hba_cmd_tbl *ct, struct sg_list *sg)
{
if (sg->count > 8)
return -EINVAL;
for (size_t i = 0; i < sg->count; i++) {
if (sg->entries[i].len > 4 * MB)
return -EINVAL;
ct->prdt_entry[i].data_base = sg->entries[i].paddr;
ct->prdt_entry[i].byte_count = sg->entries[i].len - 1;
ct->prdt_entry[i].interrupt = (i == sg->count - 1) ? 1 : 0;
}
return sg->count;
}
static int ahci_issue_wait(hbaport_t *port, int slot)
{
int spin = 0;
while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < SPIN_MAX)
spin++;
if (spin >= SPIN_MAX) {
kprintf(LOG_ERROR "AHCI: Port is hung\n");
return -EIO;
}
port->ci = 1 << slot;
while (1) {
if (port->is & HBA_PORT_IS_TFES) {
kprintf(LOG_ERROR "AHCI: Read disk error\n");
port->is = (uint32_t)-1;
return -EIO;
}
if ((port->ci & (1 << slot)) == 0)
break;
}
if (port->is & HBA_PORT_IS_TFES) {
kprintf(LOG_ERROR "AHCI: Read disk error\n");
port->is = (uint32_t)-1;
return -EIO;
}
return 0;
}
static int ahci_rw_sg(struct sata_device *dev, paddr_t lba, uint16_t count, struct sg_list *sg, bool write)
{
hbaport_t *port = &dev->host->abar->ports[dev->port];
port->is = (uint32_t)-1;
int slot = ahci_slot_alloc(port);
if (slot < 0) {
kprintf(LOG_ERROR "AHCI: No free command slot\n");
return -EAGAIN;
}
struct hba_cmd_header *ch = (struct hba_cmd_header *)(port->clb | hhdm_start) + slot;
struct hba_cmd_tbl *ct = (struct hba_cmd_tbl *)(ch->cmd_table_base | hhdm_start);
memset(ct, 0, sizeof(struct hba_cmd_tbl) + (sg->count - 1) * sizeof(struct hba_prdt_entry));
uint8_t cmd = write ? ATA_CMD_WRITE_DMA_EX : ATA_CMD_READ_DMA_EX;
ahci_build_fis_h2d(ch, ct, lba, count, cmd, write);
int prdt_entries = ahci_setup_prdt(ct, sg);
if (prdt_entries < 0)
return prdt_entries;
ch->prdt_len = prdt_entries;
return ahci_issue_wait(port, slot);
}
static int ahci_identify(hbaport_t *port, struct ahci_identify *ident, bool packet)
{
void *buf = kalloc(4096, ALLOC_KERN);
if (!buf)
return -ENOMEM;
memset(ident, 0, sizeof(*ident));
port->is = (uint32_t)-1;
int slot = ahci_slot_alloc(port);
if (slot < 0) {
kprintf(LOG_ERROR "AHCI: No free command slot for identify\n");
kfree(buf);
return -EAGAIN;
}
struct hba_cmd_header *ch = (struct hba_cmd_header *)(port->clb | hhdm_start) + slot;
struct hba_cmd_tbl *ct = (struct hba_cmd_tbl *)(ch->cmd_table_base | hhdm_start);
memset(ct, 0, sizeof(struct hba_cmd_tbl));
memset(buf, 0, 4096);
uint8_t cmd = packet ? ATA_CMD_IDENTIFY_PACKET : ATA_CMD_IDENT;
ahci_build_fis_h2d(ch, ct, 0, 0, cmd, 0);
SG_STATIC(sg, 1);
sg_append(&sg, TO_PHYS(buf), 512);
ch->prdt_len = ahci_setup_prdt(ct, &sg);
int ret = ahci_issue_wait(port, slot);
if (ret < 0) {
kfree(buf);
return ret;
}
memcpy(ident, buf, sizeof(struct ahci_identify));
uint16_t *p = (uint16_t *)ident->serial_number;
for (int i = 0; i < 10; i++)
p[i] = betoh16(p[i]);
p = (uint16_t *)ident->firmware_revision;
for (int i = 0; i < 4; i++)
p[i] = betoh16(p[i]);
p = (uint16_t *)ident->model_number;
for (int i = 0; i < 20; i++)
p[i] = betoh16(p[i]);
ident->total_lba28_sectors = letoh32(ident->total_lba28_sectors);
ident->total_lba48_sectors = letoh64(ident->total_lba48_sectors);
ident->capabilities = letoh32(ident->capabilities);
ident->current_capacity_sectors = letoh32(ident->current_capacity_sectors);
ident->physical_logical_sectors = letoh64(ident->physical_logical_sectors);
kfree(buf);
return 0;
}
static ssize_t ahci_block_read(struct block_device *bdev, struct sg_list *sg, size_t off, size_t n)
{
return ahci_rw_sg(bdev->priv, off, n, sg, false);
}
static ssize_t ahci_block_write(struct block_device *bdev, struct sg_list *sg, size_t off, size_t n)
{
return ahci_rw_sg(bdev->priv, off, n, sg, true);
}
static struct sata_device *init_sata_port(struct ahci_host *host, u32 port, int type)
{
static struct block_ops ahci_block_ops = {
.read = ahci_block_read,
.write = ahci_block_write,
};
struct sata_device *sdev = &sata_devices[sata_device_count];
sdev->port = port;
sdev->host = host;
bool packet = (type == AHCI_DEV_SATAPI);
hbamem_t *abar = host->abar;
char *name = sdev->name;
name[sizeof(sdev->name) - 1] = 0;
if (packet)
snprintf(name, sizeof(sdev->name) - 1, "cd%d", cd_device_count);
else
snprintf(name, sizeof(sdev->name) - 1, "ahci%d", sata_device_count);
vaddr_t ahci_vbase = (vaddr_t)kalloc(512 * KB, ALLOC_KERN);
ahci_rebase(TO_PHYS(ahci_vbase), ahci_vbase, &abar->ports[port], port);
struct ahci_identify ident;
int ret = ahci_identify(&abar->ports[port], &ident, packet);
if (ret < 0) {
kerr("AHCI: identify failed on port %d\n", port);
return NULL;
}
sdev->sector_count = ident.total_lba48_sectors;
if (sdev->sector_count == 0)
sdev->sector_count = ident.total_lba28_sectors;
uint16_t w106 = ident.physical_logical_sectors & 0xFFFF;
sdev->sector_size = 512;
if (w106 & (1 << 13)) {
size_t s = (ident.reserved5[7] | ((uint32_t)ident.reserved5[8] << 16)) * 2;
if (s >= 512)
sdev->sector_size = s;
} else if (w106 & (1 << 12)) {
sdev->sector_size = 1024;
}
memcpy(sdev->model, ident.model_number, 40);
sdev->model[40] = '\0';
uint16_t rate = letoh16(ident.reserved6[91]);
if (rate == 0x0001)
sdev->media_type = 2;
else if (rate >= 5400)
sdev->media_type = 1;
else
sdev->media_type = 0;
if (packet)
cd_device_count++;
else
sata_device_count++;
struct block_device *bdev;
block_device_register(sdev->name, 0, &ahci_block_ops, &bdev);
bdev->priv = sdev;
bdev->size = sdev->sector_count;
bdev->block_size = sdev->sector_size;
bdev->disk.is_disk = true;
strncpy(bdev->name, name, sizeof(bdev->name) - 1);
bdev->sg_max_len = 4 * MB;
bdev->sg_max_segments = 8;
if (!packet)
block_check_partitions(bdev);
devfs_insert_device_blk(bdev->name, bdev, NULL);
return sdev;
}
static void ahci_probe_ports(struct ahci_host *host)
{
hbamem_t *abar = host->abar;
uint32_t pi = abar->pi;
for (int i = 0; i < 32; i++) {
if (pi & 1) {
uint32_t type = ahci_port_type(&abar->ports[i]);
if (type == AHCI_DEV_SATA || type == AHCI_DEV_SATAPI) {
init_sata_port(host, i, type);
} else if (type == AHCI_DEV_SEMB) {
kprintf(LOG_INFO "SEMB drive found at port %d\n", i);
} else if (type == AHCI_DEV_PM) {
kprintf(LOG_INFO "PM drive found at port %d\n", i);
}
}
pi >>= 1;
}
}
static int ahci_init_device(struct pci_device *device, size_t ahci_dev_no)
{
struct ahci_host *host = kzalloc(sizeof(struct ahci_host), ALLOC_KERN);
if (!host)
return -ENOMEM;
host->abar = map_mmio_dev(device->bar5, PAGE_SIZE);
host->device = device;
ahci_probe_ports(host);
return 0;
}
void ahci_init()
{
list_t dev_list;
list_create(&dev_list);
krdy("AHCI ready\n");
}
DRIVER_ENTRY_PRIORITY(ahci_init, 3);