Subversion Repositories Kolibri OS

Rev

Rev 9145 | Rev 9166 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

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