Subversion Repositories Kolibri OS

Rev

Rev 9721 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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