ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / 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.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     Updated for 2.5.x by Alan Cox <alan@redhat.com>
19
20 ======================================================================*/
21
22 #define DRV_NAME        "3c589_cs"
23 #define DRV_VERSION     "1.162-ac"
24
25 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/ptrace.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/in.h>
34 #include <linux/delay.h>
35 #include <linux/ethtool.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/if_arp.h>
40 #include <linux/ioport.h>
41
42 #include <pcmcia/version.h>
43 #include <pcmcia/cs_types.h>
44 #include <pcmcia/cs.h>
45 #include <pcmcia/cistpl.h>
46 #include <pcmcia/cisreg.h>
47 #include <pcmcia/ciscode.h>
48 #include <pcmcia/ds.h>
49
50 #include <asm/uaccess.h>
51 #include <asm/io.h>
52 #include <asm/system.h>
53 #include <asm/bitops.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. */
71 enum 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
81 enum 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: */
88 enum 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
107 struct el3_private {
108     dev_link_t          link;
109     dev_node_t          node;
110     struct net_device_stats stats;
111     /* For transceiver monitoring */
112     struct timer_list   media;
113     u16                 media_status;
114     u16                 fast_poll;
115     unsigned long       last_irq;
116     spinlock_t          lock;
117 };
118
119 static char *if_names[] = { "auto", "10baseT", "10base2", "AUI" };
120
121 /*====================================================================*/
122
123 /* Module parameters */
124
125 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
126 MODULE_DESCRIPTION("3Com 3c589 series PCMCIA ethernet driver");
127 MODULE_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 */
132 INT_MODULE_PARM(if_port, 0);
133
134 /* Bit map of interrupts to choose from */
135 INT_MODULE_PARM(irq_mask, 0xdeb8);
136 static int irq_list[4] = { -1 };
137 MODULE_PARM(irq_list, "1-4i");
138
139 #ifdef PCMCIA_DEBUG
140 INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
141 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
142 static char *version =
143 DRV_NAME ".c " DRV_VERSION " 2001/10/13 00:08:50 (David Hinds)";
144 #else
145 #define DEBUG(n, args...)
146 #endif
147
148 /*====================================================================*/
149
150 static void tc589_config(dev_link_t *link);
151 static void tc589_release(dev_link_t *link);
152 static int tc589_event(event_t event, int priority,
153                        event_callback_args_t *args);
154
155 static u16 read_eeprom(ioaddr_t ioaddr, int index);
156 static void tc589_reset(struct net_device *dev);
157 static void media_check(unsigned long arg);
158 static int el3_config(struct net_device *dev, struct ifmap *map);
159 static int el3_open(struct net_device *dev);
160 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
161 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
162 static void update_stats(struct net_device *dev);
163 static struct net_device_stats *el3_get_stats(struct net_device *dev);
164 static int el3_rx(struct net_device *dev);
165 static int el3_close(struct net_device *dev);
166 static void el3_tx_timeout(struct net_device *dev);
167 static void set_multicast_list(struct net_device *dev);
168 static struct ethtool_ops netdev_ethtool_ops;
169
170 static dev_info_t dev_info = "3c589_cs";
171
172 static dev_link_t *tc589_attach(void);
173 static void tc589_detach(dev_link_t *);
174
175 static dev_link_t *dev_list;
176
177 /*======================================================================
178
179     tc589_attach() creates an "instance" of the driver, allocating
180     local data structures for one device.  The device is registered
181     with Card Services.
182
183 ======================================================================*/
184
185 static dev_link_t *tc589_attach(void)
186 {
187     struct el3_private *lp;
188     client_reg_t client_reg;
189     dev_link_t *link;
190     struct net_device *dev;
191     int i, ret;
192
193     DEBUG(0, "3c589_attach()\n");
194     
195     /* Create new ethernet device */
196     dev = alloc_etherdev(sizeof(struct el3_private));
197     if (!dev)
198          return NULL;
199     lp = netdev_priv(dev);
200     link = &lp->link;
201     link->priv = dev;
202
203     spin_lock_init(&lp->lock);
204     link->io.NumPorts1 = 16;
205     link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
206     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
207     link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
208     if (irq_list[0] == -1)
209         link->irq.IRQInfo2 = irq_mask;
210     else
211         for (i = 0; i < 4; i++)
212             link->irq.IRQInfo2 |= 1 << irq_list[i];
213     link->irq.Handler = &el3_interrupt;
214     link->irq.Instance = dev;
215     link->conf.Attributes = CONF_ENABLE_IRQ;
216     link->conf.Vcc = 50;
217     link->conf.IntType = INT_MEMORY_AND_IO;
218     link->conf.ConfigIndex = 1;
219     link->conf.Present = PRESENT_OPTION;
220     
221     /* The EL3-specific entries in the device structure. */
222     SET_MODULE_OWNER(dev);
223     dev->hard_start_xmit = &el3_start_xmit;
224     dev->set_config = &el3_config;
225     dev->get_stats = &el3_get_stats;
226     dev->set_multicast_list = &set_multicast_list;
227     dev->open = &el3_open;
228     dev->stop = &el3_close;
229 #ifdef HAVE_TX_TIMEOUT
230     dev->tx_timeout = el3_tx_timeout;
231     dev->watchdog_timeo = TX_TIMEOUT;
232 #endif
233     SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
234
235     /* Register with Card Services */
236     link->next = dev_list;
237     dev_list = link;
238     client_reg.dev_info = &dev_info;
239     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
240     client_reg.EventMask =
241         CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
242         CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
243         CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
244     client_reg.event_handler = &tc589_event;
245     client_reg.Version = 0x0210;
246     client_reg.event_callback_args.client_data = link;
247     ret = pcmcia_register_client(&link->handle, &client_reg);
248     if (ret != 0) {
249         cs_error(link->handle, RegisterClient, ret);
250         tc589_detach(link);
251         return NULL;
252     }
253     
254     return link;
255 } /* tc589_attach */
256
257 /*======================================================================
258
259     This deletes a driver "instance".  The device is de-registered
260     with Card Services.  If it has been released, all local data
261     structures are freed.  Otherwise, the structures will be freed
262     when the device is released.
263
264 ======================================================================*/
265
266 static void tc589_detach(dev_link_t *link)
267 {
268     struct net_device *dev = link->priv;
269     dev_link_t **linkp;
270     
271     DEBUG(0, "3c589_detach(0x%p)\n", link);
272     
273     /* Locate device structure */
274     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
275         if (*linkp == link) break;
276     if (*linkp == NULL)
277         return;
278
279     if (link->state & DEV_CONFIG)
280         tc589_release(link);
281     
282     if (link->handle)
283         pcmcia_deregister_client(link->handle);
284     
285     /* Unlink device structure, free bits */
286     *linkp = link->next;
287     if (link->dev)
288         unregister_netdev(dev);
289     free_netdev(dev);
290 } /* tc589_detach */
291
292 /*======================================================================
293
294     tc589_config() is scheduled to run after a CARD_INSERTION event
295     is received, to configure the PCMCIA socket, and to make the
296     ethernet device available to the system.
297     
298 ======================================================================*/
299
300 #define CS_CHECK(fn, ret) \
301 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
302
303 static void tc589_config(dev_link_t *link)
304 {
305     client_handle_t handle = link->handle;
306     struct net_device *dev = link->priv;
307     struct el3_private *lp = netdev_priv(dev);
308     tuple_t tuple;
309     cisparse_t parse;
310     u16 buf[32], *phys_addr;
311     int last_fn, last_ret, i, j, multi = 0, fifo;
312     ioaddr_t ioaddr;
313     char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
314     
315     DEBUG(0, "3c589_config(0x%p)\n", link);
316
317     phys_addr = (u16 *)dev->dev_addr;
318     tuple.Attributes = 0;
319     tuple.DesiredTuple = CISTPL_CONFIG;
320     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
321     tuple.TupleData = (cisdata_t *)buf;
322     tuple.TupleDataMax = sizeof(buf);
323     tuple.TupleOffset = 0;
324     CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
325     CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
326     link->conf.ConfigBase = parse.config.base;
327     link->conf.Present = parse.config.rmask[0];
328     
329     /* Is this a 3c562? */
330     tuple.DesiredTuple = CISTPL_MANFID;
331     tuple.Attributes = TUPLE_RETURN_COMMON;
332     if ((pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) &&
333         (pcmcia_get_tuple_data(handle, &tuple) == CS_SUCCESS)) {
334         if (le16_to_cpu(buf[0]) != MANFID_3COM)
335             printk(KERN_INFO "3c589_cs: hmmm, is this really a "
336                    "3Com card??\n");
337         multi = (le16_to_cpu(buf[1]) == PRODID_3COM_3C562);
338     }
339     
340     /* Configure card */
341     link->state |= DEV_CONFIG;
342
343     /* For the 3c562, the base address must be xx00-xx7f */
344     link->io.IOAddrLines = 16;
345     for (i = j = 0; j < 0x400; j += 0x10) {
346         if (multi && (j & 0x80)) continue;
347         link->io.BasePort1 = j ^ 0x300;
348         i = pcmcia_request_io(link->handle, &link->io);
349         if (i == CS_SUCCESS) break;
350     }
351     if (i != CS_SUCCESS) {
352         cs_error(link->handle, RequestIO, i);
353         goto failed;
354     }
355     CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
356     CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
357         
358     dev->irq = link->irq.AssignedIRQ;
359     dev->base_addr = link->io.BasePort1;
360     ioaddr = dev->base_addr;
361     EL3WINDOW(0);
362
363     /* The 3c589 has an extra EEPROM for configuration info, including
364        the hardware address.  The 3c562 puts the address in the CIS. */
365     tuple.DesiredTuple = 0x88;
366     if (pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) {
367         pcmcia_get_tuple_data(handle, &tuple);
368         for (i = 0; i < 3; i++)
369             phys_addr[i] = htons(buf[i]);
370     } else {
371         for (i = 0; i < 3; i++)
372             phys_addr[i] = htons(read_eeprom(ioaddr, i));
373         if (phys_addr[0] == 0x6060) {
374             printk(KERN_ERR "3c589_cs: IO port conflict at 0x%03lx"
375                    "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
376             goto failed;
377         }
378     }
379
380     /* The address and resource configuration register aren't loaded from
381        the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
382     outw(0x3f00, ioaddr + 8);
383     fifo = inl(ioaddr);
384
385     /* The if_port symbol can be set when the module is loaded */
386     if ((if_port >= 0) && (if_port <= 3))
387         dev->if_port = if_port;
388     else
389         printk(KERN_ERR "3c589_cs: invalid if_port requested\n");
390     
391     link->dev = &lp->node;
392     link->state &= ~DEV_CONFIG_PENDING;
393
394     if (register_netdev(dev) != 0) {
395         printk(KERN_ERR "3c589_cs: register_netdev() failed\n");
396         link->dev = NULL;
397         goto failed;
398     }
399
400     strcpy(lp->node.dev_name, dev->name);
401
402     printk(KERN_INFO "%s: 3Com 3c%s, io %#3lx, irq %d, hw_addr ",
403            dev->name, (multi ? "562" : "589"), dev->base_addr,
404            dev->irq);
405     for (i = 0; i < 6; i++)
406         printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
407     printk(KERN_INFO "  %dK FIFO split %s Rx:Tx, %s xcvr\n",
408            (fifo & 7) ? 32 : 8, ram_split[(fifo >> 16) & 3],
409            if_names[dev->if_port]);
410     return;
411
412 cs_failed:
413     cs_error(link->handle, last_fn, last_ret);
414 failed:
415     tc589_release(link);
416     return;
417     
418 } /* tc589_config */
419
420 /*======================================================================
421
422     After a card is removed, tc589_release() will unregister the net
423     device, and release the PCMCIA configuration.  If the device is
424     still open, this will be postponed until it is closed.
425     
426 ======================================================================*/
427
428 static void tc589_release(dev_link_t *link)
429 {
430     DEBUG(0, "3c589_release(0x%p)\n", link);
431     
432     pcmcia_release_configuration(link->handle);
433     pcmcia_release_io(link->handle, &link->io);
434     pcmcia_release_irq(link->handle, &link->irq);
435     
436     link->state &= ~DEV_CONFIG;
437 }
438
439 /*======================================================================
440
441     The card status event handler.  Mostly, this schedules other
442     stuff to run after an event is received.  A CARD_REMOVAL event
443     also sets some flags to discourage the net drivers from trying
444     to talk to the card any more.
445     
446 ======================================================================*/
447
448 static int tc589_event(event_t event, int priority,
449                        event_callback_args_t *args)
450 {
451     dev_link_t *link = args->client_data;
452     struct net_device *dev = link->priv;
453     
454     DEBUG(1, "3c589_event(0x%06x)\n", event);
455     
456     switch (event) {
457     case CS_EVENT_CARD_REMOVAL:
458         link->state &= ~DEV_PRESENT;
459         if (link->state & DEV_CONFIG) {
460             netif_device_detach(dev);
461             tc589_release(link);
462         }
463         break;
464     case CS_EVENT_CARD_INSERTION:
465         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
466         tc589_config(link);
467         break;
468     case CS_EVENT_PM_SUSPEND:
469         link->state |= DEV_SUSPEND;
470         /* Fall through... */
471     case CS_EVENT_RESET_PHYSICAL:
472         if (link->state & DEV_CONFIG) {
473             if (link->open)
474                 netif_device_detach(dev);
475             pcmcia_release_configuration(link->handle);
476         }
477         break;
478     case CS_EVENT_PM_RESUME:
479         link->state &= ~DEV_SUSPEND;
480         /* Fall through... */
481     case CS_EVENT_CARD_RESET:
482         if (link->state & DEV_CONFIG) {
483             pcmcia_request_configuration(link->handle, &link->conf);
484             if (link->open) {
485                 tc589_reset(dev);
486                 netif_device_attach(dev);
487             }
488         }
489         break;
490     }
491     return 0;
492 } /* tc589_event */
493
494 /*====================================================================*/
495
496 /*
497   Use this for commands that may take time to finish
498 */
499 static void tc589_wait_for_completion(struct net_device *dev, int cmd)
500 {
501     int i = 100;
502     outw(cmd, dev->base_addr + EL3_CMD);
503     while (--i > 0)
504         if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
505     if (i == 0)
506         printk(KERN_WARNING "%s: command 0x%04x did not complete!\n",
507                dev->name, cmd);
508 }
509
510 /*
511   Read a word from the EEPROM using the regular EEPROM access register.
512   Assume that we are in register window zero.
513 */
514 static u16 read_eeprom(ioaddr_t ioaddr, int index)
515 {
516     int i;
517     outw(EEPROM_READ + index, ioaddr + 10);
518     /* Reading the eeprom takes 162 us */
519     for (i = 1620; i >= 0; i--)
520         if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
521             break;
522     return inw(ioaddr + 12);
523 }
524
525 /*
526   Set transceiver type, perhaps to something other than what the user
527   specified in dev->if_port.
528 */
529 static void tc589_set_xcvr(struct net_device *dev, int if_port)
530 {
531     struct el3_private *lp = netdev_priv(dev);
532     ioaddr_t ioaddr = dev->base_addr;
533     
534     EL3WINDOW(0);
535     switch (if_port) {
536     case 0: case 1: outw(0, ioaddr + 6); break;
537     case 2: outw(3<<14, ioaddr + 6); break;
538     case 3: outw(1<<14, ioaddr + 6); break;
539     }
540     /* On PCMCIA, this just turns on the LED */
541     outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
542     /* 10baseT interface, enable link beat and jabber check. */
543     EL3WINDOW(4);
544     outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
545     EL3WINDOW(1);
546     if (if_port == 2)
547         lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
548     else
549         lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
550 }
551
552 static void dump_status(struct net_device *dev)
553 {
554     ioaddr_t ioaddr = dev->base_addr;
555     EL3WINDOW(1);
556     printk(KERN_INFO "  irq status %04x, rx status %04x, tx status "
557            "%02x  tx free %04x\n", inw(ioaddr+EL3_STATUS),
558            inw(ioaddr+RX_STATUS), inb(ioaddr+TX_STATUS),
559            inw(ioaddr+TX_FREE));
560     EL3WINDOW(4);
561     printk(KERN_INFO "  diagnostics: fifo %04x net %04x ethernet %04x"
562            " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
563            inw(ioaddr+0x08), inw(ioaddr+0x0a));
564     EL3WINDOW(1);
565 }
566
567 /* Reset and restore all of the 3c589 registers. */
568 static void tc589_reset(struct net_device *dev)
569 {
570     ioaddr_t ioaddr = dev->base_addr;
571     int i;
572     
573     EL3WINDOW(0);
574     outw(0x0001, ioaddr + 4);                   /* Activate board. */ 
575     outw(0x3f00, ioaddr + 8);                   /* Set the IRQ line. */
576     
577     /* Set the station address in window 2. */
578     EL3WINDOW(2);
579     for (i = 0; i < 6; i++)
580         outb(dev->dev_addr[i], ioaddr + i);
581
582     tc589_set_xcvr(dev, dev->if_port);
583     
584     /* Switch to the stats window, and clear all stats by reading. */
585     outw(StatsDisable, ioaddr + EL3_CMD);
586     EL3WINDOW(6);
587     for (i = 0; i < 9; i++)
588         inb(ioaddr+i);
589     inw(ioaddr + 10);
590     inw(ioaddr + 12);
591     
592     /* Switch to register set 1 for normal use. */
593     EL3WINDOW(1);
594
595     /* Accept b-cast and phys addr only. */
596     outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
597     outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
598     outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
599     outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
600     /* Allow status bits to be seen. */
601     outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
602     /* Ack all pending events, and set active indicator mask. */
603     outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
604          ioaddr + EL3_CMD);
605     outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
606          | AdapterFailure, ioaddr + EL3_CMD);
607 }
608
609 static void netdev_get_drvinfo(struct net_device *dev,
610                                struct ethtool_drvinfo *info)
611 {
612         strcpy(info->driver, DRV_NAME);
613         strcpy(info->version, DRV_VERSION);
614         sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
615 }
616
617 #ifdef PCMCIA_DEBUG
618 static u32 netdev_get_msglevel(struct net_device *dev)
619 {
620         return pc_debug;
621 }
622
623 static void netdev_set_msglevel(struct net_device *dev, u32 level)
624 {
625         pc_debug = level;
626 }
627 #endif /* PCMCIA_DEBUG */
628
629 static struct ethtool_ops netdev_ethtool_ops = {
630         .get_drvinfo            = netdev_get_drvinfo,
631 #ifdef PCMCIA_DEBUG
632         .get_msglevel           = netdev_get_msglevel,
633         .set_msglevel           = netdev_set_msglevel,
634 #endif /* PCMCIA_DEBUG */
635 };
636
637 static int el3_config(struct net_device *dev, struct ifmap *map)
638 {
639     if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
640         if (map->port <= 3) {
641             dev->if_port = map->port;
642             printk(KERN_INFO "%s: switched to %s port\n",
643                    dev->name, if_names[dev->if_port]);
644             tc589_set_xcvr(dev, dev->if_port);
645         } else
646             return -EINVAL;
647     }
648     return 0;
649 }
650
651 static int el3_open(struct net_device *dev)
652 {
653     struct el3_private *lp = netdev_priv(dev);
654     dev_link_t *link = &lp->link;
655     
656     if (!DEV_OK(link))
657         return -ENODEV;
658
659     link->open++;
660     netif_start_queue(dev);
661     
662     tc589_reset(dev);
663     init_timer(&lp->media);
664     lp->media.function = &media_check;
665     lp->media.data = (unsigned long) dev;
666     lp->media.expires = jiffies + HZ;
667     add_timer(&lp->media);
668
669     DEBUG(1, "%s: opened, status %4.4x.\n",
670           dev->name, inw(dev->base_addr + EL3_STATUS));
671     
672     return 0;
673 }
674
675 static void el3_tx_timeout(struct net_device *dev)
676 {
677     struct el3_private *lp = netdev_priv(dev);
678     ioaddr_t ioaddr = dev->base_addr;
679     
680     printk(KERN_WARNING "%s: Transmit timed out!\n", dev->name);
681     dump_status(dev);
682     lp->stats.tx_errors++;
683     dev->trans_start = jiffies;
684     /* Issue TX_RESET and TX_START commands. */
685     tc589_wait_for_completion(dev, TxReset);
686     outw(TxEnable, ioaddr + EL3_CMD);
687     netif_wake_queue(dev);
688 }
689
690 static void pop_tx_status(struct net_device *dev)
691 {
692     struct el3_private *lp = netdev_priv(dev);
693     ioaddr_t ioaddr = dev->base_addr;
694     int i;
695     
696     /* Clear the Tx status stack. */
697     for (i = 32; i > 0; i--) {
698         u_char tx_status = inb(ioaddr + TX_STATUS);
699         if (!(tx_status & 0x84)) break;
700         /* reset transmitter on jabber error or underrun */
701         if (tx_status & 0x30)
702             tc589_wait_for_completion(dev, TxReset);
703         if (tx_status & 0x38) {
704             DEBUG(1, "%s: transmit error: status 0x%02x\n",
705                   dev->name, tx_status);
706             outw(TxEnable, ioaddr + EL3_CMD);
707             lp->stats.tx_aborted_errors++;
708         }
709         outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
710     }
711 }
712
713 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
714 {
715     ioaddr_t ioaddr = dev->base_addr;
716     struct el3_private *priv = netdev_priv(dev);
717
718     DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
719           "status %4.4x.\n", dev->name, (long)skb->len,
720           inw(ioaddr + EL3_STATUS));
721
722     priv->stats.tx_bytes += skb->len;
723
724     /* Put out the doubleword header... */
725     outw(skb->len, ioaddr + TX_FIFO);
726     outw(0x00, ioaddr + TX_FIFO);
727     /* ... and the packet rounded to a doubleword. */
728     outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
729
730     dev->trans_start = jiffies;
731     if (inw(ioaddr + TX_FREE) <= 1536) {
732         netif_stop_queue(dev);
733         /* Interrupt us when the FIFO has room for max-sized packet. */
734         outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
735     }
736
737     dev_kfree_skb(skb);
738     pop_tx_status(dev);
739     
740     return 0;
741 }
742
743 /* The EL3 interrupt handler. */
744 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
745 {
746     struct net_device *dev = (struct net_device *) dev_id;
747     struct el3_private *lp = netdev_priv(dev);
748     ioaddr_t ioaddr, status;
749     int i = 0, handled = 1;
750     
751     if (!netif_device_present(dev))
752         return IRQ_NONE;
753
754     ioaddr = dev->base_addr;
755
756     DEBUG(3, "%s: interrupt, status %4.4x.\n",
757           dev->name, inw(ioaddr + EL3_STATUS));
758
759     spin_lock(&lp->lock);    
760     while ((status = inw(ioaddr + EL3_STATUS)) &
761         (IntLatch | RxComplete | StatsFull)) {
762         if ((status & 0xe000) != 0x2000) {
763             DEBUG(1, "%s: interrupt from dead card\n", dev->name);
764             handled = 0;
765             break;
766         }
767         
768         if (status & RxComplete)
769             el3_rx(dev);
770         
771         if (status & TxAvailable) {
772             DEBUG(3, "    TX room bit was handled.\n");
773             /* There's room in the FIFO for a full-sized packet. */
774             outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
775             netif_wake_queue(dev);
776         }
777         
778         if (status & TxComplete)
779             pop_tx_status(dev);
780
781         if (status & (AdapterFailure | RxEarly | StatsFull)) {
782             /* Handle all uncommon interrupts. */
783             if (status & StatsFull)             /* Empty statistics. */
784                 update_stats(dev);
785             if (status & RxEarly) {             /* Rx early is unused. */
786                 el3_rx(dev);
787                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
788             }
789             if (status & AdapterFailure) {
790                 u16 fifo_diag;
791                 EL3WINDOW(4);
792                 fifo_diag = inw(ioaddr + 4);
793                 EL3WINDOW(1);
794                 printk(KERN_WARNING "%s: adapter failure, FIFO diagnostic"
795                        " register %04x.\n", dev->name, fifo_diag);
796                 if (fifo_diag & 0x0400) {
797                     /* Tx overrun */
798                     tc589_wait_for_completion(dev, TxReset);
799                     outw(TxEnable, ioaddr + EL3_CMD);
800                 }
801                 if (fifo_diag & 0x2000) {
802                     /* Rx underrun */
803                     tc589_wait_for_completion(dev, RxReset);
804                     set_multicast_list(dev);
805                     outw(RxEnable, ioaddr + EL3_CMD);
806                 }
807                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
808             }
809         }
810         
811         if (++i > 10) {
812             printk(KERN_ERR "%s: infinite loop in interrupt, "
813                    "status %4.4x.\n", dev->name, status);
814             /* Clear all interrupts */
815             outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
816             break;
817         }
818         /* Acknowledge the IRQ. */
819         outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
820     }
821
822     lp->last_irq = jiffies;
823     spin_unlock(&lp->lock);    
824     DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
825           dev->name, inw(ioaddr + EL3_STATUS));
826     return IRQ_RETVAL(handled);
827 }
828
829 static void media_check(unsigned long arg)
830 {
831     struct net_device *dev = (struct net_device *)(arg);
832     struct el3_private *lp = netdev_priv(dev);
833     ioaddr_t ioaddr = dev->base_addr;
834     u16 media, errs;
835     unsigned 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_WARNING "%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 + HZ/100;
852         add_timer(&lp->media);
853         return;
854     }
855
856     /* lp->lock guards the EL3 window. Window should always be 1 except
857        when the lock is held */
858     spin_lock_irqsave(&lp->lock, flags);    
859     EL3WINDOW(4);
860     media = inw(ioaddr+WN4_MEDIA) & 0xc810;
861
862     /* Ignore collisions unless we've had no irq's recently */
863     if (jiffies - lp->last_irq < HZ) {
864         media &= ~0x0010;
865     } else {
866         /* Try harder to detect carrier errors */
867         EL3WINDOW(6);
868         outw(StatsDisable, ioaddr + EL3_CMD);
869         errs = inb(ioaddr + 0);
870         outw(StatsEnable, ioaddr + EL3_CMD);
871         lp->stats.tx_carrier_errors += errs;
872         if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
873     }
874
875     if (media != lp->media_status) {
876         if ((media & lp->media_status & 0x8000) &&
877             ((lp->media_status ^ media) & 0x0800))
878             printk(KERN_INFO "%s: %s link beat\n", dev->name,
879                    (lp->media_status & 0x0800 ? "lost" : "found"));
880         else if ((media & lp->media_status & 0x4000) &&
881                  ((lp->media_status ^ media) & 0x0010))
882             printk(KERN_INFO "%s: coax cable %s\n", dev->name,
883                    (lp->media_status & 0x0010 ? "ok" : "problem"));
884         if (dev->if_port == 0) {
885             if (media & 0x8000) {
886                 if (media & 0x0800)
887                     printk(KERN_INFO "%s: flipped to 10baseT\n",
888                            dev->name);
889                 else
890                     tc589_set_xcvr(dev, 2);
891             } else if (media & 0x4000) {
892                 if (media & 0x0010)
893                     tc589_set_xcvr(dev, 1);
894                 else
895                     printk(KERN_INFO "%s: flipped to 10base2\n",
896                            dev->name);
897             }
898         }
899         lp->media_status = media;
900     }
901     
902     EL3WINDOW(1);
903     spin_unlock_irqrestore(&lp->lock, flags);    
904
905 reschedule:
906     lp->media.expires = jiffies + HZ;
907     add_timer(&lp->media);
908 }
909
910 static struct net_device_stats *el3_get_stats(struct net_device *dev)
911 {
912     struct el3_private *lp = netdev_priv(dev);
913     unsigned long flags;
914     dev_link_t *link = &lp->link;
915
916     if (DEV_OK(link)) {
917         spin_lock_irqsave(&lp->lock, flags);
918         update_stats(dev);
919         spin_unlock_irqrestore(&lp->lock, 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   Caller must hold the lock for this
931 */
932 static void update_stats(struct net_device *dev)
933 {
934     struct el3_private *lp = netdev_priv(dev);
935     ioaddr_t ioaddr = dev->base_addr;
936
937     DEBUG(2, "%s: updating the statistics.\n", dev->name);
938     /* Turn off statistics updates while reading. */
939     outw(StatsDisable, ioaddr + EL3_CMD);
940     /* Switch to the stats window, and read everything. */
941     EL3WINDOW(6);
942     lp->stats.tx_carrier_errors         += inb(ioaddr + 0);
943     lp->stats.tx_heartbeat_errors       += inb(ioaddr + 1);
944     /* Multiple collisions. */          inb(ioaddr + 2);
945     lp->stats.collisions                += inb(ioaddr + 3);
946     lp->stats.tx_window_errors          += inb(ioaddr + 4);
947     lp->stats.rx_fifo_errors            += inb(ioaddr + 5);
948     lp->stats.tx_packets                += inb(ioaddr + 6);
949     /* Rx packets   */                  inb(ioaddr + 7);
950     /* Tx deferrals */                  inb(ioaddr + 8);
951     /* Rx octets */                     inw(ioaddr + 10);
952     /* Tx octets */                     inw(ioaddr + 12);
953     
954     /* Back to window 1, and turn statistics back on. */
955     EL3WINDOW(1);
956     outw(StatsEnable, ioaddr + EL3_CMD);
957 }
958
959 static int el3_rx(struct net_device *dev)
960 {
961     struct el3_private *lp = netdev_priv(dev);
962     ioaddr_t ioaddr = dev->base_addr;
963     int worklimit = 32;
964     short rx_status;
965     
966     DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
967           dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
968     while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
969            (--worklimit >= 0)) {
970         if (rx_status & 0x4000) { /* Error, update stats. */
971             short error = rx_status & 0x3800;
972             lp->stats.rx_errors++;
973             switch (error) {
974             case 0x0000:        lp->stats.rx_over_errors++; break;
975             case 0x0800:        lp->stats.rx_length_errors++; break;
976             case 0x1000:        lp->stats.rx_frame_errors++; break;
977             case 0x1800:        lp->stats.rx_length_errors++; break;
978             case 0x2000:        lp->stats.rx_frame_errors++; break;
979             case 0x2800:        lp->stats.rx_crc_errors++; break;
980             }
981         } else {
982             short pkt_len = rx_status & 0x7ff;
983             struct sk_buff *skb;
984             
985             skb = dev_alloc_skb(pkt_len+5);
986             
987             DEBUG(3, "    Receiving packet size %d status %4.4x.\n",
988                   pkt_len, rx_status);
989             if (skb != NULL) {
990                 skb->dev = dev;
991                 skb_reserve(skb, 2);
992                 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
993                         (pkt_len+3)>>2);
994                 skb->protocol = eth_type_trans(skb, dev);
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         tc589_wait_for_completion(dev, RxDiscard);
1007     }
1008     if (worklimit == 0)
1009         printk(KERN_WARNING "%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 = netdev_priv(dev);
1016     dev_link_t *link = &lp->link;
1017     ioaddr_t ioaddr = dev->base_addr;
1018     u16 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 = netdev_priv(dev);
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_sync(&lp->media);
1066     
1067     return 0;
1068 }
1069
1070 static struct pcmcia_driver tc589_driver = {
1071         .owner          = THIS_MODULE,
1072         .drv            = {
1073                 .name   = "3c589_cs",
1074         },
1075         .attach         = tc589_attach,
1076         .detach         = tc589_detach,
1077 };
1078
1079 static int __init init_tc589(void)
1080 {
1081         return pcmcia_register_driver(&tc589_driver);
1082 }
1083
1084 static void __exit exit_tc589(void)
1085 {
1086         pcmcia_unregister_driver(&tc589_driver);
1087         while (dev_list != NULL)
1088                 tc589_detach(dev_list);
1089 }
1090
1091 module_init(init_tc589);
1092 module_exit(exit_tc589);