2013-05-28 19:34:26 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2015-01-08 21:10:22 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2015. All rights reserved. ;;
|
2013-05-28 19:34:26 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; i8254x driver for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; based on i8254x.asm from baremetal os ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr (hidnplayr@gmail.com) ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
format PE DLL native
|
|
|
|
entry START
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
CURRENT_API = 0x0200
|
|
|
|
COMPATIBLE_API = 0x0100
|
|
|
|
API_VERSION = (COMPATIBLE_API shl 16) + CURRENT_API
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
MAX_DEVICES = 16
|
|
|
|
|
|
|
|
__DEBUG__ = 1
|
2013-08-05 13:00:22 +02:00
|
|
|
__DEBUG_LEVEL__ = 2 ; 1 = verbose, 2 = errors only
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
MAX_PKT_SIZE = 4096 ; Maximum packet size
|
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
RX_RING_SIZE = 8 ; Must be a power of 2, and minimum 8
|
|
|
|
TX_RING_SIZE = 8 ; Must be a power of 2, and minimum 8
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
section '.flat' readable writable executable
|
|
|
|
|
|
|
|
include '../proc32.inc'
|
2014-01-17 13:51:32 +01:00
|
|
|
include '../struct.inc'
|
|
|
|
include '../macros.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
include '../fdo.inc'
|
2014-08-31 16:09:14 +02:00
|
|
|
include '../netdrv.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Register list
|
|
|
|
REG_CTRL = 0x0000 ; Control Register
|
|
|
|
REG_STATUS = 0x0008 ; Device Status Register
|
|
|
|
REG_CTRLEXT = 0x0018 ; Extended Control Register
|
|
|
|
REG_MDIC = 0x0020 ; MDI Control Register
|
|
|
|
REG_FCAL = 0x0028 ; Flow Control Address Low
|
|
|
|
REG_FCAH = 0x002C ; Flow Control Address High
|
|
|
|
REG_FCT = 0x0030 ; Flow Control Type
|
|
|
|
REG_VET = 0x0038 ; VLAN Ether Type
|
|
|
|
REG_ICR = 0x00C0 ; Interrupt Cause Read
|
|
|
|
REG_ITR = 0x00C4 ; Interrupt Throttling Register
|
|
|
|
REG_ICS = 0x00C8 ; Interrupt Cause Set Register
|
|
|
|
REG_IMS = 0x00D0 ; Interrupt Mask Set/Read Register
|
|
|
|
REG_IMC = 0x00D8 ; Interrupt Mask Clear Register
|
|
|
|
REG_RCTL = 0x0100 ; Receive Control Register
|
|
|
|
REG_FCTTV = 0x0170 ; Flow Control Transmit Timer Value
|
|
|
|
REG_TXCW = 0x0178 ; Transmit Configuration Word
|
|
|
|
REG_RXCW = 0x0180 ; Receive Configuration Word
|
|
|
|
REG_TCTL = 0x0400 ; Transmit Control Register
|
|
|
|
REG_TIPG = 0x0410 ; Transmit Inter Packet Gap
|
|
|
|
|
|
|
|
REG_LEDCTL = 0x0E00 ; LED Control
|
|
|
|
REG_PBA = 0x1000 ; Packet Buffer Allocation
|
|
|
|
|
|
|
|
REG_RDBAL = 0x2800 ; RX Descriptor Base Address Low
|
|
|
|
REG_RDBAH = 0x2804 ; RX Descriptor Base Address High
|
|
|
|
REG_RDLEN = 0x2808 ; RX Descriptor Length
|
|
|
|
REG_RDH = 0x2810 ; RX Descriptor Head
|
|
|
|
REG_RDT = 0x2818 ; RX Descriptor Tail
|
|
|
|
REG_RDTR = 0x2820 ; RX Delay Timer Register
|
|
|
|
REG_RXDCTL = 0x3828 ; RX Descriptor Control
|
|
|
|
REG_RADV = 0x282C ; RX Int. Absolute Delay Timer
|
|
|
|
REG_RSRPD = 0x2C00 ; RX Small Packet Detect Interrupt
|
|
|
|
|
|
|
|
REG_TXDMAC = 0x3000 ; TX DMA Control
|
|
|
|
REG_TDBAL = 0x3800 ; TX Descriptor Base Address Low
|
|
|
|
REG_TDBAH = 0x3804 ; TX Descriptor Base Address High
|
|
|
|
REG_TDLEN = 0x3808 ; TX Descriptor Length
|
|
|
|
REG_TDH = 0x3810 ; TX Descriptor Head
|
|
|
|
REG_TDT = 0x3818 ; TX Descriptor Tail
|
|
|
|
REG_TIDV = 0x3820 ; TX Interrupt Delay Value
|
|
|
|
REG_TXDCTL = 0x3828 ; TX Descriptor Control
|
|
|
|
REG_TADV = 0x382C ; TX Absolute Interrupt Delay Value
|
|
|
|
REG_TSPMT = 0x3830 ; TCP Segmentation Pad & Min Threshold
|
|
|
|
|
|
|
|
REG_RXCSUM = 0x5000 ; RX Checksum Control
|
|
|
|
|
|
|
|
; Register list for i8254x
|
|
|
|
I82542_REG_RDTR = 0x0108 ; RX Delay Timer Register
|
|
|
|
I82542_REG_RDBAL = 0x0110 ; RX Descriptor Base Address Low
|
|
|
|
I82542_REG_RDBAH = 0x0114 ; RX Descriptor Base Address High
|
|
|
|
I82542_REG_RDLEN = 0x0118 ; RX Descriptor Length
|
|
|
|
I82542_REG_RDH = 0x0120 ; RDH for i82542
|
|
|
|
I82542_REG_RDT = 0x0128 ; RDT for i82542
|
|
|
|
I82542_REG_TDBAL = 0x0420 ; TX Descriptor Base Address Low
|
|
|
|
I82542_REG_TDBAH = 0x0424 ; TX Descriptor Base Address Low
|
|
|
|
I82542_REG_TDLEN = 0x0428 ; TX Descriptor Length
|
|
|
|
I82542_REG_TDH = 0x0430 ; TDH for i82542
|
|
|
|
I82542_REG_TDT = 0x0438 ; TDT for i82542
|
|
|
|
|
|
|
|
; CTRL - Control Register (0x0000)
|
|
|
|
CTRL_FD = 0x00000001 ; Full Duplex
|
|
|
|
CTRL_LRST = 0x00000008 ; Link Reset
|
|
|
|
CTRL_ASDE = 0x00000020 ; Auto-speed detection
|
|
|
|
CTRL_SLU = 0x00000040 ; Set Link Up
|
|
|
|
CTRL_ILOS = 0x00000080 ; Invert Loss of Signal
|
|
|
|
CTRL_SPEED_MASK = 0x00000300 ; Speed selection
|
|
|
|
CTRL_SPEED_SHIFT = 8
|
|
|
|
CTRL_FRCSPD = 0x00000800 ; Force Speed
|
|
|
|
CTRL_FRCDPLX = 0x00001000 ; Force Duplex
|
|
|
|
CTRL_SDP0_DATA = 0x00040000 ; SDP0 data
|
|
|
|
CTRL_SDP1_DATA = 0x00080000 ; SDP1 data
|
|
|
|
CTRL_SDP0_IODIR = 0x00400000 ; SDP0 direction
|
|
|
|
CTRL_SDP1_IODIR = 0x00800000 ; SDP1 direction
|
|
|
|
CTRL_RST = 0x04000000 ; Device Reset
|
|
|
|
CTRL_RFCE = 0x08000000 ; RX Flow Ctrl Enable
|
|
|
|
CTRL_TFCE = 0x10000000 ; TX Flow Ctrl Enable
|
|
|
|
CTRL_VME = 0x40000000 ; VLAN Mode Enable
|
|
|
|
CTRL_PHY_RST = 0x80000000 ; PHY reset
|
|
|
|
|
|
|
|
; STATUS - Device Status Register (0x0008)
|
|
|
|
STATUS_FD = 0x00000001 ; Full Duplex
|
|
|
|
STATUS_LU = 0x00000002 ; Link Up
|
|
|
|
STATUS_TXOFF = 0x00000010 ; Transmit paused
|
|
|
|
STATUS_TBIMODE = 0x00000020 ; TBI Mode
|
|
|
|
STATUS_SPEED_MASK = 0x000000C0 ; Link Speed setting
|
|
|
|
STATUS_SPEED_SHIFT = 6
|
|
|
|
STATUS_ASDV_MASK = 0x00000300 ; Auto Speed Detection
|
|
|
|
STATUS_ASDV_SHIFT = 8
|
|
|
|
STATUS_PCI66 = 0x00000800 ; PCI bus speed
|
|
|
|
STATUS_BUS64 = 0x00001000 ; PCI bus width
|
|
|
|
STATUS_PCIX_MODE = 0x00002000 ; PCI-X mode
|
|
|
|
STATUS_PCIXSPD_MASK = 0x0000C000 ; PCI-X speed
|
|
|
|
STATUS_PCIXSPD_SHIFT = 14
|
|
|
|
|
|
|
|
; CTRL_EXT - Extended Device Control Register (0x0018)
|
|
|
|
CTRLEXT_PHY_INT = 0x00000020 ; PHY interrupt
|
|
|
|
CTRLEXT_SDP6_DATA = 0x00000040 ; SDP6 data
|
|
|
|
CTRLEXT_SDP7_DATA = 0x00000080 ; SDP7 data
|
|
|
|
CTRLEXT_SDP6_IODIR = 0x00000400 ; SDP6 direction
|
|
|
|
CTRLEXT_SDP7_IODIR = 0x00000800 ; SDP7 direction
|
|
|
|
CTRLEXT_ASDCHK = 0x00001000 ; Auto-Speed Detect Chk
|
|
|
|
CTRLEXT_EE_RST = 0x00002000 ; EEPROM reset
|
|
|
|
CTRLEXT_SPD_BYPS = 0x00008000 ; Speed Select Bypass
|
|
|
|
CTRLEXT_RO_DIS = 0x00020000 ; Relaxed Ordering Dis.
|
|
|
|
CTRLEXT_LNKMOD_MASK = 0x00C00000 ; Link Mode
|
|
|
|
CTRLEXT_LNKMOD_SHIFT = 22
|
|
|
|
|
|
|
|
; MDIC - MDI Control Register (0x0020)
|
|
|
|
MDIC_DATA_MASK = 0x0000FFFF ; Data
|
|
|
|
MDIC_REG_MASK = 0x001F0000 ; PHY Register
|
|
|
|
MDIC_REG_SHIFT = 16
|
|
|
|
MDIC_PHY_MASK = 0x03E00000 ; PHY Address
|
|
|
|
MDIC_PHY_SHIFT = 21
|
|
|
|
MDIC_OP_MASK = 0x0C000000 ; Opcode
|
|
|
|
MDIC_OP_SHIFT = 26
|
|
|
|
MDIC_R = 0x10000000 ; Ready
|
|
|
|
MDIC_I = 0x20000000 ; Interrupt Enable
|
|
|
|
MDIC_E = 0x40000000 ; Error
|
|
|
|
|
|
|
|
; ICR - Interrupt Cause Read (0x00c0)
|
|
|
|
ICR_TXDW = 0x00000001 ; TX Desc Written back
|
|
|
|
ICR_TXQE = 0x00000002 ; TX Queue Empty
|
|
|
|
ICR_LSC = 0x00000004 ; Link Status Change
|
|
|
|
ICR_RXSEQ = 0x00000008 ; RX Sence Error
|
|
|
|
ICR_RXDMT0 = 0x00000010 ; RX Desc min threshold reached
|
|
|
|
ICR_RXO = 0x00000040 ; RX Overrun
|
|
|
|
ICR_RXT0 = 0x00000080 ; RX Timer Interrupt
|
|
|
|
ICR_MDAC = 0x00000200 ; MDIO Access Complete
|
|
|
|
ICR_RXCFG = 0x00000400
|
|
|
|
ICR_PHY_INT = 0x00001000 ; PHY Interrupt
|
|
|
|
ICR_GPI_SDP6 = 0x00002000 ; GPI on SDP6
|
|
|
|
ICR_GPI_SDP7 = 0x00004000 ; GPI on SDP7
|
|
|
|
ICR_TXD_LOW = 0x00008000 ; TX Desc low threshold hit
|
|
|
|
ICR_SRPD = 0x00010000 ; Small RX packet detected
|
|
|
|
|
|
|
|
; RCTL - Receive Control Register (0x0100)
|
|
|
|
RCTL_EN = 0x00000002 ; Receiver Enable
|
|
|
|
RCTL_SBP = 0x00000004 ; Store Bad Packets
|
|
|
|
RCTL_UPE = 0x00000008 ; Unicast Promiscuous Enabled
|
|
|
|
RCTL_MPE = 0x00000010 ; Xcast Promiscuous Enabled
|
|
|
|
RCTL_LPE = 0x00000020 ; Long Packet Reception Enable
|
|
|
|
RCTL_LBM_MASK = 0x000000C0 ; Loopback Mode
|
|
|
|
RCTL_LBM_SHIFT = 6
|
|
|
|
RCTL_RDMTS_MASK = 0x00000300 ; RX Desc Min Threshold Size
|
|
|
|
RCTL_RDMTS_SHIFT = 8
|
|
|
|
RCTL_MO_MASK = 0x00003000 ; Multicast Offset
|
|
|
|
RCTL_MO_SHIFT = 12
|
|
|
|
RCTL_BAM = 0x00008000 ; Broadcast Accept Mode
|
|
|
|
RCTL_BSIZE_MASK = 0x00030000 ; RX Buffer Size
|
|
|
|
RCTL_BSIZE_SHIFT = 16
|
|
|
|
RCTL_VFE = 0x00040000 ; VLAN Filter Enable
|
|
|
|
RCTL_CFIEN = 0x00080000 ; CFI Enable
|
|
|
|
RCTL_CFI = 0x00100000 ; Canonical Form Indicator Bit
|
|
|
|
RCTL_DPF = 0x00400000 ; Discard Pause Frames
|
|
|
|
RCTL_PMCF = 0x00800000 ; Pass MAC Control Frames
|
|
|
|
RCTL_BSEX = 0x02000000 ; Buffer Size Extension
|
|
|
|
RCTL_SECRC = 0x04000000 ; Strip Ethernet CRC
|
|
|
|
|
|
|
|
; TCTL - Transmit Control Register (0x0400)
|
|
|
|
TCTL_EN = 0x00000002 ; Transmit Enable
|
|
|
|
TCTL_PSP = 0x00000008 ; Pad short packets
|
|
|
|
TCTL_SWXOFF = 0x00400000 ; Software XOFF Transmission
|
|
|
|
|
|
|
|
; PBA - Packet Buffer Allocation (0x1000)
|
|
|
|
PBA_RXA_MASK = 0x0000FFFF ; RX Packet Buffer
|
|
|
|
PBA_RXA_SHIFT = 0
|
|
|
|
PBA_TXA_MASK = 0xFFFF0000 ; TX Packet Buffer
|
|
|
|
PBA_TXA_SHIFT = 16
|
|
|
|
|
|
|
|
; Flow Control Type
|
|
|
|
FCT_TYPE_DEFAULT = 0x8808
|
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
; === TX Descriptor ===
|
|
|
|
|
|
|
|
struct TDESC
|
|
|
|
addr_l dd ?
|
|
|
|
addr_h dd ?
|
|
|
|
length_cso_cmd dd ? ; 16 bits length + 8 bits cso + 8 bits cmd
|
|
|
|
status dd ? ; status, checksum start field, special
|
|
|
|
ends
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; TX Packet Length (word 2)
|
|
|
|
TXDESC_LEN_MASK = 0x0000ffff
|
|
|
|
|
|
|
|
; TX Descriptor CMD field (word 2)
|
|
|
|
TXDESC_IDE = 0x80000000 ; Interrupt Delay Enable
|
|
|
|
TXDESC_VLE = 0x40000000 ; VLAN Packet Enable
|
|
|
|
TXDESC_DEXT = 0x20000000 ; Extension
|
|
|
|
TXDESC_RPS = 0x10000000 ; Report Packet Sent
|
|
|
|
TXDESC_RS = 0x08000000 ; Report Status
|
|
|
|
TXDESC_IC = 0x04000000 ; Insert Checksum
|
|
|
|
TXDESC_IFCS = 0x02000000 ; Insert FCS
|
|
|
|
TXDESC_EOP = 0x01000000 ; End Of Packet
|
|
|
|
|
|
|
|
; TX Descriptor STA field (word 3)
|
|
|
|
TXDESC_TU = 0x00000008 ; Transmit Underrun
|
|
|
|
TXDESC_LC = 0x00000004 ; Late Collision
|
|
|
|
TXDESC_EC = 0x00000002 ; Excess Collisions
|
|
|
|
TXDESC_DD = 0x00000001 ; Descriptor Done
|
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
; === RX Descriptor ===
|
|
|
|
|
|
|
|
struct RDESC
|
|
|
|
addr_l dd ?
|
|
|
|
addr_h dd ?
|
|
|
|
status_l dd ?
|
|
|
|
status_h dd ?
|
|
|
|
ends
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; RX Packet Length (word 2)
|
|
|
|
RXDESC_LEN_MASK = 0x0000ffff
|
|
|
|
|
|
|
|
; RX Descriptor STA field (word 3)
|
|
|
|
RXDESC_PIF = 0x00000080 ; Passed In-exact Filter
|
|
|
|
RXDESC_IPCS = 0x00000040 ; IP cksum calculated
|
|
|
|
RXDESC_TCPCS = 0x00000020 ; TCP cksum calculated
|
|
|
|
RXDESC_VP = 0x00000008 ; Packet is 802.1Q
|
|
|
|
RXDESC_IXSM = 0x00000004 ; Ignore cksum indication
|
|
|
|
RXDESC_EOP = 0x00000002 ; End Of Packet
|
|
|
|
RXDESC_DD = 0x00000001 ; Descriptor Done
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
struct device ETH_DEVICE
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mmio_addr dd ?
|
|
|
|
pci_bus dd ?
|
|
|
|
pci_dev dd ?
|
|
|
|
irq_line db ?
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
cur_rx dd ?
|
|
|
|
cur_tx dd ?
|
|
|
|
last_tx dd ?
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
rb 0x100 - ($ and 0xff) ; align 256
|
|
|
|
rx_desc rb RX_RING_SIZE*sizeof.RDESC*2
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
rb 0x100 - ($ and 0xff) ; align 256
|
|
|
|
tx_desc rb TX_RING_SIZE*sizeof.TDESC*2
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
ends
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; proc START ;;
|
|
|
|
;; ;;
|
|
|
|
;; (standard driver proc) ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2014-01-24 17:40:41 +01:00
|
|
|
proc START c, reason:dword, cmdline:dword
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
cmp [reason], DRV_ENTRY
|
|
|
|
jne .fail
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-08-05 13:00:22 +02:00
|
|
|
DEBUGF 1,"Loading driver\n"
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke RegService, my_service, service_proc
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.fail:
|
2014-01-23 18:47:35 +01:00
|
|
|
xor eax, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; proc SERVICE_PROC ;;
|
|
|
|
;; ;;
|
|
|
|
;; (standard driver proc) ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
align 4
|
|
|
|
proc service_proc stdcall, ioctl:dword
|
|
|
|
|
|
|
|
mov edx, [ioctl]
|
2014-01-17 16:07:07 +01:00
|
|
|
mov eax, [edx + IOCTL.io_code]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;------------------------------------------------------
|
|
|
|
|
|
|
|
cmp eax, 0 ;SRV_GETVERSION
|
|
|
|
jne @F
|
|
|
|
|
2014-01-17 16:07:07 +01:00
|
|
|
cmp [edx + IOCTL.out_size], 4
|
2013-05-28 19:34:26 +02:00
|
|
|
jb .fail
|
2014-01-17 16:07:07 +01:00
|
|
|
mov eax, [edx + IOCTL.output]
|
2014-01-23 18:47:35 +01:00
|
|
|
mov dword[eax], API_VERSION
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
;------------------------------------------------------
|
|
|
|
@@:
|
|
|
|
cmp eax, 1 ;SRV_HOOK
|
|
|
|
jne .fail
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
cmp [edx + IOCTL.inp_size], 3 ; Data input must be at least 3 bytes
|
2013-05-28 19:34:26 +02:00
|
|
|
jb .fail
|
|
|
|
|
2014-01-17 16:07:07 +01:00
|
|
|
mov eax, [edx + IOCTL.input]
|
2014-01-24 17:40:41 +01:00
|
|
|
cmp byte[eax], 1 ; 1 means device number and bus number (pci) are given
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .fail ; other types arent supported for this card yet
|
|
|
|
|
|
|
|
; check if the device is already listed
|
|
|
|
|
|
|
|
mov esi, device_list
|
|
|
|
mov ecx, [devices]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .firstdevice
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
; mov eax, [edx + IOCTL.input] ; get the pci bus and device numbers
|
2013-05-28 19:34:26 +02:00
|
|
|
mov ax, [eax+1] ;
|
|
|
|
.nextdevice:
|
|
|
|
mov ebx, [esi]
|
2014-01-23 18:47:35 +01:00
|
|
|
cmp al, byte[ebx + device.pci_bus]
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .next
|
2014-01-23 18:47:35 +01:00
|
|
|
cmp ah, byte[ebx + device.pci_dev]
|
2013-05-28 19:34:26 +02:00
|
|
|
je .find_devicenum ; Device is already loaded, let's find it's device number
|
|
|
|
.next:
|
|
|
|
add esi, 4
|
|
|
|
loop .nextdevice
|
|
|
|
|
|
|
|
|
|
|
|
; This device doesnt have its own eth_device structure yet, lets create one
|
|
|
|
.firstdevice:
|
|
|
|
cmp [devices], MAX_DEVICES ; First check if the driver can handle one more card
|
|
|
|
jae .fail
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
allocate_and_clear ebx, sizeof.device, .fail ; Allocate the buffer for device structure
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Fill in the direct call addresses into the struct
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov [ebx + device.reset], reset
|
|
|
|
mov [ebx + device.transmit], transmit
|
|
|
|
mov [ebx + device.unload], unload
|
|
|
|
mov [ebx + device.name], my_service
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; save the pci bus and device numbers
|
|
|
|
|
2014-01-17 16:07:07 +01:00
|
|
|
mov eax, [edx + IOCTL.input]
|
2014-01-24 17:40:41 +01:00
|
|
|
movzx ecx, byte[eax+1]
|
2014-01-23 18:47:35 +01:00
|
|
|
mov [ebx + device.pci_bus], ecx
|
2014-01-24 17:40:41 +01:00
|
|
|
movzx ecx, byte[eax+2]
|
2014-01-23 18:47:35 +01:00
|
|
|
mov [ebx + device.pci_dev], ecx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Now, it's time to find the base mmio addres of the PCI device
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
stdcall PCI_find_mmio32, [ebx + device.pci_bus], [ebx + device.pci_dev] ; returns in eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Create virtual mapping of the physical memory
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke MapIoMem, eax, 10000h, PG_SW+PG_NOCACHE
|
|
|
|
mov [ebx + device.mmio_addr], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; We've found the mmio address, find IRQ now
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke PciRead8, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.interrupt_line
|
|
|
|
mov [ebx + device.irq_line], al
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
DEBUGF 1,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
|
2014-01-23 18:47:35 +01:00
|
|
|
[ebx + device.pci_dev]:1,[ebx + device.pci_bus]:1,[ebx + device.irq_line]:1,[ebx + device.mmio_addr]:8
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Ok, the eth_device structure is ready, let's probe the device
|
2014-01-23 18:47:35 +01:00
|
|
|
call probe ; this function will output in eax
|
2013-05-28 19:34:26 +02:00
|
|
|
test eax, eax
|
2014-01-23 18:47:35 +01:00
|
|
|
jnz .err ; If an error occured, exit
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov eax, [devices] ; Add the device structure to our device list
|
|
|
|
mov [device_list+4*eax], ebx ; (IRQ handler uses this list to find device)
|
|
|
|
inc [devices] ;
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
call start_i8254x
|
2014-11-01 19:39:23 +01:00
|
|
|
test eax, eax
|
|
|
|
jnz .destroy
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov [ebx + device.type], NET_TYPE_ETH
|
|
|
|
invoke NetRegDev
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp eax, -1
|
|
|
|
je .destroy
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
; If the device was already loaded, find the device number and return it in eax
|
|
|
|
|
|
|
|
.find_devicenum:
|
|
|
|
DEBUGF 1,"Trying to find device number of already registered device\n"
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke NetPtrToNum ; This kernel procedure converts a pointer to device struct in ebx
|
|
|
|
; into a device number in edi
|
|
|
|
mov eax, edi ; Application wants it in eax instead
|
2013-05-28 19:34:26 +02:00
|
|
|
DEBUGF 1,"Kernel says: %u\n", eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
; If an error occured, remove all allocated data and exit (returning -1 in eax)
|
|
|
|
|
|
|
|
.destroy:
|
|
|
|
; todo: reset device into virgin state
|
|
|
|
|
|
|
|
.err:
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke KernelFree, ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.fail:
|
2014-11-01 19:39:23 +01:00
|
|
|
DEBUGF 2,"Loading driver failed\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
or eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
;------------------------------------------------------
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
|
|
|
;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
|
|
|
|
;; ;;
|
|
|
|
;; Actual Hardware dependent code starts here ;;
|
|
|
|
;; ;;
|
|
|
|
;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
unload:
|
2014-11-01 19:39:23 +01:00
|
|
|
|
|
|
|
DEBUGF 1,"Unload\n"
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; TODO: (in this particular order)
|
|
|
|
;
|
|
|
|
; - Stop the device
|
|
|
|
; - Detach int handler
|
|
|
|
; - Remove device from local list (device_list)
|
|
|
|
; - call unregister function in kernel
|
|
|
|
; - Remove all allocated structures and buffers the card used
|
|
|
|
|
|
|
|
or eax, -1
|
2014-11-01 19:39:23 +01:00
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;
|
|
|
|
;; probe: enables the device (if it really is I8254X)
|
|
|
|
;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
align 4
|
|
|
|
probe:
|
|
|
|
|
|
|
|
DEBUGF 1,"Probe\n"
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
; Make the device a bus master
|
|
|
|
invoke PciRead32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command
|
|
|
|
or al, PCI_CMD_MASTER
|
|
|
|
invoke PciWrite32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; TODO: validate the device
|
|
|
|
|
|
|
|
call read_mac
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
movzx eax, [ebx + device.irq_line]
|
2013-05-28 19:34:26 +02:00
|
|
|
DEBUGF 1,"Attaching int handler to irq %x\n", eax:1
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke AttachIntHandler, eax, int_handler, ebx
|
2013-05-28 19:34:26 +02:00
|
|
|
test eax, eax
|
|
|
|
jnz @f
|
2013-08-05 13:00:22 +02:00
|
|
|
DEBUGF 2,"Could not attach int handler!\n"
|
2014-01-23 13:46:33 +01:00
|
|
|
or eax, -1
|
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
@@:
|
|
|
|
|
|
|
|
|
|
|
|
reset_dontstart:
|
|
|
|
DEBUGF 1,"Reset\n"
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov esi, [ebx + device.mmio_addr]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
or dword[esi + REG_CTRL], CTRL_RST ; reset device
|
2013-05-28 19:34:26 +02:00
|
|
|
.loop:
|
|
|
|
push esi
|
|
|
|
xor esi, esi
|
|
|
|
inc esi
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke Sleep
|
2013-05-28 19:34:26 +02:00
|
|
|
pop esi
|
2014-01-22 22:28:03 +01:00
|
|
|
test dword[esi + REG_CTRL], CTRL_RST
|
2013-05-28 19:34:26 +02:00
|
|
|
jnz .loop
|
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[esi + REG_IMC], 0xffffffff ; Disable all interrupt causes
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, dword [esi + REG_ICR] ; Clear any pending interrupts
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[esi + REG_ITR], 0 ; Disable interrupt throttling logic
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[esi + REG_PBA], 0x00000004 ; PBA: set the RX buffer size to 4KB (TX buffer is calculated as 64-RX buffer)
|
|
|
|
mov dword[esi + REG_RDTR], 0 ; RDTR: set no delay
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-09-23 10:27:47 +02:00
|
|
|
mov dword[esi + REG_TXCW], 0x80008060 ; TXCW: set ANE, TxConfigWord (Half/Full duplex, Next Page Reqest)
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, [esi + REG_CTRL]
|
|
|
|
or eax, 1 shl 6 + 1 shl 5
|
|
|
|
and eax, not (1 shl 3 + 1 shl 7 + 1 shl 30 + 1 shl 31)
|
|
|
|
mov dword [esi + REG_CTRL], eax ; CTRL: clear LRST, set SLU and ASDE, clear RSTPHY, VME, and ILOS
|
|
|
|
|
|
|
|
lea edi, [esi + 0x5200] ; MTA: reset
|
|
|
|
mov eax, 0xffffffff
|
|
|
|
stosd
|
|
|
|
stosd
|
|
|
|
stosd
|
|
|
|
stosd
|
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
call init_rx
|
2014-11-01 19:39:23 +01:00
|
|
|
test eax, eax
|
|
|
|
jnz .fail
|
2014-01-22 22:28:03 +01:00
|
|
|
call init_tx
|
|
|
|
|
|
|
|
xor eax, eax
|
2014-11-01 19:39:23 +01:00
|
|
|
.fail:
|
2014-01-22 22:28:03 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
init_rx:
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
lea edi, [ebx + device.rx_desc]
|
2014-01-22 22:28:03 +01:00
|
|
|
mov ecx, RX_RING_SIZE
|
|
|
|
.loop:
|
2015-03-17 22:50:29 +01:00
|
|
|
push ecx
|
|
|
|
push edi
|
|
|
|
invoke NetAlloc, MAX_PKT_SIZE+NET_BUFF.data
|
2014-11-01 19:39:23 +01:00
|
|
|
test eax, eax
|
|
|
|
jz .out_of_mem
|
2014-01-22 22:28:03 +01:00
|
|
|
DEBUGF 1,"RX buffer: 0x%x\n", eax
|
|
|
|
pop edi
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[edi + RX_RING_SIZE*sizeof.RDESC], eax
|
2014-01-22 22:28:03 +01:00
|
|
|
push edi
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke GetPhysAddr
|
2014-01-22 22:28:03 +01:00
|
|
|
pop edi
|
2015-03-17 22:50:29 +01:00
|
|
|
add eax, NET_BUFF.data
|
2014-01-23 13:46:33 +01:00
|
|
|
mov [edi + RDESC.addr_l], eax
|
|
|
|
mov [edi + RDESC.addr_h], 0
|
|
|
|
mov [edi + RDESC.status_l], 0
|
|
|
|
mov [edi + RDESC.status_h], 0
|
|
|
|
add edi, sizeof.RDESC
|
2014-01-22 22:28:03 +01:00
|
|
|
pop ecx
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov [ebx + device.cur_rx], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
lea eax, [ebx + device.rx_desc]
|
|
|
|
invoke GetPhysAddr
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[esi + REG_RDBAL], eax ; Receive Descriptor Base Address Low
|
|
|
|
mov dword[esi + REG_RDBAH], 0 ; Receive Descriptor Base Address High
|
|
|
|
mov dword[esi + REG_RDLEN], RX_RING_SIZE*sizeof.RDESC ; Receive Descriptor Length
|
|
|
|
mov dword[esi + REG_RDH], 0 ; Receive Descriptor Head
|
|
|
|
mov dword[esi + REG_RDT], RX_RING_SIZE-1 ; Receive Descriptor Tail
|
2014-01-22 22:28:03 +01:00
|
|
|
mov dword[esi + REG_RCTL], RCTL_SBP or RCTL_BAM or RCTL_SECRC or RCTL_UPE or RCTL_MPE
|
|
|
|
; Store Bad Packets, Broadcast Accept Mode, Strip Ethernet CRC from incoming packet, Promiscuous mode
|
2014-11-01 19:39:23 +01:00
|
|
|
xor eax, eax ; success!
|
|
|
|
ret
|
2014-01-22 22:28:03 +01:00
|
|
|
|
2014-11-01 19:39:23 +01:00
|
|
|
.out_of_mem:
|
|
|
|
DEBUGF 2,"Out of memory!\n"
|
|
|
|
pop edi ecx
|
|
|
|
or eax, -1 ; error!
|
2014-01-22 22:28:03 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
init_tx:
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
lea edi, [ebx + device.tx_desc]
|
2014-01-23 13:46:33 +01:00
|
|
|
mov ecx, TX_RING_SIZE
|
|
|
|
.loop:
|
|
|
|
mov [edi + TDESC.addr_l], eax
|
|
|
|
mov [edi + TDESC.addr_h], 0
|
|
|
|
mov [edi + TDESC.length_cso_cmd], 0
|
|
|
|
mov [edi + TDESC.status], 0
|
|
|
|
add edi, sizeof.TDESC
|
|
|
|
dec ecx
|
|
|
|
jnz .loop
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov [ebx + device.cur_tx], 0
|
|
|
|
mov [ebx + device.last_tx], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
lea eax, [ebx + device.tx_desc]
|
|
|
|
invoke GetPhysAddr
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[esi + REG_TDBAL], eax ; Transmit Descriptor Base Address Low
|
|
|
|
mov dword[esi + REG_TDBAH], 0 ; Transmit Descriptor Base Address High
|
|
|
|
mov dword[esi + REG_TDLEN], RX_RING_SIZE*sizeof.TDESC ; Transmit Descriptor Length
|
|
|
|
mov dword[esi + REG_TDH], 0 ; Transmit Descriptor Head
|
|
|
|
mov dword[esi + REG_TDT], 0 ; Transmit Descriptor Tail
|
|
|
|
mov dword[esi + REG_TCTL], 0x010400fa ; Enabled, Pad Short Packets, 15 retrys, 64-byte COLD, Re-transmit on Late Collision
|
|
|
|
mov dword[esi + REG_TIPG], 0x0060200A ; IPGT 10, IPGR1 8, IPGR2 6
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
align 4
|
|
|
|
reset:
|
|
|
|
call reset_dontstart
|
2014-11-01 19:39:23 +01:00
|
|
|
test eax, eax
|
|
|
|
je start_i8254x
|
|
|
|
|
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-11-01 19:39:23 +01:00
|
|
|
align 4
|
2013-05-28 19:34:26 +02:00
|
|
|
start_i8254x:
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov esi, [ebx + device.mmio_addr]
|
2014-01-23 13:46:33 +01:00
|
|
|
or dword[esi + REG_RCTL], RCTL_EN ; Enable the receiver
|
2014-01-22 22:28:03 +01:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
xor eax, eax
|
|
|
|
mov [esi + REG_RDTR], eax ; Clear the Receive Delay Timer Register
|
|
|
|
mov [esi + REG_RADV], eax ; Clear the Receive Interrupt Absolute Delay Timer
|
|
|
|
mov [esi + REG_RSRPD], eax ; Clear the Receive Small Packet Detect Interrupt
|
2014-01-22 22:28:03 +01:00
|
|
|
|
|
|
|
mov dword[esi + REG_IMS], 0x1F6DC ; Enable interrupt types
|
|
|
|
mov eax, [esi + REG_ICR] ; Clear pending interrupts
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov [ebx + device.mtu], 1514
|
2014-02-20 20:35:01 +01:00
|
|
|
mov [ebx + device.state], ETH_LINK_UNKNOWN ; Set link state to unknown
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
read_mac:
|
|
|
|
|
|
|
|
DEBUGF 1,"Read MAC\n"
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov esi, [ebx + device.mmio_addr]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mov eax, [esi+0x5400] ; RAL
|
|
|
|
test eax, eax
|
|
|
|
jz .try_eeprom
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
mov dword[ebx + device.mac], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [esi+0x5404] ; RAH
|
2014-01-23 18:47:35 +01:00
|
|
|
mov word[ebx + device.mac+4], ax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
jmp .mac_ok
|
|
|
|
|
|
|
|
.try_eeprom:
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[esi+0x14], 0x00000001
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [esi+0x14]
|
|
|
|
shr eax, 16
|
2014-01-23 18:47:35 +01:00
|
|
|
mov word[ebx + device.mac], ax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[esi+0x14], 0x00000101
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [esi+0x14]
|
|
|
|
shr eax, 16
|
2014-01-23 18:47:35 +01:00
|
|
|
mov word[ebx + device.mac+2], ax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[esi+0x14], 0x00000201
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [esi+0x14]
|
|
|
|
shr eax, 16
|
2014-01-23 18:47:35 +01:00
|
|
|
mov word[ebx + device.mac+4], ax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.mac_ok:
|
|
|
|
DEBUGF 1,"MAC = %x-%x-%x-%x-%x-%x\n",\
|
2014-01-23 18:47:35 +01:00
|
|
|
[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
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Transmit ;;
|
|
|
|
;; ;;
|
2015-03-17 22:50:29 +01:00
|
|
|
;; In: ebx = pointer to device structure ;;
|
|
|
|
;; Out: eax = 0 on success ;;
|
2013-05-28 19:34:26 +02:00
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2014-01-22 22:28:03 +01:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
proc transmit stdcall bufferptr
|
2014-01-22 22:28:03 +01:00
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
pushf
|
|
|
|
cli
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
mov esi, [bufferptr]
|
|
|
|
DEBUGF 1,"Transmitting packet, buffer:%x, size:%u\n", [bufferptr], [esi + NET_BUFF.length]
|
|
|
|
lea eax, [esi + NET_BUFF.data]
|
2013-07-09 19:59:57 +02:00
|
|
|
DEBUGF 1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
|
2013-05-28 19:34:26 +02:00
|
|
|
[eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
|
|
|
|
[eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
|
|
|
|
[eax+13]:2,[eax+12]:2
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
cmp [esi + NET_BUFF.length], 1514
|
2013-05-28 19:34:26 +02:00
|
|
|
ja .fail
|
2015-03-17 22:50:29 +01:00
|
|
|
cmp [esi + NET_BUFF.length], 60
|
2013-05-28 19:34:26 +02:00
|
|
|
jb .fail
|
|
|
|
|
|
|
|
; Program the descriptor (use legacy mode)
|
2014-01-23 18:47:35 +01:00
|
|
|
mov edi, [ebx + device.cur_tx]
|
2014-01-23 13:46:33 +01:00
|
|
|
DEBUGF 1, "Using TX desc: %u\n", edi
|
|
|
|
shl edi, 4 ; edi = edi * sizeof.TDESC
|
2014-01-23 18:47:35 +01:00
|
|
|
lea edi, [ebx + device.tx_desc + edi]
|
2015-03-17 22:50:29 +01:00
|
|
|
mov eax, esi
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[edi + TX_RING_SIZE*sizeof.TDESC], eax ; Store the data location (for driver)
|
2015-03-17 22:50:29 +01:00
|
|
|
add eax, [eax + NET_BUFF.offset]
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke GetPhysAddr
|
2014-01-23 13:46:33 +01:00
|
|
|
mov [edi + TDESC.addr_l], eax ; Data location (for hardware)
|
|
|
|
mov [edi + TDESC.addr_h], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
mov ecx, [esi + NET_BUFF.length]
|
2014-01-23 13:46:33 +01:00
|
|
|
or ecx, TXDESC_EOP + TXDESC_IFCS + TXDESC_RS
|
|
|
|
mov [edi + TDESC.length_cso_cmd], ecx
|
|
|
|
mov [edi + TDESC.status], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
; Tell i8254x wich descriptor(s) we programmed, by moving the tail
|
|
|
|
mov edi, [ebx + device.mmio_addr]
|
|
|
|
mov eax, [ebx + device.cur_tx]
|
2014-01-23 13:46:33 +01:00
|
|
|
inc eax
|
|
|
|
and eax, TX_RING_SIZE-1
|
2014-01-23 18:47:35 +01:00
|
|
|
mov [ebx + device.cur_tx], eax
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[edi + REG_TDT], eax ; TDT - Transmit Descriptor Tail
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Update stats
|
2014-01-23 18:47:35 +01:00
|
|
|
inc [ebx + device.packets_tx]
|
2015-03-17 22:50:29 +01:00
|
|
|
mov eax, [esi + NET_BUFF.length]
|
2014-01-23 18:47:35 +01:00
|
|
|
add dword[ebx + device.bytes_tx], eax
|
|
|
|
adc dword[ebx + device.bytes_tx + 4], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-09-23 10:27:47 +02:00
|
|
|
call clean_tx
|
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
popf
|
2014-01-23 16:01:01 +01:00
|
|
|
xor eax, eax
|
2014-01-22 22:28:03 +01:00
|
|
|
ret
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.fail:
|
2014-09-23 10:27:47 +02:00
|
|
|
call clean_tx
|
|
|
|
|
2013-07-09 19:59:57 +02:00
|
|
|
DEBUGF 2,"Send failed\n"
|
2015-03-17 22:50:29 +01:00
|
|
|
invoke NetFree, [bufferptr]
|
2014-01-23 13:46:33 +01:00
|
|
|
popf
|
2014-01-23 16:01:01 +01:00
|
|
|
or eax, -1
|
2014-01-22 22:28:03 +01:00
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Interrupt handler ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
align 4
|
|
|
|
int_handler:
|
|
|
|
|
|
|
|
push ebx esi edi
|
|
|
|
|
2013-08-05 13:00:22 +02:00
|
|
|
DEBUGF 1,"INT\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
;-------------------------------------------
|
|
|
|
; Find pointer of device wich made IRQ occur
|
|
|
|
|
|
|
|
mov ecx, [devices]
|
|
|
|
test ecx, ecx
|
|
|
|
jz .nothing
|
|
|
|
mov esi, device_list
|
|
|
|
.nextdevice:
|
|
|
|
mov ebx, [esi]
|
2014-01-23 18:47:35 +01:00
|
|
|
mov edi, [ebx + device.mmio_addr]
|
2013-05-28 19:34:26 +02:00
|
|
|
mov eax, [edi + REG_ICR]
|
|
|
|
test eax, eax
|
|
|
|
jnz .got_it
|
|
|
|
.continue:
|
|
|
|
add esi, 4
|
|
|
|
dec ecx
|
|
|
|
jnz .nextdevice
|
|
|
|
.nothing:
|
|
|
|
pop edi esi ebx
|
|
|
|
xor eax, eax
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
.got_it:
|
2014-01-22 22:28:03 +01:00
|
|
|
DEBUGF 1,"Device: %x Status: %x\n", ebx, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
;---------
|
|
|
|
; RX done?
|
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
test eax, ICR_RXDMT0 + ICR_RXT0
|
2013-05-28 19:34:26 +02:00
|
|
|
jz .no_rx
|
|
|
|
|
|
|
|
push eax ebx
|
2014-01-22 22:28:03 +01:00
|
|
|
.retaddr:
|
|
|
|
pop ebx eax
|
2013-05-28 19:34:26 +02:00
|
|
|
; Get last descriptor addr
|
2014-01-23 18:47:35 +01:00
|
|
|
mov esi, [ebx + device.cur_rx]
|
2014-01-23 13:46:33 +01:00
|
|
|
shl esi, 4 ; esi = esi * sizeof.RDESC
|
2014-01-23 18:47:35 +01:00
|
|
|
lea esi, [ebx + device.rx_desc + esi]
|
2014-01-23 13:46:33 +01:00
|
|
|
cmp byte[esi + RDESC.status_h], 0 ; Check status field
|
2014-01-22 22:28:03 +01:00
|
|
|
je .no_rx
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
push eax ebx
|
|
|
|
push .retaddr
|
|
|
|
movzx ecx, word[esi + 8] ; Get the packet length
|
2013-07-09 19:59:57 +02:00
|
|
|
DEBUGF 1,"got %u bytes\n", ecx
|
2015-03-17 22:50:29 +01:00
|
|
|
mov eax, [esi + RX_RING_SIZE*sizeof.RDESC] ; Get packet pointer
|
|
|
|
push eax
|
|
|
|
mov [eax + NET_BUFF.length], ecx
|
|
|
|
mov [eax + NET_BUFF.device], ebx
|
|
|
|
mov [eax + NET_BUFF.offset], NET_BUFF.data
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Update stats
|
2014-01-23 18:47:35 +01:00
|
|
|
add dword[ebx + device.bytes_rx], ecx
|
|
|
|
adc dword[ebx + device.bytes_rx + 4], 0
|
|
|
|
inc [ebx + device.packets_rx]
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
; Allocate new descriptor
|
|
|
|
push esi
|
2015-03-17 22:50:29 +01:00
|
|
|
invoke NetAlloc, MAX_PKT_SIZE+NET_BUFF.data
|
2014-01-22 22:28:03 +01:00
|
|
|
pop esi
|
2014-11-01 19:39:23 +01:00
|
|
|
test eax, eax
|
|
|
|
jz .out_of_mem
|
2014-01-23 13:46:33 +01:00
|
|
|
mov dword[esi + RX_RING_SIZE*sizeof.RDESC], eax
|
2014-01-23 18:47:35 +01:00
|
|
|
invoke GetPhysAddr
|
2015-03-17 22:50:29 +01:00
|
|
|
add eax, NET_BUFF.data
|
2014-01-23 13:46:33 +01:00
|
|
|
mov [esi + RDESC.addr_l], eax
|
|
|
|
mov [esi + RDESC.status_l], 0
|
|
|
|
mov [esi + RDESC.status_h], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
; Move the receive descriptor tail
|
2014-01-23 18:47:35 +01:00
|
|
|
mov esi, [ebx + device.mmio_addr]
|
|
|
|
mov eax, [ebx + device.cur_rx]
|
2014-01-22 22:28:03 +01:00
|
|
|
mov [esi + REG_RDT], eax
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-11-01 19:39:23 +01:00
|
|
|
; Move to next rx desc
|
|
|
|
inc [ebx + device.cur_rx]
|
|
|
|
and [ebx + device.cur_rx], RX_RING_SIZE-1
|
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
jmp [EthInput]
|
2014-11-01 19:39:23 +01:00
|
|
|
|
|
|
|
.out_of_mem:
|
|
|
|
DEBUGF 2,"Out of memory!\n"
|
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
; Move to next rx desc
|
2014-01-23 18:47:35 +01:00
|
|
|
inc [ebx + device.cur_rx]
|
|
|
|
and [ebx + device.cur_rx], RX_RING_SIZE-1
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-03-17 22:50:29 +01:00
|
|
|
jmp [EthInput]
|
2013-05-28 19:34:26 +02:00
|
|
|
.no_rx:
|
|
|
|
|
|
|
|
;--------------
|
|
|
|
; Link Changed?
|
|
|
|
|
|
|
|
test eax, ICR_LSC
|
|
|
|
jz .no_link
|
|
|
|
|
2014-01-22 22:28:03 +01:00
|
|
|
DEBUGF 2,"Link Changed\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.no_link:
|
|
|
|
|
|
|
|
;---------------
|
|
|
|
; Transmit done?
|
|
|
|
|
|
|
|
test eax, ICR_TXDW
|
|
|
|
jz .no_tx
|
|
|
|
|
2013-07-09 19:59:57 +02:00
|
|
|
DEBUGF 1,"Transmit done\n"
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-09-23 10:27:47 +02:00
|
|
|
; call clean_tx
|
|
|
|
|
|
|
|
.no_tx:
|
|
|
|
pop edi esi ebx
|
|
|
|
xor eax, eax
|
|
|
|
inc eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
clean_tx:
|
|
|
|
|
2014-01-23 13:46:33 +01:00
|
|
|
.txdesc_loop:
|
2014-01-23 18:47:35 +01:00
|
|
|
mov edi, [ebx + device.last_tx]
|
2014-01-23 13:46:33 +01:00
|
|
|
shl edi, 4 ; edi = edi * sizeof.TDESC
|
2014-01-23 18:47:35 +01:00
|
|
|
lea edi, [ebx + device.tx_desc + edi]
|
2014-01-23 13:46:33 +01:00
|
|
|
test [edi + TDESC.status], TXDESC_DD ; Descriptor done?
|
|
|
|
jz .no_tx
|
|
|
|
cmp dword[edi + TX_RING_SIZE*sizeof.TDESC], 0
|
|
|
|
je .no_tx
|
|
|
|
|
|
|
|
DEBUGF 1,"Cleaning up TX desc: 0x%x\n", edi
|
|
|
|
|
|
|
|
push ebx
|
|
|
|
push dword[edi + TX_RING_SIZE*sizeof.TDESC]
|
|
|
|
mov dword[edi + TX_RING_SIZE*sizeof.TDESC], 0
|
2015-03-17 22:50:29 +01:00
|
|
|
invoke NetFree
|
2014-01-23 13:46:33 +01:00
|
|
|
pop ebx
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
inc [ebx + device.last_tx]
|
|
|
|
and [ebx + device.last_tx], TX_RING_SIZE-1
|
2014-01-23 13:46:33 +01:00
|
|
|
jmp .txdesc_loop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
.no_tx:
|
2014-09-23 10:27:47 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; End of code
|
|
|
|
|
2014-01-24 17:40:41 +01:00
|
|
|
data fixups
|
|
|
|
end data
|
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
include '../peimport.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
include_debug_strings
|
|
|
|
my_service db 'I8254X', 0 ; max 16 chars include zero
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2014-01-23 18:47:35 +01:00
|
|
|
align 4
|
|
|
|
devices dd 0
|
|
|
|
device_list rd MAX_DEVICES ; This list contains all pointers to device structures the driver is handling
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|