1 /*======================================================================
2 
3     A PCMCIA ethernet driver for Asix AX88190-based cards
4 
5     The Asix AX88190 is a NS8390-derived chipset with a few nasty
6     idiosyncracies that make it very inconvenient to support with a
7     standard 8390 driver.  This driver is based on pcnet_cs, with the
8     tweaked 8390 code grafted on the end.  Much of what I did was to
9     clean up and update a similar driver supplied by Asix, which was
10     adapted by William Lee, william@asix.com.tw.
11 
12     Copyright (C) 2001 David A. Hinds -- dahinds@users.sourceforge.net
13 
14     axnet_cs.c 1.28 2002/06/29 06:27:37
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 ======================================================================*/
26 
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/sched.h>
31 #include <linux/ptrace.h>
32 #include <linux/slab.h>
33 #include <linux/string.h>
34 #include <linux/timer.h>
35 #include <linux/delay.h>
36 #include <linux/spinlock.h>
37 #include <linux/ethtool.h>
38 #include <asm/io.h>
39 #include <asm/system.h>
40 #include <asm/byteorder.h>
41 #include <asm/uaccess.h>
42 
43 #include <linux/netdevice.h>
44 #include "../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 AXNET_CMD	0x00
55 #define AXNET_DATAPORT	0x10	/* NatSemi-defined port window offset. */
56 #define AXNET_RESET	0x1f	/* Issue a read to reset, a write to clear. */
57 #define AXNET_MII_EEP	0x14	/* Offset of MII access port */
58 #define AXNET_TEST	0x15	/* Offset of TEST Register port */
59 #define AXNET_GPIO	0x17	/* Offset of General Purpose Register Port */
60 
61 #define AXNET_START_PG	0x40	/* First page of TX buffer */
62 #define AXNET_STOP_PG	0x80	/* Last page +1 of RX ring */
63 
64 #define AXNET_RDC_TIMEOUT 0x02	/* Max wait in jiffies for Tx RDC */
65 
66 #define IS_AX88190	0x0001
67 #define IS_AX88790	0x0002
68 
69 /*====================================================================*/
70 
71 /* Module parameters */
72 
73 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
74 MODULE_DESCRIPTION("Asix AX88190 PCMCIA ethernet driver");
75 MODULE_LICENSE("GPL");
76 
77 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
78 
79 /* Bit map of interrupts to choose from */
80 INT_MODULE_PARM(irq_mask,	0xdeb8);
81 static int irq_list[4] = { -1 };
82 MODULE_PARM(irq_list, "1-4i");
83 
84 #ifdef PCMCIA_DEBUG
85 INT_MODULE_PARM(pc_debug, PCMCIA_DEBUG);
86 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
87 static char *version =
88 "axnet_cs.c 1.28 2002/06/29 06:27:37 (David Hinds)";
89 #else
90 #define DEBUG(n, args...)
91 #endif
92 
93 /*====================================================================*/
94 
95 static void axnet_config(dev_link_t *link);
96 static void axnet_release(u_long arg);
97 static int axnet_event(event_t event, int priority,
98 		       event_callback_args_t *args);
99 static int axnet_open(struct net_device *dev);
100 static int axnet_close(struct net_device *dev);
101 static int axnet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
102 static struct ethtool_ops netdev_ethtool_ops;
103 
104 static void ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs);
105 static void ei_watchdog(u_long arg);
106 static void axnet_reset_8390(struct net_device *dev);
107 
108 static int mdio_read(ioaddr_t addr, int phy_id, int loc);
109 static void mdio_write(ioaddr_t addr, int phy_id, int loc, int value);
110 
111 static void get_8390_hdr(struct net_device *,
112 			 struct e8390_pkt_hdr *, int);
113 static void block_input(struct net_device *dev, int count,
114 			struct sk_buff *skb, int ring_offset);
115 static void block_output(struct net_device *dev, int count,
116 			 const u_char *buf, const int start_page);
117 
118 static dev_link_t *axnet_attach(void);
119 static void axnet_detach(dev_link_t *);
120 
121 static dev_info_t dev_info = "axnet_cs";
122 static dev_link_t *dev_list;
123 
124 static int axdev_init(struct net_device *dev);
125 static void AX88190_init(struct net_device *dev, int startp);
126 static int ax_open(struct net_device *dev);
127 static int ax_close(struct net_device *dev);
128 static void ax_interrupt(int irq, void *dev_id, struct pt_regs *regs);
129 
130 /*====================================================================*/
131 
132 typedef struct axnet_dev_t {
133     struct net_device	dev;	/* so &dev == &axnet_dev_t */
134     dev_link_t		link;
135     dev_node_t		node;
136     caddr_t		base;
137     struct timer_list	watchdog;
138     int			stale, fast_poll;
139     u_short		link_status;
140     u_char		duplex_flag;
141     int			phy_id;
142     int			flags;
143 } axnet_dev_t;
144 
145 /*======================================================================
146 
147     This bit of code is used to avoid unregistering network devices
148     at inappropriate times.  2.2 and later kernels are fairly picky
149     about when this can happen.
150 
151 ======================================================================*/
152 
flush_stale_links(void)153 static void flush_stale_links(void)
154 {
155     dev_link_t *link, *next;
156     for (link = dev_list; link; link = next) {
157 	next = link->next;
158 	if (link->state & DEV_STALE_LINK)
159 	    axnet_detach(link);
160     }
161 }
162 
163 /*====================================================================*/
164 
cs_error(client_handle_t handle,int func,int ret)165 static void cs_error(client_handle_t handle, int func, int ret)
166 {
167     error_info_t err = { func, ret };
168     CardServices(ReportError, handle, &err);
169 }
170 
171 /*======================================================================
172 
173     We never need to do anything when a axnet device is "initialized"
174     by the net software, because we only register already-found cards.
175 
176 ======================================================================*/
177 
axnet_init(struct net_device * dev)178 static int axnet_init(struct net_device *dev)
179 {
180     return 0;
181 }
182 
183 /*======================================================================
184 
185     axnet_attach() creates an "instance" of the driver, allocating
186     local data structures for one device.  The device is registered
187     with Card Services.
188 
189 ======================================================================*/
190 
axnet_attach(void)191 static dev_link_t *axnet_attach(void)
192 {
193     axnet_dev_t *info;
194     dev_link_t *link;
195     struct net_device *dev;
196     client_reg_t client_reg;
197     int i, ret;
198 
199     DEBUG(0, "axnet_attach()\n");
200     flush_stale_links();
201 
202     /* Create new ethernet device */
203     info = kmalloc(sizeof(*info), GFP_KERNEL);
204     if (!info) return NULL;
205     memset(info, 0, sizeof(*info));
206     link = &info->link; dev = &info->dev;
207     link->priv = info;
208 
209     link->release.function = &axnet_release;
210     link->release.data = (u_long)link;
211     link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
212     link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
213     if (irq_list[0] == -1)
214 	link->irq.IRQInfo2 = irq_mask;
215     else
216 	for (i = 0; i < 4; i++)
217 	    link->irq.IRQInfo2 |= 1 << irq_list[i];
218     link->conf.Attributes = CONF_ENABLE_IRQ;
219     link->conf.IntType = INT_MEMORY_AND_IO;
220 
221     axdev_init(dev);
222     dev->init = &axnet_init;
223     dev->open = &axnet_open;
224     dev->stop = &axnet_close;
225     dev->do_ioctl = &axnet_ioctl;
226     SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
227 
228     /* Register with Card Services */
229     link->next = dev_list;
230     dev_list = link;
231     client_reg.dev_info = &dev_info;
232     client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
233     client_reg.EventMask =
234 	CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
235 	CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
236 	CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
237     client_reg.event_handler = &axnet_event;
238     client_reg.Version = 0x0210;
239     client_reg.event_callback_args.client_data = link;
240     ret = CardServices(RegisterClient, &link->handle, &client_reg);
241     if (ret != CS_SUCCESS) {
242 	cs_error(link->handle, RegisterClient, ret);
243 	axnet_detach(link);
244 	return NULL;
245     }
246 
247     return link;
248 } /* axnet_attach */
249 
250 /*======================================================================
251 
252     This deletes a driver "instance".  The device is de-registered
253     with Card Services.  If it has been released, all local data
254     structures are freed.  Otherwise, the structures will be freed
255     when the device is released.
256 
257 ======================================================================*/
258 
axnet_detach(dev_link_t * link)259 static void axnet_detach(dev_link_t *link)
260 {
261     axnet_dev_t *info = link->priv;
262     dev_link_t **linkp;
263 
264     DEBUG(0, "axnet_detach(0x%p)\n", link);
265 
266     /* Locate device structure */
267     for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
268 	if (*linkp == link) break;
269     if (*linkp == NULL)
270 	return;
271 
272     del_timer(&link->release);
273     if (link->state & DEV_CONFIG) {
274 	axnet_release((u_long)link);
275 	if (link->state & DEV_STALE_CONFIG) {
276 	    link->state |= DEV_STALE_LINK;
277 	    return;
278 	}
279     }
280 
281     if (link->handle)
282 	CardServices(DeregisterClient, link->handle);
283 
284     /* Unlink device structure, free bits */
285     *linkp = link->next;
286     if (link->dev)
287 	unregister_netdev(&info->dev);
288     kfree(info);
289 
290 } /* axnet_detach */
291 
292 /*======================================================================
293 
294     This probes for a card's hardware address by reading the PROM.
295 
296 ======================================================================*/
297 
get_prom(dev_link_t * link)298 static int get_prom(dev_link_t *link)
299 {
300     struct net_device *dev = link->priv;
301     ioaddr_t ioaddr = dev->base_addr;
302     int i, j;
303 
304     /* This is based on drivers/net/ne.c */
305     struct {
306 	u_char value, offset;
307     } program_seq[] = {
308 	{E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
309 	{0x01,	EN0_DCFG},	/* Set word-wide access. */
310 	{0x00,	EN0_RCNTLO},	/* Clear the count regs. */
311 	{0x00,	EN0_RCNTHI},
312 	{0x00,	EN0_IMR},	/* Mask completion irq. */
313 	{0xFF,	EN0_ISR},
314 	{E8390_RXOFF|0x40, EN0_RXCR},	/* 0x60  Set to monitor */
315 	{E8390_TXOFF, EN0_TXCR},	/* 0x02  and loopback mode. */
316 	{0x10,	EN0_RCNTLO},
317 	{0x00,	EN0_RCNTHI},
318 	{0x00,	EN0_RSARLO},	/* DMA starting at 0x0400. */
319 	{0x04,	EN0_RSARHI},
320 	{E8390_RREAD+E8390_START, E8390_CMD},
321     };
322 
323     /* Not much of a test, but the alternatives are messy */
324     if (link->conf.ConfigBase != 0x03c0)
325 	return 0;
326 
327     axnet_reset_8390(dev);
328     mdelay(10);
329 
330     for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
331 	outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
332 
333     for (i = 0; i < 6; i += 2) {
334 	j = inw(ioaddr + AXNET_DATAPORT);
335 	dev->dev_addr[i] = j & 0xff;
336 	dev->dev_addr[i+1] = j >> 8;
337     }
338     return 1;
339 } /* get_prom */
340 
341 /*======================================================================
342 
343     axnet_config() is scheduled to run after a CARD_INSERTION event
344     is received, to configure the PCMCIA socket, and to make the
345     ethernet device available to the system.
346 
347 ======================================================================*/
348 
349 #define CS_CHECK(fn, args...) \
350 while ((last_ret=CardServices(last_fn=(fn), args))!=0) goto cs_failed
351 
352 #define CFG_CHECK(fn, args...) \
353 if (CardServices(fn, args) != 0) goto next_entry
354 
try_io_port(dev_link_t * link)355 static int try_io_port(dev_link_t *link)
356 {
357     int j, ret;
358     if (link->io.NumPorts1 == 32) {
359 	link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
360 	if (link->io.NumPorts2 > 0) {
361 	    /* for master/slave multifunction cards */
362 	    link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
363 	    link->irq.Attributes =
364 		IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
365 	}
366     } else {
367 	/* This should be two 16-port windows */
368 	link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
369 	link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
370     }
371     if (link->io.BasePort1 == 0) {
372 	link->io.IOAddrLines = 16;
373 	for (j = 0; j < 0x400; j += 0x20) {
374 	    link->io.BasePort1 = j ^ 0x300;
375 	    link->io.BasePort2 = (j ^ 0x300) + 0x10;
376 	    ret = CardServices(RequestIO, link->handle, &link->io);
377 	    if (ret == CS_SUCCESS) return ret;
378 	}
379 	return ret;
380     } else {
381 	return CardServices(RequestIO, link->handle, &link->io);
382     }
383 }
384 
axnet_config(dev_link_t * link)385 static void axnet_config(dev_link_t *link)
386 {
387     client_handle_t handle = link->handle;
388     axnet_dev_t *info = link->priv;
389     struct net_device *dev = &info->dev;
390     tuple_t tuple;
391     cisparse_t parse;
392     int i, j, last_ret, last_fn;
393     u_short buf[64];
394     config_info_t conf;
395 
396     DEBUG(0, "axnet_config(0x%p)\n", link);
397 
398     tuple.Attributes = 0;
399     tuple.TupleData = (cisdata_t *)buf;
400     tuple.TupleDataMax = sizeof(buf);
401     tuple.TupleOffset = 0;
402     tuple.DesiredTuple = CISTPL_CONFIG;
403     CS_CHECK(GetFirstTuple, handle, &tuple);
404     CS_CHECK(GetTupleData, handle, &tuple);
405     CS_CHECK(ParseTuple, handle, &tuple, &parse);
406     link->conf.ConfigBase = parse.config.base;
407     /* don't trust the CIS on this; Linksys got it wrong */
408     link->conf.Present = 0x63;
409 
410     /* Configure card */
411     link->state |= DEV_CONFIG;
412 
413     /* Look up current Vcc */
414     CS_CHECK(GetConfigurationInfo, handle, &conf);
415     link->conf.Vcc = conf.Vcc;
416 
417     tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
418     tuple.Attributes = 0;
419     CS_CHECK(GetFirstTuple, handle, &tuple);
420     while (last_ret == CS_SUCCESS) {
421 	cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
422 	cistpl_io_t *io = &(parse.cftable_entry.io);
423 
424 	CFG_CHECK(GetTupleData, handle, &tuple);
425 	CFG_CHECK(ParseTuple, handle, &tuple, &parse);
426 	if ((cfg->index == 0) || (cfg->io.nwin == 0))
427 	    goto next_entry;
428 
429 	link->conf.ConfigIndex = 0x05;
430 	/* For multifunction cards, by convention, we configure the
431 	   network function with window 0, and serial with window 1 */
432 	if (io->nwin > 1) {
433 	    i = (io->win[1].len > io->win[0].len);
434 	    link->io.BasePort2 = io->win[1-i].base;
435 	    link->io.NumPorts2 = io->win[1-i].len;
436 	} else {
437 	    i = link->io.NumPorts2 = 0;
438 	}
439 	link->io.BasePort1 = io->win[i].base;
440 	link->io.NumPorts1 = io->win[i].len;
441 	link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
442 	if (link->io.NumPorts1 + link->io.NumPorts2 >= 32) {
443 	    last_ret = try_io_port(link);
444 	    if (last_ret == CS_SUCCESS) break;
445 	}
446     next_entry:
447 	last_ret = CardServices(GetNextTuple, handle, &tuple);
448     }
449     if (last_ret != CS_SUCCESS) {
450 	cs_error(handle, RequestIO, last_ret);
451 	goto failed;
452     }
453 
454     CS_CHECK(RequestIRQ, handle, &link->irq);
455 
456     if (link->io.NumPorts2 == 8) {
457 	link->conf.Attributes |= CONF_ENABLE_SPKR;
458 	link->conf.Status = CCSR_AUDIO_ENA;
459     }
460 
461     CS_CHECK(RequestConfiguration, handle, &link->conf);
462     dev->irq = link->irq.AssignedIRQ;
463     dev->base_addr = link->io.BasePort1;
464     if (register_netdev(dev) != 0) {
465 	printk(KERN_NOTICE "axnet_cs: register_netdev() failed\n");
466 	goto failed;
467     }
468 
469     if (!get_prom(link)) {
470 	printk(KERN_NOTICE "axnet_cs: this is not an AX88190 card!\n");
471 	printk(KERN_NOTICE "axnet_cs: use pcnet_cs instead.\n");
472 	unregister_netdev(dev);
473 	goto failed;
474     }
475 
476     ei_status.name = "AX88190";
477     ei_status.word16 = 1;
478     ei_status.tx_start_page = AXNET_START_PG;
479     ei_status.rx_start_page = AXNET_START_PG + TX_PAGES;
480     ei_status.stop_page = AXNET_STOP_PG;
481     ei_status.reset_8390 = &axnet_reset_8390;
482     ei_status.get_8390_hdr = &get_8390_hdr;
483     ei_status.block_input = &block_input;
484     ei_status.block_output = &block_output;
485 
486     strcpy(info->node.dev_name, dev->name);
487     link->dev = &info->node;
488 
489     if (inb(dev->base_addr + AXNET_TEST) != 0)
490 	info->flags |= IS_AX88790;
491     else
492 	info->flags |= IS_AX88190;
493 
494     printk(KERN_INFO "%s: Asix AX88%d90: io %#3lx, irq %d, hw_addr ",
495 	   dev->name, ((info->flags & IS_AX88790) ? 7 : 1),
496 	   dev->base_addr, dev->irq);
497     for (i = 0; i < 6; i++)
498 	printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
499 
500     if (info->flags & IS_AX88790)
501 	outb(0x10, dev->base_addr + AXNET_GPIO);  /* select Internal PHY */
502 
503     for (i = 0; i < 32; i++) {
504 	j = mdio_read(dev->base_addr + AXNET_MII_EEP, i, 1);
505 	if ((j != 0) && (j != 0xffff)) break;
506     }
507 
508     /* Maybe PHY is in power down mode. (PPD_SET = 1)
509        Bit 2 of CCSR is active low. */
510     if (i == 32) {
511 	conf_reg_t reg = { 0, CS_WRITE, CISREG_CCSR, 0x04 };
512  	CardServices(AccessConfigurationRegister, link->handle, &reg);
513 	for (i = 0; i < 32; i++) {
514 	    j = mdio_read(dev->base_addr + AXNET_MII_EEP, i, 1);
515 	    if ((j != 0) && (j != 0xffff)) break;
516 	}
517     }
518 
519     info->phy_id = (i < 32) ? i : -1;
520     if (i < 32) {
521 	DEBUG(0, "  MII transceiver at index %d, status %x.\n", i, j);
522     } else {
523 	printk(KERN_NOTICE "  No MII transceivers found!\n");
524     }
525 
526     link->state &= ~DEV_CONFIG_PENDING;
527     return;
528 
529 cs_failed:
530     cs_error(link->handle, last_fn, last_ret);
531 failed:
532     axnet_release((u_long)link);
533     link->state &= ~DEV_CONFIG_PENDING;
534     return;
535 } /* axnet_config */
536 
537 /*======================================================================
538 
539     After a card is removed, axnet_release() will unregister the net
540     device, and release the PCMCIA configuration.  If the device is
541     still open, this will be postponed until it is closed.
542 
543 ======================================================================*/
544 
axnet_release(u_long arg)545 static void axnet_release(u_long arg)
546 {
547     dev_link_t *link = (dev_link_t *)arg;
548 
549     DEBUG(0, "axnet_release(0x%p)\n", link);
550 
551     if (link->open) {
552 	DEBUG(1, "axnet_cs: release postponed, '%s' still open\n",
553 	      ((axnet_dev_t *)(link->priv))->node.dev_name);
554 	link->state |= DEV_STALE_CONFIG;
555 	return;
556     }
557 
558     CardServices(ReleaseConfiguration, link->handle);
559     CardServices(ReleaseIO, link->handle, &link->io);
560     CardServices(ReleaseIRQ, link->handle, &link->irq);
561 
562     link->state &= ~DEV_CONFIG;
563 
564 } /* axnet_release */
565 
566 /*======================================================================
567 
568     The card status event handler.  Mostly, this schedules other
569     stuff to run after an event is received.  A CARD_REMOVAL event
570     also sets some flags to discourage the net drivers from trying
571     to talk to the card any more.
572 
573 ======================================================================*/
574 
axnet_event(event_t event,int priority,event_callback_args_t * args)575 static int axnet_event(event_t event, int priority,
576 		       event_callback_args_t *args)
577 {
578     dev_link_t *link = args->client_data;
579     axnet_dev_t *info = link->priv;
580 
581     DEBUG(2, "axnet_event(0x%06x)\n", event);
582 
583     switch (event) {
584     case CS_EVENT_CARD_REMOVAL:
585 	link->state &= ~DEV_PRESENT;
586 	if (link->state & DEV_CONFIG) {
587 	    netif_device_detach(&info->dev);
588 	    mod_timer(&link->release, jiffies + HZ/20);
589 	}
590 	break;
591     case CS_EVENT_CARD_INSERTION:
592 	link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
593 	axnet_config(link);
594 	break;
595     case CS_EVENT_PM_SUSPEND:
596 	link->state |= DEV_SUSPEND;
597 	/* Fall through... */
598     case CS_EVENT_RESET_PHYSICAL:
599 	if (link->state & DEV_CONFIG) {
600 	    if (link->open)
601 		netif_device_detach(&info->dev);
602 	    CardServices(ReleaseConfiguration, link->handle);
603 	}
604 	break;
605     case CS_EVENT_PM_RESUME:
606 	link->state &= ~DEV_SUSPEND;
607 	/* Fall through... */
608     case CS_EVENT_CARD_RESET:
609 	if (link->state & DEV_CONFIG) {
610 	    CardServices(RequestConfiguration, link->handle, &link->conf);
611 	    if (link->open) {
612 		axnet_reset_8390(&info->dev);
613 		AX88190_init(&info->dev, 1);
614 		netif_device_attach(&info->dev);
615 	    }
616 	}
617 	break;
618     }
619     return 0;
620 } /* axnet_event */
621 
622 /*======================================================================
623 
624     MII interface support
625 
626 ======================================================================*/
627 
628 #define MDIO_SHIFT_CLK		0x01
629 #define MDIO_DATA_WRITE0	0x00
630 #define MDIO_DATA_WRITE1	0x08
631 #define MDIO_DATA_READ		0x04
632 #define MDIO_MASK		0x0f
633 #define MDIO_ENB_IN		0x02
634 
mdio_sync(ioaddr_t addr)635 static void mdio_sync(ioaddr_t addr)
636 {
637     int bits;
638     for (bits = 0; bits < 32; bits++) {
639 	outb_p(MDIO_DATA_WRITE1, addr);
640 	outb_p(MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
641     }
642 }
643 
mdio_read(ioaddr_t addr,int phy_id,int loc)644 static int mdio_read(ioaddr_t addr, int phy_id, int loc)
645 {
646     u_int cmd = (0xf6<<10)|(phy_id<<5)|loc;
647     int i, retval = 0;
648 
649     mdio_sync(addr);
650     for (i = 14; i >= 0; i--) {
651 	int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
652 	outb_p(dat, addr);
653 	outb_p(dat | MDIO_SHIFT_CLK, addr);
654     }
655     for (i = 19; i > 0; i--) {
656 	outb_p(MDIO_ENB_IN, addr);
657 	retval = (retval << 1) | ((inb_p(addr) & MDIO_DATA_READ) != 0);
658 	outb_p(MDIO_ENB_IN | MDIO_SHIFT_CLK, addr);
659     }
660     return (retval>>1) & 0xffff;
661 }
662 
mdio_write(ioaddr_t addr,int phy_id,int loc,int value)663 static void mdio_write(ioaddr_t addr, int phy_id, int loc, int value)
664 {
665     u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
666     int i;
667 
668     mdio_sync(addr);
669     for (i = 31; i >= 0; i--) {
670 	int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
671 	outb_p(dat, addr);
672 	outb_p(dat | MDIO_SHIFT_CLK, addr);
673     }
674     for (i = 1; i >= 0; i--) {
675 	outb_p(MDIO_ENB_IN, addr);
676 	outb_p(MDIO_ENB_IN | MDIO_SHIFT_CLK, addr);
677     }
678 }
679 
680 /*====================================================================*/
681 
axnet_open(struct net_device * dev)682 static int axnet_open(struct net_device *dev)
683 {
684     axnet_dev_t *info = (axnet_dev_t *)dev;
685     dev_link_t *link = &info->link;
686 
687     DEBUG(2, "axnet_open('%s')\n", dev->name);
688 
689     if (!DEV_OK(link))
690 	return -ENODEV;
691 
692     link->open++;
693     MOD_INC_USE_COUNT;
694 
695     request_irq(dev->irq, ei_irq_wrapper, SA_SHIRQ, dev_info, dev);
696 
697     info->link_status = 0x00;
698     info->watchdog.function = &ei_watchdog;
699     info->watchdog.data = (u_long)info;
700     info->watchdog.expires = jiffies + HZ;
701     add_timer(&info->watchdog);
702 
703     return ax_open(dev);
704 } /* axnet_open */
705 
706 /*====================================================================*/
707 
axnet_close(struct net_device * dev)708 static int axnet_close(struct net_device *dev)
709 {
710     axnet_dev_t *info = (axnet_dev_t *)dev;
711     dev_link_t *link = &info->link;
712 
713     DEBUG(2, "axnet_close('%s')\n", dev->name);
714 
715     ax_close(dev);
716     free_irq(dev->irq, dev);
717 
718     link->open--;
719     netif_stop_queue(dev);
720     del_timer(&info->watchdog);
721     if (link->state & DEV_STALE_CONFIG)
722 	mod_timer(&link->release, jiffies + HZ/20);
723 
724     MOD_DEC_USE_COUNT;
725 
726     return 0;
727 } /* axnet_close */
728 
729 /*======================================================================
730 
731     Hard reset the card.  This used to pause for the same period that
732     a 8390 reset command required, but that shouldn't be necessary.
733 
734 ======================================================================*/
735 
axnet_reset_8390(struct net_device * dev)736 static void axnet_reset_8390(struct net_device *dev)
737 {
738     ioaddr_t nic_base = dev->base_addr;
739     int i;
740 
741     ei_status.txing = ei_status.dmaing = 0;
742 
743     outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, nic_base + E8390_CMD);
744 
745     outb(inb(nic_base + AXNET_RESET), nic_base + AXNET_RESET);
746 
747     for (i = 0; i < 100; i++) {
748 	if ((inb_p(nic_base+EN0_ISR) & ENISR_RESET) != 0)
749 	    break;
750 	udelay(100);
751     }
752     outb_p(ENISR_RESET, nic_base + EN0_ISR); /* Ack intr. */
753 
754     if (i == 100)
755 	printk(KERN_ERR "%s: axnet_reset_8390() did not complete.\n",
756 	       dev->name);
757 
758 } /* axnet_reset_8390 */
759 
760 /*====================================================================*/
761 
ei_irq_wrapper(int irq,void * dev_id,struct pt_regs * regs)762 static void ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs)
763 {
764     axnet_dev_t *info = dev_id;
765     info->stale = 0;
766     ax_interrupt(irq, dev_id, regs);
767 }
768 
ei_watchdog(u_long arg)769 static void ei_watchdog(u_long arg)
770 {
771     axnet_dev_t *info = (axnet_dev_t *)(arg);
772     struct net_device *dev = &info->dev;
773     ioaddr_t nic_base = dev->base_addr;
774     ioaddr_t mii_addr = nic_base + AXNET_MII_EEP;
775     u_short link;
776 
777     if (!netif_device_present(dev)) goto reschedule;
778 
779     /* Check for pending interrupt with expired latency timer: with
780        this, we can limp along even if the interrupt is blocked */
781     if (info->stale++ && (inb_p(nic_base + EN0_ISR) & ENISR_ALL)) {
782 	if (!info->fast_poll)
783 	    printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
784 	ei_irq_wrapper(dev->irq, dev, NULL);
785 	info->fast_poll = HZ;
786     }
787     if (info->fast_poll) {
788 	info->fast_poll--;
789 	info->watchdog.expires = jiffies + 1;
790 	add_timer(&info->watchdog);
791 	return;
792     }
793 
794     if (info->phy_id < 0)
795 	goto reschedule;
796     link = mdio_read(mii_addr, info->phy_id, 1);
797     if (!link || (link == 0xffff)) {
798 	printk(KERN_INFO "%s: MII is missing!\n", dev->name);
799 	info->phy_id = -1;
800 	goto reschedule;
801     }
802 
803     link &= 0x0004;
804     if (link != info->link_status) {
805 	u_short p = mdio_read(mii_addr, info->phy_id, 5);
806 	printk(KERN_INFO "%s: %s link beat\n", dev->name,
807 	       (link) ? "found" : "lost");
808 	if (link) {
809 	    info->duplex_flag = (p & 0x0140) ? 0x80 : 0x00;
810 	    if (p)
811 		printk(KERN_INFO "%s: autonegotiation complete: "
812 		       "%sbaseT-%cD selected\n", dev->name,
813 		       ((p & 0x0180) ? "100" : "10"),
814 		       ((p & 0x0140) ? 'F' : 'H'));
815 	    else
816 		printk(KERN_INFO "%s: link partner did not autonegotiate\n",
817 		       dev->name);
818 	    AX88190_init(dev, 1);
819 	}
820 	info->link_status = link;
821     }
822 
823 reschedule:
824     info->watchdog.expires = jiffies + HZ;
825     add_timer(&info->watchdog);
826 }
827 
netdev_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)828 static void netdev_get_drvinfo(struct net_device *dev,
829 			       struct ethtool_drvinfo *info)
830 {
831 	strcpy(info->driver, "axnet_cs");
832 }
833 
834 static struct ethtool_ops netdev_ethtool_ops = {
835 	.get_drvinfo		= netdev_get_drvinfo,
836 };
837 
838 /*====================================================================*/
839 
axnet_ioctl(struct net_device * dev,struct ifreq * rq,int cmd)840 static int axnet_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
841 {
842     axnet_dev_t *info = (axnet_dev_t *)dev;
843     u16 *data = (u16 *)&rq->ifr_data;
844     ioaddr_t mii_addr = dev->base_addr + AXNET_MII_EEP;
845     switch (cmd) {
846     case SIOCGMIIPHY:
847     case SIOCDEVPRIVATE:
848 	data[0] = info->phy_id;
849     case SIOCGMIIREG:		/* Read MII PHY register. */
850     case SIOCDEVPRIVATE+1:
851 	data[3] = mdio_read(mii_addr, data[0], data[1] & 0x1f);
852 	return 0;
853     case SIOCSMIIREG:		/* Write MII PHY register. */
854     case SIOCDEVPRIVATE+2:
855 	if (!capable(CAP_NET_ADMIN))
856 	    return -EPERM;
857 	mdio_write(mii_addr, data[0], data[1] & 0x1f, data[2]);
858 	return 0;
859     }
860     return -EOPNOTSUPP;
861 }
862 
863 /*====================================================================*/
864 
get_8390_hdr(struct net_device * dev,struct e8390_pkt_hdr * hdr,int ring_page)865 static void get_8390_hdr(struct net_device *dev,
866 			 struct e8390_pkt_hdr *hdr,
867 			 int ring_page)
868 {
869     ioaddr_t nic_base = dev->base_addr;
870 
871     outb_p(0, nic_base + EN0_RSARLO);		/* On page boundary */
872     outb_p(ring_page, nic_base + EN0_RSARHI);
873     outb_p(E8390_RREAD+E8390_START, nic_base + AXNET_CMD);
874 
875     insw(nic_base + AXNET_DATAPORT, hdr,
876 	    sizeof(struct e8390_pkt_hdr)>>1);
877     /* Fix for big endian systems */
878     hdr->count = le16_to_cpu(hdr->count);
879 
880 }
881 
882 /*====================================================================*/
883 
block_input(struct net_device * dev,int count,struct sk_buff * skb,int ring_offset)884 static void block_input(struct net_device *dev, int count,
885 			struct sk_buff *skb, int ring_offset)
886 {
887     ioaddr_t nic_base = dev->base_addr;
888     int xfer_count = count;
889     char *buf = skb->data;
890 
891 #ifdef PCMCIA_DEBUG
892     if ((ei_debug > 4) && (count != 4))
893 	printk(KERN_DEBUG "%s: [bi=%d]\n", dev->name, count+4);
894 #endif
895     outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
896     outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
897     outb_p(E8390_RREAD+E8390_START, nic_base + AXNET_CMD);
898 
899     insw(nic_base + AXNET_DATAPORT,buf,count>>1);
900     if (count & 0x01)
901 	buf[count-1] = inb(nic_base + AXNET_DATAPORT), xfer_count++;
902 
903 }
904 
905 /*====================================================================*/
906 
block_output(struct net_device * dev,int count,const u_char * buf,const int start_page)907 static void block_output(struct net_device *dev, int count,
908 			 const u_char *buf, const int start_page)
909 {
910     ioaddr_t nic_base = dev->base_addr;
911 
912 #ifdef PCMCIA_DEBUG
913     if (ei_debug > 4)
914 	printk(KERN_DEBUG "%s: [bo=%d]\n", dev->name, count);
915 #endif
916 
917     /* Round the count up for word writes.  Do we need to do this?
918        What effect will an odd byte count have on the 8390?
919        I should check someday. */
920     if (count & 0x01)
921 	count++;
922 
923     outb_p(0x00, nic_base + EN0_RSARLO);
924     outb_p(start_page, nic_base + EN0_RSARHI);
925     outb_p(E8390_RWRITE+E8390_START, nic_base + AXNET_CMD);
926     outsw(nic_base + AXNET_DATAPORT, buf, count>>1);
927 }
928 
929 /*====================================================================*/
930 
init_axnet_cs(void)931 static int __init init_axnet_cs(void)
932 {
933     servinfo_t serv;
934     DEBUG(0, "%s\n", version);
935     CardServices(GetCardServicesInfo, &serv);
936     if (serv.Revision != CS_RELEASE_CODE) {
937 	printk(KERN_NOTICE "axnet_cs: Card Services release "
938 	       "does not match!\n");
939 	return -EINVAL;
940     }
941     register_pccard_driver(&dev_info, &axnet_attach, &axnet_detach);
942     return 0;
943 }
944 
exit_axnet_cs(void)945 static void __exit exit_axnet_cs(void)
946 {
947     DEBUG(0, "axnet_cs: unloading\n");
948     unregister_pccard_driver(&dev_info);
949     while (dev_list != NULL)
950 	axnet_detach(dev_list);
951 }
952 
953 module_init(init_axnet_cs);
954 module_exit(exit_axnet_cs);
955 
956 /*====================================================================*/
957 
958 /* 8390.c: A general NS8390 ethernet driver core for linux. */
959 /*
960 	Written 1992-94 by Donald Becker.
961 
962 	Copyright 1993 United States Government as represented by the
963 	Director, National Security Agency.
964 
965 	This software may be used and distributed according to the terms
966 	of the GNU General Public License, incorporated herein by reference.
967 
968 	The author may be reached as becker@scyld.com, or C/O
969 	Scyld Computing Corporation
970 	410 Severn Ave., Suite 210
971 	Annapolis MD 21403
972 
973   This is the chip-specific code for many 8390-based ethernet adaptors.
974   This is not a complete driver, it must be combined with board-specific
975   code such as ne.c, wd.c, 3c503.c, etc.
976 
977   Seeing how at least eight drivers use this code, (not counting the
978   PCMCIA ones either) it is easy to break some card by what seems like
979   a simple innocent change. Please contact me or Donald if you think
980   you have found something that needs changing. -- PG
981 
982   Changelog:
983 
984   Paul Gortmaker	: remove set_bit lock, other cleanups.
985   Paul Gortmaker	: add ei_get_8390_hdr() so we can pass skb's to
986 			  ei_block_input() for eth_io_copy_and_sum().
987   Paul Gortmaker	: exchange static int ei_pingpong for a #define,
988 			  also add better Tx error handling.
989   Paul Gortmaker	: rewrite Rx overrun handling as per NS specs.
990   Alexey Kuznetsov	: use the 8390's six bit hash multicast filter.
991   Paul Gortmaker	: tweak ANK's above multicast changes a bit.
992   Paul Gortmaker	: update packet statistics for v2.1.x
993   Alan Cox		: support arbitary stupid port mappings on the
994   			  68K Macintosh. Support >16bit I/O spaces
995   Paul Gortmaker	: add kmod support for auto-loading of the 8390
996 			  module by all drivers that require it.
997   Alan Cox		: Spinlocking work, added 'BUG_83C690'
998   Paul Gortmaker	: Separate out Tx timeout code from Tx path.
999 
1000   Sources:
1001   The National Semiconductor LAN Databook, and the 3Com 3c503 databook.
1002 
1003   */
1004 
1005 static const char *version_8390 =
1006     "8390.c:v1.10cvs 9/23/94 Donald Becker (becker@scyld.com)\n";
1007 
1008 #include <asm/uaccess.h>
1009 #include <asm/bitops.h>
1010 #include <asm/irq.h>
1011 #include <linux/fcntl.h>
1012 #include <linux/in.h>
1013 #include <linux/interrupt.h>
1014 
1015 #include <linux/etherdevice.h>
1016 
1017 #define BUG_83C690
1018 
1019 /* These are the operational function interfaces to board-specific
1020    routines.
1021 	void reset_8390(struct net_device *dev)
1022 		Resets the board associated with DEV, including a hardware reset of
1023 		the 8390.  This is only called when there is a transmit timeout, and
1024 		it is always followed by 8390_init().
1025 	void block_output(struct net_device *dev, int count, const unsigned char *buf,
1026 					  int start_page)
1027 		Write the COUNT bytes of BUF to the packet buffer at START_PAGE.  The
1028 		"page" value uses the 8390's 256-byte pages.
1029 	void get_8390_hdr(struct net_device *dev, struct e8390_hdr *hdr, int ring_page)
1030 		Read the 4 byte, page aligned 8390 header. *If* there is a
1031 		subsequent read, it will be of the rest of the packet.
1032 	void block_input(struct net_device *dev, int count, struct sk_buff *skb, int ring_offset)
1033 		Read COUNT bytes from the packet buffer into the skb data area. Start
1034 		reading from RING_OFFSET, the address as the 8390 sees it.  This will always
1035 		follow the read of the 8390 header.
1036 */
1037 #define ei_reset_8390 (ei_local->reset_8390)
1038 #define ei_block_output (ei_local->block_output)
1039 #define ei_block_input (ei_local->block_input)
1040 #define ei_get_8390_hdr (ei_local->get_8390_hdr)
1041 
1042 /* use 0 for production, 1 for verification, >2 for debug */
1043 #ifndef ei_debug
1044 int ei_debug = 1;
1045 #endif
1046 
1047 /* Index to functions. */
1048 static void ei_tx_intr(struct net_device *dev);
1049 static void ei_tx_err(struct net_device *dev);
1050 static void ei_tx_timeout(struct net_device *dev);
1051 static void ei_receive(struct net_device *dev);
1052 static void ei_rx_overrun(struct net_device *dev);
1053 
1054 /* Routines generic to NS8390-based boards. */
1055 static void NS8390_trigger_send(struct net_device *dev, unsigned int length,
1056 								int start_page);
1057 static void set_multicast_list(struct net_device *dev);
1058 static void do_set_multicast_list(struct net_device *dev);
1059 
1060 /*
1061  *	SMP and the 8390 setup.
1062  *
1063  *	The 8390 isnt exactly designed to be multithreaded on RX/TX. There is
1064  *	a page register that controls bank and packet buffer access. We guard
1065  *	this with ei_local->page_lock. Nobody should assume or set the page other
1066  *	than zero when the lock is not held. Lock holders must restore page 0
1067  *	before unlocking. Even pure readers must take the lock to protect in
1068  *	page 0.
1069  *
1070  *	To make life difficult the chip can also be very slow. We therefore can't
1071  *	just use spinlocks. For the longer lockups we disable the irq the device
1072  *	sits on and hold the lock. We must hold the lock because there is a dual
1073  *	processor case other than interrupts (get stats/set multicast list in
1074  *	parallel with each other and transmit).
1075  *
1076  *	Note: in theory we can just disable the irq on the card _but_ there is
1077  *	a latency on SMP irq delivery. So we can easily go "disable irq" "sync irqs"
1078  *	enter lock, take the queued irq. So we waddle instead of flying.
1079  *
1080  *	Finally by special arrangement for the purpose of being generally
1081  *	annoying the transmit function is called bh atomic. That places
1082  *	restrictions on the user context callers as disable_irq won't save
1083  *	them.
1084  */
1085 
1086 /**
1087  * ax_open - Open/initialize the board.
1088  * @dev: network device to initialize
1089  *
1090  * This routine goes all-out, setting everything
1091  * up anew at each open, even though many of these registers should only
1092  * need to be set once at boot.
1093  */
ax_open(struct net_device * dev)1094 static int ax_open(struct net_device *dev)
1095 {
1096 	unsigned long flags;
1097 	struct ei_device *ei_local = (struct ei_device *) dev->priv;
1098 
1099 	/* This can't happen unless somebody forgot to call axdev_init(). */
1100 	if (ei_local == NULL)
1101 	{
1102 		printk(KERN_EMERG "%s: ax_open passed a non-existent device!\n", dev->name);
1103 		return -ENXIO;
1104 	}
1105 
1106 #ifdef HAVE_TX_TIMEOUT
1107 	/* The card I/O part of the driver (e.g. 3c503) can hook a Tx timeout
1108 	    wrapper that does e.g. media check & then calls ei_tx_timeout. */
1109 	if (dev->tx_timeout == NULL)
1110 		 dev->tx_timeout = ei_tx_timeout;
1111 	if (dev->watchdog_timeo <= 0)
1112 		 dev->watchdog_timeo = TX_TIMEOUT;
1113 #endif
1114 
1115 	/*
1116 	 *	Grab the page lock so we own the register set, then call
1117 	 *	the init function.
1118 	 */
1119 
1120       	spin_lock_irqsave(&ei_local->page_lock, flags);
1121 	AX88190_init(dev, 1);
1122 	/* Set the flag before we drop the lock, That way the IRQ arrives
1123 	   after its set and we get no silly warnings */
1124 	netif_start_queue(dev);
1125       	spin_unlock_irqrestore(&ei_local->page_lock, flags);
1126 	ei_local->irqlock = 0;
1127 	return 0;
1128 }
1129 
1130 #define dev_lock(dev) (((struct ei_device *)(dev)->priv)->page_lock)
1131 
1132 /**
1133  * ax_close - shut down network device
1134  * @dev: network device to close
1135  *
1136  * Opposite of ax_open(). Only used when "ifconfig <devname> down" is done.
1137  */
ax_close(struct net_device * dev)1138 int ax_close(struct net_device *dev)
1139 {
1140 	unsigned long flags;
1141 
1142 	/*
1143 	 *      Hold the page lock during close
1144 	 */
1145 
1146 	spin_lock_irqsave(&dev_lock(dev), flags);
1147 	AX88190_init(dev, 0);
1148 	spin_unlock_irqrestore(&dev_lock(dev), flags);
1149 	netif_stop_queue(dev);
1150 	return 0;
1151 }
1152 
1153 /**
1154  * ei_tx_timeout - handle transmit time out condition
1155  * @dev: network device which has apparently fallen asleep
1156  *
1157  * Called by kernel when device never acknowledges a transmit has
1158  * completed (or failed) - i.e. never posted a Tx related interrupt.
1159  */
1160 
ei_tx_timeout(struct net_device * dev)1161 void ei_tx_timeout(struct net_device *dev)
1162 {
1163 	long e8390_base = dev->base_addr;
1164 	struct ei_device *ei_local = (struct ei_device *) dev->priv;
1165 	int txsr, isr, tickssofar = jiffies - dev->trans_start;
1166 	unsigned long flags;
1167 
1168 	ei_local->stat.tx_errors++;
1169 
1170 	spin_lock_irqsave(&ei_local->page_lock, flags);
1171 	txsr = inb(e8390_base+EN0_TSR);
1172 	isr = inb(e8390_base+EN0_ISR);
1173 	spin_unlock_irqrestore(&ei_local->page_lock, flags);
1174 
1175 	printk(KERN_DEBUG "%s: Tx timed out, %s TSR=%#2x, ISR=%#2x, t=%d.\n",
1176 		dev->name, (txsr & ENTSR_ABT) ? "excess collisions." :
1177 		(isr) ? "lost interrupt?" : "cable problem?", txsr, isr, tickssofar);
1178 
1179 	if (!isr && !ei_local->stat.tx_packets)
1180 	{
1181 		/* The 8390 probably hasn't gotten on the cable yet. */
1182 		ei_local->interface_num ^= 1;   /* Try a different xcvr.  */
1183 	}
1184 
1185 	/* Ugly but a reset can be slow, yet must be protected */
1186 
1187 	disable_irq_nosync(dev->irq);
1188 	spin_lock(&ei_local->page_lock);
1189 
1190 	/* Try to restart the card.  Perhaps the user has fixed something. */
1191 	ei_reset_8390(dev);
1192 	AX88190_init(dev, 1);
1193 
1194 	spin_unlock(&ei_local->page_lock);
1195 	enable_irq(dev->irq);
1196 	netif_wake_queue(dev);
1197 }
1198 
1199 /**
1200  * ei_start_xmit - begin packet transmission
1201  * @skb: packet to be sent
1202  * @dev: network device to which packet is sent
1203  *
1204  * Sends a packet to an 8390 network device.
1205  */
1206 
ei_start_xmit(struct sk_buff * skb,struct net_device * dev)1207 static int ei_start_xmit(struct sk_buff *skb, struct net_device *dev)
1208 {
1209 	long e8390_base = dev->base_addr;
1210 	struct ei_device *ei_local = (struct ei_device *) dev->priv;
1211 	int length, send_length, output_page;
1212 	unsigned long flags;
1213 	u8 packet[ETH_ZLEN];
1214 
1215 	netif_stop_queue(dev);
1216 
1217 	length = skb->len;
1218 
1219 	/* Mask interrupts from the ethercard.
1220 	   SMP: We have to grab the lock here otherwise the IRQ handler
1221 	   on another CPU can flip window and race the IRQ mask set. We end
1222 	   up trashing the mcast filter not disabling irqs if we dont lock */
1223 
1224 	spin_lock_irqsave(&ei_local->page_lock, flags);
1225 	outb_p(0x00, e8390_base + EN0_IMR);
1226 	spin_unlock_irqrestore(&ei_local->page_lock, flags);
1227 
1228 	/*
1229 	 *	Slow phase with lock held.
1230 	 */
1231 
1232 	disable_irq_nosync(dev->irq);
1233 
1234 	spin_lock(&ei_local->page_lock);
1235 
1236 	ei_local->irqlock = 1;
1237 
1238 	send_length = ETH_ZLEN < length ? length : ETH_ZLEN;
1239 
1240 	/*
1241 	 * We have two Tx slots available for use. Find the first free
1242 	 * slot, and then perform some sanity checks. With two Tx bufs,
1243 	 * you get very close to transmitting back-to-back packets. With
1244 	 * only one Tx buf, the transmitter sits idle while you reload the
1245 	 * card, leaving a substantial gap between each transmitted packet.
1246 	 */
1247 
1248 	if (ei_local->tx1 == 0)
1249 	{
1250 		output_page = ei_local->tx_start_page;
1251 		ei_local->tx1 = send_length;
1252 		if (ei_debug  &&  ei_local->tx2 > 0)
1253 			printk(KERN_DEBUG "%s: idle transmitter tx2=%d, lasttx=%d, txing=%d.\n",
1254 				dev->name, ei_local->tx2, ei_local->lasttx, ei_local->txing);
1255 	}
1256 	else if (ei_local->tx2 == 0)
1257 	{
1258 		output_page = ei_local->tx_start_page + TX_1X_PAGES;
1259 		ei_local->tx2 = send_length;
1260 		if (ei_debug  &&  ei_local->tx1 > 0)
1261 			printk(KERN_DEBUG "%s: idle transmitter, tx1=%d, lasttx=%d, txing=%d.\n",
1262 				dev->name, ei_local->tx1, ei_local->lasttx, ei_local->txing);
1263 	}
1264 	else
1265 	{	/* We should never get here. */
1266 		if (ei_debug)
1267 			printk(KERN_DEBUG "%s: No Tx buffers free! tx1=%d tx2=%d last=%d\n",
1268 				dev->name, ei_local->tx1, ei_local->tx2, ei_local->lasttx);
1269 		ei_local->irqlock = 0;
1270 		netif_stop_queue(dev);
1271 		outb_p(ENISR_ALL, e8390_base + EN0_IMR);
1272 		spin_unlock(&ei_local->page_lock);
1273 		enable_irq(dev->irq);
1274 		ei_local->stat.tx_errors++;
1275 		return 1;
1276 	}
1277 
1278 	/*
1279 	 * Okay, now upload the packet and trigger a send if the transmitter
1280 	 * isn't already sending. If it is busy, the interrupt handler will
1281 	 * trigger the send later, upon receiving a Tx done interrupt.
1282 	 */
1283 
1284 	if(length == skb->len)
1285 		ei_block_output(dev, length, skb->data, output_page);
1286 	else
1287 	{
1288 		memset(packet, 0, ETH_ZLEN);
1289 		memcpy(packet, skb->data, skb->len);
1290 		ei_block_output(dev, length, packet, output_page);
1291 	}
1292 
1293 	if (! ei_local->txing)
1294 	{
1295 		ei_local->txing = 1;
1296 		NS8390_trigger_send(dev, send_length, output_page);
1297 		dev->trans_start = jiffies;
1298 		if (output_page == ei_local->tx_start_page)
1299 		{
1300 			ei_local->tx1 = -1;
1301 			ei_local->lasttx = -1;
1302 		}
1303 		else
1304 		{
1305 			ei_local->tx2 = -1;
1306 			ei_local->lasttx = -2;
1307 		}
1308 	}
1309 	else ei_local->txqueue++;
1310 
1311 	if (ei_local->tx1  &&  ei_local->tx2)
1312 		netif_stop_queue(dev);
1313 	else
1314 		netif_start_queue(dev);
1315 
1316 	/* Turn 8390 interrupts back on. */
1317 	ei_local->irqlock = 0;
1318 	outb_p(ENISR_ALL, e8390_base + EN0_IMR);
1319 
1320 	spin_unlock(&ei_local->page_lock);
1321 	enable_irq(dev->irq);
1322 
1323 	dev_kfree_skb (skb);
1324 	ei_local->stat.tx_bytes += send_length;
1325 
1326 	return 0;
1327 }
1328 
1329 /**
1330  * ax_interrupt - handle the interrupts from an 8390
1331  * @irq: interrupt number
1332  * @dev_id: a pointer to the net_device
1333  * @regs: unused
1334  *
1335  * Handle the ether interface interrupts. We pull packets from
1336  * the 8390 via the card specific functions and fire them at the networking
1337  * stack. We also handle transmit completions and wake the transmit path if
1338  * neccessary. We also update the counters and do other housekeeping as
1339  * needed.
1340  */
1341 
ax_interrupt(int irq,void * dev_id,struct pt_regs * regs)1342 static void ax_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1343 {
1344 	struct net_device *dev = dev_id;
1345 	long e8390_base;
1346 	int interrupts, nr_serviced = 0, i;
1347 	struct ei_device *ei_local;
1348 
1349 	if (dev == NULL)
1350 	{
1351 		printk ("net_interrupt(): irq %d for unknown device.\n", irq);
1352 		return;
1353 	}
1354 
1355 	e8390_base = dev->base_addr;
1356 	ei_local = (struct ei_device *) dev->priv;
1357 
1358 	/*
1359 	 *	Protect the irq test too.
1360 	 */
1361 
1362 	spin_lock(&ei_local->page_lock);
1363 
1364 	if (ei_local->irqlock)
1365 	{
1366 #if 1 /* This might just be an interrupt for a PCI device sharing this line */
1367 		/* The "irqlock" check is only for testing. */
1368 		printk(ei_local->irqlock
1369 			   ? "%s: Interrupted while interrupts are masked! isr=%#2x imr=%#2x.\n"
1370 			   : "%s: Reentering the interrupt handler! isr=%#2x imr=%#2x.\n",
1371 			   dev->name, inb_p(e8390_base + EN0_ISR),
1372 			   inb_p(e8390_base + EN0_IMR));
1373 #endif
1374 		spin_unlock(&ei_local->page_lock);
1375 		return;
1376 	}
1377 
1378 	if (ei_debug > 3)
1379 		printk(KERN_DEBUG "%s: interrupt(isr=%#2.2x).\n", dev->name,
1380 			   inb_p(e8390_base + EN0_ISR));
1381 
1382 	outb_p(0x00, e8390_base + EN0_ISR);
1383 	ei_local->irqlock = 1;
1384 
1385 	/* !!Assumption!! -- we stay in page 0.	 Don't break this. */
1386 	while ((interrupts = inb_p(e8390_base + EN0_ISR)) != 0
1387 		   && ++nr_serviced < MAX_SERVICE)
1388 	{
1389 		if (!netif_running(dev) || (interrupts == 0xff)) {
1390 			if (ei_debug > 1)
1391 				printk(KERN_WARNING "%s: interrupt from stopped card\n", dev->name);
1392 			outb_p(interrupts, e8390_base + EN0_ISR);
1393 			interrupts = 0;
1394 			break;
1395 		}
1396 		/* AX88190 bug fix. */
1397 		outb_p(interrupts, e8390_base + EN0_ISR);
1398 		for (i = 0; i < 10; i++) {
1399 			if (!(inb(e8390_base + EN0_ISR) & interrupts))
1400 				break;
1401 			outb_p(0, e8390_base + EN0_ISR);
1402 			outb_p(interrupts, e8390_base + EN0_ISR);
1403 		}
1404 		if (interrupts & ENISR_OVER)
1405 			ei_rx_overrun(dev);
1406 		else if (interrupts & (ENISR_RX+ENISR_RX_ERR))
1407 		{
1408 			/* Got a good (?) packet. */
1409 			ei_receive(dev);
1410 		}
1411 		/* Push the next to-transmit packet through. */
1412 		if (interrupts & ENISR_TX)
1413 			ei_tx_intr(dev);
1414 		else if (interrupts & ENISR_TX_ERR)
1415 			ei_tx_err(dev);
1416 
1417 		if (interrupts & ENISR_COUNTERS)
1418 		{
1419 			ei_local->stat.rx_frame_errors += inb_p(e8390_base + EN0_COUNTER0);
1420 			ei_local->stat.rx_crc_errors   += inb_p(e8390_base + EN0_COUNTER1);
1421 			ei_local->stat.rx_missed_errors+= inb_p(e8390_base + EN0_COUNTER2);
1422 		}
1423 	}
1424 
1425 	if (interrupts && ei_debug)
1426 	{
1427 		if (nr_serviced >= MAX_SERVICE)
1428 		{
1429 			/* 0xFF is valid for a card removal */
1430 			if(interrupts!=0xFF)
1431 				printk(KERN_WARNING "%s: Too much work at interrupt, status %#2.2x\n",
1432 				   dev->name, interrupts);
1433 			outb_p(ENISR_ALL, e8390_base + EN0_ISR); /* Ack. most intrs. */
1434 		} else {
1435 			printk(KERN_WARNING "%s: unknown interrupt %#2x\n", dev->name, interrupts);
1436 			outb_p(0xff, e8390_base + EN0_ISR); /* Ack. all intrs. */
1437 		}
1438 	}
1439 
1440 	/* Turn 8390 interrupts back on. */
1441 	ei_local->irqlock = 0;
1442 	outb_p(ENISR_ALL, e8390_base + EN0_IMR);
1443 
1444 	spin_unlock(&ei_local->page_lock);
1445 	return;
1446 }
1447 
1448 /**
1449  * ei_tx_err - handle transmitter error
1450  * @dev: network device which threw the exception
1451  *
1452  * A transmitter error has happened. Most likely excess collisions (which
1453  * is a fairly normal condition). If the error is one where the Tx will
1454  * have been aborted, we try and send another one right away, instead of
1455  * letting the failed packet sit and collect dust in the Tx buffer. This
1456  * is a much better solution as it avoids kernel based Tx timeouts, and
1457  * an unnecessary card reset.
1458  *
1459  * Called with lock held.
1460  */
1461 
ei_tx_err(struct net_device * dev)1462 static void ei_tx_err(struct net_device *dev)
1463 {
1464 	long e8390_base = dev->base_addr;
1465 	struct ei_device *ei_local = (struct ei_device *) dev->priv;
1466 	unsigned char txsr = inb_p(e8390_base+EN0_TSR);
1467 	unsigned char tx_was_aborted = txsr & (ENTSR_ABT+ENTSR_FU);
1468 
1469 #ifdef VERBOSE_ERROR_DUMP
1470 	printk(KERN_DEBUG "%s: transmitter error (%#2x): ", dev->name, txsr);
1471 	if (txsr & ENTSR_ABT)
1472 		printk("excess-collisions ");
1473 	if (txsr & ENTSR_ND)
1474 		printk("non-deferral ");
1475 	if (txsr & ENTSR_CRS)
1476 		printk("lost-carrier ");
1477 	if (txsr & ENTSR_FU)
1478 		printk("FIFO-underrun ");
1479 	if (txsr & ENTSR_CDH)
1480 		printk("lost-heartbeat ");
1481 	printk("\n");
1482 #endif
1483 
1484 	if (tx_was_aborted)
1485 		ei_tx_intr(dev);
1486 	else
1487 	{
1488 		ei_local->stat.tx_errors++;
1489 		if (txsr & ENTSR_CRS) ei_local->stat.tx_carrier_errors++;
1490 		if (txsr & ENTSR_CDH) ei_local->stat.tx_heartbeat_errors++;
1491 		if (txsr & ENTSR_OWC) ei_local->stat.tx_window_errors++;
1492 	}
1493 }
1494 
1495 /**
1496  * ei_tx_intr - transmit interrupt handler
1497  * @dev: network device for which tx intr is handled
1498  *
1499  * We have finished a transmit: check for errors and then trigger the next
1500  * packet to be sent. Called with lock held.
1501  */
1502 
ei_tx_intr(struct net_device * dev)1503 static void ei_tx_intr(struct net_device *dev)
1504 {
1505 	long e8390_base = dev->base_addr;
1506 	struct ei_device *ei_local = (struct ei_device *) dev->priv;
1507 	int status = inb(e8390_base + EN0_TSR);
1508 
1509 	/*
1510 	 * There are two Tx buffers, see which one finished, and trigger
1511 	 * the send of another one if it exists.
1512 	 */
1513 	ei_local->txqueue--;
1514 
1515 	if (ei_local->tx1 < 0)
1516 	{
1517 		if (ei_local->lasttx != 1 && ei_local->lasttx != -1)
1518 			printk(KERN_ERR "%s: bogus last_tx_buffer %d, tx1=%d.\n",
1519 				ei_local->name, ei_local->lasttx, ei_local->tx1);
1520 		ei_local->tx1 = 0;
1521 		if (ei_local->tx2 > 0)
1522 		{
1523 			ei_local->txing = 1;
1524 			NS8390_trigger_send(dev, ei_local->tx2, ei_local->tx_start_page + 6);
1525 			dev->trans_start = jiffies;
1526 			ei_local->tx2 = -1,
1527 			ei_local->lasttx = 2;
1528 		}
1529 		else ei_local->lasttx = 20, ei_local->txing = 0;
1530 	}
1531 	else if (ei_local->tx2 < 0)
1532 	{
1533 		if (ei_local->lasttx != 2  &&  ei_local->lasttx != -2)
1534 			printk("%s: bogus last_tx_buffer %d, tx2=%d.\n",
1535 				ei_local->name, ei_local->lasttx, ei_local->tx2);
1536 		ei_local->tx2 = 0;
1537 		if (ei_local->tx1 > 0)
1538 		{
1539 			ei_local->txing = 1;
1540 			NS8390_trigger_send(dev, ei_local->tx1, ei_local->tx_start_page);
1541 			dev->trans_start = jiffies;
1542 			ei_local->tx1 = -1;
1543 			ei_local->lasttx = 1;
1544 		}
1545 		else
1546 			ei_local->lasttx = 10, ei_local->txing = 0;
1547 	}
1548 //	else printk(KERN_WARNING "%s: unexpected TX-done interrupt, lasttx=%d.\n",
1549 //			dev->name, ei_local->lasttx);
1550 
1551 	/* Minimize Tx latency: update the statistics after we restart TXing. */
1552 	if (status & ENTSR_COL)
1553 		ei_local->stat.collisions++;
1554 	if (status & ENTSR_PTX)
1555 		ei_local->stat.tx_packets++;
1556 	else
1557 	{
1558 		ei_local->stat.tx_errors++;
1559 		if (status & ENTSR_ABT)
1560 		{
1561 			ei_local->stat.tx_aborted_errors++;
1562 			ei_local->stat.collisions += 16;
1563 		}
1564 		if (status & ENTSR_CRS)
1565 			ei_local->stat.tx_carrier_errors++;
1566 		if (status & ENTSR_FU)
1567 			ei_local->stat.tx_fifo_errors++;
1568 		if (status & ENTSR_CDH)
1569 			ei_local->stat.tx_heartbeat_errors++;
1570 		if (status & ENTSR_OWC)
1571 			ei_local->stat.tx_window_errors++;
1572 	}
1573 	netif_wake_queue(dev);
1574 }
1575 
1576 /**
1577  * ei_receive - receive some packets
1578  * @dev: network device with which receive will be run
1579  *
1580  * We have a good packet(s), get it/them out of the buffers.
1581  * Called with lock held.
1582  */
1583 
ei_receive(struct net_device * dev)1584 static void ei_receive(struct net_device *dev)
1585 {
1586 	long e8390_base = dev->base_addr;
1587 	struct ei_device *ei_local = (struct ei_device *) dev->priv;
1588 	unsigned char rxing_page, this_frame, next_frame;
1589 	unsigned short current_offset;
1590 	int rx_pkt_count = 0;
1591 	struct e8390_pkt_hdr rx_frame;
1592 
1593 	while (++rx_pkt_count < 10)
1594 	{
1595 		int pkt_len, pkt_stat;
1596 
1597 		/* Get the rx page (incoming packet pointer). */
1598 		rxing_page = inb_p(e8390_base + EN1_CURPAG -1);
1599 
1600 		/* Remove one frame from the ring.  Boundary is always a page behind. */
1601 		this_frame = inb_p(e8390_base + EN0_BOUNDARY) + 1;
1602 		if (this_frame >= ei_local->stop_page)
1603 			this_frame = ei_local->rx_start_page;
1604 
1605 		/* Someday we'll omit the previous, iff we never get this message.
1606 		   (There is at least one clone claimed to have a problem.)
1607 
1608 		   Keep quiet if it looks like a card removal. One problem here
1609 		   is that some clones crash in roughly the same way.
1610 		 */
1611 		if (ei_debug > 0  &&  this_frame != ei_local->current_page && (this_frame!=0x0 || rxing_page!=0xFF))
1612 			printk(KERN_ERR "%s: mismatched read page pointers %2x vs %2x.\n",
1613 				   dev->name, this_frame, ei_local->current_page);
1614 
1615 		if (this_frame == rxing_page)	/* Read all the frames? */
1616 			break;				/* Done for now */
1617 
1618 		current_offset = this_frame << 8;
1619 		ei_get_8390_hdr(dev, &rx_frame, this_frame);
1620 
1621 		pkt_len = rx_frame.count - sizeof(struct e8390_pkt_hdr);
1622 		pkt_stat = rx_frame.status;
1623 
1624 		next_frame = this_frame + 1 + ((pkt_len+4)>>8);
1625 
1626 		if (pkt_len < 60  ||  pkt_len > 1518)
1627 		{
1628 			if (ei_debug)
1629 				printk(KERN_DEBUG "%s: bogus packet size: %d, status=%#2x nxpg=%#2x.\n",
1630 					   dev->name, rx_frame.count, rx_frame.status,
1631 					   rx_frame.next);
1632 			ei_local->stat.rx_errors++;
1633 			ei_local->stat.rx_length_errors++;
1634 		}
1635 		 else if ((pkt_stat & 0x0F) == ENRSR_RXOK)
1636 		{
1637 			struct sk_buff *skb;
1638 
1639 			skb = dev_alloc_skb(pkt_len+2);
1640 			if (skb == NULL)
1641 			{
1642 				if (ei_debug > 1)
1643 					printk(KERN_DEBUG "%s: Couldn't allocate a sk_buff of size %d.\n",
1644 						   dev->name, pkt_len);
1645 				ei_local->stat.rx_dropped++;
1646 				break;
1647 			}
1648 			else
1649 			{
1650 				skb_reserve(skb,2);	/* IP headers on 16 byte boundaries */
1651 				skb->dev = dev;
1652 				skb_put(skb, pkt_len);	/* Make room */
1653 				ei_block_input(dev, pkt_len, skb, current_offset + sizeof(rx_frame));
1654 				skb->protocol=eth_type_trans(skb,dev);
1655 				netif_rx(skb);
1656 				dev->last_rx = jiffies;
1657 				ei_local->stat.rx_packets++;
1658 				ei_local->stat.rx_bytes += pkt_len;
1659 				if (pkt_stat & ENRSR_PHY)
1660 					ei_local->stat.multicast++;
1661 			}
1662 		}
1663 		else
1664 		{
1665 			if (ei_debug)
1666 				printk(KERN_DEBUG "%s: bogus packet: status=%#2x nxpg=%#2x size=%d\n",
1667 					   dev->name, rx_frame.status, rx_frame.next,
1668 					   rx_frame.count);
1669 			ei_local->stat.rx_errors++;
1670 			/* NB: The NIC counts CRC, frame and missed errors. */
1671 			if (pkt_stat & ENRSR_FO)
1672 				ei_local->stat.rx_fifo_errors++;
1673 		}
1674 		next_frame = rx_frame.next;
1675 
1676 		/* This _should_ never happen: it's here for avoiding bad clones. */
1677 		if (next_frame >= ei_local->stop_page) {
1678 			printk("%s: next frame inconsistency, %#2x\n", dev->name,
1679 				   next_frame);
1680 			next_frame = ei_local->rx_start_page;
1681 		}
1682 		ei_local->current_page = next_frame;
1683 		outb_p(next_frame-1, e8390_base+EN0_BOUNDARY);
1684 	}
1685 
1686 	return;
1687 }
1688 
1689 /**
1690  * ei_rx_overrun - handle receiver overrun
1691  * @dev: network device which threw exception
1692  *
1693  * We have a receiver overrun: we have to kick the 8390 to get it started
1694  * again. Problem is that you have to kick it exactly as NS prescribes in
1695  * the updated datasheets, or "the NIC may act in an unpredictable manner."
1696  * This includes causing "the NIC to defer indefinitely when it is stopped
1697  * on a busy network."  Ugh.
1698  * Called with lock held. Don't call this with the interrupts off or your
1699  * computer will hate you - it takes 10ms or so.
1700  */
1701 
ei_rx_overrun(struct net_device * dev)1702 static void ei_rx_overrun(struct net_device *dev)
1703 {
1704 	axnet_dev_t *info = (axnet_dev_t *)dev;
1705 	long e8390_base = dev->base_addr;
1706 	unsigned char was_txing, must_resend = 0;
1707 	struct ei_device *ei_local = (struct ei_device *) dev->priv;
1708 
1709 	/*
1710 	 * Record whether a Tx was in progress and then issue the
1711 	 * stop command.
1712 	 */
1713 	was_txing = inb_p(e8390_base+E8390_CMD) & E8390_TRANS;
1714 	outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD);
1715 
1716 	if (ei_debug > 1)
1717 		printk(KERN_DEBUG "%s: Receiver overrun.\n", dev->name);
1718 	ei_local->stat.rx_over_errors++;
1719 
1720 	/*
1721 	 * Wait a full Tx time (1.2ms) + some guard time, NS says 1.6ms total.
1722 	 * Early datasheets said to poll the reset bit, but now they say that
1723 	 * it "is not a reliable indicator and subsequently should be ignored."
1724 	 * We wait at least 10ms.
1725 	 */
1726 
1727 	mdelay(10);
1728 
1729 	/*
1730 	 * Reset RBCR[01] back to zero as per magic incantation.
1731 	 */
1732 	outb_p(0x00, e8390_base+EN0_RCNTLO);
1733 	outb_p(0x00, e8390_base+EN0_RCNTHI);
1734 
1735 	/*
1736 	 * See if any Tx was interrupted or not. According to NS, this
1737 	 * step is vital, and skipping it will cause no end of havoc.
1738 	 */
1739 
1740 	if (was_txing)
1741 	{
1742 		unsigned char tx_completed = inb_p(e8390_base+EN0_ISR) & (ENISR_TX+ENISR_TX_ERR);
1743 		if (!tx_completed)
1744 			must_resend = 1;
1745 	}
1746 
1747 	/*
1748 	 * Have to enter loopback mode and then restart the NIC before
1749 	 * you are allowed to slurp packets up off the ring.
1750 	 */
1751 	outb_p(E8390_TXOFF, e8390_base + EN0_TXCR);
1752 	outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START, e8390_base + E8390_CMD);
1753 
1754 	/*
1755 	 * Clear the Rx ring of all the debris, and ack the interrupt.
1756 	 */
1757 	ei_receive(dev);
1758 
1759 	/*
1760 	 * Leave loopback mode, and resend any packet that got stopped.
1761 	 */
1762 	outb_p(E8390_TXCONFIG | info->duplex_flag, e8390_base + EN0_TXCR);
1763 	if (must_resend)
1764     		outb_p(E8390_NODMA + E8390_PAGE0 + E8390_START + E8390_TRANS, e8390_base + E8390_CMD);
1765 }
1766 
1767 /*
1768  *	Collect the stats. This is called unlocked and from several contexts.
1769  */
1770 
get_stats(struct net_device * dev)1771 static struct net_device_stats *get_stats(struct net_device *dev)
1772 {
1773 	long ioaddr = dev->base_addr;
1774 	struct ei_device *ei_local = (struct ei_device *) dev->priv;
1775 	unsigned long flags;
1776 
1777 	/* If the card is stopped, just return the present stats. */
1778 	if (!netif_running(dev))
1779 		return &ei_local->stat;
1780 
1781 	spin_lock_irqsave(&ei_local->page_lock,flags);
1782 	/* Read the counter registers, assuming we are in page 0. */
1783 	ei_local->stat.rx_frame_errors += inb_p(ioaddr + EN0_COUNTER0);
1784 	ei_local->stat.rx_crc_errors   += inb_p(ioaddr + EN0_COUNTER1);
1785 	ei_local->stat.rx_missed_errors+= inb_p(ioaddr + EN0_COUNTER2);
1786 	spin_unlock_irqrestore(&ei_local->page_lock, flags);
1787 
1788 	return &ei_local->stat;
1789 }
1790 
1791 /**
1792  * do_set_multicast_list - set/clear multicast filter
1793  * @dev: net device for which multicast filter is adjusted
1794  *
1795  *	Set or clear the multicast filter for this adaptor. May be called
1796  *	from a BH in 2.1.x. Must be called with lock held.
1797  */
1798 
do_set_multicast_list(struct net_device * dev)1799 static void do_set_multicast_list(struct net_device *dev)
1800 {
1801 	long e8390_base = dev->base_addr;
1802 
1803   	if(dev->flags&IFF_PROMISC)
1804   		outb_p(E8390_RXCONFIG | 0x58, e8390_base + EN0_RXCR);
1805 	else if(dev->flags&IFF_ALLMULTI || dev->mc_list)
1806   		outb_p(E8390_RXCONFIG | 0x48, e8390_base + EN0_RXCR);
1807   	else
1808   		outb_p(E8390_RXCONFIG | 0x40, e8390_base + EN0_RXCR);
1809 }
1810 
1811 /*
1812  *	Called without lock held. This is invoked from user context and may
1813  *	be parallel to just about everything else. Its also fairly quick and
1814  *	not called too often. Must protect against both bh and irq users
1815  */
1816 
set_multicast_list(struct net_device * dev)1817 static void set_multicast_list(struct net_device *dev)
1818 {
1819 	unsigned long flags;
1820 
1821 	spin_lock_irqsave(&dev_lock(dev), flags);
1822 	do_set_multicast_list(dev);
1823 	spin_unlock_irqrestore(&dev_lock(dev), flags);
1824 }
1825 
1826 /**
1827  * axdev_init - init rest of 8390 device struct
1828  * @dev: network device structure to init
1829  *
1830  * Initialize the rest of the 8390 device structure.  Do NOT __init
1831  * this, as it is used by 8390 based modular drivers too.
1832  */
1833 
axdev_init(struct net_device * dev)1834 static int axdev_init(struct net_device *dev)
1835 {
1836 	if (ei_debug > 1)
1837 		printk(version_8390);
1838 
1839 	if (dev->priv == NULL)
1840 	{
1841 		struct ei_device *ei_local;
1842 
1843 		dev->priv = kmalloc(sizeof(struct ei_device), GFP_KERNEL);
1844 		if (dev->priv == NULL)
1845 			return -ENOMEM;
1846 		memset(dev->priv, 0, sizeof(struct ei_device));
1847 		ei_local = (struct ei_device *)dev->priv;
1848 		spin_lock_init(&ei_local->page_lock);
1849 	}
1850 
1851 	dev->hard_start_xmit = &ei_start_xmit;
1852 	dev->get_stats	= get_stats;
1853 	dev->set_multicast_list = &set_multicast_list;
1854 
1855 	ether_setup(dev);
1856 
1857 	return 0;
1858 }
1859 
1860 /* This page of functions should be 8390 generic */
1861 /* Follow National Semi's recommendations for initializing the "NIC". */
1862 
1863 /**
1864  * AX88190_init - initialize 8390 hardware
1865  * @dev: network device to initialize
1866  * @startp: boolean.  non-zero value to initiate chip processing
1867  *
1868  *	Must be called with lock held.
1869  */
1870 
AX88190_init(struct net_device * dev,int startp)1871 static void AX88190_init(struct net_device *dev, int startp)
1872 {
1873 	axnet_dev_t *info = (axnet_dev_t *)dev;
1874 	long e8390_base = dev->base_addr;
1875 	struct ei_device *ei_local = (struct ei_device *) dev->priv;
1876 	int i;
1877 	int endcfg = ei_local->word16 ? (0x48 | ENDCFG_WTS) : 0x48;
1878 
1879 	if(sizeof(struct e8390_pkt_hdr)!=4)
1880     		panic("8390.c: header struct mispacked\n");
1881 	/* Follow National Semi's recommendations for initing the DP83902. */
1882 	outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD); /* 0x21 */
1883 	outb_p(endcfg, e8390_base + EN0_DCFG);	/* 0x48 or 0x49 */
1884 	/* Clear the remote byte count registers. */
1885 	outb_p(0x00,  e8390_base + EN0_RCNTLO);
1886 	outb_p(0x00,  e8390_base + EN0_RCNTHI);
1887 	/* Set to monitor and loopback mode -- this is vital!. */
1888 	outb_p(E8390_RXOFF|0x40, e8390_base + EN0_RXCR); /* 0x60 */
1889 	outb_p(E8390_TXOFF, e8390_base + EN0_TXCR); /* 0x02 */
1890 	/* Set the transmit page and receive ring. */
1891 	outb_p(ei_local->tx_start_page, e8390_base + EN0_TPSR);
1892 	ei_local->tx1 = ei_local->tx2 = 0;
1893 	outb_p(ei_local->rx_start_page, e8390_base + EN0_STARTPG);
1894 	outb_p(ei_local->stop_page-1, e8390_base + EN0_BOUNDARY);	/* 3c503 says 0x3f,NS0x26*/
1895 	ei_local->current_page = ei_local->rx_start_page;		/* assert boundary+1 */
1896 	outb_p(ei_local->stop_page, e8390_base + EN0_STOPPG);
1897 	/* Clear the pending interrupts and mask. */
1898 	outb_p(0xFF, e8390_base + EN0_ISR);
1899 	outb_p(0x00,  e8390_base + EN0_IMR);
1900 
1901 	/* Copy the station address into the DS8390 registers. */
1902 
1903 	outb_p(E8390_NODMA + E8390_PAGE1 + E8390_STOP, e8390_base+E8390_CMD); /* 0x61 */
1904 	for(i = 0; i < 6; i++)
1905 	{
1906 		outb_p(dev->dev_addr[i], e8390_base + EN1_PHYS_SHIFT(i));
1907 		if(inb_p(e8390_base + EN1_PHYS_SHIFT(i))!=dev->dev_addr[i])
1908 			printk(KERN_ERR "Hw. address read/write mismap %d\n",i);
1909 	}
1910 	/*
1911 	 * Initialize the multicast list to accept-all.  If we enable multicast
1912 	 * the higher levels can do the filtering.
1913 	 */
1914 	for (i = 0; i < 8; i++)
1915 		outb_p(0xff, e8390_base + EN1_MULT + i);
1916 
1917 	outb_p(ei_local->rx_start_page, e8390_base + EN1_CURPAG);
1918 	outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, e8390_base+E8390_CMD);
1919 
1920 	netif_start_queue(dev);
1921 	ei_local->tx1 = ei_local->tx2 = 0;
1922 	ei_local->txing = 0;
1923 
1924 	if (startp)
1925 	{
1926 		outb_p(0xff,  e8390_base + EN0_ISR);
1927 		outb_p(ENISR_ALL,  e8390_base + EN0_IMR);
1928 		outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, e8390_base+E8390_CMD);
1929 		outb_p(E8390_TXCONFIG | info->duplex_flag,
1930 		       e8390_base + EN0_TXCR); /* xmit on. */
1931 		/* 3c503 TechMan says rxconfig only after the NIC is started. */
1932 		outb_p(E8390_RXCONFIG | 0x40, e8390_base + EN0_RXCR); /* rx on, */
1933 		do_set_multicast_list(dev);	/* (re)load the mcast table */
1934 	}
1935 }
1936 
1937 /* Trigger a transmit start, assuming the length is valid.
1938    Always called with the page lock held */
1939 
NS8390_trigger_send(struct net_device * dev,unsigned int length,int start_page)1940 static void NS8390_trigger_send(struct net_device *dev, unsigned int length,
1941 								int start_page)
1942 {
1943 	long e8390_base = dev->base_addr;
1944  	struct ei_device *ei_local __attribute((unused)) = (struct ei_device *) dev->priv;
1945 
1946 	if (inb_p(e8390_base) & E8390_TRANS)
1947 	{
1948 		printk(KERN_WARNING "%s: trigger_send() called with the transmitter busy.\n",
1949 			dev->name);
1950 		return;
1951 	}
1952 	outb_p(length & 0xff, e8390_base + EN0_TCNTLO);
1953 	outb_p(length >> 8, e8390_base + EN0_TCNTHI);
1954 	outb_p(start_page, e8390_base + EN0_TPSR);
1955 	outb_p(E8390_NODMA+E8390_TRANS+E8390_START, e8390_base+E8390_CMD);
1956 }
1957