Import 2.3.50pre1
[davej-history.git] / drivers / net / 3c515.c
blobd8a5e12af9e5db07fc4fe0f5c0206213260f1e01
1 /* 3c515.c: A 3Com ISA EtherLink XL "Corkscrew" ethernet driver for linux. */
2 /*
3 Written 1997-1998 by Donald Becker.
5 This software may be used and distributed according to the terms
6 of the GNU Public License, incorporated herein by reference.
8 This driver is for the 3Com ISA EtherLink XL "Corkscrew" 3c515 ethercard.
10 The author may be reached as becker@CESDIS.gsfc.nasa.gov, or C/O
11 Center of Excellence in Space Data and Information Sciences
12 Code 930.5, Goddard Space Flight Center, Greenbelt MD 20771
14 2/2/00- Added support for kernel-level ISAPnP
15 by Stephen Frost <sfrost@snowman.net> and Alessandro Zummo
16 Cleaned up for 2.3.x/softnet by Jeff Garzik and Alan Cox.
19 static char *version = "3c515.c:v0.99-sn 2000/02/12 becker@cesdis.gsfc.nasa.gov and others\n";
21 #define CORKSCREW 1
23 /* "Knobs" that adjust features and parameters. */
24 /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
25 Setting to > 1512 effectively disables this feature. */
26 static const int rx_copybreak = 200;
28 /* Allow setting MTU to a larger size, bypassing the normal ethernet setup. */
29 static const int mtu = 1500;
31 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
32 static int max_interrupt_work = 20;
34 /* Enable the automatic media selection code -- usually set. */
35 #define AUTOMEDIA 1
37 /* Allow the use of fragment bus master transfers instead of only
38 programmed-I/O for Vortex cards. Full-bus-master transfers are always
39 enabled by default on Boomerang cards. If VORTEX_BUS_MASTER is defined,
40 the feature may be turned on using 'options'. */
41 #define VORTEX_BUS_MASTER
43 /* A few values that may be tweaked. */
44 /* Keep the ring sizes a power of two for efficiency. */
45 #define TX_RING_SIZE 16
46 #define RX_RING_SIZE 16
47 #define PKT_BUF_SZ 1536 /* Size of each temporary Rx buffer. */
49 #include <linux/config.h>
50 #include <linux/module.h>
51 #include <linux/version.h>
52 #include <linux/isapnp.h>
54 #include <linux/kernel.h>
55 #include <linux/sched.h>
56 #include <linux/string.h>
57 #include <linux/ptrace.h>
58 #include <linux/errno.h>
59 #include <linux/in.h>
60 #include <linux/ioport.h>
61 #include <linux/malloc.h>
62 #include <linux/interrupt.h>
63 #include <linux/timer.h>
64 #include <asm/bitops.h>
65 #include <asm/io.h>
66 #include <asm/dma.h>
68 #include <linux/netdevice.h>
69 #include <linux/etherdevice.h>
70 #include <linux/skbuff.h>
72 #define NEW_MULTICAST
73 #include <linux/delay.h>
75 /* Kernel version compatibility functions. */
76 #define RUN_AT(x) (jiffies + (x))
78 #define REQUEST_IRQ(i,h,f,n, instance) request_irq(i,h,f,n, instance)
79 #define IRQ(irq, dev_id, pt_regs) (irq, dev_id, pt_regs)
81 MODULE_AUTHOR("Donald Becker <becker@cesdis.gsfc.nasa.gov>");
82 MODULE_DESCRIPTION("3Com 3c515 Corkscrew driver");
83 MODULE_PARM(debug, "i");
84 MODULE_PARM(options, "1-" __MODULE_STRING(8) "i");
85 MODULE_PARM(full_duplex, "1-" __MODULE_STRING(8) "i");
86 MODULE_PARM(rx_copybreak, "i");
87 MODULE_PARM(max_interrupt_work, "i");
89 /* "Knobs" for adjusting internal parameters. */
90 /* Put out somewhat more debugging messages. (0 - no msg, 1 minimal msgs). */
91 #define DRIVER_DEBUG 1
92 /* Some values here only for performance evaluation and path-coverage
93 debugging. */
94 static int rx_nocopy = 0, rx_copy = 0, queued_packet = 0;
96 /* Number of times to check to see if the Tx FIFO has space, used in some
97 limited cases. */
98 #define WAIT_TX_AVAIL 200
100 /* Operational parameter that usually are not changed. */
101 #define TX_TIMEOUT 40 /* Time in jiffies before concluding Tx hung */
103 /* The size here is somewhat misleading: the Corkscrew also uses the ISA
104 aliased registers at <base>+0x400.
106 #define CORKSCREW_TOTAL_SIZE 0x20
108 #ifdef DRIVER_DEBUG
109 int corkscrew_debug = DRIVER_DEBUG;
110 #else
111 int corkscrew_debug = 1;
112 #endif
114 #define CORKSCREW_ID 10
117 Theory of Operation
119 I. Board Compatibility
121 This device driver is designed for the 3Com 3c515 ISA Fast EtherLink XL,
122 3Com's ISA bus adapter for Fast Ethernet. Due to the unique I/O port layout,
123 it's not practical to integrate this driver with the other EtherLink drivers.
125 II. Board-specific settings
127 The Corkscrew has an EEPROM for configuration, but no special settings are
128 needed for Linux.
130 III. Driver operation
132 The 3c515 series use an interface that's very similar to the 3c900 "Boomerang"
133 PCI cards, with the bus master interface extensively modified to work with
134 the ISA bus.
136 The card is capable of full-bus-master transfers with separate
137 lists of transmit and receive descriptors, similar to the AMD LANCE/PCnet,
138 DEC Tulip and Intel Speedo3.
140 This driver uses a "RX_COPYBREAK" scheme rather than a fixed intermediate
141 receive buffer. This scheme allocates full-sized skbuffs as receive
142 buffers. The value RX_COPYBREAK is used as the copying breakpoint: it is
143 chosen to trade-off the memory wasted by passing the full-sized skbuff to
144 the queue layer for all frames vs. the copying cost of copying a frame to a
145 correctly-sized skbuff.
148 IIIC. Synchronization
149 The driver runs as two independent, single-threaded flows of control. One
150 is the send-packet routine, which enforces single-threaded use by the netif
151 layer. The other thread is the interrupt handler, which is single
152 threaded by the hardware and other software.
154 IV. Notes
156 Thanks to Terry Murphy of 3Com for providing documentation and a development
157 board.
159 The names "Vortex", "Boomerang" and "Corkscrew" are the internal 3Com
160 project names. I use these names to eliminate confusion -- 3Com product
161 numbers and names are very similar and often confused.
163 The new chips support both ethernet (1.5K) and FDDI (4.5K) frame sizes!
164 This driver only supports ethernet frames because of the recent MTU limit
165 of 1.5K, but the changes to support 4.5K are minimal.
168 /* Operational definitions.
169 These are not used by other compilation units and thus are not
170 exported in a ".h" file.
172 First the windows. There are eight register windows, with the command
173 and status registers available in each.
175 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
176 #define EL3_CMD 0x0e
177 #define EL3_STATUS 0x0e
179 /* The top five bits written to EL3_CMD are a command, the lower
180 11 bits are the parameter, if applicable.
181 Note that 11 parameters bits was fine for ethernet, but the new chips
182 can handle FDDI length frames (~4500 octets) and now parameters count
183 32-bit 'Dwords' rather than octets. */
185 enum corkscrew_cmd {
186 TotalReset = 0 << 11, SelectWindow = 1 << 11, StartCoax = 2 << 11,
187 RxDisable = 3 << 11, RxEnable = 4 << 11, RxReset = 5 << 11,
188 UpStall = 6 << 11, UpUnstall = (6 << 11) + 1,
189 DownStall = (6 << 11) + 2, DownUnstall = (6 << 11) + 3,
190 RxDiscard = 8 << 11, TxEnable = 9 << 11, TxDisable =
191 10 << 11, TxReset = 11 << 11,
192 FakeIntr = 12 << 11, AckIntr = 13 << 11, SetIntrEnb = 14 << 11,
193 SetStatusEnb = 15 << 11, SetRxFilter = 16 << 11, SetRxThreshold =
194 17 << 11,
195 SetTxThreshold = 18 << 11, SetTxStart = 19 << 11,
196 StartDMAUp = 20 << 11, StartDMADown = (20 << 11) + 1, StatsEnable =
197 21 << 11,
198 StatsDisable = 22 << 11, StopCoax = 23 << 11,
201 /* The SetRxFilter command accepts the following classes: */
202 enum RxFilter {
203 RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
206 /* Bits in the general status register. */
207 enum corkscrew_status {
208 IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
209 TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
210 IntReq = 0x0040, StatsFull = 0x0080,
211 DMADone = 1 << 8, DownComplete = 1 << 9, UpComplete = 1 << 10,
212 DMAInProgress = 1 << 11, /* DMA controller is still busy. */
213 CmdInProgress = 1 << 12, /* EL3_CMD is still busy. */
216 /* Register window 1 offsets, the window used in normal operation.
217 On the Corkscrew this window is always mapped at offsets 0x10-0x1f. */
218 enum Window1 {
219 TX_FIFO = 0x10, RX_FIFO = 0x10, RxErrors = 0x14,
220 RxStatus = 0x18, Timer = 0x1A, TxStatus = 0x1B,
221 TxFree = 0x1C, /* Remaining free bytes in Tx buffer. */
223 enum Window0 {
224 Wn0IRQ = 0x08,
225 #if defined(CORKSCREW)
226 Wn0EepromCmd = 0x200A, /* Corkscrew EEPROM command register. */
227 Wn0EepromData = 0x200C, /* Corkscrew EEPROM results register. */
228 #else
229 Wn0EepromCmd = 10, /* Window 0: EEPROM command register. */
230 Wn0EepromData = 12, /* Window 0: EEPROM results register. */
231 #endif
233 enum Win0_EEPROM_bits {
234 EEPROM_Read = 0x80, EEPROM_WRITE = 0x40, EEPROM_ERASE = 0xC0,
235 EEPROM_EWENB = 0x30, /* Enable erasing/writing for 10 msec. */
236 EEPROM_EWDIS = 0x00, /* Disable EWENB before 10 msec timeout. */
239 /* EEPROM locations. */
240 enum eeprom_offset {
241 PhysAddr01 = 0, PhysAddr23 = 1, PhysAddr45 = 2, ModelID = 3,
242 EtherLink3ID = 7,
245 enum Window3 { /* Window 3: MAC/config bits. */
246 Wn3_Config = 0, Wn3_MAC_Ctrl = 6, Wn3_Options = 8,
248 union wn3_config {
249 int i;
250 struct w3_config_fields {
251 unsigned int ram_size:3, ram_width:1, ram_speed:2,
252 rom_size:2;
253 int pad8:8;
254 unsigned int ram_split:2, pad18:2, xcvr:3, pad21:1,
255 autoselect:1;
256 int pad24:7;
257 } u;
260 enum Window4 {
261 Wn4_NetDiag = 6, Wn4_Media = 10, /* Window 4: Xcvr/media bits. */
263 enum Win4_Media_bits {
264 Media_SQE = 0x0008, /* Enable SQE error counting for AUI. */
265 Media_10TP = 0x00C0, /* Enable link beat and jabber for 10baseT. */
266 Media_Lnk = 0x0080, /* Enable just link beat for 100TX/100FX. */
267 Media_LnkBeat = 0x0800,
269 enum Window7 { /* Window 7: Bus Master control. */
270 Wn7_MasterAddr = 0, Wn7_MasterLen = 6, Wn7_MasterStatus = 12,
273 /* Boomerang-style bus master control registers. Note ISA aliases! */
274 enum MasterCtrl {
275 PktStatus = 0x400, DownListPtr = 0x404, FragAddr = 0x408, FragLen =
276 0x40c,
277 TxFreeThreshold = 0x40f, UpPktStatus = 0x410, UpListPtr = 0x418,
280 /* The Rx and Tx descriptor lists.
281 Caution Alpha hackers: these types are 32 bits! Note also the 8 byte
282 alignment contraint on tx_ring[] and rx_ring[]. */
283 struct boom_rx_desc {
284 u32 next;
285 s32 status;
286 u32 addr;
287 s32 length;
290 /* Values for the Rx status entry. */
291 enum rx_desc_status {
292 RxDComplete = 0x00008000, RxDError = 0x4000,
293 /* See boomerang_rx() for actual error bits */
296 struct boom_tx_desc {
297 u32 next;
298 s32 status;
299 u32 addr;
300 s32 length;
303 struct corkscrew_private {
304 char devname[8]; /* "ethN" string, also for kernel debug. */
305 const char *product_name;
306 struct net_device *next_module;
307 /* The Rx and Tx rings are here to keep them quad-word-aligned. */
308 struct boom_rx_desc rx_ring[RX_RING_SIZE];
309 struct boom_tx_desc tx_ring[TX_RING_SIZE];
310 /* The addresses of transmit- and receive-in-place skbuffs. */
311 struct sk_buff *rx_skbuff[RX_RING_SIZE];
312 struct sk_buff *tx_skbuff[TX_RING_SIZE];
313 unsigned int cur_rx, cur_tx; /* The next free ring entry */
314 unsigned int dirty_rx, dirty_tx;/* The ring entries to be free()ed. */
315 struct enet_statistics stats;
316 struct sk_buff *tx_skb; /* Packet being eaten by bus master ctrl. */
317 struct timer_list timer; /* Media selection timer. */
318 int capabilities ; /* Adapter capabilities word. */
319 int options; /* User-settable misc. driver options. */
320 int last_rx_packets; /* For media autoselection. */
321 unsigned int available_media:8, /* From Wn3_Options */
322 media_override:3, /* Passed-in media type. */
323 default_media:3, /* Read from the EEPROM. */
324 full_duplex:1, autoselect:1, bus_master:1, /* Vortex can only do a fragment bus-m. */
325 full_bus_master_tx:1, full_bus_master_rx:1, /* Boomerang */
326 tx_full:1;
329 /* The action to take with a media selection timer tick.
330 Note that we deviate from the 3Com order by checking 10base2 before AUI.
332 enum xcvr_types {
333 XCVR_10baseT =
334 0, XCVR_AUI, XCVR_10baseTOnly, XCVR_10base2, XCVR_100baseTx,
335 XCVR_100baseFx, XCVR_MII = 6, XCVR_Default = 8,
338 static struct media_table {
339 char *name;
340 unsigned int media_bits:16, /* Bits to set in Wn4_Media register. */
341 mask:8, /* The transceiver-present bit in Wn3_Config. */
342 next:8; /* The media type to try next. */
343 short wait; /* Time before we check media status. */
344 } media_tbl[] = {
345 { "10baseT", Media_10TP, 0x08, XCVR_10base2, (14 * HZ) / 10 },
346 { "10Mbs AUI", Media_SQE, 0x20, XCVR_Default, (1 * HZ) / 10},
347 { "undefined", 0, 0x80, XCVR_10baseT, 10000},
348 { "10base2", 0, 0x10, XCVR_AUI, (1 * HZ) / 10},
349 { "100baseTX", Media_Lnk, 0x02, XCVR_100baseFx, (14 * HZ) / 10},
350 { "100baseFX", Media_Lnk, 0x04, XCVR_MII, (14 * HZ) / 10},
351 { "MII", 0, 0x40, XCVR_10baseT, 3 * HZ},
352 { "undefined", 0, 0x01, XCVR_10baseT, 10000},
353 { "Default", 0, 0xFF, XCVR_10baseT, 10000},
356 #ifdef CONFIG_ISAPNP
357 struct corkscrew_isapnp_adapters_struct {
358 unsigned short vendor, function;
359 char *name;
361 struct corkscrew_isapnp_adapters_struct corkscrew_isapnp_adapters[] = {
362 {ISAPNP_VENDOR('T', 'C', 'M'), ISAPNP_FUNCTION(0x5051), "3Com Fast EtherLink ISA"},
363 {0, }
365 int corkscrew_isapnp_phys_addr[3] = {
366 0, 0, 0
368 #endif
369 static int nopnp = 0;
371 static int corkscrew_scan(struct net_device *dev);
372 static struct net_device *corkscrew_found_device(struct net_device *dev,
373 int ioaddr, int irq,
374 int product_index,
375 int options);
376 static int corkscrew_probe1(struct net_device *dev);
377 static int corkscrew_open(struct net_device *dev);
378 static void corkscrew_timer(unsigned long arg);
379 static int corkscrew_start_xmit(struct sk_buff *skb,
380 struct net_device *dev);
381 static int corkscrew_rx(struct net_device *dev);
382 static void corkscrew_timeout(struct net_device *dev);
383 static int boomerang_rx(struct net_device *dev);
384 static void corkscrew_interrupt(int irq, void *dev_id,
385 struct pt_regs *regs);
386 static int corkscrew_close(struct net_device *dev);
387 static void update_stats(int addr, struct net_device *dev);
388 static struct enet_statistics *corkscrew_get_stats(struct net_device *dev);
389 static void set_rx_mode(struct net_device *dev);
393 Unfortunately maximizing the shared code between the integrated and
394 module version of the driver results in a complicated set of initialization
395 procedures.
396 init_module() -- modules / tc59x_init() -- built-in
397 The wrappers for corkscrew_scan()
398 corkscrew_scan() The common routine that scans for PCI and EISA cards
399 corkscrew_found_device() Allocate a device structure when we find a card.
400 Different versions exist for modules and built-in.
401 corkscrew_probe1() Fill in the device structure -- this is separated
402 so that the modules code can put it in dev->init.
404 /* This driver uses 'options' to pass the media type, full-duplex flag, etc. */
405 /* Note: this is the only limit on the number of cards supported!! */
406 static int options[8] = { -1, -1, -1, -1, -1, -1, -1, -1, };
408 #ifdef MODULE
409 static int debug = -1;
410 /* A list of all installed Vortex devices, for removing the driver module. */
411 static struct net_device *root_corkscrew_dev = NULL;
413 int init_module(void)
415 int cards_found;
417 if (debug >= 0)
418 corkscrew_debug = debug;
419 if (corkscrew_debug)
420 printk(version);
422 root_corkscrew_dev = NULL;
423 cards_found = corkscrew_scan(0);
424 return cards_found ? 0 : -ENODEV;
427 #else
428 int tc515_probe(struct net_device *dev)
430 int cards_found = 0;
432 cards_found = corkscrew_scan(dev);
434 if (corkscrew_debug > 0 && cards_found)
435 printk(version);
437 return cards_found ? 0 : -ENODEV;
439 #endif /* not MODULE */
441 static int corkscrew_scan(struct net_device *dev)
443 int cards_found = 0;
444 short i;
445 static int ioaddr;
446 static int pnp_cards = 0;
448 #ifdef CONFIG_ISAPNP
449 if(nopnp == 1)
450 goto no_pnp;
451 for(i=0; corkscrew_isapnp_adapters[i].vendor != 0; i++) {
452 struct pci_dev *idev = NULL;
453 int irq, j;
454 while((idev = isapnp_find_dev(NULL,
455 corkscrew_isapnp_adapters[i].vendor,
456 corkscrew_isapnp_adapters[i].function,
457 idev))) {
459 if(idev->active) idev->deactivate(idev);
461 if(idev->prepare(idev)<0)
462 continue;
463 if (!(idev->resource[0].flags & IORESOURCE_IO))
464 continue;
465 if(idev->activate(idev)<0) {
466 printk("isapnp configure failed (out of resources?)\n");
467 return -ENOMEM;
469 if (!idev->resource[0].start || check_region(idev->resource[0].start,16))
470 continue;
471 ioaddr = idev->resource[0].start;
472 irq = idev->irq_resource[0].start;
473 if(corkscrew_debug)
474 printk ("ISAPNP reports %s at i/o 0x%x, irq %d\n",
475 corkscrew_isapnp_adapters[i].name,ioaddr, irq);
477 if ((inw(ioaddr + 0x2002) & 0x1f0) != (ioaddr & 0x1f0))
478 continue;
479 /* Verify by reading the device ID from the EEPROM. */
481 int timer;
482 outw(EEPROM_Read + 7, ioaddr + Wn0EepromCmd);
483 /* Pause for at least 162 us. for the read to take place. */
484 for (timer = 4; timer >= 0; timer--) {
485 udelay(162);
486 if ((inw(ioaddr + Wn0EepromCmd) & 0x0200)
487 == 0)
488 break;
490 if (inw(ioaddr + Wn0EepromData) != 0x6d50)
491 continue;
493 printk(KERN_INFO "3c515 Resource configuraiton register %#4.4x, DCR %4.4x.\n",
494 inl(ioaddr + 0x2002), inw(ioaddr + 0x2000));
495 /* irq = inw(ioaddr + 0x2002) & 15; */ /* Use the irq from isapnp */
496 corkscrew_isapnp_phys_addr[pnp_cards] = ioaddr;
497 corkscrew_found_device(dev, ioaddr, irq, CORKSCREW_ID, dev
498 && dev->mem_start ? dev->
499 mem_start : options[cards_found]);
500 dev = 0;
501 pnp_cards++;
502 cards_found++;
505 no_pnp:
506 #endif /* not CONFIG_ISAPNP */
508 /* Check all locations on the ISA bus -- evil! */
509 for (ioaddr = 0x100; ioaddr < 0x400; ioaddr += 0x20) {
510 int irq;
511 #ifdef CONFIG_ISAPNP
512 /* Make sure this was not already picked up by isapnp */
513 if(ioaddr == corkscrew_isapnp_phys_addr[0]) continue;
514 if(ioaddr == corkscrew_isapnp_phys_addr[1]) continue;
515 if(ioaddr == corkscrew_isapnp_phys_addr[2]) continue;
516 #endif
517 if (check_region(ioaddr, CORKSCREW_TOTAL_SIZE))
518 continue;
519 /* Check the resource configuration for a matching ioaddr. */
520 if ((inw(ioaddr + 0x2002) & 0x1f0) != (ioaddr & 0x1f0))
521 continue;
522 /* Verify by reading the device ID from the EEPROM. */
524 int timer;
525 outw(EEPROM_Read + 7, ioaddr + Wn0EepromCmd);
526 /* Pause for at least 162 us. for the read to take place. */
527 for (timer = 4; timer >= 0; timer--) {
528 udelay(162);
529 if ((inw(ioaddr + Wn0EepromCmd) & 0x0200)
530 == 0)
531 break;
533 if (inw(ioaddr + Wn0EepromData) != 0x6d50)
534 continue;
536 printk(KERN_INFO "3c515 Resource configuraiton register %#4.4x, DCR %4.4x.\n",
537 inl(ioaddr + 0x2002), inw(ioaddr + 0x2000));
538 irq = inw(ioaddr + 0x2002) & 15;
539 corkscrew_found_device(dev, ioaddr, irq, CORKSCREW_ID, dev
540 && dev->mem_start ? dev->
541 mem_start : options[cards_found]);
542 dev = 0;
543 cards_found++;
545 if (corkscrew_debug)
546 printk(KERN_INFO "%d 3c515 cards found.\n", cards_found);
547 return cards_found;
550 static struct net_device *corkscrew_found_device(struct net_device *dev,
551 int ioaddr, int irq,
552 int product_index,
553 int options)
555 struct corkscrew_private *vp;
557 #ifdef MODULE
558 /* Allocate and fill new device structure. */
559 int dev_size = sizeof(struct net_device) +
560 sizeof(struct corkscrew_private) + 15; /* Pad for alignment */
562 dev = (struct net_device *) kmalloc(dev_size, GFP_KERNEL);
563 memset(dev, 0, dev_size);
564 /* Align the Rx and Tx ring entries. */
565 dev->priv =
566 (void *) (((long) dev + sizeof(struct net_device) + 15) & ~15);
567 vp = (struct corkscrew_private *) dev->priv;
568 dev->name = vp->devname; /* An empty string. */
569 dev->base_addr = ioaddr;
570 dev->irq = irq;
571 dev->dma =
572 (product_index == CORKSCREW_ID ? inw(ioaddr + 0x2000) & 7 : 0);
573 dev->init = corkscrew_probe1;
574 vp->product_name = "3c515";
575 vp->options = options;
576 if (options >= 0) {
577 vp->media_override =
578 ((options & 7) == 2) ? 0 : options & 7;
579 vp->full_duplex = (options & 8) ? 1 : 0;
580 vp->bus_master = (options & 16) ? 1 : 0;
581 } else {
582 vp->media_override = 7;
583 vp->full_duplex = 0;
584 vp->bus_master = 0;
586 ether_setup(dev);
587 vp->next_module = root_corkscrew_dev;
588 root_corkscrew_dev = dev;
589 if (register_netdev(dev) != 0)
590 return 0;
591 #else /* not a MODULE */
592 if (dev) {
593 /* Caution: quad-word alignment required for rings! */
594 dev->priv =
595 kmalloc(sizeof(struct corkscrew_private), GFP_KERNEL);
596 memset(dev->priv, 0, sizeof(struct corkscrew_private));
598 dev = init_etherdev(dev, sizeof(struct corkscrew_private));
599 dev->base_addr = ioaddr;
600 dev->irq = irq;
601 dev->dma =
602 (product_index == CORKSCREW_ID ? inw(ioaddr + 0x2000) & 7 : 0);
603 vp = (struct corkscrew_private *) dev->priv;
604 vp->product_name = "3c515";
605 vp->options = options;
606 if (options >= 0) {
607 vp->media_override =
608 ((options & 7) == 2) ? 0 : options & 7;
609 vp->full_duplex = (options & 8) ? 1 : 0;
610 vp->bus_master = (options & 16) ? 1 : 0;
611 } else {
612 vp->media_override = 7;
613 vp->full_duplex = 0;
614 vp->bus_master = 0;
617 corkscrew_probe1(dev);
618 #endif /* MODULE */
619 return dev;
622 static int corkscrew_probe1(struct net_device *dev)
624 int ioaddr = dev->base_addr;
625 struct corkscrew_private *vp =
626 (struct corkscrew_private *) dev->priv;
627 unsigned int eeprom[0x40], checksum = 0; /* EEPROM contents */
628 int i;
630 printk(KERN_INFO "%s: 3Com %s at %#3x,", dev->name,
631 vp->product_name, ioaddr);
633 /* Read the station address from the EEPROM. */
634 EL3WINDOW(0);
635 for (i = 0; i < 0x18; i++) {
636 short *phys_addr = (short *) dev->dev_addr;
637 int timer;
638 outw(EEPROM_Read + i, ioaddr + Wn0EepromCmd);
639 /* Pause for at least 162 us. for the read to take place. */
640 for (timer = 4; timer >= 0; timer--) {
641 udelay(162);
642 if ((inw(ioaddr + Wn0EepromCmd) & 0x0200) == 0)
643 break;
645 eeprom[i] = inw(ioaddr + Wn0EepromData);
646 checksum ^= eeprom[i];
647 if (i < 3)
648 phys_addr[i] = htons(eeprom[i]);
650 checksum = (checksum ^ (checksum >> 8)) & 0xff;
651 if (checksum != 0x00)
652 printk(" ***INVALID CHECKSUM %4.4x*** ", checksum);
653 for (i = 0; i < 6; i++)
654 printk("%c%2.2x", i ? ':' : ' ', dev->dev_addr[i]);
655 if (eeprom[16] == 0x11c7) { /* Corkscrew */
656 if (request_dma(dev->dma, "3c515")) {
657 printk(", DMA %d allocation failed", dev->dma);
658 dev->dma = 0;
659 } else
660 printk(", DMA %d", dev->dma);
662 printk(", IRQ %d\n", dev->irq);
663 /* Tell them about an invalid IRQ. */
664 if (corkscrew_debug && (dev->irq <= 0 || dev->irq > 15))
665 printk(KERN_WARNING " *** Warning: this IRQ is unlikely to work! ***\n");
668 char *ram_split[] = { "5:3", "3:1", "1:1", "3:5" };
669 union wn3_config config;
670 EL3WINDOW(3);
671 vp->available_media = inw(ioaddr + Wn3_Options);
672 config.i = inl(ioaddr + Wn3_Config);
673 if (corkscrew_debug > 1)
674 printk(KERN_INFO " Internal config register is %4.4x, transceivers %#x.\n",
675 config.i, inw(ioaddr + Wn3_Options));
676 printk(KERN_INFO " %dK %s-wide RAM %s Rx:Tx split, %s%s interface.\n",
677 8 << config.u.ram_size,
678 config.u.ram_width ? "word" : "byte",
679 ram_split[config.u.ram_split],
680 config.u.autoselect ? "autoselect/" : "",
681 media_tbl[config.u.xcvr].name);
682 dev->if_port = config.u.xcvr;
683 vp->default_media = config.u.xcvr;
684 vp->autoselect = config.u.autoselect;
686 if (vp->media_override != 7) {
687 printk(KERN_INFO " Media override to transceiver type %d (%s).\n",
688 vp->media_override,
689 media_tbl[vp->media_override].name);
690 dev->if_port = vp->media_override;
693 vp->capabilities = eeprom[16];
694 vp->full_bus_master_tx = (vp->capabilities & 0x20) ? 1 : 0;
695 /* Rx is broken at 10mbps, so we always disable it. */
696 /* vp->full_bus_master_rx = 0; */
697 vp->full_bus_master_rx = (vp->capabilities & 0x20) ? 1 : 0;
699 /* We do a request_region() to register /proc/ioports info. */
700 request_region(ioaddr, CORKSCREW_TOTAL_SIZE, vp->product_name);
702 /* The 3c51x-specific entries in the device structure. */
703 dev->open = &corkscrew_open;
704 dev->hard_start_xmit = &corkscrew_start_xmit;
705 dev->tx_timeout = &corkscrew_timeout;
706 dev->watchdog_timeo = (400 * HZ) / 1000;
707 dev->stop = &corkscrew_close;
708 dev->get_stats = &corkscrew_get_stats;
709 dev->set_multicast_list = &set_rx_mode;
711 return 0;
715 static int corkscrew_open(struct net_device *dev)
717 int ioaddr = dev->base_addr;
718 struct corkscrew_private *vp =
719 (struct corkscrew_private *) dev->priv;
720 union wn3_config config;
721 int i;
723 /* Before initializing select the active media port. */
724 EL3WINDOW(3);
725 if (vp->full_duplex)
726 outb(0x20, ioaddr + Wn3_MAC_Ctrl); /* Set the full-duplex bit. */
727 config.i = inl(ioaddr + Wn3_Config);
729 if (vp->media_override != 7) {
730 if (corkscrew_debug > 1)
731 printk(KERN_INFO "%s: Media override to transceiver %d (%s).\n",
732 dev->name, vp->media_override,
733 media_tbl[vp->media_override].name);
734 dev->if_port = vp->media_override;
735 } else if (vp->autoselect) {
736 /* Find first available media type, starting with 100baseTx. */
737 dev->if_port = 4;
738 while (!(vp->available_media & media_tbl[dev->if_port].mask))
739 dev->if_port = media_tbl[dev->if_port].next;
741 if (corkscrew_debug > 1)
742 printk("%s: Initial media type %s.\n",
743 dev->name, media_tbl[dev->if_port].name);
745 init_timer(&vp->timer);
746 vp->timer.expires = RUN_AT(media_tbl[dev->if_port].wait);
747 vp->timer.data = (unsigned long) dev;
748 vp->timer.function = &corkscrew_timer; /* timer handler */
749 add_timer(&vp->timer);
750 } else
751 dev->if_port = vp->default_media;
753 config.u.xcvr = dev->if_port;
754 outl(config.i, ioaddr + Wn3_Config);
756 if (corkscrew_debug > 1) {
757 printk("%s: corkscrew_open() InternalConfig %8.8x.\n",
758 dev->name, config.i);
761 outw(TxReset, ioaddr + EL3_CMD);
762 for (i = 20; i >= 0; i--)
763 if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress))
764 break;
766 outw(RxReset, ioaddr + EL3_CMD);
767 /* Wait a few ticks for the RxReset command to complete. */
768 for (i = 20; i >= 0; i--)
769 if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress))
770 break;
772 outw(SetStatusEnb | 0x00, ioaddr + EL3_CMD);
774 /* Use the now-standard shared IRQ implementation. */
775 if (vp->capabilities == 0x11c7) {
776 /* Corkscrew: Cannot share ISA resources. */
777 if (dev->irq == 0
778 || dev->dma == 0
779 || request_irq(dev->irq, &corkscrew_interrupt, 0,
780 vp->product_name, dev)) return -EAGAIN;
781 enable_dma(dev->dma);
782 set_dma_mode(dev->dma, DMA_MODE_CASCADE);
783 } else if (request_irq(dev->irq, &corkscrew_interrupt, SA_SHIRQ,
784 vp->product_name, dev)) {
785 return -EAGAIN;
788 if (corkscrew_debug > 1) {
789 EL3WINDOW(4);
790 printk("%s: corkscrew_open() irq %d media status %4.4x.\n",
791 dev->name, dev->irq, inw(ioaddr + Wn4_Media));
794 /* Set the station address and mask in window 2 each time opened. */
795 EL3WINDOW(2);
796 for (i = 0; i < 6; i++)
797 outb(dev->dev_addr[i], ioaddr + i);
798 for (; i < 12; i += 2)
799 outw(0, ioaddr + i);
801 if (dev->if_port == 3)
802 /* Start the thinnet transceiver. We should really wait 50ms... */
803 outw(StartCoax, ioaddr + EL3_CMD);
804 EL3WINDOW(4);
805 outw((inw(ioaddr + Wn4_Media) & ~(Media_10TP | Media_SQE)) |
806 media_tbl[dev->if_port].media_bits, ioaddr + Wn4_Media);
808 /* Switch to the stats window, and clear all stats by reading. */
809 outw(StatsDisable, ioaddr + EL3_CMD);
810 EL3WINDOW(6);
811 for (i = 0; i < 10; i++)
812 inb(ioaddr + i);
813 inw(ioaddr + 10);
814 inw(ioaddr + 12);
815 /* New: On the Vortex we must also clear the BadSSD counter. */
816 EL3WINDOW(4);
817 inb(ioaddr + 12);
818 /* ..and on the Boomerang we enable the extra statistics bits. */
819 outw(0x0040, ioaddr + Wn4_NetDiag);
821 /* Switch to register set 7 for normal use. */
822 EL3WINDOW(7);
824 if (vp->full_bus_master_rx) { /* Boomerang bus master. */
825 vp->cur_rx = vp->dirty_rx = 0;
826 if (corkscrew_debug > 2)
827 printk("%s: Filling in the Rx ring.\n",
828 dev->name);
829 for (i = 0; i < RX_RING_SIZE; i++) {
830 struct sk_buff *skb;
831 if (i < (RX_RING_SIZE - 1))
832 vp->rx_ring[i].next =
833 virt_to_bus(&vp->rx_ring[i + 1]);
834 else
835 vp->rx_ring[i].next = 0;
836 vp->rx_ring[i].status = 0; /* Clear complete bit. */
837 vp->rx_ring[i].length = PKT_BUF_SZ | 0x80000000;
838 skb = dev_alloc_skb(PKT_BUF_SZ);
839 vp->rx_skbuff[i] = skb;
840 if (skb == NULL)
841 break; /* Bad news! */
842 skb->dev = dev; /* Mark as being used by this device. */
843 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
844 vp->rx_ring[i].addr = virt_to_bus(skb->tail);
846 vp->rx_ring[i - 1].next = virt_to_bus(&vp->rx_ring[0]); /* Wrap the ring. */
847 outl(virt_to_bus(&vp->rx_ring[0]), ioaddr + UpListPtr);
849 if (vp->full_bus_master_tx) { /* Boomerang bus master Tx. */
850 vp->cur_tx = vp->dirty_tx = 0;
851 outb(PKT_BUF_SZ >> 8, ioaddr + TxFreeThreshold); /* Room for a packet. */
852 /* Clear the Tx ring. */
853 for (i = 0; i < TX_RING_SIZE; i++)
854 vp->tx_skbuff[i] = 0;
855 outl(0, ioaddr + DownListPtr);
857 /* Set receiver mode: presumably accept b-case and phys addr only. */
858 set_rx_mode(dev);
859 outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
861 netif_start_queue(dev);
863 outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
864 outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
865 /* Allow status bits to be seen. */
866 outw(SetStatusEnb | AdapterFailure | IntReq | StatsFull |
867 (vp->full_bus_master_tx ? DownComplete : TxAvailable) |
868 (vp->full_bus_master_rx ? UpComplete : RxComplete) |
869 (vp->bus_master ? DMADone : 0), ioaddr + EL3_CMD);
870 /* Ack all pending events, and set active indicator mask. */
871 outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
872 ioaddr + EL3_CMD);
873 outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
874 | (vp->bus_master ? DMADone : 0) | UpComplete | DownComplete,
875 ioaddr + EL3_CMD);
877 MOD_INC_USE_COUNT;
879 return 0;
882 static void corkscrew_timer(unsigned long data)
884 #ifdef AUTOMEDIA
885 struct net_device *dev = (struct net_device *) data;
886 struct corkscrew_private *vp =
887 (struct corkscrew_private *) dev->priv;
888 int ioaddr = dev->base_addr;
889 unsigned long flags;
890 int ok = 0;
892 if (corkscrew_debug > 1)
893 printk("%s: Media selection timer tick happened, %s.\n",
894 dev->name, media_tbl[dev->if_port].name);
896 save_flags(flags);
897 cli(); {
898 int old_window = inw(ioaddr + EL3_CMD) >> 13;
899 int media_status;
900 EL3WINDOW(4);
901 media_status = inw(ioaddr + Wn4_Media);
902 switch (dev->if_port) {
903 case 0:
904 case 4:
905 case 5: /* 10baseT, 100baseTX, 100baseFX */
906 if (media_status & Media_LnkBeat) {
907 ok = 1;
908 if (corkscrew_debug > 1)
909 printk("%s: Media %s has link beat, %x.\n",
910 dev->name,
911 media_tbl[dev->if_port].name,
912 media_status);
913 } else if (corkscrew_debug > 1)
914 printk("%s: Media %s is has no link beat, %x.\n",
915 dev->name,
916 media_tbl[dev->if_port].name,
917 media_status);
919 break;
920 default: /* Other media types handled by Tx timeouts. */
921 if (corkscrew_debug > 1)
922 printk("%s: Media %s is has no indication, %x.\n",
923 dev->name,
924 media_tbl[dev->if_port].name,
925 media_status);
926 ok = 1;
928 if (!ok) {
929 union wn3_config config;
931 do {
932 dev->if_port =
933 media_tbl[dev->if_port].next;
935 while (!(vp->available_media & media_tbl[dev->if_port].mask));
937 if (dev->if_port == 8) { /* Go back to default. */
938 dev->if_port = vp->default_media;
939 if (corkscrew_debug > 1)
940 printk("%s: Media selection failing, using default %s port.\n",
941 dev->name,
942 media_tbl[dev->if_port].name);
943 } else {
944 if (corkscrew_debug > 1)
945 printk("%s: Media selection failed, now trying %s port.\n",
946 dev->name,
947 media_tbl[dev->if_port].name);
948 vp->timer.expires = RUN_AT(media_tbl[dev->if_port].wait);
949 add_timer(&vp->timer);
951 outw((media_status & ~(Media_10TP | Media_SQE)) |
952 media_tbl[dev->if_port].media_bits,
953 ioaddr + Wn4_Media);
955 EL3WINDOW(3);
956 config.i = inl(ioaddr + Wn3_Config);
957 config.u.xcvr = dev->if_port;
958 outl(config.i, ioaddr + Wn3_Config);
960 outw(dev->if_port == 3 ? StartCoax : StopCoax,
961 ioaddr + EL3_CMD);
963 EL3WINDOW(old_window);
965 restore_flags(flags);
966 if (corkscrew_debug > 1)
967 printk("%s: Media selection timer finished, %s.\n",
968 dev->name, media_tbl[dev->if_port].name);
970 #endif /* AUTOMEDIA */
971 return;
974 static void corkscrew_timeout(struct net_device *dev)
976 int i;
977 struct corkscrew_private *vp =
978 (struct corkscrew_private *) dev->priv;
979 int ioaddr = dev->base_addr;
981 printk(KERN_WARNING
982 "%s: transmit timed out, tx_status %2.2x status %4.4x.\n",
983 dev->name, inb(ioaddr + TxStatus),
984 inw(ioaddr + EL3_STATUS));
985 /* Slight code bloat to be user friendly. */
986 if ((inb(ioaddr + TxStatus) & 0x88) == 0x88)
987 printk(KERN_WARNING
988 "%s: Transmitter encountered 16 collisions -- network"
989 " network cable problem?\n", dev->name);
990 #ifndef final_version
991 printk(" Flags; bus-master %d, full %d; dirty %d current %d.\n",
992 vp->full_bus_master_tx, vp->tx_full, vp->dirty_tx,
993 vp->cur_tx);
994 printk(" Down list %8.8x vs. %p.\n", inl(ioaddr + DownListPtr),
995 &vp->tx_ring[0]);
996 for (i = 0; i < TX_RING_SIZE; i++) {
997 printk(" %d: %p length %8.8x status %8.8x\n", i,
998 &vp->tx_ring[i],
999 vp->tx_ring[i].length, vp->tx_ring[i].status);
1001 #endif
1002 /* Issue TX_RESET and TX_START commands. */
1003 outw(TxReset, ioaddr + EL3_CMD);
1004 for (i = 20; i >= 0; i--)
1005 if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress))
1006 break;
1007 outw(TxEnable, ioaddr + EL3_CMD);
1008 dev->trans_start = jiffies;
1009 vp->stats.tx_errors++;
1010 vp->stats.tx_dropped++;
1011 netif_wake_queue(dev);
1014 static int corkscrew_start_xmit(struct sk_buff *skb,
1015 struct net_device *dev)
1017 struct corkscrew_private *vp =
1018 (struct corkscrew_private *) dev->priv;
1019 int ioaddr = dev->base_addr;
1021 /* Block a timer-based transmit from overlapping. */
1023 netif_stop_queue(dev);
1025 if (vp->full_bus_master_tx) { /* BOOMERANG bus-master */
1026 /* Calculate the next Tx descriptor entry. */
1027 int entry = vp->cur_tx % TX_RING_SIZE;
1028 struct boom_tx_desc *prev_entry;
1029 unsigned long flags, i;
1031 if (vp->tx_full) /* No room to transmit with */
1032 return 1;
1033 if (vp->cur_tx != 0)
1034 prev_entry =
1035 &vp->tx_ring[(vp->cur_tx - 1) % TX_RING_SIZE];
1036 else
1037 prev_entry = NULL;
1038 if (corkscrew_debug > 3)
1039 printk("%s: Trying to send a packet, Tx index %d.\n",
1040 dev->name, vp->cur_tx);
1041 /* vp->tx_full = 1; */
1042 vp->tx_skbuff[entry] = skb;
1043 vp->tx_ring[entry].next = 0;
1044 vp->tx_ring[entry].addr = virt_to_bus(skb->data);
1045 vp->tx_ring[entry].length = skb->len | 0x80000000;
1046 vp->tx_ring[entry].status = skb->len | 0x80000000;
1048 save_flags(flags);
1049 cli();
1050 outw(DownStall, ioaddr + EL3_CMD);
1051 /* Wait for the stall to complete. */
1052 for (i = 20; i >= 0; i--)
1053 if ((inw(ioaddr + EL3_STATUS) & CmdInProgress) ==
1054 0) break;
1055 if (prev_entry)
1056 prev_entry->next =
1057 virt_to_bus(&vp->tx_ring[entry]);
1058 if (inl(ioaddr + DownListPtr) == 0) {
1059 outl(virt_to_bus(&vp->tx_ring[entry]),
1060 ioaddr + DownListPtr);
1061 queued_packet++;
1063 outw(DownUnstall, ioaddr + EL3_CMD);
1064 restore_flags(flags);
1066 vp->cur_tx++;
1067 if (vp->cur_tx - vp->dirty_tx > TX_RING_SIZE - 1)
1068 vp->tx_full = 1;
1069 else { /* Clear previous interrupt enable. */
1070 if (prev_entry)
1071 prev_entry->status &= ~0x80000000;
1072 netif_wake_queue(dev);
1074 dev->trans_start = jiffies;
1075 return 0;
1077 /* Put out the doubleword header... */
1078 outl(skb->len, ioaddr + TX_FIFO);
1079 #ifdef VORTEX_BUS_MASTER
1080 if (vp->bus_master) {
1081 /* Set the bus-master controller to transfer the packet. */
1082 outl((int) (skb->data), ioaddr + Wn7_MasterAddr);
1083 outw((skb->len + 3) & ~3, ioaddr + Wn7_MasterLen);
1084 vp->tx_skb = skb;
1085 outw(StartDMADown, ioaddr + EL3_CMD);
1086 /* queue will be woken at the DMADone interrupt. */
1087 } else {
1088 /* ... and the packet rounded to a doubleword. */
1089 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
1090 dev_kfree_skb(skb);
1091 if (inw(ioaddr + TxFree) > 1536) {
1092 netif_wake_queue(dev);
1093 } else
1094 /* Interrupt us when the FIFO has room for max-sized packet. */
1095 outw(SetTxThreshold + (1536 >> 2),
1096 ioaddr + EL3_CMD);
1098 #else
1099 /* ... and the packet rounded to a doubleword. */
1100 outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
1101 dev_kfree_skb(skb);
1102 if (inw(ioaddr + TxFree) > 1536) {
1103 netif_wake_queue(dev);
1104 } else
1105 /* Interrupt us when the FIFO has room for max-sized packet. */
1106 outw(SetTxThreshold + (1536 >> 2), ioaddr + EL3_CMD);
1107 #endif /* bus master */
1109 dev->trans_start = jiffies;
1111 /* Clear the Tx status stack. */
1113 short tx_status;
1114 int i = 4;
1116 while (--i > 0 && (tx_status = inb(ioaddr + TxStatus)) > 0) {
1117 if (tx_status & 0x3C) { /* A Tx-disabling error occurred. */
1118 if (corkscrew_debug > 2)
1119 printk("%s: Tx error, status %2.2x.\n",
1120 dev->name, tx_status);
1121 if (tx_status & 0x04)
1122 vp->stats.tx_fifo_errors++;
1123 if (tx_status & 0x38)
1124 vp->stats.tx_aborted_errors++;
1125 if (tx_status & 0x30) {
1126 int j;
1127 outw(TxReset, ioaddr + EL3_CMD);
1128 for (j = 20; j >= 0; j--)
1129 if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress))
1130 break;
1132 outw(TxEnable, ioaddr + EL3_CMD);
1134 outb(0x00, ioaddr + TxStatus); /* Pop the status stack. */
1137 vp->stats.tx_bytes += skb->len;
1138 return 0;
1141 /* The interrupt handler does all of the Rx thread work and cleans up
1142 after the Tx thread. */
1144 static void corkscrew_interrupt(int irq, void *dev_id,
1145 struct pt_regs *regs)
1147 /* Use the now-standard shared IRQ implementation. */
1148 struct net_device *dev = dev_id;
1149 struct corkscrew_private *lp;
1150 int ioaddr, status;
1151 int latency;
1152 int i = max_interrupt_work;
1154 ioaddr = dev->base_addr;
1155 latency = inb(ioaddr + Timer);
1156 lp = (struct corkscrew_private *) dev->priv;
1158 status = inw(ioaddr + EL3_STATUS);
1160 if (corkscrew_debug > 4)
1161 printk("%s: interrupt, status %4.4x, timer %d.\n",
1162 dev->name, status, latency);
1163 if ((status & 0xE000) != 0xE000) {
1164 static int donedidthis = 0;
1165 /* Some interrupt controllers store a bogus interrupt from boot-time.
1166 Ignore a single early interrupt, but don't hang the machine for
1167 other interrupt problems. */
1168 if (donedidthis++ > 100) {
1169 printk(KERN_ERR "%s: Bogus interrupt, bailing. Status %4.4x, start=%d.\n",
1170 dev->name, status, netif_running(dev));
1171 free_irq(dev->irq, dev);
1175 do {
1176 if (corkscrew_debug > 5)
1177 printk("%s: In interrupt loop, status %4.4x.\n",
1178 dev->name, status);
1179 if (status & RxComplete)
1180 corkscrew_rx(dev);
1182 if (status & TxAvailable) {
1183 if (corkscrew_debug > 5)
1184 printk
1185 (" TX room bit was handled.\n");
1186 /* There's room in the FIFO for a full-sized packet. */
1187 outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
1188 netif_wake_queue(dev);
1190 if (status & DownComplete) {
1191 unsigned int dirty_tx = lp->dirty_tx;
1193 while (lp->cur_tx - dirty_tx > 0) {
1194 int entry = dirty_tx % TX_RING_SIZE;
1195 if (inl(ioaddr + DownListPtr) ==
1196 virt_to_bus(&lp->tx_ring[entry]))
1197 break; /* It still hasn't been processed. */
1198 if (lp->tx_skbuff[entry]) {
1199 dev_kfree_skb_irq(lp->
1200 tx_skbuff
1201 [entry]);
1202 lp->tx_skbuff[entry] = 0;
1204 dirty_tx++;
1206 lp->dirty_tx = dirty_tx;
1207 outw(AckIntr | DownComplete, ioaddr + EL3_CMD);
1208 if (lp->tx_full
1209 && (lp->cur_tx - dirty_tx <= TX_RING_SIZE - 1)) {
1210 lp->tx_full = 0;
1211 netif_wake_queue(dev);
1214 #ifdef VORTEX_BUS_MASTER
1215 if (status & DMADone) {
1216 outw(0x1000, ioaddr + Wn7_MasterStatus); /* Ack the event. */
1217 dev_kfree_skb_irq(lp->tx_skb); /* Release the transfered buffer */
1218 netif_wake_queue(dev);
1220 #endif
1221 if (status & UpComplete) {
1222 boomerang_rx(dev);
1223 outw(AckIntr | UpComplete, ioaddr + EL3_CMD);
1225 if (status & (AdapterFailure | RxEarly | StatsFull)) {
1226 /* Handle all uncommon interrupts at once. */
1227 if (status & RxEarly) { /* Rx early is unused. */
1228 corkscrew_rx(dev);
1229 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
1231 if (status & StatsFull) { /* Empty statistics. */
1232 static int DoneDidThat = 0;
1233 if (corkscrew_debug > 4)
1234 printk("%s: Updating stats.\n",
1235 dev->name);
1236 update_stats(ioaddr, dev);
1237 /* DEBUG HACK: Disable statistics as an interrupt source. */
1238 /* This occurs when we have the wrong media type! */
1239 if (DoneDidThat == 0 &&
1240 inw(ioaddr + EL3_STATUS) & StatsFull) {
1241 int win, reg;
1242 printk("%s: Updating stats failed, disabling stats as an"
1243 " interrupt source.\n",
1244 dev->name);
1245 for (win = 0; win < 8; win++) {
1246 EL3WINDOW(win);
1247 printk("\n Vortex window %d:", win);
1248 for (reg = 0; reg < 16; reg++)
1249 printk(" %2.2x",
1250 inb(ioaddr + reg));
1252 EL3WINDOW(7);
1253 outw(SetIntrEnb | TxAvailable |
1254 RxComplete | AdapterFailure |
1255 UpComplete | DownComplete |
1256 TxComplete, ioaddr + EL3_CMD);
1257 DoneDidThat++;
1260 if (status & AdapterFailure) {
1261 /* Adapter failure requires Rx reset and reinit. */
1262 outw(RxReset, ioaddr + EL3_CMD);
1263 /* Set the Rx filter to the current state. */
1264 set_rx_mode(dev);
1265 outw(RxEnable, ioaddr + EL3_CMD); /* Re-enable the receiver. */
1266 outw(AckIntr | AdapterFailure,
1267 ioaddr + EL3_CMD);
1271 if (--i < 0) {
1272 printk(KERN_ERR "%s: Too much work in interrupt, status %4.4x. "
1273 "Disabling functions (%4.4x).\n", dev->name,
1274 status, SetStatusEnb | ((~status) & 0x7FE));
1275 /* Disable all pending interrupts. */
1276 outw(SetStatusEnb | ((~status) & 0x7FE),
1277 ioaddr + EL3_CMD);
1278 outw(AckIntr | 0x7FF, ioaddr + EL3_CMD);
1279 break;
1281 /* Acknowledge the IRQ. */
1282 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
1284 } while ((status = inw(ioaddr + EL3_STATUS)) &
1285 (IntLatch | RxComplete));
1287 if (corkscrew_debug > 4)
1288 printk("%s: exiting interrupt, status %4.4x.\n", dev->name,
1289 status);
1292 static int corkscrew_rx(struct net_device *dev)
1294 struct corkscrew_private *vp = (struct corkscrew_private *) dev->priv;
1295 int ioaddr = dev->base_addr;
1296 int i;
1297 short rx_status;
1299 if (corkscrew_debug > 5)
1300 printk(" In rx_packet(), status %4.4x, rx_status %4.4x.\n",
1301 inw(ioaddr + EL3_STATUS), inw(ioaddr + RxStatus));
1302 while ((rx_status = inw(ioaddr + RxStatus)) > 0) {
1303 if (rx_status & 0x4000) { /* Error, update stats. */
1304 unsigned char rx_error = inb(ioaddr + RxErrors);
1305 if (corkscrew_debug > 2)
1306 printk(" Rx error: status %2.2x.\n",
1307 rx_error);
1308 vp->stats.rx_errors++;
1309 if (rx_error & 0x01)
1310 vp->stats.rx_over_errors++;
1311 if (rx_error & 0x02)
1312 vp->stats.rx_length_errors++;
1313 if (rx_error & 0x04)
1314 vp->stats.rx_frame_errors++;
1315 if (rx_error & 0x08)
1316 vp->stats.rx_crc_errors++;
1317 if (rx_error & 0x10)
1318 vp->stats.rx_length_errors++;
1319 } else {
1320 /* The packet length: up to 4.5K!. */
1321 short pkt_len = rx_status & 0x1fff;
1322 struct sk_buff *skb;
1324 skb = dev_alloc_skb(pkt_len + 5 + 2);
1325 if (corkscrew_debug > 4)
1326 printk("Receiving packet size %d status %4.4x.\n",
1327 pkt_len, rx_status);
1328 if (skb != NULL) {
1329 skb->dev = dev;
1330 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
1331 /* 'skb_put()' points to the start of sk_buff data area. */
1332 insl(ioaddr + RX_FIFO,
1333 skb_put(skb, pkt_len),
1334 (pkt_len + 3) >> 2);
1335 outw(RxDiscard, ioaddr + EL3_CMD); /* Pop top Rx packet. */
1336 skb->protocol = eth_type_trans(skb, dev);
1337 netif_rx(skb);
1338 dev->last_rx = jiffies;
1339 vp->stats.rx_packets++;
1340 vp->stats.rx_bytes += skb->len;
1341 /* Wait a limited time to go to next packet. */
1342 for (i = 200; i >= 0; i--)
1343 if (! (inw(ioaddr + EL3_STATUS) & CmdInProgress))
1344 break;
1345 continue;
1346 } else if (corkscrew_debug)
1347 printk("%s: Couldn't allocate a sk_buff of size %d.\n", dev->name, pkt_len);
1349 outw(RxDiscard, ioaddr + EL3_CMD);
1350 vp->stats.rx_dropped++;
1351 /* Wait a limited time to skip this packet. */
1352 for (i = 200; i >= 0; i--)
1353 if (!(inw(ioaddr + EL3_STATUS) & CmdInProgress))
1354 break;
1356 return 0;
1359 static int boomerang_rx(struct net_device *dev)
1361 struct corkscrew_private *vp =
1362 (struct corkscrew_private *) dev->priv;
1363 int entry = vp->cur_rx % RX_RING_SIZE;
1364 int ioaddr = dev->base_addr;
1365 int rx_status;
1367 if (corkscrew_debug > 5)
1368 printk(" In boomerang_rx(), status %4.4x, rx_status %4.4x.\n",
1369 inw(ioaddr + EL3_STATUS), inw(ioaddr + RxStatus));
1370 while ((rx_status = vp->rx_ring[entry].status) & RxDComplete) {
1371 if (rx_status & RxDError) { /* Error, update stats. */
1372 unsigned char rx_error = rx_status >> 16;
1373 if (corkscrew_debug > 2)
1374 printk(" Rx error: status %2.2x.\n",
1375 rx_error);
1376 vp->stats.rx_errors++;
1377 if (rx_error & 0x01)
1378 vp->stats.rx_over_errors++;
1379 if (rx_error & 0x02)
1380 vp->stats.rx_length_errors++;
1381 if (rx_error & 0x04)
1382 vp->stats.rx_frame_errors++;
1383 if (rx_error & 0x08)
1384 vp->stats.rx_crc_errors++;
1385 if (rx_error & 0x10)
1386 vp->stats.rx_length_errors++;
1387 } else {
1388 /* The packet length: up to 4.5K!. */
1389 short pkt_len = rx_status & 0x1fff;
1390 struct sk_buff *skb;
1392 vp->stats.rx_bytes += pkt_len;
1393 if (corkscrew_debug > 4)
1394 printk("Receiving packet size %d status %4.4x.\n",
1395 pkt_len, rx_status);
1397 /* Check if the packet is long enough to just accept without
1398 copying to a properly sized skbuff. */
1399 if (pkt_len < rx_copybreak
1400 && (skb = dev_alloc_skb(pkt_len + 4)) != 0) {
1401 skb->dev = dev;
1402 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
1403 /* 'skb_put()' points to the start of sk_buff data area. */
1404 memcpy(skb_put(skb, pkt_len),
1405 bus_to_virt(vp->rx_ring[entry].
1406 addr), pkt_len);
1407 rx_copy++;
1408 } else {
1409 void *temp;
1410 /* Pass up the skbuff already on the Rx ring. */
1411 skb = vp->rx_skbuff[entry];
1412 vp->rx_skbuff[entry] = NULL;
1413 temp = skb_put(skb, pkt_len);
1414 /* Remove this checking code for final release. */
1415 if (bus_to_virt(vp->rx_ring[entry].addr) != temp)
1416 printk("%s: Warning -- the skbuff addresses do not match"
1417 " in boomerang_rx: %p vs. %p / %p.\n",
1418 dev->name,
1419 bus_to_virt(vp->
1420 rx_ring[entry].
1421 addr), skb->head,
1422 temp);
1423 rx_nocopy++;
1425 skb->protocol = eth_type_trans(skb, dev);
1426 netif_rx(skb);
1427 dev->last_rx = jiffies;
1428 vp->stats.rx_packets++;
1430 entry = (++vp->cur_rx) % RX_RING_SIZE;
1432 /* Refill the Rx ring buffers. */
1433 for (; vp->dirty_rx < vp->cur_rx; vp->dirty_rx++) {
1434 struct sk_buff *skb;
1435 entry = vp->dirty_rx % RX_RING_SIZE;
1436 if (vp->rx_skbuff[entry] == NULL) {
1437 skb = dev_alloc_skb(PKT_BUF_SZ);
1438 if (skb == NULL)
1439 break; /* Bad news! */
1440 skb->dev = dev; /* Mark as being used by this device. */
1441 skb_reserve(skb, 2); /* Align IP on 16 byte boundaries */
1442 vp->rx_ring[entry].addr = virt_to_bus(skb->tail);
1443 vp->rx_skbuff[entry] = skb;
1445 vp->rx_ring[entry].status = 0; /* Clear complete bit. */
1447 return 0;
1450 static int corkscrew_close(struct net_device *dev)
1452 struct corkscrew_private *vp =
1453 (struct corkscrew_private *) dev->priv;
1454 int ioaddr = dev->base_addr;
1455 int i;
1457 netif_stop_queue(dev);
1459 if (corkscrew_debug > 1) {
1460 printk("%s: corkscrew_close() status %4.4x, Tx status %2.2x.\n",
1461 dev->name, inw(ioaddr + EL3_STATUS),
1462 inb(ioaddr + TxStatus));
1463 printk("%s: corkscrew close stats: rx_nocopy %d rx_copy %d"
1464 " tx_queued %d.\n", dev->name, rx_nocopy, rx_copy,
1465 queued_packet);
1468 del_timer(&vp->timer);
1470 /* Turn off statistics ASAP. We update lp->stats below. */
1471 outw(StatsDisable, ioaddr + EL3_CMD);
1473 /* Disable the receiver and transmitter. */
1474 outw(RxDisable, ioaddr + EL3_CMD);
1475 outw(TxDisable, ioaddr + EL3_CMD);
1477 if (dev->if_port == XCVR_10base2)
1478 /* Turn off thinnet power. Green! */
1479 outw(StopCoax, ioaddr + EL3_CMD);
1481 free_irq(dev->irq, dev);
1483 outw(SetIntrEnb | 0x0000, ioaddr + EL3_CMD);
1485 update_stats(ioaddr, dev);
1486 if (vp->full_bus_master_rx) { /* Free Boomerang bus master Rx buffers. */
1487 outl(0, ioaddr + UpListPtr);
1488 for (i = 0; i < RX_RING_SIZE; i++)
1489 if (vp->rx_skbuff[i]) {
1490 dev_kfree_skb(vp->rx_skbuff[i]);
1491 vp->rx_skbuff[i] = 0;
1494 if (vp->full_bus_master_tx) { /* Free Boomerang bus master Tx buffers. */
1495 outl(0, ioaddr + DownListPtr);
1496 for (i = 0; i < TX_RING_SIZE; i++)
1497 if (vp->tx_skbuff[i]) {
1498 dev_kfree_skb(vp->tx_skbuff[i]);
1499 vp->tx_skbuff[i] = 0;
1503 MOD_DEC_USE_COUNT;
1505 return 0;
1508 static struct enet_statistics *corkscrew_get_stats(struct net_device *dev)
1510 struct corkscrew_private *vp =
1511 (struct corkscrew_private *) dev->priv;
1512 unsigned long flags;
1514 if (netif_running(dev)) {
1515 save_flags(flags);
1516 cli();
1517 update_stats(dev->base_addr, dev);
1518 restore_flags(flags);
1520 return &vp->stats;
1523 /* Update statistics.
1524 Unlike with the EL3 we need not worry about interrupts changing
1525 the window setting from underneath us, but we must still guard
1526 against a race condition with a StatsUpdate interrupt updating the
1527 table. This is done by checking that the ASM (!) code generated uses
1528 atomic updates with '+='.
1530 static void update_stats(int ioaddr, struct net_device *dev)
1532 struct corkscrew_private *vp =
1533 (struct corkscrew_private *) dev->priv;
1535 /* Unlike the 3c5x9 we need not turn off stats updates while reading. */
1536 /* Switch to the stats window, and read everything. */
1537 EL3WINDOW(6);
1538 vp->stats.tx_carrier_errors += inb(ioaddr + 0);
1539 vp->stats.tx_heartbeat_errors += inb(ioaddr + 1);
1540 /* Multiple collisions. */ inb(ioaddr + 2);
1541 vp->stats.collisions += inb(ioaddr + 3);
1542 vp->stats.tx_window_errors += inb(ioaddr + 4);
1543 vp->stats.rx_fifo_errors += inb(ioaddr + 5);
1544 vp->stats.tx_packets += inb(ioaddr + 6);
1545 vp->stats.tx_packets += (inb(ioaddr + 9) & 0x30) << 4;
1546 /* Rx packets */ inb(ioaddr + 7);
1547 /* Must read to clear */
1548 /* Tx deferrals */ inb(ioaddr + 8);
1549 /* Don't bother with register 9, an extension of registers 6&7.
1550 If we do use the 6&7 values the atomic update assumption above
1551 is invalid. */
1552 inw(ioaddr + 10); /* Total Rx and Tx octets. */
1553 inw(ioaddr + 12);
1554 /* New: On the Vortex we must also clear the BadSSD counter. */
1555 EL3WINDOW(4);
1556 inb(ioaddr + 12);
1558 /* We change back to window 7 (not 1) with the Vortex. */
1559 EL3WINDOW(7);
1560 return;
1563 /* This new version of set_rx_mode() supports v1.4 kernels.
1564 The Vortex chip has no documented multicast filter, so the only
1565 multicast setting is to receive all multicast frames. At least
1566 the chip has a very clean way to set the mode, unlike many others. */
1567 static void set_rx_mode(struct net_device *dev)
1569 int ioaddr = dev->base_addr;
1570 short new_mode;
1572 if (dev->flags & IFF_PROMISC) {
1573 if (corkscrew_debug > 3)
1574 printk("%s: Setting promiscuous mode.\n",
1575 dev->name);
1576 new_mode = SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm;
1577 } else if ((dev->mc_list) || (dev->flags & IFF_ALLMULTI)) {
1578 new_mode = SetRxFilter | RxStation | RxMulticast | RxBroadcast;
1579 } else
1580 new_mode = SetRxFilter | RxStation | RxBroadcast;
1582 outw(new_mode, ioaddr + EL3_CMD);
1585 #ifdef MODULE
1586 void cleanup_module(void)
1588 struct net_device *next_dev;
1590 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1591 while (root_corkscrew_dev) {
1592 next_dev =
1593 ((struct corkscrew_private *) root_corkscrew_dev->
1594 priv)->next_module;
1595 if (root_corkscrew_dev->dma)
1596 free_dma(root_corkscrew_dev->dma);
1597 unregister_netdev(root_corkscrew_dev);
1598 outw(TotalReset, root_corkscrew_dev->base_addr + EL3_CMD);
1599 release_region(root_corkscrew_dev->base_addr,
1600 CORKSCREW_TOTAL_SIZE);
1601 kfree(root_corkscrew_dev);
1602 root_corkscrew_dev = next_dev;
1605 #endif /* MODULE */
1608 * Local variables:
1609 * compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -c 3c515.c"
1610 * c-indent-level: 4
1611 * tab-width: 4
1612 * End: