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