Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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