Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2012. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  i8254x driver for KolibriOS                                    ;;
  7. ;;                                                                 ;;
  8. ;;  based on i8254x.asm from baremetal os                          ;;
  9. ;;                                                                 ;;
  10. ;;    Written by hidnplayr (hidnplayr@gmail.com)                   ;;
  11. ;;                                                                 ;;
  12. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  13. ;;             Version 2, June 1991                                ;;
  14. ;;                                                                 ;;
  15. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  16.  
  17.         ; TODO: make better use of the available descriptors
  18.  
  19. format MS COFF
  20.  
  21.         API_VERSION             = 0x01000100
  22.         DRIVER_VERSION          = 5
  23.  
  24.         MAX_DEVICES             = 16
  25.  
  26.         DEBUG                   = 1
  27.         __DEBUG__               = 1
  28.         __DEBUG_LEVEL__         = 1
  29.  
  30.         MAX_PKT_SIZE            = 16384         ; Maximum packet size
  31.  
  32.  
  33. include 'proc32.inc'
  34. include 'imports.inc'
  35. include 'fdo.inc'
  36. include '../struct.inc'
  37. include 'netdrv.inc'
  38.  
  39. public START
  40. public service_proc
  41. public version
  42.  
  43.  
  44.  
  45. ; Register list
  46. REG_CTRL                = 0x0000 ; Control Register
  47. REG_STATUS              = 0x0008 ; Device Status Register
  48. REG_CTRLEXT             = 0x0018 ; Extended Control Register
  49. REG_MDIC                = 0x0020 ; MDI Control Register
  50. REG_FCAL                = 0x0028 ; Flow Control Address Low
  51. REG_FCAH                = 0x002C ; Flow Control Address High
  52. REG_FCT                 = 0x0030 ; Flow Control Type
  53. REG_VET                 = 0x0038 ; VLAN Ether Type
  54. REG_ICR                 = 0x00C0 ; Interrupt Cause Read
  55. REG_ITR                 = 0x00C4 ; Interrupt Throttling Register
  56. REG_ICS                 = 0x00C8 ; Interrupt Cause Set Register
  57. REG_IMS                 = 0x00D0 ; Interrupt Mask Set/Read Register
  58. REG_IMC                 = 0x00D8 ; Interrupt Mask Clear Register
  59. REG_RCTL                = 0x0100 ; Receive Control Register
  60. REG_FCTTV               = 0x0170 ; Flow Control Transmit Timer Value
  61. REG_TXCW                = 0x0178 ; Transmit Configuration Word
  62. REG_RXCW                = 0x0180 ; Receive Configuration Word
  63. REG_TCTL                = 0x0400 ; Transmit Control Register
  64. REG_TIPG                = 0x0410 ; Transmit Inter Packet Gap
  65.  
  66. REG_LEDCTL              = 0x0E00 ; LED Control
  67. REG_PBA                 = 0x1000 ; Packet Buffer Allocation
  68.  
  69. REG_RDBAL               = 0x2800 ; RX Descriptor Base Address Low
  70. REG_RDBAH               = 0x2804 ; RX Descriptor Base Address High
  71. REG_RDLEN               = 0x2808 ; RX Descriptor Length
  72. REG_RDH                 = 0x2810 ; RX Descriptor Head
  73. REG_RDT                 = 0x2818 ; RX Descriptor Tail
  74. REG_RDTR                = 0x2820 ; RX Delay Timer Register
  75. REG_RXDCTL              = 0x3828 ; RX Descriptor Control
  76. REG_RADV                = 0x282C ; RX Int. Absolute Delay Timer
  77. REG_RSRPD               = 0x2C00 ; RX Small Packet Detect Interrupt
  78.  
  79. REG_TXDMAC              = 0x3000 ; TX DMA Control
  80. REG_TDBAL               = 0x3800 ; TX Descriptor Base Address Low
  81. REG_TDBAH               = 0x3804 ; TX Descriptor Base Address High
  82. REG_TDLEN               = 0x3808 ; TX Descriptor Length
  83. REG_TDH                 = 0x3810 ; TX Descriptor Head
  84. REG_TDT                 = 0x3818 ; TX Descriptor Tail
  85. REG_TIDV                = 0x3820 ; TX Interrupt Delay Value
  86. REG_TXDCTL              = 0x3828 ; TX Descriptor Control
  87. REG_TADV                = 0x382C ; TX Absolute Interrupt Delay Value
  88. REG_TSPMT               = 0x3830 ; TCP Segmentation Pad & Min Threshold
  89.  
  90. REG_RXCSUM              = 0x5000 ; RX Checksum Control
  91.  
  92. ; Register list for i8254x
  93. I82542_REG_RDTR         = 0x0108 ; RX Delay Timer Register
  94. I82542_REG_RDBAL        = 0x0110 ; RX Descriptor Base Address Low
  95. I82542_REG_RDBAH        = 0x0114 ; RX Descriptor Base Address High
  96. I82542_REG_RDLEN        = 0x0118 ; RX Descriptor Length
  97. I82542_REG_RDH          = 0x0120 ; RDH for i82542
  98. I82542_REG_RDT          = 0x0128 ; RDT for i82542
  99. I82542_REG_TDBAL        = 0x0420 ; TX Descriptor Base Address Low
  100. I82542_REG_TDBAH        = 0x0424 ; TX Descriptor Base Address Low
  101. I82542_REG_TDLEN        = 0x0428 ; TX Descriptor Length
  102. I82542_REG_TDH          = 0x0430 ; TDH for i82542
  103. I82542_REG_TDT          = 0x0438 ; TDT for i82542
  104.  
  105. ; CTRL - Control Register (0x0000)
  106. CTRL_FD                 = 0x00000001 ; Full Duplex
  107. CTRL_LRST               = 0x00000008 ; Link Reset
  108. CTRL_ASDE               = 0x00000020 ; Auto-speed detection
  109. CTRL_SLU                = 0x00000040 ; Set Link Up
  110. CTRL_ILOS               = 0x00000080 ; Invert Loss of Signal
  111. CTRL_SPEED_MASK         = 0x00000300 ; Speed selection
  112. CTRL_SPEED_SHIFT        = 8
  113. CTRL_FRCSPD             = 0x00000800 ; Force Speed
  114. CTRL_FRCDPLX            = 0x00001000 ; Force Duplex
  115. CTRL_SDP0_DATA          = 0x00040000 ; SDP0 data
  116. CTRL_SDP1_DATA          = 0x00080000 ; SDP1 data
  117. CTRL_SDP0_IODIR         = 0x00400000 ; SDP0 direction
  118. CTRL_SDP1_IODIR         = 0x00800000 ; SDP1 direction
  119. CTRL_RST                = 0x04000000 ; Device Reset
  120. CTRL_RFCE               = 0x08000000 ; RX Flow Ctrl Enable
  121. CTRL_TFCE               = 0x10000000 ; TX Flow Ctrl Enable
  122. CTRL_VME                = 0x40000000 ; VLAN Mode Enable
  123. CTRL_PHY_RST            = 0x80000000 ; PHY reset
  124.  
  125. ; STATUS - Device Status Register (0x0008)
  126. STATUS_FD               = 0x00000001 ; Full Duplex
  127. STATUS_LU               = 0x00000002 ; Link Up
  128. STATUS_TXOFF            = 0x00000010 ; Transmit paused
  129. STATUS_TBIMODE          = 0x00000020 ; TBI Mode
  130. STATUS_SPEED_MASK       = 0x000000C0 ; Link Speed setting
  131. STATUS_SPEED_SHIFT      = 6
  132. STATUS_ASDV_MASK        = 0x00000300 ; Auto Speed Detection
  133. STATUS_ASDV_SHIFT       = 8
  134. STATUS_PCI66            = 0x00000800 ; PCI bus speed
  135. STATUS_BUS64            = 0x00001000 ; PCI bus width
  136. STATUS_PCIX_MODE        = 0x00002000 ; PCI-X mode
  137. STATUS_PCIXSPD_MASK     = 0x0000C000 ; PCI-X speed
  138. STATUS_PCIXSPD_SHIFT    = 14
  139.  
  140. ; CTRL_EXT - Extended Device Control Register (0x0018)
  141. CTRLEXT_PHY_INT         = 0x00000020 ; PHY interrupt
  142. CTRLEXT_SDP6_DATA       = 0x00000040 ; SDP6 data
  143. CTRLEXT_SDP7_DATA       = 0x00000080 ; SDP7 data
  144. CTRLEXT_SDP6_IODIR      = 0x00000400 ; SDP6 direction
  145. CTRLEXT_SDP7_IODIR      = 0x00000800 ; SDP7 direction
  146. CTRLEXT_ASDCHK          = 0x00001000 ; Auto-Speed Detect Chk
  147. CTRLEXT_EE_RST          = 0x00002000 ; EEPROM reset
  148. CTRLEXT_SPD_BYPS        = 0x00008000 ; Speed Select Bypass
  149. CTRLEXT_RO_DIS          = 0x00020000 ; Relaxed Ordering Dis.
  150. CTRLEXT_LNKMOD_MASK     = 0x00C00000 ; Link Mode
  151. CTRLEXT_LNKMOD_SHIFT    = 22
  152.  
  153. ; MDIC - MDI Control Register (0x0020)
  154. MDIC_DATA_MASK          = 0x0000FFFF ; Data
  155. MDIC_REG_MASK           = 0x001F0000 ; PHY Register
  156. MDIC_REG_SHIFT          = 16
  157. MDIC_PHY_MASK           = 0x03E00000 ; PHY Address
  158. MDIC_PHY_SHIFT          = 21
  159. MDIC_OP_MASK            = 0x0C000000 ; Opcode
  160. MDIC_OP_SHIFT           = 26
  161. MDIC_R                  = 0x10000000 ; Ready
  162. MDIC_I                  = 0x20000000 ; Interrupt Enable
  163. MDIC_E                  = 0x40000000 ; Error
  164.  
  165. ; ICR - Interrupt Cause Read (0x00c0)
  166. ICR_TXDW                = 0x00000001 ; TX Desc Written back
  167. ICR_TXQE                = 0x00000002 ; TX Queue Empty
  168. ICR_LSC                 = 0x00000004 ; Link Status Change
  169. ICR_RXSEQ               = 0x00000008 ; RX S=  ence Error
  170. ICR_RXDMT0              = 0x00000010 ; RX Desc min threshold reached
  171. ICR_RXO                 = 0x00000040 ; RX Overrun
  172. ICR_RXT0                = 0x00000080 ; RX Timer Interrupt
  173. ICR_MDAC                = 0x00000200 ; MDIO Access Complete
  174. ICR_RXCFG               = 0x00000400
  175. ICR_PHY_INT             = 0x00001000 ; PHY Interrupt
  176. ICR_GPI_SDP6            = 0x00002000 ; GPI on SDP6
  177. ICR_GPI_SDP7            = 0x00004000 ; GPI on SDP7
  178. ICR_TXD_LOW             = 0x00008000 ; TX Desc low threshold hit
  179. ICR_SRPD                = 0x00010000 ; Small RX packet detected
  180.  
  181. ; RCTL - Receive Control Register (0x0100)
  182. RCTL_EN                 = 0x00000002 ; Receiver Enable
  183. RCTL_SBP                = 0x00000004 ; Store Bad Packets
  184. RCTL_UPE                = 0x00000008 ; Unicast Promiscuous Enabled
  185. RCTL_MPE                = 0x00000010 ; Xcast Promiscuous Enabled
  186. RCTL_LPE                = 0x00000020 ; Long Packet Reception Enable
  187. RCTL_LBM_MASK           = 0x000000C0 ; Loopback Mode
  188. RCTL_LBM_SHIFT          = 6
  189. RCTL_RDMTS_MASK         = 0x00000300 ; RX Desc Min Threshold Size
  190. RCTL_RDMTS_SHIFT        = 8
  191. RCTL_MO_MASK            = 0x00003000 ; Multicast Offset
  192. RCTL_MO_SHIFT           = 12
  193. RCTL_BAM                = 0x00008000 ; Broadcast Accept Mode
  194. RCTL_BSIZE_MASK         = 0x00030000 ; RX Buffer Size
  195. RCTL_BSIZE_SHIFT        = 16
  196. RCTL_VFE                = 0x00040000 ; VLAN Filter Enable
  197. RCTL_CFIEN              = 0x00080000 ; CFI Enable
  198. RCTL_CFI                = 0x00100000 ; Canonical Form Indicator Bit
  199. RCTL_DPF                = 0x00400000 ; Discard Pause Frames
  200. RCTL_PMCF               = 0x00800000 ; Pass MAC Control Frames
  201. RCTL_BSEX               = 0x02000000 ; Buffer Size Extension
  202. RCTL_SECRC              = 0x04000000 ; Strip Ethernet CRC
  203.  
  204. ; TCTL - Transmit Control Register (0x0400)
  205. TCTL_EN                 = 0x00000002 ; Transmit Enable
  206. TCTL_PSP                = 0x00000008 ; Pad short packets
  207. TCTL_SWXOFF             = 0x00400000 ; Software XOFF Transmission
  208.  
  209. ; PBA - Packet Buffer Allocation (0x1000)
  210. PBA_RXA_MASK            = 0x0000FFFF ; RX Packet Buffer
  211. PBA_RXA_SHIFT           = 0
  212. PBA_TXA_MASK            = 0xFFFF0000 ; TX Packet Buffer
  213. PBA_TXA_SHIFT           = 16
  214.  
  215. ; Flow Control Type
  216. FCT_TYPE_DEFAULT        = 0x8808
  217.  
  218. ; === TX Descriptor fields ===
  219.  
  220. ; TX Packet Length (word 2)
  221. TXDESC_LEN_MASK         = 0x0000ffff
  222.  
  223. ; TX Descriptor CMD field (word 2)
  224. TXDESC_IDE              = 0x80000000 ; Interrupt Delay Enable
  225. TXDESC_VLE              = 0x40000000 ; VLAN Packet Enable
  226. TXDESC_DEXT             = 0x20000000 ; Extension
  227. TXDESC_RPS              = 0x10000000 ; Report Packet Sent
  228. TXDESC_RS               = 0x08000000 ; Report Status
  229. TXDESC_IC               = 0x04000000 ; Insert Checksum
  230. TXDESC_IFCS             = 0x02000000 ; Insert FCS
  231. TXDESC_EOP              = 0x01000000 ; End Of Packet
  232.  
  233. ; TX Descriptor STA field (word 3)
  234. TXDESC_TU               = 0x00000008 ; Transmit Underrun
  235. TXDESC_LC               = 0x00000004 ; Late Collision
  236. TXDESC_EC               = 0x00000002 ; Excess Collisions
  237. TXDESC_DD               = 0x00000001 ; Descriptor Done
  238.  
  239. ; === RX Descriptor fields ===
  240.  
  241. ; RX Packet Length (word 2)
  242. RXDESC_LEN_MASK         = 0x0000ffff
  243.  
  244. ; RX Descriptor STA field (word 3)
  245. RXDESC_PIF              = 0x00000080 ; Passed In-exact Filter
  246. RXDESC_IPCS             = 0x00000040 ; IP cksum calculated
  247. RXDESC_TCPCS            = 0x00000020 ; TCP cksum calculated
  248. RXDESC_VP               = 0x00000008 ; Packet is 802.1Q
  249. RXDESC_IXSM             = 0x00000004 ; Ignore cksum indication
  250. RXDESC_EOP              = 0x00000002 ; End Of Packet
  251. RXDESC_DD               = 0x00000001 ; Descriptor Done
  252.  
  253.  
  254. virtual at ebx
  255.         device:
  256.         ETH_DEVICE
  257.  
  258.         .mmio_addr      dd ?
  259.         .pci_bus        dd ?
  260.         .pci_dev        dd ?
  261.         .irq_line       db ?
  262.  
  263.                         rb 0x100 - (($ - device) and 0xff)
  264.         .rx_desc        rd 256/8
  265.  
  266.                         rb 0x100 - (($ - device) and 0xff)
  267.         .tx_desc        rd 256/8
  268.  
  269.         sizeof.device_struct = $ - device
  270.  
  271. end virtual
  272.  
  273. section '.flat' code readable align 16
  274.  
  275.  
  276. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  277. ;;                        ;;
  278. ;; proc START             ;;
  279. ;;                        ;;
  280. ;; (standard driver proc) ;;
  281. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  282.  
  283. align 4
  284. proc START stdcall, state:dword
  285.  
  286.         cmp [state], 1
  287.         jne .exit
  288.  
  289.   .entry:
  290.  
  291.         DEBUGF  2,"Loading %s driver\n", my_service
  292.         stdcall RegService, my_service, service_proc
  293.         ret
  294.  
  295.   .fail:
  296.   .exit:
  297.         xor eax, eax
  298.         ret
  299.  
  300. endp
  301.  
  302.  
  303.  
  304.  
  305. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  306. ;;                        ;;
  307. ;; proc SERVICE_PROC      ;;
  308. ;;                        ;;
  309. ;; (standard driver proc) ;;
  310. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  311.  
  312. align 4
  313. proc service_proc stdcall, ioctl:dword
  314.  
  315.         mov     edx, [ioctl]
  316.         mov     eax, [IOCTL.io_code]
  317.  
  318. ;------------------------------------------------------
  319.  
  320.         cmp     eax, 0 ;SRV_GETVERSION
  321.         jne     @F
  322.  
  323.         cmp     [IOCTL.out_size], 4
  324.         jl      .fail
  325.         mov     eax, [IOCTL.output]
  326.         mov     [eax], dword API_VERSION
  327.  
  328.         xor     eax, eax
  329.         ret
  330.  
  331. ;------------------------------------------------------
  332.   @@:
  333.         cmp     eax, 1 ;SRV_HOOK
  334.         jne     .fail
  335.  
  336.         cmp     [IOCTL.inp_size], 3                     ; Data input must be at least 3 bytes
  337.         jl      .fail
  338.  
  339.         mov     eax, [IOCTL.input]
  340.         cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
  341.         jne     .fail                                   ; other types arent supported for this card yet
  342.  
  343. ; check if the device is already listed
  344.  
  345.         mov     esi, device_list
  346.         mov     ecx, [devices]
  347.         test    ecx, ecx
  348.         jz      .firstdevice
  349.  
  350. ;        mov     eax, [IOCTL.input]                      ; get the pci bus and device numbers
  351.         mov     ax , [eax+1]                            ;
  352.   .nextdevice:
  353.         mov     ebx, [esi]
  354.         cmp     ax , word [device.pci_bus]              ; compare with pci and device num in device list (notice the usage of word instead of byte)
  355.         je      .find_devicenum                         ; Device is already loaded, let's find it's device number
  356.         add     esi, 4
  357.         loop    .nextdevice
  358.  
  359.  
  360. ; This device doesnt have its own eth_device structure yet, lets create one
  361.   .firstdevice:
  362.         cmp     [devices], MAX_DEVICES                  ; First check if the driver can handle one more card
  363.         jge     .fail
  364.  
  365.         allocate_and_clear ebx, sizeof.device_struct, .fail      ; Allocate the buffer for device structure
  366.  
  367. ; Fill in the direct call addresses into the struct
  368.  
  369.         mov     [device.reset], reset
  370.         mov     [device.transmit], transmit
  371.         mov     [device.get_MAC], read_mac
  372.         mov     [device.set_MAC], .fail
  373.         mov     [device.unload], unload
  374.         mov     [device.name], my_service
  375.  
  376. ; save the pci bus and device numbers
  377.  
  378.         mov     eax, [IOCTL.input]
  379.         movzx   ecx, byte [eax+1]
  380.         mov     [device.pci_bus], ecx
  381.         movzx   ecx, byte [eax+2]
  382.         mov     [device.pci_dev], ecx
  383.  
  384. ; Now, it's time to find the base mmio addres of the PCI device
  385.  
  386.         find_mmio32 [device.pci_bus], [device.pci_dev], [device.mmio_addr]
  387.  
  388. ; Create virtual mapping of the physical memory
  389.  
  390.         push    1Bh             ; PG_SW+PG_NOCACHE
  391.         push    10000h          ; size of the map
  392.         push    eax
  393.         call    MapIoMem
  394.         mov     [device.mmio_addr], eax
  395.  
  396. ; We've found the mmio address, find IRQ now
  397.  
  398.         find_irq byte [device.pci_bus], byte [device.pci_dev], [device.irq_line]
  399.  
  400.         DEBUGF  1,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
  401.         [device.pci_dev]:1,[device.pci_bus]:1,[device.irq_line]:1,[device.mmio_addr]:8
  402.  
  403. ; Ok, the eth_device structure is ready, let's probe the device
  404.         call    probe                                                   ; this function will output in eax
  405.         test    eax, eax
  406.         jnz     .err                                                    ; If an error occured, exit
  407.  
  408.         mov     eax, [devices]                                          ; Add the device structure to our device list
  409.         mov     [device_list+4*eax], ebx                                ; (IRQ handler uses this list to find device)
  410.         inc     [devices]                                               ;
  411.  
  412.         mov     [device.type], NET_TYPE_ETH
  413.         call    NetRegDev
  414.  
  415.         cmp     eax, -1
  416.         je      .destroy
  417.  
  418.         ret
  419.  
  420. ; If the device was already loaded, find the device number and return it in eax
  421.  
  422.   .find_devicenum:
  423.         DEBUGF  1,"Trying to find device number of already registered device\n"
  424.         call    NetPtrToNum                                             ; This kernel procedure converts a pointer to device struct in ebx
  425.                                                                         ; into a device number in edi
  426.         mov     eax, edi                                                ; Application wants it in eax instead
  427.         DEBUGF  1,"Kernel says: %u\n", eax
  428.         ret
  429.  
  430. ; If an error occured, remove all allocated data and exit (returning -1 in eax)
  431.  
  432.   .destroy:
  433.         ; todo: reset device into virgin state
  434.  
  435.   .err:
  436.         stdcall KernelFree, ebx
  437.  
  438.   .fail:
  439.         or      eax, -1
  440.         ret
  441.  
  442. ;------------------------------------------------------
  443. endp
  444.  
  445.  
  446. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  447. ;;                                                                        ;;
  448. ;;        Actual Hardware dependent code starts here                      ;;
  449. ;;                                                                        ;;
  450. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  451.  
  452.  
  453. align 4
  454. unload:
  455.         ; TODO: (in this particular order)
  456.         ;
  457.         ; - Stop the device
  458.         ; - Detach int handler
  459.         ; - Remove device from local list (device_list)
  460.         ; - call unregister function in kernel
  461.         ; - Remove all allocated structures and buffers the card used
  462.  
  463.         or      eax, -1
  464.  
  465. ret
  466.  
  467.  
  468.  
  469. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  470. ;;
  471. ;;  probe: enables the device (if it really is I8254X)
  472. ;;
  473. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  474. align 4
  475. probe:
  476.  
  477.         DEBUGF  1,"Probe\n"
  478.  
  479.         make_bus_master byte [device.pci_bus], byte [device.pci_dev]
  480.  
  481.         ; TODO: validate the device
  482.  
  483.         call    read_mac
  484.  
  485.         movzx   eax, [device.irq_line]
  486.         DEBUGF  1,"Attaching int handler to irq %x\n", eax:1
  487.         stdcall AttachIntHandler, eax, int_handler, dword 0
  488.         test    eax, eax
  489.         jnz     @f
  490.         DEBUGF  1,"\nCould not attach int handler!\n"
  491. ;        or      eax, -1
  492. ;        ret
  493.   @@:
  494.  
  495.  
  496. reset:
  497.         DEBUGF  1,"Reset\n"
  498.  
  499.         mov     esi, [device.mmio_addr]
  500.  
  501.         mov     dword [esi + REG_IMC], 0xffffffff       ; Disable all interrupt causes
  502.         mov     eax, dword [esi + REG_ICR]              ; Clear any pending interrupts
  503.         mov     dword [esi + REG_ITR], 0                ; Disable interrupt throttling logic
  504.  
  505.         mov     dword [esi + REG_PBA], 0x00000030       ; PBA: set the RX buffer size to 48KB (TX buffer is calculated as 64-RX buffer)
  506.         mov     dword [esi + REG_RDTR], 0               ; RDTR: set no delay
  507.  
  508.         mov     dword [esi + REG_TXCW], 0x08008060      ; TXCW: set ANE, TxConfigWord (Half/Full duplex, Next Page Reqest)
  509.  
  510.         mov     eax, [esi + REG_CTRL]
  511.         or      eax, 1 shl 6 + 1 shl 5
  512.         and     eax, not (1 shl 3 + 1 shl 7 + 1 shl 30 + 1 shl 31)
  513.         mov     dword [esi + REG_CTRL], eax             ; CTRL: clear LRST, set SLU and ASDE, clear RSTPHY, VME, and ILOS
  514.  
  515.         lea     edi, [esi + 0x5200]                     ; MTA: reset
  516.         mov     eax, 0xffffffff
  517.         stosd
  518.         stosd
  519.         stosd
  520.         stosd
  521.  
  522.         stdcall KernelAlloc, 48*1024
  523.         mov     dword [device.rx_desc + 16], eax
  524.         GetRealAddr
  525.         mov     dword [device.rx_desc], eax
  526.         mov     dword [device.rx_desc + 4], 0
  527.  
  528.         lea     eax, [device.rx_desc]
  529.         GetRealAddr
  530.         mov     dword [esi + REG_RDBAL], eax            ; Receive Descriptor Base Address Low
  531.         mov     dword [esi + REG_RDBAH], 0              ; Receive Descriptor Base Address High
  532.         mov     dword [esi + REG_RDLEN], (1 * 128)      ; Receive Descriptor Length
  533.         mov     dword [esi + REG_RDH], 0                ; Receive Descriptor Head
  534.         mov     dword [esi + REG_RDT], 1                ; Receive Descriptor Tail
  535.         mov     dword [esi + REG_RCTL], 0x04008006      ; Receiver Enable, Store Bad Packets, Broadcast Accept Mode, Strip Ethernet CRC from incoming packet
  536.  
  537.         mov     dword [device.tx_desc], 0
  538.         mov     dword [device.tx_desc + 4], 0
  539.         mov     dword [device.tx_desc + 16], 0
  540.  
  541.         lea     eax, [device.tx_desc]
  542.         GetRealAddr
  543.         mov     dword [esi + REG_TDBAL], eax            ; Transmit Descriptor Base Address Low
  544.         mov     dword [esi + REG_TDBAH], 0              ; Transmit Descriptor Base Address High
  545.         mov     dword [esi + REG_TDLEN], (1 * 128)      ; Transmit Descriptor Length
  546.         mov     dword [esi + REG_TDH], 0                ; Transmit Descriptor Head
  547.         mov     dword [esi + REG_TDT], 0                ; Transmit Descriptor Tail
  548.         mov     dword [esi + REG_TCTL], 0x010400fa      ; Enabled, Pad Short Packets, 15 retrys, 64-byte COLD, Re-transmit on Late Collision
  549.         mov     dword [esi + REG_TIPG], 0x0060200A      ; IPGT 10, IPGR1 8, IPGR2 6
  550.  
  551.         xor     eax, eax
  552.         mov     [esi + REG_RDTR], eax                   ; Clear the Receive Delay Timer Register
  553.         mov     [esi + REG_RADV], eax                   ; Clear the Receive Interrupt Absolute Delay Timer
  554.         mov     [esi + REG_RSRPD], eax                  ; Clear the Receive Small Packet Detect Interrupt
  555.         or      eax, 1 shl 0 + 1 shl 7                  ; TXDW + RXT0
  556.         mov     eax, -1 ;;;; hack!
  557.         mov     [esi + REG_IMS], eax                    ; Enable interrupt types
  558.  
  559.         mov     [device.mtu], 1514
  560.  
  561.         xor     eax, eax
  562.         ret
  563.  
  564.  
  565.  
  566. align 4
  567. read_mac:
  568.  
  569.         DEBUGF  1,"Read MAC\n"
  570.  
  571.         mov     esi, [device.mmio_addr]
  572.  
  573.         mov     eax, [esi+0x5400]                       ; RAL
  574.         test    eax, eax
  575.         jz      .try_eeprom
  576.  
  577.         mov     dword [device.mac], eax
  578.         mov     eax, [esi+0x5404]                       ; RAH
  579.         mov     word [device.mac+4], ax
  580.  
  581.         jmp     .mac_ok
  582.  
  583.   .try_eeprom:
  584.         mov     dword [esi+0x14], 0x00000001
  585.         mov     eax, [esi+0x14]
  586.         shr     eax, 16
  587.         mov     word [device.mac], ax
  588.  
  589.         mov     dword [esi+0x14], 0x00000101
  590.         mov     eax, [esi+0x14]
  591.         shr     eax, 16
  592.         mov     word [device.mac+2], ax
  593.  
  594.         mov     dword [esi+0x14], 0x00000201
  595.         mov     eax, [esi+0x14]
  596.         shr     eax, 16
  597.         mov     word [device.mac+4], ax
  598.  
  599.   .mac_ok:
  600.         DEBUGF  1,"MAC = %x-%x-%x-%x-%x-%x\n",\
  601.         [device.mac+0]:2,[device.mac+1]:2,[device.mac+2]:2,[device.mac+3]:2,[device.mac+4]:2,[device.mac+5]:2
  602.  
  603.         ret
  604.  
  605.  
  606. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  607. ;;                                         ;;
  608. ;; Transmit                                ;;
  609. ;;                                         ;;
  610. ;; In: buffer pointer in [esp+4]           ;;
  611. ;;     size of buffer in [esp+8]           ;;
  612. ;;     pointer to device structure in ebx  ;;
  613. ;;                                         ;;
  614. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  615. align 4
  616. transmit:
  617.         DEBUGF  2,"\nTransmitting packet, buffer:%x, size:%u\n", [esp+4], [esp+8]
  618.         mov     eax, [esp+4]
  619.         DEBUGF  2,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
  620.         [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
  621.         [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
  622.         [eax+13]:2,[eax+12]:2
  623.  
  624.         cmp     dword [esp + 8], 1514
  625.         jg      .fail
  626.         cmp     dword [esp + 8], 60
  627.         jl      .fail
  628.  
  629. ; Program the descriptor (use legacy mode)
  630.         lea     edi, [device.tx_desc]                   ; Transmit Descriptor Base Address
  631.         mov     dword [edi + 16], eax                   ; Store the data location (for driver)
  632.         GetRealAddr                                     ;
  633.         mov     dword [edi], eax                        ; Real addr (for i8254x)
  634.         mov     dword [edi + 4], 0x00000000             ;
  635.  
  636.         mov     ecx, [esp + 8]
  637.         or      ecx, 1 shl 24 + 1 shl 25 + 1 shl 27     ; EOP + IFCS + RS
  638.         mov     dword [edi + 8], ecx                    ; Packet size
  639.         mov     dword [edi + 12], 0x00000000
  640.  
  641. ; Tell i8254x wich descriptor(s) we programmed
  642.         mov     edi, [device.mmio_addr]
  643.         mov     dword [edi + REG_TDH], 0                ; TDH - Transmit Descriptor Head
  644.         mov     dword [edi + REG_TDT], 1                ; TDT - Transmit Descriptor Tail
  645.  
  646. ; Update stats
  647.         inc     [device.packets_tx]
  648.         mov     eax, [esp + 8]
  649.         add     dword [device.bytes_tx], eax
  650.         adc     dword [device.bytes_tx + 4], 0
  651.  
  652.         ret     8
  653.  
  654.   .fail:
  655.         DEBUGF  1,"Send failed\n"
  656.         ret     8
  657.  
  658.  
  659. ;;;;;;;;;;;;;;;;;;;;;;;
  660. ;;                   ;;
  661. ;; Interrupt handler ;;
  662. ;;                   ;;
  663. ;;;;;;;;;;;;;;;;;;;;;;;
  664. align 4
  665. int_handler:
  666.         DEBUGF  2,"\nIRQ %x\n", eax:2   ; no, you cant replace 'eax:2' with 'al', this must be a bug in FDO
  667.  
  668. ;-------------------------------------------
  669. ; Find pointer of device wich made IRQ occur
  670.  
  671.         mov     esi, device_list
  672.         mov     ecx, [devices]
  673.         test    ecx, ecx
  674.         jz      .fail
  675.   .nextdevice:
  676.         mov     ebx, dword [esi]
  677.  
  678.         mov     edi, [device.mmio_addr]
  679.         test    edi, edi
  680.         jz      .nextdevice
  681.  
  682. ;--------------------
  683. ; Get interrupt cause
  684.  
  685.         mov     eax, [edi + REG_ICR]
  686.         DEBUGF  2,"interrupt cause=%x\n", eax
  687.  
  688. ;---------
  689. ; RX done?
  690.  
  691.         test    eax, ICR_RXDMT0
  692.         jz      .no_rx
  693.  
  694.         push    eax
  695.         push    .retaddr
  696.  
  697.         movzx   ecx, word [device.rx_desc + 8]          ; Get the packet length
  698.         DEBUGF  2,"got %u bytes\n", ecx
  699.         push    ecx
  700.         push    [device.rx_desc + 16]                   ; Get packet pointer
  701.  
  702. ; Update stats
  703.         add     dword [device.bytes_rx], ecx
  704.         adc     dword [device.bytes_rx + 4], 0
  705.         inc     dword [device.packets_rx]
  706.  
  707. ; allocate new descriptor
  708.         stdcall KernelAlloc, 48*1024
  709.         mov     dword [device.rx_desc + 16], eax
  710.         GetRealAddr
  711.         mov     dword [device.rx_desc], eax
  712.  
  713. ; reset descriptor status
  714.         mov     esi, [device.mmio_addr]
  715.         mov     dword [esi + REG_RDH], 0x00000000       ; Receive Descriptor Head
  716.         mov     dword [esi + REG_RDT], 0x00000001       ; Receive Descriptor Tail
  717.  
  718.         jmp     EthReceiver
  719.   .retaddr:
  720.         pop     eax
  721.  
  722.   .no_rx:
  723.  
  724. ;--------------
  725. ; Link Changed?
  726.  
  727.         test    eax, ICR_LSC
  728.         jz      .no_link
  729.  
  730.         DEBUGF  2,"Link Changed\n"
  731.  
  732.   .no_link:
  733.  
  734. ;---------------
  735. ; Transmit done?
  736.  
  737.         test    eax, ICR_TXDW
  738.         jz      .no_tx
  739.  
  740.         DEBUGF  2,"Transmit done\n"
  741.         ; TODO: clear up used buffers
  742.  
  743.   .no_tx:
  744.   .fail:
  745.         ret
  746.  
  747.  
  748.  
  749.  
  750. ; End of code
  751.  
  752. section '.data' data readable writable align 16
  753. align 4
  754.  
  755. devices         dd 0
  756. version         dd (DRIVER_VERSION shl 16) or (API_VERSION and 0xFFFF)
  757. my_service      db 'I8254X',0                    ; max 16 chars include zero
  758.  
  759. include_debug_strings                           ; All data wich FDO uses will be included here
  760.  
  761. device_list     rd MAX_DEVICES                  ; This list contains all pointers to device structures the driver is handling
  762.  
  763.  
  764.