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