commented early_printk patch because of rejects.
[linux-flexiantxendom0-3.2.10.git] / drivers / net / pcmcia / 3c574_cs.c
1 /* 3c574.c: A PCMCIA ethernet driver for the 3com 3c574 "RoadRunner".
2
3         Written 1993-1998 by
4         Donald Becker, becker@scyld.com, (driver core) and
5         David Hinds, dahinds@users.sourceforge.net (from his PC card code).
6         Locking fixes (C) Copyright 2003 Red Hat Inc
7
8         This software may be used and distributed according to the terms of
9         the GNU General Public License, incorporated herein by reference.
10
11         This driver derives from Donald Becker's 3c509 core, which has the
12         following copyright:
13         Copyright 1993 United States Government as represented by the
14         Director, National Security Agency.
15         
16
17 */
18
19 /*
20                                 Theory of Operation
21
22 I. Board Compatibility
23
24 This device driver is designed for the 3Com 3c574 PC card Fast Ethernet
25 Adapter.
26
27 II. Board-specific settings
28
29 None -- PC cards are autoconfigured.
30
31 III. Driver operation
32
33 The 3c574 uses a Boomerang-style interface, without the bus-master capability.
34 See the Boomerang driver and documentation for most details.
35
36 IV. Notes and chip documentation.
37
38 Two added registers are used to enhance PIO performance, RunnerRdCtrl and
39 RunnerWrCtrl.  These are 11 bit down-counters that are preloaded with the
40 count of word (16 bits) reads or writes the driver is about to do to the Rx
41 or Tx FIFO.  The chip is then able to hide the internal-PCI-bus to PC-card
42 translation latency by buffering the I/O operations with an 8 word FIFO.
43 Note: No other chip accesses are permitted when this buffer is used.
44
45 A second enhancement is that both attribute and common memory space
46 0x0800-0x0fff can translated to the PIO FIFO.  Thus memory operations (faster
47 with *some* PCcard bridges) may be used instead of I/O operations.
48 This is enabled by setting the 0x10 bit in the PCMCIA LAN COR.
49
50 Some slow PC card bridges work better if they never see a WAIT signal.
51 This is configured by setting the 0x20 bit in the PCMCIA LAN COR.
52 Only do this after testing that it is reliable and improves performance.
53
54 The upper five bits of RunnerRdCtrl are used to window into PCcard
55 configuration space registers.  Window 0 is the regular Boomerang/Odie
56 register set, 1-5 are various PC card control registers, and 16-31 are
57 the (reversed!) CIS table.
58
59 A final note: writing the InternalConfig register in window 3 with an
60 invalid ramWidth is Very Bad.
61
62 V. References
63
64 http://www.scyld.com/expert/NWay.html
65 http://www.national.com/pf/DP/DP83840.html
66
67 Thanks to Terry Murphy of 3Com for providing development information for
68 earlier 3Com products.
69
70 */
71
72 #include <linux/module.h>
73 #include <linux/kernel.h>
74 #include <linux/init.h>
75 #include <linux/slab.h>
76 #include <linux/string.h>
77 #include <linux/timer.h>
78 #include <linux/interrupt.h>
79 #include <linux/in.h>
80 #include <linux/delay.h>
81 #include <linux/netdevice.h>
82 #include <linux/etherdevice.h>
83 #include <linux/skbuff.h>
84 #include <linux/if_arp.h>
85 #include <linux/ioport.h>
86 #include <linux/ethtool.h>
87
88 #include <pcmcia/version.h>
89 #include <pcmcia/cs_types.h>
90 #include <pcmcia/cs.h>
91 #include <pcmcia/cistpl.h>
92 #include <pcmcia/cisreg.h>
93 #include <pcmcia/ciscode.h>
94 #include <pcmcia/ds.h>
95 #include <pcmcia/mem_op.h>
96
97 #include <asm/uaccess.h>
98 #include <asm/io.h>
99 #include <asm/system.h>
100 #include <asm/bitops.h>
101
102 /*====================================================================*/
103
104 /* Module parameters */
105
106 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
107 MODULE_DESCRIPTION("3Com 3c574 series PCMCIA ethernet driver");
108 MODULE_LICENSE("GPL");
109
110 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
111
112 /* Now-standard PC card module parameters. */
113 INT_MODULE_PARM(irq_mask, 0xdeb8);
114 static int irq_list[4] = { -1 };
115 MODULE_PARM(irq_list, "1-4i");
116
117 /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
118 INT_MODULE_PARM(max_interrupt_work, 32);
119
120 /* Force full duplex modes? */
121 INT_MODULE_PARM(full_duplex, 0);
122
123 /* Autodetect link polarity reversal? */
124 INT_MODULE_PARM(auto_polarity, 1);
125
126 #ifdef PCMCIA_DEBUG
127 INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
128 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
129 static char *version =
130 "3c574_cs.c 1.65ac1 2003/04/07 Donald Becker/David Hinds, becker@scyld.com.\n";
131 #else
132 #define DEBUG(n, args...)
133 #endif
134
135 /*====================================================================*/
136
137 /* Time in jiffies before concluding the transmitter is hung. */
138 #define TX_TIMEOUT  ((800*HZ)/1000)
139
140 /* To minimize the size of the driver source and make the driver more
141    readable not all constants are symbolically defined.
142    You'll need the manual if you want to understand driver details anyway. */
143 /* Offsets from base I/O address. */
144 #define EL3_DATA        0x00
145 #define EL3_CMD         0x0e
146 #define EL3_STATUS      0x0e
147
148 #define EL3WINDOW(win_num) outw(SelectWindow + (win_num), ioaddr + EL3_CMD)
149
150 /* The top five bits written to EL3_CMD are a command, the lower
151    11 bits are the parameter, if applicable. */
152 enum el3_cmds {
153         TotalReset = 0<<11, SelectWindow = 1<<11, StartCoax = 2<<11,
154         RxDisable = 3<<11, RxEnable = 4<<11, RxReset = 5<<11, RxDiscard = 8<<11,
155         TxEnable = 9<<11, TxDisable = 10<<11, TxReset = 11<<11,
156         FakeIntr = 12<<11, AckIntr = 13<<11, SetIntrEnb = 14<<11,
157         SetStatusEnb = 15<<11, SetRxFilter = 16<<11, SetRxThreshold = 17<<11,
158         SetTxThreshold = 18<<11, SetTxStart = 19<<11, StatsEnable = 21<<11,
159         StatsDisable = 22<<11, StopCoax = 23<<11,
160 };
161
162 enum elxl_status {
163         IntLatch = 0x0001, AdapterFailure = 0x0002, TxComplete = 0x0004,
164         TxAvailable = 0x0008, RxComplete = 0x0010, RxEarly = 0x0020,
165         IntReq = 0x0040, StatsFull = 0x0080, CmdBusy = 0x1000 };
166
167 /* The SetRxFilter command accepts the following classes: */
168 enum RxFilter {
169         RxStation = 1, RxMulticast = 2, RxBroadcast = 4, RxProm = 8
170 };
171
172 enum Window0 {
173         Wn0EepromCmd = 10, Wn0EepromData = 12, /* EEPROM command/address, data. */
174         IntrStatus=0x0E,                /* Valid in all windows. */
175 };
176 /* These assumes the larger EEPROM. */
177 enum Win0_EEPROM_cmds {
178         EEPROM_Read = 0x200, EEPROM_WRITE = 0x100, EEPROM_ERASE = 0x300,
179         EEPROM_EWENB = 0x30,            /* Enable erasing/writing for 10 msec. */
180         EEPROM_EWDIS = 0x00,            /* Disable EWENB before 10 msec timeout. */
181 };
182
183 /* Register window 1 offsets, the window used in normal operation.
184    On the "Odie" this window is always mapped at offsets 0x10-0x1f.
185    Except for TxFree, which is overlapped by RunnerWrCtrl. */
186 enum Window1 {
187         TX_FIFO = 0x10,  RX_FIFO = 0x10,  RxErrors = 0x14,
188         RxStatus = 0x18,  Timer=0x1A, TxStatus = 0x1B,
189         TxFree = 0x0C, /* Remaining free bytes in Tx buffer. */
190         RunnerRdCtrl = 0x16, RunnerWrCtrl = 0x1c,
191 };
192
193 enum Window3 {                  /* Window 3: MAC/config bits. */
194         Wn3_Config=0, Wn3_MAC_Ctrl=6, Wn3_Options=8,
195 };
196 union wn3_config {
197         int i;
198         struct w3_config_fields {
199                 unsigned int ram_size:3, ram_width:1, ram_speed:2, rom_size:2;
200                 int pad8:8;
201                 unsigned int ram_split:2, pad18:2, xcvr:3, pad21:1, autoselect:1;
202                 int pad24:7;
203         } u;
204 };
205
206 enum Window4 {          /* Window 4: Xcvr/media bits. */
207         Wn4_FIFODiag = 4, Wn4_NetDiag = 6, Wn4_PhysicalMgmt=8, Wn4_Media = 10,
208 };
209
210 #define MEDIA_TP        0x00C0  /* Enable link beat and jabber for 10baseT. */
211
212 struct el3_private {
213         dev_link_t link;
214         dev_node_t node;
215         struct net_device_stats stats;
216         u16 advertising, partner;               /* NWay media advertisement */
217         unsigned char phys;                     /* MII device address */
218         unsigned int autoselect:1, default_media:3;     /* Read from the EEPROM/Wn3_Config. */
219         /* for transceiver monitoring */
220         struct timer_list media;
221         unsigned short media_status;
222         unsigned short fast_poll;
223         unsigned long last_irq;
224         spinlock_t window_lock;                 /* Guards the Window selection */
225 };
226
227 /* Set iff a MII transceiver on any interface requires mdio preamble.
228    This only set with the original DP83840 on older 3c905 boards, so the extra
229    code size of a per-interface flag is not worthwhile. */
230 static char mii_preamble_required = 0;
231
232 /* Index of functions. */
233
234 static void tc574_config(dev_link_t *link);
235 static void tc574_release(dev_link_t *link);
236 static int tc574_event(event_t event, int priority,
237                                            event_callback_args_t *args);
238
239 static void mdio_sync(ioaddr_t ioaddr, int bits);
240 static int mdio_read(ioaddr_t ioaddr, int phy_id, int location);
241 static void mdio_write(ioaddr_t ioaddr, int phy_id, int location, int value);
242 static unsigned short read_eeprom(ioaddr_t ioaddr, int index);
243 static void tc574_wait_for_completion(struct net_device *dev, int cmd);
244
245 static void tc574_reset(struct net_device *dev);
246 static void media_check(unsigned long arg);
247 static int el3_open(struct net_device *dev);
248 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev);
249 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs);
250 static void update_stats(struct net_device *dev);
251 static struct net_device_stats *el3_get_stats(struct net_device *dev);
252 static int el3_rx(struct net_device *dev, int worklimit);
253 static int el3_close(struct net_device *dev);
254 static void el3_tx_timeout(struct net_device *dev);
255 static int el3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
256 static void set_rx_mode(struct net_device *dev);
257
258 static dev_info_t dev_info = "3c574_cs";
259
260 static dev_link_t *tc574_attach(void);
261 static void tc574_detach(dev_link_t *);
262
263 static dev_link_t *dev_list;
264
265 static void flush_stale_links(void)
266 {
267         dev_link_t *link, *next;
268         for (link = dev_list; link; link = next) {
269                 next = link->next;
270                 if (link->state & DEV_STALE_LINK)
271                         tc574_detach(link);
272         }
273 }
274
275 /*
276         tc574_attach() creates an "instance" of the driver, allocating
277         local data structures for one device.  The device is registered
278         with Card Services.
279 */
280
281 static dev_link_t *tc574_attach(void)
282 {
283         struct el3_private *lp;
284         client_reg_t client_reg;
285         dev_link_t *link;
286         struct net_device *dev;
287         int i, ret;
288
289         DEBUG(0, "3c574_attach()\n");
290         flush_stale_links();
291
292         /* Create the PC card device object. */
293         dev = alloc_etherdev(sizeof(struct el3_private));
294         if (!dev)
295                 return NULL;
296         lp = dev->priv;
297         link = &lp->link;
298         link->priv = dev;
299
300         spin_lock_init(&lp->window_lock);
301         link->io.NumPorts1 = 32;
302         link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
303         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
304         link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
305         if (irq_list[0] == -1)
306                 link->irq.IRQInfo2 = irq_mask;
307         else
308                 for (i = 0; i < 4; i++)
309                         link->irq.IRQInfo2 |= 1 << irq_list[i];
310         link->irq.Handler = &el3_interrupt;
311         link->irq.Instance = dev;
312         link->conf.Attributes = CONF_ENABLE_IRQ;
313         link->conf.Vcc = 50;
314         link->conf.IntType = INT_MEMORY_AND_IO;
315         link->conf.ConfigIndex = 1;
316         link->conf.Present = PRESENT_OPTION;
317
318         /* The EL3-specific entries in the device structure. */
319         dev->hard_start_xmit = &el3_start_xmit;
320         dev->get_stats = &el3_get_stats;
321         dev->do_ioctl = &el3_ioctl;
322         dev->set_multicast_list = &set_rx_mode;
323         dev->open = &el3_open;
324         dev->stop = &el3_close;
325 #ifdef HAVE_TX_TIMEOUT
326         dev->tx_timeout = el3_tx_timeout;
327         dev->watchdog_timeo = TX_TIMEOUT;
328 #endif
329
330         /* Register with Card Services */
331         link->next = dev_list;
332         dev_list = link;
333         client_reg.dev_info = &dev_info;
334         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
335         client_reg.EventMask =
336                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
337                         CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
338                                 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
339         client_reg.event_handler = &tc574_event;
340         client_reg.Version = 0x0210;
341         client_reg.event_callback_args.client_data = link;
342         ret = CardServices(RegisterClient, &link->handle, &client_reg);
343         if (ret != 0) {
344                 cs_error(link->handle, RegisterClient, ret);
345                 tc574_detach(link);
346                 return NULL;
347         }
348
349         return link;
350 } /* tc574_attach */
351
352 /*
353
354         This deletes a driver "instance".  The device is de-registered
355         with Card Services.  If it has been released, all local data
356         structures are freed.  Otherwise, the structures will be freed
357         when the device is released.
358
359 */
360
361 static void tc574_detach(dev_link_t *link)
362 {
363         struct net_device *dev = link->priv;
364         dev_link_t **linkp;
365
366         DEBUG(0, "3c574_detach(0x%p)\n", link);
367
368         /* Locate device structure */
369         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
370                 if (*linkp == link) break;
371         if (*linkp == NULL)
372         return;
373
374         if (link->state & DEV_CONFIG) {
375                 tc574_release(link);
376                 if (link->state & DEV_STALE_CONFIG) {
377                         link->state |= DEV_STALE_LINK;
378                         return;
379                 }
380         }
381
382         if (link->handle)
383                 CardServices(DeregisterClient, link->handle);
384
385         /* Unlink device structure, free bits */
386         *linkp = link->next;
387         if (link->dev)
388                 unregister_netdev(dev);
389         kfree(dev);
390
391 } /* tc574_detach */
392
393 /*
394         tc574_config() is scheduled to run after a CARD_INSERTION event
395         is received, to configure the PCMCIA socket, and to make the
396         ethernet device available to the system.
397 */
398
399 #define CS_CHECK(fn, args...) \
400 while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
401
402 static void tc574_config(dev_link_t *link)
403 {
404         client_handle_t handle = link->handle;
405         struct net_device *dev = link->priv;
406         struct el3_private *lp = dev->priv;
407         tuple_t tuple;
408         cisparse_t parse;
409         unsigned short buf[32];
410         int last_fn, last_ret, i, j;
411         ioaddr_t ioaddr;
412         u16 *phys_addr;
413         char *cardname;
414
415         phys_addr = (u16 *)dev->dev_addr;
416
417         DEBUG(0, "3c574_config(0x%p)\n", link);
418
419         tuple.Attributes = 0;
420         tuple.DesiredTuple = CISTPL_CONFIG;
421         CS_CHECK(GetFirstTuple, handle, &tuple);
422         tuple.TupleData = (cisdata_t *)buf;
423         tuple.TupleDataMax = 64;
424         tuple.TupleOffset = 0;
425         CS_CHECK(GetTupleData, handle, &tuple);
426         CS_CHECK(ParseTuple, handle, &tuple, &parse);
427         link->conf.ConfigBase = parse.config.base;
428         link->conf.Present = parse.config.rmask[0];
429
430         /* Configure card */
431         link->state |= DEV_CONFIG;
432
433         link->io.IOAddrLines = 16;
434         for (i = j = 0; j < 0x400; j += 0x20) {
435                 link->io.BasePort1 = j ^ 0x300;
436                 i = CardServices(RequestIO, link->handle, &link->io);
437                 if (i == CS_SUCCESS) break;
438         }
439         if (i != CS_SUCCESS) {
440                 cs_error(link->handle, RequestIO, i);
441                 goto failed;
442         }
443         CS_CHECK(RequestIRQ, link->handle, &link->irq);
444         CS_CHECK(RequestConfiguration, link->handle, &link->conf);
445
446         dev->irq = link->irq.AssignedIRQ;
447         dev->base_addr = link->io.BasePort1;
448
449         if (register_netdev(dev) != 0) {
450                 printk(KERN_NOTICE "3c574_cs: register_netdev() failed\n");
451                 goto failed;
452         }
453
454         ioaddr = dev->base_addr;
455         strcpy(lp->node.dev_name, dev->name);
456         link->dev = &lp->node;
457         link->state &= ~DEV_CONFIG_PENDING;
458
459         /* The 3c574 normally uses an EEPROM for configuration info, including
460            the hardware address.  The future products may include a modem chip
461            and put the address in the CIS. */
462         tuple.DesiredTuple = 0x88;
463         if (CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS) {
464                 CardServices(GetTupleData, handle, &tuple);
465                 for (i = 0; i < 3; i++)
466                         phys_addr[i] = htons(buf[i]);
467         } else {
468                 EL3WINDOW(0);
469                 for (i = 0; i < 3; i++)
470                         phys_addr[i] = htons(read_eeprom(ioaddr, i + 10));
471                 if (phys_addr[0] == 0x6060) {
472                         printk(KERN_NOTICE "3c574_cs: IO port conflict at 0x%03lx"
473                                    "-0x%03lx\n", dev->base_addr, dev->base_addr+15);
474                         goto failed;
475                 }
476         }
477         tuple.DesiredTuple = CISTPL_VERS_1;
478         if (CardServices(GetFirstTuple, handle, &tuple) == CS_SUCCESS &&
479                 CardServices(GetTupleData, handle, &tuple) == CS_SUCCESS &&
480                 CardServices(ParseTuple, handle, &tuple, &parse) == CS_SUCCESS) {
481                 cardname = parse.version_1.str + parse.version_1.ofs[1];
482         } else
483                 cardname = "3Com 3c574";
484
485         printk(KERN_INFO "%s: %s at io %#3lx, irq %d, hw_addr ",
486                    dev->name, cardname, dev->base_addr, dev->irq);
487
488         for (i = 0; i < 6; i++)
489                 printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : ".\n"));
490
491         {
492                 u_char mcr, *ram_split[] = {"5:3", "3:1", "1:1", "3:5"};
493                 union wn3_config config;
494                 outw(2<<11, ioaddr + RunnerRdCtrl);
495                 mcr = inb(ioaddr + 2);
496                 outw(0<<11, ioaddr + RunnerRdCtrl);
497                 printk(KERN_INFO "  ASIC rev %d,", mcr>>3);
498                 EL3WINDOW(3);
499                 config.i = inl(ioaddr + Wn3_Config);
500                 printk(" %dK FIFO split %s Rx:Tx, %sMII interface.\n",
501                            8 << config.u.ram_size, ram_split[config.u.ram_split],
502                            config.u.autoselect ? "autoselect " : "");
503                 lp->default_media = config.u.xcvr;
504                 lp->autoselect = config.u.autoselect;
505         }
506
507         init_timer(&lp->media);
508
509         {
510                 int phy;
511                 
512                 /* Roadrunner only: Turn on the MII transceiver */
513                 outw(0x8040, ioaddr + Wn3_Options);
514                 mdelay(1);
515                 outw(0xc040, ioaddr + Wn3_Options);
516                 tc574_wait_for_completion(dev, TxReset);
517                 tc574_wait_for_completion(dev, RxReset);
518                 mdelay(1);
519                 outw(0x8040, ioaddr + Wn3_Options);
520                 
521                 EL3WINDOW(4);
522                 for (phy = 1; phy <= 32; phy++) {
523                         int mii_status;
524                         mdio_sync(ioaddr, 32);
525                         mii_status = mdio_read(ioaddr, phy & 0x1f, 1);
526                         if (mii_status != 0xffff) {
527                                 lp->phys = phy & 0x1f;
528                                 DEBUG(0, "  MII transceiver at index %d, status %x.\n",
529                                           phy, mii_status);
530                                 if ((mii_status & 0x0040) == 0)
531                                         mii_preamble_required = 1;
532                                 break;
533                         }
534                 }
535                 if (phy > 32) {
536                         printk(KERN_NOTICE "  No MII transceivers found!\n");
537                         goto failed;
538                 }
539                 i = mdio_read(ioaddr, lp->phys, 16) | 0x40;
540                 mdio_write(ioaddr, lp->phys, 16, i);
541                 lp->advertising = mdio_read(ioaddr, lp->phys, 4);
542                 if (full_duplex) {
543                         /* Only advertise the FD media types. */
544                         lp->advertising &= ~0x02a0;
545                         mdio_write(ioaddr, lp->phys, 4, lp->advertising);
546                 }
547         }
548
549         return;
550
551 cs_failed:
552         cs_error(link->handle, last_fn, last_ret);
553 failed:
554         tc574_release(link);
555         return;
556
557 } /* tc574_config */
558
559 /*
560         After a card is removed, tc574_release() will unregister the net
561         device, and release the PCMCIA configuration.  If the device is
562         still open, this will be postponed until it is closed.
563 */
564
565 static void tc574_release(dev_link_t *link)
566 {
567         DEBUG(0, "3c574_release(0x%p)\n", link);
568
569         if (link->open) {
570                 DEBUG(1, "3c574_cs: release postponed, '%s' still open\n",
571                           link->dev->dev_name);
572                 link->state |= DEV_STALE_CONFIG;
573                 return;
574         }
575
576         CardServices(ReleaseConfiguration, link->handle);
577         CardServices(ReleaseIO, link->handle, &link->io);
578         CardServices(ReleaseIRQ, link->handle, &link->irq);
579
580         link->state &= ~DEV_CONFIG;
581
582 } /* tc574_release */
583
584 /*
585         The card status event handler.  Mostly, this schedules other
586         stuff to run after an event is received.  A CARD_REMOVAL event
587         also sets some flags to discourage the net drivers from trying
588         to talk to the card any more.
589 */
590
591 static int tc574_event(event_t event, int priority,
592                                            event_callback_args_t *args)
593 {
594         dev_link_t *link = args->client_data;
595         struct net_device *dev = link->priv;
596
597         DEBUG(1, "3c574_event(0x%06x)\n", event);
598
599         switch (event) {
600         case CS_EVENT_CARD_REMOVAL:
601                 link->state &= ~DEV_PRESENT;
602                 if (link->state & DEV_CONFIG) {
603                         netif_device_detach(dev);
604                         tc574_release(link);
605                 }
606                 break;
607         case CS_EVENT_CARD_INSERTION:
608                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
609                 tc574_config(link);
610                 break;
611         case CS_EVENT_PM_SUSPEND:
612                 link->state |= DEV_SUSPEND;
613                 /* Fall through... */
614         case CS_EVENT_RESET_PHYSICAL:
615                 if (link->state & DEV_CONFIG) {
616                         if (link->open)
617                                 netif_device_detach(dev);
618                         CardServices(ReleaseConfiguration, link->handle);
619                 }
620                 break;
621         case CS_EVENT_PM_RESUME:
622                 link->state &= ~DEV_SUSPEND;
623                 /* Fall through... */
624         case CS_EVENT_CARD_RESET:
625                 if (link->state & DEV_CONFIG) {
626                         CardServices(RequestConfiguration, link->handle, &link->conf);
627                         if (link->open) {
628                                 tc574_reset(dev);
629                                 netif_device_attach(dev);
630                         }
631                 }
632                 break;
633         }
634         return 0;
635 } /* tc574_event */
636
637 static void dump_status(struct net_device *dev)
638 {
639         ioaddr_t ioaddr = dev->base_addr;
640         EL3WINDOW(1);
641         printk(KERN_INFO "  irq status %04x, rx status %04x, tx status "
642                    "%02x, tx free %04x\n", inw(ioaddr+EL3_STATUS),
643                    inw(ioaddr+RxStatus), inb(ioaddr+TxStatus),
644                    inw(ioaddr+TxFree));
645         EL3WINDOW(4);
646         printk(KERN_INFO "  diagnostics: fifo %04x net %04x ethernet %04x"
647                    " media %04x\n", inw(ioaddr+0x04), inw(ioaddr+0x06),
648                    inw(ioaddr+0x08), inw(ioaddr+0x0a));
649         EL3WINDOW(1);
650 }
651
652 /*
653   Use this for commands that may take time to finish
654 */
655 static void tc574_wait_for_completion(struct net_device *dev, int cmd)
656 {
657         int i = 1500;
658         outw(cmd, dev->base_addr + EL3_CMD);
659         while (--i > 0)
660                 if (!(inw(dev->base_addr + EL3_STATUS) & 0x1000)) break;
661         if (i == 0)
662                 printk(KERN_NOTICE "%s: command 0x%04x did not complete!\n", dev->name, cmd);
663 }
664
665 /* Read a word from the EEPROM using the regular EEPROM access register.
666    Assume that we are in register window zero.
667  */
668 static unsigned short read_eeprom(ioaddr_t ioaddr, int index)
669 {
670         int timer;
671         outw(EEPROM_Read + index, ioaddr + Wn0EepromCmd);
672         /* Pause for at least 162 usec for the read to take place. */
673         for (timer = 1620; timer >= 0; timer--) {
674                 if ((inw(ioaddr + Wn0EepromCmd) & 0x8000) == 0)
675                         break;
676         }
677         return inw(ioaddr + Wn0EepromData);
678 }
679
680 /* MII transceiver control section.
681    Read and write the MII registers using software-generated serial
682    MDIO protocol.  See the MII specifications or DP83840A data sheet
683    for details.
684    The maxium data clock rate is 2.5 Mhz.  The timing is easily met by the
685    slow PC card interface. */
686
687 #define MDIO_SHIFT_CLK  0x01
688 #define MDIO_DIR_WRITE  0x04
689 #define MDIO_DATA_WRITE0 (0x00 | MDIO_DIR_WRITE)
690 #define MDIO_DATA_WRITE1 (0x02 | MDIO_DIR_WRITE)
691 #define MDIO_DATA_READ  0x02
692 #define MDIO_ENB_IN             0x00
693
694 /* Generate the preamble required for initial synchronization and
695    a few older transceivers. */
696 static void mdio_sync(ioaddr_t ioaddr, int bits)
697 {
698         int mdio_addr = ioaddr + Wn4_PhysicalMgmt;
699
700         /* Establish sync by sending at least 32 logic ones. */
701         while (-- bits >= 0) {
702                 outw(MDIO_DATA_WRITE1, mdio_addr);
703                 outw(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
704         }
705 }
706
707 static int mdio_read(ioaddr_t ioaddr, int phy_id, int location)
708 {
709         int i;
710         int read_cmd = (0xf6 << 10) | (phy_id << 5) | location;
711         unsigned int retval = 0;
712         int mdio_addr = ioaddr + Wn4_PhysicalMgmt;
713
714         if (mii_preamble_required)
715                 mdio_sync(ioaddr, 32);
716
717         /* Shift the read command bits out. */
718         for (i = 14; i >= 0; i--) {
719                 int dataval = (read_cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
720                 outw(dataval, mdio_addr);
721                 outw(dataval | MDIO_SHIFT_CLK, mdio_addr);
722         }
723         /* Read the two transition, 16 data, and wire-idle bits. */
724         for (i = 19; i > 0; i--) {
725                 outw(MDIO_ENB_IN, mdio_addr);
726                 retval = (retval << 1) | ((inw(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
727                 outw(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
728         }
729         return (retval>>1) & 0xffff;
730 }
731
732 static void mdio_write(ioaddr_t ioaddr, int phy_id, int location, int value)
733 {
734         int write_cmd = 0x50020000 | (phy_id << 23) | (location << 18) | value;
735         int mdio_addr = ioaddr + Wn4_PhysicalMgmt;
736         int i;
737
738         if (mii_preamble_required)
739                 mdio_sync(ioaddr, 32);
740
741         /* Shift the command bits out. */
742         for (i = 31; i >= 0; i--) {
743                 int dataval = (write_cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
744                 outw(dataval, mdio_addr);
745                 outw(dataval | MDIO_SHIFT_CLK, mdio_addr);
746         }
747         /* Leave the interface idle. */
748         for (i = 1; i >= 0; i--) {
749                 outw(MDIO_ENB_IN, mdio_addr);
750                 outw(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
751         }
752
753         return;
754 }
755
756 /* Reset and restore all of the 3c574 registers. */
757 static void tc574_reset(struct net_device *dev)
758 {
759         struct el3_private *lp = (struct el3_private *)dev->priv;
760         int i, ioaddr = dev->base_addr;
761         unsigned long flags;
762
763         tc574_wait_for_completion(dev, TotalReset|0x10);
764
765         spin_lock_irqsave(&lp->window_lock, flags);
766         /* Clear any transactions in progress. */
767         outw(0, ioaddr + RunnerWrCtrl);
768         outw(0, ioaddr + RunnerRdCtrl);
769
770         /* Set the station address and mask. */
771         EL3WINDOW(2);
772         for (i = 0; i < 6; i++)
773                 outb(dev->dev_addr[i], ioaddr + i);
774         for (; i < 12; i+=2)
775                 outw(0, ioaddr + i);
776
777         /* Reset config options */
778         EL3WINDOW(3);
779         outb((dev->mtu > 1500 ? 0x40 : 0), ioaddr + Wn3_MAC_Ctrl);
780         outl((lp->autoselect ? 0x01000000 : 0) | 0x0062001b,
781                  ioaddr + Wn3_Config);
782         /* Roadrunner only: Turn on the MII transceiver. */
783         outw(0x8040, ioaddr + Wn3_Options);
784         mdelay(1);
785         outw(0xc040, ioaddr + Wn3_Options);
786         EL3WINDOW(1);
787         spin_unlock_irqrestore(&lp->window_lock, flags);
788         
789         tc574_wait_for_completion(dev, TxReset);
790         tc574_wait_for_completion(dev, RxReset);
791         mdelay(1);
792         spin_lock_irqsave(&lp->window_lock, flags);
793         EL3WINDOW(3);
794         outw(0x8040, ioaddr + Wn3_Options);
795
796         /* Switch to the stats window, and clear all stats by reading. */
797         outw(StatsDisable, ioaddr + EL3_CMD);
798         EL3WINDOW(6);
799         for (i = 0; i < 10; i++)
800                 inb(ioaddr + i);
801         inw(ioaddr + 10);
802         inw(ioaddr + 12);
803         EL3WINDOW(4);
804         inb(ioaddr + 12);
805         inb(ioaddr + 13);
806
807         /* .. enable any extra statistics bits.. */
808         outw(0x0040, ioaddr + Wn4_NetDiag);
809         
810         EL3WINDOW(1);
811         spin_unlock_irqrestore(&lp->window_lock, flags);
812         
813         /* .. re-sync MII and re-fill what NWay is advertising. */
814         mdio_sync(ioaddr, 32);
815         mdio_write(ioaddr, lp->phys, 4, lp->advertising);
816         if (!auto_polarity) {
817                 /* works for TDK 78Q2120 series MII's */
818                 int i = mdio_read(ioaddr, lp->phys, 16) | 0x20;
819                 mdio_write(ioaddr, lp->phys, 16, i);
820         }
821
822         spin_lock_irqsave(&lp->window_lock, flags);
823         /* Switch to register set 1 for normal use, just for TxFree. */
824         set_rx_mode(dev);
825         spin_unlock_irqrestore(&lp->window_lock, flags);
826         outw(StatsEnable, ioaddr + EL3_CMD); /* Turn on statistics. */
827         outw(RxEnable, ioaddr + EL3_CMD); /* Enable the receiver. */
828         outw(TxEnable, ioaddr + EL3_CMD); /* Enable transmitter. */
829         /* Allow status bits to be seen. */
830         outw(SetStatusEnb | 0xff, ioaddr + EL3_CMD);
831         /* Ack all pending events, and set active indicator mask. */
832         outw(AckIntr | IntLatch | TxAvailable | RxEarly | IntReq,
833                  ioaddr + EL3_CMD);
834         outw(SetIntrEnb | IntLatch | TxAvailable | RxComplete | StatsFull
835                  | AdapterFailure | RxEarly, ioaddr + EL3_CMD);
836 }
837
838 static int el3_open(struct net_device *dev)
839 {
840         struct el3_private *lp = (struct el3_private *)dev->priv;
841         dev_link_t *link = &lp->link;
842
843         if (!DEV_OK(link))
844                 return -ENODEV;
845         
846         link->open++;
847         netif_start_queue(dev);
848         
849         tc574_reset(dev);
850         lp->media.function = &media_check;
851         lp->media.data = (unsigned long) dev;
852         lp->media.expires = jiffies + HZ;
853         add_timer(&lp->media);
854         
855         DEBUG(2, "%s: opened, status %4.4x.\n",
856                   dev->name, inw(dev->base_addr + EL3_STATUS));
857         
858         return 0;
859 }
860
861 static void el3_tx_timeout(struct net_device *dev)
862 {
863         struct el3_private *lp = (struct el3_private *)dev->priv;
864         ioaddr_t ioaddr = dev->base_addr;
865         
866         printk(KERN_NOTICE "%s: Transmit timed out!\n", dev->name);
867         dump_status(dev);
868         lp->stats.tx_errors++;
869         dev->trans_start = jiffies;
870         /* Issue TX_RESET and TX_START commands. */
871         tc574_wait_for_completion(dev, TxReset);
872         outw(TxEnable, ioaddr + EL3_CMD);
873         netif_wake_queue(dev);
874 }
875
876 static void pop_tx_status(struct net_device *dev)
877 {
878         struct el3_private *lp = (struct el3_private *)dev->priv;
879         ioaddr_t ioaddr = dev->base_addr;
880         int i;
881     
882         /* Clear the Tx status stack. */
883         for (i = 32; i > 0; i--) {
884                 u_char tx_status = inb(ioaddr + TxStatus);
885                 if (!(tx_status & 0x84))
886                         break;
887                 /* reset transmitter on jabber error or underrun */
888                 if (tx_status & 0x30)
889                         tc574_wait_for_completion(dev, TxReset);
890                 if (tx_status & 0x38) {
891                         DEBUG(1, "%s: transmit error: status 0x%02x\n",
892                                   dev->name, tx_status);
893                         outw(TxEnable, ioaddr + EL3_CMD);
894                         lp->stats.tx_aborted_errors++;
895                 }
896                 outb(0x00, ioaddr + TxStatus); /* Pop the status stack. */
897         }
898 }
899
900 static int el3_start_xmit(struct sk_buff *skb, struct net_device *dev)
901 {
902         ioaddr_t ioaddr = dev->base_addr;
903         struct el3_private *lp = (struct el3_private *)dev->priv;
904         unsigned long flags;
905
906         DEBUG(3, "%s: el3_start_xmit(length = %ld) called, "
907                   "status %4.4x.\n", dev->name, (long)skb->len,
908                   inw(ioaddr + EL3_STATUS));
909
910         spin_lock_irqsave(&lp->window_lock, flags);
911         outw(skb->len, ioaddr + TX_FIFO);
912         outw(0, ioaddr + TX_FIFO);
913         outsl(ioaddr + TX_FIFO, skb->data, (skb->len+3)>>2);
914
915         dev->trans_start = jiffies;
916
917         /* TxFree appears only in Window 1, not offset 0x1c. */
918         if (inw(ioaddr + TxFree) <= 1536) {
919                 netif_stop_queue(dev);
920                 /* Interrupt us when the FIFO has room for max-sized packet. 
921                    The threshold is in units of dwords. */
922                 outw(SetTxThreshold + (1536>>2), ioaddr + EL3_CMD);
923         }
924
925         pop_tx_status(dev);
926         spin_unlock_irqrestore(&lp->window_lock, flags);
927         dev_kfree_skb(skb);
928         return 0;
929 }
930
931 /* The EL3 interrupt handler. */
932 static irqreturn_t el3_interrupt(int irq, void *dev_id, struct pt_regs *regs)
933 {
934         struct net_device *dev = (struct net_device *) dev_id;
935         struct el3_private *lp = dev->priv;
936         ioaddr_t ioaddr, status;
937         int work_budget = max_interrupt_work;
938         int handled = 0;
939
940         if (!netif_device_present(dev))
941                 return IRQ_NONE;
942         ioaddr = dev->base_addr;
943
944         DEBUG(3, "%s: interrupt, status %4.4x.\n",
945                   dev->name, inw(ioaddr + EL3_STATUS));
946
947         spin_lock(&lp->window_lock);
948         
949         while ((status = inw(ioaddr + EL3_STATUS)) &
950                    (IntLatch | RxComplete | RxEarly | StatsFull)) {
951                 if (!netif_device_present(dev) ||
952                         ((status & 0xe000) != 0x2000)) {
953                         DEBUG(1, "%s: Interrupt from dead card\n", dev->name);
954                         break;
955                 }
956
957                 handled = 1;
958
959                 if (status & RxComplete)
960                         work_budget = el3_rx(dev, work_budget);
961
962                 if (status & TxAvailable) {
963                         DEBUG(3, "  TX room bit was handled.\n");
964                         /* There's room in the FIFO for a full-sized packet. */
965                         outw(AckIntr | TxAvailable, ioaddr + EL3_CMD);
966                         netif_wake_queue(dev);
967                 }
968
969                 if (status & TxComplete)
970                         pop_tx_status(dev);
971
972                 if (status & (AdapterFailure | RxEarly | StatsFull)) {
973                         /* Handle all uncommon interrupts. */
974                         if (status & StatsFull)
975                                 update_stats(dev);
976                         if (status & RxEarly) {
977                                 work_budget = el3_rx(dev, work_budget);
978                                 outw(AckIntr | RxEarly, ioaddr + EL3_CMD);
979                         }
980                         if (status & AdapterFailure) {
981                                 u16 fifo_diag;
982                                 EL3WINDOW(4);
983                                 fifo_diag = inw(ioaddr + Wn4_FIFODiag);
984                                 EL3WINDOW(1);
985                                 printk(KERN_NOTICE "%s: adapter failure, FIFO diagnostic"
986                                            " register %04x.\n", dev->name, fifo_diag);
987                                 if (fifo_diag & 0x0400) {
988                                         /* Tx overrun */
989                                         tc574_wait_for_completion(dev, TxReset);
990                                         outw(TxEnable, ioaddr + EL3_CMD);
991                                 }
992                                 if (fifo_diag & 0x2000) {
993                                         /* Rx underrun */
994                                         tc574_wait_for_completion(dev, RxReset);
995                                         set_rx_mode(dev);
996                                         outw(RxEnable, ioaddr + EL3_CMD);
997                                 }
998                                 outw(AckIntr | AdapterFailure, ioaddr + EL3_CMD);
999                         }
1000                 }
1001
1002                 if (--work_budget < 0) {
1003                         DEBUG(0, "%s: Too much work in interrupt, "
1004                                   "status %4.4x.\n", dev->name, status);
1005                         /* Clear all interrupts */
1006                         outw(AckIntr | 0xFF, ioaddr + EL3_CMD);
1007                         break;
1008                 }
1009                 /* Acknowledge the IRQ. */
1010                 outw(AckIntr | IntReq | IntLatch, ioaddr + EL3_CMD);
1011         }
1012
1013         DEBUG(3, "%s: exiting interrupt, status %4.4x.\n",
1014                   dev->name, inw(ioaddr + EL3_STATUS));
1015                   
1016         spin_unlock(&lp->window_lock);
1017         return IRQ_RETVAL(handled);
1018 }
1019
1020 /*
1021     This timer serves two purposes: to check for missed interrupts
1022         (and as a last resort, poll the NIC for events), and to monitor
1023         the MII, reporting changes in cable status.
1024 */
1025 static void media_check(unsigned long arg)
1026 {
1027         struct net_device *dev = (struct net_device *) arg;
1028         struct el3_private *lp = dev->priv;
1029         ioaddr_t ioaddr = dev->base_addr;
1030         unsigned long flags;
1031         unsigned short /* cable, */ media, partner;
1032
1033         if (!netif_device_present(dev))
1034                 goto reschedule;
1035         
1036         /* Check for pending interrupt with expired latency timer: with
1037            this, we can limp along even if the interrupt is blocked */
1038         if ((inw(ioaddr + EL3_STATUS) & IntLatch) && (inb(ioaddr + Timer) == 0xff)) {
1039                 if (!lp->fast_poll)
1040                         printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
1041                 el3_interrupt(dev->irq, lp, NULL);
1042                 lp->fast_poll = HZ;
1043         }
1044         if (lp->fast_poll) {
1045                 lp->fast_poll--;
1046                 lp->media.expires = jiffies + 2*HZ/100;
1047                 add_timer(&lp->media);
1048                 return;
1049         }
1050
1051         spin_lock_irqsave(&lp->window_lock, flags);
1052         EL3WINDOW(4);
1053         media = mdio_read(ioaddr, lp->phys, 1);
1054         partner = mdio_read(ioaddr, lp->phys, 5);
1055         EL3WINDOW(1);
1056         
1057         if (media != lp->media_status) {
1058                 if ((media ^ lp->media_status) & 0x0004)
1059                         printk(KERN_INFO "%s: %s link beat\n", dev->name,
1060                                    (lp->media_status & 0x0004) ? "lost" : "found");
1061                 if ((media ^ lp->media_status) & 0x0020) {
1062                         lp->partner = 0;
1063                         if (lp->media_status & 0x0020) {
1064                                 printk(KERN_INFO "%s: autonegotiation restarted\n",
1065                                            dev->name);
1066                         } else if (partner) {
1067                                 partner &= lp->advertising;
1068                                 lp->partner = partner;
1069                                 printk(KERN_INFO "%s: autonegotiation complete: "
1070                                            "%sbaseT-%cD selected\n", dev->name,
1071                                            ((partner & 0x0180) ? "100" : "10"),
1072                                            ((partner & 0x0140) ? 'F' : 'H'));
1073                         } else {
1074                                 printk(KERN_INFO "%s: link partner did not autonegotiate\n",
1075                                            dev->name);
1076                         }
1077
1078                         EL3WINDOW(3);
1079                         outb((partner & 0x0140 ? 0x20 : 0) |
1080                                  (dev->mtu > 1500 ? 0x40 : 0), ioaddr + Wn3_MAC_Ctrl);
1081                         EL3WINDOW(1);
1082
1083                 }
1084                 if (media & 0x0010)
1085                         printk(KERN_INFO "%s: remote fault detected\n",
1086                                    dev->name);
1087                 if (media & 0x0002)
1088                         printk(KERN_INFO "%s: jabber detected\n", dev->name);
1089                 lp->media_status = media;
1090         }
1091         spin_unlock_irqrestore(&lp->window_lock, flags);
1092
1093 reschedule:
1094         lp->media.expires = jiffies + HZ;
1095         add_timer(&lp->media);
1096 }
1097
1098 static struct net_device_stats *el3_get_stats(struct net_device *dev)
1099 {
1100         struct el3_private *lp = (struct el3_private *)dev->priv;
1101
1102         if (netif_device_present(dev))
1103                 update_stats(dev);
1104         return &lp->stats;
1105 }
1106
1107 /*  Update statistics.
1108         Suprisingly this need not be run single-threaded, but it effectively is.
1109         The counters clear when read, so the adds must merely be atomic.
1110  */
1111 static void update_stats(struct net_device *dev)
1112 {
1113         struct el3_private *lp = (struct el3_private *)dev->priv;
1114         ioaddr_t ioaddr = dev->base_addr;
1115         unsigned long flags;
1116         u8 rx, tx, up;
1117
1118         DEBUG(2, "%s: updating the statistics.\n", dev->name);
1119
1120         if (inw(ioaddr+EL3_STATUS) == 0xffff) /* No card. */
1121                 return;
1122                 
1123         spin_lock_irqsave(&lp->window_lock, flags);
1124
1125         /* Unlike the 3c509 we need not turn off stats updates while reading. */
1126         /* Switch to the stats window, and read everything. */
1127         EL3WINDOW(6);
1128         lp->stats.tx_carrier_errors             += inb(ioaddr + 0);
1129         lp->stats.tx_heartbeat_errors           += inb(ioaddr + 1);
1130         /* Multiple collisions. */              inb(ioaddr + 2);
1131         lp->stats.collisions                    += inb(ioaddr + 3);
1132         lp->stats.tx_window_errors              += inb(ioaddr + 4);
1133         lp->stats.rx_fifo_errors                += inb(ioaddr + 5);
1134         lp->stats.tx_packets                    += inb(ioaddr + 6);
1135         up                                       = inb(ioaddr + 9);
1136         lp->stats.tx_packets                    += (up&0x30) << 4;
1137         /* Rx packets   */                         inb(ioaddr + 7);
1138         /* Tx deferrals */                         inb(ioaddr + 8);
1139         rx                                       = inw(ioaddr + 10);
1140         tx                                       = inw(ioaddr + 12);
1141
1142         EL3WINDOW(4);
1143         /* BadSSD */                               inb(ioaddr + 12);
1144         up                                       = inb(ioaddr + 13);
1145
1146         lp->stats.tx_bytes                      += tx + ((up & 0xf0) << 12);
1147
1148         EL3WINDOW(1);
1149         spin_unlock_irqrestore(&lp->window_lock, flags);
1150 }
1151
1152 static int el3_rx(struct net_device *dev, int worklimit)
1153 {
1154         struct el3_private *lp = (struct el3_private *)dev->priv;
1155         ioaddr_t ioaddr = dev->base_addr;
1156         short rx_status;
1157         
1158         DEBUG(3, "%s: in rx_packet(), status %4.4x, rx_status %4.4x.\n",
1159                   dev->name, inw(ioaddr+EL3_STATUS), inw(ioaddr+RxStatus));
1160         while (!((rx_status = inw(ioaddr + RxStatus)) & 0x8000) &&
1161                    (--worklimit >= 0)) {
1162                 if (rx_status & 0x4000) { /* Error, update stats. */
1163                         short error = rx_status & 0x3800;
1164                         lp->stats.rx_errors++;
1165                         switch (error) {
1166                         case 0x0000:    lp->stats.rx_over_errors++; break;
1167                         case 0x0800:    lp->stats.rx_length_errors++; break;
1168                         case 0x1000:    lp->stats.rx_frame_errors++; break;
1169                         case 0x1800:    lp->stats.rx_length_errors++; break;
1170                         case 0x2000:    lp->stats.rx_frame_errors++; break;
1171                         case 0x2800:    lp->stats.rx_crc_errors++; break;
1172                         }
1173                 } else {
1174                         short pkt_len = rx_status & 0x7ff;
1175                         struct sk_buff *skb;
1176
1177                         skb = dev_alloc_skb(pkt_len+5);
1178
1179                         DEBUG(3, "  Receiving packet size %d status %4.4x.\n",
1180                                   pkt_len, rx_status);
1181                         if (skb != NULL) {
1182                                 skb->dev = dev;
1183                                 skb_reserve(skb, 2);
1184                                 insl(ioaddr+RX_FIFO, skb_put(skb, pkt_len),
1185                                                 ((pkt_len+3)>>2));
1186                                 skb->protocol = eth_type_trans(skb, dev);
1187                                 netif_rx(skb);
1188                                 dev->last_rx = jiffies;
1189                                 lp->stats.rx_packets++;
1190                                 lp->stats.rx_bytes += pkt_len;
1191                         } else {
1192                                 DEBUG(1, "%s: couldn't allocate a sk_buff of"
1193                                           " size %d.\n", dev->name, pkt_len);
1194                                 lp->stats.rx_dropped++;
1195                         }
1196                 }
1197                 tc574_wait_for_completion(dev, RxDiscard);
1198         }
1199
1200         return worklimit;
1201 }
1202
1203 static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
1204 {
1205         u32 ethcmd;
1206                 
1207         if (copy_from_user(&ethcmd, useraddr, sizeof(ethcmd)))
1208                 return -EFAULT;
1209         
1210         switch (ethcmd) {
1211         case ETHTOOL_GDRVINFO: {
1212                 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
1213                 strncpy(info.driver, "3c574_cs", sizeof(info.driver)-1);
1214                 if (copy_to_user(useraddr, &info, sizeof(info)))
1215                         return -EFAULT;
1216                 return 0;
1217         }
1218         }
1219         
1220         return -EOPNOTSUPP;
1221 }
1222
1223 /* Provide ioctl() calls to examine the MII xcvr state. */
1224 static int el3_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1225 {
1226         struct el3_private *lp = (struct el3_private *)dev->priv;
1227         ioaddr_t ioaddr = dev->base_addr;
1228         u16 *data = (u16 *)&rq->ifr_data;
1229         int phy = lp->phys & 0x1f;
1230
1231         DEBUG(2, "%s: In ioct(%-.6s, %#4.4x) %4.4x %4.4x %4.4x %4.4x.\n",
1232                   dev->name, rq->ifr_ifrn.ifrn_name, cmd,
1233                   data[0], data[1], data[2], data[3]);
1234
1235         switch(cmd) {
1236         case SIOCETHTOOL:
1237                 return netdev_ethtool_ioctl(dev, (void *)rq->ifr_data);
1238         case SIOCDEVPRIVATE:            /* Get the address of the PHY in use. */
1239                 data[0] = phy;
1240         case SIOCDEVPRIVATE+1:          /* Read the specified MII register. */
1241                 {
1242                         int saved_window;
1243                         unsigned long flags;
1244
1245                         spin_lock_irqsave(&lp->window_lock, flags);
1246                         saved_window = inw(ioaddr + EL3_CMD) >> 13;
1247                         EL3WINDOW(4);
1248                         data[3] = mdio_read(ioaddr, data[0] & 0x1f, data[1] & 0x1f);
1249                         EL3WINDOW(saved_window);
1250                         spin_unlock_irqrestore(&lp->window_lock, flags);
1251                         return 0;
1252                 }
1253         case SIOCDEVPRIVATE+2:          /* Write the specified MII register */
1254                 {
1255                         int saved_window;
1256                        unsigned long flags;
1257
1258                         if (!capable(CAP_NET_ADMIN))
1259                                 return -EPERM;
1260                         spin_lock_irqsave(&lp->window_lock, flags);
1261                         saved_window = inw(ioaddr + EL3_CMD) >> 13;
1262                         EL3WINDOW(4);
1263                         mdio_write(ioaddr, data[0] & 0x1f, data[1] & 0x1f, data[2]);
1264                         EL3WINDOW(saved_window);
1265                         spin_unlock_irqrestore(&lp->window_lock, flags);
1266                         return 0;
1267                 }
1268         default:
1269                 return -EOPNOTSUPP;
1270         }
1271 }
1272
1273 /* The Odie chip has a 64 bin multicast filter, but the bit layout is not
1274    documented.  Until it is we revert to receiving all multicast frames when
1275    any multicast reception is desired.
1276    Note: My other drivers emit a log message whenever promiscuous mode is
1277    entered to help detect password sniffers.  This is less desirable on
1278    typical PC card machines, so we omit the message.
1279    */
1280
1281 static void set_rx_mode(struct net_device *dev)
1282 {
1283         ioaddr_t ioaddr = dev->base_addr;
1284
1285         if (dev->flags & IFF_PROMISC)
1286                 outw(SetRxFilter | RxStation | RxMulticast | RxBroadcast | RxProm,
1287                          ioaddr + EL3_CMD);
1288         else if (dev->mc_count || (dev->flags & IFF_ALLMULTI))
1289                 outw(SetRxFilter|RxStation|RxMulticast|RxBroadcast, ioaddr + EL3_CMD);
1290         else
1291                 outw(SetRxFilter | RxStation | RxBroadcast, ioaddr + EL3_CMD);
1292 }
1293
1294 static int el3_close(struct net_device *dev)
1295 {
1296         ioaddr_t ioaddr = dev->base_addr;
1297         struct el3_private *lp = dev->priv;
1298         dev_link_t *link = &lp->link;
1299
1300         DEBUG(2, "%s: shutting down ethercard.\n", dev->name);
1301         
1302         if (DEV_OK(link)) {
1303                 /* Turn off statistics ASAP.  We update lp->stats below. */
1304                 outw(StatsDisable, ioaddr + EL3_CMD);
1305                 
1306                 /* Disable the receiver and transmitter. */
1307                 outw(RxDisable, ioaddr + EL3_CMD);
1308                 outw(TxDisable, ioaddr + EL3_CMD);
1309                 
1310                 /* Note: Switching to window 0 may disable the IRQ. */
1311                 EL3WINDOW(0);
1312                 
1313                 update_stats(dev);
1314         }
1315
1316         link->open--;
1317         netif_stop_queue(dev);
1318         del_timer_sync(&lp->media);
1319         if (link->state & DEV_STALE_CONFIG)
1320                 tc574_release(link);
1321         return 0;
1322 }
1323
1324 static struct pcmcia_driver tc574_driver = {
1325         .owner          = THIS_MODULE,
1326         .drv            = {
1327                 .name   = "3c574_cs",
1328         },
1329         .attach         = tc574_attach,
1330         .detach         = tc574_detach,
1331 };
1332
1333 static int __init init_tc574(void)
1334 {
1335         return pcmcia_register_driver(&tc574_driver);
1336 }
1337
1338 static void __exit exit_tc574(void)
1339 {
1340         pcmcia_unregister_driver(&tc574_driver);
1341         while (dev_list != NULL)
1342                 tc574_detach(dev_list);
1343 }
1344
1345 module_init(init_tc574);
1346 module_exit(exit_tc574);