Rev 9271 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
9020 | rgimad | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3 | ;; Copyright (C) KolibriOS team 2004-2021. All rights reserved. ;; |
||
4 | ;; Distributed under terms of the GNU General Public License ;; |
||
5 | ;; ;; |
||
6 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
7 | |||
8 | $Revision$ |
||
9 | |||
10 | PCI_REG_STATUS_COMMAND = 0x0004 |
||
11 | PCI_REG_BAR5 = 0x0024 |
||
12 | |||
9162 | rgimad | 13 | AHCI_DBGLVL = 0 ; debug output verbosity level. 0 - less verbose, 1 - more verbose |
14 | |||
9134 | rgimad | 15 | ; different SATA device signatures |
9184 | rgimad | 16 | SATA_SIG_ATA = 0x00000101 ; SATA drive |
17 | SATA_SIG_ATAPI = 0xEB140101 ; SATAPI drive |
||
18 | SATA_SIG_SEMB = 0xC33C0101 ; Enclosure management bridge |
||
19 | SATA_SIG_PM = 0x96690101 ; Port multiplier |
||
9134 | rgimad | 20 | |
21 | ; Device type constants |
||
22 | AHCI_DEV_NULL = 0 |
||
23 | AHCI_DEV_SATA = 1 |
||
24 | AHCI_DEV_SEMB = 2 |
||
25 | AHCI_DEV_PM = 3 |
||
26 | AHCI_DEV_SATAPI = 4 |
||
27 | |||
28 | ; ATA commands |
||
9162 | rgimad | 29 | ATA_IDENTIFY = 0xEC |
30 | ATA_CMD_READ_DMA_EX = 0x25 |
||
31 | ATA_CMD_WRITE_DMA_EX = 0x35 |
||
9134 | rgimad | 32 | |
9139 | rgimad | 33 | ; ATA constants |
34 | ATA_DEV_BUSY = 0x80 |
||
35 | ATA_DEV_DRQ = 0x08 |
||
36 | |||
9134 | rgimad | 37 | ; ATAPI commands |
38 | ATAPI_IDENTIFY = 0xA1 |
||
39 | |||
9162 | rgimad | 40 | PRDT_MAX_ENTRIES = 16 ;65535 |
41 | |||
9023 | rgimad | 42 | ; bit_ prefix means that its index of bit |
43 | ; format: bit_AHCI_STR_REG_BIT |
||
44 | bit_AHCI_HBA_CAP2_BOH = 0 ; Supports BIOS/OS Handoff |
||
9020 | rgimad | 45 | |
9023 | rgimad | 46 | bit_AHCI_HBA_BOHC_BOS = 0 ; BIOS-Owned Semaphore (BIOS owns controller) |
47 | bit_AHCI_HBA_BOHC_OOS = 1 ; OS-Owned Semaphore (OS owns controller) |
||
48 | bit_AHCI_HBA_BOHC_BB = 4 ; BIOS Busy (polling bit while BIOS cleans up |
||
9020 | rgimad | 49 | |
9023 | rgimad | 50 | bit_AHCI_HBA_GHC_AHCI_ENABLE = 31 ; Enable AHCI mode |
51 | bit_AHCI_HBA_GHC_RESET = 0 ; Reset HBA |
||
52 | bit_AHCI_HBA_GHC_INTERRUPT_ENABLE = 1 ; Enable interrupts from the HBA |
||
53 | |||
9065 | rgimad | 54 | bit_AHCI_HBA_PxCMD_ST = 0 |
55 | bit_AHCI_HBA_PxCMD_FRE = 4 |
||
56 | bit_AHCI_HBA_PxCMD_FR = 14 |
||
57 | bit_AHCI_HBA_PxCMD_CR = 15 |
||
9139 | rgimad | 58 | bit_AHCI_HBA_PxIS_TFES = 30 |
9065 | rgimad | 59 | |
9130 | rgimad | 60 | AHCI_HBA_PxCMD_ST = 1 shl 0 |
61 | AHCI_HBA_PxCMD_FRE = 1 shl 4 |
||
62 | AHCI_HBA_PxCMD_FR = 1 shl 14 |
||
63 | AHCI_HBA_PxCMD_CR = 1 shl 15 |
||
64 | |||
9074 | rgimad | 65 | bit_AHCI_H2D_FLAG_CMD = 7 |
66 | |||
9037 | rgimad | 67 | AHCI_HBA_PxSSTS_DET = 0xF |
68 | AHCI_HBA_PORT_IPM_ACTIVE = 1 |
||
69 | AHCI_HBA_PxSSTS_DET_PRESENT = 3 |
||
70 | |||
9023 | rgimad | 71 | AHCI_MAX_PORTS = 32 ; |
9064 | rgimad | 72 | ;HBA_MEMORY_SIZE = 0x1100 |
9020 | rgimad | 73 | |
9139 | rgimad | 74 | AHCI_PORT_TIMEOUT = 1000000 |
75 | |||
9064 | rgimad | 76 | ; Frame Information Structure Types |
77 | FIS_TYPE_REG_H2D = 0x27 ; Register FIS - host to device |
||
78 | FIS_TYPE_REG_D2H = 0x34 ; Register FIS - device to host |
||
79 | FIS_TYPE_DMA_ACT = 0x39 ; DMA activate FIS - device to host |
||
80 | FIS_TYPE_DMA_SETUP = 0x41 ; DMA setup FIS - bidirectional |
||
81 | FIS_TYPE_DATA = 0x46 ; Data FIS - bidirectional |
||
82 | FIS_TYPE_BIST = 0x58 ; BIST activate FIS - bidirectional |
||
83 | FIS_TYPE_PIO_SETUP = 0x5F ; PIO setup FIS - device to host |
||
84 | FIS_TYPE_DEV_BITS = 0xA1 ; Set device bits FIS - device to host |
||
85 | |||
9020 | rgimad | 86 | ; Generic Host Control registers |
87 | struct HBA_MEM |
||
9064 | rgimad | 88 | cap dd ? ; 0x00, Host capabilities |
89 | ghc dd ? ; 0x04, Global host control |
||
90 | is dd ? ; 0x08, Interrupt status |
||
91 | pi dd ? ; 0x0C, Port implemented |
||
92 | version dd ? ; 0x10, Version |
||
9020 | rgimad | 93 | ccc_ctl dd ? ; 0x14, Command completion coalescing control |
94 | ccc_pts dd ? ; 0x18, Command completion coalescing ports |
||
95 | em_loc dd ? ; 0x1C, Enclosure management location |
||
96 | em_ctl dd ? ; 0x20, Enclosure management control |
||
9064 | rgimad | 97 | cap2 dd ? ; 0x24, Host capabilities extended |
9020 | rgimad | 98 | bohc dd ? ; 0x28, BIOS/OS handoff control and status |
9072 | rgimad | 99 | reserved rb (0xA0-HBA_MEM.reserved) ; 0x2C - 0x9F, Reserved |
100 | vendor rb (0x100-HBA_MEM.vendor) ; 0xA0 - 0xFF, Vendor specific |
||
9023 | rgimad | 101 | ports rb (sizeof.HBA_PORT*AHCI_MAX_PORTS) ; 0x100 - 0x10FF, Port control registers, max AHCI_MAX_PORTS |
9020 | rgimad | 102 | ends |
103 | |||
104 | ; Port Control registers |
||
105 | struct HBA_PORT |
||
9064 | rgimad | 106 | command_list_base_l dd ? ; 0x00, command list base address, 1K-byte aligned |
107 | command_list_base_h dd ? ; 0x04, command list base address upper 32 bits, used on 64 bit systems |
||
108 | fis_base_l dd ? ; 0x08, FIS base address, 256-byte aligned |
||
109 | fis_base_h dd ? ; 0x0C, FIS base address upper 32 bits, used on 64 bit systems |
||
110 | interrupt_status dd ? ; 0x10 |
||
111 | interrupt_enable dd ? ; 0x14 |
||
112 | command dd ? ; 0x18, command and status |
||
113 | reserved0 dd ? ; 0x1C |
||
114 | task_file_data dd ? ; 0x20 |
||
115 | signature dd ? ; 0x24 |
||
116 | sata_status dd ? ; 0x28, SATA status (SCR0:SStatus) |
||
117 | sata_control dd ? ; 0x2C, SATA control (SCR2:SControl) |
||
118 | sata_error dd ? ; 0x30, SATA error (SCR1:SError) |
||
119 | sata_active dd ? ; 0x34, SATA active (SCR3:SActive) |
||
120 | command_issue dd ? ; 0x38 |
||
121 | sata_notification dd ? ; 0x3C, SATA notification (SCR4:SNotification) |
||
122 | fis_based_switch_control dd ? ; 0x40 |
||
123 | reserved1 rd 11 ; 0x44 - 0x6F |
||
124 | vendor rd 4 ; 0x70 - 0x7F, vendor specific |
||
9020 | rgimad | 125 | ends |
126 | |||
9074 | rgimad | 127 | ; Command header structure, size = 32 bytes |
9068 | rgimad | 128 | struct HBA_CMD_HDR |
9131 | rgimad | 129 | flags1 db ? ; 0bPWACCCCC, P - Prefetchable, W - Write (1: H2D, 0: D2H) |
9068 | rgimad | 130 | ; A - ATAPI, C - Command FIS length in DWORDS, 2 ~ 16 |
131 | |||
9131 | rgimad | 132 | flags2 db ? ; 0bPPPPRCB(Re), P - Port multiplier port, R - Reserved, |
9068 | rgimad | 133 | ; C - Clear busy upon R_OK, B - BIST, Re - Reset |
134 | |||
135 | prdtl dw ? ; Physical region descriptor table length in entries |
||
136 | prdbc dd ? ; Physical region descriptor byte count transferred |
||
137 | ctba dd ? ; Command table descriptor base address |
||
138 | ctbau dd ? ; Command table descriptor base address upper 32 bits |
||
9072 | rgimad | 139 | rd 4 ; Reserved |
9068 | rgimad | 140 | ends |
141 | |||
9074 | rgimad | 142 | ; Physical region descriptor table entry, size = 16 bytes |
9069 | rgimad | 143 | struct HBA_PRDT_ENTRY |
144 | dba dd ? ; Data base address |
||
145 | dbau dd ? ; Data base address upper 32 bits |
||
9072 | rgimad | 146 | dd ? ; Reserved |
9131 | rgimad | 147 | flags dd ? ; 0bIR..RD..D, I (1 bit) - Interrupt on completion, |
9069 | rgimad | 148 | ; R (9 bits) - Reserved, D (22 bits) - Byte count, 4M max |
149 | ends |
||
150 | |||
151 | struct HBA_CMD_TBL |
||
152 | cfis rb 64 ; 0x00, Command FIS |
||
153 | acmd rb 16 ; 0x40, ATAPI command, 12 or 16 bytes |
||
9072 | rgimad | 154 | rb 48 ; 0x50, Reserved |
9069 | rgimad | 155 | prdt_entry HBA_PRDT_ENTRY ; 0x80, Physical region descriptor table entries, 0 ~ 65535 |
156 | ; so, this structure is variable-length |
||
157 | ends |
||
158 | |||
9068 | rgimad | 159 | ; Contains virtual mappings for port phys memory regions |
160 | struct PORT_DATA |
||
161 | clb dd ? ; Command list base |
||
162 | fb dd ? ; FIS base |
||
163 | ctba_arr rd 32 ; ctba_arr[0] = clb[0].ctba, ... and so on. |
||
164 | port dd ? ; address of correspoding HBA_PORT structure |
||
9074 | rgimad | 165 | portno dd ? ; port index, 0..31 |
9134 | rgimad | 166 | drive_type db ? ; drive type |
9140 | rgimad | 167 | sector_count dq ? ; number of sectors |
9272 | rgimad | 168 | ctr_ptr dd ? ; pointer to controller to which port belongs |
9068 | rgimad | 169 | ends |
170 | |||
9064 | rgimad | 171 | ; Register FIS – Host to Device |
172 | struct FIS_REG_H2D |
||
173 | fis_type db ? ; FIS_TYPE_REG_H2D |
||
9131 | rgimad | 174 | flags db ? ; 0bCRRRPPPP, C - 1: Command, 0: Control |
9064 | rgimad | 175 | ; R - Reserved, P - Port multiplier |
176 | |||
177 | command db ? ; Command register |
||
178 | featurel db ? ; Feature register, 7:0 |
||
179 | |||
180 | lba0 db ? ; LBA low register, 7:0 |
||
181 | lba1 db ? ; LBA mid register, 15:8 |
||
182 | lba2 db ? ; LBA high register, 23:16 |
||
183 | device db ? ; Device register |
||
184 | |||
185 | lba3 db ? ; LBA register, 31:24 |
||
186 | lba4 db ? ; LBA register, 39:32 |
||
187 | lba5 db ? ; LBA register, 47:40 |
||
188 | featureh db ? ; Feature register, 15:8 |
||
189 | |||
190 | countl db ? ; Count register, 7:0 |
||
191 | counth db ? ; Count register, 15:8 |
||
192 | icc db ? ; Isochronous command completion |
||
193 | control db ? ; Control register |
||
194 | |||
9072 | rgimad | 195 | rb 4 ; Reserved |
9064 | rgimad | 196 | ends |
197 | |||
198 | ; Register FIS – Device to Host |
||
199 | struct FIS_REG_D2H |
||
200 | fis_type db ? ; FIS_TYPE_REG_D2H |
||
201 | |||
9131 | rgimad | 202 | flags db ? ; 0bRIRPPPP, P - Port multiplier, R - Reserved |
9064 | rgimad | 203 | ; I - Interrupt bit |
204 | |||
205 | status db ? ; Status register |
||
206 | error db ? ; Error register |
||
207 | |||
208 | lba0 db ? ; LBA low register, 7:0 |
||
209 | lba1 db ? ; LBA mid register, 15:8 |
||
210 | lba2 db ? ; LBA high register, 23:16 |
||
211 | device db ? ; Device register |
||
212 | |||
213 | lba3 db ? ; LBA register, 31:24 |
||
214 | lba4 db ? ; LBA register, 39:32 |
||
215 | lba5 db ? ; LBA register, 47:40 |
||
9072 | rgimad | 216 | db ? ; Reserved |
9064 | rgimad | 217 | |
218 | countl db ? ; Count register, 7:0 |
||
219 | counth db ? ; Count register, 15:8 |
||
9072 | rgimad | 220 | rb 2 ; Reserved |
9064 | rgimad | 221 | |
9072 | rgimad | 222 | rb 4 ; Reserved |
9064 | rgimad | 223 | ends |
224 | |||
225 | ; Data FIS – Bidirectional |
||
226 | struct FIS_DATA |
||
227 | fis_type db ? ; FIS_TYPE_DATA |
||
9131 | rgimad | 228 | flags db ? ; 0bRRRRPPPP, R - Reserved, P - Port multiplier |
9072 | rgimad | 229 | rb 2 ; Reserved |
9064 | rgimad | 230 | ; DWORD 1 ~ N (?) |
231 | data rd 1 ; Payload |
||
232 | ends |
||
233 | |||
234 | ; PIO Setup – Device to Host |
||
235 | struct FIS_PIO_SETUP |
||
236 | fis_type db ? ; FIS_TYPE_PIO_SETUP |
||
237 | |||
9131 | rgimad | 238 | flags db ? ; 0bRIDRPPPP, P - Port multiplier, R - Reserved |
9064 | rgimad | 239 | ; I - Interrupt bit, D - Data transfer direction, 1 - device to host |
240 | |||
241 | status db ? ; Status register |
||
242 | error db ? ; Error register |
||
243 | |||
244 | lba0 db ? ; LBA low register, 7:0 |
||
245 | lba1 db ? ; LBA mid register, 15:8 |
||
246 | lba2 db ? ; LBA high register, 23:16 |
||
247 | device db ? ; Device register |
||
248 | |||
249 | lba3 db ? ; LBA register, 31:24 |
||
250 | lba4 db ? ; LBA register, 39:32 |
||
251 | lba5 db ? ; LBA register, 47:40 |
||
9072 | rgimad | 252 | db ? ; Reserved |
9064 | rgimad | 253 | |
254 | countl db ? ; Count register, 7:0 |
||
255 | counth db ? ; Count register, 15:8 |
||
9072 | rgimad | 256 | db ? ; Reserved |
9064 | rgimad | 257 | e_status db ? ; New value of status register |
258 | |||
259 | tc dw ? ; Transfer count |
||
9072 | rgimad | 260 | rb 2 ; Reserved |
9064 | rgimad | 261 | ends |
262 | |||
263 | ; DMA Setup – Device to Host |
||
264 | struct FIS_DMA_SETUP |
||
265 | fis_type db ? ; FIS_TYPE_DMA_SETUP |
||
9131 | rgimad | 266 | flags db ? ; 0bAIDRPPPP, A - Auto-activate. Specifies if DMA Activate FIS is needed, |
9064 | rgimad | 267 | ; I - Interrupt bit, D - Data transfer direction, 1 - device to host, |
268 | ; R - Reserved, P - Port multiplier |
||
269 | |||
9072 | rgimad | 270 | rb 2 ; Reserved |
9064 | rgimad | 271 | DMAbufferID dq ? ; DMA Buffer Identifier. |
272 | ; Used to Identify DMA buffer in host memory. |
||
273 | ; SATA Spec says host specific and not in Spec. |
||
274 | ; Trying AHCI spec might work. |
||
275 | |||
9072 | rgimad | 276 | dd ? ; Reserved |
277 | DMAbufOffset dd ? ; Byte offset into buffer. First 2 bits must be 0 |
||
9064 | rgimad | 278 | TransferCount dd ? ; Number of bytes to transfer. Bit 0 must be 0 |
9072 | rgimad | 279 | dd ? ; Reserved |
9064 | rgimad | 280 | ends |
281 | |||
282 | ; Set device bits FIS - device to host |
||
283 | struct FIS_DEV_BITS |
||
284 | fis_type db ? ; FIS_TYPE_DEV_BITS |
||
9131 | rgimad | 285 | flags db ? ; 0bNIRRPPPP, N - Notification, I - Interrupt, |
9064 | rgimad | 286 | ; R - Reserved, P - Port multiplier |
287 | |||
288 | status db ? ; Status register |
||
289 | error db ? ; Error register |
||
290 | |||
291 | protocol dd ? ; Protocol |
||
292 | ends |
||
293 | |||
9069 | rgimad | 294 | struct HBA_FIS |
295 | dsfis FIS_DMA_SETUP ; 0x00, DMA Setup FIS |
||
9072 | rgimad | 296 | rb 4 ; padding |
9069 | rgimad | 297 | |
298 | psfis FIS_PIO_SETUP ; 0x20, PIO Setup FIS |
||
9072 | rgimad | 299 | rb 12 ; padding |
9069 | rgimad | 300 | |
301 | rfis FIS_REG_D2H ; 0x40, Register - Device to Host FIS |
||
9072 | rgimad | 302 | rb 4 ; padding |
9069 | rgimad | 303 | |
304 | sdbfis FIS_DEV_BITS ; 0x58, Set Device Bit FIS |
||
305 | |||
306 | ufis rb 64 ; 0x60 |
||
307 | |||
9072 | rgimad | 308 | rb (0x100 - 0xA0) ; 0xA0, Reserved |
9069 | rgimad | 309 | ends |
310 | |||
9064 | rgimad | 311 | ; -------------------------------------------------- |
9020 | rgimad | 312 | uglobal |
9264 | rgimad | 313 | |
9020 | rgimad | 314 | align 4 |
9264 | rgimad | 315 | |
9271 | rgimad | 316 | ; AHCI controller structure |
317 | struct AHCI_CTR |
||
318 | abar dd ? ; pointer to HBA Memory (BAR5) mapped to virtual kernelspace memory |
||
319 | pcidev dd ? ; pointer to corresponding PCIDEV structure |
||
9264 | rgimad | 320 | port_data_arr rb (sizeof.PORT_DATA*AHCI_MAX_PORTS) |
9271 | rgimad | 321 | mutex MUTEX |
9264 | rgimad | 322 | ends |
323 | |||
9271 | rgimad | 324 | ctr1_data AHCI_CTR |
325 | ctr2_data AHCI_CTR |
||
326 | ctr3_data AHCI_CTR |
||
327 | ctr4_data AHCI_CTR |
||
328 | ctr5_data AHCI_CTR |
||
329 | ctr6_data AHCI_CTR |
||
330 | ctr7_data AHCI_CTR |
||
331 | ctr8_data AHCI_CTR |
||
9264 | rgimad | 332 | |
9271 | rgimad | 333 | ctr_ptr dd ? |
9020 | rgimad | 334 | endg |
335 | |||
9140 | rgimad | 336 | iglobal |
337 | align 4 |
||
338 | ahci_callbacks: |
||
339 | dd ahci_callbacks.end - ahci_callbacks |
||
340 | dd 0 ; no close function |
||
341 | dd 0 ; no closemedia function |
||
342 | dd ahci_querymedia |
||
9143 | rgimad | 343 | dd ahci_read |
9166 | rgimad | 344 | dd ahci_write |
9140 | rgimad | 345 | dd 0 ; no flush function |
346 | dd 0 ; use default cache size |
||
347 | .end: |
||
9264 | rgimad | 348 | hd_name db 'sd', 0, 0, 0 |
9271 | rgimad | 349 | sata_dev_counter dd 0 ; sata devices counter |
350 | ; TODO: satapi_dev_counter dd 0 ; satapi devices (optical drives) counter |
||
351 | ctr_counter dd 0 ; controllers counter |
||
9140 | rgimad | 352 | endg |
353 | |||
9064 | rgimad | 354 | ; ----------------------------------------------------------------------- |
9020 | rgimad | 355 | ; detect ahci controller and initialize |
356 | align 4 |
||
9068 | rgimad | 357 | ahci_init: |
9020 | rgimad | 358 | mov esi, pcidev_list |
9271 | rgimad | 359 | mov [ctr_ptr], ctr1_data |
360 | mov [sata_dev_counter], 0 |
||
9020 | rgimad | 361 | .find_ahci_ctr: |
362 | mov esi, [esi + PCIDEV.fd] |
||
363 | cmp esi, pcidev_list |
||
364 | jz .ahci_ctr_not_found |
||
365 | mov eax, [esi + PCIDEV.class] |
||
366 | ;DEBUGF 1, "K: device class = %x\n", eax |
||
367 | shr eax, 8 ; shift right because lowest 8 bits if ProgIf field |
||
368 | cmp eax, 0x0106 ; 0x01 - Mass Storage Controller class, 0x06 - Serial ATA Controller subclass |
||
369 | jz .ahci_ctr_found |
||
370 | jmp .find_ahci_ctr |
||
371 | |||
372 | .ahci_ctr_not_found: |
||
373 | DEBUGF 1, "K: AHCI controller not found\n" |
||
374 | ret |
||
375 | |||
376 | .ahci_ctr_found: |
||
9264 | rgimad | 377 | push esi |
9020 | rgimad | 378 | |
9271 | rgimad | 379 | mov ecx, [ctr_ptr] |
380 | add ecx, AHCI_CTR.mutex |
||
9264 | rgimad | 381 | call mutex_init |
382 | |||
9271 | rgimad | 383 | mov ecx, [ctr_ptr] |
384 | mov [ecx + AHCI_CTR.pcidev], esi |
||
9264 | rgimad | 385 | |
9020 | rgimad | 386 | mov eax, [esi+PCIDEV.class] |
387 | movzx ebx, byte [esi+PCIDEV.bus] |
||
388 | movzx ecx, byte [esi+PCIDEV.devfn] |
||
389 | shr ecx, 3 ; get rid of 3 lowest bits (function code), the rest bits is device code |
||
390 | movzx edx, byte [esi+PCIDEV.devfn] |
||
391 | and edx, 00000111b ; get only 3 lowest bits (function code) |
||
392 | DEBUGF 1, "K: found AHCI controller, (class, subcl, progif) = %x, bus = %x, device = %x, function = %x\n", eax, ebx, ecx, edx |
||
393 | |||
9023 | rgimad | 394 | ; get BAR5 value, it is physical address |
9037 | rgimad | 395 | movzx ebx, [esi + PCIDEV.bus] |
396 | movzx ebp, [esi + PCIDEV.devfn] |
||
397 | stdcall pci_read32, ebx, ebp, PCI_REG_BAR5 |
||
398 | DEBUGF 1, "K: AHCI controller MMIO = %x\n", eax |
||
399 | mov edi, eax |
||
9020 | rgimad | 400 | |
9037 | rgimad | 401 | ; get the size of MMIO region |
402 | stdcall pci_write32, ebx, ebp, PCI_REG_BAR5, 0xFFFFFFFF |
||
403 | stdcall pci_read32, ebx, ebp, PCI_REG_BAR5 |
||
404 | not eax |
||
405 | inc eax |
||
406 | DEBUGF 1, "K: AHCI: MMIO region size = 0x%x bytes\n", eax |
||
407 | |||
408 | ; Map MMIO region to virtual memory |
||
409 | stdcall map_io_mem, edi, eax, PG_SWR + PG_NOCACHE |
||
9264 | rgimad | 410 | push ecx |
9271 | rgimad | 411 | mov ecx, [ctr_ptr] |
412 | mov [ecx + AHCI_CTR.abar], eax |
||
9264 | rgimad | 413 | pop ecx |
9020 | rgimad | 414 | DEBUGF 1, "K: AHCI controller BAR5 mapped to virtual addr %x\n", eax |
415 | |||
9037 | rgimad | 416 | ; Restore the original BAR5 value |
417 | stdcall pci_write32, ebx, ebp, PCI_REG_BAR5, edi |
||
418 | |||
9023 | rgimad | 419 | ; Enable dma bus mastering, memory space access, clear the "disable interrupts" bit |
420 | ; Usually, it is already done before us |
||
9024 | rgimad | 421 | movzx ebx, [esi + PCIDEV.bus] |
422 | movzx ebp, [esi + PCIDEV.devfn] |
||
423 | stdcall pci_read32, ebx, ebp, PCI_REG_STATUS_COMMAND |
||
9020 | rgimad | 424 | DEBUGF 1, "K: AHCI: pci_status_command = %x\nEnabling interrupts, DMA bus mastering and memory space access\n", eax |
425 | or eax, 0x06 ; pci.command |= 0x06 (dma bus mastering + memory space access) |
||
426 | btr eax, 10 ; clear the "disable interrupts" bit |
||
427 | DEBUGF 1, "K: AHCI: pci_status_command = %x\n", eax |
||
9024 | rgimad | 428 | stdcall pci_write32, ebx, ebp, PCI_REG_STATUS_COMMAND, eax |
9020 | rgimad | 429 | |
9271 | rgimad | 430 | mov esi, [ctr_ptr] |
431 | mov esi, [esi + AHCI_CTR.abar] |
||
432 | |||
9023 | rgimad | 433 | ; ; Print some register values to debug board |
9064 | rgimad | 434 | ; DEBUGF 1, "K: AHCI: HBA.cap = %x, HBA.ghc = %x, HBA_MEM.version = %x\n", [esi + HBA_MEM.cap], [esi + HBA_MEM.ghc], [esi + HBA_MEM.version] |
9020 | rgimad | 435 | |
9023 | rgimad | 436 | ;------------------------------------------------------- |
437 | ; Request BIOS/OS ownership handoff, if supported. (TODO check correctness) |
||
9064 | rgimad | 438 | ;mov ebx, [esi + HBA_MEM.cap2] |
9023 | rgimad | 439 | ;DEBUGF 1, "K: AHCI: HBA_MEM.cap2 = %x\n", ebx |
9064 | rgimad | 440 | bt [esi + HBA_MEM.cap2], bit_AHCI_HBA_CAP2_BOH |
9020 | rgimad | 441 | jnc .end_handoff |
9023 | rgimad | 442 | DEBUGF 1, "K: AHCI: requesting AHCI ownership change...\n" |
9024 | rgimad | 443 | bts [esi + HBA_MEM.bohc], bit_AHCI_HBA_BOHC_OOS |
9020 | rgimad | 444 | |
445 | .wait_not_bos: |
||
9024 | rgimad | 446 | bt [esi + HBA_MEM.bohc], bit_AHCI_HBA_BOHC_BOS |
9020 | rgimad | 447 | jc .wait_not_bos |
448 | |||
449 | mov ebx, 3 |
||
450 | call delay_hs |
||
451 | |||
9023 | rgimad | 452 | ; if Bios Busy is still set after 30 mS, wait 2 seconds. |
9024 | rgimad | 453 | bt [esi + HBA_MEM.bohc], bit_AHCI_HBA_BOHC_BB |
9020 | rgimad | 454 | jnc @f |
455 | |||
456 | mov ebx, 200 |
||
457 | call delay_hs |
||
458 | @@: |
||
9023 | rgimad | 459 | DEBUGF 1, "K: AHCI: ownership change completed.\n" |
9020 | rgimad | 460 | |
461 | .end_handoff: |
||
9023 | rgimad | 462 | ;------------------------------------------------------- |
9020 | rgimad | 463 | |
9023 | rgimad | 464 | ; enable the AHCI and reset it |
9064 | rgimad | 465 | bts [esi + HBA_MEM.ghc], bit_AHCI_HBA_GHC_AHCI_ENABLE |
466 | bts [esi + HBA_MEM.ghc], bit_AHCI_HBA_GHC_RESET |
||
9020 | rgimad | 467 | |
9023 | rgimad | 468 | ; wait for reset to complete |
469 | .wait_reset: |
||
9064 | rgimad | 470 | bt [esi + HBA_MEM.ghc], bit_AHCI_HBA_GHC_RESET |
9023 | rgimad | 471 | jc .wait_reset |
9020 | rgimad | 472 | |
9023 | rgimad | 473 | ; enable the AHCI and interrupts |
9064 | rgimad | 474 | bts [esi + HBA_MEM.ghc], bit_AHCI_HBA_GHC_AHCI_ENABLE |
475 | bts [esi + HBA_MEM.ghc], bit_AHCI_HBA_GHC_INTERRUPT_ENABLE |
||
9023 | rgimad | 476 | mov ebx, 2 |
477 | call delay_hs |
||
478 | |||
9064 | rgimad | 479 | DEBUGF 1, "K: AHCI: caps: %x %x, ver: %x, ghc: %x, pi: %x\n", [esi + HBA_MEM.cap], [esi + HBA_MEM.cap2], [esi + HBA_MEM.version], [esi + HBA_MEM.ghc], [esi + HBA_MEM.pi] |
9020 | rgimad | 480 | |
9037 | rgimad | 481 | ; TODO: |
482 | ; calculate irq line |
||
483 | ; ahciHBA->ghc |= AHCI_GHC_IE; |
||
484 | ; IDT::RegisterInterruptHandler(irq, InterruptHandler); |
||
9064 | rgimad | 485 | ; ahciHBA->is = 0xffffffff; |
9037 | rgimad | 486 | |
487 | xor ebx, ebx |
||
488 | .detect_drives: |
||
489 | cmp ebx, AHCI_MAX_PORTS |
||
490 | jae .end_detect_drives |
||
491 | |||
492 | ; if port with index ebx is not implemented then go to next |
||
9064 | rgimad | 493 | mov ecx, [esi + HBA_MEM.pi] |
9037 | rgimad | 494 | bt ecx, ebx |
495 | jnc .continue_detect_drives |
||
496 | |||
497 | mov edi, ebx |
||
9074 | rgimad | 498 | imul edi, sizeof.HBA_PORT |
9037 | rgimad | 499 | add edi, HBA_MEM.ports |
500 | add edi, esi |
||
501 | ; now edi - base of HBA_MEM.ports[ebx] |
||
502 | |||
9130 | rgimad | 503 | DEBUGF 1, "K: AHCI: port %d, cmd = %x, ssts = %x\n", ebx, [edi + HBA_PORT.command], [edi + HBA_PORT.sata_status] |
9037 | rgimad | 504 | |
9130 | rgimad | 505 | ; If port is not idle force it to be idle |
506 | mov eax, [edi + HBA_PORT.command] |
||
507 | and eax, (AHCI_HBA_PxCMD_ST or AHCI_HBA_PxCMD_CR or AHCI_HBA_PxCMD_FRE or AHCI_HBA_PxCMD_FR) |
||
508 | test eax, eax |
||
509 | jz @f |
||
510 | |||
511 | mov eax, edi |
||
512 | DEBUGF 1, "ahci_stop_cmd..\n" |
||
513 | call ahci_stop_cmd |
||
514 | @@: |
||
515 | ; TODO: what is purpose of this block of code ? |
||
516 | ; Reset port, disable slumber and partial state |
||
517 | ; mov [edi + HBA_PORT.sata_control], 0x301 |
||
518 | ; push ebx |
||
519 | ; mov ebx, 5 ; wait 50 ms |
||
520 | ; call delay_hs |
||
521 | ; pop ebx |
||
522 | ; mov [edi + HBA_PORT.sata_control], 0x300 |
||
523 | |||
524 | ; if(abar->cap & HBA_MEM_CAP_SSS) |
||
525 | ; { |
||
526 | ; abar->ports[i].cmd |= (HBA_PxCMD_SUD | HBA_PxCMD_POD | HBA_PxCMD_ICC); |
||
527 | ; Sleep(10); |
||
528 | ; } |
||
529 | ; rewritten to: |
||
530 | bt [esi + HBA_MEM.cap], 27 ; check Supports Staggered Spin-up bit in capabilities |
||
531 | jnc @f |
||
9134 | rgimad | 532 | DEBUGF 1, "Supports Staggered Spin-up, spinning up the port..\n" |
9130 | rgimad | 533 | or [edi + HBA_PORT.command], (0x0002 or 0x0004 or 0x10000000) |
534 | push ebx |
||
9162 | rgimad | 535 | mov ebx, 10 ; wait 100 ms |
9130 | rgimad | 536 | call delay_hs |
537 | pop ebx |
||
538 | @@: |
||
539 | ; Clear interrupt status and error status |
||
540 | mov [edi + HBA_PORT.sata_error], 0xFFFFFFFF |
||
541 | mov [edi + HBA_PORT.interrupt_status], 0xFFFFFFFF |
||
542 | |||
543 | ; ------------------------------------------ |
||
544 | |||
9037 | rgimad | 545 | mov ecx, [edi + HBA_PORT.sata_status] |
546 | shr ecx, 8 |
||
547 | and ecx, 0x0F |
||
548 | cmp ecx, AHCI_HBA_PORT_IPM_ACTIVE |
||
549 | jne .continue_detect_drives |
||
550 | |||
551 | mov ecx, [edi + HBA_PORT.sata_status] |
||
552 | and ecx, AHCI_HBA_PxSSTS_DET |
||
553 | cmp ecx, AHCI_HBA_PxSSTS_DET_PRESENT |
||
9068 | rgimad | 554 | jne .continue_detect_drives |
9037 | rgimad | 555 | |
9134 | rgimad | 556 | ; DEBUGF 1, "K: AHCI: found drive at port %d, cmd = 0x%x, ssts = 0x%x, signature = 0x%x\n", ebx, [edi + HBA_PORT.command], [edi + HBA_PORT.sata_status], [edi + HBA_PORT.signature] |
9037 | rgimad | 557 | |
9068 | rgimad | 558 | mov ecx, ebx |
9074 | rgimad | 559 | imul ecx, sizeof.PORT_DATA |
9271 | rgimad | 560 | add ecx, AHCI_CTR.port_data_arr |
561 | add ecx, [ctr_ptr] |
||
9272 | rgimad | 562 | mov eax, [ctr_ptr] |
563 | mov [ecx + PORT_DATA.ctr_ptr], eax ; to which controller the port belongs |
||
9068 | rgimad | 564 | stdcall ahci_port_rebase, edi, ebx, ecx |
565 | |||
9134 | rgimad | 566 | ; DEBUGF 1, "K: AHCI: After REBASING, signature = 0x%x\n", [edi + HBA_PORT.signature] |
567 | |||
9135 | rgimad | 568 | ; Determine drive type by checking port signature |
9134 | rgimad | 569 | .switch_sig: |
570 | cmp [edi + HBA_PORT.signature], SATA_SIG_ATA |
||
9135 | rgimad | 571 | mov eax, AHCI_DEV_SATA |
572 | jz .end_switch_sig |
||
573 | |||
9134 | rgimad | 574 | cmp [edi + HBA_PORT.signature], SATA_SIG_ATAPI |
9135 | rgimad | 575 | mov eax, AHCI_DEV_SATAPI |
576 | jz .end_switch_sig |
||
577 | |||
9134 | rgimad | 578 | cmp [edi + HBA_PORT.signature], SATA_SIG_SEMB |
9135 | rgimad | 579 | mov eax, AHCI_DEV_SEMB |
580 | jz .end_switch_sig |
||
581 | |||
9134 | rgimad | 582 | cmp [edi + HBA_PORT.signature], SATA_SIG_PM |
9135 | rgimad | 583 | mov eax, AHCI_DEV_PM |
584 | jz .end_switch_sig |
||
585 | |||
9134 | rgimad | 586 | DEBUGF 1, "Unknown device signature\n" |
9135 | rgimad | 587 | mov eax, AHCI_DEV_NULL |
9134 | rgimad | 588 | .end_switch_sig: |
9136 | rgimad | 589 | mov [ecx + PORT_DATA.drive_type], al |
9134 | rgimad | 590 | |
591 | DEBUGF 1, "K: AHCI: found drive on port %u: TYPE = %u\n", ebx, [ecx + PORT_DATA.drive_type] |
||
592 | |||
9074 | rgimad | 593 | stdcall ahci_port_identify, ecx |
594 | |||
9140 | rgimad | 595 | cmp [ecx + PORT_DATA.drive_type], AHCI_DEV_SATA |
596 | jne .after_add_disk ; skip adding disk code |
||
597 | ; register disk in system: |
||
9143 | rgimad | 598 | |
599 | ;stdcall ahci_read_first_sector, ecx |
||
9141 | rgimad | 600 | |
9142 | rgimad | 601 | push ecx |
9271 | rgimad | 602 | mov eax, [sata_dev_counter] |
603 | inc [sata_dev_counter] |
||
9140 | rgimad | 604 | xor edx, edx |
605 | mov ecx, 10 |
||
9271 | rgimad | 606 | div ecx ; eax = sata_dev_counter / 10, edx = sata_dev_counter % 10 |
9140 | rgimad | 607 | test eax, eax |
608 | jz .concat_one |
||
609 | add al, '0' |
||
610 | mov byte [hd_name + 2], al |
||
611 | add dl, '0' |
||
612 | mov byte [hd_name + 3], dl |
||
613 | jmp .endif1 |
||
614 | .concat_one: |
||
615 | add dl, '0' |
||
616 | mov byte [hd_name + 2], dl |
||
617 | .endif1: |
||
9142 | rgimad | 618 | pop ecx |
9140 | rgimad | 619 | |
620 | DEBUGF 1, "adding '%s'\n", hd_name |
||
621 | |||
9142 | rgimad | 622 | push ecx |
9140 | rgimad | 623 | stdcall disk_add, ahci_callbacks, hd_name, ecx, 0 |
9142 | rgimad | 624 | pop ecx |
9140 | rgimad | 625 | test eax, eax |
626 | jz .disk_add_fail |
||
9142 | rgimad | 627 | push ecx |
9140 | rgimad | 628 | stdcall disk_media_changed, eax, 1 ; system will scan for partitions on disk |
9142 | rgimad | 629 | pop ecx |
9141 | rgimad | 630 | |
9140 | rgimad | 631 | jmp .after_add_disk |
632 | |||
633 | .disk_add_fail: |
||
634 | DEBUGF 1, "Failed to add disk\n" |
||
635 | .after_add_disk: |
||
636 | |||
9037 | rgimad | 637 | .continue_detect_drives: |
638 | inc ebx |
||
639 | jmp .detect_drives |
||
640 | |||
9064 | rgimad | 641 | |
9037 | rgimad | 642 | |
643 | .end_detect_drives: |
||
9264 | rgimad | 644 | pop esi |
9271 | rgimad | 645 | add [ctr_ptr], sizeof.AHCI_CTR |
646 | inc [ctr_counter] |
||
647 | cmp [ctr_counter], 8 |
||
9264 | rgimad | 648 | jnz .find_ahci_ctr |
649 | DEBUGF 1, "AHCI: reached controllers number limit\n" |
||
9020 | rgimad | 650 | ret |
9065 | rgimad | 651 | ; ------------------------------------------------- |
9020 | rgimad | 652 | |
9074 | rgimad | 653 | modelstr rb 42 |
654 | ; Identify drive on port ; TODO check |
||
655 | ; in: pdata - address of PORT_DATA structure |
||
656 | proc ahci_port_identify stdcall, pdata: dword |
||
657 | locals |
||
658 | cmdslot dd ? |
||
659 | cmdheader dd ? |
||
660 | cmdtable dd ? |
||
661 | buf_phys dd ? |
||
662 | buf_virt dd ? |
||
663 | endl |
||
664 | |||
665 | pushad |
||
666 | |||
667 | mov esi, [pdata] ; esi - address of PORT_DATA struct of port |
||
9272 | rgimad | 668 | stdcall ahci_find_cmdslot, esi |
9074 | rgimad | 669 | |
670 | cmp eax, -1 |
||
9184 | rgimad | 671 | jne .cmdslot_found |
9074 | rgimad | 672 | |
673 | DEBUGF 1, "No free cmdslot on port %u\n", [esi + PORT_DATA.portno] |
||
9140 | rgimad | 674 | jmp .ret |
9074 | rgimad | 675 | |
676 | .cmdslot_found: |
||
677 | mov [cmdslot], eax |
||
9134 | rgimad | 678 | ; DEBUGF 1, "Found free cmdslot %u on port %u\n", [cmdslot], [esi + PORT_DATA.portno] |
9074 | rgimad | 679 | |
680 | shl eax, BSF sizeof.HBA_CMD_HDR |
||
681 | add eax, [esi + PORT_DATA.clb] |
||
682 | mov [cmdheader], eax ; address of virtual mapping of command header |
||
683 | mov eax, [cmdslot] |
||
684 | mov eax, [esi + eax*4 + PORT_DATA.ctba_arr] |
||
685 | mov [cmdtable], eax ; address of virtual mapping of command table of command header |
||
686 | |||
687 | stdcall _memset, eax, 0, sizeof.HBA_CMD_TBL |
||
688 | |||
689 | call alloc_page |
||
690 | mov [buf_phys], eax |
||
691 | |||
692 | stdcall map_io_mem, eax, 4096, PG_NOCACHE + PG_SWR ; map to virt memory so we can work with it |
||
693 | mov [buf_virt], eax |
||
694 | |||
695 | mov eax, [cmdtable] |
||
696 | mov ebx, [buf_phys] |
||
697 | mov dword [eax + HBA_CMD_TBL.prdt_entry + HBA_PRDT_ENTRY.dba], ebx |
||
698 | mov dword [eax + HBA_CMD_TBL.prdt_entry + HBA_PRDT_ENTRY.dbau], 0 |
||
9131 | rgimad | 699 | and [eax + HBA_CMD_TBL.prdt_entry + HBA_PRDT_ENTRY.flags], not 0x3FFFFF ; zero out lower 22 bits, they used for byte count |
700 | or [eax + HBA_CMD_TBL.prdt_entry + HBA_PRDT_ENTRY.flags], 512 - 1 ; reason why -1 see in spec on this field |
||
701 | ; or [eax + HBA_CMD_TBL.prdt_entry + HBA_PRDT_ENTRY.flags], 1 shl 31 ; enable interrupt on completion |
||
702 | |||
9074 | rgimad | 703 | mov eax, [cmdheader] |
9131 | rgimad | 704 | and [eax + HBA_CMD_HDR.flags1], not 0x1F ; zero out lower 5 bits, they will be used for cfl |
705 | or [eax + HBA_CMD_HDR.flags1], (sizeof.FIS_REG_H2D / 4) ; set command fis length in dwords |
||
706 | movzx bx, [eax + HBA_CMD_HDR.flags1] |
||
707 | btr bx, 6 ; flag W = 0 |
||
708 | mov [eax + HBA_CMD_HDR.flags1], bl |
||
709 | movzx bx, [eax + HBA_CMD_HDR.flags2] |
||
710 | btr bx, 2 ; flag C = 0 |
||
711 | mov [eax + HBA_CMD_HDR.flags2], bl |
||
9074 | rgimad | 712 | mov [eax + HBA_CMD_HDR.prdtl], 1 |
713 | |||
714 | mov eax, [cmdtable] |
||
715 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.fis_type], FIS_TYPE_REG_H2D |
||
9131 | rgimad | 716 | movzx ebx, byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.flags] |
9074 | rgimad | 717 | bts ebx, bit_AHCI_H2D_FLAG_CMD ; Set Command bit in H2D FIS. |
9131 | rgimad | 718 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.flags], bl |
9134 | rgimad | 719 | |
720 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.command], ATA_IDENTIFY |
||
721 | cmp [esi + PORT_DATA.drive_type], AHCI_DEV_SATAPI |
||
722 | jne @f |
||
723 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.command], ATAPI_IDENTIFY |
||
724 | @@: |
||
9074 | rgimad | 725 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.device], 0 |
726 | |||
9272 | rgimad | 727 | mov edi, [esi + PORT_DATA.port] ; edi - address of HBA_PORT struct of port |
728 | |||
9139 | rgimad | 729 | ; Wait on previous command to complete, before issuing new command. |
730 | stdcall ahci_port_wait, edi, AHCI_PORT_TIMEOUT |
||
731 | ; DEBUGF 1, "eax = %x\n", eax |
||
732 | ; TODO check eax error value |
||
9074 | rgimad | 733 | |
734 | mov eax, [cmdslot] |
||
735 | bts [edi + HBA_PORT.command_issue], eax ; Issue the command |
||
736 | |||
9139 | rgimad | 737 | ; Wait for command completion |
738 | stdcall ahci_port_cmd_wait, edi, eax;, AHCI_PORT_CMD_TIMEOUT |
||
739 | ; DEBUGF 1, " eax = %x\n", eax |
||
740 | ; TODO check eax error value |
||
9074 | rgimad | 741 | |
9134 | rgimad | 742 | ; DEBUGF 1, "sata_error register = 0x%x\n", [edi + HBA_PORT.sata_error] |
9130 | rgimad | 743 | |
744 | mov esi, [buf_virt] |
||
9074 | rgimad | 745 | add esi, 27*2 |
746 | mov edi, modelstr |
||
747 | mov ecx, ((46-27)+1)*2 |
||
748 | cld |
||
749 | rep movsb |
||
750 | mov byte [edi], 0 |
||
751 | |||
9138 | rgimad | 752 | stdcall swap_bytes_in_words, modelstr, (46-27)+1 |
9131 | rgimad | 753 | DEBUGF 1, "IDENTIFICATION RESULT: MODEL = %s\n", modelstr |
9074 | rgimad | 754 | |
9138 | rgimad | 755 | mov esi, [buf_virt] |
756 | mov eax, [esi + 200] |
||
757 | mov edx, [esi + 200 + 4] |
||
758 | DEBUGF 1, "lba48 mode sector count = 0x%x:%x\n", edx, eax |
||
759 | |||
9145 | rgimad | 760 | mov ebx, [pdata] |
761 | mov dword [ebx + PORT_DATA.sector_count], eax |
||
762 | mov dword [ebx + PORT_DATA.sector_count + 4], edx |
||
763 | |||
9138 | rgimad | 764 | shrd eax, edx, 11 ; i.e *512 / 1024 / 1024, 512 - sector size |
765 | DEBUGF 1, "disk capacity = %u MiB ", eax |
||
766 | shrd eax, edx, 10 ; / 1024 |
||
767 | DEBUGF 1, "= %u GiB\n", eax |
||
9074 | rgimad | 768 | .ret: |
769 | popad |
||
770 | ret |
||
771 | endp |
||
772 | |||
9140 | rgimad | 773 | proc ahci_querymedia stdcall, pdata, mediainfo |
774 | push ecx edx |
||
775 | mov eax, [mediainfo] |
||
776 | mov edx, [pdata] |
||
777 | mov [eax + DISKMEDIAINFO.Flags], 0 |
||
778 | mov [eax + DISKMEDIAINFO.SectorSize], 512 |
||
779 | mov ecx, dword[edx + PORT_DATA.sector_count] |
||
780 | mov dword [eax + DISKMEDIAINFO.Capacity], ecx |
||
781 | mov ecx, dword[edx + PORT_DATA.sector_count + 4] |
||
782 | mov dword [eax + DISKMEDIAINFO.Capacity + 4], ecx |
||
783 | pop edx ecx |
||
784 | xor eax, eax |
||
785 | ret |
||
786 | endp |
||
9134 | rgimad | 787 | |
9271 | rgimad | 788 | ; Read/write sectors, note: currently work for SATA, not SATAPI |
9143 | rgimad | 789 | ; return value: 0 = success, otherwise = error |
9162 | rgimad | 790 | proc ahci_rw_sectors stdcall pdata: dword, vbuf: dword, startsector: qword, numsectors: dword, is_write: dword |
9143 | rgimad | 791 | locals |
792 | cmdslot dd ? |
||
793 | cmdheader dd ? |
||
794 | cmdtable dd ? |
||
9162 | rgimad | 795 | vbuf_orig dd ? |
796 | vbuf_len dd ? |
||
797 | phys_region_start dd ? |
||
798 | new_phys_region_start dd ? |
||
799 | cur_prd dd ? |
||
800 | cur_phys dd ? |
||
801 | dbc dd ? |
||
802 | cur_phys_page dd ? |
||
803 | next_phys_page dd ? |
||
804 | cur_antioffset dd ? |
||
805 | prdt_bytes_total dd ? |
||
9143 | rgimad | 806 | endl |
807 | |||
808 | pushad |
||
809 | |||
9162 | rgimad | 810 | DEBUGF AHCI_DBGLVL, " ahci_rw_sectors: buffer = 0x%x, startsector = 0x%x:%x, numsectors = %u, is_write = %u\n", [vbuf], [startsector], [startsector + 4], [numsectors], [is_write]:1 |
9143 | rgimad | 811 | |
812 | mov esi, [pdata] ; esi - address of PORT_DATA struct of port |
||
9272 | rgimad | 813 | stdcall ahci_find_cmdslot, esi |
9143 | rgimad | 814 | cmp eax, -1 |
9184 | rgimad | 815 | jne .cmdslot_found |
9143 | rgimad | 816 | |
9162 | rgimad | 817 | DEBUGF AHCI_DBGLVL, "No free cmdslot on port %u\n", [esi + PORT_DATA.portno] |
818 | jmp .fail |
||
9143 | rgimad | 819 | |
820 | .cmdslot_found: |
||
821 | mov [cmdslot], eax |
||
9162 | rgimad | 822 | DEBUGF AHCI_DBGLVL, "Found free cmdslot %u on port %u\n", [cmdslot], [esi + PORT_DATA.portno] |
9143 | rgimad | 823 | |
824 | shl eax, BSF sizeof.HBA_CMD_HDR |
||
825 | add eax, [esi + PORT_DATA.clb] |
||
826 | mov [cmdheader], eax ; address of virtual mapping of command header |
||
827 | mov eax, [cmdslot] |
||
828 | mov eax, [esi + eax*4 + PORT_DATA.ctba_arr] |
||
829 | mov [cmdtable], eax ; address of virtual mapping of command table of command header |
||
830 | |||
831 | mov eax, [cmdheader] |
||
832 | and [eax + HBA_CMD_HDR.flags1], not 0x1F ; zero out lower 5 bits, they will be used for cfl |
||
833 | or [eax + HBA_CMD_HDR.flags1], (sizeof.FIS_REG_H2D / 4) ; set command fis length in dwords |
||
834 | movzx bx, [eax + HBA_CMD_HDR.flags1] |
||
835 | btr bx, 6 ; flag W = 0 |
||
9162 | rgimad | 836 | cmp [is_write], 1 ; if is_write then set W flag |
837 | jne @f |
||
838 | bts bx, 6 |
||
839 | @@: |
||
9143 | rgimad | 840 | mov [eax + HBA_CMD_HDR.flags1], bl |
841 | movzx bx, [eax + HBA_CMD_HDR.flags2] |
||
842 | btr bx, 2 ; flag C = 0 |
||
843 | mov [eax + HBA_CMD_HDR.flags2], bl |
||
844 | |||
9162 | rgimad | 845 | mov eax, [vbuf] |
846 | mov [vbuf_orig], eax |
||
9143 | rgimad | 847 | mov ebx, [numsectors] |
848 | shl ebx, 9 ; *= 512 |
||
9162 | rgimad | 849 | mov [vbuf_len], ebx |
850 | DEBUGF AHCI_DBGLVL, "vbuf_len = %u bytes\n", ebx |
||
9143 | rgimad | 851 | |
9162 | rgimad | 852 | mov ebx, [vbuf] |
853 | and ebx, 0xFFF |
||
854 | mov eax, [vbuf] |
||
855 | call get_pg_addr |
||
856 | add eax, ebx |
||
857 | mov [phys_region_start], eax |
||
858 | mov [prdt_bytes_total], 0 |
||
859 | mov [cur_prd], 0 |
||
860 | .fill_prdt: |
||
861 | cmp [vbuf_len], 0 |
||
862 | jbe .fill_prdt_end |
||
9143 | rgimad | 863 | |
9162 | rgimad | 864 | mov eax, [vbuf] |
865 | call get_pg_addr |
||
866 | mov [cur_phys_page], eax |
||
867 | mov eax, [vbuf] |
||
868 | add eax, 4096 |
||
869 | call get_pg_addr |
||
870 | mov [next_phys_page], eax |
||
871 | mov eax, 4096 |
||
872 | mov ebx, [vbuf] |
||
9143 | rgimad | 873 | and ebx, 0xFFF |
9162 | rgimad | 874 | sub eax, ebx |
875 | mov [cur_antioffset], eax |
||
876 | mov eax, [cur_phys_page] |
||
9143 | rgimad | 877 | add eax, ebx |
9162 | rgimad | 878 | mov [cur_phys], eax |
9143 | rgimad | 879 | |
9162 | rgimad | 880 | .check_if1: |
881 | mov eax, [vbuf_len] |
||
882 | cmp eax, [cur_antioffset] |
||
883 | ja .check_if2 |
||
9143 | rgimad | 884 | |
9162 | rgimad | 885 | mov eax, [cur_phys] |
886 | sub eax, [phys_region_start] |
||
887 | add eax, [vbuf_len] |
||
888 | dec eax |
||
889 | mov [dbc], eax |
||
890 | mov eax, [next_phys_page] |
||
891 | mov [new_phys_region_start], eax |
||
892 | jmp .add_prd |
||
9143 | rgimad | 893 | |
9162 | rgimad | 894 | .check_if2: |
895 | mov eax, [cur_phys] |
||
896 | add eax, [cur_antioffset] |
||
897 | cmp eax, [next_phys_page] |
||
898 | je .check_if3 |
||
899 | |||
900 | mov eax, [cur_phys] |
||
901 | add eax, [cur_antioffset] |
||
902 | sub eax, [phys_region_start] |
||
903 | dec eax |
||
904 | mov [dbc], eax |
||
905 | mov eax, [next_phys_page] |
||
906 | mov [new_phys_region_start], eax |
||
907 | jmp .add_prd |
||
908 | |||
909 | .check_if3: |
||
910 | mov eax, [cur_phys] |
||
911 | add eax, [cur_antioffset] |
||
912 | sub eax, [phys_region_start] |
||
913 | cmp eax, 4*1024*1024 |
||
914 | jb .after_ifs |
||
915 | |||
916 | mov [dbc], 4*1024*1024 - 1 |
||
917 | mov eax, [phys_region_start] |
||
918 | add eax, 4*1024*1024 |
||
919 | jmp .add_prd |
||
920 | |||
921 | .after_ifs: |
||
922 | jmp .step_next |
||
923 | |||
924 | .add_prd: |
||
925 | mov ebx, [cur_prd] |
||
9143 | rgimad | 926 | shl ebx, BSF sizeof.HBA_PRDT_ENTRY |
927 | add ebx, [cmdtable] |
||
9162 | rgimad | 928 | add ebx, HBA_CMD_TBL.prdt_entry ; now ebx - address of 'th prdt_entry |
929 | |||
930 | DEBUGF AHCI_DBGLVL, "Added PRDT entry: dba = 0x%x, dbc = %u\n", [phys_region_start], [dbc] |
||
931 | mov eax, [phys_region_start] |
||
9143 | rgimad | 932 | mov [ebx + HBA_PRDT_ENTRY.dba], eax |
933 | mov [ebx + HBA_PRDT_ENTRY.dbau], 0 |
||
934 | and [ebx + HBA_PRDT_ENTRY.flags], not 0x3FFFFF ; zero out lower 22 bits, they used for byte count |
||
9162 | rgimad | 935 | mov eax, [dbc] |
936 | or [ebx + HBA_PRDT_ENTRY.flags], eax |
||
937 | |||
938 | inc [cur_prd] |
||
939 | mov eax, [dbc] |
||
940 | inc eax |
||
941 | add [prdt_bytes_total], eax |
||
942 | mov eax, [new_phys_region_start] |
||
943 | mov [phys_region_start], eax |
||
944 | cmp [cur_prd], PRDT_MAX_ENTRIES |
||
945 | jne @f |
||
946 | jmp .fill_prdt_end |
||
947 | @@: |
||
948 | |||
949 | .step_next: |
||
950 | mov eax, [vbuf_len] |
||
951 | cmp eax, [cur_antioffset] |
||
952 | jbe @f |
||
953 | mov eax, [cur_antioffset] |
||
954 | @@: |
||
955 | add [vbuf], eax |
||
956 | sub [vbuf_len], eax |
||
957 | jmp .fill_prdt |
||
9143 | rgimad | 958 | |
9162 | rgimad | 959 | .fill_prdt_end: |
960 | |||
961 | mov eax, [cmdheader] |
||
962 | mov ebx, [cur_prd] |
||
963 | DEBUGF AHCI_DBGLVL, " PRDTL = %u\n", ebx |
||
964 | mov [eax + HBA_CMD_HDR.prdtl], bx |
||
965 | |||
966 | mov eax, [prdt_bytes_total] |
||
967 | DEBUGF AHCI_DBGLVL, " prdt_bytes_total = %u\n", eax |
||
968 | shr eax, 9 ; /= 512 |
||
969 | mov [numsectors], eax |
||
970 | |||
9143 | rgimad | 971 | mov eax, [cmdtable] |
972 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.fis_type], FIS_TYPE_REG_H2D |
||
973 | movzx ebx, byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.flags] |
||
974 | bts ebx, bit_AHCI_H2D_FLAG_CMD ; Set Command bit in H2D FIS. |
||
975 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.flags], bl |
||
976 | |||
977 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.command], ATA_CMD_READ_DMA_EX |
||
9162 | rgimad | 978 | cmp [is_write], 1 |
979 | jne @f |
||
980 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.command], ATA_CMD_WRITE_DMA_EX |
||
981 | @@: |
||
9143 | rgimad | 982 | mov ebx, dword [startsector] |
983 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.lba0], bl |
||
984 | shr ebx, 8 |
||
985 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.lba1], bl |
||
986 | shr ebx, 8 |
||
987 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.lba2], bl |
||
988 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.device], 1 shl 6 ; LBA mode |
||
989 | shr ebx, 8 |
||
990 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.lba3], bl |
||
991 | mov ebx, dword [startsector + 4] |
||
992 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.lba4], bl |
||
993 | shr ebx, 8 |
||
994 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.lba5], bl |
||
995 | |||
996 | mov ebx, [numsectors] |
||
997 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.countl], bl |
||
998 | shr ebx, 8 |
||
999 | mov byte [eax + HBA_CMD_TBL.cfis + FIS_REG_H2D.counth], bl |
||
1000 | |||
9272 | rgimad | 1001 | mov edi, [esi + PORT_DATA.port] ; edi - address of HBA_PORT struct of port |
1002 | |||
9143 | rgimad | 1003 | ; Wait on previous command to complete, before issuing new command. |
1004 | stdcall ahci_port_wait, edi, AHCI_PORT_TIMEOUT |
||
1005 | |||
1006 | mov eax, [cmdslot] |
||
1007 | bts [edi + HBA_PORT.command_issue], eax ; Issue the command |
||
1008 | |||
1009 | ; Wait for command completion |
||
1010 | stdcall ahci_port_cmd_wait, edi, eax;, AHCI_PORT_CMD_TIMEOUT |
||
1011 | |||
9162 | rgimad | 1012 | DEBUGF AHCI_DBGLVL, "sata_error register = 0x%x\n", [edi + HBA_PORT.sata_error] |
9143 | rgimad | 1013 | |
9166 | rgimad | 1014 | DEBUGF AHCI_DBGLVL, "R/W completed\n" |
9143 | rgimad | 1015 | |
9145 | rgimad | 1016 | ; xor ecx, ecx |
9162 | rgimad | 1017 | ; mov esi, [vbuf_orig] |
9145 | rgimad | 1018 | ; .print_data: |
1019 | ; cmp ecx, 512 |
||
1020 | ; jae .end_print_data |
||
9143 | rgimad | 1021 | |
9145 | rgimad | 1022 | ; mov al, byte [esi + ecx] |
1023 | ; mov byte [tmpstr], al |
||
1024 | ; mov byte [tmpstr + 1], 0 |
||
1025 | ; DEBUGF 1, "0x%x(%s) ", al:2, tmpstr |
||
9143 | rgimad | 1026 | |
9145 | rgimad | 1027 | ; inc ecx |
1028 | ; jmp .print_data |
||
1029 | ; .end_print_data: |
||
1030 | ; DEBUGF 1, "\n" |
||
9143 | rgimad | 1031 | |
9162 | rgimad | 1032 | popad |
1033 | ;mov eax, [cmdheader] |
||
1034 | ;mov eax, [eax + HBA_CMD_HDR.prdbc] |
||
1035 | mov eax, [numsectors] |
||
1036 | shl eax, 9 ; *= 512 |
||
1037 | ret |
||
9143 | rgimad | 1038 | |
9162 | rgimad | 1039 | .fail: |
9143 | rgimad | 1040 | popad |
1041 | xor eax, eax |
||
1042 | ret |
||
1043 | endp |
||
1044 | tmpstr rb 16 |
||
1045 | |||
9162 | rgimad | 1046 | ; Read sectors |
1047 | ; return value: 0 = success, otherwise = error |
||
1048 | proc ahci_read stdcall pdata: dword, buffer: dword, startsector: qword, numsectors_ptr:dword |
||
1049 | locals |
||
1050 | numsectors dd ? |
||
1051 | endl |
||
1052 | |||
1053 | pushad |
||
1054 | |||
9272 | rgimad | 1055 | mov ecx, [pdata] |
1056 | mov ecx, [ecx + PORT_DATA.ctr_ptr] |
||
1057 | mov ecx, [ecx + AHCI_CTR.mutex] |
||
9162 | rgimad | 1058 | call mutex_lock |
1059 | |||
1060 | mov eax, [numsectors_ptr] |
||
1061 | mov eax, [eax] |
||
1062 | mov [numsectors], eax |
||
1063 | DEBUGF AHCI_DBGLVL, " ahci_read: buffer = 0x%x, startsector = 0x%x:%x, numsectors = %u\n", [buffer], [startsector], [startsector + 4], eax |
||
1064 | |||
1065 | xor ecx, ecx ; how many sectors have been read |
||
1066 | .read_loop: |
||
1067 | cmp ecx, [numsectors] |
||
1068 | jae .read_loop_end |
||
1069 | |||
1070 | ; mov eax, [buffer] |
||
1071 | ; call get_pg_addr |
||
1072 | ; DEBUGF 1, "buf phys = 0x%x\n", eax |
||
1073 | ; mov eax, [buffer] |
||
1074 | ; add eax, 4096 |
||
1075 | ; call get_pg_addr |
||
1076 | ; DEBUGF 1, "buf + 4096 phys = 0x%x\n", eax |
||
1077 | |||
1078 | mov ebx, [numsectors] |
||
1079 | sub ebx, ecx |
||
1080 | ; DEBUGF 1, "buffer = 0x%x\n", [buffer] |
||
1081 | stdcall ahci_rw_sectors, [pdata], [buffer], dword [startsector], dword [startsector + 4], ebx, 0 |
||
1082 | ;; TODO check if eax == 0 ? |
||
1083 | |||
1084 | DEBUGF AHCI_DBGLVL, " EAX = 0x%x\n", eax |
||
1085 | |||
1086 | add [buffer], eax |
||
1087 | shr eax, 9 ; /= 512 |
||
1088 | add ecx, eax |
||
1089 | add dword [startsector], eax |
||
1090 | adc dword [startsector + 4], 0 |
||
1091 | |||
1092 | jmp .read_loop |
||
1093 | .read_loop_end: |
||
1094 | |||
9272 | rgimad | 1095 | mov ecx, [pdata] |
1096 | mov ecx, [ecx + PORT_DATA.ctr_ptr] |
||
1097 | mov ecx, [ecx + AHCI_CTR.mutex] |
||
9162 | rgimad | 1098 | call mutex_unlock |
1099 | |||
1100 | popad |
||
1101 | xor eax, eax |
||
1102 | ret |
||
1103 | endp |
||
1104 | |||
9166 | rgimad | 1105 | ; Write sectors |
1106 | ; return value: 0 = success, otherwise = error |
||
1107 | proc ahci_write stdcall pdata: dword, buffer: dword, startsector: qword, numsectors_ptr:dword |
||
1108 | locals |
||
1109 | numsectors dd ? |
||
1110 | endl |
||
1111 | |||
1112 | pushad |
||
1113 | |||
9272 | rgimad | 1114 | mov ecx, [pdata] |
1115 | mov ecx, [ecx + PORT_DATA.ctr_ptr] |
||
1116 | mov ecx, [ecx + AHCI_CTR.mutex] |
||
9166 | rgimad | 1117 | call mutex_lock |
1118 | |||
1119 | mov eax, [numsectors_ptr] |
||
1120 | mov eax, [eax] |
||
1121 | mov [numsectors], eax |
||
1122 | DEBUGF AHCI_DBGLVL, " ahci_write: buffer = 0x%x, startsector = 0x%x:%x, numsectors = %u\n", [buffer], [startsector], [startsector + 4], eax |
||
1123 | |||
1124 | xor ecx, ecx ; how many sectors have been read |
||
1125 | .write_loop: |
||
1126 | cmp ecx, [numsectors] |
||
1127 | jae .write_loop_end |
||
1128 | |||
1129 | mov ebx, [numsectors] |
||
1130 | sub ebx, ecx |
||
1131 | stdcall ahci_rw_sectors, [pdata], [buffer], dword [startsector], dword [startsector + 4], ebx, 1 |
||
1132 | ;; TODO check if eax == 0 ? |
||
1133 | |||
1134 | DEBUGF AHCI_DBGLVL, " EAX = 0x%x\n", eax |
||
1135 | |||
1136 | add [buffer], eax |
||
1137 | shr eax, 9 ; /= 512 |
||
1138 | add ecx, eax |
||
1139 | add dword [startsector], eax |
||
1140 | adc dword [startsector + 4], 0 |
||
1141 | |||
1142 | jmp .write_loop |
||
1143 | .write_loop_end: |
||
1144 | |||
9272 | rgimad | 1145 | mov ecx, [pdata] |
1146 | mov ecx, [ecx + PORT_DATA.ctr_ptr] |
||
1147 | mov ecx, [ecx + AHCI_CTR.mutex] |
||
9166 | rgimad | 1148 | call mutex_unlock |
1149 | |||
1150 | popad |
||
1151 | xor eax, eax |
||
1152 | ret |
||
1153 | endp |
||
1154 | |||
9065 | rgimad | 1155 | ; Start command engine |
1156 | ; in: eax - address of HBA_PORT structure |
||
9068 | rgimad | 1157 | ahci_start_cmd: |
9065 | rgimad | 1158 | .wait_cr: ; Wait until CR (bit15) is cleared |
1159 | bt [eax + HBA_PORT.command], bit_AHCI_HBA_PxCMD_CR |
||
1160 | jc .wait_cr |
||
1161 | |||
1162 | ; Set FRE (bit4) and ST (bit0) |
||
1163 | bts [eax + HBA_PORT.command], bit_AHCI_HBA_PxCMD_FRE |
||
1164 | bts [eax + HBA_PORT.command], bit_AHCI_HBA_PxCMD_ST |
||
9068 | rgimad | 1165 | ; maybe here call ahci flush cmd ? TODO (see seakernel) |
9065 | rgimad | 1166 | ret |
1167 | |||
1168 | ; Stop command engine |
||
1169 | ; in: eax - address of HBA_PORT structure |
||
9068 | rgimad | 1170 | ahci_stop_cmd: |
9065 | rgimad | 1171 | btr [eax + HBA_PORT.command], bit_AHCI_HBA_PxCMD_ST ; Clear ST (bit0) |
1172 | btr [eax + HBA_PORT.command], bit_AHCI_HBA_PxCMD_FRE ; Clear FRE (bit4) |
||
1173 | .wait_fr_cr: ; Wait until FR (bit14), CR (bit15) are cleared |
||
1174 | bt [eax + HBA_PORT.command], bit_AHCI_HBA_PxCMD_FR |
||
1175 | jc .wait_fr_cr |
||
1176 | bt [eax + HBA_PORT.command], bit_AHCI_HBA_PxCMD_CR |
||
1177 | jc .wait_fr_cr |
||
1178 | |||
1179 | ret |
||
1180 | |||
9139 | rgimad | 1181 | ; waits until the port is no longer busy before issuing a new command |
1182 | ; in: [port] - address of HBA_PORT structure |
||
1183 | ; [timeout] - timeout (in iterations) |
||
1184 | ; out: eax = 0 if success, 1 if timeout expired |
||
1185 | proc ahci_port_wait stdcall, port: dword, timeout: dword |
||
1186 | push ebx ecx |
||
1187 | mov ebx, [port] |
||
1188 | xor ecx, ecx |
||
1189 | .wait: |
||
1190 | cmp ecx, [timeout] |
||
1191 | jae .wait_end |
||
1192 | mov eax, [ebx + HBA_PORT.task_file_data] |
||
1193 | and eax, ATA_DEV_BUSY or ATA_DEV_DRQ |
||
1194 | test eax, eax |
||
1195 | jz .wait_end |
||
1196 | inc ecx |
||
1197 | jmp .wait |
||
1198 | .wait_end: |
||
1199 | xor eax, eax |
||
9162 | rgimad | 1200 | DEBUGF AHCI_DBGLVL, "port wait counter = %u\n", ecx |
9139 | rgimad | 1201 | cmp ecx, [timeout] ; if they equal it means port is hung |
1202 | setz al |
||
1203 | pop ecx ebx |
||
9068 | rgimad | 1204 | ret |
9139 | rgimad | 1205 | endp |
9065 | rgimad | 1206 | |
1207 | |||
9139 | rgimad | 1208 | ; Wait for command completion |
1209 | ; in: [port] - address of HBA_PORT structure |
||
1210 | ; [cmdslot] - number of command slot |
||
1211 | ; out: eax = 0 if success, 1 if error |
||
1212 | proc ahci_port_cmd_wait stdcall, port: dword, cmdslot: dword ;, timeout: dword |
||
1213 | push ebx ecx edx |
||
1214 | mov ebx, [port] |
||
1215 | mov edx, [cmdslot] |
||
1216 | xor eax, eax |
||
1217 | xor ecx, ecx |
||
1218 | .wait: |
||
1219 | bt [ebx + HBA_PORT.command_issue], edx |
||
1220 | jnc .wait_end |
||
1221 | bt [ebx + HBA_PORT.interrupt_status], bit_AHCI_HBA_PxIS_TFES ; check for Task File Error |
||
1222 | jc .error |
||
1223 | inc ecx |
||
1224 | jmp .wait |
||
1225 | .wait_end: |
||
9162 | rgimad | 1226 | DEBUGF AHCI_DBGLVL, "port cmd wait counter = %u\n", ecx |
9139 | rgimad | 1227 | bt [ebx + HBA_PORT.interrupt_status], bit_AHCI_HBA_PxIS_TFES ; check for Task File Error |
1228 | jc .error |
||
1229 | jmp .ret |
||
1230 | .error: |
||
1231 | mov eax, 1 |
||
1232 | .ret: |
||
1233 | pop edx ecx ebx |
||
9068 | rgimad | 1234 | ret |
9139 | rgimad | 1235 | endp |
9065 | rgimad | 1236 | |
9139 | rgimad | 1237 | ; ; The commands may not take effect until the command |
1238 | ; ; register is read again by software, because reasons. |
||
1239 | ; ; in: eax - address of HBA_PORT structure |
||
1240 | ; ; out: eax - command register value |
||
1241 | ; ahci_flush_cmd: |
||
1242 | ; mov eax, [eax + HBA_PORT.command] |
||
1243 | ; ret |
||
1244 | |||
1245 | ; ; Send command to port |
||
1246 | ; ; in: eax - address of HBA_PORT structure |
||
1247 | ; ; ebx - index of command slot |
||
1248 | ; ahci_send_cmd: |
||
1249 | ; push ecx |
||
1250 | ; mov [eax + HBA_PORT.interrupt_status], 0xFFFFFFFF |
||
1251 | |||
1252 | ; mov cl, bl |
||
1253 | ; mov [eax + HBA_PORT.command_issue], 1 |
||
1254 | ; shl [eax + HBA_PORT.command_issue], cl |
||
1255 | |||
1256 | ; call ahci_flush_cmd |
||
1257 | ; pop ecx |
||
1258 | ; ret |
||
1259 | |||
9068 | rgimad | 1260 | ; --------------------------------------------------------------------------- |
1261 | ; in: port - address of HBA_PORT structure |
||
1262 | ; portno - port index (0..31) |
||
1263 | ; pdata - address of PORT_DATA structure |
||
9272 | rgimad | 1264 | ; out: |
1265 | ; rebases port and fills pdata with mappings |
||
9068 | rgimad | 1266 | proc ahci_port_rebase stdcall, port: dword, portno: dword, pdata: dword |
1267 | locals |
||
1268 | phys_page1 dd ? |
||
1269 | virt_page1 dd ? |
||
1270 | phys_page23 dd ? |
||
1271 | virt_page23 dd ? |
||
1272 | tmp dd ? |
||
1273 | endl |
||
1274 | |||
1275 | pushad |
||
1276 | |||
1277 | DEBUGF 1, "Rebasing port %u\n", [portno] |
||
1278 | |||
1279 | mov eax, [port] |
||
1280 | call ahci_stop_cmd |
||
1281 | |||
1282 | ; Command list entry size = 32 |
||
1283 | ; Command list entry maxim count = 32 |
||
1284 | ; Command list maxim size = 32*32 = 1K per port |
||
1285 | call alloc_page |
||
1286 | mov [phys_page1], eax |
||
1287 | |||
1288 | stdcall map_io_mem, eax, 4096, PG_NOCACHE + PG_SWR ; map to virt memory so we can work with it |
||
1289 | mov [virt_page1], eax |
||
1290 | |||
1291 | mov esi, [port] |
||
1292 | mov ebx, [phys_page1] |
||
1293 | mov [esi + HBA_PORT.command_list_base_l], ebx ; set the command list base |
||
1294 | mov [esi + HBA_PORT.command_list_base_h], 0 ; zero upper 32 bits of addr cause we are 32 bit os |
||
1295 | |||
1296 | mov edi, [pdata] |
||
1297 | mov ebx, [virt_page1] |
||
1298 | mov [edi + PORT_DATA.clb], ebx ; set pdata->clb |
||
1299 | |||
1300 | mov eax, [port] |
||
1301 | mov [edi + PORT_DATA.port], eax ; set pdata->port |
||
9074 | rgimad | 1302 | mov eax, [portno] ; set pdata->portno |
1303 | mov [edi + PORT_DATA.portno], eax |
||
9068 | rgimad | 1304 | |
1305 | stdcall _memset, ebx, 0, 1024 ; zero out the command list |
||
1306 | |||
1307 | ; FIS entry size = 256 bytes per port |
||
1308 | mov eax, [phys_page1] |
||
1309 | add eax, 1024 |
||
1310 | mov [esi + HBA_PORT.fis_base_l], eax |
||
1311 | mov [esi + HBA_PORT.fis_base_h], 0 |
||
1312 | |||
1313 | mov eax, [virt_page1] |
||
1314 | add eax, 1024 |
||
1315 | mov [edi + PORT_DATA.fb], eax ; set pdata->fb |
||
1316 | stdcall _memset, eax, 0, 256 ; zero out |
||
1317 | |||
9162 | rgimad | 1318 | stdcall alloc_pages, 32*(64 + 16 + 48 + PRDT_MAX_ENTRIES*16)/4096 |
9068 | rgimad | 1319 | mov [phys_page23], eax |
9162 | rgimad | 1320 | stdcall map_io_mem, eax, 32*(64 + 16 + 48 + PRDT_MAX_ENTRIES*16), PG_NOCACHE + PG_SWR |
9068 | rgimad | 1321 | mov [virt_page23], eax |
1322 | |||
9162 | rgimad | 1323 | ; Command table size = N*32 per port |
9068 | rgimad | 1324 | mov edx, [edi + PORT_DATA.clb] ; cmdheader array base |
1325 | xor ecx, ecx |
||
1326 | |||
1327 | .for1: |
||
1328 | cmp ecx, 32 |
||
1329 | jae .for1_end |
||
1330 | |||
1331 | mov ebx, ecx |
||
1332 | shl ebx, BSF sizeof.HBA_CMD_HDR |
||
1333 | add ebx, edx ; ebx = cmdheader[ecx] |
||
1334 | |||
9162 | rgimad | 1335 | mov [ebx + HBA_CMD_HDR.prdtl], PRDT_MAX_ENTRIES ; prdt entries per command table |
9068 | rgimad | 1336 | |
9162 | rgimad | 1337 | ; bytes per command table = 64+16+48+PRDT_MAX_ENTRIES*16 = N |
9068 | rgimad | 1338 | |
1339 | push edx |
||
1340 | |||
9162 | rgimad | 1341 | ; cmdheader[ecx].ctba = phys_page23 + ecx*N |
9068 | rgimad | 1342 | mov [ebx + HBA_CMD_HDR.ctba], ecx |
9162 | rgimad | 1343 | mov edx, [ebx + HBA_CMD_HDR.ctba] |
1344 | imul edx, (64+16+48+PRDT_MAX_ENTRIES*16) ; *= N |
||
1345 | mov [ebx + HBA_CMD_HDR.ctba], edx |
||
9068 | rgimad | 1346 | mov eax, [ebx + HBA_CMD_HDR.ctba] |
1347 | mov edx, [phys_page23] |
||
1348 | add [ebx + HBA_CMD_HDR.ctba], edx |
||
1349 | |||
1350 | add eax, [virt_page23] |
||
9162 | rgimad | 1351 | mov [tmp], eax ; tmp = virt_page23 + ecx*N |
9069 | rgimad | 1352 | lea eax, [ecx*4 + edi + PORT_DATA.ctba_arr] ; eax = pdata->ctba_arr[ecx] |
9068 | rgimad | 1353 | mov edx, [tmp] |
9162 | rgimad | 1354 | mov [eax], edx ; pdata->ctba_arr[ecx] = virt_page23 + ecx*N |
9068 | rgimad | 1355 | |
1356 | pop edx |
||
1357 | |||
1358 | mov [ebx + HBA_CMD_HDR.ctbau], 0 |
||
9162 | rgimad | 1359 | stdcall _memset, [eax], 0, 64+16+48+PRDT_MAX_ENTRIES*16 ; zero out |
9068 | rgimad | 1360 | |
1361 | inc ecx |
||
1362 | jmp .for1 |
||
1363 | .for1_end: |
||
1364 | |||
1365 | mov eax, [port] |
||
1366 | call ahci_start_cmd |
||
1367 | |||
1368 | DEBUGF 1, "End rebasing port %u\n", [portno] |
||
1369 | popad |
||
1370 | ret |
||
1371 | endp |
||
1372 | |||
9069 | rgimad | 1373 | ; ----------------------------------------------------------- ; TODO check |
1374 | ; Find a free command list slot |
||
9272 | rgimad | 1375 | ; in: pdata - address of HBA_PORT structure |
9069 | rgimad | 1376 | ; out: eax - if not found -1, else slot index |
9272 | rgimad | 1377 | proc ahci_find_cmdslot stdcall, pdata: dword |
9069 | rgimad | 1378 | push ebx ecx edx esi |
9272 | rgimad | 1379 | mov esi, [pdata] |
1380 | mov eax, [esi + PORT_DATA.port] |
||
1381 | |||
9069 | rgimad | 1382 | ; If not set in SACT and CI, the slot is free |
1383 | mov ebx, [eax + HBA_PORT.sata_active] |
||
1384 | or ebx, [eax + HBA_PORT.command_issue] ; ebx = slots |
||
9068 | rgimad | 1385 | |
9272 | rgimad | 1386 | mov esi, [esi + PORT_DATA.ctr_ptr] |
1387 | mov esi, [esi + AHCI_CTR.abar] |
||
9069 | rgimad | 1388 | mov edx, [esi + HBA_MEM.cap] |
1389 | shr edx, 8 |
||
1390 | and edx, 0xf |
||
9134 | rgimad | 1391 | ; DEBUGF 1, "Number of Command Slots on each port = %u\n", edx |
9069 | rgimad | 1392 | xor ecx, ecx |
1393 | .for1: |
||
1394 | cmp ecx, edx |
||
1395 | jae .for1_end |
||
9068 | rgimad | 1396 | |
9069 | rgimad | 1397 | ; if ((slots&1) == 0) return i; |
1398 | bt ebx, 0 |
||
1399 | jc .cont1 |
||
1400 | |||
1401 | mov eax, ecx |
||
1402 | jmp .ret |
||
1403 | |||
1404 | .cont1: |
||
1405 | shr ebx, 1 |
||
1406 | inc ecx |
||
1407 | jmp .for1 |
||
1408 | .for1_end: |
||
1409 | DEBUGF 1, "Cannot find free command list entry\n" |
||
1410 | mov eax, -1 |
||
1411 | .ret: |
||
1412 | pop esi edx ecx ebx |
||
1413 | ret |
||
9272 | rgimad | 1414 | endp |
9069 | rgimad | 1415 | |
1416 | |||
9068 | rgimad | 1417 | proc _memset stdcall, dest:dword, val:byte, cnt:dword ; doesnt clobber any registers |
1418 | ;DEBUGF DBG_INFO, "memset(%x, %u, %u)\n", [dest], [val], [cnt] |
||
1419 | push eax ecx edi |
||
1420 | mov edi, dword [dest] |
||
9184 | rgimad | 1421 | mov al, byte [val] |
9068 | rgimad | 1422 | mov ecx, dword [cnt] |
9069 | rgimad | 1423 | rep stosb |
9068 | rgimad | 1424 | pop edi ecx eax |
1425 | ret |
||
1426 | endp |
||
9138 | rgimad | 1427 | |
1428 | ; Swaps byte order in words |
||
1429 | ; base - address of first word |
||
1430 | ; len - how many words to swap bytes in |
||
1431 | ; doesnt clobber any registers |
||
1432 | proc swap_bytes_in_words stdcall, base: dword, len: dword |
||
1433 | push eax ebx ecx |
||
1434 | xor ecx, ecx |
||
1435 | mov ebx, [base] |
||
1436 | .loop: |
||
1437 | cmp ecx, [len] |
||
1438 | jae .loop_end |
||
1439 | mov ax, word [ebx + ecx*2] |
||
1440 | xchg ah, al |
||
1441 | mov word [ebx + ecx*2], ax |
||
1442 | inc ecx |
||
1443 | jmp .loop |
||
1444 | .loop_end: |
||
1445 | pop ecx ebx eax |
||
1446 | ret |
||
1447 | endp |