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