Merge with 2.3.99-pre1.
[linux-2.6/linux-mips.git] / drivers / net / pcnet32.c
blob167d3cb1244538e939af43da427605388dd5a6ec
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/config.h>
19 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/string.h>
24 #include <linux/ptrace.h>
25 #include <linux/errno.h>
26 #include <linux/ioport.h>
27 #include <linux/malloc.h>
28 #include <linux/interrupt.h>
29 #include <linux/pci.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <asm/bitops.h>
33 #ifdef __mips__
34 #include <asm/bootinfo.h>
35 #include <asm/pgtable.h>
36 #include <asm/sni.h>
37 #endif /* __mips__ */
38 #include <asm/io.h>
39 #include <asm/dma.h>
41 #include <linux/netdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/spinlock.h>
46 static unsigned int pcnet32_portlist[] __initdata = {0x300, 0x320, 0x340, 0x360, 0};
48 static int pcnet32_debug = 1;
49 static int tx_start = 1; /* Mapping -- 0:20, 1:64, 2:128, 3:~220 (depends on chip vers) */
51 static struct net_device *pcnet32_dev = NULL;
53 static const int max_interrupt_work = 80;
54 static const int rx_copybreak = 200;
56 #define PORT_AUI 0x00
57 #define PORT_10BT 0x01
58 #define PORT_GPSI 0x02
59 #define PORT_MII 0x03
61 #define PORT_PORTSEL 0x03
62 #define PORT_ASEL 0x04
63 #define PORT_100 0x40
64 #define PORT_FD 0x80
68 * table to translate option values from tulip
69 * to internal options
71 static unsigned char options_mapping[] = {
72 PORT_ASEL, /* 0 Auto-select */
73 PORT_AUI, /* 1 BNC/AUI */
74 PORT_AUI, /* 2 AUI/BNC */
75 PORT_ASEL, /* 3 not supported */
76 PORT_10BT | PORT_FD, /* 4 10baseT-FD */
77 PORT_ASEL, /* 5 not supported */
78 PORT_ASEL, /* 6 not supported */
79 PORT_ASEL, /* 7 not supported */
80 PORT_ASEL, /* 8 not supported */
81 PORT_MII, /* 9 MII 10baseT */
82 PORT_MII | PORT_FD, /* 10 MII 10baseT-FD */
83 PORT_MII, /* 11 MII (autosel) */
84 PORT_10BT, /* 12 10BaseT */
85 PORT_MII | PORT_100, /* 13 MII 100BaseTx */
86 PORT_MII | PORT_100 | PORT_FD, /* 14 MII 100BaseTx-FD */
87 PORT_ASEL /* 15 not supported */
90 #define MAX_UNITS 8
91 static int options[MAX_UNITS] = {0, };
92 static int full_duplex[MAX_UNITS] = {0, };
95 * Theory of Operation
97 * This driver uses the same software structure as the normal lance
98 * driver. So look for a verbose description in lance.c. The differences
99 * to the normal lance driver is the use of the 32bit mode of PCnet32
100 * and PCnetPCI chips. Because these chips are 32bit chips, there is no
101 * 16MB limitation and we don't need bounce buffers.
105 * History:
106 * v0.01: Initial version
107 * only tested on Alpha Noname Board
108 * v0.02: changed IRQ handling for new interrupt scheme (dev_id)
109 * tested on a ASUS SP3G
110 * v0.10: fixed an odd problem with the 79C974 in a Compaq Deskpro XL
111 * looks like the 974 doesn't like stopping and restarting in a
112 * short period of time; now we do a reinit of the lance; the
113 * bug was triggered by doing ifconfig eth0 <ip> broadcast <addr>
114 * and hangs the machine (thanks to Klaus Liedl for debugging)
115 * v0.12: by suggestion from Donald Becker: Renamed driver to pcnet32,
116 * made it standalone (no need for lance.c)
117 * v0.13: added additional PCI detecting for special PCI devices (Compaq)
118 * v0.14: stripped down additional PCI probe (thanks to David C Niemi
119 * and sveneric@xs4all.nl for testing this on their Compaq boxes)
120 * v0.15: added 79C965 (VLB) probe
121 * added interrupt sharing for PCI chips
122 * v0.16: fixed set_multicast_list on Alpha machines
123 * v0.17: removed hack from dev.c; now pcnet32 uses ethif_probe in Space.c
124 * v0.19: changed setting of autoselect bit
125 * v0.20: removed additional Compaq PCI probe; there is now a working one
126 * in arch/i386/bios32.c
127 * v0.21: added endian conversion for ppc, from work by cort@cs.nmt.edu
128 * v0.22: added printing of status to ring dump
129 * v0.23: changed enet_statistics to net_devive_stats
130 * v0.90: added multicast filter
131 * added module support
132 * changed irq probe to new style
133 * added PCnetFast chip id
134 * added fix for receive stalls with Intel saturn chipsets
135 * added in-place rx skbs like in the tulip driver
136 * minor cleanups
137 * v0.91: added PCnetFast+ chip id
138 * back port to 2.0.x
139 * v1.00: added some stuff from Donald Becker's 2.0.34 version
140 * added support for byte counters in net_dev_stats
141 * v1.01: do ring dumps, only when debugging the driver
142 * increased the transmit timeout
143 * v1.02: fixed memory leak in pcnet32_init_ring()
144 * v1.10: workaround for stopped transmitter
145 * added port selection for modules
146 * detect special T1/E1 WAN card and setup port selection
147 * v1.11: fixed wrong checking of Tx errors
148 * v1.20: added check of return value kmalloc (cpeterso@cs.washington.edu)
149 * added save original kmalloc addr for freeing (mcr@solidum.com)
150 * added support for PCnetHome chip (joe@MIT.EDU)
151 * rewritten PCI card detection
152 * added dwio mode to get driver working on some PPC machines
153 * v1.21: added mii selection and mii ioctl
154 * v1.22: changed pci scanning code to make PPC people happy
155 * fixed switching to 32bit mode in pcnet32_open() (thanks
156 * to Michael Richard <mcr@solidum.com> for noticing this one)
157 * added sub vendor/device id matching (thanks again to
158 * Michael Richard <mcr@solidum.com>)
159 * added chip id for 79c973/975 (thanks to Zach Brown <zab@zabbo.net>)
160 * v1.23 fixed small bug, when manual selecting MII speed/duplex
161 * v1.24 Applied Thomas' patch to use TxStartPoint and thus decrease TxFIFO
162 * underflows. Added tx_start_pt module parameter. Increased
163 * TX_RING_SIZE from 16 to 32. Added #ifdef'd code to use DXSUFLO
164 * for FAST[+] chipsets. <kaf@fc.hp.com>
165 * v1.24ac Added SMP spinlocking - Alan Cox <alan@redhat.com>
166 * v1.25kf Added No Interrupt on successful Tx for some Tx's <kaf@fc.hp.com>
171 * Set the number of Tx and Rx buffers, using Log_2(# buffers).
172 * Reasonable default values are 4 Tx buffers, and 16 Rx buffers.
173 * That translates to 2 (4 == 2^^2) and 4 (16 == 2^^4).
175 #ifndef PCNET32_LOG_TX_BUFFERS
176 #define PCNET32_LOG_TX_BUFFERS 4
177 #define PCNET32_LOG_RX_BUFFERS 5
178 #endif
180 #define TX_RING_SIZE (1 << (PCNET32_LOG_TX_BUFFERS))
181 #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
182 #define TX_RING_LEN_BITS ((PCNET32_LOG_TX_BUFFERS) << 12)
184 #define RX_RING_SIZE (1 << (PCNET32_LOG_RX_BUFFERS))
185 #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
186 #define RX_RING_LEN_BITS ((PCNET32_LOG_RX_BUFFERS) << 4)
188 #define PKT_BUF_SZ 1544
190 /* Offsets from base I/O address. */
191 #define PCNET32_WIO_RDP 0x10
192 #define PCNET32_WIO_RAP 0x12
193 #define PCNET32_WIO_RESET 0x14
194 #define PCNET32_WIO_BDP 0x16
196 #define PCNET32_DWIO_RDP 0x10
197 #define PCNET32_DWIO_RAP 0x14
198 #define PCNET32_DWIO_RESET 0x18
199 #define PCNET32_DWIO_BDP 0x1C
201 #define PCNET32_TOTAL_SIZE 0x20
203 /* some PCI ids */
204 #ifndef PCI_DEVICE_ID_AMD_LANCE
205 #define PCI_VENDOR_ID_AMD 0x1022
206 #define PCI_DEVICE_ID_AMD_LANCE 0x2000
207 #endif
208 #ifndef PCI_DEVICE_ID_AMD_PCNETHOME
209 #define PCI_DEVICE_ID_AMD_PCNETHOME 0x2001
210 #endif
213 #define CRC_POLYNOMIAL_LE 0xedb88320UL /* Ethernet CRC, little endian */
215 /* The PCNET32 Rx and Tx ring descriptors. */
216 struct pcnet32_rx_head {
217 u32 base;
218 s16 buf_length;
219 s16 status;
220 u32 msg_length;
221 u32 reserved;
224 struct pcnet32_tx_head {
225 u32 base;
226 s16 length;
227 s16 status;
228 u32 misc;
229 u32 reserved;
232 /* The PCNET32 32-Bit initialization block, described in databook. */
233 struct pcnet32_init_block {
234 u16 mode;
235 u16 tlen_rlen;
236 u8 phys_addr[6];
237 u16 reserved;
238 u32 filter[2];
239 /* Receive and transmit ring base, along with extra bits. */
240 u32 rx_ring;
241 u32 tx_ring;
244 /* PCnet32 access functions */
245 struct pcnet32_access {
246 u16 (*read_csr)(unsigned long, int);
247 void (*write_csr)(unsigned long, int, u16);
248 u16 (*read_bcr)(unsigned long, int);
249 void (*write_bcr)(unsigned long, int, u16);
250 u16 (*read_rap)(unsigned long);
251 void (*write_rap)(unsigned long, u16);
252 void (*reset)(unsigned long);
255 struct pcnet32_private {
256 /* The Tx and Rx ring entries must be aligned on 16-byte boundaries in 32bit mode. */
257 struct pcnet32_rx_head rx_ring[RX_RING_SIZE];
258 struct pcnet32_tx_head tx_ring[TX_RING_SIZE];
259 struct pcnet32_init_block init_block;
260 const char *name;
261 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
262 struct sk_buff *tx_skbuff[TX_RING_SIZE];
263 struct sk_buff *rx_skbuff[RX_RING_SIZE];
264 struct pcnet32_access a;
265 void *origmem;
266 spinlock_t lock; /* Guard lock */
267 unsigned int cur_rx, cur_tx; /* The next free ring entry */
268 unsigned int dirty_rx, dirty_tx; /* The ring entries to be free()ed. */
269 struct net_device_stats stats;
270 char tx_full;
271 int options;
272 int shared_irq:1, /* shared irq possible */
273 ltint:1,
274 #ifdef DO_DXSUFLO
275 dxsuflo:1, /* disable transmit stop on uflo */
276 #endif
277 full_duplex:1, /* full duplex possible */
278 mii:1; /* mii port available */
279 struct net_device *next;
282 static int pcnet32_probe(void);
283 static int pcnet32_probe1(unsigned long, unsigned char, int, int);
284 static int pcnet32_open(struct net_device *);
285 static int pcnet32_init_ring(struct net_device *);
286 static int pcnet32_start_xmit(struct sk_buff *, struct net_device *);
287 static int pcnet32_rx(struct net_device *);
288 static void pcnet32_tx_timeout (struct net_device *dev);
289 static void pcnet32_interrupt(int, void *, struct pt_regs *);
290 static int pcnet32_close(struct net_device *);
291 static struct net_device_stats *pcnet32_get_stats(struct net_device *);
292 static void pcnet32_set_multicast_list(struct net_device *);
293 #ifdef HAVE_PRIVATE_IOCTL
294 static int pcnet32_mii_ioctl(struct net_device *, struct ifreq *, int);
295 #endif
297 enum pci_flags_bit {
298 PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
299 PCI_ADDR0=0x10<<0, PCI_ADDR1=0x10<<1, PCI_ADDR2=0x10<<2, PCI_ADDR3=0x10<<3,
302 struct pcnet32_pci_id_info {
303 const char *name;
304 u16 vendor_id, device_id, svid, sdid, flags;
305 int io_size;
306 int (*probe1) (unsigned long, unsigned char, int, int);
309 static struct pcnet32_pci_id_info pcnet32_tbl[] = {
310 { "AMD PCnetPCI series",
311 PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, 0, 0,
312 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
313 pcnet32_probe1},
314 { "AMD PCnetPCI series (IBM)",
315 PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_LANCE, 0x1014, 0x2000,
316 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
317 pcnet32_probe1},
318 { "AMD PCnetHome series",
319 PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_PCNETHOME, 0, 0,
320 PCI_USES_IO|PCI_USES_MASTER, PCNET32_TOTAL_SIZE,
321 pcnet32_probe1},
322 {0,}
325 static u16 pcnet32_wio_read_csr (unsigned long addr, int index)
327 outw (index, addr+PCNET32_WIO_RAP);
328 return inw (addr+PCNET32_WIO_RDP);
331 static void pcnet32_wio_write_csr (unsigned long addr, int index, u16 val)
333 outw (index, addr+PCNET32_WIO_RAP);
334 outw (val, addr+PCNET32_WIO_RDP);
337 static u16 pcnet32_wio_read_bcr (unsigned long addr, int index)
339 outw (index, addr+PCNET32_WIO_RAP);
340 return inw (addr+PCNET32_WIO_BDP);
343 static void pcnet32_wio_write_bcr (unsigned long addr, int index, u16 val)
345 outw (index, addr+PCNET32_WIO_RAP);
346 outw (val, addr+PCNET32_WIO_BDP);
349 static u16 pcnet32_wio_read_rap (unsigned long addr)
351 return inw (addr+PCNET32_WIO_RAP);
354 static void pcnet32_wio_write_rap (unsigned long addr, u16 val)
356 outw (val, addr+PCNET32_WIO_RAP);
359 static void pcnet32_wio_reset (unsigned long addr)
361 inw (addr+PCNET32_WIO_RESET);
364 static int pcnet32_wio_check (unsigned long addr)
366 outw (88, addr+PCNET32_WIO_RAP);
367 return (inw (addr+PCNET32_WIO_RAP) == 88);
370 static struct pcnet32_access pcnet32_wio = {
371 pcnet32_wio_read_csr,
372 pcnet32_wio_write_csr,
373 pcnet32_wio_read_bcr,
374 pcnet32_wio_write_bcr,
375 pcnet32_wio_read_rap,
376 pcnet32_wio_write_rap,
377 pcnet32_wio_reset
380 static u16 pcnet32_dwio_read_csr (unsigned long addr, int index)
382 outl (index, addr+PCNET32_DWIO_RAP);
383 return (inl (addr+PCNET32_DWIO_RDP) & 0xffff);
386 static void pcnet32_dwio_write_csr (unsigned long addr, int index, u16 val)
388 outl (index, addr+PCNET32_DWIO_RAP);
389 outl (val, addr+PCNET32_DWIO_RDP);
392 static u16 pcnet32_dwio_read_bcr (unsigned long addr, int index)
394 outl (index, addr+PCNET32_DWIO_RAP);
395 return (inl (addr+PCNET32_DWIO_BDP) & 0xffff);
398 static void pcnet32_dwio_write_bcr (unsigned long addr, int index, u16 val)
400 outl (index, addr+PCNET32_DWIO_RAP);
401 outl (val, addr+PCNET32_DWIO_BDP);
404 static u16 pcnet32_dwio_read_rap (unsigned long addr)
406 return (inl (addr+PCNET32_DWIO_RAP) & 0xffff);
409 static void pcnet32_dwio_write_rap (unsigned long addr, u16 val)
411 outl (val, addr+PCNET32_DWIO_RAP);
414 static void pcnet32_dwio_reset (unsigned long addr)
416 inl (addr+PCNET32_DWIO_RESET);
419 static int pcnet32_dwio_check (unsigned long addr)
421 outl (88, addr+PCNET32_DWIO_RAP);
422 return (inl (addr+PCNET32_DWIO_RAP) == 88);
425 static struct pcnet32_access pcnet32_dwio = {
426 pcnet32_dwio_read_csr,
427 pcnet32_dwio_write_csr,
428 pcnet32_dwio_read_bcr,
429 pcnet32_dwio_write_bcr,
430 pcnet32_dwio_read_rap,
431 pcnet32_dwio_write_rap,
432 pcnet32_dwio_reset
438 static int __init pcnet32_probe(void)
440 unsigned long ioaddr = 0; // FIXME dev ? dev->base_addr: 0;
441 unsigned int irq_line = 0; // FIXME dev ? dev->irq : 0;
442 int *port;
443 int cards_found = 0;
446 #ifndef __powerpc__
447 if (ioaddr > 0x1ff) {
448 if (check_region(ioaddr, PCNET32_TOTAL_SIZE) == 0)
449 return pcnet32_probe1(ioaddr, irq_line, 0, 0);
450 else
451 return ENODEV;
452 } else
453 #endif
454 if(ioaddr != 0)
455 return ENXIO;
457 #if defined(CONFIG_PCI)
458 if (pci_present()) {
459 struct pci_dev *pdev = NULL;
461 printk("pcnet32.c: PCI bios is present, checking for devices...\n");
462 while ((pdev = pci_find_class (PCI_CLASS_NETWORK_ETHERNET<<8, pdev))) {
463 u16 pci_command;
464 int chip_idx;
465 u16 sdid,svid;
467 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &svid);
468 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &sdid);
469 for (chip_idx = 0; pcnet32_tbl[chip_idx].vendor_id; chip_idx++)
470 if ((pdev->vendor == pcnet32_tbl[chip_idx].vendor_id) &&
471 (pdev->device == pcnet32_tbl[chip_idx].device_id) &&
472 (pcnet32_tbl[chip_idx].svid == 0 ||
473 (svid == pcnet32_tbl[chip_idx].svid)) &&
474 (pcnet32_tbl[chip_idx].sdid == 0 ||
475 (sdid == pcnet32_tbl[chip_idx].sdid)))
476 break;
477 if (pcnet32_tbl[chip_idx].vendor_id == 0)
478 continue;
480 ioaddr = pdev->resource[0].start;
481 irq_line = pdev->irq;
483 /* Avoid already found cards from previous pcnet32_probe() calls */
484 if ((pcnet32_tbl[chip_idx].flags & PCI_USES_IO) &&
485 check_region(ioaddr, pcnet32_tbl[chip_idx].io_size))
486 continue;
488 /* PCI Spec 2.1 states that it is either the driver or PCI card's
489 * responsibility to set the PCI Master Enable Bit if needed.
490 * (From Mark Stockton <marks@schooner.sys.hou.compaq.com>)
492 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
493 if ( ! (pci_command & PCI_COMMAND_MASTER)) {
494 printk("PCI Master Bit has not been set. Setting...\n");
495 pci_command |= PCI_COMMAND_MASTER|PCI_COMMAND_IO;
496 pci_write_config_word(pdev, PCI_COMMAND, pci_command);
498 #ifdef CONFIG_SNI_RM200_PCI
499 if (mips_machgroup == MACH_GROUP_SNI_RM
500 && mips_machtype == MACH_SNI_RM200_PCI)
501 irq_line = PCIMT_IRQ_ETHERNET;
502 #endif
503 printk("Found PCnet/PCI at %#lx, irq %d.\n", ioaddr, irq_line);
505 if (pcnet32_tbl[chip_idx].probe1(ioaddr, irq_line, 1, cards_found) == 0) {
506 cards_found++;
509 } else
510 #endif /* defined(CONFIG_PCI) */
512 /* now look for PCnet32 VLB cards */
513 for (port = pcnet32_portlist; *port; port++) {
514 unsigned long ioaddr = *port;
516 if ( check_region(ioaddr, PCNET32_TOTAL_SIZE) == 0) {
517 /* check if there is really a pcnet chip on that ioaddr */
518 if ((inb(ioaddr + 14) == 0x57) &&
519 (inb(ioaddr + 15) == 0x57) &&
520 (pcnet32_probe1(ioaddr, 0, 0, 0) == 0))
521 cards_found++;
524 return cards_found ? 0: ENODEV;
528 /* pcnet32_probe1 */
529 static int __init
530 pcnet32_probe1(unsigned long ioaddr, unsigned char irq_line, int shared, int card_idx)
532 struct pcnet32_private *lp;
533 int i,media,fdx = 0, mii = 0, fset = 0;
534 #ifdef DO_DXSUFLO
535 int dxsuflo = 0;
536 #endif
537 int ltint = 0;
538 int chip_version;
539 char *chipname;
540 char *priv;
541 struct net_device *dev;
542 struct pcnet32_access *a;
544 /* reset the chip */
545 pcnet32_dwio_reset(ioaddr);
546 pcnet32_wio_reset(ioaddr);
548 if (pcnet32_wio_read_csr (ioaddr, 0) == 4 && pcnet32_wio_check (ioaddr)) {
549 a = &pcnet32_wio;
550 } else {
551 if (pcnet32_dwio_read_csr (ioaddr, 0) == 4 && pcnet32_dwio_check(ioaddr)) {
552 a = &pcnet32_dwio;
553 } else
554 return ENODEV;
557 chip_version = a->read_csr (ioaddr, 88) | (a->read_csr (ioaddr,89) << 16);
558 if (pcnet32_debug > 2)
559 printk(" PCnet chip version is %#x.\n", chip_version);
560 if ((chip_version & 0xfff) != 0x003)
561 return ENODEV;
562 chip_version = (chip_version >> 12) & 0xffff;
563 switch (chip_version) {
564 case 0x2420:
565 chipname = "PCnet/PCI 79C970";
566 break;
567 case 0x2430:
568 if (shared)
569 chipname = "PCnet/PCI 79C970"; /* 970 gives the wrong chip id back */
570 else
571 chipname = "PCnet/32 79C965";
572 break;
573 case 0x2621:
574 chipname = "PCnet/PCI II 79C970A";
575 fdx = 1;
576 break;
577 case 0x2623:
578 chipname = "PCnet/FAST 79C971";
579 fdx = 1; mii = 1; fset = 1;
580 ltint = 1;
581 break;
582 case 0x2624:
583 chipname = "PCnet/FAST+ 79C972";
584 fdx = 1; mii = 1; fset = 1;
585 break;
586 case 0x2625:
587 chipname = "PCnet/FAST III 79C973";
588 fdx = 1; mii = 1;
589 break;
590 case 0x2626:
591 chipname = "PCnet/Home 79C978";
592 fdx = 1;
594 * This is based on specs published at www.amd.com. This section
595 * assumes that a card with a 79C978 wants to go into 1Mb HomePNA
596 * mode. The 79C978 can also go into standard ethernet, and there
597 * probably should be some sort of module option to select the
598 * mode by which the card should operate
600 /* switch to home wiring mode */
601 media = a->read_bcr (ioaddr, 49);
602 if (pcnet32_debug > 2)
603 printk("pcnet32: pcnet32 media value %#x.\n", media);
604 media &= ~3;
605 media |= 1;
606 if (pcnet32_debug > 2)
607 printk("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";
612 fdx = 1; mii = 1;
613 break;
614 default:
615 printk("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(" %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("\n tx_start_pt(0x%04x):",i);
650 switch(i>>10) {
651 case 0: printk(" 20 bytes,"); break;
652 case 1: printk(" 64 bytes,"); break;
653 case 2: printk(" 128 bytes,"); break;
654 case 3: printk("~220 bytes,"); break;
656 i = a->read_bcr(ioaddr, 18); /* Check Burst/Bus control */
657 printk(" BCR18(%x):",i&0xffff);
658 if (i & (1<<5)) printk("BurstWrEn ");
659 if (i & (1<<6)) printk("BurstRdEn ");
660 if (i & (1<<7)) printk("DWordIO ");
661 if (i & (1<<11)) printk("NoUFlow ");
662 i = a->read_bcr(ioaddr, 25);
663 printk("\n SRAMSIZE=0x%04x,",i<<8);
664 i = a->read_bcr(ioaddr, 26);
665 printk(" SRAM_BND=0x%04x,",i<<8);
666 i = a->read_bcr(ioaddr, 27);
667 if (i & (1<<14)) printk("LowLatRx,");
670 dev->base_addr = ioaddr;
671 request_region(ioaddr, PCNET32_TOTAL_SIZE, chipname);
673 if ((priv = kmalloc(sizeof(*lp)+15,GFP_KERNEL)) == NULL)
674 return ENOMEM;
677 * Make certain the data structures used by
678 * the PCnet32 are 16byte aligned
680 lp = (struct pcnet32_private *)(((unsigned long)priv+15) & ~15);
681 dma_cache_inv(lp, sizeof(*lp)+15);
682 #ifdef __mips__
683 /* XXX Maybe modify kmalloc() to return KSEG1 memory? This would
684 * make lots of modifications to drivers unnecessary but possibly
685 * have negative impact on the performance due to drivers not being
686 * aware of the CPU performance impact of GFP_DMA memory. It also
687 * adds a bit of extra overhead to kmalloc().
689 lp = KSEG1ADDR(lp);
690 #endif
692 memset(lp, 0, sizeof(*lp));
694 spin_lock_init(&lp->lock);
696 dev->priv = lp;
697 lp->name = chipname;
698 lp->shared_irq = shared;
699 lp->full_duplex = fdx;
700 #ifdef DO_DXSUFLO
701 lp->dxsuflo = dxsuflo;
702 #endif
703 lp->ltint = ltint;
704 lp->mii = mii;
705 if (options[card_idx] > sizeof (options_mapping))
706 lp->options = PORT_ASEL;
707 else
708 lp->options = options_mapping[options[card_idx]];
710 if (fdx && !(lp->options & PORT_ASEL) && full_duplex[card_idx])
711 lp->options |= PORT_FD;
713 lp->origmem = priv;
714 lp->a = *a;
716 /* detect special T1/E1 WAN card by checking for MAC address */
717 if (dev->dev_addr[0] == 0x00 && dev->dev_addr[1] == 0xe0 && dev->dev_addr[2] == 0x75)
718 lp->options = PORT_FD | PORT_GPSI;
720 lp->init_block.mode = le16_to_cpu(0x0003); /* Disable Rx and Tx. */
721 lp->init_block.tlen_rlen = le16_to_cpu(TX_RING_LEN_BITS | RX_RING_LEN_BITS);
722 for (i = 0; i < 6; i++)
723 lp->init_block.phys_addr[i] = dev->dev_addr[i];
724 lp->init_block.filter[0] = 0x00000000;
725 lp->init_block.filter[1] = 0x00000000;
726 lp->init_block.rx_ring = (u32)le32_to_cpu(virt_to_bus(lp->rx_ring));
727 lp->init_block.tx_ring = (u32)le32_to_cpu(virt_to_bus(lp->tx_ring));
729 /* switch pcnet32 to 32bit mode */
730 a->write_bcr (ioaddr, 20, 2);
732 a->write_csr (ioaddr, 1, virt_to_bus(&lp->init_block) & 0xffff);
733 a->write_csr (ioaddr, 2, virt_to_bus(&lp->init_block) >> 16);
735 if (irq_line) {
736 dev->irq = irq_line;
739 if (dev->irq >= 2)
740 printk(" assigned IRQ %d.\n", dev->irq);
741 else {
742 unsigned long irq_mask = probe_irq_on();
745 * To auto-IRQ we enable the initialization-done and DMA error
746 * interrupts. For ISA boards we get a DMA error, but VLB and PCI
747 * boards will work.
749 /* Trigger an initialization just for the interrupt. */
750 a->write_csr (ioaddr, 0, 0x41);
751 mdelay (1);
753 dev->irq = probe_irq_off (irq_mask);
754 if (dev->irq)
755 printk(", probed IRQ %d.\n", dev->irq);
756 else {
757 printk(", failed to detect IRQ line.\n");
758 return ENODEV;
762 if (pcnet32_debug > 0)
763 printk(version);
765 /* The PCNET32-specific entries in the device structure. */
766 dev->open = &pcnet32_open;
767 dev->hard_start_xmit = &pcnet32_start_xmit;
768 dev->stop = &pcnet32_close;
769 dev->get_stats = &pcnet32_get_stats;
770 dev->set_multicast_list = &pcnet32_set_multicast_list;
771 #ifdef HAVE_PRIVATE_IOCTL
772 dev->do_ioctl = &pcnet32_mii_ioctl;
773 #endif
774 dev->tx_timeout = pcnet32_tx_timeout;
775 dev->watchdog_timeo = (HZ >> 1);
777 lp->next = pcnet32_dev;
778 pcnet32_dev = dev;
780 /* Fill in the generic fields of the device structure. */
781 ether_setup(dev);
782 return 0;
786 static int
787 pcnet32_open(struct net_device *dev)
789 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
790 unsigned long ioaddr = dev->base_addr;
791 u16 val;
792 int i;
794 if (dev->irq == 0 ||
795 request_irq(dev->irq, &pcnet32_interrupt,
796 lp->shared_irq ? SA_SHIRQ : 0, lp->name, (void *)dev)) {
797 return -EAGAIN;
800 /* Reset the PCNET32 */
801 lp->a.reset (ioaddr);
803 /* switch pcnet32 to 32bit mode */
804 lp->a.write_bcr (ioaddr, 20, 2);
806 if (pcnet32_debug > 1)
807 printk("%s: pcnet32_open() irq %d tx/rx rings %#x/%#x init %#x.\n",
808 dev->name, dev->irq,
809 (u32) virt_to_bus(lp->tx_ring),
810 (u32) virt_to_bus(lp->rx_ring),
811 (u32) virt_to_bus(&lp->init_block));
813 /* set/reset autoselect bit */
814 val = lp->a.read_bcr (ioaddr, 2) & ~2;
815 if (lp->options & PORT_ASEL)
816 val |= 2;
817 lp->a.write_bcr (ioaddr, 2, val);
819 /* handle full duplex setting */
820 if (lp->full_duplex) {
821 val = lp->a.read_bcr (ioaddr, 9) & ~3;
822 if (lp->options & PORT_FD) {
823 val |= 1;
824 if (lp->options == (PORT_FD | PORT_AUI))
825 val |= 2;
827 lp->a.write_bcr (ioaddr, 9, val);
830 /* set/reset GPSI bit in test register */
831 val = lp->a.read_csr (ioaddr, 124) & ~0x10;
832 if ((lp->options & PORT_PORTSEL) == PORT_GPSI)
833 val |= 0x10;
834 lp->a.write_csr (ioaddr, 124, val);
836 if (lp->mii & !(lp->options & PORT_ASEL)) {
837 val = lp->a.read_bcr (ioaddr, 32) & ~0x38; /* disable Auto Negotiation, set 10Mpbs, HD */
838 if (lp->options & PORT_FD)
839 val |= 0x10;
840 if (lp->options & PORT_100)
841 val |= 0x08;
842 lp->a.write_bcr (ioaddr, 32, val);
845 #ifdef DO_DXSUFLO
846 if (lp->dxsuflo) { /* Disable transmit stop on underflow */
847 val = lp->a.read_csr (ioaddr, 3);
848 val |= 0x40;
849 lp->a.write_csr (ioaddr, 3, val);
851 #endif
852 if (lp->ltint) { /* Enable TxDone-intr inhibitor */
853 val = lp->a.read_csr (ioaddr, 5);
854 val |= (1<<14);
855 lp->a.write_csr (ioaddr, 5, val);
858 lp->init_block.mode = le16_to_cpu((lp->options & PORT_PORTSEL) << 7);
859 lp->init_block.filter[0] = 0x00000000;
860 lp->init_block.filter[1] = 0x00000000;
861 if (pcnet32_init_ring(dev))
862 return -ENOMEM;
864 /* Re-initialize the PCNET32, and start it when done. */
865 lp->a.write_csr (ioaddr, 1, virt_to_bus(&lp->init_block) &0xffff);
866 lp->a.write_csr (ioaddr, 2, virt_to_bus(&lp->init_block) >> 16);
868 lp->a.write_csr (ioaddr, 4, 0x0915);
869 lp->a.write_csr (ioaddr, 0, 0x0001);
871 netif_start_queue(dev);
873 i = 0;
874 while (i++ < 100)
875 if (lp->a.read_csr (ioaddr, 0) & 0x0100)
876 break;
878 * We used to clear the InitDone bit, 0x0100, here but Mark Stockton
879 * reports that doing so triggers a bug in the '974.
881 lp->a.write_csr (ioaddr, 0, 0x0042);
883 if (pcnet32_debug > 2)
884 printk("%s: PCNET32 open after %d ticks, init block %#x csr0 %4.4x.\n",
885 dev->name, i, (u32) virt_to_bus(&lp->init_block),
886 lp->a.read_csr (ioaddr, 0));
888 MOD_INC_USE_COUNT;
890 return 0; /* Always succeed */
894 * The LANCE has been halted for one reason or another (busmaster memory
895 * arbitration error, Tx FIFO underflow, driver stopped it to reconfigure,
896 * etc.). Modern LANCE variants always reload their ring-buffer
897 * configuration when restarted, so we must reinitialize our ring
898 * context before restarting. As part of this reinitialization,
899 * find all packets still on the Tx ring and pretend that they had been
900 * sent (in effect, drop the packets on the floor) - the higher-level
901 * protocols will time out and retransmit. It'd be better to shuffle
902 * these skbs to a temp list and then actually re-Tx them after
903 * restarting the chip, but I'm too lazy to do so right now. dplatt@3do.com
906 static void
907 pcnet32_purge_tx_ring(struct net_device *dev)
909 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
910 int i;
912 for (i = 0; i < TX_RING_SIZE; i++) {
913 if (lp->tx_skbuff[i]) {
914 dev_kfree_skb(lp->tx_skbuff[i]);
915 lp->tx_skbuff[i] = NULL;
921 /* Initialize the PCNET32 Rx and Tx rings. */
922 static int
923 pcnet32_init_ring(struct net_device *dev)
925 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
926 int i;
928 lp->tx_full = 0;
929 lp->cur_rx = lp->cur_tx = 0;
930 lp->dirty_rx = lp->dirty_tx = 0;
932 for (i = 0; i < RX_RING_SIZE; i++) {
933 if (lp->rx_skbuff[i] == NULL) {
934 if (!(lp->rx_skbuff[i] = dev_alloc_skb (PKT_BUF_SZ))) {
935 /* there is not much, we can do at this point */
936 printk ("%s: pcnet32_init_ring dev_alloc_skb failed.\n",dev->name);
937 return -1;
939 skb_reserve (lp->rx_skbuff[i], 2);
941 lp->rx_ring[i].base = (u32)le32_to_cpu(virt_to_bus(lp->rx_skbuff[i]->tail));
942 lp->rx_ring[i].buf_length = le16_to_cpu(-PKT_BUF_SZ);
943 lp->rx_ring[i].status = le16_to_cpu(0x8000);
945 /* The Tx buffer address is filled in as needed, but we do need to clear
946 the upper ownership bit. */
947 for (i = 0; i < TX_RING_SIZE; i++) {
948 lp->tx_ring[i].base = 0;
949 lp->tx_ring[i].status = 0;
952 lp->init_block.tlen_rlen = TX_RING_LEN_BITS | RX_RING_LEN_BITS;
953 for (i = 0; i < 6; i++)
954 lp->init_block.phys_addr[i] = dev->dev_addr[i];
955 lp->init_block.rx_ring = (u32)le32_to_cpu(virt_to_bus(lp->rx_ring));
956 lp->init_block.tx_ring = (u32)le32_to_cpu(virt_to_bus(lp->tx_ring));
957 return 0;
960 static void
961 pcnet32_restart(struct net_device *dev, unsigned int csr0_bits)
963 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
964 unsigned long ioaddr = dev->base_addr;
965 int i;
967 pcnet32_purge_tx_ring(dev);
968 if (pcnet32_init_ring(dev))
969 return;
971 /* ReInit Ring */
972 lp->a.write_csr (ioaddr, 0, 1);
973 i = 0;
974 while (i++ < 100)
975 if (lp->a.read_csr (ioaddr, 0) & 0x0100)
976 break;
978 lp->a.write_csr (ioaddr, 0, csr0_bits);
982 static void
983 pcnet32_tx_timeout (struct net_device *dev)
985 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
986 unsigned int ioaddr = dev->base_addr;
988 /* Transmitter timeout, serious problems. */
989 printk("%s: transmit timed out, status %4.4x, resetting.\n",
990 dev->name, lp->a.read_csr (ioaddr, 0));
991 lp->a.write_csr (ioaddr, 0, 0x0004);
992 lp->stats.tx_errors++;
993 if (pcnet32_debug > 2) {
994 int i;
995 printk(" Ring data dump: dirty_tx %d cur_tx %d%s cur_rx %d.",
996 lp->dirty_tx, lp->cur_tx, lp->tx_full ? " (full)" : "",
997 lp->cur_rx);
998 for (i = 0 ; i < RX_RING_SIZE; i++)
999 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
1000 lp->rx_ring[i].base, -lp->rx_ring[i].buf_length,
1001 lp->rx_ring[i].msg_length, (unsigned)lp->rx_ring[i].status);
1002 for (i = 0 ; i < TX_RING_SIZE; i++)
1003 printk("%s %08x %04x %08x %04x", i & 1 ? "" : "\n ",
1004 lp->tx_ring[i].base, -lp->tx_ring[i].length,
1005 lp->tx_ring[i].misc, (unsigned)lp->tx_ring[i].status);
1006 printk("\n");
1008 pcnet32_restart(dev, 0x0042);
1010 dev->trans_start = jiffies;
1011 netif_start_queue(dev);
1015 static int
1016 pcnet32_start_xmit(struct sk_buff *skb, struct net_device *dev)
1018 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1019 unsigned int ioaddr = dev->base_addr;
1020 u16 status;
1021 int entry;
1022 unsigned long flags;
1024 if (pcnet32_debug > 3) {
1025 printk("%s: pcnet32_start_xmit() called, csr0 %4.4x.\n",
1026 dev->name, lp->a.read_csr (ioaddr, 0));
1029 spin_lock_irqsave(&lp->lock, flags);
1031 /* Default status -- will not enable Successful-TxDone
1032 * interrupt when that option is available to us.
1034 status = 0x8300;
1035 if ((lp->ltint) &&
1036 ((lp->cur_tx - lp->dirty_tx == TX_RING_SIZE/2) ||
1037 (lp->cur_tx - lp->dirty_tx >= TX_RING_SIZE-2)))
1039 /* Enable Successful-TxDone interrupt if we have
1040 * 1/2 of, or nearly all of, our ring buffer Tx'd
1041 * but not yet cleaned up. Thus, most of the time,
1042 * we will not enable Successful-TxDone interrupts.
1044 status = 0x9300;
1047 /* Fill in a Tx ring entry */
1049 /* Mask to ring buffer boundary. */
1050 entry = lp->cur_tx & TX_RING_MOD_MASK;
1052 /* Caution: the write order is important here, set the base address
1053 with the "ownership" bits last. */
1055 lp->tx_ring[entry].length = le16_to_cpu(-skb->len);
1057 lp->tx_ring[entry].misc = 0x00000000;
1059 lp->tx_skbuff[entry] = skb;
1060 lp->tx_ring[entry].base = (u32)le32_to_cpu(virt_to_bus(skb->data));
1061 lp->tx_ring[entry].status = le16_to_cpu(status);
1063 dma_cache_wback_inv((void *)skb->data,
1064 (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len);
1066 lp->cur_tx++;
1067 lp->stats.tx_bytes += skb->len;
1069 /* Trigger an immediate send poll. */
1070 lp->a.write_csr (ioaddr, 0, 0x0048);
1072 dev->trans_start = jiffies;
1074 if (lp->tx_ring[(entry+1) & TX_RING_MOD_MASK].base == 0)
1075 netif_start_queue(dev);
1076 else
1077 lp->tx_full = 1;
1078 spin_unlock_irqrestore(&lp->lock, flags);
1079 return 0;
1082 /* The PCNET32 interrupt handler. */
1083 static void
1084 pcnet32_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1086 struct net_device *dev = (struct net_device *)dev_id;
1087 struct pcnet32_private *lp;
1088 unsigned long ioaddr;
1089 u16 csr0,rap;
1090 int boguscnt = max_interrupt_work;
1091 int must_restart;
1093 if (dev == NULL) {
1094 printk ("pcnet32_interrupt(): irq %d for unknown device.\n", irq);
1095 return;
1098 ioaddr = dev->base_addr;
1099 lp = (struct pcnet32_private *)dev->priv;
1101 spin_lock(&lp->lock);
1103 rap = lp->a.read_rap(ioaddr);
1104 while ((csr0 = lp->a.read_csr (ioaddr, 0)) & 0x8600 && --boguscnt >= 0) {
1105 /* Acknowledge all of the current interrupt sources ASAP. */
1106 lp->a.write_csr (ioaddr, 0, csr0 & ~0x004f);
1108 must_restart = 0;
1110 if (pcnet32_debug > 5)
1111 printk("%s: interrupt csr0=%#2.2x new csr=%#2.2x.\n",
1112 dev->name, csr0, lp->a.read_csr (ioaddr, 0));
1114 if (csr0 & 0x0400) /* Rx interrupt */
1115 pcnet32_rx(dev);
1117 if (csr0 & 0x0200) { /* Tx-done interrupt */
1118 unsigned int dirty_tx = lp->dirty_tx;
1120 while (dirty_tx < lp->cur_tx) {
1121 int entry = dirty_tx & TX_RING_MOD_MASK;
1122 int status = (short)le16_to_cpu(lp->tx_ring[entry].status);
1124 if (status < 0)
1125 break; /* It still hasn't been Txed */
1127 lp->tx_ring[entry].base = 0;
1129 if (status & 0x4000) {
1130 /* There was an major error, log it. */
1131 int err_status = le32_to_cpu(lp->tx_ring[entry].misc);
1132 lp->stats.tx_errors++;
1133 if (err_status & 0x04000000) lp->stats.tx_aborted_errors++;
1134 if (err_status & 0x08000000) lp->stats.tx_carrier_errors++;
1135 if (err_status & 0x10000000) lp->stats.tx_window_errors++;
1136 #ifndef DO_DXSUFLO
1137 if (err_status & 0x40000000) {
1138 lp->stats.tx_fifo_errors++;
1139 /* Ackk! On FIFO errors the Tx unit is turned off! */
1140 /* Remove this verbosity later! */
1141 printk("%s: Tx FIFO error! CSR0=%4.4x\n",
1142 dev->name, csr0);
1143 must_restart = 1;
1145 #else
1146 if (err_status & 0x40000000) {
1147 lp->stats.tx_fifo_errors++;
1148 if (! lp->dxsuflo) { /* If controller doesn't recover ... */
1149 /* Ackk! On FIFO errors the Tx unit is turned off! */
1150 /* Remove this verbosity later! */
1151 printk("%s: Tx FIFO error! CSR0=%4.4x\n",
1152 dev->name, csr0);
1153 must_restart = 1;
1156 #endif
1157 } else {
1158 if (status & 0x1800)
1159 lp->stats.collisions++;
1160 lp->stats.tx_packets++;
1163 /* We must free the original skb */
1164 if (lp->tx_skbuff[entry]) {
1165 dev_kfree_skb_irq(lp->tx_skbuff[entry]);
1166 lp->tx_skbuff[entry] = 0;
1168 dirty_tx++;
1171 #ifndef final_version
1172 if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
1173 printk("out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
1174 dirty_tx, lp->cur_tx, lp->tx_full);
1175 dirty_tx += TX_RING_SIZE;
1177 #endif
1178 if (lp->tx_full &&
1179 netif_queue_stopped(dev) &&
1180 dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
1181 /* The ring is no longer full, clear tbusy. */
1182 lp->tx_full = 0;
1183 netif_wake_queue (dev);
1185 lp->dirty_tx = dirty_tx;
1188 /* Log misc errors. */
1189 if (csr0 & 0x4000) lp->stats.tx_errors++; /* Tx babble. */
1190 if (csr0 & 0x1000) {
1192 * this happens when our receive ring is full. This shouldn't
1193 * be a problem as we will see normal rx interrupts for the frames
1194 * in the receive ring. But there are some PCI chipsets (I can reproduce
1195 * this on SP3G with Intel saturn chipset) which have sometimes problems
1196 * and will fill up the receive ring with error descriptors. In this
1197 * situation we don't get a rx interrupt, but a missed frame interrupt sooner
1198 * or later. So we try to clean up our receive ring here.
1200 pcnet32_rx(dev);
1201 lp->stats.rx_errors++; /* Missed a Rx frame. */
1203 if (csr0 & 0x0800) {
1204 printk("%s: Bus master arbitration failure, status %4.4x.\n",
1205 dev->name, csr0);
1206 /* unlike for the lance, there is no restart needed */
1209 if (must_restart) {
1210 /* stop the chip to clear the error condition, then restart */
1211 lp->a.write_csr (ioaddr, 0, 0x0004);
1212 pcnet32_restart(dev, 0x0002);
1216 /* Clear any other interrupt, and set interrupt enable. */
1217 lp->a.write_csr (ioaddr, 0, 0x7940);
1218 lp->a.write_rap(ioaddr,rap);
1220 if (pcnet32_debug > 4)
1221 printk("%s: exiting interrupt, csr0=%#4.4x.\n",
1222 dev->name, lp->a.read_csr (ioaddr, 0));
1224 spin_unlock(&lp->lock);
1227 static int
1228 pcnet32_rx(struct net_device *dev)
1230 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1231 int entry = lp->cur_rx & RX_RING_MOD_MASK;
1232 int i;
1234 /* If we own the next entry, it's a new packet. Send it up. */
1235 while ((short)le16_to_cpu(lp->rx_ring[entry].status) >= 0) {
1236 int status = (short)le16_to_cpu(lp->rx_ring[entry].status) >> 8;
1238 if (status != 0x03) { /* There was an error. */
1240 * There is a tricky error noted by John Murphy,
1241 * <murf@perftech.com> to Russ Nelson: Even with full-sized
1242 * buffers it's possible for a jabber packet to use two
1243 * buffers, with only the last correctly noting the error.
1245 if (status & 0x01) /* Only count a general error at the */
1246 lp->stats.rx_errors++; /* end of a packet.*/
1247 if (status & 0x20) lp->stats.rx_frame_errors++;
1248 if (status & 0x10) lp->stats.rx_over_errors++;
1249 if (status & 0x08) lp->stats.rx_crc_errors++;
1250 if (status & 0x04) lp->stats.rx_fifo_errors++;
1251 lp->rx_ring[entry].status &= le16_to_cpu(0x03ff);
1252 } else {
1253 /* Malloc up new buffer, compatible with net-2e. */
1254 short pkt_len = (le32_to_cpu(lp->rx_ring[entry].msg_length) & 0xfff)-4;
1255 struct sk_buff *skb;
1257 if(pkt_len < 60) {
1258 printk("%s: Runt packet!\n",dev->name);
1259 lp->stats.rx_errors++;
1260 } else {
1261 int rx_in_place = 0;
1263 if (pkt_len > rx_copybreak) {
1264 struct sk_buff *newskb;
1266 if ((newskb = dev_alloc_skb (PKT_BUF_SZ))) {
1267 skb_reserve (newskb, 2);
1268 skb = lp->rx_skbuff[entry];
1269 skb_put (skb, pkt_len);
1270 lp->rx_skbuff[entry] = newskb;
1271 newskb->dev = dev;
1272 lp->rx_ring[entry].base = le32_to_cpu(virt_to_bus(newskb->tail));
1273 rx_in_place = 1;
1274 } else
1275 skb = NULL;
1276 } else
1277 skb = dev_alloc_skb(pkt_len+2);
1279 if (skb == NULL) {
1280 printk("%s: Memory squeeze, deferring packet.\n", dev->name);
1281 for (i=0; i < RX_RING_SIZE; i++)
1282 if ((short)le16_to_cpu(lp->rx_ring[(entry+i) & RX_RING_MOD_MASK].status) < 0)
1283 break;
1285 if (i > RX_RING_SIZE -2) {
1286 lp->stats.rx_dropped++;
1287 lp->rx_ring[entry].status |= le16_to_cpu(0x8000);
1288 lp->cur_rx++;
1290 break;
1292 skb->dev = dev;
1293 if (!rx_in_place) {
1294 skb_reserve(skb,2); /* 16 byte align */
1295 skb_put(skb,pkt_len); /* Make room */
1296 eth_copy_and_sum(skb,
1297 (unsigned char *)bus_to_virt(le32_to_cpu(lp->rx_ring[entry].base)),
1298 pkt_len,0);
1300 lp->stats.rx_bytes += skb->len;
1301 skb->protocol=eth_type_trans(skb,dev);
1302 netif_rx(skb);
1303 dma_cache_inv(bus_to_virt(le32_to_cpu(lp->rx_ring[entry].base)),
1304 pkt_len);
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("%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 dev_kfree_skb(lp->rx_skbuff[i]);
1351 lp->rx_skbuff[i] = NULL;
1354 for (i = 0; i < TX_RING_SIZE; i++) {
1355 if (lp->tx_skbuff[i])
1356 dev_kfree_skb(lp->tx_skbuff[i]);
1357 lp->rx_skbuff[i] = NULL;
1360 MOD_DEC_USE_COUNT;
1362 return 0;
1365 static struct net_device_stats *
1366 pcnet32_get_stats(struct net_device *dev)
1368 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1369 unsigned long ioaddr = dev->base_addr;
1370 u16 saved_addr;
1371 unsigned long flags;
1373 spin_lock_irqsave(&lp->lock, flags);
1374 saved_addr = lp->a.read_rap(ioaddr);
1375 lp->stats.rx_missed_errors = lp->a.read_csr (ioaddr, 112);
1376 lp->a.write_rap(ioaddr, saved_addr);
1377 spin_unlock_irqrestore(&lp->lock, flags);
1379 return &lp->stats;
1382 /* taken from the sunlance driver, which it took from the depca driver */
1383 static void pcnet32_load_multicast (struct net_device *dev)
1385 struct pcnet32_private *lp = (struct pcnet32_private *) dev->priv;
1386 volatile struct pcnet32_init_block *ib = &lp->init_block;
1387 volatile u16 *mcast_table = (u16 *)&ib->filter;
1388 struct dev_mc_list *dmi=dev->mc_list;
1389 char *addrs;
1390 int i, j, bit, byte;
1391 u32 crc, poly = CRC_POLYNOMIAL_LE;
1393 /* set all multicast bits */
1394 if (dev->flags & IFF_ALLMULTI){
1395 ib->filter [0] = 0xffffffff;
1396 ib->filter [1] = 0xffffffff;
1397 return;
1399 /* clear the multicast filter */
1400 ib->filter [0] = 0;
1401 ib->filter [1] = 0;
1403 /* Add addresses */
1404 for (i = 0; i < dev->mc_count; i++){
1405 addrs = dmi->dmi_addr;
1406 dmi = dmi->next;
1408 /* multicast address? */
1409 if (!(*addrs & 1))
1410 continue;
1412 crc = 0xffffffff;
1413 for (byte = 0; byte < 6; byte++)
1414 for (bit = *addrs++, j = 0; j < 8; j++, bit >>= 1) {
1415 int test;
1417 test = ((bit ^ crc) & 0x01);
1418 crc >>= 1;
1420 if (test) {
1421 crc = crc ^ poly;
1425 crc = crc >> 26;
1426 mcast_table [crc >> 4] |= 1 << (crc & 0xf);
1428 return;
1433 * Set or clear the multicast filter for this adaptor.
1435 static void pcnet32_set_multicast_list(struct net_device *dev)
1437 unsigned long ioaddr = dev->base_addr;
1438 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1440 if (dev->flags&IFF_PROMISC) {
1441 /* Log any net taps. */
1442 printk("%s: Promiscuous mode enabled.\n", dev->name);
1443 lp->init_block.mode = le16_to_cpu(0x8000 | (lp->options & PORT_PORTSEL) << 7);
1444 } else {
1445 lp->init_block.mode = le16_to_cpu((lp->options & PORT_PORTSEL) << 7);
1446 pcnet32_load_multicast (dev);
1449 lp->a.write_csr (ioaddr, 0, 0x0004); /* Temporarily stop the lance. */
1451 pcnet32_restart(dev, 0x0042); /* Resume normal operation */
1454 #ifdef HAVE_PRIVATE_IOCTL
1455 static int pcnet32_mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1457 unsigned long ioaddr = dev->base_addr;
1458 struct pcnet32_private *lp = (struct pcnet32_private *)dev->priv;
1459 u16 *data = (u16 *)&rq->ifr_data;
1460 int phyaddr = lp->a.read_bcr (ioaddr, 33);
1462 if (lp->mii) {
1463 switch(cmd) {
1464 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
1465 data[0] = (phyaddr >> 5) & 0x1f;
1466 /* Fall Through */
1467 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
1468 lp->a.write_bcr (ioaddr, 33, ((data[0] & 0x1f) << 5) | (data[1] & 0x1f));
1469 data[3] = lp->a.read_bcr (ioaddr, 34);
1470 lp->a.write_bcr (ioaddr, 33, phyaddr);
1471 return 0;
1472 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
1473 if (!capable(CAP_NET_ADMIN))
1474 return -EPERM;
1475 lp->a.write_bcr (ioaddr, 33, ((data[0] & 0x1f) << 5) | (data[1] & 0x1f));
1476 lp->a.write_bcr (ioaddr, 34, data[2]);
1477 lp->a.write_bcr (ioaddr, 33, phyaddr);
1478 return 0;
1479 default:
1480 return -EOPNOTSUPP;
1483 return -EOPNOTSUPP;
1485 #endif /* HAVE_PRIVATE_IOCTL */
1487 MODULE_PARM(debug, "i");
1488 MODULE_PARM(max_interrupt_work, "i");
1489 MODULE_PARM(rx_copybreak, "i");
1490 MODULE_PARM(tx_start_pt, "i");
1491 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
1492 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
1493 MODULE_AUTHOR("Thomas Bogendoerfer");
1494 MODULE_DESCRIPTION("Driver for PCnet32 and PCnetPCI based ethercards");
1496 /* An additional parameter that may be passed in... */
1497 static int debug = -1;
1498 static int tx_start_pt = -1;
1500 static int __init pcnet32_init_module(void)
1502 if (debug > 0)
1503 pcnet32_debug = debug;
1504 if ((tx_start_pt >= 0) && (tx_start_pt <= 3))
1505 tx_start = tx_start_pt;
1507 pcnet32_dev = NULL;
1508 return pcnet32_probe();
1511 static void __exit pcnet32_cleanup_module(void)
1513 struct net_device *next_dev;
1515 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1516 while (pcnet32_dev) {
1517 next_dev = ((struct pcnet32_private *) pcnet32_dev->priv)->next;
1518 unregister_netdev(pcnet32_dev);
1519 release_region(pcnet32_dev->base_addr, PCNET32_TOTAL_SIZE);
1520 kfree(((struct pcnet32_private *)pcnet32_dev->priv)->origmem);
1521 kfree(pcnet32_dev);
1522 pcnet32_dev = next_dev;
1526 module_init(pcnet32_init_module);
1527 module_exit(pcnet32_cleanup_module);
1530 * Local variables:
1531 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c pcnet32.c"
1532 * c-indent-level: 4
1533 * tab-width: 4
1534 * End: