ahci: add ahci driver

Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
Michael Shaw 2026-07-14 19:03:10 -04:00
commit 105bdb4464
11 changed files with 522 additions and 260 deletions

1
.gitignore vendored
View file

@ -11,5 +11,4 @@ edk2-ovmf/
.config
*.hdd
*.cpio
testprogram.c
initramfs/

View file

@ -2,18 +2,26 @@
#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 <arch/page.h>
#include <drivers/pci.h>
#include <drivers/ahci.h>
#include <kernel/block.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 struct sata_device sata_devices[32] = {0};
static uint8_t cd_device_count = 0;
static struct sata_device sata_devices[512] = {0};
static paddr_t ahci_pbase = 0;
static vaddr_t ahci_vbase = 0;
@ -59,70 +67,29 @@ static void start_cmd(hbaport_t *port)
port->cmd |= HBA_PORT_CMD_ST;
}
static void ahci_rebase(hbaport_t *port, int n)
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 *)(port->clb | hhdm_start), 0, 1024);
port->clb = ahci_pbase + (n << 10);
memset((void *)(TO_VIRT(port->clb)), 0, 1024);
port->fb = ahci_pbase + (32 << 10) + (n << 8);
memset((void *)(port->fb | hhdm_start), 0, 256);
memset((void *)(TO_VIRT(port->clb)), 0, 256);
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
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);
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));
void *ctba = (void *)TO_VIRT((paddr_t)cmdheader[i].cmd_table_base);
memset(ctba, 0, 256);
}
start_cmd(port);
}
static void ahci_probe_ports(struct ahci_host *host)
{
struct pci_device *dev = host->device;
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) {
struct sata_device *sdev = &sata_devices[sata_device_count];
sdev->port = i;
sdev->host = host;
char *name = kzalloc(16, ALLOC_KERN);
name[15] = 0;
snprintf(name, 15, "ahci%d", sata_device_count);
size_t sector_count = ((size_t)abar->ports[i].ssts) << 32 | abar->ports[i].ssts;
sdev->sector_count = sector_count;
sdev->sector_size = 512;
/* bdev = block_register(name, &ahci_block_ops, sdev, sector_count, sdev->sector_size);
block_gpt_init(bdev); */
ahci_rebase(&abar->ports[i], i);
sata_device_count++;
kprintf(LOG_INFO "SATA drive found at port %d\n", i);
} else if (type == AHCI_DEV_SATAPI) {
kprintf(LOG_INFO "SATAPI drive found at port %d\n", i);
} 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 cmdslot(hbaport_t *port)
static int ahci_slot_alloc(hbaport_t *port)
{
uint32_t slots = (port->sact | port->ci);
for (int i = 0; i < 32; i++) {
@ -135,89 +102,285 @@ static int cmdslot(hbaport_t *port)
return -1;
}
int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write)
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)
{
hbaport_t *port = &dev->host->abar->ports[dev->port];
ch->cmd_fis_len = sizeof(struct fis_reg_h2d) / sizeof(uint32_t);
ch->write = write;
/* clear pending interrupt */
port->is = (uint32_t)-1;
int spin = 0;
int slot = cmdslot(port);
if (slot == -1)
return false;
struct hba_cmd_header *cmdheader = (struct hba_cmd_header *)(port->clb | hhdm_start);
cmdheader += slot;
cmdheader->cmd_fis_len = sizeof(struct fis_reg_h2d) / sizeof(uint32_t);
cmdheader->write = 0;
cmdheader->prdt_len = (uint16_t)((count - 1) >> 4) + 1;
struct hba_cmd_tbl *cmdtbl = (struct hba_cmd_tbl *)(cmdheader->cmd_table_base | hhdm_start);
memset(cmdtbl, 0, sizeof(struct hba_cmd_tbl) + (cmdheader->prdt_len - 1) * sizeof(struct hba_prdt_entry));
struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&cmdtbl->cfis);
struct fis_reg_h2d *cmdfis = (struct fis_reg_h2d *)(&ct->cfis);
cmdfis->fis_type = FIS_REG_H2D;
cmdfis->cmd_mode = 1;
cmdfis->command = write ? ATA_CMD_WRITE_DMA_EX : ATA_CMD_READ_DMA_EX;
cmdfis->command = cmd;
cmdfis->lba0 = (uint8_t)lba;
cmdfis->lba1 = (uint8_t)(lba >> 8);
cmdfis->lba2 = (uint8_t)(lba >> 16);
cmdfis->device = 1 << 6; /* LBA mode */
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;
}
int i;
for (i = 0; i < cmdheader->prdt_len; i++) {
cmdtbl->prdt_entry[i].data_base = buf;
cmdtbl->prdt_entry[i].byte_count = 8 * 1024 - 1;
cmdtbl->prdt_entry[i].interrupt = 1;
buf += 8 * 1024;
count -= 16;
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;
}
/* wait until port is free */
while ((port->tfd & (ATA_DEV_BUSY | ATA_DEV_DRQ)) && spin < 1000000) {
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 == 1000000) {
if (spin >= SPIN_MAX) {
kprintf(LOG_ERROR "AHCI: Port is hung\n");
return false;
return -EIO;
}
/* issue command */
port->ci = 1 << slot;
/* wait for completion */
while (1) {
if (port->is & HBA_PORT_IS_TFES) {
kprintf(LOG_ERROR "AHCI: Read disk error\n");
return false;
port->is = (uint32_t)-1;
return -EIO;
}
if ((port->ci & (1 << slot)) == 0)
break;
/* ELSE IF: reschedule */
}
/* check again */
if (port->is & HBA_PORT_IS_TFES) {
kprintf(LOG_ERROR "AHCI: Read disk error\n");
return false;
port->is = (uint32_t)-1;
return -EIO;
}
return true;
return 0;
}
static ssize_t ahci_block_read(struct block_device *dev, char *buf, size_t off, size_t n_blocks)
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_rw(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write)
{
SG_STATIC(sg, 1);
sg_append(&sg, buf, count * dev->sector_size);
return ahci_rw_sg(dev, lba, count, &sg, write);
}
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);
}
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) {
kprintf(LOG_ERROR "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++;
const char *type_str = packet ? "ATAPI" : (sdev->media_type == 2 ? "SSD" : "HDD");
kprintf(LOG_INFO "/dev/%s: %s at port %d: %zd sectors, %zdb, model: %s\n", sdev->name, type_str, port, sdev->sector_count,
sdev->sector_size, sdev->model);
struct block_device *bdev = kzalloc(sizeof(struct block_device), ALLOC_KERN);
if (!bdev)
return sdev;
bdev->priv = sdev;
bdev->size = sdev->sector_count;
bdev->block_size = sdev->sector_size;
bdev->ops = ahci_block_ops;
strncpy(bdev->name, name, sizeof(bdev->name) - 1);
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)
@ -226,6 +389,17 @@ static int ahci_init_device(struct pci_device *device, size_t ahci_dev_no)
if (!bdev)
return -ENOMEM;
struct ahci_host *host = kzalloc(sizeof(struct ahci_host), ALLOC_KERN);
if (!host) {
kfree(bdev);
return -ENOMEM;
}
host->abar = map_mmio_dev(device->bar5, PAGE_SIZE);
host->device = device;
ahci_probe_ports(host);
return 0;
}
@ -234,7 +408,7 @@ void ahci_init()
list_t dev_list;
list_create(&dev_list);
size_t n = pci_find_device(0x0001, 0x0006, &dev_list);
pci_find_device(1, 6, &dev_list);
list_item_t *item = dev_list.head;
size_t i = 0;

View file

@ -3,6 +3,7 @@
#define _AHCI_H_
#include <kernel/common.h>
#include <kernel/sg.h>
/* Frame information structure (FIS) */
#define FIS_REG_H2D 0x27
@ -20,6 +21,7 @@
#define ATA_CMD_READ_DMA_EX 0x25
#define ATA_CMD_WRITE_DMA_EX 0x35
#define ATA_CMD_IDENT 0xEC
#define ATA_CMD_IDENTIFY_PACKET 0xA1
#define SATA_SIG_ATA 0x00000101
#define SATA_SIG_ATAPI 0xEB140101
@ -46,203 +48,203 @@
struct fis_reg_h2d {
/* 0x00 */
uint8_t fis_type;
u8 fis_type;
uint8_t pmult : 4;
uint8_t reserved : 3;
uint8_t cmd_mode : 1; /* high: command, low: control */
u8 pmult : 4;
u8 reserved : 3;
u8 cmd_mode : 1; /* high: command, low: control */
uint8_t command;
uint8_t feature_low;
u8 command;
u8 feature_low;
/* 0x04 */
uint8_t lba0;
uint8_t lba1;
uint8_t lba2;
uint8_t device;
u8 lba0;
u8 lba1;
u8 lba2;
u8 device;
/* 0x08 */
uint8_t lba3;
uint8_t lba4;
uint8_t lba5;
uint8_t feature_high;
u8 lba3;
u8 lba4;
u8 lba5;
u8 feature_high;
/* 0x0C */
uint8_t count_low;
uint8_t count_high;
uint8_t icc;
uint8_t control;
u8 count_low;
u8 count_high;
u8 icc;
u8 control;
/* 0x10 */
uint32_t reserved2;
u32 reserved2;
} PACKED;
struct fis_reg_d2h {
/* 0x00 */
uint8_t fis_type;
u8 fis_type;
uint8_t pmult : 4;
uint8_t reserved : 2;
uint8_t intr : 1;
uint8_t reserved2 : 1;
u8 pmult : 4;
u8 reserved : 2;
u8 intr : 1;
u8 reserved2 : 1;
uint8_t status;
uint8_t error;
u8 status;
u8 error;
/* 0x04 */
uint8_t lba0;
uint8_t lba1;
uint8_t lba2;
uint8_t device;
u8 lba0;
u8 lba1;
u8 lba2;
u8 device;
/* 0x08 */
uint8_t lba3;
uint8_t lba4;
uint8_t lba5;
uint8_t feature_high;
u8 lba3;
u8 lba4;
u8 lba5;
u8 feature_high;
/* 0x0C */
uint8_t count_low;
uint8_t count_high;
uint8_t icc;
uint8_t control;
u8 count_low;
u8 count_high;
u8 icc;
u8 control;
/* 0x10 */
uint32_t reserved3;
u32 reserved3;
} PACKED;
struct fis_data {
/* 0x00 */
uint8_t fis_type;
u8 fis_type;
uint8_t pmult : 4;
uint8_t reserved : 4;
u8 pmult : 4;
u8 reserved : 4;
uint8_t reserved2[2];
u8 reserved2[2];
/* 0x04 */
uint32_t data[1]; /* Variable length */
u32 data[1]; /* Variable length */
} PACKED;
struct fis_dma_setup {
/* 0x00 */
uint8_t fis_type;
u8 fis_type;
uint8_t pmult : 4;
uint8_t reserved : 1;
uint8_t data_dir : 1;
uint8_t intr : 1;
uint8_t auto_act : 1;
u8 pmult : 4;
u8 reserved : 1;
u8 data_dir : 1;
u8 intr : 1;
u8 auto_act : 1;
uint16_t reserved2;
u16 reserved2;
/* 0x04 */
uint64_t dma_buffer_id;
u64 dma_buffer_id;
/* 0x0C */
uint32_t reserved3;
u32 reserved3;
/* 0x10 */
uint32_t dma_buffer_offset;
u32 dma_buffer_offset;
/* 0x14 */
uint32_t transfer_count;
u32 transfer_count;
/* 0x18 */
uint32_t reserved4;
u32 reserved4;
} PACKED;
struct fis_pio_setup {
/* 0x00 */
uint8_t fis_type;
u8 fis_type;
uint8_t pmult : 4;
uint8_t reserved : 1;
uint8_t data_dir : 1;
uint8_t intr : 1;
uint8_t reserved2 : 1;
u8 pmult : 4;
u8 reserved : 1;
u8 data_dir : 1;
u8 intr : 1;
u8 reserved2 : 1;
uint8_t status;
uint8_t error;
u8 status;
u8 error;
/* 0x04 */
uint8_t lba0;
uint8_t lba1;
uint8_t lba2;
uint8_t device;
u8 lba0;
u8 lba1;
u8 lba2;
u8 device;
/* 0x08 */
uint8_t lba3;
uint8_t lba4;
uint8_t lba5;
uint8_t reserved3;
u8 lba3;
u8 lba4;
u8 lba5;
u8 reserved3;
/* 0x0C */
uint8_t count_low;
uint8_t count_high;
uint8_t reserved4;
uint8_t e_status;
u8 count_low;
u8 count_high;
u8 reserved4;
u8 e_status;
/* 0x10 */
uint16_t transfer_count;
uint16_t reserved5;
u16 transfer_count;
u16 reserved5;
} PACKED;
struct fis_set_device_bits {
/* 0x00 */
uint8_t fis_type;
u8 fis_type;
uint8_t pmult : 4;
uint8_t reserved : 2;
uint8_t interrupt : 1;
uint8_t n : 1;
u8 pmult : 4;
u8 reserved : 2;
u8 interrupt : 1;
u8 n : 1;
uint8_t statusl : 3;
uint8_t reserved2 : 1;
uint8_t statush : 3;
uint8_t reserved3 : 1;
u8 statusl : 3;
u8 reserved2 : 1;
u8 statush : 3;
u8 reserved3 : 1;
/* 0x04 */
uint8_t errorl : 3;
u8 errorl : 3;
} PACKED;
struct hba_cmd_header {
uint8_t cmd_fis_len : 5;
uint8_t atapi : 1;
uint8_t write : 1;
uint8_t prefetch : 1;
u8 cmd_fis_len : 5;
u8 atapi : 1;
u8 write : 1;
u8 prefetch : 1;
uint8_t reset : 1;
uint8_t bist : 1;
uint8_t clear_busy : 1;
uint8_t reserved : 1;
uint8_t pmp : 4;
u8 reset : 1;
u8 bist : 1;
u8 clear_busy : 1;
u8 reserved : 1;
u8 pmp : 4;
uint16_t prdt_len;
u16 prdt_len;
volatile uint32_t prdb_count;
volatile u32 prdb_count;
paddr_t cmd_table_base;
uint32_t reserved2[4];
u32 reserved2[4];
} PACKED;
struct hba_prdt_entry {
paddr_t data_base;
uint32_t reserved;
u32 reserved;
uint32_t byte_count : 22;
uint32_t reserved2 : 9;
uint32_t interrupt : 1;
u32 byte_count : 22;
u32 reserved2 : 9;
u32 interrupt : 1;
} PACKED;
struct hba_cmd_tbl {
uint8_t cfis[64];
u8 cfis[64];
uint8_t acmd[16];
u8 acmd[16];
uint8_t reserved[48];
u8 reserved[48];
struct hba_prdt_entry prdt_entry[1];
} PACKED;
@ -254,53 +256,53 @@ typedef volatile struct {
paddr_t fb;
/* 0x10 */
uint32_t is;
uint32_t ie;
uint32_t cmd;
uint32_t reserved;
u32 is;
u32 ie;
u32 cmd;
u32 reserved;
/* 0x20 */
uint32_t tfd;
uint32_t sig;
uint32_t ssts;
uint32_t sctl;
u32 tfd;
u32 sig;
u32 ssts;
u32 sctl;
/* 0x30 */
uint32_t serr;
uint32_t sact;
uint32_t ci;
uint32_t sntf;
u32 serr;
u32 sact;
u32 ci;
u32 sntf;
/* 0x40 */
uint32_t fbs;
u32 fbs;
uint32_t reserved1[11];
uint32_t vendor[4];
u32 reserved1[11];
u32 vendor[4];
} PACKED hbaport_t;
typedef volatile struct {
/* 0x00 */
uint32_t cap;
uint32_t ghc;
uint32_t is;
uint32_t pi;
uint32_t vs;
u32 cap;
u32 ghc;
u32 is;
u32 pi;
u32 vs;
/* 0x14 */
uint32_t ccc_ctl;
uint32_t ccc_ports;
uint32_t em_loc;
uint32_t em_ctl;
uint32_t cap2;
u32 ccc_ctl;
u32 ccc_ports;
u32 em_loc;
u32 em_ctl;
u32 cap2;
/* 0x28 */
uint32_t bohc;
u32 bohc;
/* 0x2C */
uint8_t reserved[0x74];
u8 reserved[0x74];
/* 0xA0 */
uint8_t vendor[0x60];
u8 vendor[0x60];
/* 0x100 */
hbaport_t ports[32];
@ -309,38 +311,98 @@ typedef volatile struct {
typedef volatile struct {
/* 0x00 */
struct fis_dma_setup dfis;
uint32_t reserved;
u32 reserved;
/* 0x20 */
struct fis_pio_setup pfis;
uint32_t reserved1[3];
u32 reserved1[3];
/* 0x40 */
struct fis_reg_d2h rfis;
uint32_t reserved2;
u32 reserved2;
struct fis_set_device_bits sdbfis;
/* 0x60 */
uint8_t ufis[64];
u8 ufis[64];
/* 0xA0 */
uint8_t reserved3[0x60];
u8 reserved3[0x60];
} PACKED hbafis_t;
struct ahci_host {
struct pci_device *device;
struct pci_device *device;
hbamem_t *abar;
};
struct ahci_identify {
u16 config_flags;
u16 cylinders_obsolete;
u16 specific_config;
u16 heads_obsolete;
u16 reserved1[2];
u16 sectors_obsolete;
u16 reserved_for_compact[3];
u8 serial_number[20];
u16 buffer_type_obsolete;
u16 buffer_size_obsolete;
u16 ecc_bytes_obsolete;
u8 firmware_revision[8];
u8 model_number[40];
u16 max_multiple_sectors;
u16 trusted_computing;
u32 capabilities;
u16 obsolescent_word51;
u16 obsolescent_word52;
u16 field_validity;
u16 current_cylinders;
u16 current_heads;
u16 current_sectors;
u32 current_capacity_sectors;
u16 multiple_sector_setting;
u32 total_lba28_sectors;
u16 double_word_dma_obsolete;
u16 multiword_dma_settings;
u16 advanced_pio_modes;
u16 min_mwdma_cycle_time;
u16 rec_mwdma_cycle_time;
u16 min_pio_cycle_no_flow;
u16 min_pio_cycle_flow;
u16 reserved2[2];
u16 reserved_packet_dev[4];
u16 queue_depth;
u16 serial_ata_capabilities;
u16 reserved_sata;
u16 serial_ata_features;
u16 serial_ata_features_en;
u16 major_version;
u16 minor_version;
u16 cmd_set_supported1;
u16 cmd_set_supported2;
u16 cmd_set_supported3;
u16 cmd_set_enabled1;
u16 cmd_set_enabled2;
u16 cmd_set_enabled3;
u16 ultra_dma_modes;
u16 reserved3[11];
u64 total_lba48_sectors;
u16 streaming_transfer_time;
u16 reserved4;
u64 physical_logical_sectors;
u16 reserved5[15];
u16 data_structure_checksum;
u16 reserved6[130];
} PACKED;
struct sata_device {
struct ahci_host *host;
char name[16];
struct ahci_host *host;
uint8_t port;
size_t sector_size;
size_t sector_count;
char model[41];
int media_type;
};
int ahci_access_sectors(struct sata_device *dev, paddr_t lba, uint16_t count, paddr_t buf, bool write);
#endif /* _AHCI_H_ */

View file

@ -2,13 +2,21 @@
#ifndef _BLOCK_H_
#define _BLOCK_H_
#include <fs/vfs.h>
#include <kernel/device.h>
#include <kernel/sg.h>
#include <fs/vfs.h>
struct block_device;
struct block_ops {
ssize_t (*read)(struct block_device *dev, char *buf, size_t offset, size_t n);
ssize_t (*write)(struct block_device *dev, char *buf, size_t offset, size_t n);
ssize_t (*read)(struct block_device *dev, struct sg_list *sg, size_t offset, size_t n);
ssize_t (*write)(struct block_device *dev, struct sg_list *sg, size_t offset, size_t n);
};
struct disk {
u8 is_disk;
char model[FILE_NAME_MAX_LEN + 1];
list_t part_table;
};
struct block_device {
@ -17,14 +25,9 @@ struct block_device {
size_t size;
size_t block_size;
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;
void *priv;
size_t lba_start;
struct disk disk;
};
ssize_t block_read(struct block_device *dev, char *buf, size_t offset, size_t n_blocks);

28
include/kernel/sg.h Normal file
View file

@ -0,0 +1,28 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _SG_H_
#define _SG_H_
#include <kernel/types.h>
struct sg_entry {
paddr_t paddr;
size_t len;
};
struct sg_list {
struct sg_entry *entries;
size_t count;
size_t max_count;
};
#define SG_STATIC(name, n) \
struct sg_entry name##_buf[n]; \
struct sg_list name = {.entries = name##_buf, .count = 0, .max_count = (n)}
#define sg_for_each(entry, sg) for (struct sg_entry *entry = (sg)->entries; entry < (sg)->entries + (sg)->count; entry++)
void sg_init(struct sg_list *sg, struct sg_entry *buf, size_t n);
int sg_append(struct sg_list *sg, paddr_t paddr, size_t len);
void sg_reset(struct sg_list *sg);
#endif /* _SG_H_ */

View file

@ -14,7 +14,8 @@
#define ENOEXEC 9
#define EFAULT 10
#define EAGAIN 11
#define EBUSY 12
#define EIO 12
#define EBUSY 13
#define ENOSYS 13
#define ESYSCALLBLK 14
#define ENAMETOOLONG 15

View file

@ -1,4 +1,3 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += main.o
obj-y += requests.o
obj-y += testprogram.o

View file

@ -1,8 +0,0 @@
/* SPDX-License-Identifier: GPL-2.0-only */
#include <kernel/common.h>
void z_test_program_entry()
{
}
DRIVER_ENTRY(z_test_program_entry);

View file

@ -1,4 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-y += device.o
obj-y += block.o
obj-y += sg.o
obj-y += tty/

View file

@ -15,6 +15,7 @@ static const char *errno_str[] = {
[ENOEXEC] = "Exec format error",
[EFAULT] = "Bad address",
[EAGAIN] = "Try again",
[EIO] = "Input/output error",
[EBUSY] = "Device or resource busy",
[ENOSYS] = "Function not implemented",
[ENAMETOOLONG] = "Name too long",

View file

@ -47,6 +47,7 @@ run: edk2-ovmf $(INITRAMFS) $(IMG)
-accel kvm \
-nographic \
-hda $(IMG) \
-hdb test_drive.hdd \
$(QEMUFLAGS)
.PHONY += debug
@ -57,6 +58,7 @@ debug: edk2-ovmf $(IMG)
-drive if=pflash,unit=0,format=raw,file=edk2-ovmf/ovmf-code-$(ARCH).fd,readonly=on \
-nographic \
-hda $(IMG) \
-hdb test_drive.hdd \
$(QEMUFLAGS)
edk2-ovmf: