VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / net / pcmcia / pcnet_cs.c
1 /*======================================================================
2
3     A PCMCIA ethernet driver for NS8390-based cards
4
5     This driver supports the D-Link DE-650 and Linksys EthernetCard
6     cards, the newer D-Link and Linksys combo cards, Accton EN2212
7     cards, the RPTI EP400, and the PreMax PE-200 in non-shared-memory
8     mode, and the IBM Credit Card Adapter, the NE4100, the Thomas
9     Conrad ethernet card, and the Kingston KNE-PCM/x in shared-memory
10     mode.  It will also handle the Socket EA card in either mode.
11
12     Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
13
14     pcnet_cs.c 1.153 2003/11/09 18:53:09
15     
16     The network driver code is based on Donald Becker's NE2000 code:
17
18     Written 1992,1993 by Donald Becker.
19     Copyright 1993 United States Government as represented by the
20     Director, National Security Agency.  This software may be used and
21     distributed according to the terms of the GNU General Public License,
22     incorporated herein by reference.
23     Donald Becker may be reached at becker@scyld.com
24
25     Based also on Keith Moore's changes to Don Becker's code, for IBM
26     CCAE support.  Drivers merged back together, and shared-memory
27     Socket EA support added, by Ken Raeburn, September 1995.
28
29 ======================================================================*/
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/ptrace.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/timer.h>
38 #include <linux/delay.h>
39 #include <linux/ethtool.h>
40 #include <linux/netdevice.h>
41 #include <../drivers/net/8390.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/ciscode.h>
48 #include <pcmcia/ds.h>
49 #include <pcmcia/cisreg.h>
50
51 #include <asm/io.h>
52 #include <asm/system.h>
53 #include <asm/byteorder.h>
54 #include <asm/uaccess.h>
55
56 #define PCNET_CMD       0x00
57 #define PCNET_DATAPORT  0x10    /* NatSemi-defined port window offset. */
58 #define PCNET_RESET     0x1f    /* Issue a read to reset, a write to clear. */
59 #define PCNET_MISC      0x18    /* For IBM CCAE and Socket EA cards */
60
61 #define PCNET_START_PG  0x40    /* First page of TX buffer */
62 #define PCNET_STOP_PG   0x80    /* Last page +1 of RX ring */
63
64 /* Socket EA cards have a larger packet buffer */
65 #define SOCKET_START_PG 0x01
66 #define SOCKET_STOP_PG  0xff
67
68 #define PCNET_RDC_TIMEOUT (2*HZ/100)    /* Max wait in jiffies for Tx RDC */
69
70 static char *if_names[] = { "auto", "10baseT", "10base2"};
71
72 #ifdef PCMCIA_DEBUG
73 static int pc_debug = PCMCIA_DEBUG;
74 MODULE_PARM(pc_debug, "i");
75 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
76 static char *version =
77 "pcnet_cs.c 1.153 2003/11/09 18:53:09 (David Hinds)";
78 #else
79 #define DEBUG(n, args...)
80 #endif
81
82 /*====================================================================*/
83
84 /* Module parameters */
85
86 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
87 MODULE_DESCRIPTION("NE2000 compatible PCMCIA ethernet driver");
88 MODULE_LICENSE("GPL");
89
90 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
91
92 /* Bit map of interrupts to choose from */
93 INT_MODULE_PARM(irq_mask,       0xdeb8);
94 static int irq_list[4] = { -1 };
95 MODULE_PARM(irq_list, "1-4i");
96
97 INT_MODULE_PARM(if_port,        1);     /* Transceiver type */
98 INT_MODULE_PARM(use_big_buf,    1);     /* use 64K packet buffer? */
99 INT_MODULE_PARM(mem_speed,      0);     /* shared mem speed, in ns */
100 INT_MODULE_PARM(delay_output,   0);     /* pause after xmit? */
101 INT_MODULE_PARM(delay_time,     4);     /* in usec */
102 INT_MODULE_PARM(use_shmem,      -1);    /* use shared memory? */
103 INT_MODULE_PARM(full_duplex,    0);     /* full duplex? */
104
105 /* Ugh!  Let the user hardwire the hardware address for queer cards */
106 static int hw_addr[6] = { 0, /* ... */ };
107 MODULE_PARM(hw_addr, "6i");
108
109 /*====================================================================*/
110
111 static void mii_phy_probe(struct net_device *dev);
112 static void pcnet_config(dev_link_t *link);
113 static void pcnet_release(dev_link_t *link);
114 static int pcnet_event(event_t event, int priority,
115                        event_callback_args_t *args);
116 static int pcnet_open(struct net_device *dev);
117 static int pcnet_close(struct net_device *dev);
118 static int ei_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
119 static struct ethtool_ops netdev_ethtool_ops;
120 static irqreturn_t ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs);
121 static void ei_watchdog(u_long arg);
122 static void pcnet_reset_8390(struct net_device *dev);
123 static int set_config(struct net_device *dev, struct ifmap *map);
124 static int setup_shmem_window(dev_link_t *link, int start_pg,
125                               int stop_pg, int cm_offset);
126 static int setup_dma_config(dev_link_t *link, int start_pg,
127                             int stop_pg);
128
129 static dev_link_t *pcnet_attach(void);
130 static void pcnet_detach(dev_link_t *);
131
132 static dev_info_t dev_info = "pcnet_cs";
133 static dev_link_t *dev_list;
134
135 /*====================================================================*/
136
137 typedef struct hw_info_t {
138     u_int       offset;
139     u_char      a0, a1, a2;
140     u_int       flags;
141 } hw_info_t;
142
143 #define DELAY_OUTPUT    0x01
144 #define HAS_MISC_REG    0x02
145 #define USE_BIG_BUF     0x04
146 #define HAS_IBM_MISC    0x08
147 #define IS_DL10019      0x10
148 #define IS_DL10022      0x20
149 #define HAS_MII         0x40
150 #define USE_SHMEM       0x80    /* autodetected */
151
152 #define AM79C9XX_HOME_PHY       0x00006B90  /* HomePNA PHY */
153 #define AM79C9XX_ETH_PHY        0x00006B70  /* 10baseT PHY */
154 #define MII_PHYID_REV_MASK      0xfffffff0
155 #define MII_PHYID_REG1          0x02
156 #define MII_PHYID_REG2          0x03
157
158 static hw_info_t hw_info[] = {
159     { /* Accton EN2212 */ 0x0ff0, 0x00, 0x00, 0xe8, DELAY_OUTPUT }, 
160     { /* Allied Telesis LA-PCM */ 0x0ff0, 0x00, 0x00, 0xf4, 0 },
161     { /* APEX MultiCard */ 0x03f4, 0x00, 0x20, 0xe5, 0 },
162     { /* ASANTE FriendlyNet */ 0x4910, 0x00, 0x00, 0x94,
163       DELAY_OUTPUT | HAS_IBM_MISC },
164     { /* Danpex EN-6200P2 */ 0x0110, 0x00, 0x40, 0xc7, 0 },
165     { /* DataTrek NetCard */ 0x0ff0, 0x00, 0x20, 0xe8, 0 },
166     { /* Dayna CommuniCard E */ 0x0110, 0x00, 0x80, 0x19, 0 },
167     { /* D-Link DE-650 */ 0x0040, 0x00, 0x80, 0xc8, 0 },
168     { /* EP-210 Ethernet */ 0x0110, 0x00, 0x40, 0x33, 0 },
169     { /* EP4000 Ethernet */ 0x01c0, 0x00, 0x00, 0xb4, 0 },
170     { /* Epson EEN10B */ 0x0ff0, 0x00, 0x00, 0x48,
171       HAS_MISC_REG | HAS_IBM_MISC },
172     { /* ELECOM Laneed LD-CDWA */ 0xb8, 0x08, 0x00, 0x42, 0 },
173     { /* Hypertec Ethernet */ 0x01c0, 0x00, 0x40, 0x4c, 0 },
174     { /* IBM CCAE */ 0x0ff0, 0x08, 0x00, 0x5a,
175       HAS_MISC_REG | HAS_IBM_MISC },
176     { /* IBM CCAE */ 0x0ff0, 0x00, 0x04, 0xac,
177       HAS_MISC_REG | HAS_IBM_MISC },
178     { /* IBM CCAE */ 0x0ff0, 0x00, 0x06, 0x29,
179       HAS_MISC_REG | HAS_IBM_MISC },
180     { /* IBM FME */ 0x0374, 0x08, 0x00, 0x5a,
181       HAS_MISC_REG | HAS_IBM_MISC },
182     { /* IBM FME */ 0x0374, 0x00, 0x04, 0xac,
183       HAS_MISC_REG | HAS_IBM_MISC },
184     { /* Kansai KLA-PCM/T */ 0x0ff0, 0x00, 0x60, 0x87,
185       HAS_MISC_REG | HAS_IBM_MISC },
186     { /* NSC DP83903 */ 0x0374, 0x08, 0x00, 0x17,
187       HAS_MISC_REG | HAS_IBM_MISC },
188     { /* NSC DP83903 */ 0x0374, 0x00, 0xc0, 0xa8,
189       HAS_MISC_REG | HAS_IBM_MISC },
190     { /* NSC DP83903 */ 0x0374, 0x00, 0xa0, 0xb0,
191       HAS_MISC_REG | HAS_IBM_MISC },
192     { /* NSC DP83903 */ 0x0198, 0x00, 0x20, 0xe0,
193       HAS_MISC_REG | HAS_IBM_MISC },
194     { /* I-O DATA PCLA/T */ 0x0ff0, 0x00, 0xa0, 0xb0, 0 },
195     { /* Katron PE-520 */ 0x0110, 0x00, 0x40, 0xf6, 0 },
196     { /* Kingston KNE-PCM/x */ 0x0ff0, 0x00, 0xc0, 0xf0,
197       HAS_MISC_REG | HAS_IBM_MISC },
198     { /* Kingston KNE-PCM/x */ 0x0ff0, 0xe2, 0x0c, 0x0f,
199       HAS_MISC_REG | HAS_IBM_MISC },
200     { /* Kingston KNE-PC2 */ 0x0180, 0x00, 0xc0, 0xf0, 0 },
201     { /* Maxtech PCN2000 */ 0x5000, 0x00, 0x00, 0xe8, 0 },
202     { /* NDC Instant-Link */ 0x003a, 0x00, 0x80, 0xc6, 0 },
203     { /* NE2000 Compatible */ 0x0ff0, 0x00, 0xa0, 0x0c, 0 },
204     { /* Network General Sniffer */ 0x0ff0, 0x00, 0x00, 0x65,
205       HAS_MISC_REG | HAS_IBM_MISC },
206     { /* Panasonic VEL211 */ 0x0ff0, 0x00, 0x80, 0x45, 
207       HAS_MISC_REG | HAS_IBM_MISC },
208     { /* PreMax PE-200 */ 0x07f0, 0x00, 0x20, 0xe0, 0 },
209     { /* RPTI EP400 */ 0x0110, 0x00, 0x40, 0x95, 0 },
210     { /* SCM Ethernet */ 0x0ff0, 0x00, 0x20, 0xcb, 0 },
211     { /* Socket EA */ 0x4000, 0x00, 0xc0, 0x1b,
212       DELAY_OUTPUT | HAS_MISC_REG | USE_BIG_BUF },
213     { /* Socket LP-E CF+ */ 0x01c0, 0x00, 0xc0, 0x1b, 0 },
214     { /* SuperSocket RE450T */ 0x0110, 0x00, 0xe0, 0x98, 0 },
215     { /* Volktek NPL-402CT */ 0x0060, 0x00, 0x40, 0x05, 0 },
216     { /* NEC PC-9801N-J12 */ 0x0ff0, 0x00, 0x00, 0x4c, 0 },
217     { /* PCMCIA Technology OEM */ 0x01c8, 0x00, 0xa0, 0x0c, 0 }
218 };
219
220 #define NR_INFO         (sizeof(hw_info)/sizeof(hw_info_t))
221
222 static hw_info_t default_info = { 0, 0, 0, 0, 0 };
223 static hw_info_t dl10019_info = { 0, 0, 0, 0, IS_DL10019|HAS_MII };
224 static hw_info_t dl10022_info = { 0, 0, 0, 0, IS_DL10022|HAS_MII };
225
226 typedef struct pcnet_dev_t {
227     dev_link_t          link;
228     dev_node_t          node;
229     u_int               flags;
230     caddr_t             base;
231     struct timer_list   watchdog;
232     int                 stale, fast_poll;
233     u_char              phy_id;
234     u_char              eth_phy, pna_phy;
235     u_short             link_status;
236     u_long              mii_reset;
237 } pcnet_dev_t;
238
239 static inline pcnet_dev_t *PRIV(struct net_device *dev)
240 {
241         char *p = netdev_priv(dev);
242         return (pcnet_dev_t *)(p + sizeof(struct ei_device));
243 }
244
245 /*======================================================================
246
247     pcnet_attach() creates an "instance" of the driver, allocating
248     local data structures for one device.  The device is registered
249     with Card Services.
250
251 ======================================================================*/
252
253 static dev_link_t *pcnet_attach(void)
254 {
255     pcnet_dev_t *info;
256     dev_link_t *link;
257     struct net_device *dev;
258     client_reg_t client_reg;
259     int i, ret;
260
261     DEBUG(0, "pcnet_attach()\n");
262
263     /* Create new ethernet device */
264     dev = __alloc_ei_netdev(sizeof(pcnet_dev_t));
265     if (!dev) return NULL;
266     info = PRIV(dev);
267     link = &info->link;
268     link->priv = dev;
269
270     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
271     link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
272     if (irq_list[0] == -1)
273         link->irq.IRQInfo2 = irq_mask;
274     else
275         for (i = 0; i < 4; i++)
276             link->irq.IRQInfo2 |= 1 << irq_list[i];
277     link->conf.Attributes = CONF_ENABLE_IRQ;
278     link->conf.IntType = INT_MEMORY_AND_IO;
279
280     SET_MODULE_OWNER(dev);
281     dev->open = &pcnet_open;
282     dev->stop = &pcnet_close;
283     dev->set_config = &set_config;
284
285     /* Register with Card Services */
286     link->next = dev_list;
287     dev_list = link;
288     client_reg.dev_info = &dev_info;
289     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
290     client_reg.EventMask =
291         CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
292         CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
293         CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
294     client_reg.event_handler = &pcnet_event;
295     client_reg.Version = 0x0210;
296     client_reg.event_callback_args.client_data = link;
297     ret = pcmcia_register_client(&link->handle, &client_reg);
298     if (ret != CS_SUCCESS) {
299         cs_error(link->handle, RegisterClient, ret);
300         pcnet_detach(link);
301         return NULL;
302     }
303
304     return link;
305 } /* pcnet_attach */
306
307 /*======================================================================
308
309     This deletes a driver "instance".  The device is de-registered
310     with Card Services.  If it has been released, all local data
311     structures are freed.  Otherwise, the structures will be freed
312     when the device is released.
313
314 ======================================================================*/
315
316 static void pcnet_detach(dev_link_t *link)
317 {
318     struct net_device *dev = link->priv;
319     dev_link_t **linkp;
320
321     DEBUG(0, "pcnet_detach(0x%p)\n", link);
322
323     /* Locate device structure */
324     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
325         if (*linkp == link) break;
326     if (*linkp == NULL)
327         return;
328
329     if (link->dev)
330         unregister_netdev(dev);
331
332     if (link->state & DEV_CONFIG)
333         pcnet_release(link);
334
335     if (link->handle)
336         pcmcia_deregister_client(link->handle);
337
338     /* Unlink device structure, free bits */
339     *linkp = link->next;
340     free_netdev(dev);
341 } /* pcnet_detach */
342
343 /*======================================================================
344
345     This probes for a card's hardware address, for card types that
346     encode this information in their CIS.
347
348 ======================================================================*/
349
350 static hw_info_t *get_hwinfo(dev_link_t *link)
351 {
352     struct net_device *dev = link->priv;
353     win_req_t req;
354     memreq_t mem;
355     u_char *base, *virt;
356     int i, j;
357
358     /* Allocate a small memory window */
359     req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
360     req.Base = 0; req.Size = 0;
361     req.AccessSpeed = 0;
362     i = pcmcia_request_window(&link->handle, &req, &link->win);
363     if (i != CS_SUCCESS) {
364         cs_error(link->handle, RequestWindow, i);
365         return NULL;
366     }
367
368     virt = ioremap(req.Base, req.Size);
369     mem.Page = 0;
370     for (i = 0; i < NR_INFO; i++) {
371         mem.CardOffset = hw_info[i].offset & ~(req.Size-1);
372         pcmcia_map_mem_page(link->win, &mem);
373         base = &virt[hw_info[i].offset & (req.Size-1)];
374         if ((readb(base+0) == hw_info[i].a0) &&
375             (readb(base+2) == hw_info[i].a1) &&
376             (readb(base+4) == hw_info[i].a2))
377             break;
378     }
379     if (i < NR_INFO) {
380         for (j = 0; j < 6; j++)
381             dev->dev_addr[j] = readb(base + (j<<1));
382     }
383     
384     iounmap(virt);
385     j = pcmcia_release_window(link->win);
386     if (j != CS_SUCCESS)
387         cs_error(link->handle, ReleaseWindow, j);
388     return (i < NR_INFO) ? hw_info+i : NULL;
389 } /* get_hwinfo */
390
391 /*======================================================================
392
393     This probes for a card's hardware address by reading the PROM.
394     It checks the address against a list of known types, then falls
395     back to a simple NE2000 clone signature check.
396
397 ======================================================================*/
398
399 static hw_info_t *get_prom(dev_link_t *link)
400 {
401     struct net_device *dev = link->priv;
402     ioaddr_t ioaddr = dev->base_addr;
403     u_char prom[32];
404     int i, j;
405
406     /* This is lifted straight from drivers/net/ne.c */
407     struct {
408         u_char value, offset;
409     } program_seq[] = {
410         {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
411         {0x48,  EN0_DCFG},      /* Set byte-wide (0x48) access. */
412         {0x00,  EN0_RCNTLO},    /* Clear the count regs. */
413         {0x00,  EN0_RCNTHI},
414         {0x00,  EN0_IMR},       /* Mask completion irq. */
415         {0xFF,  EN0_ISR},
416         {E8390_RXOFF, EN0_RXCR},        /* 0x20  Set to monitor */
417         {E8390_TXOFF, EN0_TXCR},        /* 0x02  and loopback mode. */
418         {32,    EN0_RCNTLO},
419         {0x00,  EN0_RCNTHI},
420         {0x00,  EN0_RSARLO},    /* DMA starting at 0x0000. */
421         {0x00,  EN0_RSARHI},
422         {E8390_RREAD+E8390_START, E8390_CMD},
423     };
424
425     pcnet_reset_8390(dev);
426     mdelay(10);
427
428     for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
429         outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
430
431     for (i = 0; i < 32; i++)
432         prom[i] = inb(ioaddr + PCNET_DATAPORT);
433     for (i = 0; i < NR_INFO; i++) {
434         if ((prom[0] == hw_info[i].a0) &&
435             (prom[2] == hw_info[i].a1) &&
436             (prom[4] == hw_info[i].a2))
437             break;
438     }
439     if ((i < NR_INFO) || ((prom[28] == 0x57) && (prom[30] == 0x57))) {
440         for (j = 0; j < 6; j++)
441             dev->dev_addr[j] = prom[j<<1];
442         return (i < NR_INFO) ? hw_info+i : &default_info;
443     }
444     return NULL;
445 } /* get_prom */
446
447 /*======================================================================
448
449     For DL10019 based cards, like the Linksys EtherFast
450
451 ======================================================================*/
452
453 static hw_info_t *get_dl10019(dev_link_t *link)
454 {
455     struct net_device *dev = link->priv;
456     int i;
457     u_char sum;
458
459     for (sum = 0, i = 0x14; i < 0x1c; i++)
460         sum += inb_p(dev->base_addr + i);
461     if (sum != 0xff)
462         return NULL;
463     for (i = 0; i < 6; i++)
464         dev->dev_addr[i] = inb_p(dev->base_addr + 0x14 + i);
465     i = inb(dev->base_addr + 0x1f);
466     return ((i == 0x91)||(i == 0x99)) ? &dl10022_info : &dl10019_info;
467 }
468
469 /*======================================================================
470
471     For Asix AX88190 based cards
472
473 ======================================================================*/
474
475 static hw_info_t *get_ax88190(dev_link_t *link)
476 {
477     struct net_device *dev = link->priv;
478     ioaddr_t ioaddr = dev->base_addr;
479     int i, j;
480
481     /* Not much of a test, but the alternatives are messy */
482     if (link->conf.ConfigBase != 0x03c0)
483         return NULL;
484
485     outb_p(0x01, ioaddr + EN0_DCFG);    /* Set word-wide access. */
486     outb_p(0x00, ioaddr + EN0_RSARLO);  /* DMA starting at 0x0400. */
487     outb_p(0x04, ioaddr + EN0_RSARHI);
488     outb_p(E8390_RREAD+E8390_START, ioaddr + E8390_CMD);
489
490     for (i = 0; i < 6; i += 2) {
491         j = inw(ioaddr + PCNET_DATAPORT);
492         dev->dev_addr[i] = j & 0xff;
493         dev->dev_addr[i+1] = j >> 8;
494     }
495     printk(KERN_NOTICE "pcnet_cs: this is an AX88190 card!\n");
496     printk(KERN_NOTICE "pcnet_cs: use axnet_cs instead.\n");
497     return NULL;
498 }
499
500 /*======================================================================
501
502     This should be totally unnecessary... but when we can't figure
503     out the hardware address any other way, we'll let the user hard
504     wire it when the module is initialized.
505
506 ======================================================================*/
507
508 static hw_info_t *get_hwired(dev_link_t *link)
509 {
510     struct net_device *dev = link->priv;
511     int i;
512
513     for (i = 0; i < 6; i++)
514         if (hw_addr[i] != 0) break;
515     if (i == 6)
516         return NULL;
517
518     for (i = 0; i < 6; i++)
519         dev->dev_addr[i] = hw_addr[i];
520
521     return &default_info;
522 } /* get_hwired */
523
524 /*======================================================================
525
526     pcnet_config() is scheduled to run after a CARD_INSERTION event
527     is received, to configure the PCMCIA socket, and to make the
528     ethernet device available to the system.
529
530 ======================================================================*/
531
532 #define CS_CHECK(fn, ret) \
533 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
534
535 static int try_io_port(dev_link_t *link)
536 {
537     int j, ret;
538     if (link->io.NumPorts1 == 32) {
539         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
540         if (link->io.NumPorts2 > 0) {
541             /* for master/slave multifunction cards */
542             link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
543             link->irq.Attributes = 
544                 IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
545         }
546     } else {
547         /* This should be two 16-port windows */
548         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
549         link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
550     }
551     if (link->io.BasePort1 == 0) {
552         link->io.IOAddrLines = 16;
553         for (j = 0; j < 0x400; j += 0x20) {
554             link->io.BasePort1 = j ^ 0x300;
555             link->io.BasePort2 = (j ^ 0x300) + 0x10;
556             ret = pcmcia_request_io(link->handle, &link->io);
557             if (ret == CS_SUCCESS) return ret;
558         }
559         return ret;
560     } else {
561         return pcmcia_request_io(link->handle, &link->io);
562     }
563 }
564
565 static void pcnet_config(dev_link_t *link)
566 {
567     client_handle_t handle = link->handle;
568     struct net_device *dev = link->priv;
569     pcnet_dev_t *info = PRIV(dev);
570     tuple_t tuple;
571     cisparse_t parse;
572     int i, last_ret, last_fn, start_pg, stop_pg, cm_offset;
573     int manfid = 0, prodid = 0, has_shmem = 0;
574     u_short buf[64];
575     config_info_t conf;
576     hw_info_t *hw_info;
577
578     DEBUG(0, "pcnet_config(0x%p)\n", link);
579
580     tuple.Attributes = 0;
581     tuple.TupleData = (cisdata_t *)buf;
582     tuple.TupleDataMax = sizeof(buf);
583     tuple.TupleOffset = 0;
584     tuple.DesiredTuple = CISTPL_CONFIG;
585     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
586     CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
587     CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
588     link->conf.ConfigBase = parse.config.base;
589     link->conf.Present = parse.config.rmask[0];
590
591     /* Configure card */
592     link->state |= DEV_CONFIG;
593
594     /* Look up current Vcc */
595     CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
596     link->conf.Vcc = conf.Vcc;
597
598     tuple.DesiredTuple = CISTPL_MANFID;
599     tuple.Attributes = TUPLE_RETURN_COMMON;
600     if ((pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) &&
601         (pcmcia_get_tuple_data(handle, &tuple) == CS_SUCCESS)) {
602         manfid = le16_to_cpu(buf[0]);
603         prodid = le16_to_cpu(buf[1]);
604     }
605     
606     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
607     tuple.Attributes = 0;
608     CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
609     while (last_ret == CS_SUCCESS) {
610         cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
611         cistpl_io_t *io = &(parse.cftable_entry.io);
612         
613         if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
614                         pcmcia_parse_tuple(handle, &tuple, &parse) != 0 ||
615                         cfg->index == 0 || cfg->io.nwin == 0)
616                 goto next_entry;
617         
618         link->conf.ConfigIndex = cfg->index;
619         /* For multifunction cards, by convention, we configure the
620            network function with window 0, and serial with window 1 */
621         if (io->nwin > 1) {
622             i = (io->win[1].len > io->win[0].len);
623             link->io.BasePort2 = io->win[1-i].base;
624             link->io.NumPorts2 = io->win[1-i].len;
625         } else {
626             i = link->io.NumPorts2 = 0;
627         }
628         has_shmem = ((cfg->mem.nwin == 1) &&
629                      (cfg->mem.win[0].len >= 0x4000));
630         link->io.BasePort1 = io->win[i].base;
631         link->io.NumPorts1 = io->win[i].len;
632         link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
633         if (link->io.NumPorts1 + link->io.NumPorts2 >= 32) {
634             last_ret = try_io_port(link);
635             if (last_ret == CS_SUCCESS) break;
636         }
637     next_entry:
638         last_ret = pcmcia_get_next_tuple(handle, &tuple);
639     }
640     if (last_ret != CS_SUCCESS) {
641         cs_error(handle, RequestIO, last_ret);
642         goto failed;
643     }
644
645     CS_CHECK(RequestIRQ, pcmcia_request_irq(handle, &link->irq));
646     
647     if (link->io.NumPorts2 == 8) {
648         link->conf.Attributes |= CONF_ENABLE_SPKR;
649         link->conf.Status = CCSR_AUDIO_ENA;
650     }
651     if ((manfid == MANFID_IBM) &&
652         (prodid == PRODID_IBM_HOME_AND_AWAY))
653         link->conf.ConfigIndex |= 0x10;
654     
655     CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
656     dev->irq = link->irq.AssignedIRQ;
657     dev->base_addr = link->io.BasePort1;
658     if (info->flags & HAS_MISC_REG) {
659         if ((if_port == 1) || (if_port == 2))
660             dev->if_port = if_port;
661         else
662             printk(KERN_NOTICE "pcnet_cs: invalid if_port requested\n");
663     } else {
664         dev->if_port = 0;
665     }
666
667     hw_info = get_hwinfo(link);
668     if (hw_info == NULL)
669         hw_info = get_prom(link);
670     if (hw_info == NULL)
671         hw_info = get_dl10019(link);
672     if (hw_info == NULL)
673         hw_info = get_ax88190(link);
674     if (hw_info == NULL)
675         hw_info = get_hwired(link);
676     
677     if (hw_info == NULL) {
678         printk(KERN_NOTICE "pcnet_cs: unable to read hardware net"
679                " address for io base %#3lx\n", dev->base_addr);
680         goto failed;
681     }
682
683     info->flags = hw_info->flags;
684     /* Check for user overrides */
685     info->flags |= (delay_output) ? DELAY_OUTPUT : 0;
686     if ((manfid == MANFID_SOCKET) &&
687         ((prodid == PRODID_SOCKET_LPE) ||
688          (prodid == PRODID_SOCKET_LPE_CF) ||
689          (prodid == PRODID_SOCKET_EIO)))
690         info->flags &= ~USE_BIG_BUF;
691     if (!use_big_buf)
692         info->flags &= ~USE_BIG_BUF;
693     
694     if (info->flags & USE_BIG_BUF) {
695         start_pg = SOCKET_START_PG;
696         stop_pg = SOCKET_STOP_PG;
697         cm_offset = 0x10000;
698     } else {
699         start_pg = PCNET_START_PG;
700         stop_pg = PCNET_STOP_PG;
701         cm_offset = 0;
702     }
703
704     /* has_shmem is ignored if use_shmem != -1 */
705     if ((use_shmem == 0) || (!has_shmem && (use_shmem == -1)) ||
706         (setup_shmem_window(link, start_pg, stop_pg, cm_offset) != 0))
707         setup_dma_config(link, start_pg, stop_pg);
708
709     ei_status.name = "NE2000";
710     ei_status.word16 = 1;
711     ei_status.reset_8390 = &pcnet_reset_8390;
712
713     SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
714
715     if (info->flags & (IS_DL10019|IS_DL10022)) {
716         u_char id = inb(dev->base_addr + 0x1a);
717         dev->do_ioctl = &ei_ioctl;
718         mii_phy_probe(dev);
719         if ((id == 0x30) && !info->pna_phy && (info->eth_phy == 4))
720             info->eth_phy = 0;
721     }
722
723     link->dev = &info->node;
724     link->state &= ~DEV_CONFIG_PENDING;
725
726 #ifdef CONFIG_NET_POLL_CONTROLLER
727     dev->poll_controller = ei_poll;
728 #endif
729
730     if (register_netdev(dev) != 0) {
731         printk(KERN_NOTICE "pcnet_cs: register_netdev() failed\n");
732         link->dev = NULL;
733         goto failed;
734     }
735
736     strcpy(info->node.dev_name, dev->name);
737
738     if (info->flags & (IS_DL10019|IS_DL10022)) {
739         u_char id = inb(dev->base_addr + 0x1a);
740         printk(KERN_INFO "%s: NE2000 (DL100%d rev %02x): ",
741                dev->name, ((info->flags & IS_DL10022) ? 22 : 19), id);
742         if (info->pna_phy)
743             printk("PNA, ");
744     } else {
745         printk(KERN_INFO "%s: NE2000 Compatible: ", dev->name);
746     }
747     printk("io %#3lx, irq %d,", dev->base_addr, dev->irq);
748     if (info->flags & USE_SHMEM)
749         printk (" mem %#5lx,", dev->mem_start);
750     if (info->flags & HAS_MISC_REG)
751         printk(" %s xcvr,", if_names[dev->if_port]);
752     printk(" hw_addr ");
753     for (i = 0; i < 6; i++)
754         printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
755     return;
756
757 cs_failed:
758     cs_error(link->handle, last_fn, last_ret);
759 failed:
760     pcnet_release(link);
761     link->state &= ~DEV_CONFIG_PENDING;
762     return;
763 } /* pcnet_config */
764
765 /*======================================================================
766
767     After a card is removed, pcnet_release() will unregister the net
768     device, and release the PCMCIA configuration.  If the device is
769     still open, this will be postponed until it is closed.
770
771 ======================================================================*/
772
773 static void pcnet_release(dev_link_t *link)
774 {
775     pcnet_dev_t *info = PRIV(link->priv);
776
777     DEBUG(0, "pcnet_release(0x%p)\n", link);
778
779     if (info->flags & USE_SHMEM) {
780         iounmap(info->base);
781         pcmcia_release_window(link->win);
782     }
783     pcmcia_release_configuration(link->handle);
784     pcmcia_release_io(link->handle, &link->io);
785     pcmcia_release_irq(link->handle, &link->irq);
786
787     link->state &= ~DEV_CONFIG;
788 }
789
790 /*======================================================================
791
792     The card status event handler.  Mostly, this schedules other
793     stuff to run after an event is received.  A CARD_REMOVAL event
794     also sets some flags to discourage the net drivers from trying
795     to talk to the card any more.
796
797 ======================================================================*/
798
799 static int pcnet_event(event_t event, int priority,
800                        event_callback_args_t *args)
801 {
802     dev_link_t *link = args->client_data;
803     struct net_device *dev = link->priv;
804
805     DEBUG(2, "pcnet_event(0x%06x)\n", event);
806
807     switch (event) {
808     case CS_EVENT_CARD_REMOVAL:
809         link->state &= ~DEV_PRESENT;
810         if (link->state & DEV_CONFIG)
811             netif_device_detach(dev);
812         break;
813     case CS_EVENT_CARD_INSERTION:
814         link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
815         pcnet_config(link);
816         break;
817     case CS_EVENT_PM_SUSPEND:
818         link->state |= DEV_SUSPEND;
819         /* Fall through... */
820     case CS_EVENT_RESET_PHYSICAL:
821         if (link->state & DEV_CONFIG) {
822             if (link->open)
823                 netif_device_detach(dev);
824             pcmcia_release_configuration(link->handle);
825         }
826         break;
827     case CS_EVENT_PM_RESUME:
828         link->state &= ~DEV_SUSPEND;
829         /* Fall through... */
830     case CS_EVENT_CARD_RESET:
831         if (link->state & DEV_CONFIG) {
832             pcmcia_request_configuration(link->handle, &link->conf);
833             if (link->open) {
834                 pcnet_reset_8390(dev);
835                 NS8390_init(dev, 1);
836                 netif_device_attach(dev);
837             }
838         }
839         break;
840     }
841     return 0;
842 } /* pcnet_event */
843
844 /*======================================================================
845
846     MII interface support for DL10019 and DL10022 based cards
847
848     On the DL10019, the MII IO direction bit is 0x10; on the DL10022
849     it is 0x20.  Setting both bits seems to work on both card types.
850
851 ======================================================================*/
852
853 #define DLINK_GPIO              0x1c
854 #define DLINK_DIAG              0x1d
855 #define DLINK_EEPROM            0x1e
856
857 #define MDIO_SHIFT_CLK          0x80
858 #define MDIO_DATA_OUT           0x40
859 #define MDIO_DIR_WRITE          0x30
860 #define MDIO_DATA_WRITE0        (MDIO_DIR_WRITE)
861 #define MDIO_DATA_WRITE1        (MDIO_DIR_WRITE | MDIO_DATA_OUT)
862 #define MDIO_DATA_READ          0x10
863 #define MDIO_MASK               0x0f
864
865 static void mdio_sync(ioaddr_t addr)
866 {
867     int bits, mask = inb(addr) & MDIO_MASK;
868     for (bits = 0; bits < 32; bits++) {
869         outb(mask | MDIO_DATA_WRITE1, addr);
870         outb(mask | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
871     }
872 }
873
874 static int mdio_read(ioaddr_t addr, int phy_id, int loc)
875 {
876     u_int cmd = (0x06<<10)|(phy_id<<5)|loc;
877     int i, retval = 0, mask = inb(addr) & MDIO_MASK;
878
879     mdio_sync(addr);
880     for (i = 13; i >= 0; i--) {
881         int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
882         outb(mask | dat, addr);
883         outb(mask | dat | MDIO_SHIFT_CLK, addr);
884     }
885     for (i = 19; i > 0; i--) {
886         outb(mask, addr);
887         retval = (retval << 1) | ((inb(addr) & MDIO_DATA_READ) != 0);
888         outb(mask | MDIO_SHIFT_CLK, addr);
889     }
890     return (retval>>1) & 0xffff;
891 }
892
893 static void mdio_write(ioaddr_t addr, int phy_id, int loc, int value)
894 {
895     u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
896     int i, mask = inb(addr) & MDIO_MASK;
897
898     mdio_sync(addr);
899     for (i = 31; i >= 0; i--) {
900         int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
901         outb(mask | dat, addr);
902         outb(mask | dat | MDIO_SHIFT_CLK, addr);
903     }
904     for (i = 1; i >= 0; i--) {
905         outb(mask, addr);
906         outb(mask | MDIO_SHIFT_CLK, addr);
907     }
908 }
909
910 static void mdio_reset(ioaddr_t addr, int phy_id)
911 {
912     outb_p(0x08, addr);
913     outb_p(0x0c, addr);
914     outb_p(0x08, addr);
915     outb_p(0x0c, addr);
916     outb_p(0x00, addr);
917 }
918
919 /*======================================================================
920
921     EEPROM access routines for DL10019 and DL10022 based cards
922
923 ======================================================================*/
924
925 #define EE_EEP          0x40
926 #define EE_ASIC         0x10
927 #define EE_CS           0x08
928 #define EE_CK           0x04
929 #define EE_DO           0x02
930 #define EE_DI           0x01
931 #define EE_ADOT         0x01    /* DataOut for ASIC */
932 #define EE_READ_CMD     0x06
933
934 #define DL19FDUPLX      0x0400  /* DL10019 Full duplex mode */
935
936 static int read_eeprom(ioaddr_t ioaddr, int location)
937 {
938     int i, retval = 0;
939     ioaddr_t ee_addr = ioaddr + DLINK_EEPROM;
940     int read_cmd = location | (EE_READ_CMD << 8);
941
942     outb(0, ee_addr);
943     outb(EE_EEP|EE_CS, ee_addr);
944
945     /* Shift the read command bits out. */
946     for (i = 10; i >= 0; i--) {
947         short dataval = (read_cmd & (1 << i)) ? EE_DO : 0;
948         outb_p(EE_EEP|EE_CS|dataval, ee_addr);
949         outb_p(EE_EEP|EE_CS|dataval|EE_CK, ee_addr);
950     }
951     outb(EE_EEP|EE_CS, ee_addr);
952
953     for (i = 16; i > 0; i--) {
954         outb_p(EE_EEP|EE_CS | EE_CK, ee_addr);
955         retval = (retval << 1) | ((inb(ee_addr) & EE_DI) ? 1 : 0);
956         outb_p(EE_EEP|EE_CS, ee_addr);
957     }
958
959     /* Terminate the EEPROM access. */
960     outb(0, ee_addr);
961     return retval;
962 }
963
964 /*
965     The internal ASIC registers can be changed by EEPROM READ access
966     with EE_ASIC bit set.
967     In ASIC mode, EE_ADOT is used to output the data to the ASIC.
968 */
969
970 static void write_asic(ioaddr_t ioaddr, int location, short asic_data)
971 {
972         int i;
973         ioaddr_t ee_addr = ioaddr + DLINK_EEPROM;
974         short dataval;
975         int read_cmd = location | (EE_READ_CMD << 8);
976
977         asic_data |= read_eeprom(ioaddr, location);
978
979         outb(0, ee_addr);
980         outb(EE_ASIC|EE_CS|EE_DI, ee_addr);
981
982         read_cmd = read_cmd >> 1;
983
984         /* Shift the read command bits out. */
985         for (i = 9; i >= 0; i--) {
986                 dataval = (read_cmd & (1 << i)) ? EE_DO : 0;
987                 outb_p(EE_ASIC|EE_CS|EE_DI|dataval, ee_addr);
988                 outb_p(EE_ASIC|EE_CS|EE_DI|dataval|EE_CK, ee_addr);
989                 outb_p(EE_ASIC|EE_CS|EE_DI|dataval, ee_addr);
990         }
991         // sync
992         outb(EE_ASIC|EE_CS, ee_addr);
993         outb(EE_ASIC|EE_CS|EE_CK, ee_addr);
994         outb(EE_ASIC|EE_CS, ee_addr);
995
996         for (i = 15; i >= 0; i--) {
997                 dataval = (asic_data & (1 << i)) ? EE_ADOT : 0;
998                 outb_p(EE_ASIC|EE_CS|dataval, ee_addr);
999                 outb_p(EE_ASIC|EE_CS|dataval|EE_CK, ee_addr);
1000                 outb_p(EE_ASIC|EE_CS|dataval, ee_addr);
1001         }
1002
1003         /* Terminate the ASIC access. */
1004         outb(EE_ASIC|EE_DI, ee_addr);
1005         outb(EE_ASIC|EE_DI| EE_CK, ee_addr);
1006         outb(EE_ASIC|EE_DI, ee_addr);
1007
1008         outb(0, ee_addr);
1009 }
1010
1011 /*====================================================================*/
1012
1013 static void set_misc_reg(struct net_device *dev)
1014 {
1015     ioaddr_t nic_base = dev->base_addr;
1016     pcnet_dev_t *info = PRIV(dev);
1017     u_char tmp;
1018     
1019     if (info->flags & HAS_MISC_REG) {
1020         tmp = inb_p(nic_base + PCNET_MISC) & ~3;
1021         if (dev->if_port == 2)
1022             tmp |= 1;
1023         if (info->flags & USE_BIG_BUF)
1024             tmp |= 2;
1025         if (info->flags & HAS_IBM_MISC)
1026             tmp |= 8;
1027         outb_p(tmp, nic_base + PCNET_MISC);
1028     }
1029     if (info->flags & IS_DL10022) {
1030         if (info->flags & HAS_MII) {
1031             mdio_reset(nic_base + DLINK_GPIO, info->eth_phy);
1032             /* Restart MII autonegotiation */
1033             mdio_write(nic_base + DLINK_GPIO, info->eth_phy, 0, 0x0000);
1034             mdio_write(nic_base + DLINK_GPIO, info->eth_phy, 0, 0x1200);
1035             info->mii_reset = jiffies;
1036         } else {
1037             outb(full_duplex ? 4 : 0, nic_base + DLINK_DIAG);
1038         }
1039     }
1040 }
1041
1042 /*====================================================================*/
1043
1044 static void mii_phy_probe(struct net_device *dev)
1045 {
1046     pcnet_dev_t *info = PRIV(dev);
1047     ioaddr_t mii_addr = dev->base_addr + DLINK_GPIO;
1048     int i;
1049     u_int tmp, phyid;
1050
1051     for (i = 31; i >= 0; i--) {
1052         tmp = mdio_read(mii_addr, i, 1);
1053         if ((tmp == 0) || (tmp == 0xffff))
1054             continue;
1055         tmp = mdio_read(mii_addr, i, MII_PHYID_REG1);
1056         phyid = tmp << 16;
1057         phyid |= mdio_read(mii_addr, i, MII_PHYID_REG2);
1058         phyid &= MII_PHYID_REV_MASK;
1059         DEBUG(0, "%s: MII at %d is 0x%08x\n", dev->name, i, phyid);
1060         if (phyid == AM79C9XX_HOME_PHY) {
1061             info->pna_phy = i;
1062         } else if (phyid != AM79C9XX_ETH_PHY) {
1063             info->eth_phy = i;
1064         }
1065     }
1066 }
1067
1068 static int pcnet_open(struct net_device *dev)
1069 {
1070     pcnet_dev_t *info = PRIV(dev);
1071     dev_link_t *link = &info->link;
1072     
1073     DEBUG(2, "pcnet_open('%s')\n", dev->name);
1074
1075     if (!DEV_OK(link))
1076         return -ENODEV;
1077
1078     link->open++;
1079
1080     set_misc_reg(dev);
1081     request_irq(dev->irq, ei_irq_wrapper, SA_SHIRQ, dev_info, dev);
1082
1083     info->phy_id = info->eth_phy;
1084     info->link_status = 0x00;
1085     init_timer(&info->watchdog);
1086     info->watchdog.function = &ei_watchdog;
1087     info->watchdog.data = (u_long)dev;
1088     info->watchdog.expires = jiffies + HZ;
1089     add_timer(&info->watchdog);
1090
1091     return ei_open(dev);
1092 } /* pcnet_open */
1093
1094 /*====================================================================*/
1095
1096 static int pcnet_close(struct net_device *dev)
1097 {
1098     pcnet_dev_t *info = PRIV(dev);
1099     dev_link_t *link = &info->link;
1100
1101     DEBUG(2, "pcnet_close('%s')\n", dev->name);
1102
1103     ei_close(dev);
1104     free_irq(dev->irq, dev);
1105     
1106     link->open--;
1107     netif_stop_queue(dev);
1108     del_timer_sync(&info->watchdog);
1109
1110     return 0;
1111 } /* pcnet_close */
1112
1113 /*======================================================================
1114
1115     Hard reset the card.  This used to pause for the same period that
1116     a 8390 reset command required, but that shouldn't be necessary.
1117
1118 ======================================================================*/
1119
1120 static void pcnet_reset_8390(struct net_device *dev)
1121 {
1122     ioaddr_t nic_base = dev->base_addr;
1123     int i;
1124
1125     ei_status.txing = ei_status.dmaing = 0;
1126
1127     outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, nic_base + E8390_CMD);
1128
1129     outb(inb(nic_base + PCNET_RESET), nic_base + PCNET_RESET);
1130
1131     for (i = 0; i < 100; i++) {
1132         if ((inb_p(nic_base+EN0_ISR) & ENISR_RESET) != 0)
1133             break;
1134         udelay(100);
1135     }
1136     outb_p(ENISR_RESET, nic_base + EN0_ISR); /* Ack intr. */
1137     
1138     if (i == 100)
1139         printk(KERN_ERR "%s: pcnet_reset_8390() did not complete.\n",
1140                dev->name);
1141     set_misc_reg(dev);
1142     
1143 } /* pcnet_reset_8390 */
1144
1145 /*====================================================================*/
1146
1147 static int set_config(struct net_device *dev, struct ifmap *map)
1148 {
1149     pcnet_dev_t *info = PRIV(dev);
1150     if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1151         if (!(info->flags & HAS_MISC_REG))
1152             return -EOPNOTSUPP;
1153         else if ((map->port < 1) || (map->port > 2))
1154             return -EINVAL;
1155         dev->if_port = map->port;
1156         printk(KERN_INFO "%s: switched to %s port\n",
1157                dev->name, if_names[dev->if_port]);
1158         NS8390_init(dev, 1);
1159     }
1160     return 0;
1161 }
1162
1163 /*====================================================================*/
1164
1165 static irqreturn_t ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs)
1166 {
1167     struct net_device *dev = dev_id;
1168     pcnet_dev_t *info = PRIV(dev);
1169     irqreturn_t ret = ei_interrupt(irq, dev_id, regs);
1170
1171     if (ret == IRQ_HANDLED)
1172             info->stale = 0;
1173     return ret;
1174 }
1175
1176 static void ei_watchdog(u_long arg)
1177 {
1178     struct net_device *dev = (struct net_device *)arg;
1179     pcnet_dev_t *info = PRIV(dev);
1180     ioaddr_t nic_base = dev->base_addr;
1181     ioaddr_t mii_addr = nic_base + DLINK_GPIO;
1182     u_short link;
1183
1184     if (!netif_device_present(dev)) goto reschedule;
1185
1186     /* Check for pending interrupt with expired latency timer: with
1187        this, we can limp along even if the interrupt is blocked */
1188     outb_p(E8390_NODMA+E8390_PAGE0, nic_base + E8390_CMD);
1189     if (info->stale++ && (inb_p(nic_base + EN0_ISR) & ENISR_ALL)) {
1190         if (!info->fast_poll)
1191             printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
1192         ei_irq_wrapper(dev->irq, dev, NULL);
1193         info->fast_poll = HZ;
1194     }
1195     if (info->fast_poll) {
1196         info->fast_poll--;
1197         info->watchdog.expires = jiffies + 1;
1198         add_timer(&info->watchdog);
1199         return;
1200     }
1201
1202     if (!(info->flags & HAS_MII))
1203         goto reschedule;
1204
1205     mdio_read(mii_addr, info->phy_id, 1);
1206     link = mdio_read(mii_addr, info->phy_id, 1);
1207     if (!link || (link == 0xffff)) {
1208         if (info->eth_phy) {
1209             info->phy_id = info->eth_phy = 0;
1210         } else {
1211             printk(KERN_INFO "%s: MII is missing!\n", dev->name);
1212             info->flags &= ~HAS_MII;
1213         }
1214         goto reschedule;
1215     }
1216
1217     link &= 0x0004;
1218     if (link != info->link_status) {
1219         u_short p = mdio_read(mii_addr, info->phy_id, 5);
1220         printk(KERN_INFO "%s: %s link beat\n", dev->name,
1221                (link) ? "found" : "lost");
1222         if (link && (info->flags & IS_DL10022)) {
1223             /* Disable collision detection on full duplex links */
1224             outb((p & 0x0140) ? 4 : 0, nic_base + DLINK_DIAG);
1225         } else if (link && (info->flags & IS_DL10019)) {
1226             /* Disable collision detection on full duplex links */
1227             write_asic(dev->base_addr, 4, (p & 0x140) ? DL19FDUPLX : 0);
1228         }
1229         if (link) {
1230             if (info->phy_id == info->eth_phy) {
1231                 if (p)
1232                     printk(KERN_INFO "%s: autonegotiation complete: "
1233                            "%sbaseT-%cD selected\n", dev->name,
1234                            ((p & 0x0180) ? "100" : "10"),
1235                            ((p & 0x0140) ? 'F' : 'H'));
1236                 else
1237                     printk(KERN_INFO "%s: link partner did not "
1238                            "autonegotiate\n", dev->name);
1239             }
1240             NS8390_init(dev, 1);
1241         }
1242         info->link_status = link;
1243     }
1244     if (info->pna_phy && time_after(jiffies, info->mii_reset + 6*HZ)) {
1245         link = mdio_read(mii_addr, info->eth_phy, 1) & 0x0004;
1246         if (((info->phy_id == info->pna_phy) && link) ||
1247             ((info->phy_id != info->pna_phy) && !link)) {
1248             /* isolate this MII and try flipping to the other one */
1249             mdio_write(mii_addr, info->phy_id, 0, 0x0400);
1250             info->phy_id ^= info->pna_phy ^ info->eth_phy;
1251             printk(KERN_INFO "%s: switched to %s transceiver\n", dev->name,
1252                    (info->phy_id == info->eth_phy) ? "ethernet" : "PNA");
1253             mdio_write(mii_addr, info->phy_id, 0,
1254                        (info->phy_id == info->eth_phy) ? 0x1000 : 0);
1255             info->link_status = 0;
1256             info->mii_reset = jiffies;
1257         }
1258     }
1259
1260 reschedule:
1261     info->watchdog.expires = jiffies + HZ;
1262     add_timer(&info->watchdog);
1263 }
1264
1265 /*====================================================================*/
1266
1267 static void netdev_get_drvinfo(struct net_device *dev,
1268                                struct ethtool_drvinfo *info)
1269 {
1270         strcpy(info->driver, "pcnet_cs");
1271 }
1272
1273 static struct ethtool_ops netdev_ethtool_ops = {
1274         .get_drvinfo            = netdev_get_drvinfo,
1275 };
1276
1277 /*====================================================================*/
1278
1279
1280 static int ei_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1281 {
1282     pcnet_dev_t *info = PRIV(dev);
1283     u16 *data = (u16 *)&rq->ifr_ifru;
1284     ioaddr_t mii_addr = dev->base_addr + DLINK_GPIO;
1285     switch (cmd) {
1286     case SIOCGMIIPHY:
1287         data[0] = info->phy_id;
1288     case SIOCGMIIREG:           /* Read MII PHY register. */
1289         data[3] = mdio_read(mii_addr, data[0], data[1] & 0x1f);
1290         return 0;
1291     case SIOCSMIIREG:           /* Write MII PHY register. */
1292         if (!capable(CAP_NET_ADMIN))
1293             return -EPERM;
1294         mdio_write(mii_addr, data[0], data[1] & 0x1f, data[2]);
1295         return 0;
1296     }
1297     return -EOPNOTSUPP;
1298 }
1299
1300 /*====================================================================*/
1301
1302 static void dma_get_8390_hdr(struct net_device *dev,
1303                              struct e8390_pkt_hdr *hdr,
1304                              int ring_page)
1305 {
1306     ioaddr_t nic_base = dev->base_addr;
1307
1308     if (ei_status.dmaing) {
1309         printk(KERN_NOTICE "%s: DMAing conflict in dma_block_input."
1310                "[DMAstat:%1x][irqlock:%1x]\n",
1311                dev->name, ei_status.dmaing, ei_status.irqlock);
1312         return;
1313     }
1314     
1315     ei_status.dmaing |= 0x01;
1316     outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + PCNET_CMD);
1317     outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
1318     outb_p(0, nic_base + EN0_RCNTHI);
1319     outb_p(0, nic_base + EN0_RSARLO);           /* On page boundary */
1320     outb_p(ring_page, nic_base + EN0_RSARHI);
1321     outb_p(E8390_RREAD+E8390_START, nic_base + PCNET_CMD);
1322
1323     insw(nic_base + PCNET_DATAPORT, hdr,
1324             sizeof(struct e8390_pkt_hdr)>>1);
1325     /* Fix for big endian systems */
1326     hdr->count = le16_to_cpu(hdr->count);
1327
1328     outb_p(ENISR_RDC, nic_base + EN0_ISR);      /* Ack intr. */
1329     ei_status.dmaing &= ~0x01;
1330 }
1331
1332 /*====================================================================*/
1333
1334 static void dma_block_input(struct net_device *dev, int count,
1335                             struct sk_buff *skb, int ring_offset)
1336 {
1337     ioaddr_t nic_base = dev->base_addr;
1338     int xfer_count = count;
1339     char *buf = skb->data;
1340
1341 #ifdef PCMCIA_DEBUG
1342     if ((ei_debug > 4) && (count != 4))
1343         printk(KERN_DEBUG "%s: [bi=%d]\n", dev->name, count+4);
1344 #endif
1345     if (ei_status.dmaing) {
1346         printk(KERN_NOTICE "%s: DMAing conflict in dma_block_input."
1347                "[DMAstat:%1x][irqlock:%1x]\n",
1348                dev->name, ei_status.dmaing, ei_status.irqlock);
1349         return;
1350     }
1351     ei_status.dmaing |= 0x01;
1352     outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + PCNET_CMD);
1353     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
1354     outb_p(count >> 8, nic_base + EN0_RCNTHI);
1355     outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
1356     outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
1357     outb_p(E8390_RREAD+E8390_START, nic_base + PCNET_CMD);
1358
1359     insw(nic_base + PCNET_DATAPORT,buf,count>>1);
1360     if (count & 0x01)
1361         buf[count-1] = inb(nic_base + PCNET_DATAPORT), xfer_count++;
1362
1363     /* This was for the ALPHA version only, but enough people have
1364        encountering problems that it is still here. */
1365 #ifdef PCMCIA_DEBUG
1366     if (ei_debug > 4) {         /* DMA termination address check... */
1367         int addr, tries = 20;
1368         do {
1369             /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
1370                -- it's broken for Rx on some cards! */
1371             int high = inb_p(nic_base + EN0_RSARHI);
1372             int low = inb_p(nic_base + EN0_RSARLO);
1373             addr = (high << 8) + low;
1374             if (((ring_offset + xfer_count) & 0xff) == (addr & 0xff))
1375                 break;
1376         } while (--tries > 0);
1377         if (tries <= 0)
1378             printk(KERN_NOTICE "%s: RX transfer address mismatch,"
1379                    "%#4.4x (expected) vs. %#4.4x (actual).\n",
1380                    dev->name, ring_offset + xfer_count, addr);
1381     }
1382 #endif
1383     outb_p(ENISR_RDC, nic_base + EN0_ISR);      /* Ack intr. */
1384     ei_status.dmaing &= ~0x01;
1385 } /* dma_block_input */
1386
1387 /*====================================================================*/
1388
1389 static void dma_block_output(struct net_device *dev, int count,
1390                              const u_char *buf, const int start_page)
1391 {
1392     ioaddr_t nic_base = dev->base_addr;
1393     pcnet_dev_t *info = PRIV(dev);
1394 #ifdef PCMCIA_DEBUG
1395     int retries = 0;
1396 #endif
1397     u_long dma_start;
1398
1399 #ifdef PCMCIA_DEBUG
1400     if (ei_debug > 4)
1401         printk(KERN_DEBUG "%s: [bo=%d]\n", dev->name, count);
1402 #endif
1403
1404     /* Round the count up for word writes.  Do we need to do this?
1405        What effect will an odd byte count have on the 8390?
1406        I should check someday. */
1407     if (count & 0x01)
1408         count++;
1409     if (ei_status.dmaing) {
1410         printk(KERN_NOTICE "%s: DMAing conflict in dma_block_output."
1411                "[DMAstat:%1x][irqlock:%1x]\n",
1412                dev->name, ei_status.dmaing, ei_status.irqlock);
1413         return;
1414     }
1415     ei_status.dmaing |= 0x01;
1416     /* We should already be in page 0, but to be safe... */
1417     outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base+PCNET_CMD);
1418
1419 #ifdef PCMCIA_DEBUG
1420   retry:
1421 #endif
1422
1423     outb_p(ENISR_RDC, nic_base + EN0_ISR);
1424
1425     /* Now the normal output. */
1426     outb_p(count & 0xff, nic_base + EN0_RCNTLO);
1427     outb_p(count >> 8,   nic_base + EN0_RCNTHI);
1428     outb_p(0x00, nic_base + EN0_RSARLO);
1429     outb_p(start_page, nic_base + EN0_RSARHI);
1430
1431     outb_p(E8390_RWRITE+E8390_START, nic_base + PCNET_CMD);
1432     outsw(nic_base + PCNET_DATAPORT, buf, count>>1);
1433
1434     dma_start = jiffies;
1435
1436 #ifdef PCMCIA_DEBUG
1437     /* This was for the ALPHA version only, but enough people have
1438        encountering problems that it is still here. */
1439     if (ei_debug > 4) { /* DMA termination address check... */
1440         int addr, tries = 20;
1441         do {
1442             int high = inb_p(nic_base + EN0_RSARHI);
1443             int low = inb_p(nic_base + EN0_RSARLO);
1444             addr = (high << 8) + low;
1445             if ((start_page << 8) + count == addr)
1446                 break;
1447         } while (--tries > 0);
1448         if (tries <= 0) {
1449             printk(KERN_NOTICE "%s: Tx packet transfer address mismatch,"
1450                    "%#4.4x (expected) vs. %#4.4x (actual).\n",
1451                    dev->name, (start_page << 8) + count, addr);
1452             if (retries++ == 0)
1453                 goto retry;
1454         }
1455     }
1456 #endif
1457
1458     while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
1459         if (time_after(jiffies, dma_start + PCNET_RDC_TIMEOUT)) {
1460             printk(KERN_NOTICE "%s: timeout waiting for Tx RDC.\n",
1461                    dev->name);
1462             pcnet_reset_8390(dev);
1463             NS8390_init(dev, 1);
1464             break;
1465         }
1466
1467     outb_p(ENISR_RDC, nic_base + EN0_ISR);      /* Ack intr. */
1468     if (info->flags & DELAY_OUTPUT)
1469         udelay((long)delay_time);
1470     ei_status.dmaing &= ~0x01;
1471 }
1472
1473 /*====================================================================*/
1474
1475 static int setup_dma_config(dev_link_t *link, int start_pg,
1476                             int stop_pg)
1477 {
1478     struct net_device *dev = link->priv;
1479
1480     ei_status.tx_start_page = start_pg;
1481     ei_status.rx_start_page = start_pg + TX_PAGES;
1482     ei_status.stop_page = stop_pg;
1483
1484     /* set up block i/o functions */
1485     ei_status.get_8390_hdr = &dma_get_8390_hdr;
1486     ei_status.block_input = &dma_block_input;
1487     ei_status.block_output = &dma_block_output;
1488
1489     return 0;
1490 }
1491
1492 /*====================================================================*/
1493
1494 static void copyin(u_char *dest, u_char *src, int c)
1495 {
1496     u_short *d = (u_short *)dest, *s = (u_short *)src;
1497     int odd;
1498
1499     if (c <= 0)
1500         return;
1501     odd = (c & 1); c >>= 1;
1502
1503     if (c) {
1504         do { *d++ = __raw_readw(s++); } while (--c);
1505     }
1506     /* get last byte by fetching a word and masking */
1507     if (odd)
1508         *((u_char *)d) = readw(s) & 0xff;
1509 }
1510
1511 static void copyout(u_char *dest, const u_char *src, int c)
1512 {
1513     u_short *d = (u_short *)dest, *s = (u_short *)src;
1514     int odd;
1515
1516     if (c <= 0)
1517         return;
1518     odd = (c & 1); c >>= 1;
1519
1520     if (c) {
1521         do { __raw_writew(*s++, d++); } while (--c);
1522     }
1523     /* copy last byte doing a read-modify-write */
1524     if (odd)
1525         writew((readw(d) & 0xff00) | *(u_char *)s, d);
1526 }
1527
1528 /*====================================================================*/
1529
1530 static void shmem_get_8390_hdr(struct net_device *dev,
1531                                struct e8390_pkt_hdr *hdr,
1532                                int ring_page)
1533 {
1534     void *xfer_start = (void *)(ei_status.rmem_start + (ring_page << 8)
1535                                 - (ei_status.rx_start_page << 8));
1536     
1537     copyin((void *)hdr, xfer_start, sizeof(struct e8390_pkt_hdr));
1538     /* Fix for big endian systems */
1539     hdr->count = le16_to_cpu(hdr->count);
1540 }
1541
1542 /*====================================================================*/
1543
1544 static void shmem_block_input(struct net_device *dev, int count,
1545                               struct sk_buff *skb, int ring_offset)
1546 {
1547     void *xfer_start = (void *)(ei_status.rmem_start + ring_offset
1548                                 - (ei_status.rx_start_page << 8));
1549     char *buf = skb->data;
1550     
1551     if (xfer_start + count > (void *)ei_status.rmem_end) {
1552         /* We must wrap the input move. */
1553         int semi_count = (void*)ei_status.rmem_end - xfer_start;
1554         copyin(buf, xfer_start, semi_count);
1555         buf += semi_count;
1556         ring_offset = ei_status.rx_start_page << 8;
1557         xfer_start = (void *)ei_status.rmem_start;
1558         count -= semi_count;
1559     }
1560     copyin(buf, xfer_start, count);
1561 }
1562
1563 /*====================================================================*/
1564
1565 static void shmem_block_output(struct net_device *dev, int count,
1566                                const u_char *buf, const int start_page)
1567 {
1568     void *shmem = (void *)dev->mem_start + (start_page << 8);
1569     shmem -= ei_status.tx_start_page << 8;
1570     copyout(shmem, buf, count);
1571 }
1572
1573 /*====================================================================*/
1574
1575 static int setup_shmem_window(dev_link_t *link, int start_pg,
1576                               int stop_pg, int cm_offset)
1577 {
1578     struct net_device *dev = link->priv;
1579     pcnet_dev_t *info = PRIV(dev);
1580     win_req_t req;
1581     memreq_t mem;
1582     int i, window_size, offset, last_ret, last_fn;
1583
1584     window_size = (stop_pg - start_pg) << 8;
1585     if (window_size > 32 * 1024)
1586         window_size = 32 * 1024;
1587
1588     /* Make sure it's a power of two.  */
1589     while ((window_size & (window_size - 1)) != 0)
1590         window_size += window_size & ~(window_size - 1);
1591
1592     /* Allocate a memory window */
1593     req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM|WIN_ENABLE;
1594     req.Attributes |= WIN_USE_WAIT;
1595     req.Base = 0; req.Size = window_size;
1596     req.AccessSpeed = mem_speed;
1597     CS_CHECK(RequestWindow, pcmcia_request_window(&link->handle, &req, &link->win));
1598
1599     mem.CardOffset = (start_pg << 8) + cm_offset;
1600     offset = mem.CardOffset % window_size;
1601     mem.CardOffset -= offset;
1602     mem.Page = 0;
1603     CS_CHECK(MapMemPage, pcmcia_map_mem_page(link->win, &mem));
1604
1605     /* Try scribbling on the buffer */
1606     info->base = ioremap(req.Base, window_size);
1607     for (i = 0; i < (TX_PAGES<<8); i += 2)
1608         __raw_writew((i>>1), info->base+offset+i);
1609     udelay(100);
1610     for (i = 0; i < (TX_PAGES<<8); i += 2)
1611         if (__raw_readw(info->base+offset+i) != (i>>1)) break;
1612     pcnet_reset_8390(dev);
1613     if (i != (TX_PAGES<<8)) {
1614         iounmap(info->base);
1615         pcmcia_release_window(link->win);
1616         info->base = NULL; link->win = NULL;
1617         goto failed;
1618     }
1619     
1620     dev->mem_start = (u_long)info->base + offset;
1621     ei_status.rmem_start = dev->mem_start + (TX_PAGES<<8);
1622     dev->mem_end = ei_status.rmem_end = (u_long)info->base + req.Size;
1623
1624     ei_status.tx_start_page = start_pg;
1625     ei_status.rx_start_page = start_pg + TX_PAGES;
1626     ei_status.stop_page = start_pg + ((req.Size - offset) >> 8);
1627
1628     /* set up block i/o functions */
1629     ei_status.get_8390_hdr = &shmem_get_8390_hdr;
1630     ei_status.block_input = &shmem_block_input;
1631     ei_status.block_output = &shmem_block_output;
1632
1633     info->flags |= USE_SHMEM;
1634     return 0;
1635
1636 cs_failed:
1637     cs_error(link->handle, last_fn, last_ret);
1638 failed:
1639     return 1;
1640 }
1641
1642 /*====================================================================*/
1643
1644 static struct pcmcia_driver pcnet_driver = {
1645         .drv            = {
1646                 .name   = "pcnet_cs",
1647         },
1648         .attach         = pcnet_attach,
1649         .detach         = pcnet_detach,
1650         .owner          = THIS_MODULE,
1651 };
1652
1653 static int __init init_pcnet_cs(void)
1654 {
1655     return pcmcia_register_driver(&pcnet_driver);
1656 }
1657
1658 static void __exit exit_pcnet_cs(void)
1659 {
1660     DEBUG(0, "pcnet_cs: unloading\n");
1661     pcmcia_unregister_driver(&pcnet_driver);
1662     while (dev_list != NULL)
1663         pcnet_detach(dev_list);
1664 }
1665
1666 module_init(init_pcnet_cs);
1667 module_exit(exit_pcnet_cs);