[SCSI] aacraid: remove duplicate io callback code
[wandboard.git] / drivers / net / znet.c
blob3ac047bc727d0f3c4cce4c68bf3336c19586289e
1 /* znet.c: An Zenith Z-Note ethernet driver for linux. */
3 /*
4 Written by Donald Becker.
6 The author may be reached as becker@scyld.com.
7 This driver is based on the Linux skeleton driver. The copyright of the
8 skeleton driver is held by the United States Government, as represented
9 by DIRNSA, and it is released under the GPL.
11 Thanks to Mike Hollick for alpha testing and suggestions.
13 References:
14 The Crynwr packet driver.
16 "82593 CSMA/CD Core LAN Controller" Intel datasheet, 1992
17 Intel Microcommunications Databook, Vol. 1, 1990.
18 As usual with Intel, the documentation is incomplete and inaccurate.
19 I had to read the Crynwr packet driver to figure out how to actually
20 use the i82593, and guess at what register bits matched the loosely
21 related i82586.
23 Theory of Operation
25 The i82593 used in the Zenith Z-Note series operates using two(!) slave
26 DMA channels, one interrupt, and one 8-bit I/O port.
28 While there several ways to configure '593 DMA system, I chose the one
29 that seemed commensurate with the highest system performance in the face
30 of moderate interrupt latency: Both DMA channels are configured as
31 recirculating ring buffers, with one channel (#0) dedicated to Rx and
32 the other channel (#1) to Tx and configuration. (Note that this is
33 different than the Crynwr driver, where the Tx DMA channel is initialized
34 before each operation. That approach simplifies operation and Tx error
35 recovery, but requires additional I/O in normal operation and precludes
36 transmit buffer chaining.)
38 Both rings are set to 8192 bytes using {TX,RX}_RING_SIZE. This provides
39 a reasonable ring size for Rx, while simplifying DMA buffer allocation --
40 DMA buffers must not cross a 128K boundary. (In truth the size selection
41 was influenced by my lack of '593 documentation. I thus was constrained
42 to use the Crynwr '593 initialization table, which sets the Rx ring size
43 to 8K.)
45 Despite my usual low opinion about Intel-designed parts, I must admit
46 that the bulk data handling of the i82593 is a good design for
47 an integrated system, like a laptop, where using two slave DMA channels
48 doesn't pose a problem. I still take issue with using only a single I/O
49 port. In the same controlled environment there are essentially no
50 limitations on I/O space, and using multiple locations would eliminate
51 the need for multiple operations when looking at status registers,
52 setting the Rx ring boundary, or switching to promiscuous mode.
54 I also question Zenith's selection of the '593: one of the advertised
55 advantages of earlier Intel parts was that if you figured out the magic
56 initialization incantation you could use the same part on many different
57 network types. Zenith's use of the "FriendlyNet" (sic) connector rather
58 than an on-board transceiver leads me to believe that they were planning
59 to take advantage of this. But, uhmmm, the '593 omits all but ethernet
60 functionality from the serial subsystem.
63 /* 10/2002
65 o Resurected for Linux 2.5+ by Marc Zyngier <maz@wild-wind.fr.eu.org> :
67 - Removed strange DMA snooping in znet_sent_packet, which lead to
68 TX buffer corruption on my laptop.
69 - Use init_etherdev stuff.
70 - Use kmalloc-ed DMA buffers.
71 - Use as few global variables as possible.
72 - Use proper resources management.
73 - Use wireless/i82593.h as much as possible (structure, constants)
74 - Compiles as module or build-in.
75 - Now survives unplugging/replugging cable.
77 Some code was taken from wavelan_cs.
79 Tested on a vintage Zenith Z-Note 433Lnp+. Probably broken on
80 anything else. Testers (and detailed bug reports) are welcome :-).
82 o TODO :
84 - Properly handle multicast
85 - Understand why some traffic patterns add a 1s latency...
88 #include <linux/config.h>
89 #include <linux/module.h>
90 #include <linux/kernel.h>
91 #include <linux/string.h>
92 #include <linux/errno.h>
93 #include <linux/interrupt.h>
94 #include <linux/ioport.h>
95 #include <linux/init.h>
96 #include <linux/delay.h>
97 #include <linux/netdevice.h>
98 #include <linux/etherdevice.h>
99 #include <linux/skbuff.h>
100 #include <linux/if_arp.h>
101 #include <linux/bitops.h>
103 #include <asm/system.h>
104 #include <asm/io.h>
105 #include <asm/dma.h>
107 /* This include could be elsewhere, since it is not wireless specific */
108 #include "wireless/i82593.h"
110 static char version[] __initdata = "znet.c:v1.02 9/23/94 becker@scyld.com\n";
112 #ifndef ZNET_DEBUG
113 #define ZNET_DEBUG 1
114 #endif
115 static unsigned int znet_debug = ZNET_DEBUG;
116 module_param (znet_debug, int, 0);
117 MODULE_PARM_DESC (znet_debug, "ZNet debug level");
118 MODULE_LICENSE("GPL");
120 /* The DMA modes we need aren't in <dma.h>. */
121 #define DMA_RX_MODE 0x14 /* Auto init, I/O to mem, ++, demand. */
122 #define DMA_TX_MODE 0x18 /* Auto init, Mem to I/O, ++, demand. */
123 #define dma_page_eq(ptr1, ptr2) ((long)(ptr1)>>17 == (long)(ptr2)>>17)
124 #define RX_BUF_SIZE 8192
125 #define TX_BUF_SIZE 8192
126 #define DMA_BUF_SIZE (RX_BUF_SIZE + 16) /* 8k + 16 bytes for trailers */
128 #define TX_TIMEOUT 10
130 struct znet_private {
131 int rx_dma, tx_dma;
132 struct net_device_stats stats;
133 spinlock_t lock;
134 short sia_base, sia_size, io_size;
135 struct i82593_conf_block i593_init;
136 /* The starting, current, and end pointers for the packet buffers. */
137 ushort *rx_start, *rx_cur, *rx_end;
138 ushort *tx_start, *tx_cur, *tx_end;
139 ushort tx_buf_len; /* Tx buffer length, in words. */
142 /* Only one can be built-in;-> */
143 static struct net_device *znet_dev;
145 struct netidblk {
146 char magic[8]; /* The magic number (string) "NETIDBLK" */
147 unsigned char netid[8]; /* The physical station address */
148 char nettype, globalopt;
149 char vendor[8]; /* The machine vendor and product name. */
150 char product[8];
151 char irq1, irq2; /* Interrupts, only one is currently used. */
152 char dma1, dma2;
153 short dma_mem_misc[8]; /* DMA buffer locations (unused in Linux). */
154 short iobase1, iosize1;
155 short iobase2, iosize2; /* Second iobase unused. */
156 char driver_options; /* Misc. bits */
157 char pad;
160 static int znet_open(struct net_device *dev);
161 static int znet_send_packet(struct sk_buff *skb, struct net_device *dev);
162 static irqreturn_t znet_interrupt(int irq, void *dev_id, struct pt_regs *regs);
163 static void znet_rx(struct net_device *dev);
164 static int znet_close(struct net_device *dev);
165 static struct net_device_stats *net_get_stats(struct net_device *dev);
166 static void hardware_init(struct net_device *dev);
167 static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset);
168 static void znet_tx_timeout (struct net_device *dev);
170 /* Request needed resources */
171 static int znet_request_resources (struct net_device *dev)
173 struct znet_private *znet = dev->priv;
174 unsigned long flags;
176 if (request_irq (dev->irq, &znet_interrupt, 0, "ZNet", dev))
177 goto failed;
178 if (request_dma (znet->rx_dma, "ZNet rx"))
179 goto free_irq;
180 if (request_dma (znet->tx_dma, "ZNet tx"))
181 goto free_rx_dma;
182 if (!request_region (znet->sia_base, znet->sia_size, "ZNet SIA"))
183 goto free_tx_dma;
184 if (!request_region (dev->base_addr, znet->io_size, "ZNet I/O"))
185 goto free_sia;
187 return 0; /* Happy ! */
189 free_sia:
190 release_region (znet->sia_base, znet->sia_size);
191 free_tx_dma:
192 flags = claim_dma_lock();
193 free_dma (znet->tx_dma);
194 release_dma_lock (flags);
195 free_rx_dma:
196 flags = claim_dma_lock();
197 free_dma (znet->rx_dma);
198 release_dma_lock (flags);
199 free_irq:
200 free_irq (dev->irq, dev);
201 failed:
202 return -1;
205 static void znet_release_resources (struct net_device *dev)
207 struct znet_private *znet = dev->priv;
208 unsigned long flags;
210 release_region (znet->sia_base, znet->sia_size);
211 release_region (dev->base_addr, znet->io_size);
212 flags = claim_dma_lock();
213 free_dma (znet->tx_dma);
214 free_dma (znet->rx_dma);
215 release_dma_lock (flags);
216 free_irq (dev->irq, dev);
219 /* Keep the magical SIA stuff in a single function... */
220 static void znet_transceiver_power (struct net_device *dev, int on)
222 struct znet_private *znet = dev->priv;
223 unsigned char v;
225 /* Turn on/off the 82501 SIA, using zenith-specific magic. */
226 /* Select LAN control register */
227 outb(0x10, znet->sia_base);
229 if (on)
230 v = inb(znet->sia_base + 1) | 0x84;
231 else
232 v = inb(znet->sia_base + 1) & ~0x84;
234 outb(v, znet->sia_base+1); /* Turn on/off LAN power (bit 2). */
237 /* Init the i82593, with current promisc/mcast configuration.
238 Also used from hardware_init. */
239 static void znet_set_multicast_list (struct net_device *dev)
241 struct znet_private *znet = dev->priv;
242 short ioaddr = dev->base_addr;
243 struct i82593_conf_block *cfblk = &znet->i593_init;
245 memset(cfblk, 0x00, sizeof(struct i82593_conf_block));
247 /* The configuration block. What an undocumented nightmare.
248 The first set of values are those suggested (without explanation)
249 for ethernet in the Intel 82586 databook. The rest appear to be
250 completely undocumented, except for cryptic notes in the Crynwr
251 packet driver. This driver uses the Crynwr values verbatim. */
253 /* maz : Rewritten to take advantage of the wanvelan includes.
254 At least we have names, not just blind values */
256 /* Byte 0 */
257 cfblk->fifo_limit = 10; /* = 16 B rx and 80 B tx fifo thresholds */
258 cfblk->forgnesi = 0; /* 0=82C501, 1=AMD7992B compatibility */
259 cfblk->fifo_32 = 1;
260 cfblk->d6mod = 0; /* Run in i82593 advanced mode */
261 cfblk->throttle_enb = 1;
263 /* Byte 1 */
264 cfblk->throttle = 8; /* Continuous w/interrupts, 128-clock DMA. */
265 cfblk->cntrxint = 0; /* enable continuous mode receive interrupts */
266 cfblk->contin = 1; /* enable continuous mode */
268 /* Byte 2 */
269 cfblk->addr_len = ETH_ALEN;
270 cfblk->acloc = 1; /* Disable source addr insertion by i82593 */
271 cfblk->preamb_len = 2; /* 8 bytes preamble */
272 cfblk->loopback = 0; /* Loopback off */
274 /* Byte 3 */
275 cfblk->lin_prio = 0; /* Default priorities & backoff methods. */
276 cfblk->tbofstop = 0;
277 cfblk->exp_prio = 0;
278 cfblk->bof_met = 0;
280 /* Byte 4 */
281 cfblk->ifrm_spc = 6; /* 96 bit times interframe spacing */
283 /* Byte 5 */
284 cfblk->slottim_low = 0; /* 512 bit times slot time (low) */
286 /* Byte 6 */
287 cfblk->slottim_hi = 2; /* 512 bit times slot time (high) */
288 cfblk->max_retr = 15; /* 15 collisions retries */
290 /* Byte 7 */
291 cfblk->prmisc = ((dev->flags & IFF_PROMISC) ? 1 : 0); /* Promiscuous mode */
292 cfblk->bc_dis = 0; /* Enable broadcast reception */
293 cfblk->crs_1 = 0; /* Don't transmit without carrier sense */
294 cfblk->nocrc_ins = 0; /* i82593 generates CRC */
295 cfblk->crc_1632 = 0; /* 32-bit Autodin-II CRC */
296 cfblk->crs_cdt = 0; /* CD not to be interpreted as CS */
298 /* Byte 8 */
299 cfblk->cs_filter = 0; /* CS is recognized immediately */
300 cfblk->crs_src = 0; /* External carrier sense */
301 cfblk->cd_filter = 0; /* CD is recognized immediately */
303 /* Byte 9 */
304 cfblk->min_fr_len = ETH_ZLEN >> 2; /* Minimum frame length */
306 /* Byte A */
307 cfblk->lng_typ = 1; /* Type/length checks OFF */
308 cfblk->lng_fld = 1; /* Disable 802.3 length field check */
309 cfblk->rxcrc_xf = 1; /* Don't transfer CRC to memory */
310 cfblk->artx = 1; /* Disable automatic retransmission */
311 cfblk->sarec = 1; /* Disable source addr trig of CD */
312 cfblk->tx_jabber = 0; /* Disable jabber jam sequence */
313 cfblk->hash_1 = 1; /* Use bits 0-5 in mc address hash */
314 cfblk->lbpkpol = 0; /* Loopback pin active high */
316 /* Byte B */
317 cfblk->fdx = 0; /* Disable full duplex operation */
319 /* Byte C */
320 cfblk->dummy_6 = 0x3f; /* all ones, Default multicast addresses & backoff. */
321 cfblk->mult_ia = 0; /* No multiple individual addresses */
322 cfblk->dis_bof = 0; /* Disable the backoff algorithm ?! */
324 /* Byte D */
325 cfblk->dummy_1 = 1; /* set to 1 */
326 cfblk->tx_ifs_retrig = 3; /* Hmm... Disabled */
327 cfblk->mc_all = (dev->mc_list || (dev->flags&IFF_ALLMULTI));/* multicast all mode */
328 cfblk->rcv_mon = 0; /* Monitor mode disabled */
329 cfblk->frag_acpt = 0; /* Do not accept fragments */
330 cfblk->tstrttrs = 0; /* No start transmission threshold */
332 /* Byte E */
333 cfblk->fretx = 1; /* FIFO automatic retransmission */
334 cfblk->runt_eop = 0; /* drop "runt" packets */
335 cfblk->hw_sw_pin = 0; /* ?? */
336 cfblk->big_endn = 0; /* Big Endian ? no... */
337 cfblk->syncrqs = 1; /* Synchronous DRQ deassertion... */
338 cfblk->sttlen = 1; /* 6 byte status registers */
339 cfblk->rx_eop = 0; /* Signal EOP on packet reception */
340 cfblk->tx_eop = 0; /* Signal EOP on packet transmission */
342 /* Byte F */
343 cfblk->rbuf_size = RX_BUF_SIZE >> 12; /* Set receive buffer size */
344 cfblk->rcvstop = 1; /* Enable Receive Stop Register */
346 if (znet_debug > 2) {
347 int i;
348 unsigned char *c;
350 for (i = 0, c = (char *) cfblk; i < sizeof (*cfblk); i++)
351 printk ("%02X ", c[i]);
352 printk ("\n");
355 *znet->tx_cur++ = sizeof(struct i82593_conf_block);
356 memcpy(znet->tx_cur, cfblk, sizeof(struct i82593_conf_block));
357 znet->tx_cur += sizeof(struct i82593_conf_block)/2;
358 outb(OP0_CONFIGURE | CR0_CHNL, ioaddr);
360 /* XXX FIXME maz : Add multicast addresses here, so having a
361 * multicast address configured isn't equal to IFF_ALLMULTI */
364 /* The Z-Note probe is pretty easy. The NETIDBLK exists in the safe-to-probe
365 BIOS area. We just scan for the signature, and pull the vital parameters
366 out of the structure. */
368 static int __init znet_probe (void)
370 int i;
371 struct netidblk *netinfo;
372 struct znet_private *znet;
373 struct net_device *dev;
374 char *p;
375 int err = -ENOMEM;
377 /* This code scans the region 0xf0000 to 0xfffff for a "NETIDBLK". */
378 for(p = (char *)phys_to_virt(0xf0000); p < (char *)phys_to_virt(0x100000); p++)
379 if (*p == 'N' && strncmp(p, "NETIDBLK", 8) == 0)
380 break;
382 if (p >= (char *)phys_to_virt(0x100000)) {
383 if (znet_debug > 1)
384 printk(KERN_INFO "No Z-Note ethernet adaptor found.\n");
385 return -ENODEV;
388 dev = alloc_etherdev(sizeof(struct znet_private));
389 if (!dev)
390 return -ENOMEM;
392 SET_MODULE_OWNER (dev);
394 znet = dev->priv;
396 netinfo = (struct netidblk *)p;
397 dev->base_addr = netinfo->iobase1;
398 dev->irq = netinfo->irq1;
400 printk(KERN_INFO "%s: ZNET at %#3lx,", dev->name, dev->base_addr);
402 /* The station address is in the "netidblk" at 0x0f0000. */
403 for (i = 0; i < 6; i++)
404 printk(" %2.2x", dev->dev_addr[i] = netinfo->netid[i]);
406 printk(", using IRQ %d DMA %d and %d.\n", dev->irq, netinfo->dma1,
407 netinfo->dma2);
409 if (znet_debug > 1) {
410 printk(KERN_INFO "%s: vendor '%16.16s' IRQ1 %d IRQ2 %d DMA1 %d DMA2 %d.\n",
411 dev->name, netinfo->vendor,
412 netinfo->irq1, netinfo->irq2,
413 netinfo->dma1, netinfo->dma2);
414 printk(KERN_INFO "%s: iobase1 %#x size %d iobase2 %#x size %d net type %2.2x.\n",
415 dev->name, netinfo->iobase1, netinfo->iosize1,
416 netinfo->iobase2, netinfo->iosize2, netinfo->nettype);
419 if (znet_debug > 0)
420 printk(KERN_INFO "%s", version);
422 znet->rx_dma = netinfo->dma1;
423 znet->tx_dma = netinfo->dma2;
424 spin_lock_init(&znet->lock);
425 znet->sia_base = 0xe6; /* Magic address for the 82501 SIA */
426 znet->sia_size = 2;
427 /* maz: Despite the '593 being advertised above as using a
428 * single 8bits I/O port, this driver does many 16bits
429 * access. So set io_size accordingly */
430 znet->io_size = 2;
432 if (!(znet->rx_start = kmalloc (DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA)))
433 goto free_dev;
434 if (!(znet->tx_start = kmalloc (DMA_BUF_SIZE, GFP_KERNEL | GFP_DMA)))
435 goto free_rx;
437 if (!dma_page_eq (znet->rx_start, znet->rx_start + (RX_BUF_SIZE/2-1)) ||
438 !dma_page_eq (znet->tx_start, znet->tx_start + (TX_BUF_SIZE/2-1))) {
439 printk (KERN_WARNING "tx/rx crossing DMA frontiers, giving up\n");
440 goto free_tx;
443 znet->rx_end = znet->rx_start + RX_BUF_SIZE/2;
444 znet->tx_buf_len = TX_BUF_SIZE/2;
445 znet->tx_end = znet->tx_start + znet->tx_buf_len;
447 /* The ZNET-specific entries in the device structure. */
448 dev->open = &znet_open;
449 dev->hard_start_xmit = &znet_send_packet;
450 dev->stop = &znet_close;
451 dev->get_stats = net_get_stats;
452 dev->set_multicast_list = &znet_set_multicast_list;
453 dev->tx_timeout = znet_tx_timeout;
454 dev->watchdog_timeo = TX_TIMEOUT;
455 err = register_netdev(dev);
456 if (err)
457 goto free_tx;
458 znet_dev = dev;
459 return 0;
461 free_tx:
462 kfree(znet->tx_start);
463 free_rx:
464 kfree(znet->rx_start);
465 free_dev:
466 free_netdev(dev);
467 return err;
471 static int znet_open(struct net_device *dev)
473 int ioaddr = dev->base_addr;
475 if (znet_debug > 2)
476 printk(KERN_DEBUG "%s: znet_open() called.\n", dev->name);
478 /* These should never fail. You can't add devices to a sealed box! */
479 if (znet_request_resources (dev)) {
480 printk(KERN_WARNING "%s: Not opened -- resource busy?!?\n", dev->name);
481 return -EBUSY;
484 znet_transceiver_power (dev, 1);
486 /* According to the Crynwr driver we should wait 50 msec. for the
487 LAN clock to stabilize. My experiments indicates that the '593 can
488 be initialized immediately. The delay is probably needed for the
489 DC-to-DC converter to come up to full voltage, and for the oscillator
490 to be spot-on at 20Mhz before transmitting.
491 Until this proves to be a problem we rely on the higher layers for the
492 delay and save allocating a timer entry. */
494 /* maz : Well, I'm getting every time the following message
495 * without the delay on a 486@33. This machine is much too
496 * fast... :-) So maybe the Crynwr driver wasn't wrong after
497 * all, even if the message is completly harmless on my
498 * setup. */
499 mdelay (50);
501 /* This follows the packet driver's lead, and checks for success. */
502 if (inb(ioaddr) != 0x10 && inb(ioaddr) != 0x00)
503 printk(KERN_WARNING "%s: Problem turning on the transceiver power.\n",
504 dev->name);
506 hardware_init(dev);
507 netif_start_queue (dev);
509 return 0;
513 static void znet_tx_timeout (struct net_device *dev)
515 int ioaddr = dev->base_addr;
516 ushort event, tx_status, rx_offset, state;
518 outb (CR0_STATUS_0, ioaddr);
519 event = inb (ioaddr);
520 outb (CR0_STATUS_1, ioaddr);
521 tx_status = inw (ioaddr);
522 outb (CR0_STATUS_2, ioaddr);
523 rx_offset = inw (ioaddr);
524 outb (CR0_STATUS_3, ioaddr);
525 state = inb (ioaddr);
526 printk (KERN_WARNING "%s: transmit timed out, status %02x %04x %04x %02x,"
527 " resetting.\n", dev->name, event, tx_status, rx_offset, state);
528 if (tx_status == TX_LOST_CRS)
529 printk (KERN_WARNING "%s: Tx carrier error, check transceiver cable.\n",
530 dev->name);
531 outb (OP0_RESET, ioaddr);
532 hardware_init (dev);
533 netif_wake_queue (dev);
536 static int znet_send_packet(struct sk_buff *skb, struct net_device *dev)
538 int ioaddr = dev->base_addr;
539 struct znet_private *znet = dev->priv;
540 unsigned long flags;
541 short length = skb->len;
543 if (znet_debug > 4)
544 printk(KERN_DEBUG "%s: ZNet_send_packet.\n", dev->name);
546 if (length < ETH_ZLEN) {
547 skb = skb_padto(skb, ETH_ZLEN);
548 if (skb == NULL)
549 return 0;
550 length = ETH_ZLEN;
553 netif_stop_queue (dev);
555 /* Check that the part hasn't reset itself, probably from suspend. */
556 outb(CR0_STATUS_0, ioaddr);
557 if (inw(ioaddr) == 0x0010 &&
558 inw(ioaddr) == 0x0000 &&
559 inw(ioaddr) == 0x0010) {
560 if (znet_debug > 1)
561 printk (KERN_WARNING "%s : waking up\n", dev->name);
562 hardware_init(dev);
563 znet_transceiver_power (dev, 1);
566 if (1) {
567 unsigned char *buf = (void *)skb->data;
568 ushort *tx_link = znet->tx_cur - 1;
569 ushort rnd_len = (length + 1)>>1;
571 znet->stats.tx_bytes+=length;
573 if (znet->tx_cur >= znet->tx_end)
574 znet->tx_cur = znet->tx_start;
575 *znet->tx_cur++ = length;
576 if (znet->tx_cur + rnd_len + 1 > znet->tx_end) {
577 int semi_cnt = (znet->tx_end - znet->tx_cur)<<1; /* Cvrt to byte cnt. */
578 memcpy(znet->tx_cur, buf, semi_cnt);
579 rnd_len -= semi_cnt>>1;
580 memcpy(znet->tx_start, buf + semi_cnt, length - semi_cnt);
581 znet->tx_cur = znet->tx_start + rnd_len;
582 } else {
583 memcpy(znet->tx_cur, buf, skb->len);
584 znet->tx_cur += rnd_len;
586 *znet->tx_cur++ = 0;
588 spin_lock_irqsave(&znet->lock, flags);
590 *tx_link = OP0_TRANSMIT | CR0_CHNL;
591 /* Is this always safe to do? */
592 outb(OP0_TRANSMIT | CR0_CHNL, ioaddr);
594 spin_unlock_irqrestore (&znet->lock, flags);
596 dev->trans_start = jiffies;
597 netif_start_queue (dev);
599 if (znet_debug > 4)
600 printk(KERN_DEBUG "%s: Transmitter queued, length %d.\n", dev->name, length);
602 dev_kfree_skb(skb);
603 return 0;
606 /* The ZNET interrupt handler. */
607 static irqreturn_t znet_interrupt(int irq, void *dev_id, struct pt_regs * regs)
609 struct net_device *dev = dev_id;
610 struct znet_private *znet = dev->priv;
611 int ioaddr;
612 int boguscnt = 20;
613 int handled = 0;
615 if (dev == NULL) {
616 printk(KERN_WARNING "znet_interrupt(): IRQ %d for unknown device.\n", irq);
617 return IRQ_NONE;
620 spin_lock (&znet->lock);
622 ioaddr = dev->base_addr;
624 outb(CR0_STATUS_0, ioaddr);
625 do {
626 ushort status = inb(ioaddr);
627 if (znet_debug > 5) {
628 ushort result, rx_ptr, running;
629 outb(CR0_STATUS_1, ioaddr);
630 result = inw(ioaddr);
631 outb(CR0_STATUS_2, ioaddr);
632 rx_ptr = inw(ioaddr);
633 outb(CR0_STATUS_3, ioaddr);
634 running = inb(ioaddr);
635 printk(KERN_DEBUG "%s: interrupt, status %02x, %04x %04x %02x serial %d.\n",
636 dev->name, status, result, rx_ptr, running, boguscnt);
638 if ((status & SR0_INTERRUPT) == 0)
639 break;
641 handled = 1;
643 if ((status & SR0_EVENT_MASK) == SR0_TRANSMIT_DONE ||
644 (status & SR0_EVENT_MASK) == SR0_RETRANSMIT_DONE ||
645 (status & SR0_EVENT_MASK) == SR0_TRANSMIT_NO_CRC_DONE) {
646 int tx_status;
647 outb(CR0_STATUS_1, ioaddr);
648 tx_status = inw(ioaddr);
649 /* It's undocumented, but tx_status seems to match the i82586. */
650 if (tx_status & TX_OK) {
651 znet->stats.tx_packets++;
652 znet->stats.collisions += tx_status & TX_NCOL_MASK;
653 } else {
654 if (tx_status & (TX_LOST_CTS | TX_LOST_CRS))
655 znet->stats.tx_carrier_errors++;
656 if (tx_status & TX_UND_RUN)
657 znet->stats.tx_fifo_errors++;
658 if (!(tx_status & TX_HRT_BEAT))
659 znet->stats.tx_heartbeat_errors++;
660 if (tx_status & TX_MAX_COL)
661 znet->stats.tx_aborted_errors++;
662 /* ...and the catch-all. */
663 if ((tx_status | (TX_LOST_CRS | TX_LOST_CTS | TX_UND_RUN | TX_HRT_BEAT | TX_MAX_COL)) != (TX_LOST_CRS | TX_LOST_CTS | TX_UND_RUN | TX_HRT_BEAT | TX_MAX_COL))
664 znet->stats.tx_errors++;
666 /* Transceiver may be stuck if cable
667 * was removed while emiting a
668 * packet. Flip it off, then on to
669 * reset it. This is very empirical,
670 * but it seems to work. */
672 znet_transceiver_power (dev, 0);
673 znet_transceiver_power (dev, 1);
675 netif_wake_queue (dev);
678 if ((status & SR0_RECEPTION) ||
679 (status & SR0_EVENT_MASK) == SR0_STOP_REG_HIT) {
680 znet_rx(dev);
682 /* Clear the interrupts we've handled. */
683 outb(CR0_INT_ACK, ioaddr);
684 } while (boguscnt--);
686 spin_unlock (&znet->lock);
688 return IRQ_RETVAL(handled);
691 static void znet_rx(struct net_device *dev)
693 struct znet_private *znet = dev->priv;
694 int ioaddr = dev->base_addr;
695 int boguscount = 1;
696 short next_frame_end_offset = 0; /* Offset of next frame start. */
697 short *cur_frame_end;
698 short cur_frame_end_offset;
700 outb(CR0_STATUS_2, ioaddr);
701 cur_frame_end_offset = inw(ioaddr);
703 if (cur_frame_end_offset == znet->rx_cur - znet->rx_start) {
704 printk(KERN_WARNING "%s: Interrupted, but nothing to receive, offset %03x.\n",
705 dev->name, cur_frame_end_offset);
706 return;
709 /* Use same method as the Crynwr driver: construct a forward list in
710 the same area of the backwards links we now have. This allows us to
711 pass packets to the upper layers in the order they were received --
712 important for fast-path sequential operations. */
713 while (znet->rx_start + cur_frame_end_offset != znet->rx_cur
714 && ++boguscount < 5) {
715 unsigned short hi_cnt, lo_cnt, hi_status, lo_status;
716 int count, status;
718 if (cur_frame_end_offset < 4) {
719 /* Oh no, we have a special case: the frame trailer wraps around
720 the end of the ring buffer. We've saved space at the end of
721 the ring buffer for just this problem. */
722 memcpy(znet->rx_end, znet->rx_start, 8);
723 cur_frame_end_offset += (RX_BUF_SIZE/2);
725 cur_frame_end = znet->rx_start + cur_frame_end_offset - 4;
727 lo_status = *cur_frame_end++;
728 hi_status = *cur_frame_end++;
729 status = ((hi_status & 0xff) << 8) + (lo_status & 0xff);
730 lo_cnt = *cur_frame_end++;
731 hi_cnt = *cur_frame_end++;
732 count = ((hi_cnt & 0xff) << 8) + (lo_cnt & 0xff);
734 if (znet_debug > 5)
735 printk(KERN_DEBUG "Constructing trailer at location %03x, %04x %04x %04x %04x"
736 " count %#x status %04x.\n",
737 cur_frame_end_offset<<1, lo_status, hi_status, lo_cnt, hi_cnt,
738 count, status);
739 cur_frame_end[-4] = status;
740 cur_frame_end[-3] = next_frame_end_offset;
741 cur_frame_end[-2] = count;
742 next_frame_end_offset = cur_frame_end_offset;
743 cur_frame_end_offset -= ((count + 1)>>1) + 3;
744 if (cur_frame_end_offset < 0)
745 cur_frame_end_offset += RX_BUF_SIZE/2;
748 /* Now step forward through the list. */
749 do {
750 ushort *this_rfp_ptr = znet->rx_start + next_frame_end_offset;
751 int status = this_rfp_ptr[-4];
752 int pkt_len = this_rfp_ptr[-2];
754 if (znet_debug > 5)
755 printk(KERN_DEBUG "Looking at trailer ending at %04x status %04x length %03x"
756 " next %04x.\n", next_frame_end_offset<<1, status, pkt_len,
757 this_rfp_ptr[-3]<<1);
758 /* Once again we must assume that the i82586 docs apply. */
759 if ( ! (status & RX_RCV_OK)) { /* There was an error. */
760 znet->stats.rx_errors++;
761 if (status & RX_CRC_ERR) znet->stats.rx_crc_errors++;
762 if (status & RX_ALG_ERR) znet->stats.rx_frame_errors++;
763 #if 0
764 if (status & 0x0200) znet->stats.rx_over_errors++; /* Wrong. */
765 if (status & 0x0100) znet->stats.rx_fifo_errors++;
766 #else
767 /* maz : Wild guess... */
768 if (status & RX_OVRRUN) znet->stats.rx_over_errors++;
769 #endif
770 if (status & RX_SRT_FRM) znet->stats.rx_length_errors++;
771 } else if (pkt_len > 1536) {
772 znet->stats.rx_length_errors++;
773 } else {
774 /* Malloc up new buffer. */
775 struct sk_buff *skb;
777 skb = dev_alloc_skb(pkt_len);
778 if (skb == NULL) {
779 if (znet_debug)
780 printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
781 znet->stats.rx_dropped++;
782 break;
784 skb->dev = dev;
786 if (&znet->rx_cur[(pkt_len+1)>>1] > znet->rx_end) {
787 int semi_cnt = (znet->rx_end - znet->rx_cur)<<1;
788 memcpy(skb_put(skb,semi_cnt), znet->rx_cur, semi_cnt);
789 memcpy(skb_put(skb,pkt_len-semi_cnt), znet->rx_start,
790 pkt_len - semi_cnt);
791 } else {
792 memcpy(skb_put(skb,pkt_len), znet->rx_cur, pkt_len);
793 if (znet_debug > 6) {
794 unsigned int *packet = (unsigned int *) skb->data;
795 printk(KERN_DEBUG "Packet data is %08x %08x %08x %08x.\n", packet[0],
796 packet[1], packet[2], packet[3]);
799 skb->protocol=eth_type_trans(skb,dev);
800 netif_rx(skb);
801 dev->last_rx = jiffies;
802 znet->stats.rx_packets++;
803 znet->stats.rx_bytes += pkt_len;
805 znet->rx_cur = this_rfp_ptr;
806 if (znet->rx_cur >= znet->rx_end)
807 znet->rx_cur -= RX_BUF_SIZE/2;
808 update_stop_hit(ioaddr, (znet->rx_cur - znet->rx_start)<<1);
809 next_frame_end_offset = this_rfp_ptr[-3];
810 if (next_frame_end_offset == 0) /* Read all the frames? */
811 break; /* Done for now */
812 this_rfp_ptr = znet->rx_start + next_frame_end_offset;
813 } while (--boguscount);
815 /* If any worth-while packets have been received, dev_rint()
816 has done a mark_bh(INET_BH) for us and will work on them
817 when we get to the bottom-half routine. */
818 return;
821 /* The inverse routine to znet_open(). */
822 static int znet_close(struct net_device *dev)
824 int ioaddr = dev->base_addr;
826 netif_stop_queue (dev);
828 outb(OP0_RESET, ioaddr); /* CMD0_RESET */
830 if (znet_debug > 1)
831 printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
832 /* Turn off transceiver power. */
833 znet_transceiver_power (dev, 0);
835 znet_release_resources (dev);
837 return 0;
840 /* Get the current statistics. This may be called with the card open or
841 closed. */
842 static struct net_device_stats *net_get_stats(struct net_device *dev)
844 struct znet_private *znet = dev->priv;
846 return &znet->stats;
849 static void show_dma(struct net_device *dev)
851 short ioaddr = dev->base_addr;
852 unsigned char stat = inb (ioaddr);
853 struct znet_private *znet = dev->priv;
854 unsigned long flags;
855 short dma_port = ((znet->tx_dma&3)<<2) + IO_DMA2_BASE;
856 unsigned addr = inb(dma_port);
857 short residue;
859 addr |= inb(dma_port) << 8;
860 residue = get_dma_residue(znet->tx_dma);
862 if (znet_debug > 1) {
863 flags=claim_dma_lock();
864 printk(KERN_DEBUG "Stat:%02x Addr: %04x cnt:%3x\n",
865 stat, addr<<1, residue);
866 release_dma_lock(flags);
870 /* Initialize the hardware. We have to do this when the board is open()ed
871 or when we come out of suspend mode. */
872 static void hardware_init(struct net_device *dev)
874 unsigned long flags;
875 short ioaddr = dev->base_addr;
876 struct znet_private *znet = dev->priv;
878 znet->rx_cur = znet->rx_start;
879 znet->tx_cur = znet->tx_start;
881 /* Reset the chip, and start it up. */
882 outb(OP0_RESET, ioaddr);
884 flags=claim_dma_lock();
885 disable_dma(znet->rx_dma); /* reset by an interrupting task. */
886 clear_dma_ff(znet->rx_dma);
887 set_dma_mode(znet->rx_dma, DMA_RX_MODE);
888 set_dma_addr(znet->rx_dma, (unsigned int) znet->rx_start);
889 set_dma_count(znet->rx_dma, RX_BUF_SIZE);
890 enable_dma(znet->rx_dma);
891 /* Now set up the Tx channel. */
892 disable_dma(znet->tx_dma);
893 clear_dma_ff(znet->tx_dma);
894 set_dma_mode(znet->tx_dma, DMA_TX_MODE);
895 set_dma_addr(znet->tx_dma, (unsigned int) znet->tx_start);
896 set_dma_count(znet->tx_dma, znet->tx_buf_len<<1);
897 enable_dma(znet->tx_dma);
898 release_dma_lock(flags);
900 if (znet_debug > 1)
901 printk(KERN_DEBUG "%s: Initializing the i82593, rx buf %p tx buf %p\n",
902 dev->name, znet->rx_start,znet->tx_start);
903 /* Do an empty configure command, just like the Crynwr driver. This
904 resets to chip to its default values. */
905 *znet->tx_cur++ = 0;
906 *znet->tx_cur++ = 0;
907 show_dma(dev);
908 outb(OP0_CONFIGURE | CR0_CHNL, ioaddr);
910 znet_set_multicast_list (dev);
912 *znet->tx_cur++ = 6;
913 memcpy(znet->tx_cur, dev->dev_addr, 6);
914 znet->tx_cur += 3;
915 show_dma(dev);
916 outb(OP0_IA_SETUP | CR0_CHNL, ioaddr);
917 show_dma(dev);
919 update_stop_hit(ioaddr, 8192);
920 if (znet_debug > 1) printk(KERN_DEBUG "enabling Rx.\n");
921 outb(OP0_RCV_ENABLE, ioaddr);
922 netif_start_queue (dev);
925 static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset)
927 outb(OP0_SWIT_TO_PORT_1 | CR0_CHNL, ioaddr);
928 if (znet_debug > 5)
929 printk(KERN_DEBUG "Updating stop hit with value %02x.\n",
930 (rx_stop_offset >> 6) | CR1_STOP_REG_UPDATE);
931 outb((rx_stop_offset >> 6) | CR1_STOP_REG_UPDATE, ioaddr);
932 outb(OP1_SWIT_TO_PORT_0, ioaddr);
935 static __exit void znet_cleanup (void)
937 if (znet_dev) {
938 struct znet_private *znet = znet_dev->priv;
940 unregister_netdev (znet_dev);
941 kfree (znet->rx_start);
942 kfree (znet->tx_start);
943 free_netdev (znet_dev);
947 module_init (znet_probe);
948 module_exit (znet_cleanup);