Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / drivers / net / pcnet32.c
blobdc580b0fe5ccef08317c6f2a50b81721d2a59815
1 /* pcnet32.c: An AMD PCnet32 ethernet driver for linux. */
2 /*
3 * Copyright 1996-1999 Thomas Bogendoerfer
4 *
5 * Derived from the lance driver written 1993,1994,1995 by Donald Becker.
6 *
7 * Copyright 1993 United States Government as represented by the
8 * Director, National Security Agency.
9 *
10 * This software may be used and distributed according to the terms
11 * of the GNU Public License, incorporated herein by reference.
13 * This driver is for PCnet32 and PCnetPCI based ethercards
16 static const char *version = "pcnet32.c:v1.25kf 26.9.1999 tsbogend@alpha.franken.de\n";
18 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
23 #include <linux/ptrace.h>
24 #include <linux/errno.h>
25 #include <linux/ioport.h>
26 #include <linux/malloc.h>
27 #include <linux/interrupt.h>
28 #include <linux/pci.h>
29 #include <linux/delay.h>
30 #include <linux/init.h>
31 #include <asm/bitops.h>
32 #include <asm/io.h>
33 #include <asm/dma.h>
35 #include <linux/netdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/skbuff.h>
38 #include <linux/spinlock.h>
40 static unsigned int pcnet32_portlist[] __initdata = {0x300, 0x320, 0x340, 0x360, 0};
42 static int pcnet32_debug = 1;
43 static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
45 static struct net_device *pcnet32_dev = NULL;
47 static const int max_interrupt_work = 80;
48 static const int rx_copybreak = 200;
50 #define PORT_AUI 0x00
51 #define PORT_10BT 0x01
52 #define PORT_GPSI 0x02
53 #define PORT_MII 0x03
55 #define PORT_PORTSEL 0x03
56 #define PORT_ASEL 0x04
57 #define PORT_100 0x40
58 #define PORT_FD 0x80
60 #define PCNET32_DMA_MASK 0xffffffff
63 * table to translate option values from tulip
64 * to internal options
66 static unsigned char options_mapping[] = {
67 PORT_ASEL, /* 0 Auto-select */
68 PORT_AUI, /* 1 BNC/AUI */
69 PORT_AUI, /* 2 AUI/BNC */
70 PORT_ASEL, /* 3 not supported */
71 PORT_10BT | PORT_FD, /* 4 10baseT-FD */
72 PORT_ASEL, /* 5 not supported */
73 PORT_ASEL, /* 6 not supported */
74 PORT_ASEL, /* 7 not supported */
75 PORT_ASEL, /* 8 not supported */
76 PORT_MII, /* 9 MII 10baseT */
77 PORT_MII | PORT_FD, /* 10 MII 10baseT-FD */
78 PORT_MII, /* 11 MII (autosel) */
79 PORT_10BT, /* 12 10BaseT */
80 PORT_MII | PORT_100, /* 13 MII 100BaseTx */
81 PORT_MII | PORT_100 | PORT_FD, /* 14 MII 100BaseTx-FD */
82 PORT_ASEL /* 15 not supported */
85 #define MAX_UNITS 8
86 static int options[MAX_UNITS] = {0, };
87 static int full_duplex[MAX_UNITS] = {0, };
90 * Theory of Operation
92 * This driver uses the same software structure as the normal lance
93 * driver. So look for a verbose description in lance.c. The differences
94 * to the normal lance driver is the use of the 32bit mode of PCnet32
95 * and PCnetPCI chips. Because these chips are 32bit chips, there is no
96 * 16MB limitation and we don't need bounce buffers.
100 * History:
101 * v0.01: Initial version
102 * only tested on Alpha Noname Board
103 * v0.02: changed IRQ handling for new interrupt scheme (dev_id)
104 * tested on a ASUS SP3G
105 * v0.10: fixed an odd problem with the 79C974 in a Compaq Deskpro XL
106 * looks like the 974 doesn't like stopping and restarting in a
107 * short period of time; now we do a reinit of the lance; the
108 * bug was triggered by doing ifconfig eth0 <ip> broadcast <addr>
109 * and hangs the machine (thanks to Klaus Liedl for debugging)
110 * v0.12: by suggestion from Donald Becker: Renamed driver to pcnet32,
111 * made it standalone (no need for lance.c)
112 * v0.13: added additional PCI detecting for special PCI devices (Compaq)
113 * v0.14: stripped down additional PCI probe (thanks to David C Niemi
114 * and sveneric@xs4all.nl for testing this on their Compaq boxes)
115 * v0.15: added 79C965 (VLB) probe
116 * added interrupt sharing for PCI chips
117 * v0.16: fixed set_multicast_list on Alpha machines
118 * v0.17: removed hack from dev.c; now pcnet32 uses ethif_probe in Space.c
119 * v0.19: changed setting of autoselect bit
120 * v0.20: removed additional Compaq PCI probe; there is now a working one
121 * in arch/i386/bios32.c
122 * v0.21: added endian conversion for ppc, from work by cort@cs.nmt.edu
123 * v0.22: added printing of status to ring dump
124 * v0.23: changed enet_statistics to net_devive_stats
125 * v0.90: added multicast filter
126 * added module support
127 * changed irq probe to new style
128 * added PCnetFast chip id
129 * added fix for receive stalls with Intel saturn chipsets
130 * added in-place rx skbs like in the tulip driver
131 * minor cleanups
132 * v0.91: added PCnetFast+ chip id
133 * back port to 2.0.x
134 * v1.00: added some stuff from Donald Becker's 2.0.34 version
135 * added support for byte counters in net_dev_stats
136 * v1.01: do ring dumps, only when debugging the driver
137 * increased the transmit timeout
138 * v1.02: fixed memory leak in pcnet32_init_ring()
139 * v1.10: workaround for stopped transmitter
140 * added port selection for modules
141 * detect special T1/E1 WAN card and setup port selection
142 * v1.11: fixed wrong checking of Tx errors
143 * v1.20: added check of return value kmalloc (cpeterso@cs.washington.edu)
144 * added save original kmalloc addr for freeing (mcr@solidum.com)
145 * added support for PCnetHome chip (joe@MIT.EDU)
146 * rewritten PCI card detection
147 * added dwio mode to get driver working on some PPC machines
148 * v1.21: added mii selection and mii ioctl
149 * v1.22: changed pci scanning code to make PPC people happy
150 * fixed switching to 32bit mode in pcnet32_open() (thanks
151 * to Michael Richard <mcr@solidum.com> for noticing this one)
152 * added sub vendor/device id matching (thanks again to
153 * Michael Richard <mcr@solidum.com>)
154 * added chip id for 79c973/975 (thanks to Zach Brown <zab@zabbo.net>)
155 * v1.23 fixed small bug, when manual selecting MII speed/duplex
156 * v1.24 Applied Thomas' patch to use TxStartPoint and thus decrease TxFIFO
157 * underflows. Added tx_start_pt module parameter. Increased
158 * TX_RING_SIZE from 16 to 32. Added #ifdef'd code to use DXSUFLO
159 * for FAST[+] chipsets. <kaf@fc.hp.com>
160 * v1.24ac Added SMP spinlocking - Alan Cox <alan@redhat.com>
161 * v1.25kf Added No Interrupt on successful Tx for some Tx's <kaf@fc.hp.com>
162 * v1.26 Converted to pci_alloc_consistent, Jamey Hicks / George France
163 * <jamey@crl.dec.com>
168 * Set the number of Tx and Rx buffers, using Log_2(# buffers).
169 * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
170 * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
172 #ifndef PCNET32_LOG_TX_BUFFERS
173 #define PCNET32_LOG_TX_BUFFERS 4
174 #define PCNET32_LOG_RX_BUFFERS 5
175 #endif
177 #define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
178 #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
179 #define TX_RING_LEN_BITS ((PCNET32_LOG_TX_BUFFERS) << 12)
181 #define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
182 #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
183 #define RX_RING_LEN_BITS ((PCNET32_LOG_RX_BUFFERS) << 4)
185 #define PKT_BUF_SZ 1544
187 /* Offsets from base I/O address. */
188 #define PCNET32_WIO_RDP 0x10
189 #define PCNET32_WIO_RAP 0x12
190 #define PCNET32_WIO_RESET 0x14
191 #define PCNET32_WIO_BDP 0x16
193 #define PCNET32_DWIO_RDP 0x10
194 #define PCNET32_DWIO_RAP 0x14
195 #define PCNET32_DWIO_RESET 0x18
196 #define PCNET32_DWIO_BDP 0x1C
198 #define PCNET32_TOTAL_SIZE 0x20
200 /* some PCI ids */
201 #ifndef PCI_DEVICE_ID_AMD_LANCE
202 #define PCI_VENDOR_ID_AMD 0x1022
203 #define PCI_DEVICE_ID_AMD_LANCE 0x2000
204 #endif
205 #ifndef PCI_DEVICE_ID_AMD_PCNETHOME
206 #define PCI_DEVICE_ID_AMD_PCNETHOME 0x2001
207 #endif
210 #define CRC_POLYNOMIAL_LE 0xedb88320UL /* Ethernet CRC, little endian */
212 /* The PCNET32 Rx and Tx ring descriptors. */
213 struct pcnet32_rx_head {
214 u32 base;
215 s16 buf_length;
216 s16 status;
217 u32 msg_length;
218 u32 reserved;
221 struct pcnet32_tx_head {
222 u32 base;
223 s16 length;
224 s16 status;
225 u32 misc;
226 u32 reserved;
229 /* The PCNET32 32-Bit initialization block, described in databook. */
230 struct pcnet32_init_block {
231 u16 mode;
232 u16 tlen_rlen;
233 u8 phys_addr[6];
234 u16 reserved;
235 u32 filter[2];
236 /* Receive and transmit ring base, along with extra bits. */
237 u32 rx_ring;
238 u32 tx_ring;
241 /* PCnet32 access functions */
242 struct pcnet32_access {
243 u16 (*read_csr)(unsigned long, int);
244 void (*write_csr)(unsigned long, int, u16);
245 u16 (*read_bcr)(unsigned long, int);
246 void (*write_bcr)(unsigned long, int, u16);
247 u16 (*read_rap)(unsigned long);
248 void (*write_rap)(unsigned long, u16);
249 void (*reset)(unsigned long);
253 * The first three fields of pcnet32_private are read by the ethernet device
254 * so we allocate the structure should be allocated by pci_alloc_consistent().
256 struct pcnet32_private {
257 /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
258 struct pcnet32_rx_head rx_ring[RX_RING_SIZE];
259 struct pcnet32_tx_head tx_ring[TX_RING_SIZE];
260 struct pcnet32_init_block init_block;
261 dma_addr_t dma_addr; /* DMA address of beginning of this object, returned by pci_alloc_consistent */
262 struct pci_dev *pci_dev; /* Pointer to the associated pci device structure */
263 const char *name;
264 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
265 struct sk_buff *tx_skbuff[TX_RING_SIZE];
266 struct sk_buff *rx_skbuff[RX_RING_SIZE];
267 dma_addr_t tx_dma_addr[TX_RING_SIZE];
268 dma_addr_t rx_dma_addr[RX_RING_SIZE];
269 struct pcnet32_access a;
270 spinlock_t lock; /* Guard lock */
271 unsigned int cur_rx, cur_tx; /* The next free ring entry */
272 unsigned int dirty_rx, dirty_tx; /* The ring entries to be free()ed. */
273 struct net_device_stats stats;
274 char tx_full;
275 int options;
276 int shared_irq:1, /* shared irq possible */
277 ltint:1,
278 #ifdef DO_DXSUFLO
279 dxsuflo:1, /* disable transmit stop on uflo */
280 #endif
281 full_duplex:1, /* full duplex possible */
282 mii:1; /* mii port available */
283 struct net_device *next;
286 static int pcnet32_probe_vlbus(int cards_found);
287 static int pcnet32_probe_pci(struct pci_dev *, const struct pci_device_id *);
288 static int pcnet32_probe1(unsigned long, unsigned char, int, int, struct pci_dev *);
289 static int pcnet32_open(struct net_device *);
290 static int pcnet32_init_ring(struct net_device *);
291 static int pcnet32_start_xmit(struct sk_buff *, struct net_device *);
292 static int pcnet32_rx(struct net_device *);
293 static void pcnet32_tx_timeout (struct net_device *dev);
294 static void pcnet32_interrupt(int, void *, struct pt_regs *);
295 static int pcnet32_close(struct net_device *);
296 static struct net_device_stats *pcnet32_get_stats(struct net_device *);
297 static void pcnet32_set_multicast_list(struct net_device *);
298 #ifdef HAVE_PRIVATE_IOCTL
299 static int pcnet32_mii_ioctl(struct net_device *, struct ifreq *, int);
300 #endif
302 enum pci_flags_bit {
303 PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
304 PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
307 struct pcnet32_pci_id_info {
308 const char *name;
309 u16 vendor_id, device_id, svid, sdid, flags;
310 int io_size;
311 int (*probe1) (unsigned long, unsigned char, int, int, struct pci_dev *);
314 static struct pcnet32_pci_id_info pcnet32_tbl[] = {
315 { "AMD PCnetPCI series",
316 PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, 0, 0,
317 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
318 pcnet32_probe1},
319 { "AMD PCnetPCI series (IBM)",
320 PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, 0x1014, 0x2000,
321 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
322 pcnet32_probe1},
323 { "AMD PCnetHome series",
324 PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_PCNETHOME, 0, 0,
325 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
326 pcnet32_probe1},
327 {0,}
331 * PCI device identifiers for "new style" Linux PCI Device Drivers
333 static struct pci_device_id pcnet32_pci_tbl[] __devinitdata = {
334 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_PCNETHOME, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
335 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
336 { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, 0x1014, 0x2000, 0, 0, 0 },
337 { 0, }
340 MODULE_DEVICE_TABLE (pci, pcnet32_pci_tbl);
342 static u16 pcnet32_wio_read_csr (unsigned long addr, int index)
344 outw (index, addr+PCNET32_WIO_RAP);
345 return inw (addr+PCNET32_WIO_RDP);
348 static void pcnet32_wio_write_csr (unsigned long addr, int index, u16 val)
350 outw (index, addr+PCNET32_WIO_RAP);
351 outw (val, addr+PCNET32_WIO_RDP);
354 static u16 pcnet32_wio_read_bcr (unsigned long addr, int index)
356 outw (index, addr+PCNET32_WIO_RAP);
357 return inw (addr+PCNET32_WIO_BDP);
360 static void pcnet32_wio_write_bcr (unsigned long addr, int index, u16 val)
362 outw (index, addr+PCNET32_WIO_RAP);
363 outw (val, addr+PCNET32_WIO_BDP);
366 static u16 pcnet32_wio_read_rap (unsigned long addr)
368 return inw (addr+PCNET32_WIO_RAP);
371 static void pcnet32_wio_write_rap (unsigned long addr, u16 val)
373 outw (val, addr+PCNET32_WIO_RAP);
376 static void pcnet32_wio_reset (unsigned long addr)
378 inw (addr+PCNET32_WIO_RESET);
381 static int pcnet32_wio_check (unsigned long addr)
383 outw (88, addr+PCNET32_WIO_RAP);
384 return (inw (addr+PCNET32_WIO_RAP) == 88);
387 static struct pcnet32_access pcnet32_wio = {
388 pcnet32_wio_read_csr,
389 pcnet32_wio_write_csr,
390 pcnet32_wio_read_bcr,
391 pcnet32_wio_write_bcr,
392 pcnet32_wio_read_rap,
393 pcnet32_wio_write_rap,
394 pcnet32_wio_reset
397 static u16 pcnet32_dwio_read_csr (unsigned long addr, int index)
399 outl (index, addr+PCNET32_DWIO_RAP);
400 return (inl (addr+PCNET32_DWIO_RDP) & 0xffff);
403 static void pcnet32_dwio_write_csr (unsigned long addr, int index, u16 val)
405 outl (index, addr+PCNET32_DWIO_RAP);
406 outl (val, addr+PCNET32_DWIO_RDP);
409 static u16 pcnet32_dwio_read_bcr (unsigned long addr, int index)
411 outl (index, addr+PCNET32_DWIO_RAP);
412 return (inl (addr+PCNET32_DWIO_BDP) & 0xffff);
415 static void pcnet32_dwio_write_bcr (unsigned long addr, int index, u16 val)
417 outl (index, addr+PCNET32_DWIO_RAP);
418 outl (val, addr+PCNET32_DWIO_BDP);
421 static u16 pcnet32_dwio_read_rap (unsigned long addr)
423 return (inl (addr+PCNET32_DWIO_RAP) & 0xffff);
426 static void pcnet32_dwio_write_rap (unsigned long addr, u16 val)
428 outl (val, addr+PCNET32_DWIO_RAP);
431 static void pcnet32_dwio_reset (unsigned long addr)
433 inl (addr+PCNET32_DWIO_RESET);
436 static int pcnet32_dwio_check (unsigned long addr)
438 outl (88, addr+PCNET32_DWIO_RAP);
439 return (inl (addr+PCNET32_DWIO_RAP) == 88);
442 static struct pcnet32_access pcnet32_dwio = {
443 pcnet32_dwio_read_csr,
444 pcnet32_dwio_write_csr,
445 pcnet32_dwio_read_bcr,
446 pcnet32_dwio_write_bcr,
447 pcnet32_dwio_read_rap,
448 pcnet32_dwio_write_rap,
449 pcnet32_dwio_reset
455 /* only probes for non-PCI devices, the rest are handled by pci_register_driver via pcnet32_probe_pci*/
456 static int __init pcnet32_probe_vlbus(int cards_found)
458 unsigned long ioaddr = 0; // FIXME dev ? dev->base_addr: 0;
459 unsigned int irq_line = 0; // FIXME dev ? dev->irq : 0;
460 int *port;
462 printk(KERN_INFO "pcnet32_probe_vlbus: cards_found=%d\n", cards_found);
463 #ifndef __powerpc__
464 if (ioaddr > 0x1ff) {
465 if (check_region(ioaddr, PCNET32_TOTAL_SIZE) == 0)
466 return pcnet32_probe1(ioaddr, irq_line, 0, 0, NULL);
467 else
468 return -ENODEV;
469 } else
470 #endif
471 if (ioaddr != 0)
472 return -ENXIO;
474 /* now look for PCnet32 VLB cards */
475 for (port = pcnet32_portlist; *port; port++) {
476 unsigned long ioaddr = *port;
478 if ( check_region(ioaddr, PCNET32_TOTAL_SIZE) == 0) {
479 /* check if there is really a pcnet chip on that ioaddr */
480 if ((inb(ioaddr + 14) == 0x57) &&
481 (inb(ioaddr + 15) == 0x57) &&
482 (pcnet32_probe1(ioaddr, 0, 0, 0, NULL) == 0))
483 cards_found++;
486 return cards_found ? 0: -ENODEV;
491 static int __init
492 pcnet32_probe_pci(struct pci_dev *pdev, const struct pci_device_id *ent)
494 static int card_idx = 0;
495 long ioaddr;
496 int err = 0;
498 printk(KERN_INFO "pcnet32_probe_pci: found device %#08x.%#08x\n", ent->vendor, ent->device);
500 ioaddr = pci_resource_start (pdev, 0);
501 printk(KERN_INFO " ioaddr=%#08lx resource_flags=%#08lx\n", ioaddr, pci_resource_flags (pdev, 0));
502 if (!ioaddr) {
503 printk (KERN_ERR "no PCI IO resources, aborting\n");
504 return -ENODEV;
507 if (!pci_dma_supported(pdev, PCNET32_DMA_MASK)) {
508 printk(KERN_ERR "pcnet32.c: architecture does not support 32bit PCI busmaster DMA\n");
509 return -ENODEV;
512 if ((err = pci_enable_device(pdev)) < 0) {
513 printk(KERN_ERR "pcnet32.c: failed to enable device -- err=%d\n", err);
514 return err;
517 pci_set_master(pdev);
519 return pcnet32_probe1(ioaddr, pdev->irq, 1, card_idx, pdev);
523 /* pcnet32_probe1
524 * Called from both pcnet32_probe_vlbus and pcnet_probe_pci.
525 * pdev will be NULL when called from pcnet32_probe_vlbus.
527 static int __init
528 pcnet32_probe1(unsigned long ioaddr, unsigned char irq_line, int shared, int card_idx, struct pci_dev *pdev)
530 struct pcnet32_private *lp;
531 dma_addr_t lp_dma_addr;
532 int i,media,fdx = 0, mii = 0, fset = 0;
533 #ifdef DO_DXSUFLO
534 int dxsuflo = 0;
535 #endif
536 int ltint = 0;
537 int chip_version;
538 char *chipname;
539 struct net_device *dev;
540 struct pcnet32_access *a = NULL;
542 /* reset the chip */
543 pcnet32_dwio_reset(ioaddr);
544 pcnet32_wio_reset(ioaddr);
546 if (pcnet32_wio_read_csr (ioaddr, 0) == 4 && pcnet32_wio_check (ioaddr)) {
547 a = &pcnet32_wio;
548 } else {
549 if (pcnet32_dwio_read_csr (ioaddr, 0) == 4 && pcnet32_dwio_check(ioaddr)) {
550 a = &pcnet32_dwio;
551 } else
552 return -ENODEV;
555 chip_version = a->read_csr (ioaddr, 88) | (a->read_csr (ioaddr,89) << 16);
556 if (pcnet32_debug > 2)
557 printk(KERN_INFO " PCnet chip version is %#x.\n", chip_version);
558 if ((chip_version & 0xfff) != 0x003)
559 return -ENODEV;
560 chip_version = (chip_version >> 12) & 0xffff;
561 switch (chip_version) {
562 case 0x2420:
563 chipname = "PCnet/PCI 79C970"; /* PCI */
564 break;
565 case 0x2430:
566 if (shared)
567 chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
568 else
569 chipname = "PCnet/32 79C965"; /* 486/VL bus */
570 break;
571 case 0x2621:
572 chipname = "PCnet/PCI II 79C970A"; /* PCI */
573 fdx = 1;
574 break;
575 case 0x2623:
576 chipname = "PCnet/FAST 79C971"; /* PCI */
577 fdx = 1; mii = 1; fset = 1;
578 ltint = 1;
579 break;
580 case 0x2624:
581 chipname = "PCnet/FAST+ 79C972"; /* PCI */
582 fdx = 1; mii = 1; fset = 1;
583 break;
584 case 0x2625:
585 chipname = "PCnet/FAST III 79C973"; /* PCI */
586 fdx = 1; mii = 1;
587 break;
588 case 0x2626:
589 chipname = "PCnet/Home 79C978"; /* PCI */
590 fdx = 1;
592 * This is based on specs published at www.amd.com. This section
593 * assumes that a card with a 79C978 wants to go into 1Mb HomePNA
594 * mode. The 79C978 can also go into standard ethernet, and there
595 * probably should be some sort of module option to select the
596 * mode by which the card should operate
598 /* switch to home wiring mode */
599 media = a->read_bcr (ioaddr, 49);
600 #if 0
601 if (pcnet32_debug > 2)
602 printk(KERN_DEBUG "pcnet32: pcnet32 media value %#x.\n", media);
603 media &= ~3;
604 media |= 1;
605 #endif
606 if (pcnet32_debug > 2)
607 printk(KERN_DEBUG "pcnet32: pcnet32 media reset to %#x.\n", media);
608 a->write_bcr (ioaddr, 49, media);
609 break;
610 case 0x2627:
611 chipname = "PCnet/FAST III 79C975"; /* PCI */
612 fdx = 1; mii = 1;
613 break;
614 default:
615 printk(KERN_INFO "pcnet32: PCnet version %#x, no PCnet32 chip.\n",chip_version);
616 return -ENODEV;
620 * On selected chips turn on the BCR18:NOUFLO bit. This stops transmit
621 * starting until the packet is loaded. Strike one for reliability, lose
622 * one for latency - although on PCI this isnt a big loss. Older chips
623 * have FIFO's smaller than a packet, so you can't do this.
626 if(fset)
628 a->write_bcr(ioaddr, 18, (a->read_bcr(ioaddr, 18) | 0x0800));
629 a->write_csr(ioaddr, 80, (a->read_csr(ioaddr, 80) & 0x0C00) | 0x0c00);
630 #ifdef DO_DXSUFLO
631 dxsuflo = 1;
632 #endif
633 ltint = 1;
636 dev = init_etherdev(NULL, 0);
637 if(dev==NULL)
638 return -ENOMEM;
640 printk(KERN_INFO "%s: %s at %#3lx,", dev->name, chipname, ioaddr);
642 /* There is a 16 byte station address PROM at the base address.
643 The first six bytes are the station address. */
644 for (i = 0; i < 6; i++)
645 printk( KERN_INFO " %2.2x", dev->dev_addr[i] = inb(ioaddr + i));
647 if (((chip_version + 1) & 0xfffe) == 0x2624) { /* Version 0x2623 or 0x2624 */
648 i = a->read_csr(ioaddr, 80) & 0x0C00; /* Check tx_start_pt */
649 printk(KERN_INFO"\n tx_start_pt(0x%04x):",i);
650 switch(i>>10) {
651 case 0: printk(KERN_INFO " 20 bytes,"); break;
652 case 1: printk(KERN_INFO " 64 bytes,"); break;
653 case 2: printk(KERN_INFO " 128 bytes,"); break;
654 case 3: printk(KERN_INFO "~220 bytes,"); break;
656 i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
657 printk(KERN_INFO" BCR18(%x):",i&0xffff);
658 if (i & (1<<5)) printk(KERN_INFO "BurstWrEn ");
659 if (i & (1<<6)) printk(KERN_INFO "BurstRdEn ");
660 if (i & (1<<7)) printk(KERN_INFO "DWordIO ");
661 if (i & (1<<11)) printk(KERN_INFO"NoUFlow ");
662 i = a->read_bcr(ioaddr, 25);
663 printk(KERN_INFO "\n SRAMSIZE=0x%04x,",i<<8);
664 i = a->read_bcr(ioaddr, 26);
665 printk(KERN_INFO " SRAM_BND=0x%04x,",i<<8);
666 i = a->read_bcr(ioaddr, 27);
667 if (i & (1<<14)) printk(KERN_INFO "LowLatRx,");
670 dev->base_addr = ioaddr;
671 request_region(ioaddr, PCNET32_TOTAL_SIZE, chipname);
673 /* pci_alloc_consistent returns page-aligned memory, so we do not have to check the alignment */
674 if ((lp = (struct pcnet32_private *)pci_alloc_consistent(pdev, sizeof(*lp), &lp_dma_addr)) == NULL)
675 return -ENOMEM;
677 memset(lp, 0, sizeof(*lp));
678 lp->dma_addr = lp_dma_addr;
679 lp->pci_dev = pdev;
680 printk(KERN_INFO "pcnet32: pcnet32_private lp=%p lp_dma_addr=%#08x\n", lp, lp_dma_addr);
682 spin_lock_init(&lp->lock);
684 dev->priv = lp;
685 lp->name = chipname;
686 lp->shared_irq = shared;
687 lp->full_duplex = fdx;
688 #ifdef DO_DXSUFLO
689 lp->dxsuflo = dxsuflo;
690 #endif
691 lp->ltint = ltint;
692 lp->mii = mii;
693 if (options[card_idx] > sizeof (options_mapping))
694 lp->options = PORT_ASEL;
695 else
696 lp->options = options_mapping[options[card_idx]];
698 if (fdx && !(lp->options & PORT_ASEL) && full_duplex[card_idx])
699 lp->options |= PORT_FD;
701 lp->a = *a;
702 if (a == NULL) {
703 printk(KERN_ERR "pcnet32: No access methods\n");
704 return -ENODEV;
707 /* detect special T1/E1 WAN card by checking for MAC address */
708 if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0 && dev->dev_addr[2] == 0x75)
709 lp->options = PORT_FD | PORT_GPSI;
711 lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
712 lp->init_block.tlen_rlen = le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
713 for (i = 0; i < 6; i++)
714 lp->init_block.phys_addr[i] = dev->dev_addr[i];
715 lp->init_block.filter[0] = 0x00000000;
716 lp->init_block.filter[1] = 0x00000000;
717 lp->init_block.rx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, rx_ring));
718 lp->init_block.tx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, tx_ring));
720 /* switch pcnet32 to 32bit mode */
721 a->write_bcr (ioaddr, 20, 2);
723 a->write_csr (ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) & 0xffff);
724 a->write_csr (ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) >> 16);
726 if (irq_line) {
727 dev->irq = irq_line;
730 if (dev->irq >= 2)
731 printk(KERN_INFO " assigned IRQ %d.\n", dev->irq);
732 else {
733 unsigned long irq_mask = probe_irq_on();
736 * To auto-IRQ we enable the initialization-done and DMA error
737 * interrupts. For ISA boards we get a DMA error, but VLB and PCI
738 * boards will work.
740 /* Trigger an initialization just for the interrupt. */
741 a->write_csr (ioaddr, 0, 0x41);
742 mdelay (1);
744 dev->irq = probe_irq_off (irq_mask);
745 if (dev->irq)
746 printk(KERN_INFO ", probed IRQ %d.\n", dev->irq);
747 else {
748 printk(KERN_ERR ", failed to detect IRQ line.\n");
749 return -ENODEV;
753 if (pcnet32_debug > 0)
754 printk(KERN_INFO, version);
756 /* The PCNET32-specific entries in the device structure. */
757 dev->open = &pcnet32_open;
758 dev->hard_start_xmit = &pcnet32_start_xmit;
759 dev->stop = &pcnet32_close;
760 dev->get_stats = &pcnet32_get_stats;
761 dev->set_multicast_list = &pcnet32_set_multicast_list;
762 #ifdef HAVE_PRIVATE_IOCTL
763 dev->do_ioctl = &pcnet32_mii_ioctl;
764 #endif
765 dev->tx_timeout = pcnet32_tx_timeout;
766 dev->watchdog_timeo = (HZ >> 1);
768 lp->next = pcnet32_dev;
769 pcnet32_dev = dev;
771 /* Fill in the generic fields of the device structure. */
772 ether_setup(dev);
773 return 0;
777 static int
778 pcnet32_open(struct net_device *dev)
780 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
781 unsigned long ioaddr = dev->base_addr;
782 u16 val;
783 int i;
785 if (dev->irq == 0 ||
786 request_irq(dev->irq, &pcnet32_interrupt,
787 lp->shared_irq ? SA_SHIRQ : 0, lp->name, (void *)dev)) {
788 return -EAGAIN;
791 /* Reset the PCNET32 */
792 lp->a.reset (ioaddr);
794 /* switch pcnet32 to 32bit mode */
795 lp->a.write_bcr (ioaddr, 20, 2);
797 if (pcnet32_debug > 1)
798 printk(KERN_DEBUG "%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
799 dev->name, dev->irq,
800 (u32) (lp->dma_addr + offsetof(struct pcnet32_private, tx_ring)),
801 (u32) (lp->dma_addr + offsetof(struct pcnet32_private, rx_ring)),
802 (u32) (lp->dma_addr + offsetof(struct pcnet32_private, init_block)));
804 /* set/reset autoselect bit */
805 val = lp->a.read_bcr (ioaddr, 2) & ~2;
806 if (lp->options & PORT_ASEL)
807 val |= 2;
808 lp->a.write_bcr (ioaddr, 2, val);
810 /* handle full duplex setting */
811 if (lp->full_duplex) {
812 val = lp->a.read_bcr (ioaddr, 9) & ~3;
813 if (lp->options & PORT_FD) {
814 val |= 1;
815 if (lp->options == (PORT_FD | PORT_AUI))
816 val |= 2;
818 lp->a.write_bcr (ioaddr, 9, val);
821 /* set/reset GPSI bit in test register */
822 val = lp->a.read_csr (ioaddr, 124) & ~0x10;
823 if ((lp->options & PORT_PORTSEL) == PORT_GPSI)
824 val |= 0x10;
825 lp->a.write_csr (ioaddr, 124, val);
827 if (lp->mii & !(lp->options & PORT_ASEL)) {
828 val = lp->a.read_bcr (ioaddr, 32) & ~0x38; /* disable Auto Negotiation, set 10Mpbs, HD */
829 if (lp->options & PORT_FD)
830 val |= 0x10;
831 if (lp->options & PORT_100)
832 val |= 0x08;
833 lp->a.write_bcr (ioaddr, 32, val);
836 #ifdef DO_DXSUFLO
837 if (lp->dxsuflo) { /* Disable transmit stop on underflow */
838 val = lp->a.read_csr (ioaddr, 3);
839 val |= 0x40;
840 lp->a.write_csr (ioaddr, 3, val);
842 #endif
843 if (lp->ltint) { /* Enable TxDone-intr inhibitor */
844 val = lp->a.read_csr (ioaddr, 5);
845 val |= (1<<14);
846 lp->a.write_csr (ioaddr, 5, val);
849 lp->init_block.mode = le16_to_cpu((lp->options & PORT_PORTSEL) << 7);
850 lp->init_block.filter[0] = 0x00000000;
851 lp->init_block.filter[1] = 0x00000000;
852 if (pcnet32_init_ring(dev))
853 return -ENOMEM;
855 /* Re-initialize the PCNET32, and start it when done. */
856 lp->a.write_csr (ioaddr, 1, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) &0xffff);
857 lp->a.write_csr (ioaddr, 2, (lp->dma_addr + offsetof(struct pcnet32_private, init_block)) >> 16);
859 lp->a.write_csr (ioaddr, 4, 0x0915);
860 lp->a.write_csr (ioaddr, 0, 0x0001);
862 netif_start_queue(dev);
864 i = 0;
865 while (i++ < 100)
866 if (lp->a.read_csr (ioaddr, 0) & 0x0100)
867 break;
869 * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
870 * reports that doing so triggers a bug in the '974.
872 lp->a.write_csr (ioaddr, 0, 0x0042);
874 if (pcnet32_debug > 2)
875 printk(KERN_DEBUG "%s: pcnet32 open after %d ticks, init block %#x csr0 %4.4x.\n",
876 dev->name, i, (u32) (lp->dma_addr + offsetof(struct pcnet32_private, init_block)),
877 lp->a.read_csr (ioaddr, 0));
880 MOD_INC_USE_COUNT;
882 return 0; /* Always succeed */
886 * The LANCE has been halted for one reason or another (busmaster memory
887 * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
888 * etc.). Modern LANCE variants always reload their ring-buffer
889 * configuration when restarted, so we must reinitialize our ring
890 * context before restarting. As part of this reinitialization,
891 * find all packets still on the Tx ring and pretend that they had been
892 * sent (in effect, drop the packets on the floor) - the higher-level
893 * protocols will time out and retransmit. It'd be better to shuffle
894 * these skbs to a temp list and then actually re-Tx them after
895 * restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
898 static void
899 pcnet32_purge_tx_ring(struct net_device *dev)
901 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
902 int i;
904 for (i = 0; i < TX_RING_SIZE; i++) {
905 if (lp->tx_skbuff[i]) {
906 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i], lp->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
907 dev_kfree_skb(lp->tx_skbuff[i]);
908 lp->tx_skbuff[i] = NULL;
909 lp->tx_dma_addr[i] = 0;
915 /* Initialize the PCNET32 Rx and Tx rings. */
916 static int
917 pcnet32_init_ring(struct net_device *dev)
919 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
920 int i;
922 lp->tx_full = 0;
923 lp->cur_rx = lp->cur_tx = 0;
924 lp->dirty_rx = lp->dirty_tx = 0;
926 for (i = 0; i < RX_RING_SIZE; i++) {
927 struct sk_buff *rx_skbuff = lp->rx_skbuff[i];
928 if (rx_skbuff == NULL) {
929 if (!(rx_skbuff = lp->rx_skbuff[i] = dev_alloc_skb (PKT_BUF_SZ))) {
930 /* there is not much, we can do at this point */
931 printk(KERN_ERR "%s: pcnet32_init_ring dev_alloc_skb failed.\n",dev->name);
932 return -1;
934 skb_reserve (rx_skbuff, 2);
936 lp->rx_dma_addr[i] = pci_map_single(lp->pci_dev, rx_skbuff->tail, rx_skbuff->len, PCI_DMA_FROMDEVICE);
937 lp->rx_ring[i].base = (u32)le32_to_cpu(lp->rx_dma_addr[i]);
938 lp->rx_ring[i].buf_length = le16_to_cpu(-PKT_BUF_SZ);
939 lp->rx_ring[i].status = le16_to_cpu(0x8000);
941 /* The Tx buffer address is filled in as needed, but we do need to clear
942 the upper ownership bit. */
943 for (i = 0; i < TX_RING_SIZE; i++) {
944 lp->tx_ring[i].base = 0;
945 lp->tx_ring[i].status = 0;
946 lp->tx_dma_addr[i] = 0;
949 lp->init_block.tlen_rlen = le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
950 for (i = 0; i < 6; i++)
951 lp->init_block.phys_addr[i] = dev->dev_addr[i];
952 lp->init_block.rx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, rx_ring));
953 lp->init_block.tx_ring = (u32)le32_to_cpu(lp->dma_addr + offsetof(struct pcnet32_private, tx_ring));
954 return 0;
957 static void
958 pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
960 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
961 unsigned long ioaddr = dev->base_addr;
962 int i;
964 pcnet32_purge_tx_ring(dev);
965 if (pcnet32_init_ring(dev))
966 return;
968 /* ReInit Ring */
969 lp->a.write_csr (ioaddr, 0, 1);
970 i = 0;
971 while (i++ < 100)
972 if (lp->a.read_csr (ioaddr, 0) & 0x0100)
973 break;
975 lp->a.write_csr (ioaddr, 0, csr0_bits);
979 static void
980 pcnet32_tx_timeout (struct net_device *dev)
982 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
983 unsigned int ioaddr = dev->base_addr;
985 /* Transmitter timeout, serious problems. */
986 printk(KERN_ERR "%s: transmit timed out, status %4.4x, resetting.\n",
987 dev->name, lp->a.read_csr (ioaddr, 0));
988 lp->a.write_csr (ioaddr, 0, 0x0004);
989 lp->stats.tx_errors++;
990 if (pcnet32_debug > 2) {
991 int i;
992 printk(KERN_DEBUG " Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
993 lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
994 lp->cur_rx);
995 for (i = 0 ; i < RX_RING_SIZE; i++)
996 printk(KERN_DEBUG "%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
997 lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
998 lp->rx_ring[i].msg_length, (unsigned)lp->rx_ring[i].status);
999 for (i = 0 ; i < TX_RING_SIZE; i++)
1000 printk(KERN_DEBUG "%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
1001 lp->tx_ring[i].base, -lp->tx_ring[i].length,
1002 lp->tx_ring[i].misc, (unsigned)lp->tx_ring[i].status);
1003 printk(KERN_DEBUG "\n");
1005 pcnet32_restart(dev, 0x0042);
1007 dev->trans_start = jiffies;
1008 netif_start_queue(dev);
1012 static int
1013 pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
1015 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1016 unsigned int ioaddr = dev->base_addr;
1017 u16 status;
1018 int entry;
1019 unsigned long flags;
1021 if (pcnet32_debug > 3) {
1022 printk(KERN_DEBUG "%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
1023 dev->name, lp->a.read_csr (ioaddr, 0));
1026 spin_lock_irqsave(&lp->lock, flags);
1028 /* Default status -- will not enable Successful-TxDone
1029 * interrupt when that option is available to us.
1031 status = 0x8300;
1032 if ((lp->ltint) &&
1033 ((lp->cur_tx - lp->dirty_tx == TX_RING_SIZE/2) ||
1034 (lp->cur_tx - lp->dirty_tx >= TX_RING_SIZE-2)))
1036 /* Enable Successful-TxDone interrupt if we have
1037 * 1/2 of, or nearly all of, our ring buffer Tx'd
1038 * but not yet cleaned up. Thus, most of the time,
1039 * we will not enable Successful-TxDone interrupts.
1041 status = 0x9300;
1044 /* Fill in a Tx ring entry */
1046 /* Mask to ring buffer boundary. */
1047 entry = lp->cur_tx & TX_RING_MOD_MASK;
1049 /* Caution: the write order is important here, set the base address
1050 with the "ownership" bits last. */
1052 lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
1054 lp->tx_ring[entry].misc = 0x00000000;
1056 lp->tx_skbuff[entry] = skb;
1057 lp->tx_dma_addr[entry] = pci_map_single(lp->pci_dev, skb->data, skb->len, PCI_DMA_TODEVICE);
1058 lp->tx_ring[entry].base = (u32)le32_to_cpu(lp->tx_dma_addr[entry]);
1059 lp->tx_ring[entry].status = le16_to_cpu(status);
1061 lp->cur_tx++;
1062 lp->stats.tx_bytes += skb->len;
1064 /* Trigger an immediate send poll. */
1065 lp->a.write_csr (ioaddr, 0, 0x0048);
1067 dev->trans_start = jiffies;
1069 if (lp->tx_ring[(entry+1) & TX_RING_MOD_MASK].base == 0)
1070 netif_start_queue(dev);
1071 else {
1072 lp->tx_full = 1;
1073 netif_stop_queue(dev);
1075 spin_unlock_irqrestore(&lp->lock, flags);
1076 return 0;
1079 /* The PCNET32 interrupt handler. */
1080 static void
1081 pcnet32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1083 struct net_device *dev = (struct net_device *)dev_id;
1084 struct pcnet32_private *lp;
1085 unsigned long ioaddr;
1086 u16 csr0,rap;
1087 int boguscnt = max_interrupt_work;
1088 int must_restart;
1090 if (dev == NULL) {
1091 printk (KERN_DEBUG "pcnet32_interrupt(): irq %d for unknown device.\n", irq);
1092 return;
1095 ioaddr = dev->base_addr;
1096 lp = (struct pcnet32_private *)dev->priv;
1098 spin_lock(&lp->lock);
1100 rap = lp->a.read_rap(ioaddr);
1101 while ((csr0 = lp->a.read_csr (ioaddr, 0)) & 0x8600 && --boguscnt >= 0) {
1102 /* Acknowledge all of the current interrupt sources ASAP. */
1103 lp->a.write_csr (ioaddr, 0, csr0 & ~0x004f);
1105 must_restart = 0;
1107 if (pcnet32_debug > 5)
1108 printk(KERN_DEBUG "%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
1109 dev->name, csr0, lp->a.read_csr (ioaddr, 0));
1111 if (csr0 & 0x0400) /* Rx interrupt */
1112 pcnet32_rx(dev);
1114 if (csr0 & 0x0200) { /* Tx-done interrupt */
1115 unsigned int dirty_tx = lp->dirty_tx;
1117 while (dirty_tx < lp->cur_tx) {
1118 int entry = dirty_tx & TX_RING_MOD_MASK;
1119 int status = (short)le16_to_cpu(lp->tx_ring[entry].status);
1121 if (status < 0)
1122 break; /* It still hasn't been Txed */
1124 lp->tx_ring[entry].base = 0;
1126 if (status & 0x4000) {
1127 /* There was an major error, log it. */
1128 int err_status = le32_to_cpu(lp->tx_ring[entry].misc);
1129 lp->stats.tx_errors++;
1130 if (err_status & 0x04000000) lp->stats.tx_aborted_errors++;
1131 if (err_status & 0x08000000) lp->stats.tx_carrier_errors++;
1132 if (err_status & 0x10000000) lp->stats.tx_window_errors++;
1133 #ifndef DO_DXSUFLO
1134 if (err_status & 0x40000000) {
1135 lp->stats.tx_fifo_errors++;
1136 /* Ackk! On FIFO errors the Tx unit is turned off! */
1137 /* Remove this verbosity later! */
1138 printk(KERN_ERR "%s: Tx FIFO error! CSR0=%4.4x\n",
1139 dev->name, csr0);
1140 must_restart = 1;
1142 #else
1143 if (err_status & 0x40000000) {
1144 lp->stats.tx_fifo_errors++;
1145 if (! lp->dxsuflo) { /* If controller doesn't recover ... */
1146 /* Ackk! On FIFO errors the Tx unit is turned off! */
1147 /* Remove this verbosity later! */
1148 printk(KERN_ERR "%s: Tx FIFO error! CSR0=%4.4x\n",
1149 dev->name, csr0);
1150 must_restart = 1;
1153 #endif
1154 } else {
1155 if (status & 0x1800)
1156 lp->stats.collisions++;
1157 lp->stats.tx_packets++;
1160 /* We must free the original skb */
1161 if (lp->tx_skbuff[entry]) {
1162 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[entry], lp->tx_skbuff[entry]->len, PCI_DMA_TODEVICE);
1163 dev_kfree_skb_irq(lp->tx_skbuff[entry]);
1164 lp->tx_skbuff[entry] = 0;
1165 lp->tx_dma_addr[entry] = 0;
1167 dirty_tx++;
1170 #ifndef final_version
1171 if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
1172 printk(KERN_ERR "out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
1173 dirty_tx, lp->cur_tx, lp->tx_full);
1174 dirty_tx += TX_RING_SIZE;
1176 #endif
1177 if (lp->tx_full &&
1178 netif_queue_stopped(dev) &&
1179 dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
1180 /* The ring is no longer full, clear tbusy. */
1181 lp->tx_full = 0;
1182 netif_wake_queue (dev);
1184 lp->dirty_tx = dirty_tx;
1187 /* Log misc errors. */
1188 if (csr0 & 0x4000) lp->stats.tx_errors++; /* Tx babble. */
1189 if (csr0 & 0x1000) {
1191 * this happens when our receive ring is full. This shouldn't
1192 * be a problem as we will see normal rx interrupts for the frames
1193 * in the receive ring. But there are some PCI chipsets (I can reproduce
1194 * this on SP3G with Intel saturn chipset) which have sometimes problems
1195 * and will fill up the receive ring with error descriptors. In this
1196 * situation we don't get a rx interrupt, but a missed frame interrupt sooner
1197 * or later. So we try to clean up our receive ring here.
1199 pcnet32_rx(dev);
1200 lp->stats.rx_errors++; /* Missed a Rx frame. */
1202 if (csr0 & 0x0800) {
1203 printk(KERN_ERR "%s: Bus master arbitration failure, status %4.4x.\n",
1204 dev->name, csr0);
1205 /* unlike for the lance, there is no restart needed */
1208 if (must_restart) {
1209 /* stop the chip to clear the error condition, then restart */
1210 lp->a.write_csr (ioaddr, 0, 0x0004);
1211 pcnet32_restart(dev, 0x0002);
1215 /* Clear any other interrupt, and set interrupt enable. */
1216 lp->a.write_csr (ioaddr, 0, 0x7940);
1217 lp->a.write_rap(ioaddr,rap);
1219 if (pcnet32_debug > 4)
1220 printk(KERN_DEBUG "%s: exiting interrupt, csr0=%#4.4x.\n",
1221 dev->name, lp->a.read_csr (ioaddr, 0));
1223 spin_unlock(&lp->lock);
1226 static int
1227 pcnet32_rx(struct net_device *dev)
1229 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1230 int entry = lp->cur_rx & RX_RING_MOD_MASK;
1232 /* If we own the next entry, it's a new packet. Send it up. */
1233 while ((short)le16_to_cpu(lp->rx_ring[entry].status) >= 0) {
1234 int status = (short)le16_to_cpu(lp->rx_ring[entry].status) >> 8;
1236 if (status != 0x03) { /* There was an error. */
1238 * There is a tricky error noted by John Murphy,
1239 * <murf@perftech.com> to Russ Nelson: Even with full-sized
1240 * buffers it's possible for a jabber packet to use two
1241 * buffers, with only the last correctly noting the error.
1243 if (status & 0x01) /* Only count a general error at the */
1244 lp->stats.rx_errors++; /* end of a packet.*/
1245 if (status & 0x20) lp->stats.rx_frame_errors++;
1246 if (status & 0x10) lp->stats.rx_over_errors++;
1247 if (status & 0x08) lp->stats.rx_crc_errors++;
1248 if (status & 0x04) lp->stats.rx_fifo_errors++;
1249 lp->rx_ring[entry].status &= le16_to_cpu(0x03ff);
1250 } else {
1251 /* Malloc up new buffer, compatible with net-2e. */
1252 short pkt_len = (le32_to_cpu(lp->rx_ring[entry].msg_length) & 0xfff)-4;
1253 struct sk_buff *skb;
1255 if(pkt_len < 60) {
1256 printk(KERN_ERR "%s: Runt packet!\n",dev->name);
1257 lp->stats.rx_errors++;
1258 } else {
1259 int rx_in_place = 0;
1260 dma_addr_t rx_dma_addr = lp->rx_dma_addr[entry];
1262 if (pkt_len > rx_copybreak) {
1263 struct sk_buff *newskb;
1265 if ((newskb = dev_alloc_skb (PKT_BUF_SZ))) {
1266 skb_reserve (newskb, 2);
1267 skb = lp->rx_skbuff[entry];
1268 skb_put (skb, pkt_len);
1269 lp->rx_skbuff[entry] = newskb;
1270 newskb->dev = dev;
1271 lp->rx_dma_addr[entry] = pci_map_single(lp->pci_dev, newskb->tail, newskb->len, PCI_DMA_FROMDEVICE);
1272 lp->rx_ring[entry].base = le32_to_cpu(lp->rx_dma_addr[entry]);
1273 rx_in_place = 1;
1274 } else
1275 skb = NULL;
1276 } else {
1277 skb = dev_alloc_skb(pkt_len+2);
1280 if (skb == NULL) {
1281 int i;
1282 printk(KERN_ERR "%s: Memory squeeze, deferring packet.\n", dev->name);
1283 for (i = 0; i < RX_RING_SIZE; i++)
1284 if ((short)le16_to_cpu(lp->rx_ring[(entry+i) & RX_RING_MOD_MASK].status) < 0)
1285 break;
1287 if (i > RX_RING_SIZE -2) {
1288 lp->stats.rx_dropped++;
1289 lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
1290 lp->cur_rx++;
1292 break;
1294 skb->dev = dev;
1295 if (!rx_in_place) {
1296 skb_reserve(skb,2); /* 16 byte align */
1297 skb_put(skb,pkt_len); /* Make room */
1298 eth_copy_and_sum(skb,
1299 (unsigned char *)(lp->rx_skbuff[entry]->tail),
1300 pkt_len,0);
1302 lp->stats.rx_bytes += skb->len;
1303 skb->protocol=eth_type_trans(skb,dev);
1304 netif_rx(skb);
1305 lp->stats.rx_packets++;
1309 * The docs say that the buffer length isn't touched, but Andrew Boyd
1310 * of QNX reports that some revs of the 79C965 clear it.
1312 lp->rx_ring[entry].buf_length = le16_to_cpu(-PKT_BUF_SZ);
1313 lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
1314 entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1317 return 0;
1320 static int
1321 pcnet32_close(struct net_device *dev)
1323 unsigned long ioaddr = dev->base_addr;
1324 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1325 int i;
1327 netif_stop_queue(dev);
1329 lp->stats.rx_missed_errors = lp->a.read_csr (ioaddr, 112);
1331 if (pcnet32_debug > 1)
1332 printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
1333 dev->name, lp->a.read_csr (ioaddr, 0));
1335 /* We stop the PCNET32 here -- it occasionally polls memory if we don't. */
1336 lp->a.write_csr (ioaddr, 0, 0x0004);
1339 * Switch back to 16bit mode to avoid problems with dumb
1340 * DOS packet driver after a warm reboot
1342 lp->a.write_bcr (ioaddr, 20, 4);
1344 free_irq(dev->irq, dev);
1346 /* free all allocated skbuffs */
1347 for (i = 0; i < RX_RING_SIZE; i++) {
1348 lp->rx_ring[i].status = 0;
1349 if (lp->rx_skbuff[i]) {
1350 pci_unmap_single(lp->pci_dev, lp->rx_dma_addr[i], lp->rx_skbuff[i]->len, PCI_DMA_FROMDEVICE);
1351 dev_kfree_skb(lp->rx_skbuff[i]);
1353 lp->rx_skbuff[i] = NULL;
1354 lp->rx_dma_addr[i] = 0;
1357 for (i = 0; i < TX_RING_SIZE; i++) {
1358 if (lp->tx_skbuff[i]) {
1359 pci_unmap_single(lp->pci_dev, lp->tx_dma_addr[i], lp->tx_skbuff[i]->len, PCI_DMA_TODEVICE);
1360 dev_kfree_skb(lp->tx_skbuff[i]);
1362 lp->tx_skbuff[i] = NULL;
1363 lp->tx_dma_addr[i] = 0;
1366 MOD_DEC_USE_COUNT;
1368 return 0;
1371 static struct net_device_stats *
1372 pcnet32_get_stats(struct net_device *dev)
1374 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1375 unsigned long ioaddr = dev->base_addr;
1376 u16 saved_addr;
1377 unsigned long flags;
1379 spin_lock_irqsave(&lp->lock, flags);
1380 saved_addr = lp->a.read_rap(ioaddr);
1381 lp->stats.rx_missed_errors = lp->a.read_csr (ioaddr, 112);
1382 lp->a.write_rap(ioaddr, saved_addr);
1383 spin_unlock_irqrestore(&lp->lock, flags);
1385 return &lp->stats;
1388 /* taken from the sunlance driver, which it took from the depca driver */
1389 static void pcnet32_load_multicast (struct net_device *dev)
1391 struct pcnet32_private *lp = (struct pcnet32_private *) dev->priv;
1392 volatile struct pcnet32_init_block *ib = &lp->init_block;
1393 volatile u16 *mcast_table = (u16 *)&ib->filter;
1394 struct dev_mc_list *dmi=dev->mc_list;
1395 char *addrs;
1396 int i, j, bit, byte;
1397 u32 crc, poly = CRC_POLYNOMIAL_LE;
1399 /* set all multicast bits */
1400 if (dev->flags & IFF_ALLMULTI){
1401 ib->filter [0] = 0xffffffff;
1402 ib->filter [1] = 0xffffffff;
1403 return;
1405 /* clear the multicast filter */
1406 ib->filter [0] = 0;
1407 ib->filter [1] = 0;
1409 /* Add addresses */
1410 for (i = 0; i < dev->mc_count; i++){
1411 addrs = dmi->dmi_addr;
1412 dmi = dmi->next;
1414 /* multicast address? */
1415 if (!(*addrs & 1))
1416 continue;
1418 crc = 0xffffffff;
1419 for (byte = 0; byte < 6; byte++)
1420 for (bit = *addrs++, j = 0; j < 8; j++, bit >>= 1) {
1421 int test;
1423 test = ((bit ^ crc) & 0x01);
1424 crc >>= 1;
1426 if (test) {
1427 crc = crc ^ poly;
1431 crc = crc >> 26;
1432 mcast_table [crc >> 4] |= 1 << (crc & 0xf);
1434 return;
1439 * Set or clear the multicast filter for this adaptor.
1441 static void pcnet32_set_multicast_list(struct net_device *dev)
1443 unsigned long ioaddr = dev->base_addr;
1444 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1446 if (dev->flags&IFF_PROMISC) {
1447 /* Log any net taps. */
1448 printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
1449 lp->init_block.mode = le16_to_cpu(0x8000 | (lp->options & PORT_PORTSEL) << 7);
1450 } else {
1451 lp->init_block.mode = le16_to_cpu((lp->options & PORT_PORTSEL) << 7);
1452 pcnet32_load_multicast (dev);
1455 lp->a.write_csr (ioaddr, 0, 0x0004); /* Temporarily stop the lance. */
1457 pcnet32_restart(dev, 0x0042); /* Resume normal operation */
1460 #ifdef HAVE_PRIVATE_IOCTL
1461 static int pcnet32_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1463 unsigned long ioaddr = dev->base_addr;
1464 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1465 u16 *data = (u16 *)&rq->ifr_data;
1466 int phyaddr = lp->a.read_bcr (ioaddr, 33);
1468 if (lp->mii) {
1469 switch(cmd) {
1470 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
1471 data[0] = (phyaddr >> 5) & 0x1f;
1472 /* Fall Through */
1473 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
1474 lp->a.write_bcr (ioaddr, 33, ((data[0] & 0x1f) << 5) | (data[1] & 0x1f));
1475 data[3] = lp->a.read_bcr (ioaddr, 34);
1476 lp->a.write_bcr (ioaddr, 33, phyaddr);
1477 return 0;
1478 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
1479 if (!capable(CAP_NET_ADMIN))
1480 return -EPERM;
1481 lp->a.write_bcr (ioaddr, 33, ((data[0] & 0x1f) << 5) | (data[1] & 0x1f));
1482 lp->a.write_bcr (ioaddr, 34, data[2]);
1483 lp->a.write_bcr (ioaddr, 33, phyaddr);
1484 return 0;
1485 default:
1486 return -EOPNOTSUPP;
1489 return -EOPNOTSUPP;
1491 #endif /* HAVE_PRIVATE_IOCTL */
1493 static struct pci_driver pcnet32_driver = {
1494 name: "pcnet32",
1495 probe: pcnet32_probe_pci,
1496 remove: NULL,
1497 id_table: pcnet32_pci_tbl,
1500 MODULE_PARM(debug, "i");
1501 MODULE_PARM(max_interrupt_work, "i");
1502 MODULE_PARM(rx_copybreak, "i");
1503 MODULE_PARM(tx_start_pt, "i");
1504 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
1505 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
1506 MODULE_AUTHOR("Thomas Bogendoerfer");
1507 MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
1509 /* An additional parameter that may be passed in... */
1510 static int debug = -1;
1511 static int tx_start_pt = -1;
1513 static int __init pcnet32_init_module(void)
1515 int cards_found = 0;
1516 int err;
1518 if (debug > 0)
1519 pcnet32_debug = debug;
1520 if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
1521 tx_start = tx_start_pt;
1523 pcnet32_dev = NULL;
1524 /* find the PCI devices */
1525 #define USE_PCI_REGISTER_DRIVER
1526 #ifdef USE_PCI_REGISTER_DRIVER
1527 if (err = pci_module_init(&pcnet32_driver) < 0 )
1528 return err;
1529 #else
1531 struct pci_device_id *devid = pcnet32_pci_tbl;
1532 for (devid = pcnet32_pci_tbl; devid != NULL && devid->vendor != 0; devid++) {
1533 struct pci_dev *pdev = pci_find_subsys(devid->vendor, devid->device, devid->subvendor, devid->subdevice, NULL);
1534 if (pdev != NULL) {
1535 if (pcnet32_probe_pci(pdev, devid) >= 0) {
1536 cards_found++;
1541 #endif
1542 return 0;
1543 /* find any remaining VLbus devices */
1544 return pcnet32_probe_vlbus(cards_found);
1547 static void __exit pcnet32_cleanup_module(void)
1549 struct net_device *next_dev;
1551 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1552 while (pcnet32_dev) {
1553 struct pcnet32_private *lp = (struct pcnet32_private *) pcnet32_dev->priv;
1554 next_dev = lp->next;
1555 unregister_netdev(pcnet32_dev);
1556 release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
1557 pci_free_consistent(lp->pci_dev, sizeof(*lp), lp, lp->dma_addr);
1558 kfree(pcnet32_dev);
1559 pcnet32_dev = next_dev;
1563 module_init(pcnet32_init_module);
1564 module_exit(pcnet32_cleanup_module);
1567 * Local variables:
1568 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c pcnet32.c"
1569 * c-indent-level: 4
1570 * tab-width: 8
1571 * End: