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