blob: 18946a56d9b5fb9b634dd6e1ca0d53cf2f152167 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 8139cp.c: A Linux PCI Ethernet driver for the RealTek 8139C+ chips. */
2/*
3 Copyright 2001-2004 Jeff Garzik <jgarzik@pobox.com>
4
5 Copyright (C) 2001, 2002 David S. Miller (davem@redhat.com) [tg3.c]
6 Copyright (C) 2000, 2001 David S. Miller (davem@redhat.com) [sungem.c]
7 Copyright 2001 Manfred Spraul [natsemi.c]
8 Copyright 1999-2001 by Donald Becker. [natsemi.c]
9 Written 1997-2001 by Donald Becker. [8139too.c]
10 Copyright 1998-2001 by Jes Sorensen, <jes@trained-monkey.org>. [acenic.c]
11
12 This software may be used and distributed according to the terms of
13 the GNU General Public License (GPL), incorporated herein by reference.
14 Drivers based on or derived from this code fall under the GPL and must
15 retain the authorship, copyright and license notice. This file is not
16 a complete program and may only be used when the entire operating
17 system is licensed under the GPL.
18
19 See the file COPYING in this distribution for more information.
20
21 Contributors:
22
23 Wake-on-LAN support - Felipe Damasio <felipewd@terra.com.br>
24 PCI suspend/resume - Felipe Damasio <felipewd@terra.com.br>
25 LinkChg interrupt - Felipe Damasio <felipewd@terra.com.br>
26
27 TODO:
28 * Test Tx checksumming thoroughly
29 * Implement dev->tx_timeout
30
31 Low priority TODO:
32 * Complete reset on PciErr
33 * Consider Rx interrupt mitigation using TimerIntr
34 * Investigate using skb->priority with h/w VLAN priority
35 * Investigate using High Priority Tx Queue with skb->priority
36 * Adjust Rx FIFO threshold and Max Rx DMA burst on Rx FIFO error
37 * Adjust Tx FIFO threshold and Max Tx DMA burst on Tx FIFO error
38 * Implement Tx software interrupt mitigation via
39 Tx descriptor bit
40 * The real minimum of CP_MIN_MTU is 4 bytes. However,
41 for this to be supported, one must(?) turn on packet padding.
42 * Support external MII transceivers (patch available)
43
44 NOTES:
45 * TX checksumming is considered experimental. It is off by
46 default, use ethtool to turn it on.
47
48 */
49
50#define DRV_NAME "8139cp"
51#define DRV_VERSION "1.2"
52#define DRV_RELDATE "Mar 22, 2004"
53
54
55#include <linux/config.h>
56#include <linux/module.h>
Stephen Hemmingere21ba282005-05-12 19:33:26 -040057#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/kernel.h>
59#include <linux/compiler.h>
60#include <linux/netdevice.h>
61#include <linux/etherdevice.h>
62#include <linux/init.h>
63#include <linux/pci.h>
64#include <linux/delay.h>
65#include <linux/ethtool.h>
66#include <linux/mii.h>
67#include <linux/if_vlan.h>
68#include <linux/crc32.h>
69#include <linux/in.h>
70#include <linux/ip.h>
71#include <linux/tcp.h>
72#include <linux/udp.h>
73#include <linux/cache.h>
74#include <asm/io.h>
75#include <asm/irq.h>
76#include <asm/uaccess.h>
77
78/* VLAN tagging feature enable/disable */
79#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
80#define CP_VLAN_TAG_USED 1
81#define CP_VLAN_TX_TAG(tx_desc,vlan_tag_value) \
82 do { (tx_desc)->opts2 = (vlan_tag_value); } while (0)
83#else
84#define CP_VLAN_TAG_USED 0
85#define CP_VLAN_TX_TAG(tx_desc,vlan_tag_value) \
86 do { (tx_desc)->opts2 = 0; } while (0)
87#endif
88
89/* These identify the driver base version and may not be removed. */
90static char version[] =
91KERN_INFO DRV_NAME ": 10/100 PCI Ethernet driver v" DRV_VERSION " (" DRV_RELDATE ")\n";
92
93MODULE_AUTHOR("Jeff Garzik <jgarzik@pobox.com>");
94MODULE_DESCRIPTION("RealTek RTL-8139C+ series 10/100 PCI Ethernet driver");
95MODULE_LICENSE("GPL");
96
97static int debug = -1;
Stephen Hemmingere21ba282005-05-12 19:33:26 -040098module_param(debug, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099MODULE_PARM_DESC (debug, "8139cp: bitmapped message enable number");
100
101/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
102 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
103static int multicast_filter_limit = 32;
Stephen Hemmingere21ba282005-05-12 19:33:26 -0400104module_param(multicast_filter_limit, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105MODULE_PARM_DESC (multicast_filter_limit, "8139cp: maximum number of filtered multicast addresses");
106
107#define PFX DRV_NAME ": "
108
109#ifndef TRUE
110#define FALSE 0
111#define TRUE (!FALSE)
112#endif
113
114#define CP_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
115 NETIF_MSG_PROBE | \
116 NETIF_MSG_LINK)
117#define CP_NUM_STATS 14 /* struct cp_dma_stats, plus one */
118#define CP_STATS_SIZE 64 /* size in bytes of DMA stats block */
119#define CP_REGS_SIZE (0xff + 1)
120#define CP_REGS_VER 1 /* version 1 */
121#define CP_RX_RING_SIZE 64
122#define CP_TX_RING_SIZE 64
123#define CP_RING_BYTES \
124 ((sizeof(struct cp_desc) * CP_RX_RING_SIZE) + \
125 (sizeof(struct cp_desc) * CP_TX_RING_SIZE) + \
126 CP_STATS_SIZE)
127#define NEXT_TX(N) (((N) + 1) & (CP_TX_RING_SIZE - 1))
128#define NEXT_RX(N) (((N) + 1) & (CP_RX_RING_SIZE - 1))
129#define TX_BUFFS_AVAIL(CP) \
130 (((CP)->tx_tail <= (CP)->tx_head) ? \
131 (CP)->tx_tail + (CP_TX_RING_SIZE - 1) - (CP)->tx_head : \
132 (CP)->tx_tail - (CP)->tx_head - 1)
133
134#define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
135#define RX_OFFSET 2
136#define CP_INTERNAL_PHY 32
137
138/* The following settings are log_2(bytes)-4: 0 == 16 bytes .. 6==1024, 7==end of packet. */
139#define RX_FIFO_THRESH 5 /* Rx buffer level before first PCI xfer. */
140#define RX_DMA_BURST 4 /* Maximum PCI burst, '4' is 256 */
141#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
142#define TX_EARLY_THRESH 256 /* Early Tx threshold, in bytes */
143
144/* Time in jiffies before concluding the transmitter is hung. */
145#define TX_TIMEOUT (6*HZ)
146
147/* hardware minimum and maximum for a single frame's data payload */
148#define CP_MIN_MTU 60 /* TODO: allow lower, but pad */
149#define CP_MAX_MTU 4096
150
151enum {
152 /* NIC register offsets */
153 MAC0 = 0x00, /* Ethernet hardware address. */
154 MAR0 = 0x08, /* Multicast filter. */
155 StatsAddr = 0x10, /* 64-bit start addr of 64-byte DMA stats blk */
156 TxRingAddr = 0x20, /* 64-bit start addr of Tx ring */
157 HiTxRingAddr = 0x28, /* 64-bit start addr of high priority Tx ring */
158 Cmd = 0x37, /* Command register */
159 IntrMask = 0x3C, /* Interrupt mask */
160 IntrStatus = 0x3E, /* Interrupt status */
161 TxConfig = 0x40, /* Tx configuration */
162 ChipVersion = 0x43, /* 8-bit chip version, inside TxConfig */
163 RxConfig = 0x44, /* Rx configuration */
164 RxMissed = 0x4C, /* 24 bits valid, write clears */
165 Cfg9346 = 0x50, /* EEPROM select/control; Cfg reg [un]lock */
166 Config1 = 0x52, /* Config1 */
167 Config3 = 0x59, /* Config3 */
168 Config4 = 0x5A, /* Config4 */
169 MultiIntr = 0x5C, /* Multiple interrupt select */
170 BasicModeCtrl = 0x62, /* MII BMCR */
171 BasicModeStatus = 0x64, /* MII BMSR */
172 NWayAdvert = 0x66, /* MII ADVERTISE */
173 NWayLPAR = 0x68, /* MII LPA */
174 NWayExpansion = 0x6A, /* MII Expansion */
175 Config5 = 0xD8, /* Config5 */
176 TxPoll = 0xD9, /* Tell chip to check Tx descriptors for work */
177 RxMaxSize = 0xDA, /* Max size of an Rx packet (8169 only) */
178 CpCmd = 0xE0, /* C+ Command register (C+ mode only) */
179 IntrMitigate = 0xE2, /* rx/tx interrupt mitigation control */
180 RxRingAddr = 0xE4, /* 64-bit start addr of Rx ring */
181 TxThresh = 0xEC, /* Early Tx threshold */
182 OldRxBufAddr = 0x30, /* DMA address of Rx ring buffer (C mode) */
183 OldTSD0 = 0x10, /* DMA address of first Tx desc (C mode) */
184
185 /* Tx and Rx status descriptors */
186 DescOwn = (1 << 31), /* Descriptor is owned by NIC */
187 RingEnd = (1 << 30), /* End of descriptor ring */
188 FirstFrag = (1 << 29), /* First segment of a packet */
189 LastFrag = (1 << 28), /* Final segment of a packet */
Jeff Garzikfcec3452005-05-12 19:28:49 -0400190 LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
191 MSSShift = 16, /* MSS value position */
192 MSSMask = 0xfff, /* MSS value: 11 bits */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 TxError = (1 << 23), /* Tx error summary */
194 RxError = (1 << 20), /* Rx error summary */
195 IPCS = (1 << 18), /* Calculate IP checksum */
196 UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
197 TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
198 TxVlanTag = (1 << 17), /* Add VLAN tag */
199 RxVlanTagged = (1 << 16), /* Rx VLAN tag available */
200 IPFail = (1 << 15), /* IP checksum failed */
201 UDPFail = (1 << 14), /* UDP/IP checksum failed */
202 TCPFail = (1 << 13), /* TCP/IP checksum failed */
203 NormalTxPoll = (1 << 6), /* One or more normal Tx packets to send */
204 PID1 = (1 << 17), /* 2 protocol id bits: 0==non-IP, */
205 PID0 = (1 << 16), /* 1==UDP/IP, 2==TCP/IP, 3==IP */
206 RxProtoTCP = 1,
207 RxProtoUDP = 2,
208 RxProtoIP = 3,
209 TxFIFOUnder = (1 << 25), /* Tx FIFO underrun */
210 TxOWC = (1 << 22), /* Tx Out-of-window collision */
211 TxLinkFail = (1 << 21), /* Link failed during Tx of packet */
212 TxMaxCol = (1 << 20), /* Tx aborted due to excessive collisions */
213 TxColCntShift = 16, /* Shift, to get 4-bit Tx collision cnt */
214 TxColCntMask = 0x01 | 0x02 | 0x04 | 0x08, /* 4-bit collision count */
215 RxErrFrame = (1 << 27), /* Rx frame alignment error */
216 RxMcast = (1 << 26), /* Rx multicast packet rcv'd */
217 RxErrCRC = (1 << 18), /* Rx CRC error */
218 RxErrRunt = (1 << 19), /* Rx error, packet < 64 bytes */
219 RxErrLong = (1 << 21), /* Rx error, packet > 4096 bytes */
220 RxErrFIFO = (1 << 22), /* Rx error, FIFO overflowed, pkt bad */
221
222 /* StatsAddr register */
223 DumpStats = (1 << 3), /* Begin stats dump */
224
225 /* RxConfig register */
226 RxCfgFIFOShift = 13, /* Shift, to get Rx FIFO thresh value */
227 RxCfgDMAShift = 8, /* Shift, to get Rx Max DMA value */
228 AcceptErr = 0x20, /* Accept packets with CRC errors */
229 AcceptRunt = 0x10, /* Accept runt (<64 bytes) packets */
230 AcceptBroadcast = 0x08, /* Accept broadcast packets */
231 AcceptMulticast = 0x04, /* Accept multicast packets */
232 AcceptMyPhys = 0x02, /* Accept pkts with our MAC as dest */
233 AcceptAllPhys = 0x01, /* Accept all pkts w/ physical dest */
234
235 /* IntrMask / IntrStatus registers */
236 PciErr = (1 << 15), /* System error on the PCI bus */
237 TimerIntr = (1 << 14), /* Asserted when TCTR reaches TimerInt value */
238 LenChg = (1 << 13), /* Cable length change */
239 SWInt = (1 << 8), /* Software-requested interrupt */
240 TxEmpty = (1 << 7), /* No Tx descriptors available */
241 RxFIFOOvr = (1 << 6), /* Rx FIFO Overflow */
242 LinkChg = (1 << 5), /* Packet underrun, or link change */
243 RxEmpty = (1 << 4), /* No Rx descriptors available */
244 TxErr = (1 << 3), /* Tx error */
245 TxOK = (1 << 2), /* Tx packet sent */
246 RxErr = (1 << 1), /* Rx error */
247 RxOK = (1 << 0), /* Rx packet received */
248 IntrResvd = (1 << 10), /* reserved, according to RealTek engineers,
249 but hardware likes to raise it */
250
251 IntrAll = PciErr | TimerIntr | LenChg | SWInt | TxEmpty |
252 RxFIFOOvr | LinkChg | RxEmpty | TxErr | TxOK |
253 RxErr | RxOK | IntrResvd,
254
255 /* C mode command register */
256 CmdReset = (1 << 4), /* Enable to reset; self-clearing */
257 RxOn = (1 << 3), /* Rx mode enable */
258 TxOn = (1 << 2), /* Tx mode enable */
259
260 /* C+ mode command register */
261 RxVlanOn = (1 << 6), /* Rx VLAN de-tagging enable */
262 RxChkSum = (1 << 5), /* Rx checksum offload enable */
263 PCIDAC = (1 << 4), /* PCI Dual Address Cycle (64-bit PCI) */
264 PCIMulRW = (1 << 3), /* Enable PCI read/write multiple */
265 CpRxOn = (1 << 1), /* Rx mode enable */
266 CpTxOn = (1 << 0), /* Tx mode enable */
267
268 /* Cfg9436 EEPROM control register */
269 Cfg9346_Lock = 0x00, /* Lock ConfigX/MII register access */
270 Cfg9346_Unlock = 0xC0, /* Unlock ConfigX/MII register access */
271
272 /* TxConfig register */
273 IFG = (1 << 25) | (1 << 24), /* standard IEEE interframe gap */
274 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
275
276 /* Early Tx Threshold register */
277 TxThreshMask = 0x3f, /* Mask bits 5-0 */
278 TxThreshMax = 2048, /* Max early Tx threshold */
279
280 /* Config1 register */
281 DriverLoaded = (1 << 5), /* Software marker, driver is loaded */
282 LWACT = (1 << 4), /* LWAKE active mode */
283 PMEnable = (1 << 0), /* Enable various PM features of chip */
284
285 /* Config3 register */
286 PARMEnable = (1 << 6), /* Enable auto-loading of PHY parms */
287 MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
288 LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
289
290 /* Config4 register */
291 LWPTN = (1 << 1), /* LWAKE Pattern */
292 LWPME = (1 << 4), /* LANWAKE vs PMEB */
293
294 /* Config5 register */
295 BWF = (1 << 6), /* Accept Broadcast wakeup frame */
296 MWF = (1 << 5), /* Accept Multicast wakeup frame */
297 UWF = (1 << 4), /* Accept Unicast wakeup frame */
298 LANWake = (1 << 1), /* Enable LANWake signal */
299 PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
300
301 cp_norx_intr_mask = PciErr | LinkChg | TxOK | TxErr | TxEmpty,
302 cp_rx_intr_mask = RxOK | RxErr | RxEmpty | RxFIFOOvr,
303 cp_intr_mask = cp_rx_intr_mask | cp_norx_intr_mask,
304};
305
306static const unsigned int cp_rx_config =
307 (RX_FIFO_THRESH << RxCfgFIFOShift) |
308 (RX_DMA_BURST << RxCfgDMAShift);
309
310struct cp_desc {
311 u32 opts1;
312 u32 opts2;
313 u64 addr;
314};
315
316struct ring_info {
317 struct sk_buff *skb;
318 dma_addr_t mapping;
Francois Romieu57344182005-05-12 19:31:31 -0400319 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320};
321
322struct cp_dma_stats {
323 u64 tx_ok;
324 u64 rx_ok;
325 u64 tx_err;
326 u32 rx_err;
327 u16 rx_fifo;
328 u16 frame_align;
329 u32 tx_ok_1col;
330 u32 tx_ok_mcol;
331 u64 rx_ok_phys;
332 u64 rx_ok_bcast;
333 u32 rx_ok_mcast;
334 u16 tx_abort;
335 u16 tx_underrun;
336} __attribute__((packed));
337
338struct cp_extra_stats {
339 unsigned long rx_frags;
340};
341
342struct cp_private {
343 void __iomem *regs;
344 struct net_device *dev;
345 spinlock_t lock;
346 u32 msg_enable;
347
348 struct pci_dev *pdev;
349 u32 rx_config;
350 u16 cpcmd;
351
352 struct net_device_stats net_stats;
353 struct cp_extra_stats cp_stats;
354 struct cp_dma_stats *nic_stats;
355 dma_addr_t nic_stats_dma;
356
357 unsigned rx_tail ____cacheline_aligned;
358 struct cp_desc *rx_ring;
359 struct ring_info rx_skb[CP_RX_RING_SIZE];
360 unsigned rx_buf_sz;
361
362 unsigned tx_head ____cacheline_aligned;
363 unsigned tx_tail;
364
365 struct cp_desc *tx_ring;
366 struct ring_info tx_skb[CP_TX_RING_SIZE];
367 dma_addr_t ring_dma;
368
369#if CP_VLAN_TAG_USED
370 struct vlan_group *vlgrp;
371#endif
372
373 unsigned int wol_enabled : 1; /* Is Wake-on-LAN enabled? */
374
375 struct mii_if_info mii_if;
376};
377
378#define cpr8(reg) readb(cp->regs + (reg))
379#define cpr16(reg) readw(cp->regs + (reg))
380#define cpr32(reg) readl(cp->regs + (reg))
381#define cpw8(reg,val) writeb((val), cp->regs + (reg))
382#define cpw16(reg,val) writew((val), cp->regs + (reg))
383#define cpw32(reg,val) writel((val), cp->regs + (reg))
384#define cpw8_f(reg,val) do { \
385 writeb((val), cp->regs + (reg)); \
386 readb(cp->regs + (reg)); \
387 } while (0)
388#define cpw16_f(reg,val) do { \
389 writew((val), cp->regs + (reg)); \
390 readw(cp->regs + (reg)); \
391 } while (0)
392#define cpw32_f(reg,val) do { \
393 writel((val), cp->regs + (reg)); \
394 readl(cp->regs + (reg)); \
395 } while (0)
396
397
398static void __cp_set_rx_mode (struct net_device *dev);
399static void cp_tx (struct cp_private *cp);
400static void cp_clean_rings (struct cp_private *cp);
401
402static struct pci_device_id cp_pci_tbl[] = {
403 { PCI_VENDOR_ID_REALTEK, PCI_DEVICE_ID_REALTEK_8139,
404 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
405 { PCI_VENDOR_ID_TTTECH, PCI_DEVICE_ID_TTTECH_MC322,
406 PCI_ANY_ID, PCI_ANY_ID, 0, 0, },
407 { },
408};
409MODULE_DEVICE_TABLE(pci, cp_pci_tbl);
410
411static struct {
412 const char str[ETH_GSTRING_LEN];
413} ethtool_stats_keys[] = {
414 { "tx_ok" },
415 { "rx_ok" },
416 { "tx_err" },
417 { "rx_err" },
418 { "rx_fifo" },
419 { "frame_align" },
420 { "tx_ok_1col" },
421 { "tx_ok_mcol" },
422 { "rx_ok_phys" },
423 { "rx_ok_bcast" },
424 { "rx_ok_mcast" },
425 { "tx_abort" },
426 { "tx_underrun" },
427 { "rx_frags" },
428};
429
430
431#if CP_VLAN_TAG_USED
432static void cp_vlan_rx_register(struct net_device *dev, struct vlan_group *grp)
433{
434 struct cp_private *cp = netdev_priv(dev);
435 unsigned long flags;
436
437 spin_lock_irqsave(&cp->lock, flags);
438 cp->vlgrp = grp;
439 cp->cpcmd |= RxVlanOn;
440 cpw16(CpCmd, cp->cpcmd);
441 spin_unlock_irqrestore(&cp->lock, flags);
442}
443
444static void cp_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
445{
446 struct cp_private *cp = netdev_priv(dev);
447 unsigned long flags;
448
449 spin_lock_irqsave(&cp->lock, flags);
450 cp->cpcmd &= ~RxVlanOn;
451 cpw16(CpCmd, cp->cpcmd);
452 if (cp->vlgrp)
453 cp->vlgrp->vlan_devices[vid] = NULL;
454 spin_unlock_irqrestore(&cp->lock, flags);
455}
456#endif /* CP_VLAN_TAG_USED */
457
458static inline void cp_set_rxbufsize (struct cp_private *cp)
459{
460 unsigned int mtu = cp->dev->mtu;
461
462 if (mtu > ETH_DATA_LEN)
463 /* MTU + ethernet header + FCS + optional VLAN tag */
464 cp->rx_buf_sz = mtu + ETH_HLEN + 8;
465 else
466 cp->rx_buf_sz = PKT_BUF_SZ;
467}
468
469static inline void cp_rx_skb (struct cp_private *cp, struct sk_buff *skb,
470 struct cp_desc *desc)
471{
472 skb->protocol = eth_type_trans (skb, cp->dev);
473
474 cp->net_stats.rx_packets++;
475 cp->net_stats.rx_bytes += skb->len;
476 cp->dev->last_rx = jiffies;
477
478#if CP_VLAN_TAG_USED
479 if (cp->vlgrp && (desc->opts2 & RxVlanTagged)) {
480 vlan_hwaccel_receive_skb(skb, cp->vlgrp,
481 be16_to_cpu(desc->opts2 & 0xffff));
482 } else
483#endif
484 netif_receive_skb(skb);
485}
486
487static void cp_rx_err_acct (struct cp_private *cp, unsigned rx_tail,
488 u32 status, u32 len)
489{
490 if (netif_msg_rx_err (cp))
491 printk (KERN_DEBUG
492 "%s: rx err, slot %d status 0x%x len %d\n",
493 cp->dev->name, rx_tail, status, len);
494 cp->net_stats.rx_errors++;
495 if (status & RxErrFrame)
496 cp->net_stats.rx_frame_errors++;
497 if (status & RxErrCRC)
498 cp->net_stats.rx_crc_errors++;
499 if ((status & RxErrRunt) || (status & RxErrLong))
500 cp->net_stats.rx_length_errors++;
501 if ((status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag))
502 cp->net_stats.rx_length_errors++;
503 if (status & RxErrFIFO)
504 cp->net_stats.rx_fifo_errors++;
505}
506
507static inline unsigned int cp_rx_csum_ok (u32 status)
508{
509 unsigned int protocol = (status >> 16) & 0x3;
510
511 if (likely((protocol == RxProtoTCP) && (!(status & TCPFail))))
512 return 1;
513 else if ((protocol == RxProtoUDP) && (!(status & UDPFail)))
514 return 1;
515 else if ((protocol == RxProtoIP) && (!(status & IPFail)))
516 return 1;
517 return 0;
518}
519
520static int cp_rx_poll (struct net_device *dev, int *budget)
521{
522 struct cp_private *cp = netdev_priv(dev);
523 unsigned rx_tail = cp->rx_tail;
524 unsigned rx_work = dev->quota;
525 unsigned rx;
526
527rx_status_loop:
528 rx = 0;
529 cpw16(IntrStatus, cp_rx_intr_mask);
530
531 while (1) {
532 u32 status, len;
533 dma_addr_t mapping;
534 struct sk_buff *skb, *new_skb;
535 struct cp_desc *desc;
536 unsigned buflen;
537
538 skb = cp->rx_skb[rx_tail].skb;
539 if (!skb)
540 BUG();
541
542 desc = &cp->rx_ring[rx_tail];
543 status = le32_to_cpu(desc->opts1);
544 if (status & DescOwn)
545 break;
546
547 len = (status & 0x1fff) - 4;
548 mapping = cp->rx_skb[rx_tail].mapping;
549
550 if ((status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag)) {
551 /* we don't support incoming fragmented frames.
552 * instead, we attempt to ensure that the
553 * pre-allocated RX skbs are properly sized such
554 * that RX fragments are never encountered
555 */
556 cp_rx_err_acct(cp, rx_tail, status, len);
557 cp->net_stats.rx_dropped++;
558 cp->cp_stats.rx_frags++;
559 goto rx_next;
560 }
561
562 if (status & (RxError | RxErrFIFO)) {
563 cp_rx_err_acct(cp, rx_tail, status, len);
564 goto rx_next;
565 }
566
567 if (netif_msg_rx_status(cp))
568 printk(KERN_DEBUG "%s: rx slot %d status 0x%x len %d\n",
569 cp->dev->name, rx_tail, status, len);
570
571 buflen = cp->rx_buf_sz + RX_OFFSET;
572 new_skb = dev_alloc_skb (buflen);
573 if (!new_skb) {
574 cp->net_stats.rx_dropped++;
575 goto rx_next;
576 }
577
578 skb_reserve(new_skb, RX_OFFSET);
579 new_skb->dev = cp->dev;
580
581 pci_unmap_single(cp->pdev, mapping,
582 buflen, PCI_DMA_FROMDEVICE);
583
584 /* Handle checksum offloading for incoming packets. */
585 if (cp_rx_csum_ok(status))
586 skb->ip_summed = CHECKSUM_UNNECESSARY;
587 else
588 skb->ip_summed = CHECKSUM_NONE;
589
590 skb_put(skb, len);
591
592 mapping =
593 cp->rx_skb[rx_tail].mapping =
594 pci_map_single(cp->pdev, new_skb->tail,
595 buflen, PCI_DMA_FROMDEVICE);
596 cp->rx_skb[rx_tail].skb = new_skb;
597
598 cp_rx_skb(cp, skb, desc);
599 rx++;
600
601rx_next:
602 cp->rx_ring[rx_tail].opts2 = 0;
603 cp->rx_ring[rx_tail].addr = cpu_to_le64(mapping);
604 if (rx_tail == (CP_RX_RING_SIZE - 1))
605 desc->opts1 = cpu_to_le32(DescOwn | RingEnd |
606 cp->rx_buf_sz);
607 else
608 desc->opts1 = cpu_to_le32(DescOwn | cp->rx_buf_sz);
609 rx_tail = NEXT_RX(rx_tail);
610
611 if (!rx_work--)
612 break;
613 }
614
615 cp->rx_tail = rx_tail;
616
617 dev->quota -= rx;
618 *budget -= rx;
619
620 /* if we did not reach work limit, then we're done with
621 * this round of polling
622 */
623 if (rx_work) {
624 if (cpr16(IntrStatus) & cp_rx_intr_mask)
625 goto rx_status_loop;
626
627 local_irq_disable();
628 cpw16_f(IntrMask, cp_intr_mask);
629 __netif_rx_complete(dev);
630 local_irq_enable();
631
632 return 0; /* done */
633 }
634
635 return 1; /* not done */
636}
637
638static irqreturn_t
639cp_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
640{
641 struct net_device *dev = dev_instance;
642 struct cp_private *cp;
643 u16 status;
644
645 if (unlikely(dev == NULL))
646 return IRQ_NONE;
647 cp = netdev_priv(dev);
648
649 status = cpr16(IntrStatus);
650 if (!status || (status == 0xFFFF))
651 return IRQ_NONE;
652
653 if (netif_msg_intr(cp))
654 printk(KERN_DEBUG "%s: intr, status %04x cmd %02x cpcmd %04x\n",
655 dev->name, status, cpr8(Cmd), cpr16(CpCmd));
656
657 cpw16(IntrStatus, status & ~cp_rx_intr_mask);
658
659 spin_lock(&cp->lock);
660
661 /* close possible race's with dev_close */
662 if (unlikely(!netif_running(dev))) {
663 cpw16(IntrMask, 0);
664 spin_unlock(&cp->lock);
665 return IRQ_HANDLED;
666 }
667
668 if (status & (RxOK | RxErr | RxEmpty | RxFIFOOvr))
669 if (netif_rx_schedule_prep(dev)) {
670 cpw16_f(IntrMask, cp_norx_intr_mask);
671 __netif_rx_schedule(dev);
672 }
673
674 if (status & (TxOK | TxErr | TxEmpty | SWInt))
675 cp_tx(cp);
676 if (status & LinkChg)
677 mii_check_media(&cp->mii_if, netif_msg_link(cp), FALSE);
678
679 spin_unlock(&cp->lock);
680
681 if (status & PciErr) {
682 u16 pci_status;
683
684 pci_read_config_word(cp->pdev, PCI_STATUS, &pci_status);
685 pci_write_config_word(cp->pdev, PCI_STATUS, pci_status);
686 printk(KERN_ERR "%s: PCI bus error, status=%04x, PCI status=%04x\n",
687 dev->name, status, pci_status);
688
689 /* TODO: reset hardware */
690 }
691
692 return IRQ_HANDLED;
693}
694
695static void cp_tx (struct cp_private *cp)
696{
697 unsigned tx_head = cp->tx_head;
698 unsigned tx_tail = cp->tx_tail;
699
700 while (tx_tail != tx_head) {
701 struct sk_buff *skb;
702 u32 status;
703
704 rmb();
705 status = le32_to_cpu(cp->tx_ring[tx_tail].opts1);
706 if (status & DescOwn)
707 break;
708
709 skb = cp->tx_skb[tx_tail].skb;
710 if (!skb)
711 BUG();
712
713 pci_unmap_single(cp->pdev, cp->tx_skb[tx_tail].mapping,
Francois Romieu57344182005-05-12 19:31:31 -0400714 cp->tx_skb[tx_tail].len, PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (status & LastFrag) {
717 if (status & (TxError | TxFIFOUnder)) {
718 if (netif_msg_tx_err(cp))
719 printk(KERN_DEBUG "%s: tx err, status 0x%x\n",
720 cp->dev->name, status);
721 cp->net_stats.tx_errors++;
722 if (status & TxOWC)
723 cp->net_stats.tx_window_errors++;
724 if (status & TxMaxCol)
725 cp->net_stats.tx_aborted_errors++;
726 if (status & TxLinkFail)
727 cp->net_stats.tx_carrier_errors++;
728 if (status & TxFIFOUnder)
729 cp->net_stats.tx_fifo_errors++;
730 } else {
731 cp->net_stats.collisions +=
732 ((status >> TxColCntShift) & TxColCntMask);
733 cp->net_stats.tx_packets++;
734 cp->net_stats.tx_bytes += skb->len;
735 if (netif_msg_tx_done(cp))
736 printk(KERN_DEBUG "%s: tx done, slot %d\n", cp->dev->name, tx_tail);
737 }
738 dev_kfree_skb_irq(skb);
739 }
740
741 cp->tx_skb[tx_tail].skb = NULL;
742
743 tx_tail = NEXT_TX(tx_tail);
744 }
745
746 cp->tx_tail = tx_tail;
747
748 if (TX_BUFFS_AVAIL(cp) > (MAX_SKB_FRAGS + 1))
749 netif_wake_queue(cp->dev);
750}
751
752static int cp_start_xmit (struct sk_buff *skb, struct net_device *dev)
753{
754 struct cp_private *cp = netdev_priv(dev);
755 unsigned entry;
Jeff Garzikfcec3452005-05-12 19:28:49 -0400756 u32 eor, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757#if CP_VLAN_TAG_USED
758 u32 vlan_tag = 0;
759#endif
Jeff Garzikfcec3452005-05-12 19:28:49 -0400760 int mss = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 spin_lock_irq(&cp->lock);
763
764 /* This is a hard error, log it. */
765 if (TX_BUFFS_AVAIL(cp) <= (skb_shinfo(skb)->nr_frags + 1)) {
766 netif_stop_queue(dev);
767 spin_unlock_irq(&cp->lock);
768 printk(KERN_ERR PFX "%s: BUG! Tx Ring full when queue awake!\n",
769 dev->name);
770 return 1;
771 }
772
773#if CP_VLAN_TAG_USED
774 if (cp->vlgrp && vlan_tx_tag_present(skb))
775 vlan_tag = TxVlanTag | cpu_to_be16(vlan_tx_tag_get(skb));
776#endif
777
778 entry = cp->tx_head;
779 eor = (entry == (CP_TX_RING_SIZE - 1)) ? RingEnd : 0;
Jeff Garzikfcec3452005-05-12 19:28:49 -0400780 if (dev->features & NETIF_F_TSO)
781 mss = skb_shinfo(skb)->tso_size;
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (skb_shinfo(skb)->nr_frags == 0) {
784 struct cp_desc *txd = &cp->tx_ring[entry];
785 u32 len;
786 dma_addr_t mapping;
787
788 len = skb->len;
789 mapping = pci_map_single(cp->pdev, skb->data, len, PCI_DMA_TODEVICE);
790 CP_VLAN_TX_TAG(txd, vlan_tag);
791 txd->addr = cpu_to_le64(mapping);
792 wmb();
793
Jeff Garzikfcec3452005-05-12 19:28:49 -0400794 flags = eor | len | DescOwn | FirstFrag | LastFrag;
795
796 if (mss)
797 flags |= LargeSend | ((mss & MSSMask) << MSSShift);
798 else if (skb->ip_summed == CHECKSUM_HW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 const struct iphdr *ip = skb->nh.iph;
800 if (ip->protocol == IPPROTO_TCP)
Jeff Garzikfcec3452005-05-12 19:28:49 -0400801 flags |= IPCS | TCPCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 else if (ip->protocol == IPPROTO_UDP)
Jeff Garzikfcec3452005-05-12 19:28:49 -0400803 flags |= IPCS | UDPCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 else
Francois Romieu57344182005-05-12 19:31:31 -0400805 WARN_ON(1); /* we need a WARN() */
Jeff Garzikfcec3452005-05-12 19:28:49 -0400806 }
807
808 txd->opts1 = cpu_to_le32(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 wmb();
810
811 cp->tx_skb[entry].skb = skb;
812 cp->tx_skb[entry].mapping = mapping;
Francois Romieu57344182005-05-12 19:31:31 -0400813 cp->tx_skb[entry].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 entry = NEXT_TX(entry);
815 } else {
816 struct cp_desc *txd;
817 u32 first_len, first_eor;
818 dma_addr_t first_mapping;
819 int frag, first_entry = entry;
820 const struct iphdr *ip = skb->nh.iph;
821
822 /* We must give this initial chunk to the device last.
823 * Otherwise we could race with the device.
824 */
825 first_eor = eor;
826 first_len = skb_headlen(skb);
827 first_mapping = pci_map_single(cp->pdev, skb->data,
828 first_len, PCI_DMA_TODEVICE);
829 cp->tx_skb[entry].skb = skb;
830 cp->tx_skb[entry].mapping = first_mapping;
Francois Romieu57344182005-05-12 19:31:31 -0400831 cp->tx_skb[entry].len = first_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 entry = NEXT_TX(entry);
833
834 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
835 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
836 u32 len;
837 u32 ctrl;
838 dma_addr_t mapping;
839
840 len = this_frag->size;
841 mapping = pci_map_single(cp->pdev,
842 ((void *) page_address(this_frag->page) +
843 this_frag->page_offset),
844 len, PCI_DMA_TODEVICE);
845 eor = (entry == (CP_TX_RING_SIZE - 1)) ? RingEnd : 0;
846
Jeff Garzikfcec3452005-05-12 19:28:49 -0400847 ctrl = eor | len | DescOwn;
848
849 if (mss)
850 ctrl |= LargeSend |
851 ((mss & MSSMask) << MSSShift);
852 else if (skb->ip_summed == CHECKSUM_HW) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (ip->protocol == IPPROTO_TCP)
Jeff Garzikfcec3452005-05-12 19:28:49 -0400854 ctrl |= IPCS | TCPCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 else if (ip->protocol == IPPROTO_UDP)
Jeff Garzikfcec3452005-05-12 19:28:49 -0400856 ctrl |= IPCS | UDPCS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 else
858 BUG();
Jeff Garzikfcec3452005-05-12 19:28:49 -0400859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 if (frag == skb_shinfo(skb)->nr_frags - 1)
862 ctrl |= LastFrag;
863
864 txd = &cp->tx_ring[entry];
865 CP_VLAN_TX_TAG(txd, vlan_tag);
866 txd->addr = cpu_to_le64(mapping);
867 wmb();
868
869 txd->opts1 = cpu_to_le32(ctrl);
870 wmb();
871
872 cp->tx_skb[entry].skb = skb;
873 cp->tx_skb[entry].mapping = mapping;
Francois Romieu57344182005-05-12 19:31:31 -0400874 cp->tx_skb[entry].len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 entry = NEXT_TX(entry);
876 }
877
878 txd = &cp->tx_ring[first_entry];
879 CP_VLAN_TX_TAG(txd, vlan_tag);
880 txd->addr = cpu_to_le64(first_mapping);
881 wmb();
882
883 if (skb->ip_summed == CHECKSUM_HW) {
884 if (ip->protocol == IPPROTO_TCP)
885 txd->opts1 = cpu_to_le32(first_eor | first_len |
886 FirstFrag | DescOwn |
887 IPCS | TCPCS);
888 else if (ip->protocol == IPPROTO_UDP)
889 txd->opts1 = cpu_to_le32(first_eor | first_len |
890 FirstFrag | DescOwn |
891 IPCS | UDPCS);
892 else
893 BUG();
894 } else
895 txd->opts1 = cpu_to_le32(first_eor | first_len |
896 FirstFrag | DescOwn);
897 wmb();
898 }
899 cp->tx_head = entry;
900 if (netif_msg_tx_queued(cp))
901 printk(KERN_DEBUG "%s: tx queued, slot %d, skblen %d\n",
902 dev->name, entry, skb->len);
903 if (TX_BUFFS_AVAIL(cp) <= (MAX_SKB_FRAGS + 1))
904 netif_stop_queue(dev);
905
906 spin_unlock_irq(&cp->lock);
907
908 cpw8(TxPoll, NormalTxPoll);
909 dev->trans_start = jiffies;
910
911 return 0;
912}
913
914/* Set or clear the multicast filter for this adaptor.
915 This routine is not state sensitive and need not be SMP locked. */
916
917static void __cp_set_rx_mode (struct net_device *dev)
918{
919 struct cp_private *cp = netdev_priv(dev);
920 u32 mc_filter[2]; /* Multicast hash filter */
921 int i, rx_mode;
922 u32 tmp;
923
924 /* Note: do not reorder, GCC is clever about common statements. */
925 if (dev->flags & IFF_PROMISC) {
926 /* Unconditionally log net taps. */
927 printk (KERN_NOTICE "%s: Promiscuous mode enabled.\n",
928 dev->name);
929 rx_mode =
930 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
931 AcceptAllPhys;
932 mc_filter[1] = mc_filter[0] = 0xffffffff;
933 } else if ((dev->mc_count > multicast_filter_limit)
934 || (dev->flags & IFF_ALLMULTI)) {
935 /* Too many to filter perfectly -- accept all multicasts. */
936 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
937 mc_filter[1] = mc_filter[0] = 0xffffffff;
938 } else {
939 struct dev_mc_list *mclist;
940 rx_mode = AcceptBroadcast | AcceptMyPhys;
941 mc_filter[1] = mc_filter[0] = 0;
942 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
943 i++, mclist = mclist->next) {
944 int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
945
946 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
947 rx_mode |= AcceptMulticast;
948 }
949 }
950
951 /* We can safely update without stopping the chip. */
952 tmp = cp_rx_config | rx_mode;
953 if (cp->rx_config != tmp) {
954 cpw32_f (RxConfig, tmp);
955 cp->rx_config = tmp;
956 }
957 cpw32_f (MAR0 + 0, mc_filter[0]);
958 cpw32_f (MAR0 + 4, mc_filter[1]);
959}
960
961static void cp_set_rx_mode (struct net_device *dev)
962{
963 unsigned long flags;
964 struct cp_private *cp = netdev_priv(dev);
965
966 spin_lock_irqsave (&cp->lock, flags);
967 __cp_set_rx_mode(dev);
968 spin_unlock_irqrestore (&cp->lock, flags);
969}
970
971static void __cp_get_stats(struct cp_private *cp)
972{
973 /* only lower 24 bits valid; write any value to clear */
974 cp->net_stats.rx_missed_errors += (cpr32 (RxMissed) & 0xffffff);
975 cpw32 (RxMissed, 0);
976}
977
978static struct net_device_stats *cp_get_stats(struct net_device *dev)
979{
980 struct cp_private *cp = netdev_priv(dev);
981 unsigned long flags;
982
983 /* The chip only need report frame silently dropped. */
984 spin_lock_irqsave(&cp->lock, flags);
985 if (netif_running(dev) && netif_device_present(dev))
986 __cp_get_stats(cp);
987 spin_unlock_irqrestore(&cp->lock, flags);
988
989 return &cp->net_stats;
990}
991
992static void cp_stop_hw (struct cp_private *cp)
993{
994 cpw16(IntrStatus, ~(cpr16(IntrStatus)));
995 cpw16_f(IntrMask, 0);
996 cpw8(Cmd, 0);
997 cpw16_f(CpCmd, 0);
998 cpw16_f(IntrStatus, ~(cpr16(IntrStatus)));
999
1000 cp->rx_tail = 0;
1001 cp->tx_head = cp->tx_tail = 0;
1002}
1003
1004static void cp_reset_hw (struct cp_private *cp)
1005{
1006 unsigned work = 1000;
1007
1008 cpw8(Cmd, CmdReset);
1009
1010 while (work--) {
1011 if (!(cpr8(Cmd) & CmdReset))
1012 return;
1013
1014 set_current_state(TASK_UNINTERRUPTIBLE);
1015 schedule_timeout(10);
1016 }
1017
1018 printk(KERN_ERR "%s: hardware reset timeout\n", cp->dev->name);
1019}
1020
1021static inline void cp_start_hw (struct cp_private *cp)
1022{
1023 cpw16(CpCmd, cp->cpcmd);
1024 cpw8(Cmd, RxOn | TxOn);
1025}
1026
1027static void cp_init_hw (struct cp_private *cp)
1028{
1029 struct net_device *dev = cp->dev;
1030 dma_addr_t ring_dma;
1031
1032 cp_reset_hw(cp);
1033
1034 cpw8_f (Cfg9346, Cfg9346_Unlock);
1035
1036 /* Restore our idea of the MAC address. */
1037 cpw32_f (MAC0 + 0, cpu_to_le32 (*(u32 *) (dev->dev_addr + 0)));
1038 cpw32_f (MAC0 + 4, cpu_to_le32 (*(u32 *) (dev->dev_addr + 4)));
1039
1040 cp_start_hw(cp);
1041 cpw8(TxThresh, 0x06); /* XXX convert magic num to a constant */
1042
1043 __cp_set_rx_mode(dev);
1044 cpw32_f (TxConfig, IFG | (TX_DMA_BURST << TxDMAShift));
1045
1046 cpw8(Config1, cpr8(Config1) | DriverLoaded | PMEnable);
1047 /* Disable Wake-on-LAN. Can be turned on with ETHTOOL_SWOL */
1048 cpw8(Config3, PARMEnable);
1049 cp->wol_enabled = 0;
1050
1051 cpw8(Config5, cpr8(Config5) & PMEStatus);
1052
1053 cpw32_f(HiTxRingAddr, 0);
1054 cpw32_f(HiTxRingAddr + 4, 0);
1055
1056 ring_dma = cp->ring_dma;
1057 cpw32_f(RxRingAddr, ring_dma & 0xffffffff);
1058 cpw32_f(RxRingAddr + 4, (ring_dma >> 16) >> 16);
1059
1060 ring_dma += sizeof(struct cp_desc) * CP_RX_RING_SIZE;
1061 cpw32_f(TxRingAddr, ring_dma & 0xffffffff);
1062 cpw32_f(TxRingAddr + 4, (ring_dma >> 16) >> 16);
1063
1064 cpw16(MultiIntr, 0);
1065
1066 cpw16_f(IntrMask, cp_intr_mask);
1067
1068 cpw8_f(Cfg9346, Cfg9346_Lock);
1069}
1070
1071static int cp_refill_rx (struct cp_private *cp)
1072{
1073 unsigned i;
1074
1075 for (i = 0; i < CP_RX_RING_SIZE; i++) {
1076 struct sk_buff *skb;
1077
1078 skb = dev_alloc_skb(cp->rx_buf_sz + RX_OFFSET);
1079 if (!skb)
1080 goto err_out;
1081
1082 skb->dev = cp->dev;
1083 skb_reserve(skb, RX_OFFSET);
1084
1085 cp->rx_skb[i].mapping = pci_map_single(cp->pdev,
1086 skb->tail, cp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1087 cp->rx_skb[i].skb = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 cp->rx_ring[i].opts2 = 0;
1090 cp->rx_ring[i].addr = cpu_to_le64(cp->rx_skb[i].mapping);
1091 if (i == (CP_RX_RING_SIZE - 1))
1092 cp->rx_ring[i].opts1 =
1093 cpu_to_le32(DescOwn | RingEnd | cp->rx_buf_sz);
1094 else
1095 cp->rx_ring[i].opts1 =
1096 cpu_to_le32(DescOwn | cp->rx_buf_sz);
1097 }
1098
1099 return 0;
1100
1101err_out:
1102 cp_clean_rings(cp);
1103 return -ENOMEM;
1104}
1105
1106static int cp_init_rings (struct cp_private *cp)
1107{
1108 memset(cp->tx_ring, 0, sizeof(struct cp_desc) * CP_TX_RING_SIZE);
1109 cp->tx_ring[CP_TX_RING_SIZE - 1].opts1 = cpu_to_le32(RingEnd);
1110
1111 cp->rx_tail = 0;
1112 cp->tx_head = cp->tx_tail = 0;
1113
1114 return cp_refill_rx (cp);
1115}
1116
1117static int cp_alloc_rings (struct cp_private *cp)
1118{
1119 void *mem;
1120
1121 mem = pci_alloc_consistent(cp->pdev, CP_RING_BYTES, &cp->ring_dma);
1122 if (!mem)
1123 return -ENOMEM;
1124
1125 cp->rx_ring = mem;
1126 cp->tx_ring = &cp->rx_ring[CP_RX_RING_SIZE];
1127
1128 mem += (CP_RING_BYTES - CP_STATS_SIZE);
1129 cp->nic_stats = mem;
1130 cp->nic_stats_dma = cp->ring_dma + (CP_RING_BYTES - CP_STATS_SIZE);
1131
1132 return cp_init_rings(cp);
1133}
1134
1135static void cp_clean_rings (struct cp_private *cp)
1136{
1137 unsigned i;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 for (i = 0; i < CP_RX_RING_SIZE; i++) {
1140 if (cp->rx_skb[i].skb) {
1141 pci_unmap_single(cp->pdev, cp->rx_skb[i].mapping,
1142 cp->rx_buf_sz, PCI_DMA_FROMDEVICE);
1143 dev_kfree_skb(cp->rx_skb[i].skb);
1144 }
1145 }
1146
1147 for (i = 0; i < CP_TX_RING_SIZE; i++) {
1148 if (cp->tx_skb[i].skb) {
1149 struct sk_buff *skb = cp->tx_skb[i].skb;
Francois Romieu57344182005-05-12 19:31:31 -04001150
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 pci_unmap_single(cp->pdev, cp->tx_skb[i].mapping,
Francois Romieu57344182005-05-12 19:31:31 -04001152 cp->tx_skb[i].len, PCI_DMA_TODEVICE);
1153 if (le32_to_cpu(cp->tx_ring[i].opts1) & LastFrag)
1154 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 cp->net_stats.tx_dropped++;
1156 }
1157 }
1158
Francois Romieu57344182005-05-12 19:31:31 -04001159 memset(cp->rx_ring, 0, sizeof(struct cp_desc) * CP_RX_RING_SIZE);
1160 memset(cp->tx_ring, 0, sizeof(struct cp_desc) * CP_TX_RING_SIZE);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 memset(&cp->rx_skb, 0, sizeof(struct ring_info) * CP_RX_RING_SIZE);
1163 memset(&cp->tx_skb, 0, sizeof(struct ring_info) * CP_TX_RING_SIZE);
1164}
1165
1166static void cp_free_rings (struct cp_private *cp)
1167{
1168 cp_clean_rings(cp);
1169 pci_free_consistent(cp->pdev, CP_RING_BYTES, cp->rx_ring, cp->ring_dma);
1170 cp->rx_ring = NULL;
1171 cp->tx_ring = NULL;
1172 cp->nic_stats = NULL;
1173}
1174
1175static int cp_open (struct net_device *dev)
1176{
1177 struct cp_private *cp = netdev_priv(dev);
1178 int rc;
1179
1180 if (netif_msg_ifup(cp))
1181 printk(KERN_DEBUG "%s: enabling interface\n", dev->name);
1182
1183 rc = cp_alloc_rings(cp);
1184 if (rc)
1185 return rc;
1186
1187 cp_init_hw(cp);
1188
1189 rc = request_irq(dev->irq, cp_interrupt, SA_SHIRQ, dev->name, dev);
1190 if (rc)
1191 goto err_out_hw;
1192
1193 netif_carrier_off(dev);
1194 mii_check_media(&cp->mii_if, netif_msg_link(cp), TRUE);
1195 netif_start_queue(dev);
1196
1197 return 0;
1198
1199err_out_hw:
1200 cp_stop_hw(cp);
1201 cp_free_rings(cp);
1202 return rc;
1203}
1204
1205static int cp_close (struct net_device *dev)
1206{
1207 struct cp_private *cp = netdev_priv(dev);
1208 unsigned long flags;
1209
1210 if (netif_msg_ifdown(cp))
1211 printk(KERN_DEBUG "%s: disabling interface\n", dev->name);
1212
1213 spin_lock_irqsave(&cp->lock, flags);
1214
1215 netif_stop_queue(dev);
1216 netif_carrier_off(dev);
1217
1218 cp_stop_hw(cp);
1219
1220 spin_unlock_irqrestore(&cp->lock, flags);
1221
1222 synchronize_irq(dev->irq);
1223 free_irq(dev->irq, dev);
1224
1225 cp_free_rings(cp);
1226 return 0;
1227}
1228
1229#ifdef BROKEN
1230static int cp_change_mtu(struct net_device *dev, int new_mtu)
1231{
1232 struct cp_private *cp = netdev_priv(dev);
1233 int rc;
1234 unsigned long flags;
1235
1236 /* check for invalid MTU, according to hardware limits */
1237 if (new_mtu < CP_MIN_MTU || new_mtu > CP_MAX_MTU)
1238 return -EINVAL;
1239
1240 /* if network interface not up, no need for complexity */
1241 if (!netif_running(dev)) {
1242 dev->mtu = new_mtu;
1243 cp_set_rxbufsize(cp); /* set new rx buf size */
1244 return 0;
1245 }
1246
1247 spin_lock_irqsave(&cp->lock, flags);
1248
1249 cp_stop_hw(cp); /* stop h/w and free rings */
1250 cp_clean_rings(cp);
1251
1252 dev->mtu = new_mtu;
1253 cp_set_rxbufsize(cp); /* set new rx buf size */
1254
1255 rc = cp_init_rings(cp); /* realloc and restart h/w */
1256 cp_start_hw(cp);
1257
1258 spin_unlock_irqrestore(&cp->lock, flags);
1259
1260 return rc;
1261}
1262#endif /* BROKEN */
1263
1264static char mii_2_8139_map[8] = {
1265 BasicModeCtrl,
1266 BasicModeStatus,
1267 0,
1268 0,
1269 NWayAdvert,
1270 NWayLPAR,
1271 NWayExpansion,
1272 0
1273};
1274
1275static int mdio_read(struct net_device *dev, int phy_id, int location)
1276{
1277 struct cp_private *cp = netdev_priv(dev);
1278
1279 return location < 8 && mii_2_8139_map[location] ?
1280 readw(cp->regs + mii_2_8139_map[location]) : 0;
1281}
1282
1283
1284static void mdio_write(struct net_device *dev, int phy_id, int location,
1285 int value)
1286{
1287 struct cp_private *cp = netdev_priv(dev);
1288
1289 if (location == 0) {
1290 cpw8(Cfg9346, Cfg9346_Unlock);
1291 cpw16(BasicModeCtrl, value);
1292 cpw8(Cfg9346, Cfg9346_Lock);
1293 } else if (location < 8 && mii_2_8139_map[location])
1294 cpw16(mii_2_8139_map[location], value);
1295}
1296
1297/* Set the ethtool Wake-on-LAN settings */
1298static int netdev_set_wol (struct cp_private *cp,
1299 const struct ethtool_wolinfo *wol)
1300{
1301 u8 options;
1302
1303 options = cpr8 (Config3) & ~(LinkUp | MagicPacket);
1304 /* If WOL is being disabled, no need for complexity */
1305 if (wol->wolopts) {
1306 if (wol->wolopts & WAKE_PHY) options |= LinkUp;
1307 if (wol->wolopts & WAKE_MAGIC) options |= MagicPacket;
1308 }
1309
1310 cpw8 (Cfg9346, Cfg9346_Unlock);
1311 cpw8 (Config3, options);
1312 cpw8 (Cfg9346, Cfg9346_Lock);
1313
1314 options = 0; /* Paranoia setting */
1315 options = cpr8 (Config5) & ~(UWF | MWF | BWF);
1316 /* If WOL is being disabled, no need for complexity */
1317 if (wol->wolopts) {
1318 if (wol->wolopts & WAKE_UCAST) options |= UWF;
1319 if (wol->wolopts & WAKE_BCAST) options |= BWF;
1320 if (wol->wolopts & WAKE_MCAST) options |= MWF;
1321 }
1322
1323 cpw8 (Config5, options);
1324
1325 cp->wol_enabled = (wol->wolopts) ? 1 : 0;
1326
1327 return 0;
1328}
1329
1330/* Get the ethtool Wake-on-LAN settings */
1331static void netdev_get_wol (struct cp_private *cp,
1332 struct ethtool_wolinfo *wol)
1333{
1334 u8 options;
1335
1336 wol->wolopts = 0; /* Start from scratch */
1337 wol->supported = WAKE_PHY | WAKE_BCAST | WAKE_MAGIC |
1338 WAKE_MCAST | WAKE_UCAST;
1339 /* We don't need to go on if WOL is disabled */
1340 if (!cp->wol_enabled) return;
1341
1342 options = cpr8 (Config3);
1343 if (options & LinkUp) wol->wolopts |= WAKE_PHY;
1344 if (options & MagicPacket) wol->wolopts |= WAKE_MAGIC;
1345
1346 options = 0; /* Paranoia setting */
1347 options = cpr8 (Config5);
1348 if (options & UWF) wol->wolopts |= WAKE_UCAST;
1349 if (options & BWF) wol->wolopts |= WAKE_BCAST;
1350 if (options & MWF) wol->wolopts |= WAKE_MCAST;
1351}
1352
1353static void cp_get_drvinfo (struct net_device *dev, struct ethtool_drvinfo *info)
1354{
1355 struct cp_private *cp = netdev_priv(dev);
1356
1357 strcpy (info->driver, DRV_NAME);
1358 strcpy (info->version, DRV_VERSION);
1359 strcpy (info->bus_info, pci_name(cp->pdev));
1360}
1361
1362static int cp_get_regs_len(struct net_device *dev)
1363{
1364 return CP_REGS_SIZE;
1365}
1366
1367static int cp_get_stats_count (struct net_device *dev)
1368{
1369 return CP_NUM_STATS;
1370}
1371
1372static int cp_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1373{
1374 struct cp_private *cp = netdev_priv(dev);
1375 int rc;
1376 unsigned long flags;
1377
1378 spin_lock_irqsave(&cp->lock, flags);
1379 rc = mii_ethtool_gset(&cp->mii_if, cmd);
1380 spin_unlock_irqrestore(&cp->lock, flags);
1381
1382 return rc;
1383}
1384
1385static int cp_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1386{
1387 struct cp_private *cp = netdev_priv(dev);
1388 int rc;
1389 unsigned long flags;
1390
1391 spin_lock_irqsave(&cp->lock, flags);
1392 rc = mii_ethtool_sset(&cp->mii_if, cmd);
1393 spin_unlock_irqrestore(&cp->lock, flags);
1394
1395 return rc;
1396}
1397
1398static int cp_nway_reset(struct net_device *dev)
1399{
1400 struct cp_private *cp = netdev_priv(dev);
1401 return mii_nway_restart(&cp->mii_if);
1402}
1403
1404static u32 cp_get_msglevel(struct net_device *dev)
1405{
1406 struct cp_private *cp = netdev_priv(dev);
1407 return cp->msg_enable;
1408}
1409
1410static void cp_set_msglevel(struct net_device *dev, u32 value)
1411{
1412 struct cp_private *cp = netdev_priv(dev);
1413 cp->msg_enable = value;
1414}
1415
1416static u32 cp_get_rx_csum(struct net_device *dev)
1417{
1418 struct cp_private *cp = netdev_priv(dev);
1419 return (cpr16(CpCmd) & RxChkSum) ? 1 : 0;
1420}
1421
1422static int cp_set_rx_csum(struct net_device *dev, u32 data)
1423{
1424 struct cp_private *cp = netdev_priv(dev);
1425 u16 cmd = cp->cpcmd, newcmd;
1426
1427 newcmd = cmd;
1428
1429 if (data)
1430 newcmd |= RxChkSum;
1431 else
1432 newcmd &= ~RxChkSum;
1433
1434 if (newcmd != cmd) {
1435 unsigned long flags;
1436
1437 spin_lock_irqsave(&cp->lock, flags);
1438 cp->cpcmd = newcmd;
1439 cpw16_f(CpCmd, newcmd);
1440 spin_unlock_irqrestore(&cp->lock, flags);
1441 }
1442
1443 return 0;
1444}
1445
1446static void cp_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1447 void *p)
1448{
1449 struct cp_private *cp = netdev_priv(dev);
1450 unsigned long flags;
1451
1452 if (regs->len < CP_REGS_SIZE)
1453 return /* -EINVAL */;
1454
1455 regs->version = CP_REGS_VER;
1456
1457 spin_lock_irqsave(&cp->lock, flags);
1458 memcpy_fromio(p, cp->regs, CP_REGS_SIZE);
1459 spin_unlock_irqrestore(&cp->lock, flags);
1460}
1461
1462static void cp_get_wol (struct net_device *dev, struct ethtool_wolinfo *wol)
1463{
1464 struct cp_private *cp = netdev_priv(dev);
1465 unsigned long flags;
1466
1467 spin_lock_irqsave (&cp->lock, flags);
1468 netdev_get_wol (cp, wol);
1469 spin_unlock_irqrestore (&cp->lock, flags);
1470}
1471
1472static int cp_set_wol (struct net_device *dev, struct ethtool_wolinfo *wol)
1473{
1474 struct cp_private *cp = netdev_priv(dev);
1475 unsigned long flags;
1476 int rc;
1477
1478 spin_lock_irqsave (&cp->lock, flags);
1479 rc = netdev_set_wol (cp, wol);
1480 spin_unlock_irqrestore (&cp->lock, flags);
1481
1482 return rc;
1483}
1484
1485static void cp_get_strings (struct net_device *dev, u32 stringset, u8 *buf)
1486{
1487 switch (stringset) {
1488 case ETH_SS_STATS:
1489 memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
1490 break;
1491 default:
1492 BUG();
1493 break;
1494 }
1495}
1496
1497static void cp_get_ethtool_stats (struct net_device *dev,
1498 struct ethtool_stats *estats, u64 *tmp_stats)
1499{
1500 struct cp_private *cp = netdev_priv(dev);
1501 unsigned int work = 100;
1502 int i;
1503
1504 /* begin NIC statistics dump */
1505 cpw32(StatsAddr + 4, (cp->nic_stats_dma >> 16) >> 16);
1506 cpw32(StatsAddr, (cp->nic_stats_dma & 0xffffffff) | DumpStats);
1507 cpr32(StatsAddr);
1508
1509 while (work-- > 0) {
1510 if ((cpr32(StatsAddr) & DumpStats) == 0)
1511 break;
1512 cpu_relax();
1513 }
1514
1515 if (cpr32(StatsAddr) & DumpStats)
1516 return /* -EIO */;
1517
1518 i = 0;
1519 tmp_stats[i++] = le64_to_cpu(cp->nic_stats->tx_ok);
1520 tmp_stats[i++] = le64_to_cpu(cp->nic_stats->rx_ok);
1521 tmp_stats[i++] = le64_to_cpu(cp->nic_stats->tx_err);
1522 tmp_stats[i++] = le32_to_cpu(cp->nic_stats->rx_err);
1523 tmp_stats[i++] = le16_to_cpu(cp->nic_stats->rx_fifo);
1524 tmp_stats[i++] = le16_to_cpu(cp->nic_stats->frame_align);
1525 tmp_stats[i++] = le32_to_cpu(cp->nic_stats->tx_ok_1col);
1526 tmp_stats[i++] = le32_to_cpu(cp->nic_stats->tx_ok_mcol);
1527 tmp_stats[i++] = le64_to_cpu(cp->nic_stats->rx_ok_phys);
1528 tmp_stats[i++] = le64_to_cpu(cp->nic_stats->rx_ok_bcast);
1529 tmp_stats[i++] = le32_to_cpu(cp->nic_stats->rx_ok_mcast);
1530 tmp_stats[i++] = le16_to_cpu(cp->nic_stats->tx_abort);
1531 tmp_stats[i++] = le16_to_cpu(cp->nic_stats->tx_underrun);
1532 tmp_stats[i++] = cp->cp_stats.rx_frags;
1533 if (i != CP_NUM_STATS)
1534 BUG();
1535}
1536
1537static struct ethtool_ops cp_ethtool_ops = {
1538 .get_drvinfo = cp_get_drvinfo,
1539 .get_regs_len = cp_get_regs_len,
1540 .get_stats_count = cp_get_stats_count,
1541 .get_settings = cp_get_settings,
1542 .set_settings = cp_set_settings,
1543 .nway_reset = cp_nway_reset,
1544 .get_link = ethtool_op_get_link,
1545 .get_msglevel = cp_get_msglevel,
1546 .set_msglevel = cp_set_msglevel,
1547 .get_rx_csum = cp_get_rx_csum,
1548 .set_rx_csum = cp_set_rx_csum,
1549 .get_tx_csum = ethtool_op_get_tx_csum,
1550 .set_tx_csum = ethtool_op_set_tx_csum, /* local! */
1551 .get_sg = ethtool_op_get_sg,
1552 .set_sg = ethtool_op_set_sg,
Jeff Garzikfcec3452005-05-12 19:28:49 -04001553 .get_tso = ethtool_op_get_tso,
1554 .set_tso = ethtool_op_set_tso,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 .get_regs = cp_get_regs,
1556 .get_wol = cp_get_wol,
1557 .set_wol = cp_set_wol,
1558 .get_strings = cp_get_strings,
1559 .get_ethtool_stats = cp_get_ethtool_stats,
1560};
1561
1562static int cp_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1563{
1564 struct cp_private *cp = netdev_priv(dev);
1565 int rc;
1566 unsigned long flags;
1567
1568 if (!netif_running(dev))
1569 return -EINVAL;
1570
1571 spin_lock_irqsave(&cp->lock, flags);
1572 rc = generic_mii_ioctl(&cp->mii_if, if_mii(rq), cmd, NULL);
1573 spin_unlock_irqrestore(&cp->lock, flags);
1574 return rc;
1575}
1576
1577/* Serial EEPROM section. */
1578
1579/* EEPROM_Ctrl bits. */
1580#define EE_SHIFT_CLK 0x04 /* EEPROM shift clock. */
1581#define EE_CS 0x08 /* EEPROM chip select. */
1582#define EE_DATA_WRITE 0x02 /* EEPROM chip data in. */
1583#define EE_WRITE_0 0x00
1584#define EE_WRITE_1 0x02
1585#define EE_DATA_READ 0x01 /* EEPROM chip data out. */
1586#define EE_ENB (0x80 | EE_CS)
1587
1588/* Delay between EEPROM clock transitions.
1589 No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
1590 */
1591
1592#define eeprom_delay() readl(ee_addr)
1593
1594/* The EEPROM commands include the alway-set leading bit. */
1595#define EE_WRITE_CMD (5)
1596#define EE_READ_CMD (6)
1597#define EE_ERASE_CMD (7)
1598
1599static int read_eeprom (void __iomem *ioaddr, int location, int addr_len)
1600{
1601 int i;
1602 unsigned retval = 0;
1603 void __iomem *ee_addr = ioaddr + Cfg9346;
1604 int read_cmd = location | (EE_READ_CMD << addr_len);
1605
1606 writeb (EE_ENB & ~EE_CS, ee_addr);
1607 writeb (EE_ENB, ee_addr);
1608 eeprom_delay ();
1609
1610 /* Shift the read command bits out. */
1611 for (i = 4 + addr_len; i >= 0; i--) {
1612 int dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
1613 writeb (EE_ENB | dataval, ee_addr);
1614 eeprom_delay ();
1615 writeb (EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
1616 eeprom_delay ();
1617 }
1618 writeb (EE_ENB, ee_addr);
1619 eeprom_delay ();
1620
1621 for (i = 16; i > 0; i--) {
1622 writeb (EE_ENB | EE_SHIFT_CLK, ee_addr);
1623 eeprom_delay ();
1624 retval =
1625 (retval << 1) | ((readb (ee_addr) & EE_DATA_READ) ? 1 :
1626 0);
1627 writeb (EE_ENB, ee_addr);
1628 eeprom_delay ();
1629 }
1630
1631 /* Terminate the EEPROM access. */
1632 writeb (~EE_CS, ee_addr);
1633 eeprom_delay ();
1634
1635 return retval;
1636}
1637
1638/* Put the board into D3cold state and wait for WakeUp signal */
1639static void cp_set_d3_state (struct cp_private *cp)
1640{
1641 pci_enable_wake (cp->pdev, 0, 1); /* Enable PME# generation */
1642 pci_set_power_state (cp->pdev, PCI_D3hot);
1643}
1644
1645static int cp_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1646{
1647 struct net_device *dev;
1648 struct cp_private *cp;
1649 int rc;
1650 void __iomem *regs;
1651 long pciaddr;
1652 unsigned int addr_len, i, pci_using_dac;
1653 u8 pci_rev;
1654
1655#ifndef MODULE
1656 static int version_printed;
1657 if (version_printed++ == 0)
1658 printk("%s", version);
1659#endif
1660
1661 pci_read_config_byte(pdev, PCI_REVISION_ID, &pci_rev);
1662
1663 if (pdev->vendor == PCI_VENDOR_ID_REALTEK &&
1664 pdev->device == PCI_DEVICE_ID_REALTEK_8139 && pci_rev < 0x20) {
1665 printk(KERN_ERR PFX "pci dev %s (id %04x:%04x rev %02x) is not an 8139C+ compatible chip\n",
1666 pci_name(pdev), pdev->vendor, pdev->device, pci_rev);
1667 printk(KERN_ERR PFX "Try the \"8139too\" driver instead.\n");
1668 return -ENODEV;
1669 }
1670
1671 dev = alloc_etherdev(sizeof(struct cp_private));
1672 if (!dev)
1673 return -ENOMEM;
1674 SET_MODULE_OWNER(dev);
1675 SET_NETDEV_DEV(dev, &pdev->dev);
1676
1677 cp = netdev_priv(dev);
1678 cp->pdev = pdev;
1679 cp->dev = dev;
1680 cp->msg_enable = (debug < 0 ? CP_DEF_MSG_ENABLE : debug);
1681 spin_lock_init (&cp->lock);
1682 cp->mii_if.dev = dev;
1683 cp->mii_if.mdio_read = mdio_read;
1684 cp->mii_if.mdio_write = mdio_write;
1685 cp->mii_if.phy_id = CP_INTERNAL_PHY;
1686 cp->mii_if.phy_id_mask = 0x1f;
1687 cp->mii_if.reg_num_mask = 0x1f;
1688 cp_set_rxbufsize(cp);
1689
1690 rc = pci_enable_device(pdev);
1691 if (rc)
1692 goto err_out_free;
1693
1694 rc = pci_set_mwi(pdev);
1695 if (rc)
1696 goto err_out_disable;
1697
1698 rc = pci_request_regions(pdev, DRV_NAME);
1699 if (rc)
1700 goto err_out_mwi;
1701
1702 pciaddr = pci_resource_start(pdev, 1);
1703 if (!pciaddr) {
1704 rc = -EIO;
1705 printk(KERN_ERR PFX "no MMIO resource for pci dev %s\n",
1706 pci_name(pdev));
1707 goto err_out_res;
1708 }
1709 if (pci_resource_len(pdev, 1) < CP_REGS_SIZE) {
1710 rc = -EIO;
1711 printk(KERN_ERR PFX "MMIO resource (%lx) too small on pci dev %s\n",
1712 pci_resource_len(pdev, 1), pci_name(pdev));
1713 goto err_out_res;
1714 }
1715
1716 /* Configure DMA attributes. */
1717 if ((sizeof(dma_addr_t) > 4) &&
1718 !pci_set_consistent_dma_mask(pdev, 0xffffffffffffffffULL) &&
1719 !pci_set_dma_mask(pdev, 0xffffffffffffffffULL)) {
1720 pci_using_dac = 1;
1721 } else {
1722 pci_using_dac = 0;
1723
1724 rc = pci_set_dma_mask(pdev, 0xffffffffULL);
1725 if (rc) {
1726 printk(KERN_ERR PFX "No usable DMA configuration, "
1727 "aborting.\n");
1728 goto err_out_res;
1729 }
1730 rc = pci_set_consistent_dma_mask(pdev, 0xffffffffULL);
1731 if (rc) {
1732 printk(KERN_ERR PFX "No usable consistent DMA configuration, "
1733 "aborting.\n");
1734 goto err_out_res;
1735 }
1736 }
1737
1738 cp->cpcmd = (pci_using_dac ? PCIDAC : 0) |
1739 PCIMulRW | RxChkSum | CpRxOn | CpTxOn;
1740
1741 regs = ioremap(pciaddr, CP_REGS_SIZE);
1742 if (!regs) {
1743 rc = -EIO;
1744 printk(KERN_ERR PFX "Cannot map PCI MMIO (%lx@%lx) on pci dev %s\n",
1745 pci_resource_len(pdev, 1), pciaddr, pci_name(pdev));
1746 goto err_out_res;
1747 }
1748 dev->base_addr = (unsigned long) regs;
1749 cp->regs = regs;
1750
1751 cp_stop_hw(cp);
1752
1753 /* read MAC address from EEPROM */
1754 addr_len = read_eeprom (regs, 0, 8) == 0x8129 ? 8 : 6;
1755 for (i = 0; i < 3; i++)
1756 ((u16 *) (dev->dev_addr))[i] =
1757 le16_to_cpu (read_eeprom (regs, i + 7, addr_len));
1758
1759 dev->open = cp_open;
1760 dev->stop = cp_close;
1761 dev->set_multicast_list = cp_set_rx_mode;
1762 dev->hard_start_xmit = cp_start_xmit;
1763 dev->get_stats = cp_get_stats;
1764 dev->do_ioctl = cp_ioctl;
1765 dev->poll = cp_rx_poll;
1766 dev->weight = 16; /* arbitrary? from NAPI_HOWTO.txt. */
1767#ifdef BROKEN
1768 dev->change_mtu = cp_change_mtu;
1769#endif
1770 dev->ethtool_ops = &cp_ethtool_ops;
1771#if 0
1772 dev->tx_timeout = cp_tx_timeout;
1773 dev->watchdog_timeo = TX_TIMEOUT;
1774#endif
1775
1776#if CP_VLAN_TAG_USED
1777 dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
1778 dev->vlan_rx_register = cp_vlan_rx_register;
1779 dev->vlan_rx_kill_vid = cp_vlan_rx_kill_vid;
1780#endif
1781
1782 if (pci_using_dac)
1783 dev->features |= NETIF_F_HIGHDMA;
1784
Jeff Garzikfcec3452005-05-12 19:28:49 -04001785#if 0 /* disabled by default until verified */
1786 dev->features |= NETIF_F_TSO;
1787#endif
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 dev->irq = pdev->irq;
1790
1791 rc = register_netdev(dev);
1792 if (rc)
1793 goto err_out_iomap;
1794
1795 printk (KERN_INFO "%s: RTL-8139C+ at 0x%lx, "
1796 "%02x:%02x:%02x:%02x:%02x:%02x, "
1797 "IRQ %d\n",
1798 dev->name,
1799 dev->base_addr,
1800 dev->dev_addr[0], dev->dev_addr[1],
1801 dev->dev_addr[2], dev->dev_addr[3],
1802 dev->dev_addr[4], dev->dev_addr[5],
1803 dev->irq);
1804
1805 pci_set_drvdata(pdev, dev);
1806
1807 /* enable busmastering and memory-write-invalidate */
1808 pci_set_master(pdev);
1809
1810 if (cp->wol_enabled) cp_set_d3_state (cp);
1811
1812 return 0;
1813
1814err_out_iomap:
1815 iounmap(regs);
1816err_out_res:
1817 pci_release_regions(pdev);
1818err_out_mwi:
1819 pci_clear_mwi(pdev);
1820err_out_disable:
1821 pci_disable_device(pdev);
1822err_out_free:
1823 free_netdev(dev);
1824 return rc;
1825}
1826
1827static void cp_remove_one (struct pci_dev *pdev)
1828{
1829 struct net_device *dev = pci_get_drvdata(pdev);
1830 struct cp_private *cp = netdev_priv(dev);
1831
1832 if (!dev)
1833 BUG();
1834 unregister_netdev(dev);
1835 iounmap(cp->regs);
1836 if (cp->wol_enabled) pci_set_power_state (pdev, PCI_D0);
1837 pci_release_regions(pdev);
1838 pci_clear_mwi(pdev);
1839 pci_disable_device(pdev);
1840 pci_set_drvdata(pdev, NULL);
1841 free_netdev(dev);
1842}
1843
1844#ifdef CONFIG_PM
Pavel Machek05adc3b2005-04-16 15:25:25 -07001845static int cp_suspend (struct pci_dev *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 struct net_device *dev;
1848 struct cp_private *cp;
1849 unsigned long flags;
1850
1851 dev = pci_get_drvdata (pdev);
1852 cp = netdev_priv(dev);
1853
1854 if (!dev || !netif_running (dev)) return 0;
1855
1856 netif_device_detach (dev);
1857 netif_stop_queue (dev);
1858
1859 spin_lock_irqsave (&cp->lock, flags);
1860
1861 /* Disable Rx and Tx */
1862 cpw16 (IntrMask, 0);
1863 cpw8 (Cmd, cpr8 (Cmd) & (~RxOn | ~TxOn));
1864
1865 spin_unlock_irqrestore (&cp->lock, flags);
1866
1867 if (cp->pdev && cp->wol_enabled) {
1868 pci_save_state (cp->pdev);
1869 cp_set_d3_state (cp);
1870 }
1871
1872 return 0;
1873}
1874
1875static int cp_resume (struct pci_dev *pdev)
1876{
1877 struct net_device *dev;
1878 struct cp_private *cp;
1879
1880 dev = pci_get_drvdata (pdev);
1881 cp = netdev_priv(dev);
1882
1883 netif_device_attach (dev);
1884
1885 if (cp->pdev && cp->wol_enabled) {
1886 pci_set_power_state (cp->pdev, PCI_D0);
1887 pci_restore_state (cp->pdev);
1888 }
1889
1890 cp_init_hw (cp);
1891 netif_start_queue (dev);
1892
1893 return 0;
1894}
1895#endif /* CONFIG_PM */
1896
1897static struct pci_driver cp_driver = {
1898 .name = DRV_NAME,
1899 .id_table = cp_pci_tbl,
1900 .probe = cp_init_one,
1901 .remove = cp_remove_one,
1902#ifdef CONFIG_PM
1903 .resume = cp_resume,
1904 .suspend = cp_suspend,
1905#endif
1906};
1907
1908static int __init cp_init (void)
1909{
1910#ifdef MODULE
1911 printk("%s", version);
1912#endif
1913 return pci_module_init (&cp_driver);
1914}
1915
1916static void __exit cp_exit (void)
1917{
1918 pci_unregister_driver (&cp_driver);
1919}
1920
1921module_init(cp_init);
1922module_exit(cp_exit);