|
ginnOS
The reference: libc, the libraries and the kernel
|
#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). | |
| #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).
| #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 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 |
| uint32_t ahci_init | ( | void | ) |
find the AHCI controller (the first one) and its disks.
call after pci_init and mmio_init.
| ahci_disk_t * ahci_disk | ( | uint32_t | index | ) |
a disk by index (0 to the number ahci_init returned).