Subversion Repositories Kolibri OS

Rev

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

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