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