Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
3545 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
7251 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2018. All rights reserved.    ;;
3545 hidnplayr 4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  Broadcom NetXtreme 57xx driver for KolibriOS                   ;;
7
;;                                                                 ;;
8
;;                                                                 ;;
9
;;          GNU GENERAL PUBLIC LICENSE                             ;;
10
;;             Version 2, June 1991                                ;;
11
;;                                                                 ;;
12
;; Broadcom's programmers's manual for the BCM57xx                 ;;
13
;; http://www.broadcom.com/collateral/pg/57XX-PG105-R.pdf          ;;
14
;;                                                                 ;;
15
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
16
 
4663 hidnplayr 17
format PE DLL native
18
entry START
3545 hidnplayr 19
 
4663 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
27
        __DEBUG_LEVEL__         = 2
28
 
4663 hidnplayr 29
section '.flat' readable writable executable
30
 
31
include '../proc32.inc'
4467 hidnplayr 32
include '../struct.inc'
33
include '../macros.inc'
3545 hidnplayr 34
include '../fdo.inc'
5074 hidnplayr 35
include '../netdrv.inc'
3545 hidnplayr 36
 
4663 hidnplayr 37
struct  device          ETH_DEVICE
3545 hidnplayr 38
 
4663 hidnplayr 39
        mmio_addr       dd ?
40
        pci_bus         dd ?
41
        pci_dev         dd ?
42
        irq_line        db ?
3545 hidnplayr 43
 
4663 hidnplayr 44
        cur_tx          dd ?
45
        last_tx         dd ?
3545 hidnplayr 46
 
4663 hidnplayr 47
        rb 0x100 - ($ and 0xff) ; align 256
48
        rx_desc         rd 256/8
3545 hidnplayr 49
 
4663 hidnplayr 50
        rb 0x100 - ($ and 0xff) ; align 256
51
        tx_desc         rd 256/8
3545 hidnplayr 52
 
4663 hidnplayr 53
ends
3545 hidnplayr 54
 
55
 
56
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
57
;;                        ;;
58
;; proc START             ;;
59
;;                        ;;
60
;; (standard driver proc) ;;
61
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62
 
4663 hidnplayr 63
proc START c, reason:dword, cmdline:dword
3545 hidnplayr 64
 
4663 hidnplayr 65
        cmp     [reason], DRV_ENTRY
66
        jne     .fail
3545 hidnplayr 67
 
4663 hidnplayr 68
        DEBUGF  1,"Loading driver\n"
69
        invoke  RegService, my_service, service_proc
3545 hidnplayr 70
        ret
71
 
72
  .fail:
4663 hidnplayr 73
        xor     eax, eax
3545 hidnplayr 74
        ret
75
 
76
endp
77
 
78
 
79
 
80
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
81
;;                        ;;
82
;; proc SERVICE_PROC      ;;
83
;;                        ;;
84
;; (standard driver proc) ;;
85
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
86
 
87
proc service_proc stdcall, ioctl:dword
88
 
89
        mov     edx, [ioctl]
4470 hidnplayr 90
        mov     eax, [edx + IOCTL.io_code]
3545 hidnplayr 91
 
92
;------------------------------------------------------
93
 
94
        cmp     eax, 0 ;SRV_GETVERSION
95
        jne     @F
96
 
4470 hidnplayr 97
        cmp     [edx + IOCTL.out_size], 4
3545 hidnplayr 98
        jb      .fail
4470 hidnplayr 99
        mov     eax, [edx + IOCTL.output]
3545 hidnplayr 100
        mov     [eax], dword API_VERSION
101
 
102
        xor     eax, eax
103
        ret
104
 
105
;------------------------------------------------------
106
  @@:
107
        cmp     eax, 1 ;SRV_HOOK
108
        jne     .fail
109
 
4470 hidnplayr 110
        cmp     [edx + IOCTL.inp_size], 3               ; Data input must be at least 3 bytes
3545 hidnplayr 111
        jb      .fail
112
 
4470 hidnplayr 113
        mov     eax, [edx + IOCTL.input]
3545 hidnplayr 114
        cmp     byte [eax], 1                           ; 1 means device number and bus number (pci) are given
115
        jne     .fail                                   ; other types arent supported for this card yet
116
 
117
; check if the device is already listed
118
 
119
        mov     esi, device_list
120
        mov     ecx, [devices]
121
        test    ecx, ecx
122
        jz      .firstdevice
123
 
4470 hidnplayr 124
;        mov     eax, [edx + IOCTL.input]                      ; get the pci bus and device numbers
3545 hidnplayr 125
        mov     ax, [eax+1]                             ;
126
  .nextdevice:
127
        mov     ebx, [esi]
4663 hidnplayr 128
        cmp     al, byte [ebx + device.pci_bus]
3545 hidnplayr 129
        jne     .next
4663 hidnplayr 130
        cmp     ah, byte [ebx + device.pci_dev]
3545 hidnplayr 131
        je      .find_devicenum                         ; Device is already loaded, let's find it's device number
132
  .next:
133
        add     esi, 4
134
        loop    .nextdevice
135
 
136
 
137
; This device doesnt have its own eth_device structure yet, lets create one
138
  .firstdevice:
139
        cmp     [devices], MAX_DEVICES                  ; First check if the driver can handle one more card
140
        jae     .fail
141
 
4663 hidnplayr 142
        allocate_and_clear ebx, sizeof.device, .fail      ; Allocate the buffer for device structure
3545 hidnplayr 143
 
144
; Fill in the direct call addresses into the struct
145
 
4663 hidnplayr 146
        mov     [ebx + device.reset], reset
147
        mov     [ebx + device.transmit], transmit
148
        mov     [ebx + device.unload], unload
149
        mov     [ebx + device.name], my_service
3545 hidnplayr 150
 
151
; save the pci bus and device numbers
152
 
4470 hidnplayr 153
        mov     eax, [edx + IOCTL.input]
3545 hidnplayr 154
        movzx   ecx, byte [eax+1]
4663 hidnplayr 155
        mov     [ebx + device.pci_bus], ecx
3545 hidnplayr 156
        movzx   ecx, byte [eax+2]
4663 hidnplayr 157
        mov     [ebx + device.pci_dev], ecx
3545 hidnplayr 158
 
159
; Now, it's time to find the base mmio addres of the PCI device
160
 
7251 hidnplayr 161
        stdcall PCI_find_mmio, [ebx + device.pci_bus], [ebx + device.pci_dev]
162
        test    eax, eax
163
        jz      .destroy
3545 hidnplayr 164
 
165
; Create virtual mapping of the physical memory
166
 
7251 hidnplayr 167
        invoke  MapIoMem, eax, 10000h, PG_SW+PG_NOCACHE
4663 hidnplayr 168
        mov     [ebx + device.mmio_addr], eax
3545 hidnplayr 169
 
170
; We've found the mmio address, find IRQ now
171
 
4663 hidnplayr 172
        invoke  PciRead8, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.interrupt_line
173
        mov     [ebx + device.irq_line], al
3545 hidnplayr 174
 
175
        DEBUGF  1,"Hooking into device, dev:%x, bus:%x, irq:%x, addr:%x\n",\
4663 hidnplayr 176
        [ebx + device.pci_dev]:1,[ebx + device.pci_bus]:1,[ebx + device.irq_line]:1,[ebx + device.mmio_addr]:8
3545 hidnplayr 177
 
178
; Ok, the eth_device structure is ready, let's probe the device
179
        call    probe                                                   ; this function will output in eax
180
        test    eax, eax
181
        jnz     .err                                                    ; If an error occured, exit
182
 
183
        mov     eax, [devices]                                          ; Add the device structure to our device list
184
        mov     [device_list+4*eax], ebx                                ; (IRQ handler uses this list to find device)
185
        inc     [devices]                                               ;
186
 
4663 hidnplayr 187
        mov     [ebx + device.type], NET_TYPE_ETH
188
        invoke  NetRegDev
3545 hidnplayr 189
 
190
        cmp     eax, -1
191
        je      .destroy
192
 
193
        ret
194
 
195
; If the device was already loaded, find the device number and return it in eax
196
 
197
  .find_devicenum:
198
        DEBUGF  1,"Trying to find device number of already registered device\n"
4663 hidnplayr 199
        invoke  NetPtrToNum                                             ; This kernel procedure converts a pointer to device struct in ebx
3545 hidnplayr 200
                                                                        ; into a device number in edi
201
        mov     eax, edi                                                ; Application wants it in eax instead
202
        DEBUGF  1,"Kernel says: %u\n", eax
203
        ret
204
 
205
; If an error occured, remove all allocated data and exit (returning -1 in eax)
206
 
207
  .destroy:
208
        ; todo: reset device into virgin state
209
 
210
  .err:
4663 hidnplayr 211
        invoke  KernelFree, ebx
3545 hidnplayr 212
 
213
  .fail:
214
        or      eax, -1
215
        ret
216
 
217
;------------------------------------------------------
218
endp
219
 
220
 
221
;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
222
;;                                                                        ;;
223
;;        Actual Hardware dependent code starts here                      ;;
224
;;                                                                        ;;
225
;;/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\;;
226
 
227
unload:
228
        ; TODO: (in this particular order)
229
        ;
230
        ; - Stop the device
231
        ; - Detach int handler
232
        ; - Remove device from local list (device_list)
233
        ; - call unregister function in kernel
234
        ; - Remove all allocated structures and buffers the card used
235
 
236
        or      eax, -1
237
 
238
ret
239
 
240
 
241
 
242
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
243
;;
4663 hidnplayr 244
;;  probe: enables the device (if it really is BCM57XX)
3545 hidnplayr 245
;;
246
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4663 hidnplayr 247
 
3545 hidnplayr 248
probe:
249
 
250
        DEBUGF  1,"Probe\n"
251
 
4663 hidnplayr 252
; Make the device a bus master
253
        invoke  PciRead32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command
254
        or      al, PCI_CMD_MASTER
255
        invoke  PciWrite32, [ebx + device.pci_bus], [ebx + device.pci_dev], PCI_header00.command, eax
3545 hidnplayr 256
 
4663 hidnplayr 257
 
3545 hidnplayr 258
        ; TODO: validate the device
259
 
260
 
261
 
262
 
263
reset:
264
 
265
        DEBUGF  1,"Reset\n"
266
 
4663 hidnplayr 267
        movzx   eax, [ebx + device.irq_line]
3545 hidnplayr 268
        DEBUGF  1,"Attaching int handler to irq %x\n", eax:1
4663 hidnplayr 269
        invoke  AttachIntHandler, eax, int_handler, ebx
3545 hidnplayr 270
        test    eax, eax
271
        jnz     @f
272
        DEBUGF  1,"\nCould not attach int handler!\n"
273
;        or      eax, -1
274
;        ret
275
  @@:
276
 
277
        call    read_mac
278
 
279
; Set the mtu, kernel will be able to send now
4663 hidnplayr 280
        mov     [ebx + device.mtu], 1514
3545 hidnplayr 281
 
282
; Set link state to unknown
4663 hidnplayr 283
        mov     [ebx + device.state], ETH_LINK_UNKNOWN
3545 hidnplayr 284
 
285
        ret
286
 
287
 
288
 
289
 
290
align 4
291
read_mac:
292
 
293
        DEBUGF  1,"Read MAC\n"
294
 
4663 hidnplayr 295
        mov     esi, [ebx + device.mmio_addr]
296
        lea     edi, [ebx + device.mac]
3545 hidnplayr 297
        movsd
298
        movsw
299
 
300
  .mac_ok:
301
        DEBUGF  1,"MAC = %x-%x-%x-%x-%x-%x\n",\
4663 hidnplayr 302
        [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
3545 hidnplayr 303
 
304
        ret
305
 
306
 
307
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
308
;;                                         ;;
309
;; Transmit                                ;;
310
;;                                         ;;
311
;; In: buffer pointer in [esp+4]           ;;
312
;;     size of buffer in [esp+8]           ;;
313
;;     pointer to device structure in ebx  ;;
314
;;                                         ;;
315
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4663 hidnplayr 316
 
317
proc transmit stdcall bufferptr, buffersize
318
 
319
        pushf
320
        cli
321
 
322
        DEBUGF  1,"Transmitting packet, buffer:%x, size:%u\n", [bufferptr], [buffersize]
323
        mov     eax, [bufferptr]
324
        DEBUGF  1,"To: %x-%x-%x-%x-%x-%x From: %x-%x-%x-%x-%x-%x Type:%x%x\n",\
3545 hidnplayr 325
        [eax+00]:2,[eax+01]:2,[eax+02]:2,[eax+03]:2,[eax+04]:2,[eax+05]:2,\
326
        [eax+06]:2,[eax+07]:2,[eax+08]:2,[eax+09]:2,[eax+10]:2,[eax+11]:2,\
327
        [eax+13]:2,[eax+12]:2
328
 
4663 hidnplayr 329
        cmp     [buffersize], 1514
3545 hidnplayr 330
        ja      .fail
4663 hidnplayr 331
        cmp     [buffersize], 60
3545 hidnplayr 332
        jb      .fail
333
 
334
 
335
 
336
 
4663 hidnplayr 337
 
3545 hidnplayr 338
; Update stats
4663 hidnplayr 339
        inc     [ebx + device.packets_tx]
340
        mov     eax, [buffersize]
341
        add     dword[ebx + device.bytes_tx], eax
342
        adc     dword[ebx + device.bytes_tx + 4], 0
3545 hidnplayr 343
 
4663 hidnplayr 344
        DEBUGF  1,"Transmit OK\n"
345
        popf
4334 hidnplayr 346
        xor     eax, eax
4663 hidnplayr 347
        ret
3545 hidnplayr 348
 
349
  .fail:
4663 hidnplayr 350
        DEBUGF  2,"Transmit failed\n"
351
        invoke  KernelFree, [bufferptr]
352
        popf
4334 hidnplayr 353
        or      eax, -1
4663 hidnplayr 354
        ret
3545 hidnplayr 355
 
4663 hidnplayr 356
endp
3545 hidnplayr 357
 
4663 hidnplayr 358
 
3545 hidnplayr 359
;;;;;;;;;;;;;;;;;;;;;;;
360
;;                   ;;
361
;; Interrupt handler ;;
362
;;                   ;;
363
;;;;;;;;;;;;;;;;;;;;;;;
364
 
365
align 4
366
int_handler:
367
 
368
        push    ebx esi edi
369
 
4663 hidnplayr 370
        DEBUGF  1,"INT\n"
3545 hidnplayr 371
;-------------------------------------------
372
; Find pointer of device wich made IRQ occur
373
 
374
        mov     ecx, [devices]
375
        test    ecx, ecx
376
        jz      .nothing
377
        mov     esi, device_list
378
  .nextdevice:
379
        mov     ebx, [esi]
380
 
4663 hidnplayr 381
;        mov     edi, [ebx + device.mmio_addr]
3545 hidnplayr 382
;        mov     eax, [edi + REG_ICR]
383
        test    eax, eax
384
        jnz     .got_it
385
  .continue:
386
        add     esi, 4
387
        dec     ecx
388
        jnz     .nextdevice
389
  .nothing:
390
        pop     edi esi ebx
391
        xor     eax, eax
392
 
393
        ret
394
 
395
  .got_it:
396
 
397
        DEBUGF  1,"Device: %x Status: %x ", ebx, eax
398
 
399
        pop     edi esi ebx
400
        xor     eax, eax
401
        inc     eax
402
 
403
        ret
404
 
405
 
406
 
407
 
408
; End of code
409
 
4663 hidnplayr 410
data fixups
411
end data
3545 hidnplayr 412
 
4663 hidnplayr 413
include '../peimport.inc'
414
 
3545 hidnplayr 415
my_service      db 'BCM57XX',0                   ; max 16 chars include zero
416
 
417
include_debug_strings                           ; All data wich FDO uses will be included here
418
 
4663 hidnplayr 419
align 4
420
devices         dd 0
3545 hidnplayr 421
device_list     rd MAX_DEVICES                  ; This list contains all pointers to device structures the driver is handling
422