9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 #include <aros/debug.h>
32 if (unit->rtl8139u_flags & IFF_DEBUG) \
37 #include <exec/types.h>
38 #include <exec/libraries.h>
39 #include <exec/semaphores.h>
40 #include <exec/devices.h>
41 #include <exec/interrupts.h>
48 #include <devices/timer.h>
49 #include <devices/sana2.h>
50 #include <devices/sana2specialstats.h>
52 #include <proto/exec.h>
54 #include LC_LIBDEFS_FILE
55 #define LIBBASETYPEPTR struct RTL8139Base *
57 #define net_device RTL8139Unit
59 #define RTL8139_TASK_NAME "%s.task"
60 #define RTL8139_PORT_NAME "%s.port"
62 /** Operational parameters that are set at compile time **/
63 #define ETH_ZLEN 60 // Min. octets in frame sans FCS
65 // Maximum size of the in-memory receive ring (smaller if no memory)
66 #define RX_BUF_LEN_IDX 2 // 0=8K, 1=16K, 2=32K, 3=64K
67 #define RX_FIFO_THRESH 6 // Rx buffer level before first PCI xfer
68 #define RX_DMA_BURST 6 // Maximum PCI burst, '4' is 256 bytes
70 // Size of the Tx bounce buffers -- must be at least (mtu+14+4)
71 #define TX_BUF_SIZE 1536
72 #define NUM_TX_DESC 4 // Number of Tx descriptor registers
73 #define TX_FIFO_THRESH 256 // In bytes, rounded down to 32 byte units
74 #define TX_DMA_BURST 6 // Calculate as 16 << val
76 /** Device Driver Structures **/
78 extern struct Library
*OOPBase
;
82 struct Device rtl8139b_Device
;
84 OOP_Object
*rtl8139b_PCI
;
85 OOP_AttrBase rtl8139b_PCIDeviceAttrBase
;
87 ULONG rtl8139b_UnitCount
;
88 struct List rtl8139b_Units
;
91 #undef HiddPCIDeviceAttrBase
92 #define HiddPCIDeviceAttrBase (LIBBASE->rtl8139b_PCIDeviceAttrBase)
96 struct MsgPort
*rtl8139sm_SyncPort
;
97 struct RTL8139Unit
*rtl8139sm_Unit
;
112 struct MsgPort read_port
;
113 BOOL (*rx_function
)(APTR
, APTR
, ULONG
);
114 BOOL (*tx_function
)(APTR
, APTR
, ULONG
);
115 struct Hook
*filter_hook
;
116 struct MinList initial_stats
;
123 struct Sana2PacketTypeStats stats
;
131 struct Sana2PacketTypeStats stats
;
140 ULONG lower_bound_left
;
141 ULONG upper_bound_left
;
142 UWORD lower_bound_right
;
143 UWORD upper_bound_right
;
146 /* Big endian: should work, but is untested */
153 ULONG BufferMsgLength
;
169 struct MinNode rtl8139u_Node
;
171 struct RTL8139Base
*rtl8139u_device
;
173 STRPTR rtl8139u_name
;
175 ULONG rtl8139u_UnitNum
;
176 IPTR rtl8139u_DriverFlags
;
178 OOP_Object
*rtl8139u_PCIDevice
;
179 OOP_Object
*rtl8139u_PCIDriver
;
182 int rtl8139u_open_count
;
183 struct SignalSemaphore rtl8139u_unit_lock
;
185 LONG rtl8139u_range_count
;
186 struct MinList rtl8139u_Openers
;
187 struct MinList rtl8139u_multicast_ranges
;
188 struct MinList rtl8139u_type_trackers
;
190 struct timeval rtl8139u_toutPOLL
;
191 BOOL rtl8139u_toutNEED
;
192 BOOL rtl8139u_IntsAdded
;
194 struct MsgPort
*rtl8139u_TimerSlowPort
;
195 struct timerequest
*rtl8139u_TimerSlowReq
;
197 struct MsgPort
*rtl8139u_TimerFastPort
;
198 struct timerequest
*rtl8139u_TimerFastReq
;
201 ULONG rtl8139u_flags
;
202 struct Sana2DeviceQuery rtl8139u_Sana2Info
;
203 struct Sana2DeviceStats rtl8139u_stats
;
204 ULONG rtl8139u_special_stats
[STAT_COUNT
];
206 char *rtl8139u_rtl_cardname
;
207 char *rtl8139u_rtl_chipname
;
208 ULONG rtl8139u_rtl_chipcapabilities
;
210 ULONG rtl8139u_rtl_LinkSpeed
;
211 #define support_fdx (1 << 0) // Supports Full Duplex
212 #define support_mii (1 << 1)
213 #define support_fset (1 << 2)
214 #define support_ltint (1 << 3)
215 #define support_dxsuflo (1 << 4)
217 void (*initialize
)(struct RTL8139Unit
*);
218 void (*deinitialize
)(struct RTL8139Unit
*);
219 int (*start
)(struct RTL8139Unit
*);
220 int (*stop
)(struct RTL8139Unit
*);
221 int (*alloc_rx
)(struct RTL8139Unit
*);
222 void (*set_mac_address
)(struct RTL8139Unit
*);
223 void (*linkchange
)(struct RTL8139Unit
*);
224 void (*linkirq
)(struct RTL8139Unit
*);
225 // ULONG (*descr_getlength)(struct ring_desc *prd, ULONG v);
226 void (*set_multicast
)(struct RTL8139Unit
*);
228 struct Process
*rtl8139u_Process
;
230 struct Interrupt rtl8139u_irqhandler
;
231 struct Interrupt rtl8139u_touthandler
;
232 IPTR rtl8139u_DeviceID
;
233 APTR rtl8139u_BaseMem
;
234 IPTR rtl8139u_SizeMem
;
235 IPTR rtl8139u_BaseIO
;
237 BYTE rtl8139u_signal_0
;
238 BYTE rtl8139u_signal_1
;
239 BYTE rtl8139u_signal_2
;
240 BYTE rtl8139u_signal_3
;
242 struct MsgPort
*rtl8139u_input_port
;
244 struct MsgPort
*rtl8139u_request_ports
[REQUEST_QUEUE_COUNT
];
246 struct Interrupt rtl8139u_rx_int
;
247 struct Interrupt rtl8139u_tx_int
;
249 ULONG rtl8139u_state
;
250 APTR rtl8139u_mc_list
;
251 UBYTE rtl8139u_dev_addr
[6];
252 UBYTE rtl8139u_org_addr
[6];
253 struct fe_priv
*rtl8139u_fe_priv
;
256 void handle_request(LIBBASETYPEPTR
, struct IOSana2Req
*);
258 /* Media selection options. */
270 /* Standard interface flags (netdevice->flags). */
271 #define IFF_UP 0x1 /* interface is up */
272 #define IFF_BROADCAST 0x2 /* broadcast address valid */
273 #define IFF_DEBUG 0x4 /* turn on debugging */
274 #define IFF_LOOPBACK 0x8 /* is a loopback net */
275 #define IFF_POINTOPOINT 0x10 /* interface is has p-p link */
276 #define IFF_NOTRAILERS 0x20 /* avoid use of trailers */
277 #define IFF_RUNNING 0x40 /* resources allocated */
278 #define IFF_NOARP 0x80 /* no ARP protocol */
279 #define IFF_PROMISC 0x100 /* receive all packets */
280 #define IFF_ALLMULTI 0x200 /* receive all multicast packets*/
282 #define IFF_MASTER 0x400 /* master of a load balancer */
283 #define IFF_SLAVE 0x800 /* slave of a load balancer */
285 #define IFF_MULTICAST 0x1000 /* Supports multicast */
287 #define IFF_VOLATILE (IFF_LOOPBACK|IFF_POINTOPOINT|IFF_BROADCAST|IFF_MASTER|IFF_SLAVE|IFF_RUNNING)
289 #define IFF_PORTSEL 0x2000 /* can set media type */
290 #define IFF_AUTOMEDIA 0x4000 /* auto media select active */
291 #define IFF_DYNAMIC 0x8000 /* dialup device with changing addresses*/
292 #define IFF_SHARED 0x10000 /* interface may be shared */
293 #define IFF_CONFIGURED 0x20000 /* interface already configured */
296 * We tag multicasts with these structures.
299 #define MAX_ADDR_LEN 32
303 struct dev_mc_list
*next
;
304 UBYTE dmi_addr
[MAX_ADDR_LEN
];
305 unsigned char dmi_addrlen
;
311 struct RTL8139Unit
*pci_dev
;
319 unsigned int phy_oui
;
322 struct SignalSemaphore lock
;
326 /* Start - rtl new */
329 char mii_phys
[4]; //MII device address
330 unsigned short advertising
; //NWay media advertising
332 unsigned int rx_config
;
334 unsigned int rx_buf_len
;
335 unsigned int rx_current
;
339 unsigned char *tx_pbuf
[NUM_TX_DESC
];
340 unsigned char *tx_buf
[NUM_TX_DESC
];
341 unsigned int tx_dirty
;
342 unsigned int tx_current
;
345 unsigned short cur_rx
;
348 ULONG next_tx
, nic_tx
;
352 ULONG need_linktimer
;
353 struct timeval link_timeout
;
357 #define pci_name(unit) (unit->rtl8139u_name)
362 #define ETH_DATA_LEN 1500
364 #define ETH_ADDRESSSIZE 6
365 #define ETH_HEADERSIZE 14
366 #define ETH_CRCSIZE 4
367 #define ETH_MTU (ETH_DATA_LEN)
368 #define ETH_MAXPACKETSIZE ((ETH_HEADERSIZE) + (ETH_MTU) + (ETH_CRCSIZE))
370 #define ETH_PACKET_DEST 0
371 #define ETH_PACKET_SOURCE 6
372 #define ETH_PACKET_TYPE 12
373 #define ETH_PACKET_IEEELEN 12
374 #define ETH_PACKET_SNAPTYPE 20
375 #define ETH_PACKET_DATA 14
376 #define ETH_PACKET_CRC (ETH_PACKET_DATA + ETH_MTU)
378 #define RXTX_ALLOC_BUFSIZE (ETH_MAXPACKETSIZE + 26)
380 #define TX_LIMIT_STOP 63
381 #define TX_LIMIT_START 62
384 UBYTE eth_packet_dest
[6];
385 UBYTE eth_packet_source
[6];
386 UWORD eth_packet_type
;
387 UBYTE eth_packet_data
[ETH_MTU
];
388 UBYTE eth_packet_crc
[4];
389 UBYTE eth_pad
[RXTX_ALLOC_BUFSIZE
- ETH_MAXPACKETSIZE
];
390 } __attribute__((packed
));
391 #define eth_packet_ieeelen eth_packet_type
393 void rtl8139nic_get_functions(struct RTL8139Unit
*Unit
);
395 /* ***************************** */
396 /* RTL8139 DEFINES */
397 /* ***************************** */
399 enum rtl_boardcapabilities
401 RTLc_HAS_MII_XCVR
= 0x01,
402 RTLc_HAS_CHIP_XCVR
= 0x02,
403 RTLc_HAS_LNK_CHNG
= 0x04,
407 // Symbolic offsets to registers
411 RTLr_MAC0
= 0x00, // Ethernet hardware address
412 RTLr_MAR0
= 0x08, // Multicast filter
413 RTLr_TxStatus0
= 0x10, // Transmit status (Four 32bit registers)
414 RTLr_TxStatus1
= 0x14,
415 RTLr_TxStatus2
= 0x18,
416 RTLr_TxStatus3
= 0x1C,
417 RTLr_TxAddr0
= 0x20, // Tx descriptors (also four 32bit)
422 RTLr_RxEarlyCnt
= 0x34,
423 RTLr_RxEarlyStatus
= 0x36,
425 RTLr_RxBufPtr
= 0x38,
426 RTLr_RxBufAddr
= 0x3A,
427 RTLr_IntrMask
= 0x3C,
428 RTLr_IntrStatus
= 0x3E,
429 RTLr_TxConfig
= 0x40,
430 RTLr_RxConfig
= 0x44,
431 RTLr_Timer
= 0x48, // A general-purpose counter
432 RTLr_RxMissed
= 0x4C, // 24 bits valid, write clears
436 RTLr_FlashReg
= 0x54, // Timer Interrupt Register ?
437 RTLr_GPPinData
= 0x58, // Media status register ?
438 RTLr_GPPinDir
= 0x59, // Configuration register 3
439 RTLr_MII_SMI
= 0x5A, // Configuration register 4
440 RTLr_HltClk
= 0x5B, // Reserved
441 RTLr_MultiIntr
= 0x5C,
443 RTLr_TxSummary
= 0x60, // Transmit status of all descriptors
444 RTLr_MII_BMCR
= 0x62, // Basic mode control register
445 RTLr_MII_BMSR
= 0x64, // Basic mode status register
446 RTLr_NWayAdvert
= 0x66, // Auto negociation expansion register
447 RTLr_NWayLPAR
= 0x68, // Auto negociation link partner register
448 RTLr_NWayExpansion
= 0x6A, // Auto negociation expansion register
449 RTLr_DIS
= 0x6C, // Disconnect counter
450 RTLr_FCSC
= 0x6E, // False carrier sense counter
452 // Undocumented registers, but required for proper operation
453 RTLr_FIFOTMS
= 0x70, // FIFO Control and test (N-way tezt register)
454 RTLr_CSCR
= 0x74, // Chip Status and Configuration Register
455 RTLr_PARA78
= 0x78, // PHY parameter 1
456 RTLr_PARA7c
= 0x7c, // Magic transceiver parameter register (Twister parameter)
459 enum rtl_chipclearbitmasks
461 MultiIntrClear
= 0xf000,
463 ConfigClear
= (1 << 7) | (1 << 6) | (1 << 3) | (1 << 2) | (1 << 1)
474 // Interrupt register bits
476 enum rtl_intrstatusbits
489 #define RxAckBits (RxFIFOOver | RxOverflow | RxOK)
491 enum rtl_txstatusbits
493 TxHostOwns
= 0x00002000,
494 TxUnderrun
= 0x00004000,
495 TxStatOK
= 0x00008000,
496 TxOutOfWindow
= 0x20000000,
497 TxAborted
= 0x40000000,
498 TxCarrierLost
= 0x80000000,
501 enum rtl_rxstatusbits
508 RxBadSymbol
= 0x0020,
509 RxBroadcast
= 0x2000,
511 RxMulticast
= 0x8000,
516 enum rtl_rxconfigbits
518 AcceptAllPhys
= 0x01,
520 AcceptMulticast
= 0x04,
523 AcceptBroadcast
= 0x08,
526 enum rtl_txconfigbits
528 /* Interframe Gap Time. Only TxIFG96 doesnt violate IEEE 802.3 */
530 TxIFG84
= (0<<TxIFGShift
), /* 8.4us / 840ns */
531 TxIFG88
= (1<<TxIFGShift
), /* 8.8us / 880ns */
532 TxIFG92
= (2<<TxIFGShift
), /* 9.2us / 920ns */
533 TxIFG96
= (3<<TxIFGShift
), /* 9.6us / 960ns */
535 TxLoopBack
= (1<<18) | (1<<17), /* Enable loopback test mode */
536 TxCRC
= (1<<16), /* Disable appending CRC to end of Tx Packet */
537 TxClearAbt
= (1<<0), /* Clear abort (WO) */
538 TxDMAShift
= 8, /* DMA burst value (0-7) is shifted this many bits */
539 TxRetryShift
= 4, /* TXRR value (0-15) is shifted this many bits */
541 TxVersionMask
= 0x7c800000 /* Mask out version bits 30-26, 23 */
547 Cfg1_PM_Enable
= 0x01,
548 Cfg1_VPD_Enable
= 0x02,
551 LWAKE
= 0x10, /* Not on 8139/8139A */
552 Cfg1_Driver_Load
= 0x20,
559 CSCR_LinkOKBit
= 0x00400,
560 CSCR_LinkDownOffCmd
= 0x003c0,
561 CSCR_LinkChangeBit
= 0x00800,
562 CSCR_LinkStatusBits
= 0x0f000,
563 CSCR_LinkDownCmd
= 0x0f3c0,
566 /** Serial EEPROM section **/
570 #define EE_SHIFT_CLK 0x04 // EEPROM shift clock
571 #define EE_CS 0x08 // EEPROM chip select
572 #define EE_DATA_WRITE 0x02 // EEPROM chip data in
573 #define EE_WRITE_0 0x00
574 #define EE_WRITE_1 0x02
575 #define EE_DATA_READ 0x01 // EEPROM chip data out
576 #define EE_ENB (0x80 | EE_CS)
578 // Delay between EEPROM clock transitions.
579 // No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
581 #define eeprom_delay(ee_addr) LONGIN(ee_addr)
583 // The EEPROM commands include the alway-set leading bit
585 #define EE_WRITE_CMD (5)
586 #define EE_READ_CMD (6)
587 #define EE_ERASE_CMD (7)
589 /** MII serial management **/
591 // Read and write the MII management registers using software-generated
592 // serial MDIO protocol.
593 // The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
594 // met by back-to-back PCI I/O cycles, but we insert a delay to avoid
595 // "overclocking" issues
597 #define MDIO_DIR 0x80
598 #define MDIO_DATA_OUT 0x04
599 #define MDIO_DATA_IN 0x02
600 #define MDIO_CLK 0x01
601 #define MDIO_WRITE0 (MDIO_DIR)
602 #define MDIO_WRITE1 (MDIO_DIR | MDIO_DATA_OUT)
604 #define mdio_delay(mdio_addr) LONGIN(mdio_addr)
606 int rtl8139nic_set_rxmode(struct net_device
*dev
);