Subversion Repositories Kolibri OS

Rev

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

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2004-2014. All rights reserved.    ;;
  4. ;; Distributed under terms of the GNU General Public License       ;;
  5. ;;                                                                 ;;
  6. ;;  Realtek 8139 driver for KolibriOS                              ;;
  7. ;;                                                                 ;;
  8. ;;  based on RTL8139.asm driver for menuetos                       ;;
  9. ;;  and realtek8139.asm for SolarOS by Eugen Brasoveanu            ;;
  10. ;;                                                                 ;;
  11. ;;    Written by hidnplayr@kolibrios.org                           ;;
  12. ;;                                                                 ;;
  13. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  14. ;;             Version 2, June 1991                                ;;
  15. ;;                                                                 ;;
  16. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  17.  
  18. format PE DLL native
  19. entry START
  20.  
  21.         CURRENT_API             = 0x0200
  22.         COMPATIBLE_API          = 0x0100
  23.         API_VERSION             = (COMPATIBLE_API shl 16) + CURRENT_API
  24.  
  25.         MAX_DEVICES             = 16
  26.  
  27.         RBLEN                   = 3     ; Receive buffer size: 0==8K 1==16k 2==32k 3==64k
  28.  
  29.         TXRR                    = 8     ; total retries = 16+(TXRR*16)
  30.         TX_MXDMA                = 6     ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=2048
  31.         ERTXTH                  = 8     ; in unit of 32 bytes e.g:(8*32)=256
  32.         RX_MXDMA                = 7     ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=unlimited
  33.         RXFTH                   = 7     ; 0=16 1=32 2=64 3=128 4=256 5=512 6=1024 7=no threshold
  34.  
  35.         __DEBUG__               = 1
  36.         __DEBUG_LEVEL__         = 2     ; 1 = verbose, 2 = errors only
  37.  
  38. section '.flat' readable writable executable
  39.  
  40. include '../proc32.inc'
  41. include '../struct.inc'
  42. include '../macros.inc'
  43. include '../fdo.inc'
  44. include '../netdrv.inc'
  45.  
  46.         REG_IDR0                = 0x00
  47.         REG_MAR0                = 0x08 ; multicast filter register 0
  48.         REG_MAR4                = 0x0c ; multicast filter register 4
  49.         REG_TSD0                = 0x10 ; transmit status of descriptor
  50.         REG_TSAD0               = 0x20 ; transmit start address of descriptor
  51.         REG_RBSTART             = 0x30 ; RxBuffer start address
  52.         REG_COMMAND             = 0x37 ; command register
  53.         REG_CAPR                = 0x38 ; current address of packet read (word) R/W
  54.         REG_IMR                 = 0x3c ; interrupt mask register
  55.         REG_ISR                 = 0x3e ; interrupt status register
  56.         REG_TXCONFIG            = 0x40 ; transmit configuration register
  57.         REG_RXCONFIG            = 0x44 ; receive configuration register 0
  58.         REG_MPC                 = 0x4c ; missed packet counter
  59.         REG_9346CR              = 0x50 ; serial eeprom 93C46 command register
  60.         REG_CONFIG1             = 0x52 ; configuration register 1
  61.         REG_MSR                 = 0x58
  62.         REG_CONFIG4             = 0x5a ; configuration register 4
  63.         REG_HLTCLK              = 0x5b ; undocumented halt clock register
  64.         REG_BMCR                = 0x62 ; basic mode control register
  65.         REG_ANAR                = 0x66 ; auto negotiation advertisement register
  66.         REG_9346CR_WE           = 11b shl 6
  67.  
  68.         BIT_RUNT                = 4 ; total packet length < 64 bytes
  69.         BIT_LONG                = 3 ; total packet length > 4k
  70.         BIT_CRC                 = 2 ; crc error occured
  71.         BIT_FAE                 = 1 ; frame alignment error occured
  72.         BIT_ROK                 = 0 ; received packet is ok
  73.  
  74.         BIT_RST                 = 4 ; reset bit
  75.         BIT_RE                  = 3 ; receiver enabled
  76.         BIT_TE                  = 2 ; transmitter enabled
  77.         BUFE                    = 1 ; rx buffer is empty, no packet stored
  78.  
  79.         BIT_ISR_TOK             = 2 ; transmit ok
  80.         BIT_ISR_RER             = 1 ; receive error interrupt
  81.         BIT_ISR_ROK             = 0 ; receive ok
  82.  
  83.         BIT_TX_MXDMA            = 8 ; Max DMA burst size per Tx DMA burst
  84.         BIT_TXRR                = 4 ; Tx Retry count 16+(TXRR*16)
  85.  
  86.         BIT_RXFTH               = 13 ; Rx fifo threshold
  87.         BIT_RBLEN               = 11 ; Ring buffer length indicator
  88.         BIT_RX_MXDMA            = 8 ; Max DMA burst size per Rx DMA burst
  89.         BIT_NOWRAP              = 7 ; transfered data wrapping
  90.         BIT_9356SEL             = 6 ; eeprom selector 9346/9356
  91.         BIT_AER                 = 5 ; accept error packets
  92.         BIT_AR                  = 4 ; accept runt packets
  93.         BIT_AB                  = 3 ; accept broadcast packets
  94.         BIT_AM                  = 2 ; accept multicast packets
  95.         BIT_APM                 = 1 ; accept physical match packets
  96.         BIT_AAP                 = 0 ; accept all packets
  97.  
  98.         BIT_93C46_EEM1          = 7 ; RTL8139 eeprom operating mode1
  99.         BIT_93C46_EEM0          = 6 ; RTL8139 eeprom operating mode0
  100.         BIT_93C46_EECS          = 3 ; chip select
  101.         BIT_93C46_EESK          = 2 ; serial data clock
  102.         BIT_93C46_EEDI          = 1 ; serial data input
  103.         BIT_93C46_EEDO          = 0 ; serial data output
  104.  
  105.         BIT_LWACT               = 4 ; see REG_CONFIG1
  106.         BIT_SLEEP               = 1 ; sleep bit at older chips
  107.         BIT_PWRDWN              = 0 ; power down bit at older chips
  108.         BIT_PMEn                = 0 ; power management enabled
  109.  
  110.         BIT_LWPTN               = 2 ; see REG_CONFIG4
  111.  
  112.         BIT_ERTXTH              = 16 ; early TX threshold
  113.         BIT_TOK                 = 15 ; transmit ok
  114.         BIT_OWN                 = 13 ; tx DMA operation is completed
  115.  
  116.         BIT_ANE                 = 12 ; auto negotiation enable
  117.  
  118.         BIT_TXFD                = 8 ; 100base-T full duplex
  119.         BIT_TX                  = 7 ; 100base-T
  120.         BIT_10FD                = 6 ; 10base-T full duplex
  121.         BIT_10                  = 5 ; 10base-T
  122.         BIT_SELECTOR            = 0 ; binary encoded selector CSMA/CD=00001
  123.  
  124.         BIT_IFG1                = 25
  125.         BIT_IFG0                = 24
  126.  
  127.         RX_CONFIG               = (RBLEN shl BIT_RBLEN) or \
  128.                                   (RX_MXDMA shl BIT_RX_MXDMA) or \
  129.                                   (1 shl BIT_NOWRAP) or \
  130.                                   (RXFTH shl BIT_RXFTH) or\
  131.                                   (1 shl BIT_AB) or \                   ; Accept broadcast packets
  132.                                   (1 shl BIT_APM) or \                  ; Accept physical match packets
  133.                                   (1 shl BIT_AER) or \                  ; Accept error packets
  134.                                   (1 shl BIT_AR) or \                   ; Accept Runt packets (smaller then 64 bytes)
  135.                                   (1 shl BIT_AM)                        ; Accept multicast packets
  136.  
  137.         RX_BUFFER_SIZE          = (8192 shl RBLEN);+16+1500
  138.         NUM_TX_DESC             = 4                                     ; not user selectable
  139.  
  140.         EE_93C46_REG_ETH_ID     = 7 ; MAC offset
  141.         EE_93C46_READ_CMD       = (6 shl 6) ; 110b + 6bit address
  142.         EE_93C56_READ_CMD       = (6 shl 8) ; 110b + 8bit address
  143.         EE_93C46_CMD_LENGTH     = 9  ; start bit + cmd + 6bit address
  144.         EE_93C56_CMD_LENGTH     = 11 ; start bit + cmd + 8bit ddress
  145.  
  146.         VER_RTL8139             = 1100000b
  147.         VER_RTL8139A            = 1110000b
  148.         VER_RTL8139AG           = 1110100b
  149.         VER_RTL8139B            = 1111000b
  150.         VER_RTL8130             = VER_RTL8139B
  151.         VER_RTL8139C            = 1110100b
  152.         VER_RTL8100             = 1111010b
  153.         VER_RTL8100B            = 1110101b
  154.         VER_RTL8139D            = VER_RTL8100B
  155.         VER_RTL8139CP           = 1110110b
  156.         VER_RTL8101             = 1110111b
  157.  
  158.         IDX_RTL8139             = 0
  159.         IDX_RTL8139A            = 1
  160.         IDX_RTL8139B            = 2
  161.         IDX_RTL8139C            = 3
  162.         IDX_RTL8100             = 4
  163.         IDX_RTL8139D            = 5
  164.         IDX_RTL8139D            = 6
  165.         IDX_RTL8101             = 7
  166.  
  167.         ISR_SERR                = 1 shl 15
  168.         ISR_TIMEOUT             = 1 shl 14
  169.         ISR_LENCHG              = 1 shl 13
  170.         ISR_FIFOOVW             = 1 shl 6
  171.         ISR_PUN                 = 1 shl 5
  172.         ISR_RXOVW               = 1 shl 4
  173.         ISR_TER                 = 1 shl 3
  174.         ISR_TOK                 = 1 shl 2
  175.         ISR_RER                 = 1 shl 1
  176.         ISR_ROK                 = 1 shl 0
  177.  
  178.         INTERRUPT_MASK          = ISR_ROK or \
  179.                                   ISR_RER or \
  180.                                   ISR_TOK or \
  181.                                   ISR_TER or \
  182.                                   ISR_RXOVW or \
  183.                                   ISR_PUN or \
  184.                                   ISR_FIFOOVW or \
  185.                                   ISR_LENCHG or \
  186.                                   ISR_TIMEOUT or \
  187.                                   ISR_SERR
  188.  
  189.         TSR_OWN                 = 1 shl 13
  190.         TSR_TUN                 = 1 shl 14
  191.         TSR_TOK                 = 1 shl 15
  192.  
  193.         TSR_CDH                 = 1 shl 28
  194.         TSR_OWC                 = 1 shl 29
  195.         TSR_TABT                = 1 shl 30
  196.         TSR_CRS                 = 1 shl 31
  197.  
  198.  
  199. struct  device          ETH_DEVICE
  200.  
  201.         io_addr         dd ?
  202.         pci_bus         dd ?
  203.         pci_dev         dd ?
  204.         irq_line        db ?
  205.                         rb 3    ; align 4
  206.  
  207.         rx_buffer       dd ?
  208.         rx_data_offset  dd ?
  209.         curr_tx_desc    db ?
  210.         hw_ver_id       db ?
  211.                         rb 2    ; align 4
  212.  
  213.         TX_DESC         rd NUM_TX_DESC
  214.  
  215. ends
  216.  
  217.  
  218. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  219. ;;                        ;;
  220. ;; proc START             ;;
  221. ;;                        ;;
  222. ;; (standard driver proc) ;;
  223. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  224.  
  225. proc START c, reason:dword, cmdline:dword
  226.  
  227.         cmp     [reason], DRV_ENTRY
  228.         jne     .fail
  229.  
  230.         DEBUGF  2,"Loading driver\n"
  231.         invoke  RegService, my_service, service_proc
  232.         ret
  233.  
  234.   .fail:
  235.         xor     eax, eax
  236.         ret
  237.  
  238. endp
  239.  
  240.  
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;;                        ;;
  243. ;; proc SERVICE_PROC      ;;
  244. ;;                        ;;
  245. ;; (standard driver proc) ;;
  246. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  247.  
  248. proc service_proc stdcall, ioctl:dword
  249.  
  250.         mov     edx, [ioctl]
  251.         mov     eax, [edx + IOCTL.io_code]
  252.  
  253. ;------------------------------------------------------
  254.  
  255.         cmp     eax, 0 ;SRV_GETVERSION
  256.         jne     @F
  257.  
  258.         cmp     [edx + IOCTL.out_size], 4
  259.         jb      .fail
  260.         mov     eax, [edx + IOCTL.output]
  261.         mov     [eax], dword API_VERSION
  262.  
  263.         xor     eax, eax
  264.         ret
  265.  
  266. ;------------------------------------------------------
  267.   @@:
  268.         cmp     eax, 1 ;SRV_HOOK
  269.         jne     .fail
  270.  
  271.         cmp     [edx + IOCTL.inp_size], 3               ; Data input must be at least 3 bytes
  272.         jb      .fail
  273.  
  274.         mov     eax, [edx + IOCTL.input]
  275.         cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
  276.         jne     .fail                                   ; other types arent supported for this card yet
  277.  
  278. ; check if the device is already listed
  279.  
  280.         mov     esi, device_list
  281.         mov     ecx, [devices]
  282.         test    ecx, ecx
  283.         jz      .firstdevice
  284.  
  285.         mov     ax, [eax+1]                             ; get the pci bus and device numbers
  286.   .nextdevice:
  287.         mov     ebx, [esi]
  288.         cmp     al, byte[ebx + device.pci_bus]
  289.         jne     @f
  290.         cmp     ah, byte[ebx + device.pci_dev]
  291.         je      .find_devicenum                         ; Device is already loaded, let's find it's device number
  292.        @@:
  293.         add     esi, 4
  294.         loop    .nextdevice
  295.  
  296.  
  297. ; This device doesnt have its own eth_device structure yet, lets create one
  298.   .firstdevice:
  299.         cmp     [devices], MAX_DEVICES                  ; First check if the driver can handle one more card
  300.         jae     .fail
  301.  
  302.         allocate_and_clear ebx, sizeof.device, .fail      ; Allocate the buffer for device structure
  303.  
  304. ; Fill in the direct call addresses into the struct
  305.  
  306.         mov     [ebx + device.reset], reset
  307.         mov     [ebx + device.transmit], transmit
  308.         mov     [ebx + device.unload], unload
  309.         mov     [ebx + device.name], my_service
  310.  
  311. ; save the pci bus and device numbers
  312.  
  313.         mov     eax, [edx + IOCTL.input]
  314.         movzx   ecx, byte[eax+1]
  315.         mov     [ebx + device.pci_bus], ecx
  316.         movzx   ecx, byte[eax+2]
  317.         mov     [ebx + device.pci_dev], ecx
  318.  
  319. ; Now, it's time to find the base io addres of the PCI device
  320.  
  321.         stdcall PCI_find_io, [ebx + device.pci_bus], [ebx + device.pci_dev]
  322.         mov     [ebx + device.io_addr], eax
  323.  
  324. ; We've found the io address, find IRQ now
  325.  
  326.         invoke  PciRead8, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.interrupt_line
  327.         mov     [ebx + device.irq_line], al
  328.  
  329.         DEBUGF  1, "Hooking into device, dev:%x, bus:%x, irq:%x, I/O addr:%x\n",\
  330.         [ebx + device.pci_dev]:1,[ebx + device.pci_bus]:1,[ebx + device.irq_line]:1,[ebx + device.io_addr]:4
  331.  
  332. ; Allocate the receive buffer
  333.  
  334.         invoke  CreateRingBuffer, dword (RX_BUFFER_SIZE), dword PG_SW
  335.         test    eax, eax
  336.         jz      .err
  337.         mov     [ebx + device.rx_buffer], eax
  338.  
  339. ; Ok, the eth_device structure is ready, let's probe the device
  340.  
  341.         call    probe                                                   ; this function will output in eax
  342.         test    eax, eax
  343.         jnz     .err                                                    ; If an error occured, exit
  344.  
  345.         mov     eax, [devices]                                          ; Add the device structure to our device list
  346.         mov     [device_list+4*eax], ebx                                ; (IRQ handler uses this list to find device)
  347.         inc     [devices]                                               ;
  348.  
  349.         call    reset
  350.         test    eax, eax
  351.         jnz     .destroy
  352.  
  353.         mov     [ebx + device.type], NET_TYPE_ETH
  354.         invoke  NetRegDev
  355.  
  356.         cmp     eax, -1
  357.         je      .destroy
  358.  
  359.         ret
  360.  
  361. ; If the device was already loaded, find the device number and return it in eax
  362.  
  363.   .find_devicenum:
  364.         DEBUGF  1, "Trying to find device number of already registered device\n"
  365.         invoke  NetPtrToNum                                             ; This kernel procedure converts a pointer to device struct in ebx
  366.                                                                         ; into a device number in edi
  367.         mov     eax, edi                                                ; Application wants it in eax instead
  368.         DEBUGF  1, "Kernel says: %u\n", eax
  369.         ret
  370.  
  371. ; If an error occured, remove all allocated data and exit (returning -1 in eax)
  372.  
  373.   .destroy:
  374.         ; todo: unregister device from device_list
  375.         ; todo: reset device into virgin state
  376.  
  377.   .err:
  378.         DEBUGF  2, "Error, removing all data !\n"
  379.         invoke  KernelFree, [ebx + device.rx_buffer]
  380.         invoke  KernelFree, ebx
  381.  
  382.   .fail:
  383.         or      eax, -1
  384.         ret
  385.  
  386. ;------------------------------------------------------
  387. endp
  388.  
  389.  
  390. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  391. ;;                                                                        ;;
  392. ;;        Actual Hardware dependent code starts here                      ;;
  393. ;;                                                                        ;;
  394. ;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
  395.  
  396. align 4
  397. unload:
  398.         ; TODO: (in this particular order)
  399.         ;
  400.         ; - Stop the device
  401.         ; - Detach int handler
  402.         ; - Remove device from local list (RTL8139_LIST)
  403.         ; - call unregister function in kernel
  404.         ; - Remove all allocated structures and buffers the card used
  405.  
  406.         or      eax, -1
  407.  
  408. ret
  409.  
  410.  
  411. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  412. ;;
  413. ;;  probe: enables the device (if it really is RTL8139)
  414. ;;
  415. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  416.  
  417. align 4
  418. probe:
  419.         DEBUGF  1, "Probing\n"
  420.  
  421. ; Make the device a bus master
  422.         invoke  PciRead32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command
  423.         or      al, PCI_CMD_MASTER
  424.         invoke  PciWrite32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command, eax
  425.  
  426. ; get chip version
  427.         set_io  [ebx + device.io_addr], 0
  428.         set_io  [ebx + device.io_addr], REG_TXCONFIG + 2
  429.         in      ax, dx
  430.         shr     ah, 2
  431.         shr     ax, 6
  432.         and     al, 01111111b
  433.  
  434. ; now find it in our array
  435.         mov     ecx, HW_VER_ARRAY_SIZE-1
  436.   .chip_ver_loop:
  437.         cmp     al, [hw_ver_array + ecx]
  438.         je      .chip_ver_found
  439.         dec     ecx
  440.         jns     .chip_ver_loop
  441.   .unknown:
  442.         mov     ecx, 8
  443.   .chip_ver_found:
  444.         cmp     ecx, 8
  445.         ja      .unknown
  446.  
  447.         mov     [ebx + device.hw_ver_id], cl
  448.  
  449.         mov     ecx, [crosslist+ecx*4]
  450.         mov     [ebx + device.name], ecx
  451.  
  452.         DEBUGF  1, "Chip version: %s\n", ecx
  453.  
  454. ; wake up the chip
  455.         set_io  [ebx + device.io_addr], 0
  456.         set_io  [ebx + device.io_addr], REG_HLTCLK
  457.         mov     al, 'R'         ; run the clock
  458.         out     dx, al
  459.  
  460. ; unlock config and BMCR registers
  461.         set_io  [ebx + device.io_addr], REG_9346CR
  462.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  463.         out     dx, al
  464.  
  465. ; enable power management
  466.         set_io  [ebx + device.io_addr], REG_CONFIG1
  467.         in      al, dx
  468.         cmp     [ebx + device.hw_ver_id], IDX_RTL8139B
  469.         jae     .new_chip
  470. ; wake up older chips
  471.         and     al, not ((1 shl BIT_SLEEP) or (1 shl BIT_PWRDWN))
  472.         out     dx, al
  473.         jmp     .finish_wake_up
  474.  
  475. ; set LWAKE pin to active high (default value).
  476. ; it is for Wake-On-LAN functionality of some motherboards.
  477. ; this signal is used to inform the motherboard to execute a wake-up process.
  478. ; only at newer chips.
  479.   .new_chip:
  480.         or      al, (1 shl BIT_PMEn)
  481.         and     al, not (1 shl BIT_LWACT)
  482.         out     dx, al
  483.  
  484.         set_io  [ebx + device.io_addr], REG_CONFIG4
  485.         in      al, dx
  486.         and     al, not (1 shl BIT_LWPTN)
  487.         out     dx, al
  488.  
  489. ; lock config and BMCR registers
  490.   .finish_wake_up:
  491.         xor     al, al
  492.         set_io  [ebx + device.io_addr], 0
  493.         set_io  [ebx + device.io_addr], REG_9346CR
  494.         out     dx, al
  495.         DEBUGF  1, "probing done!\n"
  496.  
  497.         xor     eax, eax
  498.  
  499.         ret
  500.  
  501.  
  502. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  503. ;;
  504. ;;   reset: Set up all registers and descriptors, clear some values
  505. ;;
  506. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  507.  
  508. reset:
  509.         DEBUGF  1, "Reset\n"
  510.  
  511. ; attach int handler
  512.         movzx   eax, [ebx + device.irq_line]
  513.         DEBUGF  1, "Attaching int handler to irq %x\n", eax:1
  514.         invoke  AttachIntHandler, eax, int_handler, ebx
  515.         test    eax, eax
  516.         jnz     @f
  517.         DEBUGF  2, "Could not attach int handler!\n"
  518.         or      eax, -1
  519.         ret
  520.        @@:
  521.  
  522. ; reset chip
  523.         DEBUGF  1, "Resetting chip\n"
  524.         set_io  [ebx + device.io_addr], 0
  525.         set_io  [ebx + device.io_addr], REG_COMMAND
  526.         mov     al, 1 shl BIT_RST
  527.         out     dx, al
  528.         mov     cx, 1000                ; wait no longer for the reset
  529.   .wait_for_reset:
  530.         in      al, dx
  531.         test    al, 1 shl BIT_RST
  532.         jz      .reset_completed        ; RST remains 1 during reset
  533.         dec     cx
  534.         jns     .wait_for_reset
  535.         DEBUGF  2, "Reset timeout!\n"
  536.   .reset_completed:
  537.  
  538. ; Read MAC address
  539.         call    read_mac
  540.  
  541. ; unlock config and BMCR registers
  542.         set_io  [ebx + device.io_addr], 0
  543.         set_io  [ebx + device.io_addr], REG_9346CR
  544.         mov     al, (1 shl BIT_93C46_EEM1) or (1 shl BIT_93C46_EEM0)
  545.         out     dx, al
  546.  
  547. ; initialize multicast registers (no filtering)
  548.         mov     eax, 0xffffffff
  549.         set_io  [ebx + device.io_addr], REG_MAR0
  550.         out     dx, eax
  551.         set_io  [ebx + device.io_addr], REG_MAR4
  552.         out     dx, eax
  553.  
  554. ; enable Rx/Tx
  555.         mov     al, (1 shl BIT_RE) or (1 shl BIT_TE)
  556.         set_io  [ebx + device.io_addr], REG_COMMAND
  557.         out     dx, al
  558.  
  559. ; Rxbuffer size, unlimited dma burst, no wrapping, no rx threshold
  560. ; accept broadcast packets, accept physical match packets
  561.         mov     eax, RX_CONFIG
  562.         set_io  [ebx + device.io_addr], REG_RXCONFIG
  563.         out     dx, eax
  564.  
  565. ; 1024 bytes DMA burst, total retries = 16 + 8 * 16 = 144
  566.         mov     eax, (TX_MXDMA shl BIT_TX_MXDMA) or (TXRR shl BIT_TXRR) or BIT_IFG1 or BIT_IFG0
  567.         set_io  [ebx + device.io_addr], REG_TXCONFIG
  568.         out     dx, eax
  569.  
  570. ; enable auto negotiation
  571.         set_io  [ebx + device.io_addr], REG_BMCR
  572.         in      ax, dx
  573.         or      ax, (1 shl BIT_ANE)
  574.         out     dx, ax
  575.  
  576. ; set auto negotiation advertisement
  577.         set_io  [ebx + device.io_addr], REG_ANAR
  578.         in      ax, dx
  579.         or      ax, (1 shl BIT_SELECTOR) or (1 shl BIT_10) or (1 shl BIT_10FD) or (1 shl BIT_TX) or (1 shl BIT_TXFD)
  580.         out     dx, ax
  581.  
  582. ; lock config and BMCR registers
  583.         xor     eax, eax
  584.         set_io  [ebx + device.io_addr], REG_9346CR
  585.         out     dx, al
  586.  
  587. ; init RX/TX pointers
  588.         mov     [ebx + device.rx_data_offset], eax
  589.         mov     [ebx + device.curr_tx_desc], al
  590. ;        set_io  [ebx + device.io_addr], REG_CAPR
  591. ;        out     dx, ax
  592.  
  593. ; clear packet/byte counters
  594.         lea     edi, [ebx + device.bytes_tx]
  595.         mov     ecx, 6
  596.         rep     stosd
  597.  
  598. ; clear missing packet counter
  599.         set_io  [ebx + device.io_addr], REG_MPC
  600.         out     dx, eax
  601.  
  602. ; set RxBuffer address, init RX buffer offset
  603.         mov     eax, [ebx + device.rx_buffer]
  604.         mov     dword[eax], 0                   ; clear receive flags for first packet (really needed??)
  605.         DEBUGF  1, "RX buffer virtual addr=0x%x\n", eax
  606.         invoke  GetPhysAddr
  607.         DEBUGF  1, "RX buffer physical addr=0x%x\n", eax
  608.         set_io  [ebx + device.io_addr], REG_RBSTART
  609.         out     dx, eax
  610.  
  611. ; enable interrupts
  612.         set_io  [ebx + device.io_addr], 0
  613.         set_io  [ebx + device.io_addr], REG_IMR
  614.         mov     ax, INTERRUPT_MASK
  615.         out     dx, ax
  616.  
  617. ; Set the mtu, kernel will be able to send now
  618.         mov     [ebx + device.mtu], 1514
  619.  
  620.         call    cable
  621.  
  622. ; Indicate that we have successfully reset the card
  623.         xor     eax, eax
  624.         ret
  625.  
  626.  
  627. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  628. ;;                                         ;;
  629. ;; Transmit                                ;;
  630. ;;                                         ;;
  631. ;; In: buffer pointer in [esp+4]           ;;
  632. ;;     size of buffer in [esp+8]           ;;
  633. ;;     pointer to device structure in ebx  ;;
  634. ;;                                         ;;
  635. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  636.  
  637. proc transmit stdcall bufferptr, buffersize
  638.  
  639.         pushf
  640.         cli
  641.  
  642.         DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n", [bufferptr], [buffersize]
  643.         mov     eax, [bufferptr]
  644.         DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
  645.         [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
  646.         [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
  647.         [eax+13]:2,[eax+12]:2
  648.  
  649.         cmp     [buffersize], 1514
  650.         ja      .fail
  651.         cmp     [buffersize], 60
  652.         jb      .fail
  653.  
  654. ; check if we own the current discriptor
  655.         set_io  [ebx + device.io_addr], 0
  656.         set_io  [ebx + device.io_addr], REG_TSD0
  657.         movzx   ecx, [ebx + device.curr_tx_desc]
  658.         shl     ecx, 2
  659.         add     edx, ecx
  660.         in      eax, dx
  661.         test    eax, (1 shl BIT_OWN)
  662.         jz      .wait_to_send
  663.  
  664.   .send_packet:
  665. ; get next descriptor
  666.         inc     [ebx + device.curr_tx_desc]
  667.         and     [ebx + device.curr_tx_desc], NUM_TX_DESC-1
  668.  
  669. ; Update stats
  670.         inc     [ebx + device.packets_tx]
  671.         mov     eax, [buffersize]
  672.         add     dword [ebx + device.bytes_tx], eax
  673.         adc     dword [ebx + device.bytes_tx+4], 0
  674.  
  675. ; Set the buffer address
  676.         set_io  [ebx + device.io_addr], REG_TSAD0
  677.         mov     eax, [bufferptr]
  678.         mov     [ebx + device.TX_DESC+ecx], eax
  679.         invoke  GetPhysAddr
  680.         out     dx, eax
  681.  
  682. ; And the size of the buffer
  683.         set_io  [ebx + device.io_addr], REG_TSD0
  684.         mov     eax, [buffersize]
  685.         or      eax, (ERTXTH shl BIT_ERTXTH)    ; Early threshold
  686.         out     dx, eax
  687.  
  688.         DEBUGF  1, "Packet Sent!\n"
  689.         popf
  690.         xor     eax, eax
  691.         ret
  692.  
  693.   .wait_to_send:
  694.         DEBUGF  1, "Waiting for timeout\n"
  695.  
  696.         push    edx
  697.         mov     esi, 30
  698.         invoke  Sleep
  699.         pop     edx
  700.  
  701.         in      ax, dx
  702.         test    ax, (1 shl BIT_OWN)
  703.         jnz     .send_packet
  704.  
  705.         pusha
  706.         call    reset                            ; if chip hung, reset it
  707.         popa
  708.  
  709.         jmp     .send_packet
  710.  
  711.   .fail:
  712.         DEBUGF  2, "transmit failed!\n"
  713.         invoke  KernelFree, [bufferptr]
  714.         popf
  715.         or      eax, -1
  716.         ret
  717.  
  718. endp
  719.  
  720.  
  721.  
  722.  
  723.  
  724. ;;;;;;;;;;;;;;;;;;;;;;;
  725. ;;                   ;;
  726. ;; Interrupt handler ;;
  727. ;;                   ;;
  728. ;;;;;;;;;;;;;;;;;;;;;;;
  729.  
  730. align 4
  731. int_handler:
  732.  
  733.         push    ebx esi edi
  734.  
  735.         DEBUGF  1, "INT\n"
  736.  
  737. ; find pointer of device wich made IRQ occur
  738.         mov     ecx, [devices]
  739.         test    ecx, ecx
  740.         jz      .nothing
  741.         mov     esi, device_list
  742.   .nextdevice:
  743.         mov     ebx, [esi]
  744.  
  745.         set_io  [ebx + device.io_addr], 0
  746.         set_io  [ebx + device.io_addr], REG_ISR
  747.         in      ax, dx                          ; Get interrupt status
  748.         out     dx, ax                          ; send it back to ACK
  749.         test    ax, ax
  750.         jnz     .got_it
  751.   .continue:
  752.         add     esi, 4
  753.         dec     ecx
  754.         jnz     .nextdevice
  755.   .nothing:
  756.         pop     edi esi ebx
  757.         xor     eax, eax
  758.  
  759.         ret                                     ; If no device was found, abort (The irq was probably for a device, not registered to this driver)
  760.  
  761.   .got_it:
  762.  
  763.         DEBUGF  1, "Device: %x Status: %x\n", ebx, ax
  764.  
  765. ;----------------------------------------------------
  766. ; Received packet ok?
  767.  
  768.         test    ax, ISR_ROK
  769.         jz      @f
  770.         push    ax
  771.  
  772.   .receive:
  773.         set_io  [ebx + device.io_addr], 0
  774.         set_io  [ebx + device.io_addr], REG_COMMAND
  775.         in      al, dx
  776.         test    al, BUFE                        ; test if RX buffer is empty
  777.         jnz     .finish
  778.  
  779.         DEBUGF  1, "RX:\n"
  780.  
  781.         mov     eax, [ebx + device.rx_buffer]
  782.         add     eax, [ebx + device.rx_data_offset]
  783.         test    byte [eax], (1 shl BIT_ROK)     ; check if packet is ok
  784.         jz      .reset_rx
  785.  
  786. ; packet is ok, copy it
  787.         movzx   ecx, word [eax+2]               ; packet length
  788.         sub     cx, 4                           ; don't copy CRC
  789.  
  790. ; Update stats
  791.         add     dword [ebx + device.bytes_rx], ecx
  792.         adc     dword [ebx + device.bytes_rx + 4], 0
  793.         inc     [ebx + device.packets_rx]
  794.  
  795.         DEBUGF  1, "Received %u bytes\n", ecx
  796.  
  797.         push    ebx eax ecx
  798.         invoke  KernelAlloc, ecx                ; Allocate a buffer to put packet into
  799.         pop     ecx
  800.         test    eax, eax                        ; Test if we allocated succesfully
  801.         jz      .abort
  802.  
  803.         mov     edi, eax                        ; Where we will copy too
  804.  
  805.         mov     esi, [esp]                      ; The buffer we will copy from
  806.         add     esi, 4                          ; Dont copy CRC
  807.  
  808.         push    dword .abort                    ; Kernel will return to this address after EthReceiver
  809.         push    ecx edi                         ; Save buffer pointer and size, to pass to kernel
  810.  
  811.   .copy:
  812.         shr     ecx, 1
  813.         jnc     .nb
  814.         movsb
  815.   .nb:
  816.         shr     ecx, 1
  817.         jnc     .nw
  818.         movsw
  819.   .nw:
  820.         jz      .nd
  821.         rep     movsd
  822.   .nd:
  823.  
  824.         jmp     [Eth_input]                     ; Send it to kernel
  825.  
  826.   .abort:
  827.         pop     eax ebx
  828.                                                 ; update eth_data_start_offset
  829.         movzx   eax, word [eax+2]               ; packet length
  830.         add     eax, [ebx + device.rx_data_offset]
  831.         add     eax, 4+3                        ; packet header is 4 bytes long + dword alignment
  832.         and     eax, not 3                      ; dword alignment
  833.  
  834.         cmp     eax, RX_BUFFER_SIZE
  835.         jb      .no_wrap
  836.         DEBUGF  1, "Wrapping\n"
  837.         sub     eax, RX_BUFFER_SIZE
  838.   .no_wrap:
  839.         mov     [ebx + device.rx_data_offset], eax
  840.         DEBUGF  1, "New RX ptr: %d\n", eax
  841.  
  842.         set_io  [ebx + device.io_addr], 0
  843.         set_io  [ebx + device.io_addr], REG_CAPR                        ; update 'Current Address of Packet Read register'
  844.         sub     eax, 0x10                       ; value 0x10 is a constant for CAPR
  845.         out     dx, ax
  846.  
  847.         jmp     .receive                        ; check for multiple packets
  848.  
  849.   .reset_rx:
  850.         test    byte [eax], (1 shl BIT_CRC)
  851.         jz      .no_crc_error
  852.         DEBUGF  2, "RX: CRC error!\n"
  853.  
  854.   .no_crc_error:
  855.         test    byte [eax], (1 shl BIT_FAE)
  856.         jz      .no_fae_error
  857.         DEBUGF  2, "RX: Frame alignment error!\n"
  858.  
  859.   .no_fae_error:
  860.         DEBUGF  1, "Reset RX\n"
  861.         in      al, dx                          ; read command register
  862.         push    ax
  863.         and     al, not (1 shl BIT_RE)          ; Clear the RE bit
  864.         out     dx, al
  865.         pop     ax
  866.         out     dx, al                          ; write original command back
  867.  
  868.         add     edx, REG_RXCONFIG - REG_COMMAND         ; Restore RX configuration
  869.         mov     ax, RX_CONFIG
  870.         out     dx, ax
  871.  
  872.   .finish:
  873.         pop     ax
  874.  
  875. ;----------------------------------------------------
  876. ; Transmit ok / Transmit error
  877.   @@:
  878.         test    ax, ISR_TOK + ISR_TER
  879.         jz      @f
  880.  
  881.         DEBUGF  1, "Transmit done!\n"
  882.  
  883.         push    ax
  884.         mov     ecx, (NUM_TX_DESC-1)*4
  885.   .txdescloop:
  886.         set_io  [ebx + device.io_addr], 0
  887.         set_io  [ebx + device.io_addr], REG_TSD0
  888.         add     edx, ecx
  889.         in      eax, dx
  890.  
  891.         test    eax, TSR_OWN                    ; DMA operation completed
  892.         jz      .notthisone
  893.  
  894.         cmp     [ebx + device.TX_DESC+ecx], 0
  895.         je      .notthisone
  896.  
  897.         DEBUGF  1, "TSD: 0x%x\n", eax
  898.  
  899.         test    eax, TSR_TUN
  900.         jz      .no_bun
  901.         DEBUGF  2, "TX: FIFO Buffer underrun!\n"
  902.  
  903.   .no_bun:
  904.         test    eax, TSR_OWC
  905.         jz      .no_owc
  906.         DEBUGF  2, "TX: OWC!\n"
  907.  
  908.   .no_owc:
  909.         test    eax, TSR_TABT
  910.         jz      .no_tabt
  911.         DEBUGF  2, "TX: TABT!\n"
  912.  
  913.   .no_tabt:
  914.         test    eax, TSR_CRS
  915.         jz      .no_csl
  916.         DEBUGF  2, "TX: Carrier Sense Lost!\n"
  917.  
  918.   .no_csl:
  919.         test    eax, TSR_TOK
  920.         jz      .no_tok
  921.         DEBUGF  1, "TX: Transmit OK!\n"
  922.  
  923.   .no_tok:
  924.         DEBUGF  1, "free transmit buffer 0x%x\n", [ebx + device.TX_DESC+ecx]:8
  925.         push    ecx ebx
  926.         invoke  KernelFree, [ebx + device.TX_DESC+ecx]
  927.         pop     ebx ecx
  928.         mov     [ebx + device.TX_DESC+ecx], 0
  929.  
  930.   .notthisone:
  931.         sub     ecx, 4
  932.         jae     .txdescloop
  933.         pop     ax
  934.  
  935. ;----------------------------------------------------
  936. ; Rx buffer overflow ?
  937.   @@:
  938.         test    ax, ISR_RXOVW
  939.         jz      @f
  940.  
  941.         push    ax
  942.         DEBUGF  2, "RX:buffer overflow!\n"
  943.  
  944.         set_io  [ebx + device.io_addr], 0
  945.         set_io  [ebx + device.io_addr], REG_ISR
  946.         mov     ax, ISR_FIFOOVW or ISR_RXOVW
  947.         out     dx, ax
  948.         pop     ax
  949.  
  950. ;----------------------------------------------------
  951. ; Packet underrun?
  952.   @@:
  953.         test    ax, ISR_PUN
  954.         jz      @f
  955.  
  956.         DEBUGF  1, "Packet underrun or link changed!\n"
  957.  
  958.         call    cable
  959.  
  960. ;----------------------------------------------------
  961. ; Receive FIFO overflow ?
  962.   @@:
  963.         test    ax, ISR_FIFOOVW
  964.         jz      @f
  965.  
  966.         push    ax
  967.         DEBUGF  2, "RX fifo overflow!\n"
  968.  
  969.         set_io  [ebx + device.io_addr], 0
  970.         set_io  [ebx + device.io_addr], REG_ISR
  971.         mov     ax, ISR_FIFOOVW or ISR_RXOVW
  972.         out     dx, ax
  973.         pop     ax
  974.  
  975. ;----------------------------------------------------
  976. ; cable length changed ?
  977.   @@:
  978.         test    ax, ISR_LENCHG
  979.         jz      .fail
  980.  
  981.         DEBUGF  2, "Cable length changed!\n"
  982.  
  983.         call    cable
  984.  
  985.   .fail:
  986.         pop     edi esi ebx
  987.         xor     eax, eax
  988.         inc     eax
  989.  
  990.         ret
  991.  
  992.  
  993.  
  994.  
  995. ;;;;;;;;;;;;;;;;;;;;;;;;;
  996. ;;                     ;;
  997. ;; Update Cable status ;;
  998. ;;                     ;;
  999. ;;;;;;;;;;;;;;;;;;;;;;;;;
  1000.  
  1001. align 4
  1002. cable:
  1003.         DEBUGF  1, "Checking link status:\n"
  1004.  
  1005.         set_io  [ebx + device.io_addr], 0
  1006.         set_io  [ebx + device.io_addr], REG_MSR
  1007.         in      al, dx
  1008.  
  1009.         test    al, 1 shl 2             ; 0 = link ok 1 = link fail
  1010.         jnz     .notconnected
  1011.  
  1012.         test    al, 1 shl 3             ; 0 = 100 Mbps 1 = 10 Mbps
  1013.         jnz     .10mbps
  1014.  
  1015.   .100mbps:
  1016.         mov     [ebx + device.state], ETH_LINK_100M
  1017.         invoke  NetLinkChanged
  1018.         DEBUGF  2, "link changed to 100 mbit\n"
  1019.  
  1020.         ret
  1021.  
  1022.   .10mbps:
  1023.         mov     [ebx + device.state], ETH_LINK_10M
  1024.         invoke  NetLinkChanged
  1025.         DEBUGF  2, "link changed to 10 mbit\n"
  1026.  
  1027.         ret
  1028.  
  1029.   .notconnected:
  1030.         mov     [ebx + device.state], ETH_LINK_DOWN
  1031.         invoke  NetLinkChanged
  1032.         DEBUGF  2, "no link\n"
  1033.  
  1034.         ret
  1035.  
  1036.  
  1037.  
  1038. ;;;;;;;;;;;;;;;;;;;;;;;
  1039. ;;                   ;;
  1040. ;; Write MAC address ;;
  1041. ;;                   ;;
  1042. ;;;;;;;;;;;;;;;;;;;;;;;
  1043.  
  1044. align 4
  1045. write_mac:      ; in: mac pushed onto stack (as 3 words)
  1046.  
  1047.         DEBUGF  1, "Writing MAC\n"
  1048.  
  1049. ; disable all in command registers
  1050.         set_io  [ebx + device.io_addr], 0
  1051.         set_io  [ebx + device.io_addr], REG_9346CR
  1052.         xor     eax, eax
  1053.         out     dx, al
  1054.  
  1055.         set_io  [ebx + device.io_addr], REG_IMR
  1056.         xor     eax, eax
  1057.         out     dx, ax
  1058.  
  1059.         set_io  [ebx + device.io_addr], REG_ISR
  1060.         mov     eax, -1
  1061.         out     dx, ax
  1062.  
  1063. ; enable writing
  1064.         set_io  [ebx + device.io_addr], REG_9346CR
  1065.         mov     eax, REG_9346CR_WE
  1066.         out     dx, al
  1067.  
  1068.  ; write the mac ...
  1069.         set_io  [ebx + device.io_addr], REG_IDR0
  1070.         pop     eax
  1071.         out     dx, eax
  1072.  
  1073.         set_io  [ebx + device.io_addr], REG_IDR0+4
  1074.         xor     eax, eax
  1075.         pop     ax
  1076.         out     dx, eax
  1077.  
  1078. ; disable writing
  1079.         set_io  [ebx + device.io_addr], REG_9346CR
  1080.         xor     eax, eax
  1081.         out     dx, al
  1082.  
  1083.         DEBUGF  1, "MAC write ok!\n"
  1084.  
  1085. ; Notice this procedure does not ret, but continues to read_mac instead.
  1086.  
  1087.  
  1088. ;;;;;;;;;;;;;;;;;;;;;;
  1089. ;;                  ;;
  1090. ;; Read MAC address ;;
  1091. ;;                  ;;
  1092. ;;;;;;;;;;;;;;;;;;;;;;
  1093.  
  1094. read_mac:
  1095.         DEBUGF  1, "Reading MAC:\n"
  1096.  
  1097.         set_io  [ebx + device.io_addr], 0
  1098.         lea     edi, [ebx + device.mac]
  1099.         in      eax, dx
  1100.         stosd
  1101.         add     edx, 4
  1102.         in      ax, dx
  1103.         stosw
  1104.  
  1105.         DEBUGF  1, "%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
  1106.  
  1107.         ret
  1108.  
  1109.  
  1110. ; End of code
  1111.  
  1112. data fixups
  1113. end data
  1114.  
  1115. include '../peimport.inc'
  1116.  
  1117. my_service      db 'RTL8139',0                    ; max 16 chars include zero
  1118.  
  1119. device_1        db 'Realtek 8139',0
  1120. device_2        db 'Realtek 8139A',0
  1121. device_3        db 'Realtek 8139B',0
  1122. device_4        db 'Realtek 8139C',0
  1123. device_5        db 'Realtek 8100',0
  1124. device_6        db 'Realtek 8139D',0
  1125. device_7        db 'Realtek 8139CP',0
  1126. device_8        db 'Realtek 8101',0
  1127. device_unknown  db 'Unknown RTL8139 clone', 0
  1128.  
  1129. crosslist:
  1130.         dd device_1
  1131.         dd device_2
  1132.         dd device_3
  1133.         dd device_4
  1134.         dd device_5
  1135.         dd device_6
  1136.         dd device_7
  1137.         dd device_8
  1138.         dd device_unknown
  1139.  
  1140. hw_ver_array:                    ; This array is used by the probe routine to find out wich version of the RTL8139 we are working with
  1141.         db VER_RTL8139
  1142.         db VER_RTL8139A
  1143.         db VER_RTL8139B
  1144.         db VER_RTL8139C
  1145.         db VER_RTL8100
  1146.         db VER_RTL8139D
  1147.         db VER_RTL8139CP
  1148.         db VER_RTL8101
  1149.         db 0
  1150.  
  1151. HW_VER_ARRAY_SIZE = $-hw_ver_array
  1152.  
  1153. include_debug_strings                           ; All data wich FDO uses will be included here
  1154.  
  1155. align 4
  1156. devices         dd 0
  1157. device_list     rd MAX_DEVICES                   ; This list contains all pointers to device structures the driver is handling
  1158.  
  1159.