File: /usr/src/linux/drivers/net/pcmcia/xircom_tulip_cb.c
1 /* tulip.c: A DEC 21040-family ethernet driver for Linux. */
2 /*
3 Written/copyright 1994-1999 by Donald Becker.
4
5 This software may be used and distributed according to the terms
6 of the GNU General Public License, incorporated herein by reference.
7
8 This driver is for the Digital "Tulip" Ethernet adapter interface.
9 It should work with most DEC 21*4*-based chips/ethercards, as well as
10 with work-alike chips from Lite-On (PNIC) and Macronix (MXIC) and ASIX.
11
12 The author may be reached as becker@scyld.com, or C/O
13 Scyld Computing Corporation
14 410 Severn Ave., Suite 210
15 Annapolis MD 21403
16
17 Support and updates available at
18 http://cesdis.gsfc.nasa.gov/linux/drivers/tulip.html
19 */
20
21 #define CARDBUS 1
22 static const char version[] = "xircom_tulip_cb.c:v0.91 4/14/99 becker@scyld.com (modified by danilo@cs.uni-magdeburg.de for XIRCOM CBE, fixed by Doug Ledford)\n";
23
24 /* A few user-configurable values. */
25
26 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
27 static int max_interrupt_work = 25;
28
29 #define MAX_UNITS 8
30 /* Used to pass the full-duplex flag, etc. */
31 static int full_duplex[MAX_UNITS];
32 static int options[MAX_UNITS];
33 static int mtu[MAX_UNITS]; /* Jumbo MTU for interfaces. */
34
35 /* The possible media types that can be set in options[] are: */
36 static const char * const medianame[] = {
37 "10baseT", "10base2", "AUI", "100baseTx",
38 "10baseT-FD", "100baseTx-FD", "100baseT4", "100baseFx",
39 "100baseFx-FD", "MII 10baseT", "MII 10baseT-FD", "MII",
40 "10baseT(forced)", "MII 100baseTx", "MII 100baseTx-FD", "MII 100baseT4",
41 };
42
43 /* Keep the ring sizes a power of two for efficiency.
44 Making the Tx ring too large decreases the effectiveness of channel
45 bonding and packet priority.
46 There are no ill effects from too-large receive rings. */
47 #define TX_RING_SIZE 16
48 #define RX_RING_SIZE 32
49
50 /* Set the copy breakpoint for the copy-only-tiny-buffer Rx structure. */
51 #ifdef __alpha__
52 static int rx_copybreak = 1518;
53 #else
54 static int rx_copybreak = 100;
55 #endif
56
57 /*
58 Set the bus performance register.
59 Typical: Set 16 longword cache alignment, no burst limit.
60 Cache alignment bits 15:14 Burst length 13:8
61 0000 No alignment 0x00000000 unlimited 0800 8 longwords
62 4000 8 longwords 0100 1 longword 1000 16 longwords
63 8000 16 longwords 0200 2 longwords 2000 32 longwords
64 C000 32 longwords 0400 4 longwords
65 Warning: many older 486 systems are broken and require setting 0x00A04800
66 8 longword cache alignment, 8 longword burst.
67 ToDo: Non-Intel setting could be better.
68 */
69
70 #if defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
71 static int csr0 = 0x01A00000 | 0xE000;
72 #elif defined(__powerpc__)
73 static int csr0 = 0x01B00000 | 0x8000;
74 #elif defined(__sparc__)
75 static int csr0 = 0x01B00080 | 0x8000;
76 #elif defined(__i386__)
77 static int csr0 = 0x01A00000 | 0x8000;
78 #else
79 #warning Processor architecture undefined!
80 static int csr0 = 0x00A00000 | 0x4800;
81 #endif
82
83 /* Operational parameters that usually are not changed. */
84 /* Time in jiffies before concluding the transmitter is hung. */
85 #define TX_TIMEOUT (4*HZ)
86 #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer.*/
87
88 #if !defined(__OPTIMIZE__) || !defined(__KERNEL__)
89 #warning You must compile this file with the correct options!
90 #warning See the last lines of the source file.
91 #error You must compile this driver with "-O".
92 #endif
93
94 #include <linux/version.h>
95 #include <linux/module.h>
96 #include <linux/kernel.h>
97 #include <linux/pci.h>
98 #include <linux/netdevice.h>
99 #include <linux/etherdevice.h>
100 #include <linux/delay.h>
101 #include <linux/init.h>
102 #include <asm/processor.h> /* Processor type for cache alignment. */
103
104 /* Kernel compatibility defines, some common to David Hinds' PCMCIA package.
105 This is only in the support-all-kernels source code. */
106
107 MODULE_AUTHOR("Donald Becker <becker@scyld.com>");
108 MODULE_DESCRIPTION("Digital 21*4* Tulip ethernet driver");
109 MODULE_PARM(debug, "i");
110 MODULE_PARM(max_interrupt_work, "i");
111 MODULE_PARM(rx_copybreak, "i");
112 MODULE_PARM(csr0, "i");
113 MODULE_PARM(options, "1-" __MODULE_STRING(MAX_UNITS) "i");
114 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(MAX_UNITS) "i");
115
116 #define RUN_AT(x) (jiffies + (x))
117
118 #define tulip_debug debug
119 #ifdef TULIP_DEBUG
120 static int tulip_debug = TULIP_DEBUG;
121 #else
122 static int tulip_debug = 1;
123 #endif
124
125 /*
126 Theory of Operation
127
128 I. Board Compatibility
129
130 This device driver is designed for the DECchip "Tulip", Digital's
131 single-chip ethernet controllers for PCI. Supported members of the family
132 are the 21040, 21041, 21140, 21140A, 21142, and 21143. Similar work-alike
133 chips from Lite-On, Macronics, ASIX, Compex and other listed below are also
134 supported.
135
136 These chips are used on at least 140 unique PCI board designs. The great
137 number of chips and board designs supported is the reason for the
138 driver size and complexity. Almost of the increasing complexity is in the
139 board configuration and media selection code. There is very little
140 increasing in the operational critical path length.
141
142 II. Board-specific settings
143
144 PCI bus devices are configured by the system at boot time, so no jumpers
145 need to be set on the board. The system BIOS preferably should assign the
146 PCI INTA signal to an otherwise unused system IRQ line.
147
148 III. Driver operation
149
150 IIIa. Ring buffers
151
152 The Tulip can use either ring buffers or lists of Tx and Rx descriptors.
153 This driver uses statically allocated rings of Rx and Tx descriptors, set at
154 compile time by RX/TX_RING_SIZE. This version of the driver allocates skbuffs
155 for the Rx ring buffers at open() time and passes the skb->data field to the
156 Tulip as receive data buffers. When an incoming frame is less than
157 RX_COPYBREAK bytes long, a fresh skbuff is allocated and the frame is
158 copied to the new skbuff. When the incoming frame is larger, the skbuff is
159 passed directly up the protocol stack and replaced by a newly allocated
160 skbuff.
161
162 The RX_COPYBREAK value is chosen to trade-off the memory wasted by
163 using a full-sized skbuff for small frames vs. the copying costs of larger
164 frames. For small frames the copying cost is negligible (esp. considering
165 that we are pre-loading the cache with immediately useful header
166 information). For large frames the copying cost is non-trivial, and the
167 larger copy might flush the cache of useful data. A subtle aspect of this
168 choice is that the Tulip only receives into longword aligned buffers, thus
169 the IP header at offset 14 isn't longword aligned for further processing.
170 Copied frames are put into the new skbuff at an offset of "+2", thus copying
171 has the beneficial effect of aligning the IP header and preloading the
172 cache.
173
174 IIIC. Synchronization
175 The driver runs as two independent, single-threaded flows of control. One
176 is the send-packet routine, which enforces single-threaded use by the
177 dev->tbusy flag. The other thread is the interrupt handler, which is single
178 threaded by the hardware and other software.
179
180 The send packet thread has partial control over the Tx ring and 'dev->tbusy'
181 flag. It sets the tbusy flag whenever it's queuing a Tx packet. If the next
182 queue slot is empty, it clears the tbusy flag when finished otherwise it sets
183 the 'tp->tx_full' flag.
184
185 The interrupt handler has exclusive control over the Rx ring and records stats
186 from the Tx ring. (The Tx-done interrupt can't be selectively turned off, so
187 we can't avoid the interrupt overhead by having the Tx routine reap the Tx
188 stats.) After reaping the stats, it marks the queue entry as empty by setting
189 the 'base' to zero. Iff the 'tp->tx_full' flag is set, it clears both the
190 tx_full and tbusy flags.
191
192 IV. Notes
193
194 IVb. References
195
196 http://cesdis.gsfc.nasa.gov/linux/misc/NWay.html
197 http://www.digital.com (search for current 21*4* datasheets and "21X4 SROM")
198 http://www.national.com/pf/DP/DP83840A.html
199
200 IVc. Errata
201
202 We cannot use MII interrupts because there is no defined GPIO pin to attach
203 them. The MII transceiver status is polled using an kernel timer.
204
205 */
206
207 static void tulip_timer(unsigned long data);
208
209 enum tbl_flag {
210 HAS_MII=1, HAS_ACPI=2,
211 };
212 static struct tulip_chip_table {
213 char *chip_name;
214 int io_size;
215 int valid_intrs; /* CSR7 interrupt enable settings */
216 int flags;
217 void (*media_timer)(unsigned long data);
218 } tulip_tbl[] = {
219 { "Xircom Cardbus Adapter (DEC 21143 compatible mode)", 128, 0x0801fbff,
220 HAS_MII | HAS_ACPI, tulip_timer },
221 {0},
222 };
223 /* This matches the table above. */
224 enum chips {
225 X3201_3,
226 };
227
228 /* A full-duplex map for media types. */
229 enum MediaIs {
230 MediaIsFD = 1, MediaAlwaysFD=2, MediaIsMII=4, MediaIsFx=8,
231 MediaIs100=16};
232 static const char media_cap[] =
233 {0,0,0,16, 3,19,16,24, 27,4,7,5, 0,20,23,20 };
234
235 /* Offsets to the Command and Status Registers, "CSRs". All accesses
236 must be longword instructions and quadword aligned. */
237 enum tulip_offsets {
238 CSR0=0, CSR1=0x08, CSR2=0x10, CSR3=0x18, CSR4=0x20, CSR5=0x28,
239 CSR6=0x30, CSR7=0x38, CSR8=0x40, CSR9=0x48, CSR10=0x50, CSR11=0x58,
240 CSR12=0x60, CSR13=0x68, CSR14=0x70, CSR15=0x78 };
241
242 /* The bits in the CSR5 status registers, mostly interrupt sources. */
243 enum status_bits {
244 TimerInt=0x800, TPLnkFail=0x1000, TPLnkPass=0x10,
245 NormalIntr=0x10000, AbnormalIntr=0x8000,
246 RxJabber=0x200, RxDied=0x100, RxNoBuf=0x80, RxIntr=0x40,
247 TxFIFOUnderflow=0x20, TxJabber=0x08, TxNoBuf=0x04, TxDied=0x02, TxIntr=0x01,
248 };
249
250 /* The Tulip Rx and Tx buffer descriptors. */
251 struct tulip_rx_desc {
252 s32 status;
253 s32 length;
254 u32 buffer1, buffer2;
255 };
256
257 struct tulip_tx_desc {
258 s32 status;
259 s32 length;
260 u32 buffer1, buffer2; /* We use only buffer 1. */
261 };
262
263 enum desc_status_bits {
264 DescOwned=0x80000000, RxDescFatalErr=0x8000, RxWholePkt=0x0300,
265 };
266
267 /* Ring-wrap flag in length field, use for last ring entry.
268 0x01000000 means chain on buffer2 address,
269 0x02000000 means use the ring start address in CSR2/3.
270 Note: Some work-alike chips do not function correctly in chained mode.
271 The ASIX chip works only in chained mode.
272 Thus we indicates ring mode, but always write the 'next' field for
273 chained mode as well.
274 */
275 #define DESC_RING_WRAP 0x02000000
276
277 struct tulip_private {
278 char devname[8]; /* Used only for kernel debugging. */
279 const char *product_name;
280 struct tulip_rx_desc rx_ring[RX_RING_SIZE];
281 struct tulip_tx_desc tx_ring[TX_RING_SIZE];
282 /* The saved address of a sent-in-place packet/buffer, for skfree(). */
283 struct sk_buff* tx_skbuff[TX_RING_SIZE];
284 #ifdef CARDBUS
285 /* The X3201-3 requires double word aligned tx bufs */
286 struct sk_buff* tx_aligned_skbuff[TX_RING_SIZE];
287 #endif
288 /* The addresses of receive-in-place skbuffs. */
289 struct sk_buff* rx_skbuff[RX_RING_SIZE];
290 char *rx_buffs; /* Address of temporary Rx buffers. */
291 u8 setup_buf[96*sizeof(u16) + 7];
292 u16 *setup_frame; /* Pseudo-Tx frame to init address table. */
293 int chip_id;
294 int revision;
295 struct net_device_stats stats;
296 struct timer_list timer; /* Media selection timer. */
297 int interrupt; /* In-interrupt flag. */
298 unsigned int cur_rx, cur_tx; /* The next free ring entry */
299 unsigned int dirty_rx, dirty_tx; /* The ring entries to be free()ed. */
300 unsigned int tx_full:1; /* The Tx queue is full. */
301 unsigned int full_duplex:1; /* Full-duplex operation requested. */
302 unsigned int full_duplex_lock:1;
303 unsigned int default_port:4; /* Last dev->if_port value. */
304 unsigned int medialock:1; /* Don't sense media type. */
305 unsigned int mediasense:1; /* Media sensing in progress. */
306 unsigned int open:1;
307 unsigned int csr0; /* CSR0 setting. */
308 unsigned int csr6; /* Current CSR6 control settings. */
309 u16 to_advertise; /* NWay capabilities advertised. */
310 u16 advertising[4];
311 signed char phys[4], mii_cnt; /* MII device addresses. */
312 int saved_if_port;
313 struct pci_dev *pdev;
314 spinlock_t lock;
315 };
316
317 static int mdio_read(struct net_device *dev, int phy_id, int location);
318 static void mdio_write(struct net_device *dev, int phy_id, int location, int value);
319 static void tulip_up(struct net_device *dev);
320 static void tulip_down(struct net_device *dev);
321 static int tulip_open(struct net_device *dev);
322 static void tulip_timer(unsigned long data);
323 static void tulip_tx_timeout(struct net_device *dev);
324 static void tulip_init_ring(struct net_device *dev);
325 static int tulip_start_xmit(struct sk_buff *skb, struct net_device *dev);
326 static int tulip_rx(struct net_device *dev);
327 static void tulip_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
328 static int tulip_close(struct net_device *dev);
329 static struct net_device_stats *tulip_get_stats(struct net_device *dev);
330 #ifdef HAVE_PRIVATE_IOCTL
331 static int private_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
332 #endif
333 static void set_rx_mode(struct net_device *dev);
334
335 /* The Xircom cards are picky about when certain bits in CSR6 can be
336 manipulated. Keith Owens <kaos@ocs.com.au>. */
337
338 static void outl_CSR6 (u32 newcsr6, long ioaddr, int chip_idx)
339 {
340 const int strict_bits = 0x0060e202;
341 int csr5, csr5_22_20, csr5_19_17, currcsr6, attempts = 200;
342 long flags;
343 save_flags(flags);
344 cli();
345 newcsr6 &= 0x726cfecb; /* mask out the reserved CSR6 bits that always */
346 /* read 0 on the Xircom cards */
347 newcsr6 |= 0x320c0000; /* or in the reserved bits that always read 1 */
348 currcsr6 = inl(ioaddr + CSR6);
349 if (((newcsr6 & strict_bits) == (currcsr6 & strict_bits)) ||
350 ((currcsr6 & ~0x2002) == 0)) {
351 outl(newcsr6, ioaddr + CSR6); /* safe */
352 restore_flags(flags);
353 return;
354 }
355 /* make sure the transmitter and receiver are stopped first */
356 currcsr6 &= ~0x2002;
357 while (1) {
358 csr5 = inl(ioaddr + CSR5);
359 if (csr5 == 0xffffffff)
360 break; /* cannot read csr5, card removed? */
361 csr5_22_20 = csr5 & 0x700000;
362 csr5_19_17 = csr5 & 0x0e0000;
363 if ((csr5_22_20 == 0 || csr5_22_20 == 0x600000) &&
364 (csr5_19_17 == 0 || csr5_19_17 == 0x80000 || csr5_19_17 == 0xc0000))
365 break; /* both are stopped or suspended */
366 if (!--attempts) {
367 printk(KERN_INFO "tulip.c: outl_CSR6 too many attempts,"
368 "csr5=0x%08x\n", csr5);
369 outl(newcsr6, ioaddr + CSR6); /* unsafe but do it anyway */
370 restore_flags(flags);
371 return;
372 }
373 outl(currcsr6, ioaddr + CSR6);
374 udelay(1);
375 }
376 /* now it is safe to change csr6 */
377 outl(newcsr6, ioaddr + CSR6);
378 restore_flags(flags);
379 }
380
381 static struct net_device *tulip_probe1(struct pci_dev *pdev,
382 long ioaddr, int irq,
383 int chip_idx, int board_idx)
384 {
385 static int did_version; /* Already printed version info. */
386 struct net_device *dev;
387 struct tulip_private *tp;
388 u8 chip_rev;
389 int i;
390
391 if (tulip_debug > 0 && did_version++ == 0)
392 printk(KERN_INFO "%s", version);
393
394 dev = alloc_etherdev(0);
395 if (!dev)
396 return NULL;
397
398 pci_read_config_byte(pdev, PCI_REVISION_ID, &chip_rev);
399 /* Bring the 21143 out of sleep mode.
400 Caution: Snooze mode does not work with some boards! */
401 if (tulip_tbl[chip_idx].flags & HAS_ACPI)
402 pci_write_config_dword(pdev, 0x40, 0x00000000);
403
404 /* Stop the chip's Tx and Rx processes. */
405 outl_CSR6(inl(ioaddr + CSR6) & ~0x2002, ioaddr, chip_idx);
406 /* Clear the missed-packet counter. */
407 (volatile int)inl(ioaddr + CSR8);
408
409 /* The station address ROM is read byte serially. The register must
410 be polled, waiting for the value to be read bit serially from the
411 EEPROM.
412 */
413 if (chip_idx == X3201_3) {
414 /* Xircom has its address stored in the CIS
415 * we access it through the boot rom interface for now
416 * this might not work, as the CIS is not parsed but I
417 * (danilo) use the offset I found on my card's CIS !!!
418 *
419 * Doug Ledford: I changed this routine around so that it
420 * walks the CIS memory space, parsing the config items, and
421 * finds the proper lan_node_id tuple and uses the data
422 * stored there.
423 */
424 unsigned char j, tuple, link, data_id, data_count;
425 outl(1<<12, ioaddr + CSR9); /* enable boot rom access */
426 for (i = 0x100; i < 0x1f7; i += link+2) {
427 outl(i, ioaddr + CSR10);
428 tuple = inl(ioaddr + CSR9) & 0xff;
429 outl(i + 1, ioaddr + CSR10);
430 link = inl(ioaddr + CSR9) & 0xff;
431 outl(i + 2, ioaddr + CSR10);
432 data_id = inl(ioaddr + CSR9) & 0xff;
433 outl(i + 3, ioaddr + CSR10);
434 data_count = inl(ioaddr + CSR9) & 0xff;
435 if ( (tuple == 0x22) &&
436 (data_id == 0x04) && (data_count == 0x06) ) {
437 /*
438 * This is it. We have the data we want.
439 */
440 for (j = 0; j < 6; j++) {
441 outl(i + j + 4, ioaddr + CSR10);
442 dev->dev_addr[j] = inl(ioaddr + CSR9) & 0xff;
443 }
444 break;
445 } else if (link == 0) {
446 break;
447 }
448 }
449 }
450
451 /* We do a request_region() only to register /proc/ioports info. */
452 request_region(ioaddr, tulip_tbl[chip_idx].io_size, "xircom_tulip_cb");
453
454 dev->base_addr = ioaddr;
455 dev->irq = irq;
456
457 /* Make certain the data structures are quadword aligned. */
458 tp = (void *)(((long)kmalloc(sizeof(*tp), GFP_KERNEL | GFP_DMA) + 7) & ~7);
459 memset(tp, 0, sizeof(*tp));
460 dev->priv = tp;
461
462 tp->lock = SPIN_LOCK_UNLOCKED;
463 tp->pdev = pdev;
464 tp->chip_id = chip_idx;
465 tp->revision = chip_rev;
466 tp->csr0 = csr0;
467 tp->setup_frame = (u16 *)(((unsigned long)tp->setup_buf + 7) & ~7);
468
469 /* BugFixes: The 21143-TD hangs with PCI Write-and-Invalidate cycles.
470 And the ASIX must have a burst limit or horrible things happen. */
471 if (chip_idx == X3201_3)
472 tp->csr0 &= ~0x01000000;
473
474 /* The lower four bits are the media type. */
475 if (board_idx >= 0 && board_idx < MAX_UNITS) {
476 tp->default_port = options[board_idx] & 15;
477 if ((options[board_idx] & 0x90) || full_duplex[board_idx] > 0)
478 tp->full_duplex = 1;
479 if (mtu[board_idx] > 0)
480 dev->mtu = mtu[board_idx];
481 }
482 if (dev->mem_start)
483 tp->default_port = dev->mem_start;
484 if (tp->default_port) {
485 tp->medialock = 1;
486 if (media_cap[tp->default_port] & MediaAlwaysFD)
487 tp->full_duplex = 1;
488 }
489 if (tp->full_duplex)
490 tp->full_duplex_lock = 1;
491
492 if (media_cap[tp->default_port] & MediaIsMII) {
493 u16 media2advert[] = { 0x20, 0x40, 0x03e0, 0x60, 0x80, 0x100, 0x200 };
494 tp->to_advertise = media2advert[tp->default_port - 9];
495 } else
496 tp->to_advertise = 0x03e1;
497
498 if (tulip_tbl[chip_idx].flags & HAS_MII) {
499 int phy, phy_idx;
500 /* Find the connected MII xcvrs.
501 Doing this in open() would allow detecting external xcvrs later,
502 but takes much time. */
503 for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(tp->phys);
504 phy++) {
505 int mii_status = mdio_read(dev, phy, 1);
506 if ((mii_status & 0x8301) == 0x8001 ||
507 ((mii_status & 0x8000) == 0 && (mii_status & 0x7800) != 0)) {
508 int mii_reg0 = mdio_read(dev, phy, 0);
509 int mii_advert = mdio_read(dev, phy, 4);
510 int reg4 = ((mii_status>>6) & tp->to_advertise) | 1;
511 tp->phys[phy_idx] = phy;
512 tp->advertising[phy_idx++] = reg4;
513 printk(KERN_INFO "xircom(%s): MII transceiver #%d "
514 "config %4.4x status %4.4x advertising %4.4x.\n",
515 pdev->slot_name, phy, mii_reg0, mii_status, mii_advert);
516 /* Fixup for DLink with miswired PHY. */
517 if (mii_advert != reg4) {
518 printk(KERN_DEBUG "xircom(%s): Advertising %4.4x on PHY %d,"
519 " previously advertising %4.4x.\n",
520 pdev->slot_name, reg4, phy, mii_advert);
521 mdio_write(dev, phy, 4, reg4);
522 }
523 /* Enable autonegotiation: some boards default to off. */
524 mdio_write(dev, phy, 0, mii_reg0 |
525 (tp->full_duplex ? 0x1100 : 0x1000) |
526 (media_cap[tp->default_port]&MediaIs100 ? 0x2000:0));
527 }
528 }
529 tp->mii_cnt = phy_idx;
530 if (phy_idx == 0) {
531 printk(KERN_INFO "xircom(%s): ***WARNING***: No MII transceiver found!\n",
532 pdev->slot_name);
533 tp->phys[0] = 1;
534 }
535 }
536
537 /* The Tulip-specific entries in the device structure. */
538 dev->open = &tulip_open;
539 dev->hard_start_xmit = &tulip_start_xmit;
540 dev->stop = &tulip_close;
541 dev->get_stats = &tulip_get_stats;
542 #ifdef HAVE_PRIVATE_IOCTL
543 dev->do_ioctl = &private_ioctl;
544 #endif
545 #ifdef HAVE_MULTICAST
546 dev->set_multicast_list = &set_rx_mode;
547 #endif
548 dev->tx_timeout = tulip_tx_timeout;
549 dev->watchdog_timeo = TX_TIMEOUT;
550
551 /* Reset the xcvr interface and turn on heartbeat. */
552 switch (chip_idx) {
553 case X3201_3:
554 outl(0x0008, ioaddr + CSR15);
555 udelay(5); /* The delays are Xircom recommended to give the
556 * chipset time to reset the actual hardware
557 * on the PCMCIA card
558 */
559 outl(0xa8050000, ioaddr + CSR15);
560 udelay(5);
561 outl(0xa00f0000, ioaddr + CSR15);
562 udelay(5);
563 outl_CSR6(0x32000200, ioaddr, chip_idx);
564 break;
565 }
566
567 if (register_netdev(dev)) {
568 request_region(ioaddr, tulip_tbl[chip_idx].io_size, "xircom_tulip_cb");
569 kfree(dev->priv);
570 kfree(dev);
571 return NULL;
572 }
573
574 printk(KERN_INFO "%s: %s rev %d at %#3lx,",
575 dev->name, tulip_tbl[chip_idx].chip_name, chip_rev, ioaddr);
576 for (i = 0; i < 6; i++)
577 printk("%c%2.2X", i ? ':' : ' ', dev->dev_addr[i]);
578 printk(", IRQ %d.\n", irq);
579
580 return dev;
581 }
582
583 /* MII transceiver control section.
584 Read and write the MII registers using software-generated serial
585 MDIO protocol. See the MII specifications or DP83840A data sheet
586 for details. */
587
588 /* The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
589 met by back-to-back PCI I/O cycles, but we insert a delay to avoid
590 "overclocking" issues or future 66Mhz PCI. */
591 #define mdio_delay() inl(mdio_addr)
592
593 /* Read and write the MII registers using software-generated serial
594 MDIO protocol. It is just different enough from the EEPROM protocol
595 to not share code. The maxium data clock rate is 2.5 Mhz. */
596 #define MDIO_SHIFT_CLK 0x10000
597 #define MDIO_DATA_WRITE0 0x00000
598 #define MDIO_DATA_WRITE1 0x20000
599 #define MDIO_ENB 0x00000 /* Ignore the 0x02000 databook setting. */
600 #define MDIO_ENB_IN 0x40000
601 #define MDIO_DATA_READ 0x80000
602
603 static int mdio_read(struct net_device *dev, int phy_id, int location)
604 {
605 int i;
606 int read_cmd = (0xf6 << 10) | (phy_id << 5) | location;
607 int retval = 0;
608 long ioaddr = dev->base_addr;
609 long mdio_addr = ioaddr + CSR9;
610
611 /* Establish sync by sending at least 32 logic ones. */
612 for (i = 32; i >= 0; i--) {
613 outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
614 mdio_delay();
615 outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
616 mdio_delay();
617 }
618 /* Shift the read command bits out. */
619 for (i = 15; i >= 0; i--) {
620 int dataval = (read_cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
621
622 outl(MDIO_ENB | dataval, mdio_addr);
623 mdio_delay();
624 outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
625 mdio_delay();
626 }
627 /* Read the two transition, 16 data, and wire-idle bits. */
628 for (i = 19; i > 0; i--) {
629 outl(MDIO_ENB_IN, mdio_addr);
630 mdio_delay();
631 retval = (retval << 1) | ((inl(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
632 outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
633 mdio_delay();
634 }
635 return (retval>>1) & 0xffff;
636 }
637
638 static void mdio_write(struct net_device *dev, int phy_id, int location, int value)
639 {
640 int i;
641 int cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
642 long ioaddr = dev->base_addr;
643 long mdio_addr = ioaddr + CSR9;
644
645 /* Establish sync by sending 32 logic ones. */
646 for (i = 32; i >= 0; i--) {
647 outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
648 mdio_delay();
649 outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
650 mdio_delay();
651 }
652 /* Shift the command bits out. */
653 for (i = 31; i >= 0; i--) {
654 int dataval = (cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
655 outl(MDIO_ENB | dataval, mdio_addr);
656 mdio_delay();
657 outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
658 mdio_delay();
659 }
660 /* Clear out extra bits. */
661 for (i = 2; i > 0; i--) {
662 outl(MDIO_ENB_IN, mdio_addr);
663 mdio_delay();
664 outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
665 mdio_delay();
666 }
667 return;
668 }
669
670 static void
671 tulip_up(struct net_device *dev)
672 {
673 struct tulip_private *tp = (struct tulip_private *)dev->priv;
674 long ioaddr = dev->base_addr;
675 int i;
676
677 /* On some chip revs we must set the MII/SYM port before the reset!? */
678 if (tp->mii_cnt)
679 outl_CSR6(0x00040000, ioaddr, tp->chip_id);
680
681 /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
682 outl(0x00000001, ioaddr + CSR0);
683
684 /* Deassert reset. */
685 outl(tp->csr0, ioaddr + CSR0);
686 udelay(2);
687
688 if (tulip_tbl[tp->chip_id].flags & HAS_ACPI)
689 pci_write_config_dword(tp->pdev, 0x40, 0x00000000);
690
691 /* Clear the tx ring */
692 for (i = 0; i < TX_RING_SIZE; i++) {
693 tp->tx_skbuff[i] = 0;
694 tp->tx_ring[i].status = 0x00000000;
695 }
696
697 if (tulip_debug > 1)
698 printk(KERN_DEBUG "%s: tulip_open() irq %d.\n", dev->name, dev->irq);
699
700 { /* X3201_3 */
701 u16 *eaddrs = (u16 *)dev->dev_addr;
702 u16 *setup_frm = &tp->setup_frame[0*6];
703
704 /* fill the table with the broadcast address */
705 memset(tp->setup_frame, 0xff, 96*sizeof(u16));
706 /* re-fill the first 14 table entries with our address */
707 for(i=0; i<14; i++) {
708 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
709 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
710 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
711 }
712
713 /* Put the setup frame on the Tx list. */
714 tp->tx_ring[tp->cur_tx].length = 0x68000000 | 192;
715 /* Lie about the address of our setup frame to make the */
716 /* chip happy */
717 tp->tx_ring[tp->cur_tx].buffer1 = virt_to_bus(tp->setup_frame);
718 tp->tx_ring[tp->cur_tx].status = DescOwned;
719
720 tp->cur_tx++;
721 }
722 outl(virt_to_bus(tp->rx_ring), ioaddr + CSR3);
723 outl(virt_to_bus(tp->tx_ring), ioaddr + CSR4);
724
725 tp->saved_if_port = dev->if_port;
726 if (dev->if_port == 0)
727 dev->if_port = tp->default_port;
728
729 /* Allow selecting a default media. */
730 tp->csr6 = 0;
731 if (tp->chip_id == X3201_3) {
732 outl(0x0008, ioaddr + CSR15);
733 udelay(5);
734 outl(0xa8050000, ioaddr + CSR15);
735 udelay(5);
736 outl(0xa00f0000, ioaddr + CSR15);
737 udelay(5);
738 tp->csr6 = 0x32400000;
739 }
740 /* Start the chip's Tx to process setup frame. */
741 outl_CSR6(tp->csr6, ioaddr, tp->chip_id);
742 outl_CSR6(tp->csr6 | 0x2000, ioaddr, tp->chip_id);
743
744 /* Enable interrupts by setting the interrupt mask. */
745 outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR5);
746 outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR7);
747 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
748 outl(0, ioaddr + CSR2); /* Rx poll demand */
749
750 netif_start_queue (dev);
751
752 if (tulip_debug > 2) {
753 printk(KERN_DEBUG "%s: Done tulip_open(), CSR0 %8.8x, CSR5 %8.8x CSR6 %8.8x.\n",
754 dev->name, inl(ioaddr + CSR0), inl(ioaddr + CSR5),
755 inl(ioaddr + CSR6));
756 }
757 /* Set the timer to switch to check for link beat and perhaps switch
758 to an alternate media type. */
759 init_timer(&tp->timer);
760 tp->timer.expires = RUN_AT(5*HZ);
761 tp->timer.data = (unsigned long)dev;
762 tp->timer.function = tulip_tbl[tp->chip_id].media_timer;
763 add_timer(&tp->timer);
764 }
765
766 static int
767 tulip_open(struct net_device *dev)
768 {
769 struct tulip_private *tp = (struct tulip_private *)dev->priv;
770
771 if (request_irq(dev->irq, &tulip_interrupt, SA_SHIRQ, dev->name, dev))
772 return -EAGAIN;
773
774 tulip_init_ring(dev);
775
776 tulip_up(dev);
777 tp->open = 1;
778 MOD_INC_USE_COUNT;
779
780 return 0;
781 }
782
783 /*
784 Check the MII negotiated duplex, and change the CSR6 setting if
785 required.
786 Return 0 if everything is OK.
787 Return < 0 if the transceiver is missing or has no link beat.
788 */
789 #if 0
790 static int check_duplex(struct net_device *dev)
791 {
792 long ioaddr = dev->base_addr;
793 struct tulip_private *tp = (struct tulip_private *)dev->priv;
794 int mii_reg1, mii_reg5, negotiated, duplex;
795
796 if (tp->full_duplex_lock)
797 return 0;
798 mii_reg1 = mdio_read(dev, tp->phys[0], 1);
799 mii_reg5 = mdio_read(dev, tp->phys[0], 5);
800 if (tulip_debug > 1)
801 printk(KERN_INFO "%s: MII status %4.4x, Link partner report "
802 "%4.4x.\n", dev->name, mii_reg1, mii_reg5);
803 if (mii_reg1 == 0xffff)
804 return -2;
805 if ((mii_reg1 & 0x0004) == 0) {
806 int new_reg1 = mdio_read(dev, tp->phys[0], 1);
807 if ((new_reg1 & 0x0004) == 0) {
808 if (tulip_debug > 1)
809 printk(KERN_INFO "%s: No link beat on the MII interface,"
810 " status %4.4x.\n", dev->name, new_reg1);
811 return -1;
812 }
813 }
814 negotiated = mii_reg5 & tp->advertising[0];
815 duplex = ((negotiated & 0x0300) == 0x0100
816 || (negotiated & 0x00C0) == 0x0040);
817 /* 100baseTx-FD or 10T-FD, but not 100-HD */
818 if (tp->full_duplex != duplex) {
819 tp->full_duplex = duplex;
820 if (tp->full_duplex) tp->csr6 |= 0x0200;
821 else tp->csr6 &= ~0x0200;
822 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
823 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
824 if (tulip_debug > 0)
825 printk(KERN_INFO "%s: Setting %s-duplex based on MII"
826 "#%d link partner capability of %4.4x.\n",
827 dev->name, tp->full_duplex ? "full" : "half",
828 tp->phys[0], mii_reg5);
829 }
830 return 0;
831 }
832 #endif
833
834 static void tulip_timer(unsigned long data)
835 {
836 struct net_device *dev = (struct net_device *)data;
837 struct tulip_private *tp = (struct tulip_private *)dev->priv;
838 long ioaddr = dev->base_addr;
839 u32 csr12 = inl(ioaddr + CSR12);
840 int next_tick = 2*HZ;
841
842 if (tulip_debug > 2) {
843 printk(KERN_DEBUG "%s: Media selection tick, status %8.8x mode %8.8x "
844 "SIA %8.8x %8.8x %8.8x %8.8x.\n",
845 dev->name, inl(ioaddr + CSR5), inl(ioaddr + CSR6),
846 csr12, inl(ioaddr + CSR13),
847 inl(ioaddr + CSR14), inl(ioaddr + CSR15));
848 }
849
850 /* Not much that can be done.
851 Assume this a generic MII or SYM transceiver. */
852 next_tick = 60*HZ;
853 if (tulip_debug > 2)
854 printk(KERN_DEBUG "%s: network media monitor CSR6 %8.8x "
855 "CSR12 0x%2.2x.\n",
856 dev->name, inl(ioaddr + CSR6), csr12 & 0xff);
857
858 tp->timer.expires = RUN_AT(next_tick);
859 add_timer(&tp->timer);
860 }
861
862 static void tulip_tx_timeout(struct net_device *dev)
863 {
864 struct tulip_private *tp = (struct tulip_private *)dev->priv;
865 long ioaddr = dev->base_addr;
866
867 if (media_cap[dev->if_port] & MediaIsMII) {
868 /* Do nothing -- the media monitor should handle this. */
869 if (tulip_debug > 1)
870 printk(KERN_WARNING "%s: Transmit timeout using MII device.\n",
871 dev->name);
872 }
873
874 #if defined(way_too_many_messages)
875 if (tulip_debug > 3) {
876 int i;
877 for (i = 0; i < RX_RING_SIZE; i++) {
878 u8 *buf = (u8 *)(tp->rx_ring[i].buffer1);
879 int j;
880 printk(KERN_DEBUG "%2d: %8.8x %8.8x %8.8x %8.8x "
881 "%2.2x %2.2x %2.2x.\n",
882 i, (unsigned int)tp->rx_ring[i].status,
883 (unsigned int)tp->rx_ring[i].length,
884 (unsigned int)tp->rx_ring[i].buffer1,
885 (unsigned int)tp->rx_ring[i].buffer2,
886 buf[0], buf[1], buf[2]);
887 for (j = 0; buf[j] != 0xee && j < 1600; j++)
888 if (j < 100) printk(" %2.2x", buf[j]);
889 printk(" j=%d.\n", j);
890 }
891 printk(KERN_DEBUG " Rx ring %8.8x: ", (int)tp->rx_ring);
892 for (i = 0; i < RX_RING_SIZE; i++)
893 printk(" %8.8x", (unsigned int)tp->rx_ring[i].status);
894 printk("\n" KERN_DEBUG " Tx ring %8.8x: ", (int)tp->tx_ring);
895 for (i = 0; i < TX_RING_SIZE; i++)
896 printk(" %8.8x", (unsigned int)tp->tx_ring[i].status);
897 printk("\n");
898 }
899 #endif
900
901 /* Stop and restart the chip's Tx processes . */
902 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
903 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
904 /* Trigger an immediate transmit demand. */
905 outl(0, ioaddr + CSR1);
906
907 dev->trans_start = jiffies;
908 netif_wake_queue (dev);
909 tp->stats.tx_errors++;
910 }
911
912 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
913 static void tulip_init_ring(struct net_device *dev)
914 {
915 struct tulip_private *tp = (struct tulip_private *)dev->priv;
916 int i;
917
918 tp->tx_full = 0;
919 tp->cur_rx = tp->cur_tx = 0;
920 tp->dirty_rx = tp->dirty_tx = 0;
921
922 for (i = 0; i < RX_RING_SIZE; i++) {
923 tp->rx_ring[i].status = 0x00000000;
924 tp->rx_ring[i].length = PKT_BUF_SZ;
925 tp->rx_ring[i].buffer2 = virt_to_bus(&tp->rx_ring[i+1]);
926 tp->rx_skbuff[i] = NULL;
927 }
928 /* Mark the last entry as wrapping the ring. */
929 tp->rx_ring[i-1].length = PKT_BUF_SZ | DESC_RING_WRAP;
930 tp->rx_ring[i-1].buffer2 = virt_to_bus(&tp->rx_ring[0]);
931
932 for (i = 0; i < RX_RING_SIZE; i++) {
933 /* Note the receive buffer must be longword aligned.
934 dev_alloc_skb() provides 16 byte alignment. But do *not*
935 use skb_reserve() to align the IP header! */
936 struct sk_buff *skb = dev_alloc_skb(PKT_BUF_SZ);
937 tp->rx_skbuff[i] = skb;
938 if (skb == NULL)
939 break;
940 skb->dev = dev; /* Mark as being used by this device. */
941 tp->rx_ring[i].status = DescOwned; /* Owned by Tulip chip */
942 tp->rx_ring[i].buffer1 = virt_to_bus(skb->tail);
943 }
944 tp->dirty_rx = (unsigned int)(i - RX_RING_SIZE);
945
946 /* The Tx buffer descriptor is filled in as needed, but we
947 do need to clear the ownership bit. */
948 for (i = 0; i < TX_RING_SIZE; i++) {
949 tp->tx_skbuff[i] = 0;
950 tp->tx_ring[i].status = 0x00000000;
951 tp->tx_ring[i].buffer2 = virt_to_bus(&tp->tx_ring[i+1]);
952 #ifdef CARDBUS
953 if (tp->chip_id == X3201_3)
954 tp->tx_aligned_skbuff[i] = dev_alloc_skb(PKT_BUF_SZ);
955 #endif /* CARDBUS */
956 }
957 tp->tx_ring[i-1].buffer2 = virt_to_bus(&tp->tx_ring[0]);
958 }
959
960 static int
961 tulip_start_xmit(struct sk_buff *skb, struct net_device *dev)
962 {
963 struct tulip_private *tp = (struct tulip_private *)dev->priv;
964 int entry;
965 u32 flag;
966
967 /* Caution: the write order is important here, set the base address
968 with the "ownership" bits last. */
969
970 /* Calculate the next Tx descriptor entry. */
971 entry = tp->cur_tx % TX_RING_SIZE;
972
973 tp->tx_skbuff[entry] = skb;
974 #ifdef CARDBUS
975 if (tp->chip_id == X3201_3) {
976 memcpy(tp->tx_aligned_skbuff[entry]->data,skb->data,skb->len);
977 tp->tx_ring[entry].buffer1 = virt_to_bus(tp->tx_aligned_skbuff[entry]->data);
978 } else
979 #endif
980 tp->tx_ring[entry].buffer1 = virt_to_bus(skb->data);
981
982 if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE/2) {/* Typical path */
983 flag = 0x60000000; /* No interrupt */
984 } else if (tp->cur_tx - tp->dirty_tx == TX_RING_SIZE/2) {
985 flag = 0xe0000000; /* Tx-done intr. */
986 } else if (tp->cur_tx - tp->dirty_tx < TX_RING_SIZE - 2) {
987 flag = 0x60000000; /* No Tx-done intr. */
988 } else {
989 /* Leave room for set_rx_mode() to fill entries. */
990 flag = 0xe0000000; /* Tx-done intr. */
991 tp->tx_full = 1;
992 }
993 if (entry == TX_RING_SIZE-1)
994 flag |= 0xe0000000 | DESC_RING_WRAP;
995
996 tp->tx_ring[entry].length = skb->len | flag;
997 tp->tx_ring[entry].status = DescOwned; /* Pass ownership to the chip. */
998 tp->cur_tx++;
999 if (tp->tx_full)
1000 netif_stop_queue (dev);
1001 else
1002 netif_wake_queue (dev);
1003
1004 /* Trigger an immediate transmit demand. */
1005 outl(0, dev->base_addr + CSR1);
1006
1007 dev->trans_start = jiffies;
1008
1009 return 0;
1010 }
1011
1012 /* The interrupt handler does all of the Rx thread work and cleans up
1013 after the Tx thread. */
1014 static void tulip_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1015 {
1016 struct net_device *dev = (struct net_device *)dev_instance;
1017 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1018 long ioaddr = dev->base_addr;
1019 int csr5, work_budget = max_interrupt_work;
1020
1021 spin_lock (&tp->lock);
1022
1023 do {
1024 csr5 = inl(ioaddr + CSR5);
1025 /* Acknowledge all of the current interrupt sources ASAP. */
1026 outl(csr5 & 0x0001ffff, ioaddr + CSR5);
1027
1028 if (tulip_debug > 4)
1029 printk(KERN_DEBUG "%s: interrupt csr5=%#8.8x new csr5=%#8.8x.\n",
1030 dev->name, csr5, inl(dev->base_addr + CSR5));
1031
1032 if (csr5 == 0xffffffff)
1033 break; /* all bits set, assume PCMCIA card removed */
1034
1035 if ((csr5 & (NormalIntr|AbnormalIntr)) == 0)
1036 break;
1037
1038 if (csr5 & (RxIntr | RxNoBuf))
1039 work_budget -= tulip_rx(dev);
1040
1041 if (csr5 & (TxNoBuf | TxDied | TxIntr)) {
1042 unsigned int dirty_tx;
1043
1044 for (dirty_tx = tp->dirty_tx; tp->cur_tx - dirty_tx > 0;
1045 dirty_tx++) {
1046 int entry = dirty_tx % TX_RING_SIZE;
1047 int status = tp->tx_ring[entry].status;
1048
1049 if (status < 0)
1050 break; /* It still hasn't been Txed */
1051 /* Check for Rx filter setup frames. */
1052 if (tp->tx_skbuff[entry] == NULL)
1053 continue;
1054
1055 if (status & 0x8000) {
1056 /* There was an major error, log it. */
1057 #ifndef final_version
1058 if (tulip_debug > 1)
1059 printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n",
1060 dev->name, status);
1061 #endif
1062 tp->stats.tx_errors++;
1063 if (status & 0x4104) tp->stats.tx_aborted_errors++;
1064 if (status & 0x0C00) tp->stats.tx_carrier_errors++;
1065 if (status & 0x0200) tp->stats.tx_window_errors++;
1066 if (status & 0x0002) tp->stats.tx_fifo_errors++;
1067 if ((status & 0x0080) && tp->full_duplex == 0)
1068 tp->stats.tx_heartbeat_errors++;
1069 #ifdef ETHER_STATS
1070 if (status & 0x0100) tp->stats.collisions16++;
1071 #endif
1072 } else {
1073 #ifdef ETHER_STATS
1074 if (status & 0x0001) tp->stats.tx_deferred++;
1075 #endif
1076 tp->stats.tx_bytes += tp->tx_ring[entry].length & 0x7ff;
1077 tp->stats.collisions += (status >> 3) & 15;
1078 tp->stats.tx_packets++;
1079 }
1080
1081 /* Free the original skb. */
1082 dev_kfree_skb_irq(tp->tx_skbuff[entry]);
1083 tp->tx_skbuff[entry] = 0;
1084 }
1085
1086 #ifndef final_version
1087 if (tp->cur_tx - dirty_tx > TX_RING_SIZE) {
1088 printk(KERN_ERR "%s: Out-of-sync dirty pointer, %d vs. %d, full=%d.\n",
1089 dev->name, dirty_tx, tp->cur_tx, tp->tx_full);
1090 dirty_tx += TX_RING_SIZE;
1091 }
1092 #endif
1093
1094 if (tp->tx_full &&
1095 tp->cur_tx - dirty_tx < TX_RING_SIZE - 2)
1096 /* The ring is no longer full */
1097 tp->tx_full = 0;
1098
1099 if (tp->tx_full)
1100 netif_stop_queue (dev);
1101 else
1102 netif_wake_queue (dev);
1103
1104 tp->dirty_tx = dirty_tx;
1105 if (csr5 & TxDied) {
1106 if (tulip_debug > 2)
1107 printk(KERN_WARNING "%s: The transmitter stopped."
1108 " CSR5 is %x, CSR6 %x, new CSR6 %x.\n",
1109 dev->name, csr5, inl(ioaddr + CSR6), tp->csr6);
1110 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
1111 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
1112 }
1113 }
1114
1115 /* Log errors. */
1116 if (csr5 & AbnormalIntr) { /* Abnormal error summary bit. */
1117 if (csr5 == 0xffffffff)
1118 break;
1119 if (csr5 & TxJabber) tp->stats.tx_errors++;
1120 if (csr5 & TxFIFOUnderflow) {
1121 if ((tp->csr6 & 0xC000) != 0xC000)
1122 tp->csr6 += 0x4000; /* Bump up the Tx threshold */
1123 else
1124 tp->csr6 |= 0x00200000; /* Store-n-forward. */
1125 /* Restart the transmit process. */
1126 outl_CSR6(tp->csr6 | 0x0002, ioaddr, tp->chip_id);
1127 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
1128 }
1129 if (csr5 & RxDied) { /* Missed a Rx frame. */
1130 tp->stats.rx_errors++;
1131 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
1132 outl_CSR6(tp->csr6 | 0x2002, ioaddr, tp->chip_id);
1133 }
1134 if (csr5 & TimerInt) {
1135 if (tulip_debug > 2)
1136 printk(KERN_ERR "%s: Re-enabling interrupts, %8.8x.\n",
1137 dev->name, csr5);
1138 outl(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR7);
1139 }
1140 /* Clear all error sources, included undocumented ones! */
1141 outl(0x0800f7ba, ioaddr + CSR5);
1142 }
1143 if (--work_budget < 0) {
1144 if (tulip_debug > 1)
1145 printk(KERN_WARNING "%s: Too much work during an interrupt, "
1146 "csr5=0x%8.8x.\n", dev->name, csr5);
1147 /* Acknowledge all interrupt sources. */
1148 outl(0x8001ffff, ioaddr + CSR5);
1149 #ifdef notdef
1150 /* Clear all but standard interrupt sources. */
1151 outl((~csr5) & 0x0001ebef, ioaddr + CSR7);
1152 #endif
1153 break;
1154 }
1155 } while (1);
1156
1157 if (tulip_debug > 3)
1158 printk(KERN_DEBUG "%s: exiting interrupt, csr5=%#4.4x.\n",
1159 dev->name, inl(ioaddr + CSR5));
1160
1161 spin_unlock (&tp->lock);
1162 }
1163
1164 static int
1165 tulip_rx(struct net_device *dev)
1166 {
1167 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1168 int entry = tp->cur_rx % RX_RING_SIZE;
1169 int rx_work_limit = tp->dirty_rx + RX_RING_SIZE - tp->cur_rx;
1170 int work_done = 0;
1171
1172 if (tulip_debug > 4)
1173 printk(KERN_DEBUG " In tulip_rx(), entry %d %8.8x.\n", entry,
1174 tp->rx_ring[entry].status);
1175 /* If we own the next entry, it's a new packet. Send it up. */
1176 while (tp->rx_ring[entry].status >= 0) {
1177 s32 status = tp->rx_ring[entry].status;
1178
1179 if (tulip_debug > 5)
1180 printk(KERN_DEBUG " In tulip_rx(), entry %d %8.8x.\n", entry,
1181 tp->rx_ring[entry].status);
1182 if (--rx_work_limit < 0)
1183 break;
1184 if ((status & 0x38008300) != 0x0300) {
1185 if ((status & 0x38000300) != 0x0300) {
1186 /* Ingore earlier buffers. */
1187 if ((status & 0xffff) != 0x7fff) {
1188 if (tulip_debug > 1)
1189 printk(KERN_WARNING "%s: Oversized Ethernet frame "
1190 "spanned multiple buffers, status %8.8x!\n",
1191 dev->name, status);
1192 tp->stats.rx_length_errors++;
1193 }
1194 } else if (status & RxDescFatalErr) {
1195 /* There was a fatal error. */
1196 if (tulip_debug > 2)
1197 printk(KERN_DEBUG "%s: Receive error, Rx status %8.8x.\n",
1198 dev->name, status);
1199 tp->stats.rx_errors++; /* end of a packet.*/
1200 if (status & 0x0890) tp->stats.rx_length_errors++;
1201 if (status & 0x0004) tp->stats.rx_frame_errors++;
1202 if (status & 0x0002) tp->stats.rx_crc_errors++;
1203 if (status & 0x0001) tp->stats.rx_fifo_errors++;
1204 }
1205 } else {
1206 /* Omit the four octet CRC from the length. */
1207 short pkt_len = ((status >> 16) & 0x7ff) - 4;
1208 struct sk_buff *skb;
1209
1210 #ifndef final_version
1211 if (pkt_len > 1518) {
1212 printk(KERN_WARNING "%s: Bogus packet size of %d (%#x).\n",
1213 dev->name, pkt_len, pkt_len);
1214 pkt_len = 1518;
1215 tp->stats.rx_length_errors++;
1216 }
1217 #endif
1218 /* Check if the packet is long enough to accept without copying
1219 to a minimally-sized skbuff. */
1220 if (pkt_len < rx_copybreak
1221 && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
1222 skb->dev = dev;
1223 skb_reserve(skb, 2); /* 16 byte align the IP header */
1224 #if ! defined(__alpha__)
1225 eth_copy_and_sum(skb, bus_to_virt(tp->rx_ring[entry].buffer1),
1226 pkt_len, 0);
1227 skb_put(skb, pkt_len);
1228 #else
1229 memcpy(skb_put(skb, pkt_len),
1230 bus_to_virt(tp->rx_ring[entry].buffer1), pkt_len);
1231 #endif
1232 work_done++;
1233 } else { /* Pass up the skb already on the Rx ring. */
1234 char *temp = skb_put(skb = tp->rx_skbuff[entry], pkt_len);
1235 tp->rx_skbuff[entry] = NULL;
1236 #ifndef final_version
1237 if (bus_to_virt(tp->rx_ring[entry].buffer1) != temp)
1238 printk(KERN_ERR "%s: Internal fault: The skbuff addresses "
1239 "do not match in tulip_rx: %p vs. %p / %p.\n",
1240 dev->name, bus_to_virt(tp->rx_ring[entry].buffer1),
1241 skb->head, temp);
1242 #endif
1243 }
1244 skb->protocol = eth_type_trans(skb, dev);
1245 netif_rx(skb);
1246 dev->last_rx = jiffies;
1247 tp->stats.rx_packets++;
1248 tp->stats.rx_bytes += pkt_len;
1249 }
1250 entry = (++tp->cur_rx) % RX_RING_SIZE;
1251 }
1252
1253 /* Refill the Rx ring buffers. */
1254 for (; tp->cur_rx - tp->dirty_rx > 0; tp->dirty_rx++) {
1255 entry = tp->dirty_rx % RX_RING_SIZE;
1256 if (tp->rx_skbuff[entry] == NULL) {
1257 struct sk_buff *skb;
1258 skb = tp->rx_skbuff[entry] = dev_alloc_skb(PKT_BUF_SZ);
1259 if (skb == NULL)
1260 break;
1261 skb->dev = dev; /* Mark as being used by this device. */
1262 tp->rx_ring[entry].buffer1 = virt_to_bus(skb->tail);
1263 work_done++;
1264 }
1265 tp->rx_ring[entry].status = DescOwned;
1266 }
1267
1268 return work_done;
1269 }
1270
1271 static void
1272 tulip_down(struct net_device *dev)
1273 {
1274 long ioaddr = dev->base_addr;
1275 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1276
1277 del_timer_sync(&tp->timer);
1278
1279 /* Disable interrupts by clearing the interrupt mask. */
1280 outl(0x00000000, ioaddr + CSR7);
1281 /* Stop the chip's Tx and Rx processes. */
1282 outl_CSR6(inl(ioaddr + CSR6) & ~0x2002, ioaddr, tp->chip_id);
1283
1284 if (inl(ioaddr + CSR6) != 0xffffffff)
1285 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
1286
1287 dev->if_port = tp->saved_if_port;
1288 }
1289
1290 static int
1291 tulip_close(struct net_device *dev)
1292 {
1293 long ioaddr = dev->base_addr;
1294 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1295 int i;
1296
1297 if (tulip_debug > 1)
1298 printk(KERN_DEBUG "%s: Shutting down ethercard, status was %2.2x.\n",
1299 dev->name, inl(ioaddr + CSR5));
1300
1301 del_timer_sync(&tp->timer);
1302
1303 netif_stop_queue(dev);
1304
1305 if (netif_device_present(dev))
1306 tulip_down(dev);
1307
1308 free_irq(dev->irq, dev);
1309
1310 /* Free all the skbuffs in the Rx queue. */
1311 for (i = 0; i < RX_RING_SIZE; i++) {
1312 struct sk_buff *skb = tp->rx_skbuff[i];
1313 tp->rx_skbuff[i] = 0;
1314 tp->rx_ring[i].status = 0; /* Not owned by Tulip chip. */
1315 tp->rx_ring[i].length = 0;
1316 tp->rx_ring[i].buffer1 = 0xBADF00D0; /* An invalid address. */
1317 if (skb) {
1318 dev_kfree_skb(skb);
1319 }
1320 }
1321 for (i = 0; i < TX_RING_SIZE; i++) {
1322 if (tp->tx_skbuff[i])
1323 dev_kfree_skb(tp->tx_skbuff[i]);
1324 tp->tx_skbuff[i] = 0;
1325 }
1326
1327 MOD_DEC_USE_COUNT;
1328 tp->open = 0;
1329 return 0;
1330 }
1331
1332 static struct net_device_stats *tulip_get_stats(struct net_device *dev)
1333 {
1334 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1335 long ioaddr = dev->base_addr;
1336
1337 if (netif_device_present(dev))
1338 tp->stats.rx_missed_errors += inl(ioaddr + CSR8) & 0xffff;
1339
1340 return &tp->stats;
1341 }
1342
1343 #ifdef HAVE_PRIVATE_IOCTL
1344 /* Provide ioctl() calls to examine the MII xcvr state. */
1345 static int private_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1346 {
1347 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1348 u16 *data = (u16 *)&rq->ifr_data;
1349 int phy = tp->phys[0] & 0x1f;
1350 long flags;
1351
1352 switch(cmd) {
1353 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
1354 if (tp->mii_cnt)
1355 data[0] = phy;
1356 else
1357 return -ENODEV;
1358 return 0;
1359 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
1360 {
1361 save_flags(flags);
1362 cli();
1363 data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f);
1364 restore_flags(flags);
1365 }
1366 return 0;
1367 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
1368 #if defined(CAP_NET_ADMIN)
1369 if (!capable(CAP_NET_ADMIN))
1370 return -EPERM;
1371 #else
1372 if (!suser())
1373 return -EPERM;
1374 #endif
1375 {
1376 save_flags(flags);
1377 cli();
1378 mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]);
1379 restore_flags(flags);
1380 }
1381 return 0;
1382 default:
1383 return -EOPNOTSUPP;
1384 }
1385
1386 return -EOPNOTSUPP;
1387 }
1388 #endif /* HAVE_PRIVATE_IOCTL */
1389
1390 /* Set or clear the multicast filter for this adaptor.
1391 Note that we only use exclusion around actually queueing the
1392 new frame, not around filling tp->setup_frame. This is non-deterministic
1393 when re-entered but still correct. */
1394
1395 /* The little-endian AUTODIN32 ethernet CRC calculation.
1396 N.B. Do not use for bulk data, use a table-based routine instead.
1397 This is common code and should be moved to net/core/crc.c */
1398 static unsigned const ethernet_polynomial_le = 0xedb88320U;
1399 static inline u32 ether_crc_le(int length, unsigned char *data)
1400 {
1401 u32 crc = 0xffffffff; /* Initial value. */
1402 while(--length >= 0) {
1403 unsigned char current_octet = *data++;
1404 int bit;
1405 for (bit = 8; --bit >= 0; current_octet >>= 1) {
1406 if ((crc ^ current_octet) & 1) {
1407 crc >>= 1;
1408 crc ^= ethernet_polynomial_le;
1409 } else
1410 crc >>= 1;
1411 }
1412 }
1413 return crc;
1414 }
1415 static unsigned const ethernet_polynomial = 0x04c11db7U;
1416 static inline u32 ether_crc(int length, unsigned char *data)
1417 {
1418 int crc = -1;
1419
1420 while(--length >= 0) {
1421 unsigned char current_octet = *data++;
1422 int bit;
1423 for (bit = 0; bit < 8; bit++, current_octet >>= 1)
1424 crc = (crc << 1) ^
1425 ((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
1426 }
1427 return crc;
1428 }
1429
1430 static void set_rx_mode(struct net_device *dev)
1431 {
1432 long ioaddr = dev->base_addr;
1433 int csr6 = inl(ioaddr + CSR6) & ~0x00D5;
1434 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1435
1436 tp->csr6 &= ~0x00D5;
1437 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
1438 tp->csr6 |= 0x00C0;
1439 csr6 |= 0x00C0;
1440 /* Unconditionally log net taps. */
1441 printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
1442 } else if ((dev->mc_count > 1000) || (dev->flags & IFF_ALLMULTI)) {
1443 /* Too many to filter well -- accept all multicasts. */
1444 tp->csr6 |= 0x0080;
1445 csr6 |= 0x0080;
1446 } else {
1447 u16 *eaddrs, *setup_frm = tp->setup_frame;
1448 struct dev_mc_list *mclist;
1449 u32 tx_flags = 0x68000000 | 192;
1450 int i;
1451
1452 /* Note that only the low-address shortword of setup_frame is valid!
1453 The values are doubled for big-endian architectures. */
1454 if (dev->mc_count > 14) { /* Must use a multicast hash table. */
1455 u16 hash_table[32];
1456 tx_flags = 0x68400000 | 192; /* Use hash filter. */
1457 memset(hash_table, 0, sizeof(hash_table));
1458 set_bit(255, hash_table); /* Broadcast entry */
1459 /* This should work on big-endian machines as well. */
1460 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
1461 i++, mclist = mclist->next)
1462 set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff,
1463 hash_table);
1464 for (i = 0; i < 32; i++) {
1465 *setup_frm++ = hash_table[i];
1466 *setup_frm++ = hash_table[i];
1467 }
1468 setup_frm = &tp->setup_frame[13*6];
1469 } else {
1470 /* We have <= 14 addresses so we can use the wonderful
1471 16 address perfect filtering of the Tulip. */
1472 for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
1473 i++, mclist = mclist->next) {
1474 eaddrs = (u16 *)mclist->dmi_addr;
1475 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
1476 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
1477 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
1478 }
1479 /* Fill the unused entries with the broadcast address. */
1480 memset(setup_frm, 0xff, (15-i)*12);
1481 setup_frm = &tp->setup_frame[15*6];
1482 }
1483
1484 /* Fill the final entry with our physical address. */
1485 eaddrs = (u16 *)dev->dev_addr;
1486 *setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
1487 *setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
1488 *setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
1489 /* Now add this frame to the Tx list. */
1490 if (tp->cur_tx - tp->dirty_tx > TX_RING_SIZE - 2) {
1491 /* Same setup recently queued, we need not add it. */
1492 } else {
1493 unsigned long flags;
1494 unsigned int entry;
1495 int dummy = -1;
1496
1497 save_flags(flags); cli();
1498 entry = tp->cur_tx++ % TX_RING_SIZE;
1499
1500 if (entry != 0) {
1501 /* Avoid a chip errata by prefixing a dummy entry. */
1502 tp->tx_skbuff[entry] = 0;
1503 tp->tx_ring[entry].length =
1504 (entry == TX_RING_SIZE-1) ? DESC_RING_WRAP : 0;
1505 tp->tx_ring[entry].buffer1 = 0;
1506 /* race with chip, set DescOwned later */
1507 dummy = entry;
1508 entry = tp->cur_tx++ % TX_RING_SIZE;
1509 }
1510
1511 tp->tx_skbuff[entry] = 0;
1512 /* Put the setup frame on the Tx list. */
1513 if (entry == TX_RING_SIZE-1)
1514 tx_flags |= DESC_RING_WRAP; /* Wrap ring. */
1515 tp->tx_ring[entry].length = tx_flags;
1516 tp->tx_ring[entry].buffer1 = virt_to_bus(tp->setup_frame);
1517 tp->tx_ring[entry].status = DescOwned;
1518 if (tp->cur_tx - tp->dirty_tx >= TX_RING_SIZE - 2) {
1519 tp->tx_full = 1;
1520 netif_stop_queue (dev);
1521 }
1522 if (dummy >= 0)
1523 tp->tx_ring[dummy].status = DescOwned;
1524 restore_flags(flags);
1525 /* Trigger an immediate transmit demand. */
1526 outl(0, ioaddr + CSR1);
1527 }
1528 }
1529 outl_CSR6(csr6 | 0x0000, ioaddr, tp->chip_id);
1530 }
1531
1532 static struct pci_device_id tulip_pci_table[] __devinitdata = {
1533 { 0x115D, 0x0003, PCI_ANY_ID, PCI_ANY_ID, 0, 0, X3201_3 },
1534 {0},
1535 };
1536
1537 MODULE_DEVICE_TABLE(pci, tulip_pci_table);
1538
1539 static int __devinit tulip_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1540 {
1541 struct net_device *dev;
1542 static int board_idx;
1543
1544 printk(KERN_INFO "tulip_attach(%s)\n", pdev->slot_name);
1545
1546 if (pci_enable_device (pdev))
1547 return -ENODEV;
1548 pci_set_master (pdev);
1549 dev = tulip_probe1(pdev, pci_resource_start (pdev, 0), pdev->irq,
1550 id->driver_data, board_idx++);
1551 if (dev) {
1552 pdev->driver_data = dev;
1553 return 0;
1554 }
1555 return -ENODEV;
1556 }
1557
1558 static int tulip_suspend(struct pci_dev *pdev, u32 state)
1559 {
1560 struct net_device *dev = pdev->driver_data;
1561 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1562 printk(KERN_INFO "tulip_suspend(%s)\n", dev->name);
1563 if (tp->open) tulip_down(dev);
1564 return 0;
1565 }
1566
1567 static int tulip_resume(struct pci_dev *pdev)
1568 {
1569 struct net_device *dev = pdev->driver_data;
1570 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1571 printk(KERN_INFO "tulip_resume(%s)\n", dev->name);
1572 if (tp->open) tulip_up(dev);
1573 return 0;
1574 }
1575
1576 static void __devexit tulip_remove(struct pci_dev *pdev)
1577 {
1578 struct net_device *dev = pdev->driver_data;
1579 struct tulip_private *tp = (struct tulip_private *)dev->priv;
1580
1581 printk(KERN_INFO "tulip_detach(%s)\n", dev->name);
1582 unregister_netdev(dev);
1583 kfree(dev);
1584 kfree(tp);
1585 }
1586
1587 static struct pci_driver tulip_ops = {
1588 name: "tulip_cb",
1589 id_table: tulip_pci_table,
1590 probe: tulip_pci_probe,
1591 remove: tulip_remove,
1592 suspend: tulip_suspend,
1593 resume: tulip_resume
1594 };
1595
1596 static int __init tulip_init(void)
1597 {
1598 pci_register_driver(&tulip_ops);
1599 return 0;
1600 }
1601
1602 static void __exit tulip_exit(void)
1603 {
1604 pci_unregister_driver(&tulip_ops);
1605 }
1606
1607 module_init(tulip_init)
1608 module_exit(tulip_exit)
1609
1610
1611 /*
1612 * Local variables:
1613 * compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c tulip.c `[ -f /usr/include/linux/modversions.h ] && echo -DMODVERSIONS`"
1614 * cardbus-compile-command: "gcc -DCARDBUS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c tulip.c -o tulip_cb.o -I/usr/src/pcmcia-cs-3.0.9/include/"
1615 * c-indent-level: 4
1616 * c-basic-offset: 4
1617 * tab-width: 4
1618 * End:
1619 */
1620