2 =========================================================================
3 r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver for Linux kernel 2.4.x.
4 --------------------------------------------------------------------
7 Feb 4 2002 - created initially by ShuChen <shuchen@realtek.com.tw>.
8 May 20 2002 - Add link status force-mode and TBI mode support.
9 2004 - Massive updates. See kernel SCM system for details.
10 =========================================================================
11 1. [DEPRECATED: use ethtool instead] The media can be forced in 5 modes.
12 Command: 'insmod r8169 media = SET_MEDIA'
13 Ex: 'insmod r8169 media = 0x04' will force PHY to operate in 100Mpbs Half-duplex.
23 =========================================================================
24 VERSION 1.1 <2002/10/4>
26 The bit4:0 of MII register 4 is called "selector field", and have to be
27 00001b to indicate support of IEEE std 802.3 during NWay process of
28 exchanging Link Code Word (FLP).
30 VERSION 1.2 <2002/11/30>
33 - Use ether_crc in stock kernel (linux/crc32.h)
34 - Copy mc_filter setup code from 8139cp
35 (includes an optimization, and avoids set_bit use)
37 VERSION 1.6LK <2004/04/14>
39 - Merge of Realtek's version 1.6
40 - Conversion to DMA API
45 VERSION 2.2LK <2005/01/25>
47 - RX csum, TX csum/SG, TSO
49 - baby (< 7200) Jumbo frames support
50 - Merge of Realtek's version 2.2 (new phy)
53 #include <linux/module.h>
54 #include <linux/moduleparam.h>
55 #include <linux/pci.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58 #include <linux/delay.h>
59 #include <linux/ethtool.h>
60 #include <linux/mii.h>
61 #include <linux/if_vlan.h>
62 #include <linux/crc32.h>
65 #include <linux/tcp.h>
66 #include <linux/init.h>
67 #include <linux/dma-mapping.h>
72 #define RTL8169_VERSION "2.2LK"
73 #define MODULENAME "r8169"
74 #define PFX MODULENAME ": "
77 #define assert(expr) \
79 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
80 #expr,__FILE__,__FUNCTION__,__LINE__); \
82 #define dprintk(fmt, args...) do { printk(PFX fmt, ## args); } while (0)
84 #define assert(expr) do {} while (0)
85 #define dprintk(fmt, args...) do {} while (0)
86 #endif /* RTL8169_DEBUG */
88 #define TX_BUFFS_AVAIL(tp) \
89 (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
91 #ifdef CONFIG_R8169_NAPI
92 #define rtl8169_rx_skb netif_receive_skb
93 #define rtl8169_rx_hwaccel_skb vlan_hwaccel_rx
94 #define rtl8169_rx_quota(count, quota) min(count, quota)
96 #define rtl8169_rx_skb netif_rx
97 #define rtl8169_rx_hwaccel_skb vlan_hwaccel_receive_skb
98 #define rtl8169_rx_quota(count, quota) count
103 static int media
[MAX_UNITS
] = { -1, -1, -1, -1, -1, -1, -1, -1 };
104 static int num_media
= 0;
106 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
107 static int max_interrupt_work
= 20;
109 /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
110 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
111 static int multicast_filter_limit
= 32;
113 /* MAC address length */
114 #define MAC_ADDR_LEN 6
116 #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
117 #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
118 #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
119 #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
120 #define RxPacketMaxSize 0x3FE8 /* 16K - 1 - ETH_HLEN - VLAN - CRC... */
121 #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
122 #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
124 #define R8169_REGS_SIZE 256
125 #define R8169_NAPI_WEIGHT 64
126 #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
127 #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
128 #define RX_BUF_SIZE 1536 /* Rx Buffer size */
129 #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
130 #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
132 #define RTL8169_TX_TIMEOUT (6*HZ)
133 #define RTL8169_PHY_TIMEOUT (10*HZ)
135 /* write/read MMIO register */
136 #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
137 #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
138 #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
139 #define RTL_R8(reg) readb (ioaddr + (reg))
140 #define RTL_R16(reg) readw (ioaddr + (reg))
141 #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
144 RTL_GIGA_MAC_VER_B
= 0x00,
145 /* RTL_GIGA_MAC_VER_C = 0x03, */
146 RTL_GIGA_MAC_VER_D
= 0x01,
147 RTL_GIGA_MAC_VER_E
= 0x02,
148 RTL_GIGA_MAC_VER_X
= 0x04 /* Greater than RTL_GIGA_MAC_VER_E */
152 RTL_GIGA_PHY_VER_C
= 0x03, /* PHY Reg 0x03 bit0-3 == 0x0000 */
153 RTL_GIGA_PHY_VER_D
= 0x04, /* PHY Reg 0x03 bit0-3 == 0x0000 */
154 RTL_GIGA_PHY_VER_E
= 0x05, /* PHY Reg 0x03 bit0-3 == 0x0000 */
155 RTL_GIGA_PHY_VER_F
= 0x06, /* PHY Reg 0x03 bit0-3 == 0x0001 */
156 RTL_GIGA_PHY_VER_G
= 0x07, /* PHY Reg 0x03 bit0-3 == 0x0002 */
157 RTL_GIGA_PHY_VER_H
= 0x08, /* PHY Reg 0x03 bit0-3 == 0x0003 */
161 #define _R(NAME,MAC,MASK) \
162 { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
164 const static struct {
167 u32 RxConfigMask
; /* Clears the bits supported by this chip */
168 } rtl_chip_info
[] = {
169 _R("RTL8169", RTL_GIGA_MAC_VER_B
, 0xff7e1880),
170 _R("RTL8169s/8110s", RTL_GIGA_MAC_VER_D
, 0xff7e1880),
171 _R("RTL8169s/8110s", RTL_GIGA_MAC_VER_E
, 0xff7e1880),
172 _R("RTL8169s/8110s", RTL_GIGA_MAC_VER_X
, 0xff7e1880),
176 static struct pci_device_id rtl8169_pci_tbl
[] = {
177 {0x10ec, 0x8169, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
178 {0x1186, 0x4300, PCI_ANY_ID
, PCI_ANY_ID
, 0, 0, 0},
182 MODULE_DEVICE_TABLE(pci
, rtl8169_pci_tbl
);
184 static int rx_copybreak
= 200;
187 enum RTL8169_registers
{
188 MAC0
= 0, /* Ethernet hardware address. */
189 MAR0
= 8, /* Multicast filter. */
190 TxDescStartAddrLow
= 0x20,
191 TxDescStartAddrHigh
= 0x24,
192 TxHDescStartAddrLow
= 0x28,
193 TxHDescStartAddrHigh
= 0x2c,
219 RxDescAddrLow
= 0xE4,
220 RxDescAddrHigh
= 0xE8,
223 FuncEventMask
= 0xF4,
224 FuncPresetState
= 0xF8,
225 FuncForceEvent
= 0xFC,
228 enum RTL8169_register_content
{
229 /* InterruptStatusBits */
233 TxDescUnavail
= 0x80,
256 Cfg9346_Unlock
= 0xC0,
261 AcceptBroadcast
= 0x08,
262 AcceptMulticast
= 0x04,
264 AcceptAllPhys
= 0x01,
271 TxInterFrameGapShift
= 24,
272 TxDMAShift
= 8, /* DMA burst value (0-7) is shift this many bits */
275 TBIReset
= 0x80000000,
276 TBILoopback
= 0x40000000,
277 TBINwEnable
= 0x20000000,
278 TBINwRestart
= 0x10000000,
279 TBILinkOk
= 0x02000000,
280 TBINwComplete
= 0x01000000,
288 /* rtl8169_PHYstatus */
298 /* GIGABIT_PHY_registers */
301 PHY_AUTO_NEGO_REG
= 4,
302 PHY_1000_CTRL_REG
= 9,
304 /* GIGABIT_PHY_REG_BIT */
305 PHY_Restart_Auto_Nego
= 0x0200,
306 PHY_Enable_Auto_Nego
= 0x1000,
308 /* PHY_STAT_REG = 1 */
309 PHY_Auto_Neco_Comp
= 0x0020,
311 /* PHY_AUTO_NEGO_REG = 4 */
312 PHY_Cap_10_Half
= 0x0020,
313 PHY_Cap_10_Full
= 0x0040,
314 PHY_Cap_100_Half
= 0x0080,
315 PHY_Cap_100_Full
= 0x0100,
317 /* PHY_1000_CTRL_REG = 9 */
318 PHY_Cap_1000_Full
= 0x0200,
330 TBILinkOK
= 0x02000000,
333 enum _DescStatusBit
{
334 DescOwn
= (1 << 31), /* Descriptor is owned by NIC */
335 RingEnd
= (1 << 30), /* End of descriptor ring */
336 FirstFrag
= (1 << 29), /* First segment of a packet */
337 LastFrag
= (1 << 28), /* Final segment of a packet */
340 LargeSend
= (1 << 27), /* TCP Large Send Offload (TSO) */
341 MSSShift
= 16, /* MSS value position */
342 MSSMask
= 0xfff, /* MSS value + LargeSend bit: 12 bits */
343 IPCS
= (1 << 18), /* Calculate IP checksum */
344 UDPCS
= (1 << 17), /* Calculate UDP/IP checksum */
345 TCPCS
= (1 << 16), /* Calculate TCP/IP checksum */
346 TxVlanTag
= (1 << 17), /* Add VLAN tag */
349 PID1
= (1 << 18), /* Protocol ID bit 1/2 */
350 PID0
= (1 << 17), /* Protocol ID bit 2/2 */
352 #define RxProtoUDP (PID1)
353 #define RxProtoTCP (PID0)
354 #define RxProtoIP (PID1 | PID0)
355 #define RxProtoMask RxProtoIP
357 IPFail
= (1 << 16), /* IP checksum failed */
358 UDPFail
= (1 << 15), /* UDP/IP checksum failed */
359 TCPFail
= (1 << 14), /* TCP/IP checksum failed */
360 RxVlanTag
= (1 << 16), /* VLAN tag available */
363 #define RsvdMask 0x3fffc000
380 u8 __pad
[sizeof(void *) - sizeof(u32
)];
383 struct rtl8169_private
{
384 void __iomem
*mmio_addr
; /* memory map physical address */
385 struct pci_dev
*pci_dev
; /* Index of PCI device */
386 struct net_device_stats stats
; /* statistics of net device */
387 spinlock_t lock
; /* spin lock flag */
391 u32 cur_rx
; /* Index into the Rx descriptor buffer of next Rx pkt. */
392 u32 cur_tx
; /* Index into the Tx descriptor buffer of next Rx pkt. */
395 struct TxDesc
*TxDescArray
; /* 256-aligned Tx descriptor ring */
396 struct RxDesc
*RxDescArray
; /* 256-aligned Rx descriptor ring */
397 dma_addr_t TxPhyAddr
;
398 dma_addr_t RxPhyAddr
;
399 struct sk_buff
*Rx_skbuff
[NUM_RX_DESC
]; /* Rx data buffers */
400 struct ring_info tx_skb
[NUM_TX_DESC
]; /* Tx data buffers */
402 struct timer_list timer
;
405 int phy_auto_nego_reg
;
406 int phy_1000_ctrl_reg
;
407 #ifdef CONFIG_R8169_VLAN
408 struct vlan_group
*vlgrp
;
410 int (*set_speed
)(struct net_device
*, u8 autoneg
, u16 speed
, u8 duplex
);
411 void (*get_settings
)(struct net_device
*, struct ethtool_cmd
*);
412 void (*phy_reset_enable
)(void __iomem
*);
413 unsigned int (*phy_reset_pending
)(void __iomem
*);
414 unsigned int (*link_ok
)(void __iomem
*);
415 struct work_struct task
;
418 MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@oss.sgi.com>");
419 MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
420 module_param_array(media
, int, &num_media
, 0);
421 module_param(rx_copybreak
, int, 0);
422 module_param(use_dac
, int, 0);
423 MODULE_PARM_DESC(use_dac
, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
424 MODULE_LICENSE("GPL");
425 MODULE_VERSION(RTL8169_VERSION
);
427 static int rtl8169_open(struct net_device
*dev
);
428 static int rtl8169_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
);
429 static irqreturn_t
rtl8169_interrupt(int irq
, void *dev_instance
,
430 struct pt_regs
*regs
);
431 static int rtl8169_init_ring(struct net_device
*dev
);
432 static void rtl8169_hw_start(struct net_device
*dev
);
433 static int rtl8169_close(struct net_device
*dev
);
434 static void rtl8169_set_rx_mode(struct net_device
*dev
);
435 static void rtl8169_tx_timeout(struct net_device
*dev
);
436 static struct net_device_stats
*rtl8169_get_stats(struct net_device
*netdev
);
437 static int rtl8169_rx_interrupt(struct net_device
*, struct rtl8169_private
*,
439 static int rtl8169_change_mtu(struct net_device
*netdev
, int new_mtu
);
440 static void rtl8169_down(struct net_device
*dev
);
442 #ifdef CONFIG_R8169_NAPI
443 static int rtl8169_poll(struct net_device
*dev
, int *budget
);
446 static const u16 rtl8169_intr_mask
=
447 SYSErr
| LinkChg
| RxOverflow
| RxFIFOOver
| TxErr
| TxOK
| RxErr
| RxOK
;
448 static const u16 rtl8169_napi_event
=
449 RxOK
| RxOverflow
| RxFIFOOver
| TxOK
| TxErr
;
450 static const unsigned int rtl8169_rx_config
=
451 (RX_FIFO_THRESH
<< RxCfgFIFOShift
) | (RX_DMA_BURST
<< RxCfgDMAShift
);
453 #define PHY_Cap_10_Half_Or_Less PHY_Cap_10_Half
454 #define PHY_Cap_10_Full_Or_Less PHY_Cap_10_Full | PHY_Cap_10_Half_Or_Less
455 #define PHY_Cap_100_Half_Or_Less PHY_Cap_100_Half | PHY_Cap_10_Full_Or_Less
456 #define PHY_Cap_100_Full_Or_Less PHY_Cap_100_Full | PHY_Cap_100_Half_Or_Less
458 static void mdio_write(void __iomem
*ioaddr
, int RegAddr
, int value
)
462 RTL_W32(PHYAR
, 0x80000000 | (RegAddr
& 0xFF) << 16 | value
);
465 for (i
= 2000; i
> 0; i
--) {
466 /* Check if the RTL8169 has completed writing to the specified MII register */
467 if (!(RTL_R32(PHYAR
) & 0x80000000))
473 static int mdio_read(void __iomem
*ioaddr
, int RegAddr
)
477 RTL_W32(PHYAR
, 0x0 | (RegAddr
& 0xFF) << 16);
480 for (i
= 2000; i
> 0; i
--) {
481 /* Check if the RTL8169 has completed retrieving data from the specified MII register */
482 if (RTL_R32(PHYAR
) & 0x80000000) {
483 value
= (int) (RTL_R32(PHYAR
) & 0xFFFF);
491 static void rtl8169_irq_mask_and_ack(void __iomem
*ioaddr
)
493 RTL_W16(IntrMask
, 0x0000);
495 RTL_W16(IntrStatus
, 0xffff);
498 static void rtl8169_asic_down(void __iomem
*ioaddr
)
500 RTL_W8(ChipCmd
, 0x00);
501 rtl8169_irq_mask_and_ack(ioaddr
);
505 static unsigned int rtl8169_tbi_reset_pending(void __iomem
*ioaddr
)
507 return RTL_R32(TBICSR
) & TBIReset
;
510 static unsigned int rtl8169_xmii_reset_pending(void __iomem
*ioaddr
)
512 return mdio_read(ioaddr
, 0) & 0x8000;
515 static unsigned int rtl8169_tbi_link_ok(void __iomem
*ioaddr
)
517 return RTL_R32(TBICSR
) & TBILinkOk
;
520 static unsigned int rtl8169_xmii_link_ok(void __iomem
*ioaddr
)
522 return RTL_R8(PHYstatus
) & LinkStatus
;
525 static void rtl8169_tbi_reset_enable(void __iomem
*ioaddr
)
527 RTL_W32(TBICSR
, RTL_R32(TBICSR
) | TBIReset
);
530 static void rtl8169_xmii_reset_enable(void __iomem
*ioaddr
)
534 val
= (mdio_read(ioaddr
, PHY_CTRL_REG
) | 0x8000) & 0xffff;
535 mdio_write(ioaddr
, PHY_CTRL_REG
, val
);
538 static void rtl8169_check_link_status(struct net_device
*dev
,
539 struct rtl8169_private
*tp
, void __iomem
*ioaddr
)
543 spin_lock_irqsave(&tp
->lock
, flags
);
544 if (tp
->link_ok(ioaddr
)) {
545 netif_carrier_on(dev
);
546 printk(KERN_INFO PFX
"%s: link up\n", dev
->name
);
548 netif_carrier_off(dev
);
549 spin_unlock_irqrestore(&tp
->lock
, flags
);
552 static void rtl8169_link_option(int idx
, u8
*autoneg
, u16
*speed
, u8
*duplex
)
559 } link_settings
[] = {
560 { SPEED_10
, DUPLEX_HALF
, AUTONEG_DISABLE
, _10_Half
},
561 { SPEED_10
, DUPLEX_FULL
, AUTONEG_DISABLE
, _10_Full
},
562 { SPEED_100
, DUPLEX_HALF
, AUTONEG_DISABLE
, _100_Half
},
563 { SPEED_100
, DUPLEX_FULL
, AUTONEG_DISABLE
, _100_Full
},
564 { SPEED_1000
, DUPLEX_FULL
, AUTONEG_DISABLE
, _1000_Full
},
566 { SPEED_1000
, DUPLEX_FULL
, AUTONEG_ENABLE
, 0xff }
568 unsigned char option
;
570 option
= ((idx
< MAX_UNITS
) && (idx
>= 0)) ? media
[idx
] : 0xff;
572 if ((option
!= 0xff) && !idx
)
573 printk(KERN_WARNING PFX
"media option is deprecated.\n");
575 for (p
= link_settings
; p
->media
!= 0xff; p
++) {
576 if (p
->media
== option
)
579 *autoneg
= p
->autoneg
;
584 static void rtl8169_get_drvinfo(struct net_device
*dev
,
585 struct ethtool_drvinfo
*info
)
587 struct rtl8169_private
*tp
= netdev_priv(dev
);
589 strcpy(info
->driver
, MODULENAME
);
590 strcpy(info
->version
, RTL8169_VERSION
);
591 strcpy(info
->bus_info
, pci_name(tp
->pci_dev
));
594 static int rtl8169_get_regs_len(struct net_device
*dev
)
596 return R8169_REGS_SIZE
;
599 static int rtl8169_set_speed_tbi(struct net_device
*dev
,
600 u8 autoneg
, u16 speed
, u8 duplex
)
602 struct rtl8169_private
*tp
= netdev_priv(dev
);
603 void __iomem
*ioaddr
= tp
->mmio_addr
;
607 reg
= RTL_R32(TBICSR
);
608 if ((autoneg
== AUTONEG_DISABLE
) && (speed
== SPEED_1000
) &&
609 (duplex
== DUPLEX_FULL
)) {
610 RTL_W32(TBICSR
, reg
& ~(TBINwEnable
| TBINwRestart
));
611 } else if (autoneg
== AUTONEG_ENABLE
)
612 RTL_W32(TBICSR
, reg
| TBINwEnable
| TBINwRestart
);
614 printk(KERN_WARNING PFX
615 "%s: incorrect speed setting refused in TBI mode\n",
623 static int rtl8169_set_speed_xmii(struct net_device
*dev
,
624 u8 autoneg
, u16 speed
, u8 duplex
)
626 struct rtl8169_private
*tp
= netdev_priv(dev
);
627 void __iomem
*ioaddr
= tp
->mmio_addr
;
628 int auto_nego
, giga_ctrl
;
630 auto_nego
= mdio_read(ioaddr
, PHY_AUTO_NEGO_REG
);
631 auto_nego
&= ~(PHY_Cap_10_Half
| PHY_Cap_10_Full
|
632 PHY_Cap_100_Half
| PHY_Cap_100_Full
);
633 giga_ctrl
= mdio_read(ioaddr
, PHY_1000_CTRL_REG
);
634 giga_ctrl
&= ~(PHY_Cap_1000_Full
| PHY_Cap_Null
);
636 if (autoneg
== AUTONEG_ENABLE
) {
637 auto_nego
|= (PHY_Cap_10_Half
| PHY_Cap_10_Full
|
638 PHY_Cap_100_Half
| PHY_Cap_100_Full
);
639 giga_ctrl
|= PHY_Cap_1000_Full
;
641 if (speed
== SPEED_10
)
642 auto_nego
|= PHY_Cap_10_Half
| PHY_Cap_10_Full
;
643 else if (speed
== SPEED_100
)
644 auto_nego
|= PHY_Cap_100_Half
| PHY_Cap_100_Full
;
645 else if (speed
== SPEED_1000
)
646 giga_ctrl
|= PHY_Cap_1000_Full
;
648 if (duplex
== DUPLEX_HALF
)
649 auto_nego
&= ~(PHY_Cap_10_Full
| PHY_Cap_100_Full
);
652 tp
->phy_auto_nego_reg
= auto_nego
;
653 tp
->phy_1000_ctrl_reg
= giga_ctrl
;
655 mdio_write(ioaddr
, PHY_AUTO_NEGO_REG
, auto_nego
);
656 mdio_write(ioaddr
, PHY_1000_CTRL_REG
, giga_ctrl
);
657 mdio_write(ioaddr
, PHY_CTRL_REG
, PHY_Enable_Auto_Nego
|
658 PHY_Restart_Auto_Nego
);
662 static int rtl8169_set_speed(struct net_device
*dev
,
663 u8 autoneg
, u16 speed
, u8 duplex
)
665 struct rtl8169_private
*tp
= netdev_priv(dev
);
668 ret
= tp
->set_speed(dev
, autoneg
, speed
, duplex
);
670 if (netif_running(dev
) && (tp
->phy_1000_ctrl_reg
& PHY_Cap_1000_Full
))
671 mod_timer(&tp
->timer
, jiffies
+ RTL8169_PHY_TIMEOUT
);
676 static int rtl8169_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
678 struct rtl8169_private
*tp
= netdev_priv(dev
);
682 spin_lock_irqsave(&tp
->lock
, flags
);
683 ret
= rtl8169_set_speed(dev
, cmd
->autoneg
, cmd
->speed
, cmd
->duplex
);
684 spin_unlock_irqrestore(&tp
->lock
, flags
);
689 static u32
rtl8169_get_rx_csum(struct net_device
*dev
)
691 struct rtl8169_private
*tp
= netdev_priv(dev
);
693 return tp
->cp_cmd
& RxChkSum
;
696 static int rtl8169_set_rx_csum(struct net_device
*dev
, u32 data
)
698 struct rtl8169_private
*tp
= netdev_priv(dev
);
699 void __iomem
*ioaddr
= tp
->mmio_addr
;
702 spin_lock_irqsave(&tp
->lock
, flags
);
705 tp
->cp_cmd
|= RxChkSum
;
707 tp
->cp_cmd
&= ~RxChkSum
;
709 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
712 spin_unlock_irqrestore(&tp
->lock
, flags
);
717 #ifdef CONFIG_R8169_VLAN
719 static inline u32
rtl8169_tx_vlan_tag(struct rtl8169_private
*tp
,
722 return (tp
->vlgrp
&& vlan_tx_tag_present(skb
)) ?
723 TxVlanTag
| swab16(vlan_tx_tag_get(skb
)) : 0x00;
726 static void rtl8169_vlan_rx_register(struct net_device
*dev
,
727 struct vlan_group
*grp
)
729 struct rtl8169_private
*tp
= netdev_priv(dev
);
730 void __iomem
*ioaddr
= tp
->mmio_addr
;
733 spin_lock_irqsave(&tp
->lock
, flags
);
736 tp
->cp_cmd
|= RxVlan
;
738 tp
->cp_cmd
&= ~RxVlan
;
739 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
741 spin_unlock_irqrestore(&tp
->lock
, flags
);
744 static void rtl8169_vlan_rx_kill_vid(struct net_device
*dev
, unsigned short vid
)
746 struct rtl8169_private
*tp
= netdev_priv(dev
);
749 spin_lock_irqsave(&tp
->lock
, flags
);
751 tp
->vlgrp
->vlan_devices
[vid
] = NULL
;
752 spin_unlock_irqrestore(&tp
->lock
, flags
);
755 static int rtl8169_rx_vlan_skb(struct rtl8169_private
*tp
, struct RxDesc
*desc
,
758 u32 opts2
= le32_to_cpu(desc
->opts2
);
761 if (tp
->vlgrp
&& (opts2
& RxVlanTag
)) {
762 rtl8169_rx_hwaccel_skb(skb
, tp
->vlgrp
,
763 swab16(opts2
& 0xffff));
771 #else /* !CONFIG_R8169_VLAN */
773 static inline u32
rtl8169_tx_vlan_tag(struct rtl8169_private
*tp
,
779 static int rtl8169_rx_vlan_skb(struct rtl8169_private
*tp
, struct RxDesc
*desc
,
787 static void rtl8169_gset_tbi(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
789 struct rtl8169_private
*tp
= netdev_priv(dev
);
790 void __iomem
*ioaddr
= tp
->mmio_addr
;
794 SUPPORTED_1000baseT_Full
| SUPPORTED_Autoneg
| SUPPORTED_FIBRE
;
795 cmd
->port
= PORT_FIBRE
;
796 cmd
->transceiver
= XCVR_INTERNAL
;
798 status
= RTL_R32(TBICSR
);
799 cmd
->advertising
= (status
& TBINwEnable
) ? ADVERTISED_Autoneg
: 0;
800 cmd
->autoneg
= !!(status
& TBINwEnable
);
802 cmd
->speed
= SPEED_1000
;
803 cmd
->duplex
= DUPLEX_FULL
; /* Always set */
806 static void rtl8169_gset_xmii(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
808 struct rtl8169_private
*tp
= netdev_priv(dev
);
809 void __iomem
*ioaddr
= tp
->mmio_addr
;
812 cmd
->supported
= SUPPORTED_10baseT_Half
|
813 SUPPORTED_10baseT_Full
|
814 SUPPORTED_100baseT_Half
|
815 SUPPORTED_100baseT_Full
|
816 SUPPORTED_1000baseT_Full
|
821 cmd
->advertising
= ADVERTISED_TP
| ADVERTISED_Autoneg
;
823 if (tp
->phy_auto_nego_reg
& PHY_Cap_10_Half
)
824 cmd
->advertising
|= ADVERTISED_10baseT_Half
;
825 if (tp
->phy_auto_nego_reg
& PHY_Cap_10_Full
)
826 cmd
->advertising
|= ADVERTISED_10baseT_Full
;
827 if (tp
->phy_auto_nego_reg
& PHY_Cap_100_Half
)
828 cmd
->advertising
|= ADVERTISED_100baseT_Half
;
829 if (tp
->phy_auto_nego_reg
& PHY_Cap_100_Full
)
830 cmd
->advertising
|= ADVERTISED_100baseT_Full
;
831 if (tp
->phy_1000_ctrl_reg
& PHY_Cap_1000_Full
)
832 cmd
->advertising
|= ADVERTISED_1000baseT_Full
;
834 status
= RTL_R8(PHYstatus
);
836 if (status
& _1000bpsF
)
837 cmd
->speed
= SPEED_1000
;
838 else if (status
& _100bps
)
839 cmd
->speed
= SPEED_100
;
840 else if (status
& _10bps
)
841 cmd
->speed
= SPEED_10
;
843 cmd
->duplex
= ((status
& _1000bpsF
) || (status
& FullDup
)) ?
844 DUPLEX_FULL
: DUPLEX_HALF
;
847 static int rtl8169_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
849 struct rtl8169_private
*tp
= netdev_priv(dev
);
852 spin_lock_irqsave(&tp
->lock
, flags
);
854 tp
->get_settings(dev
, cmd
);
856 spin_unlock_irqrestore(&tp
->lock
, flags
);
860 static void rtl8169_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
863 struct rtl8169_private
*tp
= netdev_priv(dev
);
866 if (regs
->len
> R8169_REGS_SIZE
)
867 regs
->len
= R8169_REGS_SIZE
;
869 spin_lock_irqsave(&tp
->lock
, flags
);
870 memcpy_fromio(p
, tp
->mmio_addr
, regs
->len
);
871 spin_unlock_irqrestore(&tp
->lock
, flags
);
874 static struct ethtool_ops rtl8169_ethtool_ops
= {
875 .get_drvinfo
= rtl8169_get_drvinfo
,
876 .get_regs_len
= rtl8169_get_regs_len
,
877 .get_link
= ethtool_op_get_link
,
878 .get_settings
= rtl8169_get_settings
,
879 .set_settings
= rtl8169_set_settings
,
880 .get_rx_csum
= rtl8169_get_rx_csum
,
881 .set_rx_csum
= rtl8169_set_rx_csum
,
882 .get_tx_csum
= ethtool_op_get_tx_csum
,
883 .set_tx_csum
= ethtool_op_set_tx_csum
,
884 .get_sg
= ethtool_op_get_sg
,
885 .set_sg
= ethtool_op_set_sg
,
886 .get_tso
= ethtool_op_get_tso
,
887 .set_tso
= ethtool_op_set_tso
,
888 .get_regs
= rtl8169_get_regs
,
891 static void rtl8169_write_gmii_reg_bit(void __iomem
*ioaddr
, int reg
, int bitnum
,
896 val
= mdio_read(ioaddr
, reg
);
897 val
= (bitval
== 1) ?
898 val
| (bitval
<< bitnum
) : val
& ~(0x0001 << bitnum
);
899 mdio_write(ioaddr
, reg
, val
& 0xffff);
902 static void rtl8169_get_mac_version(struct rtl8169_private
*tp
, void __iomem
*ioaddr
)
908 { 0x1 << 28, RTL_GIGA_MAC_VER_X
},
909 { 0x1 << 26, RTL_GIGA_MAC_VER_E
},
910 { 0x1 << 23, RTL_GIGA_MAC_VER_D
},
911 { 0x00000000, RTL_GIGA_MAC_VER_B
} /* Catch-all */
915 reg
= RTL_R32(TxConfig
) & 0x7c800000;
916 while ((reg
& p
->mask
) != p
->mask
)
918 tp
->mac_version
= p
->mac_version
;
921 static void rtl8169_print_mac_version(struct rtl8169_private
*tp
)
927 { RTL_GIGA_MAC_VER_E
, "RTL_GIGA_MAC_VER_E" },
928 { RTL_GIGA_MAC_VER_D
, "RTL_GIGA_MAC_VER_D" },
929 { RTL_GIGA_MAC_VER_B
, "RTL_GIGA_MAC_VER_B" },
933 for (p
= mac_print
; p
->msg
; p
++) {
934 if (tp
->mac_version
== p
->version
) {
935 dprintk("mac_version == %s (%04d)\n", p
->msg
,
940 dprintk("mac_version == Unknown\n");
943 static void rtl8169_get_phy_version(struct rtl8169_private
*tp
, void __iomem
*ioaddr
)
950 { 0x000f, 0x0002, RTL_GIGA_PHY_VER_G
},
951 { 0x000f, 0x0001, RTL_GIGA_PHY_VER_F
},
952 { 0x000f, 0x0000, RTL_GIGA_PHY_VER_E
},
953 { 0x0000, 0x0000, RTL_GIGA_PHY_VER_D
} /* Catch-all */
957 reg
= mdio_read(ioaddr
, 3) & 0xffff;
958 while ((reg
& p
->mask
) != p
->set
)
960 tp
->phy_version
= p
->phy_version
;
963 static void rtl8169_print_phy_version(struct rtl8169_private
*tp
)
970 { RTL_GIGA_PHY_VER_G
, "RTL_GIGA_PHY_VER_G", 0x0002 },
971 { RTL_GIGA_PHY_VER_F
, "RTL_GIGA_PHY_VER_F", 0x0001 },
972 { RTL_GIGA_PHY_VER_E
, "RTL_GIGA_PHY_VER_E", 0x0000 },
973 { RTL_GIGA_PHY_VER_D
, "RTL_GIGA_PHY_VER_D", 0x0000 },
977 for (p
= phy_print
; p
->msg
; p
++) {
978 if (tp
->phy_version
== p
->version
) {
979 dprintk("phy_version == %s (%04x)\n", p
->msg
, p
->reg
);
983 dprintk("phy_version == Unknown\n");
986 static void rtl8169_hw_phy_config(struct net_device
*dev
)
988 struct rtl8169_private
*tp
= netdev_priv(dev
);
989 void __iomem
*ioaddr
= tp
->mmio_addr
;
991 u16 regs
[5]; /* Beware of bit-sign propagation */
993 { 0x0000, //w 4 15 12 0
994 0x00a1, //w 3 15 0 00a1
995 0x0008, //w 2 15 0 0008
996 0x1020, //w 1 15 0 1020
997 0x1000 } },{ //w 0 15 0 1000
998 { 0x7000, //w 4 15 12 7
999 0xff41, //w 3 15 0 ff41
1000 0xde60, //w 2 15 0 de60
1001 0x0140, //w 1 15 0 0140
1002 0x0077 } },{ //w 0 15 0 0077
1003 { 0xa000, //w 4 15 12 a
1004 0xdf01, //w 3 15 0 df01
1005 0xdf20, //w 2 15 0 df20
1006 0xff95, //w 1 15 0 ff95
1007 0xfa00 } },{ //w 0 15 0 fa00
1008 { 0xb000, //w 4 15 12 b
1009 0xff41, //w 3 15 0 ff41
1010 0xde20, //w 2 15 0 de20
1011 0x0140, //w 1 15 0 0140
1012 0x00bb } },{ //w 0 15 0 00bb
1013 { 0xf000, //w 4 15 12 f
1014 0xdf01, //w 3 15 0 df01
1015 0xdf20, //w 2 15 0 df20
1016 0xff95, //w 1 15 0 ff95
1017 0xbf00 } //w 0 15 0 bf00
1022 rtl8169_print_mac_version(tp
);
1023 rtl8169_print_phy_version(tp
);
1025 if (tp
->mac_version
<= RTL_GIGA_MAC_VER_B
)
1027 if (tp
->phy_version
>= RTL_GIGA_PHY_VER_H
)
1030 dprintk("MAC version != 0 && PHY version == 0 or 1\n");
1031 dprintk("Do final_reg2.cfg\n");
1035 if (tp
->mac_version
== RTL_GIGA_MAC_VER_X
) {
1036 mdio_write(ioaddr
, 31, 0x0001);
1037 mdio_write(ioaddr
, 9, 0x273a);
1038 mdio_write(ioaddr
, 14, 0x7bfb);
1039 mdio_write(ioaddr
, 27, 0x841e);
1041 mdio_write(ioaddr
, 31, 0x0002);
1042 mdio_write(ioaddr
, 1, 0x90d0);
1043 mdio_write(ioaddr
, 31, 0x0000);
1047 /* phy config for RTL8169s mac_version C chip */
1048 mdio_write(ioaddr
, 31, 0x0001); //w 31 2 0 1
1049 mdio_write(ioaddr
, 21, 0x1000); //w 21 15 0 1000
1050 mdio_write(ioaddr
, 24, 0x65c7); //w 24 15 0 65c7
1051 rtl8169_write_gmii_reg_bit(ioaddr
, 4, 11, 0); //w 4 11 11 0
1053 for (i
= 0; i
< ARRAY_SIZE(phy_magic
); i
++, p
++) {
1056 val
= (mdio_read(ioaddr
, pos
) & 0x0fff) | (p
->regs
[0] & 0xffff);
1057 mdio_write(ioaddr
, pos
, val
);
1059 mdio_write(ioaddr
, pos
, p
->regs
[4 - pos
] & 0xffff);
1060 rtl8169_write_gmii_reg_bit(ioaddr
, 4, 11, 1); //w 4 11 11 1
1061 rtl8169_write_gmii_reg_bit(ioaddr
, 4, 11, 0); //w 4 11 11 0
1063 mdio_write(ioaddr
, 31, 0x0000); //w 31 2 0 0
1066 static void rtl8169_phy_timer(unsigned long __opaque
)
1068 struct net_device
*dev
= (struct net_device
*)__opaque
;
1069 struct rtl8169_private
*tp
= netdev_priv(dev
);
1070 struct timer_list
*timer
= &tp
->timer
;
1071 void __iomem
*ioaddr
= tp
->mmio_addr
;
1072 unsigned long timeout
= RTL8169_PHY_TIMEOUT
;
1074 assert(tp
->mac_version
> RTL_GIGA_MAC_VER_B
);
1075 assert(tp
->phy_version
< RTL_GIGA_PHY_VER_H
);
1077 if (!(tp
->phy_1000_ctrl_reg
& PHY_Cap_1000_Full
))
1080 spin_lock_irq(&tp
->lock
);
1082 if (tp
->phy_reset_pending(ioaddr
)) {
1084 * A busy loop could burn quite a few cycles on nowadays CPU.
1085 * Let's delay the execution of the timer for a few ticks.
1091 if (tp
->link_ok(ioaddr
))
1094 printk(KERN_WARNING PFX
"%s: PHY reset until link up\n", dev
->name
);
1096 tp
->phy_reset_enable(ioaddr
);
1099 mod_timer(timer
, jiffies
+ timeout
);
1101 spin_unlock_irq(&tp
->lock
);
1104 static inline void rtl8169_delete_timer(struct net_device
*dev
)
1106 struct rtl8169_private
*tp
= netdev_priv(dev
);
1107 struct timer_list
*timer
= &tp
->timer
;
1109 if ((tp
->mac_version
<= RTL_GIGA_MAC_VER_B
) ||
1110 (tp
->phy_version
>= RTL_GIGA_PHY_VER_H
))
1113 del_timer_sync(timer
);
1116 static inline void rtl8169_request_timer(struct net_device
*dev
)
1118 struct rtl8169_private
*tp
= netdev_priv(dev
);
1119 struct timer_list
*timer
= &tp
->timer
;
1121 if ((tp
->mac_version
<= RTL_GIGA_MAC_VER_B
) ||
1122 (tp
->phy_version
>= RTL_GIGA_PHY_VER_H
))
1126 timer
->expires
= jiffies
+ RTL8169_PHY_TIMEOUT
;
1127 timer
->data
= (unsigned long)(dev
);
1128 timer
->function
= rtl8169_phy_timer
;
1132 #ifdef CONFIG_NET_POLL_CONTROLLER
1134 * Polling 'interrupt' - used by things like netconsole to send skbs
1135 * without having to re-enable interrupts. It's not called while
1136 * the interrupt routine is executing.
1138 static void rtl8169_netpoll(struct net_device
*dev
)
1140 struct rtl8169_private
*tp
= netdev_priv(dev
);
1141 struct pci_dev
*pdev
= tp
->pci_dev
;
1143 disable_irq(pdev
->irq
);
1144 rtl8169_interrupt(pdev
->irq
, dev
, NULL
);
1145 enable_irq(pdev
->irq
);
1149 static void rtl8169_release_board(struct pci_dev
*pdev
, struct net_device
*dev
,
1150 void __iomem
*ioaddr
)
1153 pci_release_regions(pdev
);
1154 pci_disable_device(pdev
);
1158 static int __devinit
1159 rtl8169_init_board(struct pci_dev
*pdev
, struct net_device
**dev_out
,
1160 void __iomem
**ioaddr_out
)
1162 void __iomem
*ioaddr
;
1163 struct net_device
*dev
;
1164 struct rtl8169_private
*tp
;
1165 int rc
= -ENOMEM
, i
, acpi_idle_state
= 0, pm_cap
;
1167 assert(ioaddr_out
!= NULL
);
1169 /* dev zeroed in alloc_etherdev */
1170 dev
= alloc_etherdev(sizeof (*tp
));
1172 printk(KERN_ERR PFX
"unable to alloc new ethernet\n");
1176 SET_MODULE_OWNER(dev
);
1177 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1178 tp
= netdev_priv(dev
);
1180 /* enable device (incl. PCI PM wakeup and hotplug setup) */
1181 rc
= pci_enable_device(pdev
);
1183 printk(KERN_ERR PFX
"%s: enable failure\n", pci_name(pdev
));
1184 goto err_out_free_dev
;
1187 rc
= pci_set_mwi(pdev
);
1189 goto err_out_disable
;
1191 /* save power state before pci_enable_device overwrites it */
1192 pm_cap
= pci_find_capability(pdev
, PCI_CAP_ID_PM
);
1196 pci_read_config_word(pdev
, pm_cap
+ PCI_PM_CTRL
, &pwr_command
);
1197 acpi_idle_state
= pwr_command
& PCI_PM_CTRL_STATE_MASK
;
1200 "Cannot find PowerManagement capability, aborting.\n");
1204 /* make sure PCI base addr 1 is MMIO */
1205 if (!(pci_resource_flags(pdev
, 1) & IORESOURCE_MEM
)) {
1207 "region #1 not an MMIO resource, aborting\n");
1211 /* check for weird/broken PCI region reporting */
1212 if (pci_resource_len(pdev
, 1) < R8169_REGS_SIZE
) {
1213 printk(KERN_ERR PFX
"Invalid PCI region size(s), aborting\n");
1218 rc
= pci_request_regions(pdev
, MODULENAME
);
1220 printk(KERN_ERR PFX
"%s: could not request regions.\n",
1225 tp
->cp_cmd
= PCIMulRW
| RxChkSum
;
1227 if ((sizeof(dma_addr_t
) > 4) &&
1228 !pci_set_dma_mask(pdev
, DMA_64BIT_MASK
) && use_dac
) {
1229 tp
->cp_cmd
|= PCIDAC
;
1230 dev
->features
|= NETIF_F_HIGHDMA
;
1232 rc
= pci_set_dma_mask(pdev
, DMA_32BIT_MASK
);
1234 printk(KERN_ERR PFX
"DMA configuration failed.\n");
1235 goto err_out_free_res
;
1239 pci_set_master(pdev
);
1241 /* ioremap MMIO region */
1242 ioaddr
= ioremap(pci_resource_start(pdev
, 1), R8169_REGS_SIZE
);
1243 if (ioaddr
== NULL
) {
1244 printk(KERN_ERR PFX
"cannot remap MMIO, aborting\n");
1246 goto err_out_free_res
;
1249 /* Unneeded ? Don't mess with Mrs. Murphy. */
1250 rtl8169_irq_mask_and_ack(ioaddr
);
1252 /* Soft reset the chip. */
1253 RTL_W8(ChipCmd
, CmdReset
);
1255 /* Check that the chip has finished the reset. */
1256 for (i
= 1000; i
> 0; i
--) {
1257 if ((RTL_R8(ChipCmd
) & CmdReset
) == 0)
1262 /* Identify chip attached to board */
1263 rtl8169_get_mac_version(tp
, ioaddr
);
1264 rtl8169_get_phy_version(tp
, ioaddr
);
1266 rtl8169_print_mac_version(tp
);
1267 rtl8169_print_phy_version(tp
);
1269 for (i
= ARRAY_SIZE(rtl_chip_info
) - 1; i
>= 0; i
--) {
1270 if (tp
->mac_version
== rtl_chip_info
[i
].mac_version
)
1274 /* Unknown chip: assume array element #0, original RTL-8169 */
1275 printk(KERN_DEBUG PFX
1276 "PCI device %s: unknown chip version, assuming %s\n",
1277 pci_name(pdev
), rtl_chip_info
[0].name
);
1282 *ioaddr_out
= ioaddr
;
1288 pci_release_regions(pdev
);
1291 pci_clear_mwi(pdev
);
1294 pci_disable_device(pdev
);
1304 static int __devinit
1305 rtl8169_init_one(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
1307 struct net_device
*dev
= NULL
;
1308 struct rtl8169_private
*tp
;
1309 void __iomem
*ioaddr
= NULL
;
1310 static int board_idx
= -1;
1311 static int printed_version
= 0;
1316 assert(pdev
!= NULL
);
1317 assert(ent
!= NULL
);
1321 if (!printed_version
) {
1322 printk(KERN_INFO
"%s Gigabit Ethernet driver %s loaded\n",
1323 MODULENAME
, RTL8169_VERSION
);
1324 printed_version
= 1;
1327 rc
= rtl8169_init_board(pdev
, &dev
, &ioaddr
);
1331 tp
= netdev_priv(dev
);
1332 assert(ioaddr
!= NULL
);
1334 if (RTL_R8(PHYstatus
) & TBI_Enable
) {
1335 tp
->set_speed
= rtl8169_set_speed_tbi
;
1336 tp
->get_settings
= rtl8169_gset_tbi
;
1337 tp
->phy_reset_enable
= rtl8169_tbi_reset_enable
;
1338 tp
->phy_reset_pending
= rtl8169_tbi_reset_pending
;
1339 tp
->link_ok
= rtl8169_tbi_link_ok
;
1341 tp
->phy_1000_ctrl_reg
= PHY_Cap_1000_Full
; /* Implied by TBI */
1343 tp
->set_speed
= rtl8169_set_speed_xmii
;
1344 tp
->get_settings
= rtl8169_gset_xmii
;
1345 tp
->phy_reset_enable
= rtl8169_xmii_reset_enable
;
1346 tp
->phy_reset_pending
= rtl8169_xmii_reset_pending
;
1347 tp
->link_ok
= rtl8169_xmii_link_ok
;
1350 /* Get MAC address. FIXME: read EEPROM */
1351 for (i
= 0; i
< MAC_ADDR_LEN
; i
++)
1352 dev
->dev_addr
[i
] = RTL_R8(MAC0
+ i
);
1354 dev
->open
= rtl8169_open
;
1355 dev
->hard_start_xmit
= rtl8169_start_xmit
;
1356 dev
->get_stats
= rtl8169_get_stats
;
1357 SET_ETHTOOL_OPS(dev
, &rtl8169_ethtool_ops
);
1358 dev
->stop
= rtl8169_close
;
1359 dev
->tx_timeout
= rtl8169_tx_timeout
;
1360 dev
->set_multicast_list
= rtl8169_set_rx_mode
;
1361 dev
->watchdog_timeo
= RTL8169_TX_TIMEOUT
;
1362 dev
->irq
= pdev
->irq
;
1363 dev
->base_addr
= (unsigned long) ioaddr
;
1364 dev
->change_mtu
= rtl8169_change_mtu
;
1366 #ifdef CONFIG_R8169_NAPI
1367 dev
->poll
= rtl8169_poll
;
1368 dev
->weight
= R8169_NAPI_WEIGHT
;
1369 printk(KERN_INFO PFX
"NAPI enabled\n");
1372 #ifdef CONFIG_R8169_VLAN
1373 dev
->features
|= NETIF_F_HW_VLAN_TX
| NETIF_F_HW_VLAN_RX
;
1374 dev
->vlan_rx_register
= rtl8169_vlan_rx_register
;
1375 dev
->vlan_rx_kill_vid
= rtl8169_vlan_rx_kill_vid
;
1378 #ifdef CONFIG_NET_POLL_CONTROLLER
1379 dev
->poll_controller
= rtl8169_netpoll
;
1382 tp
->intr_mask
= 0xffff;
1384 tp
->mmio_addr
= ioaddr
;
1386 spin_lock_init(&tp
->lock
);
1388 rc
= register_netdev(dev
);
1390 rtl8169_release_board(pdev
, dev
, ioaddr
);
1394 printk(KERN_DEBUG
"%s: Identified chip type is '%s'.\n", dev
->name
,
1395 rtl_chip_info
[tp
->chipset
].name
);
1397 pci_set_drvdata(pdev
, dev
);
1399 printk(KERN_INFO
"%s: %s at 0x%lx, "
1400 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
1403 rtl_chip_info
[ent
->driver_data
].name
,
1405 dev
->dev_addr
[0], dev
->dev_addr
[1],
1406 dev
->dev_addr
[2], dev
->dev_addr
[3],
1407 dev
->dev_addr
[4], dev
->dev_addr
[5], dev
->irq
);
1409 rtl8169_hw_phy_config(dev
);
1411 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
1414 if (tp
->mac_version
< RTL_GIGA_MAC_VER_E
) {
1415 dprintk("Set PCI Latency=0x40\n");
1416 pci_write_config_byte(pdev
, PCI_LATENCY_TIMER
, 0x40);
1419 if (tp
->mac_version
== RTL_GIGA_MAC_VER_D
) {
1420 dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
1422 dprintk("Set PHY Reg 0x0bh = 0x00h\n");
1423 mdio_write(ioaddr
, 0x0b, 0x0000); //w 0x0b 15 0 0
1426 rtl8169_link_option(board_idx
, &autoneg
, &speed
, &duplex
);
1428 rtl8169_set_speed(dev
, autoneg
, speed
, duplex
);
1430 if (RTL_R8(PHYstatus
) & TBI_Enable
)
1431 printk(KERN_INFO PFX
"%s: TBI auto-negotiating\n", dev
->name
);
1436 static void __devexit
1437 rtl8169_remove_one(struct pci_dev
*pdev
)
1439 struct net_device
*dev
= pci_get_drvdata(pdev
);
1440 struct rtl8169_private
*tp
= netdev_priv(dev
);
1442 assert(dev
!= NULL
);
1445 unregister_netdev(dev
);
1446 rtl8169_release_board(pdev
, dev
, tp
->mmio_addr
);
1447 pci_set_drvdata(pdev
, NULL
);
1452 static int rtl8169_suspend(struct pci_dev
*pdev
, u32 state
)
1454 struct net_device
*dev
= pci_get_drvdata(pdev
);
1455 struct rtl8169_private
*tp
= netdev_priv(dev
);
1456 void __iomem
*ioaddr
= tp
->mmio_addr
;
1457 unsigned long flags
;
1459 if (!netif_running(dev
))
1462 netif_device_detach(dev
);
1463 netif_stop_queue(dev
);
1464 spin_lock_irqsave(&tp
->lock
, flags
);
1466 /* Disable interrupts, stop Rx and Tx */
1467 RTL_W16(IntrMask
, 0);
1470 /* Update the error counts. */
1471 tp
->stats
.rx_missed_errors
+= RTL_R32(RxMissed
);
1472 RTL_W32(RxMissed
, 0);
1473 spin_unlock_irqrestore(&tp
->lock
, flags
);
1478 static int rtl8169_resume(struct pci_dev
*pdev
)
1480 struct net_device
*dev
= pci_get_drvdata(pdev
);
1482 if (!netif_running(dev
))
1485 netif_device_attach(dev
);
1486 rtl8169_hw_start(dev
);
1491 #endif /* CONFIG_PM */
1493 static void rtl8169_set_rxbufsize(struct rtl8169_private
*tp
,
1494 struct net_device
*dev
)
1496 unsigned int mtu
= dev
->mtu
;
1498 tp
->rx_buf_sz
= (mtu
> RX_BUF_SIZE
) ? mtu
+ ETH_HLEN
+ 8 : RX_BUF_SIZE
;
1501 static int rtl8169_open(struct net_device
*dev
)
1503 struct rtl8169_private
*tp
= netdev_priv(dev
);
1504 struct pci_dev
*pdev
= tp
->pci_dev
;
1507 rtl8169_set_rxbufsize(tp
, dev
);
1510 request_irq(dev
->irq
, rtl8169_interrupt
, SA_SHIRQ
, dev
->name
, dev
);
1517 * Rx and Tx desscriptors needs 256 bytes alignment.
1518 * pci_alloc_consistent provides more.
1520 tp
->TxDescArray
= pci_alloc_consistent(pdev
, R8169_TX_RING_BYTES
,
1522 if (!tp
->TxDescArray
)
1525 tp
->RxDescArray
= pci_alloc_consistent(pdev
, R8169_RX_RING_BYTES
,
1527 if (!tp
->RxDescArray
)
1530 retval
= rtl8169_init_ring(dev
);
1534 INIT_WORK(&tp
->task
, NULL
, dev
);
1536 rtl8169_hw_start(dev
);
1538 rtl8169_request_timer(dev
);
1540 rtl8169_check_link_status(dev
, tp
, tp
->mmio_addr
);
1545 pci_free_consistent(pdev
, R8169_RX_RING_BYTES
, tp
->RxDescArray
,
1548 pci_free_consistent(pdev
, R8169_TX_RING_BYTES
, tp
->TxDescArray
,
1551 free_irq(dev
->irq
, dev
);
1555 static void rtl8169_hw_reset(void __iomem
*ioaddr
)
1557 /* Disable interrupts */
1558 rtl8169_irq_mask_and_ack(ioaddr
);
1560 /* Reset the chipset */
1561 RTL_W8(ChipCmd
, CmdReset
);
1568 rtl8169_hw_start(struct net_device
*dev
)
1570 struct rtl8169_private
*tp
= netdev_priv(dev
);
1571 void __iomem
*ioaddr
= tp
->mmio_addr
;
1574 /* Soft reset the chip. */
1575 RTL_W8(ChipCmd
, CmdReset
);
1577 /* Check that the chip has finished the reset. */
1578 for (i
= 1000; i
> 0; i
--) {
1579 if ((RTL_R8(ChipCmd
) & CmdReset
) == 0)
1584 RTL_W8(Cfg9346
, Cfg9346_Unlock
);
1585 RTL_W8(ChipCmd
, CmdTxEnb
| CmdRxEnb
);
1586 RTL_W8(EarlyTxThres
, EarlyTxThld
);
1588 /* For gigabit rtl8169, MTU + header + CRC + VLAN */
1589 RTL_W16(RxMaxSize
, tp
->rx_buf_sz
);
1591 /* Set Rx Config register */
1592 i
= rtl8169_rx_config
|
1593 (RTL_R32(RxConfig
) & rtl_chip_info
[tp
->chipset
].RxConfigMask
);
1594 RTL_W32(RxConfig
, i
);
1596 /* Set DMA burst size and Interframe Gap Time */
1598 (TX_DMA_BURST
<< TxDMAShift
) | (InterFrameGap
<<
1599 TxInterFrameGapShift
));
1600 tp
->cp_cmd
|= RTL_R16(CPlusCmd
);
1601 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
1603 if ((tp
->mac_version
== RTL_GIGA_MAC_VER_D
) ||
1604 (tp
->mac_version
== RTL_GIGA_MAC_VER_E
)) {
1605 dprintk(KERN_INFO PFX
"Set MAC Reg C+CR Offset 0xE0. "
1606 "Bit-3 and bit-14 MUST be 1\n");
1607 tp
->cp_cmd
|= (1 << 14) | PCIMulRW
;
1608 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
1612 * Undocumented corner. Supposedly:
1613 * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
1615 RTL_W16(IntrMitigate
, 0x0000);
1617 RTL_W32(TxDescStartAddrLow
, ((u64
) tp
->TxPhyAddr
& DMA_32BIT_MASK
));
1618 RTL_W32(TxDescStartAddrHigh
, ((u64
) tp
->TxPhyAddr
>> 32));
1619 RTL_W32(RxDescAddrLow
, ((u64
) tp
->RxPhyAddr
& DMA_32BIT_MASK
));
1620 RTL_W32(RxDescAddrHigh
, ((u64
) tp
->RxPhyAddr
>> 32));
1621 RTL_W8(Cfg9346
, Cfg9346_Lock
);
1624 RTL_W32(RxMissed
, 0);
1626 rtl8169_set_rx_mode(dev
);
1628 /* no early-rx interrupts */
1629 RTL_W16(MultiIntr
, RTL_R16(MultiIntr
) & 0xF000);
1631 /* Enable all known interrupts by setting the interrupt mask. */
1632 RTL_W16(IntrMask
, rtl8169_intr_mask
);
1634 netif_start_queue(dev
);
1637 static int rtl8169_change_mtu(struct net_device
*dev
, int new_mtu
)
1639 struct rtl8169_private
*tp
= netdev_priv(dev
);
1642 if (new_mtu
< ETH_ZLEN
|| new_mtu
> SafeMtu
)
1647 if (!netif_running(dev
))
1652 rtl8169_set_rxbufsize(tp
, dev
);
1654 ret
= rtl8169_init_ring(dev
);
1658 netif_poll_enable(dev
);
1660 rtl8169_hw_start(dev
);
1662 rtl8169_request_timer(dev
);
1668 static inline void rtl8169_make_unusable_by_asic(struct RxDesc
*desc
)
1670 desc
->addr
= 0x0badbadbadbadbadull
;
1671 desc
->opts1
&= ~cpu_to_le32(DescOwn
| RsvdMask
);
1674 static void rtl8169_free_rx_skb(struct rtl8169_private
*tp
,
1675 struct sk_buff
**sk_buff
, struct RxDesc
*desc
)
1677 struct pci_dev
*pdev
= tp
->pci_dev
;
1679 pci_unmap_single(pdev
, le64_to_cpu(desc
->addr
), tp
->rx_buf_sz
,
1680 PCI_DMA_FROMDEVICE
);
1681 dev_kfree_skb(*sk_buff
);
1683 rtl8169_make_unusable_by_asic(desc
);
1686 static inline void rtl8169_mark_to_asic(struct RxDesc
*desc
, u32 rx_buf_sz
)
1688 u32 eor
= le32_to_cpu(desc
->opts1
) & RingEnd
;
1690 desc
->opts1
= cpu_to_le32(DescOwn
| eor
| rx_buf_sz
);
1693 static inline void rtl8169_map_to_asic(struct RxDesc
*desc
, dma_addr_t mapping
,
1696 desc
->addr
= cpu_to_le64(mapping
);
1698 rtl8169_mark_to_asic(desc
, rx_buf_sz
);
1701 static int rtl8169_alloc_rx_skb(struct pci_dev
*pdev
, struct sk_buff
**sk_buff
,
1702 struct RxDesc
*desc
, int rx_buf_sz
)
1704 struct sk_buff
*skb
;
1708 skb
= dev_alloc_skb(rx_buf_sz
+ NET_IP_ALIGN
);
1712 skb_reserve(skb
, NET_IP_ALIGN
);
1715 mapping
= pci_map_single(pdev
, skb
->tail
, rx_buf_sz
,
1716 PCI_DMA_FROMDEVICE
);
1718 rtl8169_map_to_asic(desc
, mapping
, rx_buf_sz
);
1725 rtl8169_make_unusable_by_asic(desc
);
1729 static void rtl8169_rx_clear(struct rtl8169_private
*tp
)
1733 for (i
= 0; i
< NUM_RX_DESC
; i
++) {
1734 if (tp
->Rx_skbuff
[i
]) {
1735 rtl8169_free_rx_skb(tp
, tp
->Rx_skbuff
+ i
,
1736 tp
->RxDescArray
+ i
);
1741 static u32
rtl8169_rx_fill(struct rtl8169_private
*tp
, struct net_device
*dev
,
1746 for (cur
= start
; end
- cur
> 0; cur
++) {
1747 int ret
, i
= cur
% NUM_RX_DESC
;
1749 if (tp
->Rx_skbuff
[i
])
1752 ret
= rtl8169_alloc_rx_skb(tp
->pci_dev
, tp
->Rx_skbuff
+ i
,
1753 tp
->RxDescArray
+ i
, tp
->rx_buf_sz
);
1760 static inline void rtl8169_mark_as_last_descriptor(struct RxDesc
*desc
)
1762 desc
->opts1
|= cpu_to_le32(RingEnd
);
1765 static void rtl8169_init_ring_indexes(struct rtl8169_private
*tp
)
1767 tp
->dirty_tx
= tp
->dirty_rx
= tp
->cur_tx
= tp
->cur_rx
= 0;
1770 static int rtl8169_init_ring(struct net_device
*dev
)
1772 struct rtl8169_private
*tp
= netdev_priv(dev
);
1774 rtl8169_init_ring_indexes(tp
);
1776 memset(tp
->tx_skb
, 0x0, NUM_TX_DESC
* sizeof(struct ring_info
));
1777 memset(tp
->Rx_skbuff
, 0x0, NUM_RX_DESC
* sizeof(struct sk_buff
*));
1779 if (rtl8169_rx_fill(tp
, dev
, 0, NUM_RX_DESC
) != NUM_RX_DESC
)
1782 rtl8169_mark_as_last_descriptor(tp
->RxDescArray
+ NUM_RX_DESC
- 1);
1787 rtl8169_rx_clear(tp
);
1791 static void rtl8169_unmap_tx_skb(struct pci_dev
*pdev
, struct ring_info
*tx_skb
,
1792 struct TxDesc
*desc
)
1794 unsigned int len
= tx_skb
->len
;
1796 pci_unmap_single(pdev
, le64_to_cpu(desc
->addr
), len
, PCI_DMA_TODEVICE
);
1803 static void rtl8169_tx_clear(struct rtl8169_private
*tp
)
1807 for (i
= tp
->dirty_tx
; i
< tp
->dirty_tx
+ NUM_TX_DESC
; i
++) {
1808 unsigned int entry
= i
% NUM_TX_DESC
;
1809 struct ring_info
*tx_skb
= tp
->tx_skb
+ entry
;
1810 unsigned int len
= tx_skb
->len
;
1813 struct sk_buff
*skb
= tx_skb
->skb
;
1815 rtl8169_unmap_tx_skb(tp
->pci_dev
, tx_skb
,
1816 tp
->TxDescArray
+ entry
);
1821 tp
->stats
.tx_dropped
++;
1824 tp
->cur_tx
= tp
->dirty_tx
= 0;
1827 static void rtl8169_schedule_work(struct net_device
*dev
, void (*task
)(void *))
1829 struct rtl8169_private
*tp
= netdev_priv(dev
);
1831 PREPARE_WORK(&tp
->task
, task
, dev
);
1832 schedule_delayed_work(&tp
->task
, 4);
1835 static void rtl8169_wait_for_quiescence(struct net_device
*dev
)
1837 struct rtl8169_private
*tp
= netdev_priv(dev
);
1838 void __iomem
*ioaddr
= tp
->mmio_addr
;
1840 synchronize_irq(dev
->irq
);
1842 /* Wait for any pending NAPI task to complete */
1843 netif_poll_disable(dev
);
1845 rtl8169_irq_mask_and_ack(ioaddr
);
1847 netif_poll_enable(dev
);
1850 static void rtl8169_reinit_task(void *_data
)
1852 struct net_device
*dev
= _data
;
1855 if (netif_running(dev
)) {
1856 rtl8169_wait_for_quiescence(dev
);
1860 ret
= rtl8169_open(dev
);
1861 if (unlikely(ret
< 0)) {
1862 if (net_ratelimit()) {
1863 printk(PFX KERN_ERR
"%s: reinit failure (status = %d)."
1864 " Rescheduling.\n", dev
->name
, ret
);
1866 rtl8169_schedule_work(dev
, rtl8169_reinit_task
);
1870 static void rtl8169_reset_task(void *_data
)
1872 struct net_device
*dev
= _data
;
1873 struct rtl8169_private
*tp
= netdev_priv(dev
);
1875 if (!netif_running(dev
))
1878 rtl8169_wait_for_quiescence(dev
);
1880 rtl8169_rx_interrupt(dev
, tp
, tp
->mmio_addr
);
1881 rtl8169_tx_clear(tp
);
1883 if (tp
->dirty_rx
== tp
->cur_rx
) {
1884 rtl8169_init_ring_indexes(tp
);
1885 rtl8169_hw_start(dev
);
1886 netif_wake_queue(dev
);
1888 if (net_ratelimit()) {
1889 printk(PFX KERN_EMERG
"%s: Rx buffers shortage\n",
1892 rtl8169_schedule_work(dev
, rtl8169_reset_task
);
1896 static void rtl8169_tx_timeout(struct net_device
*dev
)
1898 struct rtl8169_private
*tp
= netdev_priv(dev
);
1900 rtl8169_hw_reset(tp
->mmio_addr
);
1902 /* Let's wait a bit while any (async) irq lands on */
1903 rtl8169_schedule_work(dev
, rtl8169_reset_task
);
1906 static int rtl8169_xmit_frags(struct rtl8169_private
*tp
, struct sk_buff
*skb
,
1909 struct skb_shared_info
*info
= skb_shinfo(skb
);
1910 unsigned int cur_frag
, entry
;
1914 for (cur_frag
= 0; cur_frag
< info
->nr_frags
; cur_frag
++) {
1915 skb_frag_t
*frag
= info
->frags
+ cur_frag
;
1920 entry
= (entry
+ 1) % NUM_TX_DESC
;
1922 txd
= tp
->TxDescArray
+ entry
;
1924 addr
= ((void *) page_address(frag
->page
)) + frag
->page_offset
;
1925 mapping
= pci_map_single(tp
->pci_dev
, addr
, len
, PCI_DMA_TODEVICE
);
1927 /* anti gcc 2.95.3 bugware (sic) */
1928 status
= opts1
| len
| (RingEnd
* !((entry
+ 1) % NUM_TX_DESC
));
1930 txd
->opts1
= cpu_to_le32(status
);
1931 txd
->addr
= cpu_to_le64(mapping
);
1933 tp
->tx_skb
[entry
].len
= len
;
1937 tp
->tx_skb
[entry
].skb
= skb
;
1938 txd
->opts1
|= cpu_to_le32(LastFrag
);
1944 static inline u32
rtl8169_tso_csum(struct sk_buff
*skb
, struct net_device
*dev
)
1946 if (dev
->features
& NETIF_F_TSO
) {
1947 u32 mss
= skb_shinfo(skb
)->tso_size
;
1950 return LargeSend
| ((mss
& MSSMask
) << MSSShift
);
1952 if (skb
->ip_summed
== CHECKSUM_HW
) {
1953 const struct iphdr
*ip
= skb
->nh
.iph
;
1955 if (ip
->protocol
== IPPROTO_TCP
)
1956 return IPCS
| TCPCS
;
1957 else if (ip
->protocol
== IPPROTO_UDP
)
1958 return IPCS
| UDPCS
;
1959 WARN_ON(1); /* we need a WARN() */
1964 static int rtl8169_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1966 struct rtl8169_private
*tp
= netdev_priv(dev
);
1967 unsigned int frags
, entry
= tp
->cur_tx
% NUM_TX_DESC
;
1968 struct TxDesc
*txd
= tp
->TxDescArray
+ entry
;
1969 void __iomem
*ioaddr
= tp
->mmio_addr
;
1975 if (unlikely(TX_BUFFS_AVAIL(tp
) < skb_shinfo(skb
)->nr_frags
)) {
1976 printk(KERN_ERR PFX
"%s: BUG! Tx Ring full when queue awake!\n",
1981 if (unlikely(le32_to_cpu(txd
->opts1
) & DescOwn
))
1984 opts1
= DescOwn
| rtl8169_tso_csum(skb
, dev
);
1986 frags
= rtl8169_xmit_frags(tp
, skb
, opts1
);
1988 len
= skb_headlen(skb
);
1993 if (unlikely(len
< ETH_ZLEN
)) {
1994 skb
= skb_padto(skb
, ETH_ZLEN
);
1996 goto err_update_stats
;
2000 opts1
|= FirstFrag
| LastFrag
;
2001 tp
->tx_skb
[entry
].skb
= skb
;
2004 mapping
= pci_map_single(tp
->pci_dev
, skb
->data
, len
, PCI_DMA_TODEVICE
);
2006 tp
->tx_skb
[entry
].len
= len
;
2007 txd
->addr
= cpu_to_le64(mapping
);
2008 txd
->opts2
= cpu_to_le32(rtl8169_tx_vlan_tag(tp
, skb
));
2012 /* anti gcc 2.95.3 bugware (sic) */
2013 status
= opts1
| len
| (RingEnd
* !((entry
+ 1) % NUM_TX_DESC
));
2014 txd
->opts1
= cpu_to_le32(status
);
2016 dev
->trans_start
= jiffies
;
2018 tp
->cur_tx
+= frags
+ 1;
2022 RTL_W8(TxPoll
, 0x40); /* set polling bit */
2024 if (TX_BUFFS_AVAIL(tp
) < MAX_SKB_FRAGS
) {
2025 netif_stop_queue(dev
);
2027 if (TX_BUFFS_AVAIL(tp
) >= MAX_SKB_FRAGS
)
2028 netif_wake_queue(dev
);
2035 netif_stop_queue(dev
);
2038 tp
->stats
.tx_dropped
++;
2042 static void rtl8169_pcierr_interrupt(struct net_device
*dev
)
2044 struct rtl8169_private
*tp
= netdev_priv(dev
);
2045 struct pci_dev
*pdev
= tp
->pci_dev
;
2046 void __iomem
*ioaddr
= tp
->mmio_addr
;
2047 u16 pci_status
, pci_cmd
;
2049 pci_read_config_word(pdev
, PCI_COMMAND
, &pci_cmd
);
2050 pci_read_config_word(pdev
, PCI_STATUS
, &pci_status
);
2052 printk(KERN_ERR PFX
"%s: PCI error (cmd = 0x%04x, status = 0x%04x).\n",
2053 dev
->name
, pci_cmd
, pci_status
);
2056 * The recovery sequence below admits a very elaborated explanation:
2057 * - it seems to work;
2058 * - I did not see what else could be done.
2060 * Feel free to adjust to your needs.
2062 pci_write_config_word(pdev
, PCI_COMMAND
,
2063 pci_cmd
| PCI_COMMAND_SERR
| PCI_COMMAND_PARITY
);
2065 pci_write_config_word(pdev
, PCI_STATUS
,
2066 pci_status
& (PCI_STATUS_DETECTED_PARITY
|
2067 PCI_STATUS_SIG_SYSTEM_ERROR
| PCI_STATUS_REC_MASTER_ABORT
|
2068 PCI_STATUS_REC_TARGET_ABORT
| PCI_STATUS_SIG_TARGET_ABORT
));
2070 /* The infamous DAC f*ckup only happens at boot time */
2071 if ((tp
->cp_cmd
& PCIDAC
) && !tp
->dirty_rx
&& !tp
->cur_rx
) {
2072 printk(KERN_INFO PFX
"%s: disabling PCI DAC.\n", dev
->name
);
2073 tp
->cp_cmd
&= ~PCIDAC
;
2074 RTL_W16(CPlusCmd
, tp
->cp_cmd
);
2075 dev
->features
&= ~NETIF_F_HIGHDMA
;
2076 rtl8169_schedule_work(dev
, rtl8169_reinit_task
);
2079 rtl8169_hw_reset(ioaddr
);
2083 rtl8169_tx_interrupt(struct net_device
*dev
, struct rtl8169_private
*tp
,
2084 void __iomem
*ioaddr
)
2086 unsigned int dirty_tx
, tx_left
;
2088 assert(dev
!= NULL
);
2090 assert(ioaddr
!= NULL
);
2092 dirty_tx
= tp
->dirty_tx
;
2094 tx_left
= tp
->cur_tx
- dirty_tx
;
2096 while (tx_left
> 0) {
2097 unsigned int entry
= dirty_tx
% NUM_TX_DESC
;
2098 struct ring_info
*tx_skb
= tp
->tx_skb
+ entry
;
2099 u32 len
= tx_skb
->len
;
2103 status
= le32_to_cpu(tp
->TxDescArray
[entry
].opts1
);
2104 if (status
& DescOwn
)
2107 tp
->stats
.tx_bytes
+= len
;
2108 tp
->stats
.tx_packets
++;
2110 rtl8169_unmap_tx_skb(tp
->pci_dev
, tx_skb
, tp
->TxDescArray
+ entry
);
2112 if (status
& LastFrag
) {
2113 dev_kfree_skb_irq(tx_skb
->skb
);
2120 if (tp
->dirty_tx
!= dirty_tx
) {
2121 tp
->dirty_tx
= dirty_tx
;
2123 if (netif_queue_stopped(dev
) &&
2124 (TX_BUFFS_AVAIL(tp
) >= MAX_SKB_FRAGS
)) {
2125 netif_wake_queue(dev
);
2130 static inline void rtl8169_rx_csum(struct sk_buff
*skb
, struct RxDesc
*desc
)
2132 u32 opts1
= le32_to_cpu(desc
->opts1
);
2133 u32 status
= opts1
& RxProtoMask
;
2135 if (((status
== RxProtoTCP
) && !(opts1
& TCPFail
)) ||
2136 ((status
== RxProtoUDP
) && !(opts1
& UDPFail
)) ||
2137 ((status
== RxProtoIP
) && !(opts1
& IPFail
)))
2138 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
2140 skb
->ip_summed
= CHECKSUM_NONE
;
2143 static inline int rtl8169_try_rx_copy(struct sk_buff
**sk_buff
, int pkt_size
,
2144 struct RxDesc
*desc
, int rx_buf_sz
)
2148 if (pkt_size
< rx_copybreak
) {
2149 struct sk_buff
*skb
;
2151 skb
= dev_alloc_skb(pkt_size
+ NET_IP_ALIGN
);
2153 skb_reserve(skb
, NET_IP_ALIGN
);
2154 eth_copy_and_sum(skb
, sk_buff
[0]->tail
, pkt_size
, 0);
2156 rtl8169_mark_to_asic(desc
, rx_buf_sz
);
2164 rtl8169_rx_interrupt(struct net_device
*dev
, struct rtl8169_private
*tp
,
2165 void __iomem
*ioaddr
)
2167 unsigned int cur_rx
, rx_left
;
2168 unsigned int delta
, count
;
2170 assert(dev
!= NULL
);
2172 assert(ioaddr
!= NULL
);
2174 cur_rx
= tp
->cur_rx
;
2175 rx_left
= NUM_RX_DESC
+ tp
->dirty_rx
- cur_rx
;
2176 rx_left
= rtl8169_rx_quota(rx_left
, (u32
) dev
->quota
);
2178 while (rx_left
> 0) {
2179 unsigned int entry
= cur_rx
% NUM_RX_DESC
;
2183 status
= le32_to_cpu(tp
->RxDescArray
[entry
].opts1
);
2185 if (status
& DescOwn
)
2187 if (status
& RxRES
) {
2188 printk(KERN_INFO
"%s: Rx ERROR!!!\n", dev
->name
);
2189 tp
->stats
.rx_errors
++;
2190 if (status
& (RxRWT
| RxRUNT
))
2191 tp
->stats
.rx_length_errors
++;
2193 tp
->stats
.rx_crc_errors
++;
2195 struct RxDesc
*desc
= tp
->RxDescArray
+ entry
;
2196 struct sk_buff
*skb
= tp
->Rx_skbuff
[entry
];
2197 int pkt_size
= (status
& 0x00001FFF) - 4;
2198 void (*pci_action
)(struct pci_dev
*, dma_addr_t
,
2199 size_t, int) = pci_dma_sync_single_for_device
;
2201 rtl8169_rx_csum(skb
, desc
);
2203 pci_dma_sync_single_for_cpu(tp
->pci_dev
,
2204 le64_to_cpu(desc
->addr
), tp
->rx_buf_sz
,
2205 PCI_DMA_FROMDEVICE
);
2207 if (rtl8169_try_rx_copy(&skb
, pkt_size
, desc
,
2209 pci_action
= pci_unmap_single
;
2210 tp
->Rx_skbuff
[entry
] = NULL
;
2213 pci_action(tp
->pci_dev
, le64_to_cpu(desc
->addr
),
2214 tp
->rx_buf_sz
, PCI_DMA_FROMDEVICE
);
2217 skb_put(skb
, pkt_size
);
2218 skb
->protocol
= eth_type_trans(skb
, dev
);
2220 if (rtl8169_rx_vlan_skb(tp
, desc
, skb
) < 0)
2221 rtl8169_rx_skb(skb
);
2223 dev
->last_rx
= jiffies
;
2224 tp
->stats
.rx_bytes
+= pkt_size
;
2225 tp
->stats
.rx_packets
++;
2232 count
= cur_rx
- tp
->cur_rx
;
2233 tp
->cur_rx
= cur_rx
;
2235 delta
= rtl8169_rx_fill(tp
, dev
, tp
->dirty_rx
, tp
->cur_rx
);
2236 if (!delta
&& count
)
2237 printk(KERN_INFO
"%s: no Rx buffer allocated\n", dev
->name
);
2238 tp
->dirty_rx
+= delta
;
2241 * FIXME: until there is periodic timer to try and refill the ring,
2242 * a temporary shortage may definitely kill the Rx process.
2243 * - disable the asic to try and avoid an overflow and kick it again
2245 * - how do others driver handle this condition (Uh oh...).
2247 if (tp
->dirty_rx
+ NUM_RX_DESC
== tp
->cur_rx
)
2248 printk(KERN_EMERG
"%s: Rx buffers exhausted\n", dev
->name
);
2253 /* The interrupt handler does all of the Rx thread work and cleans up after the Tx thread. */
2255 rtl8169_interrupt(int irq
, void *dev_instance
, struct pt_regs
*regs
)
2257 struct net_device
*dev
= (struct net_device
*) dev_instance
;
2258 struct rtl8169_private
*tp
= netdev_priv(dev
);
2259 int boguscnt
= max_interrupt_work
;
2260 void __iomem
*ioaddr
= tp
->mmio_addr
;
2265 status
= RTL_R16(IntrStatus
);
2267 /* hotplug/major error/no more work/shared irq */
2268 if ((status
== 0xFFFF) || !status
)
2273 if (unlikely(!netif_running(dev
))) {
2274 rtl8169_asic_down(ioaddr
);
2278 status
&= tp
->intr_mask
;
2280 (status
& RxFIFOOver
) ? (status
| RxOverflow
) : status
);
2282 if (!(status
& rtl8169_intr_mask
))
2285 if (unlikely(status
& SYSErr
)) {
2286 rtl8169_pcierr_interrupt(dev
);
2290 if (status
& LinkChg
)
2291 rtl8169_check_link_status(dev
, tp
, ioaddr
);
2293 #ifdef CONFIG_R8169_NAPI
2294 RTL_W16(IntrMask
, rtl8169_intr_mask
& ~rtl8169_napi_event
);
2295 tp
->intr_mask
= ~rtl8169_napi_event
;
2297 if (likely(netif_rx_schedule_prep(dev
)))
2298 __netif_rx_schedule(dev
);
2300 printk(KERN_INFO
"%s: interrupt %04x taken in poll\n",
2306 if (status
& (RxOK
| RxOverflow
| RxFIFOOver
)) {
2307 rtl8169_rx_interrupt(dev
, tp
, ioaddr
);
2310 if (status
& (TxOK
| TxErr
))
2311 rtl8169_tx_interrupt(dev
, tp
, ioaddr
);
2315 } while (boguscnt
> 0);
2317 if (boguscnt
<= 0) {
2318 printk(KERN_WARNING
"%s: Too much work at interrupt!\n",
2320 /* Clear all interrupt sources. */
2321 RTL_W16(IntrStatus
, 0xffff);
2324 return IRQ_RETVAL(handled
);
2327 #ifdef CONFIG_R8169_NAPI
2328 static int rtl8169_poll(struct net_device
*dev
, int *budget
)
2330 unsigned int work_done
, work_to_do
= min(*budget
, dev
->quota
);
2331 struct rtl8169_private
*tp
= netdev_priv(dev
);
2332 void __iomem
*ioaddr
= tp
->mmio_addr
;
2334 work_done
= rtl8169_rx_interrupt(dev
, tp
, ioaddr
);
2335 rtl8169_tx_interrupt(dev
, tp
, ioaddr
);
2337 *budget
-= work_done
;
2338 dev
->quota
-= work_done
;
2340 if (work_done
< work_to_do
) {
2341 netif_rx_complete(dev
);
2342 tp
->intr_mask
= 0xffff;
2344 * 20040426: the barrier is not strictly required but the
2345 * behavior of the irq handler could be less predictable
2346 * without it. Btw, the lack of flush for the posted pci
2347 * write is safe - FR
2350 RTL_W16(IntrMask
, rtl8169_intr_mask
);
2353 return (work_done
>= work_to_do
);
2357 static void rtl8169_down(struct net_device
*dev
)
2359 struct rtl8169_private
*tp
= netdev_priv(dev
);
2360 void __iomem
*ioaddr
= tp
->mmio_addr
;
2361 unsigned int poll_locked
= 0;
2363 rtl8169_delete_timer(dev
);
2365 netif_stop_queue(dev
);
2367 flush_scheduled_work();
2370 spin_lock_irq(&tp
->lock
);
2372 rtl8169_asic_down(ioaddr
);
2374 /* Update the error counts. */
2375 tp
->stats
.rx_missed_errors
+= RTL_R32(RxMissed
);
2376 RTL_W32(RxMissed
, 0);
2378 spin_unlock_irq(&tp
->lock
);
2380 synchronize_irq(dev
->irq
);
2383 netif_poll_disable(dev
);
2387 /* Give a racing hard_start_xmit a few cycles to complete. */
2388 synchronize_kernel();
2391 * And now for the 50k$ question: are IRQ disabled or not ?
2393 * Two paths lead here:
2395 * -> netif_running() is available to sync the current code and the
2396 * IRQ handler. See rtl8169_interrupt for details.
2397 * 2) dev->change_mtu
2398 * -> rtl8169_poll can not be issued again and re-enable the
2399 * interruptions. Let's simply issue the IRQ down sequence again.
2401 if (RTL_R16(IntrMask
))
2404 rtl8169_tx_clear(tp
);
2406 rtl8169_rx_clear(tp
);
2409 static int rtl8169_close(struct net_device
*dev
)
2411 struct rtl8169_private
*tp
= netdev_priv(dev
);
2412 struct pci_dev
*pdev
= tp
->pci_dev
;
2416 free_irq(dev
->irq
, dev
);
2418 netif_poll_enable(dev
);
2420 pci_free_consistent(pdev
, R8169_RX_RING_BYTES
, tp
->RxDescArray
,
2422 pci_free_consistent(pdev
, R8169_TX_RING_BYTES
, tp
->TxDescArray
,
2424 tp
->TxDescArray
= NULL
;
2425 tp
->RxDescArray
= NULL
;
2431 rtl8169_set_rx_mode(struct net_device
*dev
)
2433 struct rtl8169_private
*tp
= netdev_priv(dev
);
2434 void __iomem
*ioaddr
= tp
->mmio_addr
;
2435 unsigned long flags
;
2436 u32 mc_filter
[2]; /* Multicast hash filter */
2440 if (dev
->flags
& IFF_PROMISC
) {
2441 /* Unconditionally log net taps. */
2442 printk(KERN_NOTICE
"%s: Promiscuous mode enabled.\n",
2445 AcceptBroadcast
| AcceptMulticast
| AcceptMyPhys
|
2447 mc_filter
[1] = mc_filter
[0] = 0xffffffff;
2448 } else if ((dev
->mc_count
> multicast_filter_limit
)
2449 || (dev
->flags
& IFF_ALLMULTI
)) {
2450 /* Too many to filter perfectly -- accept all multicasts. */
2451 rx_mode
= AcceptBroadcast
| AcceptMulticast
| AcceptMyPhys
;
2452 mc_filter
[1] = mc_filter
[0] = 0xffffffff;
2454 struct dev_mc_list
*mclist
;
2455 rx_mode
= AcceptBroadcast
| AcceptMyPhys
;
2456 mc_filter
[1] = mc_filter
[0] = 0;
2457 for (i
= 0, mclist
= dev
->mc_list
; mclist
&& i
< dev
->mc_count
;
2458 i
++, mclist
= mclist
->next
) {
2459 int bit_nr
= ether_crc(ETH_ALEN
, mclist
->dmi_addr
) >> 26;
2460 mc_filter
[bit_nr
>> 5] |= 1 << (bit_nr
& 31);
2461 rx_mode
|= AcceptMulticast
;
2465 spin_lock_irqsave(&tp
->lock
, flags
);
2467 tmp
= rtl8169_rx_config
| rx_mode
|
2468 (RTL_R32(RxConfig
) & rtl_chip_info
[tp
->chipset
].RxConfigMask
);
2470 RTL_W32(RxConfig
, tmp
);
2471 RTL_W32(MAR0
+ 0, mc_filter
[0]);
2472 RTL_W32(MAR0
+ 4, mc_filter
[1]);
2474 spin_unlock_irqrestore(&tp
->lock
, flags
);
2478 * rtl8169_get_stats - Get rtl8169 read/write statistics
2479 * @dev: The Ethernet Device to get statistics for
2481 * Get TX/RX statistics for rtl8169
2483 static struct net_device_stats
*rtl8169_get_stats(struct net_device
*dev
)
2485 struct rtl8169_private
*tp
= netdev_priv(dev
);
2486 void __iomem
*ioaddr
= tp
->mmio_addr
;
2487 unsigned long flags
;
2489 if (netif_running(dev
)) {
2490 spin_lock_irqsave(&tp
->lock
, flags
);
2491 tp
->stats
.rx_missed_errors
+= RTL_R32(RxMissed
);
2492 RTL_W32(RxMissed
, 0);
2493 spin_unlock_irqrestore(&tp
->lock
, flags
);
2499 static struct pci_driver rtl8169_pci_driver
= {
2501 .id_table
= rtl8169_pci_tbl
,
2502 .probe
= rtl8169_init_one
,
2503 .remove
= __devexit_p(rtl8169_remove_one
),
2505 .suspend
= rtl8169_suspend
,
2506 .resume
= rtl8169_resume
,
2511 rtl8169_init_module(void)
2513 return pci_module_init(&rtl8169_pci_driver
);
2517 rtl8169_cleanup_module(void)
2519 pci_unregister_driver(&rtl8169_pci_driver
);
2522 module_init(rtl8169_init_module
);
2523 module_exit(rtl8169_cleanup_module
);