VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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->dev)
280         unregister_netdev(dev);
281
282     if (link->state & DEV_CONFIG)
283         tc589_release(link);
284     
285     if (link->handle)
286         pcmcia_deregister_client(link->handle);
287     
288     /* Unlink device structure, free bits */
289     *linkp = link->next;
290     free_netdev(dev);
291 } /* tc589_detach */
292
293 /*======================================================================
294
295     tc589_config() is scheduled to run after a CARD_INSERTION event
296     is received, to configure the PCMCIA socket, and to make the
297     ethernet device available to the system.
298     
299 ======================================================================*/
300
301 #define CS_CHECK(fn, ret) \
302 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
303
304 static void tc589_config(dev_link_t *link)
305 {
306     client_handle_t handle = link->handle;
307     struct net_device *dev = link->priv;
308     struct el3_private *lp = netdev_priv(dev);
309     tuple_t tuple;
310     cisparse_t parse;
311     u16 buf[32], *phys_addr;
312     int last_fn, last_ret, i, j, multi = 0, fifo;
313     ioaddr_t ioaddr;
314     char *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
315     
316     DEBUG(0, "3c589_config(0x%p)\n", link);
317
318     phys_addr = (u16 *)dev->dev_addr;
319     tuple.Attributes = 0;
320     tuple.DesiredTuple = CISTPL_CONFIG;
321     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
322     tuple.TupleData = (cisdata_t *)buf;
323     tuple.TupleDataMax = sizeof(buf);
324     tuple.TupleOffset = 0;
325     CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
326     CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
327     link->conf.ConfigBase = parse.config.base;
328     link->conf.Present = parse.config.rmask[0];
329     
330     /* Is this a 3c562? */
331     tuple.DesiredTuple = CISTPL_MANFID;
332     tuple.Attributes = TUPLE_RETURN_COMMON;
333     if ((pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) &&
334         (pcmcia_get_tuple_data(handle, &tuple) == CS_SUCCESS)) {
335         if (le16_to_cpu(buf[0]) != MANFID_3COM)
336             printk(KERN_INFO "3c589_cs: hmmm, is this really a "
337                    "3Com card??\n");
338         multi = (le16_to_cpu(buf[1]) == PRODID_3COM_3C562);
339     }
340     
341     /* Configure card */
342     link->state |= DEV_CONFIG;
343
344     /* For the 3c562, the base address must be xx00-xx7f */
345     link->io.IOAddrLines = 16;
346     for (i = j = 0; j < 0x400; j += 0x10) {
347         if (multi && (j & 0x80)) continue;
348         link->io.BasePort1 = j ^ 0x300;
349         i = pcmcia_request_io(link->handle, &link->io);
350         if (i == CS_SUCCESS) break;
351     }
352     if (i != CS_SUCCESS) {
353         cs_error(link->handle, RequestIO, i);
354         goto failed;
355     }
356     CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
357     CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
358         
359     dev->irq = link->irq.AssignedIRQ;
360     dev->base_addr = link->io.BasePort1;
361     ioaddr = dev->base_addr;
362     EL3WINDOW(0);
363
364     /* The 3c589 has an extra EEPROM for configuration info, including
365        the hardware address.  The 3c562 puts the address in the CIS. */
366     tuple.DesiredTuple = 0x88;
367     if (pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) {
368         pcmcia_get_tuple_data(handle, &tuple);
369         for (i = 0; i < 3; i++)
370             phys_addr[i] = htons(buf[i]);
371     } else {
372         for (i = 0; i < 3; i++)
373             phys_addr[i] = htons(read_eeprom(ioaddr, i));
374         if (phys_addr[0] == 0x6060) {
375             printk(KERN_ERR "3c589_cs: IO port conflict at 0x%03lx"
376                    "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
377             goto failed;
378         }
379     }
380
381     /* The address and resource configuration register aren't loaded from
382        the EEPROM and *must* be set to 0 and IRQ3 for the PCMCIA version. */
383     outw(0x3f00, ioaddr + 8);
384     fifo = inl(ioaddr);
385
386     /* The if_port symbol can be set when the module is loaded */
387     if ((if_port >= 0) && (if_port <= 3))
388         dev->if_port = if_port;
389     else
390         printk(KERN_ERR "3c589_cs: invalid if_port requested\n");
391     
392     link->dev = &lp->node;
393     link->state &= ~DEV_CONFIG_PENDING;
394
395     if (register_netdev(dev) != 0) {
396         printk(KERN_ERR "3c589_cs: register_netdev() failed\n");
397         link->dev = NULL;
398         goto failed;
399     }
400
401     strcpy(lp->node.dev_name, dev->name);
402
403     printk(KERN_INFO "%s: 3Com 3c%s, io %#3lx, irq %d, hw_addr ",
404            dev->name, (multi ? "562" : "589"), dev->base_addr,
405            dev->irq);
406     for (i = 0; i < 6; i++)
407         printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
408     printk(KERN_INFO "  %dK FIFO split %s Rx:Tx, %s xcvr\n",
409            (fifo & 7) ? 32 : 8, ram_split[(fifo >> 16) & 3],
410            if_names[dev->if_port]);
411     return;
412
413 cs_failed:
414     cs_error(link->handle, last_fn, last_ret);
415 failed:
416     tc589_release(link);
417     return;
418     
419 } /* tc589_config */
420
421 /*======================================================================
422
423     After a card is removed, tc589_release() will unregister the net
424     device, and release the PCMCIA configuration.  If the device is
425     still open, this will be postponed until it is closed.
426     
427 ======================================================================*/
428
429 static void tc589_release(dev_link_t *link)
430 {
431     DEBUG(0, "3c589_release(0x%p)\n", link);
432     
433     pcmcia_release_configuration(link->handle);
434     pcmcia_release_io(link->handle, &link->io);
435     pcmcia_release_irq(link->handle, &link->irq);
436     
437     link->state &= ~DEV_CONFIG;
438 }
439
440 /*======================================================================
441
442     The card status event handler.  Mostly, this schedules other
443     stuff to run after an event is received.  A CARD_REMOVAL event
444     also sets some flags to discourage the net drivers from trying
445     to talk to the card any more.
446     
447 ======================================================================*/
448
449 static int tc589_event(event_t event, int priority,
450                        event_callback_args_t *args)
451 {
452     dev_link_t *link = args->client_data;
453     struct net_device *dev = link->priv;
454     
455     DEBUG(1, "3c589_event(0x%06x)\n", event);
456     
457     switch (event) {
458     case CS_EVENT_CARD_REMOVAL:
459         link->state &= ~DEV_PRESENT;
460         if (link->state & DEV_CONFIG)
461             netif_device_detach(dev);
462         break;
463     case CS_EVENT_CARD_INSERTION:
464         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
465         tc589_config(link);
466         break;
467     case CS_EVENT_PM_SUSPEND:
468         link->state |= DEV_SUSPEND;
469         /* Fall through... */
470     case CS_EVENT_RESET_PHYSICAL:
471         if (link->state & DEV_CONFIG) {
472             if (link->open)
473                 netif_device_detach(dev);
474             pcmcia_release_configuration(link->handle);
475         }
476         break;
477     case CS_EVENT_PM_RESUME:
478         link->state &= ~DEV_SUSPEND;
479         /* Fall through... */
480     case CS_EVENT_CARD_RESET:
481         if (link->state & DEV_CONFIG) {
482             pcmcia_request_configuration(link->handle, &link->conf);
483             if (link->open) {
484                 tc589_reset(dev);
485                 netif_device_attach(dev);
486             }
487         }
488         break;
489     }
490     return 0;
491 } /* tc589_event */
492
493 /*====================================================================*/
494
495 /*
496   Use this for commands that may take time to finish
497 */
498 static void tc589_wait_for_completion(struct net_device *dev, int cmd)
499 {
500     int i = 100;
501     outw(cmd, dev->base_addr + EL3_CMD);
502     while (--i > 0)
503         if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
504     if (i == 0)
505         printk(KERN_WARNING "%s: command 0x%04x did not complete!\n",
506                dev->name, cmd);
507 }
508
509 /*
510   Read a word from the EEPROM using the regular EEPROM access register.
511   Assume that we are in register window zero.
512 */
513 static u16 read_eeprom(ioaddr_t ioaddr, int index)
514 {
515     int i;
516     outw(EEPROM_READ + index, ioaddr + 10);
517     /* Reading the eeprom takes 162 us */
518     for (i = 1620; i >= 0; i--)
519         if ((inw(ioaddr + 10) & EEPROM_BUSY) == 0)
520             break;
521     return inw(ioaddr + 12);
522 }
523
524 /*
525   Set transceiver type, perhaps to something other than what the user
526   specified in dev->if_port.
527 */
528 static void tc589_set_xcvr(struct net_device *dev, int if_port)
529 {
530     struct el3_private *lp = netdev_priv(dev);
531     ioaddr_t ioaddr = dev->base_addr;
532     
533     EL3WINDOW(0);
534     switch (if_port) {
535     case 0: case 1: outw(0, ioaddr + 6); break;
536     case 2: outw(3<<14, ioaddr + 6); break;
537     case 3: outw(1<<14, ioaddr + 6); break;
538     }
539     /* On PCMCIA, this just turns on the LED */
540     outw((if_port == 2) ? StartCoax : StopCoax, ioaddr + EL3_CMD);
541     /* 10baseT interface, enable link beat and jabber check. */
542     EL3WINDOW(4);
543     outw(MEDIA_LED | ((if_port < 2) ? MEDIA_TP : 0), ioaddr + WN4_MEDIA);
544     EL3WINDOW(1);
545     if (if_port == 2)
546         lp->media_status = ((dev->if_port == 0) ? 0x8000 : 0x4000);
547     else
548         lp->media_status = ((dev->if_port == 0) ? 0x4010 : 0x8800);
549 }
550
551 static void dump_status(struct net_device *dev)
552 {
553     ioaddr_t ioaddr = dev->base_addr;
554     EL3WINDOW(1);
555     printk(KERN_INFO "  irq status %04x, rx status %04x, tx status "
556            "%02x  tx free %04x\n", inw(ioaddr+EL3_STATUS),
557            inw(ioaddr+RX_STATUS), inb(ioaddr+TX_STATUS),
558            inw(ioaddr+TX_FREE));
559     EL3WINDOW(4);
560     printk(KERN_INFO "  diagnostics: fifo %04x net %04x ethernet %04x"
561            " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
562            inw(ioaddr+0x08), inw(ioaddr+0x0a));
563     EL3WINDOW(1);
564 }
565
566 /* Reset and restore all of the 3c589 registers. */
567 static void tc589_reset(struct net_device *dev)
568 {
569     ioaddr_t ioaddr = dev->base_addr;
570     int i;
571     
572     EL3WINDOW(0);
573     outw(0x0001, ioaddr + 4);                   /* Activate board. */ 
574     outw(0x3f00, ioaddr + 8);                   /* Set the IRQ line. */
575     
576     /* Set the station address in window 2. */
577     EL3WINDOW(2);
578     for (i = 0; i < 6; i++)
579         outb(dev->dev_addr[i], ioaddr + i);
580
581     tc589_set_xcvr(dev, dev->if_port);
582     
583     /* Switch to the stats window, and clear all stats by reading. */
584     outw(StatsDisable, ioaddr + EL3_CMD);
585     EL3WINDOW(6);
586     for (i = 0; i < 9; i++)
587         inb(ioaddr+i);
588     inw(ioaddr + 10);
589     inw(ioaddr + 12);
590     
591     /* Switch to register set 1 for normal use. */
592     EL3WINDOW(1);
593
594     /* Accept b-cast and phys addr only. */
595     outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
596     outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
597     outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
598     outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
599     /* Allow status bits to be seen. */
600     outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
601     /* Ack all pending events, and set active indicator mask. */
602     outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
603          ioaddr + EL3_CMD);
604     outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
605          | AdapterFailure, ioaddr + EL3_CMD);
606 }
607
608 static void netdev_get_drvinfo(struct net_device *dev,
609                                struct ethtool_drvinfo *info)
610 {
611         strcpy(info->driver, DRV_NAME);
612         strcpy(info->version, DRV_VERSION);
613         sprintf(info->bus_info, "PCMCIA 0x%lx", dev->base_addr);
614 }
615
616 #ifdef PCMCIA_DEBUG
617 static u32 netdev_get_msglevel(struct net_device *dev)
618 {
619         return pc_debug;
620 }
621
622 static void netdev_set_msglevel(struct net_device *dev, u32 level)
623 {
624         pc_debug = level;
625 }
626 #endif /* PCMCIA_DEBUG */
627
628 static struct ethtool_ops netdev_ethtool_ops = {
629         .get_drvinfo            = netdev_get_drvinfo,
630 #ifdef PCMCIA_DEBUG
631         .get_msglevel           = netdev_get_msglevel,
632         .set_msglevel           = netdev_set_msglevel,
633 #endif /* PCMCIA_DEBUG */
634 };
635
636 static int el3_config(struct net_device *dev, struct ifmap *map)
637 {
638     if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
639         if (map->port <= 3) {
640             dev->if_port = map->port;
641             printk(KERN_INFO "%s: switched to %s port\n",
642                    dev->name, if_names[dev->if_port]);
643             tc589_set_xcvr(dev, dev->if_port);
644         } else
645             return -EINVAL;
646     }
647     return 0;
648 }
649
650 static int el3_open(struct net_device *dev)
651 {
652     struct el3_private *lp = netdev_priv(dev);
653     dev_link_t *link = &lp->link;
654     
655     if (!DEV_OK(link))
656         return -ENODEV;
657
658     link->open++;
659     netif_start_queue(dev);
660     
661     tc589_reset(dev);
662     init_timer(&lp->media);
663     lp->media.function = &media_check;
664     lp->media.data = (unsigned long) dev;
665     lp->media.expires = jiffies + HZ;
666     add_timer(&lp->media);
667
668     DEBUG(1, "%s: opened, status %4.4x.\n",
669           dev->name, inw(dev->base_addr + EL3_STATUS));
670     
671     return 0;
672 }
673
674 static void el3_tx_timeout(struct net_device *dev)
675 {
676     struct el3_private *lp = netdev_priv(dev);
677     ioaddr_t ioaddr = dev->base_addr;
678     
679     printk(KERN_WARNING "%s: Transmit timed out!\n", dev->name);
680     dump_status(dev);
681     lp->stats.tx_errors++;
682     dev->trans_start = jiffies;
683     /* Issue TX_RESET and TX_START commands. */
684     tc589_wait_for_completion(dev, TxReset);
685     outw(TxEnable, ioaddr + EL3_CMD);
686     netif_wake_queue(dev);
687 }
688
689 static void pop_tx_status(struct net_device *dev)
690 {
691     struct el3_private *lp = netdev_priv(dev);
692     ioaddr_t ioaddr = dev->base_addr;
693     int i;
694     
695     /* Clear the Tx status stack. */
696     for (i = 32; i > 0; i--) {
697         u_char tx_status = inb(ioaddr + TX_STATUS);
698         if (!(tx_status & 0x84)) break;
699         /* reset transmitter on jabber error or underrun */
700         if (tx_status & 0x30)
701             tc589_wait_for_completion(dev, TxReset);
702         if (tx_status & 0x38) {
703             DEBUG(1, "%s: transmit error: status 0x%02x\n",
704                   dev->name, tx_status);
705             outw(TxEnable, ioaddr + EL3_CMD);
706             lp->stats.tx_aborted_errors++;
707         }
708         outb(0x00, ioaddr + TX_STATUS); /* Pop the status stack. */
709     }
710 }
711
712 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
713 {
714     ioaddr_t ioaddr = dev->base_addr;
715     struct el3_private *priv = netdev_priv(dev);
716
717     DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
718           "status %4.4x.\n", dev->name, (long)skb->len,
719           inw(ioaddr + EL3_STATUS));
720
721     priv->stats.tx_bytes += skb->len;
722
723     /* Put out the doubleword header... */
724     outw(skb->len, ioaddr + TX_FIFO);
725     outw(0x00, ioaddr + TX_FIFO);
726     /* ... and the packet rounded to a doubleword. */
727     outsl(ioaddr + TX_FIFO, skb->data, (skb->len + 3) >> 2);
728
729     dev->trans_start = jiffies;
730     if (inw(ioaddr + TX_FREE) <= 1536) {
731         netif_stop_queue(dev);
732         /* Interrupt us when the FIFO has room for max-sized packet. */
733         outw(SetTxThreshold + 1536, ioaddr + EL3_CMD);
734     }
735
736     dev_kfree_skb(skb);
737     pop_tx_status(dev);
738     
739     return 0;
740 }
741
742 /* The EL3 interrupt handler. */
743 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
744 {
745     struct net_device *dev = (struct net_device *) dev_id;
746     struct el3_private *lp = netdev_priv(dev);
747     ioaddr_t ioaddr, status;
748     int i = 0, handled = 1;
749     
750     if (!netif_device_present(dev))
751         return IRQ_NONE;
752
753     ioaddr = dev->base_addr;
754
755     DEBUG(3, "%s: interrupt, status %4.4x.\n",
756           dev->name, inw(ioaddr + EL3_STATUS));
757
758     spin_lock(&lp->lock);    
759     while ((status = inw(ioaddr + EL3_STATUS)) &
760         (IntLatch | RxComplete | StatsFull)) {
761         if ((status & 0xe000) != 0x2000) {
762             DEBUG(1, "%s: interrupt from dead card\n", dev->name);
763             handled = 0;
764             break;
765         }
766         
767         if (status & RxComplete)
768             el3_rx(dev);
769         
770         if (status & TxAvailable) {
771             DEBUG(3, "    TX room bit was handled.\n");
772             /* There's room in the FIFO for a full-sized packet. */
773             outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
774             netif_wake_queue(dev);
775         }
776         
777         if (status & TxComplete)
778             pop_tx_status(dev);
779
780         if (status & (AdapterFailure | RxEarly | StatsFull)) {
781             /* Handle all uncommon interrupts. */
782             if (status & StatsFull)             /* Empty statistics. */
783                 update_stats(dev);
784             if (status & RxEarly) {             /* Rx early is unused. */
785                 el3_rx(dev);
786                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
787             }
788             if (status & AdapterFailure) {
789                 u16 fifo_diag;
790                 EL3WINDOW(4);
791                 fifo_diag = inw(ioaddr + 4);
792                 EL3WINDOW(1);
793                 printk(KERN_WARNING "%s: adapter failure, FIFO diagnostic"
794                        " register %04x.\n", dev->name, fifo_diag);
795                 if (fifo_diag & 0x0400) {
796                     /* Tx overrun */
797                     tc589_wait_for_completion(dev, TxReset);
798                     outw(TxEnable, ioaddr + EL3_CMD);
799                 }
800                 if (fifo_diag & 0x2000) {
801                     /* Rx underrun */
802                     tc589_wait_for_completion(dev, RxReset);
803                     set_multicast_list(dev);
804                     outw(RxEnable, ioaddr + EL3_CMD);
805                 }
806                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
807             }
808         }
809         
810         if (++i > 10) {
811             printk(KERN_ERR "%s: infinite loop in interrupt, "
812                    "status %4.4x.\n", dev->name, status);
813             /* Clear all interrupts */
814             outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
815             break;
816         }
817         /* Acknowledge the IRQ. */
818         outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
819     }
820
821     lp->last_irq = jiffies;
822     spin_unlock(&lp->lock);    
823     DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
824           dev->name, inw(ioaddr + EL3_STATUS));
825     return IRQ_RETVAL(handled);
826 }
827
828 static void media_check(unsigned long arg)
829 {
830     struct net_device *dev = (struct net_device *)(arg);
831     struct el3_private *lp = netdev_priv(dev);
832     ioaddr_t ioaddr = dev->base_addr;
833     u16 media, errs;
834     unsigned long flags;
835
836     if (!netif_device_present(dev)) goto reschedule;
837
838     EL3WINDOW(1);
839     /* Check for pending interrupt with expired latency timer: with
840        this, we can limp along even if the interrupt is blocked */
841     if ((inw(ioaddr + EL3_STATUS) & IntLatch) &&
842         (inb(ioaddr + EL3_TIMER) == 0xff)) {
843         if (!lp->fast_poll)
844             printk(KERN_WARNING "%s: interrupt(s) dropped!\n", dev->name);
845         el3_interrupt(dev->irq, lp, NULL);
846         lp->fast_poll = HZ;
847     }
848     if (lp->fast_poll) {
849         lp->fast_poll--;
850         lp->media.expires = jiffies + HZ/100;
851         add_timer(&lp->media);
852         return;
853     }
854
855     /* lp->lock guards the EL3 window. Window should always be 1 except
856        when the lock is held */
857     spin_lock_irqsave(&lp->lock, flags);    
858     EL3WINDOW(4);
859     media = inw(ioaddr+WN4_MEDIA) & 0xc810;
860
861     /* Ignore collisions unless we've had no irq's recently */
862     if (jiffies - lp->last_irq < HZ) {
863         media &= ~0x0010;
864     } else {
865         /* Try harder to detect carrier errors */
866         EL3WINDOW(6);
867         outw(StatsDisable, ioaddr + EL3_CMD);
868         errs = inb(ioaddr + 0);
869         outw(StatsEnable, ioaddr + EL3_CMD);
870         lp->stats.tx_carrier_errors += errs;
871         if (errs || (lp->media_status & 0x0010)) media |= 0x0010;
872     }
873
874     if (media != lp->media_status) {
875         if ((media & lp->media_status & 0x8000) &&
876             ((lp->media_status ^ media) & 0x0800))
877             printk(KERN_INFO "%s: %s link beat\n", dev->name,
878                    (lp->media_status & 0x0800 ? "lost" : "found"));
879         else if ((media & lp->media_status & 0x4000) &&
880                  ((lp->media_status ^ media) & 0x0010))
881             printk(KERN_INFO "%s: coax cable %s\n", dev->name,
882                    (lp->media_status & 0x0010 ? "ok" : "problem"));
883         if (dev->if_port == 0) {
884             if (media & 0x8000) {
885                 if (media & 0x0800)
886                     printk(KERN_INFO "%s: flipped to 10baseT\n",
887                            dev->name);
888                 else
889                     tc589_set_xcvr(dev, 2);
890             } else if (media & 0x4000) {
891                 if (media & 0x0010)
892                     tc589_set_xcvr(dev, 1);
893                 else
894                     printk(KERN_INFO "%s: flipped to 10base2\n",
895                            dev->name);
896             }
897         }
898         lp->media_status = media;
899     }
900     
901     EL3WINDOW(1);
902     spin_unlock_irqrestore(&lp->lock, flags);    
903
904 reschedule:
905     lp->media.expires = jiffies + HZ;
906     add_timer(&lp->media);
907 }
908
909 static struct net_device_stats *el3_get_stats(struct net_device *dev)
910 {
911     struct el3_private *lp = netdev_priv(dev);
912     unsigned long flags;
913     dev_link_t *link = &lp->link;
914
915     if (DEV_OK(link)) {
916         spin_lock_irqsave(&lp->lock, flags);
917         update_stats(dev);
918         spin_unlock_irqrestore(&lp->lock, flags);
919     }
920     return &lp->stats;
921 }
922
923 /*
924   Update statistics.  We change to register window 6, so this should be run
925   single-threaded if the device is active. This is expected to be a rare
926   operation, and it's simpler for the rest of the driver to assume that
927   window 1 is always valid rather than use a special window-state variable.
928   
929   Caller must hold the lock for this
930 */
931 static void update_stats(struct net_device *dev)
932 {
933     struct el3_private *lp = netdev_priv(dev);
934     ioaddr_t ioaddr = dev->base_addr;
935
936     DEBUG(2, "%s: updating the statistics.\n", dev->name);
937     /* Turn off statistics updates while reading. */
938     outw(StatsDisable, ioaddr + EL3_CMD);
939     /* Switch to the stats window, and read everything. */
940     EL3WINDOW(6);
941     lp->stats.tx_carrier_errors         += inb(ioaddr + 0);
942     lp->stats.tx_heartbeat_errors       += inb(ioaddr + 1);
943     /* Multiple collisions. */          inb(ioaddr + 2);
944     lp->stats.collisions                += inb(ioaddr + 3);
945     lp->stats.tx_window_errors          += inb(ioaddr + 4);
946     lp->stats.rx_fifo_errors            += inb(ioaddr + 5);
947     lp->stats.tx_packets                += inb(ioaddr + 6);
948     /* Rx packets   */                  inb(ioaddr + 7);
949     /* Tx deferrals */                  inb(ioaddr + 8);
950     /* Rx octets */                     inw(ioaddr + 10);
951     /* Tx octets */                     inw(ioaddr + 12);
952     
953     /* Back to window 1, and turn statistics back on. */
954     EL3WINDOW(1);
955     outw(StatsEnable, ioaddr + EL3_CMD);
956 }
957
958 static int el3_rx(struct net_device *dev)
959 {
960     struct el3_private *lp = netdev_priv(dev);
961     ioaddr_t ioaddr = dev->base_addr;
962     int worklimit = 32;
963     short rx_status;
964     
965     DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
966           dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RX_STATUS));
967     while (!((rx_status = inw(ioaddr + RX_STATUS)) & 0x8000) &&
968            (--worklimit >= 0)) {
969         if (rx_status & 0x4000) { /* Error, update stats. */
970             short error = rx_status & 0x3800;
971             lp->stats.rx_errors++;
972             switch (error) {
973             case 0x0000:        lp->stats.rx_over_errors++; break;
974             case 0x0800:        lp->stats.rx_length_errors++; break;
975             case 0x1000:        lp->stats.rx_frame_errors++; break;
976             case 0x1800:        lp->stats.rx_length_errors++; break;
977             case 0x2000:        lp->stats.rx_frame_errors++; break;
978             case 0x2800:        lp->stats.rx_crc_errors++; break;
979             }
980         } else {
981             short pkt_len = rx_status & 0x7ff;
982             struct sk_buff *skb;
983             
984             skb = dev_alloc_skb(pkt_len+5);
985             
986             DEBUG(3, "    Receiving packet size %d status %4.4x.\n",
987                   pkt_len, rx_status);
988             if (skb != NULL) {
989                 skb->dev = dev;
990                 skb_reserve(skb, 2);
991                 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
992                         (pkt_len+3)>>2);
993                 skb->protocol = eth_type_trans(skb, dev);
994                 netif_rx(skb);
995                 dev->last_rx = jiffies;
996                 lp->stats.rx_packets++;
997                 lp->stats.rx_bytes += pkt_len;
998             } else {
999                 DEBUG(1, "%s: couldn't allocate a sk_buff of"
1000                       " size %d.\n", dev->name, pkt_len);
1001                 lp->stats.rx_dropped++;
1002             }
1003         }
1004         /* Pop the top of the Rx FIFO */
1005         tc589_wait_for_completion(dev, RxDiscard);
1006     }
1007     if (worklimit == 0)
1008         printk(KERN_WARNING "%s: too much work in el3_rx!\n", dev->name);
1009     return 0;
1010 }
1011
1012 static void set_multicast_list(struct net_device *dev)
1013 {
1014     struct el3_private *lp = netdev_priv(dev);
1015     dev_link_t *link = &lp->link;
1016     ioaddr_t ioaddr = dev->base_addr;
1017     u16 opts = SetRxFilter | RxStation | RxBroadcast;
1018
1019     if (!(DEV_OK(link))) return;
1020     if (dev->flags & IFF_PROMISC)
1021         opts |= RxMulticast | RxProm;
1022     else if (dev->mc_count || (dev->flags & IFF_ALLMULTI))
1023         opts |= RxMulticast;
1024     outw(opts, ioaddr + EL3_CMD);
1025 }
1026
1027 static int el3_close(struct net_device *dev)
1028 {
1029     struct el3_private *lp = netdev_priv(dev);
1030     dev_link_t *link = &lp->link;
1031     ioaddr_t ioaddr = dev->base_addr;
1032     
1033     DEBUG(1, "%s: shutting down ethercard.\n", dev->name);
1034
1035     if (DEV_OK(link)) {
1036         /* Turn off statistics ASAP.  We update lp->stats below. */
1037         outw(StatsDisable, ioaddr + EL3_CMD);
1038         
1039         /* Disable the receiver and transmitter. */
1040         outw(RxDisable, ioaddr + EL3_CMD);
1041         outw(TxDisable, ioaddr + EL3_CMD);
1042         
1043         if (dev->if_port == 2)
1044             /* Turn off thinnet power.  Green! */
1045             outw(StopCoax, ioaddr + EL3_CMD);
1046         else if (dev->if_port == 1) {
1047             /* Disable link beat and jabber */
1048             EL3WINDOW(4);
1049             outw(0, ioaddr + WN4_MEDIA);
1050         }
1051         
1052         /* Switching back to window 0 disables the IRQ. */
1053         EL3WINDOW(0);
1054         /* But we explicitly zero the IRQ line select anyway. */
1055         outw(0x0f00, ioaddr + WN0_IRQ);
1056         
1057         /* Check if the card still exists */
1058         if ((inw(ioaddr+EL3_STATUS) & 0xe000) == 0x2000)
1059             update_stats(dev);
1060     }
1061
1062     link->open--;
1063     netif_stop_queue(dev);
1064     del_timer_sync(&lp->media);
1065     
1066     return 0;
1067 }
1068
1069 static struct pcmcia_driver tc589_driver = {
1070         .owner          = THIS_MODULE,
1071         .drv            = {
1072                 .name   = "3c589_cs",
1073         },
1074         .attach         = tc589_attach,
1075         .detach         = tc589_detach,
1076 };
1077
1078 static int __init init_tc589(void)
1079 {
1080         return pcmcia_register_driver(&tc589_driver);
1081 }
1082
1083 static void __exit exit_tc589(void)
1084 {
1085         pcmcia_unregister_driver(&tc589_driver);
1086         while (dev_list != NULL)
1087                 tc589_detach(dev_list);
1088 }
1089
1090 module_init(init_tc589);
1091 module_exit(exit_tc589);