/* Copyright (C) 2021 Rain */
/* This file is part of XNIX. */
/*
XNIX is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) and later version.
*/
/*
XNIX is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
/*
You should have received a copy of the GNU General Public License
along with XNIX. If not, see .
*/
#include
#include
#include
#include
/* i tried to keep this as simple as i can... */
/* pci-configure.bar5 points to this, and this points to another things */
/*
* PCI-config.bar5 -> AHCI HBA mem
* AHCI HBA mem -> AHCI HBA ports (max 32)
* AHCI_HBA ports -> command list (32 command headers)
* command list [32] -> command headers
* command headers -> command table
* command table -> prdts
* prdts [65536] (optional) -> data buffer
* command table -> fis
* fis -> lba addresses and others
* */
struct ahci_hba_port {
__uint64_t cmd_list;
/* received fis base */
__uint64_t rfis_base;
__uint32_t int_status;
__uint32_t int_enable;
__uint32_t cmd;
__uint32_t rsvd0[3];
__uint32_t sata_status;
__uint32_t sata_ctl;
__uint32_t sata_error;
/* these look like bitmaps */
__uint32_t sata_active;
__uint32_t cmd_issue;
__uint32_t rsvd[17];
} __attribute__ ((packed));
struct ahci_hba_mem {
#define HBA_MEM_CAP_MASK 0x1f
__uint32_t cap;
#define HBA_MEM_GHC_INT_ENABLE 0x00000002
#define HBA_MEM_GHC_AHCI_ENABLE 0x80000000
__uint32_t global_ctl;
__uint32_t int_status;
/* ports implemented */
__uint32_t ports_impled;
__uint32_t rsvd0[6];
/* ? */
#define HBA_MEM_BHO_OWN_BIOS 1
#define HBA_MEM_BHO_OWN_OS 2
#define HBA_MEM_BHO_OWN_CHANGED 8
#define HBA_MEM_BHO_OWN_RWC 8
__uint32_t bios_handoff;
__uint32_t rsvd1[53];
struct ahci_hba_port ports[32];
} __attribute__ ((packed));
struct ahci_cmd_header {
#define CMDHDR_CFG_CFISLEN_SHIFT 0
#define CMDHDR_CFG_CFISLEN_MASK ((1 << 5) - 1)
/* the mask for config */
#define CMDHDR_CFG_ATAPI 0x20
#define CMDHDR_CFG_WRITE 0x40
#define CMDHDR_CFG_PREFETCHABLE 0x80
#define CMDHDR_CFG_RESET 0x100
#define CMDHDR_CFG_BIST 0x200
#define CMDHDR_CFG_CLEAR_R_OK 0x400
#define CMDHDR_CFG_PORTML_MASK 0xf000
#define CMDHDR_CFG_PORTML_SHIFT 12
#define CMDHDR_CFG_PRDTLEN_MASK 0xffff0000
#define CMDHDR_CFG_PRDTLEN_SHIFT 16
#define CMDHDR_CFG_INTR 0x800000000
/* config word and prdt length (word) */
__uint32_t config;
/* total data length we transferred */
__uint32_t transfered;
__uint64_t cmd_table;
__uint32_t rsvd[4];
} __attribute__((packed));
struct ahci_prdt_entry {
__uint64_t buf;
__uint32_t rsvd;
/* size (of this.buf) */
#define PRDT_SIZE_MASK ((1 << 22) - 1)
/* interrupt on completion */
#define PRDT_SIZE_INTR (0x80000000u)
__uint32_t size;
} __attribute__ ((packed));
struct ahci_cmd_table {
__uint8_t cmd_fis[64];
__uint8_t atapi_cmd[16];
__uint32_t rsvd[0x0c];
/* max 65536 */
/* TODO: support more prdt-entries */
struct ahci_prdt_entry prdts[1];
} __attribute__ ((packed));
struct fis_h2d {
__uint8_t fis_type;
#define FISH2D_PORTMUL_MASK 15
#define FISH2D_PORTMUL_SHIFT 0
#define FISH2D_CMD 0x80
__uint8_t config;
__uint8_t command;
__uint8_t lba0, lba1, lba2;
__uint8_t dev;
__uint8_t lba3, lba4, lba5;
__uint8_t feature0;
__uint16_t nsectors;
__uint8_t icc;
__uint8_t control;
__uint32_t rsvd;
} __attribute__ ((packed));
#define ahci_info(fmt, ...) printk(yellow, "ahci: " fmt, ##__VA_ARGS__)
extern struct ahci_hba_mem *hba;
int ahci_rw_sect(volatile struct ahci_hba_port *, int, char *, __uint64_t);
void do_ahci_intr(void);