setup enviroment for compilation
[linux-2.4.21-pre4.git] / drivers / net / znet.c
1 /* znet.c: An Zenith Z-Note ethernet driver for linux. */
2
3 static const char version[] = "znet.c:v1.02 9/23/94 becker@cesdis.gsfc.nasa.gov\n";
4
5 /*
6         Written by Donald Becker.
7
8         The author may be reached as becker@scyld.com.
9         This driver is based on the Linux skeleton driver.  The copyright of the
10         skeleton driver is held by the United States Government, as represented
11         by DIRNSA, and it is released under the GPL.
12
13         Thanks to Mike Hollick for alpha testing and suggestions.
14
15   References:
16            The Crynwr packet driver.
17
18           "82593 CSMA/CD Core LAN Controller" Intel datasheet, 1992
19           Intel Microcommunications Databook, Vol. 1, 1990.
20     As usual with Intel, the documentation is incomplete and inaccurate.
21         I had to read the Crynwr packet driver to figure out how to actually
22         use the i82593, and guess at what register bits matched the loosely
23         related i82586.
24
25                                         Theory of Operation
26
27         The i82593 used in the Zenith Z-Note series operates using two(!) slave
28         DMA     channels, one interrupt, and one 8-bit I/O port.
29
30         While there     several ways to configure '593 DMA system, I chose the one
31         that seemed commensurate with the highest system performance in the face
32         of moderate interrupt latency: Both DMA channels are configured as
33         recirculating ring buffers, with one channel (#0) dedicated to Rx and
34         the other channel (#1) to Tx and configuration.  (Note that this is
35         different than the Crynwr driver, where the Tx DMA channel is initialized
36         before each operation.  That approach simplifies operation and Tx error
37         recovery, but requires additional I/O in normal operation and precludes
38         transmit buffer chaining.)
39
40         Both rings are set to 8192 bytes using {TX,RX}_RING_SIZE.  This provides
41         a reasonable ring size for Rx, while simplifying DMA buffer allocation --
42         DMA buffers must not cross a 128K boundary.  (In truth the size selection
43         was influenced by my lack of '593 documentation.  I thus was constrained
44         to use the Crynwr '593 initialization table, which sets the Rx ring size
45         to 8K.)
46
47         Despite my usual low opinion about Intel-designed parts, I must admit
48         that the bulk data handling of the i82593 is a good design for
49         an integrated system, like a laptop, where using two slave DMA channels
50         doesn't pose a problem.  I still take issue with using only a single I/O
51         port.  In the same controlled environment there are essentially no
52         limitations on I/O space, and using multiple locations would eliminate
53         the     need for multiple operations when looking at status registers,
54         setting the Rx ring boundary, or switching to promiscuous mode.
55
56         I also question Zenith's selection of the '593: one of the advertised
57         advantages of earlier Intel parts was that if you figured out the magic
58         initialization incantation you could use the same part on many different
59         network types.  Zenith's use of the "FriendlyNet" (sic) connector rather
60         than an on-board transceiver leads me to believe that they were planning
61         to take advantage of this.  But, uhmmm, the '593 omits all but ethernet
62         functionality from the serial subsystem.
63  */
64
65 #include <linux/kernel.h>
66 #include <linux/sched.h>
67 #include <linux/string.h>
68 #include <linux/ptrace.h>
69 #include <linux/errno.h>
70 #include <linux/interrupt.h>
71 #include <linux/ioport.h>
72 #include <linux/init.h>
73 #include <asm/system.h>
74 #include <asm/bitops.h>
75 #include <asm/io.h>
76 #include <asm/dma.h>
77
78 #include <linux/netdevice.h>
79 #include <linux/etherdevice.h>
80 #include <linux/skbuff.h>
81 #include <linux/if_arp.h>
82
83 #ifndef ZNET_DEBUG
84 #define ZNET_DEBUG 1
85 #endif
86 static unsigned int znet_debug = ZNET_DEBUG;
87
88 /* The DMA modes we need aren't in <dma.h>. */
89 #define DMA_RX_MODE             0x14    /* Auto init, I/O to mem, ++, demand. */
90 #define DMA_TX_MODE             0x18    /* Auto init, Mem to I/O, ++, demand. */
91 #define dma_page_eq(ptr1, ptr2) ((long)(ptr1)>>17 == (long)(ptr2)>>17)
92 #define DMA_BUF_SIZE 8192
93 #define RX_BUF_SIZE 8192
94 #define TX_BUF_SIZE 8192
95
96 /* Commands to the i82593 channel 0. */
97 #define CMD0_CHNL_0                     0x00
98 #define CMD0_CHNL_1                     0x10            /* Switch to channel 1. */
99 #define CMD0_NOP (CMD0_CHNL_0)
100 #define CMD0_PORT_1     CMD0_CHNL_1
101 #define CMD1_PORT_0     1
102 #define CMD0_IA_SETUP           1
103 #define CMD0_CONFIGURE          2
104 #define CMD0_MULTICAST_LIST 3
105 #define CMD0_TRANSMIT           4
106 #define CMD0_DUMP                       6
107 #define CMD0_DIAGNOSE           7
108 #define CMD0_Rx_ENABLE          8
109 #define CMD0_Rx_DISABLE         10
110 #define CMD0_Rx_STOP            11
111 #define CMD0_RETRANSMIT         12
112 #define CMD0_ABORT                      13
113 #define CMD0_RESET                      14
114
115 #define CMD0_ACK 0x80
116
117 #define CMD0_STAT0 (0 << 5)
118 #define CMD0_STAT1 (1 << 5)
119 #define CMD0_STAT2 (2 << 5)
120 #define CMD0_STAT3 (3 << 5)
121
122 #define TX_TIMEOUT      10
123
124 #define net_local znet_private
125 struct znet_private {
126         int rx_dma, tx_dma;
127         struct net_device_stats stats;
128         spinlock_t lock;
129         /* The starting, current, and end pointers for the packet buffers. */
130         ushort *rx_start, *rx_cur, *rx_end;
131         ushort *tx_start, *tx_cur, *tx_end;
132         ushort tx_buf_len;                      /* Tx buffer length, in words. */
133 };
134
135 /* Only one can be built-in;-> */
136 static struct znet_private zn;
137 static ushort dma_buffer1[DMA_BUF_SIZE/2];
138 static ushort dma_buffer2[DMA_BUF_SIZE/2];
139 static ushort dma_buffer3[DMA_BUF_SIZE/2 + 8];
140
141 /* The configuration block.  What an undocumented nightmare.  The first
142    set of values are those suggested (without explanation) for ethernet
143    in the Intel 82586 databook.  The rest appear to be completely undocumented,
144    except for cryptic notes in the Crynwr packet driver.  This driver uses
145    the Crynwr values verbatim. */
146
147 static unsigned char i593_init[] = {
148   0xAA,                                 /* 0: 16-byte input & 80-byte output FIFO. */
149                                                 /*        threshold, 96-byte FIFO, 82593 mode. */
150   0x88,                                 /* 1: Continuous w/interrupts, 128-clock DMA.*/
151   0x2E,                                 /* 2: 8-byte preamble, NO address insertion, */
152                                                 /*        6-byte Ethernet address, loopback off.*/
153   0x00,                                 /* 3: Default priorities & backoff methods. */
154   0x60,                                 /* 4: 96-bit interframe spacing. */
155   0x00,                                 /* 5: 512-bit slot time (low-order). */
156   0xF2,                                 /* 6: Slot time (high-order), 15 COLL retries. */
157   0x00,                                 /* 7: Promisc-off, broadcast-on, default CRC. */
158   0x00,                                 /* 8: Default carrier-sense, collision-detect. */
159   0x40,                                 /* 9: 64-byte minimum frame length. */
160   0x5F,                                 /* A: Type/length checks OFF, no CRC input,
161                                                    "jabber" termination, etc. */
162   0x00,                                 /* B: Full-duplex disabled. */
163   0x3F,                                 /* C: Default multicast addresses & backoff. */
164   0x07,                                 /* D: Default IFS retriggering. */
165   0x31,                                 /* E: Internal retransmit, drop "runt" packets,
166                                                    synchr. DRQ deassertion, 6 status bytes. */
167   0x22,                                 /* F: Receive ring-buffer size (8K), 
168                                                    receive-stop register enable. */
169 };
170
171 struct netidblk {
172         char magic[8];          /* The magic number (string) "NETIDBLK" */
173         unsigned char netid[8]; /* The physical station address */
174         char nettype, globalopt;
175         char vendor[8];         /* The machine vendor and product name. */
176         char product[8];
177         char irq1, irq2;                /* Interrupts, only one is currently used.      */
178         char dma1, dma2;
179         short dma_mem_misc[8];          /* DMA buffer locations (unused in Linux). */
180         short iobase1, iosize1;
181         short iobase2, iosize2;         /* Second iobase unused. */
182         char driver_options;                    /* Misc. bits */
183         char pad;
184 };
185
186 int znet_probe(struct net_device *dev);
187 static int      znet_open(struct net_device *dev);
188 static int      znet_send_packet(struct sk_buff *skb, struct net_device *dev);
189 static void     znet_interrupt(int irq, void *dev_id, struct pt_regs *regs);
190 static void     znet_rx(struct net_device *dev);
191 static int      znet_close(struct net_device *dev);
192 static struct net_device_stats *net_get_stats(struct net_device *dev);
193 static void set_multicast_list(struct net_device *dev);
194 static void hardware_init(struct net_device *dev);
195 static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset);
196 static void znet_tx_timeout (struct net_device *dev);
197
198 #ifdef notdef
199 static struct sigaction znet_sigaction = { &znet_interrupt, 0, 0, NULL, };
200 #endif
201
202 \f
203 /* The Z-Note probe is pretty easy.  The NETIDBLK exists in the safe-to-probe
204    BIOS area.  We just scan for the signature, and pull the vital parameters
205    out of the structure. */
206
207 int __init znet_probe(struct net_device *dev)
208 {
209         int i;
210         struct netidblk *netinfo;
211         char *p;
212
213         /* This code scans the region 0xf0000 to 0xfffff for a "NETIDBLK". */
214         for(p = (char *)phys_to_virt(0xf0000); p < (char *)phys_to_virt(0x100000); p++)
215                 if (*p == 'N'  &&  strncmp(p, "NETIDBLK", 8) == 0)
216                         break;
217
218         if (p >= (char *)phys_to_virt(0x100000)) {
219                 if (znet_debug > 1)
220                         printk(KERN_INFO "No Z-Note ethernet adaptor found.\n");
221                 return -ENODEV;
222         }
223         netinfo = (struct netidblk *)p;
224         dev->base_addr = netinfo->iobase1;
225         dev->irq = netinfo->irq1;
226
227         printk(KERN_INFO "%s: ZNET at %#3lx,", dev->name, dev->base_addr);
228
229         /* The station address is in the "netidblk" at 0x0f0000. */
230         for (i = 0; i < 6; i++)
231                 printk(" %2.2x", dev->dev_addr[i] = netinfo->netid[i]);
232
233         printk(", using IRQ %d DMA %d and %d.\n", dev->irq, netinfo->dma1,
234                 netinfo->dma2);
235
236         if (znet_debug > 1) {
237                 printk(KERN_INFO "%s: vendor '%16.16s' IRQ1 %d IRQ2 %d DMA1 %d DMA2 %d.\n",
238                            dev->name, netinfo->vendor,
239                            netinfo->irq1, netinfo->irq2,
240                            netinfo->dma1, netinfo->dma2);
241                 printk(KERN_INFO "%s: iobase1 %#x size %d iobase2 %#x size %d net type %2.2x.\n",
242                            dev->name, netinfo->iobase1, netinfo->iosize1,
243                            netinfo->iobase2, netinfo->iosize2, netinfo->nettype);
244         }
245
246         if (znet_debug > 0)
247                 printk("%s%s", KERN_INFO, version);
248
249         dev->priv = (void *) &zn;
250         zn.rx_dma = netinfo->dma1;
251         zn.tx_dma = netinfo->dma2;
252         zn.lock = SPIN_LOCK_UNLOCKED;
253
254         /* These should never fail.  You can't add devices to a sealed box! */
255         if (request_irq(dev->irq, &znet_interrupt, 0, "ZNet", dev)
256                 || request_dma(zn.rx_dma,"ZNet rx")
257                 || request_dma(zn.tx_dma,"ZNet tx")) {
258                 printk(KERN_WARNING "%s: Not opened -- resource busy?!?\n", dev->name);
259                 return -EBUSY;
260         }
261
262         /* Allocate buffer memory.      We can cross a 128K boundary, so we
263            must be careful about the allocation.  It's easiest to waste 8K. */
264         if (dma_page_eq(dma_buffer1, &dma_buffer1[RX_BUF_SIZE/2-1]))
265           zn.rx_start = dma_buffer1;
266         else 
267           zn.rx_start = dma_buffer2;
268
269         if (dma_page_eq(dma_buffer3, &dma_buffer3[RX_BUF_SIZE/2-1]))
270           zn.tx_start = dma_buffer3;
271         else
272           zn.tx_start = dma_buffer2;
273         zn.rx_end = zn.rx_start + RX_BUF_SIZE/2;
274         zn.tx_buf_len = TX_BUF_SIZE/2;
275         zn.tx_end = zn.tx_start + zn.tx_buf_len;
276
277         /* The ZNET-specific entries in the device structure. */
278         dev->open = &znet_open;
279         dev->hard_start_xmit = &znet_send_packet;
280         dev->stop = &znet_close;
281         dev->get_stats  = net_get_stats;
282         dev->set_multicast_list = &set_multicast_list;
283         dev->tx_timeout = znet_tx_timeout;
284         dev->watchdog_timeo = TX_TIMEOUT;
285
286         /* Fill in the 'dev' with ethernet-generic values. */
287         ether_setup(dev);
288
289         return 0;
290 }
291
292 \f
293 static int znet_open(struct net_device *dev)
294 {
295         int ioaddr = dev->base_addr;
296
297         if (znet_debug > 2)
298                 printk(KERN_DEBUG "%s: znet_open() called.\n", dev->name);
299
300         /* Turn on the 82501 SIA, using zenith-specific magic. */
301         outb(0x10, 0xe6);                                       /* Select LAN control register */
302         outb(inb(0xe7) | 0x84, 0xe7);           /* Turn on LAN power (bit 2). */
303         /* According to the Crynwr driver we should wait 50 msec. for the
304            LAN clock to stabilize.  My experiments indicates that the '593 can
305            be initialized immediately.  The delay is probably needed for the
306            DC-to-DC converter to come up to full voltage, and for the oscillator
307            to be spot-on at 20Mhz before transmitting.
308            Until this proves to be a problem we rely on the higher layers for the
309            delay and save allocating a timer entry. */
310
311         /* This follows the packet driver's lead, and checks for success. */
312         if (inb(ioaddr) != 0x10 && inb(ioaddr) != 0x00)
313                 printk(KERN_WARNING "%s: Problem turning on the transceiver power.\n",
314                            dev->name);
315
316         hardware_init(dev);
317         netif_start_queue (dev);
318
319         return 0;
320 }
321
322
323 static void znet_tx_timeout (struct net_device *dev)
324 {
325         int ioaddr = dev->base_addr;
326         ushort event, tx_status, rx_offset, state;
327
328         outb (CMD0_STAT0, ioaddr);
329         event = inb (ioaddr);
330         outb (CMD0_STAT1, ioaddr);
331         tx_status = inw (ioaddr);
332         outb (CMD0_STAT2, ioaddr);
333         rx_offset = inw (ioaddr);
334         outb (CMD0_STAT3, ioaddr);
335         state = inb (ioaddr);
336         printk (KERN_WARNING "%s: transmit timed out, status %02x %04x %04x %02x,"
337          " resetting.\n", dev->name, event, tx_status, rx_offset, state);
338         if (tx_status == 0x0400)
339                 printk (KERN_WARNING "%s: Tx carrier error, check transceiver cable.\n",
340                         dev->name);
341         outb (CMD0_RESET, ioaddr);
342         hardware_init (dev);
343         netif_wake_queue (dev);
344 }
345
346 static int znet_send_packet(struct sk_buff *skb, struct net_device *dev)
347 {
348         int ioaddr = dev->base_addr;
349         struct net_local *lp = (struct net_local *)dev->priv;
350         unsigned long flags;
351
352         if (znet_debug > 4)
353                 printk(KERN_DEBUG "%s: ZNet_send_packet.\n", dev->name);
354
355         netif_stop_queue (dev);
356         
357         /* Check that the part hasn't reset itself, probably from suspend. */
358         outb(CMD0_STAT0, ioaddr);
359         if (inw(ioaddr) == 0x0010
360                 && inw(ioaddr) == 0x0000
361                 && inw(ioaddr) == 0x0010)
362           hardware_init(dev);
363
364         if (1) {
365                 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
366                 unsigned char *buf = (void *)skb->data;
367                 ushort *tx_link = zn.tx_cur - 1;
368                 ushort rnd_len = (length + 1)>>1;
369                 
370                 lp->stats.tx_bytes+=length;
371
372                 {
373                         short dma_port = ((zn.tx_dma&3)<<2) + IO_DMA2_BASE;
374                         unsigned addr = inb(dma_port);
375                         addr |= inb(dma_port) << 8;
376                         addr <<= 1;
377                         if (((int)zn.tx_cur & 0x1ffff) != addr)
378                           printk(KERN_WARNING "Address mismatch at Tx: %#x vs %#x.\n",
379                                          (int)zn.tx_cur & 0xffff, addr);
380                         zn.tx_cur = (ushort *)(((int)zn.tx_cur & 0xfe0000) | addr);
381                 }
382
383                 if (zn.tx_cur >= zn.tx_end)
384                   zn.tx_cur = zn.tx_start;
385                 *zn.tx_cur++ = length;
386                 if (zn.tx_cur + rnd_len + 1 > zn.tx_end) {
387                         int semi_cnt = (zn.tx_end - zn.tx_cur)<<1; /* Cvrt to byte cnt. */
388                         memcpy(zn.tx_cur, buf, semi_cnt);
389                         rnd_len -= semi_cnt>>1;
390                         memcpy(zn.tx_start, buf + semi_cnt, length - semi_cnt);
391                         zn.tx_cur = zn.tx_start + rnd_len;
392                 } else {
393                         memcpy(zn.tx_cur, buf, skb->len);
394                         zn.tx_cur += rnd_len;
395                 }
396                 *zn.tx_cur++ = 0;
397
398                 spin_lock_irqsave(&lp->lock, flags);
399                 {
400                         *tx_link = CMD0_TRANSMIT + CMD0_CHNL_1;
401                         /* Is this always safe to do? */
402                         outb(CMD0_TRANSMIT + CMD0_CHNL_1,ioaddr);
403                 }
404                 spin_unlock_irqrestore (&lp->lock, flags);
405
406                 dev->trans_start = jiffies;
407                 netif_start_queue (dev);
408
409                 if (znet_debug > 4)
410                   printk(KERN_DEBUG "%s: Transmitter queued, length %d.\n", dev->name, length);
411         }
412         dev_kfree_skb(skb); 
413         return 0;
414 }
415
416 /* The ZNET interrupt handler. */
417 static void     znet_interrupt(int irq, void *dev_id, struct pt_regs * regs)
418 {
419         struct net_device *dev = dev_id;
420         struct net_local *lp = (struct net_local *)dev->priv;
421         int ioaddr;
422         int boguscnt = 20;
423
424         if (dev == NULL) {
425                 printk(KERN_WARNING "znet_interrupt(): IRQ %d for unknown device.\n", irq);
426                 return;
427         }
428
429         spin_lock (&lp->lock);
430         
431         ioaddr = dev->base_addr;
432
433         outb(CMD0_STAT0, ioaddr);
434         do {
435                 ushort status = inb(ioaddr);
436                 if (znet_debug > 5) {
437                         ushort result, rx_ptr, running;
438                         outb(CMD0_STAT1, ioaddr);
439                         result = inw(ioaddr);
440                         outb(CMD0_STAT2, ioaddr);
441                         rx_ptr = inw(ioaddr);
442                         outb(CMD0_STAT3, ioaddr);
443                         running = inb(ioaddr);
444                         printk(KERN_DEBUG "%s: interrupt, status %02x, %04x %04x %02x serial %d.\n",
445                                  dev->name, status, result, rx_ptr, running, boguscnt);
446                 }
447                 if ((status & 0x80) == 0)
448                         break;
449
450                 if ((status & 0x0F) == 4) {     /* Transmit done. */
451                         int tx_status;
452                         outb(CMD0_STAT1, ioaddr);
453                         tx_status = inw(ioaddr);
454                         /* It's undocumented, but tx_status seems to match the i82586. */
455                         if (tx_status & 0x2000) {
456                                 lp->stats.tx_packets++;
457                                 lp->stats.collisions += tx_status & 0xf;
458                         } else {
459                                 if (tx_status & 0x0600)  lp->stats.tx_carrier_errors++;
460                                 if (tx_status & 0x0100)  lp->stats.tx_fifo_errors++;
461                                 if (!(tx_status & 0x0040)) lp->stats.tx_heartbeat_errors++;
462                                 if (tx_status & 0x0020)  lp->stats.tx_aborted_errors++;
463                                 /* ...and the catch-all. */
464                                 if ((tx_status | 0x0760) != 0x0760)
465                                   lp->stats.tx_errors++;
466                         }
467                         netif_wake_queue (dev);
468                 }
469
470                 if ((status & 0x40)
471                         || (status & 0x0f) == 11) {
472                         znet_rx(dev);
473                 }
474                 /* Clear the interrupts we've handled. */
475                 outb(CMD0_ACK,ioaddr);
476         } while (boguscnt--);
477
478         spin_unlock (&lp->lock);
479         
480         return;
481 }
482
483 static void znet_rx(struct net_device *dev)
484 {
485         struct net_local *lp = (struct net_local *)dev->priv;
486         int ioaddr = dev->base_addr;
487         int boguscount = 1;
488         short next_frame_end_offset = 0;                /* Offset of next frame start. */
489         short *cur_frame_end;
490         short cur_frame_end_offset;
491
492         outb(CMD0_STAT2, ioaddr);
493         cur_frame_end_offset = inw(ioaddr);
494
495         if (cur_frame_end_offset == zn.rx_cur - zn.rx_start) {
496                 printk(KERN_WARNING "%s: Interrupted, but nothing to receive, offset %03x.\n",
497                            dev->name, cur_frame_end_offset);
498                 return;
499         }
500
501         /* Use same method as the Crynwr driver: construct a forward list in
502            the same area of the backwards links we now have.  This allows us to
503            pass packets to the upper layers in the order they were received --
504            important for fast-path sequential operations. */
505          while (zn.rx_start + cur_frame_end_offset != zn.rx_cur
506                         && ++boguscount < 5) {
507                 unsigned short hi_cnt, lo_cnt, hi_status, lo_status;
508                 int count, status;
509
510                 if (cur_frame_end_offset < 4) {
511                         /* Oh no, we have a special case: the frame trailer wraps around
512                            the end of the ring buffer.  We've saved space at the end of
513                            the ring buffer for just this problem. */
514                         memcpy(zn.rx_end, zn.rx_start, 8);
515                         cur_frame_end_offset += (RX_BUF_SIZE/2);
516                 }
517                 cur_frame_end = zn.rx_start + cur_frame_end_offset - 4;
518
519                 lo_status = *cur_frame_end++;
520                 hi_status = *cur_frame_end++;
521                 status = ((hi_status & 0xff) << 8) + (lo_status & 0xff);
522                 lo_cnt = *cur_frame_end++;
523                 hi_cnt = *cur_frame_end++;
524                 count = ((hi_cnt & 0xff) << 8) + (lo_cnt & 0xff);
525
526                 if (znet_debug > 5)
527                   printk(KERN_DEBUG "Constructing trailer at location %03x, %04x %04x %04x %04x"
528                                  " count %#x status %04x.\n",
529                                  cur_frame_end_offset<<1, lo_status, hi_status, lo_cnt, hi_cnt,
530                                  count, status);
531                 cur_frame_end[-4] = status;
532                 cur_frame_end[-3] = next_frame_end_offset;
533                 cur_frame_end[-2] = count;
534                 next_frame_end_offset = cur_frame_end_offset;
535                 cur_frame_end_offset -= ((count + 1)>>1) + 3;
536                 if (cur_frame_end_offset < 0)
537                   cur_frame_end_offset += RX_BUF_SIZE/2;
538         };
539
540         /* Now step  forward through the list. */
541         do {
542                 ushort *this_rfp_ptr = zn.rx_start + next_frame_end_offset;
543                 int status = this_rfp_ptr[-4];
544                 int pkt_len = this_rfp_ptr[-2];
545           
546                 if (znet_debug > 5)
547                   printk(KERN_DEBUG "Looking at trailer ending at %04x status %04x length %03x"
548                                  " next %04x.\n", next_frame_end_offset<<1, status, pkt_len,
549                                  this_rfp_ptr[-3]<<1);
550                 /* Once again we must assume that the i82586 docs apply. */
551                 if ( ! (status & 0x2000)) {                             /* There was an error. */
552                         lp->stats.rx_errors++;
553                         if (status & 0x0800) lp->stats.rx_crc_errors++;
554                         if (status & 0x0400) lp->stats.rx_frame_errors++;
555                         if (status & 0x0200) lp->stats.rx_over_errors++; /* Wrong. */
556                         if (status & 0x0100) lp->stats.rx_fifo_errors++;
557                         if (status & 0x0080) lp->stats.rx_length_errors++;
558                 } else if (pkt_len > 1536) {
559                         lp->stats.rx_length_errors++;
560                 } else {
561                         /* Malloc up new buffer. */
562                         struct sk_buff *skb;
563
564                         skb = dev_alloc_skb(pkt_len);
565                         if (skb == NULL) {
566                                 if (znet_debug)
567                                   printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
568                                 lp->stats.rx_dropped++;
569                                 break;
570                         }
571                         skb->dev = dev;
572
573                         if (&zn.rx_cur[(pkt_len+1)>>1] > zn.rx_end) {
574                                 int semi_cnt = (zn.rx_end - zn.rx_cur)<<1;
575                                 memcpy(skb_put(skb,semi_cnt), zn.rx_cur, semi_cnt);
576                                 memcpy(skb_put(skb,pkt_len-semi_cnt), zn.rx_start,
577                                            pkt_len - semi_cnt);
578                         } else {
579                                 memcpy(skb_put(skb,pkt_len), zn.rx_cur, pkt_len);
580                                 if (znet_debug > 6) {
581                                         unsigned int *packet = (unsigned int *) skb->data;
582                                         printk(KERN_DEBUG "Packet data is %08x %08x %08x %08x.\n", packet[0],
583                                                    packet[1], packet[2], packet[3]);
584                                 }
585                   }
586                   skb->protocol=eth_type_trans(skb,dev);
587                   netif_rx(skb);
588                   dev->last_rx = jiffies;
589                   lp->stats.rx_packets++;
590                   lp->stats.rx_bytes += pkt_len;
591                 }
592                 zn.rx_cur = this_rfp_ptr;
593                 if (zn.rx_cur >= zn.rx_end)
594                         zn.rx_cur -= RX_BUF_SIZE/2;
595                 update_stop_hit(ioaddr, (zn.rx_cur - zn.rx_start)<<1);
596                 next_frame_end_offset = this_rfp_ptr[-3];
597                 if (next_frame_end_offset == 0)         /* Read all the frames? */
598                         break;                  /* Done for now */
599                 this_rfp_ptr = zn.rx_start + next_frame_end_offset;
600         } while (--boguscount);
601
602         /* If any worth-while packets have been received, dev_rint()
603            has done a mark_bh(INET_BH) for us and will work on them
604            when we get to the bottom-half routine. */
605         return;
606 }
607
608 /* The inverse routine to znet_open(). */
609 static int znet_close(struct net_device *dev)
610 {
611         unsigned long flags;
612         int ioaddr = dev->base_addr;
613
614         netif_stop_queue (dev);
615
616         outb(CMD0_RESET, ioaddr);                       /* CMD0_RESET */
617
618         flags=claim_dma_lock();
619         disable_dma(zn.rx_dma);
620         disable_dma(zn.tx_dma);
621         release_dma_lock(flags);
622
623         free_irq(dev->irq, dev);
624
625         if (znet_debug > 1)
626                 printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
627         /* Turn off transceiver power. */
628         outb(0x10, 0xe6);                                       /* Select LAN control register */
629         outb(inb(0xe7) & ~0x84, 0xe7);          /* Turn on LAN power (bit 2). */
630
631         return 0;
632 }
633
634 /* Get the current statistics.  This may be called with the card open or
635    closed. */
636 static struct net_device_stats *net_get_stats(struct net_device *dev)
637 {
638                 struct net_local *lp = (struct net_local *)dev->priv;
639
640                 return &lp->stats;
641 }
642
643 /* Set or clear the multicast filter for this adaptor.
644    As a side effect this routine must also initialize the device parameters.
645    This is taken advantage of in open().
646
647    N.B. that we change i593_init[] in place.  This (properly) makes the
648    mode change persistent, but must be changed if this code is moved to
649    a multiple adaptor environment.
650  */
651 static void set_multicast_list(struct net_device *dev)
652 {
653         short ioaddr = dev->base_addr;
654
655         if (dev->flags&IFF_PROMISC) {
656                 /* Enable promiscuous mode */
657                 i593_init[7] &= ~3;             i593_init[7] |= 1;
658                 i593_init[13] &= ~8;    i593_init[13] |= 8;
659         } else if (dev->mc_list || (dev->flags&IFF_ALLMULTI)) {
660                 /* Enable accept-all-multicast mode */
661                 i593_init[7] &= ~3;             i593_init[7] |= 0;
662                 i593_init[13] &= ~8;    i593_init[13] |= 8;
663         } else {                                        /* Enable normal mode. */
664                 i593_init[7] &= ~3;             i593_init[7] |= 0;
665                 i593_init[13] &= ~8;    i593_init[13] |= 0;
666         }
667         *zn.tx_cur++ = sizeof(i593_init);
668         memcpy(zn.tx_cur, i593_init, sizeof(i593_init));
669         zn.tx_cur += sizeof(i593_init)/2;
670         outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);
671 #ifdef not_tested
672         if (num_addrs > 0) {
673                 int addrs_len = 6*num_addrs;
674                 *zn.tx_cur++ = addrs_len;
675                 memcpy(zn.tx_cur, addrs, addrs_len);
676                 outb(CMD0_MULTICAST_LIST+CMD0_CHNL_1, ioaddr);
677                 zn.tx_cur += addrs_len>>1;
678         }
679 #endif
680 }
681
682 void show_dma(void)
683 {
684         unsigned long flags;
685         short dma_port = ((zn.tx_dma&3)<<2) + IO_DMA2_BASE;
686         unsigned addr = inb(dma_port);
687         addr |= inb(dma_port) << 8;
688
689         flags=claim_dma_lock();
690         printk("Addr: %04x cnt:%3x...", addr<<1, get_dma_residue(zn.tx_dma));
691         release_dma_lock(flags);
692 }
693
694 /* Initialize the hardware.  We have to do this when the board is open()ed
695    or when we come out of suspend mode. */
696 static void hardware_init(struct net_device *dev)
697 {
698         unsigned long flags;
699         short ioaddr = dev->base_addr;
700
701         zn.rx_cur = zn.rx_start;
702         zn.tx_cur = zn.tx_start;
703
704         /* Reset the chip, and start it up. */
705         outb(CMD0_RESET, ioaddr);
706
707         flags=claim_dma_lock();
708         disable_dma(zn.rx_dma);                 /* reset by an interrupting task. */
709         clear_dma_ff(zn.rx_dma);
710         set_dma_mode(zn.rx_dma, DMA_RX_MODE);
711         set_dma_addr(zn.rx_dma, (unsigned int) zn.rx_start);
712         set_dma_count(zn.rx_dma, RX_BUF_SIZE);
713         enable_dma(zn.rx_dma);
714         /* Now set up the Tx channel. */
715         disable_dma(zn.tx_dma);
716         clear_dma_ff(zn.tx_dma);
717         set_dma_mode(zn.tx_dma, DMA_TX_MODE);
718         set_dma_addr(zn.tx_dma, (unsigned int) zn.tx_start);
719         set_dma_count(zn.tx_dma, zn.tx_buf_len<<1);
720         enable_dma(zn.tx_dma);
721         release_dma_lock(flags);
722         
723         if (znet_debug > 1)
724           printk(KERN_DEBUG "%s: Initializing the i82593, tx buf %p... ", dev->name,
725                          zn.tx_start);
726         /* Do an empty configure command, just like the Crynwr driver.  This
727            resets to chip to its default values. */
728         *zn.tx_cur++ = 0;
729         *zn.tx_cur++ = 0;
730         printk("stat:%02x ", inb(ioaddr)); show_dma();
731         outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);
732         *zn.tx_cur++ = sizeof(i593_init);
733         memcpy(zn.tx_cur, i593_init, sizeof(i593_init));
734         zn.tx_cur += sizeof(i593_init)/2;
735         printk("stat:%02x ", inb(ioaddr)); show_dma();
736         outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);
737         *zn.tx_cur++ = 6;
738         memcpy(zn.tx_cur, dev->dev_addr, 6);
739         zn.tx_cur += 3;
740         printk("stat:%02x ", inb(ioaddr)); show_dma();
741         outb(CMD0_IA_SETUP + CMD0_CHNL_1, ioaddr);
742         printk("stat:%02x ", inb(ioaddr)); show_dma();
743
744         update_stop_hit(ioaddr, 8192);
745         if (znet_debug > 1)  printk("enabling Rx.\n");
746         outb(CMD0_Rx_ENABLE+CMD0_CHNL_0, ioaddr);
747         netif_start_queue (dev);
748 }
749
750 static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset)
751 {
752         outb(CMD0_PORT_1, ioaddr);
753         if (znet_debug > 5)
754           printk(KERN_DEBUG "Updating stop hit with value %02x.\n",
755                          (rx_stop_offset >> 6) | 0x80);
756         outb((rx_stop_offset >> 6) | 0x80, ioaddr);
757         outb(CMD1_PORT_0, ioaddr);
758 }
759 \f
760 /*
761  * Local variables:
762  *  compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c znet.c"
763  *  version-control: t
764  *  kept-new-versions: 5
765  *  c-indent-level: 4
766  *  tab-width: 4
767  * End:
768  */