Subversion Repositories Kolibri OS

Rev

Rev 4521 | Rev 4524 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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