blob: 9c1ce318b077b58432339cdbbdcd3480221ede82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2
3 drivers/net/pci-skeleton.c
4
5 Maintained by Jeff Garzik <jgarzik@pobox.com>
6
7 Original code came from 8139too.c, which in turns was based
8 originally on Donald Becker's rtl8139.c driver, versions 1.11
9 and older. This driver was originally based on rtl8139.c
10 version 1.07. Header of rtl8139.c version 1.11:
11
12 -----<snip>-----
13
14 Written 1997-2000 by Donald Becker.
15 This software may be used and distributed according to the
16 terms of the GNU General Public License (GPL), incorporated
17 herein by reference. Drivers based on or derived from this
18 code fall under the GPL and must retain the authorship,
19 copyright and license notice. This file is not a complete
20 program and may only be used when the entire operating
21 system is licensed under the GPL.
22
23 This driver is for boards based on the RTL8129 and RTL8139
24 PCI ethernet chips.
25
26 The author may be reached as becker@scyld.com, or C/O Scyld
27 Computing Corporation 410 Severn Ave., Suite 210 Annapolis
28 MD 21403
29
30 Support and updates available at
31 http://www.scyld.com/network/rtl8139.html
32
33 Twister-tuning table provided by Kinston
34 <shangh@realtek.com.tw>.
35
36 -----<snip>-----
37
38 This software may be used and distributed according to the terms
39 of the GNU General Public License, incorporated herein by reference.
40
41
42-----------------------------------------------------------------------------
43
44 Theory of Operation
45
46I. Board Compatibility
47
48This device driver is designed for the RealTek RTL8139 series, the RealTek
49Fast Ethernet controllers for PCI and CardBus. This chip is used on many
50low-end boards, sometimes with its markings changed.
51
52
53II. Board-specific settings
54
55PCI bus devices are configured by the system at boot time, so no jumpers
56need to be set on the board. The system BIOS will assign the
57PCI INTA signal to a (preferably otherwise unused) system IRQ line.
58
59III. Driver operation
60
61IIIa. Rx Ring buffers
62
63The receive unit uses a single linear ring buffer rather than the more
64common (and more efficient) descriptor-based architecture. Incoming frames
65are sequentially stored into the Rx region, and the host copies them into
66skbuffs.
67
68Comment: While it is theoretically possible to process many frames in place,
69any delay in Rx processing would cause us to drop frames. More importantly,
70the Linux protocol stack is not designed to operate in this manner.
71
72IIIb. Tx operation
73
74The RTL8139 uses a fixed set of four Tx descriptors in register space.
75In a stunningly bad design choice, Tx frames must be 32 bit aligned. Linux
76aligns the IP header on word boundaries, and 14 byte ethernet header means
77that almost all frames will need to be copied to an alignment buffer.
78
79IVb. References
80
81http://www.realtek.com.tw/cn/cn.html
82http://www.scyld.com/expert/NWay.html
83
84IVc. Errata
85
86*/
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/module.h>
89#include <linux/kernel.h>
90#include <linux/pci.h>
91#include <linux/init.h>
92#include <linux/ioport.h>
93#include <linux/netdevice.h>
94#include <linux/etherdevice.h>
95#include <linux/delay.h>
96#include <linux/ethtool.h>
97#include <linux/mii.h>
98#include <linux/crc32.h>
99#include <asm/io.h>
100
101#define NETDRV_VERSION "1.0.0"
102#define MODNAME "netdrv"
103#define NETDRV_DRIVER_LOAD_MSG "MyVendor Fast Ethernet driver " NETDRV_VERSION " loaded"
104#define PFX MODNAME ": "
105
106static char version[] __devinitdata =
107KERN_INFO NETDRV_DRIVER_LOAD_MSG "\n"
108KERN_INFO " Support available from http://foo.com/bar/baz.html\n";
109
110/* define to 1 to enable PIO instead of MMIO */
111#undef USE_IO_OPS
112
113/* define to 1 to enable copious debugging info */
114#undef NETDRV_DEBUG
115
116/* define to 1 to disable lightweight runtime debugging checks */
117#undef NETDRV_NDEBUG
118
119
120#ifdef NETDRV_DEBUG
121/* note: prints function name for you */
122# define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt, __FUNCTION__ , ## args)
123#else
124# define DPRINTK(fmt, args...)
125#endif
126
127#ifdef NETDRV_NDEBUG
128# define assert(expr) do {} while (0)
129#else
130# define assert(expr) \
131 if(!(expr)) { \
132 printk( "Assertion failed! %s,%s,%s,line=%d\n", \
133 #expr,__FILE__,__FUNCTION__,__LINE__); \
134 }
135#endif
136
137
138/* A few user-configurable values. */
139/* media options */
140static int media[] = {-1, -1, -1, -1, -1, -1, -1, -1};
141
142/* Maximum events (Rx packets, etc.) to handle at each interrupt. */
143static int max_interrupt_work = 20;
144
145/* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
146 The RTL chips use a 64 element hash table based on the Ethernet CRC. */
147static int multicast_filter_limit = 32;
148
149/* Size of the in-memory receive ring. */
150#define RX_BUF_LEN_IDX 2 /* 0==8K, 1==16K, 2==32K, 3==64K */
151#define RX_BUF_LEN (8192 << RX_BUF_LEN_IDX)
152#define RX_BUF_PAD 16
153#define RX_BUF_WRAP_PAD 2048 /* spare padding to handle lack of packet wrap */
154#define RX_BUF_TOT_LEN (RX_BUF_LEN + RX_BUF_PAD + RX_BUF_WRAP_PAD)
155
156/* Number of Tx descriptor registers. */
157#define NUM_TX_DESC 4
158
159/* max supported ethernet frame size -- must be at least (dev->mtu+14+4).*/
160#define MAX_ETH_FRAME_SIZE 1536
161
162/* Size of the Tx bounce buffers -- must be at least (dev->mtu+14+4). */
163#define TX_BUF_SIZE MAX_ETH_FRAME_SIZE
164#define TX_BUF_TOT_LEN (TX_BUF_SIZE * NUM_TX_DESC)
165
166/* PCI Tuning Parameters
167 Threshold is bytes transferred to chip before transmission starts. */
168#define TX_FIFO_THRESH 256 /* In bytes, rounded down to 32 byte units. */
169
170/* The following settings are log_2(bytes)-4: 0 == 16 bytes .. 6==1024, 7==end of packet. */
171#define RX_FIFO_THRESH 6 /* Rx buffer level before first PCI xfer. */
172#define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
173#define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
174
175
176/* Operational parameters that usually are not changed. */
177/* Time in jiffies before concluding the transmitter is hung. */
178#define TX_TIMEOUT (6*HZ)
179
180
181enum {
182 HAS_CHIP_XCVR = 0x020000,
183 HAS_LNK_CHNG = 0x040000,
184};
185
186#define NETDRV_MIN_IO_SIZE 0x80
187#define RTL8139B_IO_SIZE 256
188
189#define NETDRV_CAPS HAS_CHIP_XCVR|HAS_LNK_CHNG
190
191typedef enum {
192 RTL8139 = 0,
193 NETDRV_CB,
194 SMC1211TX,
195 /*MPX5030,*/
196 DELTA8139,
197 ADDTRON8139,
198} board_t;
199
200
201/* indexed by board_t, above */
202static struct {
203 const char *name;
204} board_info[] __devinitdata = {
205 { "RealTek RTL8139 Fast Ethernet" },
206 { "RealTek RTL8139B PCI/CardBus" },
207 { "SMC1211TX EZCard 10/100 (RealTek RTL8139)" },
208/* { MPX5030, "Accton MPX5030 (RealTek RTL8139)" },*/
209 { "Delta Electronics 8139 10/100BaseTX" },
210 { "Addtron Technolgy 8139 10/100BaseTX" },
211};
212
213
214static struct pci_device_id netdrv_pci_tbl[] = {
215 {0x10ec, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
216 {0x10ec, 0x8138, PCI_ANY_ID, PCI_ANY_ID, 0, 0, NETDRV_CB },
217 {0x1113, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, SMC1211TX },
218/* {0x1113, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MPX5030 },*/
219 {0x1500, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, DELTA8139 },
220 {0x4033, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ADDTRON8139 },
221 {0,}
222};
223MODULE_DEVICE_TABLE (pci, netdrv_pci_tbl);
224
225
226/* The rest of these values should never change. */
227
228/* Symbolic offsets to registers. */
229enum NETDRV_registers {
230 MAC0 = 0, /* Ethernet hardware address. */
231 MAR0 = 8, /* Multicast filter. */
232 TxStatus0 = 0x10, /* Transmit status (Four 32bit registers). */
233 TxAddr0 = 0x20, /* Tx descriptors (also four 32bit). */
234 RxBuf = 0x30,
235 RxEarlyCnt = 0x34,
236 RxEarlyStatus = 0x36,
237 ChipCmd = 0x37,
238 RxBufPtr = 0x38,
239 RxBufAddr = 0x3A,
240 IntrMask = 0x3C,
241 IntrStatus = 0x3E,
242 TxConfig = 0x40,
243 ChipVersion = 0x43,
244 RxConfig = 0x44,
245 Timer = 0x48, /* A general-purpose counter. */
246 RxMissed = 0x4C, /* 24 bits valid, write clears. */
247 Cfg9346 = 0x50,
248 Config0 = 0x51,
249 Config1 = 0x52,
250 FlashReg = 0x54,
251 MediaStatus = 0x58,
252 Config3 = 0x59,
253 Config4 = 0x5A, /* absent on RTL-8139A */
254 HltClk = 0x5B,
255 MultiIntr = 0x5C,
256 TxSummary = 0x60,
257 BasicModeCtrl = 0x62,
258 BasicModeStatus = 0x64,
259 NWayAdvert = 0x66,
260 NWayLPAR = 0x68,
261 NWayExpansion = 0x6A,
262 /* Undocumented registers, but required for proper operation. */
263 FIFOTMS = 0x70, /* FIFO Control and test. */
264 CSCR = 0x74, /* Chip Status and Configuration Register. */
265 PARA78 = 0x78,
266 PARA7c = 0x7c, /* Magic transceiver parameter register. */
267 Config5 = 0xD8, /* absent on RTL-8139A */
268};
269
270enum ClearBitMasks {
271 MultiIntrClear = 0xF000,
272 ChipCmdClear = 0xE2,
273 Config1Clear = (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1),
274};
275
276enum ChipCmdBits {
277 CmdReset = 0x10,
278 CmdRxEnb = 0x08,
279 CmdTxEnb = 0x04,
280 RxBufEmpty = 0x01,
281};
282
283/* Interrupt register bits, using my own meaningful names. */
284enum IntrStatusBits {
285 PCIErr = 0x8000,
286 PCSTimeout = 0x4000,
287 RxFIFOOver = 0x40,
288 RxUnderrun = 0x20,
289 RxOverflow = 0x10,
290 TxErr = 0x08,
291 TxOK = 0x04,
292 RxErr = 0x02,
293 RxOK = 0x01,
294};
295enum TxStatusBits {
296 TxHostOwns = 0x2000,
297 TxUnderrun = 0x4000,
298 TxStatOK = 0x8000,
299 TxOutOfWindow = 0x20000000,
300 TxAborted = 0x40000000,
301 TxCarrierLost = 0x80000000,
302};
303enum RxStatusBits {
304 RxMulticast = 0x8000,
305 RxPhysical = 0x4000,
306 RxBroadcast = 0x2000,
307 RxBadSymbol = 0x0020,
308 RxRunt = 0x0010,
309 RxTooLong = 0x0008,
310 RxCRCErr = 0x0004,
311 RxBadAlign = 0x0002,
312 RxStatusOK = 0x0001,
313};
314
315/* Bits in RxConfig. */
316enum rx_mode_bits {
317 AcceptErr = 0x20,
318 AcceptRunt = 0x10,
319 AcceptBroadcast = 0x08,
320 AcceptMulticast = 0x04,
321 AcceptMyPhys = 0x02,
322 AcceptAllPhys = 0x01,
323};
324
325/* Bits in TxConfig. */
326enum tx_config_bits {
327 TxIFG1 = (1 << 25), /* Interframe Gap Time */
328 TxIFG0 = (1 << 24), /* Enabling these bits violates IEEE 802.3 */
329 TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */
330 TxCRC = (1 << 16), /* DISABLE appending CRC to end of Tx packets */
331 TxClearAbt = (1 << 0), /* Clear abort (WO) */
332 TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
333
334 TxVersionMask = 0x7C800000, /* mask out version bits 30-26, 23 */
335};
336
337/* Bits in Config1 */
338enum Config1Bits {
339 Cfg1_PM_Enable = 0x01,
340 Cfg1_VPD_Enable = 0x02,
341 Cfg1_PIO = 0x04,
342 Cfg1_MMIO = 0x08,
343 Cfg1_LWAKE = 0x10,
344 Cfg1_Driver_Load = 0x20,
345 Cfg1_LED0 = 0x40,
346 Cfg1_LED1 = 0x80,
347};
348
349enum RxConfigBits {
350 /* Early Rx threshold, none or X/16 */
351 RxCfgEarlyRxNone = 0,
352 RxCfgEarlyRxShift = 24,
353
354 /* rx fifo threshold */
355 RxCfgFIFOShift = 13,
356 RxCfgFIFONone = (7 << RxCfgFIFOShift),
357
358 /* Max DMA burst */
359 RxCfgDMAShift = 8,
360 RxCfgDMAUnlimited = (7 << RxCfgDMAShift),
361
362 /* rx ring buffer length */
363 RxCfgRcv8K = 0,
364 RxCfgRcv16K = (1 << 11),
365 RxCfgRcv32K = (1 << 12),
366 RxCfgRcv64K = (1 << 11) | (1 << 12),
367
368 /* Disable packet wrap at end of Rx buffer */
369 RxNoWrap = (1 << 7),
370};
371
372
373/* Twister tuning parameters from RealTek.
374 Completely undocumented, but required to tune bad links. */
375enum CSCRBits {
376 CSCR_LinkOKBit = 0x0400,
377 CSCR_LinkChangeBit = 0x0800,
378 CSCR_LinkStatusBits = 0x0f000,
379 CSCR_LinkDownOffCmd = 0x003c0,
380 CSCR_LinkDownCmd = 0x0f3c0,
381};
382
383
384enum Cfg9346Bits {
385 Cfg9346_Lock = 0x00,
386 Cfg9346_Unlock = 0xC0,
387};
388
389
390#define PARA78_default 0x78fa8388
391#define PARA7c_default 0xcb38de43 /* param[0][3] */
392#define PARA7c_xxx 0xcb38de43
393static const unsigned long param[4][4] = {
394 {0xcb39de43, 0xcb39ce43, 0xfb38de03, 0xcb38de43},
395 {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
396 {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
397 {0xbb39de43, 0xbb39ce43, 0xbb39ce83, 0xbb39ce83}
398};
399
400struct ring_info {
401 struct sk_buff *skb;
402 dma_addr_t mapping;
403};
404
405
406typedef enum {
407 CH_8139 = 0,
408 CH_8139_K,
409 CH_8139A,
410 CH_8139B,
411 CH_8130,
412 CH_8139C,
413} chip_t;
414
415
416/* directly indexed by chip_t, above */
Jesper Juhl3c6bee12006-01-09 20:54:01 -0800417static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 const char *name;
419 u8 version; /* from RTL8139C docs */
420 u32 RxConfigMask; /* should clear the bits supported by this chip */
421} rtl_chip_info[] = {
422 { "RTL-8139",
423 0x40,
424 0xf0fe0040, /* XXX copied from RTL8139A, verify */
425 },
426
427 { "RTL-8139 rev K",
428 0x60,
429 0xf0fe0040,
430 },
431
432 { "RTL-8139A",
433 0x70,
434 0xf0fe0040,
435 },
436
437 { "RTL-8139B",
438 0x78,
439 0xf0fc0040
440 },
441
442 { "RTL-8130",
443 0x7C,
444 0xf0fe0040, /* XXX copied from RTL8139A, verify */
445 },
446
447 { "RTL-8139C",
448 0x74,
449 0xf0fc0040, /* XXX copied from RTL8139B, verify */
450 },
451
452};
453
454
455struct netdrv_private {
456 board_t board;
457 void *mmio_addr;
458 int drv_flags;
459 struct pci_dev *pci_dev;
460 struct net_device_stats stats;
461 struct timer_list timer; /* Media selection timer. */
462 unsigned char *rx_ring;
463 unsigned int cur_rx; /* Index into the Rx buffer of next Rx pkt. */
464 unsigned int tx_flag;
465 atomic_t cur_tx;
466 atomic_t dirty_tx;
467 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
468 struct ring_info tx_info[NUM_TX_DESC];
469 unsigned char *tx_buf[NUM_TX_DESC]; /* Tx bounce buffers */
470 unsigned char *tx_bufs; /* Tx bounce buffer region. */
471 dma_addr_t rx_ring_dma;
472 dma_addr_t tx_bufs_dma;
473 char phys[4]; /* MII device addresses. */
474 char twistie, twist_row, twist_col; /* Twister tune state. */
475 unsigned int full_duplex:1; /* Full-duplex operation requested. */
476 unsigned int duplex_lock:1;
477 unsigned int default_port:4; /* Last dev->if_port value. */
478 unsigned int media2:4; /* Secondary monitored media port. */
479 unsigned int medialock:1; /* Don't sense media type. */
480 unsigned int mediasense:1; /* Media sensing in progress. */
481 spinlock_t lock;
482 chip_t chipset;
483};
484
485MODULE_AUTHOR ("Jeff Garzik <jgarzik@pobox.com>");
486MODULE_DESCRIPTION ("Skeleton for a PCI Fast Ethernet driver");
487MODULE_LICENSE("GPL");
Victor Fusco2f761472005-07-01 00:03:12 +0200488module_param(multicast_filter_limit, int, 0);
489module_param(max_interrupt_work, int, 0);
490module_param_array(media, int, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491MODULE_PARM_DESC (multicast_filter_limit, "pci-skeleton maximum number of filtered multicast addresses");
492MODULE_PARM_DESC (max_interrupt_work, "pci-skeleton maximum events handled per interrupt");
493MODULE_PARM_DESC (media, "pci-skeleton: Bits 0-3: media type, bit 17: full duplex");
494
495static int read_eeprom (void *ioaddr, int location, int addr_len);
496static int netdrv_open (struct net_device *dev);
497static int mdio_read (struct net_device *dev, int phy_id, int location);
498static void mdio_write (struct net_device *dev, int phy_id, int location,
499 int val);
500static void netdrv_timer (unsigned long data);
501static void netdrv_tx_timeout (struct net_device *dev);
502static void netdrv_init_ring (struct net_device *dev);
503static int netdrv_start_xmit (struct sk_buff *skb,
504 struct net_device *dev);
505static irqreturn_t netdrv_interrupt (int irq, void *dev_instance,
506 struct pt_regs *regs);
507static int netdrv_close (struct net_device *dev);
508static int netdrv_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
509static struct net_device_stats *netdrv_get_stats (struct net_device *dev);
510static void netdrv_set_rx_mode (struct net_device *dev);
511static void netdrv_hw_start (struct net_device *dev);
512
513
514#ifdef USE_IO_OPS
515
516#define NETDRV_R8(reg) inb (((unsigned long)ioaddr) + (reg))
517#define NETDRV_R16(reg) inw (((unsigned long)ioaddr) + (reg))
518#define NETDRV_R32(reg) ((unsigned long) inl (((unsigned long)ioaddr) + (reg)))
519#define NETDRV_W8(reg, val8) outb ((val8), ((unsigned long)ioaddr) + (reg))
520#define NETDRV_W16(reg, val16) outw ((val16), ((unsigned long)ioaddr) + (reg))
521#define NETDRV_W32(reg, val32) outl ((val32), ((unsigned long)ioaddr) + (reg))
522#define NETDRV_W8_F NETDRV_W8
523#define NETDRV_W16_F NETDRV_W16
524#define NETDRV_W32_F NETDRV_W32
525#undef readb
526#undef readw
527#undef readl
528#undef writeb
529#undef writew
530#undef writel
531#define readb(addr) inb((unsigned long)(addr))
532#define readw(addr) inw((unsigned long)(addr))
533#define readl(addr) inl((unsigned long)(addr))
534#define writeb(val,addr) outb((val),(unsigned long)(addr))
535#define writew(val,addr) outw((val),(unsigned long)(addr))
536#define writel(val,addr) outl((val),(unsigned long)(addr))
537
538#else
539
540/* write MMIO register, with flush */
541/* Flush avoids rtl8139 bug w/ posted MMIO writes */
542#define NETDRV_W8_F(reg, val8) do { writeb ((val8), ioaddr + (reg)); readb (ioaddr + (reg)); } while (0)
543#define NETDRV_W16_F(reg, val16) do { writew ((val16), ioaddr + (reg)); readw (ioaddr + (reg)); } while (0)
544#define NETDRV_W32_F(reg, val32) do { writel ((val32), ioaddr + (reg)); readl (ioaddr + (reg)); } while (0)
545
546
547#if MMIO_FLUSH_AUDIT_COMPLETE
548
549/* write MMIO register */
550#define NETDRV_W8(reg, val8) writeb ((val8), ioaddr + (reg))
551#define NETDRV_W16(reg, val16) writew ((val16), ioaddr + (reg))
552#define NETDRV_W32(reg, val32) writel ((val32), ioaddr + (reg))
553
554#else
555
556/* write MMIO register, then flush */
557#define NETDRV_W8 NETDRV_W8_F
558#define NETDRV_W16 NETDRV_W16_F
559#define NETDRV_W32 NETDRV_W32_F
560
561#endif /* MMIO_FLUSH_AUDIT_COMPLETE */
562
563/* read MMIO register */
564#define NETDRV_R8(reg) readb (ioaddr + (reg))
565#define NETDRV_R16(reg) readw (ioaddr + (reg))
566#define NETDRV_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
567
568#endif /* USE_IO_OPS */
569
570
571static const u16 netdrv_intr_mask =
572 PCIErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver |
573 TxErr | TxOK | RxErr | RxOK;
574
575static const unsigned int netdrv_rx_config =
576 RxCfgEarlyRxNone | RxCfgRcv32K | RxNoWrap |
577 (RX_FIFO_THRESH << RxCfgFIFOShift) |
578 (RX_DMA_BURST << RxCfgDMAShift);
579
580
581static int __devinit netdrv_init_board (struct pci_dev *pdev,
582 struct net_device **dev_out,
583 void **ioaddr_out)
584{
585 void *ioaddr = NULL;
586 struct net_device *dev;
587 struct netdrv_private *tp;
588 int rc, i;
589 u32 pio_start, pio_end, pio_flags, pio_len;
590 unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
591 u32 tmp;
592
593 DPRINTK ("ENTER\n");
594
595 assert (pdev != NULL);
596 assert (ioaddr_out != NULL);
597
598 *ioaddr_out = NULL;
599 *dev_out = NULL;
600
601 /* dev zeroed in alloc_etherdev */
602 dev = alloc_etherdev (sizeof (*tp));
603 if (dev == NULL) {
Jeff Garzik2e8a5382006-06-27 10:47:51 -0400604 dev_printk (KERN_ERR, &pdev->dev,
605 "unable to alloc new ethernet\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 DPRINTK ("EXIT, returning -ENOMEM\n");
607 return -ENOMEM;
608 }
609 SET_MODULE_OWNER(dev);
610 SET_NETDEV_DEV(dev, &pdev->dev);
611 tp = dev->priv;
612
613 /* enable device (incl. PCI PM wakeup), and bus-mastering */
614 rc = pci_enable_device (pdev);
615 if (rc)
616 goto err_out;
617
618 pio_start = pci_resource_start (pdev, 0);
619 pio_end = pci_resource_end (pdev, 0);
620 pio_flags = pci_resource_flags (pdev, 0);
621 pio_len = pci_resource_len (pdev, 0);
622
623 mmio_start = pci_resource_start (pdev, 1);
624 mmio_end = pci_resource_end (pdev, 1);
625 mmio_flags = pci_resource_flags (pdev, 1);
626 mmio_len = pci_resource_len (pdev, 1);
627
628 /* set this immediately, we need to know before
629 * we talk to the chip directly */
630 DPRINTK("PIO region size == 0x%02X\n", pio_len);
631 DPRINTK("MMIO region size == 0x%02lX\n", mmio_len);
632
633 /* make sure PCI base addr 0 is PIO */
634 if (!(pio_flags & IORESOURCE_IO)) {
Jeff Garzik2e8a5382006-06-27 10:47:51 -0400635 dev_printk (KERN_ERR, &pdev->dev,
636 "region #0 not a PIO resource, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 rc = -ENODEV;
638 goto err_out;
639 }
640
641 /* make sure PCI base addr 1 is MMIO */
642 if (!(mmio_flags & IORESOURCE_MEM)) {
Jeff Garzik2e8a5382006-06-27 10:47:51 -0400643 dev_printk (KERN_ERR, &pdev->dev,
644 "region #1 not an MMIO resource, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 rc = -ENODEV;
646 goto err_out;
647 }
648
649 /* check for weird/broken PCI region reporting */
650 if ((pio_len < NETDRV_MIN_IO_SIZE) ||
651 (mmio_len < NETDRV_MIN_IO_SIZE)) {
Jeff Garzik2e8a5382006-06-27 10:47:51 -0400652 dev_printk (KERN_ERR, &pdev->dev,
653 "Invalid PCI region size(s), aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 rc = -ENODEV;
655 goto err_out;
656 }
657
Jeff Garzik2e8a5382006-06-27 10:47:51 -0400658 rc = pci_request_regions (pdev, MODNAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if (rc)
660 goto err_out;
661
662 pci_set_master (pdev);
663
664#ifdef USE_IO_OPS
665 ioaddr = (void *) pio_start;
666#else
667 /* ioremap MMIO region */
668 ioaddr = ioremap (mmio_start, mmio_len);
669 if (ioaddr == NULL) {
Jeff Garzik2e8a5382006-06-27 10:47:51 -0400670 dev_printk (KERN_ERR, &pdev->dev,
671 "cannot remap MMIO, aborting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 rc = -EIO;
673 goto err_out_free_res;
674 }
675#endif /* USE_IO_OPS */
676
677 /* Soft reset the chip. */
678 NETDRV_W8 (ChipCmd, (NETDRV_R8 (ChipCmd) & ChipCmdClear) | CmdReset);
679
680 /* Check that the chip has finished the reset. */
681 for (i = 1000; i > 0; i--)
682 if ((NETDRV_R8 (ChipCmd) & CmdReset) == 0)
683 break;
684 else
685 udelay (10);
686
687 /* Bring the chip out of low-power mode. */
688 /* <insert device-specific code here> */
689
690#ifndef USE_IO_OPS
691 /* sanity checks -- ensure PIO and MMIO registers agree */
692 assert (inb (pio_start+Config0) == readb (ioaddr+Config0));
693 assert (inb (pio_start+Config1) == readb (ioaddr+Config1));
694 assert (inb (pio_start+TxConfig) == readb (ioaddr+TxConfig));
695 assert (inb (pio_start+RxConfig) == readb (ioaddr+RxConfig));
696#endif /* !USE_IO_OPS */
697
698 /* identify chip attached to board */
699 tmp = NETDRV_R8 (ChipVersion);
700 for (i = ARRAY_SIZE (rtl_chip_info) - 1; i >= 0; i--)
701 if (tmp == rtl_chip_info[i].version) {
702 tp->chipset = i;
703 goto match;
704 }
705
706 /* if unknown chip, assume array element #0, original RTL-8139 in this case */
Jeff Garzik2e8a5382006-06-27 10:47:51 -0400707 dev_printk (KERN_DEBUG, &pdev->dev,
708 "unknown chip version, assuming RTL-8139\n");
709 dev_printk (KERN_DEBUG, &pdev->dev, "TxConfig = 0x%lx\n",
710 NETDRV_R32 (TxConfig));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 tp->chipset = 0;
712
713match:
714 DPRINTK ("chipset id (%d) == index %d, '%s'\n",
715 tmp,
716 tp->chipset,
717 rtl_chip_info[tp->chipset].name);
718
719 i = register_netdev (dev);
720 if (i)
721 goto err_out_unmap;
722
723 DPRINTK ("EXIT, returning 0\n");
724 *ioaddr_out = ioaddr;
725 *dev_out = dev;
726 return 0;
727
728err_out_unmap:
729#ifndef USE_IO_OPS
730 iounmap(ioaddr);
731err_out_free_res:
732#endif
733 pci_release_regions (pdev);
734err_out:
735 free_netdev (dev);
736 DPRINTK ("EXIT, returning %d\n", rc);
737 return rc;
738}
739
740
741static int __devinit netdrv_init_one (struct pci_dev *pdev,
742 const struct pci_device_id *ent)
743{
744 struct net_device *dev = NULL;
745 struct netdrv_private *tp;
746 int i, addr_len, option;
747 void *ioaddr = NULL;
748 static int board_idx = -1;
749
750/* when built into the kernel, we only print version if device is found */
751#ifndef MODULE
752 static int printed_version;
753 if (!printed_version++)
754 printk(version);
755#endif
756
757 DPRINTK ("ENTER\n");
758
759 assert (pdev != NULL);
760 assert (ent != NULL);
761
762 board_idx++;
763
764 i = netdrv_init_board (pdev, &dev, &ioaddr);
765 if (i < 0) {
766 DPRINTK ("EXIT, returning %d\n", i);
767 return i;
768 }
769
770 tp = dev->priv;
771
772 assert (ioaddr != NULL);
773 assert (dev != NULL);
774 assert (tp != NULL);
775
776 addr_len = read_eeprom (ioaddr, 0, 8) == 0x8129 ? 8 : 6;
777 for (i = 0; i < 3; i++)
778 ((u16 *) (dev->dev_addr))[i] =
779 le16_to_cpu (read_eeprom (ioaddr, i + 7, addr_len));
780
781 /* The Rtl8139-specific entries in the device structure. */
782 dev->open = netdrv_open;
783 dev->hard_start_xmit = netdrv_start_xmit;
784 dev->stop = netdrv_close;
785 dev->get_stats = netdrv_get_stats;
786 dev->set_multicast_list = netdrv_set_rx_mode;
787 dev->do_ioctl = netdrv_ioctl;
788 dev->tx_timeout = netdrv_tx_timeout;
789 dev->watchdog_timeo = TX_TIMEOUT;
790
791 dev->irq = pdev->irq;
792 dev->base_addr = (unsigned long) ioaddr;
793
794 /* dev->priv/tp zeroed and aligned in alloc_etherdev */
795 tp = dev->priv;
796
797 /* note: tp->chipset set in netdrv_init_board */
798 tp->drv_flags = PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
799 PCI_COMMAND_MASTER | NETDRV_CAPS;
800 tp->pci_dev = pdev;
801 tp->board = ent->driver_data;
802 tp->mmio_addr = ioaddr;
803 spin_lock_init(&tp->lock);
804
805 pci_set_drvdata(pdev, dev);
806
807 tp->phys[0] = 32;
808
809 printk (KERN_INFO "%s: %s at 0x%lx, "
810 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
811 "IRQ %d\n",
812 dev->name,
813 board_info[ent->driver_data].name,
814 dev->base_addr,
815 dev->dev_addr[0], dev->dev_addr[1],
816 dev->dev_addr[2], dev->dev_addr[3],
817 dev->dev_addr[4], dev->dev_addr[5],
818 dev->irq);
819
820 printk (KERN_DEBUG "%s: Identified 8139 chip type '%s'\n",
821 dev->name, rtl_chip_info[tp->chipset].name);
822
823 /* Put the chip into low-power mode. */
824 NETDRV_W8_F (Cfg9346, Cfg9346_Unlock);
825
826 /* The lower four bits are the media type. */
827 option = (board_idx > 7) ? 0 : media[board_idx];
828 if (option > 0) {
829 tp->full_duplex = (option & 0x200) ? 1 : 0;
830 tp->default_port = option & 15;
831 if (tp->default_port)
832 tp->medialock = 1;
833 }
834
835 if (tp->full_duplex) {
836 printk (KERN_INFO
837 "%s: Media type forced to Full Duplex.\n",
838 dev->name);
839 mdio_write (dev, tp->phys[0], MII_ADVERTISE, ADVERTISE_FULL);
840 tp->duplex_lock = 1;
841 }
842
843 DPRINTK ("EXIT - returning 0\n");
844 return 0;
845}
846
847
848static void __devexit netdrv_remove_one (struct pci_dev *pdev)
849{
850 struct net_device *dev = pci_get_drvdata (pdev);
851 struct netdrv_private *np;
852
853 DPRINTK ("ENTER\n");
854
855 assert (dev != NULL);
856
857 np = dev->priv;
858 assert (np != NULL);
859
860 unregister_netdev (dev);
861
862#ifndef USE_IO_OPS
863 iounmap (np->mmio_addr);
864#endif /* !USE_IO_OPS */
865
866 pci_release_regions (pdev);
867
868 free_netdev (dev);
869
870 pci_set_drvdata (pdev, NULL);
871
872 pci_disable_device (pdev);
873
874 DPRINTK ("EXIT\n");
875}
876
877
878/* Serial EEPROM section. */
879
880/* EEPROM_Ctrl bits. */
881#define EE_SHIFT_CLK 0x04 /* EEPROM shift clock. */
882#define EE_CS 0x08 /* EEPROM chip select. */
883#define EE_DATA_WRITE 0x02 /* EEPROM chip data in. */
884#define EE_WRITE_0 0x00
885#define EE_WRITE_1 0x02
886#define EE_DATA_READ 0x01 /* EEPROM chip data out. */
887#define EE_ENB (0x80 | EE_CS)
888
889/* Delay between EEPROM clock transitions.
890 No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
891 */
892
893#define eeprom_delay() readl(ee_addr)
894
895/* The EEPROM commands include the alway-set leading bit. */
896#define EE_WRITE_CMD (5)
897#define EE_READ_CMD (6)
898#define EE_ERASE_CMD (7)
899
900static int __devinit read_eeprom (void *ioaddr, int location, int addr_len)
901{
902 int i;
903 unsigned retval = 0;
904 void *ee_addr = ioaddr + Cfg9346;
905 int read_cmd = location | (EE_READ_CMD << addr_len);
906
907 DPRINTK ("ENTER\n");
908
909 writeb (EE_ENB & ~EE_CS, ee_addr);
910 writeb (EE_ENB, ee_addr);
911 eeprom_delay ();
912
913 /* Shift the read command bits out. */
914 for (i = 4 + addr_len; i >= 0; i--) {
915 int dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
916 writeb (EE_ENB | dataval, ee_addr);
917 eeprom_delay ();
918 writeb (EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
919 eeprom_delay ();
920 }
921 writeb (EE_ENB, ee_addr);
922 eeprom_delay ();
923
924 for (i = 16; i > 0; i--) {
925 writeb (EE_ENB | EE_SHIFT_CLK, ee_addr);
926 eeprom_delay ();
927 retval =
928 (retval << 1) | ((readb (ee_addr) & EE_DATA_READ) ? 1 :
929 0);
930 writeb (EE_ENB, ee_addr);
931 eeprom_delay ();
932 }
933
934 /* Terminate the EEPROM access. */
935 writeb (~EE_CS, ee_addr);
936 eeprom_delay ();
937
938 DPRINTK ("EXIT - returning %d\n", retval);
939 return retval;
940}
941
942/* MII serial management: mostly bogus for now. */
943/* Read and write the MII management registers using software-generated
944 serial MDIO protocol.
945 The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
946 met by back-to-back PCI I/O cycles, but we insert a delay to avoid
947 "overclocking" issues. */
948#define MDIO_DIR 0x80
949#define MDIO_DATA_OUT 0x04
950#define MDIO_DATA_IN 0x02
951#define MDIO_CLK 0x01
952#define MDIO_WRITE0 (MDIO_DIR)
953#define MDIO_WRITE1 (MDIO_DIR | MDIO_DATA_OUT)
954
955#define mdio_delay() readb(mdio_addr)
956
957
958static char mii_2_8139_map[8] = {
959 BasicModeCtrl,
960 BasicModeStatus,
961 0,
962 0,
963 NWayAdvert,
964 NWayLPAR,
965 NWayExpansion,
966 0
967};
968
969
970/* Syncronize the MII management interface by shifting 32 one bits out. */
971static void mdio_sync (void *mdio_addr)
972{
973 int i;
974
975 DPRINTK ("ENTER\n");
976
977 for (i = 32; i >= 0; i--) {
978 writeb (MDIO_WRITE1, mdio_addr);
979 mdio_delay ();
980 writeb (MDIO_WRITE1 | MDIO_CLK, mdio_addr);
981 mdio_delay ();
982 }
983
984 DPRINTK ("EXIT\n");
985}
986
987
988static int mdio_read (struct net_device *dev, int phy_id, int location)
989{
990 struct netdrv_private *tp = dev->priv;
991 void *mdio_addr = tp->mmio_addr + Config4;
992 int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
993 int retval = 0;
994 int i;
995
996 DPRINTK ("ENTER\n");
997
998 if (phy_id > 31) { /* Really a 8139. Use internal registers. */
999 DPRINTK ("EXIT after directly using 8139 internal regs\n");
1000 return location < 8 && mii_2_8139_map[location] ?
1001 readw (tp->mmio_addr + mii_2_8139_map[location]) : 0;
1002 }
1003 mdio_sync (mdio_addr);
1004 /* Shift the read command bits out. */
1005 for (i = 15; i >= 0; i--) {
1006 int dataval = (mii_cmd & (1 << i)) ? MDIO_DATA_OUT : 0;
1007
1008 writeb (MDIO_DIR | dataval, mdio_addr);
1009 mdio_delay ();
1010 writeb (MDIO_DIR | dataval | MDIO_CLK, mdio_addr);
1011 mdio_delay ();
1012 }
1013
1014 /* Read the two transition, 16 data, and wire-idle bits. */
1015 for (i = 19; i > 0; i--) {
1016 writeb (0, mdio_addr);
1017 mdio_delay ();
1018 retval =
1019 (retval << 1) | ((readb (mdio_addr) & MDIO_DATA_IN) ? 1
1020 : 0);
1021 writeb (MDIO_CLK, mdio_addr);
1022 mdio_delay ();
1023 }
1024
1025 DPRINTK ("EXIT, returning %d\n", (retval >> 1) & 0xffff);
1026 return (retval >> 1) & 0xffff;
1027}
1028
1029
1030static void mdio_write (struct net_device *dev, int phy_id, int location,
1031 int value)
1032{
1033 struct netdrv_private *tp = dev->priv;
1034 void *mdio_addr = tp->mmio_addr + Config4;
1035 int mii_cmd =
1036 (0x5002 << 16) | (phy_id << 23) | (location << 18) | value;
1037 int i;
1038
1039 DPRINTK ("ENTER\n");
1040
1041 if (phy_id > 31) { /* Really a 8139. Use internal registers. */
1042 if (location < 8 && mii_2_8139_map[location]) {
1043 writew (value,
1044 tp->mmio_addr + mii_2_8139_map[location]);
1045 readw (tp->mmio_addr + mii_2_8139_map[location]);
1046 }
1047 DPRINTK ("EXIT after directly using 8139 internal regs\n");
1048 return;
1049 }
1050 mdio_sync (mdio_addr);
1051
1052 /* Shift the command bits out. */
1053 for (i = 31; i >= 0; i--) {
1054 int dataval =
1055 (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
1056 writeb (dataval, mdio_addr);
1057 mdio_delay ();
1058 writeb (dataval | MDIO_CLK, mdio_addr);
1059 mdio_delay ();
1060 }
1061
1062 /* Clear out extra bits. */
1063 for (i = 2; i > 0; i--) {
1064 writeb (0, mdio_addr);
1065 mdio_delay ();
1066 writeb (MDIO_CLK, mdio_addr);
1067 mdio_delay ();
1068 }
1069
1070 DPRINTK ("EXIT\n");
1071}
1072
1073
1074static int netdrv_open (struct net_device *dev)
1075{
1076 struct netdrv_private *tp = dev->priv;
1077 int retval;
1078#ifdef NETDRV_DEBUG
1079 void *ioaddr = tp->mmio_addr;
1080#endif
1081
1082 DPRINTK ("ENTER\n");
1083
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001084 retval = request_irq (dev->irq, netdrv_interrupt, IRQF_SHARED, dev->name, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 if (retval) {
1086 DPRINTK ("EXIT, returning %d\n", retval);
1087 return retval;
1088 }
1089
1090 tp->tx_bufs = pci_alloc_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
1091 &tp->tx_bufs_dma);
1092 tp->rx_ring = pci_alloc_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
1093 &tp->rx_ring_dma);
1094 if (tp->tx_bufs == NULL || tp->rx_ring == NULL) {
1095 free_irq(dev->irq, dev);
1096
1097 if (tp->tx_bufs)
1098 pci_free_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
1099 tp->tx_bufs, tp->tx_bufs_dma);
1100 if (tp->rx_ring)
1101 pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
1102 tp->rx_ring, tp->rx_ring_dma);
1103
1104 DPRINTK ("EXIT, returning -ENOMEM\n");
1105 return -ENOMEM;
1106
1107 }
1108
1109 tp->full_duplex = tp->duplex_lock;
1110 tp->tx_flag = (TX_FIFO_THRESH << 11) & 0x003f0000;
1111
1112 netdrv_init_ring (dev);
1113 netdrv_hw_start (dev);
1114
1115 DPRINTK ("%s: netdrv_open() ioaddr %#lx IRQ %d"
1116 " GP Pins %2.2x %s-duplex.\n",
1117 dev->name, pci_resource_start (tp->pci_dev, 1),
1118 dev->irq, NETDRV_R8 (MediaStatus),
1119 tp->full_duplex ? "full" : "half");
1120
1121 /* Set the timer to switch to check for link beat and perhaps switch
1122 to an alternate media type. */
1123 init_timer (&tp->timer);
1124 tp->timer.expires = jiffies + 3 * HZ;
1125 tp->timer.data = (unsigned long) dev;
1126 tp->timer.function = &netdrv_timer;
1127 add_timer (&tp->timer);
1128
1129 DPRINTK ("EXIT, returning 0\n");
1130 return 0;
1131}
1132
1133
1134/* Start the hardware at open or resume. */
1135static void netdrv_hw_start (struct net_device *dev)
1136{
1137 struct netdrv_private *tp = dev->priv;
1138 void *ioaddr = tp->mmio_addr;
1139 u32 i;
1140
1141 DPRINTK ("ENTER\n");
1142
1143 /* Soft reset the chip. */
1144 NETDRV_W8 (ChipCmd, (NETDRV_R8 (ChipCmd) & ChipCmdClear) | CmdReset);
1145 udelay (100);
1146
1147 /* Check that the chip has finished the reset. */
1148 for (i = 1000; i > 0; i--)
1149 if ((NETDRV_R8 (ChipCmd) & CmdReset) == 0)
1150 break;
1151
1152 /* Restore our idea of the MAC address. */
1153 NETDRV_W32_F (MAC0 + 0, cpu_to_le32 (*(u32 *) (dev->dev_addr + 0)));
1154 NETDRV_W32_F (MAC0 + 4, cpu_to_le32 (*(u32 *) (dev->dev_addr + 4)));
1155
1156 /* Must enable Tx/Rx before setting transfer thresholds! */
1157 NETDRV_W8_F (ChipCmd, (NETDRV_R8 (ChipCmd) & ChipCmdClear) |
1158 CmdRxEnb | CmdTxEnb);
1159
1160 i = netdrv_rx_config |
1161 (NETDRV_R32 (RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
1162 NETDRV_W32_F (RxConfig, i);
1163
1164 /* Check this value: the documentation for IFG contradicts ifself. */
1165 NETDRV_W32 (TxConfig, (TX_DMA_BURST << TxDMAShift));
1166
1167 /* unlock Config[01234] and BMCR register writes */
1168 NETDRV_W8_F (Cfg9346, Cfg9346_Unlock);
1169 udelay (10);
1170
1171 tp->cur_rx = 0;
1172
1173 /* Lock Config[01234] and BMCR register writes */
1174 NETDRV_W8_F (Cfg9346, Cfg9346_Lock);
1175 udelay (10);
1176
1177 /* init Rx ring buffer DMA address */
1178 NETDRV_W32_F (RxBuf, tp->rx_ring_dma);
1179
1180 /* init Tx buffer DMA addresses */
1181 for (i = 0; i < NUM_TX_DESC; i++)
1182 NETDRV_W32_F (TxAddr0 + (i * 4), tp->tx_bufs_dma + (tp->tx_buf[i] - tp->tx_bufs));
1183
1184 NETDRV_W32_F (RxMissed, 0);
1185
1186 netdrv_set_rx_mode (dev);
1187
1188 /* no early-rx interrupts */
1189 NETDRV_W16 (MultiIntr, NETDRV_R16 (MultiIntr) & MultiIntrClear);
1190
1191 /* make sure RxTx has started */
1192 NETDRV_W8_F (ChipCmd, (NETDRV_R8 (ChipCmd) & ChipCmdClear) |
1193 CmdRxEnb | CmdTxEnb);
1194
1195 /* Enable all known interrupts by setting the interrupt mask. */
1196 NETDRV_W16_F (IntrMask, netdrv_intr_mask);
1197
1198 netif_start_queue (dev);
1199
1200 DPRINTK ("EXIT\n");
1201}
1202
1203
1204/* Initialize the Rx and Tx rings, along with various 'dev' bits. */
1205static void netdrv_init_ring (struct net_device *dev)
1206{
1207 struct netdrv_private *tp = dev->priv;
1208 int i;
1209
1210 DPRINTK ("ENTER\n");
1211
1212 tp->cur_rx = 0;
1213 atomic_set (&tp->cur_tx, 0);
1214 atomic_set (&tp->dirty_tx, 0);
1215
1216 for (i = 0; i < NUM_TX_DESC; i++) {
1217 tp->tx_info[i].skb = NULL;
1218 tp->tx_info[i].mapping = 0;
1219 tp->tx_buf[i] = &tp->tx_bufs[i * TX_BUF_SIZE];
1220 }
1221
1222 DPRINTK ("EXIT\n");
1223}
1224
1225
1226static void netdrv_timer (unsigned long data)
1227{
1228 struct net_device *dev = (struct net_device *) data;
1229 struct netdrv_private *tp = dev->priv;
1230 void *ioaddr = tp->mmio_addr;
1231 int next_tick = 60 * HZ;
1232 int mii_lpa;
1233
1234 mii_lpa = mdio_read (dev, tp->phys[0], MII_LPA);
1235
1236 if (!tp->duplex_lock && mii_lpa != 0xffff) {
1237 int duplex = (mii_lpa & LPA_100FULL)
1238 || (mii_lpa & 0x01C0) == 0x0040;
1239 if (tp->full_duplex != duplex) {
1240 tp->full_duplex = duplex;
1241 printk (KERN_INFO
1242 "%s: Setting %s-duplex based on MII #%d link"
1243 " partner ability of %4.4x.\n", dev->name,
1244 tp->full_duplex ? "full" : "half",
1245 tp->phys[0], mii_lpa);
1246 NETDRV_W8 (Cfg9346, Cfg9346_Unlock);
1247 NETDRV_W8 (Config1, tp->full_duplex ? 0x60 : 0x20);
1248 NETDRV_W8 (Cfg9346, Cfg9346_Lock);
1249 }
1250 }
1251
1252 DPRINTK ("%s: Media selection tick, Link partner %4.4x.\n",
1253 dev->name, NETDRV_R16 (NWayLPAR));
1254 DPRINTK ("%s: Other registers are IntMask %4.4x IntStatus %4.4x"
1255 " RxStatus %4.4x.\n", dev->name,
1256 NETDRV_R16 (IntrMask),
1257 NETDRV_R16 (IntrStatus),
1258 NETDRV_R32 (RxEarlyStatus));
1259 DPRINTK ("%s: Chip config %2.2x %2.2x.\n",
1260 dev->name, NETDRV_R8 (Config0),
1261 NETDRV_R8 (Config1));
1262
1263 tp->timer.expires = jiffies + next_tick;
1264 add_timer (&tp->timer);
1265}
1266
1267
1268static void netdrv_tx_clear (struct netdrv_private *tp)
1269{
1270 int i;
1271
1272 atomic_set (&tp->cur_tx, 0);
1273 atomic_set (&tp->dirty_tx, 0);
1274
1275 /* Dump the unsent Tx packets. */
1276 for (i = 0; i < NUM_TX_DESC; i++) {
1277 struct ring_info *rp = &tp->tx_info[i];
1278 if (rp->mapping != 0) {
1279 pci_unmap_single (tp->pci_dev, rp->mapping,
1280 rp->skb->len, PCI_DMA_TODEVICE);
1281 rp->mapping = 0;
1282 }
1283 if (rp->skb) {
1284 dev_kfree_skb (rp->skb);
1285 rp->skb = NULL;
1286 tp->stats.tx_dropped++;
1287 }
1288 }
1289}
1290
1291
1292static void netdrv_tx_timeout (struct net_device *dev)
1293{
1294 struct netdrv_private *tp = dev->priv;
1295 void *ioaddr = tp->mmio_addr;
1296 int i;
1297 u8 tmp8;
1298 unsigned long flags;
1299
1300 DPRINTK ("%s: Transmit timeout, status %2.2x %4.4x "
1301 "media %2.2x.\n", dev->name,
1302 NETDRV_R8 (ChipCmd),
1303 NETDRV_R16 (IntrStatus),
1304 NETDRV_R8 (MediaStatus));
1305
1306 /* disable Tx ASAP, if not already */
1307 tmp8 = NETDRV_R8 (ChipCmd);
1308 if (tmp8 & CmdTxEnb)
1309 NETDRV_W8 (ChipCmd, tmp8 & ~CmdTxEnb);
1310
1311 /* Disable interrupts by clearing the interrupt mask. */
1312 NETDRV_W16 (IntrMask, 0x0000);
1313
1314 /* Emit info to figure out what went wrong. */
1315 printk (KERN_DEBUG "%s: Tx queue start entry %d dirty entry %d.\n",
1316 dev->name, atomic_read (&tp->cur_tx),
1317 atomic_read (&tp->dirty_tx));
1318 for (i = 0; i < NUM_TX_DESC; i++)
1319 printk (KERN_DEBUG "%s: Tx descriptor %d is %8.8lx.%s\n",
1320 dev->name, i, NETDRV_R32 (TxStatus0 + (i * 4)),
1321 i == atomic_read (&tp->dirty_tx) % NUM_TX_DESC ?
1322 " (queue head)" : "");
1323
1324 /* Stop a shared interrupt from scavenging while we are. */
1325 spin_lock_irqsave (&tp->lock, flags);
1326
1327 netdrv_tx_clear (tp);
1328
1329 spin_unlock_irqrestore (&tp->lock, flags);
1330
1331 /* ...and finally, reset everything */
1332 netdrv_hw_start (dev);
1333
1334 netif_wake_queue (dev);
1335}
1336
1337
1338
1339static int netdrv_start_xmit (struct sk_buff *skb, struct net_device *dev)
1340{
1341 struct netdrv_private *tp = dev->priv;
1342 void *ioaddr = tp->mmio_addr;
1343 int entry;
1344
1345 /* Calculate the next Tx descriptor entry. */
1346 entry = atomic_read (&tp->cur_tx) % NUM_TX_DESC;
1347
1348 assert (tp->tx_info[entry].skb == NULL);
1349 assert (tp->tx_info[entry].mapping == 0);
1350
1351 tp->tx_info[entry].skb = skb;
1352 /* tp->tx_info[entry].mapping = 0; */
1353 memcpy (tp->tx_buf[entry], skb->data, skb->len);
1354
1355 /* Note: the chip doesn't have auto-pad! */
1356 NETDRV_W32 (TxStatus0 + (entry * sizeof(u32)),
1357 tp->tx_flag | (skb->len >= ETH_ZLEN ? skb->len : ETH_ZLEN));
1358
1359 dev->trans_start = jiffies;
1360 atomic_inc (&tp->cur_tx);
1361 if ((atomic_read (&tp->cur_tx) - atomic_read (&tp->dirty_tx)) >= NUM_TX_DESC)
1362 netif_stop_queue (dev);
1363
1364 DPRINTK ("%s: Queued Tx packet at %p size %u to slot %d.\n",
1365 dev->name, skb->data, skb->len, entry);
1366
1367 return 0;
1368}
1369
1370
1371static void netdrv_tx_interrupt (struct net_device *dev,
1372 struct netdrv_private *tp,
1373 void *ioaddr)
1374{
1375 int cur_tx, dirty_tx, tx_left;
1376
1377 assert (dev != NULL);
1378 assert (tp != NULL);
1379 assert (ioaddr != NULL);
1380
1381 dirty_tx = atomic_read (&tp->dirty_tx);
1382
1383 cur_tx = atomic_read (&tp->cur_tx);
1384 tx_left = cur_tx - dirty_tx;
1385 while (tx_left > 0) {
1386 int entry = dirty_tx % NUM_TX_DESC;
1387 int txstatus;
1388
1389 txstatus = NETDRV_R32 (TxStatus0 + (entry * sizeof (u32)));
1390
1391 if (!(txstatus & (TxStatOK | TxUnderrun | TxAborted)))
1392 break; /* It still hasn't been Txed */
1393
1394 /* Note: TxCarrierLost is always asserted at 100mbps. */
1395 if (txstatus & (TxOutOfWindow | TxAborted)) {
1396 /* There was an major error, log it. */
1397 DPRINTK ("%s: Transmit error, Tx status %8.8x.\n",
1398 dev->name, txstatus);
1399 tp->stats.tx_errors++;
1400 if (txstatus & TxAborted) {
1401 tp->stats.tx_aborted_errors++;
1402 NETDRV_W32 (TxConfig, TxClearAbt | (TX_DMA_BURST << TxDMAShift));
1403 }
1404 if (txstatus & TxCarrierLost)
1405 tp->stats.tx_carrier_errors++;
1406 if (txstatus & TxOutOfWindow)
1407 tp->stats.tx_window_errors++;
1408 } else {
1409 if (txstatus & TxUnderrun) {
1410 /* Add 64 to the Tx FIFO threshold. */
1411 if (tp->tx_flag < 0x00300000)
1412 tp->tx_flag += 0x00020000;
1413 tp->stats.tx_fifo_errors++;
1414 }
1415 tp->stats.collisions += (txstatus >> 24) & 15;
1416 tp->stats.tx_bytes += txstatus & 0x7ff;
1417 tp->stats.tx_packets++;
1418 }
1419
1420 /* Free the original skb. */
1421 if (tp->tx_info[entry].mapping != 0) {
1422 pci_unmap_single(tp->pci_dev,
1423 tp->tx_info[entry].mapping,
1424 tp->tx_info[entry].skb->len,
1425 PCI_DMA_TODEVICE);
1426 tp->tx_info[entry].mapping = 0;
1427 }
1428 dev_kfree_skb_irq (tp->tx_info[entry].skb);
1429 tp->tx_info[entry].skb = NULL;
1430 dirty_tx++;
1431 if (dirty_tx < 0) { /* handle signed int overflow */
1432 atomic_sub (cur_tx, &tp->cur_tx); /* XXX racy? */
1433 dirty_tx = cur_tx - tx_left + 1;
1434 }
1435 if (netif_queue_stopped (dev))
1436 netif_wake_queue (dev);
1437
1438 cur_tx = atomic_read (&tp->cur_tx);
1439 tx_left = cur_tx - dirty_tx;
1440
1441 }
1442
1443#ifndef NETDRV_NDEBUG
1444 if (atomic_read (&tp->cur_tx) - dirty_tx > NUM_TX_DESC) {
1445 printk (KERN_ERR
1446 "%s: Out-of-sync dirty pointer, %d vs. %d.\n",
1447 dev->name, dirty_tx, atomic_read (&tp->cur_tx));
1448 dirty_tx += NUM_TX_DESC;
1449 }
1450#endif /* NETDRV_NDEBUG */
1451
1452 atomic_set (&tp->dirty_tx, dirty_tx);
1453}
1454
1455
1456/* TODO: clean this up! Rx reset need not be this intensive */
1457static void netdrv_rx_err (u32 rx_status, struct net_device *dev,
1458 struct netdrv_private *tp, void *ioaddr)
1459{
1460 u8 tmp8;
1461 int tmp_work = 1000;
1462
1463 DPRINTK ("%s: Ethernet frame had errors, status %8.8x.\n",
1464 dev->name, rx_status);
1465 if (rx_status & RxTooLong) {
1466 DPRINTK ("%s: Oversized Ethernet frame, status %4.4x!\n",
1467 dev->name, rx_status);
1468 /* A.C.: The chip hangs here. */
1469 }
1470 tp->stats.rx_errors++;
1471 if (rx_status & (RxBadSymbol | RxBadAlign))
1472 tp->stats.rx_frame_errors++;
1473 if (rx_status & (RxRunt | RxTooLong))
1474 tp->stats.rx_length_errors++;
1475 if (rx_status & RxCRCErr)
1476 tp->stats.rx_crc_errors++;
1477 /* Reset the receiver, based on RealTek recommendation. (Bug?) */
1478 tp->cur_rx = 0;
1479
1480 /* disable receive */
1481 tmp8 = NETDRV_R8 (ChipCmd) & ChipCmdClear;
1482 NETDRV_W8_F (ChipCmd, tmp8 | CmdTxEnb);
1483
1484 /* A.C.: Reset the multicast list. */
1485 netdrv_set_rx_mode (dev);
1486
1487 /* XXX potentially temporary hack to
1488 * restart hung receiver */
1489 while (--tmp_work > 0) {
1490 tmp8 = NETDRV_R8 (ChipCmd);
1491 if ((tmp8 & CmdRxEnb) && (tmp8 & CmdTxEnb))
1492 break;
1493 NETDRV_W8_F (ChipCmd,
1494 (tmp8 & ChipCmdClear) | CmdRxEnb | CmdTxEnb);
1495 }
1496
1497 /* G.S.: Re-enable receiver */
1498 /* XXX temporary hack to work around receiver hang */
1499 netdrv_set_rx_mode (dev);
1500
1501 if (tmp_work <= 0)
1502 printk (KERN_WARNING PFX "tx/rx enable wait too long\n");
1503}
1504
1505
1506/* The data sheet doesn't describe the Rx ring at all, so I'm guessing at the
1507 field alignments and semantics. */
1508static void netdrv_rx_interrupt (struct net_device *dev,
1509 struct netdrv_private *tp, void *ioaddr)
1510{
1511 unsigned char *rx_ring;
1512 u16 cur_rx;
1513
1514 assert (dev != NULL);
1515 assert (tp != NULL);
1516 assert (ioaddr != NULL);
1517
1518 rx_ring = tp->rx_ring;
1519 cur_rx = tp->cur_rx;
1520
1521 DPRINTK ("%s: In netdrv_rx(), current %4.4x BufAddr %4.4x,"
1522 " free to %4.4x, Cmd %2.2x.\n", dev->name, cur_rx,
1523 NETDRV_R16 (RxBufAddr),
1524 NETDRV_R16 (RxBufPtr), NETDRV_R8 (ChipCmd));
1525
1526 while ((NETDRV_R8 (ChipCmd) & RxBufEmpty) == 0) {
1527 int ring_offset = cur_rx % RX_BUF_LEN;
1528 u32 rx_status;
1529 unsigned int rx_size;
1530 unsigned int pkt_size;
1531 struct sk_buff *skb;
1532
1533 /* read size+status of next frame from DMA ring buffer */
1534 rx_status = le32_to_cpu (*(u32 *) (rx_ring + ring_offset));
1535 rx_size = rx_status >> 16;
1536 pkt_size = rx_size - 4;
1537
1538 DPRINTK ("%s: netdrv_rx() status %4.4x, size %4.4x,"
1539 " cur %4.4x.\n", dev->name, rx_status,
1540 rx_size, cur_rx);
1541#if NETDRV_DEBUG > 2
1542 {
1543 int i;
1544 DPRINTK ("%s: Frame contents ", dev->name);
1545 for (i = 0; i < 70; i++)
1546 printk (" %2.2x",
1547 rx_ring[ring_offset + i]);
1548 printk (".\n");
1549 }
1550#endif
1551
1552 /* If Rx err or invalid rx_size/rx_status received
1553 * (which happens if we get lost in the ring),
1554 * Rx process gets reset, so we abort any further
1555 * Rx processing.
1556 */
1557 if ((rx_size > (MAX_ETH_FRAME_SIZE+4)) ||
1558 (!(rx_status & RxStatusOK))) {
1559 netdrv_rx_err (rx_status, dev, tp, ioaddr);
1560 return;
1561 }
1562
1563 /* Malloc up new buffer, compatible with net-2e. */
1564 /* Omit the four octet CRC from the length. */
1565
1566 /* TODO: consider allocating skb's outside of
1567 * interrupt context, both to speed interrupt processing,
1568 * and also to reduce the chances of having to
1569 * drop packets here under memory pressure.
1570 */
1571
1572 skb = dev_alloc_skb (pkt_size + 2);
1573 if (skb) {
1574 skb->dev = dev;
1575 skb_reserve (skb, 2); /* 16 byte align the IP fields. */
1576
1577 eth_copy_and_sum (skb, &rx_ring[ring_offset + 4], pkt_size, 0);
1578 skb_put (skb, pkt_size);
1579
1580 skb->protocol = eth_type_trans (skb, dev);
1581 netif_rx (skb);
1582 dev->last_rx = jiffies;
1583 tp->stats.rx_bytes += pkt_size;
1584 tp->stats.rx_packets++;
1585 } else {
1586 printk (KERN_WARNING
1587 "%s: Memory squeeze, dropping packet.\n",
1588 dev->name);
1589 tp->stats.rx_dropped++;
1590 }
1591
1592 cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
1593 NETDRV_W16_F (RxBufPtr, cur_rx - 16);
1594 }
1595
1596 DPRINTK ("%s: Done netdrv_rx(), current %4.4x BufAddr %4.4x,"
1597 " free to %4.4x, Cmd %2.2x.\n", dev->name, cur_rx,
1598 NETDRV_R16 (RxBufAddr),
1599 NETDRV_R16 (RxBufPtr), NETDRV_R8 (ChipCmd));
1600
1601 tp->cur_rx = cur_rx;
1602}
1603
1604
1605static void netdrv_weird_interrupt (struct net_device *dev,
1606 struct netdrv_private *tp,
1607 void *ioaddr,
1608 int status, int link_changed)
1609{
1610 printk (KERN_DEBUG "%s: Abnormal interrupt, status %8.8x.\n",
1611 dev->name, status);
1612
1613 assert (dev != NULL);
1614 assert (tp != NULL);
1615 assert (ioaddr != NULL);
1616
1617 /* Update the error count. */
1618 tp->stats.rx_missed_errors += NETDRV_R32 (RxMissed);
1619 NETDRV_W32 (RxMissed, 0);
1620
1621 if ((status & RxUnderrun) && link_changed &&
1622 (tp->drv_flags & HAS_LNK_CHNG)) {
1623 /* Really link-change on new chips. */
1624 int lpar = NETDRV_R16 (NWayLPAR);
1625 int duplex = (lpar & 0x0100) || (lpar & 0x01C0) == 0x0040
1626 || tp->duplex_lock;
1627 if (tp->full_duplex != duplex) {
1628 tp->full_duplex = duplex;
1629 NETDRV_W8 (Cfg9346, Cfg9346_Unlock);
1630 NETDRV_W8 (Config1, tp->full_duplex ? 0x60 : 0x20);
1631 NETDRV_W8 (Cfg9346, Cfg9346_Lock);
1632 }
1633 status &= ~RxUnderrun;
1634 }
1635
1636 /* XXX along with netdrv_rx_err, are we double-counting errors? */
1637 if (status &
1638 (RxUnderrun | RxOverflow | RxErr | RxFIFOOver))
1639 tp->stats.rx_errors++;
1640
1641 if (status & (PCSTimeout))
1642 tp->stats.rx_length_errors++;
1643 if (status & (RxUnderrun | RxFIFOOver))
1644 tp->stats.rx_fifo_errors++;
1645 if (status & RxOverflow) {
1646 tp->stats.rx_over_errors++;
1647 tp->cur_rx = NETDRV_R16 (RxBufAddr) % RX_BUF_LEN;
1648 NETDRV_W16_F (RxBufPtr, tp->cur_rx - 16);
1649 }
1650 if (status & PCIErr) {
1651 u16 pci_cmd_status;
1652 pci_read_config_word (tp->pci_dev, PCI_STATUS, &pci_cmd_status);
1653
1654 printk (KERN_ERR "%s: PCI Bus error %4.4x.\n",
1655 dev->name, pci_cmd_status);
1656 }
1657}
1658
1659
1660/* The interrupt handler does all of the Rx thread work and cleans up
1661 after the Tx thread. */
1662static irqreturn_t netdrv_interrupt (int irq, void *dev_instance,
1663 struct pt_regs *regs)
1664{
1665 struct net_device *dev = (struct net_device *) dev_instance;
1666 struct netdrv_private *tp = dev->priv;
1667 int boguscnt = max_interrupt_work;
1668 void *ioaddr = tp->mmio_addr;
1669 int status = 0, link_changed = 0; /* avoid bogus "uninit" warning */
1670 int handled = 0;
1671
1672 spin_lock (&tp->lock);
1673
1674 do {
1675 status = NETDRV_R16 (IntrStatus);
1676
1677 /* h/w no longer present (hotplug?) or major error, bail */
1678 if (status == 0xFFFF)
1679 break;
1680
1681 handled = 1;
1682 /* Acknowledge all of the current interrupt sources ASAP */
1683 NETDRV_W16_F (IntrStatus, status);
1684
1685 DPRINTK ("%s: interrupt status=%#4.4x new intstat=%#4.4x.\n",
1686 dev->name, status,
1687 NETDRV_R16 (IntrStatus));
1688
1689 if ((status &
1690 (PCIErr | PCSTimeout | RxUnderrun | RxOverflow |
1691 RxFIFOOver | TxErr | TxOK | RxErr | RxOK)) == 0)
1692 break;
1693
1694 /* Check uncommon events with one test. */
1695 if (status & (PCIErr | PCSTimeout | RxUnderrun | RxOverflow |
1696 RxFIFOOver | TxErr | RxErr))
1697 netdrv_weird_interrupt (dev, tp, ioaddr,
1698 status, link_changed);
1699
1700 if (status & (RxOK | RxUnderrun | RxOverflow | RxFIFOOver)) /* Rx interrupt */
1701 netdrv_rx_interrupt (dev, tp, ioaddr);
1702
1703 if (status & (TxOK | TxErr))
1704 netdrv_tx_interrupt (dev, tp, ioaddr);
1705
1706 boguscnt--;
1707 } while (boguscnt > 0);
1708
1709 if (boguscnt <= 0) {
1710 printk (KERN_WARNING
1711 "%s: Too much work at interrupt, "
1712 "IntrStatus=0x%4.4x.\n", dev->name,
1713 status);
1714
1715 /* Clear all interrupt sources. */
1716 NETDRV_W16 (IntrStatus, 0xffff);
1717 }
1718
1719 spin_unlock (&tp->lock);
1720
1721 DPRINTK ("%s: exiting interrupt, intr_status=%#4.4x.\n",
1722 dev->name, NETDRV_R16 (IntrStatus));
1723 return IRQ_RETVAL(handled);
1724}
1725
1726
1727static int netdrv_close (struct net_device *dev)
1728{
1729 struct netdrv_private *tp = dev->priv;
1730 void *ioaddr = tp->mmio_addr;
1731 unsigned long flags;
1732
1733 DPRINTK ("ENTER\n");
1734
1735 netif_stop_queue (dev);
1736
1737 DPRINTK ("%s: Shutting down ethercard, status was 0x%4.4x.\n",
1738 dev->name, NETDRV_R16 (IntrStatus));
1739
1740 del_timer_sync (&tp->timer);
1741
1742 spin_lock_irqsave (&tp->lock, flags);
1743
1744 /* Stop the chip's Tx and Rx DMA processes. */
1745 NETDRV_W8 (ChipCmd, (NETDRV_R8 (ChipCmd) & ChipCmdClear));
1746
1747 /* Disable interrupts by clearing the interrupt mask. */
1748 NETDRV_W16 (IntrMask, 0x0000);
1749
1750 /* Update the error counts. */
1751 tp->stats.rx_missed_errors += NETDRV_R32 (RxMissed);
1752 NETDRV_W32 (RxMissed, 0);
1753
1754 spin_unlock_irqrestore (&tp->lock, flags);
1755
1756 synchronize_irq ();
1757 free_irq (dev->irq, dev);
1758
1759 netdrv_tx_clear (tp);
1760
1761 pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
1762 tp->rx_ring, tp->rx_ring_dma);
1763 pci_free_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
1764 tp->tx_bufs, tp->tx_bufs_dma);
1765 tp->rx_ring = NULL;
1766 tp->tx_bufs = NULL;
1767
1768 /* Green! Put the chip in low-power mode. */
1769 NETDRV_W8 (Cfg9346, Cfg9346_Unlock);
1770 NETDRV_W8 (Config1, 0x03);
1771 NETDRV_W8 (Cfg9346, Cfg9346_Lock);
1772
1773 DPRINTK ("EXIT\n");
1774 return 0;
1775}
1776
1777
1778static int netdrv_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
1779{
1780 struct netdrv_private *tp = dev->priv;
1781 struct mii_ioctl_data *data = if_mii(rq);
1782 unsigned long flags;
1783 int rc = 0;
1784
1785 DPRINTK ("ENTER\n");
1786
1787 switch (cmd) {
1788 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
1789 data->phy_id = tp->phys[0] & 0x3f;
1790 /* Fall Through */
1791
1792 case SIOCGMIIREG: /* Read MII PHY register. */
1793 spin_lock_irqsave (&tp->lock, flags);
1794 data->val_out = mdio_read (dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
1795 spin_unlock_irqrestore (&tp->lock, flags);
1796 break;
1797
1798 case SIOCSMIIREG: /* Write MII PHY register. */
1799 if (!capable (CAP_NET_ADMIN)) {
1800 rc = -EPERM;
1801 break;
1802 }
1803
1804 spin_lock_irqsave (&tp->lock, flags);
1805 mdio_write (dev, data->phy_id & 0x1f, data->reg_num & 0x1f, data->val_in);
1806 spin_unlock_irqrestore (&tp->lock, flags);
1807 break;
1808
1809 default:
1810 rc = -EOPNOTSUPP;
1811 break;
1812 }
1813
1814 DPRINTK ("EXIT, returning %d\n", rc);
1815 return rc;
1816}
1817
1818
1819static struct net_device_stats *netdrv_get_stats (struct net_device *dev)
1820{
1821 struct netdrv_private *tp = dev->priv;
1822 void *ioaddr = tp->mmio_addr;
1823
1824 DPRINTK ("ENTER\n");
1825
1826 assert (tp != NULL);
1827
1828 if (netif_running(dev)) {
1829 unsigned long flags;
1830
1831 spin_lock_irqsave (&tp->lock, flags);
1832
1833 tp->stats.rx_missed_errors += NETDRV_R32 (RxMissed);
1834 NETDRV_W32 (RxMissed, 0);
1835
1836 spin_unlock_irqrestore (&tp->lock, flags);
1837 }
1838
1839 DPRINTK ("EXIT\n");
1840 return &tp->stats;
1841}
1842
1843/* Set or clear the multicast filter for this adaptor.
1844 This routine is not state sensitive and need not be SMP locked. */
1845
1846static void netdrv_set_rx_mode (struct net_device *dev)
1847{
1848 struct netdrv_private *tp = dev->priv;
1849 void *ioaddr = tp->mmio_addr;
1850 u32 mc_filter[2]; /* Multicast hash filter */
1851 int i, rx_mode;
1852 u32 tmp;
1853
1854 DPRINTK ("ENTER\n");
1855
1856 DPRINTK ("%s: netdrv_set_rx_mode(%4.4x) done -- Rx config %8.8x.\n",
1857 dev->name, dev->flags, NETDRV_R32 (RxConfig));
1858
1859 /* Note: do not reorder, GCC is clever about common statements. */
1860 if (dev->flags & IFF_PROMISC) {
1861 /* Unconditionally log net taps. */
1862 printk (KERN_NOTICE "%s: Promiscuous mode enabled.\n",
1863 dev->name);
1864 rx_mode =
1865 AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
1866 AcceptAllPhys;
1867 mc_filter[1] = mc_filter[0] = 0xffffffff;
1868 } else if ((dev->mc_count > multicast_filter_limit)
1869 || (dev->flags & IFF_ALLMULTI)) {
1870 /* Too many to filter perfectly -- accept all multicasts. */
1871 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1872 mc_filter[1] = mc_filter[0] = 0xffffffff;
1873 } else {
1874 struct dev_mc_list *mclist;
1875 rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
1876 mc_filter[1] = mc_filter[0] = 0;
1877 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1878 i++, mclist = mclist->next) {
1879 int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
1880
1881 mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
1882 }
1883 }
1884
1885 /* if called from irq handler, lock already acquired */
1886 if (!in_irq ())
1887 spin_lock_irq (&tp->lock);
1888
1889 /* We can safely update without stopping the chip. */
1890 tmp = netdrv_rx_config | rx_mode |
1891 (NETDRV_R32 (RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
1892 NETDRV_W32_F (RxConfig, tmp);
1893 NETDRV_W32_F (MAR0 + 0, mc_filter[0]);
1894 NETDRV_W32_F (MAR0 + 4, mc_filter[1]);
1895
1896 if (!in_irq ())
1897 spin_unlock_irq (&tp->lock);
1898
1899 DPRINTK ("EXIT\n");
1900}
1901
1902
1903#ifdef CONFIG_PM
1904
Pavel Machek05adc3b2005-04-16 15:25:25 -07001905static int netdrv_suspend (struct pci_dev *pdev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906{
1907 struct net_device *dev = pci_get_drvdata (pdev);
1908 struct netdrv_private *tp = dev->priv;
1909 void *ioaddr = tp->mmio_addr;
1910 unsigned long flags;
1911
1912 if (!netif_running(dev))
1913 return 0;
1914 netif_device_detach (dev);
1915
1916 spin_lock_irqsave (&tp->lock, flags);
1917
1918 /* Disable interrupts, stop Tx and Rx. */
1919 NETDRV_W16 (IntrMask, 0x0000);
1920 NETDRV_W8 (ChipCmd, (NETDRV_R8 (ChipCmd) & ChipCmdClear));
1921
1922 /* Update the error counts. */
1923 tp->stats.rx_missed_errors += NETDRV_R32 (RxMissed);
1924 NETDRV_W32 (RxMissed, 0);
1925
1926 spin_unlock_irqrestore (&tp->lock, flags);
1927
1928 pci_save_state (pdev);
1929 pci_set_power_state (pdev, PCI_D3hot);
1930
1931 return 0;
1932}
1933
1934
1935static int netdrv_resume (struct pci_dev *pdev)
1936{
1937 struct net_device *dev = pci_get_drvdata (pdev);
1938 struct netdrv_private *tp = dev->priv;
1939
1940 if (!netif_running(dev))
1941 return 0;
1942 pci_set_power_state (pdev, PCI_D0);
1943 pci_restore_state (pdev);
1944 netif_device_attach (dev);
1945 netdrv_hw_start (dev);
1946
1947 return 0;
1948}
1949
1950#endif /* CONFIG_PM */
1951
1952
1953static struct pci_driver netdrv_pci_driver = {
1954 .name = MODNAME,
1955 .id_table = netdrv_pci_tbl,
1956 .probe = netdrv_init_one,
1957 .remove = __devexit_p(netdrv_remove_one),
1958#ifdef CONFIG_PM
1959 .suspend = netdrv_suspend,
1960 .resume = netdrv_resume,
1961#endif /* CONFIG_PM */
1962};
1963
1964
1965static int __init netdrv_init_module (void)
1966{
1967/* when a module, this is printed whether or not devices are found in probe */
1968#ifdef MODULE
1969 printk(version);
1970#endif
1971 return pci_module_init (&netdrv_pci_driver);
1972}
1973
1974
1975static void __exit netdrv_cleanup_module (void)
1976{
1977 pci_unregister_driver (&netdrv_pci_driver);
1978}
1979
1980
1981module_init(netdrv_init_module);
1982module_exit(netdrv_cleanup_module);