* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / net / znet.c
blobf48e41e7ed683dd8479112e5d3cb4dde8e2dfe65
1 /* znet.c: An Zenith Z-Note ethernet driver for linux. */
3 static const char *version = "znet.c:v1.02 9/23/94 becker@cesdis.gsfc.nasa.gov\n";
5 /*
6 Written by Donald Becker.
8 The author may be reached as becker@cesdis.gsfc.nasa.gov.
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.
13 Thanks to Mike Hollick for alpha testing and suggestions.
15 References:
16 The Crynwr packet driver.
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.
25 Theory of Operation
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.
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.)
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.)
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.
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.
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>
78 #include <linux/netdevice.h>
79 #include <linux/etherdevice.h>
80 #include <linux/skbuff.h>
81 #include <linux/if_arp.h>
83 #ifndef ZNET_DEBUG
84 #define ZNET_DEBUG 1
85 #endif
86 static unsigned int znet_debug = ZNET_DEBUG;
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
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
115 #define CMD0_ACK 0x80
117 #define CMD0_STAT0 (0 << 5)
118 #define CMD0_STAT1 (1 << 5)
119 #define CMD0_STAT2 (2 << 5)
120 #define CMD0_STAT3 (3 << 5)
122 #define net_local znet_private
123 struct znet_private {
124 int rx_dma, tx_dma;
125 struct net_device_stats stats;
126 /* The starting, current, and end pointers for the packet buffers. */
127 ushort *rx_start, *rx_cur, *rx_end;
128 ushort *tx_start, *tx_cur, *tx_end;
129 ushort tx_buf_len; /* Tx buffer length, in words. */
132 /* Only one can be built-in;-> */
133 static struct znet_private zn;
134 static ushort dma_buffer1[DMA_BUF_SIZE/2];
135 static ushort dma_buffer2[DMA_BUF_SIZE/2];
136 static ushort dma_buffer3[DMA_BUF_SIZE/2 + 8];
138 /* The configuration block. What an undocumented nightmare. The first
139 set of values are those suggested (without explanation) for ethernet
140 in the Intel 82586 databook. The rest appear to be completely undocumented,
141 except for cryptic notes in the Crynwr packet driver. This driver uses
142 the Crynwr values verbatim. */
144 static unsigned char i593_init[] = {
145 0xAA, /* 0: 16-byte input & 80-byte output FIFO. */
146 /* threshold, 96-byte FIFO, 82593 mode. */
147 0x88, /* 1: Continuous w/interrupts, 128-clock DMA.*/
148 0x2E, /* 2: 8-byte preamble, NO address insertion, */
149 /* 6-byte Ethernet address, loopback off.*/
150 0x00, /* 3: Default priorities & backoff methods. */
151 0x60, /* 4: 96-bit interframe spacing. */
152 0x00, /* 5: 512-bit slot time (low-order). */
153 0xF2, /* 6: Slot time (high-order), 15 COLL retries. */
154 0x00, /* 7: Promisc-off, broadcast-on, default CRC. */
155 0x00, /* 8: Default carrier-sense, collision-detect. */
156 0x40, /* 9: 64-byte minimum frame length. */
157 0x5F, /* A: Type/length checks OFF, no CRC input,
158 "jabber" termination, etc. */
159 0x00, /* B: Full-duplex disabled. */
160 0x3F, /* C: Default multicast addresses & backoff. */
161 0x07, /* D: Default IFS retriggering. */
162 0x31, /* E: Internal retransmit, drop "runt" packets,
163 synchr. DRQ deassertion, 6 status bytes. */
164 0x22, /* F: Receive ring-buffer size (8K),
165 receive-stop register enable. */
168 struct netidblk {
169 char magic[8]; /* The magic number (string) "NETIDBLK" */
170 unsigned char netid[8]; /* The physical station address */
171 char nettype, globalopt;
172 char vendor[8]; /* The machine vendor and product name. */
173 char product[8];
174 char irq1, irq2; /* Interrupts, only one is currently used. */
175 char dma1, dma2;
176 short dma_mem_misc[8]; /* DMA buffer locations (unused in Linux). */
177 short iobase1, iosize1;
178 short iobase2, iosize2; /* Second iobase unused. */
179 char driver_options; /* Misc. bits */
180 char pad;
183 int znet_probe(struct net_device *dev);
184 static int znet_open(struct net_device *dev);
185 static int znet_send_packet(struct sk_buff *skb, struct net_device *dev);
186 static void znet_interrupt(int irq, void *dev_id, struct pt_regs *regs);
187 static void znet_rx(struct net_device *dev);
188 static int znet_close(struct net_device *dev);
189 static struct net_device_stats *net_get_stats(struct net_device *dev);
190 static void set_multicast_list(struct net_device *dev);
191 static void hardware_init(struct net_device *dev);
192 static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset);
194 #ifdef notdef
195 static struct sigaction znet_sigaction = { &znet_interrupt, 0, 0, NULL, };
196 #endif
199 /* The Z-Note probe is pretty easy. The NETIDBLK exists in the safe-to-probe
200 BIOS area. We just scan for the signature, and pull the vital parameters
201 out of the structure. */
203 int __init znet_probe(struct net_device *dev)
205 int i;
206 struct netidblk *netinfo;
207 char *p;
209 /* This code scans the region 0xf0000 to 0xfffff for a "NETIDBLK". */
210 for(p = (char *)phys_to_virt(0xf0000); p < (char *)phys_to_virt(0x100000); p++)
211 if (*p == 'N' && strncmp(p, "NETIDBLK", 8) == 0)
212 break;
214 if (p >= (char *)phys_to_virt(0x100000)) {
215 if (znet_debug > 1)
216 printk(KERN_INFO "No Z-Note ethernet adaptor found.\n");
217 return ENODEV;
219 netinfo = (struct netidblk *)p;
220 dev->base_addr = netinfo->iobase1;
221 dev->irq = netinfo->irq1;
223 printk(KERN_INFO "%s: ZNET at %#3lx,", dev->name, dev->base_addr);
225 /* The station address is in the "netidblk" at 0x0f0000. */
226 for (i = 0; i < 6; i++)
227 printk(" %2.2x", dev->dev_addr[i] = netinfo->netid[i]);
229 printk(", using IRQ %d DMA %d and %d.\n", dev->irq, netinfo->dma1,
230 netinfo->dma2);
232 if (znet_debug > 1) {
233 printk(KERN_INFO "%s: vendor '%16.16s' IRQ1 %d IRQ2 %d DMA1 %d DMA2 %d.\n",
234 dev->name, netinfo->vendor,
235 netinfo->irq1, netinfo->irq2,
236 netinfo->dma1, netinfo->dma2);
237 printk(KERN_INFO "%s: iobase1 %#x size %d iobase2 %#x size %d net type %2.2x.\n",
238 dev->name, netinfo->iobase1, netinfo->iosize1,
239 netinfo->iobase2, netinfo->iosize2, netinfo->nettype);
242 if (znet_debug > 0)
243 printk("%s%s", KERN_INFO, version);
245 dev->priv = (void *) &zn;
246 zn.rx_dma = netinfo->dma1;
247 zn.tx_dma = netinfo->dma2;
249 /* These should never fail. You can't add devices to a sealed box! */
250 if (request_irq(dev->irq, &znet_interrupt, 0, "ZNet", dev)
251 || request_dma(zn.rx_dma,"ZNet rx")
252 || request_dma(zn.tx_dma,"ZNet tx")) {
253 printk(KERN_WARNING "%s: Not opened -- resource busy?!?\n", dev->name);
254 return EBUSY;
257 /* Allocate buffer memory. We can cross a 128K boundary, so we
258 must be careful about the allocation. It's easiest to waste 8K. */
259 if (dma_page_eq(dma_buffer1, &dma_buffer1[RX_BUF_SIZE/2-1]))
260 zn.rx_start = dma_buffer1;
261 else
262 zn.rx_start = dma_buffer2;
264 if (dma_page_eq(dma_buffer3, &dma_buffer3[RX_BUF_SIZE/2-1]))
265 zn.tx_start = dma_buffer3;
266 else
267 zn.tx_start = dma_buffer2;
268 zn.rx_end = zn.rx_start + RX_BUF_SIZE/2;
269 zn.tx_buf_len = TX_BUF_SIZE/2;
270 zn.tx_end = zn.tx_start + zn.tx_buf_len;
272 /* The ZNET-specific entries in the device structure. */
273 dev->open = &znet_open;
274 dev->hard_start_xmit = &znet_send_packet;
275 dev->stop = &znet_close;
276 dev->get_stats = net_get_stats;
277 dev->set_multicast_list = &set_multicast_list;
279 /* Fill in the 'dev' with ethernet-generic values. */
280 ether_setup(dev);
282 return 0;
286 static int znet_open(struct net_device *dev)
288 int ioaddr = dev->base_addr;
290 if (znet_debug > 2)
291 printk(KERN_DEBUG "%s: znet_open() called.\n", dev->name);
293 /* Turn on the 82501 SIA, using zenith-specific magic. */
294 outb(0x10, 0xe6); /* Select LAN control register */
295 outb(inb(0xe7) | 0x84, 0xe7); /* Turn on LAN power (bit 2). */
296 /* According to the Crynwr driver we should wait 50 msec. for the
297 LAN clock to stabilize. My experiments indicates that the '593 can
298 be initialized immediately. The delay is probably needed for the
299 DC-to-DC converter to come up to full voltage, and for the oscillator
300 to be spot-on at 20Mhz before transmitting.
301 Until this proves to be a problem we rely on the higher layers for the
302 delay and save allocating a timer entry. */
304 /* This follows the packet driver's lead, and checks for success. */
305 if (inb(ioaddr) != 0x10 && inb(ioaddr) != 0x00)
306 printk(KERN_WARNING "%s: Problem turning on the transceiver power.\n",
307 dev->name);
309 dev->tbusy = 0;
310 dev->interrupt = 0;
311 hardware_init(dev);
312 dev->start = 1;
314 return 0;
317 static int znet_send_packet(struct sk_buff *skb, struct net_device *dev)
319 int ioaddr = dev->base_addr;
320 struct net_local *lp = (struct net_local *)dev->priv;
322 if (znet_debug > 4)
323 printk(KERN_DEBUG "%s: ZNet_send_packet(%ld).\n", dev->name, dev->tbusy);
325 /* Transmitter timeout, likely just recovery after suspending the machine. */
326 if (dev->tbusy) {
327 ushort event, tx_status, rx_offset, state;
328 int tickssofar = jiffies - dev->trans_start;
329 if (tickssofar < 10)
330 return 1;
331 outb(CMD0_STAT0, ioaddr); event = inb(ioaddr);
332 outb(CMD0_STAT1, ioaddr); tx_status = inw(ioaddr);
333 outb(CMD0_STAT2, ioaddr); rx_offset = inw(ioaddr);
334 outb(CMD0_STAT3, ioaddr); state = inb(ioaddr);
335 printk(KERN_WARNING "%s: transmit timed out, status %02x %04x %04x %02x,"
336 " resetting.\n", dev->name, event, tx_status, rx_offset, state);
337 if (tx_status == 0x0400)
338 printk(KERN_WARNING "%s: Tx carrier error, check transceiver cable.\n",
339 dev->name);
340 outb(CMD0_RESET, ioaddr);
341 hardware_init(dev);
344 /* Check that the part hasn't reset itself, probably from suspend. */
345 outb(CMD0_STAT0, ioaddr);
346 if (inw(ioaddr) == 0x0010
347 && inw(ioaddr) == 0x0000
348 && inw(ioaddr) == 0x0010)
349 hardware_init(dev);
351 /* Block a timer-based transmit from overlapping. This could better be
352 done with atomic_swap(1, dev->tbusy), but set_bit() works as well. */
353 if (test_and_set_bit(0, (void*)&dev->tbusy) != 0)
354 printk(KERN_WARNING "%s: Transmitter access conflict.\n", dev->name);
355 else {
356 short length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
357 unsigned char *buf = (void *)skb->data;
358 ushort *tx_link = zn.tx_cur - 1;
359 ushort rnd_len = (length + 1)>>1;
361 lp->stats.tx_bytes+=length;
364 short dma_port = ((zn.tx_dma&3)<<2) + IO_DMA2_BASE;
365 unsigned addr = inb(dma_port);
366 addr |= inb(dma_port) << 8;
367 addr <<= 1;
368 if (((int)zn.tx_cur & 0x1ffff) != addr)
369 printk(KERN_WARNING "Address mismatch at Tx: %#x vs %#x.\n",
370 (int)zn.tx_cur & 0xffff, addr);
371 zn.tx_cur = (ushort *)(((int)zn.tx_cur & 0xfe0000) | addr);
374 if (zn.tx_cur >= zn.tx_end)
375 zn.tx_cur = zn.tx_start;
376 *zn.tx_cur++ = length;
377 if (zn.tx_cur + rnd_len + 1 > zn.tx_end) {
378 int semi_cnt = (zn.tx_end - zn.tx_cur)<<1; /* Cvrt to byte cnt. */
379 memcpy(zn.tx_cur, buf, semi_cnt);
380 rnd_len -= semi_cnt>>1;
381 memcpy(zn.tx_start, buf + semi_cnt, length - semi_cnt);
382 zn.tx_cur = zn.tx_start + rnd_len;
383 } else {
384 memcpy(zn.tx_cur, buf, skb->len);
385 zn.tx_cur += rnd_len;
387 *zn.tx_cur++ = 0;
388 cli(); {
389 *tx_link = CMD0_TRANSMIT + CMD0_CHNL_1;
390 /* Is this always safe to do? */
391 outb(CMD0_TRANSMIT + CMD0_CHNL_1,ioaddr);
392 } sti();
394 dev->trans_start = jiffies;
395 if (znet_debug > 4)
396 printk(KERN_DEBUG "%s: Transmitter queued, length %d.\n", dev->name, length);
398 dev_kfree_skb(skb);
399 return 0;
402 /* The ZNET interrupt handler. */
403 static void znet_interrupt(int irq, void *dev_id, struct pt_regs * regs)
405 struct net_device *dev = dev_id;
406 int ioaddr;
407 int boguscnt = 20;
409 if (dev == NULL) {
410 printk(KERN_WARNING "znet_interrupt(): IRQ %d for unknown device.\n", irq);
411 return;
414 dev->interrupt = 1;
415 ioaddr = dev->base_addr;
417 outb(CMD0_STAT0, ioaddr);
418 do {
419 ushort status = inb(ioaddr);
420 if (znet_debug > 5) {
421 ushort result, rx_ptr, running;
422 outb(CMD0_STAT1, ioaddr);
423 result = inw(ioaddr);
424 outb(CMD0_STAT2, ioaddr);
425 rx_ptr = inw(ioaddr);
426 outb(CMD0_STAT3, ioaddr);
427 running = inb(ioaddr);
428 printk(KERN_DEBUG "%s: interrupt, status %02x, %04x %04x %02x serial %d.\n",
429 dev->name, status, result, rx_ptr, running, boguscnt);
431 if ((status & 0x80) == 0)
432 break;
434 if ((status & 0x0F) == 4) { /* Transmit done. */
435 struct net_local *lp = (struct net_local *)dev->priv;
436 int tx_status;
437 outb(CMD0_STAT1, ioaddr);
438 tx_status = inw(ioaddr);
439 /* It's undocumented, but tx_status seems to match the i82586. */
440 if (tx_status & 0x2000) {
441 lp->stats.tx_packets++;
442 lp->stats.collisions += tx_status & 0xf;
443 } else {
444 if (tx_status & 0x0600) lp->stats.tx_carrier_errors++;
445 if (tx_status & 0x0100) lp->stats.tx_fifo_errors++;
446 if (!(tx_status & 0x0040)) lp->stats.tx_heartbeat_errors++;
447 if (tx_status & 0x0020) lp->stats.tx_aborted_errors++;
448 /* ...and the catch-all. */
449 if ((tx_status | 0x0760) != 0x0760)
450 lp->stats.tx_errors++;
452 dev->tbusy = 0;
453 mark_bh(NET_BH); /* Inform upper layers. */
456 if ((status & 0x40)
457 || (status & 0x0f) == 11) {
458 znet_rx(dev);
460 /* Clear the interrupts we've handled. */
461 outb(CMD0_ACK,ioaddr);
462 } while (boguscnt--);
464 dev->interrupt = 0;
465 return;
468 static void znet_rx(struct net_device *dev)
470 struct net_local *lp = (struct net_local *)dev->priv;
471 int ioaddr = dev->base_addr;
472 int boguscount = 1;
473 short next_frame_end_offset = 0; /* Offset of next frame start. */
474 short *cur_frame_end;
475 short cur_frame_end_offset;
477 outb(CMD0_STAT2, ioaddr);
478 cur_frame_end_offset = inw(ioaddr);
480 if (cur_frame_end_offset == zn.rx_cur - zn.rx_start) {
481 printk(KERN_WARNING "%s: Interrupted, but nothing to receive, offset %03x.\n",
482 dev->name, cur_frame_end_offset);
483 return;
486 /* Use same method as the Crynwr driver: construct a forward list in
487 the same area of the backwards links we now have. This allows us to
488 pass packets to the upper layers in the order they were received --
489 important for fast-path sequential operations. */
490 while (zn.rx_start + cur_frame_end_offset != zn.rx_cur
491 && ++boguscount < 5) {
492 unsigned short hi_cnt, lo_cnt, hi_status, lo_status;
493 int count, status;
495 if (cur_frame_end_offset < 4) {
496 /* Oh no, we have a special case: the frame trailer wraps around
497 the end of the ring buffer. We've saved space at the end of
498 the ring buffer for just this problem. */
499 memcpy(zn.rx_end, zn.rx_start, 8);
500 cur_frame_end_offset += (RX_BUF_SIZE/2);
502 cur_frame_end = zn.rx_start + cur_frame_end_offset - 4;
504 lo_status = *cur_frame_end++;
505 hi_status = *cur_frame_end++;
506 status = ((hi_status & 0xff) << 8) + (lo_status & 0xff);
507 lo_cnt = *cur_frame_end++;
508 hi_cnt = *cur_frame_end++;
509 count = ((hi_cnt & 0xff) << 8) + (lo_cnt & 0xff);
511 if (znet_debug > 5)
512 printk(KERN_DEBUG "Constructing trailer at location %03x, %04x %04x %04x %04x"
513 " count %#x status %04x.\n",
514 cur_frame_end_offset<<1, lo_status, hi_status, lo_cnt, hi_cnt,
515 count, status);
516 cur_frame_end[-4] = status;
517 cur_frame_end[-3] = next_frame_end_offset;
518 cur_frame_end[-2] = count;
519 next_frame_end_offset = cur_frame_end_offset;
520 cur_frame_end_offset -= ((count + 1)>>1) + 3;
521 if (cur_frame_end_offset < 0)
522 cur_frame_end_offset += RX_BUF_SIZE/2;
525 /* Now step forward through the list. */
526 do {
527 ushort *this_rfp_ptr = zn.rx_start + next_frame_end_offset;
528 int status = this_rfp_ptr[-4];
529 int pkt_len = this_rfp_ptr[-2];
531 if (znet_debug > 5)
532 printk(KERN_DEBUG "Looking at trailer ending at %04x status %04x length %03x"
533 " next %04x.\n", next_frame_end_offset<<1, status, pkt_len,
534 this_rfp_ptr[-3]<<1);
535 /* Once again we must assume that the i82586 docs apply. */
536 if ( ! (status & 0x2000)) { /* There was an error. */
537 lp->stats.rx_errors++;
538 if (status & 0x0800) lp->stats.rx_crc_errors++;
539 if (status & 0x0400) lp->stats.rx_frame_errors++;
540 if (status & 0x0200) lp->stats.rx_over_errors++; /* Wrong. */
541 if (status & 0x0100) lp->stats.rx_fifo_errors++;
542 if (status & 0x0080) lp->stats.rx_length_errors++;
543 } else if (pkt_len > 1536) {
544 lp->stats.rx_length_errors++;
545 } else {
546 /* Malloc up new buffer. */
547 struct sk_buff *skb;
549 skb = dev_alloc_skb(pkt_len);
550 if (skb == NULL) {
551 if (znet_debug)
552 printk(KERN_WARNING "%s: Memory squeeze, dropping packet.\n", dev->name);
553 lp->stats.rx_dropped++;
554 break;
556 skb->dev = dev;
558 if (&zn.rx_cur[(pkt_len+1)>>1] > zn.rx_end) {
559 int semi_cnt = (zn.rx_end - zn.rx_cur)<<1;
560 memcpy(skb_put(skb,semi_cnt), zn.rx_cur, semi_cnt);
561 memcpy(skb_put(skb,pkt_len-semi_cnt), zn.rx_start,
562 pkt_len - semi_cnt);
563 } else {
564 memcpy(skb_put(skb,pkt_len), zn.rx_cur, pkt_len);
565 if (znet_debug > 6) {
566 unsigned int *packet = (unsigned int *) skb->data;
567 printk(KERN_DEBUG "Packet data is %08x %08x %08x %08x.\n", packet[0],
568 packet[1], packet[2], packet[3]);
571 skb->protocol=eth_type_trans(skb,dev);
572 netif_rx(skb);
573 lp->stats.rx_packets++;
575 zn.rx_cur = this_rfp_ptr;
576 if (zn.rx_cur >= zn.rx_end)
577 zn.rx_cur -= RX_BUF_SIZE/2;
578 update_stop_hit(ioaddr, (zn.rx_cur - zn.rx_start)<<1);
579 next_frame_end_offset = this_rfp_ptr[-3];
580 if (next_frame_end_offset == 0) /* Read all the frames? */
581 break; /* Done for now */
582 this_rfp_ptr = zn.rx_start + next_frame_end_offset;
583 } while (--boguscount);
585 /* If any worth-while packets have been received, dev_rint()
586 has done a mark_bh(INET_BH) for us and will work on them
587 when we get to the bottom-half routine. */
588 return;
591 /* The inverse routine to znet_open(). */
592 static int znet_close(struct net_device *dev)
594 unsigned long flags;
595 int ioaddr = dev->base_addr;
597 dev->tbusy = 1;
598 dev->start = 0;
600 outb(CMD0_RESET, ioaddr); /* CMD0_RESET */
602 flags=claim_dma_lock();
603 disable_dma(zn.rx_dma);
604 disable_dma(zn.tx_dma);
605 release_dma_lock(flags);
607 free_irq(dev->irq, dev);
609 if (znet_debug > 1)
610 printk(KERN_DEBUG "%s: Shutting down ethercard.\n", dev->name);
611 /* Turn off transceiver power. */
612 outb(0x10, 0xe6); /* Select LAN control register */
613 outb(inb(0xe7) & ~0x84, 0xe7); /* Turn on LAN power (bit 2). */
615 return 0;
618 /* Get the current statistics. This may be called with the card open or
619 closed. */
620 static struct net_device_stats *net_get_stats(struct net_device *dev)
622 struct net_local *lp = (struct net_local *)dev->priv;
624 return &lp->stats;
627 /* Set or clear the multicast filter for this adaptor.
628 As a side effect this routine must also initialize the device parameters.
629 This is taken advantage of in open().
631 N.B. that we change i593_init[] in place. This (properly) makes the
632 mode change persistent, but must be changed if this code is moved to
633 a multiple adaptor environment.
635 static void set_multicast_list(struct net_device *dev)
637 short ioaddr = dev->base_addr;
639 if (dev->flags&IFF_PROMISC) {
640 /* Enable promiscuous mode */
641 i593_init[7] &= ~3; i593_init[7] |= 1;
642 i593_init[13] &= ~8; i593_init[13] |= 8;
643 } else if (dev->mc_list || (dev->flags&IFF_ALLMULTI)) {
644 /* Enable accept-all-multicast mode */
645 i593_init[7] &= ~3; i593_init[7] |= 0;
646 i593_init[13] &= ~8; i593_init[13] |= 8;
647 } else { /* Enable normal mode. */
648 i593_init[7] &= ~3; i593_init[7] |= 0;
649 i593_init[13] &= ~8; i593_init[13] |= 0;
651 *zn.tx_cur++ = sizeof(i593_init);
652 memcpy(zn.tx_cur, i593_init, sizeof(i593_init));
653 zn.tx_cur += sizeof(i593_init)/2;
654 outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);
655 #ifdef not_tested
656 if (num_addrs > 0) {
657 int addrs_len = 6*num_addrs;
658 *zn.tx_cur++ = addrs_len;
659 memcpy(zn.tx_cur, addrs, addrs_len);
660 outb(CMD0_MULTICAST_LIST+CMD0_CHNL_1, ioaddr);
661 zn.tx_cur += addrs_len>>1;
663 #endif
666 void show_dma(void)
668 unsigned long flags;
669 short dma_port = ((zn.tx_dma&3)<<2) + IO_DMA2_BASE;
670 unsigned addr = inb(dma_port);
671 addr |= inb(dma_port) << 8;
673 flags=claim_dma_lock();
674 printk("Addr: %04x cnt:%3x...", addr<<1, get_dma_residue(zn.tx_dma));
675 release_dma_lock(flags);
678 /* Initialize the hardware. We have to do this when the board is open()ed
679 or when we come out of suspend mode. */
680 static void hardware_init(struct net_device *dev)
682 unsigned long flags;
683 short ioaddr = dev->base_addr;
685 zn.rx_cur = zn.rx_start;
686 zn.tx_cur = zn.tx_start;
688 /* Reset the chip, and start it up. */
689 outb(CMD0_RESET, ioaddr);
691 flags=claim_dma_lock();
692 disable_dma(zn.rx_dma); /* reset by an interrupting task. */
693 clear_dma_ff(zn.rx_dma);
694 set_dma_mode(zn.rx_dma, DMA_RX_MODE);
695 set_dma_addr(zn.rx_dma, (unsigned int) zn.rx_start);
696 set_dma_count(zn.rx_dma, RX_BUF_SIZE);
697 enable_dma(zn.rx_dma);
698 /* Now set up the Tx channel. */
699 disable_dma(zn.tx_dma);
700 clear_dma_ff(zn.tx_dma);
701 set_dma_mode(zn.tx_dma, DMA_TX_MODE);
702 set_dma_addr(zn.tx_dma, (unsigned int) zn.tx_start);
703 set_dma_count(zn.tx_dma, zn.tx_buf_len<<1);
704 enable_dma(zn.tx_dma);
705 release_dma_lock(flags);
707 if (znet_debug > 1)
708 printk(KERN_DEBUG "%s: Initializing the i82593, tx buf %p... ", dev->name,
709 zn.tx_start);
710 /* Do an empty configure command, just like the Crynwr driver. This
711 resets to chip to its default values. */
712 *zn.tx_cur++ = 0;
713 *zn.tx_cur++ = 0;
714 printk("stat:%02x ", inb(ioaddr)); show_dma();
715 outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);
716 *zn.tx_cur++ = sizeof(i593_init);
717 memcpy(zn.tx_cur, i593_init, sizeof(i593_init));
718 zn.tx_cur += sizeof(i593_init)/2;
719 printk("stat:%02x ", inb(ioaddr)); show_dma();
720 outb(CMD0_CONFIGURE+CMD0_CHNL_1, ioaddr);
721 *zn.tx_cur++ = 6;
722 memcpy(zn.tx_cur, dev->dev_addr, 6);
723 zn.tx_cur += 3;
724 printk("stat:%02x ", inb(ioaddr)); show_dma();
725 outb(CMD0_IA_SETUP + CMD0_CHNL_1, ioaddr);
726 printk("stat:%02x ", inb(ioaddr)); show_dma();
728 update_stop_hit(ioaddr, 8192);
729 if (znet_debug > 1) printk("enabling Rx.\n");
730 outb(CMD0_Rx_ENABLE+CMD0_CHNL_0, ioaddr);
731 dev->tbusy = 0;
734 static void update_stop_hit(short ioaddr, unsigned short rx_stop_offset)
736 outb(CMD0_PORT_1, ioaddr);
737 if (znet_debug > 5)
738 printk(KERN_DEBUG "Updating stop hit with value %02x.\n",
739 (rx_stop_offset >> 6) | 0x80);
740 outb((rx_stop_offset >> 6) | 0x80, ioaddr);
741 outb(CMD1_PORT_0, ioaddr);
745 * Local variables:
746 * compile-command: "gcc -D__KERNEL__ -I/usr/src/linux/net/inet -Wall -Wstrict-prototypes -O6 -m486 -c znet.c"
747 * version-control: t
748 * kept-new-versions: 5
749 * c-indent-level: 4
750 * tab-width: 4
751 * End: