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