Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2015. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  RTL8169 driver for KolibriOS                                   ;;
  7. ;;                                                                 ;;
  8. ;;  Copyright 2007 mike.dld,                                       ;;
  9. ;;   mike.dld@gmail.com                                            ;;
  10. ;;                                                                 ;;
  11. ;; port to net branch by hidnplayr                                 ;;
  12. ;;                                                                 ;;
  13. ;;  References:                                                    ;;
  14. ;;    r8169.c - linux driver (etherboot project)                   ;;
  15. ;;                                                                 ;;
  16. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  17. ;;             Version 2, June 1991                                ;;
  18. ;;                                                                 ;;
  19. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  20.  
  21. format PE DLL native
  22. entry START
  23.  
  24.         CURRENT_API             = 0x0200
  25.         COMPATIBLE_API          = 0x0100
  26.         API_VERSION             = (COMPATIBLE_API shl 16) + CURRENT_API
  27.  
  28.         MAX_DEVICES             = 16
  29.  
  30.         __DEBUG__               = 1
  31.         __DEBUG_LEVEL__         = 2     ; 1 = verbose, 2 = errors only
  32.  
  33.         NUM_TX_DESC             = 4
  34.         NUM_RX_DESC             = 4
  35.  
  36. section '.flat' readable writable executable
  37.  
  38. include '../proc32.inc'
  39. include '../struct.inc'
  40. include '../macros.inc'
  41. include '../fdo.inc'
  42. include '../netdrv.inc'
  43.  
  44.         REG_MAC0                = 0x0 ; Ethernet hardware address
  45.         REG_MAR0                = 0x8 ; Multicast filter
  46.         REG_TxDescStartAddr     = 0x20
  47.         REG_TxHDescStartAddr    = 0x28
  48.         REG_FLASH               = 0x30
  49.         REG_ERSR                = 0x36
  50.         REG_ChipCmd             = 0x37
  51.         REG_TxPoll              = 0x38
  52.         REG_IntrMask            = 0x3C
  53.         REG_IntrStatus          = 0x3E
  54.         REG_TxConfig            = 0x40
  55.         REG_RxConfig            = 0x44
  56.         REG_RxMissed            = 0x4C
  57.         REG_Cfg9346             = 0x50
  58.         REG_Config0             = 0x51
  59.         REG_Config1             = 0x52
  60.         REG_Config2             = 0x53
  61.         REG_Config3             = 0x54
  62.         REG_Config4             = 0x55
  63.         REG_Config5             = 0x56
  64.         REG_MultiIntr           = 0x5C
  65.         REG_PHYAR               = 0x60
  66.         REG_TBICSR              = 0x64
  67.         REG_TBI_ANAR            = 0x68
  68.         REG_TBI_LPAR            = 0x6A
  69.         REG_PHYstatus           = 0x6C
  70.         REG_RxMaxSize           = 0xDA
  71.         REG_CPlusCmd            = 0xE0
  72.         REG_RxDescStartAddr     = 0xE4
  73.         REG_ETThReg             = 0xEC
  74.         REG_FuncEvent           = 0xF0
  75.         REG_FuncEventMask       = 0xF4
  76.         REG_FuncPresetState     = 0xF8
  77.         REG_FuncForceEvent      = 0xFC
  78.  
  79.         ; InterruptStatusBits
  80.         ISB_SYSErr              = 0x8000
  81.         ISB_PCSTimeout          = 0x4000
  82.         ISB_SWInt               = 0x0100
  83.         ISB_TxDescUnavail       = 0x80
  84.         ISB_RxFIFOOver          = 0x40
  85.         ISB_LinkChg             = 0x20
  86.         ISB_RxOverflow          = 0x10
  87.         ISB_TxErr               = 0x08
  88.         ISB_TxOK                = 0x04
  89.         ISB_RxErr               = 0x02
  90.         ISB_RxOK                = 0x01
  91.  
  92.         ; RxStatusDesc
  93.         SD_RxRES                = 0x00200000
  94.         SD_RxCRC                = 0x00080000
  95.         SD_RxRUNT               = 0x00100000
  96.         SD_RxRWT                = 0x00400000
  97.  
  98.         ; ChipCmdBits
  99.         CMD_Reset               = 0x10
  100.         CMD_RxEnb               = 0x08
  101.         CMD_TxEnb               = 0x04
  102.         CMD_RxBufEmpty          = 0x01
  103.  
  104.         ; Cfg9346Bits
  105.         CFG_9346_Lock           = 0x00
  106.         CFG_9346_Unlock         = 0xC0
  107.  
  108.         ; rx_mode_bits
  109.         RXM_AcceptErr           = 0x20
  110.         RXM_AcceptRunt          = 0x10
  111.         RXM_AcceptBroadcast     = 0x08
  112.         RXM_AcceptMulticast     = 0x04
  113.         RXM_AcceptMyPhys        = 0x02
  114.         RXM_AcceptAllPhys       = 0x01
  115.  
  116.         ; RxConfigBits
  117.         RXC_FIFOShift           = 13
  118.         RXC_DMAShift            = 8
  119.  
  120.         ; TxConfigBits
  121.         TXC_InterFrameGapShift  = 24
  122.         TXC_DMAShift            = 8    ; DMA burst value (0-7) is shift this many bits
  123.  
  124.         ; PHYstatus
  125.         PHYS_TBI_Enable         = 0x80
  126.         PHYS_TxFlowCtrl         = 0x40
  127.         PHYS_RxFlowCtrl         = 0x20
  128.         PHYS_1000bpsF           = 0x10
  129.         PHYS_100bps             = 0x08
  130.         PHYS_10bps              = 0x04
  131.         PHYS_LinkStatus         = 0x02
  132.         PHYS_FullDup            = 0x01
  133.  
  134.         ; GIGABIT_PHY_registers
  135.         PHY_CTRL_REG            = 0
  136.         PHY_STAT_REG            = 1
  137.         PHY_AUTO_NEGO_REG       = 4
  138.         PHY_1000_CTRL_REG       = 9
  139.  
  140.         ; GIGABIT_PHY_REG_BIT
  141.         PHY_Restart_Auto_Nego   = 0x0200
  142.         PHY_Enable_Auto_Nego    = 0x1000
  143.  
  144.         ; PHY_STAT_REG = 1
  145.         PHY_Auto_Neco_Comp      = 0x0020
  146.  
  147.         ; PHY_AUTO_NEGO_REG = 4
  148.         PHY_Cap_10_Half         = 0x0020
  149.         PHY_Cap_10_Full         = 0x0040
  150.         PHY_Cap_100_Half        = 0x0080
  151.         PHY_Cap_100_Full        = 0x0100
  152.  
  153.         ; PHY_1000_CTRL_REG = 9
  154.         PHY_Cap_1000_Full       = 0x0200
  155.         PHY_Cap_1000_Half       = 0x0100
  156.  
  157.         PHY_Cap_PAUSE           = 0x0400
  158.         PHY_Cap_ASYM_PAUSE      = 0x0800
  159.  
  160.         PHY_Cap_Null            = 0x0
  161.  
  162.         ; _MediaType
  163.         MT_10_Half              = 0x01
  164.         MT_10_Full              = 0x02
  165.         MT_100_Half             = 0x04
  166.         MT_100_Full             = 0x08
  167.         MT_1000_Full            = 0x10
  168.  
  169.         ; _TBICSRBit
  170.         TBI_LinkOK              = 0x02000000
  171.  
  172.         ; _DescStatusBit
  173.         DSB_OWNbit              = 0x80000000
  174.         DSB_EORbit              = 0x40000000
  175.         DSB_FSbit               = 0x20000000
  176.         DSB_LSbit               = 0x10000000
  177.  
  178.         RX_BUF_SIZE             = 1514          ; Rx Buffer size
  179.  
  180. ; max supported gigabit ethernet frame size -- must be at least (dev->mtu+14+4)
  181.         MAX_ETH_FRAME_SIZE      = 1514
  182.  
  183.         TX_FIFO_THRESH          = 256           ; In bytes
  184.  
  185.         RX_FIFO_THRESH          = 7             ; 7 means NO threshold, Rx buffer level before first PCI xfer
  186.         RX_DMA_BURST            = 7             ; Maximum PCI burst, '6' is 1024
  187.         TX_DMA_BURST            = 7             ; Maximum PCI burst, '6' is 1024
  188.         ETTh                    = 0x3F          ; 0x3F means NO threshold
  189.  
  190.         EarlyTxThld             = 0x3F          ; 0x3F means NO early transmit
  191.         RxPacketMaxSize         = 0x0800        ; Maximum size supported is 16K-1
  192.         InterFrameGap           = 0x03          ; 3 means InterFrameGap = the shortest one
  193.  
  194.         HZ                      = 1000
  195.  
  196.         RTL_MIN_IO_SIZE         = 0x80
  197.         TX_TIMEOUT              = (6*HZ)
  198.  
  199.         TIMER_EXPIRE_TIME       = 100
  200.  
  201.         ETH_HDR_LEN             = 14
  202.         DEFAULT_MTU             = 1500
  203.         DEFAULT_RX_BUF_LEN      = 1514
  204.  
  205.  
  206. ;ifdef   JUMBO_FRAME_SUPPORT
  207. ;        MAX_JUMBO_FRAME_MTU     = 10000
  208. ;        MAX_RX_SKBDATA_SIZE     = (MAX_JUMBO_FRAME_MTU + ETH_HDR_LEN )
  209. ;else
  210.         MAX_RX_SKBDATA_SIZE     = 1600
  211. ;end if
  212.  
  213.         MCFG_METHOD_01          = 0x01
  214.         MCFG_METHOD_02          = 0x02
  215.         MCFG_METHOD_03          = 0x03
  216.         MCFG_METHOD_04          = 0x04
  217.         MCFG_METHOD_05          = 0x05
  218.         MCFG_METHOD_11          = 0x0b
  219.         MCFG_METHOD_12          = 0x0c
  220.         MCFG_METHOD_13          = 0x0d
  221.         MCFG_METHOD_14          = 0x0e
  222.         MCFG_METHOD_15          = 0x0f
  223.  
  224.         PCFG_METHOD_1           = 0x01          ; PHY Reg 0x03 bit0-3 == 0x0000
  225.         PCFG_METHOD_2           = 0x02          ; PHY Reg 0x03 bit0-3 == 0x0001
  226.         PCFG_METHOD_3           = 0x03          ; PHY Reg 0x03 bit0-3 == 0x0002
  227.  
  228. struct  tx_desc
  229.         status    dd ?
  230.         vlan_tag  dd ?
  231.         buf_addr  dq ?
  232. ends
  233.         tx_desc.buf_soft_addr = NUM_TX_DESC*sizeof.tx_desc
  234.  
  235. struct  rx_desc
  236.         status    dd ?
  237.         vlan_tag  dd ?
  238.         buf_addr  dq ?
  239. ends
  240.         rx_desc.buf_soft_addr = NUM_RX_DESC*sizeof.rx_desc
  241.  
  242. struct  device          ETH_DEVICE
  243.  
  244.         io_addr         dd ?
  245.         pci_bus         dd ?
  246.         pci_dev         dd ?
  247.         irq_line        db ?
  248.                         rb 3 ; align 4
  249.         mmio_addr       dd ? ; memory map physical address
  250.         pcfg            dd ?
  251.         mcfg            dd ?
  252.  
  253.         cur_rx          dd ? ; Index into the Rx descriptor buffer of next Rx pkt
  254.         cur_tx          dd ? ; Index into the Tx descriptor buffer of next Rx pkt
  255.         last_tx         dd ?
  256.         mac_version     dd ?
  257.  
  258.         rb 0x100-($ and 0xff)   ; align 256
  259.         tx_ring         rb NUM_TX_DESC * sizeof.tx_desc * 2
  260.  
  261.         rb 0x100-($ and 0xff)   ; align 256
  262.         rx_ring         rb NUM_RX_DESC * sizeof.rx_desc * 2
  263.  
  264. ends
  265.  
  266.         intr_mask = ISB_LinkChg or ISB_RxOverflow or ISB_RxFIFOOver or ISB_TxErr or ISB_TxOK or ISB_RxErr or ISB_RxOK
  267.         rx_config = (RX_FIFO_THRESH shl RXC_FIFOShift) or (RX_DMA_BURST shl RXC_DMAShift) or 0x0000000E
  268.  
  269.  
  270. macro   udelay msec {
  271.  
  272.         push    esi ecx
  273.         mov     esi, msec
  274.         invoke  Sleep
  275.         pop     ecx esi
  276.  
  277. }
  278.  
  279. macro   WRITE_GMII_REG  RegAddr, value {
  280.  
  281.         set_io  [ebx + device.io_addr], REG_PHYAR
  282.         if      value eq ax
  283.         and     eax, 0x0000ffff
  284.         or      eax, 0x80000000 + (RegAddr shl 16)
  285.         else
  286.         mov     eax, 0x80000000 + (RegAddr shl 16) + value
  287.         end if
  288.         out     dx, eax
  289.  
  290.         call    PHY_WAIT_WRITE
  291. }
  292.  
  293. macro   READ_GMII_REG  RegAddr {
  294.  
  295. local   .error, .done
  296.  
  297.         set_io  [ebx + device.io_addr], REG_PHYAR
  298.         mov     eax, RegAddr shl 16
  299.         out     dx, eax
  300.  
  301.         call    PHY_WAIT_READ
  302.         jz      .error
  303.  
  304.         in      eax, dx
  305.         and     eax, 0xFFFF
  306.         jmp     .done
  307.  
  308.   .error:
  309.         or      eax, -1
  310.   .done:
  311. }
  312.  
  313. align 4
  314. PHY_WAIT_READ:       ; io addr must already be set to REG_PHYAR
  315.  
  316.         udelay  1        ;;;1000
  317.  
  318.         push    ecx
  319.         mov     ecx, 2000
  320.         ; Check if the RTL8169 has completed writing/reading to the specified MII register
  321.     @@:
  322.         in      eax, dx
  323.         test    eax, 0x80000000
  324.         jnz     .exit
  325.         udelay  1        ;;;100
  326.         loop    @b
  327.   .exit:
  328.         pop     ecx
  329.         ret
  330.  
  331. align 4
  332. PHY_WAIT_WRITE:       ; io addr must already be set to REG_PHYAR
  333.  
  334.         udelay  1        ;;;1000
  335.  
  336.         push    ecx
  337.         mov     ecx, 2000
  338.         ; Check if the RTL8169 has completed writing/reading to the specified MII register
  339.     @@:
  340.         in      eax, dx
  341.         test    eax, 0x80000000
  342.         jz      .exit
  343.         udelay  1        ;;;100
  344.         loop    @b
  345.   .exit:
  346.         pop     ecx
  347.         ret
  348.  
  349.  
  350.  
  351. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  352. ;;                        ;;
  353. ;; proc START             ;;
  354. ;;                        ;;
  355. ;; (standard driver proc) ;;
  356. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  357.  
  358. proc START c, reason:dword, cmdline:dword
  359.  
  360.         cmp     [reason], DRV_ENTRY
  361.         jne     .fail
  362.  
  363.         DEBUGF  2,"Loading driver\n"
  364.         invoke  RegService, my_service, service_proc
  365.         ret
  366.  
  367.   .fail:
  368.         xor     eax, eax
  369.         ret
  370.  
  371. endp
  372.  
  373.  
  374. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  375. ;;                        ;;
  376. ;; proc SERVICE_PROC      ;;
  377. ;;                        ;;
  378. ;; (standard driver proc) ;;
  379. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  380.  
  381. proc service_proc stdcall, ioctl:dword
  382.  
  383.         mov     edx, [ioctl]
  384.         mov     eax, [edx + IOCTL.io_code]
  385.  
  386. ;------------------------------------------------------
  387.  
  388.         cmp     eax, 0 ;SRV_GETVERSION
  389.         jne     @F
  390.  
  391.         cmp     [edx + IOCTL.out_size], 4
  392.         jb      .fail
  393.         mov     eax, [edx + IOCTL.output]
  394.         mov     [eax], dword API_VERSION
  395.  
  396.         xor     eax, eax
  397.         ret
  398.  
  399. ;------------------------------------------------------
  400.   @@:
  401.         cmp     eax, 1 ;SRV_HOOK
  402.         jne     .fail
  403.  
  404.         cmp     [edx + IOCTL.inp_size], 3               ; Data input must be at least 3 bytes
  405.         jb      .fail
  406.  
  407.         mov     eax, [edx + IOCTL.input]
  408.         cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
  409.         jne     .fail                                   ; other types arent supported for this card yet
  410.  
  411. ; check if the device is already listed
  412.  
  413.         mov     esi, device_list
  414.         mov     ecx, [devices]
  415.         test    ecx, ecx
  416.         jz      .firstdevice
  417.  
  418. ;        mov     eax, [edx + IOCTL.input]                ; get the pci bus and device numbers
  419.         mov     ax, [eax+1]                             ;
  420.   .nextdevice:
  421.         mov     ebx, [esi]
  422.         cmp     al, byte[ebx + device.pci_bus]
  423.         jne     @f
  424.         cmp     ah, byte[ebx + device.pci_dev]
  425.         je      .find_devicenum                         ; Device is already loaded, let's find it's device number
  426.        @@:
  427.         add     esi, 4
  428.         loop    .nextdevice
  429.  
  430.  
  431. ; This device doesnt have its own eth_device structure yet, lets create one
  432.   .firstdevice:
  433.         cmp     [devices], MAX_DEVICES                  ; First check if the driver can handle one more card
  434.         jae     .fail
  435.  
  436.         allocate_and_clear ebx, sizeof.device, .fail    ; Allocate memory to put the device structure in
  437.  
  438. ; Fill in the direct call addresses into the struct
  439.  
  440.         mov     [ebx + device.reset], reset
  441.         mov     [ebx + device.transmit], transmit
  442.         mov     [ebx + device.unload], unload
  443.         mov     [ebx + device.name], my_service
  444.  
  445. ; save the pci bus and device numbers
  446.  
  447.         mov     eax, [edx + IOCTL.input]
  448.         movzx   ecx, byte[eax+1]
  449.         mov     [ebx + device.pci_bus], ecx
  450.         movzx   ecx, byte[eax+2]
  451.         mov     [ebx + device.pci_dev], ecx
  452.  
  453. ; Now, it's time to find the base io addres of the PCI device
  454.  
  455.         stdcall PCI_find_io, [ebx + device.pci_bus], [ebx + device.pci_dev]
  456.         mov     [ebx + device.io_addr], eax
  457.  
  458. ; We've found the io address, find IRQ now
  459.  
  460.         invoke  PciRead8, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.interrupt_line
  461.         mov     [ebx + device.irq_line], al
  462.  
  463.         DEBUGF  2,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
  464.         [ebx + device.pci_dev]:1,[ebx + device.pci_bus]:1,[ebx + device.irq_line]:1,[ebx + device.io_addr]:8
  465.  
  466. ; Ok, the eth_device structure is ready, let's probe the device
  467. ; Because initialization fires IRQ, IRQ handler must be aware of this device
  468.         mov     eax, [devices]                                          ; Add the device structure to our device list
  469.         mov     [device_list + 4*eax], ebx                              ; (IRQ handler uses this list to find device)
  470.         inc     [devices]                                               ;
  471.  
  472.         call    probe                                                   ; this function will output in eax
  473.         test    eax, eax
  474.         jnz     .err2                                                   ; If an error occured, exit
  475.  
  476.         mov     [ebx + device.type], NET_TYPE_ETH
  477.         invoke  NetRegDev
  478.  
  479.         cmp     eax, -1
  480.         je      .destroy
  481.  
  482.         ret
  483.  
  484. ; If the device was already loaded, find the device number and return it in eax
  485.  
  486.   .find_devicenum:
  487.         DEBUGF  2,"Trying to find device number of already registered device\n"
  488.         invoke  NetPtrToNum                                             ; This kernel procedure converts a pointer to device struct in ebx
  489.                                                                         ; into a device number in edi
  490.         mov     eax, edi                                                ; Application wants it in eax instead
  491.         DEBUGF  2,"Kernel says: %u\n", eax
  492.         ret
  493.  
  494. ; If an error occured, remove all allocated data and exit (returning -1 in eax)
  495.  
  496.   .destroy:
  497.         ; todo: reset device into virgin state
  498.  
  499.   .err2:
  500.         dec     [devices]
  501.   .err:
  502.         DEBUGF  2,"removing device structure\n"
  503.         invoke  KernelFree, ebx
  504.   .fail:
  505.         or      eax, -1
  506.         ret
  507.  
  508. ;------------------------------------------------------
  509. endp
  510.  
  511.  
  512. align 4
  513. unload:
  514.  
  515.         ret
  516.  
  517.  
  518. align 4
  519. init_board:
  520.  
  521.         DEBUGF  1,"init_board\n"
  522.  
  523. ; Make the device a bus master
  524.         invoke  PciRead32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command
  525.         or      al, PCI_CMD_MASTER
  526.         invoke  PciWrite32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command, eax
  527.  
  528.         ; Soft reset the chip
  529.         set_io  [ebx + device.io_addr], 0
  530.         set_io  [ebx + device.io_addr], REG_ChipCmd
  531.         mov     al, CMD_Reset
  532.         out     dx, al
  533.  
  534.         ; Check that the chip has finished the reset
  535.         mov     ecx, 1000
  536.         set_io  [ebx + device.io_addr], REG_ChipCmd
  537.   @@:
  538.         in      al, dx
  539.         test    al, CMD_Reset
  540.         jz      @f
  541.         udelay  10
  542.         loop    @b
  543.   @@:
  544.  
  545.  
  546.         set_io  [ebx + device.io_addr], REG_TxConfig
  547.         in      eax, dx
  548.         mov     esi, MAC_VERSION_LIST
  549.   @@:
  550.         mov     ecx, eax
  551.         and     ecx, dword[esi]
  552.         cmp     ecx, dword[esi+4]
  553.         je      @f
  554.         add     esi, 4*4
  555.         jmp     @r
  556.   @@:
  557.  
  558.         mov     eax, [esi+8]
  559.         mov     [ebx + device.mac_version], eax
  560.         mov     eax, [esi+12]
  561.         mov     [ebx + device.name], eax
  562.  
  563.         xor     eax, eax
  564.         ret
  565.  
  566.  
  567.  
  568. ;***************************************************************************
  569. ;   Function
  570. ;      probe
  571. ;   Description
  572. ;      Searches for an ethernet card, enables it and clears the rx buffer
  573. ;      If a card was found, it enables the ethernet -> TCPIP link
  574. ;   Destroyed registers
  575. ;      eax, ebx, ecx, edx
  576. ;
  577. ;***************************************************************************
  578. align 4
  579. probe:
  580.  
  581.         DEBUGF  1,"probe\n"
  582.  
  583.         call    init_board
  584.         call    read_mac
  585.         call    PHY_config
  586.  
  587.         DEBUGF  1,"Set MAC Reg C+CR Offset 0x82h = 0x01h\n"
  588.         set_io  [ebx + device.io_addr], 0
  589.         set_io  [ebx + device.io_addr], 0x82
  590.         mov     al, 0x01
  591.         out     dx, al
  592.         cmp     [ebx + device.mcfg], MCFG_METHOD_03
  593.         jae     @f
  594.         DEBUGF  1,"Set PCI Latency=0x40\n"
  595. ; Adjust PCI latency to be at least 64
  596.         invoke  PciRead8, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.max_latency
  597.         cmp     al, 64
  598.         jae     @f
  599.         mov     al, 64
  600.         invoke  PciWrite8, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.max_latency, eax
  601.   @@:
  602.         cmp     [ebx + device.mcfg], MCFG_METHOD_02
  603.         jne     @f
  604.         DEBUGF  1,"Set MAC Reg C+CR Offset 0x82h = 0x01h\n"
  605.         set_io  [ebx + device.io_addr], 0
  606.         set_io  [ebx + device.io_addr], 0x82
  607.         mov     al, 0x01
  608.         out     dx, al
  609.         DEBUGF  1,"Set PHY Reg 0x0bh = 0x00h\n"
  610.         WRITE_GMII_REG 0x0b, 0x0000      ; w 0x0b 15 0 0
  611.     @@:
  612.         ; if TBI is not enabled
  613.         set_io  [ebx + device.io_addr], 0
  614.         set_io  [ebx + device.io_addr], REG_PHYstatus
  615.         in      al, dx
  616.         test    al, PHYS_TBI_Enable
  617.         jz      .tbi_dis
  618.         READ_GMII_REG PHY_AUTO_NEGO_REG
  619.  
  620.         ; enable 10/100 Full/Half Mode, leave PHY_AUTO_NEGO_REG bit4:0 unchanged
  621.         and     eax, 0x0C1F
  622.         or      eax, PHY_Cap_10_Half or PHY_Cap_10_Full or PHY_Cap_100_Half or PHY_Cap_100_Full
  623.         WRITE_GMII_REG PHY_AUTO_NEGO_REG, ax
  624.  
  625.         ; enable 1000 Full Mode
  626.         WRITE_GMII_REG PHY_1000_CTRL_REG, PHY_Cap_1000_Full or PHY_Cap_1000_Half ; rtl8168
  627.  
  628.         ; Enable auto-negotiation and restart auto-nigotiation
  629.         WRITE_GMII_REG PHY_CTRL_REG, PHY_Enable_Auto_Nego or PHY_Restart_Auto_Nego
  630.  
  631.         udelay  1                       ; 100
  632.         mov     ecx, 200                ; 10000
  633.         DEBUGF  1, "Waiting for auto-negotiation to complete\n"
  634.         ; wait for auto-negotiation process
  635.     @@: dec     ecx
  636.         jz      @f
  637.         set_io  [ebx + device.io_addr], 0
  638.         READ_GMII_REG PHY_STAT_REG
  639.         udelay  1                       ; 100
  640.         test    eax, PHY_Auto_Neco_Comp
  641.         jz      @b
  642.         set_io  [ebx + device.io_addr], REG_PHYstatus
  643.         in      al, dx
  644.         jmp     @f
  645.   .tbi_dis:
  646.         udelay  1                       ; 100
  647.     @@:
  648.         DEBUGF  1, "auto-negotiation complete\n"
  649.  
  650. ;***************************************************************************
  651. ;   Function
  652. ;      rt8169_reset
  653. ;   Description
  654. ;      Place the chip (ie, the ethernet card) into a virgin state
  655. ;   Destroyed registers
  656. ;      eax, ebx, ecx, edx
  657. ;
  658. ;***************************************************************************
  659. align 4
  660. reset:
  661.  
  662.         DEBUGF  1,"resetting\n"
  663.  
  664.         call    init_ring
  665.         test    eax, eax
  666.         jnz     .err
  667.  
  668.         call    hw_start
  669.  
  670. ; clear packet/byte counters
  671.  
  672.         xor     eax, eax
  673.         lea     edi, [ebx + device.bytes_tx]
  674.         mov     ecx, 6
  675.         rep     stosd
  676.  
  677.         mov     [ebx + device.mtu], 1500
  678.         call    detect_link
  679.  
  680.         DEBUGF  2,"init OK!\n"
  681.         xor     eax, eax
  682.         ret
  683.  
  684.   .err:
  685.         DEBUGF  2,"failed!\n"
  686.         or      eax, -1
  687.         ret
  688.  
  689.  
  690.  
  691. align 4
  692. PHY_config:
  693.  
  694.         DEBUGF  1,"hw_PHY_config: priv.mcfg=%d, priv.pcfg=%d\n", [ebx + device.mcfg], [ebx + device.pcfg]
  695.  
  696.         cmp     [ebx + device.mcfg], MCFG_METHOD_04
  697.         jne     .not_4
  698.         set_io  [ebx + device.io_addr], 0
  699. ;       WRITE_GMII_REG 0x1F, 0x0001
  700. ;       WRITE_GMII_REG 0x1b, 0x841e
  701. ;       WRITE_GMII_REG 0x0e, 0x7bfb
  702. ;       WRITE_GMII_REG 0x09, 0x273a
  703.         WRITE_GMII_REG 0x1F, 0x0002
  704.         WRITE_GMII_REG 0x01, 0x90D0
  705.         WRITE_GMII_REG 0x1F, 0x0000
  706.         jmp     .exit
  707.   .not_4:
  708.         cmp     [ebx + device.mcfg], MCFG_METHOD_02
  709.         je      @f
  710.         cmp     [ebx + device.mcfg], MCFG_METHOD_03
  711.         jne     .not_2_or_3
  712.     @@:
  713.         set_io  [ebx + device.io_addr], 0
  714.         WRITE_GMII_REG 0x1F, 0x0001
  715.         WRITE_GMII_REG 0x15, 0x1000
  716.         WRITE_GMII_REG 0x18, 0x65C7
  717.         WRITE_GMII_REG 0x04, 0x0000
  718.         WRITE_GMII_REG 0x03, 0x00A1
  719.         WRITE_GMII_REG 0x02, 0x0008
  720.         WRITE_GMII_REG 0x01, 0x1020
  721.         WRITE_GMII_REG 0x00, 0x1000
  722.         WRITE_GMII_REG 0x04, 0x0800
  723.         WRITE_GMII_REG 0x04, 0x0000
  724.         WRITE_GMII_REG 0x04, 0x7000
  725.         WRITE_GMII_REG 0x03, 0xFF41
  726.         WRITE_GMII_REG 0x02, 0xDE60
  727.         WRITE_GMII_REG 0x01, 0x0140
  728.         WRITE_GMII_REG 0x00, 0x0077
  729.         WRITE_GMII_REG 0x04, 0x7800
  730.         WRITE_GMII_REG 0x04, 0x7000
  731.         WRITE_GMII_REG 0x04, 0xA000
  732.         WRITE_GMII_REG 0x03, 0xDF01
  733.         WRITE_GMII_REG 0x02, 0xDF20
  734.         WRITE_GMII_REG 0x01, 0xFF95
  735.         WRITE_GMII_REG 0x00, 0xFA00
  736.         WRITE_GMII_REG 0x04, 0xA800
  737.         WRITE_GMII_REG 0x04, 0xA000
  738.         WRITE_GMII_REG 0x04, 0xB000
  739.         WRITE_GMII_REG 0x03, 0xFF41
  740.         WRITE_GMII_REG 0x02, 0xDE20
  741.         WRITE_GMII_REG 0x01, 0x0140
  742.         WRITE_GMII_REG 0x00, 0x00BB
  743.         WRITE_GMII_REG 0x04, 0xB800
  744.         WRITE_GMII_REG 0x04, 0xB000
  745.         WRITE_GMII_REG 0x04, 0xF000
  746.         WRITE_GMII_REG 0x03, 0xDF01
  747.         WRITE_GMII_REG 0x02, 0xDF20
  748.         WRITE_GMII_REG 0x01, 0xFF95
  749.         WRITE_GMII_REG 0x00, 0xBF00
  750.         WRITE_GMII_REG 0x04, 0xF800
  751.         WRITE_GMII_REG 0x04, 0xF000
  752.         WRITE_GMII_REG 0x04, 0x0000
  753.         WRITE_GMII_REG 0x1F, 0x0000
  754.         WRITE_GMII_REG 0x0B, 0x0000
  755.         jmp     .exit
  756.   .not_2_or_3:
  757.         DEBUGF  1,"mcfg=%d, discard hw PHY config\n", [ebx + device.mcfg]
  758.   .exit:
  759.         ret
  760.  
  761.  
  762.  
  763. align 4
  764. set_rx_mode:
  765.  
  766.         DEBUGF  1,"set_rx_mode\n"
  767.  
  768.         ; IFF_ALLMULTI
  769.         ; Too many to filter perfectly -- accept all multicasts
  770.         set_io  [ebx + device.io_addr], 0
  771.         set_io  [ebx + device.io_addr], REG_RxConfig
  772.         in      eax, dx
  773.         and     eax, 0xff7e1880
  774.         or      eax, rx_config or (RXM_AcceptBroadcast or RXM_AcceptMulticast or RXM_AcceptMyPhys)
  775.         out     dx, eax
  776.  
  777.         ; Multicast hash filter
  778.         set_io  [ebx + device.io_addr], REG_MAR0 + 0
  779.         or      eax, -1
  780.         out     dx, eax
  781.         set_io  [ebx + device.io_addr], REG_MAR0 + 4
  782.         out     dx, eax
  783.  
  784.         ret
  785.  
  786.  
  787. align 4
  788. init_ring:
  789.  
  790.         DEBUGF  1,"init_ring\n"
  791.  
  792.         xor     eax, eax
  793.         mov     [ebx + device.cur_rx], eax
  794.         mov     [ebx + device.cur_tx], eax
  795.         mov     [ebx + device.last_tx], eax
  796.  
  797.         lea     edi, [ebx + device.tx_ring]
  798.         mov     ecx, (NUM_TX_DESC * sizeof.tx_desc) / 4 * 2
  799.         rep     stosd
  800.  
  801.         lea     edi, [ebx + device.rx_ring]
  802.         mov     ecx, (NUM_RX_DESC * sizeof.rx_desc) / 4
  803.         rep     stosd
  804.  
  805.         lea     edi, [ebx + device.rx_ring]
  806.         mov     ecx, NUM_RX_DESC
  807.   .loop:
  808.         push    ecx
  809.         invoke  NetAlloc, RX_BUF_SIZE+NET_BUFF.data
  810.         test    eax, eax
  811.         jz      .err
  812.         mov     dword [edi + rx_desc.buf_soft_addr], eax
  813.         invoke  GetPhysAddr
  814.         add     eax, NET_BUFF.data
  815.         mov     dword [edi + rx_desc.buf_addr], eax
  816.         mov     [edi + rx_desc.status], DSB_OWNbit or RX_BUF_SIZE
  817.         add     edi, sizeof.rx_desc
  818.         pop     ecx
  819.         dec     ecx
  820.         jnz     .loop
  821.         or      [edi - sizeof.rx_desc + rx_desc.status], DSB_EORbit
  822.  
  823.         xor     eax, eax
  824.         ret
  825.  
  826.   .err:
  827.         pop     eax
  828.         or      eax, -1
  829.         ret
  830.  
  831. align 4
  832. hw_start:
  833.  
  834.         DEBUGF  1,"hw_start\n"
  835.  
  836. ; attach int handler
  837.         movzx   eax, [ebx + device.irq_line]
  838.         DEBUGF  1,"Attaching int handler to irq %x\n", eax:1
  839.         invoke  AttachIntHandler, eax, int_handler, ebx
  840.         test    eax, eax
  841.         jnz     @f
  842.         DEBUGF  2,"Could not attach int handler!\n"
  843.         or      eax, -1
  844.         ret
  845.        @@:
  846.  
  847.         ; Soft reset the chip
  848.         set_io  [ebx + device.io_addr], 0
  849.         set_io  [ebx + device.io_addr], REG_ChipCmd
  850.         mov     al, CMD_Reset
  851.         out     dx, al
  852.  
  853.         DEBUGF  1,"Waiting for chip to reset... "
  854.         ; Check that the chip has finished the reset
  855.         mov     ecx, 1000
  856.         set_io  [ebx + device.io_addr], REG_ChipCmd
  857.     @@: in      al, dx
  858.         test    al, CMD_Reset
  859.         jz      @f
  860.         udelay  10
  861.         loop    @b
  862.     @@:
  863.         DEBUGF  1,"done!\n"
  864.  
  865.         set_io  [ebx + device.io_addr], REG_Cfg9346
  866.         mov     al, CFG_9346_Unlock
  867.         out     dx, al
  868.  
  869.         set_io  [ebx + device.io_addr], REG_ChipCmd
  870.         mov     al, CMD_TxEnb or CMD_RxEnb
  871.         out     dx, al
  872.  
  873.         set_io  [ebx + device.io_addr], REG_ETThReg
  874.         mov     al, ETTh
  875.         out     dx, al
  876.  
  877.         ; For gigabit rtl8169
  878.         set_io  [ebx + device.io_addr], REG_RxMaxSize
  879.         mov     ax, RxPacketMaxSize
  880.         out     dx, ax
  881.  
  882.         ; Set Rx Config register
  883.         set_io  [ebx + device.io_addr], REG_RxConfig
  884.         in      ax, dx
  885.         and     eax, 0xff7e1880
  886.         or      eax, rx_config
  887.         out     dx, eax
  888.  
  889.         ; Set DMA burst size and Interframe Gap Time
  890.         set_io  [ebx + device.io_addr], REG_TxConfig
  891.         mov     eax, (TX_DMA_BURST shl TXC_DMAShift) or (InterFrameGap shl TXC_InterFrameGapShift)
  892.         out     dx, eax
  893.  
  894.         set_io  [ebx + device.io_addr], REG_CPlusCmd
  895.         in      ax, dx
  896.         out     dx, ax
  897.  
  898.         in      ax, dx
  899.         or      ax, 1 shl 3
  900.         cmp     [ebx + device.mcfg], MCFG_METHOD_02
  901.         jne     @f
  902.         cmp     [ebx + device.mcfg], MCFG_METHOD_03
  903.         jne     @f
  904.         or      ax,1 shl 14
  905.         DEBUGF  1,"Set MAC Reg C+CR Offset 0xE0: bit-3 and bit-14\n"
  906.         jmp     .set
  907.     @@:
  908.         DEBUGF  1,"Set MAC Reg C+CR Offset 0xE0: bit-3\n"
  909.   .set:
  910.         set_io  [ebx + device.io_addr], REG_CPlusCmd
  911.         out     dx, ax
  912.  
  913.         set_io  [ebx + device.io_addr], 0xE2
  914. ;        mov     ax, 0x1517
  915. ;        out     dx, ax
  916. ;        mov     ax, 0x152a
  917. ;        out     dx, ax
  918. ;        mov     ax, 0x282a
  919. ;        out     dx, ax
  920.         xor     ax, ax
  921.         out     dx, ax
  922.  
  923.         xor     eax, eax
  924.         mov     [ebx + device.cur_rx], eax
  925.         lea     eax, [ebx + device.tx_ring]
  926.         invoke  GetPhysAddr
  927.         set_io  [ebx + device.io_addr], REG_TxDescStartAddr
  928.         out     dx, eax
  929.         set_io  [ebx + device.io_addr], REG_TxDescStartAddr + 4
  930.         xor     eax, eax
  931.         out     dx, eax
  932.  
  933.         lea     eax, [ebx + device.rx_ring]
  934.         invoke  GetPhysAddr
  935.         set_io  [ebx + device.io_addr], REG_RxDescStartAddr
  936.         out     dx, eax
  937.         xor     eax, eax
  938.         set_io  [ebx + device.io_addr], REG_RxDescStartAddr + 4
  939.         out     dx, eax
  940.  
  941.         set_io  [ebx + device.io_addr], REG_Cfg9346
  942.         mov     al, CFG_9346_Lock
  943.         out     dx, al
  944.  
  945.         udelay  10
  946.  
  947.         xor     eax, eax
  948.         set_io  [ebx + device.io_addr], REG_RxMissed
  949.         out     dx, eax
  950.  
  951.         call    set_rx_mode
  952.  
  953.         set_io  [ebx + device.io_addr], 0
  954.         ; no early-rx interrupts
  955.         set_io  [ebx + device.io_addr], REG_MultiIntr
  956.         in      ax, dx
  957.         and     ax, 0xF000
  958.         out     dx, ax
  959.  
  960.         ; set interrupt mask
  961.         set_io  [ebx + device.io_addr], REG_IntrMask
  962.         mov     ax, intr_mask
  963.         out     dx, ax
  964.  
  965.         xor     eax, eax
  966.         ret
  967.  
  968.  
  969. align 4
  970. read_mac:
  971.  
  972.         set_io  [ebx + device.io_addr], 0
  973.         set_io  [ebx + device.io_addr], REG_MAC0
  974.         xor     ecx, ecx
  975.         lea     edi, [ebx + device.mac]
  976.         mov     ecx, 6
  977.  
  978.         ; Get MAC address. FIXME: read EEPROM
  979.     @@:
  980.         in      al, dx
  981.         stosb
  982.         inc     edx
  983.         loop    @r
  984.  
  985.         DEBUGF  1,"MAC = %x-%x-%x-%x-%x-%x\n",\
  986.         [ebx + device.mac+0]:2,[ebx + device.mac+1]:2,[ebx + device.mac+2]:2,[ebx + device.mac+3]:2,[ebx + device.mac+4]:2,[ebx + device.mac+5]:2
  987.  
  988.         ret
  989.  
  990. align 4
  991. write_mac:
  992.  
  993.         ret     6
  994.  
  995.  
  996. ;***************************************************************************
  997. ;   Function
  998. ;      transmit
  999. ;   Description
  1000. ;      Transmits a packet of data via the ethernet card
  1001. ;
  1002. ;   Destroyed registers
  1003. ;      eax, edx, esi, edi
  1004. ;
  1005. ;***************************************************************************
  1006.  
  1007. proc transmit stdcall bufferptr
  1008.  
  1009.         pushf
  1010.         cli
  1011.  
  1012.         mov     esi, [bufferptr]
  1013.         DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n", [bufferptr], [esi + NET_BUFF.length]
  1014.         lea     eax, [esi + NET_BUFF.data]
  1015.         DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
  1016.         [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
  1017.         [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
  1018.         [eax+13]:2,[eax+12]:2
  1019.  
  1020.         cmp     [esi + NET_BUFF.length], 1514
  1021.         ja      .fail
  1022.         cmp     [esi + NET_BUFF.length], 60
  1023.         jb      .fail
  1024.  
  1025. ;----------------------------------
  1026. ; Find currentTX descriptor address
  1027.  
  1028.         mov     eax, sizeof.tx_desc
  1029.         mul     [ebx + device.cur_tx]
  1030.         lea     esi, [ebx + device.tx_ring + eax]
  1031.  
  1032.         DEBUGF  1,"Using TX desc: %x\n", esi
  1033.  
  1034. ;----------------------------------
  1035. ; Check if the descriptor is in use
  1036.  
  1037.         test    [esi + tx_desc.status], DSB_OWNbit
  1038.         jnz     .desc
  1039.  
  1040. ;---------------------------
  1041. ; Program the packet pointer
  1042.  
  1043.         mov     eax, [bufferptr]
  1044.         mov     ecx, [eax + NET_BUFF.length]
  1045.         mov     [esi + tx_desc.buf_soft_addr], eax
  1046.         add     eax, [eax + NET_BUFF.offset]
  1047.         invoke  GetPhysAddr
  1048.         mov     dword [esi + tx_desc.buf_addr], eax
  1049.  
  1050. ;------------------------
  1051. ; Program the packet size
  1052.  
  1053.         mov     eax, ecx
  1054.         or      eax, DSB_OWNbit or DSB_FSbit or DSB_LSbit
  1055.         cmp     [ebx + device.cur_tx], NUM_TX_DESC - 1
  1056.         jne     @f
  1057.         or      eax, DSB_EORbit
  1058.     @@:
  1059.         mov     [esi + tx_desc.status], eax
  1060.  
  1061. ;-----------------------------------------
  1062. ; Set the polling bit (start transmission)
  1063.  
  1064.         set_io  [ebx + device.io_addr], 0
  1065.         set_io  [ebx + device.io_addr], REG_TxPoll
  1066.         mov     al, 0x40     ; set polling bit
  1067.         out     dx, al
  1068.  
  1069. ;-----------------------
  1070. ; Update TX descriptor
  1071.  
  1072.         inc     [ebx + device.cur_tx]
  1073.         and     [ebx + device.cur_tx], NUM_TX_DESC - 1
  1074.  
  1075. ;-------------
  1076. ; Update stats
  1077.  
  1078.         inc     [ebx + device.packets_tx]
  1079.         add     dword [ebx + device.bytes_tx], ecx
  1080.         adc     dword [ebx + device.bytes_tx + 4], 0
  1081.  
  1082.         popf
  1083.         xor     eax, eax
  1084.         ret
  1085.  
  1086.   .desc:
  1087.         DEBUGF  2,"Descriptor is still in use!\n"
  1088.   .fail:
  1089.         DEBUGF  2,"Transmit failed\n"
  1090.         invoke  NetFree, [bufferptr]
  1091.         popf
  1092.         or      eax, -1
  1093.         ret
  1094.  
  1095. endp
  1096.  
  1097.  
  1098.  
  1099. ;;;;;;;;;;;;;;;;;;;;;;;
  1100. ;;                   ;;
  1101. ;; Interrupt handler ;;
  1102. ;;                   ;;
  1103. ;;;;;;;;;;;;;;;;;;;;;;;
  1104.  
  1105. align 4
  1106. int_handler:
  1107.  
  1108.         push    ebx esi edi
  1109.  
  1110.         DEBUGF  1,"INT\n"
  1111.  
  1112. ; find pointer of device wich made IRQ occur
  1113.  
  1114.         mov     ecx, [devices]
  1115.         test    ecx, ecx
  1116.         jz      .nothing
  1117.         mov     esi, device_list
  1118.   .nextdevice:
  1119.         mov     ebx, [esi]
  1120.  
  1121.         set_io  [ebx + device.io_addr], 0
  1122.         set_io  [ebx + device.io_addr], REG_IntrStatus
  1123.         in      ax, dx
  1124.         out     dx, ax                                  ; ACK all interrupts
  1125.         cmp     ax, 0xffff                              ; if so, hardware is no longer present
  1126.         je      .nothing
  1127.         test    ax, ax
  1128.         jnz     .got_it
  1129.   .continue:
  1130.         add     esi, 4
  1131.         dec     ecx
  1132.         jnz     .nextdevice
  1133.   .nothing:
  1134.         pop     edi esi ebx
  1135.         xor     eax, eax
  1136.  
  1137.         ret                                             ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
  1138.  
  1139.   .got_it:
  1140.         DEBUGF  1,"Device: %x Status: %x\n", ebx, ax
  1141.  
  1142. ;--------
  1143. ; Receive
  1144.         test    ax, ISB_RxOK
  1145.         jz      .no_rx
  1146.  
  1147.         push    ax
  1148.         push    ebx
  1149.  
  1150.   .check_more:
  1151.         pop     ebx
  1152.         mov     eax, sizeof.rx_desc
  1153.         mul     [ebx + device.cur_rx]
  1154.         lea     esi, [ebx + device.rx_ring + eax]
  1155.  
  1156.         DEBUGF  1,"RxDesc.status = 0x%x\n", [esi + rx_desc.status]
  1157.         mov     ecx, [esi + rx_desc.status]
  1158.         test    ecx, DSB_OWNbit ;;;
  1159.         jnz     .rx_return
  1160.  
  1161.         DEBUGF  1,"cur_rx = %u\n", [ebx + device.cur_rx]
  1162.  
  1163.         test    ecx, SD_RxRES
  1164.         jnz     .rx_return      ;;;;; RX error!
  1165.  
  1166.         push    ebx
  1167.         push    .check_more
  1168.         and     ecx, 0x00001FFF
  1169.         add     ecx, -4                         ; we dont need CRC
  1170.         DEBUGF  1,"data length = %u\n", ecx
  1171.         mov     eax, [esi + rx_desc.buf_soft_addr]
  1172.         push    eax
  1173.         mov     [eax + NET_BUFF.length], ecx
  1174.         mov     [eax + NET_BUFF.device], ebx
  1175.         mov     [eax + NET_BUFF.offset], NET_BUFF.data
  1176.  
  1177. ;-------------
  1178. ; Update stats
  1179.  
  1180.         add     dword [ebx + device.bytes_rx], eax
  1181.         adc     dword [ebx + device.bytes_rx + 4], 0
  1182.         inc     [ebx + device.packets_rx]
  1183.  
  1184. ;----------------------
  1185. ; Allocate a new buffer
  1186.  
  1187.         invoke  NetAlloc, RX_BUF_SIZE+NET_BUFF.data
  1188.         mov     [esi + rx_desc.buf_soft_addr], eax
  1189.         invoke  GetPhysAddr
  1190.         add     eax, NET_BUFF.data
  1191.         mov     dword [esi + rx_desc.buf_addr], eax
  1192.  
  1193. ;---------------
  1194. ; re set OWN bit
  1195.  
  1196.         mov     eax, DSB_OWNbit or RX_BUF_SIZE
  1197.         cmp     [ebx + device.cur_rx], NUM_RX_DESC - 1
  1198.         jne     @f
  1199.         or      eax, DSB_EORbit
  1200.     @@:
  1201.         mov     [esi + rx_desc.status], eax
  1202.  
  1203. ;--------------
  1204. ; Update rx ptr
  1205.  
  1206.         inc     [ebx + device.cur_rx]
  1207.         and     [ebx + device.cur_rx], NUM_RX_DESC - 1
  1208.  
  1209.         jmp     [EthInput]
  1210.   .rx_return:
  1211.  
  1212.         pop     ax
  1213.   .no_rx:
  1214.  
  1215. ;-----------------
  1216. ; Transmit cleanup
  1217.  
  1218.         test    ax, ISB_TxOK or ISB_TxErr or ISB_TxDescUnavail
  1219.         jz      .no_tx
  1220.         push    ax
  1221.  
  1222.         DEBUGF  1,"TX done!\n"
  1223.  
  1224.         mov     ecx, NUM_TX_DESC
  1225.         lea     esi, [ebx + device.tx_ring]
  1226.   .txloop:
  1227.         cmp     dword [esi + tx_desc.buf_soft_addr], 0
  1228.         jz      .maybenext
  1229.  
  1230.         test    [esi + tx_desc.status], DSB_OWNbit
  1231.         jnz     .maybenext
  1232.  
  1233.         push    ecx
  1234.         DEBUGF  1,"Freeing up TX desc: %x\n", esi
  1235.         invoke  NetFree, [esi + tx_desc.buf_soft_addr]
  1236.         pop     ecx
  1237.         and     dword [esi + tx_desc.buf_soft_addr], 0
  1238.  
  1239.   .maybenext:
  1240.         add     esi, sizeof.tx_desc
  1241.         dec     ecx
  1242.         jnz     .txloop
  1243.  
  1244.         pop     ax
  1245.   .no_tx:
  1246.  
  1247.         test    ax, ISB_LinkChg
  1248.         jz      .no_linkchange
  1249.         DEBUGF  2, "Link change detected\n"
  1250.         call    detect_link
  1251.   .no_linkchange:
  1252.  
  1253.         pop     edi esi ebx
  1254.         xor     eax, eax
  1255.         inc     eax
  1256.  
  1257.         ret
  1258.  
  1259.  
  1260.  
  1261. align 4
  1262. detect_link:
  1263.  
  1264.         set_io  [ebx + device.io_addr], 0
  1265.  
  1266. ;        set_io  [ebx + device.io_addr], REG_TBICSR
  1267. ;        in      eax, dx
  1268. ;        test    eax, TBI_LinkOK
  1269. ;        jz      .down
  1270.  
  1271. ;        mov     [ebx + device.state], ETH_LINK_UNKNOWN
  1272. ;        invoke  NetLinkChanged
  1273. ;        ret
  1274.  
  1275.         set_io  [ebx + device.io_addr], REG_PHYstatus
  1276.         in      al, dx
  1277.         test    al, PHYS_LinkStatus
  1278.         jz      .down
  1279.         DEBUGF  2, "Link is up, phystatus=0x%x\n", al
  1280.         xor     ecx, ecx
  1281.         test    al, PHYS_10bps
  1282.         jz      @f
  1283.         or      cl, ETH_LINK_10M
  1284.   @@:
  1285.         test    al, PHYS_100bps
  1286.         jz      @f
  1287.         or      cl, ETH_LINK_100M
  1288.   @@:
  1289.         test    al, PHYS_1000bpsF
  1290.         jz      @f
  1291.         or      cl, ETH_LINK_1G ;or ETH_LINK_FD
  1292.   @@:
  1293.         test    al, PHYS_FullDup
  1294.         jz      @f
  1295.         or      cl, ETH_LINK_FD
  1296.   @@:
  1297.         mov     [ebx + device.state], ecx
  1298.         invoke  NetLinkChanged
  1299.         ret
  1300.  
  1301.   .down:
  1302.         DEBUGF  2, "Link is down\n"
  1303.         mov     [ebx + device.state], ETH_LINK_DOWN
  1304.         invoke  NetLinkChanged
  1305.         ret
  1306.  
  1307.  
  1308.  
  1309. ; End of code
  1310.  
  1311. data fixups
  1312. end data
  1313.  
  1314. include '../peimport.inc'
  1315.  
  1316. my_service    db 'RTL8169',0                    ; max 16 chars include zero
  1317.  
  1318. include_debug_strings                           ; All data wich FDO uses will be included here
  1319.  
  1320. MAC_VERSION_LIST:
  1321.  
  1322. ; 8168EP family.
  1323. dd 0x7cf00000, 0x50200000, 51, name_49
  1324. dd 0x7cf00000, 0x50100000, 50, name_49
  1325. dd 0x7cf00000, 0x50000000, 49, name_49
  1326.  
  1327. ; 8168H family.
  1328. dd 0x7cf00000, 0x54100000, 46, name_45
  1329. dd 0x7cf00000, 0x54000000, 45, name_45
  1330.  
  1331. ; 8168G family.
  1332. dd 0x7cf00000, 0x5c800000, 44, name_44
  1333. dd 0x7cf00000, 0x50900000, 42, name_40
  1334. dd 0x7cf00000, 0x4c100000, 41, name_40
  1335. dd 0x7cf00000, 0x4c000000, 40, name_40
  1336.  
  1337. ; 8168F family.
  1338. dd 0x7c800000, 0x48800000, 38, name_38
  1339. dd 0x7cf00000, 0x48100000, 36, name_35
  1340. dd 0x7cf00000, 0x48000000, 35, name_35
  1341.  
  1342. ; 8168E family.
  1343. dd 0x7c800000, 0x2c800000, 34, name_34
  1344. dd 0x7cf00000, 0x2c200000, 33, name_32
  1345. dd 0x7cf00000, 0x2c100000, 32, name_32
  1346. dd 0x7c800000, 0x2c000000, 33, name_32
  1347.  
  1348. ; 8168D family.
  1349. dd 0x7cf00000, 0x28300000, 26, name_25
  1350. dd 0x7cf00000, 0x28100000, 25, name_25
  1351. dd 0x7c800000, 0x28000000, 26, name_25
  1352.  
  1353. ; 8168DP family.
  1354. dd 0x7cf00000, 0x28800000, 27, name_27
  1355. dd 0x7cf00000, 0x28a00000, 28, name_27
  1356.  
  1357. ; 8168C family.
  1358. dd 0x7cf00000, 0x3cb00000, 24, name_18
  1359. dd 0x7cf00000, 0x3c900000, 23, name_18
  1360. dd 0x7cf00000, 0x3c800000, 18, name_18
  1361. dd 0x7c800000, 0x3c800000, 24, name_18
  1362. dd 0x7cf00000, 0x3c000000, 19, name_19
  1363. dd 0x7cf00000, 0x3c200000, 20, name_19
  1364. dd 0x7cf00000, 0x3c300000, 21, name_19
  1365. dd 0x7cf00000, 0x3c400000, 22, name_19
  1366. dd 0x7c800000, 0x3c000000, 22, name_19
  1367.  
  1368. ; 8168B family.
  1369. dd 0x7cf00000, 0x38000000, 12, name_11
  1370. dd 0x7cf00000, 0x38500000, 17, name_10
  1371. dd 0x7c800000, 0x38000000, 17, name_10
  1372. dd 0x7c800000, 0x30000000, 11, name_11
  1373.  
  1374. ; 8101 family.
  1375. dd 0x7cf00000, 0x34a00000, 09, name_07
  1376. dd 0x7cf00000, 0x24a00000, 09, name_07
  1377. dd 0x7cf00000, 0x34900000, 08, name_07
  1378. dd 0x7cf00000, 0x24900000, 08, name_07
  1379. dd 0x7cf00000, 0x34800000, 07, name_07
  1380. dd 0x7cf00000, 0x24800000, 07, name_07
  1381. dd 0x7cf00000, 0x34000000, 13, name_10
  1382. dd 0x7cf00000, 0x34300000, 10, name_10
  1383. dd 0x7cf00000, 0x34200000, 16, name_11
  1384. dd 0x7c800000, 0x34800000, 09, name_07
  1385. dd 0x7c800000, 0x24800000, 09, name_07
  1386. dd 0x7c800000, 0x34000000, 16, name_11
  1387. dd 0xfc800000, 0x38800000, 15, name_14
  1388. dd 0xfc800000, 0x30800000, 14, name_14
  1389.  
  1390. ; 8110 family.
  1391. dd 0xfc800000, 0x98000000, 06, name_05
  1392. dd 0xfc800000, 0x18000000, 05, name_05
  1393. dd 0xfc800000, 0x10000000, 04, name_04
  1394. dd 0xfc800000, 0x04000000, 03, name_03
  1395. dd 0xfc800000, 0x00800000, 02, name_02
  1396. dd 0xfc800000, 0x00000000, 01, name_01
  1397.  
  1398. ; Catch-all
  1399. dd 0x00000000, 0x00000000, 0, name_unknown
  1400.  
  1401. ; PCI-devices
  1402. name_01 db "RTL8169",0
  1403. name_02 db "RTL8169s",0
  1404. name_03 db "RTL8110s",0
  1405. name_04 db "RTL8169sb/8110sb",0
  1406. name_05 db "RTL8169sc/8110sc",0
  1407. ;name_06 db "RTL8169sc/8110sc",0
  1408.  
  1409. ; PCI-E devices
  1410. name_07 db "RTL8102e",0
  1411. ;name_08 db "RTL8102e",0
  1412. ;name_09 db "RTL8102e",0
  1413. name_10 db "RTL8101e",0
  1414. name_11 db "RTL8168b/8111b",0
  1415. ;name_12 db "RTL8168b/8111b",0
  1416. ;name_13 db "RTL8101e",0
  1417. name_14 db "RTL8100e",0
  1418. ;name_15 db "RTL8100e",0
  1419. ;name_16 db "RTL8168b/8111b",0
  1420. ;name_17 db "RTL8101e",0
  1421. name_18 db "RTL8168cp/8111cp",0
  1422. name_19 db "RTL8168c/8111c",0
  1423. ;name_20 db "RTL8168c/8111c",0
  1424. ;name_21 db "RTL8168c/8111c",0
  1425. ;name_22 db "RTL8168c/8111c",0
  1426. ;name_23 db "RTL8168cp/8111cp",0
  1427. ;name_24 db "RTL8168cp/8111cp",0
  1428. name_25 db "RTL8168d/8111d",0
  1429. ;name_26 db "RTL8168d/8111d",0
  1430. name_27 db "RTL8168dp/8111dp",0
  1431. ;name_28 db "RTL8168dp/8111dp",0
  1432. name_29 db "RTL8105e",0
  1433. ;name_30 db "RTL8105e",0
  1434. ;name_31 db "RTL8168dp/8111dp",0
  1435. name_32 db "RTL8168e/8111e",0
  1436. ;name_33 db "RTL8168e/8111e",0
  1437. name_34 db "RTL8168evl/8111evl",0
  1438. name_35 db "RTL8168f/8111f",0
  1439. ;name_36 db "RTL8168f/8111f",0
  1440. name_37 db "RTL8402",0
  1441. name_38 db "RTL8411",0
  1442. name_39 db "RTL8106e",0
  1443. name_40 db "RTL8168g/8111g",0
  1444. ;name_41 db "RTL8168g/8111g",0
  1445. ;name_42 db "RTL8168g/8111g",0
  1446. ;name_43 db "RTL8106e",0
  1447. name_44 db "RTL8411",0
  1448. name_45 db "RTL8168h/8111h",0
  1449. ;name_46 db "RTL8168h/8111h",0
  1450. name_47 db "RTL8107e",0
  1451. ;name_48 db "RTL8107e",0
  1452. name_49 db "RTL8168ep/8111ep",0
  1453. ;name_50 db "RTL8168ep/8111ep",0
  1454. ;name_51 db "RTL8168ep/8111ep",0
  1455.  
  1456. name_unknown db "unknown RTL8169 clone",0
  1457.  
  1458. align 4
  1459. devices         dd 0
  1460. device_list rd MAX_DEVICES                     ; This list contains all pointers to device structures the driver is handling
  1461.  
  1462.  
  1463.