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-2008. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;; DEC 21x4x driver for KolibriOS                                  ;;
  7. ;;                                                                 ;;
  8. ;;  Based on dec21140.Asm from Solar OS by                         ;;
  9. ;;     Eugen Brasoveanu,                                           ;;
  10. ;;       Ontanu Bogdan Valentin                                    ;;
  11. ;;                                                                 ;;
  12. ;;    Written by hidnplayr@kolibrios.org                           ;;
  13. ;;                                                                 ;;
  14. ;;     0.1 - 5 june 2010                                           ;;
  15. ;;                                                                 ;;
  16. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  17. ;;             Version 2, June 1991                                ;;
  18. ;;                                                                 ;;
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20.  
  21. format MS COFF
  22.  
  23.         API_VERSION             equ 0x01000100
  24.  
  25.         DEBUG                   equ 1
  26.         __DEBUG__               equ 1
  27.         __DEBUG_LEVEL__         equ 1
  28.  
  29. include 'proc32.inc'
  30. include 'imports.inc'
  31. include 'fdo.inc'
  32. include 'netdrv.inc'
  33.  
  34. OS_BASE         equ 0
  35. new_app_base    equ 0x60400000
  36. PROC_BASE       equ OS_BASE+0x0080000
  37.  
  38. public START
  39. public service_proc
  40. public version
  41.  
  42.  
  43. virtual at ebx
  44.  
  45.         device:
  46.  
  47.         ETH_DEVICE
  48.  
  49. ; device specific
  50.       .rx_p_des         dd ?  ; descriptors ring with received packets
  51.       .tx_p_des         dd ?  ; descriptors ring with 'to transmit' packets
  52.       .tx_free_des      dd ?  ; Tx descriptors available
  53.       .tx_wr_des        dd ?  ; Tx current descriptor to write data to
  54.       .tx_rd_des        dd ?  ; Tx current descriptor to read TX completion
  55.       .rx_crt_des       dd ?  ; Rx current descriptor
  56.  
  57.       .io_addr          dd ?
  58.       .pci_bus          db ?
  59.       .pci_dev          db ?
  60.       .irq_line         db ?
  61.  
  62.       .size = $ - device
  63.  
  64. end virtual
  65.  
  66.  
  67.  
  68. MAX_DEVICES     equ 16
  69.  
  70. ;-------------------------------------------
  71. ; configuration registers
  72. ;-------------------------------------------
  73. CFCS    equ     4       ; configuration and status register
  74.  
  75. CSR0    equ     0x00     ; Bus mode
  76. CSR1    equ     0x08     ; Transmit Poll Command
  77. CSR2    equ     0x10     ; Receive Poll Command
  78. CSR3    equ     0x18     ; Receive list base address
  79. CSR4    equ     0x20     ; Transmit list base address
  80. CSR5    equ     0x28     ; Status
  81. CSR6    equ     0x30     ; Operation mode
  82. CSR7    equ     0x38     ; Interrupt enable
  83. CSR8    equ     0x40     ; Missed frames and overflow counter
  84. CSR9    equ     0x48     ; Boot ROM, serial ROM, and MII management
  85. CSR10   equ     0x50     ; Boot ROM programming address
  86. ;CSR11 General-purpose timer 0x58
  87. CSR12   equ     0x60     ; General-purpose port
  88. CSR13   equ     0x68
  89. ;CSR14 Reserved 0x70
  90. ;CSR15 Watchdog timer 0x78
  91.  
  92. ;--------bits/commands of CSR0-------------------
  93. CSR0_RESET              equ     1b
  94.  
  95. CSR0_WIE                equ     1 SHL 24        ; Write and Invalidate Enable
  96. CSR0_RLE                equ     1 SHL 23        ; PCI Read Line Enable
  97. CSR0_RML                equ     1 SHL 21        ; PCI Read Multiple
  98.  
  99. CSR0_CACHEALIGN_NONE    equ     00b SHL 14
  100. CSR0_CACHEALIGN_32      equ     01b SHL 14
  101. CSR0_CACHEALIGN_64      equ     10b SHL 14
  102. CSR0_CACHEALIGN_128     equ     11b SHL 14
  103.  
  104. ; using values from linux driver.. :P
  105. CSR0_DEFAULT            equ     CSR0_WIE+CSR0_RLE+CSR0_RML+CSR0_CACHEALIGN_NONE ;32
  106.  
  107. ;------- CSR5 -STATUS- bits --------------------------------
  108. CSR5_TI                 equ     1 SHL 0         ; Transmit interupt - frame transmition completed
  109. CSR5_TPS                equ     1 SHL 1         ; Transmit process stopped
  110. CSR5_TU                 equ     1 SHL 2         ; Transmit Buffer unavailable
  111. CSR5_TJT                equ     1 SHL 3         ; Transmit Jabber Timeout (transmitter had been excessively active)
  112. CSR5_UNF                equ     1 SHL 5         ; Transmit underflow - FIFO underflow
  113. CSR5_RI                 equ     1 SHL 6         ; Receive Interrupt
  114. CSR5_RU                 equ     1 SHL 7         ; Receive Buffer unavailable
  115. CSR5_RPS                equ     1 SHL 8         ; Receive Process stopped
  116. CSR5_RWT                equ     1 SHL 9         ; Receive Watchdow Timeout
  117. CSR5_ETI                equ     1 SHL 10        ; Early transmit Interrupt
  118. CSR5_GTE                equ     1 SHL 11        ; General Purpose Timer Expired
  119. CSR5_FBE                equ     1 SHL 13        ; Fatal bus error
  120. CSR5_ERI                equ     1 SHL 14        ; Early receive Interrupt
  121. CSR5_AIS                equ     1 SHL 15        ; Abnormal interrupt summary
  122. CSR5_NIS                equ     1 SHL 16        ; normal interrupt summary
  123. CSR5_RS_SH              equ     1 SHL 17        ; Receive process state  -shift
  124. CSR5_RS_MASK            equ     111b            ;                        -mask
  125. CSR5_TS_SH              equ     1 SHL 20        ; Transmit process state -shift
  126. CSR5_TS_MASK            equ     111b            ;                        -mask
  127. CSR5_EB_SH              equ     1 SHL 23        ; Error bits             -shift
  128. CSR5_EB_MASK            equ     111b            ; Error bits             -mask
  129.  
  130. ;CSR5 TS values
  131. CSR5_TS_STOPPED                equ     000b
  132. CSR5_TS_RUNNING_FETCHING_DESC  equ     001b
  133. CSR5_TS_RUNNING_WAITING_TX     equ     010b
  134. CSR5_TS_RUNNING_READING_BUFF   equ     011b
  135. CSR5_TS_RUNNING_SETUP_PCKT     equ     101b
  136. CSR5_TS_SUSPENDED              equ     110b
  137. CSR5_TS_RUNNING_CLOSING_DESC   equ     111b
  138.  
  139. ;------- CSR6 -OPERATION MODE- bits --------------------------------
  140. CSR6_HP                 equ     1 SHL 0         ; Hash/Perfect Receive Filtering mode
  141. CSR6_SR                 equ     1 SHL 1         ; Start/Stop receive
  142. CSR6_HO                 equ     1 SHL 2         ; Hash only Filtering mode
  143. CSR6_PB                 equ     1 SHL 3         ; Pass bad frames
  144. CSR6_IF                 equ     1 SHL 4         ; Inverse filtering
  145. CSR6_SB                 equ     1 SHL 5         ; Start/Stop backoff counter
  146. CSR6_PR                 equ     1 SHL 6         ; Promiscuos mode -default after reset
  147. CSR6_PM                 equ     1 SHL 7         ; Pass all multicast
  148. CSR6_F                  equ     1 SHL 9         ; Full Duplex mode
  149. CSR6_OM_SH              equ     1 SHL 10        ; Operating Mode -shift
  150. CSR6_OM_MASK            equ     11b             ;                -mask
  151. CSR6_FC                 equ     1 SHL 12        ; Force Collision Mode
  152. CSR6_ST                 equ     1 SHL 13        ; Start/Stop Transmission Command
  153. CSR6_TR_SH              equ     1 SHL 14        ; Threshold Control      -shift
  154. CSR6_TR_MASK            equ     11b             ;                        -mask
  155. CSR6_CA                 equ     1 SHL 17        ; Capture Effect Enable
  156. CSR6_PS                 equ     1 SHL 18        ; Port select SRL / MII/SYM
  157. CSR6_HBD                equ     1 SHL 19        ; Heartbeat Disable
  158. CSR6_SF                 equ     1 SHL 21        ; Store and Forward -transmit full packet only
  159. CSR6_TTM                equ     1 SHL 22        ; Transmit Threshold Mode -
  160. CSR6_PCS                equ     1 SHL 23        ; PCS active and MII/SYM port operates in symbol mode
  161. CSR6_SCR                equ     1 SHL 24        ; Scrambler Mode
  162. CSR6_MBO                equ     1 SHL 25        ; Must Be One
  163. CSR6_RA                 equ     1 SHL 30        ; Receive All
  164. CSR6_SC                 equ     1 SHL 31        ; Special Capture Effect Enable
  165.  
  166.  
  167. ;------- CSR7 -INTERRUPT ENABLE- bits --------------------------------
  168. CSR7_TI                 equ     1 SHL 0         ; transmit Interrupt Enable (set with CSR7<16> & CSR5<0> )
  169. CSR7_TS                 equ     1 SHL 1         ; transmit Stopped Enable (set with CSR7<15> & CSR5<1> )
  170. CSR7_TU                 equ     1 SHL 2         ; transmit buffer underrun Enable (set with CSR7<16> & CSR5<2> )
  171. CSR7_TJ                 equ     1 SHL 3         ; transmit jabber timeout enable (set with CSR7<15> & CSR5<3> )
  172. CSR7_UN                 equ     1 SHL 5         ; underflow Interrupt enable (set with CSR7<15> & CSR5<5> )
  173. CSR7_RI                 equ     1 SHL 6         ; receive Interrupt enable (set with CSR7<16> & CSR5<5> )
  174. CSR7_RU                 equ     1 SHL 7         ; receive buffer unavailable enable (set with CSR7<15> & CSR5<7> )
  175. CSR7_RS                 equ     1 SHL 8         ; Receive stopped enable (set with CSR7<15> & CSR5<8> )
  176. CSR7_RW                 equ     1 SHL 9         ; receive watchdog timeout enable (set with CSR7<15> & CSR5<9> )
  177. CSR7_ETE                equ     1 SHL 10        ; Early transmit Interrupt enable (set with CSR7<15> & CSR5<10> )
  178. CSR7_GPT                equ     1 SHL 11        ; general purpose timer enable (set with CSR7<15> & CSR5<11> )
  179. CSR7_FBE                equ     1 SHL 13        ; Fatal bus error enable (set with CSR7<15> & CSR5<13> )
  180. CSR7_ERE                equ     1 SHL 14        ; Early receive enable (set with CSR7<16> & CSR5<14> )
  181. CSR7_AI                 equ     1 SHL 15        ; Abnormal Interrupt Summary Enable (enables CSR5<0,3,7,8,9,10,13>)
  182. CSR7_NI                 equ     1 SHL 16        ; Normal Interrup Enable (enables CSR5<0,2,6,11,14>)
  183. CSR7_DEFAULT            equ     CSR7_TI+CSR7_TS+CSR7_RI+CSR7_RS+CSR7_TU+CSR7_TJ+CSR7_UN+\
  184.                                         CSR7_RU+CSR7_RW+CSR7_FBE+CSR7_AI+CSR7_NI
  185.  
  186. ;----------- descriptor structure ---------------------
  187. struc DES {
  188.         .DES0    DD      ?       ; bit 31 is 'own' and rest is 'status'
  189.         .DES1    DD      ?       ; control bits + bytes-count buffer 1 + bytes-count buffer 2
  190.         .DES2    DD      ?       ; pointer to buffer1
  191.         .DES3    DD      ?       ; pointer to buffer2 or in this case to next descriptor, as we use a chained structure
  192.         .realaddr dd ?
  193.         .size = 64
  194. }
  195.  
  196. virtual at 0
  197.         DES DES
  198. end virtual
  199.  
  200. ;common to Rx and Tx
  201. DES0_OWN        equ     1 SHL 31        ; if set, the NIC controls the descriptor, otherwise driver 'owns' the descriptors
  202.  
  203. ;receive
  204. RDES0_ZER       equ     1 SHL 0         ; must be 0 if legal length :D
  205. RDES0_CE        equ     1 SHL 1         ; CRC error, valid only on last desc (RDES0<8>=1)
  206. RDES0_DB        equ     1 SHL 2         ; dribbling bit - not multiple of 8 bits, valid only on last desc (RDES0<8>=1)
  207. RDES0_RE        equ     1 SHL 3         ; Report on MII error.. i dont realy know what this means :P
  208. RDES0_RW        equ     1 SHL 4         ; received watchdog timer expiration - must set CSR5<9>, valid only on last desc (RDES0<8>=1)
  209. RDES0_FT        equ     1 SHL 5         ; frame type: 0->IEEE802.0 (len<1500) 1-> ETHERNET frame (len>1500), valid only on last desc (RDES0<8>=1)
  210. RDES0_CS        equ     1 SHL 6         ; Collision seen, valid only on last desc (RDES0<8>=1)
  211. RDES0_TL        equ     1 SHL 7         ; Too long(>1518)-NOT AN ERROR, valid only on last desc (RDES0<8>=1)
  212. RDES0_LS        equ     1 SHL 8         ; Last descriptor of current frame
  213. RDES0_FS        equ     1 SHL 9         ; First descriptor of current frame
  214. RDES0_MF        equ     1 SHL 10        ; Multicast frame, valid only on last desc (RDES0<8>=1)
  215. RDES0_RF        equ     1 SHL 11        ; Runt frame, valid only on last desc (RDES0<8>=1) and id overflow
  216. RDES0_DT_SERIAL equ     00b SHL 12      ; Data type-Serial recv frame, valid only on last desc (RDES0<8>=1)
  217. RDES0_DT_INTERNAL equ   01b SHL 12      ; Data type-Internal loopback recv frame, valid only on last desc (RDES0<8>=1)
  218. RDES0_DT_EXTERNAL equ   11b SHL 12      ; Data type-External loopback recv frame, valid only on last desc (RDES0<8>=1)
  219. RDES0_DE        equ     1 SHL 14        ; Descriptor error - cant own a new desc and frame doesnt fit, valid only on last desc (RDES0<8>=1)
  220. RDES0_ES        equ     1 SHL 15        ; Error Summmary - bits 1+6+11+14, valid only on last desc (RDES0<8>=1)
  221. RDES0_FL_SH     equ     16              ; Field length shift, valid only on last desc (RDES0<8>=1)
  222. RDES0_FL_MASK   equ     11111111111111b ; Field length mask (+CRC), valid only on last desc (RDES0<8>=1)
  223. RDES0_FF        equ     1 SHL 30        ; Filtering fail-frame failed address recognition test(must CSR6<30>=1), valid only on last desc (RDES0<8>=1)
  224.  
  225. RDES1_RBS1_MASK equ     11111111111b    ; firsd buffer size MASK
  226. RDES1_RBS2_SH   equ     1 SHL 11        ; second buffer size SHIFT
  227. RDES1_RBS2_MASK equ     11111111111b    ; second buffer size MASK
  228. RDES1_RCH       equ     1 SHL 24        ; Second address chained - second address (buffer) is next desc address
  229. RDES1_RER       equ     1 SHL 25        ; Receive End of Ring - final descriptor, NIC must return to first desc
  230.  
  231. ;transmition
  232. TDES0_DE        equ     1 SHL 0         ; Deffered
  233. TDES0_UF        equ     1 SHL 1         ; Underflow error
  234. TDES0_LF        equ     1 SHL 2         ; Link fail report (only if CSR6<23>=1)
  235. TDES0_CC_SH     equ     3               ; Collision Count shift - no of collision before transmition
  236. TDES0_CC_MASK   equ     1111b           ; Collision Count mask
  237. TDES0_HF        equ     1 SHL 7         ; Heartbeat fail
  238. TDES0_EC        equ     1 SHL 8         ; Excessive Collisions - >16 collisions
  239. TDES0_LC        equ     1 SHL 9         ; Late collision
  240. TDES0_NC        equ     1 SHL 10        ; No carrier
  241. TDES0_LO        equ     1 SHL 11        ; Loss of carrier
  242. TDES0_TO        equ     1 SHL 14        ; Transmit Jabber Timeout
  243. TDES0_ES        equ     1 SHL 15        ; Error summary TDES0<1+8+9+10+11+14>=1
  244.  
  245. TDES1_TBS1_MASK equ     11111111111b    ; Buffer 1 size mask
  246. TDES1_TBS2_SH   equ     11              ; Buffer 2 size shift
  247. TDES1_TBS2_MASK equ     11111111111b    ; Buffer 2 size mask
  248. TDES1_FT0       equ     1 SHL 22        ; Filtering type 0
  249. TDES1_DPD       equ     1 SHL 23        ; Disabled padding for packets <64bytes, no padding
  250. TDES1_TCH       equ     1 SHL 24        ; Second address chained - second buffer pointer is to next desc
  251. TDES1_TER       equ     1 SHL 25        ; Transmit end of ring - final descriptor
  252. TDES1_AC        equ     1 SHL 26        ; Add CRC disable -pretty obvious
  253. TDES1_SET       equ     1 SHL 27        ; Setup packet
  254. TDES1_FT1       equ     1 SHL 28        ; Filtering type 1
  255. TDES1_FS        equ     1 SHL 29        ; First segment - buffer is first segment of frame
  256. TDES1_LS        equ     1 SHL 30        ; Last segment
  257. TDES1_IC        equ     1 SHL 31        ; Interupt on completion (CSR5<0>=1) valid when TDES1<30>=1
  258.  
  259. MAX_ETH_FRAME_SIZE      equ     1514
  260.  
  261. RX_DES_COUNT            equ     4              ; no of RX descriptors, must be power of 2
  262. RX_BUFF_SIZE            equ     2048            ; size of buffer for each descriptor, must be multiple of 4 and <= 2048 TDES1_TBS1_MASK
  263. TX_DES_COUNT            equ     4              ; no of TX descriptors, must be power of 2
  264. TX_BUFF_SIZE            equ     2048            ; size of buffer for each descriptor, used for memory allocation only
  265.  
  266. RX_MEM_TOTAL_SIZE       equ     RX_DES_COUNT*(DES.size+RX_BUFF_SIZE)
  267. TX_MEM_TOTAL_SIZE       equ     TX_DES_COUNT*(DES.size+TX_BUFF_SIZE)
  268.  
  269. ;=============================================================================
  270. ; serial ROM operations
  271. ;=============================================================================
  272. CSR9_SR                 equ     1 SHL 11        ; SROM Select
  273. CSR9_RD                 equ     1 SHL 14        ; ROM Read Operation
  274. CSR9_SROM_DO            equ     1 SHL 3         ; Data Out for SROM
  275. CSR9_SROM_DI            equ     1 SHL 2         ; Data In to SROM
  276. CSR9_SROM_CK            equ     1 SHL 1         ; clock for SROM
  277. CSR9_SROM_CS            equ     1 SHL 0         ; chip select.. always needed
  278.  
  279. ; assume dx is CSR9 (SROM port)
  280. macro SROM_Delay {
  281.         push    eax
  282.         in      eax, dx
  283.         in      eax, dx
  284.         in      eax, dx
  285.         in      eax, dx
  286.         in      eax, dx
  287.         in      eax, dx
  288.         in      eax, dx
  289.         in      eax, dx
  290.         in      eax, dx
  291.         in      eax, dx
  292.         pop     eax
  293. }
  294.  
  295. macro Bit_Set a_bit {
  296.         in      eax, dx
  297.         or      eax, a_bit
  298.         out     dx , eax
  299. }
  300.  
  301. macro Bit_Clear a_bit {
  302.         in      eax, dx
  303.         and     eax, NOT (a_bit)
  304.         out     dx , eax
  305. }
  306.  
  307.  
  308. section '.flat' code readable align 16
  309.  
  310. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  311. ;;                        ;;
  312. ;; proc START             ;;
  313. ;;                        ;;
  314. ;; (standard driver proc) ;;
  315. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  316.  
  317. align 4
  318. proc START stdcall, state:dword
  319.  
  320.         cmp [state], 1
  321.         jne .exit
  322.  
  323.   .entry:
  324.  
  325.         DEBUGF  2,"Loading dec21x4x driver\n"
  326.         stdcall RegService, my_service, service_proc
  327.         ret
  328.  
  329.   .fail:
  330.   .exit:
  331.         xor eax, eax
  332.         ret
  333.  
  334. endp
  335.  
  336.  
  337. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  338. ;;                        ;;
  339. ;; proc SERVICE_PROC      ;;
  340. ;;                        ;;
  341. ;; (standard driver proc) ;;
  342. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  343.  
  344. align 4
  345. proc service_proc stdcall, ioctl:dword
  346.  
  347.         mov     edx, [ioctl]
  348.         mov     eax, [IOCTL.io_code]
  349.  
  350. ;------------------------------------------------------
  351.  
  352.         cmp     eax, 0 ;SRV_GETVERSION
  353.         jne     @F
  354.  
  355.         cmp     [IOCTL.out_size], 4
  356.         jl      .fail
  357.         mov     eax, [IOCTL.output]
  358.         mov     [eax], dword API_VERSION
  359.  
  360.         xor     eax, eax
  361.         ret
  362.  
  363. ;------------------------------------------------------
  364.   @@:
  365.         cmp     eax, 1 ;SRV_HOOK
  366.         jne     .fail
  367.  
  368.         cmp     [IOCTL.inp_size], 3                     ; Data input must be at least 3 bytes
  369.         jl      .fail
  370.  
  371.         mov     eax, [IOCTL.input]
  372.         cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
  373.         jne     .fail                                   ; other types arent supported for this card yet
  374.  
  375. ; check if the device is already listed
  376.  
  377.         mov     esi, DEVICE_LIST
  378.         mov     ecx, [devices]
  379.         test    ecx, ecx
  380.         jz      .firstdevice
  381.  
  382. ;        mov     eax, [IOCTL.input]                     ; get the pci bus and device numbers
  383.         mov     ax , [eax+1]                            ;
  384.   .nextdevice:
  385.         mov     ebx, [esi]
  386.         cmp     ax , word [device.pci_bus]              ; compare with pci and device num in device list (notice the usage of word instead of byte)
  387.         je      .find_devicenum                         ; Device is already loaded, let's find it's device number
  388.         add     esi, 4
  389.         loop    .nextdevice
  390.  
  391.  
  392. ; This device doesnt have its own eth_device structure yet, lets create one
  393.   .firstdevice:
  394.         cmp     [devices], MAX_DEVICES                  ; First check if the driver can handle one more card
  395.         jge     .fail
  396.  
  397.         push    edx
  398.         stdcall KernelAlloc, dword device.size          ; Allocate the buffer for eth_device structure
  399.         pop     edx
  400.         test    eax, eax
  401.         jz      .fail
  402.         mov     ebx, eax                                ; ebx is always used as a pointer to the structure (in driver, but also in kernel code)
  403.  
  404. ; Fill in the direct call addresses into the struct
  405.  
  406.         mov     [device.reset], reset
  407.         mov     [device.transmit], transmit
  408.         mov     [device.get_MAC], read_mac
  409.         mov     [device.set_MAC], write_mac
  410.         mov     [device.unload], unload
  411.         mov     [device.name], my_service
  412.  
  413. ; save the pci bus and device numbers
  414.  
  415.         mov     eax, [IOCTL.input]
  416.         mov     cl , [eax+1]
  417.         mov     [device.pci_bus], cl
  418.         mov     cl , [eax+2]
  419.         mov     [device.pci_dev], cl
  420.  
  421. ; Now, it's time to find the base io addres of the PCI device
  422.  
  423.         find_io [device.pci_bus], [device.pci_dev], [device.io_addr]
  424.  
  425. ; We've found the io address, find IRQ now
  426.  
  427.         find_irq [device.pci_bus], [device.pci_dev], [device.irq_line]
  428.  
  429.         DEBUGF  2,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
  430.         [device.pci_dev]:1,[device.pci_bus]:1,[device.irq_line]:1,[device.io_addr]:8
  431.  
  432.         allocate_and_clear [device.rx_p_des], RX_DES_COUNT*(DES.size+RX_BUFF_SIZE), .err
  433.         allocate_and_clear [device.tx_p_des], TX_DES_COUNT*(DES.size+TX_BUFF_SIZE), .err
  434.  
  435. ; Ok, the eth_device structure is ready, let's probe the device
  436. ; Because initialization fires IRQ, IRQ handler must be aware of this device
  437.         mov     eax, [devices]                                          ; Add the device structure to our device list
  438.         mov     [DEVICE_LIST+4*eax], ebx                                ; (IRQ handler uses this list to find device)
  439.         inc     [devices]                                               ;
  440.  
  441.         call    probe                                                   ; this function will output in eax
  442.         test    eax, eax
  443.         jnz     .err2                                                   ; If an error occured, exit
  444.  
  445.         call    EthRegDev
  446.         cmp     eax, -1
  447.         je      .destroy
  448.  
  449.         ret
  450.  
  451. ; If the device was already loaded, find the device number and return it in eax
  452.  
  453.   .find_devicenum:
  454.         DEBUGF  2,"Trying to find device number of already registered device\n"
  455.         mov     ebx, eax
  456.         call    EthStruc2Dev                                            ; This kernel procedure converts a pointer to device struct in ebx
  457.                                                                         ; into a device number in edi
  458.         mov     eax, edi                                                ; Application wants it in eax instead
  459.         DEBUGF  2,"Kernel says: %u\n", eax
  460.         ret
  461.  
  462. ; If an error occured, remove all allocated data and exit (returning -1 in eax)
  463.  
  464.   .destroy:
  465.         ; todo: reset device into virgin state
  466.  
  467.   .err2:
  468.         dec     [devices]
  469.   .err:
  470.         DEBUGF  2,"removing device structure\n"
  471.         stdcall KernelFree, [device.rx_p_des]
  472.         stdcall KernelFree, [device.tx_p_des]
  473.         stdcall KernelFree, ebx
  474.  
  475.  
  476.   .fail:
  477.         or      eax, -1
  478.         ret
  479.  
  480. ;------------------------------------------------------
  481. endp
  482.  
  483.  
  484. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  485. ;;                                                                        ;;
  486. ;;        Actual Hardware dependent code starts here                      ;;
  487. ;;                                                                        ;;
  488. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  489.  
  490.  
  491.  
  492. align 4
  493. unload:
  494.         ; TODO: (in this particular order)
  495.         ;
  496.         ; - Stop the device
  497.         ; - Detach int handler
  498.         ; - Remove device from local list (RTL8139_LIST)
  499.         ; - call unregister function in kernel
  500.         ; - Remove all allocated structures and buffers the card used
  501.  
  502.         or      eax,-1
  503.  
  504. ret
  505.  
  506.  
  507. macro status {
  508.         set_io  CSR5
  509.         in      eax, dx
  510.         DEBUGF  1,"CSR5: %x\n", eax
  511. }
  512.  
  513.  
  514. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  515. ;;                                         ;;
  516. ;; Probe                                   ;;
  517. ;;                                         ;;
  518. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  519.  
  520. align 4
  521. probe:
  522.  
  523.         DEBUGF  2,"Probing dec21x4x device: "
  524.  
  525.         make_bus_master [device.pci_bus], [device.pci_dev]
  526.  
  527.         movzx   eax, [device.pci_bus]
  528.         movzx   ecx, [device.pci_dev]
  529.         stdcall PciRead32, eax ,ecx ,0                                ; get device/vendor id
  530.  
  531.         DEBUGF  1,"Vendor id: 0x%x\n", ax
  532.  
  533.         cmp     ax , 0x1011
  534.         jne     .notfound
  535.         shr     eax, 16
  536.  
  537.         DEBUGF  1,"Vendor ok!, device id: 0x%x\n", ax                 ; TODO: use another method to detect chip!
  538.  
  539.         cmp     ax , 0x0009
  540.         je      .supported_device
  541.  
  542.         cmp     ax , 0x0019
  543.         je      .supported_device2
  544.  
  545.   .notfound:
  546.         DEBUGF  1,"Device not supported!\n"
  547.         or      eax, -1
  548.         ret
  549.  
  550.   .supported_device2:
  551.  
  552.         ; wake up the 21143
  553.  
  554.         movzx   ecx, [device.pci_bus]
  555.         movzx   edx, [device.pci_dev]
  556.         xor     eax, eax
  557.         stdcall PciWrite32, ecx, edx, 0x40, eax
  558.  
  559.  
  560.   .supported_device:
  561.         call    SROM_GetWidth           ; TODO: use this value returned in ecx
  562.                                         ; in the read_word routine!
  563.  
  564. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  565. ;;                                         ;;
  566. ;; Reset                                   ;;
  567. ;;                                         ;;
  568. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  569.  
  570. align 4
  571. reset:
  572.  
  573.         DEBUGF  2,"Resetting dec21x4x\n"
  574.  
  575. ;-----------------------------------------------------------
  576. ; board software reset - if fails, dont do nothing else
  577.  
  578.         set_io  0
  579.         status
  580.         set_io  CSR0
  581.         mov     eax, CSR0_RESET
  582.         out     dx , eax
  583.  
  584. ; wait at least 50 PCI cycles
  585.         mov     esi, 1000
  586.         call    Sleep
  587.  
  588. ;-----------
  589. ; setup CSR0
  590.  
  591.         set_io  0
  592.         status
  593.         set_io  CSR0
  594.         mov     eax, CSR0_DEFAULT
  595.         out     dx , eax
  596.  
  597.  
  598. ; wait at least 50 PCI cycles
  599.         mov     esi, 1000
  600.         call    Sleep
  601.  
  602. ;-----------------------------------
  603. ; Read mac from eeprom to driver ram
  604.  
  605.         call    read_mac_eeprom
  606.  
  607. ;--------------------------------
  608. ; insert irq handler on given irq
  609.  
  610.         movzx   eax, [device.irq_line]
  611.         DEBUGF  1,"Attaching int handler to irq %x\n", eax:1
  612.         stdcall AttachIntHandler, eax, int_handler, dword 0
  613.         test    eax, eax
  614.         jnz     @f
  615.         DEBUGF  1,"\nCould not attach int handler!\n"
  616. ;        or      eax, -1
  617. ;        ret
  618.   @@:
  619.  
  620.         set_io  0
  621.         status
  622.  
  623. ;------------------------------------------
  624. ; Setup RX descriptors (use chained method)
  625.  
  626.         mov     eax, [device.rx_p_des]
  627.         call    GetPgAddr
  628.         mov     edx, eax
  629.         lea     esi, [eax + RX_DES_COUNT*(DES.size)]    ; jump over RX descriptors
  630.  
  631.         mov     eax, [device.rx_p_des]
  632.         add     eax, RX_DES_COUNT*(DES.size)            ; jump over RX descriptors
  633.  
  634.         mov     edi, [device.rx_p_des]
  635.         mov     ecx, RX_DES_COUNT
  636.  
  637.         push    edx                    ;;
  638.   .loop_rx_des:
  639.         add     edx, DES.size
  640.         mov     [edi+DES.DES0], DES0_OWN                        ; hardware owns buffer
  641.         mov     [edi+DES.DES1], 1984+RDES1_RCH                  ; only size of first buffer, chained buffers
  642.         mov     [edi+DES.DES2], esi                             ; hw buffer address
  643.         mov     [edi+DES.DES3], edx                             ; pointer to next descriptor
  644.         mov     [edi+DES.realaddr], eax                         ; virtual buffer address
  645.  
  646.         DEBUGF  1,"RX desc %u, buff addr: %x, next desc: %x, real buff addr: %x, real descr addr: %x \n", ecx, esi, edx, eax, edi
  647.  
  648.         add     esi, RX_BUFF_SIZE
  649.         add     eax, RX_BUFF_SIZE
  650.         add     edi, DES.size
  651.         dec     ecx
  652.         jnz     .loop_rx_des
  653.  
  654. ; set last descriptor as LAST
  655.         sub     edi, DES.size
  656.         or      [edi+DES.DES1], RDES1_RER       ; EndOfRing
  657.         pop     edx                     ;;
  658.         mov     [edi+DES.DES3], edx     ;;
  659.  
  660. ;---------------------
  661. ; Setup TX descriptors
  662.  
  663.         mov     eax, [device.tx_p_des]
  664.         call    GetPgAddr
  665.         mov     edx, eax
  666.         lea     esi, [eax + TX_DES_COUNT*(DES.size)]    ; jump over TX descriptors
  667.  
  668.         mov     eax, [device.tx_p_des]
  669.         add     eax, TX_DES_COUNT*(DES.size)    ; jump over TX descriptors
  670.  
  671.         mov     edi, [device.tx_p_des]
  672.         mov     ecx, TX_DES_COUNT
  673.  
  674.         push    edx                    ;;
  675.   .loop_tx_des:
  676.         add     edx, DES.size
  677.         mov     [edi+DES.DES0], 0               ; owned by driver
  678.         mov     [edi+DES.DES1], TDES1_TCH       ; chained method
  679.         mov     [edi+DES.DES2], esi             ; pointer to buffer
  680.         mov     [edi+DES.DES3], edx             ; pointer to next descr
  681.         mov     [edi+DES.realaddr], eax
  682.  
  683.         DEBUGF  1,"TX desc %u, buff addr: %x, next desc: %x, real buff addr: %x, real descr addr: %x \n", ecx, esi, edx, eax, edi
  684.  
  685.         add     esi, TX_BUFF_SIZE
  686.         add     eax, TX_BUFF_SIZE
  687.         add     edi, DES.size
  688.         dec     ecx
  689.         jnz     .loop_tx_des
  690.        
  691. ; set last descriptor as LAST
  692.         sub     edi, DES.size
  693.         or      [edi+DES.DES1], TDES1_TER       ; EndOfRing
  694.         pop     edx                            ;;;
  695.         mov     [edi+DES.DES3], edx            ;;;
  696.  
  697. ;------------------
  698. ; Reset descriptors
  699.  
  700.         mov     [device.tx_wr_des], 0
  701.         mov     [device.tx_rd_des], 0
  702.         mov     [device.rx_crt_des], 0
  703.         mov     [device.tx_free_des], TX_DES_COUNT
  704.  
  705. ;--------------------------------------------
  706. ; setup CSR3 & CSR4 (pointers to descriptors)
  707.  
  708.         set_io  0
  709.         status
  710.         set_io  CSR3
  711.         mov     eax, [device.rx_p_des]
  712.         call    GetPgAddr
  713.         DEBUGF  1,"RX descriptor base address: %x\n", eax
  714.         out     dx , eax
  715.  
  716.         set_io  CSR4
  717.         mov     eax, [device.tx_p_des]
  718.         call    GetPgAddr
  719.         DEBUGF  1,"TX descriptor base address: %x\n", eax
  720.         out     dx , eax
  721.  
  722. ;-------------------------------------------------------
  723. ; setup interrupt mask register -expect IRQs from now on
  724.  
  725.         status
  726.         DEBUGF  1,"Enabling interrupts\n"
  727.         set_io  CSR7
  728.         mov     eax, CSR7_DEFAULT
  729.         out     dx , eax
  730.         status
  731.  
  732. ;----------
  733. ; enable RX
  734.  
  735.         set_io  0
  736.         status
  737.         DEBUGF  1,"Enable RX\n"
  738.  
  739.         set_io  CSR6
  740.         Bit_Set CSR6_SR; or CSR6_PR or CSR6_ST
  741.         DEBUGF  1,"CSR6: %x\n", eax
  742.  
  743.         status
  744.  
  745.  
  746. ; wait a bit
  747.         mov     esi, 3000
  748.         call    Sleep
  749.  
  750. ;----------------------------------------------------
  751. ; send setup packet to notify the board about the MAC
  752.  
  753.         call    Send_Setup_Packet
  754.  
  755.         set_io  0
  756.         status
  757.  
  758.         DEBUGF  1,"Reset done\n"
  759.         xor     eax, eax
  760.  
  761.         ret
  762.  
  763. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  764. ;;                                         ;;
  765. ;; Send setup packet                       ;;
  766. ;;                                         ;;
  767. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  768.  
  769. align 4
  770. Send_Setup_Packet:
  771.  
  772.         DEBUGF  1,"Sending setup packet\n"
  773.  
  774. ; if no descriptors available, out
  775.         mov     ecx, 1000
  776. @@loop_wait_desc:
  777.         cmp     [device.tx_free_des], 0
  778.         jne     @f
  779.  
  780.         dec     ecx
  781.         jnz     @@loop_wait_desc
  782.  
  783.         mov     eax, -1
  784.         ret
  785.       @@:
  786.  
  787. ; go to current send descriptor
  788.         mov     edi, [device.tx_p_des]
  789.         mov     eax, [device.tx_wr_des]
  790.         DEBUGF  1,"Got free descriptor: %u (%x)", eax, edi
  791.         mov     edx, DES.size
  792.         mul     edx
  793.         add     edi, eax
  794.         DEBUGF  1,"=>%x\n",  edi
  795.  
  796. ; if NOT sending FIRST setup packet, must set current descriptor to 0 size for both buffers,
  797. ;  and go to next descriptor for real setup packet...            ;; TODO: check if 2 descriptors are available
  798.  
  799. ;       cmp     [device.tx_packets], 0
  800. ;       je      .not_first
  801. ;              
  802. ;       and     [edi+DES.des1], 0
  803. ;       mov     [edi+DES.des0], DES0_OWN
  804. ;              
  805. ; go to next descriptor
  806. ;        inc     [device.tx_wr_des]
  807. ;        and     [device.tx_wr_des], TX_DES_COUNT-1
  808. ;
  809. ; dec free descriptors count
  810. ;        cmp     [device.tx_free_des], 0
  811. ;        jz      @f
  812. ;        dec     [device.tx_free_des]
  813. ;       @@:
  814. ;
  815. ;       ; recompute pointer to current descriptor
  816. ;       mov     edi, [device.tx_p_des]
  817. ;       mov     eax, [device.tx_wr_des]
  818. ;       mov     edx, DES.size
  819. ;       mul     edx
  820. ;       add     edi, eax
  821.  
  822.   .not_first:
  823.  
  824.         push    edi
  825. ; copy setup packet to current descriptor
  826.         mov     edi, [edi+DES.realaddr]
  827. ; copy once the address
  828.         lea     esi, [device.mac]
  829.         DEBUGF  1,"copying packet to %x from %x\n", edi, esi
  830.         mov     ecx, 3  ; mac is 6 bytes thus 3 words
  831.      .loop:
  832.         DEBUGF  1,"%x ", [esi]:4
  833.         movsw
  834.         dec     esi
  835.         dec     esi
  836.         movsw
  837.         dec     ecx
  838.         jnz     .loop
  839.  
  840.         DEBUGF  1,"\n"
  841.  
  842. ; copy 15 times the broadcast address
  843.         mov     ecx, 3*15
  844.         mov     eax, 0xffffffff
  845.         rep     stosd
  846.  
  847.         pop     edi
  848.  
  849. ; setup descriptor
  850.         DEBUGF  1,"setting up descriptor\n"
  851.         mov     [edi+DES.DES1], TDES1_IC+TDES1_SET+TDES1_TCH+192        ; size must be EXACTLY 192 bytes
  852.         mov     [edi+DES.DES0], DES0_OWN
  853.  
  854.         DEBUGF  1,"TDES0: %x\n", [edi+DES.DES0]:8
  855.         DEBUGF  1,"TDES1: %x\n", [edi+DES.DES1]:8
  856.         DEBUGF  1,"TDES2: %x\n", [edi+DES.DES2]:8
  857.         DEBUGF  1,"TDES3: %x\n", [edi+DES.DES3]:8
  858.  
  859. ; go to next descriptor
  860.         inc     [device.tx_wr_des]
  861.         and     [device.tx_wr_des], TX_DES_COUNT-1
  862.  
  863. ; dec free descriptors count
  864.         cmp     [device.tx_free_des], 0
  865.         jz      @f
  866.         dec     [device.tx_free_des]
  867.        @@:
  868.  
  869.         wbinvd;;;
  870.  
  871. ; start tx
  872.         set_io  0
  873.         status
  874.         set_io  CSR6
  875.         in      eax, dx
  876.         test    eax, CSR6_ST            ; if NOT started, start now
  877.         jnz     .already_started
  878.         or      eax, CSR6_ST
  879.         DEBUGF  1,"Starting TX\n"
  880.         jmp     .do_it
  881.   .already_started:
  882.                                         ; if already started, issue a Transmit Poll command
  883.         set_io  CSR1
  884.         mov     eax, 0
  885.         DEBUGF  1,"Issuing transmit poll command\n"
  886.   .do_it:
  887.         out     dx , eax
  888.         status
  889.  
  890.         DEBUGF  1,"Sending setup packet, completed!\n"
  891.  
  892.         ret
  893.  
  894.  
  895.  
  896. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  897. ;;                                         ;;
  898. ;; Transmit                                ;;
  899. ;;                                         ;;
  900. ;; In: buffer pointer in [esp+4]           ;;
  901. ;;     size of buffer in [esp+8]           ;;
  902. ;;     pointer to device structure in ebx  ;;
  903. ;;                                         ;;
  904. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  905.  
  906. align 4
  907. transmit:
  908.  
  909.         DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n",[esp+4],[esp+8]
  910.         mov     eax, [esp+4]
  911.         DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
  912.         [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
  913.         [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
  914.         [eax+13]:2,[eax+12]:2
  915.  
  916.         cmp     dword [esp+8], MAX_ETH_FRAME_SIZE
  917.         ja      .fail
  918.  
  919.         cmp     [device.tx_free_des], 0
  920.         je      .fail
  921.  
  922. ;--------------------------
  923. ; copy packet to crt buffer
  924.        
  925.         mov     eax, [device.tx_wr_des]
  926.         mov     edx, DES.size
  927.         mul     edx
  928.         add     eax, [device.tx_p_des]
  929.         mov     edi, [eax+DES.realaddr]                 ; pointer to buffer
  930.         mov     esi, [esp+4]
  931.         mov     ecx, [esp+8]
  932.         DEBUGF  1,"copying %u bytes from %x to %x\n", ecx, esi, edi
  933.         rep     movsb
  934.  
  935. ; set packet size
  936.         mov     ecx, [eax+DES.DES1]
  937.         and     ecx, TDES1_TER                          ; preserve 'End of Ring' bit
  938.         or      ecx, [esp+8]                            ; set size
  939.         or      ecx, TDES1_FS or TDES1_LS or TDES1_IC or TDES1_TCH    ; first descr, last descr, interrupt on complete, chained modus
  940.         mov     [eax+DES.DES1], ecx
  941.  
  942. ; set descriptor info
  943.         mov     [eax+DES.DES0], DES0_OWN                ; say it is now owned by the 21x4x
  944.  
  945.         DEBUGF  1,"TDES0: %x\n", [eax+DES.DES0]:8
  946.         DEBUGF  1,"TDES1: %x\n", [eax+DES.DES1]:8
  947.         DEBUGF  1,"TDES2: %x\n", [eax+DES.DES2]:8
  948.         DEBUGF  1,"TDES3: %x\n", [eax+DES.DES3]:8
  949.  
  950.  
  951. ; start tx
  952.         set_io  0
  953.         status
  954.         set_io  CSR6
  955.         in      eax, dx
  956.         test    eax, CSR6_ST            ; if NOT started, start now
  957.         jnz     .already_started
  958.         or      eax, CSR6_ST
  959.         DEBUGF  1,"Starting TX\n"
  960.         jmp     .do_it
  961.   .already_started:
  962.                                         ; if already started, issues a Transmit Poll command
  963.         set_io  CSR1
  964.         mov     eax, -1
  965.         DEBUGF  1,"Sending transmit poll command\n"
  966.   .do_it:
  967.         out     dx , eax
  968.         status
  969.  
  970.  
  971.         wbinvd;;;;
  972.  
  973. ; go to next descriptor
  974.         inc     [device.tx_wr_des]
  975.         and     [device.tx_wr_des], TX_DES_COUNT-1
  976.  
  977. ; dec free descriptors count
  978.         test    [device.tx_free_des], -1
  979.         jz      .end
  980.         dec     [device.tx_free_des]
  981.   .end:
  982.         status
  983.  
  984.         DEBUGF  1,"transmit ok\n"
  985.         xor     eax, eax
  986.         ret
  987.  
  988.   .fail:
  989.         DEBUGF  1,"transmit failed\n"
  990.         or      eax, -1
  991.         ret
  992.  
  993.  
  994.  
  995. ;;;;;;;;;;;;;;;;;;;;;;;
  996. ;;                   ;;
  997. ;; Interrupt handler ;;
  998. ;;                   ;;
  999. ;;;;;;;;;;;;;;;;;;;;;;;
  1000.  
  1001. align 4
  1002. int_handler:
  1003.  
  1004.         DEBUGF  1,"IRQ %x ",eax:2                       ; no, you cant replace 'eax:2' with 'al', this must be a bug in FDO
  1005.  
  1006. ; find pointer of device wich made IRQ occur
  1007.  
  1008.         mov     ecx, [devices]
  1009.         test    ecx, ecx
  1010.         jz      .fail
  1011.         mov     esi, DEVICE_LIST
  1012.   .nextdevice:
  1013.         mov     ebx, dword [esi]
  1014.  
  1015.         set_io  0
  1016.         set_io  CSR5
  1017.         in      eax, dx
  1018.         out     dx , eax                                ; send it back to ACK
  1019.  
  1020.         and     eax, CSR7_DEFAULT                       ; int mask
  1021.         test    eax, eax
  1022.         jnz     .got_it
  1023.  
  1024.   .continue:
  1025.         add     esi, 4
  1026.         dec     ecx
  1027.         jnz     .nextdevice
  1028.  
  1029.         ret                                             ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
  1030.  
  1031.   .got_it:
  1032.  
  1033.         DEBUGF  1,"CSR7: %x ", eax
  1034.  
  1035. ; looks like we've found it!
  1036.  
  1037. ; Lets found out why the irq occured then..
  1038.  
  1039. ;----------------------------------
  1040. ; TX ok?
  1041.  
  1042.         test    eax, CSR5_TI
  1043.         jz      .not_tx
  1044.         push    ax esi ecx
  1045.  
  1046.         DEBUGF 1,"TX ok!\n"
  1047.                
  1048.         ; go to current descriptor
  1049.         mov     edi, [device.tx_p_des]
  1050.  
  1051.         mov     eax, [device.tx_rd_des]
  1052.         mov     edx, DES.size
  1053.         mul     edx
  1054.         add     edi, eax
  1055.                
  1056.       .loop_tx:
  1057.                
  1058.         ; done if all desc are free
  1059.         cmp     [device.tx_free_des], TX_DES_COUNT
  1060.         jz      .end_tx
  1061.  
  1062.         mov     eax, [edi+DES.DES0]
  1063.  
  1064.         ; we stop at first desc that is owned be NIC
  1065.         test    eax, DES0_OWN
  1066.         jnz     .end_tx
  1067.  
  1068.         ; detect is setup packet
  1069.         cmp     eax, (0ffffffffh - DES0_OWN)            ; all other bits are 1
  1070.         jne     .not_setup_packet
  1071.         DEBUGF  1,"Setup Packet detected\n"
  1072.       .not_setup_packet:
  1073.  
  1074.         DEBUGF  1,"packet status: %x\n", eax
  1075.  
  1076.         ; next descriptor
  1077.         add     edi, DES.size
  1078.         inc     [device.tx_rd_des]
  1079.         and     [device.tx_rd_des], TX_DES_COUNT-1
  1080.  
  1081.         ; inc free desc
  1082.         inc     [device.tx_free_des]
  1083.         cmp     [device.tx_free_des], TX_DES_COUNT
  1084.         jle     @f
  1085.         mov     [device.tx_free_des], TX_DES_COUNT
  1086.        @@:
  1087.  
  1088.         jmp     .loop_tx
  1089.       .end_tx:
  1090.                
  1091.         ;------------------------------------------------------
  1092.         ; here must be called standard Ethernet Tx Irq Handler
  1093.         ;------------------------------------------------------
  1094.  
  1095.         pop     ecx esi ax
  1096.  
  1097. ;----------------------------------
  1098. ; RX irq
  1099.   .not_tx:
  1100.         test    eax, CSR5_RI
  1101.         jz      .not_rx
  1102.         push    ax esi ecx
  1103.  
  1104.         DEBUGF 1,"RX ok!\n"
  1105.                
  1106.         ;go to current descriptor
  1107.         mov     edi, [device.rx_p_des]
  1108.  
  1109.         mov     eax, [device.rx_crt_des]
  1110.         mov     edx, DES.size
  1111.         mul     edx
  1112.         add     edi, eax
  1113.                
  1114.       .loop_rx_start_of_packet:
  1115.         mov     eax, [edi+DES.DES0]
  1116.  
  1117.         test    eax, DES0_OWN
  1118.         jnz     .end_rx                                 ; current desc is busy, nothing to do
  1119.  
  1120.         test    eax, RDES0_FS
  1121.         jz      .end_rx                                 ; current desc is NOT first packet, ERROR!
  1122.  
  1123.         test    eax, RDES0_LS                           ; if not last desc of packet, error for now
  1124.         jz      .end_rx
  1125.  
  1126. ;        .IF     ZERO?
  1127. ;                ODS2 <"Net_Interrupt: packet > 1 descriptor, not supported yet :P">
  1128. ;                jmp     @@end_rx
  1129. ;        .ENDIF
  1130.  
  1131.         test    eax, RDES0_ES
  1132.         jnz     .end_rx
  1133.  
  1134. ;        .IF     !ZERO?
  1135. ;                ODS2 <"Net_Interrupt: RX error">
  1136. ;                jmp     @@end_rx
  1137. ;        .ENDIF
  1138.  
  1139.         mov     esi, [edi+DES.realaddr]
  1140.         mov     ecx, [edi+DES.DES0]
  1141.         shr     ecx, RDES0_FL_SH
  1142.         and     ecx, RDES0_FL_MASK
  1143.         sub     ecx, 4                                   ; crc
  1144.  
  1145.         DEBUGF  1,"Received packet!, size=%u, addr:%x\n", ecx, esi
  1146.  
  1147.         push    esi edi ecx
  1148.         stdcall KernelAlloc, ecx ; Allocate a buffer to put packet into
  1149.         pop     ecx edi esi
  1150.         test    eax, eax
  1151.         jz      .fail
  1152.  
  1153.         push    edi
  1154.         push    dword .continue_rx
  1155.         push    ecx eax
  1156.         mov     edi, eax
  1157.  
  1158. ; update statistics
  1159.         inc     [device.packets_rx]
  1160.  
  1161.         add     dword [device.bytes_rx], ecx
  1162.         adc     dword [device.bytes_rx + 4], 0
  1163.  
  1164. ; copy packet data
  1165.         shr     cx , 1
  1166.         jnc     .nb
  1167.         movsb
  1168.   .nb:
  1169.         shr     cx , 1
  1170.         jnc     .nw
  1171.         movsw
  1172.   .nw:
  1173.         rep     movsd
  1174.  
  1175.         jmp     EthReceiver
  1176.  
  1177.     .continue_rx:
  1178.         pop     edi
  1179.  
  1180.         ; free descriptor
  1181.         mov     [edi+DES.DES0], DES0_OWN
  1182.                
  1183.         ; next descriptor
  1184.         add     edi, DES.size
  1185.  
  1186.         inc     [device.rx_crt_des]
  1187.         and     [device.rx_crt_des], RX_DES_COUNT-1
  1188.  
  1189.         jmp     .loop_rx_start_of_packet
  1190.  
  1191.       .end_rx:
  1192.       .fail:
  1193.         pop     ecx esi ax
  1194.       .not_rx:
  1195.  
  1196.         jmp     .continue
  1197.  
  1198.  
  1199.  
  1200. align 4
  1201. write_mac:      ; in: mac pushed onto stack (as 3 words)
  1202.  
  1203.         DEBUGF  2,"Writing MAC: "
  1204.  
  1205. ; write data into driver cache
  1206.         mov     esi, esp
  1207.         lea     edi, [device.mac]
  1208.         movsd
  1209.         movsw
  1210.         add     esp, 6
  1211.        
  1212. ; send setup packet (only if driver is started)
  1213.         call    Send_Setup_Packet
  1214.  
  1215. align 4
  1216. read_mac:
  1217.  
  1218.         DEBUGF 1,"Read_mac\n"
  1219.  
  1220.         ret
  1221.  
  1222.  
  1223.  
  1224. align 4
  1225. read_mac_eeprom:
  1226.  
  1227.         DEBUGF 1,"Read_mac_eeprom\n"
  1228.  
  1229.         lea     edi, [device.mac]
  1230.         mov     esi, 20/2               ; read words, start address is 20
  1231.      .loop:
  1232.         push    esi edi
  1233.         call    SROM_Read_Word
  1234.         pop     edi esi
  1235.         stosw
  1236.         inc     esi
  1237.         cmp     esi, 26/2
  1238.         jl      .loop
  1239.  
  1240.         DEBUGF  2,"%x-%x-%x-%x-%x-%x\n",[edi-6]:2,[edi-5]:2,[edi-4]:2,[edi-3]:2,[edi-2]:2,[edi-1]:2
  1241.  
  1242.         ret
  1243.  
  1244. align 4
  1245. write_mac_eeprom:
  1246.  
  1247.         DEBUGF 1,"Write_mac_eeprom\n"
  1248.  
  1249.         ret
  1250.  
  1251.  
  1252. align 4
  1253. SROM_GetWidth:  ; should be 6 or 8 according to some manuals (returns in ecx)
  1254.  
  1255.         DEBUGF 1,"SROM_GetWidth\n"
  1256.  
  1257.         call    SROM_Idle
  1258.         call    SROM_EnterAccessMode
  1259.  
  1260. ;        set_io  0
  1261. ;        set_io  CSR9
  1262.  
  1263.         ; send 110b
  1264.  
  1265.         in      eax, dx
  1266.         or      eax, CSR9_SROM_DI
  1267.         call    SROM_out
  1268.  
  1269.         in      eax, dx
  1270.         or      eax, CSR9_SROM_DI
  1271.         call    SROM_out
  1272.  
  1273.         in      eax, dx
  1274.         and     eax, not (CSR9_SROM_DI)
  1275.         call    SROM_out
  1276.        
  1277.         mov     ecx,1
  1278.   .loop2:
  1279.         Bit_Set CSR9_SROM_CK
  1280.         SROM_Delay
  1281.        
  1282.         in      eax, dx
  1283.         and     eax, CSR9_SROM_DO
  1284.         jnz     .not_zero
  1285.  
  1286.         Bit_Clear CSR9_SROM_CK
  1287.         SROM_Delay
  1288.         jmp     .end_loop2
  1289.   .not_zero:
  1290.        
  1291.         Bit_Clear CSR9_SROM_CK
  1292.         SROM_Delay
  1293.        
  1294.         inc     ecx
  1295.         cmp     ecx, 12
  1296.         jle     .loop2
  1297.   .end_loop2:
  1298.        
  1299.         DEBUGF 1,"Srom width=%u\n", ecx
  1300.        
  1301.         call    SROM_Idle
  1302.         call    SROM_EnterAccessMode
  1303.         call    SROM_Idle
  1304.        
  1305.         ret
  1306.  
  1307.  
  1308. align 4
  1309. SROM_out:
  1310.  
  1311.         out     dx, eax
  1312.         SROM_Delay
  1313.         Bit_Set CSR9_SROM_CK
  1314.         SROM_Delay
  1315.         Bit_Clear CSR9_SROM_CK
  1316.         SROM_Delay
  1317.  
  1318.         ret
  1319.  
  1320.  
  1321.  
  1322. align 4
  1323. SROM_EnterAccessMode:
  1324.  
  1325.         DEBUGF 1,"SROM_EnterAccessMode\n"
  1326.  
  1327.         set_io  0
  1328.         set_io  CSR9
  1329.         mov     eax, CSR9_SR
  1330.         out     dx, eax
  1331.         SROM_Delay
  1332.  
  1333.         Bit_Set CSR9_RD
  1334.         SROM_Delay
  1335.  
  1336.         Bit_Clear CSR9_SROM_CK
  1337.         SROM_Delay
  1338.  
  1339.         Bit_Set CSR9_SROM_CS
  1340.         SROM_Delay
  1341.        
  1342.         ret
  1343.  
  1344.  
  1345.  
  1346. align 4
  1347. SROM_Idle:
  1348.  
  1349.         DEBUGF 1,"SROM_Idle\n"
  1350.  
  1351.         call    SROM_EnterAccessMode
  1352.        
  1353. ;        set_io  0
  1354. ;        set_io  CSR9
  1355.        
  1356.         mov     ecx, 25
  1357.      .loop_clk:
  1358.  
  1359.         Bit_Clear CSR9_SROM_CK
  1360.         SROM_Delay
  1361.         Bit_Set CSR9_SROM_CK
  1362.         SROM_Delay
  1363.        
  1364.         dec     ecx
  1365.         jnz     .loop_clk
  1366.  
  1367.        
  1368.         Bit_Clear CSR9_SROM_CK
  1369.         SROM_Delay
  1370.         Bit_Clear CSR9_SROM_CS
  1371.         SROM_Delay
  1372.        
  1373.         xor     eax, eax
  1374.         out     dx, eax
  1375.        
  1376.         ret
  1377.  
  1378.  
  1379. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1380. ;;                                                                      ;;
  1381. ;; Read serial EEprom word                                              ;;
  1382. ;;                                                                      ;;
  1383. ;; In: esi = read address                                               ;;
  1384. ;; OUT: ax = data word                                                  ;;
  1385. ;;                                                                      ;;
  1386. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1387. align 4
  1388. SROM_Read_Word:
  1389.  
  1390.         DEBUGF 1,"SROM_Read_word at: %x result: ", esi
  1391.  
  1392.         set_io  0
  1393.         set_io  CSR9
  1394.  
  1395. ; enter access mode
  1396.         mov     eax, CSR9_SR + CSR9_RD
  1397.         out     dx , eax
  1398.         or      eax, CSR9_SROM_CS
  1399.         out     dx , eax
  1400.  
  1401.         ; TODO: change this hard-coded 6-bit stuff to use value from srom_getwidth
  1402.        
  1403. ; send read command "110b" + address to read from
  1404.         and     esi, 111111b
  1405.         or      esi, 110b shl 6
  1406.        
  1407.         mov     ecx, 1 shl 9
  1408.   .loop_cmd:
  1409.         mov     eax, CSR9_SR + CSR9_RD + CSR9_SROM_CS
  1410.         test    esi, ecx
  1411.         jz      @f
  1412.         or      eax, CSR9_SROM_DI
  1413.        @@:
  1414.         out     dx , eax
  1415.         SROM_Delay
  1416.         or      eax, CSR9_SROM_CK
  1417.         out     dx , eax
  1418.         SROM_Delay
  1419.        
  1420.         shr     ecx, 1
  1421.         jnz     .loop_cmd
  1422.  
  1423. ; read data from SROM
  1424.  
  1425.         xor     esi, esi
  1426.         mov     ecx, 17 ;;; TODO: figure out why 17, not 16
  1427.   .loop_read:
  1428.        
  1429.         mov     eax, CSR9_SR + CSR9_RD + CSR9_SROM_CS + CSR9_SROM_CK
  1430.         out     dx , eax
  1431.         SROM_Delay
  1432.        
  1433.         in      eax, dx
  1434.         and     eax, CSR9_SROM_DO
  1435.         shr     eax, 3
  1436.         shl     esi, 1
  1437.         or      esi, eax
  1438.        
  1439.         mov     eax, CSR9_SR + CSR9_RD + CSR9_SROM_CS
  1440.         out     dx , eax
  1441.         SROM_Delay
  1442.        
  1443.         dec     ecx
  1444.         jnz     .loop_read
  1445.        
  1446.         mov     eax, esi
  1447.  
  1448.         DEBUGF 1,"%x\n", ax
  1449.  
  1450.         ret
  1451.  
  1452.  
  1453. ; End of code
  1454.  
  1455. align 4                                         ; Place all initialised data here
  1456.  
  1457. devices       dd 0
  1458. version       dd (5 shl 16) or (API_VERSION and 0xFFFF)
  1459. my_service    db 'DEC21X4X',0                    ; max 16 chars include zero
  1460.  
  1461. include_debug_strings                           ; All data wich FDO uses will be included here
  1462.  
  1463. section '.data' data readable writable align 16 ; place all uninitialized data place here
  1464.  
  1465. DEVICE_LIST rd MAX_DEVICES                     ; This list contains all pointers to device structures the driver is handling
  1466.  
  1467.  
  1468.