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