8139cp.c 54.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* 8139cp.c: A Linux PCI Ethernet driver for the RealTek 8139C+ chips. */
/*
	Copyright 2001-2004 Jeff Garzik <jgarzik@pobox.com>

	Copyright (C) 2001, 2002 David S. Miller (davem@redhat.com) [tg3.c]
	Copyright (C) 2000, 2001 David S. Miller (davem@redhat.com) [sungem.c]
	Copyright 2001 Manfred Spraul				    [natsemi.c]
	Copyright 1999-2001 by Donald Becker.			    [natsemi.c]
       	Written 1997-2001 by Donald Becker.			    [8139too.c]
	Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>. [acenic.c]

	This software may be used and distributed according to the terms of
	the GNU General Public License (GPL), incorporated herein by reference.
	Drivers based on or derived from this code fall under the GPL and must
	retain the authorship, copyright and license notice.  This file is not
	a complete program and may only be used when the entire operating
	system is licensed under the GPL.

	See the file COPYING in this distribution for more information.

	Contributors:
22

L
Linus Torvalds 已提交
23 24 25
		Wake-on-LAN support - Felipe Damasio <felipewd@terra.com.br>
		PCI suspend/resume  - Felipe Damasio <felipewd@terra.com.br>
		LinkChg interrupt   - Felipe Damasio <felipewd@terra.com.br>
26

L
Linus Torvalds 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
	TODO:
	* Test Tx checksumming thoroughly

	Low priority TODO:
	* Complete reset on PciErr
	* Consider Rx interrupt mitigation using TimerIntr
	* Investigate using skb->priority with h/w VLAN priority
	* Investigate using High Priority Tx Queue with skb->priority
	* Adjust Rx FIFO threshold and Max Rx DMA burst on Rx FIFO error
	* Adjust Tx FIFO threshold and Max Tx DMA burst on Tx FIFO error
	* Implement Tx software interrupt mitigation via
	  Tx descriptor bit
	* The real minimum of CP_MIN_MTU is 4 bytes.  However,
	  for this to be supported, one must(?) turn on packet padding.
	* Support external MII transceivers (patch available)

	NOTES:
	* TX checksumming is considered experimental.  It is off by
	  default, use ethtool to turn it on.

 */

49 50
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

L
Linus Torvalds 已提交
51
#define DRV_NAME		"8139cp"
52
#define DRV_VERSION		"1.3"
L
Linus Torvalds 已提交
53 54 55 56
#define DRV_RELDATE		"Mar 22, 2004"


#include <linux/module.h>
57
#include <linux/moduleparam.h>
L
Linus Torvalds 已提交
58 59 60 61 62
#include <linux/kernel.h>
#include <linux/compiler.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/init.h>
63
#include <linux/interrupt.h>
L
Linus Torvalds 已提交
64
#include <linux/pci.h>
65
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
66 67
#include <linux/delay.h>
#include <linux/ethtool.h>
68
#include <linux/gfp.h>
L
Linus Torvalds 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <linux/mii.h>
#include <linux/if_vlan.h>
#include <linux/crc32.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/cache.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>

/* These identify the driver base version and may not be removed. */
static char version[] =
83
DRV_NAME ": 10/100 PCI Ethernet driver v" DRV_VERSION " (" DRV_RELDATE ")\n";
L
Linus Torvalds 已提交
84 85 86

MODULE_AUTHOR("Jeff Garzik <jgarzik@pobox.com>");
MODULE_DESCRIPTION("RealTek RTL-8139C+ series 10/100 PCI Ethernet driver");
87
MODULE_VERSION(DRV_VERSION);
L
Linus Torvalds 已提交
88 89 90
MODULE_LICENSE("GPL");

static int debug = -1;
91
module_param(debug, int, 0);
L
Linus Torvalds 已提交
92 93 94 95 96
MODULE_PARM_DESC (debug, "8139cp: bitmapped message enable number");

/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
   The RTL chips use a 64 element hash table based on the Ethernet CRC.  */
static int multicast_filter_limit = 32;
97
module_param(multicast_filter_limit, int, 0);
L
Linus Torvalds 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
MODULE_PARM_DESC (multicast_filter_limit, "8139cp: maximum number of filtered multicast addresses");

#define CP_DEF_MSG_ENABLE	(NETIF_MSG_DRV		| \
				 NETIF_MSG_PROBE 	| \
				 NETIF_MSG_LINK)
#define CP_NUM_STATS		14	/* struct cp_dma_stats, plus one */
#define CP_STATS_SIZE		64	/* size in bytes of DMA stats block */
#define CP_REGS_SIZE		(0xff + 1)
#define CP_REGS_VER		1		/* version 1 */
#define CP_RX_RING_SIZE		64
#define CP_TX_RING_SIZE		64
#define CP_RING_BYTES		\
		((sizeof(struct cp_desc) * CP_RX_RING_SIZE) +	\
		 (sizeof(struct cp_desc) * CP_TX_RING_SIZE) +	\
		 CP_STATS_SIZE)
#define NEXT_TX(N)		(((N) + 1) & (CP_TX_RING_SIZE - 1))
#define NEXT_RX(N)		(((N) + 1) & (CP_RX_RING_SIZE - 1))
#define TX_BUFFS_AVAIL(CP)					\
	(((CP)->tx_tail <= (CP)->tx_head) ?			\
	  (CP)->tx_tail + (CP_TX_RING_SIZE - 1) - (CP)->tx_head :	\
	  (CP)->tx_tail - (CP)->tx_head - 1)

#define PKT_BUF_SZ		1536	/* Size of each temporary Rx buffer.*/
#define CP_INTERNAL_PHY		32

/* The following settings are log_2(bytes)-4:  0 == 16 bytes .. 6==1024, 7==end of packet. */
#define RX_FIFO_THRESH		5	/* Rx buffer level before first PCI xfer.  */
#define RX_DMA_BURST		4	/* Maximum PCI burst, '4' is 256 */
#define TX_DMA_BURST		6	/* Maximum PCI burst, '6' is 1024 */
#define TX_EARLY_THRESH		256	/* Early Tx threshold, in bytes */

/* Time in jiffies before concluding the transmitter is hung. */
#define TX_TIMEOUT		(6*HZ)

/* hardware minimum and maximum for a single frame's data payload */
#define CP_MIN_MTU		60	/* TODO: allow lower, but pad */
#define CP_MAX_MTU		4096

enum {
	/* NIC register offsets */
	MAC0		= 0x00,	/* Ethernet hardware address. */
	MAR0		= 0x08,	/* Multicast filter. */
	StatsAddr	= 0x10,	/* 64-bit start addr of 64-byte DMA stats blk */
	TxRingAddr	= 0x20, /* 64-bit start addr of Tx ring */
	HiTxRingAddr	= 0x28, /* 64-bit start addr of high priority Tx ring */
	Cmd		= 0x37, /* Command register */
	IntrMask	= 0x3C, /* Interrupt mask */
	IntrStatus	= 0x3E, /* Interrupt status */
	TxConfig	= 0x40, /* Tx configuration */
	ChipVersion	= 0x43, /* 8-bit chip version, inside TxConfig */
	RxConfig	= 0x44, /* Rx configuration */
	RxMissed	= 0x4C,	/* 24 bits valid, write clears */
	Cfg9346		= 0x50, /* EEPROM select/control; Cfg reg [un]lock */
	Config1		= 0x52, /* Config1 */
	Config3		= 0x59, /* Config3 */
	Config4		= 0x5A, /* Config4 */
	MultiIntr	= 0x5C, /* Multiple interrupt select */
	BasicModeCtrl	= 0x62,	/* MII BMCR */
	BasicModeStatus	= 0x64, /* MII BMSR */
	NWayAdvert	= 0x66, /* MII ADVERTISE */
	NWayLPAR	= 0x68, /* MII LPA */
	NWayExpansion	= 0x6A, /* MII Expansion */
	Config5		= 0xD8,	/* Config5 */
	TxPoll		= 0xD9,	/* Tell chip to check Tx descriptors for work */
	RxMaxSize	= 0xDA, /* Max size of an Rx packet (8169 only) */
	CpCmd		= 0xE0, /* C+ Command register (C+ mode only) */
	IntrMitigate	= 0xE2,	/* rx/tx interrupt mitigation control */
	RxRingAddr	= 0xE4, /* 64-bit start addr of Rx ring */
	TxThresh	= 0xEC, /* Early Tx threshold */
	OldRxBufAddr	= 0x30, /* DMA address of Rx ring buffer (C mode) */
	OldTSD0		= 0x10, /* DMA address of first Tx desc (C mode) */

	/* Tx and Rx status descriptors */
	DescOwn		= (1 << 31), /* Descriptor is owned by NIC */
	RingEnd		= (1 << 30), /* End of descriptor ring */
	FirstFrag	= (1 << 29), /* First segment of a packet */
	LastFrag	= (1 << 28), /* Final segment of a packet */
J
Jeff Garzik 已提交
175 176 177
	LargeSend	= (1 << 27), /* TCP Large Send Offload (TSO) */
	MSSShift	= 16,	     /* MSS value position */
	MSSMask		= 0xfff,     /* MSS value: 11 bits */
L
Linus Torvalds 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	TxError		= (1 << 23), /* Tx error summary */
	RxError		= (1 << 20), /* Rx error summary */
	IPCS		= (1 << 18), /* Calculate IP checksum */
	UDPCS		= (1 << 17), /* Calculate UDP/IP checksum */
	TCPCS		= (1 << 16), /* Calculate TCP/IP checksum */
	TxVlanTag	= (1 << 17), /* Add VLAN tag */
	RxVlanTagged	= (1 << 16), /* Rx VLAN tag available */
	IPFail		= (1 << 15), /* IP checksum failed */
	UDPFail		= (1 << 14), /* UDP/IP checksum failed */
	TCPFail		= (1 << 13), /* TCP/IP checksum failed */
	NormalTxPoll	= (1 << 6),  /* One or more normal Tx packets to send */
	PID1		= (1 << 17), /* 2 protocol id bits:  0==non-IP, */
	PID0		= (1 << 16), /* 1==UDP/IP, 2==TCP/IP, 3==IP */
	RxProtoTCP	= 1,
	RxProtoUDP	= 2,
	RxProtoIP	= 3,
	TxFIFOUnder	= (1 << 25), /* Tx FIFO underrun */
	TxOWC		= (1 << 22), /* Tx Out-of-window collision */
	TxLinkFail	= (1 << 21), /* Link failed during Tx of packet */
	TxMaxCol	= (1 << 20), /* Tx aborted due to excessive collisions */
	TxColCntShift	= 16,	     /* Shift, to get 4-bit Tx collision cnt */
	TxColCntMask	= 0x01 | 0x02 | 0x04 | 0x08, /* 4-bit collision count */
	RxErrFrame	= (1 << 27), /* Rx frame alignment error */
	RxMcast		= (1 << 26), /* Rx multicast packet rcv'd */
	RxErrCRC	= (1 << 18), /* Rx CRC error */
	RxErrRunt	= (1 << 19), /* Rx error, packet < 64 bytes */
	RxErrLong	= (1 << 21), /* Rx error, packet > 4096 bytes */
	RxErrFIFO	= (1 << 22), /* Rx error, FIFO overflowed, pkt bad */

	/* StatsAddr register */
	DumpStats	= (1 << 3),  /* Begin stats dump */

	/* RxConfig register */
	RxCfgFIFOShift	= 13,	     /* Shift, to get Rx FIFO thresh value */
	RxCfgDMAShift	= 8,	     /* Shift, to get Rx Max DMA value */
	AcceptErr	= 0x20,	     /* Accept packets with CRC errors */
	AcceptRunt	= 0x10,	     /* Accept runt (<64 bytes) packets */
	AcceptBroadcast	= 0x08,	     /* Accept broadcast packets */
	AcceptMulticast	= 0x04,	     /* Accept multicast packets */
	AcceptMyPhys	= 0x02,	     /* Accept pkts with our MAC as dest */
	AcceptAllPhys	= 0x01,	     /* Accept all pkts w/ physical dest */

	/* IntrMask / IntrStatus registers */
	PciErr		= (1 << 15), /* System error on the PCI bus */
	TimerIntr	= (1 << 14), /* Asserted when TCTR reaches TimerInt value */
	LenChg		= (1 << 13), /* Cable length change */
	SWInt		= (1 << 8),  /* Software-requested interrupt */
	TxEmpty		= (1 << 7),  /* No Tx descriptors available */
	RxFIFOOvr	= (1 << 6),  /* Rx FIFO Overflow */
	LinkChg		= (1 << 5),  /* Packet underrun, or link change */
	RxEmpty		= (1 << 4),  /* No Rx descriptors available */
	TxErr		= (1 << 3),  /* Tx error */
	TxOK		= (1 << 2),  /* Tx packet sent */
	RxErr		= (1 << 1),  /* Rx error */
	RxOK		= (1 << 0),  /* Rx packet received */
	IntrResvd	= (1 << 10), /* reserved, according to RealTek engineers,
					but hardware likes to raise it */

	IntrAll		= PciErr | TimerIntr | LenChg | SWInt | TxEmpty |
			  RxFIFOOvr | LinkChg | RxEmpty | TxErr | TxOK |
			  RxErr | RxOK | IntrResvd,

	/* C mode command register */
	CmdReset	= (1 << 4),  /* Enable to reset; self-clearing */
	RxOn		= (1 << 3),  /* Rx mode enable */
	TxOn		= (1 << 2),  /* Tx mode enable */

	/* C+ mode command register */
	RxVlanOn	= (1 << 6),  /* Rx VLAN de-tagging enable */
	RxChkSum	= (1 << 5),  /* Rx checksum offload enable */
	PCIDAC		= (1 << 4),  /* PCI Dual Address Cycle (64-bit PCI) */
	PCIMulRW	= (1 << 3),  /* Enable PCI read/write multiple */
	CpRxOn		= (1 << 1),  /* Rx mode enable */
	CpTxOn		= (1 << 0),  /* Tx mode enable */

	/* Cfg9436 EEPROM control register */
	Cfg9346_Lock	= 0x00,	     /* Lock ConfigX/MII register access */
	Cfg9346_Unlock	= 0xC0,	     /* Unlock ConfigX/MII register access */

	/* TxConfig register */
	IFG		= (1 << 25) | (1 << 24), /* standard IEEE interframe gap */
	TxDMAShift	= 8,	     /* DMA burst value (0-7) is shift this many bits */

	/* Early Tx Threshold register */
	TxThreshMask	= 0x3f,	     /* Mask bits 5-0 */
	TxThreshMax	= 2048,	     /* Max early Tx threshold */

	/* Config1 register */
	DriverLoaded	= (1 << 5),  /* Software marker, driver is loaded */
	LWACT           = (1 << 4),  /* LWAKE active mode */
	PMEnable	= (1 << 0),  /* Enable various PM features of chip */

	/* Config3 register */
	PARMEnable	= (1 << 6),  /* Enable auto-loading of PHY parms */
	MagicPacket     = (1 << 5),  /* Wake up when receives a Magic Packet */
	LinkUp          = (1 << 4),  /* Wake up when the cable connection is re-established */

	/* Config4 register */
	LWPTN           = (1 << 1),  /* LWAKE Pattern */
	LWPME           = (1 << 4),  /* LANWAKE vs PMEB */

	/* Config5 register */
	BWF             = (1 << 6),  /* Accept Broadcast wakeup frame */
	MWF             = (1 << 5),  /* Accept Multicast wakeup frame */
	UWF             = (1 << 4),  /* Accept Unicast wakeup frame */
	LANWake         = (1 << 1),  /* Enable LANWake signal */
	PMEStatus	= (1 << 0),  /* PME status can be reset by PCI RST# */

	cp_norx_intr_mask = PciErr | LinkChg | TxOK | TxErr | TxEmpty,
	cp_rx_intr_mask = RxOK | RxErr | RxEmpty | RxFIFOOvr,
	cp_intr_mask = cp_rx_intr_mask | cp_norx_intr_mask,
};

static const unsigned int cp_rx_config =
	  (RX_FIFO_THRESH << RxCfgFIFOShift) |
	  (RX_DMA_BURST << RxCfgDMAShift);

struct cp_desc {
A
Al Viro 已提交
296
	__le32		opts1;
A
Al Viro 已提交
297
	__le32		opts2;
A
Al Viro 已提交
298
	__le64		addr;
L
Linus Torvalds 已提交
299 300 301
};

struct cp_dma_stats {
A
Al Viro 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314
	__le64			tx_ok;
	__le64			rx_ok;
	__le64			tx_err;
	__le32			rx_err;
	__le16			rx_fifo;
	__le16			frame_align;
	__le32			tx_ok_1col;
	__le32			tx_ok_mcol;
	__le64			rx_ok_phys;
	__le64			rx_ok_bcast;
	__le32			rx_ok_mcast;
	__le16			tx_abort;
	__le16			tx_underrun;
315
} __packed;
L
Linus Torvalds 已提交
316 317 318 319 320 321 322 323 324 325 326

struct cp_extra_stats {
	unsigned long		rx_frags;
};

struct cp_private {
	void			__iomem *regs;
	struct net_device	*dev;
	spinlock_t		lock;
	u32			msg_enable;

327 328
	struct napi_struct	napi;

L
Linus Torvalds 已提交
329 330 331 332 333 334
	struct pci_dev		*pdev;
	u32			rx_config;
	u16			cpcmd;

	struct cp_extra_stats	cp_stats;

335 336
	unsigned		rx_head		____cacheline_aligned;
	unsigned		rx_tail;
L
Linus Torvalds 已提交
337
	struct cp_desc		*rx_ring;
338
	struct sk_buff		*rx_skb[CP_RX_RING_SIZE];
L
Linus Torvalds 已提交
339 340 341 342

	unsigned		tx_head		____cacheline_aligned;
	unsigned		tx_tail;
	struct cp_desc		*tx_ring;
343
	struct sk_buff		*tx_skb[CP_TX_RING_SIZE];
344 345 346

	unsigned		rx_buf_sz;
	unsigned		wol_enabled : 1; /* Is Wake-on-LAN enabled? */
L
Linus Torvalds 已提交
347

348
	dma_addr_t		ring_dma;
L
Linus Torvalds 已提交
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375

	struct mii_if_info	mii_if;
};

#define cpr8(reg)	readb(cp->regs + (reg))
#define cpr16(reg)	readw(cp->regs + (reg))
#define cpr32(reg)	readl(cp->regs + (reg))
#define cpw8(reg,val)	writeb((val), cp->regs + (reg))
#define cpw16(reg,val)	writew((val), cp->regs + (reg))
#define cpw32(reg,val)	writel((val), cp->regs + (reg))
#define cpw8_f(reg,val) do {			\
	writeb((val), cp->regs + (reg));	\
	readb(cp->regs + (reg));		\
	} while (0)
#define cpw16_f(reg,val) do {			\
	writew((val), cp->regs + (reg));	\
	readw(cp->regs + (reg));		\
	} while (0)
#define cpw32_f(reg,val) do {			\
	writel((val), cp->regs + (reg));	\
	readl(cp->regs + (reg));		\
	} while (0)


static void __cp_set_rx_mode (struct net_device *dev);
static void cp_tx (struct cp_private *cp);
static void cp_clean_rings (struct cp_private *cp);
376 377 378
#ifdef CONFIG_NET_POLL_CONTROLLER
static void cp_poll_controller(struct net_device *dev);
#endif
379 380 381 382 383
static int cp_get_eeprom_len(struct net_device *dev);
static int cp_get_eeprom(struct net_device *dev,
			 struct ethtool_eeprom *eeprom, u8 *data);
static int cp_set_eeprom(struct net_device *dev,
			 struct ethtool_eeprom *eeprom, u8 *data);
L
Linus Torvalds 已提交
384

385
static DEFINE_PCI_DEVICE_TABLE(cp_pci_tbl) = {
386 387
	{ PCI_DEVICE(PCI_VENDOR_ID_REALTEK,	PCI_DEVICE_ID_REALTEK_8139), },
	{ PCI_DEVICE(PCI_VENDOR_ID_TTTECH,	PCI_DEVICE_ID_TTTECH_MC322), },
L
Linus Torvalds 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
	{ },
};
MODULE_DEVICE_TABLE(pci, cp_pci_tbl);

static struct {
	const char str[ETH_GSTRING_LEN];
} ethtool_stats_keys[] = {
	{ "tx_ok" },
	{ "rx_ok" },
	{ "tx_err" },
	{ "rx_err" },
	{ "rx_fifo" },
	{ "frame_align" },
	{ "tx_ok_1col" },
	{ "tx_ok_mcol" },
	{ "rx_ok_phys" },
	{ "rx_ok_bcast" },
	{ "rx_ok_mcast" },
	{ "tx_abort" },
	{ "tx_underrun" },
	{ "rx_frags" },
};


static inline void cp_set_rxbufsize (struct cp_private *cp)
{
	unsigned int mtu = cp->dev->mtu;
415

L
Linus Torvalds 已提交
416 417 418 419 420 421 422 423 424 425
	if (mtu > ETH_DATA_LEN)
		/* MTU + ethernet header + FCS + optional VLAN tag */
		cp->rx_buf_sz = mtu + ETH_HLEN + 8;
	else
		cp->rx_buf_sz = PKT_BUF_SZ;
}

static inline void cp_rx_skb (struct cp_private *cp, struct sk_buff *skb,
			      struct cp_desc *desc)
{
426 427
	u32 opts2 = le32_to_cpu(desc->opts2);

L
Linus Torvalds 已提交
428 429
	skb->protocol = eth_type_trans (skb, cp->dev);

430 431
	cp->dev->stats.rx_packets++;
	cp->dev->stats.rx_bytes += skb->len;
L
Linus Torvalds 已提交
432

433 434 435 436
	if (opts2 & RxVlanTagged)
		__vlan_hwaccel_put_tag(skb, swab16(opts2 & 0xffff));

	napi_gro_receive(&cp->napi, skb);
L
Linus Torvalds 已提交
437 438 439 440 441
}

static void cp_rx_err_acct (struct cp_private *cp, unsigned rx_tail,
			    u32 status, u32 len)
{
442 443
	netif_dbg(cp, rx_err, cp->dev, "rx err, slot %d status 0x%x len %d\n",
		  rx_tail, status, len);
444
	cp->dev->stats.rx_errors++;
L
Linus Torvalds 已提交
445
	if (status & RxErrFrame)
446
		cp->dev->stats.rx_frame_errors++;
L
Linus Torvalds 已提交
447
	if (status & RxErrCRC)
448
		cp->dev->stats.rx_crc_errors++;
L
Linus Torvalds 已提交
449
	if ((status & RxErrRunt) || (status & RxErrLong))
450
		cp->dev->stats.rx_length_errors++;
L
Linus Torvalds 已提交
451
	if ((status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag))
452
		cp->dev->stats.rx_length_errors++;
L
Linus Torvalds 已提交
453
	if (status & RxErrFIFO)
454
		cp->dev->stats.rx_fifo_errors++;
L
Linus Torvalds 已提交
455 456 457 458 459
}

static inline unsigned int cp_rx_csum_ok (u32 status)
{
	unsigned int protocol = (status >> 16) & 0x3;
460

S
Shan Wei 已提交
461 462
	if (((protocol == RxProtoTCP) && !(status & TCPFail)) ||
	    ((protocol == RxProtoUDP) && !(status & UDPFail)))
L
Linus Torvalds 已提交
463
		return 1;
S
Shan Wei 已提交
464 465
	else
		return 0;
L
Linus Torvalds 已提交
466 467
}

468
static int cp_rx_poll(struct napi_struct *napi, int budget)
L
Linus Torvalds 已提交
469
{
470 471 472 473
	struct cp_private *cp = container_of(napi, struct cp_private, napi);
	struct net_device *dev = cp->dev;
	unsigned int rx_tail = cp->rx_tail;
	int rx;
L
Linus Torvalds 已提交
474 475 476 477 478 479 480 481 482 483

rx_status_loop:
	rx = 0;
	cpw16(IntrStatus, cp_rx_intr_mask);

	while (1) {
		u32 status, len;
		dma_addr_t mapping;
		struct sk_buff *skb, *new_skb;
		struct cp_desc *desc;
484
		const unsigned buflen = cp->rx_buf_sz;
L
Linus Torvalds 已提交
485

486
		skb = cp->rx_skb[rx_tail];
487
		BUG_ON(!skb);
L
Linus Torvalds 已提交
488 489 490 491 492 493 494

		desc = &cp->rx_ring[rx_tail];
		status = le32_to_cpu(desc->opts1);
		if (status & DescOwn)
			break;

		len = (status & 0x1fff) - 4;
F
Francois Romieu 已提交
495
		mapping = le64_to_cpu(desc->addr);
L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503

		if ((status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag)) {
			/* we don't support incoming fragmented frames.
			 * instead, we attempt to ensure that the
			 * pre-allocated RX skbs are properly sized such
			 * that RX fragments are never encountered
			 */
			cp_rx_err_acct(cp, rx_tail, status, len);
504
			dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
505 506 507 508 509 510 511 512 513
			cp->cp_stats.rx_frags++;
			goto rx_next;
		}

		if (status & (RxError | RxErrFIFO)) {
			cp_rx_err_acct(cp, rx_tail, status, len);
			goto rx_next;
		}

514 515
		netif_dbg(cp, rx_status, dev, "rx slot %d status 0x%x len %d\n",
			  rx_tail, status, len);
L
Linus Torvalds 已提交
516

517
		new_skb = netdev_alloc_skb_ip_align(dev, buflen);
L
Linus Torvalds 已提交
518
		if (!new_skb) {
519
			dev->stats.rx_dropped++;
L
Linus Torvalds 已提交
520 521 522
			goto rx_next;
		}

523
		dma_unmap_single(&cp->pdev->dev, mapping,
L
Linus Torvalds 已提交
524 525 526 527 528 529
				 buflen, PCI_DMA_FROMDEVICE);

		/* Handle checksum offloading for incoming packets. */
		if (cp_rx_csum_ok(status))
			skb->ip_summed = CHECKSUM_UNNECESSARY;
		else
530
			skb_checksum_none_assert(skb);
L
Linus Torvalds 已提交
531 532 533

		skb_put(skb, len);

534
		mapping = dma_map_single(&cp->pdev->dev, new_skb->data, buflen,
F
Francois Romieu 已提交
535
					 PCI_DMA_FROMDEVICE);
536
		cp->rx_skb[rx_tail] = new_skb;
L
Linus Torvalds 已提交
537 538 539 540 541 542 543 544 545 546 547 548 549 550

		cp_rx_skb(cp, skb, desc);
		rx++;

rx_next:
		cp->rx_ring[rx_tail].opts2 = 0;
		cp->rx_ring[rx_tail].addr = cpu_to_le64(mapping);
		if (rx_tail == (CP_RX_RING_SIZE - 1))
			desc->opts1 = cpu_to_le32(DescOwn | RingEnd |
						  cp->rx_buf_sz);
		else
			desc->opts1 = cpu_to_le32(DescOwn | cp->rx_buf_sz);
		rx_tail = NEXT_RX(rx_tail);

551
		if (rx >= budget)
L
Linus Torvalds 已提交
552 553 554 555 556 557 558 559
			break;
	}

	cp->rx_tail = rx_tail;

	/* if we did not reach work limit, then we're done with
	 * this round of polling
	 */
560
	if (rx < budget) {
561 562
		unsigned long flags;

L
Linus Torvalds 已提交
563 564 565
		if (cpr16(IntrStatus) & cp_rx_intr_mask)
			goto rx_status_loop;

566
		napi_gro_flush(napi, false);
567
		spin_lock_irqsave(&cp->lock, flags);
568
		__napi_complete(napi);
569
		cpw16_f(IntrMask, cp_intr_mask);
570
		spin_unlock_irqrestore(&cp->lock, flags);
L
Linus Torvalds 已提交
571 572
	}

573
	return rx;
L
Linus Torvalds 已提交
574 575
}

576
static irqreturn_t cp_interrupt (int irq, void *dev_instance)
L
Linus Torvalds 已提交
577 578 579
{
	struct net_device *dev = dev_instance;
	struct cp_private *cp;
580
	int handled = 0;
L
Linus Torvalds 已提交
581 582 583 584 585 586
	u16 status;

	if (unlikely(dev == NULL))
		return IRQ_NONE;
	cp = netdev_priv(dev);

587 588
	spin_lock(&cp->lock);

L
Linus Torvalds 已提交
589 590
	status = cpr16(IntrStatus);
	if (!status || (status == 0xFFFF))
591 592 593
		goto out_unlock;

	handled = 1;
L
Linus Torvalds 已提交
594

595 596
	netif_dbg(cp, intr, dev, "intr, status %04x cmd %02x cpcmd %04x\n",
		  status, cpr8(Cmd), cpr16(CpCmd));
L
Linus Torvalds 已提交
597 598 599 600 601 602

	cpw16(IntrStatus, status & ~cp_rx_intr_mask);

	/* close possible race's with dev_close */
	if (unlikely(!netif_running(dev))) {
		cpw16(IntrMask, 0);
603
		goto out_unlock;
L
Linus Torvalds 已提交
604 605 606
	}

	if (status & (RxOK | RxErr | RxEmpty | RxFIFOOvr))
607
		if (napi_schedule_prep(&cp->napi)) {
L
Linus Torvalds 已提交
608
			cpw16_f(IntrMask, cp_norx_intr_mask);
609
			__napi_schedule(&cp->napi);
L
Linus Torvalds 已提交
610 611 612 613 614
		}

	if (status & (TxOK | TxErr | TxEmpty | SWInt))
		cp_tx(cp);
	if (status & LinkChg)
615
		mii_check_media(&cp->mii_if, netif_msg_link(cp), false);
L
Linus Torvalds 已提交
616 617 618 619 620 621 622


	if (status & PciErr) {
		u16 pci_status;

		pci_read_config_word(cp->pdev, PCI_STATUS, &pci_status);
		pci_write_config_word(cp->pdev, PCI_STATUS, pci_status);
623 624
		netdev_err(dev, "PCI bus error, status=%04x, PCI status=%04x\n",
			   status, pci_status);
L
Linus Torvalds 已提交
625 626 627 628

		/* TODO: reset hardware */
	}

629 630 631 632
out_unlock:
	spin_unlock(&cp->lock);

	return IRQ_RETVAL(handled);
L
Linus Torvalds 已提交
633 634
}

635 636 637 638 639 640 641
#ifdef CONFIG_NET_POLL_CONTROLLER
/*
 * Polling receive - used by netconsole and other diagnostic tools
 * to allow network i/o with interrupts disabled.
 */
static void cp_poll_controller(struct net_device *dev)
{
642 643 644 645 646 647
	struct cp_private *cp = netdev_priv(dev);
	const int irq = cp->pdev->irq;

	disable_irq(irq);
	cp_interrupt(irq, dev);
	enable_irq(irq);
648 649 650
}
#endif

L
Linus Torvalds 已提交
651 652 653 654
static void cp_tx (struct cp_private *cp)
{
	unsigned tx_head = cp->tx_head;
	unsigned tx_tail = cp->tx_tail;
D
David Woodhouse 已提交
655
	unsigned bytes_compl = 0, pkts_compl = 0;
L
Linus Torvalds 已提交
656 657

	while (tx_tail != tx_head) {
F
Francois Romieu 已提交
658
		struct cp_desc *txd = cp->tx_ring + tx_tail;
L
Linus Torvalds 已提交
659 660 661 662
		struct sk_buff *skb;
		u32 status;

		rmb();
F
Francois Romieu 已提交
663
		status = le32_to_cpu(txd->opts1);
L
Linus Torvalds 已提交
664 665 666
		if (status & DescOwn)
			break;

667
		skb = cp->tx_skb[tx_tail];
668
		BUG_ON(!skb);
L
Linus Torvalds 已提交
669

670
		dma_unmap_single(&cp->pdev->dev, le64_to_cpu(txd->addr),
671 672
				 le32_to_cpu(txd->opts1) & 0xffff,
				 PCI_DMA_TODEVICE);
L
Linus Torvalds 已提交
673

D
David Woodhouse 已提交
674 675 676
		bytes_compl += skb->len;
		pkts_compl++;

L
Linus Torvalds 已提交
677 678
		if (status & LastFrag) {
			if (status & (TxError | TxFIFOUnder)) {
679 680
				netif_dbg(cp, tx_err, cp->dev,
					  "tx err, status 0x%x\n", status);
681
				cp->dev->stats.tx_errors++;
L
Linus Torvalds 已提交
682
				if (status & TxOWC)
683
					cp->dev->stats.tx_window_errors++;
L
Linus Torvalds 已提交
684
				if (status & TxMaxCol)
685
					cp->dev->stats.tx_aborted_errors++;
L
Linus Torvalds 已提交
686
				if (status & TxLinkFail)
687
					cp->dev->stats.tx_carrier_errors++;
L
Linus Torvalds 已提交
688
				if (status & TxFIFOUnder)
689
					cp->dev->stats.tx_fifo_errors++;
L
Linus Torvalds 已提交
690
			} else {
691
				cp->dev->stats.collisions +=
L
Linus Torvalds 已提交
692
					((status >> TxColCntShift) & TxColCntMask);
693 694
				cp->dev->stats.tx_packets++;
				cp->dev->stats.tx_bytes += skb->len;
695 696
				netif_dbg(cp, tx_done, cp->dev,
					  "tx done, slot %d\n", tx_tail);
L
Linus Torvalds 已提交
697 698 699 700
			}
			dev_kfree_skb_irq(skb);
		}

701
		cp->tx_skb[tx_tail] = NULL;
L
Linus Torvalds 已提交
702 703 704 705 706 707

		tx_tail = NEXT_TX(tx_tail);
	}

	cp->tx_tail = tx_tail;

D
David Woodhouse 已提交
708
	netdev_completed_queue(cp->dev, pkts_compl, bytes_compl);
L
Linus Torvalds 已提交
709 710 711 712
	if (TX_BUFFS_AVAIL(cp) > (MAX_SKB_FRAGS + 1))
		netif_wake_queue(cp->dev);
}

713 714 715 716 717 718
static inline u32 cp_tx_vlan_tag(struct sk_buff *skb)
{
	return vlan_tx_tag_present(skb) ?
		TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
}

719 720
static netdev_tx_t cp_start_xmit (struct sk_buff *skb,
					struct net_device *dev)
L
Linus Torvalds 已提交
721 722 723
{
	struct cp_private *cp = netdev_priv(dev);
	unsigned entry;
J
Jeff Garzik 已提交
724
	u32 eor, flags;
725
	unsigned long intr_flags;
726
	__le32 opts2;
J
Jeff Garzik 已提交
727
	int mss = 0;
L
Linus Torvalds 已提交
728

729
	spin_lock_irqsave(&cp->lock, intr_flags);
L
Linus Torvalds 已提交
730 731 732 733

	/* This is a hard error, log it. */
	if (TX_BUFFS_AVAIL(cp) <= (skb_shinfo(skb)->nr_frags + 1)) {
		netif_stop_queue(dev);
734
		spin_unlock_irqrestore(&cp->lock, intr_flags);
735
		netdev_err(dev, "BUG! Tx Ring full when queue awake!\n");
736
		return NETDEV_TX_BUSY;
L
Linus Torvalds 已提交
737 738 739 740
	}

	entry = cp->tx_head;
	eor = (entry == (CP_TX_RING_SIZE - 1)) ? RingEnd : 0;
741
	mss = skb_shinfo(skb)->gso_size;
J
Jeff Garzik 已提交
742

743 744
	opts2 = cpu_to_le32(cp_tx_vlan_tag(skb));

L
Linus Torvalds 已提交
745 746 747 748 749 750
	if (skb_shinfo(skb)->nr_frags == 0) {
		struct cp_desc *txd = &cp->tx_ring[entry];
		u32 len;
		dma_addr_t mapping;

		len = skb->len;
751
		mapping = dma_map_single(&cp->pdev->dev, skb->data, len, PCI_DMA_TODEVICE);
752
		txd->opts2 = opts2;
L
Linus Torvalds 已提交
753 754 755
		txd->addr = cpu_to_le64(mapping);
		wmb();

J
Jeff Garzik 已提交
756 757 758 759
		flags = eor | len | DescOwn | FirstFrag | LastFrag;

		if (mss)
			flags |= LargeSend | ((mss & MSSMask) << MSSShift);
760
		else if (skb->ip_summed == CHECKSUM_PARTIAL) {
761
			const struct iphdr *ip = ip_hdr(skb);
L
Linus Torvalds 已提交
762
			if (ip->protocol == IPPROTO_TCP)
J
Jeff Garzik 已提交
763
				flags |= IPCS | TCPCS;
L
Linus Torvalds 已提交
764
			else if (ip->protocol == IPPROTO_UDP)
J
Jeff Garzik 已提交
765
				flags |= IPCS | UDPCS;
L
Linus Torvalds 已提交
766
			else
767
				WARN_ON(1);	/* we need a WARN() */
J
Jeff Garzik 已提交
768 769 770
		}

		txd->opts1 = cpu_to_le32(flags);
L
Linus Torvalds 已提交
771 772
		wmb();

773
		cp->tx_skb[entry] = skb;
L
Linus Torvalds 已提交
774 775 776 777 778 779
		entry = NEXT_TX(entry);
	} else {
		struct cp_desc *txd;
		u32 first_len, first_eor;
		dma_addr_t first_mapping;
		int frag, first_entry = entry;
780
		const struct iphdr *ip = ip_hdr(skb);
L
Linus Torvalds 已提交
781 782 783 784 785 786

		/* We must give this initial chunk to the device last.
		 * Otherwise we could race with the device.
		 */
		first_eor = eor;
		first_len = skb_headlen(skb);
787
		first_mapping = dma_map_single(&cp->pdev->dev, skb->data,
L
Linus Torvalds 已提交
788
					       first_len, PCI_DMA_TODEVICE);
789
		cp->tx_skb[entry] = skb;
L
Linus Torvalds 已提交
790 791 792
		entry = NEXT_TX(entry);

		for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
E
Eric Dumazet 已提交
793
			const skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
L
Linus Torvalds 已提交
794 795 796 797
			u32 len;
			u32 ctrl;
			dma_addr_t mapping;

E
Eric Dumazet 已提交
798
			len = skb_frag_size(this_frag);
799
			mapping = dma_map_single(&cp->pdev->dev,
800
						 skb_frag_address(this_frag),
L
Linus Torvalds 已提交
801 802 803
						 len, PCI_DMA_TODEVICE);
			eor = (entry == (CP_TX_RING_SIZE - 1)) ? RingEnd : 0;

J
Jeff Garzik 已提交
804 805 806 807 808
			ctrl = eor | len | DescOwn;

			if (mss)
				ctrl |= LargeSend |
					((mss & MSSMask) << MSSShift);
809
			else if (skb->ip_summed == CHECKSUM_PARTIAL) {
L
Linus Torvalds 已提交
810
				if (ip->protocol == IPPROTO_TCP)
J
Jeff Garzik 已提交
811
					ctrl |= IPCS | TCPCS;
L
Linus Torvalds 已提交
812
				else if (ip->protocol == IPPROTO_UDP)
J
Jeff Garzik 已提交
813
					ctrl |= IPCS | UDPCS;
L
Linus Torvalds 已提交
814 815
				else
					BUG();
J
Jeff Garzik 已提交
816
			}
L
Linus Torvalds 已提交
817 818 819 820 821

			if (frag == skb_shinfo(skb)->nr_frags - 1)
				ctrl |= LastFrag;

			txd = &cp->tx_ring[entry];
822
			txd->opts2 = opts2;
L
Linus Torvalds 已提交
823 824 825 826 827 828
			txd->addr = cpu_to_le64(mapping);
			wmb();

			txd->opts1 = cpu_to_le32(ctrl);
			wmb();

829
			cp->tx_skb[entry] = skb;
L
Linus Torvalds 已提交
830 831 832 833
			entry = NEXT_TX(entry);
		}

		txd = &cp->tx_ring[first_entry];
834
		txd->opts2 = opts2;
L
Linus Torvalds 已提交
835 836 837
		txd->addr = cpu_to_le64(first_mapping);
		wmb();

838
		if (skb->ip_summed == CHECKSUM_PARTIAL) {
L
Linus Torvalds 已提交
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
			if (ip->protocol == IPPROTO_TCP)
				txd->opts1 = cpu_to_le32(first_eor | first_len |
							 FirstFrag | DescOwn |
							 IPCS | TCPCS);
			else if (ip->protocol == IPPROTO_UDP)
				txd->opts1 = cpu_to_le32(first_eor | first_len |
							 FirstFrag | DescOwn |
							 IPCS | UDPCS);
			else
				BUG();
		} else
			txd->opts1 = cpu_to_le32(first_eor | first_len |
						 FirstFrag | DescOwn);
		wmb();
	}
	cp->tx_head = entry;
D
David Woodhouse 已提交
855 856

	netdev_sent_queue(dev, skb->len);
857 858
	netif_dbg(cp, tx_queued, cp->dev, "tx queued, slot %d, skblen %d\n",
		  entry, skb->len);
L
Linus Torvalds 已提交
859 860 861
	if (TX_BUFFS_AVAIL(cp) <= (MAX_SKB_FRAGS + 1))
		netif_stop_queue(dev);

862
	spin_unlock_irqrestore(&cp->lock, intr_flags);
L
Linus Torvalds 已提交
863 864 865

	cpw8(TxPoll, NormalTxPoll);

866
	return NETDEV_TX_OK;
L
Linus Torvalds 已提交
867 868 869 870 871 872 873 874 875
}

/* Set or clear the multicast filter for this adaptor.
   This routine is not state sensitive and need not be SMP locked. */

static void __cp_set_rx_mode (struct net_device *dev)
{
	struct cp_private *cp = netdev_priv(dev);
	u32 mc_filter[2];	/* Multicast hash filter */
876
	int rx_mode;
L
Linus Torvalds 已提交
877 878 879 880 881 882 883 884

	/* Note: do not reorder, GCC is clever about common statements. */
	if (dev->flags & IFF_PROMISC) {
		/* Unconditionally log net taps. */
		rx_mode =
		    AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
		    AcceptAllPhys;
		mc_filter[1] = mc_filter[0] = 0xffffffff;
885
	} else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
886
		   (dev->flags & IFF_ALLMULTI)) {
L
Linus Torvalds 已提交
887 888 889 890
		/* Too many to filter perfectly -- accept all multicasts. */
		rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
		mc_filter[1] = mc_filter[0] = 0xffffffff;
	} else {
891
		struct netdev_hw_addr *ha;
L
Linus Torvalds 已提交
892 893
		rx_mode = AcceptBroadcast | AcceptMyPhys;
		mc_filter[1] = mc_filter[0] = 0;
894 895
		netdev_for_each_mc_addr(ha, dev) {
			int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
L
Linus Torvalds 已提交
896 897 898 899 900 901 902

			mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
			rx_mode |= AcceptMulticast;
		}
	}

	/* We can safely update without stopping the chip. */
903 904 905
	cp->rx_config = cp_rx_config | rx_mode;
	cpw32_f(RxConfig, cp->rx_config);

L
Linus Torvalds 已提交
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
	cpw32_f (MAR0 + 0, mc_filter[0]);
	cpw32_f (MAR0 + 4, mc_filter[1]);
}

static void cp_set_rx_mode (struct net_device *dev)
{
	unsigned long flags;
	struct cp_private *cp = netdev_priv(dev);

	spin_lock_irqsave (&cp->lock, flags);
	__cp_set_rx_mode(dev);
	spin_unlock_irqrestore (&cp->lock, flags);
}

static void __cp_get_stats(struct cp_private *cp)
{
	/* only lower 24 bits valid; write any value to clear */
923
	cp->dev->stats.rx_missed_errors += (cpr32 (RxMissed) & 0xffffff);
L
Linus Torvalds 已提交
924 925 926 927 928 929 930 931 932 933 934 935 936 937
	cpw32 (RxMissed, 0);
}

static struct net_device_stats *cp_get_stats(struct net_device *dev)
{
	struct cp_private *cp = netdev_priv(dev);
	unsigned long flags;

	/* The chip only need report frame silently dropped. */
	spin_lock_irqsave(&cp->lock, flags);
 	if (netif_running(dev) && netif_device_present(dev))
 		__cp_get_stats(cp);
	spin_unlock_irqrestore(&cp->lock, flags);

938
	return &dev->stats;
L
Linus Torvalds 已提交
939 940 941 942 943 944 945 946 947 948 949 950
}

static void cp_stop_hw (struct cp_private *cp)
{
	cpw16(IntrStatus, ~(cpr16(IntrStatus)));
	cpw16_f(IntrMask, 0);
	cpw8(Cmd, 0);
	cpw16_f(CpCmd, 0);
	cpw16_f(IntrStatus, ~(cpr16(IntrStatus)));

	cp->rx_tail = 0;
	cp->tx_head = cp->tx_tail = 0;
D
David Woodhouse 已提交
951 952

	netdev_reset_queue(cp->dev);
L
Linus Torvalds 已提交
953 954 955 956 957 958 959 960 961 962 963 964
}

static void cp_reset_hw (struct cp_private *cp)
{
	unsigned work = 1000;

	cpw8(Cmd, CmdReset);

	while (work--) {
		if (!(cpr8(Cmd) & CmdReset))
			return;

965
		schedule_timeout_uninterruptible(10);
L
Linus Torvalds 已提交
966 967
	}

968
	netdev_err(cp->dev, "hardware reset timeout\n");
L
Linus Torvalds 已提交
969 970 971 972
}

static inline void cp_start_hw (struct cp_private *cp)
{
973 974
	dma_addr_t ring_dma;

L
Linus Torvalds 已提交
975
	cpw16(CpCmd, cp->cpcmd);
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001

	/*
	 * These (at least TxRingAddr) need to be configured after the
	 * corresponding bits in CpCmd are enabled. Datasheet v1.6 §6.33
	 * (C+ Command Register) recommends that these and more be configured
	 * *after* the [RT]xEnable bits in CpCmd are set. And on some hardware
	 * it's been observed that the TxRingAddr is actually reset to garbage
	 * when C+ mode Tx is enabled in CpCmd.
	 */
	cpw32_f(HiTxRingAddr, 0);
	cpw32_f(HiTxRingAddr + 4, 0);

	ring_dma = cp->ring_dma;
	cpw32_f(RxRingAddr, ring_dma & 0xffffffff);
	cpw32_f(RxRingAddr + 4, (ring_dma >> 16) >> 16);

	ring_dma += sizeof(struct cp_desc) * CP_RX_RING_SIZE;
	cpw32_f(TxRingAddr, ring_dma & 0xffffffff);
	cpw32_f(TxRingAddr + 4, (ring_dma >> 16) >> 16);

	/*
	 * Strictly speaking, the datasheet says this should be enabled
	 * *before* setting the descriptor addresses. But what, then, would
	 * prevent it from doing DMA to random unconfigured addresses?
	 * This variant appears to work fine.
	 */
L
Linus Torvalds 已提交
1002
	cpw8(Cmd, RxOn | TxOn);
D
David Woodhouse 已提交
1003 1004

	netdev_reset_queue(cp->dev);
L
Linus Torvalds 已提交
1005 1006
}

1007 1008 1009 1010 1011
static void cp_enable_irq(struct cp_private *cp)
{
	cpw16_f(IntrMask, cp_intr_mask);
}

L
Linus Torvalds 已提交
1012 1013 1014 1015 1016 1017 1018 1019 1020
static void cp_init_hw (struct cp_private *cp)
{
	struct net_device *dev = cp->dev;

	cp_reset_hw(cp);

	cpw8_f (Cfg9346, Cfg9346_Unlock);

	/* Restore our idea of the MAC address. */
A
Al Viro 已提交
1021 1022
	cpw32_f (MAC0 + 0, le32_to_cpu (*(__le32 *) (dev->dev_addr + 0)));
	cpw32_f (MAC0 + 4, le32_to_cpu (*(__le32 *) (dev->dev_addr + 4)));
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034

	cp_start_hw(cp);
	cpw8(TxThresh, 0x06); /* XXX convert magic num to a constant */

	__cp_set_rx_mode(dev);
	cpw32_f (TxConfig, IFG | (TX_DMA_BURST << TxDMAShift));

	cpw8(Config1, cpr8(Config1) | DriverLoaded | PMEnable);
	/* Disable Wake-on-LAN. Can be turned on with ETHTOOL_SWOL */
	cpw8(Config3, PARMEnable);
	cp->wol_enabled = 0;

1035
	cpw8(Config5, cpr8(Config5) & PMEStatus);
L
Linus Torvalds 已提交
1036 1037 1038 1039 1040 1041

	cpw16(MultiIntr, 0);

	cpw8_f(Cfg9346, Cfg9346_Lock);
}

K
Kevin Lo 已提交
1042
static int cp_refill_rx(struct cp_private *cp)
L
Linus Torvalds 已提交
1043
{
K
Kevin Lo 已提交
1044
	struct net_device *dev = cp->dev;
L
Linus Torvalds 已提交
1045 1046 1047 1048
	unsigned i;

	for (i = 0; i < CP_RX_RING_SIZE; i++) {
		struct sk_buff *skb;
F
Francois Romieu 已提交
1049
		dma_addr_t mapping;
L
Linus Torvalds 已提交
1050

1051
		skb = netdev_alloc_skb_ip_align(dev, cp->rx_buf_sz);
L
Linus Torvalds 已提交
1052 1053 1054
		if (!skb)
			goto err_out;

1055 1056
		mapping = dma_map_single(&cp->pdev->dev, skb->data,
					 cp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1057
		cp->rx_skb[i] = skb;
L
Linus Torvalds 已提交
1058 1059

		cp->rx_ring[i].opts2 = 0;
F
Francois Romieu 已提交
1060
		cp->rx_ring[i].addr = cpu_to_le64(mapping);
L
Linus Torvalds 已提交
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
		if (i == (CP_RX_RING_SIZE - 1))
			cp->rx_ring[i].opts1 =
				cpu_to_le32(DescOwn | RingEnd | cp->rx_buf_sz);
		else
			cp->rx_ring[i].opts1 =
				cpu_to_le32(DescOwn | cp->rx_buf_sz);
	}

	return 0;

err_out:
	cp_clean_rings(cp);
	return -ENOMEM;
}

1076 1077 1078 1079 1080 1081
static void cp_init_rings_index (struct cp_private *cp)
{
	cp->rx_tail = 0;
	cp->tx_head = cp->tx_tail = 0;
}

L
Linus Torvalds 已提交
1082 1083 1084 1085 1086
static int cp_init_rings (struct cp_private *cp)
{
	memset(cp->tx_ring, 0, sizeof(struct cp_desc) * CP_TX_RING_SIZE);
	cp->tx_ring[CP_TX_RING_SIZE - 1].opts1 = cpu_to_le32(RingEnd);

1087
	cp_init_rings_index(cp);
L
Linus Torvalds 已提交
1088 1089 1090 1091 1092 1093

	return cp_refill_rx (cp);
}

static int cp_alloc_rings (struct cp_private *cp)
{
1094
	struct device *d = &cp->pdev->dev;
L
Linus Torvalds 已提交
1095
	void *mem;
1096
	int rc;
L
Linus Torvalds 已提交
1097

1098
	mem = dma_alloc_coherent(d, CP_RING_BYTES, &cp->ring_dma, GFP_KERNEL);
L
Linus Torvalds 已提交
1099 1100 1101 1102 1103 1104
	if (!mem)
		return -ENOMEM;

	cp->rx_ring = mem;
	cp->tx_ring = &cp->rx_ring[CP_RX_RING_SIZE];

1105 1106 1107 1108 1109
	rc = cp_init_rings(cp);
	if (rc < 0)
		dma_free_coherent(d, CP_RING_BYTES, cp->rx_ring, cp->ring_dma);

	return rc;
L
Linus Torvalds 已提交
1110 1111 1112 1113
}

static void cp_clean_rings (struct cp_private *cp)
{
F
Francois Romieu 已提交
1114
	struct cp_desc *desc;
L
Linus Torvalds 已提交
1115 1116 1117
	unsigned i;

	for (i = 0; i < CP_RX_RING_SIZE; i++) {
1118
		if (cp->rx_skb[i]) {
F
Francois Romieu 已提交
1119
			desc = cp->rx_ring + i;
1120
			dma_unmap_single(&cp->pdev->dev,le64_to_cpu(desc->addr),
L
Linus Torvalds 已提交
1121
					 cp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1122
			dev_kfree_skb(cp->rx_skb[i]);
L
Linus Torvalds 已提交
1123 1124 1125 1126
		}
	}

	for (i = 0; i < CP_TX_RING_SIZE; i++) {
1127 1128
		if (cp->tx_skb[i]) {
			struct sk_buff *skb = cp->tx_skb[i];
1129

F
Francois Romieu 已提交
1130
			desc = cp->tx_ring + i;
1131
			dma_unmap_single(&cp->pdev->dev,le64_to_cpu(desc->addr),
1132 1133
					 le32_to_cpu(desc->opts1) & 0xffff,
					 PCI_DMA_TODEVICE);
F
Francois Romieu 已提交
1134
			if (le32_to_cpu(desc->opts1) & LastFrag)
1135
				dev_kfree_skb(skb);
1136
			cp->dev->stats.tx_dropped++;
L
Linus Torvalds 已提交
1137 1138 1139
		}
	}

1140 1141 1142
	memset(cp->rx_ring, 0, sizeof(struct cp_desc) * CP_RX_RING_SIZE);
	memset(cp->tx_ring, 0, sizeof(struct cp_desc) * CP_TX_RING_SIZE);

1143
	memset(cp->rx_skb, 0, sizeof(struct sk_buff *) * CP_RX_RING_SIZE);
1144
	memset(cp->tx_skb, 0, sizeof(struct sk_buff *) * CP_TX_RING_SIZE);
L
Linus Torvalds 已提交
1145 1146 1147 1148 1149
}

static void cp_free_rings (struct cp_private *cp)
{
	cp_clean_rings(cp);
1150 1151
	dma_free_coherent(&cp->pdev->dev, CP_RING_BYTES, cp->rx_ring,
			  cp->ring_dma);
L
Linus Torvalds 已提交
1152 1153 1154 1155 1156 1157 1158
	cp->rx_ring = NULL;
	cp->tx_ring = NULL;
}

static int cp_open (struct net_device *dev)
{
	struct cp_private *cp = netdev_priv(dev);
1159
	const int irq = cp->pdev->irq;
L
Linus Torvalds 已提交
1160 1161
	int rc;

1162
	netif_dbg(cp, ifup, dev, "enabling interface\n");
L
Linus Torvalds 已提交
1163 1164 1165 1166 1167

	rc = cp_alloc_rings(cp);
	if (rc)
		return rc;

1168 1169
	napi_enable(&cp->napi);

L
Linus Torvalds 已提交
1170 1171
	cp_init_hw(cp);

1172
	rc = request_irq(irq, cp_interrupt, IRQF_SHARED, dev->name, dev);
L
Linus Torvalds 已提交
1173 1174 1175
	if (rc)
		goto err_out_hw;

1176 1177
	cp_enable_irq(cp);

L
Linus Torvalds 已提交
1178
	netif_carrier_off(dev);
1179
	mii_check_media(&cp->mii_if, netif_msg_link(cp), true);
L
Linus Torvalds 已提交
1180 1181 1182 1183 1184
	netif_start_queue(dev);

	return 0;

err_out_hw:
1185
	napi_disable(&cp->napi);
L
Linus Torvalds 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
	cp_stop_hw(cp);
	cp_free_rings(cp);
	return rc;
}

static int cp_close (struct net_device *dev)
{
	struct cp_private *cp = netdev_priv(dev);
	unsigned long flags;

1196 1197
	napi_disable(&cp->napi);

1198
	netif_dbg(cp, ifdown, dev, "disabling interface\n");
L
Linus Torvalds 已提交
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208

	spin_lock_irqsave(&cp->lock, flags);

	netif_stop_queue(dev);
	netif_carrier_off(dev);

	cp_stop_hw(cp);

	spin_unlock_irqrestore(&cp->lock, flags);

1209
	free_irq(cp->pdev->irq, dev);
L
Linus Torvalds 已提交
1210 1211 1212 1213 1214

	cp_free_rings(cp);
	return 0;
}

1215 1216 1217 1218 1219 1220
static void cp_tx_timeout(struct net_device *dev)
{
	struct cp_private *cp = netdev_priv(dev);
	unsigned long flags;
	int rc;

1221 1222 1223
	netdev_warn(dev, "Transmit timeout, status %2x %4x %4x %4x\n",
		    cpr8(Cmd), cpr16(CpCmd),
		    cpr16(IntrStatus), cpr16(IntrMask));
1224 1225 1226 1227 1228 1229 1230

	spin_lock_irqsave(&cp->lock, flags);

	cp_stop_hw(cp);
	cp_clean_rings(cp);
	rc = cp_init_rings(cp);
	cp_start_hw(cp);
1231
	cp_enable_irq(cp);
1232 1233 1234 1235 1236 1237

	netif_wake_queue(dev);

	spin_unlock_irqrestore(&cp->lock, flags);
}

L
Linus Torvalds 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
static int cp_change_mtu(struct net_device *dev, int new_mtu)
{
	struct cp_private *cp = netdev_priv(dev);

	/* check for invalid MTU, according to hardware limits */
	if (new_mtu < CP_MIN_MTU || new_mtu > CP_MAX_MTU)
		return -EINVAL;

	/* if network interface not up, no need for complexity */
	if (!netif_running(dev)) {
		dev->mtu = new_mtu;
		cp_set_rxbufsize(cp);	/* set new rx buf size */
		return 0;
	}

1253 1254
	/* network IS up, close it, reset MTU, and come up again. */
	cp_close(dev);
L
Linus Torvalds 已提交
1255
	dev->mtu = new_mtu;
1256 1257
	cp_set_rxbufsize(cp);
	return cp_open(dev);
L
Linus Torvalds 已提交
1258 1259
}

1260
static const char mii_2_8139_map[8] = {
L
Linus Torvalds 已提交
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	BasicModeCtrl,
	BasicModeStatus,
	0,
	0,
	NWayAdvert,
	NWayLPAR,
	NWayExpansion,
	0
};

static int mdio_read(struct net_device *dev, int phy_id, int location)
{
	struct cp_private *cp = netdev_priv(dev);

	return location < 8 && mii_2_8139_map[location] ?
	       readw(cp->regs + mii_2_8139_map[location]) : 0;
}


static void mdio_write(struct net_device *dev, int phy_id, int location,
		       int value)
{
	struct cp_private *cp = netdev_priv(dev);

	if (location == 0) {
		cpw8(Cfg9346, Cfg9346_Unlock);
		cpw16(BasicModeCtrl, value);
		cpw8(Cfg9346, Cfg9346_Lock);
	} else if (location < 8 && mii_2_8139_map[location])
		cpw16(mii_2_8139_map[location], value);
}

/* Set the ethtool Wake-on-LAN settings */
static int netdev_set_wol (struct cp_private *cp,
			   const struct ethtool_wolinfo *wol)
{
	u8 options;

	options = cpr8 (Config3) & ~(LinkUp | MagicPacket);
	/* If WOL is being disabled, no need for complexity */
	if (wol->wolopts) {
		if (wol->wolopts & WAKE_PHY)	options |= LinkUp;
		if (wol->wolopts & WAKE_MAGIC)	options |= MagicPacket;
	}

	cpw8 (Cfg9346, Cfg9346_Unlock);
	cpw8 (Config3, options);
	cpw8 (Cfg9346, Cfg9346_Lock);

	options = 0; /* Paranoia setting */
	options = cpr8 (Config5) & ~(UWF | MWF | BWF);
	/* If WOL is being disabled, no need for complexity */
	if (wol->wolopts) {
		if (wol->wolopts & WAKE_UCAST)  options |= UWF;
		if (wol->wolopts & WAKE_BCAST)	options |= BWF;
		if (wol->wolopts & WAKE_MCAST)	options |= MWF;
	}

	cpw8 (Config5, options);

	cp->wol_enabled = (wol->wolopts) ? 1 : 0;

	return 0;
}

/* Get the ethtool Wake-on-LAN settings */
static void netdev_get_wol (struct cp_private *cp,
	             struct ethtool_wolinfo *wol)
{
	u8 options;

	wol->wolopts   = 0; /* Start from scratch */
	wol->supported = WAKE_PHY   | WAKE_BCAST | WAKE_MAGIC |
		         WAKE_MCAST | WAKE_UCAST;
	/* We don't need to go on if WOL is disabled */
	if (!cp->wol_enabled) return;
1337

L
Linus Torvalds 已提交
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
	options        = cpr8 (Config3);
	if (options & LinkUp)        wol->wolopts |= WAKE_PHY;
	if (options & MagicPacket)   wol->wolopts |= WAKE_MAGIC;

	options        = 0; /* Paranoia setting */
	options        = cpr8 (Config5);
	if (options & UWF)           wol->wolopts |= WAKE_UCAST;
	if (options & BWF)           wol->wolopts |= WAKE_BCAST;
	if (options & MWF)           wol->wolopts |= WAKE_MCAST;
}

static void cp_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
{
	struct cp_private *cp = netdev_priv(dev);

1353 1354 1355
	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
	strlcpy(info->bus_info, pci_name(cp->pdev), sizeof(info->bus_info));
L
Linus Torvalds 已提交
1356 1357
}

R
Rick Jones 已提交
1358 1359 1360 1361 1362 1363 1364 1365 1366
static void cp_get_ringparam(struct net_device *dev,
				struct ethtool_ringparam *ring)
{
	ring->rx_max_pending = CP_RX_RING_SIZE;
	ring->tx_max_pending = CP_TX_RING_SIZE;
	ring->rx_pending = CP_RX_RING_SIZE;
	ring->tx_pending = CP_TX_RING_SIZE;
}

L
Linus Torvalds 已提交
1367 1368 1369 1370 1371
static int cp_get_regs_len(struct net_device *dev)
{
	return CP_REGS_SIZE;
}

1372
static int cp_get_sset_count (struct net_device *dev, int sset)
L
Linus Torvalds 已提交
1373
{
1374 1375 1376 1377 1378 1379
	switch (sset) {
	case ETH_SS_STATS:
		return CP_NUM_STATS;
	default:
		return -EOPNOTSUPP;
	}
L
Linus Torvalds 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
}

static int cp_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct cp_private *cp = netdev_priv(dev);
	int rc;
	unsigned long flags;

	spin_lock_irqsave(&cp->lock, flags);
	rc = mii_ethtool_gset(&cp->mii_if, cmd);
	spin_unlock_irqrestore(&cp->lock, flags);

	return rc;
}

static int cp_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
{
	struct cp_private *cp = netdev_priv(dev);
	int rc;
	unsigned long flags;

	spin_lock_irqsave(&cp->lock, flags);
	rc = mii_ethtool_sset(&cp->mii_if, cmd);
	spin_unlock_irqrestore(&cp->lock, flags);

	return rc;
}

static int cp_nway_reset(struct net_device *dev)
{
	struct cp_private *cp = netdev_priv(dev);
	return mii_nway_restart(&cp->mii_if);
}

static u32 cp_get_msglevel(struct net_device *dev)
{
	struct cp_private *cp = netdev_priv(dev);
	return cp->msg_enable;
}

static void cp_set_msglevel(struct net_device *dev, u32 value)
{
	struct cp_private *cp = netdev_priv(dev);
	cp->msg_enable = value;
}

1426
static int cp_set_features(struct net_device *dev, netdev_features_t features)
L
Linus Torvalds 已提交
1427 1428
{
	struct cp_private *cp = netdev_priv(dev);
1429
	unsigned long flags;
L
Linus Torvalds 已提交
1430

1431 1432
	if (!((dev->features ^ features) & NETIF_F_RXCSUM))
		return 0;
L
Linus Torvalds 已提交
1433

1434
	spin_lock_irqsave(&cp->lock, flags);
L
Linus Torvalds 已提交
1435

1436 1437
	if (features & NETIF_F_RXCSUM)
		cp->cpcmd |= RxChkSum;
L
Linus Torvalds 已提交
1438
	else
1439
		cp->cpcmd &= ~RxChkSum;
L
Linus Torvalds 已提交
1440

1441
	if (features & NETIF_F_HW_VLAN_CTAG_RX)
1442 1443 1444 1445
		cp->cpcmd |= RxVlanOn;
	else
		cp->cpcmd &= ~RxVlanOn;

1446 1447
	cpw16_f(CpCmd, cp->cpcmd);
	spin_unlock_irqrestore(&cp->lock, flags);
L
Linus Torvalds 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506

	return 0;
}

static void cp_get_regs(struct net_device *dev, struct ethtool_regs *regs,
		        void *p)
{
	struct cp_private *cp = netdev_priv(dev);
	unsigned long flags;

	if (regs->len < CP_REGS_SIZE)
		return /* -EINVAL */;

	regs->version = CP_REGS_VER;

	spin_lock_irqsave(&cp->lock, flags);
	memcpy_fromio(p, cp->regs, CP_REGS_SIZE);
	spin_unlock_irqrestore(&cp->lock, flags);
}

static void cp_get_wol (struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct cp_private *cp = netdev_priv(dev);
	unsigned long flags;

	spin_lock_irqsave (&cp->lock, flags);
	netdev_get_wol (cp, wol);
	spin_unlock_irqrestore (&cp->lock, flags);
}

static int cp_set_wol (struct net_device *dev, struct ethtool_wolinfo *wol)
{
	struct cp_private *cp = netdev_priv(dev);
	unsigned long flags;
	int rc;

	spin_lock_irqsave (&cp->lock, flags);
	rc = netdev_set_wol (cp, wol);
	spin_unlock_irqrestore (&cp->lock, flags);

	return rc;
}

static void cp_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
{
	switch (stringset) {
	case ETH_SS_STATS:
		memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
		break;
	default:
		BUG();
		break;
	}
}

static void cp_get_ethtool_stats (struct net_device *dev,
				  struct ethtool_stats *estats, u64 *tmp_stats)
{
	struct cp_private *cp = netdev_priv(dev);
1507 1508
	struct cp_dma_stats *nic_stats;
	dma_addr_t dma;
L
Linus Torvalds 已提交
1509 1510
	int i;

1511 1512
	nic_stats = dma_alloc_coherent(&cp->pdev->dev, sizeof(*nic_stats),
				       &dma, GFP_KERNEL);
1513 1514
	if (!nic_stats)
		return;
1515

L
Linus Torvalds 已提交
1516
	/* begin NIC statistics dump */
1517
	cpw32(StatsAddr + 4, (u64)dma >> 32);
1518
	cpw32(StatsAddr, ((u64)dma & DMA_BIT_MASK(32)) | DumpStats);
L
Linus Torvalds 已提交
1519 1520
	cpr32(StatsAddr);

1521
	for (i = 0; i < 1000; i++) {
L
Linus Torvalds 已提交
1522 1523
		if ((cpr32(StatsAddr) & DumpStats) == 0)
			break;
1524
		udelay(10);
L
Linus Torvalds 已提交
1525
	}
1526 1527
	cpw32(StatsAddr, 0);
	cpw32(StatsAddr + 4, 0);
1528
	cpr32(StatsAddr);
L
Linus Torvalds 已提交
1529 1530

	i = 0;
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543
	tmp_stats[i++] = le64_to_cpu(nic_stats->tx_ok);
	tmp_stats[i++] = le64_to_cpu(nic_stats->rx_ok);
	tmp_stats[i++] = le64_to_cpu(nic_stats->tx_err);
	tmp_stats[i++] = le32_to_cpu(nic_stats->rx_err);
	tmp_stats[i++] = le16_to_cpu(nic_stats->rx_fifo);
	tmp_stats[i++] = le16_to_cpu(nic_stats->frame_align);
	tmp_stats[i++] = le32_to_cpu(nic_stats->tx_ok_1col);
	tmp_stats[i++] = le32_to_cpu(nic_stats->tx_ok_mcol);
	tmp_stats[i++] = le64_to_cpu(nic_stats->rx_ok_phys);
	tmp_stats[i++] = le64_to_cpu(nic_stats->rx_ok_bcast);
	tmp_stats[i++] = le32_to_cpu(nic_stats->rx_ok_mcast);
	tmp_stats[i++] = le16_to_cpu(nic_stats->tx_abort);
	tmp_stats[i++] = le16_to_cpu(nic_stats->tx_underrun);
L
Linus Torvalds 已提交
1544
	tmp_stats[i++] = cp->cp_stats.rx_frags;
1545
	BUG_ON(i != CP_NUM_STATS);
1546

1547
	dma_free_coherent(&cp->pdev->dev, sizeof(*nic_stats), nic_stats, dma);
L
Linus Torvalds 已提交
1548 1549
}

1550
static const struct ethtool_ops cp_ethtool_ops = {
L
Linus Torvalds 已提交
1551 1552
	.get_drvinfo		= cp_get_drvinfo,
	.get_regs_len		= cp_get_regs_len,
1553
	.get_sset_count		= cp_get_sset_count,
L
Linus Torvalds 已提交
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
	.get_settings		= cp_get_settings,
	.set_settings		= cp_set_settings,
	.nway_reset		= cp_nway_reset,
	.get_link		= ethtool_op_get_link,
	.get_msglevel		= cp_get_msglevel,
	.set_msglevel		= cp_set_msglevel,
	.get_regs		= cp_get_regs,
	.get_wol		= cp_get_wol,
	.set_wol		= cp_set_wol,
	.get_strings		= cp_get_strings,
	.get_ethtool_stats	= cp_get_ethtool_stats,
1565 1566 1567
	.get_eeprom_len		= cp_get_eeprom_len,
	.get_eeprom		= cp_get_eeprom,
	.set_eeprom		= cp_set_eeprom,
R
Rick Jones 已提交
1568
	.get_ringparam		= cp_get_ringparam,
L
Linus Torvalds 已提交
1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
};

static int cp_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct cp_private *cp = netdev_priv(dev);
	int rc;
	unsigned long flags;

	if (!netif_running(dev))
		return -EINVAL;

	spin_lock_irqsave(&cp->lock, flags);
	rc = generic_mii_ioctl(&cp->mii_if, if_mii(rq), cmd, NULL);
	spin_unlock_irqrestore(&cp->lock, flags);
	return rc;
}

1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607
static int cp_set_mac_address(struct net_device *dev, void *p)
{
	struct cp_private *cp = netdev_priv(dev);
	struct sockaddr *addr = p;

	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);

	spin_lock_irq(&cp->lock);

	cpw8_f(Cfg9346, Cfg9346_Unlock);
	cpw32_f(MAC0 + 0, le32_to_cpu (*(__le32 *) (dev->dev_addr + 0)));
	cpw32_f(MAC0 + 4, le32_to_cpu (*(__le32 *) (dev->dev_addr + 4)));
	cpw8_f(Cfg9346, Cfg9346_Lock);

	spin_unlock_irq(&cp->lock);

	return 0;
}

L
Linus Torvalds 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622
/* Serial EEPROM section. */

/*  EEPROM_Ctrl bits. */
#define EE_SHIFT_CLK	0x04	/* EEPROM shift clock. */
#define EE_CS			0x08	/* EEPROM chip select. */
#define EE_DATA_WRITE	0x02	/* EEPROM chip data in. */
#define EE_WRITE_0		0x00
#define EE_WRITE_1		0x02
#define EE_DATA_READ	0x01	/* EEPROM chip data out. */
#define EE_ENB			(0x80 | EE_CS)

/* Delay between EEPROM clock transitions.
   No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
 */

1623
#define eeprom_delay()	readb(ee_addr)
L
Linus Torvalds 已提交
1624 1625

/* The EEPROM commands include the alway-set leading bit. */
1626
#define EE_EXTEND_CMD	(4)
L
Linus Torvalds 已提交
1627 1628 1629 1630
#define EE_WRITE_CMD	(5)
#define EE_READ_CMD		(6)
#define EE_ERASE_CMD	(7)

1631 1632 1633 1634 1635 1636
#define EE_EWDS_ADDR	(0)
#define EE_WRAL_ADDR	(1)
#define EE_ERAL_ADDR	(2)
#define EE_EWEN_ADDR	(3)

#define CP_EEPROM_MAGIC PCI_DEVICE_ID_REALTEK_8139
L
Linus Torvalds 已提交
1637

1638 1639
static void eeprom_cmd_start(void __iomem *ee_addr)
{
L
Linus Torvalds 已提交
1640 1641 1642
	writeb (EE_ENB & ~EE_CS, ee_addr);
	writeb (EE_ENB, ee_addr);
	eeprom_delay ();
1643
}
L
Linus Torvalds 已提交
1644

1645 1646 1647 1648 1649 1650 1651
static void eeprom_cmd(void __iomem *ee_addr, int cmd, int cmd_len)
{
	int i;

	/* Shift the command bits out. */
	for (i = cmd_len - 1; i >= 0; i--) {
		int dataval = (cmd & (1 << i)) ? EE_DATA_WRITE : 0;
L
Linus Torvalds 已提交
1652 1653 1654 1655 1656 1657 1658
		writeb (EE_ENB | dataval, ee_addr);
		eeprom_delay ();
		writeb (EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
		eeprom_delay ();
	}
	writeb (EE_ENB, ee_addr);
	eeprom_delay ();
1659 1660 1661 1662
}

static void eeprom_cmd_end(void __iomem *ee_addr)
{
1663
	writeb(0, ee_addr);
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685
	eeprom_delay ();
}

static void eeprom_extend_cmd(void __iomem *ee_addr, int extend_cmd,
			      int addr_len)
{
	int cmd = (EE_EXTEND_CMD << addr_len) | (extend_cmd << (addr_len - 2));

	eeprom_cmd_start(ee_addr);
	eeprom_cmd(ee_addr, cmd, 3 + addr_len);
	eeprom_cmd_end(ee_addr);
}

static u16 read_eeprom (void __iomem *ioaddr, int location, int addr_len)
{
	int i;
	u16 retval = 0;
	void __iomem *ee_addr = ioaddr + Cfg9346;
	int read_cmd = location | (EE_READ_CMD << addr_len);

	eeprom_cmd_start(ee_addr);
	eeprom_cmd(ee_addr, read_cmd, 3 + addr_len);
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696

	for (i = 16; i > 0; i--) {
		writeb (EE_ENB | EE_SHIFT_CLK, ee_addr);
		eeprom_delay ();
		retval =
		    (retval << 1) | ((readb (ee_addr) & EE_DATA_READ) ? 1 :
				     0);
		writeb (EE_ENB, ee_addr);
		eeprom_delay ();
	}

1697
	eeprom_cmd_end(ee_addr);
L
Linus Torvalds 已提交
1698 1699 1700 1701

	return retval;
}

1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815
static void write_eeprom(void __iomem *ioaddr, int location, u16 val,
			 int addr_len)
{
	int i;
	void __iomem *ee_addr = ioaddr + Cfg9346;
	int write_cmd = location | (EE_WRITE_CMD << addr_len);

	eeprom_extend_cmd(ee_addr, EE_EWEN_ADDR, addr_len);

	eeprom_cmd_start(ee_addr);
	eeprom_cmd(ee_addr, write_cmd, 3 + addr_len);
	eeprom_cmd(ee_addr, val, 16);
	eeprom_cmd_end(ee_addr);

	eeprom_cmd_start(ee_addr);
	for (i = 0; i < 20000; i++)
		if (readb(ee_addr) & EE_DATA_READ)
			break;
	eeprom_cmd_end(ee_addr);

	eeprom_extend_cmd(ee_addr, EE_EWDS_ADDR, addr_len);
}

static int cp_get_eeprom_len(struct net_device *dev)
{
	struct cp_private *cp = netdev_priv(dev);
	int size;

	spin_lock_irq(&cp->lock);
	size = read_eeprom(cp->regs, 0, 8) == 0x8129 ? 256 : 128;
	spin_unlock_irq(&cp->lock);

	return size;
}

static int cp_get_eeprom(struct net_device *dev,
			 struct ethtool_eeprom *eeprom, u8 *data)
{
	struct cp_private *cp = netdev_priv(dev);
	unsigned int addr_len;
	u16 val;
	u32 offset = eeprom->offset >> 1;
	u32 len = eeprom->len;
	u32 i = 0;

	eeprom->magic = CP_EEPROM_MAGIC;

	spin_lock_irq(&cp->lock);

	addr_len = read_eeprom(cp->regs, 0, 8) == 0x8129 ? 8 : 6;

	if (eeprom->offset & 1) {
		val = read_eeprom(cp->regs, offset, addr_len);
		data[i++] = (u8)(val >> 8);
		offset++;
	}

	while (i < len - 1) {
		val = read_eeprom(cp->regs, offset, addr_len);
		data[i++] = (u8)val;
		data[i++] = (u8)(val >> 8);
		offset++;
	}

	if (i < len) {
		val = read_eeprom(cp->regs, offset, addr_len);
		data[i] = (u8)val;
	}

	spin_unlock_irq(&cp->lock);
	return 0;
}

static int cp_set_eeprom(struct net_device *dev,
			 struct ethtool_eeprom *eeprom, u8 *data)
{
	struct cp_private *cp = netdev_priv(dev);
	unsigned int addr_len;
	u16 val;
	u32 offset = eeprom->offset >> 1;
	u32 len = eeprom->len;
	u32 i = 0;

	if (eeprom->magic != CP_EEPROM_MAGIC)
		return -EINVAL;

	spin_lock_irq(&cp->lock);

	addr_len = read_eeprom(cp->regs, 0, 8) == 0x8129 ? 8 : 6;

	if (eeprom->offset & 1) {
		val = read_eeprom(cp->regs, offset, addr_len) & 0xff;
		val |= (u16)data[i++] << 8;
		write_eeprom(cp->regs, offset, val, addr_len);
		offset++;
	}

	while (i < len - 1) {
		val = (u16)data[i++];
		val |= (u16)data[i++] << 8;
		write_eeprom(cp->regs, offset, val, addr_len);
		offset++;
	}

	if (i < len) {
		val = read_eeprom(cp->regs, offset, addr_len) & 0xff00;
		val |= (u16)data[i];
		write_eeprom(cp->regs, offset, val, addr_len);
	}

	spin_unlock_irq(&cp->lock);
	return 0;
}

L
Linus Torvalds 已提交
1816 1817 1818 1819 1820 1821 1822
/* Put the board into D3cold state and wait for WakeUp signal */
static void cp_set_d3_state (struct cp_private *cp)
{
	pci_enable_wake (cp->pdev, 0, 1); /* Enable PME# generation */
	pci_set_power_state (cp->pdev, PCI_D3hot);
}

1823 1824 1825 1826
static const struct net_device_ops cp_netdev_ops = {
	.ndo_open		= cp_open,
	.ndo_stop		= cp_close,
	.ndo_validate_addr	= eth_validate_addr,
1827
	.ndo_set_mac_address 	= cp_set_mac_address,
1828
	.ndo_set_rx_mode	= cp_set_rx_mode,
1829 1830
	.ndo_get_stats		= cp_get_stats,
	.ndo_do_ioctl		= cp_ioctl,
1831
	.ndo_start_xmit		= cp_start_xmit,
1832
	.ndo_tx_timeout		= cp_tx_timeout,
1833
	.ndo_set_features	= cp_set_features,
1834
	.ndo_change_mtu		= cp_change_mtu,
1835

1836 1837 1838 1839 1840
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= cp_poll_controller,
#endif
};

L
Linus Torvalds 已提交
1841 1842 1843 1844 1845 1846
static int cp_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
{
	struct net_device *dev;
	struct cp_private *cp;
	int rc;
	void __iomem *regs;
1847
	resource_size_t pciaddr;
L
Linus Torvalds 已提交
1848 1849 1850 1851 1852
	unsigned int addr_len, i, pci_using_dac;

#ifndef MODULE
	static int version_printed;
	if (version_printed++ == 0)
1853
		pr_info("%s", version);
L
Linus Torvalds 已提交
1854 1855 1856
#endif

	if (pdev->vendor == PCI_VENDOR_ID_REALTEK &&
1857
	    pdev->device == PCI_DEVICE_ID_REALTEK_8139 && pdev->revision < 0x20) {
1858
		dev_info(&pdev->dev,
1859 1860
			 "This (id %04x:%04x rev %02x) is not an 8139C+ compatible chip, use 8139too\n",
			 pdev->vendor, pdev->device, pdev->revision);
L
Linus Torvalds 已提交
1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
		return -ENODEV;
	}

	dev = alloc_etherdev(sizeof(struct cp_private));
	if (!dev)
		return -ENOMEM;
	SET_NETDEV_DEV(dev, &pdev->dev);

	cp = netdev_priv(dev);
	cp->pdev = pdev;
	cp->dev = dev;
	cp->msg_enable = (debug < 0 ? CP_DEF_MSG_ENABLE : debug);
	spin_lock_init (&cp->lock);
	cp->mii_if.dev = dev;
	cp->mii_if.mdio_read = mdio_read;
	cp->mii_if.mdio_write = mdio_write;
	cp->mii_if.phy_id = CP_INTERNAL_PHY;
	cp->mii_if.phy_id_mask = 0x1f;
	cp->mii_if.reg_num_mask = 0x1f;
	cp_set_rxbufsize(cp);

	rc = pci_enable_device(pdev);
	if (rc)
		goto err_out_free;

	rc = pci_set_mwi(pdev);
	if (rc)
		goto err_out_disable;

	rc = pci_request_regions(pdev, DRV_NAME);
	if (rc)
		goto err_out_mwi;

	pciaddr = pci_resource_start(pdev, 1);
	if (!pciaddr) {
		rc = -EIO;
1897
		dev_err(&pdev->dev, "no MMIO resource\n");
L
Linus Torvalds 已提交
1898 1899 1900 1901
		goto err_out_res;
	}
	if (pci_resource_len(pdev, 1) < CP_REGS_SIZE) {
		rc = -EIO;
1902
		dev_err(&pdev->dev, "MMIO resource (%llx) too small\n",
1903
		       (unsigned long long)pci_resource_len(pdev, 1));
L
Linus Torvalds 已提交
1904 1905 1906 1907 1908
		goto err_out_res;
	}

	/* Configure DMA attributes. */
	if ((sizeof(dma_addr_t) > 4) &&
1909 1910
	    !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)) &&
	    !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
L
Linus Torvalds 已提交
1911 1912 1913 1914
		pci_using_dac = 1;
	} else {
		pci_using_dac = 0;

1915
		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
L
Linus Torvalds 已提交
1916
		if (rc) {
1917
			dev_err(&pdev->dev,
1918
				"No usable DMA configuration, aborting\n");
L
Linus Torvalds 已提交
1919 1920
			goto err_out_res;
		}
1921
		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
L
Linus Torvalds 已提交
1922
		if (rc) {
1923
			dev_err(&pdev->dev,
1924
				"No usable consistent DMA configuration, aborting\n");
L
Linus Torvalds 已提交
1925 1926 1927 1928 1929 1930 1931
			goto err_out_res;
		}
	}

	cp->cpcmd = (pci_using_dac ? PCIDAC : 0) |
		    PCIMulRW | RxChkSum | CpRxOn | CpTxOn;

1932 1933 1934
	dev->features |= NETIF_F_RXCSUM;
	dev->hw_features |= NETIF_F_RXCSUM;

L
Linus Torvalds 已提交
1935 1936 1937
	regs = ioremap(pciaddr, CP_REGS_SIZE);
	if (!regs) {
		rc = -EIO;
A
Andrew Morton 已提交
1938
		dev_err(&pdev->dev, "Cannot map PCI MMIO (%Lx@%Lx)\n",
1939
			(unsigned long long)pci_resource_len(pdev, 1),
1940
		       (unsigned long long)pciaddr);
L
Linus Torvalds 已提交
1941 1942 1943 1944 1945 1946 1947 1948 1949
		goto err_out_res;
	}
	cp->regs = regs;

	cp_stop_hw(cp);

	/* read MAC address from EEPROM */
	addr_len = read_eeprom (regs, 0, 8) == 0x8129 ? 8 : 6;
	for (i = 0; i < 3; i++)
A
Al Viro 已提交
1950 1951
		((__le16 *) (dev->dev_addr))[i] =
		    cpu_to_le16(read_eeprom (regs, i + 7, addr_len));
L
Linus Torvalds 已提交
1952

1953
	dev->netdev_ops = &cp_netdev_ops;
1954
	netif_napi_add(dev, &cp->napi, cp_rx_poll, 16);
L
Linus Torvalds 已提交
1955 1956 1957
	dev->ethtool_ops = &cp_ethtool_ops;
	dev->watchdog_timeo = TX_TIMEOUT;

1958
	dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
L
Linus Torvalds 已提交
1959 1960 1961 1962

	if (pci_using_dac)
		dev->features |= NETIF_F_HIGHDMA;

1963
	/* disabled by default until verified */
1964
	dev->hw_features |= NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
1965
		NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX;
1966 1967
	dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
		NETIF_F_HIGHDMA;
J
Jeff Garzik 已提交
1968

L
Linus Torvalds 已提交
1969 1970 1971 1972
	rc = register_netdev(dev);
	if (rc)
		goto err_out_iomap;

1973 1974
	netdev_info(dev, "RTL-8139C+ at 0x%p, %pM, IRQ %d\n",
		    regs, dev->dev_addr, pdev->irq);
L
Linus Torvalds 已提交
1975 1976 1977 1978 1979 1980

	pci_set_drvdata(pdev, dev);

	/* enable busmastering and memory-write-invalidate */
	pci_set_master(pdev);

1981 1982
	if (cp->wol_enabled)
		cp_set_d3_state (cp);
L
Linus Torvalds 已提交
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005

	return 0;

err_out_iomap:
	iounmap(regs);
err_out_res:
	pci_release_regions(pdev);
err_out_mwi:
	pci_clear_mwi(pdev);
err_out_disable:
	pci_disable_device(pdev);
err_out_free:
	free_netdev(dev);
	return rc;
}

static void cp_remove_one (struct pci_dev *pdev)
{
	struct net_device *dev = pci_get_drvdata(pdev);
	struct cp_private *cp = netdev_priv(dev);

	unregister_netdev(dev);
	iounmap(cp->regs);
2006 2007
	if (cp->wol_enabled)
		pci_set_power_state (pdev, PCI_D0);
L
Linus Torvalds 已提交
2008 2009 2010 2011 2012 2013 2014 2015
	pci_release_regions(pdev);
	pci_clear_mwi(pdev);
	pci_disable_device(pdev);
	pci_set_drvdata(pdev, NULL);
	free_netdev(dev);
}

#ifdef CONFIG_PM
2016
static int cp_suspend (struct pci_dev *pdev, pm_message_t state)
L
Linus Torvalds 已提交
2017
{
2018 2019
	struct net_device *dev = pci_get_drvdata(pdev);
	struct cp_private *cp = netdev_priv(dev);
L
Linus Torvalds 已提交
2020 2021
	unsigned long flags;

2022 2023
	if (!netif_running(dev))
		return 0;
L
Linus Torvalds 已提交
2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035

	netif_device_detach (dev);
	netif_stop_queue (dev);

	spin_lock_irqsave (&cp->lock, flags);

	/* Disable Rx and Tx */
	cpw16 (IntrMask, 0);
	cpw8  (Cmd, cpr8 (Cmd) & (~RxOn | ~TxOn));

	spin_unlock_irqrestore (&cp->lock, flags);

2036 2037 2038
	pci_save_state(pdev);
	pci_enable_wake(pdev, pci_choose_state(pdev, state), cp->wol_enabled);
	pci_set_power_state(pdev, pci_choose_state(pdev, state));
L
Linus Torvalds 已提交
2039 2040 2041 2042 2043 2044

	return 0;
}

static int cp_resume (struct pci_dev *pdev)
{
2045 2046
	struct net_device *dev = pci_get_drvdata (pdev);
	struct cp_private *cp = netdev_priv(dev);
2047
	unsigned long flags;
L
Linus Torvalds 已提交
2048

2049 2050
	if (!netif_running(dev))
		return 0;
L
Linus Torvalds 已提交
2051 2052

	netif_device_attach (dev);
2053 2054 2055 2056 2057 2058 2059

	pci_set_power_state(pdev, PCI_D0);
	pci_restore_state(pdev);
	pci_enable_wake(pdev, PCI_D0, 0);

	/* FIXME: sh*t may happen if the Rx ring buffer is depleted */
	cp_init_rings_index (cp);
L
Linus Torvalds 已提交
2060
	cp_init_hw (cp);
2061
	cp_enable_irq(cp);
L
Linus Torvalds 已提交
2062
	netif_start_queue (dev);
2063 2064 2065

	spin_lock_irqsave (&cp->lock, flags);

2066
	mii_check_media(&cp->mii_if, netif_msg_link(cp), false);
2067 2068

	spin_unlock_irqrestore (&cp->lock, flags);
2069

L
Linus Torvalds 已提交
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
	return 0;
}
#endif /* CONFIG_PM */

static struct pci_driver cp_driver = {
	.name         = DRV_NAME,
	.id_table     = cp_pci_tbl,
	.probe        =	cp_init_one,
	.remove       = cp_remove_one,
#ifdef CONFIG_PM
	.resume       = cp_resume,
	.suspend      = cp_suspend,
#endif
};

static int __init cp_init (void)
{
#ifdef MODULE
2088
	pr_info("%s", version);
L
Linus Torvalds 已提交
2089
#endif
2090
	return pci_register_driver(&cp_driver);
L
Linus Torvalds 已提交
2091 2092 2093 2094 2095 2096 2097 2098 2099
}

static void __exit cp_exit (void)
{
	pci_unregister_driver (&cp_driver);
}

module_init(cp_init);
module_exit(cp_exit);