vbos/include/drivers/ahci.h
Michael Shaw 105bdb4464 ahci: add ahci driver
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
2026-07-14 19:03:10 -04:00

408 lines
5.9 KiB
C

/* SPDX-License-Identifier: GPL-2.0-only */
#ifndef _AHCI_H_
#define _AHCI_H_
#include <kernel/common.h>
#include <kernel/sg.h>
/* Frame information structure (FIS) */
#define FIS_REG_H2D 0x27
#define FIS_REG_D2H 0x34
#define FIS_DMA_ACT 0x39
#define FIS_DMA_SETUP 0x41
#define FIS_DATA 0x46
#define FIS_BIST 0x58
#define FIS_PIO_SETUP 0x5F
#define FIS_DEV_BITS 0xA1
#define ATA_DEV_BUSY 0x80
#define ATA_DEV_DRQ 0x08
#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
#define SATA_SIG_SEMB 0xC33C0101
#define SATA_SIG_PM 0x96690101
#define AHCI_DEV_NULL 0
#define AHCI_DEV_SATA 1
#define AHCI_DEV_SEMB 2
#define AHCI_DEV_PM 3
#define AHCI_DEV_SATAPI 4
#define AHCI_GHC_IE 0x2
#define AHCI_GHC_AE 0x80000000
#define HBA_PORT_IPM_ACTIVE 0x1
#define HBA_PORT_DET_PRESENT 0x3
#define HBA_PORT_CMD_ST 0x1
#define HBA_PORT_CMD_FRE 0x10
#define HBA_PORT_CMD_FR 0x4000
#define HBA_PORT_CMD_CR 0x8000
#define HBA_PORT_IS_TFES 0x40000000
struct fis_reg_h2d {
/* 0x00 */
u8 fis_type;
u8 pmult : 4;
u8 reserved : 3;
u8 cmd_mode : 1; /* high: command, low: control */
u8 command;
u8 feature_low;
/* 0x04 */
u8 lba0;
u8 lba1;
u8 lba2;
u8 device;
/* 0x08 */
u8 lba3;
u8 lba4;
u8 lba5;
u8 feature_high;
/* 0x0C */
u8 count_low;
u8 count_high;
u8 icc;
u8 control;
/* 0x10 */
u32 reserved2;
} PACKED;
struct fis_reg_d2h {
/* 0x00 */
u8 fis_type;
u8 pmult : 4;
u8 reserved : 2;
u8 intr : 1;
u8 reserved2 : 1;
u8 status;
u8 error;
/* 0x04 */
u8 lba0;
u8 lba1;
u8 lba2;
u8 device;
/* 0x08 */
u8 lba3;
u8 lba4;
u8 lba5;
u8 feature_high;
/* 0x0C */
u8 count_low;
u8 count_high;
u8 icc;
u8 control;
/* 0x10 */
u32 reserved3;
} PACKED;
struct fis_data {
/* 0x00 */
u8 fis_type;
u8 pmult : 4;
u8 reserved : 4;
u8 reserved2[2];
/* 0x04 */
u32 data[1]; /* Variable length */
} PACKED;
struct fis_dma_setup {
/* 0x00 */
u8 fis_type;
u8 pmult : 4;
u8 reserved : 1;
u8 data_dir : 1;
u8 intr : 1;
u8 auto_act : 1;
u16 reserved2;
/* 0x04 */
u64 dma_buffer_id;
/* 0x0C */
u32 reserved3;
/* 0x10 */
u32 dma_buffer_offset;
/* 0x14 */
u32 transfer_count;
/* 0x18 */
u32 reserved4;
} PACKED;
struct fis_pio_setup {
/* 0x00 */
u8 fis_type;
u8 pmult : 4;
u8 reserved : 1;
u8 data_dir : 1;
u8 intr : 1;
u8 reserved2 : 1;
u8 status;
u8 error;
/* 0x04 */
u8 lba0;
u8 lba1;
u8 lba2;
u8 device;
/* 0x08 */
u8 lba3;
u8 lba4;
u8 lba5;
u8 reserved3;
/* 0x0C */
u8 count_low;
u8 count_high;
u8 reserved4;
u8 e_status;
/* 0x10 */
u16 transfer_count;
u16 reserved5;
} PACKED;
struct fis_set_device_bits {
/* 0x00 */
u8 fis_type;
u8 pmult : 4;
u8 reserved : 2;
u8 interrupt : 1;
u8 n : 1;
u8 statusl : 3;
u8 reserved2 : 1;
u8 statush : 3;
u8 reserved3 : 1;
/* 0x04 */
u8 errorl : 3;
} PACKED;
struct hba_cmd_header {
u8 cmd_fis_len : 5;
u8 atapi : 1;
u8 write : 1;
u8 prefetch : 1;
u8 reset : 1;
u8 bist : 1;
u8 clear_busy : 1;
u8 reserved : 1;
u8 pmp : 4;
u16 prdt_len;
volatile u32 prdb_count;
paddr_t cmd_table_base;
u32 reserved2[4];
} PACKED;
struct hba_prdt_entry {
paddr_t data_base;
u32 reserved;
u32 byte_count : 22;
u32 reserved2 : 9;
u32 interrupt : 1;
} PACKED;
struct hba_cmd_tbl {
u8 cfis[64];
u8 acmd[16];
u8 reserved[48];
struct hba_prdt_entry prdt_entry[1];
} PACKED;
typedef volatile struct {
/* 0x00 */
paddr_t clb;
paddr_t fb;
/* 0x10 */
u32 is;
u32 ie;
u32 cmd;
u32 reserved;
/* 0x20 */
u32 tfd;
u32 sig;
u32 ssts;
u32 sctl;
/* 0x30 */
u32 serr;
u32 sact;
u32 ci;
u32 sntf;
/* 0x40 */
u32 fbs;
u32 reserved1[11];
u32 vendor[4];
} PACKED hbaport_t;
typedef volatile struct {
/* 0x00 */
u32 cap;
u32 ghc;
u32 is;
u32 pi;
u32 vs;
/* 0x14 */
u32 ccc_ctl;
u32 ccc_ports;
u32 em_loc;
u32 em_ctl;
u32 cap2;
/* 0x28 */
u32 bohc;
/* 0x2C */
u8 reserved[0x74];
/* 0xA0 */
u8 vendor[0x60];
/* 0x100 */
hbaport_t ports[32];
} PACKED hbamem_t;
typedef volatile struct {
/* 0x00 */
struct fis_dma_setup dfis;
u32 reserved;
/* 0x20 */
struct fis_pio_setup pfis;
u32 reserved1[3];
/* 0x40 */
struct fis_reg_d2h rfis;
u32 reserved2;
struct fis_set_device_bits sdbfis;
/* 0x60 */
u8 ufis[64];
/* 0xA0 */
u8 reserved3[0x60];
} PACKED hbafis_t;
struct ahci_host {
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 {
char name[16];
struct ahci_host *host;
uint8_t port;
size_t sector_size;
size_t sector_count;
char model[41];
int media_type;
};
#endif /* _AHCI_H_ */