File: /usr/src/linux-2.4.7/drivers/net/pcmcia/3c589_cs.c

1     /*======================================================================
2     
3         A PCMCIA ethernet driver for the 3com 3c589 card.
4         
5         Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
6     
7         3c589_cs.c 1.156 2001/02/07 00:19:41
8     
9         The network driver code is based on Donald Becker's 3c589 code:
10         
11         Written 1994 by Donald Becker.
12         Copyright 1993 United States Government as represented by the
13         Director, National Security Agency.  This software may be used and
14         distributed according to the terms of the GNU General Public License,
15         incorporated herein by reference.
16         Donald Becker may be reached at becker@scyld.com
17     
18     ======================================================================*/
19     
20     #include <linux/module.h>
21     #include <linux/init.h>
22     #include <linux/kernel.h>
23     #include <linux/sched.h>
24     #include <linux/ptrace.h>
25     #include <linux/slab.h>
26     #include <linux/string.h>
27     #include <linux/timer.h>
28     #include <linux/interrupt.h>
29     #include <linux/in.h>
30     #include <linux/delay.h>
31     #include <asm/io.h>
32     #include <asm/system.h>
33     #include <asm/bitops.h>
34     
35     #include <linux/netdevice.h>
36     #include <linux/etherdevice.h>
37     #include <linux/skbuff.h>
38     #include <linux/if_arp.h>
39     #include <linux/ioport.h>
40     
41     #include <pcmcia/version.h>
42     #include <pcmcia/cs_types.h>
43     #include <pcmcia/cs.h>
44     #include <pcmcia/cistpl.h>
45     #include <pcmcia/cisreg.h>
46     #include <pcmcia/ciscode.h>
47     #include <pcmcia/ds.h>
48     
49     /* To minimize the size of the driver source I only define operating
50        constants if they are used several times.  You'll need the manual
51        if you want to understand driver details. */
52     /* Offsets from base I/O address. */
53     #define EL3_DATA	0x00
54     #define EL3_TIMER	0x0a
55     #define EL3_CMD		0x0e
56     #define EL3_STATUS	0x0e
57     
58     #define EEPROM_READ	0x0080
59     #define EEPROM_BUSY	0x8000
60     
61     #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
62     
63     /* The top five bits written to EL3_CMD are a command, the lower
64        11 bits are the parameter, if applicable. */
65     enum c509cmd {
66         TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
67         RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
68         TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
69         FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
70         SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
71         SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
72         StatsDisable = 22<<11, StopCoax = 23<<11,
73     };
74     
75     enum c509status {
76         IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
77         TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
78         IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000
79     };
80     
81     /* The SetRxFilter command accepts the following classes: */
82     enum RxFilter {
83         RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
84     };
85     
86     /* Register window 1 offsets, the window used in normal operation. */
87     #define TX_FIFO		0x00
88     #define RX_FIFO		0x00
89     #define RX_STATUS 	0x08
90     #define TX_STATUS 	0x0B
91     #define TX_FREE		0x0C	/* Remaining free bytes in Tx buffer. */
92     
93     #define WN0_IRQ		0x08	/* Window 0: Set IRQ line in bits 12-15. */
94     #define WN4_MEDIA	0x0A	/* Window 4: Various transcvr/media bits. */
95     #define MEDIA_TP	0x00C0	/* Enable link beat and jabber for 10baseT. */
96     #define MEDIA_LED	0x0001	/* Enable link light on 3C589E cards. */
97     
98     /* Time in jiffies before concluding Tx hung */
99     #define TX_TIMEOUT	((400*HZ)/1000)
100     
101     struct el3_private {
102         dev_link_t		link;
103         struct net_device	dev;
104         dev_node_t 		node;
105         struct net_device_stats stats;
106         /* For transceiver monitoring */
107         struct timer_list	media;
108         u_short		media_status;
109         u_short		fast_poll;
110         u_long		last_irq;
111     };
112     
113     static char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
114     
115     #ifdef PCMCIA_DEBUG
116     static int pc_debug = PCMCIA_DEBUG;
117     MODULE_PARM(pc_debug, "i");
118     #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
119     static char *version =
120     "3c589_cs.c 1.156 2001/02/07 00:19:41 (David Hinds)";
121     #else
122     #define DEBUG(n, args...)
123     #endif
124     
125     /*====================================================================*/
126     
127     /* Parameters that can be set with 'insmod' */
128     
129     /* Special hook for setting if_port when module is loaded */
130     static int if_port;
131     
132     /* Bit map of interrupts to choose from */
133     static u_int irq_mask = 0xdeb8;
134     static int irq_list[4] = { -1 };
135     
136     MODULE_PARM(if_port, "i");
137     MODULE_PARM(irq_mask, "i");
138     MODULE_PARM(irq_list, "1-4i");
139     
140     /*====================================================================*/
141     
142     static void tc589_config(dev_link_t *link);
143     static void tc589_release(u_long arg);
144     static int tc589_event(event_t event, int priority,
145     		       event_callback_args_t *args);
146     
147     static u_short read_eeprom(ioaddr_t ioaddr, int index);
148     static void tc589_reset(struct net_device *dev);
149     static void media_check(u_long arg);
150     static int el3_config(struct net_device *dev, struct ifmap *map);
151     static int el3_open(struct net_device *dev);
152     static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
153     static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
154     static void update_stats(struct net_device *dev);
155     static struct net_device_stats *el3_get_stats(struct net_device *dev);
156     static int el3_rx(struct net_device *dev);
157     static int el3_close(struct net_device *dev);
158     static void el3_tx_timeout(struct net_device *dev);
159     static void set_multicast_list(struct net_device *dev);
160     
161     static dev_info_t dev_info = "3c589_cs";
162     
163     static dev_link_t *tc589_attach(void);
164     static void tc589_detach(dev_link_t *);
165     
166     static dev_link_t *dev_list;
167     
168     /*======================================================================
169     
170         This bit of code is used to avoid unregistering network devices
171         at inappropriate times.  2.2 and later kernels are fairly picky
172         about when this can happen.
173         
174     ======================================================================*/
175     
176     static void flush_stale_links(void)
177     {
178         dev_link_t *link, *next;
179         for (link = dev_list; link; link = next) {
180     	next = link->next;
181     	if (link->state & DEV_STALE_LINK)
182     	    tc589_detach(link);
183         }
184     }
185     
186     /*====================================================================*/
187     
188     static void cs_error(client_handle_t handle, int func, int ret)
189     {
190         error_info_t err = { func, ret };
191         CardServices(ReportError, handle, &err);
192     }
193     
194     /*======================================================================
195     
196         tc589_attach() creates an "instance" of the driver, allocating
197         local data structures for one device.  The device is registered
198         with Card Services.
199     
200     ======================================================================*/
201     
202     static dev_link_t *tc589_attach(void)
203     {
204         struct el3_private *lp;
205         client_reg_t client_reg;
206         dev_link_t *link;
207         struct net_device *dev;
208         int i, ret;
209     
210         DEBUG(0, "3c589_attach()\n");
211         flush_stale_links();
212         
213         /* Create new ethernet device */
214         lp = kmalloc(sizeof(*lp), GFP_KERNEL);
215         if (!lp) return NULL;
216         memset(lp, 0, sizeof(*lp));
217         link = &lp->link; dev = &lp->dev;
218         link->priv = dev->priv = link->irq.Instance = lp;
219         
220         link->release.function = &tc589_release;
221         link->release.data = (u_long)link;
222         link->io.NumPorts1 = 16;
223         link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
224         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
225         link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
226         if (irq_list[0] == -1)
227     	link->irq.IRQInfo2 = irq_mask;
228         else
229     	for (i = 0; i < 4; i++)
230     	    link->irq.IRQInfo2 |= 1 << irq_list[i];
231         link->irq.Handler = &el3_interrupt;
232         link->conf.Attributes = CONF_ENABLE_IRQ;
233         link->conf.Vcc = 50;
234         link->conf.IntType = INT_MEMORY_AND_IO;
235         link->conf.ConfigIndex = 1;
236         link->conf.Present = PRESENT_OPTION;
237         
238         /* The EL3-specific entries in the device structure. */
239         dev->hard_start_xmit = &el3_start_xmit;
240         dev->set_config = &el3_config;
241         dev->get_stats = &el3_get_stats;
242         dev->set_multicast_list = &set_multicast_list;
243         ether_setup(dev);
244         dev->open = &el3_open;
245         dev->stop = &el3_close;
246     #ifdef HAVE_TX_TIMEOUT
247         dev->tx_timeout = el3_tx_timeout;
248         dev->watchdog_timeo = TX_TIMEOUT;
249     #endif
250         
251         /* Register with Card Services */
252         link->next = dev_list;
253         dev_list = link;
254         client_reg.dev_info = &dev_info;
255         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
256         client_reg.EventMask =
257     	CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
258     	CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
259     	CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
260         client_reg.event_handler = &tc589_event;
261         client_reg.Version = 0x0210;
262         client_reg.event_callback_args.client_data = link;
263         ret = CardServices(RegisterClient, &link->handle, &client_reg);
264         if (ret != 0) {
265     	cs_error(link->handle, RegisterClient, ret);
266     	tc589_detach(link);
267     	return NULL;
268         }
269         
270         return link;
271     } /* tc589_attach */
272     
273     /*======================================================================
274     
275         This deletes a driver "instance".  The device is de-registered
276         with Card Services.  If it has been released, all local data
277         structures are freed.  Otherwise, the structures will be freed
278         when the device is released.
279     
280     ======================================================================*/
281     
282     static void tc589_detach(dev_link_t *link)
283     {
284         struct el3_private *lp = link->priv;
285         dev_link_t **linkp;
286         
287         DEBUG(0, "3c589_detach(0x%p)\n", link);
288         
289         /* Locate device structure */
290         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
291     	if (*linkp == link) break;
292         if (*linkp == NULL)
293     	return;
294     
295         del_timer(&link->release);
296         if (link->state & DEV_CONFIG) {
297     	tc589_release((u_long)link);
298     	if (link->state & DEV_STALE_CONFIG) {
299     	    link->state |= DEV_STALE_LINK;
300     	    return;
301     	}
302         }
303         
304         if (link->handle)
305     	CardServices(DeregisterClient, link->handle);
306         
307         /* Unlink device structure, free bits */
308         *linkp = link->next;
309         if (link->dev)
310     	unregister_netdev(&lp->dev);
311         kfree(lp);
312         
313     } /* tc589_detach */
314     
315     /*======================================================================
316     
317         tc589_config() is scheduled to run after a CARD_INSERTION event
318         is received, to configure the PCMCIA socket, and to make the
319         ethernet device available to the system.
320         
321     ======================================================================*/
322     
323     #define CS_CHECK(fn, args...) \
324     while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
325     
326     static void tc589_config(dev_link_t *link)
327     {
328         client_handle_t handle = link->handle;
329         struct el3_private *lp = link->priv;
330         struct net_device *dev = &lp->dev;
331         tuple_t tuple;
332         cisparse_t parse;
333         u_short buf[32], *phys_addr;
334         int last_fn, last_ret, i, j, multi = 0;
335         ioaddr_t ioaddr;
336         char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
337         
338         DEBUG(0, "3c589_config(0x%p)\n", link);
339     
340         phys_addr = (u_short *)dev->dev_addr;
341         tuple.Attributes = 0;
342         tuple.DesiredTuple = CISTPL_CONFIG;
343         CS_CHECK(GetFirstTuple, handle, &tuple);
344         tuple.TupleData = (cisdata_t *)buf;
345         tuple.TupleDataMax = sizeof(buf);
346         tuple.TupleOffset = 0;
347         CS_CHECK(GetTupleData, handle, &tuple);
348         CS_CHECK(ParseTuple, handle, &tuple, &parse);
349         link->conf.ConfigBase = parse.config.base;
350         link->conf.Present = parse.config.rmask[0];
351         
352         /* Is this a 3c562? */
353         tuple.DesiredTuple = CISTPL_MANFID;
354         tuple.Attributes = TUPLE_RETURN_COMMON;
355         if ((CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) &&
356     	(CardServices(GetTupleData, handle, &tuple) == CS_SUCCESS)) {
357     	if (le16_to_cpu(buf[0]) != MANFID_3COM)
358     	    printk(KERN_INFO "3c589_cs: hmmm, is this really a "
359     		   "3Com card??\n");
360     	multi = (le16_to_cpu(buf[1]) == PRODID_3COM_3C562);
361         }
362         
363         /* Configure card */
364         link->state |= DEV_CONFIG;
365     
366         /* For the 3c562, the base address must be xx00-xx7f */
367         link->io.IOAddrLines = 16;
368         for (i = j = 0; j < 0x400; j += 0x10) {
369     	if (multi && (j & 0x80)) continue;
370     	link->io.BasePort1 = j ^ 0x300;
371     	i = CardServices(RequestIO, link->handle, &link->io);
372     	if (i == CS_SUCCESS) break;
373         }
374         if (i != CS_SUCCESS) {
375     	cs_error(link->handle, RequestIO, i);
376     	goto failed;
377         }
378         CS_CHECK(RequestIRQ, link->handle, &link->irq);
379         CS_CHECK(RequestConfiguration, link->handle, &link->conf);
380     	
381         dev->irq = link->irq.AssignedIRQ;
382         dev->base_addr = link->io.BasePort1;
383         if (register_netdev(dev) != 0) {
384     	printk(KERN_NOTICE "3c589_cs: register_netdev() failed\n");
385     	goto failed;
386         }
387         
388         ioaddr = dev->base_addr;
389         EL3WINDOW(0);
390     
391         /* The 3c589 has an extra EEPROM for configuration info, including
392            the hardware address.  The 3c562 puts the address in the CIS. */
393         tuple.DesiredTuple = 0x88;
394         if (CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) {
395     	CardServices(GetTupleData, handle, &tuple);
396     	for (i = 0; i < 3; i++)
397     	    phys_addr[i] = htons(buf[i]);
398         } else {
399     	for (i = 0; i < 3; i++)
400     	    phys_addr[i] = htons(read_eeprom(ioaddr, i));
401     	if (phys_addr[0] == 0x6060) {
402     	    printk(KERN_NOTICE "3c589_cs: IO port conflict at 0x%03lx"
403     		   "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
404     	    goto failed;
405     	}
406         }
407     
408         strcpy(lp->node.dev_name, dev->name);
409         link->dev = &lp->node;
410         link->state &= ~DEV_CONFIG_PENDING;
411         
412         /* The address and resource configuration register aren't loaded from
413            the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
414         outw(0x3f00, ioaddr + 8);
415     
416         /* The if_port symbol can be set when the module is loaded */
417         if ((if_port >= 0) && (if_port <= 3))
418     	dev->if_port = if_port;
419         else
420     	printk(KERN_NOTICE "3c589_cs: invalid if_port requested\n");
421         
422         printk(KERN_INFO "%s: 3Com 3c%s, io %#3lx, irq %d, hw_addr ",
423     	   dev->name, (multi ? "562" : "589"), dev->base_addr,
424     	   dev->irq);
425         for (i = 0; i < 6; i++)
426     	printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
427         i = inl(ioaddr);
428         printk(KERN_INFO "  %dK FIFO split %s Rx:Tx, %s xcvr\n",
429     	   (i & 7) ? 32 : 8, ram_split[(i >> 16) & 3],
430     	   if_names[dev->if_port]);
431         return;
432     
433     cs_failed:
434         cs_error(link->handle, last_fn, last_ret);
435     failed:
436         tc589_release((u_long)link);
437         return;
438         
439     } /* tc589_config */
440     
441     /*======================================================================
442     
443         After a card is removed, tc589_release() will unregister the net
444         device, and release the PCMCIA configuration.  If the device is
445         still open, this will be postponed until it is closed.
446         
447     ======================================================================*/
448     
449     static void tc589_release(u_long arg)
450     {
451         dev_link_t *link = (dev_link_t *)arg;
452     
453         DEBUG(0, "3c589_release(0x%p)\n", link);
454         
455         if (link->open) {
456     	DEBUG(1, "3c589_cs: release postponed, '%s' still open\n",
457     	      link->dev->dev_name);
458     	link->state |= DEV_STALE_CONFIG;
459     	return;
460         }
461         
462         CardServices(ReleaseConfiguration, link->handle);
463         CardServices(ReleaseIO, link->handle, &link->io);
464         CardServices(ReleaseIRQ, link->handle, &link->irq);
465         
466         link->state &= ~DEV_CONFIG;
467         
468     } /* tc589_release */
469     
470     /*======================================================================
471     
472         The card status event handler.  Mostly, this schedules other
473         stuff to run after an event is received.  A CARD_REMOVAL event
474         also sets some flags to discourage the net drivers from trying
475         to talk to the card any more.
476         
477     ======================================================================*/
478     
479     static int tc589_event(event_t event, int priority,
480     		       event_callback_args_t *args)
481     {
482         dev_link_t *link = args->client_data;
483         struct el3_private *lp = link->priv;
484         struct net_device *dev = &lp->dev;
485         
486         DEBUG(1, "3c589_event(0x%06x)\n", event);
487         
488         switch (event) {
489         case CS_EVENT_CARD_REMOVAL:
490     	link->state &= ~DEV_PRESENT;
491     	if (link->state & DEV_CONFIG) {
492     	    netif_device_detach(dev);
493     	    mod_timer(&link->release, jiffies + HZ/20);
494     	}
495     	break;
496         case CS_EVENT_CARD_INSERTION:
497     	link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
498     	tc589_config(link);
499     	break;
500         case CS_EVENT_PM_SUSPEND:
501     	link->state |= DEV_SUSPEND;
502     	/* Fall through... */
503         case CS_EVENT_RESET_PHYSICAL:
504     	if (link->state & DEV_CONFIG) {
505     	    if (link->open)
506     		netif_device_detach(dev);
507     	    CardServices(ReleaseConfiguration, link->handle);
508     	}
509     	break;
510         case CS_EVENT_PM_RESUME:
511     	link->state &= ~DEV_SUSPEND;
512     	/* Fall through... */
513         case CS_EVENT_CARD_RESET:
514     	if (link->state & DEV_CONFIG) {
515     	    CardServices(RequestConfiguration, link->handle, &link->conf);
516     	    if (link->open) {
517     		tc589_reset(dev);
518     		netif_device_attach(dev);
519     	    }
520     	}
521     	break;
522         }
523         return 0;
524     } /* tc589_event */
525     
526     /*====================================================================*/
527     
528     /*
529       Use this for commands that may take time to finish
530     */
531     static void wait_for_completion(struct net_device *dev, int cmd)
532     {
533         int i = 100;
534         outw(cmd, dev->base_addr + EL3_CMD);
535         while (--i > 0)
536     	if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
537         if (i == 0)
538     	printk(KERN_NOTICE "%s: command 0x%04x did not complete!\n",
539     	       dev->name, cmd);
540     }
541     
542     /*
543       Read a word from the EEPROM using the regular EEPROM access register.
544       Assume that we are in register window zero.
545     */
546     static u_short read_eeprom(ioaddr_t ioaddr, int index)
547     {
548         int i;
549         outw(EEPROM_READ + index, ioaddr + 10);
550         /* Reading the eeprom takes 162 us */
551         for (i = 1620; i >= 0; i--)
552     	if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
553     	    break;
554         return inw(ioaddr + 12);
555     }
556     
557     /*
558       Set transceiver type, perhaps to something other than what the user
559       specified in dev->if_port.
560     */
561     static void tc589_set_xcvr(struct net_device *dev, int if_port)
562     {
563         struct el3_private *lp = (struct el3_private *)dev->priv;
564         ioaddr_t ioaddr = dev->base_addr;
565         
566         EL3WINDOW(0);
567         switch (if_port) {
568         case 0: case 1: outw(0, ioaddr + 6); break;
569         case 2: outw(3<<14, ioaddr + 6); break;
570         case 3: outw(1<<14, ioaddr + 6); break;
571         }
572         /* On PCMCIA, this just turns on the LED */
573         outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
574         /* 10baseT interface, enable link beat and jabber check. */
575         EL3WINDOW(4);
576         outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
577         EL3WINDOW(1);
578         if (if_port == 2)
579     	lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
580         else
581     	lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
582     }
583     
584     static void dump_status(struct net_device *dev)
585     {
586         ioaddr_t ioaddr = dev->base_addr;
587         EL3WINDOW(1);
588         printk(KERN_INFO "  irq status %04x, rx status %04x, tx status "
589     	   "%02x  tx free %04x\n", inw(ioaddr+EL3_STATUS),
590     	   inw(ioaddr+RX_STATUS), inb(ioaddr+TX_STATUS),
591     	   inw(ioaddr+TX_FREE));
592         EL3WINDOW(4);
593         printk(KERN_INFO "  diagnostics: fifo %04x net %04x ethernet %04x"
594     	   " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
595     	   inw(ioaddr+0x08), inw(ioaddr+0x0a));
596         EL3WINDOW(1);
597     }
598     
599     /* Reset and restore all of the 3c589 registers. */
600     static void tc589_reset(struct net_device *dev)
601     {
602         ioaddr_t ioaddr = dev->base_addr;
603         int i;
604         
605         EL3WINDOW(0);
606         outw(0x0001, ioaddr + 4);			/* Activate board. */ 
607         outw(0x3f00, ioaddr + 8);			/* Set the IRQ line. */
608         
609         /* Set the station address in window 2. */
610         EL3WINDOW(2);
611         for (i = 0; i < 6; i++)
612     	outb(dev->dev_addr[i], ioaddr + i);
613     
614         tc589_set_xcvr(dev, dev->if_port);
615         
616         /* Switch to the stats window, and clear all stats by reading. */
617         outw(StatsDisable, ioaddr + EL3_CMD);
618         EL3WINDOW(6);
619         for (i = 0; i < 9; i++)
620     	inb(ioaddr+i);
621         inw(ioaddr + 10);
622         inw(ioaddr + 12);
623         
624         /* Switch to register set 1 for normal use. */
625         EL3WINDOW(1);
626     
627         /* Accept b-cast and phys addr only. */
628         outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
629         outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
630         outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
631         outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
632         /* Allow status bits to be seen. */
633         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
634         /* Ack all pending events, and set active indicator mask. */
635         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
636     	 ioaddr + EL3_CMD);
637         outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
638     	 | AdapterFailure, ioaddr + EL3_CMD);
639     }
640     
641     static int el3_config(struct net_device *dev, struct ifmap *map)
642     {
643         if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
644     	if (map->port <= 3) {
645     	    dev->if_port = map->port;
646     	    printk(KERN_INFO "%s: switched to %s port\n",
647     		   dev->name, if_names[dev->if_port]);
648     	    tc589_set_xcvr(dev, dev->if_port);
649     	} else
650     	    return -EINVAL;
651         }
652         return 0;
653     }
654     
655     static int el3_open(struct net_device *dev)
656     {
657         struct el3_private *lp = (struct el3_private *)dev->priv;
658         dev_link_t *link = &lp->link;
659         
660         if (!DEV_OK(link))
661     	return -ENODEV;
662     
663         link->open++;
664         MOD_INC_USE_COUNT;
665         netif_start_queue(dev);
666         
667         tc589_reset(dev);
668         lp->media.function = &media_check;
669         lp->media.data = (u_long)lp;
670         lp->media.expires = jiffies + HZ;
671         add_timer(&lp->media);
672     
673         DEBUG(1, "%s: opened, status %4.4x.\n",
674     	  dev->name, inw(dev->base_addr + EL3_STATUS));
675         
676         return 0;
677     }
678     
679     static void el3_tx_timeout(struct net_device *dev)
680     {
681         struct el3_private *lp = (struct el3_private *)dev->priv;
682         ioaddr_t ioaddr = dev->base_addr;
683         
684         printk(KERN_NOTICE "%s: Transmit timed out!\n", dev->name);
685         dump_status(dev);
686         lp->stats.tx_errors++;
687         dev->trans_start = jiffies;
688         /* Issue TX_RESET and TX_START commands. */
689         wait_for_completion(dev, TxReset);
690         outw(TxEnable, ioaddr + EL3_CMD);
691         netif_wake_queue(dev);
692     }
693     
694     static void pop_tx_status(struct net_device *dev)
695     {
696         struct el3_private *lp = (struct el3_private *)dev->priv;
697         ioaddr_t ioaddr = dev->base_addr;
698         int i;
699         
700         /* Clear the Tx status stack. */
701         for (i = 32; i > 0; i--) {
702     	u_char tx_status = inb(ioaddr + TX_STATUS);
703     	if (!(tx_status & 0x84)) break;
704     	/* reset transmitter on jabber error or underrun */
705     	if (tx_status & 0x30)
706     	    wait_for_completion(dev, TxReset);
707     	if (tx_status & 0x38) {
708     	    DEBUG(1, "%s: transmit error: status 0x%02x\n",
709     		  dev->name, tx_status);
710     	    outw(TxEnable, ioaddr + EL3_CMD);
711     	    lp->stats.tx_aborted_errors++;
712     	}
713     	outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
714         }
715     }
716     
717     static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
718     {
719         ioaddr_t ioaddr = dev->base_addr;
720     
721         DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
722     	  "status %4.4x.\n", dev->name, (long)skb->len,
723     	  inw(ioaddr + EL3_STATUS));
724     
725         ((struct el3_private *)dev->priv)->stats.tx_bytes += skb->len;
726     
727         /* Put out the doubleword header... */
728         outw(skb->len, ioaddr + TX_FIFO);
729         outw(0x00, ioaddr + TX_FIFO);
730         /* ... and the packet rounded to a doubleword. */
731         outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
732     
733         dev->trans_start = jiffies;
734         if (inw(ioaddr + TX_FREE) <= 1536) {
735     	netif_stop_queue(dev);
736     	/* Interrupt us when the FIFO has room for max-sized packet. */
737     	outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
738         }
739     
740         dev_kfree_skb(skb);
741         pop_tx_status(dev);
742         
743         return 0;
744     }
745     
746     /* The EL3 interrupt handler. */
747     static void el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
748     {
749         struct el3_private *lp = dev_id;
750         struct net_device *dev = &lp->dev;
751         ioaddr_t ioaddr, status;
752         int i = 0;
753         
754         if (!netif_device_present(dev))
755     	return;
756         ioaddr = dev->base_addr;
757     
758         DEBUG(3, "%s: interrupt, status %4.4x.\n",
759     	  dev->name, inw(ioaddr + EL3_STATUS));
760         
761         while ((status = inw(ioaddr + EL3_STATUS)) &
762     	(IntLatch | RxComplete | StatsFull)) {
763     	if (!netif_device_present(dev) ||
764     	    ((status & 0xe000) != 0x2000)) {
765     	    DEBUG(1, "%s: interrupt from dead card\n", dev->name);
766     	    break;
767     	}
768     	
769     	if (status & RxComplete)
770     	    el3_rx(dev);
771     	
772     	if (status & TxAvailable) {
773     	    DEBUG(3, "    TX room bit was handled.\n");
774     	    /* There's room in the FIFO for a full-sized packet. */
775     	    outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
776     	    netif_wake_queue(dev);
777     	}
778     	
779     	if (status & TxComplete)
780     	    pop_tx_status(dev);
781     
782     	if (status & (AdapterFailure | RxEarly | StatsFull)) {
783     	    /* Handle all uncommon interrupts. */
784     	    if (status & StatsFull)		/* Empty statistics. */
785     		update_stats(dev);
786     	    if (status & RxEarly) {		/* Rx early is unused. */
787     		el3_rx(dev);
788     		outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
789     	    }
790     	    if (status & AdapterFailure) {
791     		u16 fifo_diag;
792     		EL3WINDOW(4);
793     		fifo_diag = inw(ioaddr + 4);
794     		EL3WINDOW(1);
795     		printk(KERN_NOTICE "%s: adapter failure, FIFO diagnostic"
796     		       " register %04x.\n", dev->name, fifo_diag);
797     		if (fifo_diag & 0x0400) {
798     		    /* Tx overrun */
799     		    wait_for_completion(dev, TxReset);
800     		    outw(TxEnable, ioaddr + EL3_CMD);
801     		}
802     		if (fifo_diag & 0x2000) {
803     		    /* Rx underrun */
804     		    wait_for_completion(dev, RxReset);
805     		    set_multicast_list(dev);
806     		    outw(RxEnable, ioaddr + EL3_CMD);
807     		}
808     		outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
809     	    }
810     	}
811     	
812     	if (++i > 10) {
813     	    printk(KERN_NOTICE "%s: infinite loop in interrupt, "
814     		   "status %4.4x.\n", dev->name, status);
815     	    /* Clear all interrupts */
816     	    outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
817     	    break;
818     	}
819     	/* Acknowledge the IRQ. */
820     	outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
821         }
822     
823         lp->last_irq = jiffies;
824         DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
825     	  dev->name, inw(ioaddr + EL3_STATUS));
826         return;
827     }
828     
829     static void media_check(u_long arg)
830     {
831         struct el3_private *lp = (struct el3_private *)(arg);
832         struct net_device *dev = &lp->dev;
833         ioaddr_t ioaddr = dev->base_addr;
834         u_short media, errs;
835         u_long flags;
836     
837         if (!netif_device_present(dev)) goto reschedule;
838     
839         EL3WINDOW(1);
840         /* Check for pending interrupt with expired latency timer: with
841            this, we can limp along even if the interrupt is blocked */
842         if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
843     	(inb(ioaddr + EL3_TIMER) == 0xff)) {
844     	if (!lp->fast_poll)
845     	    printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
846     	el3_interrupt(dev->irq, lp, NULL);
847     	lp->fast_poll = HZ;
848         }
849         if (lp->fast_poll) {
850     	lp->fast_poll--;
851     	lp->media.expires = jiffies + 1;
852     	add_timer(&lp->media);
853     	return;
854         }
855         
856         save_flags(flags);
857         cli();
858         EL3WINDOW(4);
859         media = inw(ioaddr+WN4_MEDIA) & 0xc810;
860     
861         /* Ignore collisions unless we've had no irq's recently */
862         if (jiffies - lp->last_irq < HZ) {
863     	media &= ~0x0010;
864         } else {
865     	/* Try harder to detect carrier errors */
866     	EL3WINDOW(6);
867     	outw(StatsDisable, ioaddr + EL3_CMD);
868     	errs = inb(ioaddr + 0);
869     	outw(StatsEnable, ioaddr + EL3_CMD);
870     	lp->stats.tx_carrier_errors += errs;
871     	if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
872         }
873     
874         if (media != lp->media_status) {
875     	if ((media & lp->media_status & 0x8000) &&
876     	    ((lp->media_status ^ media) & 0x0800))
877     	    printk(KERN_INFO "%s: %s link beat\n", dev->name,
878     		   (lp->media_status & 0x0800 ? "lost" : "found"));
879     	else if ((media & lp->media_status & 0x4000) &&
880     		 ((lp->media_status ^ media) & 0x0010))
881     	    printk(KERN_INFO "%s: coax cable %s\n", dev->name,
882     		   (lp->media_status & 0x0010 ? "ok" : "problem"));
883     	if (dev->if_port == 0) {
884     	    if (media & 0x8000) {
885     		if (media & 0x0800)
886     		    printk(KERN_INFO "%s: flipped to 10baseT\n",
887     			   dev->name);
888     		else
889     		    tc589_set_xcvr(dev, 2);
890     	    } else if (media & 0x4000) {
891     		if (media & 0x0010)
892     		    tc589_set_xcvr(dev, 1);
893     		else
894     		    printk(KERN_INFO "%s: flipped to 10base2\n",
895     			   dev->name);
896     	    }
897     	}
898     	lp->media_status = media;
899         }
900         
901         EL3WINDOW(1);
902         restore_flags(flags);
903     
904     reschedule:
905         lp->media.expires = jiffies + HZ;
906         add_timer(&lp->media);
907     }
908     
909     static struct net_device_stats *el3_get_stats(struct net_device *dev)
910     {
911         struct el3_private *lp = (struct el3_private *)dev->priv;
912         unsigned long flags;
913         dev_link_t *link = &lp->link;
914     
915         if (DEV_OK(link)) {
916     	save_flags(flags);
917     	cli();
918     	update_stats(dev);
919     	restore_flags(flags);
920         }
921         return &lp->stats;
922     }
923     
924     /*
925       Update statistics.  We change to register window 6, so this should be run
926       single-threaded if the device is active. This is expected to be a rare
927       operation, and it's simpler for the rest of the driver to assume that
928       window 1 is always valid rather than use a special window-state variable.
929     */
930     static void update_stats(struct net_device *dev)
931     {
932         struct el3_private *lp = (struct el3_private *)dev->priv;
933         ioaddr_t ioaddr = dev->base_addr;
934     
935         DEBUG(2, "%s: updating the statistics.\n", dev->name);
936         /* Turn off statistics updates while reading. */
937         outw(StatsDisable, ioaddr + EL3_CMD);
938         /* Switch to the stats window, and read everything. */
939         EL3WINDOW(6);
940         lp->stats.tx_carrier_errors 	+= inb(ioaddr + 0);
941         lp->stats.tx_heartbeat_errors	+= inb(ioaddr + 1);
942         /* Multiple collisions. */	   	inb(ioaddr + 2);
943         lp->stats.collisions		+= inb(ioaddr + 3);
944         lp->stats.tx_window_errors		+= inb(ioaddr + 4);
945         lp->stats.rx_fifo_errors		+= inb(ioaddr + 5);
946         lp->stats.tx_packets		+= inb(ioaddr + 6);
947         /* Rx packets   */			inb(ioaddr + 7);
948         /* Tx deferrals */			inb(ioaddr + 8);
949         /* Rx octets */			inw(ioaddr + 10);
950         /* Tx octets */			inw(ioaddr + 12);
951         
952         /* Back to window 1, and turn statistics back on. */
953         EL3WINDOW(1);
954         outw(StatsEnable, ioaddr + EL3_CMD);
955     }
956     
957     static int el3_rx(struct net_device *dev)
958     {
959         struct el3_private *lp = (struct el3_private *)dev->priv;
960         ioaddr_t ioaddr = dev->base_addr;
961         int worklimit = 32;
962         short rx_status;
963         
964         DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
965     	  dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
966         while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
967     	   (--worklimit >= 0)) {
968     	if (rx_status & 0x4000) { /* Error, update stats. */
969     	    short error = rx_status & 0x3800;
970     	    lp->stats.rx_errors++;
971     	    switch (error) {
972     	    case 0x0000:	lp->stats.rx_over_errors++; break;
973     	    case 0x0800:	lp->stats.rx_length_errors++; break;
974     	    case 0x1000:	lp->stats.rx_frame_errors++; break;
975     	    case 0x1800:	lp->stats.rx_length_errors++; break;
976     	    case 0x2000:	lp->stats.rx_frame_errors++; break;
977     	    case 0x2800:	lp->stats.rx_crc_errors++; break;
978     	    }
979     	} else {
980     	    short pkt_len = rx_status & 0x7ff;
981     	    struct sk_buff *skb;
982     	    
983     	    skb = dev_alloc_skb(pkt_len+5);
984     	    
985     	    DEBUG(3, "    Receiving packet size %d status %4.4x.\n",
986     		  pkt_len, rx_status);
987     	    if (skb != NULL) {
988     		skb->dev = dev;
989     		
990     		skb_reserve(skb, 2);
991     		insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
992     			(pkt_len+3)>>2);
993     		skb->protocol = eth_type_trans(skb, dev);
994     		
995     		netif_rx(skb);
996     		dev->last_rx = jiffies;
997     		lp->stats.rx_packets++;
998     		lp->stats.rx_bytes += pkt_len;
999     	    } else {
1000     		DEBUG(1, "%s: couldn't allocate a sk_buff of"
1001     		      " size %d.\n", dev->name, pkt_len);
1002     		lp->stats.rx_dropped++;
1003     	    }
1004     	}
1005     	/* Pop the top of the Rx FIFO */
1006     	wait_for_completion(dev, RxDiscard);
1007         }
1008         if (worklimit == 0)
1009     	printk(KERN_NOTICE "%s: too much work in el3_rx!\n", dev->name);
1010         return 0;
1011     }
1012     
1013     static void set_multicast_list(struct net_device *dev)
1014     {
1015         struct el3_private *lp = dev->priv;
1016         dev_link_t *link = &lp->link;
1017         ioaddr_t ioaddr = dev->base_addr;
1018         u_short opts = SetRxFilter | RxStation | RxBroadcast;
1019     
1020         if (!(DEV_OK(link))) return;
1021         if (dev->flags & IFF_PROMISC)
1022     	opts |= RxMulticast | RxProm;
1023         else if (dev->mc_count || (dev->flags & IFF_ALLMULTI))
1024     	opts |= RxMulticast;
1025         outw(opts, ioaddr + EL3_CMD);
1026     }
1027     
1028     static int el3_close(struct net_device *dev)
1029     {
1030         struct el3_private *lp = dev->priv;
1031         dev_link_t *link = &lp->link;
1032         ioaddr_t ioaddr = dev->base_addr;
1033         
1034         DEBUG(1, "%s: shutting down ethercard.\n", dev->name);
1035     
1036         if (DEV_OK(link)) {
1037     	/* Turn off statistics ASAP.  We update lp->stats below. */
1038     	outw(StatsDisable, ioaddr + EL3_CMD);
1039     	
1040     	/* Disable the receiver and transmitter. */
1041     	outw(RxDisable, ioaddr + EL3_CMD);
1042     	outw(TxDisable, ioaddr + EL3_CMD);
1043     	
1044     	if (dev->if_port == 2)
1045     	    /* Turn off thinnet power.  Green! */
1046     	    outw(StopCoax, ioaddr + EL3_CMD);
1047     	else if (dev->if_port == 1) {
1048     	    /* Disable link beat and jabber */
1049     	    EL3WINDOW(4);
1050     	    outw(0, ioaddr + WN4_MEDIA);
1051     	}
1052     	
1053     	/* Switching back to window 0 disables the IRQ. */
1054     	EL3WINDOW(0);
1055     	/* But we explicitly zero the IRQ line select anyway. */
1056     	outw(0x0f00, ioaddr + WN0_IRQ);
1057             
1058     	/* Check if the card still exists */
1059     	if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
1060     	    update_stats(dev);
1061         }
1062     
1063         link->open--;
1064         netif_stop_queue(dev);
1065         del_timer(&lp->media);
1066         if (link->state & DEV_STALE_CONFIG)
1067     	mod_timer(&link->release, jiffies + HZ/20);
1068         
1069         MOD_DEC_USE_COUNT;
1070         
1071         return 0;
1072     }
1073     
1074     /*====================================================================*/
1075     
1076     static int __init init_3c589_cs(void)
1077     {
1078         servinfo_t serv;
1079         DEBUG(0, "%s\n", version);
1080         CardServices(GetCardServicesInfo, &serv);
1081         if (serv.Revision != CS_RELEASE_CODE) {
1082     	printk(KERN_NOTICE "3c589_cs: Card Services release "
1083     	       "does not match!\n");
1084     	return -1;
1085         }
1086         register_pccard_driver(&dev_info, &tc589_attach, &tc589_detach);
1087         return 0;
1088     }
1089     
1090     static void __exit exit_3c589_cs(void)
1091     {
1092         DEBUG(0, "3c589_cs: unloading\n");
1093         unregister_pccard_driver(&dev_info);
1094         while (dev_list != NULL)
1095     	tc589_detach(dev_list);
1096     }
1097     
1098     module_init(init_3c589_cs);
1099     module_exit(exit_3c589_cs);
1100