The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
ahci.c File Reference
#include "ahci.h"
#include "common/memory.h"
#include "drivers/pci/pci.h"
#include "drivers/pci/pci_irq.h"
#include "kernel/klog/klog.h"
#include "kernel/memory/mmio.h"
#include "kernel/memory/page.h"
#include "kernel/memory/pmm.h"

Macros

#define HBA_CAP   0x00
 the AHCI driver (Serial ATA AHCI 1.3.1 specification).
#define HBA_GHC   0x04
#define HBA_IS   0x08
#define HBA_PI   0x0C
#define CAP_S64A   (1u << 31)
#define GHC_AE   (1u << 31)
#define GHC_IE   (1u << 1)
#define PORT_BASE   0x100
#define PORT_SIZE   0x80
#define PX_CLB   0x00
#define PX_CLBU   0x04
#define PX_FB   0x08
#define PX_FBU   0x0C
#define PX_IS   0x10
#define PX_IE   0x14
#define PX_CMD   0x18
#define PX_TFD   0x20
#define PX_SIG   0x24
#define PX_SSTS   0x28
#define PX_SERR   0x30
#define PX_CI   0x38
#define CMD_ST   (1u << 0)
#define CMD_FRE   (1u << 4)
#define CMD_FR   (1u << 14)
#define CMD_CR   (1u << 15)
#define TFD_ERR   (1u << 0)
#define TFD_DRQ   (1u << 3)
#define TFD_BSY   (1u << 7)
#define IS_TFES   (1u << 30)
#define IE_DHRS   (1u << 0)
#define IE_PSS   (1u << 1)
#define IE_DSS   (1u << 2)
#define IE_SDBS   (1u << 3)
#define IE_ERRORS   0xFDC00050u
#define PORT_IE   (IE_DHRS | IE_PSS | IE_DSS | IE_SDBS | IE_ERRORS)
#define SIG_SATA_DISK   0x00000101u
#define SSTS_DET_PRESENT   3u
#define SSTS_IPM_ACTIVE   1u
#define ATA_READ_DMA_EXT   0x25
#define ATA_WRITE_DMA_EXT   0x35
#define ATA_FLUSH_CACHE_EXT   0xEA
#define ATA_IDENTIFY   0xEC
#define FIS_TYPE_H2D   0x27
#define OFF_CMD_LIST   0x000
#define OFF_FIS   0x400
#define OFF_CMD_TABLE   0x500
#define OFF_IDENTIFY   0x600
#define SECTOR_SIZE   512u
#define BOUNCE_SECTORS   (PAGE_SIZE / SECTOR_SIZE)
#define SPIN_LIMIT   100000000u
#define COMMAND_TIMEOUT_MS   5000u

Functions

uint32_t ahci_init (void)
 find the AHCI controller (the first one) and its disks.
ahci_disk_t * ahci_disk (uint32_t index)
 a disk by index (0 to the number ahci_init returned).

Macro Definition Documentation

◆ HBA_CAP

#define HBA_CAP   0x00

the AHCI driver (Serial ATA AHCI 1.3.1 specification).

the HBA registers (ABAR) have a generic part and one 128-byte register set per port from offset 0x100. a port reads its commands from a command list (32 headers of 32 bytes); a header points to a command table with the command FIS and the PRDT, the list of memory areas for the data. the HBA writes received FISes into the FIS area.

the driver uses one command slot per port and one page of RAM per port: 0x000 command list (1 KiB aligned; only header 0 is used) 0x400 received FIS area (256 bytes, 256-byte aligned) 0x500 command table (128-byte aligned): the FIS, then one PRDT entry 0x600 the IDENTIFY buffer and the start of the bounce buffer the data goes through the bounce buffer, a second page, because a caller's buffer can be on a kernel stack, which is not physically contiguous.

locking: one transfer at a time per disk (a mutex). the waiting process sleeps until the port interrupt (MSI, or the shared INTx line); without an interrupt it polls (see kernel/sync/completion.h).

◆ HBA_GHC

#define HBA_GHC   0x04

◆ HBA_IS

#define HBA_IS   0x08

◆ HBA_PI

#define HBA_PI   0x0C

◆ CAP_S64A

#define CAP_S64A   (1u << 31)

◆ GHC_AE

#define GHC_AE   (1u << 31)

◆ GHC_IE

#define GHC_IE   (1u << 1)

◆ PORT_BASE

#define PORT_BASE   0x100

◆ PORT_SIZE

#define PORT_SIZE   0x80

◆ PX_CLB

#define PX_CLB   0x00

◆ PX_CLBU

#define PX_CLBU   0x04

◆ PX_FB

#define PX_FB   0x08

◆ PX_FBU

#define PX_FBU   0x0C

◆ PX_IS

#define PX_IS   0x10

◆ PX_IE

#define PX_IE   0x14

◆ PX_CMD

#define PX_CMD   0x18

◆ PX_TFD

#define PX_TFD   0x20

◆ PX_SIG

#define PX_SIG   0x24

◆ PX_SSTS

#define PX_SSTS   0x28

◆ PX_SERR

#define PX_SERR   0x30

◆ PX_CI

#define PX_CI   0x38

◆ CMD_ST

#define CMD_ST   (1u << 0)

◆ CMD_FRE

#define CMD_FRE   (1u << 4)

◆ CMD_FR

#define CMD_FR   (1u << 14)

◆ CMD_CR

#define CMD_CR   (1u << 15)

◆ TFD_ERR

#define TFD_ERR   (1u << 0)

◆ TFD_DRQ

#define TFD_DRQ   (1u << 3)

◆ TFD_BSY

#define TFD_BSY   (1u << 7)

◆ IS_TFES

#define IS_TFES   (1u << 30)

◆ IE_DHRS

#define IE_DHRS   (1u << 0)

◆ IE_PSS

#define IE_PSS   (1u << 1)

◆ IE_DSS

#define IE_DSS   (1u << 2)

◆ IE_SDBS

#define IE_SDBS   (1u << 3)

◆ IE_ERRORS

#define IE_ERRORS   0xFDC00050u

◆ PORT_IE

#define PORT_IE   (IE_DHRS | IE_PSS | IE_DSS | IE_SDBS | IE_ERRORS)

◆ SIG_SATA_DISK

#define SIG_SATA_DISK   0x00000101u

◆ SSTS_DET_PRESENT

#define SSTS_DET_PRESENT   3u

◆ SSTS_IPM_ACTIVE

#define SSTS_IPM_ACTIVE   1u

◆ ATA_READ_DMA_EXT

#define ATA_READ_DMA_EXT   0x25

◆ ATA_WRITE_DMA_EXT

#define ATA_WRITE_DMA_EXT   0x35

◆ ATA_FLUSH_CACHE_EXT

#define ATA_FLUSH_CACHE_EXT   0xEA

◆ ATA_IDENTIFY

#define ATA_IDENTIFY   0xEC

◆ FIS_TYPE_H2D

#define FIS_TYPE_H2D   0x27

◆ OFF_CMD_LIST

#define OFF_CMD_LIST   0x000

◆ OFF_FIS

#define OFF_FIS   0x400

◆ OFF_CMD_TABLE

#define OFF_CMD_TABLE   0x500

◆ OFF_IDENTIFY

#define OFF_IDENTIFY   0x600

◆ SECTOR_SIZE

#define SECTOR_SIZE   512u

◆ BOUNCE_SECTORS

#define BOUNCE_SECTORS   (PAGE_SIZE / SECTOR_SIZE)

◆ SPIN_LIMIT

#define SPIN_LIMIT   100000000u

◆ COMMAND_TIMEOUT_MS

#define COMMAND_TIMEOUT_MS   5000u

Function Documentation

◆ ahci_init()

uint32_t ahci_init ( void )

find the AHCI controller (the first one) and its disks.

call after pci_init and mmio_init.

Returns
the number of disks found.

◆ ahci_disk()

ahci_disk_t * ahci_disk ( uint32_t index)

a disk by index (0 to the number ahci_init returned).

Returns
the disk, or NULL.