66#include "alloc.h"
77#include "pcie.h"
88#include "io.h"
9+ #include "klog.h"
910
1011static uint32_t ahci_ports [32 ];
1112static uint32_t port_total = 0 ;
1213static uint64_t ahci_ports_base_addr ;
1314HBA_MEM * hba_mem ;
1415
1516static int check_type (HBA_PORT * port ) {
16- uint32_t ssts = port -> ssts ;
17+ port -> cmd &= ~HBA_PxCMD_ST ;
18+ port -> cmd &= ~HBA_PxCMD_FRE ;
19+ while (port -> cmd & HBA_PxCMD_CR );
20+ port -> cmd |= HBA_PxCMD_FRE | HBA_PxCMD_ST ;
1721
22+ uint32_t ssts = port -> ssts ;
1823 uint8_t ipm = (ssts >> 8 ) & 0x0F ;
1924 uint8_t det = ssts & 0x0F ;
20- if (det != HBA_PORT_DET_PRESENT ) return AHCI_DEV_NULL ;
21- if (ipm != HBA_PORT_IPM_ACTIVE ) return AHCI_DEV_NULL ;
25+ if (det != HBA_PORT_DET_PRESENT || ipm != HBA_PORT_IPM_ACTIVE ) return AHCI_DEV_NULL ;
26+
27+ logkf ("SATA drive found at port %d\n" , port_total );
2228
2329 switch (port -> sig ) {
2430 case SATA_SIG_ATAPI :
@@ -78,17 +84,17 @@ void ahci_port_rebase(HBA_PORT *port, int portno) {
7884 // Command list maxim size = 32*32 = 1K per port
7985 port -> clb = ahci_ports_base_addr + (portno << 10 );
8086 port -> clbu = 0 ;
81- memset ((void * ) (port -> clb ), 0 , 1024 );
87+ memset ((void * ) phys_to_virt (port -> clb ), 0 , 1024 );
8288
8389 // FIS offset: 32K+256*portno
8490 // FIS entry size = 256 bytes per port
8591 port -> fb = ahci_ports_base_addr + (32 << 10 ) + (portno << 8 );
8692 port -> fbu = 0 ;
87- memset ((void * ) (port -> fb ), 0 , 256 );
93+ memset ((void * ) phys_to_virt (port -> fb ), 0 , 256 );
8894
8995 // Command table offset: 40K + 8K*portno
9096 // Command table size = 256*32 = 8K per port
91- HBA_CMD_HEADER * cmdheader = (HBA_CMD_HEADER * ) (port -> clb );
97+ HBA_CMD_HEADER * cmdheader = (HBA_CMD_HEADER * ) phys_to_virt (port -> clb );
9298 for (int i = 0 ; i < 32 ; i ++ ) {
9399 cmdheader [i ].prdtl = 8 ; // 8 prdt entries per command table
94100 // 256 bytes per command table, 64+16+48+16*8
@@ -107,23 +113,24 @@ bool ahci_identify(HBA_PORT *port, void *buf) {
107113 int slot = find_cmdslot (port );
108114 if (slot == -1 ) return false;
109115
110- HBA_CMD_HEADER * cmdheader = (HBA_CMD_HEADER * ) port -> clb ;
116+ HBA_CMD_HEADER * cmdheader = (HBA_CMD_HEADER * ) phys_to_virt (port -> clb );
117+ printk ("cmdheader: %p\n" ,cmdheader );
111118 cmdheader += slot ;
112119 cmdheader -> cfl = sizeof (FIS_REG_H2D ) / sizeof (uint32_t ); // Command FIS size
113120 cmdheader -> w = 0 ; // Read from device
114121 cmdheader -> prdtl = 1 ; // PRDT entries count
115122 cmdheader -> c = 1 ;
116123 HBA_CMD_TBL * cmdtbl = (HBA_CMD_TBL * ) (cmdheader -> ctba );
117124 memset (cmdtbl , 0 , sizeof (HBA_CMD_TBL ) + (cmdheader -> prdtl - 1 ) * sizeof (HBA_PRDT_ENTRY ));
118-
125+ printk ( "MEMSET cmdtbl: %p\n" , cmdtbl );
119126 cmdtbl -> prdt_entry [0 ].dba = (uint64_t ) buf ;
120127 cmdtbl -> prdt_entry [0 ].dbau = 0 ;
121128 cmdtbl -> prdt_entry [0 ].dbc = 0x200 - 1 ;
122129 cmdtbl -> prdt_entry [0 ].i = 1 ;
123130
124131 // Setup command
125132 FIS_REG_H2D * cmdfis = (FIS_REG_H2D * ) (& cmdtbl -> cfis );
126-
133+ printk ( "cmdfis: %p\n" , cmdfis );
127134 cmdfis -> fis_type = FIS_TYPE_REG_H2D ;
128135 cmdfis -> c = 1 ; // Command
129136 cmdfis -> command = 0xec ; // ATA IDENTIFY
@@ -140,6 +147,7 @@ bool ahci_identify(HBA_PORT *port, void *buf) {
140147
141148 port -> ci = 1 << slot ; // Issue command
142149
150+ printk ("III slot: %d\n" ,slot );
143151 // Wait for completion
144152 while (1 ) {
145153 // In some longer duration reads, it may be helpful to spin on the DPS bit
@@ -152,6 +160,8 @@ bool ahci_identify(HBA_PORT *port, void *buf) {
152160 }
153161 }
154162
163+ printk ("I\n" );
164+
155165 // Check again
156166 if (port -> is & HBA_PxIS_TFES ) {
157167 kwarn ("Read ahci disk error\n" );
@@ -171,14 +181,17 @@ void ahci_search_ports(HBA_MEM *abar) {
171181 int dt = check_type (& abar -> ports [i ]);
172182 if (dt == AHCI_DEV_SATA ) {
173183 ahci_ports [port_total ++ ] = i ;
184+ logkf ("SATA drive found at port %d\n" , i );
174185 } else if (dt == AHCI_DEV_SATAPI ) {
175186 ahci_ports [port_total ++ ] = i ;
187+ logkf ("SATAPI drive found at port %d\n" , i );
176188 } else if (dt == AHCI_DEV_SEMB ) {
189+ logkf ("SEMB drive found at port %d\n" , i );
177190 } else if (dt == AHCI_DEV_PM ) {
191+ logkf ("PM drive found at port %d\n" , i );
178192 } else {
179193 }
180194 }
181-
182195 pi >>= 1 ;
183196 i ++ ;
184197 }
@@ -212,10 +225,12 @@ void ahci_setup() {
212225 hba_mem = (HBA_MEM * )device -> bars [5 ].address ;
213226 }
214227
215- hba_mem -> ghc |= AHCI_GHC_AE ;
228+ hba_mem -> ghc |= ( 1 << 31 ) ;
216229
217230 ahci_ports_base_addr = (uint64_t ) malloc (1048576 );
218231
232+ logkf ("AHCI cap %x, vs %x\n" , hba_mem -> cap ,hba_mem -> vs );
233+
219234 ahci_search_ports (hba_mem );
220235 uint32_t i ;
221236 for (i = 0 ; i < port_total ; i ++ ) {
0 commit comments